blob: 1af797a0a0924a9f1396b4a8274df967c87c9dce [file] [log] [blame]
David S. Miller74bf4312006-01-31 18:29:18 -08001/* arch/sparc64/mm/tsb.c
2 *
3 * Copyright (C) 2006 David S. Miller <davem@davemloft.net>
4 */
5
6#include <linux/kernel.h>
7#include <asm/system.h>
8#include <asm/page.h>
9#include <asm/tlbflush.h>
10#include <asm/tlb.h>
David S. Miller09f94282006-01-31 18:31:06 -080011#include <asm/mmu_context.h>
David S. Miller98c55842006-01-31 18:31:20 -080012#include <asm/pgtable.h>
David S. Millerbd407912006-01-31 18:31:38 -080013#include <asm/tsb.h>
David S. Miller9b4006d2006-03-18 18:12:42 -080014#include <asm/oplib.h>
David S. Miller74bf4312006-01-31 18:29:18 -080015
David S. Miller74bf4312006-01-31 18:29:18 -080016extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
17
David S. Miller98c55842006-01-31 18:31:20 -080018static inline unsigned long tsb_hash(unsigned long vaddr, unsigned long nentries)
David S. Miller74bf4312006-01-31 18:29:18 -080019{
20 vaddr >>= PAGE_SHIFT;
David S. Miller98c55842006-01-31 18:31:20 -080021 return vaddr & (nentries - 1);
David S. Miller74bf4312006-01-31 18:29:18 -080022}
23
David S. Miller8b234272006-02-17 18:01:02 -080024static inline int tag_compare(unsigned long tag, unsigned long vaddr)
David S. Miller74bf4312006-01-31 18:29:18 -080025{
David S. Miller8b234272006-02-17 18:01:02 -080026 return (tag == (vaddr >> 22));
David S. Miller74bf4312006-01-31 18:29:18 -080027}
28
29/* TSB flushes need only occur on the processor initiating the address
30 * space modification, not on each cpu the address space has run on.
31 * Only the TLB flush needs that treatment.
32 */
33
34void flush_tsb_kernel_range(unsigned long start, unsigned long end)
35{
36 unsigned long v;
37
38 for (v = start; v < end; v += PAGE_SIZE) {
David S. Miller98c55842006-01-31 18:31:20 -080039 unsigned long hash = tsb_hash(v, KERNEL_TSB_NENTRIES);
40 struct tsb *ent = &swapper_tsb[hash];
David S. Miller74bf4312006-01-31 18:29:18 -080041
David S. Miller8b234272006-02-17 18:01:02 -080042 if (tag_compare(ent->tag, v)) {
43 ent->tag = (1UL << TSB_TAG_INVALID_BIT);
David S. Miller74bf4312006-01-31 18:29:18 -080044 membar_storeload_storestore();
45 }
46 }
47}
48
49void flush_tsb_user(struct mmu_gather *mp)
50{
51 struct mm_struct *mm = mp->mm;
David S. Miller7a1ac522006-03-16 02:02:32 -080052 unsigned long nentries, base, flags;
53 struct tsb *tsb;
David S. Miller74bf4312006-01-31 18:29:18 -080054 int i;
55
David S. Miller7a1ac522006-03-16 02:02:32 -080056 spin_lock_irqsave(&mm->context.lock, flags);
57
58 tsb = mm->context.tsb;
59 nentries = mm->context.tsb_nentries;
60
David S. Millerde635d82006-02-15 21:01:31 -080061 if (tlb_type == cheetah_plus || tlb_type == hypervisor)
David S. Miller517af332006-02-01 15:55:21 -080062 base = __pa(tsb);
63 else
64 base = (unsigned long) tsb;
65
David S. Miller74bf4312006-01-31 18:29:18 -080066 for (i = 0; i < mp->tlb_nr; i++) {
67 unsigned long v = mp->vaddrs[i];
David S. Miller517af332006-02-01 15:55:21 -080068 unsigned long tag, ent, hash;
David S. Miller74bf4312006-01-31 18:29:18 -080069
70 v &= ~0x1UL;
71
David S. Miller517af332006-02-01 15:55:21 -080072 hash = tsb_hash(v, nentries);
73 ent = base + (hash * sizeof(struct tsb));
David S. Miller8b234272006-02-17 18:01:02 -080074 tag = (v >> 22UL);
David S. Miller517af332006-02-01 15:55:21 -080075
76 tsb_flush(ent, tag);
David S. Miller74bf4312006-01-31 18:29:18 -080077 }
David S. Miller7a1ac522006-03-16 02:02:32 -080078
79 spin_unlock_irqrestore(&mm->context.lock, flags);
David S. Miller74bf4312006-01-31 18:29:18 -080080}
David S. Miller09f94282006-01-31 18:31:06 -080081
David S. Miller98c55842006-01-31 18:31:20 -080082static void setup_tsb_params(struct mm_struct *mm, unsigned long tsb_bytes)
83{
84 unsigned long tsb_reg, base, tsb_paddr;
85 unsigned long page_sz, tte;
86
87 mm->context.tsb_nentries = tsb_bytes / sizeof(struct tsb);
88
89 base = TSBMAP_BASE;
David S. Millerc4bce902006-02-11 21:57:54 -080090 tte = pgprot_val(PAGE_KERNEL_LOCKED);
David S. Miller98c55842006-01-31 18:31:20 -080091 tsb_paddr = __pa(mm->context.tsb);
David S. Miller517af332006-02-01 15:55:21 -080092 BUG_ON(tsb_paddr & (tsb_bytes - 1UL));
David S. Miller98c55842006-01-31 18:31:20 -080093
94 /* Use the smallest page size that can map the whole TSB
95 * in one TLB entry.
96 */
97 switch (tsb_bytes) {
98 case 8192 << 0:
99 tsb_reg = 0x0UL;
100#ifdef DCACHE_ALIASING_POSSIBLE
101 base += (tsb_paddr & 8192);
102#endif
David S. Miller98c55842006-01-31 18:31:20 -0800103 page_sz = 8192;
104 break;
105
106 case 8192 << 1:
107 tsb_reg = 0x1UL;
David S. Miller98c55842006-01-31 18:31:20 -0800108 page_sz = 64 * 1024;
109 break;
110
111 case 8192 << 2:
112 tsb_reg = 0x2UL;
David S. Miller98c55842006-01-31 18:31:20 -0800113 page_sz = 64 * 1024;
114 break;
115
116 case 8192 << 3:
117 tsb_reg = 0x3UL;
David S. Miller98c55842006-01-31 18:31:20 -0800118 page_sz = 64 * 1024;
119 break;
120
121 case 8192 << 4:
122 tsb_reg = 0x4UL;
David S. Miller98c55842006-01-31 18:31:20 -0800123 page_sz = 512 * 1024;
124 break;
125
126 case 8192 << 5:
127 tsb_reg = 0x5UL;
David S. Miller98c55842006-01-31 18:31:20 -0800128 page_sz = 512 * 1024;
129 break;
130
131 case 8192 << 6:
132 tsb_reg = 0x6UL;
David S. Miller98c55842006-01-31 18:31:20 -0800133 page_sz = 512 * 1024;
134 break;
135
136 case 8192 << 7:
137 tsb_reg = 0x7UL;
David S. Miller98c55842006-01-31 18:31:20 -0800138 page_sz = 4 * 1024 * 1024;
139 break;
David S. Millerbd407912006-01-31 18:31:38 -0800140
141 default:
142 BUG();
David S. Miller98c55842006-01-31 18:31:20 -0800143 };
David S. Millerc4bce902006-02-11 21:57:54 -0800144 tte |= pte_sz_bits(page_sz);
David S. Miller98c55842006-01-31 18:31:20 -0800145
David S. Miller618e9ed2006-02-09 17:21:53 -0800146 if (tlb_type == cheetah_plus || tlb_type == hypervisor) {
David S. Miller517af332006-02-01 15:55:21 -0800147 /* Physical mapping, no locked TLB entry for TSB. */
148 tsb_reg |= tsb_paddr;
David S. Miller98c55842006-01-31 18:31:20 -0800149
David S. Miller517af332006-02-01 15:55:21 -0800150 mm->context.tsb_reg_val = tsb_reg;
151 mm->context.tsb_map_vaddr = 0;
152 mm->context.tsb_map_pte = 0;
153 } else {
154 tsb_reg |= base;
155 tsb_reg |= (tsb_paddr & (page_sz - 1UL));
156 tte |= (tsb_paddr & ~(page_sz - 1UL));
157
158 mm->context.tsb_reg_val = tsb_reg;
159 mm->context.tsb_map_vaddr = base;
160 mm->context.tsb_map_pte = tte;
161 }
162
David S. Miller618e9ed2006-02-09 17:21:53 -0800163 /* Setup the Hypervisor TSB descriptor. */
164 if (tlb_type == hypervisor) {
165 struct hv_tsb_descr *hp = &mm->context.tsb_descr;
166
167 switch (PAGE_SIZE) {
168 case 8192:
169 default:
170 hp->pgsz_idx = HV_PGSZ_IDX_8K;
171 break;
172
173 case 64 * 1024:
174 hp->pgsz_idx = HV_PGSZ_IDX_64K;
175 break;
176
177 case 512 * 1024:
178 hp->pgsz_idx = HV_PGSZ_IDX_512K;
179 break;
180
181 case 4 * 1024 * 1024:
182 hp->pgsz_idx = HV_PGSZ_IDX_4MB;
183 break;
184 };
185 hp->assoc = 1;
186 hp->num_ttes = tsb_bytes / 16;
187 hp->ctx_idx = 0;
188 switch (PAGE_SIZE) {
189 case 8192:
190 default:
191 hp->pgsz_mask = HV_PGSZ_MASK_8K;
192 break;
193
194 case 64 * 1024:
195 hp->pgsz_mask = HV_PGSZ_MASK_64K;
196 break;
197
198 case 512 * 1024:
199 hp->pgsz_mask = HV_PGSZ_MASK_512K;
200 break;
201
202 case 4 * 1024 * 1024:
203 hp->pgsz_mask = HV_PGSZ_MASK_4MB;
204 break;
205 };
206 hp->tsb_base = tsb_paddr;
207 hp->resv = 0;
208 }
David S. Miller98c55842006-01-31 18:31:20 -0800209}
210
David S. Miller9b4006d2006-03-18 18:12:42 -0800211static kmem_cache_t *tsb_caches[8] __read_mostly;
212
213static const char *tsb_cache_names[8] = {
214 "tsb_8KB",
215 "tsb_16KB",
216 "tsb_32KB",
217 "tsb_64KB",
218 "tsb_128KB",
219 "tsb_256KB",
220 "tsb_512KB",
221 "tsb_1MB",
222};
223
224void __init tsb_cache_init(void)
225{
226 unsigned long i;
227
228 for (i = 0; i < 8; i++) {
229 unsigned long size = 8192 << i;
230 const char *name = tsb_cache_names[i];
231
232 tsb_caches[i] = kmem_cache_create(name,
233 size, size,
234 SLAB_HWCACHE_ALIGN |
235 SLAB_MUST_HWCACHE_ALIGN,
236 NULL, NULL);
237 if (!tsb_caches[i]) {
238 prom_printf("Could not create %s cache\n", name);
239 prom_halt();
240 }
241 }
242}
243
David S. Millerbd407912006-01-31 18:31:38 -0800244/* When the RSS of an address space exceeds mm->context.tsb_rss_limit,
David S. Miller7a1ac522006-03-16 02:02:32 -0800245 * do_sparc64_fault() invokes this routine to try and grow the TSB.
246 *
David S. Millerbd407912006-01-31 18:31:38 -0800247 * When we reach the maximum TSB size supported, we stick ~0UL into
248 * mm->context.tsb_rss_limit so the grow checks in update_mmu_cache()
249 * will not trigger any longer.
250 *
251 * The TSB can be anywhere from 8K to 1MB in size, in increasing powers
252 * of two. The TSB must be aligned to it's size, so f.e. a 512K TSB
David S. Millerb52439c2006-03-17 23:40:47 -0800253 * must be 512K aligned. It also must be physically contiguous, so we
254 * cannot use vmalloc().
David S. Millerbd407912006-01-31 18:31:38 -0800255 *
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 */
David S. Miller7a1ac522006-03-16 02:02:32 -0800260void tsb_grow(struct mm_struct *mm, unsigned long rss)
David S. Millerbd407912006-01-31 18:31:38 -0800261{
262 unsigned long max_tsb_size = 1 * 1024 * 1024;
David S. Miller9b4006d2006-03-18 18:12:42 -0800263 unsigned long new_size, old_size, flags;
David S. Miller7a1ac522006-03-16 02:02:32 -0800264 struct tsb *old_tsb, *new_tsb;
David S. Miller9b4006d2006-03-18 18:12:42 -0800265 unsigned long new_cache_index, old_cache_index;
266 unsigned long new_rss_limit;
David S. Millerb52439c2006-03-17 23:40:47 -0800267 gfp_t gfp_flags;
David S. Millerbd407912006-01-31 18:31:38 -0800268
269 if (max_tsb_size > (PAGE_SIZE << MAX_ORDER))
270 max_tsb_size = (PAGE_SIZE << MAX_ORDER);
271
David S. Miller9b4006d2006-03-18 18:12:42 -0800272 new_cache_index = 0;
273 for (new_size = 8192; new_size < max_tsb_size; new_size <<= 1UL) {
274 unsigned long n_entries = new_size / sizeof(struct tsb);
David S. Millerbd407912006-01-31 18:31:38 -0800275
276 n_entries = (n_entries * 3) / 4;
277 if (n_entries > rss)
278 break;
David S. Miller9b4006d2006-03-18 18:12:42 -0800279
280 new_cache_index++;
David S. Millerbd407912006-01-31 18:31:38 -0800281 }
282
David S. Miller9b4006d2006-03-18 18:12:42 -0800283 if (new_size == max_tsb_size)
David S. Millerb52439c2006-03-17 23:40:47 -0800284 new_rss_limit = ~0UL;
285 else
David S. Miller9b4006d2006-03-18 18:12:42 -0800286 new_rss_limit = ((new_size / sizeof(struct tsb)) * 3) / 4;
David S. Millerb52439c2006-03-17 23:40:47 -0800287
David S. Miller9b4006d2006-03-18 18:12:42 -0800288retry_tsb_alloc:
David S. Millerb52439c2006-03-17 23:40:47 -0800289 gfp_flags = GFP_KERNEL;
David S. Miller9b4006d2006-03-18 18:12:42 -0800290 if (new_size > (PAGE_SIZE * 2))
David S. Millerb52439c2006-03-17 23:40:47 -0800291 gfp_flags = __GFP_NOWARN | __GFP_NORETRY;
292
David S. Miller9b4006d2006-03-18 18:12:42 -0800293 new_tsb = kmem_cache_alloc(tsb_caches[new_cache_index], gfp_flags);
294 if (unlikely(!new_tsb)) {
David S. Millerb52439c2006-03-17 23:40:47 -0800295 /* Not being able to fork due to a high-order TSB
296 * allocation failure is very bad behavior. Just back
297 * down to a 0-order allocation and force no TSB
298 * growing for this address space.
299 */
David S. Miller9b4006d2006-03-18 18:12:42 -0800300 if (mm->context.tsb == NULL && new_cache_index > 0) {
301 new_cache_index = 0;
302 new_size = 8192;
David S. Millerb52439c2006-03-17 23:40:47 -0800303 new_rss_limit = ~0UL;
David S. Miller9b4006d2006-03-18 18:12:42 -0800304 goto retry_tsb_alloc;
David S. Millerb52439c2006-03-17 23:40:47 -0800305 }
306
307 /* If we failed on a TSB grow, we are under serious
308 * memory pressure so don't try to grow any more.
309 */
310 if (mm->context.tsb != NULL)
311 mm->context.tsb_rss_limit = ~0UL;
David S. Millerbd407912006-01-31 18:31:38 -0800312 return;
David S. Millerb52439c2006-03-17 23:40:47 -0800313 }
David S. Millerbd407912006-01-31 18:31:38 -0800314
David S. Miller8b234272006-02-17 18:01:02 -0800315 /* Mark all tags as invalid. */
David S. Miller9b4006d2006-03-18 18:12:42 -0800316 memset(new_tsb, 0x40, new_size);
David S. Miller7a1ac522006-03-16 02:02:32 -0800317
318 /* Ok, we are about to commit the changes. If we are
319 * growing an existing TSB the locking is very tricky,
320 * so WATCH OUT!
321 *
322 * We have to hold mm->context.lock while committing to the
323 * new TSB, this synchronizes us with processors in
324 * flush_tsb_user() and switch_mm() for this address space.
325 *
326 * But even with that lock held, processors run asynchronously
327 * accessing the old TSB via TLB miss handling. This is OK
328 * because those actions are just propagating state from the
329 * Linux page tables into the TSB, page table mappings are not
330 * being changed. If a real fault occurs, the processor will
331 * synchronize with us when it hits flush_tsb_user(), this is
332 * also true for the case where vmscan is modifying the page
333 * tables. The only thing we need to be careful with is to
334 * skip any locked TSB entries during copy_tsb().
335 *
336 * When we finish committing to the new TSB, we have to drop
337 * the lock and ask all other cpus running this address space
338 * to run tsb_context_switch() to see the new TSB table.
339 */
340 spin_lock_irqsave(&mm->context.lock, flags);
341
342 old_tsb = mm->context.tsb;
David S. Miller9b4006d2006-03-18 18:12:42 -0800343 old_cache_index = (mm->context.tsb_reg_val & 0x7UL);
David S. Miller7a1ac522006-03-16 02:02:32 -0800344 old_size = mm->context.tsb_nentries * sizeof(struct tsb);
345
David S. Miller9b4006d2006-03-18 18:12:42 -0800346
David S. Miller7a1ac522006-03-16 02:02:32 -0800347 /* Handle multiple threads trying to grow the TSB at the same time.
348 * One will get in here first, and bump the size and the RSS limit.
349 * The others will get in here next and hit this check.
350 */
351 if (unlikely(old_tsb && (rss < mm->context.tsb_rss_limit))) {
352 spin_unlock_irqrestore(&mm->context.lock, flags);
353
David S. Miller9b4006d2006-03-18 18:12:42 -0800354 kmem_cache_free(tsb_caches[new_cache_index], new_tsb);
David S. Miller7a1ac522006-03-16 02:02:32 -0800355 return;
356 }
David S. Miller8b234272006-02-17 18:01:02 -0800357
David S. Millerb52439c2006-03-17 23:40:47 -0800358 mm->context.tsb_rss_limit = new_rss_limit;
David S. Millerbd407912006-01-31 18:31:38 -0800359
David S. Miller7a1ac522006-03-16 02:02:32 -0800360 if (old_tsb) {
361 extern void copy_tsb(unsigned long old_tsb_base,
362 unsigned long old_tsb_size,
363 unsigned long new_tsb_base,
364 unsigned long new_tsb_size);
365 unsigned long old_tsb_base = (unsigned long) old_tsb;
366 unsigned long new_tsb_base = (unsigned long) new_tsb;
David S. Millerbd407912006-01-31 18:31:38 -0800367
David S. Miller7a1ac522006-03-16 02:02:32 -0800368 if (tlb_type == cheetah_plus || tlb_type == hypervisor) {
369 old_tsb_base = __pa(old_tsb_base);
370 new_tsb_base = __pa(new_tsb_base);
371 }
David S. Miller9b4006d2006-03-18 18:12:42 -0800372 copy_tsb(old_tsb_base, old_size, new_tsb_base, new_size);
David S. Miller7a1ac522006-03-16 02:02:32 -0800373 }
David S. Millerbd407912006-01-31 18:31:38 -0800374
David S. Miller7a1ac522006-03-16 02:02:32 -0800375 mm->context.tsb = new_tsb;
David S. Miller9b4006d2006-03-18 18:12:42 -0800376 setup_tsb_params(mm, new_size);
David S. Millerbd407912006-01-31 18:31:38 -0800377
David S. Miller7a1ac522006-03-16 02:02:32 -0800378 spin_unlock_irqrestore(&mm->context.lock, flags);
379
David S. Millerbd407912006-01-31 18:31:38 -0800380 /* If old_tsb is NULL, we're being invoked for the first time
381 * from init_new_context().
382 */
383 if (old_tsb) {
David S. Miller7a1ac522006-03-16 02:02:32 -0800384 /* Reload it on the local cpu. */
David S. Millerbd407912006-01-31 18:31:38 -0800385 tsb_context_switch(mm);
386
David S. Miller7a1ac522006-03-16 02:02:32 -0800387 /* Now force other processors to do the same. */
388 smp_tsb_sync(mm);
389
390 /* Now it is safe to free the old tsb. */
David S. Miller9b4006d2006-03-18 18:12:42 -0800391 kmem_cache_free(tsb_caches[old_cache_index], old_tsb);
David S. Millerbd407912006-01-31 18:31:38 -0800392 }
393}
394
David S. Miller09f94282006-01-31 18:31:06 -0800395int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
396{
David S. Millera77754b2006-03-06 19:59:50 -0800397 spin_lock_init(&mm->context.lock);
David S. Miller09f94282006-01-31 18:31:06 -0800398
399 mm->context.sparc64_ctx_val = 0UL;
David S. Miller09f94282006-01-31 18:31:06 -0800400
David S. Millerbd407912006-01-31 18:31:38 -0800401 /* copy_mm() copies over the parent's mm_struct before calling
402 * us, so we need to zero out the TSB pointer or else tsb_grow()
403 * will be confused and think there is an older TSB to free up.
404 */
405 mm->context.tsb = NULL;
David S. Miller7a1ac522006-03-16 02:02:32 -0800406
407 /* If this is fork, inherit the parent's TSB size. We would
408 * grow it to that size on the first page fault anyways.
409 */
410 tsb_grow(mm, get_mm_rss(mm));
David S. Millerbd407912006-01-31 18:31:38 -0800411
412 if (unlikely(!mm->context.tsb))
413 return -ENOMEM;
David S. Miller09f94282006-01-31 18:31:06 -0800414
415 return 0;
416}
417
418void destroy_context(struct mm_struct *mm)
419{
David S. Miller9b4006d2006-03-18 18:12:42 -0800420 unsigned long flags, cache_index;
David S. Millerbd407912006-01-31 18:31:38 -0800421
David S. Miller9b4006d2006-03-18 18:12:42 -0800422 cache_index = (mm->context.tsb_reg_val & 0x7UL);
423 kmem_cache_free(tsb_caches[cache_index], mm->context.tsb);
David S. Miller98c55842006-01-31 18:31:20 -0800424
425 /* We can remove these later, but for now it's useful
426 * to catch any bogus post-destroy_context() references
427 * to the TSB.
428 */
429 mm->context.tsb = NULL;
430 mm->context.tsb_reg_val = 0UL;
David S. Miller09f94282006-01-31 18:31:06 -0800431
David S. Miller77b838f2006-02-23 21:40:15 -0800432 spin_lock_irqsave(&ctx_alloc_lock, flags);
David S. Miller09f94282006-01-31 18:31:06 -0800433
434 if (CTX_VALID(mm->context)) {
435 unsigned long nr = CTX_NRBITS(mm->context);
436 mmu_context_bmap[nr>>6] &= ~(1UL << (nr & 63));
437 }
438
David S. Miller77b838f2006-02-23 21:40:15 -0800439 spin_unlock_irqrestore(&ctx_alloc_lock, flags);
David S. Miller09f94282006-01-31 18:31:06 -0800440}