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