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