hash_native.c 10 KB

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