hash_native.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. #include <linux/spinlock.h>
  13. #include <linux/bitops.h>
  14. #include <linux/threads.h>
  15. #include <linux/smp.h>
  16. #include <asm/abs_addr.h>
  17. #include <asm/machdep.h>
  18. #include <asm/mmu.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/tlbflush.h>
  22. #include <asm/tlb.h>
  23. #include <asm/cputable.h>
  24. #define HPTE_LOCK_BIT 3
  25. static DEFINE_SPINLOCK(native_tlbie_lock);
  26. static inline void native_lock_hpte(hpte_t *hptep)
  27. {
  28. unsigned long *word = &hptep->v;
  29. while (1) {
  30. if (!test_and_set_bit(HPTE_LOCK_BIT, word))
  31. break;
  32. while(test_bit(HPTE_LOCK_BIT, word))
  33. cpu_relax();
  34. }
  35. }
  36. static inline void native_unlock_hpte(hpte_t *hptep)
  37. {
  38. unsigned long *word = &hptep->v;
  39. asm volatile("lwsync":::"memory");
  40. clear_bit(HPTE_LOCK_BIT, word);
  41. }
  42. long native_hpte_insert(unsigned long hpte_group, unsigned long va,
  43. unsigned long prpn, unsigned long vflags,
  44. unsigned long rflags)
  45. {
  46. unsigned long arpn = physRpn_to_absRpn(prpn);
  47. hpte_t *hptep = htab_address + hpte_group;
  48. unsigned long hpte_v, hpte_r;
  49. int i;
  50. for (i = 0; i < HPTES_PER_GROUP; i++) {
  51. if (! (hptep->v & HPTE_V_VALID)) {
  52. /* retry with lock held */
  53. native_lock_hpte(hptep);
  54. if (! (hptep->v & HPTE_V_VALID))
  55. break;
  56. native_unlock_hpte(hptep);
  57. }
  58. hptep++;
  59. }
  60. if (i == HPTES_PER_GROUP)
  61. return -1;
  62. hpte_v = (va >> 23) << HPTE_V_AVPN_SHIFT | vflags | HPTE_V_VALID;
  63. if (vflags & HPTE_V_LARGE)
  64. va &= ~(1UL << HPTE_V_AVPN_SHIFT);
  65. hpte_r = (arpn << HPTE_R_RPN_SHIFT) | rflags;
  66. hptep->r = hpte_r;
  67. /* Guarantee the second dword is visible before the valid bit */
  68. __asm__ __volatile__ ("eieio" : : : "memory");
  69. /*
  70. * Now set the first dword including the valid bit
  71. * NOTE: this also unlocks the hpte
  72. */
  73. hptep->v = hpte_v;
  74. __asm__ __volatile__ ("ptesync" : : : "memory");
  75. return i | (!!(vflags & HPTE_V_SECONDARY) << 3);
  76. }
  77. static long native_hpte_remove(unsigned long hpte_group)
  78. {
  79. hpte_t *hptep;
  80. int i;
  81. int slot_offset;
  82. unsigned long hpte_v;
  83. /* pick a random entry to start at */
  84. slot_offset = mftb() & 0x7;
  85. for (i = 0; i < HPTES_PER_GROUP; i++) {
  86. hptep = htab_address + hpte_group + slot_offset;
  87. hpte_v = hptep->v;
  88. if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {
  89. /* retry with lock held */
  90. native_lock_hpte(hptep);
  91. hpte_v = hptep->v;
  92. if ((hpte_v & HPTE_V_VALID)
  93. && !(hpte_v & HPTE_V_BOLTED))
  94. break;
  95. native_unlock_hpte(hptep);
  96. }
  97. slot_offset++;
  98. slot_offset &= 0x7;
  99. }
  100. if (i == HPTES_PER_GROUP)
  101. return -1;
  102. /* Invalidate the hpte. NOTE: this also unlocks it */
  103. hptep->v = 0;
  104. return i;
  105. }
  106. static inline void set_pp_bit(unsigned long pp, hpte_t *addr)
  107. {
  108. unsigned long old;
  109. unsigned long *p = &addr->r;
  110. __asm__ __volatile__(
  111. "1: ldarx %0,0,%3\n\
  112. rldimi %0,%2,0,61\n\
  113. stdcx. %0,0,%3\n\
  114. bne 1b"
  115. : "=&r" (old), "=m" (*p)
  116. : "r" (pp), "r" (p), "m" (*p)
  117. : "cc");
  118. }
  119. /*
  120. * Only works on small pages. Yes its ugly to have to check each slot in
  121. * the group but we only use this during bootup.
  122. */
  123. static long native_hpte_find(unsigned long vpn)
  124. {
  125. hpte_t *hptep;
  126. unsigned long hash;
  127. unsigned long i, j;
  128. long slot;
  129. unsigned long hpte_v;
  130. hash = hpt_hash(vpn, 0);
  131. for (j = 0; j < 2; j++) {
  132. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  133. for (i = 0; i < HPTES_PER_GROUP; i++) {
  134. hptep = htab_address + slot;
  135. hpte_v = hptep->v;
  136. if ((HPTE_V_AVPN_VAL(hpte_v) == (vpn >> 11))
  137. && (hpte_v & HPTE_V_VALID)
  138. && ( !!(hpte_v & HPTE_V_SECONDARY) == j)) {
  139. /* HPTE matches */
  140. if (j)
  141. slot = -slot;
  142. return slot;
  143. }
  144. ++slot;
  145. }
  146. hash = ~hash;
  147. }
  148. return -1;
  149. }
  150. static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,
  151. unsigned long va, int large, int local)
  152. {
  153. hpte_t *hptep = htab_address + slot;
  154. unsigned long hpte_v;
  155. unsigned long avpn = va >> 23;
  156. int ret = 0;
  157. if (large)
  158. avpn &= ~1;
  159. native_lock_hpte(hptep);
  160. hpte_v = hptep->v;
  161. /* Even if we miss, we need to invalidate the TLB */
  162. if ((HPTE_V_AVPN_VAL(hpte_v) != avpn)
  163. || !(hpte_v & HPTE_V_VALID)) {
  164. native_unlock_hpte(hptep);
  165. ret = -1;
  166. } else {
  167. set_pp_bit(newpp, hptep);
  168. native_unlock_hpte(hptep);
  169. }
  170. /* Ensure it is out of the tlb too */
  171. if (cpu_has_feature(CPU_FTR_TLBIEL) && !large && local) {
  172. tlbiel(va);
  173. } else {
  174. int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
  175. if (lock_tlbie)
  176. spin_lock(&native_tlbie_lock);
  177. tlbie(va, large);
  178. if (lock_tlbie)
  179. spin_unlock(&native_tlbie_lock);
  180. }
  181. return ret;
  182. }
  183. /*
  184. * Update the page protection bits. Intended to be used to create
  185. * guard pages for kernel data structures on pages which are bolted
  186. * in the HPT. Assumes pages being operated on will not be stolen.
  187. * Does not work on large pages.
  188. *
  189. * No need to lock here because we should be the only user.
  190. */
  191. static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea)
  192. {
  193. unsigned long vsid, va, vpn, flags = 0;
  194. long slot;
  195. hpte_t *hptep;
  196. int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
  197. vsid = get_kernel_vsid(ea);
  198. va = (vsid << 28) | (ea & 0x0fffffff);
  199. vpn = va >> PAGE_SHIFT;
  200. slot = native_hpte_find(vpn);
  201. if (slot == -1)
  202. panic("could not find page to bolt\n");
  203. hptep = htab_address + slot;
  204. set_pp_bit(newpp, hptep);
  205. /* Ensure it is out of the tlb too */
  206. if (lock_tlbie)
  207. spin_lock_irqsave(&native_tlbie_lock, flags);
  208. tlbie(va, 0);
  209. if (lock_tlbie)
  210. spin_unlock_irqrestore(&native_tlbie_lock, flags);
  211. }
  212. static void native_hpte_invalidate(unsigned long slot, unsigned long va,
  213. int large, int local)
  214. {
  215. hpte_t *hptep = htab_address + slot;
  216. unsigned long hpte_v;
  217. unsigned long avpn = va >> 23;
  218. unsigned long flags;
  219. int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
  220. if (large)
  221. avpn &= ~1;
  222. local_irq_save(flags);
  223. native_lock_hpte(hptep);
  224. hpte_v = hptep->v;
  225. /* Even if we miss, we need to invalidate the TLB */
  226. if ((HPTE_V_AVPN_VAL(hpte_v) != avpn)
  227. || !(hpte_v & HPTE_V_VALID)) {
  228. native_unlock_hpte(hptep);
  229. } else {
  230. /* Invalidate the hpte. NOTE: this also unlocks it */
  231. hptep->v = 0;
  232. }
  233. /* Invalidate the tlb */
  234. if (cpu_has_feature(CPU_FTR_TLBIEL) && !large && local) {
  235. tlbiel(va);
  236. } else {
  237. if (lock_tlbie)
  238. spin_lock(&native_tlbie_lock);
  239. tlbie(va, large);
  240. if (lock_tlbie)
  241. spin_unlock(&native_tlbie_lock);
  242. }
  243. local_irq_restore(flags);
  244. }
  245. /*
  246. * clear all mappings on kexec. All cpus are in real mode (or they will
  247. * be when they isi), and we are the only one left. We rely on our kernel
  248. * mapping being 0xC0's and the hardware ignoring those two real bits.
  249. *
  250. * TODO: add batching support when enabled. remember, no dynamic memory here,
  251. * athough there is the control page available...
  252. */
  253. static void native_hpte_clear(void)
  254. {
  255. unsigned long slot, slots, flags;
  256. hpte_t *hptep = htab_address;
  257. unsigned long hpte_v;
  258. unsigned long pteg_count;
  259. pteg_count = htab_hash_mask + 1;
  260. local_irq_save(flags);
  261. /* we take the tlbie lock and hold it. Some hardware will
  262. * deadlock if we try to tlbie from two processors at once.
  263. */
  264. spin_lock(&native_tlbie_lock);
  265. slots = pteg_count * HPTES_PER_GROUP;
  266. for (slot = 0; slot < slots; slot++, hptep++) {
  267. /*
  268. * we could lock the pte here, but we are the only cpu
  269. * running, right? and for crash dump, we probably
  270. * don't want to wait for a maybe bad cpu.
  271. */
  272. hpte_v = hptep->v;
  273. if (hpte_v & HPTE_V_VALID) {
  274. hptep->v = 0;
  275. tlbie(slot2va(hpte_v, slot), hpte_v & HPTE_V_LARGE);
  276. }
  277. }
  278. spin_unlock(&native_tlbie_lock);
  279. local_irq_restore(flags);
  280. }
  281. static void native_flush_hash_range(unsigned long context,
  282. unsigned long number, int local)
  283. {
  284. unsigned long vsid, vpn, va, hash, secondary, slot, flags, avpn;
  285. int i, j;
  286. hpte_t *hptep;
  287. unsigned long hpte_v;
  288. struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
  289. /* XXX fix for large ptes */
  290. unsigned long large = 0;
  291. local_irq_save(flags);
  292. j = 0;
  293. for (i = 0; i < number; i++) {
  294. if (batch->addr[i] < KERNELBASE)
  295. vsid = get_vsid(context, batch->addr[i]);
  296. else
  297. vsid = get_kernel_vsid(batch->addr[i]);
  298. va = (vsid << 28) | (batch->addr[i] & 0x0fffffff);
  299. batch->vaddr[j] = va;
  300. if (large)
  301. vpn = va >> HPAGE_SHIFT;
  302. else
  303. vpn = va >> PAGE_SHIFT;
  304. hash = hpt_hash(vpn, large);
  305. secondary = (pte_val(batch->pte[i]) & _PAGE_SECONDARY) >> 15;
  306. if (secondary)
  307. hash = ~hash;
  308. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  309. slot += (pte_val(batch->pte[i]) & _PAGE_GROUP_IX) >> 12;
  310. hptep = htab_address + slot;
  311. avpn = va >> 23;
  312. if (large)
  313. avpn &= ~0x1UL;
  314. native_lock_hpte(hptep);
  315. hpte_v = hptep->v;
  316. /* Even if we miss, we need to invalidate the TLB */
  317. if ((HPTE_V_AVPN_VAL(hpte_v) != avpn)
  318. || !(hpte_v & HPTE_V_VALID)) {
  319. native_unlock_hpte(hptep);
  320. } else {
  321. /* Invalidate the hpte. NOTE: this also unlocks it */
  322. hptep->v = 0;
  323. }
  324. j++;
  325. }
  326. if (cpu_has_feature(CPU_FTR_TLBIEL) && !large && local) {
  327. asm volatile("ptesync":::"memory");
  328. for (i = 0; i < j; i++)
  329. __tlbiel(batch->vaddr[i]);
  330. asm volatile("ptesync":::"memory");
  331. } else {
  332. int lock_tlbie = !cpu_has_feature(CPU_FTR_LOCKLESS_TLBIE);
  333. if (lock_tlbie)
  334. spin_lock(&native_tlbie_lock);
  335. asm volatile("ptesync":::"memory");
  336. for (i = 0; i < j; i++)
  337. __tlbie(batch->vaddr[i], 0);
  338. asm volatile("eieio; tlbsync; ptesync":::"memory");
  339. if (lock_tlbie)
  340. spin_unlock(&native_tlbie_lock);
  341. }
  342. local_irq_restore(flags);
  343. }
  344. #ifdef CONFIG_PPC_PSERIES
  345. /* Disable TLB batching on nighthawk */
  346. static inline int tlb_batching_enabled(void)
  347. {
  348. struct device_node *root = of_find_node_by_path("/");
  349. int enabled = 1;
  350. if (root) {
  351. const char *model = get_property(root, "model", NULL);
  352. if (model && !strcmp(model, "IBM,9076-N81"))
  353. enabled = 0;
  354. of_node_put(root);
  355. }
  356. return enabled;
  357. }
  358. #else
  359. static inline int tlb_batching_enabled(void)
  360. {
  361. return 1;
  362. }
  363. #endif
  364. void hpte_init_native(void)
  365. {
  366. ppc_md.hpte_invalidate = native_hpte_invalidate;
  367. ppc_md.hpte_updatepp = native_hpte_updatepp;
  368. ppc_md.hpte_updateboltedpp = native_hpte_updateboltedpp;
  369. ppc_md.hpte_insert = native_hpte_insert;
  370. ppc_md.hpte_remove = native_hpte_remove;
  371. ppc_md.hpte_clear_all = native_hpte_clear;
  372. if (tlb_batching_enabled())
  373. ppc_md.flush_hash_range = native_flush_hash_range;
  374. htab_finish_init();
  375. }