lpar.c 26 KB

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