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