hash_native_64.c 14 KB

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