tsb.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* arch/sparc64/mm/tsb.c
  2. *
  3. * Copyright (C) 2006 David S. Miller <davem@davemloft.net>
  4. */
  5. #include <linux/kernel.h>
  6. #include <asm/system.h>
  7. #include <asm/page.h>
  8. #include <asm/tlbflush.h>
  9. #include <asm/tlb.h>
  10. #include <asm/mmu_context.h>
  11. #include <asm/pgtable.h>
  12. #include <asm/tsb.h>
  13. extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
  14. static inline unsigned long tsb_hash(unsigned long vaddr, unsigned long nentries)
  15. {
  16. vaddr >>= PAGE_SHIFT;
  17. return vaddr & (nentries - 1);
  18. }
  19. static inline int tag_compare(unsigned long tag, unsigned long vaddr, unsigned long context)
  20. {
  21. return (tag == ((vaddr >> 22) | (context << 48)));
  22. }
  23. /* TSB flushes need only occur on the processor initiating the address
  24. * space modification, not on each cpu the address space has run on.
  25. * Only the TLB flush needs that treatment.
  26. */
  27. void flush_tsb_kernel_range(unsigned long start, unsigned long end)
  28. {
  29. unsigned long v;
  30. for (v = start; v < end; v += PAGE_SIZE) {
  31. unsigned long hash = tsb_hash(v, KERNEL_TSB_NENTRIES);
  32. struct tsb *ent = &swapper_tsb[hash];
  33. if (tag_compare(ent->tag, v, 0)) {
  34. ent->tag = 0UL;
  35. membar_storeload_storestore();
  36. }
  37. }
  38. }
  39. void flush_tsb_user(struct mmu_gather *mp)
  40. {
  41. struct mm_struct *mm = mp->mm;
  42. struct tsb *tsb = mm->context.tsb;
  43. unsigned long nentries = mm->context.tsb_nentries;
  44. unsigned long ctx, base;
  45. int i;
  46. if (unlikely(!CTX_VALID(mm->context)))
  47. return;
  48. ctx = CTX_HWBITS(mm->context);
  49. if (tlb_type == cheetah_plus)
  50. base = __pa(tsb);
  51. else
  52. base = (unsigned long) tsb;
  53. for (i = 0; i < mp->tlb_nr; i++) {
  54. unsigned long v = mp->vaddrs[i];
  55. unsigned long tag, ent, hash;
  56. v &= ~0x1UL;
  57. hash = tsb_hash(v, nentries);
  58. ent = base + (hash * sizeof(struct tsb));
  59. tag = (v >> 22UL) | (ctx << 48UL);
  60. tsb_flush(ent, tag);
  61. }
  62. }
  63. static void setup_tsb_params(struct mm_struct *mm, unsigned long tsb_bytes)
  64. {
  65. unsigned long tsb_reg, base, tsb_paddr;
  66. unsigned long page_sz, tte;
  67. mm->context.tsb_nentries = tsb_bytes / sizeof(struct tsb);
  68. base = TSBMAP_BASE;
  69. tte = (_PAGE_VALID | _PAGE_L | _PAGE_CP |
  70. _PAGE_CV | _PAGE_P | _PAGE_W);
  71. tsb_paddr = __pa(mm->context.tsb);
  72. BUG_ON(tsb_paddr & (tsb_bytes - 1UL));
  73. /* Use the smallest page size that can map the whole TSB
  74. * in one TLB entry.
  75. */
  76. switch (tsb_bytes) {
  77. case 8192 << 0:
  78. tsb_reg = 0x0UL;
  79. #ifdef DCACHE_ALIASING_POSSIBLE
  80. base += (tsb_paddr & 8192);
  81. #endif
  82. tte |= _PAGE_SZ8K;
  83. page_sz = 8192;
  84. break;
  85. case 8192 << 1:
  86. tsb_reg = 0x1UL;
  87. tte |= _PAGE_SZ64K;
  88. page_sz = 64 * 1024;
  89. break;
  90. case 8192 << 2:
  91. tsb_reg = 0x2UL;
  92. tte |= _PAGE_SZ64K;
  93. page_sz = 64 * 1024;
  94. break;
  95. case 8192 << 3:
  96. tsb_reg = 0x3UL;
  97. tte |= _PAGE_SZ64K;
  98. page_sz = 64 * 1024;
  99. break;
  100. case 8192 << 4:
  101. tsb_reg = 0x4UL;
  102. tte |= _PAGE_SZ512K;
  103. page_sz = 512 * 1024;
  104. break;
  105. case 8192 << 5:
  106. tsb_reg = 0x5UL;
  107. tte |= _PAGE_SZ512K;
  108. page_sz = 512 * 1024;
  109. break;
  110. case 8192 << 6:
  111. tsb_reg = 0x6UL;
  112. tte |= _PAGE_SZ512K;
  113. page_sz = 512 * 1024;
  114. break;
  115. case 8192 << 7:
  116. tsb_reg = 0x7UL;
  117. tte |= _PAGE_SZ4MB;
  118. page_sz = 4 * 1024 * 1024;
  119. break;
  120. default:
  121. BUG();
  122. };
  123. if (tlb_type == cheetah_plus || tlb_type == hypervisor) {
  124. /* Physical mapping, no locked TLB entry for TSB. */
  125. tsb_reg |= tsb_paddr;
  126. mm->context.tsb_reg_val = tsb_reg;
  127. mm->context.tsb_map_vaddr = 0;
  128. mm->context.tsb_map_pte = 0;
  129. } else {
  130. tsb_reg |= base;
  131. tsb_reg |= (tsb_paddr & (page_sz - 1UL));
  132. tte |= (tsb_paddr & ~(page_sz - 1UL));
  133. mm->context.tsb_reg_val = tsb_reg;
  134. mm->context.tsb_map_vaddr = base;
  135. mm->context.tsb_map_pte = tte;
  136. }
  137. /* Setup the Hypervisor TSB descriptor. */
  138. if (tlb_type == hypervisor) {
  139. struct hv_tsb_descr *hp = &mm->context.tsb_descr;
  140. switch (PAGE_SIZE) {
  141. case 8192:
  142. default:
  143. hp->pgsz_idx = HV_PGSZ_IDX_8K;
  144. break;
  145. case 64 * 1024:
  146. hp->pgsz_idx = HV_PGSZ_IDX_64K;
  147. break;
  148. case 512 * 1024:
  149. hp->pgsz_idx = HV_PGSZ_IDX_512K;
  150. break;
  151. case 4 * 1024 * 1024:
  152. hp->pgsz_idx = HV_PGSZ_IDX_4MB;
  153. break;
  154. };
  155. hp->assoc = 1;
  156. hp->num_ttes = tsb_bytes / 16;
  157. hp->ctx_idx = 0;
  158. switch (PAGE_SIZE) {
  159. case 8192:
  160. default:
  161. hp->pgsz_mask = HV_PGSZ_MASK_8K;
  162. break;
  163. case 64 * 1024:
  164. hp->pgsz_mask = HV_PGSZ_MASK_64K;
  165. break;
  166. case 512 * 1024:
  167. hp->pgsz_mask = HV_PGSZ_MASK_512K;
  168. break;
  169. case 4 * 1024 * 1024:
  170. hp->pgsz_mask = HV_PGSZ_MASK_4MB;
  171. break;
  172. };
  173. hp->tsb_base = tsb_paddr;
  174. hp->resv = 0;
  175. }
  176. }
  177. /* The page tables are locked against modifications while this
  178. * runs.
  179. *
  180. * XXX do some prefetching...
  181. */
  182. static void copy_tsb(struct tsb *old_tsb, unsigned long old_size,
  183. struct tsb *new_tsb, unsigned long new_size)
  184. {
  185. unsigned long old_nentries = old_size / sizeof(struct tsb);
  186. unsigned long new_nentries = new_size / sizeof(struct tsb);
  187. unsigned long i;
  188. for (i = 0; i < old_nentries; i++) {
  189. register unsigned long tag asm("o4");
  190. register unsigned long pte asm("o5");
  191. unsigned long v, hash;
  192. if (tlb_type == hypervisor) {
  193. __asm__ __volatile__(
  194. "ldda [%2] %3, %0"
  195. : "=r" (tag), "=r" (pte)
  196. : "r" (__pa(&old_tsb[i])),
  197. "i" (ASI_QUAD_LDD_PHYS_4V));
  198. } else if (tlb_type == cheetah_plus) {
  199. __asm__ __volatile__(
  200. "ldda [%2] %3, %0"
  201. : "=r" (tag), "=r" (pte)
  202. : "r" (__pa(&old_tsb[i])),
  203. "i" (ASI_QUAD_LDD_PHYS));
  204. } else {
  205. __asm__ __volatile__(
  206. "ldda [%2] %3, %0"
  207. : "=r" (tag), "=r" (pte)
  208. : "r" (&old_tsb[i]),
  209. "i" (ASI_NUCLEUS_QUAD_LDD));
  210. }
  211. if (!tag || (tag & (1UL << TSB_TAG_LOCK_BIT)))
  212. continue;
  213. /* We only put base page size PTEs into the TSB,
  214. * but that might change in the future. This code
  215. * would need to be changed if we start putting larger
  216. * page size PTEs into there.
  217. */
  218. WARN_ON((pte & _PAGE_ALL_SZ_BITS) != _PAGE_SZBITS);
  219. /* The tag holds bits 22 to 63 of the virtual address
  220. * and the context. Clear out the context, and shift
  221. * up to make a virtual address.
  222. */
  223. v = (tag & ((1UL << 42UL) - 1UL)) << 22UL;
  224. /* The implied bits of the tag (bits 13 to 21) are
  225. * determined by the TSB entry index, so fill that in.
  226. */
  227. v |= (i & (512UL - 1UL)) << 13UL;
  228. hash = tsb_hash(v, new_nentries);
  229. if (tlb_type == cheetah_plus ||
  230. tlb_type == hypervisor) {
  231. __asm__ __volatile__(
  232. "stxa %0, [%1] %2\n\t"
  233. "stxa %3, [%4] %2"
  234. : /* no outputs */
  235. : "r" (tag),
  236. "r" (__pa(&new_tsb[hash].tag)),
  237. "i" (ASI_PHYS_USE_EC),
  238. "r" (pte),
  239. "r" (__pa(&new_tsb[hash].pte)));
  240. } else {
  241. new_tsb[hash].tag = tag;
  242. new_tsb[hash].pte = pte;
  243. }
  244. }
  245. }
  246. /* When the RSS of an address space exceeds mm->context.tsb_rss_limit,
  247. * update_mmu_cache() invokes this routine to try and grow the TSB.
  248. * When we reach the maximum TSB size supported, we stick ~0UL into
  249. * mm->context.tsb_rss_limit so the grow checks in update_mmu_cache()
  250. * will not trigger any longer.
  251. *
  252. * The TSB can be anywhere from 8K to 1MB in size, in increasing powers
  253. * of two. The TSB must be aligned to it's size, so f.e. a 512K TSB
  254. * must be 512K aligned.
  255. *
  256. * The idea here is to grow the TSB when the RSS of the process approaches
  257. * the number of entries that the current TSB can hold at once. Currently,
  258. * we trigger when the RSS hits 3/4 of the TSB capacity.
  259. */
  260. void tsb_grow(struct mm_struct *mm, unsigned long rss, gfp_t gfp_flags)
  261. {
  262. unsigned long max_tsb_size = 1 * 1024 * 1024;
  263. unsigned long size, old_size;
  264. struct page *page;
  265. struct tsb *old_tsb;
  266. if (max_tsb_size > (PAGE_SIZE << MAX_ORDER))
  267. max_tsb_size = (PAGE_SIZE << MAX_ORDER);
  268. for (size = PAGE_SIZE; size < max_tsb_size; size <<= 1UL) {
  269. unsigned long n_entries = size / sizeof(struct tsb);
  270. n_entries = (n_entries * 3) / 4;
  271. if (n_entries > rss)
  272. break;
  273. }
  274. page = alloc_pages(gfp_flags | __GFP_ZERO, get_order(size));
  275. if (unlikely(!page))
  276. return;
  277. if (size == max_tsb_size)
  278. mm->context.tsb_rss_limit = ~0UL;
  279. else
  280. mm->context.tsb_rss_limit =
  281. ((size / sizeof(struct tsb)) * 3) / 4;
  282. old_tsb = mm->context.tsb;
  283. old_size = mm->context.tsb_nentries * sizeof(struct tsb);
  284. if (old_tsb)
  285. copy_tsb(old_tsb, old_size, page_address(page), size);
  286. mm->context.tsb = page_address(page);
  287. setup_tsb_params(mm, size);
  288. /* If old_tsb is NULL, we're being invoked for the first time
  289. * from init_new_context().
  290. */
  291. if (old_tsb) {
  292. /* Now force all other processors to reload the new
  293. * TSB state.
  294. */
  295. smp_tsb_sync(mm);
  296. /* Finally reload it on the local cpu. No further
  297. * references will remain to the old TSB and we can
  298. * thus free it up.
  299. */
  300. tsb_context_switch(mm);
  301. free_pages((unsigned long) old_tsb, get_order(old_size));
  302. }
  303. }
  304. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  305. {
  306. mm->context.sparc64_ctx_val = 0UL;
  307. /* copy_mm() copies over the parent's mm_struct before calling
  308. * us, so we need to zero out the TSB pointer or else tsb_grow()
  309. * will be confused and think there is an older TSB to free up.
  310. */
  311. mm->context.tsb = NULL;
  312. tsb_grow(mm, 0, GFP_KERNEL);
  313. if (unlikely(!mm->context.tsb))
  314. return -ENOMEM;
  315. return 0;
  316. }
  317. void destroy_context(struct mm_struct *mm)
  318. {
  319. unsigned long size = mm->context.tsb_nentries * sizeof(struct tsb);
  320. free_pages((unsigned long) mm->context.tsb, get_order(size));
  321. /* We can remove these later, but for now it's useful
  322. * to catch any bogus post-destroy_context() references
  323. * to the TSB.
  324. */
  325. mm->context.tsb = NULL;
  326. mm->context.tsb_reg_val = 0UL;
  327. spin_lock(&ctx_alloc_lock);
  328. if (CTX_VALID(mm->context)) {
  329. unsigned long nr = CTX_NRBITS(mm->context);
  330. mmu_context_bmap[nr>>6] &= ~(1UL << (nr & 63));
  331. }
  332. spin_unlock(&ctx_alloc_lock);
  333. }