hash_native_64.c 14 KB

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