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