hash_native_64.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * native hashtable management.
  3. *
  4. * SMP scalability work:
  5. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #undef DEBUG_LOW
  13. #include <linux/spinlock.h>
  14. #include <linux/bitops.h>
  15. #include <linux/of.h>
  16. #include <linux/processor.h>
  17. #include <linux/threads.h>
  18. #include <linux/smp.h>
  19. #include <asm/machdep.h>
  20. #include <asm/mmu.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/trace.h>
  25. #include <asm/tlb.h>
  26. #include <asm/cputable.h>
  27. #include <asm/udbg.h>
  28. #include <asm/kexec.h>
  29. #include <asm/ppc-opcode.h>
  30. #include <misc/cxl-base.h>
  31. #ifdef DEBUG_LOW
  32. #define DBG_LOW(fmt...) udbg_printf(fmt)
  33. #else
  34. #define DBG_LOW(fmt...)
  35. #endif
  36. #ifdef __BIG_ENDIAN__
  37. #define HPTE_LOCK_BIT 3
  38. #else
  39. #define HPTE_LOCK_BIT (56+3)
  40. #endif
  41. DEFINE_RAW_SPINLOCK(native_tlbie_lock);
  42. static inline void tlbiel_hash_set_isa206(unsigned int set, unsigned int is)
  43. {
  44. unsigned long rb;
  45. rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
  46. asm volatile("tlbiel %0" : : "r" (rb));
  47. }
  48. /*
  49. * tlbiel instruction for hash, set invalidation
  50. * i.e., r=1 and is=01 or is=10 or is=11
  51. */
  52. static inline void tlbiel_hash_set_isa300(unsigned int set, unsigned int is,
  53. unsigned int pid,
  54. unsigned int ric, unsigned int prs)
  55. {
  56. unsigned long rb;
  57. unsigned long rs;
  58. unsigned int r = 0; /* hash format */
  59. rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
  60. rs = ((unsigned long)pid << PPC_BITLSHIFT(31));
  61. asm volatile(PPC_TLBIEL(%0, %1, %2, %3, %4)
  62. : : "r"(rb), "r"(rs), "i"(ric), "i"(prs), "r"(r)
  63. : "memory");
  64. }
  65. static void tlbiel_all_isa206(unsigned int num_sets, unsigned int is)
  66. {
  67. unsigned int set;
  68. asm volatile("ptesync": : :"memory");
  69. for (set = 0; set < num_sets; set++)
  70. tlbiel_hash_set_isa206(set, is);
  71. asm volatile("ptesync": : :"memory");
  72. }
  73. static void tlbiel_all_isa300(unsigned int num_sets, unsigned int is)
  74. {
  75. unsigned int set;
  76. asm volatile("ptesync": : :"memory");
  77. /*
  78. * Flush the first set of the TLB, and any caching of partition table
  79. * entries. Then flush the remaining sets of the TLB. Hash mode uses
  80. * partition scoped TLB translations.
  81. */
  82. tlbiel_hash_set_isa300(0, is, 0, 2, 0);
  83. for (set = 1; set < num_sets; set++)
  84. tlbiel_hash_set_isa300(set, is, 0, 0, 0);
  85. /*
  86. * Now invalidate the process table cache.
  87. *
  88. * From ISA v3.0B p. 1078:
  89. * The following forms are invalid.
  90. * * PRS=1, R=0, and RIC!=2 (The only process-scoped
  91. * HPT caching is of the Process Table.)
  92. */
  93. tlbiel_hash_set_isa300(0, is, 0, 2, 1);
  94. asm volatile("ptesync": : :"memory");
  95. }
  96. void hash__tlbiel_all(unsigned int action)
  97. {
  98. unsigned int is;
  99. switch (action) {
  100. case TLB_INVAL_SCOPE_GLOBAL:
  101. is = 3;
  102. break;
  103. case TLB_INVAL_SCOPE_LPID:
  104. is = 2;
  105. break;
  106. default:
  107. BUG();
  108. }
  109. if (early_cpu_has_feature(CPU_FTR_ARCH_300))
  110. tlbiel_all_isa300(POWER9_TLB_SETS_HASH, is);
  111. else if (early_cpu_has_feature(CPU_FTR_ARCH_207S))
  112. tlbiel_all_isa206(POWER8_TLB_SETS, is);
  113. else if (early_cpu_has_feature(CPU_FTR_ARCH_206))
  114. tlbiel_all_isa206(POWER7_TLB_SETS, is);
  115. else
  116. WARN(1, "%s called on pre-POWER7 CPU\n", __func__);
  117. asm volatile(PPC_INVALIDATE_ERAT "; isync" : : :"memory");
  118. }
  119. static inline unsigned long ___tlbie(unsigned long vpn, int psize,
  120. int apsize, int ssize)
  121. {
  122. unsigned long va;
  123. unsigned int penc;
  124. unsigned long sllp;
  125. /*
  126. * We need 14 to 65 bits of va for a tlibe of 4K page
  127. * With vpn we ignore the lower VPN_SHIFT bits already.
  128. * And top two bits are already ignored because we can
  129. * only accomodate 76 bits in a 64 bit vpn with a VPN_SHIFT
  130. * of 12.
  131. */
  132. va = vpn << VPN_SHIFT;
  133. /*
  134. * clear top 16 bits of 64bit va, non SLS segment
  135. * Older versions of the architecture (2.02 and earler) require the
  136. * masking of the top 16 bits.
  137. */
  138. if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
  139. va &= ~(0xffffULL << 48);
  140. switch (psize) {
  141. case MMU_PAGE_4K:
  142. /* clear out bits after (52) [0....52.....63] */
  143. va &= ~((1ul << (64 - 52)) - 1);
  144. va |= ssize << 8;
  145. sllp = get_sllp_encoding(apsize);
  146. va |= sllp << 5;
  147. asm volatile(ASM_FTR_IFCLR("tlbie %0,0", PPC_TLBIE(%1,%0), %2)
  148. : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
  149. : "memory");
  150. break;
  151. default:
  152. /* We need 14 to 14 + i bits of va */
  153. penc = mmu_psize_defs[psize].penc[apsize];
  154. va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
  155. va |= penc << 12;
  156. va |= ssize << 8;
  157. /*
  158. * AVAL bits:
  159. * We don't need all the bits, but rest of the bits
  160. * must be ignored by the processor.
  161. * vpn cover upto 65 bits of va. (0...65) and we need
  162. * 58..64 bits of va.
  163. */
  164. va |= (vpn & 0xfe); /* AVAL */
  165. va |= 1; /* L */
  166. asm volatile(ASM_FTR_IFCLR("tlbie %0,1", PPC_TLBIE(%1,%0), %2)
  167. : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
  168. : "memory");
  169. break;
  170. }
  171. return va;
  172. }
  173. static inline void fixup_tlbie(unsigned long vpn, int psize, int apsize, int ssize)
  174. {
  175. if (cpu_has_feature(CPU_FTR_P9_TLBIE_BUG)) {
  176. /* Need the extra ptesync to ensure we don't reorder tlbie*/
  177. asm volatile("ptesync": : :"memory");
  178. ___tlbie(vpn, psize, apsize, ssize);
  179. }
  180. }
  181. static inline void __tlbie(unsigned long vpn, int psize, int apsize, int ssize)
  182. {
  183. unsigned long rb;
  184. rb = ___tlbie(vpn, psize, apsize, ssize);
  185. trace_tlbie(0, 0, rb, 0, 0, 0, 0);
  186. }
  187. static inline void __tlbiel(unsigned long vpn, int psize, int apsize, int ssize)
  188. {
  189. unsigned long va;
  190. unsigned int penc;
  191. unsigned long sllp;
  192. /* VPN_SHIFT can be atmost 12 */
  193. va = vpn << VPN_SHIFT;
  194. /*
  195. * clear top 16 bits of 64 bit va, non SLS segment
  196. * Older versions of the architecture (2.02 and earler) require the
  197. * masking of the top 16 bits.
  198. */
  199. if (mmu_has_feature(MMU_FTR_TLBIE_CROP_VA))
  200. va &= ~(0xffffULL << 48);
  201. switch (psize) {
  202. case MMU_PAGE_4K:
  203. /* clear out bits after(52) [0....52.....63] */
  204. va &= ~((1ul << (64 - 52)) - 1);
  205. va |= ssize << 8;
  206. sllp = get_sllp_encoding(apsize);
  207. va |= sllp << 5;
  208. asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,0", %1)
  209. : : "r" (va), "i" (CPU_FTR_ARCH_206)
  210. : "memory");
  211. break;
  212. default:
  213. /* We need 14 to 14 + i bits of va */
  214. penc = mmu_psize_defs[psize].penc[apsize];
  215. va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
  216. va |= penc << 12;
  217. va |= ssize << 8;
  218. /*
  219. * AVAL bits:
  220. * We don't need all the bits, but rest of the bits
  221. * must be ignored by the processor.
  222. * vpn cover upto 65 bits of va. (0...65) and we need
  223. * 58..64 bits of va.
  224. */
  225. va |= (vpn & 0xfe);
  226. va |= 1; /* L */
  227. asm volatile(ASM_FTR_IFSET("tlbiel %0", "tlbiel %0,1", %1)
  228. : : "r" (va), "i" (CPU_FTR_ARCH_206)
  229. : "memory");
  230. break;
  231. }
  232. trace_tlbie(0, 1, va, 0, 0, 0, 0);
  233. }
  234. static inline void tlbie(unsigned long vpn, int psize, int apsize,
  235. int ssize, int local)
  236. {
  237. unsigned int use_local;
  238. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  239. use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) && !cxl_ctx_in_use();
  240. if (use_local)
  241. use_local = mmu_psize_defs[psize].tlbiel;
  242. if (lock_tlbie && !use_local)
  243. raw_spin_lock(&native_tlbie_lock);
  244. asm volatile("ptesync": : :"memory");
  245. if (use_local) {
  246. __tlbiel(vpn, psize, apsize, ssize);
  247. asm volatile("ptesync": : :"memory");
  248. } else {
  249. __tlbie(vpn, psize, apsize, ssize);
  250. fixup_tlbie(vpn, psize, apsize, ssize);
  251. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  252. }
  253. if (lock_tlbie && !use_local)
  254. raw_spin_unlock(&native_tlbie_lock);
  255. }
  256. static inline void native_lock_hpte(struct hash_pte *hptep)
  257. {
  258. unsigned long *word = (unsigned long *)&hptep->v;
  259. while (1) {
  260. if (!test_and_set_bit_lock(HPTE_LOCK_BIT, word))
  261. break;
  262. spin_begin();
  263. while(test_bit(HPTE_LOCK_BIT, word))
  264. spin_cpu_relax();
  265. spin_end();
  266. }
  267. }
  268. static inline void native_unlock_hpte(struct hash_pte *hptep)
  269. {
  270. unsigned long *word = (unsigned long *)&hptep->v;
  271. clear_bit_unlock(HPTE_LOCK_BIT, word);
  272. }
  273. static long native_hpte_insert(unsigned long hpte_group, unsigned long vpn,
  274. unsigned long pa, unsigned long rflags,
  275. unsigned long vflags, int psize, int apsize, int ssize)
  276. {
  277. struct hash_pte *hptep = htab_address + hpte_group;
  278. unsigned long hpte_v, hpte_r;
  279. int i;
  280. if (!(vflags & HPTE_V_BOLTED)) {
  281. DBG_LOW(" insert(group=%lx, vpn=%016lx, pa=%016lx,"
  282. " rflags=%lx, vflags=%lx, psize=%d)\n",
  283. hpte_group, vpn, pa, rflags, vflags, psize);
  284. }
  285. for (i = 0; i < HPTES_PER_GROUP; i++) {
  286. if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID)) {
  287. /* retry with lock held */
  288. native_lock_hpte(hptep);
  289. if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID))
  290. break;
  291. native_unlock_hpte(hptep);
  292. }
  293. hptep++;
  294. }
  295. if (i == HPTES_PER_GROUP)
  296. return -1;
  297. hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
  298. hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
  299. if (!(vflags & HPTE_V_BOLTED)) {
  300. DBG_LOW(" i=%x hpte_v=%016lx, hpte_r=%016lx\n",
  301. i, hpte_v, hpte_r);
  302. }
  303. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  304. hpte_r = hpte_old_to_new_r(hpte_v, hpte_r);
  305. hpte_v = hpte_old_to_new_v(hpte_v);
  306. }
  307. hptep->r = cpu_to_be64(hpte_r);
  308. /* Guarantee the second dword is visible before the valid bit */
  309. eieio();
  310. /*
  311. * Now set the first dword including the valid bit
  312. * NOTE: this also unlocks the hpte
  313. */
  314. hptep->v = cpu_to_be64(hpte_v);
  315. __asm__ __volatile__ ("ptesync" : : : "memory");
  316. return i | (!!(vflags & HPTE_V_SECONDARY) << 3);
  317. }
  318. static long native_hpte_remove(unsigned long hpte_group)
  319. {
  320. struct hash_pte *hptep;
  321. int i;
  322. int slot_offset;
  323. unsigned long hpte_v;
  324. DBG_LOW(" remove(group=%lx)\n", hpte_group);
  325. /* pick a random entry to start at */
  326. slot_offset = mftb() & 0x7;
  327. for (i = 0; i < HPTES_PER_GROUP; i++) {
  328. hptep = htab_address + hpte_group + slot_offset;
  329. hpte_v = be64_to_cpu(hptep->v);
  330. if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {
  331. /* retry with lock held */
  332. native_lock_hpte(hptep);
  333. hpte_v = be64_to_cpu(hptep->v);
  334. if ((hpte_v & HPTE_V_VALID)
  335. && !(hpte_v & HPTE_V_BOLTED))
  336. break;
  337. native_unlock_hpte(hptep);
  338. }
  339. slot_offset++;
  340. slot_offset &= 0x7;
  341. }
  342. if (i == HPTES_PER_GROUP)
  343. return -1;
  344. /* Invalidate the hpte. NOTE: this also unlocks it */
  345. hptep->v = 0;
  346. return i;
  347. }
  348. static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
  349. unsigned long vpn, int bpsize,
  350. int apsize, int ssize, unsigned long flags)
  351. {
  352. struct hash_pte *hptep = htab_address + slot;
  353. unsigned long hpte_v, want_v;
  354. int ret = 0, local = 0;
  355. want_v = hpte_encode_avpn(vpn, bpsize, ssize);
  356. DBG_LOW(" update(vpn=%016lx, avpnv=%016lx, group=%lx, newpp=%lx)",
  357. vpn, want_v & HPTE_V_AVPN, slot, newpp);
  358. hpte_v = be64_to_cpu(hptep->v);
  359. if (cpu_has_feature(CPU_FTR_ARCH_300))
  360. hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
  361. /*
  362. * We need to invalidate the TLB always because hpte_remove doesn't do
  363. * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
  364. * random entry from it. When we do that we don't invalidate the TLB
  365. * (hpte_remove) because we assume the old translation is still
  366. * technically "valid".
  367. */
  368. if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
  369. DBG_LOW(" -> miss\n");
  370. ret = -1;
  371. } else {
  372. native_lock_hpte(hptep);
  373. /* recheck with locks held */
  374. hpte_v = be64_to_cpu(hptep->v);
  375. if (cpu_has_feature(CPU_FTR_ARCH_300))
  376. hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
  377. if (unlikely(!HPTE_V_COMPARE(hpte_v, want_v) ||
  378. !(hpte_v & HPTE_V_VALID))) {
  379. ret = -1;
  380. } else {
  381. DBG_LOW(" -> hit\n");
  382. /* Update the HPTE */
  383. hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
  384. ~(HPTE_R_PPP | HPTE_R_N)) |
  385. (newpp & (HPTE_R_PPP | HPTE_R_N |
  386. HPTE_R_C)));
  387. }
  388. native_unlock_hpte(hptep);
  389. }
  390. if (flags & HPTE_LOCAL_UPDATE)
  391. local = 1;
  392. /*
  393. * Ensure it is out of the tlb too if it is not a nohpte fault
  394. */
  395. if (!(flags & HPTE_NOHPTE_UPDATE))
  396. tlbie(vpn, bpsize, apsize, ssize, local);
  397. return ret;
  398. }
  399. static long native_hpte_find(unsigned long vpn, int psize, int ssize)
  400. {
  401. struct hash_pte *hptep;
  402. unsigned long hash;
  403. unsigned long i;
  404. long slot;
  405. unsigned long want_v, hpte_v;
  406. hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
  407. want_v = hpte_encode_avpn(vpn, psize, ssize);
  408. /* Bolted mappings are only ever in the primary group */
  409. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  410. for (i = 0; i < HPTES_PER_GROUP; i++) {
  411. hptep = htab_address + slot;
  412. hpte_v = be64_to_cpu(hptep->v);
  413. if (cpu_has_feature(CPU_FTR_ARCH_300))
  414. hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
  415. if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))
  416. /* HPTE matches */
  417. return slot;
  418. ++slot;
  419. }
  420. return -1;
  421. }
  422. /*
  423. * Update the page protection bits. Intended to be used to create
  424. * guard pages for kernel data structures on pages which are bolted
  425. * in the HPT. Assumes pages being operated on will not be stolen.
  426. *
  427. * No need to lock here because we should be the only user.
  428. */
  429. static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
  430. int psize, int ssize)
  431. {
  432. unsigned long vpn;
  433. unsigned long vsid;
  434. long slot;
  435. struct hash_pte *hptep;
  436. vsid = get_kernel_vsid(ea, ssize);
  437. vpn = hpt_vpn(ea, vsid, ssize);
  438. slot = native_hpte_find(vpn, psize, ssize);
  439. if (slot == -1)
  440. panic("could not find page to bolt\n");
  441. hptep = htab_address + slot;
  442. /* Update the HPTE */
  443. hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
  444. ~(HPTE_R_PPP | HPTE_R_N)) |
  445. (newpp & (HPTE_R_PPP | HPTE_R_N)));
  446. /*
  447. * Ensure it is out of the tlb too. Bolted entries base and
  448. * actual page size will be same.
  449. */
  450. tlbie(vpn, psize, psize, ssize, 0);
  451. }
  452. /*
  453. * Remove a bolted kernel entry. Memory hotplug uses this.
  454. *
  455. * No need to lock here because we should be the only user.
  456. */
  457. static int native_hpte_removebolted(unsigned long ea, int psize, int ssize)
  458. {
  459. unsigned long vpn;
  460. unsigned long vsid;
  461. long slot;
  462. struct hash_pte *hptep;
  463. vsid = get_kernel_vsid(ea, ssize);
  464. vpn = hpt_vpn(ea, vsid, ssize);
  465. slot = native_hpte_find(vpn, psize, ssize);
  466. if (slot == -1)
  467. return -ENOENT;
  468. hptep = htab_address + slot;
  469. VM_WARN_ON(!(be64_to_cpu(hptep->v) & HPTE_V_BOLTED));
  470. /* Invalidate the hpte */
  471. hptep->v = 0;
  472. /* Invalidate the TLB */
  473. tlbie(vpn, psize, psize, ssize, 0);
  474. return 0;
  475. }
  476. static void native_hpte_invalidate(unsigned long slot, unsigned long vpn,
  477. int bpsize, int apsize, int ssize, int local)
  478. {
  479. struct hash_pte *hptep = htab_address + slot;
  480. unsigned long hpte_v;
  481. unsigned long want_v;
  482. unsigned long flags;
  483. local_irq_save(flags);
  484. DBG_LOW(" invalidate(vpn=%016lx, hash: %lx)\n", vpn, slot);
  485. want_v = hpte_encode_avpn(vpn, bpsize, ssize);
  486. native_lock_hpte(hptep);
  487. hpte_v = be64_to_cpu(hptep->v);
  488. if (cpu_has_feature(CPU_FTR_ARCH_300))
  489. hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
  490. /*
  491. * We need to invalidate the TLB always because hpte_remove doesn't do
  492. * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
  493. * random entry from it. When we do that we don't invalidate the TLB
  494. * (hpte_remove) because we assume the old translation is still
  495. * technically "valid".
  496. */
  497. if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
  498. native_unlock_hpte(hptep);
  499. else
  500. /* Invalidate the hpte. NOTE: this also unlocks it */
  501. hptep->v = 0;
  502. /* Invalidate the TLB */
  503. tlbie(vpn, bpsize, apsize, ssize, local);
  504. local_irq_restore(flags);
  505. }
  506. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  507. static void native_hugepage_invalidate(unsigned long vsid,
  508. unsigned long addr,
  509. unsigned char *hpte_slot_array,
  510. int psize, int ssize, int local)
  511. {
  512. int i;
  513. struct hash_pte *hptep;
  514. int actual_psize = MMU_PAGE_16M;
  515. unsigned int max_hpte_count, valid;
  516. unsigned long flags, s_addr = addr;
  517. unsigned long hpte_v, want_v, shift;
  518. unsigned long hidx, vpn = 0, hash, slot;
  519. shift = mmu_psize_defs[psize].shift;
  520. max_hpte_count = 1U << (PMD_SHIFT - shift);
  521. local_irq_save(flags);
  522. for (i = 0; i < max_hpte_count; i++) {
  523. valid = hpte_valid(hpte_slot_array, i);
  524. if (!valid)
  525. continue;
  526. hidx = hpte_hash_index(hpte_slot_array, i);
  527. /* get the vpn */
  528. addr = s_addr + (i * (1ul << shift));
  529. vpn = hpt_vpn(addr, vsid, ssize);
  530. hash = hpt_hash(vpn, shift, ssize);
  531. if (hidx & _PTEIDX_SECONDARY)
  532. hash = ~hash;
  533. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  534. slot += hidx & _PTEIDX_GROUP_IX;
  535. hptep = htab_address + slot;
  536. want_v = hpte_encode_avpn(vpn, psize, ssize);
  537. native_lock_hpte(hptep);
  538. hpte_v = be64_to_cpu(hptep->v);
  539. if (cpu_has_feature(CPU_FTR_ARCH_300))
  540. hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
  541. /* Even if we miss, we need to invalidate the TLB */
  542. if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
  543. native_unlock_hpte(hptep);
  544. else
  545. /* Invalidate the hpte. NOTE: this also unlocks it */
  546. hptep->v = 0;
  547. /*
  548. * We need to do tlb invalidate for all the address, tlbie
  549. * instruction compares entry_VA in tlb with the VA specified
  550. * here
  551. */
  552. tlbie(vpn, psize, actual_psize, ssize, local);
  553. }
  554. local_irq_restore(flags);
  555. }
  556. #else
  557. static void native_hugepage_invalidate(unsigned long vsid,
  558. unsigned long addr,
  559. unsigned char *hpte_slot_array,
  560. int psize, int ssize, int local)
  561. {
  562. WARN(1, "%s called without THP support\n", __func__);
  563. }
  564. #endif
  565. static void hpte_decode(struct hash_pte *hpte, unsigned long slot,
  566. int *psize, int *apsize, int *ssize, unsigned long *vpn)
  567. {
  568. unsigned long avpn, pteg, vpi;
  569. unsigned long hpte_v = be64_to_cpu(hpte->v);
  570. unsigned long hpte_r = be64_to_cpu(hpte->r);
  571. unsigned long vsid, seg_off;
  572. int size, a_size, shift;
  573. /* Look at the 8 bit LP value */
  574. unsigned int lp = (hpte_r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
  575. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  576. hpte_v = hpte_new_to_old_v(hpte_v, hpte_r);
  577. hpte_r = hpte_new_to_old_r(hpte_r);
  578. }
  579. if (!(hpte_v & HPTE_V_LARGE)) {
  580. size = MMU_PAGE_4K;
  581. a_size = MMU_PAGE_4K;
  582. } else {
  583. size = hpte_page_sizes[lp] & 0xf;
  584. a_size = hpte_page_sizes[lp] >> 4;
  585. }
  586. /* This works for all page sizes, and for 256M and 1T segments */
  587. *ssize = hpte_v >> HPTE_V_SSIZE_SHIFT;
  588. shift = mmu_psize_defs[size].shift;
  589. avpn = (HPTE_V_AVPN_VAL(hpte_v) & ~mmu_psize_defs[size].avpnm);
  590. pteg = slot / HPTES_PER_GROUP;
  591. if (hpte_v & HPTE_V_SECONDARY)
  592. pteg = ~pteg;
  593. switch (*ssize) {
  594. case MMU_SEGSIZE_256M:
  595. /* We only have 28 - 23 bits of seg_off in avpn */
  596. seg_off = (avpn & 0x1f) << 23;
  597. vsid = avpn >> 5;
  598. /* We can find more bits from the pteg value */
  599. if (shift < 23) {
  600. vpi = (vsid ^ pteg) & htab_hash_mask;
  601. seg_off |= vpi << shift;
  602. }
  603. *vpn = vsid << (SID_SHIFT - VPN_SHIFT) | seg_off >> VPN_SHIFT;
  604. break;
  605. case MMU_SEGSIZE_1T:
  606. /* We only have 40 - 23 bits of seg_off in avpn */
  607. seg_off = (avpn & 0x1ffff) << 23;
  608. vsid = avpn >> 17;
  609. if (shift < 23) {
  610. vpi = (vsid ^ (vsid << 25) ^ pteg) & htab_hash_mask;
  611. seg_off |= vpi << shift;
  612. }
  613. *vpn = vsid << (SID_SHIFT_1T - VPN_SHIFT) | seg_off >> VPN_SHIFT;
  614. break;
  615. default:
  616. *vpn = size = 0;
  617. }
  618. *psize = size;
  619. *apsize = a_size;
  620. }
  621. /*
  622. * clear all mappings on kexec. All cpus are in real mode (or they will
  623. * be when they isi), and we are the only one left. We rely on our kernel
  624. * mapping being 0xC0's and the hardware ignoring those two real bits.
  625. *
  626. * This must be called with interrupts disabled.
  627. *
  628. * Taking the native_tlbie_lock is unsafe here due to the possibility of
  629. * lockdep being on. On pre POWER5 hardware, not taking the lock could
  630. * cause deadlock. POWER5 and newer not taking the lock is fine. This only
  631. * gets called during boot before secondary CPUs have come up and during
  632. * crashdump and all bets are off anyway.
  633. *
  634. * TODO: add batching support when enabled. remember, no dynamic memory here,
  635. * although there is the control page available...
  636. */
  637. static void native_hpte_clear(void)
  638. {
  639. unsigned long vpn = 0;
  640. unsigned long slot, slots;
  641. struct hash_pte *hptep = htab_address;
  642. unsigned long hpte_v;
  643. unsigned long pteg_count;
  644. int psize, apsize, ssize;
  645. pteg_count = htab_hash_mask + 1;
  646. slots = pteg_count * HPTES_PER_GROUP;
  647. for (slot = 0; slot < slots; slot++, hptep++) {
  648. /*
  649. * we could lock the pte here, but we are the only cpu
  650. * running, right? and for crash dump, we probably
  651. * don't want to wait for a maybe bad cpu.
  652. */
  653. hpte_v = be64_to_cpu(hptep->v);
  654. /*
  655. * Call __tlbie() here rather than tlbie() since we can't take the
  656. * native_tlbie_lock.
  657. */
  658. if (hpte_v & HPTE_V_VALID) {
  659. hpte_decode(hptep, slot, &psize, &apsize, &ssize, &vpn);
  660. hptep->v = 0;
  661. ___tlbie(vpn, psize, apsize, ssize);
  662. }
  663. }
  664. asm volatile("eieio; tlbsync; ptesync":::"memory");
  665. }
  666. /*
  667. * Batched hash table flush, we batch the tlbie's to avoid taking/releasing
  668. * the lock all the time
  669. */
  670. static void native_flush_hash_range(unsigned long number, int local)
  671. {
  672. unsigned long vpn = 0;
  673. unsigned long hash, index, hidx, shift, slot;
  674. struct hash_pte *hptep;
  675. unsigned long hpte_v;
  676. unsigned long want_v;
  677. unsigned long flags;
  678. real_pte_t pte;
  679. struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
  680. unsigned long psize = batch->psize;
  681. int ssize = batch->ssize;
  682. int i;
  683. unsigned int use_local;
  684. use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) &&
  685. mmu_psize_defs[psize].tlbiel && !cxl_ctx_in_use();
  686. local_irq_save(flags);
  687. for (i = 0; i < number; i++) {
  688. vpn = batch->vpn[i];
  689. pte = batch->pte[i];
  690. pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
  691. hash = hpt_hash(vpn, shift, ssize);
  692. hidx = __rpte_to_hidx(pte, index);
  693. if (hidx & _PTEIDX_SECONDARY)
  694. hash = ~hash;
  695. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  696. slot += hidx & _PTEIDX_GROUP_IX;
  697. hptep = htab_address + slot;
  698. want_v = hpte_encode_avpn(vpn, psize, ssize);
  699. native_lock_hpte(hptep);
  700. hpte_v = be64_to_cpu(hptep->v);
  701. if (cpu_has_feature(CPU_FTR_ARCH_300))
  702. hpte_v = hpte_new_to_old_v(hpte_v,
  703. be64_to_cpu(hptep->r));
  704. if (!HPTE_V_COMPARE(hpte_v, want_v) ||
  705. !(hpte_v & HPTE_V_VALID))
  706. native_unlock_hpte(hptep);
  707. else
  708. hptep->v = 0;
  709. } pte_iterate_hashed_end();
  710. }
  711. if (use_local) {
  712. asm volatile("ptesync":::"memory");
  713. for (i = 0; i < number; i++) {
  714. vpn = batch->vpn[i];
  715. pte = batch->pte[i];
  716. pte_iterate_hashed_subpages(pte, psize,
  717. vpn, index, shift) {
  718. __tlbiel(vpn, psize, psize, ssize);
  719. } pte_iterate_hashed_end();
  720. }
  721. asm volatile("ptesync":::"memory");
  722. } else {
  723. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  724. if (lock_tlbie)
  725. raw_spin_lock(&native_tlbie_lock);
  726. asm volatile("ptesync":::"memory");
  727. for (i = 0; i < number; i++) {
  728. vpn = batch->vpn[i];
  729. pte = batch->pte[i];
  730. pte_iterate_hashed_subpages(pte, psize,
  731. vpn, index, shift) {
  732. __tlbie(vpn, psize, psize, ssize);
  733. } pte_iterate_hashed_end();
  734. }
  735. /*
  736. * Just do one more with the last used values.
  737. */
  738. fixup_tlbie(vpn, psize, psize, ssize);
  739. asm volatile("eieio; tlbsync; ptesync":::"memory");
  740. if (lock_tlbie)
  741. raw_spin_unlock(&native_tlbie_lock);
  742. }
  743. local_irq_restore(flags);
  744. }
  745. void __init hpte_init_native(void)
  746. {
  747. mmu_hash_ops.hpte_invalidate = native_hpte_invalidate;
  748. mmu_hash_ops.hpte_updatepp = native_hpte_updatepp;
  749. mmu_hash_ops.hpte_updateboltedpp = native_hpte_updateboltedpp;
  750. mmu_hash_ops.hpte_removebolted = native_hpte_removebolted;
  751. mmu_hash_ops.hpte_insert = native_hpte_insert;
  752. mmu_hash_ops.hpte_remove = native_hpte_remove;
  753. mmu_hash_ops.hpte_clear_all = native_hpte_clear;
  754. mmu_hash_ops.flush_hash_range = native_flush_hash_range;
  755. mmu_hash_ops.hugepage_invalidate = native_hugepage_invalidate;
  756. }