hash_native_64.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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/threads.h>
  16. #include <linux/smp.h>
  17. #include <asm/abs_addr.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. #ifdef DEBUG_LOW
  27. #define DBG_LOW(fmt...) udbg_printf(fmt)
  28. #else
  29. #define DBG_LOW(fmt...)
  30. #endif
  31. #define HPTE_LOCK_BIT 3
  32. static DEFINE_SPINLOCK(native_tlbie_lock);
  33. static inline void __tlbie(unsigned long va, unsigned int psize)
  34. {
  35. unsigned int penc;
  36. /* clear top 16 bits, non SLS segment */
  37. va &= ~(0xffffULL << 48);
  38. switch (psize) {
  39. case MMU_PAGE_4K:
  40. va &= ~0xffful;
  41. asm volatile("tlbie %0,0" : : "r" (va) : "memory");
  42. break;
  43. default:
  44. penc = mmu_psize_defs[psize].penc;
  45. va &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  46. va |= (0x7f >> (8 - penc)) << 12;
  47. asm volatile("tlbie %0,1" : : "r" (va) : "memory");
  48. break;
  49. }
  50. }
  51. static inline void __tlbiel(unsigned long va, unsigned int psize)
  52. {
  53. unsigned int penc;
  54. /* clear top 16 bits, non SLS segment */
  55. va &= ~(0xffffULL << 48);
  56. switch (psize) {
  57. case MMU_PAGE_4K:
  58. va &= ~0xffful;
  59. asm volatile(".long 0x7c000224 | (%0 << 11) | (0 << 21)"
  60. : : "r"(va) : "memory");
  61. break;
  62. default:
  63. penc = mmu_psize_defs[psize].penc;
  64. va &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  65. va |= (0x7f >> (8 - penc)) << 12;
  66. asm volatile(".long 0x7c000224 | (%0 << 11) | (1 << 21)"
  67. : : "r"(va) : "memory");
  68. break;
  69. }
  70. }
  71. static inline void tlbie(unsigned long va, int psize, int local)
  72. {
  73. unsigned int use_local = local && cpu_has_feature(CPU_FTR_TLBIEL);
  74. int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
  75. if (use_local)
  76. use_local = mmu_psize_defs[psize].tlbiel;
  77. if (lock_tlbie && !use_local)
  78. spin_lock(&native_tlbie_lock);
  79. asm volatile("ptesync": : :"memory");
  80. if (use_local) {
  81. __tlbiel(va, psize);
  82. asm volatile("ptesync": : :"memory");
  83. } else {
  84. __tlbie(va, psize);
  85. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  86. }
  87. if (lock_tlbie && !use_local)
  88. spin_unlock(&native_tlbie_lock);
  89. }
  90. static inline void native_lock_hpte(hpte_t *hptep)
  91. {
  92. unsigned long *word = &hptep->v;
  93. while (1) {
  94. if (!test_and_set_bit(HPTE_LOCK_BIT, word))
  95. break;
  96. while(test_bit(HPTE_LOCK_BIT, word))
  97. cpu_relax();
  98. }
  99. }
  100. static inline void native_unlock_hpte(hpte_t *hptep)
  101. {
  102. unsigned long *word = &hptep->v;
  103. asm volatile("lwsync":::"memory");
  104. clear_bit(HPTE_LOCK_BIT, word);
  105. }
  106. long native_hpte_insert(unsigned long hpte_group, unsigned long va,
  107. unsigned long pa, unsigned long rflags,
  108. unsigned long vflags, int psize)
  109. {
  110. hpte_t *hptep = htab_address + hpte_group;
  111. unsigned long hpte_v, hpte_r;
  112. int i;
  113. if (!(vflags & HPTE_V_BOLTED)) {
  114. DBG_LOW(" insert(group=%lx, va=%016lx, pa=%016lx,"
  115. " rflags=%lx, vflags=%lx, psize=%d)\n",
  116. hpte_group, va, pa, rflags, vflags, psize);
  117. }
  118. for (i = 0; i < HPTES_PER_GROUP; i++) {
  119. if (! (hptep->v & HPTE_V_VALID)) {
  120. /* retry with lock held */
  121. native_lock_hpte(hptep);
  122. if (! (hptep->v & HPTE_V_VALID))
  123. break;
  124. native_unlock_hpte(hptep);
  125. }
  126. hptep++;
  127. }
  128. if (i == HPTES_PER_GROUP)
  129. return -1;
  130. hpte_v = hpte_encode_v(va, psize) | vflags | HPTE_V_VALID;
  131. hpte_r = hpte_encode_r(pa, psize) | rflags;
  132. if (!(vflags & HPTE_V_BOLTED)) {
  133. DBG_LOW(" i=%x hpte_v=%016lx, hpte_r=%016lx\n",
  134. i, hpte_v, hpte_r);
  135. }
  136. hptep->r = hpte_r;
  137. /* Guarantee the second dword is visible before the valid bit */
  138. __asm__ __volatile__ ("eieio" : : : "memory");
  139. /*
  140. * Now set the first dword including the valid bit
  141. * NOTE: this also unlocks the hpte
  142. */
  143. hptep->v = hpte_v;
  144. __asm__ __volatile__ ("ptesync" : : : "memory");
  145. return i | (!!(vflags & HPTE_V_SECONDARY) << 3);
  146. }
  147. static long native_hpte_remove(unsigned long hpte_group)
  148. {
  149. hpte_t *hptep;
  150. int i;
  151. int slot_offset;
  152. unsigned long hpte_v;
  153. DBG_LOW(" remove(group=%lx)\n", hpte_group);
  154. /* pick a random entry to start at */
  155. slot_offset = mftb() & 0x7;
  156. for (i = 0; i < HPTES_PER_GROUP; i++) {
  157. hptep = htab_address + hpte_group + slot_offset;
  158. hpte_v = hptep->v;
  159. if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {
  160. /* retry with lock held */
  161. native_lock_hpte(hptep);
  162. hpte_v = hptep->v;
  163. if ((hpte_v & HPTE_V_VALID)
  164. && !(hpte_v & HPTE_V_BOLTED))
  165. break;
  166. native_unlock_hpte(hptep);
  167. }
  168. slot_offset++;
  169. slot_offset &= 0x7;
  170. }
  171. if (i == HPTES_PER_GROUP)
  172. return -1;
  173. /* Invalidate the hpte. NOTE: this also unlocks it */
  174. hptep->v = 0;
  175. return i;
  176. }
  177. static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
  178. unsigned long va, int psize, int local)
  179. {
  180. hpte_t *hptep = htab_address + slot;
  181. unsigned long hpte_v, want_v;
  182. int ret = 0;
  183. want_v = hpte_encode_v(va, psize);
  184. DBG_LOW(" update(va=%016lx, avpnv=%016lx, hash=%016lx, newpp=%x)",
  185. va, want_v & HPTE_V_AVPN, slot, newpp);
  186. native_lock_hpte(hptep);
  187. hpte_v = hptep->v;
  188. /* Even if we miss, we need to invalidate the TLB */
  189. if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {
  190. DBG_LOW(" -> miss\n");
  191. native_unlock_hpte(hptep);
  192. ret = -1;
  193. } else {
  194. DBG_LOW(" -> hit\n");
  195. /* Update the HPTE */
  196. hptep->r = (hptep->r & ~(HPTE_R_PP | HPTE_R_N)) |
  197. (newpp & (HPTE_R_PP | HPTE_R_N));
  198. native_unlock_hpte(hptep);
  199. }
  200. /* Ensure it is out of the tlb too. */
  201. tlbie(va, psize, local);
  202. return ret;
  203. }
  204. static long native_hpte_find(unsigned long va, int psize)
  205. {
  206. hpte_t *hptep;
  207. unsigned long hash;
  208. unsigned long i, j;
  209. long slot;
  210. unsigned long want_v, hpte_v;
  211. hash = hpt_hash(va, mmu_psize_defs[psize].shift);
  212. want_v = hpte_encode_v(va, psize);
  213. for (j = 0; j < 2; j++) {
  214. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  215. for (i = 0; i < HPTES_PER_GROUP; i++) {
  216. hptep = htab_address + slot;
  217. hpte_v = hptep->v;
  218. if (HPTE_V_COMPARE(hpte_v, want_v)
  219. && (hpte_v & HPTE_V_VALID)
  220. && ( !!(hpte_v & HPTE_V_SECONDARY) == j)) {
  221. /* HPTE matches */
  222. if (j)
  223. slot = -slot;
  224. return slot;
  225. }
  226. ++slot;
  227. }
  228. hash = ~hash;
  229. }
  230. return -1;
  231. }
  232. /*
  233. * Update the page protection bits. Intended to be used to create
  234. * guard pages for kernel data structures on pages which are bolted
  235. * in the HPT. Assumes pages being operated on will not be stolen.
  236. *
  237. * No need to lock here because we should be the only user.
  238. */
  239. static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,
  240. int psize)
  241. {
  242. unsigned long vsid, va;
  243. long slot;
  244. hpte_t *hptep;
  245. vsid = get_kernel_vsid(ea);
  246. va = (vsid << 28) | (ea & 0x0fffffff);
  247. slot = native_hpte_find(va, psize);
  248. if (slot == -1)
  249. panic("could not find page to bolt\n");
  250. hptep = htab_address + slot;
  251. /* Update the HPTE */
  252. hptep->r = (hptep->r & ~(HPTE_R_PP | HPTE_R_N)) |
  253. (newpp & (HPTE_R_PP | HPTE_R_N));
  254. /* Ensure it is out of the tlb too. */
  255. tlbie(va, psize, 0);
  256. }
  257. static void native_hpte_invalidate(unsigned long slot, unsigned long va,
  258. int psize, int local)
  259. {
  260. hpte_t *hptep = htab_address + slot;
  261. unsigned long hpte_v;
  262. unsigned long want_v;
  263. unsigned long flags;
  264. local_irq_save(flags);
  265. DBG_LOW(" invalidate(va=%016lx, hash: %x)\n", va, slot);
  266. want_v = hpte_encode_v(va, psize);
  267. native_lock_hpte(hptep);
  268. hpte_v = hptep->v;
  269. /* Even if we miss, we need to invalidate the TLB */
  270. if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))
  271. native_unlock_hpte(hptep);
  272. else
  273. /* Invalidate the hpte. NOTE: this also unlocks it */
  274. hptep->v = 0;
  275. /* Invalidate the TLB */
  276. tlbie(va, psize, local);
  277. local_irq_restore(flags);
  278. }
  279. /*
  280. * XXX This need fixing based on page size. It's only used by
  281. * native_hpte_clear() for now which needs fixing too so they
  282. * make a good pair...
  283. */
  284. static unsigned long slot2va(unsigned long hpte_v, unsigned long slot)
  285. {
  286. unsigned long avpn = HPTE_V_AVPN_VAL(hpte_v);
  287. unsigned long va;
  288. va = avpn << 23;
  289. if (! (hpte_v & HPTE_V_LARGE)) {
  290. unsigned long vpi, pteg;
  291. pteg = slot / HPTES_PER_GROUP;
  292. if (hpte_v & HPTE_V_SECONDARY)
  293. pteg = ~pteg;
  294. vpi = ((va >> 28) ^ pteg) & htab_hash_mask;
  295. va |= vpi << PAGE_SHIFT;
  296. }
  297. return va;
  298. }
  299. /*
  300. * clear all mappings on kexec. All cpus are in real mode (or they will
  301. * be when they isi), and we are the only one left. We rely on our kernel
  302. * mapping being 0xC0's and the hardware ignoring those two real bits.
  303. *
  304. * TODO: add batching support when enabled. remember, no dynamic memory here,
  305. * athough there is the control page available...
  306. *
  307. * XXX FIXME: 4k only for now !
  308. */
  309. static void native_hpte_clear(void)
  310. {
  311. unsigned long slot, slots, flags;
  312. hpte_t *hptep = htab_address;
  313. unsigned long hpte_v;
  314. unsigned long pteg_count;
  315. pteg_count = htab_hash_mask + 1;
  316. local_irq_save(flags);
  317. /* we take the tlbie lock and hold it. Some hardware will
  318. * deadlock if we try to tlbie from two processors at once.
  319. */
  320. spin_lock(&native_tlbie_lock);
  321. slots = pteg_count * HPTES_PER_GROUP;
  322. for (slot = 0; slot < slots; slot++, hptep++) {
  323. /*
  324. * we could lock the pte here, but we are the only cpu
  325. * running, right? and for crash dump, we probably
  326. * don't want to wait for a maybe bad cpu.
  327. */
  328. hpte_v = hptep->v;
  329. /*
  330. * Call __tlbie() here rather than tlbie() since we
  331. * already hold the native_tlbie_lock.
  332. */
  333. if (hpte_v & HPTE_V_VALID) {
  334. hptep->v = 0;
  335. __tlbie(slot2va(hpte_v, slot), MMU_PAGE_4K);
  336. }
  337. }
  338. asm volatile("eieio; tlbsync; ptesync":::"memory");
  339. spin_unlock(&native_tlbie_lock);
  340. local_irq_restore(flags);
  341. }
  342. /*
  343. * Batched hash table flush, we batch the tlbie's to avoid taking/releasing
  344. * the lock all the time
  345. */
  346. static void native_flush_hash_range(unsigned long number, int local)
  347. {
  348. unsigned long va, hash, index, hidx, shift, slot;
  349. hpte_t *hptep;
  350. unsigned long hpte_v;
  351. unsigned long want_v;
  352. unsigned long flags;
  353. real_pte_t pte;
  354. struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
  355. unsigned long psize = batch->psize;
  356. int i;
  357. local_irq_save(flags);
  358. for (i = 0; i < number; i++) {
  359. va = batch->vaddr[i];
  360. pte = batch->pte[i];
  361. pte_iterate_hashed_subpages(pte, psize, va, index, shift) {
  362. hash = hpt_hash(va, shift);
  363. hidx = __rpte_to_hidx(pte, index);
  364. if (hidx & _PTEIDX_SECONDARY)
  365. hash = ~hash;
  366. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  367. slot += hidx & _PTEIDX_GROUP_IX;
  368. hptep = htab_address + slot;
  369. want_v = hpte_encode_v(va, psize);
  370. native_lock_hpte(hptep);
  371. hpte_v = hptep->v;
  372. if (!HPTE_V_COMPARE(hpte_v, want_v) ||
  373. !(hpte_v & HPTE_V_VALID))
  374. native_unlock_hpte(hptep);
  375. else
  376. hptep->v = 0;
  377. } pte_iterate_hashed_end();
  378. }
  379. if (cpu_has_feature(CPU_FTR_TLBIEL) &&
  380. mmu_psize_defs[psize].tlbiel && local) {
  381. asm volatile("ptesync":::"memory");
  382. for (i = 0; i < number; i++) {
  383. va = batch->vaddr[i];
  384. pte = batch->pte[i];
  385. pte_iterate_hashed_subpages(pte, psize, va, index,
  386. shift) {
  387. __tlbiel(va, psize);
  388. } pte_iterate_hashed_end();
  389. }
  390. asm volatile("ptesync":::"memory");
  391. } else {
  392. int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
  393. if (lock_tlbie)
  394. spin_lock(&native_tlbie_lock);
  395. asm volatile("ptesync":::"memory");
  396. for (i = 0; i < number; i++) {
  397. va = batch->vaddr[i];
  398. pte = batch->pte[i];
  399. pte_iterate_hashed_subpages(pte, psize, va, index,
  400. shift) {
  401. __tlbie(va, psize);
  402. } pte_iterate_hashed_end();
  403. }
  404. asm volatile("eieio; tlbsync; ptesync":::"memory");
  405. if (lock_tlbie)
  406. spin_unlock(&native_tlbie_lock);
  407. }
  408. local_irq_restore(flags);
  409. }
  410. #ifdef CONFIG_PPC_PSERIES
  411. /* Disable TLB batching on nighthawk */
  412. static inline int tlb_batching_enabled(void)
  413. {
  414. struct device_node *root = of_find_node_by_path("/");
  415. int enabled = 1;
  416. if (root) {
  417. const char *model = get_property(root, "model", NULL);
  418. if (model && !strcmp(model, "IBM,9076-N81"))
  419. enabled = 0;
  420. of_node_put(root);
  421. }
  422. return enabled;
  423. }
  424. #else
  425. static inline int tlb_batching_enabled(void)
  426. {
  427. return 1;
  428. }
  429. #endif
  430. void hpte_init_native(void)
  431. {
  432. ppc_md.hpte_invalidate = native_hpte_invalidate;
  433. ppc_md.hpte_updatepp = native_hpte_updatepp;
  434. ppc_md.hpte_updateboltedpp = native_hpte_updateboltedpp;
  435. ppc_md.hpte_insert = native_hpte_insert;
  436. ppc_md.hpte_remove = native_hpte_remove;
  437. ppc_md.hpte_clear_all = native_hpte_clear;
  438. if (tlb_batching_enabled())
  439. ppc_md.flush_hash_range = native_flush_hash_range;
  440. htab_finish_init();
  441. }