lpar.c 27 KB

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