vserver 1.9.5.x5
[linux-2.6.git] / arch / sparc64 / kernel / setup.c
1 /*  $Id: setup.c,v 1.72 2002/02/09 19:49:30 davem Exp $
2  *  linux/arch/sparc64/kernel/setup.c
3  *
4  *  Copyright (C) 1995,1996  David S. Miller (davem@caip.rutgers.edu)
5  *  Copyright (C) 1997       Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6  */
7
8 #include <linux/errno.h>
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.h>
12 #include <linux/stddef.h>
13 #include <linux/unistd.h>
14 #include <linux/ptrace.h>
15 #include <linux/slab.h>
16 #include <asm/smp.h>
17 #include <linux/user.h>
18 #include <linux/a.out.h>
19 #include <linux/tty.h>
20 #include <linux/delay.h>
21 #include <linux/config.h>
22 #include <linux/fs.h>
23 #include <linux/seq_file.h>
24 #include <linux/syscalls.h>
25 #include <linux/kdev_t.h>
26 #include <linux/major.h>
27 #include <linux/string.h>
28 #include <linux/init.h>
29 #include <linux/inet.h>
30 #include <linux/console.h>
31 #include <linux/root_dev.h>
32 #include <linux/interrupt.h>
33 #include <linux/cpu.h>
34 #include <linux/initrd.h>
35
36 #include <asm/segment.h>
37 #include <asm/system.h>
38 #include <asm/io.h>
39 #include <asm/processor.h>
40 #include <asm/oplib.h>
41 #include <asm/page.h>
42 #include <asm/pgtable.h>
43 #include <asm/idprom.h>
44 #include <asm/head.h>
45 #include <asm/starfire.h>
46 #include <asm/mmu_context.h>
47 #include <asm/timer.h>
48 #include <asm/sections.h>
49 #include <asm/setup.h>
50
51 #ifdef CONFIG_IP_PNP
52 #include <net/ipconfig.h>
53 #endif
54
55 struct screen_info screen_info = {
56         0, 0,                   /* orig-x, orig-y */
57         0,                      /* unused */
58         0,                      /* orig-video-page */
59         0,                      /* orig-video-mode */
60         128,                    /* orig-video-cols */
61         0, 0, 0,                /* unused, ega_bx, unused */
62         54,                     /* orig-video-lines */
63         0,                      /* orig-video-isVGA */
64         16                      /* orig-video-points */
65 };
66
67 /* Typing sync at the prom prompt calls the function pointed to by
68  * the sync callback which I set to the following function.
69  * This should sync all filesystems and return, for now it just
70  * prints out pretty messages and returns.
71  */
72
73 void (*prom_palette)(int);
74 void (*prom_keyboard)(void);
75
76 static void
77 prom_console_write(struct console *con, const char *s, unsigned n)
78 {
79         prom_write(s, n);
80 }
81
82 static struct console prom_console = {
83         .name =         "prom",
84         .write =        prom_console_write,
85         .flags =        CON_CONSDEV | CON_ENABLED,
86         .index =        -1,
87 };
88
89 #define PROM_TRUE       -1
90 #define PROM_FALSE      0
91
92 /* Pretty sick eh? */
93 int prom_callback(long *args)
94 {
95         struct console *cons, *saved_console = NULL;
96         unsigned long flags;
97         char *cmd;
98         extern spinlock_t prom_entry_lock;
99
100         if (!args)
101                 return -1;
102         if (!(cmd = (char *)args[0]))
103                 return -1;
104
105         /*
106          * The callback can be invoked on the cpu that first dropped 
107          * into prom_cmdline after taking the serial interrupt, or on 
108          * a slave processor that was smp_captured() if the 
109          * administrator has done a switch-cpu inside obp. In either 
110          * case, the cpu is marked as in-interrupt. Drop IRQ locks.
111          */
112         irq_exit();
113
114         /* XXX Revisit the locking here someday.  This is a debugging
115          * XXX feature so it isnt all that critical.  -DaveM
116          */
117         local_irq_save(flags);
118
119         spin_unlock(&prom_entry_lock);
120         cons = console_drivers;
121         while (cons) {
122                 unregister_console(cons);
123                 cons->flags &= ~(CON_PRINTBUFFER);
124                 cons->next = saved_console;
125                 saved_console = cons;
126                 cons = console_drivers;
127         }
128         register_console(&prom_console);
129         if (!strcmp(cmd, "sync")) {
130                 prom_printf("PROM `%s' command...\n", cmd);
131                 show_free_areas();
132                 if (current->pid != 0) {
133                         local_irq_enable();
134                         sys_sync();
135                         local_irq_disable();
136                 }
137                 args[2] = 0;
138                 args[args[1] + 3] = -1;
139                 prom_printf("Returning to PROM\n");
140         } else if (!strcmp(cmd, "va>tte-data")) {
141                 unsigned long ctx, va;
142                 unsigned long tte = 0;
143                 long res = PROM_FALSE;
144
145                 ctx = args[3];
146                 va = args[4];
147                 if (ctx) {
148                         /*
149                          * Find process owning ctx, lookup mapping.
150                          */
151                         struct task_struct *p;
152                         struct mm_struct *mm = NULL;
153                         pgd_t *pgdp;
154                         pud_t *pudp;
155                         pmd_t *pmdp;
156                         pte_t *ptep;
157
158                         for_each_process(p) {
159                                 mm = p->mm;
160                                 if (CTX_HWBITS(mm->context) == ctx)
161                                         break;
162                         }
163                         if (!mm ||
164                             CTX_HWBITS(mm->context) != ctx)
165                                 goto done;
166
167                         pgdp = pgd_offset(mm, va);
168                         if (pgd_none(*pgdp))
169                                 goto done;
170                         pudp = pud_offset(pgdp, va);
171                         if (pud_none(*pudp))
172                                 goto done;
173                         pmdp = pmd_offset(pudp, va);
174                         if (pmd_none(*pmdp))
175                                 goto done;
176
177                         /* Preemption implicitly disabled by virtue of
178                          * being called from inside OBP.
179                          */
180                         ptep = pte_offset_map(pmdp, va);
181                         if (pte_present(*ptep)) {
182                                 tte = pte_val(*ptep);
183                                 res = PROM_TRUE;
184                         }
185                         pte_unmap(ptep);
186                         goto done;
187                 }
188
189                 if ((va >= KERNBASE) && (va < (KERNBASE + (4 * 1024 * 1024)))) {
190                         /* Spitfire Errata #32 workaround */
191                         __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
192                                              "flush     %%g6"
193                                              : /* No outputs */
194                                              : "r" (0),
195                                              "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
196
197                         /*
198                          * Locked down tlb entry.
199                          */
200
201                         if (tlb_type == spitfire)
202                                 tte = spitfire_get_dtlb_data(SPITFIRE_HIGHEST_LOCKED_TLBENT);
203                         else if (tlb_type == cheetah || tlb_type == cheetah_plus)
204                                 tte = cheetah_get_ldtlb_data(CHEETAH_HIGHEST_LOCKED_TLBENT);
205
206                         res = PROM_TRUE;
207                         goto done;
208                 }
209
210                 if (va < PGDIR_SIZE) {
211                         /*
212                          * vmalloc or prom_inherited mapping.
213                          */
214                         pgd_t *pgdp;
215                         pud_t *pudp;
216                         pmd_t *pmdp;
217                         pte_t *ptep;
218                         int error;
219
220                         if ((va >= LOW_OBP_ADDRESS) && (va < HI_OBP_ADDRESS)) {
221                                 tte = prom_virt_to_phys(va, &error);
222                                 if (!error)
223                                         res = PROM_TRUE;
224                                 goto done;
225                         }
226                         pgdp = pgd_offset_k(va);
227                         if (pgd_none(*pgdp))
228                                 goto done;
229                         pudp = pud_offset(pgdp, va);
230                         if (pud_none(*pudp))
231                                 goto done;
232                         pmdp = pmd_offset(pudp, va);
233                         if (pmd_none(*pmdp))
234                                 goto done;
235
236                         /* Preemption implicitly disabled by virtue of
237                          * being called from inside OBP.
238                          */
239                         ptep = pte_offset_kernel(pmdp, va);
240                         if (pte_present(*ptep)) {
241                                 tte = pte_val(*ptep);
242                                 res = PROM_TRUE;
243                         }
244                         goto done;
245                 }
246
247                 if (va < PAGE_OFFSET) {
248                         /*
249                          * No mappings here.
250                          */
251                         goto done;
252                 }
253
254                 if (va & (1UL << 40)) {
255                         /*
256                          * I/O page.
257                          */
258
259                         tte = (__pa(va) & _PAGE_PADDR) |
260                               _PAGE_VALID | _PAGE_SZ4MB |
261                               _PAGE_E | _PAGE_P | _PAGE_W;
262                         res = PROM_TRUE;
263                         goto done;
264                 }
265
266                 /*
267                  * Normal page.
268                  */
269                 tte = (__pa(va) & _PAGE_PADDR) |
270                       _PAGE_VALID | _PAGE_SZ4MB |
271                       _PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_W;
272                 res = PROM_TRUE;
273
274         done:
275                 if (res == PROM_TRUE) {
276                         args[2] = 3;
277                         args[args[1] + 3] = 0;
278                         args[args[1] + 4] = res;
279                         args[args[1] + 5] = tte;
280                 } else {
281                         args[2] = 2;
282                         args[args[1] + 3] = 0;
283                         args[args[1] + 4] = res;
284                 }
285         } else if (!strcmp(cmd, ".soft1")) {
286                 unsigned long tte;
287
288                 tte = args[3];
289                 prom_printf("%lx:\"%s%s%s%s%s\" ",
290                             (tte & _PAGE_SOFT) >> 7,
291                             tte & _PAGE_MODIFIED ? "M" : "-",
292                             tte & _PAGE_ACCESSED ? "A" : "-",
293                             tte & _PAGE_READ     ? "W" : "-",
294                             tte & _PAGE_WRITE    ? "R" : "-",
295                             tte & _PAGE_PRESENT  ? "P" : "-");
296
297                 args[2] = 2;
298                 args[args[1] + 3] = 0;
299                 args[args[1] + 4] = PROM_TRUE;
300         } else if (!strcmp(cmd, ".soft2")) {
301                 unsigned long tte;
302
303                 tte = args[3];
304                 prom_printf("%lx ", (tte & 0x07FC000000000000UL) >> 50);
305
306                 args[2] = 2;
307                 args[args[1] + 3] = 0;
308                 args[args[1] + 4] = PROM_TRUE;
309         } else {
310                 prom_printf("unknown PROM `%s' command...\n", cmd);
311         }
312         unregister_console(&prom_console);
313         while (saved_console) {
314                 cons = saved_console;
315                 saved_console = cons->next;
316                 register_console(cons);
317         }
318         spin_lock(&prom_entry_lock);
319         local_irq_restore(flags);
320
321         /*
322          * Restore in-interrupt status for a resume from obp.
323          */
324         irq_enter();
325         return 0;
326 }
327
328 unsigned int boot_flags = 0;
329 #define BOOTME_DEBUG  0x1
330 #define BOOTME_SINGLE 0x2
331
332 /* Exported for mm/init.c:paging_init. */
333 unsigned long cmdline_memory_size = 0;
334
335 static struct console prom_debug_console = {
336         .name =         "debug",
337         .write =        prom_console_write,
338         .flags =        CON_PRINTBUFFER,
339         .index =        -1,
340 };
341
342 /* XXX Implement this at some point... */
343 void kernel_enter_debugger(void)
344 {
345 }
346
347 int obp_system_intr(void)
348 {
349         if (boot_flags & BOOTME_DEBUG) {
350                 printk("OBP: system interrupted\n");
351                 prom_halt();
352                 return 1;
353         }
354         return 0;
355 }
356
357 /* 
358  * Process kernel command line switches that are specific to the
359  * SPARC or that require special low-level processing.
360  */
361 static void __init process_switch(char c)
362 {
363         switch (c) {
364         case 'd':
365                 boot_flags |= BOOTME_DEBUG;
366                 break;
367         case 's':
368                 boot_flags |= BOOTME_SINGLE;
369                 break;
370         case 'h':
371                 prom_printf("boot_flags_init: Halt!\n");
372                 prom_halt();
373                 break;
374         case 'p':
375                 /* Use PROM debug console. */
376                 register_console(&prom_debug_console);
377                 break;
378         default:
379                 printk("Unknown boot switch (-%c)\n", c);
380                 break;
381         }
382 }
383
384 static void __init process_console(char *commands)
385 {
386         serial_console = 0;
387         commands += 8;
388         /* Linux-style serial */
389         if (!strncmp(commands, "ttyS", 4))
390                 serial_console = simple_strtoul(commands + 4, NULL, 10) + 1;
391         else if (!strncmp(commands, "tty", 3)) {
392                 char c = *(commands + 3);
393                 /* Solaris-style serial */
394                 if (c == 'a' || c == 'b') {
395                         serial_console = c - 'a' + 1;
396                         prom_printf ("Using /dev/tty%c as console.\n", c);
397                 }
398                 /* else Linux-style fbcon, not serial */
399         }
400 #if defined(CONFIG_PROM_CONSOLE)
401         if (!strncmp(commands, "prom", 4)) {
402                 char *p;
403
404                 for (p = commands - 8; *p && *p != ' '; p++)
405                         *p = ' ';
406                 conswitchp = &prom_con;
407         }
408 #endif
409 }
410
411 static void __init boot_flags_init(char *commands)
412 {
413         while (*commands) {
414                 /* Move to the start of the next "argument". */
415                 while (*commands && *commands == ' ')
416                         commands++;
417
418                 /* Process any command switches, otherwise skip it. */
419                 if (*commands == '\0')
420                         break;
421                 if (*commands == '-') {
422                         commands++;
423                         while (*commands && *commands != ' ')
424                                 process_switch(*commands++);
425                         continue;
426                 }
427                 if (!strncmp(commands, "console=", 8)) {
428                         process_console(commands);
429                 } else if (!strncmp(commands, "mem=", 4)) {
430                         /*
431                          * "mem=XXX[kKmM]" overrides the PROM-reported
432                          * memory size.
433                          */
434                         cmdline_memory_size = simple_strtoul(commands + 4,
435                                                              &commands, 0);
436                         if (*commands == 'K' || *commands == 'k') {
437                                 cmdline_memory_size <<= 10;
438                                 commands++;
439                         } else if (*commands=='M' || *commands=='m') {
440                                 cmdline_memory_size <<= 20;
441                                 commands++;
442                         }
443                 }
444                 while (*commands && *commands != ' ')
445                         commands++;
446         }
447 }
448
449 extern int prom_probe_memory(void);
450 extern unsigned long start, end;
451 extern void panic_setup(char *, int *);
452
453 extern unsigned short root_flags;
454 extern unsigned short root_dev;
455 extern unsigned short ram_flags;
456 #define RAMDISK_IMAGE_START_MASK        0x07FF
457 #define RAMDISK_PROMPT_FLAG             0x8000
458 #define RAMDISK_LOAD_FLAG               0x4000
459
460 extern int root_mountflags;
461
462 char reboot_command[COMMAND_LINE_SIZE];
463
464 static struct pt_regs fake_swapper_regs = { { 0, }, 0, 0, 0, 0 };
465
466 void register_prom_callbacks(void)
467 {
468         prom_setcallback(prom_callback);
469         prom_feval(": linux-va>tte-data 2 \" va>tte-data\" $callback drop ; "
470                    "' linux-va>tte-data to va>tte-data");
471         prom_feval(": linux-.soft1 1 \" .soft1\" $callback 2drop ; "
472                    "' linux-.soft1 to .soft1");
473         prom_feval(": linux-.soft2 1 \" .soft2\" $callback 2drop ; "
474                    "' linux-.soft2 to .soft2");
475 }
476
477 extern void paging_init(void);
478
479 void __init setup_arch(char **cmdline_p)
480 {
481         unsigned long highest_paddr;
482         int i;
483
484         /* Initialize PROM console and command line. */
485         *cmdline_p = prom_getbootargs();
486         strcpy(saved_command_line, *cmdline_p);
487
488         printk("ARCH: SUN4U\n");
489
490 #ifdef CONFIG_DUMMY_CONSOLE
491         conswitchp = &dummy_con;
492 #elif defined(CONFIG_PROM_CONSOLE)
493         conswitchp = &prom_con;
494 #endif
495
496 #ifdef CONFIG_SMP
497         i = (unsigned long)&irq_stat[1] - (unsigned long)&irq_stat[0];
498         if ((i == SMP_CACHE_BYTES) || (i == (2 * SMP_CACHE_BYTES))) {
499                 extern unsigned int irqsz_patchme[1];
500                 irqsz_patchme[0] |= ((i == SMP_CACHE_BYTES) ? SMP_CACHE_BYTES_SHIFT : \
501                                                         SMP_CACHE_BYTES_SHIFT + 1);
502                 flushi((long)&irqsz_patchme[0]);
503         } else {
504                 prom_printf("Unexpected size of irq_stat[] elements\n");
505                 prom_halt();
506         }
507 #endif
508         /* Work out if we are starfire early on */
509         check_if_starfire();
510
511         boot_flags_init(*cmdline_p);
512
513         idprom_init();
514         (void) prom_probe_memory();
515
516         /* In paging_init() we tip off this value to see if we need
517          * to change init_mm.pgd to point to the real alias mapping.
518          */
519         phys_base = 0xffffffffffffffffUL;
520         highest_paddr = 0UL;
521         for (i = 0; sp_banks[i].num_bytes != 0; i++) {
522                 unsigned long top;
523
524                 if (sp_banks[i].base_addr < phys_base)
525                         phys_base = sp_banks[i].base_addr;
526                 top = sp_banks[i].base_addr +
527                         sp_banks[i].num_bytes;
528                 if (highest_paddr < top)
529                         highest_paddr = top;
530         }
531         pfn_base = phys_base >> PAGE_SHIFT;
532
533         switch (tlb_type) {
534         default:
535         case spitfire:
536                 kern_base = spitfire_get_itlb_data(sparc64_highest_locked_tlbent());
537                 kern_base &= _PAGE_PADDR_SF;
538                 break;
539
540         case cheetah:
541         case cheetah_plus:
542                 kern_base = cheetah_get_litlb_data(sparc64_highest_locked_tlbent());
543                 kern_base &= _PAGE_PADDR;
544                 break;
545         };
546
547         kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
548
549         if (!root_flags)
550                 root_mountflags &= ~MS_RDONLY;
551         ROOT_DEV = old_decode_dev(root_dev);
552 #ifdef CONFIG_BLK_DEV_INITRD
553         rd_image_start = ram_flags & RAMDISK_IMAGE_START_MASK;
554         rd_prompt = ((ram_flags & RAMDISK_PROMPT_FLAG) != 0);
555         rd_doload = ((ram_flags & RAMDISK_LOAD_FLAG) != 0);     
556 #endif
557
558         init_task.thread_info->kregs = &fake_swapper_regs;
559
560 #ifdef CONFIG_IP_PNP
561         if (!ic_set_manually) {
562                 int chosen = prom_finddevice ("/chosen");
563                 u32 cl, sv, gw;
564                 
565                 cl = prom_getintdefault (chosen, "client-ip", 0);
566                 sv = prom_getintdefault (chosen, "server-ip", 0);
567                 gw = prom_getintdefault (chosen, "gateway-ip", 0);
568                 if (cl && sv) {
569                         ic_myaddr = cl;
570                         ic_servaddr = sv;
571                         if (gw)
572                                 ic_gateway = gw;
573 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_RARP)
574                         ic_proto_enabled = 0;
575 #endif
576                 }
577         }
578 #endif
579
580         paging_init();
581 }
582
583 static int __init set_preferred_console(void)
584 {
585         int idev, odev;
586
587         /* The user has requested a console so this is already set up. */
588         if (serial_console >= 0)
589                 return -EBUSY;
590
591         idev = prom_query_input_device();
592         odev = prom_query_output_device();
593         if (idev == PROMDEV_IKBD && odev == PROMDEV_OSCREEN) {
594                 serial_console = 0;
595         } else if (idev == PROMDEV_ITTYA && odev == PROMDEV_OTTYA) {
596                 serial_console = 1;
597         } else if (idev == PROMDEV_ITTYB && odev == PROMDEV_OTTYB) {
598                 serial_console = 2;
599         } else {
600                 prom_printf("Inconsistent console: "
601                             "input %d, output %d\n",
602                             idev, odev);
603                 prom_halt();
604         }
605
606         if (serial_console)
607                 return add_preferred_console("ttyS", serial_console - 1, NULL);
608
609         return -ENODEV;
610 }
611 console_initcall(set_preferred_console);
612
613 /* BUFFER is PAGE_SIZE bytes long. */
614
615 extern char *sparc_cpu_type;
616 extern char *sparc_fpu_type;
617
618 extern void smp_info(struct seq_file *);
619 extern void smp_bogo(struct seq_file *);
620 extern void mmu_info(struct seq_file *);
621
622 static int show_cpuinfo(struct seq_file *m, void *__unused)
623 {
624         seq_printf(m, 
625                    "cpu\t\t: %s\n"
626                    "fpu\t\t: %s\n"
627                    "promlib\t\t: Version 3 Revision %d\n"
628                    "prom\t\t: %d.%d.%d\n"
629                    "type\t\t: sun4u\n"
630                    "ncpus probed\t: %ld\n"
631                    "ncpus active\t: %ld\n"
632 #ifndef CONFIG_SMP
633                    "Cpu0Bogo\t: %lu.%02lu\n"
634                    "Cpu0ClkTck\t: %016lx\n"
635 #endif
636                    ,
637                    sparc_cpu_type,
638                    sparc_fpu_type,
639                    prom_rev,
640                    prom_prev >> 16,
641                    (prom_prev >> 8) & 0xff,
642                    prom_prev & 0xff,
643                    (long)num_possible_cpus(),
644                    (long)num_online_cpus()
645 #ifndef CONFIG_SMP
646                    , cpu_data(0).udelay_val/(500000/HZ),
647                    (cpu_data(0).udelay_val/(5000/HZ)) % 100,
648                    cpu_data(0).clock_tick
649 #endif
650                 );
651 #ifdef CONFIG_SMP
652         smp_bogo(m);
653 #endif
654         mmu_info(m);
655 #ifdef CONFIG_SMP
656         smp_info(m);
657 #endif
658         return 0;
659 }
660
661 static void *c_start(struct seq_file *m, loff_t *pos)
662 {
663         /* The pointer we are returning is arbitrary,
664          * it just has to be non-NULL and not IS_ERR
665          * in the success case.
666          */
667         return *pos == 0 ? &c_start : NULL;
668 }
669
670 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
671 {
672         ++*pos;
673         return c_start(m, pos);
674 }
675
676 static void c_stop(struct seq_file *m, void *v)
677 {
678 }
679
680 struct seq_operations cpuinfo_op = {
681         .start =c_start,
682         .next = c_next,
683         .stop = c_stop,
684         .show = show_cpuinfo,
685 };
686
687 extern int stop_a_enabled;
688
689 void sun_do_break(void)
690 {
691         if (!stop_a_enabled)
692                 return;
693
694         prom_printf("\n");
695         flush_user_windows();
696
697         prom_cmdline();
698 }
699
700 int serial_console = -1;
701 int stop_a_enabled = 1;
702
703 static int __init topology_init(void)
704 {
705         int i, err;
706
707         err = -ENOMEM;
708         for (i = 0; i < NR_CPUS; i++) {
709                 if (cpu_possible(i)) {
710                         struct cpu *p = kmalloc(sizeof(*p), GFP_KERNEL);
711
712                         if (p) {
713                                 memset(p, 0, sizeof(*p));
714                                 register_cpu(p, i, NULL);
715                                 err = 0;
716                         }
717                 }
718         }
719
720         return err;
721 }
722
723 subsys_initcall(topology_init);