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