hash_native_64.c 14 KB

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