lpar.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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_STD_MMU_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[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_STD_MMU_64 */
  97. /*
  98. * Register dispatch trace log, if one has been allocated.
  99. */
  100. pp = &paca[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_STD_MMU_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 = (newpp & 7) | H_AVPN;
  263. unsigned long want_v;
  264. want_v = hpte_encode_avpn(vpn, psize, ssize);
  265. pr_devel(" update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...",
  266. want_v, slot, flags, psize);
  267. lpar_rc = plpar_pte_protect(flags, slot, want_v);
  268. if (lpar_rc == H_NOT_FOUND) {
  269. pr_devel("not found !\n");
  270. return -1;
  271. }
  272. pr_devel("ok\n");
  273. BUG_ON(lpar_rc != H_SUCCESS);
  274. return 0;
  275. }
  276. static long __pSeries_lpar_hpte_find(unsigned long want_v, unsigned long hpte_group)
  277. {
  278. long lpar_rc;
  279. unsigned long i, j;
  280. struct {
  281. unsigned long pteh;
  282. unsigned long ptel;
  283. } ptes[4];
  284. for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
  285. lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
  286. if (lpar_rc != H_SUCCESS)
  287. continue;
  288. for (j = 0; j < 4; j++) {
  289. if (HPTE_V_COMPARE(ptes[j].pteh, want_v) &&
  290. (ptes[j].pteh & HPTE_V_VALID))
  291. return i + j;
  292. }
  293. }
  294. return -1;
  295. }
  296. static long pSeries_lpar_hpte_find(unsigned long vpn, int psize, int ssize)
  297. {
  298. long slot;
  299. unsigned long hash;
  300. unsigned long want_v;
  301. unsigned long hpte_group;
  302. hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
  303. want_v = hpte_encode_avpn(vpn, psize, ssize);
  304. /* Bolted entries are always in the primary group */
  305. hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  306. slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
  307. if (slot < 0)
  308. return -1;
  309. return hpte_group + slot;
  310. }
  311. static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
  312. unsigned long ea,
  313. int psize, int ssize)
  314. {
  315. unsigned long vpn;
  316. unsigned long lpar_rc, slot, vsid, flags;
  317. vsid = get_kernel_vsid(ea, ssize);
  318. vpn = hpt_vpn(ea, vsid, ssize);
  319. slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
  320. BUG_ON(slot == -1);
  321. flags = newpp & 7;
  322. lpar_rc = plpar_pte_protect(flags, slot, 0);
  323. BUG_ON(lpar_rc != H_SUCCESS);
  324. }
  325. static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn,
  326. int psize, int apsize,
  327. int ssize, int local)
  328. {
  329. unsigned long want_v;
  330. unsigned long lpar_rc;
  331. unsigned long dummy1, dummy2;
  332. pr_devel(" inval : slot=%lx, vpn=%016lx, psize: %d, local: %d\n",
  333. slot, vpn, psize, local);
  334. want_v = hpte_encode_avpn(vpn, psize, ssize);
  335. lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2);
  336. if (lpar_rc == H_NOT_FOUND)
  337. return;
  338. BUG_ON(lpar_rc != H_SUCCESS);
  339. }
  340. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  341. /*
  342. * Limit iterations holding pSeries_lpar_tlbie_lock to 3. We also need
  343. * to make sure that we avoid bouncing the hypervisor tlbie lock.
  344. */
  345. #define PPC64_HUGE_HPTE_BATCH 12
  346. static void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
  347. unsigned long *vpn, int count,
  348. int psize, int ssize)
  349. {
  350. unsigned long param[PLPAR_HCALL9_BUFSIZE];
  351. int i = 0, pix = 0, rc;
  352. unsigned long flags = 0;
  353. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  354. if (lock_tlbie)
  355. spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
  356. for (i = 0; i < count; i++) {
  357. if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
  358. pSeries_lpar_hpte_invalidate(slot[i], vpn[i], psize, 0,
  359. ssize, 0);
  360. } else {
  361. param[pix] = HBR_REQUEST | HBR_AVPN | slot[i];
  362. param[pix+1] = hpte_encode_avpn(vpn[i], psize, ssize);
  363. pix += 2;
  364. if (pix == 8) {
  365. rc = plpar_hcall9(H_BULK_REMOVE, param,
  366. param[0], param[1], param[2],
  367. param[3], param[4], param[5],
  368. param[6], param[7]);
  369. BUG_ON(rc != H_SUCCESS);
  370. pix = 0;
  371. }
  372. }
  373. }
  374. if (pix) {
  375. param[pix] = HBR_END;
  376. rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
  377. param[2], param[3], param[4], param[5],
  378. param[6], param[7]);
  379. BUG_ON(rc != H_SUCCESS);
  380. }
  381. if (lock_tlbie)
  382. spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
  383. }
  384. static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
  385. unsigned long addr,
  386. unsigned char *hpte_slot_array,
  387. int psize, int ssize, int local)
  388. {
  389. int i, index = 0;
  390. unsigned long s_addr = addr;
  391. unsigned int max_hpte_count, valid;
  392. unsigned long vpn_array[PPC64_HUGE_HPTE_BATCH];
  393. unsigned long slot_array[PPC64_HUGE_HPTE_BATCH];
  394. unsigned long shift, hidx, vpn = 0, hash, slot;
  395. shift = mmu_psize_defs[psize].shift;
  396. max_hpte_count = 1U << (PMD_SHIFT - shift);
  397. for (i = 0; i < max_hpte_count; i++) {
  398. valid = hpte_valid(hpte_slot_array, i);
  399. if (!valid)
  400. continue;
  401. hidx = hpte_hash_index(hpte_slot_array, i);
  402. /* get the vpn */
  403. addr = s_addr + (i * (1ul << shift));
  404. vpn = hpt_vpn(addr, vsid, ssize);
  405. hash = hpt_hash(vpn, shift, ssize);
  406. if (hidx & _PTEIDX_SECONDARY)
  407. hash = ~hash;
  408. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  409. slot += hidx & _PTEIDX_GROUP_IX;
  410. slot_array[index] = slot;
  411. vpn_array[index] = vpn;
  412. if (index == PPC64_HUGE_HPTE_BATCH - 1) {
  413. /*
  414. * Now do a bluk invalidate
  415. */
  416. __pSeries_lpar_hugepage_invalidate(slot_array,
  417. vpn_array,
  418. PPC64_HUGE_HPTE_BATCH,
  419. psize, ssize);
  420. index = 0;
  421. } else
  422. index++;
  423. }
  424. if (index)
  425. __pSeries_lpar_hugepage_invalidate(slot_array, vpn_array,
  426. index, psize, ssize);
  427. }
  428. #else
  429. static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
  430. unsigned long addr,
  431. unsigned char *hpte_slot_array,
  432. int psize, int ssize, int local)
  433. {
  434. WARN(1, "%s called without THP support\n", __func__);
  435. }
  436. #endif
  437. static int pSeries_lpar_hpte_removebolted(unsigned long ea,
  438. int psize, int ssize)
  439. {
  440. unsigned long vpn;
  441. unsigned long slot, vsid;
  442. vsid = get_kernel_vsid(ea, ssize);
  443. vpn = hpt_vpn(ea, vsid, ssize);
  444. slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
  445. if (slot == -1)
  446. return -ENOENT;
  447. /*
  448. * lpar doesn't use the passed actual page size
  449. */
  450. pSeries_lpar_hpte_invalidate(slot, vpn, psize, 0, ssize, 0);
  451. return 0;
  452. }
  453. /*
  454. * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
  455. * lock.
  456. */
  457. static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
  458. {
  459. unsigned long vpn;
  460. unsigned long i, pix, rc;
  461. unsigned long flags = 0;
  462. struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
  463. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  464. unsigned long param[PLPAR_HCALL9_BUFSIZE];
  465. unsigned long hash, index, shift, hidx, slot;
  466. real_pte_t pte;
  467. int psize, ssize;
  468. if (lock_tlbie)
  469. spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
  470. psize = batch->psize;
  471. ssize = batch->ssize;
  472. pix = 0;
  473. for (i = 0; i < number; i++) {
  474. vpn = batch->vpn[i];
  475. pte = batch->pte[i];
  476. pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
  477. hash = hpt_hash(vpn, shift, ssize);
  478. hidx = __rpte_to_hidx(pte, index);
  479. if (hidx & _PTEIDX_SECONDARY)
  480. hash = ~hash;
  481. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  482. slot += hidx & _PTEIDX_GROUP_IX;
  483. if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
  484. /*
  485. * lpar doesn't use the passed actual page size
  486. */
  487. pSeries_lpar_hpte_invalidate(slot, vpn, psize,
  488. 0, ssize, local);
  489. } else {
  490. param[pix] = HBR_REQUEST | HBR_AVPN | slot;
  491. param[pix+1] = hpte_encode_avpn(vpn, psize,
  492. ssize);
  493. pix += 2;
  494. if (pix == 8) {
  495. rc = plpar_hcall9(H_BULK_REMOVE, param,
  496. param[0], param[1], param[2],
  497. param[3], param[4], param[5],
  498. param[6], param[7]);
  499. BUG_ON(rc != H_SUCCESS);
  500. pix = 0;
  501. }
  502. }
  503. } pte_iterate_hashed_end();
  504. }
  505. if (pix) {
  506. param[pix] = HBR_END;
  507. rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
  508. param[2], param[3], param[4], param[5],
  509. param[6], param[7]);
  510. BUG_ON(rc != H_SUCCESS);
  511. }
  512. if (lock_tlbie)
  513. spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
  514. }
  515. static int __init disable_bulk_remove(char *str)
  516. {
  517. if (strcmp(str, "off") == 0 &&
  518. firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
  519. printk(KERN_INFO "Disabling BULK_REMOVE firmware feature");
  520. powerpc_firmware_features &= ~FW_FEATURE_BULK_REMOVE;
  521. }
  522. return 1;
  523. }
  524. __setup("bulk_remove=", disable_bulk_remove);
  525. #define HPT_RESIZE_TIMEOUT 10000 /* ms */
  526. struct hpt_resize_state {
  527. unsigned long shift;
  528. int commit_rc;
  529. };
  530. static int pseries_lpar_resize_hpt_commit(void *data)
  531. {
  532. struct hpt_resize_state *state = data;
  533. state->commit_rc = plpar_resize_hpt_commit(0, state->shift);
  534. if (state->commit_rc != H_SUCCESS)
  535. return -EIO;
  536. /* Hypervisor has transitioned the HTAB, update our globals */
  537. ppc64_pft_size = state->shift;
  538. htab_size_bytes = 1UL << ppc64_pft_size;
  539. htab_hash_mask = (htab_size_bytes >> 7) - 1;
  540. return 0;
  541. }
  542. /* Must be called in user context */
  543. static int pseries_lpar_resize_hpt(unsigned long shift)
  544. {
  545. struct hpt_resize_state state = {
  546. .shift = shift,
  547. .commit_rc = H_FUNCTION,
  548. };
  549. unsigned int delay, total_delay = 0;
  550. int rc;
  551. ktime_t t0, t1, t2;
  552. might_sleep();
  553. if (!firmware_has_feature(FW_FEATURE_HPT_RESIZE))
  554. return -ENODEV;
  555. printk(KERN_INFO "lpar: Attempting to resize HPT to shift %lu\n",
  556. shift);
  557. t0 = ktime_get();
  558. rc = plpar_resize_hpt_prepare(0, shift);
  559. while (H_IS_LONG_BUSY(rc)) {
  560. delay = get_longbusy_msecs(rc);
  561. total_delay += delay;
  562. if (total_delay > HPT_RESIZE_TIMEOUT) {
  563. /* prepare with shift==0 cancels an in-progress resize */
  564. rc = plpar_resize_hpt_prepare(0, 0);
  565. if (rc != H_SUCCESS)
  566. printk(KERN_WARNING
  567. "lpar: Unexpected error %d cancelling timed out HPT resize\n",
  568. rc);
  569. return -ETIMEDOUT;
  570. }
  571. msleep(delay);
  572. rc = plpar_resize_hpt_prepare(0, shift);
  573. };
  574. switch (rc) {
  575. case H_SUCCESS:
  576. /* Continue on */
  577. break;
  578. case H_PARAMETER:
  579. return -EINVAL;
  580. case H_RESOURCE:
  581. return -EPERM;
  582. default:
  583. printk(KERN_WARNING
  584. "lpar: Unexpected error %d from H_RESIZE_HPT_PREPARE\n",
  585. rc);
  586. return -EIO;
  587. }
  588. t1 = ktime_get();
  589. rc = stop_machine(pseries_lpar_resize_hpt_commit, &state, NULL);
  590. t2 = ktime_get();
  591. if (rc != 0) {
  592. switch (state.commit_rc) {
  593. case H_PTEG_FULL:
  594. printk(KERN_WARNING
  595. "lpar: Hash collision while resizing HPT\n");
  596. return -ENOSPC;
  597. default:
  598. printk(KERN_WARNING
  599. "lpar: Unexpected error %d from H_RESIZE_HPT_COMMIT\n",
  600. state.commit_rc);
  601. return -EIO;
  602. };
  603. }
  604. printk(KERN_INFO
  605. "lpar: HPT resize to shift %lu complete (%lld ms / %lld ms)\n",
  606. shift, (long long) ktime_ms_delta(t1, t0),
  607. (long long) ktime_ms_delta(t2, t1));
  608. return 0;
  609. }
  610. /* Actually only used for radix, so far */
  611. static int pseries_lpar_register_process_table(unsigned long base,
  612. unsigned long page_size, unsigned long table_size)
  613. {
  614. long rc;
  615. unsigned long flags = PROC_TABLE_NEW;
  616. if (radix_enabled())
  617. flags |= PROC_TABLE_RADIX | PROC_TABLE_GTSE;
  618. for (;;) {
  619. rc = plpar_hcall_norets(H_REGISTER_PROC_TBL, flags, base,
  620. page_size, table_size);
  621. if (!H_IS_LONG_BUSY(rc))
  622. break;
  623. mdelay(get_longbusy_msecs(rc));
  624. }
  625. if (rc != H_SUCCESS) {
  626. pr_err("Failed to register process table (rc=%ld)\n", rc);
  627. BUG();
  628. }
  629. return rc;
  630. }
  631. void __init hpte_init_pseries(void)
  632. {
  633. mmu_hash_ops.hpte_invalidate = pSeries_lpar_hpte_invalidate;
  634. mmu_hash_ops.hpte_updatepp = pSeries_lpar_hpte_updatepp;
  635. mmu_hash_ops.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
  636. mmu_hash_ops.hpte_insert = pSeries_lpar_hpte_insert;
  637. mmu_hash_ops.hpte_remove = pSeries_lpar_hpte_remove;
  638. mmu_hash_ops.hpte_removebolted = pSeries_lpar_hpte_removebolted;
  639. mmu_hash_ops.flush_hash_range = pSeries_lpar_flush_hash_range;
  640. mmu_hash_ops.hpte_clear_all = pseries_hpte_clear_all;
  641. mmu_hash_ops.hugepage_invalidate = pSeries_lpar_hugepage_invalidate;
  642. mmu_hash_ops.resize_hpt = pseries_lpar_resize_hpt;
  643. }
  644. void radix_init_pseries(void)
  645. {
  646. pr_info("Using radix MMU under hypervisor\n");
  647. register_process_table = pseries_lpar_register_process_table;
  648. }
  649. #ifdef CONFIG_PPC_SMLPAR
  650. #define CMO_FREE_HINT_DEFAULT 1
  651. static int cmo_free_hint_flag = CMO_FREE_HINT_DEFAULT;
  652. static int __init cmo_free_hint(char *str)
  653. {
  654. char *parm;
  655. parm = strstrip(str);
  656. if (strcasecmp(parm, "no") == 0 || strcasecmp(parm, "off") == 0) {
  657. printk(KERN_INFO "cmo_free_hint: CMO free page hinting is not active.\n");
  658. cmo_free_hint_flag = 0;
  659. return 1;
  660. }
  661. cmo_free_hint_flag = 1;
  662. printk(KERN_INFO "cmo_free_hint: CMO free page hinting is active.\n");
  663. if (strcasecmp(parm, "yes") == 0 || strcasecmp(parm, "on") == 0)
  664. return 1;
  665. return 0;
  666. }
  667. __setup("cmo_free_hint=", cmo_free_hint);
  668. static void pSeries_set_page_state(struct page *page, int order,
  669. unsigned long state)
  670. {
  671. int i, j;
  672. unsigned long cmo_page_sz, addr;
  673. cmo_page_sz = cmo_get_page_size();
  674. addr = __pa((unsigned long)page_address(page));
  675. for (i = 0; i < (1 << order); i++, addr += PAGE_SIZE) {
  676. for (j = 0; j < PAGE_SIZE; j += cmo_page_sz)
  677. plpar_hcall_norets(H_PAGE_INIT, state, addr + j, 0);
  678. }
  679. }
  680. void arch_free_page(struct page *page, int order)
  681. {
  682. if (radix_enabled())
  683. return;
  684. if (!cmo_free_hint_flag || !firmware_has_feature(FW_FEATURE_CMO))
  685. return;
  686. pSeries_set_page_state(page, order, H_PAGE_SET_UNUSED);
  687. }
  688. EXPORT_SYMBOL(arch_free_page);
  689. #endif /* CONFIG_PPC_SMLPAR */
  690. #endif /* CONFIG_PPC_STD_MMU_64 */
  691. #ifdef CONFIG_TRACEPOINTS
  692. #ifdef HAVE_JUMP_LABEL
  693. struct static_key hcall_tracepoint_key = STATIC_KEY_INIT;
  694. int hcall_tracepoint_regfunc(void)
  695. {
  696. static_key_slow_inc(&hcall_tracepoint_key);
  697. return 0;
  698. }
  699. void hcall_tracepoint_unregfunc(void)
  700. {
  701. static_key_slow_dec(&hcall_tracepoint_key);
  702. }
  703. #else
  704. /*
  705. * We optimise our hcall path by placing hcall_tracepoint_refcount
  706. * directly in the TOC so we can check if the hcall tracepoints are
  707. * enabled via a single load.
  708. */
  709. /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
  710. extern long hcall_tracepoint_refcount;
  711. int hcall_tracepoint_regfunc(void)
  712. {
  713. hcall_tracepoint_refcount++;
  714. return 0;
  715. }
  716. void hcall_tracepoint_unregfunc(void)
  717. {
  718. hcall_tracepoint_refcount--;
  719. }
  720. #endif
  721. /*
  722. * Since the tracing code might execute hcalls we need to guard against
  723. * recursion. One example of this are spinlocks calling H_YIELD on
  724. * shared processor partitions.
  725. */
  726. static DEFINE_PER_CPU(unsigned int, hcall_trace_depth);
  727. void __trace_hcall_entry(unsigned long opcode, unsigned long *args)
  728. {
  729. unsigned long flags;
  730. unsigned int *depth;
  731. /*
  732. * We cannot call tracepoints inside RCU idle regions which
  733. * means we must not trace H_CEDE.
  734. */
  735. if (opcode == H_CEDE)
  736. return;
  737. local_irq_save(flags);
  738. depth = this_cpu_ptr(&hcall_trace_depth);
  739. if (*depth)
  740. goto out;
  741. (*depth)++;
  742. preempt_disable();
  743. trace_hcall_entry(opcode, args);
  744. (*depth)--;
  745. out:
  746. local_irq_restore(flags);
  747. }
  748. void __trace_hcall_exit(long opcode, unsigned long retval,
  749. unsigned long *retbuf)
  750. {
  751. unsigned long flags;
  752. unsigned int *depth;
  753. if (opcode == H_CEDE)
  754. return;
  755. local_irq_save(flags);
  756. depth = this_cpu_ptr(&hcall_trace_depth);
  757. if (*depth)
  758. goto out;
  759. (*depth)++;
  760. trace_hcall_exit(opcode, retval, retbuf);
  761. preempt_enable();
  762. (*depth)--;
  763. out:
  764. local_irq_restore(flags);
  765. }
  766. #endif
  767. /**
  768. * h_get_mpp
  769. * H_GET_MPP hcall returns info in 7 parms
  770. */
  771. int h_get_mpp(struct hvcall_mpp_data *mpp_data)
  772. {
  773. int rc;
  774. unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
  775. rc = plpar_hcall9(H_GET_MPP, retbuf);
  776. mpp_data->entitled_mem = retbuf[0];
  777. mpp_data->mapped_mem = retbuf[1];
  778. mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
  779. mpp_data->pool_num = retbuf[2] & 0xffff;
  780. mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
  781. mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
  782. mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffffUL;
  783. mpp_data->pool_size = retbuf[4];
  784. mpp_data->loan_request = retbuf[5];
  785. mpp_data->backing_mem = retbuf[6];
  786. return rc;
  787. }
  788. EXPORT_SYMBOL(h_get_mpp);
  789. int h_get_mpp_x(struct hvcall_mpp_x_data *mpp_x_data)
  790. {
  791. int rc;
  792. unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };
  793. rc = plpar_hcall9(H_GET_MPP_X, retbuf);
  794. mpp_x_data->coalesced_bytes = retbuf[0];
  795. mpp_x_data->pool_coalesced_bytes = retbuf[1];
  796. mpp_x_data->pool_purr_cycles = retbuf[2];
  797. mpp_x_data->pool_spurr_cycles = retbuf[3];
  798. return rc;
  799. }