hash_native_64.c 19 KB

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