hash_native_64.c 13 KB

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