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