]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/sparc/mm/sun4c.c
MM: Pass a PTE pointer to update_mmu_cache() rather than the PTE itself
[mirror_ubuntu-zesty-kernel.git] / arch / sparc / mm / sun4c.c
1 /* sun4c.c: Doing in software what should be done in hardware.
2 *
3 * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
4 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1996 Andrew Tridgell (Andrew.Tridgell@anu.edu.au)
6 * Copyright (C) 1997-2000 Anton Blanchard (anton@samba.org)
7 * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 */
9
10 #define NR_TASK_BUCKETS 512
11
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/init.h>
15 #include <linux/bootmem.h>
16 #include <linux/highmem.h>
17 #include <linux/fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/scatterlist.h>
20 #include <linux/bitmap.h>
21
22 #include <asm/sections.h>
23 #include <asm/page.h>
24 #include <asm/pgalloc.h>
25 #include <asm/pgtable.h>
26 #include <asm/vaddrs.h>
27 #include <asm/idprom.h>
28 #include <asm/machines.h>
29 #include <asm/memreg.h>
30 #include <asm/processor.h>
31 #include <asm/auxio.h>
32 #include <asm/io.h>
33 #include <asm/oplib.h>
34 #include <asm/openprom.h>
35 #include <asm/mmu_context.h>
36 #include <asm/highmem.h>
37 #include <asm/btfixup.h>
38 #include <asm/cacheflush.h>
39 #include <asm/tlbflush.h>
40
41 /* Because of our dynamic kernel TLB miss strategy, and how
42 * our DVMA mapping allocation works, you _MUST_:
43 *
44 * 1) Disable interrupts _and_ not touch any dynamic kernel
45 * memory while messing with kernel MMU state. By
46 * dynamic memory I mean any object which is not in
47 * the kernel image itself or a thread_union (both of
48 * which are locked into the MMU).
49 * 2) Disable interrupts while messing with user MMU state.
50 */
51
52 extern int num_segmaps, num_contexts;
53
54 extern unsigned long page_kernel;
55
56 /* That's it, we prom_halt() on sun4c if the cache size is something other than 65536.
57 * So let's save some cycles and just use that everywhere except for that bootup
58 * sanity check.
59 */
60 #define SUN4C_VAC_SIZE 65536
61
62 #define SUN4C_KERNEL_BUCKETS 32
63
64 /* Flushing the cache. */
65 struct sun4c_vac_props sun4c_vacinfo;
66 unsigned long sun4c_kernel_faults;
67
68 /* Invalidate every sun4c cache line tag. */
69 static void __init sun4c_flush_all(void)
70 {
71 unsigned long begin, end;
72
73 if (sun4c_vacinfo.on)
74 panic("SUN4C: AIEEE, trying to invalidate vac while it is on.");
75
76 /* Clear 'valid' bit in all cache line tags */
77 begin = AC_CACHETAGS;
78 end = (AC_CACHETAGS + SUN4C_VAC_SIZE);
79 while (begin < end) {
80 __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : :
81 "r" (begin), "i" (ASI_CONTROL));
82 begin += sun4c_vacinfo.linesize;
83 }
84 }
85
86 static void sun4c_flush_context_hw(void)
87 {
88 unsigned long end = SUN4C_VAC_SIZE;
89
90 __asm__ __volatile__(
91 "1: addcc %0, -4096, %0\n\t"
92 " bne 1b\n\t"
93 " sta %%g0, [%0] %2"
94 : "=&r" (end)
95 : "0" (end), "i" (ASI_HWFLUSHCONTEXT)
96 : "cc");
97 }
98
99 /* Must be called minimally with IRQs disabled. */
100 static void sun4c_flush_segment_hw(unsigned long addr)
101 {
102 if (sun4c_get_segmap(addr) != invalid_segment) {
103 unsigned long vac_size = SUN4C_VAC_SIZE;
104
105 __asm__ __volatile__(
106 "1: addcc %0, -4096, %0\n\t"
107 " bne 1b\n\t"
108 " sta %%g0, [%2 + %0] %3"
109 : "=&r" (vac_size)
110 : "0" (vac_size), "r" (addr), "i" (ASI_HWFLUSHSEG)
111 : "cc");
112 }
113 }
114
115 /* File local boot time fixups. */
116 BTFIXUPDEF_CALL(void, sun4c_flush_page, unsigned long)
117 BTFIXUPDEF_CALL(void, sun4c_flush_segment, unsigned long)
118 BTFIXUPDEF_CALL(void, sun4c_flush_context, void)
119
120 #define sun4c_flush_page(addr) BTFIXUP_CALL(sun4c_flush_page)(addr)
121 #define sun4c_flush_segment(addr) BTFIXUP_CALL(sun4c_flush_segment)(addr)
122 #define sun4c_flush_context() BTFIXUP_CALL(sun4c_flush_context)()
123
124 /* Must be called minimally with interrupts disabled. */
125 static void sun4c_flush_page_hw(unsigned long addr)
126 {
127 addr &= PAGE_MASK;
128 if ((int)sun4c_get_pte(addr) < 0)
129 __asm__ __volatile__("sta %%g0, [%0] %1"
130 : : "r" (addr), "i" (ASI_HWFLUSHPAGE));
131 }
132
133 /* Don't inline the software version as it eats too many cache lines if expanded. */
134 static void sun4c_flush_context_sw(void)
135 {
136 unsigned long nbytes = SUN4C_VAC_SIZE;
137 unsigned long lsize = sun4c_vacinfo.linesize;
138
139 __asm__ __volatile__(
140 "add %2, %2, %%g1\n\t"
141 "add %2, %%g1, %%g2\n\t"
142 "add %2, %%g2, %%g3\n\t"
143 "add %2, %%g3, %%g4\n\t"
144 "add %2, %%g4, %%g5\n\t"
145 "add %2, %%g5, %%o4\n\t"
146 "add %2, %%o4, %%o5\n"
147 "1:\n\t"
148 "subcc %0, %%o5, %0\n\t"
149 "sta %%g0, [%0] %3\n\t"
150 "sta %%g0, [%0 + %2] %3\n\t"
151 "sta %%g0, [%0 + %%g1] %3\n\t"
152 "sta %%g0, [%0 + %%g2] %3\n\t"
153 "sta %%g0, [%0 + %%g3] %3\n\t"
154 "sta %%g0, [%0 + %%g4] %3\n\t"
155 "sta %%g0, [%0 + %%g5] %3\n\t"
156 "bg 1b\n\t"
157 " sta %%g0, [%1 + %%o4] %3\n"
158 : "=&r" (nbytes)
159 : "0" (nbytes), "r" (lsize), "i" (ASI_FLUSHCTX)
160 : "g1", "g2", "g3", "g4", "g5", "o4", "o5", "cc");
161 }
162
163 /* Don't inline the software version as it eats too many cache lines if expanded. */
164 static void sun4c_flush_segment_sw(unsigned long addr)
165 {
166 if (sun4c_get_segmap(addr) != invalid_segment) {
167 unsigned long nbytes = SUN4C_VAC_SIZE;
168 unsigned long lsize = sun4c_vacinfo.linesize;
169
170 __asm__ __volatile__(
171 "add %2, %2, %%g1\n\t"
172 "add %2, %%g1, %%g2\n\t"
173 "add %2, %%g2, %%g3\n\t"
174 "add %2, %%g3, %%g4\n\t"
175 "add %2, %%g4, %%g5\n\t"
176 "add %2, %%g5, %%o4\n\t"
177 "add %2, %%o4, %%o5\n"
178 "1:\n\t"
179 "subcc %1, %%o5, %1\n\t"
180 "sta %%g0, [%0] %6\n\t"
181 "sta %%g0, [%0 + %2] %6\n\t"
182 "sta %%g0, [%0 + %%g1] %6\n\t"
183 "sta %%g0, [%0 + %%g2] %6\n\t"
184 "sta %%g0, [%0 + %%g3] %6\n\t"
185 "sta %%g0, [%0 + %%g4] %6\n\t"
186 "sta %%g0, [%0 + %%g5] %6\n\t"
187 "sta %%g0, [%0 + %%o4] %6\n\t"
188 "bg 1b\n\t"
189 " add %0, %%o5, %0\n"
190 : "=&r" (addr), "=&r" (nbytes), "=&r" (lsize)
191 : "0" (addr), "1" (nbytes), "2" (lsize),
192 "i" (ASI_FLUSHSEG)
193 : "g1", "g2", "g3", "g4", "g5", "o4", "o5", "cc");
194 }
195 }
196
197 /* Don't inline the software version as it eats too many cache lines if expanded. */
198 static void sun4c_flush_page_sw(unsigned long addr)
199 {
200 addr &= PAGE_MASK;
201 if ((sun4c_get_pte(addr) & (_SUN4C_PAGE_NOCACHE | _SUN4C_PAGE_VALID)) ==
202 _SUN4C_PAGE_VALID) {
203 unsigned long left = PAGE_SIZE;
204 unsigned long lsize = sun4c_vacinfo.linesize;
205
206 __asm__ __volatile__(
207 "add %2, %2, %%g1\n\t"
208 "add %2, %%g1, %%g2\n\t"
209 "add %2, %%g2, %%g3\n\t"
210 "add %2, %%g3, %%g4\n\t"
211 "add %2, %%g4, %%g5\n\t"
212 "add %2, %%g5, %%o4\n\t"
213 "add %2, %%o4, %%o5\n"
214 "1:\n\t"
215 "subcc %1, %%o5, %1\n\t"
216 "sta %%g0, [%0] %6\n\t"
217 "sta %%g0, [%0 + %2] %6\n\t"
218 "sta %%g0, [%0 + %%g1] %6\n\t"
219 "sta %%g0, [%0 + %%g2] %6\n\t"
220 "sta %%g0, [%0 + %%g3] %6\n\t"
221 "sta %%g0, [%0 + %%g4] %6\n\t"
222 "sta %%g0, [%0 + %%g5] %6\n\t"
223 "sta %%g0, [%0 + %%o4] %6\n\t"
224 "bg 1b\n\t"
225 " add %0, %%o5, %0\n"
226 : "=&r" (addr), "=&r" (left), "=&r" (lsize)
227 : "0" (addr), "1" (left), "2" (lsize),
228 "i" (ASI_FLUSHPG)
229 : "g1", "g2", "g3", "g4", "g5", "o4", "o5", "cc");
230 }
231 }
232
233 /* The sun4c's do have an on chip store buffer. And the way you
234 * clear them out isn't so obvious. The only way I can think of
235 * to accomplish this is to read the current context register,
236 * store the same value there, then read an external hardware
237 * register.
238 */
239 void sun4c_complete_all_stores(void)
240 {
241 volatile int _unused;
242
243 _unused = sun4c_get_context();
244 sun4c_set_context(_unused);
245 _unused = get_auxio();
246 }
247
248 /* Bootup utility functions. */
249 static inline void sun4c_init_clean_segmap(unsigned char pseg)
250 {
251 unsigned long vaddr;
252
253 sun4c_put_segmap(0, pseg);
254 for (vaddr = 0; vaddr < SUN4C_REAL_PGDIR_SIZE; vaddr += PAGE_SIZE)
255 sun4c_put_pte(vaddr, 0);
256 sun4c_put_segmap(0, invalid_segment);
257 }
258
259 static inline void sun4c_init_clean_mmu(unsigned long kernel_end)
260 {
261 unsigned long vaddr;
262 unsigned char savectx, ctx;
263
264 savectx = sun4c_get_context();
265 for (ctx = 0; ctx < num_contexts; ctx++) {
266 sun4c_set_context(ctx);
267 for (vaddr = 0; vaddr < 0x20000000; vaddr += SUN4C_REAL_PGDIR_SIZE)
268 sun4c_put_segmap(vaddr, invalid_segment);
269 for (vaddr = 0xe0000000; vaddr < KERNBASE; vaddr += SUN4C_REAL_PGDIR_SIZE)
270 sun4c_put_segmap(vaddr, invalid_segment);
271 for (vaddr = kernel_end; vaddr < KADB_DEBUGGER_BEGVM; vaddr += SUN4C_REAL_PGDIR_SIZE)
272 sun4c_put_segmap(vaddr, invalid_segment);
273 for (vaddr = LINUX_OPPROM_ENDVM; vaddr; vaddr += SUN4C_REAL_PGDIR_SIZE)
274 sun4c_put_segmap(vaddr, invalid_segment);
275 }
276 sun4c_set_context(savectx);
277 }
278
279 void __init sun4c_probe_vac(void)
280 {
281 sun4c_disable_vac();
282
283 if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS1)) ||
284 (idprom->id_machtype == (SM_SUN4C | SM_4C_SS1PLUS))) {
285 /* PROM on SS1 lacks this info, to be super safe we
286 * hard code it here since this arch is cast in stone.
287 */
288 sun4c_vacinfo.num_bytes = 65536;
289 sun4c_vacinfo.linesize = 16;
290 } else {
291 sun4c_vacinfo.num_bytes =
292 prom_getintdefault(prom_root_node, "vac-size", 65536);
293 sun4c_vacinfo.linesize =
294 prom_getintdefault(prom_root_node, "vac-linesize", 16);
295 }
296 sun4c_vacinfo.do_hwflushes =
297 prom_getintdefault(prom_root_node, "vac-hwflush", 0);
298
299 if (sun4c_vacinfo.do_hwflushes == 0)
300 sun4c_vacinfo.do_hwflushes =
301 prom_getintdefault(prom_root_node, "vac_hwflush", 0);
302
303 if (sun4c_vacinfo.num_bytes != 65536) {
304 prom_printf("WEIRD Sun4C VAC cache size, "
305 "tell sparclinux@vger.kernel.org");
306 prom_halt();
307 }
308
309 switch (sun4c_vacinfo.linesize) {
310 case 16:
311 sun4c_vacinfo.log2lsize = 4;
312 break;
313 case 32:
314 sun4c_vacinfo.log2lsize = 5;
315 break;
316 default:
317 prom_printf("probe_vac: Didn't expect vac-linesize of %d, halting\n",
318 sun4c_vacinfo.linesize);
319 prom_halt();
320 };
321
322 sun4c_flush_all();
323 sun4c_enable_vac();
324 }
325
326 /* Patch instructions for the low level kernel fault handler. */
327 extern unsigned long invalid_segment_patch1, invalid_segment_patch1_ff;
328 extern unsigned long invalid_segment_patch2, invalid_segment_patch2_ff;
329 extern unsigned long invalid_segment_patch1_1ff, invalid_segment_patch2_1ff;
330 extern unsigned long num_context_patch1, num_context_patch1_16;
331 extern unsigned long num_context_patch2_16;
332 extern unsigned long vac_linesize_patch, vac_linesize_patch_32;
333 extern unsigned long vac_hwflush_patch1, vac_hwflush_patch1_on;
334 extern unsigned long vac_hwflush_patch2, vac_hwflush_patch2_on;
335
336 #define PATCH_INSN(src, dst) do { \
337 daddr = &(dst); \
338 iaddr = &(src); \
339 *daddr = *iaddr; \
340 } while (0)
341
342 static void __init patch_kernel_fault_handler(void)
343 {
344 unsigned long *iaddr, *daddr;
345
346 switch (num_segmaps) {
347 case 128:
348 /* Default, nothing to do. */
349 break;
350 case 256:
351 PATCH_INSN(invalid_segment_patch1_ff,
352 invalid_segment_patch1);
353 PATCH_INSN(invalid_segment_patch2_ff,
354 invalid_segment_patch2);
355 break;
356 case 512:
357 PATCH_INSN(invalid_segment_patch1_1ff,
358 invalid_segment_patch1);
359 PATCH_INSN(invalid_segment_patch2_1ff,
360 invalid_segment_patch2);
361 break;
362 default:
363 prom_printf("Unhandled number of segmaps: %d\n",
364 num_segmaps);
365 prom_halt();
366 };
367 switch (num_contexts) {
368 case 8:
369 /* Default, nothing to do. */
370 break;
371 case 16:
372 PATCH_INSN(num_context_patch1_16,
373 num_context_patch1);
374 break;
375 default:
376 prom_printf("Unhandled number of contexts: %d\n",
377 num_contexts);
378 prom_halt();
379 };
380
381 if (sun4c_vacinfo.do_hwflushes != 0) {
382 PATCH_INSN(vac_hwflush_patch1_on, vac_hwflush_patch1);
383 PATCH_INSN(vac_hwflush_patch2_on, vac_hwflush_patch2);
384 } else {
385 switch (sun4c_vacinfo.linesize) {
386 case 16:
387 /* Default, nothing to do. */
388 break;
389 case 32:
390 PATCH_INSN(vac_linesize_patch_32, vac_linesize_patch);
391 break;
392 default:
393 prom_printf("Impossible VAC linesize %d, halting...\n",
394 sun4c_vacinfo.linesize);
395 prom_halt();
396 };
397 }
398 }
399
400 static void __init sun4c_probe_mmu(void)
401 {
402 if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS1)) ||
403 (idprom->id_machtype == (SM_SUN4C | SM_4C_SS1PLUS))) {
404 /* Hardcode these just to be safe, PROM on SS1 does
405 * not have this info available in the root node.
406 */
407 num_segmaps = 128;
408 num_contexts = 8;
409 } else {
410 num_segmaps =
411 prom_getintdefault(prom_root_node, "mmu-npmg", 128);
412 num_contexts =
413 prom_getintdefault(prom_root_node, "mmu-nctx", 0x8);
414 }
415 patch_kernel_fault_handler();
416 }
417
418 volatile unsigned long __iomem *sun4c_memerr_reg = NULL;
419
420 void __init sun4c_probe_memerr_reg(void)
421 {
422 int node;
423 struct linux_prom_registers regs[1];
424
425 node = prom_getchild(prom_root_node);
426 node = prom_searchsiblings(prom_root_node, "memory-error");
427 if (!node)
428 return;
429 if (prom_getproperty(node, "reg", (char *)regs, sizeof(regs)) <= 0)
430 return;
431 /* hmm I think regs[0].which_io is zero here anyways */
432 sun4c_memerr_reg = ioremap(regs[0].phys_addr, regs[0].reg_size);
433 }
434
435 static inline void sun4c_init_ss2_cache_bug(void)
436 {
437 extern unsigned long start;
438
439 if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS2)) ||
440 (idprom->id_machtype == (SM_SUN4C | SM_4C_IPX)) ||
441 (idprom->id_machtype == (SM_SUN4C | SM_4C_ELC))) {
442 /* Whee.. */
443 printk("SS2 cache bug detected, uncaching trap table page\n");
444 sun4c_flush_page((unsigned int) &start);
445 sun4c_put_pte(((unsigned long) &start),
446 (sun4c_get_pte((unsigned long) &start) | _SUN4C_PAGE_NOCACHE));
447 }
448 }
449
450 /* Addr is always aligned on a page boundary for us already. */
451 static int sun4c_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va,
452 unsigned long addr, int len)
453 {
454 unsigned long page, end;
455
456 *pba = addr;
457
458 end = PAGE_ALIGN((addr + len));
459 while (addr < end) {
460 page = va;
461 sun4c_flush_page(page);
462 page -= PAGE_OFFSET;
463 page >>= PAGE_SHIFT;
464 page |= (_SUN4C_PAGE_VALID | _SUN4C_PAGE_DIRTY |
465 _SUN4C_PAGE_NOCACHE | _SUN4C_PAGE_PRIV);
466 sun4c_put_pte(addr, page);
467 addr += PAGE_SIZE;
468 va += PAGE_SIZE;
469 }
470
471 return 0;
472 }
473
474 static void sun4c_unmap_dma_area(struct device *dev, unsigned long busa, int len)
475 {
476 /* Fortunately for us, bus_addr == uncached_virt in sun4c. */
477 /* XXX Implement this */
478 }
479
480 /* TLB management. */
481
482 /* Don't change this struct without changing entry.S. This is used
483 * in the in-window kernel fault handler, and you don't want to mess
484 * with that. (See sun4c_fault in entry.S).
485 */
486 struct sun4c_mmu_entry {
487 struct sun4c_mmu_entry *next;
488 struct sun4c_mmu_entry *prev;
489 unsigned long vaddr;
490 unsigned char pseg;
491 unsigned char locked;
492
493 /* For user mappings only, and completely hidden from kernel
494 * TLB miss code.
495 */
496 unsigned char ctx;
497 struct sun4c_mmu_entry *lru_next;
498 struct sun4c_mmu_entry *lru_prev;
499 };
500
501 static struct sun4c_mmu_entry mmu_entry_pool[SUN4C_MAX_SEGMAPS];
502
503 static void __init sun4c_init_mmu_entry_pool(void)
504 {
505 int i;
506
507 for (i=0; i < SUN4C_MAX_SEGMAPS; i++) {
508 mmu_entry_pool[i].pseg = i;
509 mmu_entry_pool[i].next = NULL;
510 mmu_entry_pool[i].prev = NULL;
511 mmu_entry_pool[i].vaddr = 0;
512 mmu_entry_pool[i].locked = 0;
513 mmu_entry_pool[i].ctx = 0;
514 mmu_entry_pool[i].lru_next = NULL;
515 mmu_entry_pool[i].lru_prev = NULL;
516 }
517 mmu_entry_pool[invalid_segment].locked = 1;
518 }
519
520 static inline void fix_permissions(unsigned long vaddr, unsigned long bits_on,
521 unsigned long bits_off)
522 {
523 unsigned long start, end;
524
525 end = vaddr + SUN4C_REAL_PGDIR_SIZE;
526 for (start = vaddr; start < end; start += PAGE_SIZE)
527 if (sun4c_get_pte(start) & _SUN4C_PAGE_VALID)
528 sun4c_put_pte(start, (sun4c_get_pte(start) | bits_on) &
529 ~bits_off);
530 }
531
532 static inline void sun4c_init_map_kernelprom(unsigned long kernel_end)
533 {
534 unsigned long vaddr;
535 unsigned char pseg, ctx;
536
537 for (vaddr = KADB_DEBUGGER_BEGVM;
538 vaddr < LINUX_OPPROM_ENDVM;
539 vaddr += SUN4C_REAL_PGDIR_SIZE) {
540 pseg = sun4c_get_segmap(vaddr);
541 if (pseg != invalid_segment) {
542 mmu_entry_pool[pseg].locked = 1;
543 for (ctx = 0; ctx < num_contexts; ctx++)
544 prom_putsegment(ctx, vaddr, pseg);
545 fix_permissions(vaddr, _SUN4C_PAGE_PRIV, 0);
546 }
547 }
548
549 for (vaddr = KERNBASE; vaddr < kernel_end; vaddr += SUN4C_REAL_PGDIR_SIZE) {
550 pseg = sun4c_get_segmap(vaddr);
551 mmu_entry_pool[pseg].locked = 1;
552 for (ctx = 0; ctx < num_contexts; ctx++)
553 prom_putsegment(ctx, vaddr, pseg);
554 fix_permissions(vaddr, _SUN4C_PAGE_PRIV, _SUN4C_PAGE_NOCACHE);
555 }
556 }
557
558 static void __init sun4c_init_lock_area(unsigned long start, unsigned long end)
559 {
560 int i, ctx;
561
562 while (start < end) {
563 for (i = 0; i < invalid_segment; i++)
564 if (!mmu_entry_pool[i].locked)
565 break;
566 mmu_entry_pool[i].locked = 1;
567 sun4c_init_clean_segmap(i);
568 for (ctx = 0; ctx < num_contexts; ctx++)
569 prom_putsegment(ctx, start, mmu_entry_pool[i].pseg);
570 start += SUN4C_REAL_PGDIR_SIZE;
571 }
572 }
573
574 /* Don't change this struct without changing entry.S. This is used
575 * in the in-window kernel fault handler, and you don't want to mess
576 * with that. (See sun4c_fault in entry.S).
577 */
578 struct sun4c_mmu_ring {
579 struct sun4c_mmu_entry ringhd;
580 int num_entries;
581 };
582
583 static struct sun4c_mmu_ring sun4c_context_ring[SUN4C_MAX_CONTEXTS]; /* used user entries */
584 static struct sun4c_mmu_ring sun4c_ufree_ring; /* free user entries */
585 static struct sun4c_mmu_ring sun4c_ulru_ring; /* LRU user entries */
586 struct sun4c_mmu_ring sun4c_kernel_ring; /* used kernel entries */
587 struct sun4c_mmu_ring sun4c_kfree_ring; /* free kernel entries */
588
589 static inline void sun4c_init_rings(void)
590 {
591 int i;
592
593 for (i = 0; i < SUN4C_MAX_CONTEXTS; i++) {
594 sun4c_context_ring[i].ringhd.next =
595 sun4c_context_ring[i].ringhd.prev =
596 &sun4c_context_ring[i].ringhd;
597 sun4c_context_ring[i].num_entries = 0;
598 }
599 sun4c_ufree_ring.ringhd.next = sun4c_ufree_ring.ringhd.prev =
600 &sun4c_ufree_ring.ringhd;
601 sun4c_ufree_ring.num_entries = 0;
602 sun4c_ulru_ring.ringhd.lru_next = sun4c_ulru_ring.ringhd.lru_prev =
603 &sun4c_ulru_ring.ringhd;
604 sun4c_ulru_ring.num_entries = 0;
605 sun4c_kernel_ring.ringhd.next = sun4c_kernel_ring.ringhd.prev =
606 &sun4c_kernel_ring.ringhd;
607 sun4c_kernel_ring.num_entries = 0;
608 sun4c_kfree_ring.ringhd.next = sun4c_kfree_ring.ringhd.prev =
609 &sun4c_kfree_ring.ringhd;
610 sun4c_kfree_ring.num_entries = 0;
611 }
612
613 static void add_ring(struct sun4c_mmu_ring *ring,
614 struct sun4c_mmu_entry *entry)
615 {
616 struct sun4c_mmu_entry *head = &ring->ringhd;
617
618 entry->prev = head;
619 (entry->next = head->next)->prev = entry;
620 head->next = entry;
621 ring->num_entries++;
622 }
623
624 static inline void add_lru(struct sun4c_mmu_entry *entry)
625 {
626 struct sun4c_mmu_ring *ring = &sun4c_ulru_ring;
627 struct sun4c_mmu_entry *head = &ring->ringhd;
628
629 entry->lru_next = head;
630 (entry->lru_prev = head->lru_prev)->lru_next = entry;
631 head->lru_prev = entry;
632 }
633
634 static void add_ring_ordered(struct sun4c_mmu_ring *ring,
635 struct sun4c_mmu_entry *entry)
636 {
637 struct sun4c_mmu_entry *head = &ring->ringhd;
638 unsigned long addr = entry->vaddr;
639
640 while ((head->next != &ring->ringhd) && (head->next->vaddr < addr))
641 head = head->next;
642
643 entry->prev = head;
644 (entry->next = head->next)->prev = entry;
645 head->next = entry;
646 ring->num_entries++;
647
648 add_lru(entry);
649 }
650
651 static inline void remove_ring(struct sun4c_mmu_ring *ring,
652 struct sun4c_mmu_entry *entry)
653 {
654 struct sun4c_mmu_entry *next = entry->next;
655
656 (next->prev = entry->prev)->next = next;
657 ring->num_entries--;
658 }
659
660 static void remove_lru(struct sun4c_mmu_entry *entry)
661 {
662 struct sun4c_mmu_entry *next = entry->lru_next;
663
664 (next->lru_prev = entry->lru_prev)->lru_next = next;
665 }
666
667 static void free_user_entry(int ctx, struct sun4c_mmu_entry *entry)
668 {
669 remove_ring(sun4c_context_ring+ctx, entry);
670 remove_lru(entry);
671 add_ring(&sun4c_ufree_ring, entry);
672 }
673
674 static void free_kernel_entry(struct sun4c_mmu_entry *entry,
675 struct sun4c_mmu_ring *ring)
676 {
677 remove_ring(ring, entry);
678 add_ring(&sun4c_kfree_ring, entry);
679 }
680
681 static void __init sun4c_init_fill_kernel_ring(int howmany)
682 {
683 int i;
684
685 while (howmany) {
686 for (i = 0; i < invalid_segment; i++)
687 if (!mmu_entry_pool[i].locked)
688 break;
689 mmu_entry_pool[i].locked = 1;
690 sun4c_init_clean_segmap(i);
691 add_ring(&sun4c_kfree_ring, &mmu_entry_pool[i]);
692 howmany--;
693 }
694 }
695
696 static void __init sun4c_init_fill_user_ring(void)
697 {
698 int i;
699
700 for (i = 0; i < invalid_segment; i++) {
701 if (mmu_entry_pool[i].locked)
702 continue;
703 sun4c_init_clean_segmap(i);
704 add_ring(&sun4c_ufree_ring, &mmu_entry_pool[i]);
705 }
706 }
707
708 static void sun4c_kernel_unmap(struct sun4c_mmu_entry *kentry)
709 {
710 int savectx, ctx;
711
712 savectx = sun4c_get_context();
713 for (ctx = 0; ctx < num_contexts; ctx++) {
714 sun4c_set_context(ctx);
715 sun4c_put_segmap(kentry->vaddr, invalid_segment);
716 }
717 sun4c_set_context(savectx);
718 }
719
720 static void sun4c_kernel_map(struct sun4c_mmu_entry *kentry)
721 {
722 int savectx, ctx;
723
724 savectx = sun4c_get_context();
725 for (ctx = 0; ctx < num_contexts; ctx++) {
726 sun4c_set_context(ctx);
727 sun4c_put_segmap(kentry->vaddr, kentry->pseg);
728 }
729 sun4c_set_context(savectx);
730 }
731
732 #define sun4c_user_unmap(__entry) \
733 sun4c_put_segmap((__entry)->vaddr, invalid_segment)
734
735 static void sun4c_demap_context(struct sun4c_mmu_ring *crp, unsigned char ctx)
736 {
737 struct sun4c_mmu_entry *head = &crp->ringhd;
738 unsigned long flags;
739
740 local_irq_save(flags);
741 if (head->next != head) {
742 struct sun4c_mmu_entry *entry = head->next;
743 int savectx = sun4c_get_context();
744
745 flush_user_windows();
746 sun4c_set_context(ctx);
747 sun4c_flush_context();
748 do {
749 struct sun4c_mmu_entry *next = entry->next;
750
751 sun4c_user_unmap(entry);
752 free_user_entry(ctx, entry);
753
754 entry = next;
755 } while (entry != head);
756 sun4c_set_context(savectx);
757 }
758 local_irq_restore(flags);
759 }
760
761 static int sun4c_user_taken_entries; /* This is how much we have. */
762 static int max_user_taken_entries; /* This limits us and prevents deadlock. */
763
764 static struct sun4c_mmu_entry *sun4c_kernel_strategy(void)
765 {
766 struct sun4c_mmu_entry *this_entry;
767
768 /* If some are free, return first one. */
769 if (sun4c_kfree_ring.num_entries) {
770 this_entry = sun4c_kfree_ring.ringhd.next;
771 return this_entry;
772 }
773
774 /* Else free one up. */
775 this_entry = sun4c_kernel_ring.ringhd.prev;
776 sun4c_flush_segment(this_entry->vaddr);
777 sun4c_kernel_unmap(this_entry);
778 free_kernel_entry(this_entry, &sun4c_kernel_ring);
779 this_entry = sun4c_kfree_ring.ringhd.next;
780
781 return this_entry;
782 }
783
784 /* Using this method to free up mmu entries eliminates a lot of
785 * potential races since we have a kernel that incurs tlb
786 * replacement faults. There may be performance penalties.
787 *
788 * NOTE: Must be called with interrupts disabled.
789 */
790 static struct sun4c_mmu_entry *sun4c_user_strategy(void)
791 {
792 struct sun4c_mmu_entry *entry;
793 unsigned char ctx;
794 int savectx;
795
796 /* If some are free, return first one. */
797 if (sun4c_ufree_ring.num_entries) {
798 entry = sun4c_ufree_ring.ringhd.next;
799 goto unlink_out;
800 }
801
802 if (sun4c_user_taken_entries) {
803 entry = sun4c_kernel_strategy();
804 sun4c_user_taken_entries--;
805 goto kunlink_out;
806 }
807
808 /* Grab from the beginning of the LRU list. */
809 entry = sun4c_ulru_ring.ringhd.lru_next;
810 ctx = entry->ctx;
811
812 savectx = sun4c_get_context();
813 flush_user_windows();
814 sun4c_set_context(ctx);
815 sun4c_flush_segment(entry->vaddr);
816 sun4c_user_unmap(entry);
817 remove_ring(sun4c_context_ring + ctx, entry);
818 remove_lru(entry);
819 sun4c_set_context(savectx);
820
821 return entry;
822
823 unlink_out:
824 remove_ring(&sun4c_ufree_ring, entry);
825 return entry;
826 kunlink_out:
827 remove_ring(&sun4c_kfree_ring, entry);
828 return entry;
829 }
830
831 /* NOTE: Must be called with interrupts disabled. */
832 void sun4c_grow_kernel_ring(void)
833 {
834 struct sun4c_mmu_entry *entry;
835
836 /* Prevent deadlock condition. */
837 if (sun4c_user_taken_entries >= max_user_taken_entries)
838 return;
839
840 if (sun4c_ufree_ring.num_entries) {
841 entry = sun4c_ufree_ring.ringhd.next;
842 remove_ring(&sun4c_ufree_ring, entry);
843 add_ring(&sun4c_kfree_ring, entry);
844 sun4c_user_taken_entries++;
845 }
846 }
847
848 /* 2 page buckets for task struct and kernel stack allocation.
849 *
850 * TASK_STACK_BEGIN
851 * bucket[0]
852 * bucket[1]
853 * [ ... ]
854 * bucket[NR_TASK_BUCKETS-1]
855 * TASK_STACK_BEGIN + (sizeof(struct task_bucket) * NR_TASK_BUCKETS)
856 *
857 * Each slot looks like:
858 *
859 * page 1 -- task struct + beginning of kernel stack
860 * page 2 -- rest of kernel stack
861 */
862
863 union task_union *sun4c_bucket[NR_TASK_BUCKETS];
864
865 static int sun4c_lowbucket_avail;
866
867 #define BUCKET_EMPTY ((union task_union *) 0)
868 #define BUCKET_SHIFT (PAGE_SHIFT + 1) /* log2(sizeof(struct task_bucket)) */
869 #define BUCKET_SIZE (1 << BUCKET_SHIFT)
870 #define BUCKET_NUM(addr) ((((addr) - SUN4C_LOCK_VADDR) >> BUCKET_SHIFT))
871 #define BUCKET_ADDR(num) (((num) << BUCKET_SHIFT) + SUN4C_LOCK_VADDR)
872 #define BUCKET_PTE(page) \
873 ((((page) - PAGE_OFFSET) >> PAGE_SHIFT) | pgprot_val(SUN4C_PAGE_KERNEL))
874 #define BUCKET_PTE_PAGE(pte) \
875 (PAGE_OFFSET + (((pte) & SUN4C_PFN_MASK) << PAGE_SHIFT))
876
877 static void get_locked_segment(unsigned long addr)
878 {
879 struct sun4c_mmu_entry *stolen;
880 unsigned long flags;
881
882 local_irq_save(flags);
883 addr &= SUN4C_REAL_PGDIR_MASK;
884 stolen = sun4c_user_strategy();
885 max_user_taken_entries--;
886 stolen->vaddr = addr;
887 flush_user_windows();
888 sun4c_kernel_map(stolen);
889 local_irq_restore(flags);
890 }
891
892 static void free_locked_segment(unsigned long addr)
893 {
894 struct sun4c_mmu_entry *entry;
895 unsigned long flags;
896 unsigned char pseg;
897
898 local_irq_save(flags);
899 addr &= SUN4C_REAL_PGDIR_MASK;
900 pseg = sun4c_get_segmap(addr);
901 entry = &mmu_entry_pool[pseg];
902
903 flush_user_windows();
904 sun4c_flush_segment(addr);
905 sun4c_kernel_unmap(entry);
906 add_ring(&sun4c_ufree_ring, entry);
907 max_user_taken_entries++;
908 local_irq_restore(flags);
909 }
910
911 static inline void garbage_collect(int entry)
912 {
913 int start, end;
914
915 /* 32 buckets per segment... */
916 entry &= ~31;
917 start = entry;
918 for (end = (start + 32); start < end; start++)
919 if (sun4c_bucket[start] != BUCKET_EMPTY)
920 return;
921
922 /* Entire segment empty, release it. */
923 free_locked_segment(BUCKET_ADDR(entry));
924 }
925
926 static struct thread_info *sun4c_alloc_thread_info(void)
927 {
928 unsigned long addr, pages;
929 int entry;
930
931 pages = __get_free_pages(GFP_KERNEL, THREAD_INFO_ORDER);
932 if (!pages)
933 return NULL;
934
935 for (entry = sun4c_lowbucket_avail; entry < NR_TASK_BUCKETS; entry++)
936 if (sun4c_bucket[entry] == BUCKET_EMPTY)
937 break;
938 if (entry == NR_TASK_BUCKETS) {
939 free_pages(pages, THREAD_INFO_ORDER);
940 return NULL;
941 }
942 if (entry >= sun4c_lowbucket_avail)
943 sun4c_lowbucket_avail = entry + 1;
944
945 addr = BUCKET_ADDR(entry);
946 sun4c_bucket[entry] = (union task_union *) addr;
947 if(sun4c_get_segmap(addr) == invalid_segment)
948 get_locked_segment(addr);
949
950 /* We are changing the virtual color of the page(s)
951 * so we must flush the cache to guarantee consistency.
952 */
953 sun4c_flush_page(pages);
954 sun4c_flush_page(pages + PAGE_SIZE);
955
956 sun4c_put_pte(addr, BUCKET_PTE(pages));
957 sun4c_put_pte(addr + PAGE_SIZE, BUCKET_PTE(pages + PAGE_SIZE));
958
959 #ifdef CONFIG_DEBUG_STACK_USAGE
960 memset((void *)addr, 0, PAGE_SIZE << THREAD_INFO_ORDER);
961 #endif /* DEBUG_STACK_USAGE */
962
963 return (struct thread_info *) addr;
964 }
965
966 static void sun4c_free_thread_info(struct thread_info *ti)
967 {
968 unsigned long tiaddr = (unsigned long) ti;
969 unsigned long pages = BUCKET_PTE_PAGE(sun4c_get_pte(tiaddr));
970 int entry = BUCKET_NUM(tiaddr);
971
972 /* We are deleting a mapping, so the flush here is mandatory. */
973 sun4c_flush_page(tiaddr);
974 sun4c_flush_page(tiaddr + PAGE_SIZE);
975
976 sun4c_put_pte(tiaddr, 0);
977 sun4c_put_pte(tiaddr + PAGE_SIZE, 0);
978
979 sun4c_bucket[entry] = BUCKET_EMPTY;
980 if (entry < sun4c_lowbucket_avail)
981 sun4c_lowbucket_avail = entry;
982
983 free_pages(pages, THREAD_INFO_ORDER);
984 garbage_collect(entry);
985 }
986
987 static void __init sun4c_init_buckets(void)
988 {
989 int entry;
990
991 if (sizeof(union thread_union) != (PAGE_SIZE << THREAD_INFO_ORDER)) {
992 extern void thread_info_size_is_bolixed_pete(void);
993 thread_info_size_is_bolixed_pete();
994 }
995
996 for (entry = 0; entry < NR_TASK_BUCKETS; entry++)
997 sun4c_bucket[entry] = BUCKET_EMPTY;
998 sun4c_lowbucket_avail = 0;
999 }
1000
1001 static unsigned long sun4c_iobuffer_start;
1002 static unsigned long sun4c_iobuffer_end;
1003 static unsigned long sun4c_iobuffer_high;
1004 static unsigned long *sun4c_iobuffer_map;
1005 static int iobuffer_map_size;
1006
1007 /*
1008 * Alias our pages so they do not cause a trap.
1009 * Also one page may be aliased into several I/O areas and we may
1010 * finish these I/O separately.
1011 */
1012 static char *sun4c_lockarea(char *vaddr, unsigned long size)
1013 {
1014 unsigned long base, scan;
1015 unsigned long npages;
1016 unsigned long vpage;
1017 unsigned long pte;
1018 unsigned long apage;
1019 unsigned long high;
1020 unsigned long flags;
1021
1022 npages = (((unsigned long)vaddr & ~PAGE_MASK) +
1023 size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
1024
1025 local_irq_save(flags);
1026 base = bitmap_find_next_zero_area(sun4c_iobuffer_map, iobuffer_map_size,
1027 0, npages, 0);
1028 if (base >= iobuffer_map_size)
1029 goto abend;
1030
1031 high = ((base + npages) << PAGE_SHIFT) + sun4c_iobuffer_start;
1032 high = SUN4C_REAL_PGDIR_ALIGN(high);
1033 while (high > sun4c_iobuffer_high) {
1034 get_locked_segment(sun4c_iobuffer_high);
1035 sun4c_iobuffer_high += SUN4C_REAL_PGDIR_SIZE;
1036 }
1037
1038 vpage = ((unsigned long) vaddr) & PAGE_MASK;
1039 for (scan = base; scan < base+npages; scan++) {
1040 pte = ((vpage-PAGE_OFFSET) >> PAGE_SHIFT);
1041 pte |= pgprot_val(SUN4C_PAGE_KERNEL);
1042 pte |= _SUN4C_PAGE_NOCACHE;
1043 set_bit(scan, sun4c_iobuffer_map);
1044 apage = (scan << PAGE_SHIFT) + sun4c_iobuffer_start;
1045
1046 /* Flush original mapping so we see the right things later. */
1047 sun4c_flush_page(vpage);
1048
1049 sun4c_put_pte(apage, pte);
1050 vpage += PAGE_SIZE;
1051 }
1052 local_irq_restore(flags);
1053 return (char *) ((base << PAGE_SHIFT) + sun4c_iobuffer_start +
1054 (((unsigned long) vaddr) & ~PAGE_MASK));
1055
1056 abend:
1057 local_irq_restore(flags);
1058 printk("DMA vaddr=0x%p size=%08lx\n", vaddr, size);
1059 panic("Out of iobuffer table");
1060 return NULL;
1061 }
1062
1063 static void sun4c_unlockarea(char *vaddr, unsigned long size)
1064 {
1065 unsigned long vpage, npages;
1066 unsigned long flags;
1067 int scan, high;
1068
1069 vpage = (unsigned long)vaddr & PAGE_MASK;
1070 npages = (((unsigned long)vaddr & ~PAGE_MASK) +
1071 size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
1072
1073 local_irq_save(flags);
1074 while (npages != 0) {
1075 --npages;
1076
1077 /* This mapping is marked non-cachable, no flush necessary. */
1078 sun4c_put_pte(vpage, 0);
1079 clear_bit((vpage - sun4c_iobuffer_start) >> PAGE_SHIFT,
1080 sun4c_iobuffer_map);
1081 vpage += PAGE_SIZE;
1082 }
1083
1084 /* garbage collect */
1085 scan = (sun4c_iobuffer_high - sun4c_iobuffer_start) >> PAGE_SHIFT;
1086 while (scan >= 0 && !sun4c_iobuffer_map[scan >> 5])
1087 scan -= 32;
1088 scan += 32;
1089 high = sun4c_iobuffer_start + (scan << PAGE_SHIFT);
1090 high = SUN4C_REAL_PGDIR_ALIGN(high) + SUN4C_REAL_PGDIR_SIZE;
1091 while (high < sun4c_iobuffer_high) {
1092 sun4c_iobuffer_high -= SUN4C_REAL_PGDIR_SIZE;
1093 free_locked_segment(sun4c_iobuffer_high);
1094 }
1095 local_irq_restore(flags);
1096 }
1097
1098 /* Note the scsi code at init time passes to here buffers
1099 * which sit on the kernel stack, those are already locked
1100 * by implication and fool the page locking code above
1101 * if passed to by mistake.
1102 */
1103 static __u32 sun4c_get_scsi_one(struct device *dev, char *bufptr, unsigned long len)
1104 {
1105 unsigned long page;
1106
1107 page = ((unsigned long)bufptr) & PAGE_MASK;
1108 if (!virt_addr_valid(page)) {
1109 sun4c_flush_page(page);
1110 return (__u32)bufptr; /* already locked */
1111 }
1112 return (__u32)sun4c_lockarea(bufptr, len);
1113 }
1114
1115 static void sun4c_get_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
1116 {
1117 while (sz != 0) {
1118 --sz;
1119 sg->dma_address = (__u32)sun4c_lockarea(sg_virt(sg), sg->length);
1120 sg->dma_length = sg->length;
1121 sg = sg_next(sg);
1122 }
1123 }
1124
1125 static void sun4c_release_scsi_one(struct device *dev, __u32 bufptr, unsigned long len)
1126 {
1127 if (bufptr < sun4c_iobuffer_start)
1128 return; /* On kernel stack or similar, see above */
1129 sun4c_unlockarea((char *)bufptr, len);
1130 }
1131
1132 static void sun4c_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
1133 {
1134 while (sz != 0) {
1135 --sz;
1136 sun4c_unlockarea((char *)sg->dma_address, sg->length);
1137 sg = sg_next(sg);
1138 }
1139 }
1140
1141 #define TASK_ENTRY_SIZE BUCKET_SIZE /* see above */
1142 #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
1143
1144 struct vm_area_struct sun4c_kstack_vma;
1145
1146 static void __init sun4c_init_lock_areas(void)
1147 {
1148 unsigned long sun4c_taskstack_start;
1149 unsigned long sun4c_taskstack_end;
1150 int bitmap_size;
1151
1152 sun4c_init_buckets();
1153 sun4c_taskstack_start = SUN4C_LOCK_VADDR;
1154 sun4c_taskstack_end = (sun4c_taskstack_start +
1155 (TASK_ENTRY_SIZE * NR_TASK_BUCKETS));
1156 if (sun4c_taskstack_end >= SUN4C_LOCK_END) {
1157 prom_printf("Too many tasks, decrease NR_TASK_BUCKETS please.\n");
1158 prom_halt();
1159 }
1160
1161 sun4c_iobuffer_start = sun4c_iobuffer_high =
1162 SUN4C_REAL_PGDIR_ALIGN(sun4c_taskstack_end);
1163 sun4c_iobuffer_end = SUN4C_LOCK_END;
1164 bitmap_size = (sun4c_iobuffer_end - sun4c_iobuffer_start) >> PAGE_SHIFT;
1165 bitmap_size = (bitmap_size + 7) >> 3;
1166 bitmap_size = LONG_ALIGN(bitmap_size);
1167 iobuffer_map_size = bitmap_size << 3;
1168 sun4c_iobuffer_map = __alloc_bootmem(bitmap_size, SMP_CACHE_BYTES, 0UL);
1169 memset((void *) sun4c_iobuffer_map, 0, bitmap_size);
1170
1171 sun4c_kstack_vma.vm_mm = &init_mm;
1172 sun4c_kstack_vma.vm_start = sun4c_taskstack_start;
1173 sun4c_kstack_vma.vm_end = sun4c_taskstack_end;
1174 sun4c_kstack_vma.vm_page_prot = PAGE_SHARED;
1175 sun4c_kstack_vma.vm_flags = VM_READ | VM_WRITE | VM_EXEC;
1176 insert_vm_struct(&init_mm, &sun4c_kstack_vma);
1177 }
1178
1179 /* Cache flushing on the sun4c. */
1180 static void sun4c_flush_cache_all(void)
1181 {
1182 unsigned long begin, end;
1183
1184 flush_user_windows();
1185 begin = (KERNBASE + SUN4C_REAL_PGDIR_SIZE);
1186 end = (begin + SUN4C_VAC_SIZE);
1187
1188 if (sun4c_vacinfo.linesize == 32) {
1189 while (begin < end) {
1190 __asm__ __volatile__(
1191 "ld [%0 + 0x00], %%g0\n\t"
1192 "ld [%0 + 0x20], %%g0\n\t"
1193 "ld [%0 + 0x40], %%g0\n\t"
1194 "ld [%0 + 0x60], %%g0\n\t"
1195 "ld [%0 + 0x80], %%g0\n\t"
1196 "ld [%0 + 0xa0], %%g0\n\t"
1197 "ld [%0 + 0xc0], %%g0\n\t"
1198 "ld [%0 + 0xe0], %%g0\n\t"
1199 "ld [%0 + 0x100], %%g0\n\t"
1200 "ld [%0 + 0x120], %%g0\n\t"
1201 "ld [%0 + 0x140], %%g0\n\t"
1202 "ld [%0 + 0x160], %%g0\n\t"
1203 "ld [%0 + 0x180], %%g0\n\t"
1204 "ld [%0 + 0x1a0], %%g0\n\t"
1205 "ld [%0 + 0x1c0], %%g0\n\t"
1206 "ld [%0 + 0x1e0], %%g0\n"
1207 : : "r" (begin));
1208 begin += 512;
1209 }
1210 } else {
1211 while (begin < end) {
1212 __asm__ __volatile__(
1213 "ld [%0 + 0x00], %%g0\n\t"
1214 "ld [%0 + 0x10], %%g0\n\t"
1215 "ld [%0 + 0x20], %%g0\n\t"
1216 "ld [%0 + 0x30], %%g0\n\t"
1217 "ld [%0 + 0x40], %%g0\n\t"
1218 "ld [%0 + 0x50], %%g0\n\t"
1219 "ld [%0 + 0x60], %%g0\n\t"
1220 "ld [%0 + 0x70], %%g0\n\t"
1221 "ld [%0 + 0x80], %%g0\n\t"
1222 "ld [%0 + 0x90], %%g0\n\t"
1223 "ld [%0 + 0xa0], %%g0\n\t"
1224 "ld [%0 + 0xb0], %%g0\n\t"
1225 "ld [%0 + 0xc0], %%g0\n\t"
1226 "ld [%0 + 0xd0], %%g0\n\t"
1227 "ld [%0 + 0xe0], %%g0\n\t"
1228 "ld [%0 + 0xf0], %%g0\n"
1229 : : "r" (begin));
1230 begin += 256;
1231 }
1232 }
1233 }
1234
1235 static void sun4c_flush_cache_mm(struct mm_struct *mm)
1236 {
1237 int new_ctx = mm->context;
1238
1239 if (new_ctx != NO_CONTEXT) {
1240 flush_user_windows();
1241
1242 if (sun4c_context_ring[new_ctx].num_entries) {
1243 struct sun4c_mmu_entry *head = &sun4c_context_ring[new_ctx].ringhd;
1244 unsigned long flags;
1245
1246 local_irq_save(flags);
1247 if (head->next != head) {
1248 struct sun4c_mmu_entry *entry = head->next;
1249 int savectx = sun4c_get_context();
1250
1251 sun4c_set_context(new_ctx);
1252 sun4c_flush_context();
1253 do {
1254 struct sun4c_mmu_entry *next = entry->next;
1255
1256 sun4c_user_unmap(entry);
1257 free_user_entry(new_ctx, entry);
1258
1259 entry = next;
1260 } while (entry != head);
1261 sun4c_set_context(savectx);
1262 }
1263 local_irq_restore(flags);
1264 }
1265 }
1266 }
1267
1268 static void sun4c_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1269 {
1270 struct mm_struct *mm = vma->vm_mm;
1271 int new_ctx = mm->context;
1272
1273 if (new_ctx != NO_CONTEXT) {
1274 struct sun4c_mmu_entry *head = &sun4c_context_ring[new_ctx].ringhd;
1275 struct sun4c_mmu_entry *entry;
1276 unsigned long flags;
1277
1278 flush_user_windows();
1279
1280 local_irq_save(flags);
1281 /* All user segmap chains are ordered on entry->vaddr. */
1282 for (entry = head->next;
1283 (entry != head) && ((entry->vaddr+SUN4C_REAL_PGDIR_SIZE) < start);
1284 entry = entry->next)
1285 ;
1286
1287 /* Tracing various job mixtures showed that this conditional
1288 * only passes ~35% of the time for most worse case situations,
1289 * therefore we avoid all of this gross overhead ~65% of the time.
1290 */
1291 if ((entry != head) && (entry->vaddr < end)) {
1292 int octx = sun4c_get_context();
1293 sun4c_set_context(new_ctx);
1294
1295 /* At this point, always, (start >= entry->vaddr) and
1296 * (entry->vaddr < end), once the latter condition
1297 * ceases to hold, or we hit the end of the list, we
1298 * exit the loop. The ordering of all user allocated
1299 * segmaps makes this all work out so beautifully.
1300 */
1301 do {
1302 struct sun4c_mmu_entry *next = entry->next;
1303 unsigned long realend;
1304
1305 /* "realstart" is always >= entry->vaddr */
1306 realend = entry->vaddr + SUN4C_REAL_PGDIR_SIZE;
1307 if (end < realend)
1308 realend = end;
1309 if ((realend - entry->vaddr) <= (PAGE_SIZE << 3)) {
1310 unsigned long page = entry->vaddr;
1311 while (page < realend) {
1312 sun4c_flush_page(page);
1313 page += PAGE_SIZE;
1314 }
1315 } else {
1316 sun4c_flush_segment(entry->vaddr);
1317 sun4c_user_unmap(entry);
1318 free_user_entry(new_ctx, entry);
1319 }
1320 entry = next;
1321 } while ((entry != head) && (entry->vaddr < end));
1322 sun4c_set_context(octx);
1323 }
1324 local_irq_restore(flags);
1325 }
1326 }
1327
1328 static void sun4c_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
1329 {
1330 struct mm_struct *mm = vma->vm_mm;
1331 int new_ctx = mm->context;
1332
1333 /* Sun4c has no separate I/D caches so cannot optimize for non
1334 * text page flushes.
1335 */
1336 if (new_ctx != NO_CONTEXT) {
1337 int octx = sun4c_get_context();
1338 unsigned long flags;
1339
1340 flush_user_windows();
1341 local_irq_save(flags);
1342 sun4c_set_context(new_ctx);
1343 sun4c_flush_page(page);
1344 sun4c_set_context(octx);
1345 local_irq_restore(flags);
1346 }
1347 }
1348
1349 static void sun4c_flush_page_to_ram(unsigned long page)
1350 {
1351 unsigned long flags;
1352
1353 local_irq_save(flags);
1354 sun4c_flush_page(page);
1355 local_irq_restore(flags);
1356 }
1357
1358 /* Sun4c cache is unified, both instructions and data live there, so
1359 * no need to flush the on-stack instructions for new signal handlers.
1360 */
1361 static void sun4c_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
1362 {
1363 }
1364
1365 /* TLB flushing on the sun4c. These routines count on the cache
1366 * flushing code to flush the user register windows so that we need
1367 * not do so when we get here.
1368 */
1369
1370 static void sun4c_flush_tlb_all(void)
1371 {
1372 struct sun4c_mmu_entry *this_entry, *next_entry;
1373 unsigned long flags;
1374 int savectx, ctx;
1375
1376 local_irq_save(flags);
1377 this_entry = sun4c_kernel_ring.ringhd.next;
1378 savectx = sun4c_get_context();
1379 flush_user_windows();
1380 while (sun4c_kernel_ring.num_entries) {
1381 next_entry = this_entry->next;
1382 sun4c_flush_segment(this_entry->vaddr);
1383 for (ctx = 0; ctx < num_contexts; ctx++) {
1384 sun4c_set_context(ctx);
1385 sun4c_put_segmap(this_entry->vaddr, invalid_segment);
1386 }
1387 free_kernel_entry(this_entry, &sun4c_kernel_ring);
1388 this_entry = next_entry;
1389 }
1390 sun4c_set_context(savectx);
1391 local_irq_restore(flags);
1392 }
1393
1394 static void sun4c_flush_tlb_mm(struct mm_struct *mm)
1395 {
1396 int new_ctx = mm->context;
1397
1398 if (new_ctx != NO_CONTEXT) {
1399 struct sun4c_mmu_entry *head = &sun4c_context_ring[new_ctx].ringhd;
1400 unsigned long flags;
1401
1402 local_irq_save(flags);
1403 if (head->next != head) {
1404 struct sun4c_mmu_entry *entry = head->next;
1405 int savectx = sun4c_get_context();
1406
1407 sun4c_set_context(new_ctx);
1408 sun4c_flush_context();
1409 do {
1410 struct sun4c_mmu_entry *next = entry->next;
1411
1412 sun4c_user_unmap(entry);
1413 free_user_entry(new_ctx, entry);
1414
1415 entry = next;
1416 } while (entry != head);
1417 sun4c_set_context(savectx);
1418 }
1419 local_irq_restore(flags);
1420 }
1421 }
1422
1423 static void sun4c_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1424 {
1425 struct mm_struct *mm = vma->vm_mm;
1426 int new_ctx = mm->context;
1427
1428 if (new_ctx != NO_CONTEXT) {
1429 struct sun4c_mmu_entry *head = &sun4c_context_ring[new_ctx].ringhd;
1430 struct sun4c_mmu_entry *entry;
1431 unsigned long flags;
1432
1433 local_irq_save(flags);
1434 /* See commentary in sun4c_flush_cache_range(). */
1435 for (entry = head->next;
1436 (entry != head) && ((entry->vaddr+SUN4C_REAL_PGDIR_SIZE) < start);
1437 entry = entry->next)
1438 ;
1439
1440 if ((entry != head) && (entry->vaddr < end)) {
1441 int octx = sun4c_get_context();
1442
1443 sun4c_set_context(new_ctx);
1444 do {
1445 struct sun4c_mmu_entry *next = entry->next;
1446
1447 sun4c_flush_segment(entry->vaddr);
1448 sun4c_user_unmap(entry);
1449 free_user_entry(new_ctx, entry);
1450
1451 entry = next;
1452 } while ((entry != head) && (entry->vaddr < end));
1453 sun4c_set_context(octx);
1454 }
1455 local_irq_restore(flags);
1456 }
1457 }
1458
1459 static void sun4c_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
1460 {
1461 struct mm_struct *mm = vma->vm_mm;
1462 int new_ctx = mm->context;
1463
1464 if (new_ctx != NO_CONTEXT) {
1465 int savectx = sun4c_get_context();
1466 unsigned long flags;
1467
1468 local_irq_save(flags);
1469 sun4c_set_context(new_ctx);
1470 page &= PAGE_MASK;
1471 sun4c_flush_page(page);
1472 sun4c_put_pte(page, 0);
1473 sun4c_set_context(savectx);
1474 local_irq_restore(flags);
1475 }
1476 }
1477
1478 static inline void sun4c_mapioaddr(unsigned long physaddr, unsigned long virt_addr)
1479 {
1480 unsigned long page_entry, pg_iobits;
1481
1482 pg_iobits = _SUN4C_PAGE_PRESENT | _SUN4C_READABLE | _SUN4C_WRITEABLE |
1483 _SUN4C_PAGE_IO | _SUN4C_PAGE_NOCACHE;
1484
1485 page_entry = ((physaddr >> PAGE_SHIFT) & SUN4C_PFN_MASK);
1486 page_entry |= ((pg_iobits | _SUN4C_PAGE_PRIV) & ~(_SUN4C_PAGE_PRESENT));
1487 sun4c_put_pte(virt_addr, page_entry);
1488 }
1489
1490 static void sun4c_mapiorange(unsigned int bus, unsigned long xpa,
1491 unsigned long xva, unsigned int len)
1492 {
1493 while (len != 0) {
1494 len -= PAGE_SIZE;
1495 sun4c_mapioaddr(xpa, xva);
1496 xva += PAGE_SIZE;
1497 xpa += PAGE_SIZE;
1498 }
1499 }
1500
1501 static void sun4c_unmapiorange(unsigned long virt_addr, unsigned int len)
1502 {
1503 while (len != 0) {
1504 len -= PAGE_SIZE;
1505 sun4c_put_pte(virt_addr, 0);
1506 virt_addr += PAGE_SIZE;
1507 }
1508 }
1509
1510 static void sun4c_alloc_context(struct mm_struct *old_mm, struct mm_struct *mm)
1511 {
1512 struct ctx_list *ctxp;
1513
1514 ctxp = ctx_free.next;
1515 if (ctxp != &ctx_free) {
1516 remove_from_ctx_list(ctxp);
1517 add_to_used_ctxlist(ctxp);
1518 mm->context = ctxp->ctx_number;
1519 ctxp->ctx_mm = mm;
1520 return;
1521 }
1522 ctxp = ctx_used.next;
1523 if (ctxp->ctx_mm == old_mm)
1524 ctxp = ctxp->next;
1525 remove_from_ctx_list(ctxp);
1526 add_to_used_ctxlist(ctxp);
1527 ctxp->ctx_mm->context = NO_CONTEXT;
1528 ctxp->ctx_mm = mm;
1529 mm->context = ctxp->ctx_number;
1530 sun4c_demap_context(&sun4c_context_ring[ctxp->ctx_number],
1531 ctxp->ctx_number);
1532 }
1533
1534 /* Switch the current MM context. */
1535 static void sun4c_switch_mm(struct mm_struct *old_mm, struct mm_struct *mm, struct task_struct *tsk, int cpu)
1536 {
1537 struct ctx_list *ctx;
1538 int dirty = 0;
1539
1540 if (mm->context == NO_CONTEXT) {
1541 dirty = 1;
1542 sun4c_alloc_context(old_mm, mm);
1543 } else {
1544 /* Update the LRU ring of contexts. */
1545 ctx = ctx_list_pool + mm->context;
1546 remove_from_ctx_list(ctx);
1547 add_to_used_ctxlist(ctx);
1548 }
1549 if (dirty || old_mm != mm)
1550 sun4c_set_context(mm->context);
1551 }
1552
1553 static void sun4c_destroy_context(struct mm_struct *mm)
1554 {
1555 struct ctx_list *ctx_old;
1556
1557 if (mm->context != NO_CONTEXT) {
1558 sun4c_demap_context(&sun4c_context_ring[mm->context], mm->context);
1559 ctx_old = ctx_list_pool + mm->context;
1560 remove_from_ctx_list(ctx_old);
1561 add_to_free_ctxlist(ctx_old);
1562 mm->context = NO_CONTEXT;
1563 }
1564 }
1565
1566 static void sun4c_mmu_info(struct seq_file *m)
1567 {
1568 int used_user_entries, i;
1569
1570 used_user_entries = 0;
1571 for (i = 0; i < num_contexts; i++)
1572 used_user_entries += sun4c_context_ring[i].num_entries;
1573
1574 seq_printf(m,
1575 "vacsize\t\t: %d bytes\n"
1576 "vachwflush\t: %s\n"
1577 "vaclinesize\t: %d bytes\n"
1578 "mmuctxs\t\t: %d\n"
1579 "mmupsegs\t: %d\n"
1580 "kernelpsegs\t: %d\n"
1581 "kfreepsegs\t: %d\n"
1582 "usedpsegs\t: %d\n"
1583 "ufreepsegs\t: %d\n"
1584 "user_taken\t: %d\n"
1585 "max_taken\t: %d\n",
1586 sun4c_vacinfo.num_bytes,
1587 (sun4c_vacinfo.do_hwflushes ? "yes" : "no"),
1588 sun4c_vacinfo.linesize,
1589 num_contexts,
1590 (invalid_segment + 1),
1591 sun4c_kernel_ring.num_entries,
1592 sun4c_kfree_ring.num_entries,
1593 used_user_entries,
1594 sun4c_ufree_ring.num_entries,
1595 sun4c_user_taken_entries,
1596 max_user_taken_entries);
1597 }
1598
1599 /* Nothing below here should touch the mmu hardware nor the mmu_entry
1600 * data structures.
1601 */
1602
1603 /* First the functions which the mid-level code uses to directly
1604 * manipulate the software page tables. Some defines since we are
1605 * emulating the i386 page directory layout.
1606 */
1607 #define PGD_PRESENT 0x001
1608 #define PGD_RW 0x002
1609 #define PGD_USER 0x004
1610 #define PGD_ACCESSED 0x020
1611 #define PGD_DIRTY 0x040
1612 #define PGD_TABLE (PGD_PRESENT | PGD_RW | PGD_USER | PGD_ACCESSED | PGD_DIRTY)
1613
1614 static void sun4c_set_pte(pte_t *ptep, pte_t pte)
1615 {
1616 *ptep = pte;
1617 }
1618
1619 static void sun4c_pgd_set(pgd_t * pgdp, pmd_t * pmdp)
1620 {
1621 }
1622
1623 static void sun4c_pmd_set(pmd_t * pmdp, pte_t * ptep)
1624 {
1625 pmdp->pmdv[0] = PGD_TABLE | (unsigned long) ptep;
1626 }
1627
1628 static void sun4c_pmd_populate(pmd_t * pmdp, struct page * ptep)
1629 {
1630 if (page_address(ptep) == NULL) BUG(); /* No highmem on sun4c */
1631 pmdp->pmdv[0] = PGD_TABLE | (unsigned long) page_address(ptep);
1632 }
1633
1634 static int sun4c_pte_present(pte_t pte)
1635 {
1636 return ((pte_val(pte) & (_SUN4C_PAGE_PRESENT | _SUN4C_PAGE_PRIV)) != 0);
1637 }
1638 static void sun4c_pte_clear(pte_t *ptep) { *ptep = __pte(0); }
1639
1640 static int sun4c_pmd_bad(pmd_t pmd)
1641 {
1642 return (((pmd_val(pmd) & ~PAGE_MASK) != PGD_TABLE) ||
1643 (!virt_addr_valid(pmd_val(pmd))));
1644 }
1645
1646 static int sun4c_pmd_present(pmd_t pmd)
1647 {
1648 return ((pmd_val(pmd) & PGD_PRESENT) != 0);
1649 }
1650
1651 #if 0 /* if PMD takes one word */
1652 static void sun4c_pmd_clear(pmd_t *pmdp) { *pmdp = __pmd(0); }
1653 #else /* if pmd_t is a longish aggregate */
1654 static void sun4c_pmd_clear(pmd_t *pmdp) {
1655 memset((void *)pmdp, 0, sizeof(pmd_t));
1656 }
1657 #endif
1658
1659 static int sun4c_pgd_none(pgd_t pgd) { return 0; }
1660 static int sun4c_pgd_bad(pgd_t pgd) { return 0; }
1661 static int sun4c_pgd_present(pgd_t pgd) { return 1; }
1662 static void sun4c_pgd_clear(pgd_t * pgdp) { }
1663
1664 /*
1665 * The following only work if pte_present() is true.
1666 * Undefined behaviour if not..
1667 */
1668 static pte_t sun4c_pte_mkwrite(pte_t pte)
1669 {
1670 pte = __pte(pte_val(pte) | _SUN4C_PAGE_WRITE);
1671 if (pte_val(pte) & _SUN4C_PAGE_MODIFIED)
1672 pte = __pte(pte_val(pte) | _SUN4C_PAGE_SILENT_WRITE);
1673 return pte;
1674 }
1675
1676 static pte_t sun4c_pte_mkdirty(pte_t pte)
1677 {
1678 pte = __pte(pte_val(pte) | _SUN4C_PAGE_MODIFIED);
1679 if (pte_val(pte) & _SUN4C_PAGE_WRITE)
1680 pte = __pte(pte_val(pte) | _SUN4C_PAGE_SILENT_WRITE);
1681 return pte;
1682 }
1683
1684 static pte_t sun4c_pte_mkyoung(pte_t pte)
1685 {
1686 pte = __pte(pte_val(pte) | _SUN4C_PAGE_ACCESSED);
1687 if (pte_val(pte) & _SUN4C_PAGE_READ)
1688 pte = __pte(pte_val(pte) | _SUN4C_PAGE_SILENT_READ);
1689 return pte;
1690 }
1691
1692 /*
1693 * Conversion functions: convert a page and protection to a page entry,
1694 * and a page entry and page directory to the page they refer to.
1695 */
1696 static pte_t sun4c_mk_pte(struct page *page, pgprot_t pgprot)
1697 {
1698 return __pte(page_to_pfn(page) | pgprot_val(pgprot));
1699 }
1700
1701 static pte_t sun4c_mk_pte_phys(unsigned long phys_page, pgprot_t pgprot)
1702 {
1703 return __pte((phys_page >> PAGE_SHIFT) | pgprot_val(pgprot));
1704 }
1705
1706 static pte_t sun4c_mk_pte_io(unsigned long page, pgprot_t pgprot, int space)
1707 {
1708 return __pte(((page - PAGE_OFFSET) >> PAGE_SHIFT) | pgprot_val(pgprot));
1709 }
1710
1711 static unsigned long sun4c_pte_pfn(pte_t pte)
1712 {
1713 return pte_val(pte) & SUN4C_PFN_MASK;
1714 }
1715
1716 static pte_t sun4c_pgoff_to_pte(unsigned long pgoff)
1717 {
1718 return __pte(pgoff | _SUN4C_PAGE_FILE);
1719 }
1720
1721 static unsigned long sun4c_pte_to_pgoff(pte_t pte)
1722 {
1723 return pte_val(pte) & ((1UL << PTE_FILE_MAX_BITS) - 1);
1724 }
1725
1726
1727 static inline unsigned long sun4c_pmd_page_v(pmd_t pmd)
1728 {
1729 return (pmd_val(pmd) & PAGE_MASK);
1730 }
1731
1732 static struct page *sun4c_pmd_page(pmd_t pmd)
1733 {
1734 return virt_to_page(sun4c_pmd_page_v(pmd));
1735 }
1736
1737 static unsigned long sun4c_pgd_page(pgd_t pgd) { return 0; }
1738
1739 /* to find an entry in a page-table-directory */
1740 static inline pgd_t *sun4c_pgd_offset(struct mm_struct * mm, unsigned long address)
1741 {
1742 return mm->pgd + (address >> SUN4C_PGDIR_SHIFT);
1743 }
1744
1745 /* Find an entry in the second-level page table.. */
1746 static pmd_t *sun4c_pmd_offset(pgd_t * dir, unsigned long address)
1747 {
1748 return (pmd_t *) dir;
1749 }
1750
1751 /* Find an entry in the third-level page table.. */
1752 pte_t *sun4c_pte_offset_kernel(pmd_t * dir, unsigned long address)
1753 {
1754 return (pte_t *) sun4c_pmd_page_v(*dir) +
1755 ((address >> PAGE_SHIFT) & (SUN4C_PTRS_PER_PTE - 1));
1756 }
1757
1758 static unsigned long sun4c_swp_type(swp_entry_t entry)
1759 {
1760 return (entry.val & SUN4C_SWP_TYPE_MASK);
1761 }
1762
1763 static unsigned long sun4c_swp_offset(swp_entry_t entry)
1764 {
1765 return (entry.val >> SUN4C_SWP_OFF_SHIFT) & SUN4C_SWP_OFF_MASK;
1766 }
1767
1768 static swp_entry_t sun4c_swp_entry(unsigned long type, unsigned long offset)
1769 {
1770 return (swp_entry_t) {
1771 (offset & SUN4C_SWP_OFF_MASK) << SUN4C_SWP_OFF_SHIFT
1772 | (type & SUN4C_SWP_TYPE_MASK) };
1773 }
1774
1775 static void sun4c_free_pte_slow(pte_t *pte)
1776 {
1777 free_page((unsigned long)pte);
1778 }
1779
1780 static void sun4c_free_pgd_slow(pgd_t *pgd)
1781 {
1782 free_page((unsigned long)pgd);
1783 }
1784
1785 static pgd_t *sun4c_get_pgd_fast(void)
1786 {
1787 unsigned long *ret;
1788
1789 if ((ret = pgd_quicklist) != NULL) {
1790 pgd_quicklist = (unsigned long *)(*ret);
1791 ret[0] = ret[1];
1792 pgtable_cache_size--;
1793 } else {
1794 pgd_t *init;
1795
1796 ret = (unsigned long *)__get_free_page(GFP_KERNEL);
1797 memset (ret, 0, (KERNBASE / SUN4C_PGDIR_SIZE) * sizeof(pgd_t));
1798 init = sun4c_pgd_offset(&init_mm, 0);
1799 memcpy (((pgd_t *)ret) + USER_PTRS_PER_PGD, init + USER_PTRS_PER_PGD,
1800 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
1801 }
1802 return (pgd_t *)ret;
1803 }
1804
1805 static void sun4c_free_pgd_fast(pgd_t *pgd)
1806 {
1807 *(unsigned long *)pgd = (unsigned long) pgd_quicklist;
1808 pgd_quicklist = (unsigned long *) pgd;
1809 pgtable_cache_size++;
1810 }
1811
1812
1813 static inline pte_t *
1814 sun4c_pte_alloc_one_fast(struct mm_struct *mm, unsigned long address)
1815 {
1816 unsigned long *ret;
1817
1818 if ((ret = (unsigned long *)pte_quicklist) != NULL) {
1819 pte_quicklist = (unsigned long *)(*ret);
1820 ret[0] = ret[1];
1821 pgtable_cache_size--;
1822 }
1823 return (pte_t *)ret;
1824 }
1825
1826 static pte_t *sun4c_pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
1827 {
1828 pte_t *pte;
1829
1830 if ((pte = sun4c_pte_alloc_one_fast(mm, address)) != NULL)
1831 return pte;
1832
1833 pte = (pte_t *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT);
1834 return pte;
1835 }
1836
1837 static pgtable_t sun4c_pte_alloc_one(struct mm_struct *mm, unsigned long address)
1838 {
1839 pte_t *pte;
1840 struct page *page;
1841
1842 pte = sun4c_pte_alloc_one_kernel(mm, address);
1843 if (pte == NULL)
1844 return NULL;
1845 page = virt_to_page(pte);
1846 pgtable_page_ctor(page);
1847 return page;
1848 }
1849
1850 static inline void sun4c_free_pte_fast(pte_t *pte)
1851 {
1852 *(unsigned long *)pte = (unsigned long) pte_quicklist;
1853 pte_quicklist = (unsigned long *) pte;
1854 pgtable_cache_size++;
1855 }
1856
1857 static void sun4c_pte_free(pgtable_t pte)
1858 {
1859 pgtable_page_dtor(pte);
1860 sun4c_free_pte_fast(page_address(pte));
1861 }
1862
1863 /*
1864 * allocating and freeing a pmd is trivial: the 1-entry pmd is
1865 * inside the pgd, so has no extra memory associated with it.
1866 */
1867 static pmd_t *sun4c_pmd_alloc_one(struct mm_struct *mm, unsigned long address)
1868 {
1869 BUG();
1870 return NULL;
1871 }
1872
1873 static void sun4c_free_pmd_fast(pmd_t * pmd) { }
1874
1875 static void sun4c_check_pgt_cache(int low, int high)
1876 {
1877 if (pgtable_cache_size > high) {
1878 do {
1879 if (pgd_quicklist)
1880 sun4c_free_pgd_slow(sun4c_get_pgd_fast());
1881 if (pte_quicklist)
1882 sun4c_free_pte_slow(sun4c_pte_alloc_one_fast(NULL, 0));
1883 } while (pgtable_cache_size > low);
1884 }
1885 }
1886
1887 /* An experiment, turn off by default for now... -DaveM */
1888 #define SUN4C_PRELOAD_PSEG
1889
1890 void sun4c_update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
1891 {
1892 unsigned long flags;
1893 int pseg;
1894
1895 if (vma->vm_mm->context == NO_CONTEXT)
1896 return;
1897
1898 local_irq_save(flags);
1899 address &= PAGE_MASK;
1900 if ((pseg = sun4c_get_segmap(address)) == invalid_segment) {
1901 struct sun4c_mmu_entry *entry = sun4c_user_strategy();
1902 struct mm_struct *mm = vma->vm_mm;
1903 unsigned long start, end;
1904
1905 entry->vaddr = start = (address & SUN4C_REAL_PGDIR_MASK);
1906 entry->ctx = mm->context;
1907 add_ring_ordered(sun4c_context_ring + mm->context, entry);
1908 sun4c_put_segmap(entry->vaddr, entry->pseg);
1909 end = start + SUN4C_REAL_PGDIR_SIZE;
1910 while (start < end) {
1911 #ifdef SUN4C_PRELOAD_PSEG
1912 pgd_t *pgdp = sun4c_pgd_offset(mm, start);
1913 pte_t *ptep;
1914
1915 if (!pgdp)
1916 goto no_mapping;
1917 ptep = sun4c_pte_offset_kernel((pmd_t *) pgdp, start);
1918 if (!ptep || !(pte_val(*ptep) & _SUN4C_PAGE_PRESENT))
1919 goto no_mapping;
1920 sun4c_put_pte(start, pte_val(*ptep));
1921 goto next;
1922
1923 no_mapping:
1924 #endif
1925 sun4c_put_pte(start, 0);
1926 #ifdef SUN4C_PRELOAD_PSEG
1927 next:
1928 #endif
1929 start += PAGE_SIZE;
1930 }
1931 #ifndef SUN4C_PRELOAD_PSEG
1932 sun4c_put_pte(address, pte_val(*ptep));
1933 #endif
1934 local_irq_restore(flags);
1935 return;
1936 } else {
1937 struct sun4c_mmu_entry *entry = &mmu_entry_pool[pseg];
1938
1939 remove_lru(entry);
1940 add_lru(entry);
1941 }
1942
1943 sun4c_put_pte(address, pte_val(*ptep));
1944 local_irq_restore(flags);
1945 }
1946
1947 extern void sparc_context_init(int);
1948 extern unsigned long bootmem_init(unsigned long *pages_avail);
1949 extern unsigned long last_valid_pfn;
1950
1951 void __init sun4c_paging_init(void)
1952 {
1953 int i, cnt;
1954 unsigned long kernel_end, vaddr;
1955 extern struct resource sparc_iomap;
1956 unsigned long end_pfn, pages_avail;
1957
1958 kernel_end = (unsigned long) &_end;
1959 kernel_end = SUN4C_REAL_PGDIR_ALIGN(kernel_end);
1960
1961 pages_avail = 0;
1962 last_valid_pfn = bootmem_init(&pages_avail);
1963 end_pfn = last_valid_pfn;
1964
1965 sun4c_probe_mmu();
1966 invalid_segment = (num_segmaps - 1);
1967 sun4c_init_mmu_entry_pool();
1968 sun4c_init_rings();
1969 sun4c_init_map_kernelprom(kernel_end);
1970 sun4c_init_clean_mmu(kernel_end);
1971 sun4c_init_fill_kernel_ring(SUN4C_KERNEL_BUCKETS);
1972 sun4c_init_lock_area(sparc_iomap.start, IOBASE_END);
1973 sun4c_init_lock_area(DVMA_VADDR, DVMA_END);
1974 sun4c_init_lock_areas();
1975 sun4c_init_fill_user_ring();
1976
1977 sun4c_set_context(0);
1978 memset(swapper_pg_dir, 0, PAGE_SIZE);
1979 memset(pg0, 0, PAGE_SIZE);
1980 memset(pg1, 0, PAGE_SIZE);
1981 memset(pg2, 0, PAGE_SIZE);
1982 memset(pg3, 0, PAGE_SIZE);
1983
1984 /* Save work later. */
1985 vaddr = VMALLOC_START;
1986 swapper_pg_dir[vaddr>>SUN4C_PGDIR_SHIFT] = __pgd(PGD_TABLE | (unsigned long) pg0);
1987 vaddr += SUN4C_PGDIR_SIZE;
1988 swapper_pg_dir[vaddr>>SUN4C_PGDIR_SHIFT] = __pgd(PGD_TABLE | (unsigned long) pg1);
1989 vaddr += SUN4C_PGDIR_SIZE;
1990 swapper_pg_dir[vaddr>>SUN4C_PGDIR_SHIFT] = __pgd(PGD_TABLE | (unsigned long) pg2);
1991 vaddr += SUN4C_PGDIR_SIZE;
1992 swapper_pg_dir[vaddr>>SUN4C_PGDIR_SHIFT] = __pgd(PGD_TABLE | (unsigned long) pg3);
1993 sun4c_init_ss2_cache_bug();
1994 sparc_context_init(num_contexts);
1995
1996 {
1997 unsigned long zones_size[MAX_NR_ZONES];
1998 unsigned long zholes_size[MAX_NR_ZONES];
1999 unsigned long npages;
2000 int znum;
2001
2002 for (znum = 0; znum < MAX_NR_ZONES; znum++)
2003 zones_size[znum] = zholes_size[znum] = 0;
2004
2005 npages = max_low_pfn - pfn_base;
2006
2007 zones_size[ZONE_DMA] = npages;
2008 zholes_size[ZONE_DMA] = npages - pages_avail;
2009
2010 npages = highend_pfn - max_low_pfn;
2011 zones_size[ZONE_HIGHMEM] = npages;
2012 zholes_size[ZONE_HIGHMEM] = npages - calc_highpages();
2013
2014 free_area_init_node(0, zones_size, pfn_base, zholes_size);
2015 }
2016
2017 cnt = 0;
2018 for (i = 0; i < num_segmaps; i++)
2019 if (mmu_entry_pool[i].locked)
2020 cnt++;
2021
2022 max_user_taken_entries = num_segmaps - cnt - 40 - 1;
2023
2024 printk("SUN4C: %d mmu entries for the kernel\n", cnt);
2025 }
2026
2027 static pgprot_t sun4c_pgprot_noncached(pgprot_t prot)
2028 {
2029 prot |= __pgprot(_SUN4C_PAGE_IO | _SUN4C_PAGE_NOCACHE);
2030
2031 return prot;
2032 }
2033
2034 /* Load up routines and constants for sun4c mmu */
2035 void __init ld_mmu_sun4c(void)
2036 {
2037 extern void ___xchg32_sun4c(void);
2038
2039 printk("Loading sun4c MMU routines\n");
2040
2041 /* First the constants */
2042 BTFIXUPSET_SIMM13(pgdir_shift, SUN4C_PGDIR_SHIFT);
2043 BTFIXUPSET_SETHI(pgdir_size, SUN4C_PGDIR_SIZE);
2044 BTFIXUPSET_SETHI(pgdir_mask, SUN4C_PGDIR_MASK);
2045
2046 BTFIXUPSET_SIMM13(ptrs_per_pmd, SUN4C_PTRS_PER_PMD);
2047 BTFIXUPSET_SIMM13(ptrs_per_pgd, SUN4C_PTRS_PER_PGD);
2048 BTFIXUPSET_SIMM13(user_ptrs_per_pgd, KERNBASE / SUN4C_PGDIR_SIZE);
2049
2050 BTFIXUPSET_INT(page_none, pgprot_val(SUN4C_PAGE_NONE));
2051 PAGE_SHARED = pgprot_val(SUN4C_PAGE_SHARED);
2052 BTFIXUPSET_INT(page_copy, pgprot_val(SUN4C_PAGE_COPY));
2053 BTFIXUPSET_INT(page_readonly, pgprot_val(SUN4C_PAGE_READONLY));
2054 BTFIXUPSET_INT(page_kernel, pgprot_val(SUN4C_PAGE_KERNEL));
2055 page_kernel = pgprot_val(SUN4C_PAGE_KERNEL);
2056
2057 /* Functions */
2058 BTFIXUPSET_CALL(pgprot_noncached, sun4c_pgprot_noncached, BTFIXUPCALL_NORM);
2059 BTFIXUPSET_CALL(___xchg32, ___xchg32_sun4c, BTFIXUPCALL_NORM);
2060 BTFIXUPSET_CALL(do_check_pgt_cache, sun4c_check_pgt_cache, BTFIXUPCALL_NORM);
2061
2062 BTFIXUPSET_CALL(flush_cache_all, sun4c_flush_cache_all, BTFIXUPCALL_NORM);
2063
2064 if (sun4c_vacinfo.do_hwflushes) {
2065 BTFIXUPSET_CALL(sun4c_flush_page, sun4c_flush_page_hw, BTFIXUPCALL_NORM);
2066 BTFIXUPSET_CALL(sun4c_flush_segment, sun4c_flush_segment_hw, BTFIXUPCALL_NORM);
2067 BTFIXUPSET_CALL(sun4c_flush_context, sun4c_flush_context_hw, BTFIXUPCALL_NORM);
2068 } else {
2069 BTFIXUPSET_CALL(sun4c_flush_page, sun4c_flush_page_sw, BTFIXUPCALL_NORM);
2070 BTFIXUPSET_CALL(sun4c_flush_segment, sun4c_flush_segment_sw, BTFIXUPCALL_NORM);
2071 BTFIXUPSET_CALL(sun4c_flush_context, sun4c_flush_context_sw, BTFIXUPCALL_NORM);
2072 }
2073
2074 BTFIXUPSET_CALL(flush_tlb_mm, sun4c_flush_tlb_mm, BTFIXUPCALL_NORM);
2075 BTFIXUPSET_CALL(flush_cache_mm, sun4c_flush_cache_mm, BTFIXUPCALL_NORM);
2076 BTFIXUPSET_CALL(destroy_context, sun4c_destroy_context, BTFIXUPCALL_NORM);
2077 BTFIXUPSET_CALL(switch_mm, sun4c_switch_mm, BTFIXUPCALL_NORM);
2078 BTFIXUPSET_CALL(flush_cache_page, sun4c_flush_cache_page, BTFIXUPCALL_NORM);
2079 BTFIXUPSET_CALL(flush_tlb_page, sun4c_flush_tlb_page, BTFIXUPCALL_NORM);
2080 BTFIXUPSET_CALL(flush_tlb_range, sun4c_flush_tlb_range, BTFIXUPCALL_NORM);
2081 BTFIXUPSET_CALL(flush_cache_range, sun4c_flush_cache_range, BTFIXUPCALL_NORM);
2082 BTFIXUPSET_CALL(__flush_page_to_ram, sun4c_flush_page_to_ram, BTFIXUPCALL_NORM);
2083 BTFIXUPSET_CALL(flush_tlb_all, sun4c_flush_tlb_all, BTFIXUPCALL_NORM);
2084
2085 BTFIXUPSET_CALL(flush_sig_insns, sun4c_flush_sig_insns, BTFIXUPCALL_NOP);
2086
2087 BTFIXUPSET_CALL(set_pte, sun4c_set_pte, BTFIXUPCALL_STO1O0);
2088
2089 /* The 2.4.18 code does not set this on sun4c, how does it work? XXX */
2090 /* BTFIXUPSET_SETHI(none_mask, 0x00000000); */ /* Defaults to zero? */
2091
2092 BTFIXUPSET_CALL(pte_pfn, sun4c_pte_pfn, BTFIXUPCALL_NORM);
2093 #if 0 /* PAGE_SHIFT <= 12 */ /* Eek. Investigate. XXX */
2094 BTFIXUPSET_CALL(pmd_page, sun4c_pmd_page, BTFIXUPCALL_ANDNINT(PAGE_SIZE - 1));
2095 #else
2096 BTFIXUPSET_CALL(pmd_page, sun4c_pmd_page, BTFIXUPCALL_NORM);
2097 #endif
2098 BTFIXUPSET_CALL(pmd_set, sun4c_pmd_set, BTFIXUPCALL_NORM);
2099 BTFIXUPSET_CALL(pmd_populate, sun4c_pmd_populate, BTFIXUPCALL_NORM);
2100
2101 BTFIXUPSET_CALL(pte_present, sun4c_pte_present, BTFIXUPCALL_NORM);
2102 BTFIXUPSET_CALL(pte_clear, sun4c_pte_clear, BTFIXUPCALL_STG0O0);
2103
2104 BTFIXUPSET_CALL(pmd_bad, sun4c_pmd_bad, BTFIXUPCALL_NORM);
2105 BTFIXUPSET_CALL(pmd_present, sun4c_pmd_present, BTFIXUPCALL_NORM);
2106 BTFIXUPSET_CALL(pmd_clear, sun4c_pmd_clear, BTFIXUPCALL_STG0O0);
2107
2108 BTFIXUPSET_CALL(pgd_none, sun4c_pgd_none, BTFIXUPCALL_RETINT(0));
2109 BTFIXUPSET_CALL(pgd_bad, sun4c_pgd_bad, BTFIXUPCALL_RETINT(0));
2110 BTFIXUPSET_CALL(pgd_present, sun4c_pgd_present, BTFIXUPCALL_RETINT(1));
2111 BTFIXUPSET_CALL(pgd_clear, sun4c_pgd_clear, BTFIXUPCALL_NOP);
2112
2113 BTFIXUPSET_CALL(mk_pte, sun4c_mk_pte, BTFIXUPCALL_NORM);
2114 BTFIXUPSET_CALL(mk_pte_phys, sun4c_mk_pte_phys, BTFIXUPCALL_NORM);
2115 BTFIXUPSET_CALL(mk_pte_io, sun4c_mk_pte_io, BTFIXUPCALL_NORM);
2116
2117 BTFIXUPSET_INT(pte_modify_mask, _SUN4C_PAGE_CHG_MASK);
2118 BTFIXUPSET_CALL(pmd_offset, sun4c_pmd_offset, BTFIXUPCALL_NORM);
2119 BTFIXUPSET_CALL(pte_offset_kernel, sun4c_pte_offset_kernel, BTFIXUPCALL_NORM);
2120 BTFIXUPSET_CALL(free_pte_fast, sun4c_free_pte_fast, BTFIXUPCALL_NORM);
2121 BTFIXUPSET_CALL(pte_free, sun4c_pte_free, BTFIXUPCALL_NORM);
2122 BTFIXUPSET_CALL(pte_alloc_one_kernel, sun4c_pte_alloc_one_kernel, BTFIXUPCALL_NORM);
2123 BTFIXUPSET_CALL(pte_alloc_one, sun4c_pte_alloc_one, BTFIXUPCALL_NORM);
2124 BTFIXUPSET_CALL(free_pmd_fast, sun4c_free_pmd_fast, BTFIXUPCALL_NOP);
2125 BTFIXUPSET_CALL(pmd_alloc_one, sun4c_pmd_alloc_one, BTFIXUPCALL_RETO0);
2126 BTFIXUPSET_CALL(free_pgd_fast, sun4c_free_pgd_fast, BTFIXUPCALL_NORM);
2127 BTFIXUPSET_CALL(get_pgd_fast, sun4c_get_pgd_fast, BTFIXUPCALL_NORM);
2128
2129 BTFIXUPSET_HALF(pte_writei, _SUN4C_PAGE_WRITE);
2130 BTFIXUPSET_HALF(pte_dirtyi, _SUN4C_PAGE_MODIFIED);
2131 BTFIXUPSET_HALF(pte_youngi, _SUN4C_PAGE_ACCESSED);
2132 BTFIXUPSET_HALF(pte_filei, _SUN4C_PAGE_FILE);
2133 BTFIXUPSET_HALF(pte_wrprotecti, _SUN4C_PAGE_WRITE|_SUN4C_PAGE_SILENT_WRITE);
2134 BTFIXUPSET_HALF(pte_mkcleani, _SUN4C_PAGE_MODIFIED|_SUN4C_PAGE_SILENT_WRITE);
2135 BTFIXUPSET_HALF(pte_mkoldi, _SUN4C_PAGE_ACCESSED|_SUN4C_PAGE_SILENT_READ);
2136 BTFIXUPSET_CALL(pte_mkwrite, sun4c_pte_mkwrite, BTFIXUPCALL_NORM);
2137 BTFIXUPSET_CALL(pte_mkdirty, sun4c_pte_mkdirty, BTFIXUPCALL_NORM);
2138 BTFIXUPSET_CALL(pte_mkyoung, sun4c_pte_mkyoung, BTFIXUPCALL_NORM);
2139 BTFIXUPSET_CALL(update_mmu_cache, sun4c_update_mmu_cache, BTFIXUPCALL_NORM);
2140
2141 BTFIXUPSET_CALL(pte_to_pgoff, sun4c_pte_to_pgoff, BTFIXUPCALL_NORM);
2142 BTFIXUPSET_CALL(pgoff_to_pte, sun4c_pgoff_to_pte, BTFIXUPCALL_NORM);
2143
2144 BTFIXUPSET_CALL(mmu_lockarea, sun4c_lockarea, BTFIXUPCALL_NORM);
2145 BTFIXUPSET_CALL(mmu_unlockarea, sun4c_unlockarea, BTFIXUPCALL_NORM);
2146
2147 BTFIXUPSET_CALL(mmu_get_scsi_one, sun4c_get_scsi_one, BTFIXUPCALL_NORM);
2148 BTFIXUPSET_CALL(mmu_get_scsi_sgl, sun4c_get_scsi_sgl, BTFIXUPCALL_NORM);
2149 BTFIXUPSET_CALL(mmu_release_scsi_one, sun4c_release_scsi_one, BTFIXUPCALL_NORM);
2150 BTFIXUPSET_CALL(mmu_release_scsi_sgl, sun4c_release_scsi_sgl, BTFIXUPCALL_NORM);
2151
2152 BTFIXUPSET_CALL(mmu_map_dma_area, sun4c_map_dma_area, BTFIXUPCALL_NORM);
2153 BTFIXUPSET_CALL(mmu_unmap_dma_area, sun4c_unmap_dma_area, BTFIXUPCALL_NORM);
2154
2155 BTFIXUPSET_CALL(sparc_mapiorange, sun4c_mapiorange, BTFIXUPCALL_NORM);
2156 BTFIXUPSET_CALL(sparc_unmapiorange, sun4c_unmapiorange, BTFIXUPCALL_NORM);
2157
2158 BTFIXUPSET_CALL(__swp_type, sun4c_swp_type, BTFIXUPCALL_NORM);
2159 BTFIXUPSET_CALL(__swp_offset, sun4c_swp_offset, BTFIXUPCALL_NORM);
2160 BTFIXUPSET_CALL(__swp_entry, sun4c_swp_entry, BTFIXUPCALL_NORM);
2161
2162 BTFIXUPSET_CALL(alloc_thread_info, sun4c_alloc_thread_info, BTFIXUPCALL_NORM);
2163 BTFIXUPSET_CALL(free_thread_info, sun4c_free_thread_info, BTFIXUPCALL_NORM);
2164
2165 BTFIXUPSET_CALL(mmu_info, sun4c_mmu_info, BTFIXUPCALL_NORM);
2166
2167 /* These should _never_ get called with two level tables. */
2168 BTFIXUPSET_CALL(pgd_set, sun4c_pgd_set, BTFIXUPCALL_NOP);
2169 BTFIXUPSET_CALL(pgd_page_vaddr, sun4c_pgd_page, BTFIXUPCALL_RETO0);
2170 }