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