hash_native_64.c 14 KB

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