hash_native_64.c 14 KB

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