tsb.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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)
  20. {
  21. return (tag == (vaddr >> 22));
  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)) {
  34. ent->tag = (1UL << TSB_TAG_INVALID_BIT);
  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. unsigned long nentries, base, flags;
  43. struct tsb *tsb;
  44. int i;
  45. spin_lock_irqsave(&mm->context.lock, flags);
  46. tsb = mm->context.tsb;
  47. nentries = mm->context.tsb_nentries;
  48. if (tlb_type == cheetah_plus || tlb_type == hypervisor)
  49. base = __pa(tsb);
  50. else
  51. base = (unsigned long) tsb;
  52. for (i = 0; i < mp->tlb_nr; i++) {
  53. unsigned long v = mp->vaddrs[i];
  54. unsigned long tag, ent, hash;
  55. v &= ~0x1UL;
  56. hash = tsb_hash(v, nentries);
  57. ent = base + (hash * sizeof(struct tsb));
  58. tag = (v >> 22UL);
  59. tsb_flush(ent, tag);
  60. }
  61. spin_unlock_irqrestore(&mm->context.lock, flags);
  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 = pgprot_val(PAGE_KERNEL_LOCKED);
  70. tsb_paddr = __pa(mm->context.tsb);
  71. BUG_ON(tsb_paddr & (tsb_bytes - 1UL));
  72. /* Use the smallest page size that can map the whole TSB
  73. * in one TLB entry.
  74. */
  75. switch (tsb_bytes) {
  76. case 8192 << 0:
  77. tsb_reg = 0x0UL;
  78. #ifdef DCACHE_ALIASING_POSSIBLE
  79. base += (tsb_paddr & 8192);
  80. #endif
  81. page_sz = 8192;
  82. break;
  83. case 8192 << 1:
  84. tsb_reg = 0x1UL;
  85. page_sz = 64 * 1024;
  86. break;
  87. case 8192 << 2:
  88. tsb_reg = 0x2UL;
  89. page_sz = 64 * 1024;
  90. break;
  91. case 8192 << 3:
  92. tsb_reg = 0x3UL;
  93. page_sz = 64 * 1024;
  94. break;
  95. case 8192 << 4:
  96. tsb_reg = 0x4UL;
  97. page_sz = 512 * 1024;
  98. break;
  99. case 8192 << 5:
  100. tsb_reg = 0x5UL;
  101. page_sz = 512 * 1024;
  102. break;
  103. case 8192 << 6:
  104. tsb_reg = 0x6UL;
  105. page_sz = 512 * 1024;
  106. break;
  107. case 8192 << 7:
  108. tsb_reg = 0x7UL;
  109. page_sz = 4 * 1024 * 1024;
  110. break;
  111. default:
  112. BUG();
  113. };
  114. tte |= pte_sz_bits(page_sz);
  115. if (tlb_type == cheetah_plus || tlb_type == hypervisor) {
  116. /* Physical mapping, no locked TLB entry for TSB. */
  117. tsb_reg |= tsb_paddr;
  118. mm->context.tsb_reg_val = tsb_reg;
  119. mm->context.tsb_map_vaddr = 0;
  120. mm->context.tsb_map_pte = 0;
  121. } else {
  122. tsb_reg |= base;
  123. tsb_reg |= (tsb_paddr & (page_sz - 1UL));
  124. tte |= (tsb_paddr & ~(page_sz - 1UL));
  125. mm->context.tsb_reg_val = tsb_reg;
  126. mm->context.tsb_map_vaddr = base;
  127. mm->context.tsb_map_pte = tte;
  128. }
  129. /* Setup the Hypervisor TSB descriptor. */
  130. if (tlb_type == hypervisor) {
  131. struct hv_tsb_descr *hp = &mm->context.tsb_descr;
  132. switch (PAGE_SIZE) {
  133. case 8192:
  134. default:
  135. hp->pgsz_idx = HV_PGSZ_IDX_8K;
  136. break;
  137. case 64 * 1024:
  138. hp->pgsz_idx = HV_PGSZ_IDX_64K;
  139. break;
  140. case 512 * 1024:
  141. hp->pgsz_idx = HV_PGSZ_IDX_512K;
  142. break;
  143. case 4 * 1024 * 1024:
  144. hp->pgsz_idx = HV_PGSZ_IDX_4MB;
  145. break;
  146. };
  147. hp->assoc = 1;
  148. hp->num_ttes = tsb_bytes / 16;
  149. hp->ctx_idx = 0;
  150. switch (PAGE_SIZE) {
  151. case 8192:
  152. default:
  153. hp->pgsz_mask = HV_PGSZ_MASK_8K;
  154. break;
  155. case 64 * 1024:
  156. hp->pgsz_mask = HV_PGSZ_MASK_64K;
  157. break;
  158. case 512 * 1024:
  159. hp->pgsz_mask = HV_PGSZ_MASK_512K;
  160. break;
  161. case 4 * 1024 * 1024:
  162. hp->pgsz_mask = HV_PGSZ_MASK_4MB;
  163. break;
  164. };
  165. hp->tsb_base = tsb_paddr;
  166. hp->resv = 0;
  167. }
  168. }
  169. /* When the RSS of an address space exceeds mm->context.tsb_rss_limit,
  170. * do_sparc64_fault() invokes this routine to try and grow the TSB.
  171. *
  172. * When we reach the maximum TSB size supported, we stick ~0UL into
  173. * mm->context.tsb_rss_limit so the grow checks in update_mmu_cache()
  174. * will not trigger any longer.
  175. *
  176. * The TSB can be anywhere from 8K to 1MB in size, in increasing powers
  177. * of two. The TSB must be aligned to it's size, so f.e. a 512K TSB
  178. * must be 512K aligned.
  179. *
  180. * The idea here is to grow the TSB when the RSS of the process approaches
  181. * the number of entries that the current TSB can hold at once. Currently,
  182. * we trigger when the RSS hits 3/4 of the TSB capacity.
  183. */
  184. void tsb_grow(struct mm_struct *mm, unsigned long rss)
  185. {
  186. unsigned long max_tsb_size = 1 * 1024 * 1024;
  187. unsigned long size, old_size, flags;
  188. struct page *page;
  189. struct tsb *old_tsb, *new_tsb;
  190. if (max_tsb_size > (PAGE_SIZE << MAX_ORDER))
  191. max_tsb_size = (PAGE_SIZE << MAX_ORDER);
  192. for (size = PAGE_SIZE; size < max_tsb_size; size <<= 1UL) {
  193. unsigned long n_entries = size / sizeof(struct tsb);
  194. n_entries = (n_entries * 3) / 4;
  195. if (n_entries > rss)
  196. break;
  197. }
  198. page = alloc_pages(GFP_KERNEL, get_order(size));
  199. if (unlikely(!page))
  200. return;
  201. /* Mark all tags as invalid. */
  202. new_tsb = page_address(page);
  203. memset(new_tsb, 0x40, size);
  204. /* Ok, we are about to commit the changes. If we are
  205. * growing an existing TSB the locking is very tricky,
  206. * so WATCH OUT!
  207. *
  208. * We have to hold mm->context.lock while committing to the
  209. * new TSB, this synchronizes us with processors in
  210. * flush_tsb_user() and switch_mm() for this address space.
  211. *
  212. * But even with that lock held, processors run asynchronously
  213. * accessing the old TSB via TLB miss handling. This is OK
  214. * because those actions are just propagating state from the
  215. * Linux page tables into the TSB, page table mappings are not
  216. * being changed. If a real fault occurs, the processor will
  217. * synchronize with us when it hits flush_tsb_user(), this is
  218. * also true for the case where vmscan is modifying the page
  219. * tables. The only thing we need to be careful with is to
  220. * skip any locked TSB entries during copy_tsb().
  221. *
  222. * When we finish committing to the new TSB, we have to drop
  223. * the lock and ask all other cpus running this address space
  224. * to run tsb_context_switch() to see the new TSB table.
  225. */
  226. spin_lock_irqsave(&mm->context.lock, flags);
  227. old_tsb = mm->context.tsb;
  228. old_size = mm->context.tsb_nentries * sizeof(struct tsb);
  229. /* Handle multiple threads trying to grow the TSB at the same time.
  230. * One will get in here first, and bump the size and the RSS limit.
  231. * The others will get in here next and hit this check.
  232. */
  233. if (unlikely(old_tsb && (rss < mm->context.tsb_rss_limit))) {
  234. spin_unlock_irqrestore(&mm->context.lock, flags);
  235. free_pages((unsigned long) new_tsb, get_order(size));
  236. return;
  237. }
  238. if (size == max_tsb_size)
  239. mm->context.tsb_rss_limit = ~0UL;
  240. else
  241. mm->context.tsb_rss_limit =
  242. ((size / sizeof(struct tsb)) * 3) / 4;
  243. if (old_tsb) {
  244. extern void copy_tsb(unsigned long old_tsb_base,
  245. unsigned long old_tsb_size,
  246. unsigned long new_tsb_base,
  247. unsigned long new_tsb_size);
  248. unsigned long old_tsb_base = (unsigned long) old_tsb;
  249. unsigned long new_tsb_base = (unsigned long) new_tsb;
  250. if (tlb_type == cheetah_plus || tlb_type == hypervisor) {
  251. old_tsb_base = __pa(old_tsb_base);
  252. new_tsb_base = __pa(new_tsb_base);
  253. }
  254. copy_tsb(old_tsb_base, old_size, new_tsb_base, size);
  255. }
  256. mm->context.tsb = new_tsb;
  257. setup_tsb_params(mm, size);
  258. spin_unlock_irqrestore(&mm->context.lock, flags);
  259. /* If old_tsb is NULL, we're being invoked for the first time
  260. * from init_new_context().
  261. */
  262. if (old_tsb) {
  263. /* Reload it on the local cpu. */
  264. tsb_context_switch(mm);
  265. /* Now force other processors to do the same. */
  266. smp_tsb_sync(mm);
  267. /* Now it is safe to free the old tsb. */
  268. free_pages((unsigned long) old_tsb, get_order(old_size));
  269. }
  270. }
  271. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  272. {
  273. spin_lock_init(&mm->context.lock);
  274. mm->context.sparc64_ctx_val = 0UL;
  275. /* copy_mm() copies over the parent's mm_struct before calling
  276. * us, so we need to zero out the TSB pointer or else tsb_grow()
  277. * will be confused and think there is an older TSB to free up.
  278. */
  279. mm->context.tsb = NULL;
  280. /* If this is fork, inherit the parent's TSB size. We would
  281. * grow it to that size on the first page fault anyways.
  282. */
  283. tsb_grow(mm, get_mm_rss(mm));
  284. if (unlikely(!mm->context.tsb))
  285. return -ENOMEM;
  286. return 0;
  287. }
  288. void destroy_context(struct mm_struct *mm)
  289. {
  290. unsigned long size = mm->context.tsb_nentries * sizeof(struct tsb);
  291. unsigned long flags;
  292. free_pages((unsigned long) mm->context.tsb, get_order(size));
  293. /* We can remove these later, but for now it's useful
  294. * to catch any bogus post-destroy_context() references
  295. * to the TSB.
  296. */
  297. mm->context.tsb = NULL;
  298. mm->context.tsb_reg_val = 0UL;
  299. spin_lock_irqsave(&ctx_alloc_lock, flags);
  300. if (CTX_VALID(mm->context)) {
  301. unsigned long nr = CTX_NRBITS(mm->context);
  302. mmu_context_bmap[nr>>6] &= ~(1UL << (nr & 63));
  303. }
  304. spin_unlock_irqrestore(&ctx_alloc_lock, flags);
  305. }