1 /* arch/sparc64/mm/tsb.c
3 * Copyright (C) 2006 David S. Miller <davem@davemloft.net>
6 #include <linux/kernel.h>
7 #include <asm/system.h>
9 #include <asm/tlbflush.h>
11 #include <asm/mmu_context.h>
12 #include <asm/pgtable.h>
15 extern struct tsb swapper_tsb[KERNEL_TSB_NENTRIES];
17 static inline unsigned long tsb_hash(unsigned long vaddr, unsigned long nentries)
20 return vaddr & (nentries - 1);
23 static inline int tag_compare(struct tsb *entry, unsigned long vaddr, unsigned long context)
28 return (entry->tag == ((vaddr >> 22) | (context << 48)));
31 /* TSB flushes need only occur on the processor initiating the address
32 * space modification, not on each cpu the address space has run on.
33 * Only the TLB flush needs that treatment.
36 void flush_tsb_kernel_range(unsigned long start, unsigned long end)
40 for (v = start; v < end; v += PAGE_SIZE) {
41 unsigned long hash = tsb_hash(v, KERNEL_TSB_NENTRIES);
42 struct tsb *ent = &swapper_tsb[hash];
44 if (tag_compare(ent, v, 0)) {
46 membar_storeload_storestore();
51 void flush_tsb_user(struct mmu_gather *mp)
53 struct mm_struct *mm = mp->mm;
54 struct tsb *tsb = mm->context.tsb;
55 unsigned long ctx = ~0UL;
56 unsigned long nentries = mm->context.tsb_nentries;
59 if (CTX_VALID(mm->context))
60 ctx = CTX_HWBITS(mm->context);
62 for (i = 0; i < mp->tlb_nr; i++) {
63 unsigned long v = mp->vaddrs[i];
68 ent = &tsb[tsb_hash(v, nentries)];
69 if (tag_compare(ent, v, ctx)) {
71 membar_storeload_storestore();
76 static void setup_tsb_params(struct mm_struct *mm, unsigned long tsb_bytes)
78 unsigned long tsb_reg, base, tsb_paddr;
79 unsigned long page_sz, tte;
81 mm->context.tsb_nentries = tsb_bytes / sizeof(struct tsb);
84 tte = (_PAGE_VALID | _PAGE_L | _PAGE_CP |
85 _PAGE_CV | _PAGE_P | _PAGE_W);
86 tsb_paddr = __pa(mm->context.tsb);
88 /* Use the smallest page size that can map the whole TSB
94 #ifdef DCACHE_ALIASING_POSSIBLE
95 base += (tsb_paddr & 8192);
122 page_sz = 512 * 1024;
128 page_sz = 512 * 1024;
134 page_sz = 512 * 1024;
140 page_sz = 4 * 1024 * 1024;
148 tsb_reg |= (tsb_paddr & (page_sz - 1UL));
149 tte |= (tsb_paddr & ~(page_sz - 1UL));
151 mm->context.tsb_reg_val = tsb_reg;
152 mm->context.tsb_map_vaddr = base;
153 mm->context.tsb_map_pte = tte;
156 /* The page tables are locked against modifications while this
159 * XXX do some prefetching...
161 static void copy_tsb(struct tsb *old_tsb, unsigned long old_size,
162 struct tsb *new_tsb, unsigned long new_size)
164 unsigned long old_nentries = old_size / sizeof(struct tsb);
165 unsigned long new_nentries = new_size / sizeof(struct tsb);
168 for (i = 0; i < old_nentries; i++) {
169 register unsigned long tag asm("o4");
170 register unsigned long pte asm("o5");
174 __asm__ __volatile__(
176 : "=r" (tag), "=r" (pte)
177 : "r" (&old_tsb[i]), "i" (ASI_NUCLEUS_QUAD_LDD));
179 if (!tag || (tag & (1UL << TSB_TAG_LOCK_BIT)))
182 /* We only put base page size PTEs into the TSB,
183 * but that might change in the future. This code
184 * would need to be changed if we start putting larger
185 * page size PTEs into there.
187 WARN_ON((pte & _PAGE_ALL_SZ_BITS) != _PAGE_SZBITS);
189 /* The tag holds bits 22 to 63 of the virtual address
190 * and the context. Clear out the context, and shift
191 * up to make a virtual address.
193 v = (tag & ((1UL << 42UL) - 1UL)) << 22UL;
195 /* The implied bits of the tag (bits 13 to 21) are
196 * determined by the TSB entry index, so fill that in.
198 v |= (i & (512UL - 1UL)) << 13UL;
200 hash = tsb_hash(v, new_nentries);
201 new_tsb[hash].tag = tag;
202 new_tsb[hash].pte = pte;
206 /* When the RSS of an address space exceeds mm->context.tsb_rss_limit,
207 * update_mmu_cache() invokes this routine to try and grow the TSB.
208 * When we reach the maximum TSB size supported, we stick ~0UL into
209 * mm->context.tsb_rss_limit so the grow checks in update_mmu_cache()
210 * will not trigger any longer.
212 * The TSB can be anywhere from 8K to 1MB in size, in increasing powers
213 * of two. The TSB must be aligned to it's size, so f.e. a 512K TSB
214 * must be 512K aligned.
216 * The idea here is to grow the TSB when the RSS of the process approaches
217 * the number of entries that the current TSB can hold at once. Currently,
218 * we trigger when the RSS hits 3/4 of the TSB capacity.
220 void tsb_grow(struct mm_struct *mm, unsigned long rss, gfp_t gfp_flags)
222 unsigned long max_tsb_size = 1 * 1024 * 1024;
223 unsigned long size, old_size;
227 if (max_tsb_size > (PAGE_SIZE << MAX_ORDER))
228 max_tsb_size = (PAGE_SIZE << MAX_ORDER);
230 for (size = PAGE_SIZE; size < max_tsb_size; size <<= 1UL) {
231 unsigned long n_entries = size / sizeof(struct tsb);
233 n_entries = (n_entries * 3) / 4;
238 page = alloc_pages(gfp_flags | __GFP_ZERO, get_order(size));
242 if (size == max_tsb_size)
243 mm->context.tsb_rss_limit = ~0UL;
245 mm->context.tsb_rss_limit =
246 ((size / sizeof(struct tsb)) * 3) / 4;
248 old_tsb = mm->context.tsb;
249 old_size = mm->context.tsb_nentries * sizeof(struct tsb);
252 copy_tsb(old_tsb, old_size, page_address(page), size);
254 mm->context.tsb = page_address(page);
255 setup_tsb_params(mm, size);
257 /* If old_tsb is NULL, we're being invoked for the first time
258 * from init_new_context().
261 /* Now force all other processors to reload the new
266 /* Finally reload it on the local cpu. No further
267 * references will remain to the old TSB and we can
270 tsb_context_switch(mm);
272 free_pages((unsigned long) old_tsb, get_order(old_size));
276 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
278 unsigned long initial_rss;
280 mm->context.sparc64_ctx_val = 0UL;
282 /* copy_mm() copies over the parent's mm_struct before calling
283 * us, so we need to zero out the TSB pointer or else tsb_grow()
284 * will be confused and think there is an older TSB to free up.
286 mm->context.tsb = NULL;
288 /* If this is fork, inherit the parent's TSB size. We would
289 * grow it to that size on the first page fault anyways.
291 initial_rss = mm->context.tsb_nentries;
295 tsb_grow(mm, initial_rss, GFP_KERNEL);
297 if (unlikely(!mm->context.tsb))
303 void destroy_context(struct mm_struct *mm)
305 unsigned long size = mm->context.tsb_nentries * sizeof(struct tsb);
307 free_pages((unsigned long) mm->context.tsb, get_order(size));
309 /* We can remove these later, but for now it's useful
310 * to catch any bogus post-destroy_context() references
313 mm->context.tsb = NULL;
314 mm->context.tsb_reg_val = 0UL;
316 spin_lock(&ctx_alloc_lock);
318 if (CTX_VALID(mm->context)) {
319 unsigned long nr = CTX_NRBITS(mm->context);
320 mmu_context_bmap[nr>>6] &= ~(1UL << (nr & 63));
323 spin_unlock(&ctx_alloc_lock);