hash_native_64.c 19 KB

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