GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / powerpc / platforms / pseries / lpar.c
1 /*
2  * pSeries_lpar.c
3  * Copyright (C) 2001 Todd Inglett, IBM Corporation
4  *
5  * pSeries LPAR support.
6  * 
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  * 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22 /* Enables debugging of low-level hash table routines - careful! */
23 #undef DEBUG
24
25 #include <linux/kernel.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/console.h>
28 #include <linux/export.h>
29 #include <linux/jump_label.h>
30 #include <linux/delay.h>
31 #include <linux/stop_machine.h>
32 #include <asm/processor.h>
33 #include <asm/mmu.h>
34 #include <asm/page.h>
35 #include <asm/pgtable.h>
36 #include <asm/machdep.h>
37 #include <asm/mmu_context.h>
38 #include <asm/iommu.h>
39 #include <asm/tlbflush.h>
40 #include <asm/tlb.h>
41 #include <asm/prom.h>
42 #include <asm/cputable.h>
43 #include <asm/udbg.h>
44 #include <asm/smp.h>
45 #include <asm/trace.h>
46 #include <asm/firmware.h>
47 #include <asm/plpar_wrappers.h>
48 #include <asm/kexec.h>
49 #include <asm/fadump.h>
50 #include <asm/asm-prototypes.h>
51 #include <asm/debugfs.h>
52
53 #include "pseries.h"
54
55 /* Flag bits for H_BULK_REMOVE */
56 #define HBR_REQUEST     0x4000000000000000UL
57 #define HBR_RESPONSE    0x8000000000000000UL
58 #define HBR_END         0xc000000000000000UL
59 #define HBR_AVPN        0x0200000000000000UL
60 #define HBR_ANDCOND     0x0100000000000000UL
61
62
63 /* in hvCall.S */
64 EXPORT_SYMBOL(plpar_hcall);
65 EXPORT_SYMBOL(plpar_hcall9);
66 EXPORT_SYMBOL(plpar_hcall_norets);
67
68 void vpa_init(int cpu)
69 {
70         int hwcpu = get_hard_smp_processor_id(cpu);
71         unsigned long addr;
72         long ret;
73         struct paca_struct *pp;
74         struct dtl_entry *dtl;
75
76         /*
77          * The spec says it "may be problematic" if CPU x registers the VPA of
78          * CPU y. We should never do that, but wail if we ever do.
79          */
80         WARN_ON(cpu != smp_processor_id());
81
82         if (cpu_has_feature(CPU_FTR_ALTIVEC))
83                 lppaca_of(cpu).vmxregs_in_use = 1;
84
85         if (cpu_has_feature(CPU_FTR_ARCH_207S))
86                 lppaca_of(cpu).ebb_regs_in_use = 1;
87
88         addr = __pa(&lppaca_of(cpu));
89         ret = register_vpa(hwcpu, addr);
90
91         if (ret) {
92                 pr_err("WARNING: VPA registration for cpu %d (hw %d) of area "
93                        "%lx failed with %ld\n", cpu, hwcpu, addr, ret);
94                 return;
95         }
96
97 #ifdef CONFIG_PPC_STD_MMU_64
98         /*
99          * PAPR says this feature is SLB-Buffer but firmware never
100          * reports that.  All SPLPAR support SLB shadow buffer.
101          */
102         if (!radix_enabled() && firmware_has_feature(FW_FEATURE_SPLPAR)) {
103                 addr = __pa(paca[cpu].slb_shadow_ptr);
104                 ret = register_slb_shadow(hwcpu, addr);
105                 if (ret)
106                         pr_err("WARNING: SLB shadow buffer registration for "
107                                "cpu %d (hw %d) of area %lx failed with %ld\n",
108                                cpu, hwcpu, addr, ret);
109         }
110 #endif /* CONFIG_PPC_STD_MMU_64 */
111
112         /*
113          * Register dispatch trace log, if one has been allocated.
114          */
115         pp = &paca[cpu];
116         dtl = pp->dispatch_log;
117         if (dtl) {
118                 pp->dtl_ridx = 0;
119                 pp->dtl_curr = dtl;
120                 lppaca_of(cpu).dtl_idx = 0;
121
122                 /* hypervisor reads buffer length from this field */
123                 dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);
124                 ret = register_dtl(hwcpu, __pa(dtl));
125                 if (ret)
126                         pr_err("WARNING: DTL registration of cpu %d (hw %d) "
127                                "failed with %ld\n", smp_processor_id(),
128                                hwcpu, ret);
129                 lppaca_of(cpu).dtl_enable_mask = 2;
130         }
131 }
132
133 #ifdef CONFIG_PPC_STD_MMU_64
134
135 static long pSeries_lpar_hpte_insert(unsigned long hpte_group,
136                                      unsigned long vpn, unsigned long pa,
137                                      unsigned long rflags, unsigned long vflags,
138                                      int psize, int apsize, int ssize)
139 {
140         unsigned long lpar_rc;
141         unsigned long flags;
142         unsigned long slot;
143         unsigned long hpte_v, hpte_r;
144
145         if (!(vflags & HPTE_V_BOLTED))
146                 pr_devel("hpte_insert(group=%lx, vpn=%016lx, "
147                          "pa=%016lx, rflags=%lx, vflags=%lx, psize=%d)\n",
148                          hpte_group, vpn,  pa, rflags, vflags, psize);
149
150         hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
151         hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
152
153         if (!(vflags & HPTE_V_BOLTED))
154                 pr_devel(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
155
156         /* Now fill in the actual HPTE */
157         /* Set CEC cookie to 0         */
158         /* Zero page = 0               */
159         /* I-cache Invalidate = 0      */
160         /* I-cache synchronize = 0     */
161         /* Exact = 0                   */
162         flags = 0;
163
164         if (firmware_has_feature(FW_FEATURE_XCMO) && !(hpte_r & HPTE_R_N))
165                 flags |= H_COALESCE_CAND;
166
167         lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot);
168         if (unlikely(lpar_rc == H_PTEG_FULL)) {
169                 if (!(vflags & HPTE_V_BOLTED))
170                         pr_devel(" full\n");
171                 return -1;
172         }
173
174         /*
175          * Since we try and ioremap PHBs we don't own, the pte insert
176          * will fail. However we must catch the failure in hash_page
177          * or we will loop forever, so return -2 in this case.
178          */
179         if (unlikely(lpar_rc != H_SUCCESS)) {
180                 if (!(vflags & HPTE_V_BOLTED))
181                         pr_devel(" lpar err %ld\n", lpar_rc);
182                 return -2;
183         }
184         if (!(vflags & HPTE_V_BOLTED))
185                 pr_devel(" -> slot: %lu\n", slot & 7);
186
187         /* Because of iSeries, we have to pass down the secondary
188          * bucket bit here as well
189          */
190         return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);
191 }
192
193 static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
194
195 static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
196 {
197         unsigned long slot_offset;
198         unsigned long lpar_rc;
199         int i;
200         unsigned long dummy1, dummy2;
201
202         /* pick a random slot to start at */
203         slot_offset = mftb() & 0x7;
204
205         for (i = 0; i < HPTES_PER_GROUP; i++) {
206
207                 /* don't remove a bolted entry */
208                 lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
209                                            (0x1UL << 4), &dummy1, &dummy2);
210                 if (lpar_rc == H_SUCCESS)
211                         return i;
212
213                 /*
214                  * The test for adjunct partition is performed before the
215                  * ANDCOND test.  H_RESOURCE may be returned, so we need to
216                  * check for that as well.
217                  */
218                 BUG_ON(lpar_rc != H_NOT_FOUND && lpar_rc != H_RESOURCE);
219
220                 slot_offset++;
221                 slot_offset &= 0x7;
222         }
223
224         return -1;
225 }
226
227 static void manual_hpte_clear_all(void)
228 {
229         unsigned long size_bytes = 1UL << ppc64_pft_size;
230         unsigned long hpte_count = size_bytes >> 4;
231         struct {
232                 unsigned long pteh;
233                 unsigned long ptel;
234         } ptes[4];
235         long lpar_rc;
236         unsigned long i, j;
237
238         /* Read in batches of 4,
239          * invalidate only valid entries not in the VRMA
240          * hpte_count will be a multiple of 4
241          */
242         for (i = 0; i < hpte_count; i += 4) {
243                 lpar_rc = plpar_pte_read_4_raw(0, i, (void *)ptes);
244                 if (lpar_rc != H_SUCCESS)
245                         continue;
246                 for (j = 0; j < 4; j++){
247                         if ((ptes[j].pteh & HPTE_V_VRMA_MASK) ==
248                                 HPTE_V_VRMA_MASK)
249                                 continue;
250                         if (ptes[j].pteh & HPTE_V_VALID)
251                                 plpar_pte_remove_raw(0, i + j, 0,
252                                         &(ptes[j].pteh), &(ptes[j].ptel));
253                 }
254         }
255 }
256
257 static int hcall_hpte_clear_all(void)
258 {
259         int rc;
260
261         do {
262                 rc = plpar_hcall_norets(H_CLEAR_HPT);
263         } while (rc == H_CONTINUE);
264
265         return rc;
266 }
267
268 static void pseries_hpte_clear_all(void)
269 {
270         int rc;
271
272         rc = hcall_hpte_clear_all();
273         if (rc != H_SUCCESS)
274                 manual_hpte_clear_all();
275
276 #ifdef __LITTLE_ENDIAN__
277         /*
278          * Reset exceptions to big endian.
279          *
280          * FIXME this is a hack for kexec, we need to reset the exception
281          * endian before starting the new kernel and this is a convenient place
282          * to do it.
283          *
284          * This is also called on boot when a fadump happens. In that case we
285          * must not change the exception endian mode.
286          */
287         if (firmware_has_feature(FW_FEATURE_SET_MODE) && !is_fadump_active())
288                 pseries_big_endian_exceptions();
289 #endif
290 }
291
292 /*
293  * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
294  * the low 3 bits of flags happen to line up.  So no transform is needed.
295  * We can probably optimize here and assume the high bits of newpp are
296  * already zero.  For now I am paranoid.
297  */
298 static long pSeries_lpar_hpte_updatepp(unsigned long slot,
299                                        unsigned long newpp,
300                                        unsigned long vpn,
301                                        int psize, int apsize,
302                                        int ssize, unsigned long inv_flags)
303 {
304         unsigned long lpar_rc;
305         unsigned long flags;
306         unsigned long want_v;
307
308         want_v = hpte_encode_avpn(vpn, psize, ssize);
309
310         pr_devel("    update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...",
311                  want_v, slot, flags, psize);
312
313         flags = (newpp & 7) | H_AVPN;
314         if (mmu_has_feature(MMU_FTR_KERNEL_RO))
315                 /* Move pp0 into bit 8 (IBM 55) */
316                 flags |= (newpp & HPTE_R_PP0) >> 55;
317
318         lpar_rc = plpar_pte_protect(flags, slot, want_v);
319
320         if (lpar_rc == H_NOT_FOUND) {
321                 pr_devel("not found !\n");
322                 return -1;
323         }
324
325         pr_devel("ok\n");
326
327         BUG_ON(lpar_rc != H_SUCCESS);
328
329         return 0;
330 }
331
332 static long __pSeries_lpar_hpte_find(unsigned long want_v, unsigned long hpte_group)
333 {
334         long lpar_rc;
335         unsigned long i, j;
336         struct {
337                 unsigned long pteh;
338                 unsigned long ptel;
339         } ptes[4];
340
341         for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
342
343                 lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
344                 if (lpar_rc != H_SUCCESS)
345                         continue;
346
347                 for (j = 0; j < 4; j++) {
348                         if (HPTE_V_COMPARE(ptes[j].pteh, want_v) &&
349                             (ptes[j].pteh & HPTE_V_VALID))
350                                 return i + j;
351                 }
352         }
353
354         return -1;
355 }
356
357 static long pSeries_lpar_hpte_find(unsigned long vpn, int psize, int ssize)
358 {
359         long slot;
360         unsigned long hash;
361         unsigned long want_v;
362         unsigned long hpte_group;
363
364         hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
365         want_v = hpte_encode_avpn(vpn, psize, ssize);
366
367         /* Bolted entries are always in the primary group */
368         hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
369         slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
370         if (slot < 0)
371                 return -1;
372         return hpte_group + slot;
373 }
374
375 static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
376                                              unsigned long ea,
377                                              int psize, int ssize)
378 {
379         unsigned long vpn;
380         unsigned long lpar_rc, slot, vsid, flags;
381
382         vsid = get_kernel_vsid(ea, ssize);
383         vpn = hpt_vpn(ea, vsid, ssize);
384
385         slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
386         BUG_ON(slot == -1);
387
388         flags = newpp & 7;
389         if (mmu_has_feature(MMU_FTR_KERNEL_RO))
390                 /* Move pp0 into bit 8 (IBM 55) */
391                 flags |= (newpp & HPTE_R_PP0) >> 55;
392
393         lpar_rc = plpar_pte_protect(flags, slot, 0);
394
395         BUG_ON(lpar_rc != H_SUCCESS);
396 }
397
398 static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn,
399                                          int psize, int apsize,
400                                          int ssize, int local)
401 {
402         unsigned long want_v;
403         unsigned long lpar_rc;
404         unsigned long dummy1, dummy2;
405
406         pr_devel("    inval : slot=%lx, vpn=%016lx, psize: %d, local: %d\n",
407                  slot, vpn, psize, local);
408
409         want_v = hpte_encode_avpn(vpn, psize, ssize);
410         lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2);
411         if (lpar_rc == H_NOT_FOUND)
412                 return;
413
414         BUG_ON(lpar_rc != H_SUCCESS);
415 }
416
417 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
418 /*
419  * Limit iterations holding pSeries_lpar_tlbie_lock to 3. We also need
420  * to make sure that we avoid bouncing the hypervisor tlbie lock.
421  */
422 #define PPC64_HUGE_HPTE_BATCH 12
423
424 static void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
425                                              unsigned long *vpn, int count,
426                                              int psize, int ssize)
427 {
428         unsigned long param[PLPAR_HCALL9_BUFSIZE];
429         int i = 0, pix = 0, rc;
430         unsigned long flags = 0;
431         int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
432
433         if (lock_tlbie)
434                 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
435
436         for (i = 0; i < count; i++) {
437
438                 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
439                         pSeries_lpar_hpte_invalidate(slot[i], vpn[i], psize, 0,
440                                                      ssize, 0);
441                 } else {
442                         param[pix] = HBR_REQUEST | HBR_AVPN | slot[i];
443                         param[pix+1] = hpte_encode_avpn(vpn[i], psize, ssize);
444                         pix += 2;
445                         if (pix == 8) {
446                                 rc = plpar_hcall9(H_BULK_REMOVE, param,
447                                                   param[0], param[1], param[2],
448                                                   param[3], param[4], param[5],
449                                                   param[6], param[7]);
450                                 BUG_ON(rc != H_SUCCESS);
451                                 pix = 0;
452                         }
453                 }
454         }
455         if (pix) {
456                 param[pix] = HBR_END;
457                 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
458                                   param[2], param[3], param[4], param[5],
459                                   param[6], param[7]);
460                 BUG_ON(rc != H_SUCCESS);
461         }
462
463         if (lock_tlbie)
464                 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
465 }
466
467 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
468                                              unsigned long addr,
469                                              unsigned char *hpte_slot_array,
470                                              int psize, int ssize, int local)
471 {
472         int i, index = 0;
473         unsigned long s_addr = addr;
474         unsigned int max_hpte_count, valid;
475         unsigned long vpn_array[PPC64_HUGE_HPTE_BATCH];
476         unsigned long slot_array[PPC64_HUGE_HPTE_BATCH];
477         unsigned long shift, hidx, vpn = 0, hash, slot;
478
479         shift = mmu_psize_defs[psize].shift;
480         max_hpte_count = 1U << (PMD_SHIFT - shift);
481
482         for (i = 0; i < max_hpte_count; i++) {
483                 valid = hpte_valid(hpte_slot_array, i);
484                 if (!valid)
485                         continue;
486                 hidx =  hpte_hash_index(hpte_slot_array, i);
487
488                 /* get the vpn */
489                 addr = s_addr + (i * (1ul << shift));
490                 vpn = hpt_vpn(addr, vsid, ssize);
491                 hash = hpt_hash(vpn, shift, ssize);
492                 if (hidx & _PTEIDX_SECONDARY)
493                         hash = ~hash;
494
495                 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
496                 slot += hidx & _PTEIDX_GROUP_IX;
497
498                 slot_array[index] = slot;
499                 vpn_array[index] = vpn;
500                 if (index == PPC64_HUGE_HPTE_BATCH - 1) {
501                         /*
502                          * Now do a bluk invalidate
503                          */
504                         __pSeries_lpar_hugepage_invalidate(slot_array,
505                                                            vpn_array,
506                                                            PPC64_HUGE_HPTE_BATCH,
507                                                            psize, ssize);
508                         index = 0;
509                 } else
510                         index++;
511         }
512         if (index)
513                 __pSeries_lpar_hugepage_invalidate(slot_array, vpn_array,
514                                                    index, psize, ssize);
515 }
516 #else
517 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
518                                              unsigned long addr,
519                                              unsigned char *hpte_slot_array,
520                                              int psize, int ssize, int local)
521 {
522         WARN(1, "%s called without THP support\n", __func__);
523 }
524 #endif
525
526 static int pSeries_lpar_hpte_removebolted(unsigned long ea,
527                                           int psize, int ssize)
528 {
529         unsigned long vpn;
530         unsigned long slot, vsid;
531
532         vsid = get_kernel_vsid(ea, ssize);
533         vpn = hpt_vpn(ea, vsid, ssize);
534
535         slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
536         if (slot == -1)
537                 return -ENOENT;
538
539         /*
540          * lpar doesn't use the passed actual page size
541          */
542         pSeries_lpar_hpte_invalidate(slot, vpn, psize, 0, ssize, 0);
543         return 0;
544 }
545
546 /*
547  * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
548  * lock.
549  */
550 static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
551 {
552         unsigned long vpn;
553         unsigned long i, pix, rc;
554         unsigned long flags = 0;
555         struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
556         int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
557         unsigned long param[PLPAR_HCALL9_BUFSIZE];
558         unsigned long hash, index, shift, hidx, slot;
559         real_pte_t pte;
560         int psize, ssize;
561
562         if (lock_tlbie)
563                 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
564
565         psize = batch->psize;
566         ssize = batch->ssize;
567         pix = 0;
568         for (i = 0; i < number; i++) {
569                 vpn = batch->vpn[i];
570                 pte = batch->pte[i];
571                 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
572                         hash = hpt_hash(vpn, shift, ssize);
573                         hidx = __rpte_to_hidx(pte, index);
574                         if (hidx & _PTEIDX_SECONDARY)
575                                 hash = ~hash;
576                         slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
577                         slot += hidx & _PTEIDX_GROUP_IX;
578                         if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
579                                 /*
580                                  * lpar doesn't use the passed actual page size
581                                  */
582                                 pSeries_lpar_hpte_invalidate(slot, vpn, psize,
583                                                              0, ssize, local);
584                         } else {
585                                 param[pix] = HBR_REQUEST | HBR_AVPN | slot;
586                                 param[pix+1] = hpte_encode_avpn(vpn, psize,
587                                                                 ssize);
588                                 pix += 2;
589                                 if (pix == 8) {
590                                         rc = plpar_hcall9(H_BULK_REMOVE, param,
591                                                 param[0], param[1], param[2],
592                                                 param[3], param[4], param[5],
593                                                 param[6], param[7]);
594                                         BUG_ON(rc != H_SUCCESS);
595                                         pix = 0;
596                                 }
597                         }
598                 } pte_iterate_hashed_end();
599         }
600         if (pix) {
601                 param[pix] = HBR_END;
602                 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
603                                   param[2], param[3], param[4], param[5],
604                                   param[6], param[7]);
605                 BUG_ON(rc != H_SUCCESS);
606         }
607
608         if (lock_tlbie)
609                 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
610 }
611
612 static int __init disable_bulk_remove(char *str)
613 {
614         if (strcmp(str, "off") == 0 &&
615             firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
616                         printk(KERN_INFO "Disabling BULK_REMOVE firmware feature");
617                         powerpc_firmware_features &= ~FW_FEATURE_BULK_REMOVE;
618         }
619         return 1;
620 }
621
622 __setup("bulk_remove=", disable_bulk_remove);
623
624 #define HPT_RESIZE_TIMEOUT      10000 /* ms */
625
626 struct hpt_resize_state {
627         unsigned long shift;
628         int commit_rc;
629 };
630
631 static int pseries_lpar_resize_hpt_commit(void *data)
632 {
633         struct hpt_resize_state *state = data;
634
635         state->commit_rc = plpar_resize_hpt_commit(0, state->shift);
636         if (state->commit_rc != H_SUCCESS)
637                 return -EIO;
638
639         /* Hypervisor has transitioned the HTAB, update our globals */
640         ppc64_pft_size = state->shift;
641         htab_size_bytes = 1UL << ppc64_pft_size;
642         htab_hash_mask = (htab_size_bytes >> 7) - 1;
643
644         return 0;
645 }
646
647 /*
648  * Must be called in process context. The caller must hold the
649  * cpus_lock.
650  */
651 static int pseries_lpar_resize_hpt(unsigned long shift)
652 {
653         struct hpt_resize_state state = {
654                 .shift = shift,
655                 .commit_rc = H_FUNCTION,
656         };
657         unsigned int delay, total_delay = 0;
658         int rc;
659         ktime_t t0, t1, t2;
660
661         might_sleep();
662
663         if (!firmware_has_feature(FW_FEATURE_HPT_RESIZE))
664                 return -ENODEV;
665
666         printk(KERN_INFO "lpar: Attempting to resize HPT to shift %lu\n",
667                shift);
668
669         t0 = ktime_get();
670
671         rc = plpar_resize_hpt_prepare(0, shift);
672         while (H_IS_LONG_BUSY(rc)) {
673                 delay = get_longbusy_msecs(rc);
674                 total_delay += delay;
675                 if (total_delay > HPT_RESIZE_TIMEOUT) {
676                         /* prepare with shift==0 cancels an in-progress resize */
677                         rc = plpar_resize_hpt_prepare(0, 0);
678                         if (rc != H_SUCCESS)
679                                 printk(KERN_WARNING
680                                        "lpar: Unexpected error %d cancelling timed out HPT resize\n",
681                                        rc);
682                         return -ETIMEDOUT;
683                 }
684                 msleep(delay);
685                 rc = plpar_resize_hpt_prepare(0, shift);
686         };
687
688         switch (rc) {
689         case H_SUCCESS:
690                 /* Continue on */
691                 break;
692
693         case H_PARAMETER:
694                 return -EINVAL;
695         case H_RESOURCE:
696                 return -EPERM;
697         default:
698                 printk(KERN_WARNING
699                        "lpar: Unexpected error %d from H_RESIZE_HPT_PREPARE\n",
700                        rc);
701                 return -EIO;
702         }
703
704         t1 = ktime_get();
705
706         rc = stop_machine_cpuslocked(pseries_lpar_resize_hpt_commit,
707                                      &state, NULL);
708
709         t2 = ktime_get();
710
711         if (rc != 0) {
712                 switch (state.commit_rc) {
713                 case H_PTEG_FULL:
714                         printk(KERN_WARNING
715                                "lpar: Hash collision while resizing HPT\n");
716                         return -ENOSPC;
717
718                 default:
719                         printk(KERN_WARNING
720                                "lpar: Unexpected error %d from H_RESIZE_HPT_COMMIT\n",
721                                state.commit_rc);
722                         return -EIO;
723                 };
724         }
725
726         printk(KERN_INFO
727                "lpar: HPT resize to shift %lu complete (%lld ms / %lld ms)\n",
728                shift, (long long) ktime_ms_delta(t1, t0),
729                (long long) ktime_ms_delta(t2, t1));
730
731         return 0;
732 }
733
734 static int pseries_lpar_register_process_table(unsigned long base,
735                         unsigned long page_size, unsigned long table_size)
736 {
737         long rc;
738         unsigned long flags = 0;
739
740         if (table_size)
741                 flags |= PROC_TABLE_NEW;
742         if (radix_enabled())
743                 flags |= PROC_TABLE_RADIX | PROC_TABLE_GTSE;
744         else
745                 flags |= PROC_TABLE_HPT_SLB;
746         for (;;) {
747                 rc = plpar_hcall_norets(H_REGISTER_PROC_TBL, flags, base,
748                                         page_size, table_size);
749                 if (!H_IS_LONG_BUSY(rc))
750                         break;
751                 mdelay(get_longbusy_msecs(rc));
752         }
753         if (rc != H_SUCCESS) {
754                 pr_err("Failed to register process table (rc=%ld)\n", rc);
755                 BUG();
756         }
757         return rc;
758 }
759
760 void __init hpte_init_pseries(void)
761 {
762         mmu_hash_ops.hpte_invalidate     = pSeries_lpar_hpte_invalidate;
763         mmu_hash_ops.hpte_updatepp       = pSeries_lpar_hpte_updatepp;
764         mmu_hash_ops.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
765         mmu_hash_ops.hpte_insert         = pSeries_lpar_hpte_insert;
766         mmu_hash_ops.hpte_remove         = pSeries_lpar_hpte_remove;
767         mmu_hash_ops.hpte_removebolted   = pSeries_lpar_hpte_removebolted;
768         mmu_hash_ops.flush_hash_range    = pSeries_lpar_flush_hash_range;
769         mmu_hash_ops.hpte_clear_all      = pseries_hpte_clear_all;
770         mmu_hash_ops.hugepage_invalidate = pSeries_lpar_hugepage_invalidate;
771         register_process_table           = pseries_lpar_register_process_table;
772
773         if (firmware_has_feature(FW_FEATURE_HPT_RESIZE))
774                 mmu_hash_ops.resize_hpt = pseries_lpar_resize_hpt;
775 }
776
777 void radix_init_pseries(void)
778 {
779         pr_info("Using radix MMU under hypervisor\n");
780         register_process_table = pseries_lpar_register_process_table;
781 }
782
783 #ifdef CONFIG_PPC_SMLPAR
784 #define CMO_FREE_HINT_DEFAULT 1
785 static int cmo_free_hint_flag = CMO_FREE_HINT_DEFAULT;
786
787 static int __init cmo_free_hint(char *str)
788 {
789         char *parm;
790         parm = strstrip(str);
791
792         if (strcasecmp(parm, "no") == 0 || strcasecmp(parm, "off") == 0) {
793                 printk(KERN_INFO "cmo_free_hint: CMO free page hinting is not active.\n");
794                 cmo_free_hint_flag = 0;
795                 return 1;
796         }
797
798         cmo_free_hint_flag = 1;
799         printk(KERN_INFO "cmo_free_hint: CMO free page hinting is active.\n");
800
801         if (strcasecmp(parm, "yes") == 0 || strcasecmp(parm, "on") == 0)
802                 return 1;
803
804         return 0;
805 }
806
807 __setup("cmo_free_hint=", cmo_free_hint);
808
809 static void pSeries_set_page_state(struct page *page, int order,
810                                    unsigned long state)
811 {
812         int i, j;
813         unsigned long cmo_page_sz, addr;
814
815         cmo_page_sz = cmo_get_page_size();
816         addr = __pa((unsigned long)page_address(page));
817
818         for (i = 0; i < (1 << order); i++, addr += PAGE_SIZE) {
819                 for (j = 0; j < PAGE_SIZE; j += cmo_page_sz)
820                         plpar_hcall_norets(H_PAGE_INIT, state, addr + j, 0);
821         }
822 }
823
824 void arch_free_page(struct page *page, int order)
825 {
826         if (radix_enabled())
827                 return;
828         if (!cmo_free_hint_flag || !firmware_has_feature(FW_FEATURE_CMO))
829                 return;
830
831         pSeries_set_page_state(page, order, H_PAGE_SET_UNUSED);
832 }
833 EXPORT_SYMBOL(arch_free_page);
834
835 #endif /* CONFIG_PPC_SMLPAR */
836 #endif /* CONFIG_PPC_STD_MMU_64 */
837
838 #ifdef CONFIG_TRACEPOINTS
839 #ifdef HAVE_JUMP_LABEL
840 struct static_key hcall_tracepoint_key = STATIC_KEY_INIT;
841
842 int hcall_tracepoint_regfunc(void)
843 {
844         static_key_slow_inc(&hcall_tracepoint_key);
845         return 0;
846 }
847
848 void hcall_tracepoint_unregfunc(void)
849 {
850         static_key_slow_dec(&hcall_tracepoint_key);
851 }
852 #else
853 /*
854  * We optimise our hcall path by placing hcall_tracepoint_refcount
855  * directly in the TOC so we can check if the hcall tracepoints are
856  * enabled via a single load.
857  */
858
859 /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
860 extern long hcall_tracepoint_refcount;
861
862 int hcall_tracepoint_regfunc(void)
863 {
864         hcall_tracepoint_refcount++;
865         return 0;
866 }
867
868 void hcall_tracepoint_unregfunc(void)
869 {
870         hcall_tracepoint_refcount--;
871 }
872 #endif
873
874 /*
875  * Since the tracing code might execute hcalls we need to guard against
876  * recursion. One example of this are spinlocks calling H_YIELD on
877  * shared processor partitions.
878  */
879 static DEFINE_PER_CPU(unsigned int, hcall_trace_depth);
880
881
882 void __trace_hcall_entry(unsigned long opcode, unsigned long *args)
883 {
884         unsigned long flags;
885         unsigned int *depth;
886
887         /*
888          * We cannot call tracepoints inside RCU idle regions which
889          * means we must not trace H_CEDE.
890          */
891         if (opcode == H_CEDE)
892                 return;
893
894         local_irq_save(flags);
895
896         depth = this_cpu_ptr(&hcall_trace_depth);
897
898         if (*depth)
899                 goto out;
900
901         (*depth)++;
902         preempt_disable();
903         trace_hcall_entry(opcode, args);
904         (*depth)--;
905
906 out:
907         local_irq_restore(flags);
908 }
909
910 void __trace_hcall_exit(long opcode, unsigned long retval,
911                         unsigned long *retbuf)
912 {
913         unsigned long flags;
914         unsigned int *depth;
915
916         if (opcode == H_CEDE)
917                 return;
918
919         local_irq_save(flags);
920
921         depth = this_cpu_ptr(&hcall_trace_depth);
922
923         if (*depth)
924                 goto out;
925
926         (*depth)++;
927         trace_hcall_exit(opcode, retval, retbuf);
928         preempt_enable();
929         (*depth)--;
930
931 out:
932         local_irq_restore(flags);
933 }
934 #endif
935
936 /**
937  * h_get_mpp
938  * H_GET_MPP hcall returns info in 7 parms
939  */
940 int h_get_mpp(struct hvcall_mpp_data *mpp_data)
941 {
942         int rc;
943         unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
944
945         rc = plpar_hcall9(H_GET_MPP, retbuf);
946
947         mpp_data->entitled_mem = retbuf[0];
948         mpp_data->mapped_mem = retbuf[1];
949
950         mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
951         mpp_data->pool_num = retbuf[2] & 0xffff;
952
953         mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
954         mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
955         mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffffUL;
956
957         mpp_data->pool_size = retbuf[4];
958         mpp_data->loan_request = retbuf[5];
959         mpp_data->backing_mem = retbuf[6];
960
961         return rc;
962 }
963 EXPORT_SYMBOL(h_get_mpp);
964
965 int h_get_mpp_x(struct hvcall_mpp_x_data *mpp_x_data)
966 {
967         int rc;
968         unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };
969
970         rc = plpar_hcall9(H_GET_MPP_X, retbuf);
971
972         mpp_x_data->coalesced_bytes = retbuf[0];
973         mpp_x_data->pool_coalesced_bytes = retbuf[1];
974         mpp_x_data->pool_purr_cycles = retbuf[2];
975         mpp_x_data->pool_spurr_cycles = retbuf[3];
976
977         return rc;
978 }
979
980 static unsigned long vsid_unscramble(unsigned long vsid, int ssize)
981 {
982         unsigned long protovsid;
983         unsigned long va_bits = VA_BITS;
984         unsigned long modinv, vsid_modulus;
985         unsigned long max_mod_inv, tmp_modinv;
986
987         if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
988                 va_bits = 65;
989
990         if (ssize == MMU_SEGSIZE_256M) {
991                 modinv = VSID_MULINV_256M;
992                 vsid_modulus = ((1UL << (va_bits - SID_SHIFT)) - 1);
993         } else {
994                 modinv = VSID_MULINV_1T;
995                 vsid_modulus = ((1UL << (va_bits - SID_SHIFT_1T)) - 1);
996         }
997
998         /*
999          * vsid outside our range.
1000          */
1001         if (vsid >= vsid_modulus)
1002                 return 0;
1003
1004         /*
1005          * If modinv is the modular multiplicate inverse of (x % vsid_modulus)
1006          * and vsid = (protovsid * x) % vsid_modulus, then we say:
1007          *   protovsid = (vsid * modinv) % vsid_modulus
1008          */
1009
1010         /* Check if (vsid * modinv) overflow (63 bits) */
1011         max_mod_inv = 0x7fffffffffffffffull / vsid;
1012         if (modinv < max_mod_inv)
1013                 return (vsid * modinv) % vsid_modulus;
1014
1015         tmp_modinv = modinv/max_mod_inv;
1016         modinv %= max_mod_inv;
1017
1018         protovsid = (((vsid * max_mod_inv) % vsid_modulus) * tmp_modinv) % vsid_modulus;
1019         protovsid = (protovsid + vsid * modinv) % vsid_modulus;
1020
1021         return protovsid;
1022 }
1023
1024 static int __init reserve_vrma_context_id(void)
1025 {
1026         unsigned long protovsid;
1027
1028         /*
1029          * Reserve context ids which map to reserved virtual addresses. For now
1030          * we only reserve the context id which maps to the VRMA VSID. We ignore
1031          * the addresses in "ibm,adjunct-virtual-addresses" because we don't
1032          * enable adjunct support via the "ibm,client-architecture-support"
1033          * interface.
1034          */
1035         protovsid = vsid_unscramble(VRMA_VSID, MMU_SEGSIZE_1T);
1036         hash__reserve_context_id(protovsid >> ESID_BITS_1T);
1037         return 0;
1038 }
1039 machine_device_initcall(pseries, reserve_vrma_context_id);
1040
1041 #ifdef CONFIG_DEBUG_FS
1042 /* debugfs file interface for vpa data */
1043 static ssize_t vpa_file_read(struct file *filp, char __user *buf, size_t len,
1044                               loff_t *pos)
1045 {
1046         int cpu = (long)filp->private_data;
1047         struct lppaca *lppaca = &lppaca_of(cpu);
1048
1049         return simple_read_from_buffer(buf, len, pos, lppaca,
1050                                 sizeof(struct lppaca));
1051 }
1052
1053 static const struct file_operations vpa_fops = {
1054         .open           = simple_open,
1055         .read           = vpa_file_read,
1056         .llseek         = default_llseek,
1057 };
1058
1059 static int __init vpa_debugfs_init(void)
1060 {
1061         char name[16];
1062         long i;
1063         struct dentry *vpa_dir;
1064
1065         if (!firmware_has_feature(FW_FEATURE_SPLPAR))
1066                 return 0;
1067
1068         vpa_dir = debugfs_create_dir("vpa", powerpc_debugfs_root);
1069         if (!vpa_dir) {
1070                 pr_warn("%s: can't create vpa root dir\n", __func__);
1071                 return -ENOMEM;
1072         }
1073
1074         /* set up the per-cpu vpa file*/
1075         for_each_possible_cpu(i) {
1076                 struct dentry *d;
1077
1078                 sprintf(name, "cpu-%ld", i);
1079
1080                 d = debugfs_create_file(name, 0400, vpa_dir, (void *)i,
1081                                         &vpa_fops);
1082                 if (!d) {
1083                         pr_warn("%s: can't create per-cpu vpa file\n",
1084                                         __func__);
1085                         return -ENOMEM;
1086                 }
1087         }
1088
1089         return 0;
1090 }
1091 machine_arch_initcall(pseries, vpa_debugfs_init);
1092 #endif /* CONFIG_DEBUG_FS */