hash_native_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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/threads.h>
  17. #include <linux/smp.h>
  18. #include <asm/machdep.h>
  19. #include <asm/mmu.h>
  20. #include <asm/mmu_context.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/tlbflush.h>
  23. #include <asm/tlb.h>
  24. #include <asm/cputable.h>
  25. #include <asm/udbg.h>
  26. #include <asm/kexec.h>
  27. #include <asm/ppc-opcode.h>
  28. #include <misc/cxl.h>
  29. #ifdef DEBUG_LOW
  30. #define DBG_LOW(fmt...) udbg_printf(fmt)
  31. #else
  32. #define DBG_LOW(fmt...)
  33. #endif
  34. #ifdef __BIG_ENDIAN__
  35. #define HPTE_LOCK_BIT 3
  36. #else
  37. #define HPTE_LOCK_BIT (56+3)
  38. #endif
  39. DEFINE_RAW_SPINLOCK(native_tlbie_lock);
  40. static inline void __tlbie(unsigned long vpn, int psize, int apsize, int ssize)
  41. {
  42. unsigned long va;
  43. unsigned int penc;
  44. unsigned long sllp;
  45. /*
  46. * We need 14 to 65 bits of va for a tlibe of 4K page
  47. * With vpn we ignore the lower VPN_SHIFT bits already.
  48. * And top two bits are already ignored because we can
  49. * only accomadate 76 bits in a 64 bit vpn with a VPN_SHIFT
  50. * of 12.
  51. */
  52. va = vpn << VPN_SHIFT;
  53. /*
  54. * clear top 16 bits of 64bit va, non SLS segment
  55. * Older versions of the architecture (2.02 and earler) require the
  56. * masking of the top 16 bits.
  57. */
  58. va &= ~(0xffffULL << 48);
  59. switch (psize) {
  60. case MMU_PAGE_4K:
  61. /* clear out bits after (52) [0....52.....63] */
  62. va &= ~((1ul << (64 - 52)) - 1);
  63. va |= ssize << 8;
  64. sllp = ((mmu_psize_defs[apsize].sllp & SLB_VSID_L) >> 6) |
  65. ((mmu_psize_defs[apsize].sllp & SLB_VSID_LP) >> 4);
  66. va |= sllp << 5;
  67. asm volatile(ASM_FTR_IFCLR("tlbie %0,0", PPC_TLBIE(%1,%0), %2)
  68. : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
  69. : "memory");
  70. break;
  71. default:
  72. /* We need 14 to 14 + i bits of va */
  73. penc = mmu_psize_defs[psize].penc[apsize];
  74. va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
  75. va |= penc << 12;
  76. va |= ssize << 8;
  77. /*
  78. * AVAL bits:
  79. * We don't need all the bits, but rest of the bits
  80. * must be ignored by the processor.
  81. * vpn cover upto 65 bits of va. (0...65) and we need
  82. * 58..64 bits of va.
  83. */
  84. va |= (vpn & 0xfe); /* AVAL */
  85. va |= 1; /* L */
  86. asm volatile(ASM_FTR_IFCLR("tlbie %0,1", PPC_TLBIE(%1,%0), %2)
  87. : : "r" (va), "r"(0), "i" (CPU_FTR_ARCH_206)
  88. : "memory");
  89. break;
  90. }
  91. }
  92. static inline void __tlbiel(unsigned long vpn, int psize, int apsize, int ssize)
  93. {
  94. unsigned long va;
  95. unsigned int penc;
  96. unsigned long sllp;
  97. /* VPN_SHIFT can be atmost 12 */
  98. va = vpn << VPN_SHIFT;
  99. /*
  100. * clear top 16 bits of 64 bit va, non SLS segment
  101. * Older versions of the architecture (2.02 and earler) require the
  102. * masking of the top 16 bits.
  103. */
  104. va &= ~(0xffffULL << 48);
  105. switch (psize) {
  106. case MMU_PAGE_4K:
  107. /* clear out bits after(52) [0....52.....63] */
  108. va &= ~((1ul << (64 - 52)) - 1);
  109. va |= ssize << 8;
  110. sllp = ((mmu_psize_defs[apsize].sllp & SLB_VSID_L) >> 6) |
  111. ((mmu_psize_defs[apsize].sllp & SLB_VSID_LP) >> 4);
  112. va |= sllp << 5;
  113. asm volatile(".long 0x7c000224 | (%0 << 11) | (0 << 21)"
  114. : : "r"(va) : "memory");
  115. break;
  116. default:
  117. /* We need 14 to 14 + i bits of va */
  118. penc = mmu_psize_defs[psize].penc[apsize];
  119. va &= ~((1ul << mmu_psize_defs[apsize].shift) - 1);
  120. va |= penc << 12;
  121. va |= ssize << 8;
  122. /*
  123. * AVAL bits:
  124. * We don't need all the bits, but rest of the bits
  125. * must be ignored by the processor.
  126. * vpn cover upto 65 bits of va. (0...65) and we need
  127. * 58..64 bits of va.
  128. */
  129. va |= (vpn & 0xfe);
  130. va |= 1; /* L */
  131. asm volatile(".long 0x7c000224 | (%0 << 11) | (1 << 21)"
  132. : : "r"(va) : "memory");
  133. break;
  134. }
  135. }
  136. static inline void tlbie(unsigned long vpn, int psize, int apsize,
  137. int ssize, int local)
  138. {
  139. unsigned int use_local;
  140. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  141. use_local = local && mmu_has_feature(MMU_FTR_TLBIEL) && !cxl_ctx_in_use();
  142. if (use_local)
  143. use_local = mmu_psize_defs[psize].tlbiel;
  144. if (lock_tlbie && !use_local)
  145. raw_spin_lock(&native_tlbie_lock);
  146. asm volatile("ptesync": : :"memory");
  147. if (use_local) {
  148. __tlbiel(vpn, psize, apsize, ssize);
  149. asm volatile("ptesync": : :"memory");
  150. } else {
  151. __tlbie(vpn, psize, apsize, ssize);
  152. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  153. }
  154. if (lock_tlbie && !use_local)
  155. raw_spin_unlock(&native_tlbie_lock);
  156. }
  157. static inline void native_lock_hpte(struct hash_pte *hptep)
  158. {
  159. unsigned long *word = (unsigned long *)&hptep->v;
  160. while (1) {
  161. if (!test_and_set_bit_lock(HPTE_LOCK_BIT, word))
  162. break;
  163. while(test_bit(HPTE_LOCK_BIT, word))
  164. cpu_relax();
  165. }
  166. }
  167. static inline void native_unlock_hpte(struct hash_pte *hptep)
  168. {
  169. unsigned long *word = (unsigned long *)&hptep->v;
  170. clear_bit_unlock(HPTE_LOCK_BIT, word);
  171. }
  172. static long native_hpte_insert(unsigned long hpte_group, unsigned long vpn,
  173. unsigned long pa, unsigned long rflags,
  174. unsigned long vflags, int psize, int apsize, int ssize)
  175. {
  176. struct hash_pte *hptep = htab_address + hpte_group;
  177. unsigned long hpte_v, hpte_r;
  178. int i;
  179. if (!(vflags & HPTE_V_BOLTED)) {
  180. DBG_LOW(" insert(group=%lx, vpn=%016lx, pa=%016lx,"
  181. " rflags=%lx, vflags=%lx, psize=%d)\n",
  182. hpte_group, vpn, pa, rflags, vflags, psize);
  183. }
  184. for (i = 0; i < HPTES_PER_GROUP; i++) {
  185. if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID)) {
  186. /* retry with lock held */
  187. native_lock_hpte(hptep);
  188. if (! (be64_to_cpu(hptep->v) & HPTE_V_VALID))
  189. break;
  190. native_unlock_hpte(hptep);
  191. }
  192. hptep++;
  193. }
  194. if (i == HPTES_PER_GROUP)
  195. return -1;
  196. hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
  197. hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
  198. if (!(vflags & HPTE_V_BOLTED)) {
  199. DBG_LOW(" i=%x hpte_v=%016lx, hpte_r=%016lx\n",
  200. i, hpte_v, hpte_r);
  201. }
  202. hptep->r = cpu_to_be64(hpte_r);
  203. /* Guarantee the second dword is visible before the valid bit */
  204. eieio();
  205. /*
  206. * Now set the first dword including the valid bit
  207. * NOTE: this also unlocks the hpte
  208. */
  209. hptep->v = cpu_to_be64(hpte_v);
  210. __asm__ __volatile__ ("ptesync" : : : "memory");
  211. return i | (!!(vflags & HPTE_V_SECONDARY) << 3);
  212. }
  213. static long native_hpte_remove(unsigned long hpte_group)
  214. {
  215. struct hash_pte *hptep;
  216. int i;
  217. int slot_offset;
  218. unsigned long hpte_v;
  219. DBG_LOW(" remove(group=%lx)\n", hpte_group);
  220. /* pick a random entry to start at */
  221. slot_offset = mftb() & 0x7;
  222. for (i = 0; i < HPTES_PER_GROUP; i++) {
  223. hptep = htab_address + hpte_group + slot_offset;
  224. hpte_v = be64_to_cpu(hptep->v);
  225. if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {
  226. /* retry with lock held */
  227. native_lock_hpte(hptep);
  228. hpte_v = be64_to_cpu(hptep->v);
  229. if ((hpte_v & HPTE_V_VALID)
  230. && !(hpte_v & HPTE_V_BOLTED))
  231. break;
  232. native_unlock_hpte(hptep);
  233. }
  234. slot_offset++;
  235. slot_offset &= 0x7;
  236. }
  237. if (i == HPTES_PER_GROUP)
  238. return -1;
  239. /* Invalidate the hpte. NOTE: this also unlocks it */
  240. hptep->v = 0;
  241. return i;
  242. }
  243. static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
  244. unsigned long vpn, int bpsize,
  245. int apsize, int ssize, int local)
  246. {
  247. struct hash_pte *hptep = htab_address + slot;
  248. unsigned long hpte_v, want_v;
  249. int ret = 0;
  250. want_v = hpte_encode_avpn(vpn, bpsize, ssize);
  251. DBG_LOW(" update(vpn=%016lx, avpnv=%016lx, group=%lx, newpp=%lx)",
  252. vpn, want_v & HPTE_V_AVPN, slot, newpp);
  253. native_lock_hpte(hptep);
  254. hpte_v = be64_to_cpu(hptep->v);
  255. /*
  256. * We need to invalidate the TLB always because hpte_remove doesn't do
  257. * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
  258. * random entry from it. When we do that we don't invalidate the TLB
  259. * (hpte_remove) because we assume the old translation is still
  260. * technically "valid".
  261. */
  262. if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
  263. DBG_LOW(" -> miss\n");
  264. ret = -1;
  265. } else {
  266. DBG_LOW(" -> hit\n");
  267. /* Update the HPTE */
  268. hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) & ~(HPTE_R_PP | HPTE_R_N)) |
  269. (newpp & (HPTE_R_PP | HPTE_R_N | HPTE_R_C)));
  270. }
  271. native_unlock_hpte(hptep);
  272. /* Ensure it is out of the tlb too. */
  273. tlbie(vpn, bpsize, apsize, ssize, local);
  274. return ret;
  275. }
  276. static long native_hpte_find(unsigned long vpn, int psize, int ssize)
  277. {
  278. struct hash_pte *hptep;
  279. unsigned long hash;
  280. unsigned long i;
  281. long slot;
  282. unsigned long want_v, hpte_v;
  283. hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
  284. want_v = hpte_encode_avpn(vpn, psize, ssize);
  285. /* Bolted mappings are only ever in the primary group */
  286. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  287. for (i = 0; i < HPTES_PER_GROUP; i++) {
  288. hptep = htab_address + slot;
  289. hpte_v = be64_to_cpu(hptep->v);
  290. if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))
  291. /* HPTE matches */
  292. return slot;
  293. ++slot;
  294. }
  295. return -1;
  296. }
  297. /*
  298. * Update the page protection bits. Intended to be used to create
  299. * guard pages for kernel data structures on pages which are bolted
  300. * in the HPT. Assumes pages being operated on will not be stolen.
  301. *
  302. * No need to lock here because we should be the only user.
  303. */
  304. static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
  305. int psize, int ssize)
  306. {
  307. unsigned long vpn;
  308. unsigned long vsid;
  309. long slot;
  310. struct hash_pte *hptep;
  311. vsid = get_kernel_vsid(ea, ssize);
  312. vpn = hpt_vpn(ea, vsid, ssize);
  313. slot = native_hpte_find(vpn, psize, ssize);
  314. if (slot == -1)
  315. panic("could not find page to bolt\n");
  316. hptep = htab_address + slot;
  317. /* Update the HPTE */
  318. hptep->r = cpu_to_be64((be64_to_cpu(hptep->r) &
  319. ~(HPTE_R_PP | HPTE_R_N)) |
  320. (newpp & (HPTE_R_PP | HPTE_R_N)));
  321. /*
  322. * Ensure it is out of the tlb too. Bolted entries base and
  323. * actual page size will be same.
  324. */
  325. tlbie(vpn, psize, psize, ssize, 0);
  326. }
  327. static void native_hpte_invalidate(unsigned long slot, unsigned long vpn,
  328. int bpsize, int apsize, int ssize, int local)
  329. {
  330. struct hash_pte *hptep = htab_address + slot;
  331. unsigned long hpte_v;
  332. unsigned long want_v;
  333. unsigned long flags;
  334. local_irq_save(flags);
  335. DBG_LOW(" invalidate(vpn=%016lx, hash: %lx)\n", vpn, slot);
  336. want_v = hpte_encode_avpn(vpn, bpsize, ssize);
  337. native_lock_hpte(hptep);
  338. hpte_v = be64_to_cpu(hptep->v);
  339. /*
  340. * We need to invalidate the TLB always because hpte_remove doesn't do
  341. * a tlb invalidate. If a hash bucket gets full, we "evict" a more/less
  342. * random entry from it. When we do that we don't invalidate the TLB
  343. * (hpte_remove) because we assume the old translation is still
  344. * technically "valid".
  345. */
  346. if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
  347. native_unlock_hpte(hptep);
  348. else
  349. /* Invalidate the hpte. NOTE: this also unlocks it */
  350. hptep->v = 0;
  351. /* Invalidate the TLB */
  352. tlbie(vpn, bpsize, apsize, ssize, local);
  353. local_irq_restore(flags);
  354. }
  355. static void native_hugepage_invalidate(unsigned long vsid,
  356. unsigned long addr,
  357. unsigned char *hpte_slot_array,
  358. int psize, int ssize)
  359. {
  360. int i;
  361. struct hash_pte *hptep;
  362. int actual_psize = MMU_PAGE_16M;
  363. unsigned int max_hpte_count, valid;
  364. unsigned long flags, s_addr = addr;
  365. unsigned long hpte_v, want_v, shift;
  366. unsigned long hidx, vpn = 0, hash, slot;
  367. shift = mmu_psize_defs[psize].shift;
  368. max_hpte_count = 1U << (PMD_SHIFT - shift);
  369. local_irq_save(flags);
  370. for (i = 0; i < max_hpte_count; i++) {
  371. valid = hpte_valid(hpte_slot_array, i);
  372. if (!valid)
  373. continue;
  374. hidx = hpte_hash_index(hpte_slot_array, i);
  375. /* get the vpn */
  376. addr = s_addr + (i * (1ul << shift));
  377. vpn = hpt_vpn(addr, vsid, ssize);
  378. hash = hpt_hash(vpn, shift, ssize);
  379. if (hidx & _PTEIDX_SECONDARY)
  380. hash = ~hash;
  381. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  382. slot += hidx & _PTEIDX_GROUP_IX;
  383. hptep = htab_address + slot;
  384. want_v = hpte_encode_avpn(vpn, psize, ssize);
  385. native_lock_hpte(hptep);
  386. hpte_v = be64_to_cpu(hptep->v);
  387. /* Even if we miss, we need to invalidate the TLB */
  388. if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
  389. native_unlock_hpte(hptep);
  390. else
  391. /* Invalidate the hpte. NOTE: this also unlocks it */
  392. hptep->v = 0;
  393. /*
  394. * We need to do tlb invalidate for all the address, tlbie
  395. * instruction compares entry_VA in tlb with the VA specified
  396. * here
  397. */
  398. tlbie(vpn, psize, actual_psize, ssize, 0);
  399. }
  400. local_irq_restore(flags);
  401. }
  402. static inline int __hpte_actual_psize(unsigned int lp, int psize)
  403. {
  404. int i, shift;
  405. unsigned int mask;
  406. /* start from 1 ignoring MMU_PAGE_4K */
  407. for (i = 1; i < MMU_PAGE_COUNT; i++) {
  408. /* invalid penc */
  409. if (mmu_psize_defs[psize].penc[i] == -1)
  410. continue;
  411. /*
  412. * encoding bits per actual page size
  413. * PTE LP actual page size
  414. * rrrr rrrz >=8KB
  415. * rrrr rrzz >=16KB
  416. * rrrr rzzz >=32KB
  417. * rrrr zzzz >=64KB
  418. * .......
  419. */
  420. shift = mmu_psize_defs[i].shift - LP_SHIFT;
  421. if (shift > LP_BITS)
  422. shift = LP_BITS;
  423. mask = (1 << shift) - 1;
  424. if ((lp & mask) == mmu_psize_defs[psize].penc[i])
  425. return i;
  426. }
  427. return -1;
  428. }
  429. static void hpte_decode(struct hash_pte *hpte, unsigned long slot,
  430. int *psize, int *apsize, int *ssize, unsigned long *vpn)
  431. {
  432. unsigned long avpn, pteg, vpi;
  433. unsigned long hpte_v = be64_to_cpu(hpte->v);
  434. unsigned long hpte_r = be64_to_cpu(hpte->r);
  435. unsigned long vsid, seg_off;
  436. int size, a_size, shift;
  437. /* Look at the 8 bit LP value */
  438. unsigned int lp = (hpte_r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
  439. if (!(hpte_v & HPTE_V_LARGE)) {
  440. size = MMU_PAGE_4K;
  441. a_size = MMU_PAGE_4K;
  442. } else {
  443. for (size = 0; size < MMU_PAGE_COUNT; size++) {
  444. /* valid entries have a shift value */
  445. if (!mmu_psize_defs[size].shift)
  446. continue;
  447. a_size = __hpte_actual_psize(lp, size);
  448. if (a_size != -1)
  449. break;
  450. }
  451. }
  452. /* This works for all page sizes, and for 256M and 1T segments */
  453. *ssize = hpte_v >> HPTE_V_SSIZE_SHIFT;
  454. shift = mmu_psize_defs[size].shift;
  455. avpn = (HPTE_V_AVPN_VAL(hpte_v) & ~mmu_psize_defs[size].avpnm);
  456. pteg = slot / HPTES_PER_GROUP;
  457. if (hpte_v & HPTE_V_SECONDARY)
  458. pteg = ~pteg;
  459. switch (*ssize) {
  460. case MMU_SEGSIZE_256M:
  461. /* We only have 28 - 23 bits of seg_off in avpn */
  462. seg_off = (avpn & 0x1f) << 23;
  463. vsid = avpn >> 5;
  464. /* We can find more bits from the pteg value */
  465. if (shift < 23) {
  466. vpi = (vsid ^ pteg) & htab_hash_mask;
  467. seg_off |= vpi << shift;
  468. }
  469. *vpn = vsid << (SID_SHIFT - VPN_SHIFT) | seg_off >> VPN_SHIFT;
  470. break;
  471. case MMU_SEGSIZE_1T:
  472. /* We only have 40 - 23 bits of seg_off in avpn */
  473. seg_off = (avpn & 0x1ffff) << 23;
  474. vsid = avpn >> 17;
  475. if (shift < 23) {
  476. vpi = (vsid ^ (vsid << 25) ^ pteg) & htab_hash_mask;
  477. seg_off |= vpi << shift;
  478. }
  479. *vpn = vsid << (SID_SHIFT_1T - VPN_SHIFT) | seg_off >> VPN_SHIFT;
  480. break;
  481. default:
  482. *vpn = size = 0;
  483. }
  484. *psize = size;
  485. *apsize = a_size;
  486. }
  487. /*
  488. * clear all mappings on kexec. All cpus are in real mode (or they will
  489. * be when they isi), and we are the only one left. We rely on our kernel
  490. * mapping being 0xC0's and the hardware ignoring those two real bits.
  491. *
  492. * TODO: add batching support when enabled. remember, no dynamic memory here,
  493. * athough there is the control page available...
  494. */
  495. static void native_hpte_clear(void)
  496. {
  497. unsigned long vpn = 0;
  498. unsigned long slot, slots, flags;
  499. struct hash_pte *hptep = htab_address;
  500. unsigned long hpte_v;
  501. unsigned long pteg_count;
  502. int psize, apsize, ssize;
  503. pteg_count = htab_hash_mask + 1;
  504. local_irq_save(flags);
  505. /* we take the tlbie lock and hold it. Some hardware will
  506. * deadlock if we try to tlbie from two processors at once.
  507. */
  508. raw_spin_lock(&native_tlbie_lock);
  509. slots = pteg_count * HPTES_PER_GROUP;
  510. for (slot = 0; slot < slots; slot++, hptep++) {
  511. /*
  512. * we could lock the pte here, but we are the only cpu
  513. * running, right? and for crash dump, we probably
  514. * don't want to wait for a maybe bad cpu.
  515. */
  516. hpte_v = be64_to_cpu(hptep->v);
  517. /*
  518. * Call __tlbie() here rather than tlbie() since we
  519. * already hold the native_tlbie_lock.
  520. */
  521. if (hpte_v & HPTE_V_VALID) {
  522. hpte_decode(hptep, slot, &psize, &apsize, &ssize, &vpn);
  523. hptep->v = 0;
  524. __tlbie(vpn, psize, apsize, ssize);
  525. }
  526. }
  527. asm volatile("eieio; tlbsync; ptesync":::"memory");
  528. raw_spin_unlock(&native_tlbie_lock);
  529. local_irq_restore(flags);
  530. }
  531. /*
  532. * Batched hash table flush, we batch the tlbie's to avoid taking/releasing
  533. * the lock all the time
  534. */
  535. static void native_flush_hash_range(unsigned long number, int local)
  536. {
  537. unsigned long vpn;
  538. unsigned long hash, index, hidx, shift, slot;
  539. struct hash_pte *hptep;
  540. unsigned long hpte_v;
  541. unsigned long want_v;
  542. unsigned long flags;
  543. real_pte_t pte;
  544. struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
  545. unsigned long psize = batch->psize;
  546. int ssize = batch->ssize;
  547. int i;
  548. local_irq_save(flags);
  549. for (i = 0; i < number; i++) {
  550. vpn = batch->vpn[i];
  551. pte = batch->pte[i];
  552. pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
  553. hash = hpt_hash(vpn, shift, ssize);
  554. hidx = __rpte_to_hidx(pte, index);
  555. if (hidx & _PTEIDX_SECONDARY)
  556. hash = ~hash;
  557. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  558. slot += hidx & _PTEIDX_GROUP_IX;
  559. hptep = htab_address + slot;
  560. want_v = hpte_encode_avpn(vpn, psize, ssize);
  561. native_lock_hpte(hptep);
  562. hpte_v = be64_to_cpu(hptep->v);
  563. if (!HPTE_V_COMPARE(hpte_v, want_v) ||
  564. !(hpte_v & HPTE_V_VALID))
  565. native_unlock_hpte(hptep);
  566. else
  567. hptep->v = 0;
  568. } pte_iterate_hashed_end();
  569. }
  570. if (mmu_has_feature(MMU_FTR_TLBIEL) &&
  571. mmu_psize_defs[psize].tlbiel && local) {
  572. asm volatile("ptesync":::"memory");
  573. for (i = 0; i < number; i++) {
  574. vpn = batch->vpn[i];
  575. pte = batch->pte[i];
  576. pte_iterate_hashed_subpages(pte, psize,
  577. vpn, index, shift) {
  578. __tlbiel(vpn, psize, psize, ssize);
  579. } pte_iterate_hashed_end();
  580. }
  581. asm volatile("ptesync":::"memory");
  582. } else {
  583. int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
  584. if (lock_tlbie)
  585. raw_spin_lock(&native_tlbie_lock);
  586. asm volatile("ptesync":::"memory");
  587. for (i = 0; i < number; i++) {
  588. vpn = batch->vpn[i];
  589. pte = batch->pte[i];
  590. pte_iterate_hashed_subpages(pte, psize,
  591. vpn, index, shift) {
  592. __tlbie(vpn, psize, psize, ssize);
  593. } pte_iterate_hashed_end();
  594. }
  595. asm volatile("eieio; tlbsync; ptesync":::"memory");
  596. if (lock_tlbie)
  597. raw_spin_unlock(&native_tlbie_lock);
  598. }
  599. local_irq_restore(flags);
  600. }
  601. void __init hpte_init_native(void)
  602. {
  603. ppc_md.hpte_invalidate = native_hpte_invalidate;
  604. ppc_md.hpte_updatepp = native_hpte_updatepp;
  605. ppc_md.hpte_updateboltedpp = native_hpte_updateboltedpp;
  606. ppc_md.hpte_insert = native_hpte_insert;
  607. ppc_md.hpte_remove = native_hpte_remove;
  608. ppc_md.hpte_clear_all = native_hpte_clear;
  609. ppc_md.flush_hash_range = native_flush_hash_range;
  610. ppc_md.hugepage_invalidate = native_hugepage_invalidate;
  611. }