vserver 1.9.3
[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                         pmd_t *pmdp;
155                         pte_t *ptep;
156
157                         for_each_process(p) {
158                                 mm = p->mm;
159                                 if (CTX_HWBITS(mm->context) == ctx)
160                                         break;
161                         }
162                         if (!mm ||
163                             CTX_HWBITS(mm->context) != ctx)
164                                 goto done;
165
166                         pgdp = pgd_offset(mm, va);
167                         if (pgd_none(*pgdp))
168                                 goto done;
169                         pmdp = pmd_offset(pgdp, va);
170                         if (pmd_none(*pmdp))
171                                 goto done;
172
173                         /* Preemption implicitly disabled by virtue of
174                          * being called from inside OBP.
175                          */
176                         ptep = pte_offset_map(pmdp, va);
177                         if (pte_present(*ptep)) {
178                                 tte = pte_val(*ptep);
179                                 res = PROM_TRUE;
180                         }
181                         pte_unmap(ptep);
182                         goto done;
183                 }
184
185                 if ((va >= KERNBASE) && (va < (KERNBASE + (4 * 1024 * 1024)))) {
186                         /* Spitfire Errata #32 workaround */
187                         __asm__ __volatile__("stxa      %0, [%1] %2\n\t"
188                                              "flush     %%g6"
189                                              : /* No outputs */
190                                              : "r" (0),
191                                              "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
192
193                         /*
194                          * Locked down tlb entry.
195                          */
196
197                         if (tlb_type == spitfire)
198                                 tte = spitfire_get_dtlb_data(SPITFIRE_HIGHEST_LOCKED_TLBENT);
199                         else if (tlb_type == cheetah || tlb_type == cheetah_plus)
200                                 tte = cheetah_get_ldtlb_data(CHEETAH_HIGHEST_LOCKED_TLBENT);
201
202                         res = PROM_TRUE;
203                         goto done;
204                 }
205
206                 if (va < PGDIR_SIZE) {
207                         /*
208                          * vmalloc or prom_inherited mapping.
209                          */
210                         pgd_t *pgdp;
211                         pmd_t *pmdp;
212                         pte_t *ptep;
213                         int error;
214
215                         if ((va >= LOW_OBP_ADDRESS) && (va < HI_OBP_ADDRESS)) {
216                                 tte = prom_virt_to_phys(va, &error);
217                                 if (!error)
218                                         res = PROM_TRUE;
219                                 goto done;
220                         }
221                         pgdp = pgd_offset_k(va);
222                         if (pgd_none(*pgdp))
223                                 goto done;
224                         pmdp = pmd_offset(pgdp, va);
225                         if (pmd_none(*pmdp))
226                                 goto done;
227
228                         /* Preemption implicitly disabled by virtue of
229                          * being called from inside OBP.
230                          */
231                         ptep = pte_offset_kernel(pmdp, va);
232                         if (pte_present(*ptep)) {
233                                 tte = pte_val(*ptep);
234                                 res = PROM_TRUE;
235                         }
236                         goto done;
237                 }
238
239                 if (va < PAGE_OFFSET) {
240                         /*
241                          * No mappings here.
242                          */
243                         goto done;
244                 }
245
246                 if (va & (1UL << 40)) {
247                         /*
248                          * I/O page.
249                          */
250
251                         tte = (__pa(va) & _PAGE_PADDR) |
252                               _PAGE_VALID | _PAGE_SZ4MB |
253                               _PAGE_E | _PAGE_P | _PAGE_W;
254                         res = PROM_TRUE;
255                         goto done;
256                 }
257
258                 /*
259                  * Normal page.
260                  */
261                 tte = (__pa(va) & _PAGE_PADDR) |
262                       _PAGE_VALID | _PAGE_SZ4MB |
263                       _PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_W;
264                 res = PROM_TRUE;
265
266         done:
267                 if (res == PROM_TRUE) {
268                         args[2] = 3;
269                         args[args[1] + 3] = 0;
270                         args[args[1] + 4] = res;
271                         args[args[1] + 5] = tte;
272                 } else {
273                         args[2] = 2;
274                         args[args[1] + 3] = 0;
275                         args[args[1] + 4] = res;
276                 }
277         } else if (!strcmp(cmd, ".soft1")) {
278                 unsigned long tte;
279
280                 tte = args[3];
281                 prom_printf("%lx:\"%s%s%s%s%s\" ",
282                             (tte & _PAGE_SOFT) >> 7,
283                             tte & _PAGE_MODIFIED ? "M" : "-",
284                             tte & _PAGE_ACCESSED ? "A" : "-",
285                             tte & _PAGE_READ     ? "W" : "-",
286                             tte & _PAGE_WRITE    ? "R" : "-",
287                             tte & _PAGE_PRESENT  ? "P" : "-");
288
289                 args[2] = 2;
290                 args[args[1] + 3] = 0;
291                 args[args[1] + 4] = PROM_TRUE;
292         } else if (!strcmp(cmd, ".soft2")) {
293                 unsigned long tte;
294
295                 tte = args[3];
296                 prom_printf("%lx ", (tte & 0x07FC000000000000UL) >> 50);
297
298                 args[2] = 2;
299                 args[args[1] + 3] = 0;
300                 args[args[1] + 4] = PROM_TRUE;
301         } else {
302                 prom_printf("unknown PROM `%s' command...\n", cmd);
303         }
304         unregister_console(&prom_console);
305         while (saved_console) {
306                 cons = saved_console;
307                 saved_console = cons->next;
308                 register_console(cons);
309         }
310         spin_lock(&prom_entry_lock);
311         local_irq_restore(flags);
312
313         /*
314          * Restore in-interrupt status for a resume from obp.
315          */
316         irq_enter();
317         return 0;
318 }
319
320 unsigned int boot_flags = 0;
321 #define BOOTME_DEBUG  0x1
322 #define BOOTME_SINGLE 0x2
323
324 /* Exported for mm/init.c:paging_init. */
325 unsigned long cmdline_memory_size = 0;
326
327 static struct console prom_debug_console = {
328         .name =         "debug",
329         .write =        prom_console_write,
330         .flags =        CON_PRINTBUFFER,
331         .index =        -1,
332 };
333
334 /* XXX Implement this at some point... */
335 void kernel_enter_debugger(void)
336 {
337 }
338
339 int obp_system_intr(void)
340 {
341         if (boot_flags & BOOTME_DEBUG) {
342                 printk("OBP: system interrupted\n");
343                 prom_halt();
344                 return 1;
345         }
346         return 0;
347 }
348
349 /* 
350  * Process kernel command line switches that are specific to the
351  * SPARC or that require special low-level processing.
352  */
353 static void __init process_switch(char c)
354 {
355         switch (c) {
356         case 'd':
357                 boot_flags |= BOOTME_DEBUG;
358                 break;
359         case 's':
360                 boot_flags |= BOOTME_SINGLE;
361                 break;
362         case 'h':
363                 prom_printf("boot_flags_init: Halt!\n");
364                 prom_halt();
365                 break;
366         case 'p':
367                 /* Use PROM debug console. */
368                 register_console(&prom_debug_console);
369                 break;
370         default:
371                 printk("Unknown boot switch (-%c)\n", c);
372                 break;
373         }
374 }
375
376 static void __init process_console(char *commands)
377 {
378         serial_console = 0;
379         commands += 8;
380         /* Linux-style serial */
381         if (!strncmp(commands, "ttyS", 4))
382                 serial_console = simple_strtoul(commands + 4, NULL, 10) + 1;
383         else if (!strncmp(commands, "tty", 3)) {
384                 char c = *(commands + 3);
385                 /* Solaris-style serial */
386                 if (c == 'a' || c == 'b') {
387                         serial_console = c - 'a' + 1;
388                         prom_printf ("Using /dev/tty%c as console.\n", c);
389                 }
390                 /* else Linux-style fbcon, not serial */
391         }
392 #if defined(CONFIG_PROM_CONSOLE)
393         if (!strncmp(commands, "prom", 4)) {
394                 char *p;
395
396                 for (p = commands - 8; *p && *p != ' '; p++)
397                         *p = ' ';
398                 conswitchp = &prom_con;
399         }
400 #endif
401 }
402
403 static void __init boot_flags_init(char *commands)
404 {
405         while (*commands) {
406                 /* Move to the start of the next "argument". */
407                 while (*commands && *commands == ' ')
408                         commands++;
409
410                 /* Process any command switches, otherwise skip it. */
411                 if (*commands == '\0')
412                         break;
413                 if (*commands == '-') {
414                         commands++;
415                         while (*commands && *commands != ' ')
416                                 process_switch(*commands++);
417                         continue;
418                 }
419                 if (!strncmp(commands, "console=", 8)) {
420                         process_console(commands);
421                 } else if (!strncmp(commands, "mem=", 4)) {
422                         /*
423                          * "mem=XXX[kKmM]" overrides the PROM-reported
424                          * memory size.
425                          */
426                         cmdline_memory_size = simple_strtoul(commands + 4,
427                                                              &commands, 0);
428                         if (*commands == 'K' || *commands == 'k') {
429                                 cmdline_memory_size <<= 10;
430                                 commands++;
431                         } else if (*commands=='M' || *commands=='m') {
432                                 cmdline_memory_size <<= 20;
433                                 commands++;
434                         }
435                 }
436                 while (*commands && *commands != ' ')
437                         commands++;
438         }
439 }
440
441 extern int prom_probe_memory(void);
442 extern unsigned long start, end;
443 extern void panic_setup(char *, int *);
444
445 extern unsigned short root_flags;
446 extern unsigned short root_dev;
447 extern unsigned short ram_flags;
448 #define RAMDISK_IMAGE_START_MASK        0x07FF
449 #define RAMDISK_PROMPT_FLAG             0x8000
450 #define RAMDISK_LOAD_FLAG               0x4000
451
452 extern int root_mountflags;
453
454 char reboot_command[COMMAND_LINE_SIZE];
455
456 static struct pt_regs fake_swapper_regs = { { 0, }, 0, 0, 0, 0 };
457
458 void register_prom_callbacks(void)
459 {
460         prom_setcallback(prom_callback);
461         prom_feval(": linux-va>tte-data 2 \" va>tte-data\" $callback drop ; "
462                    "' linux-va>tte-data to va>tte-data");
463         prom_feval(": linux-.soft1 1 \" .soft1\" $callback 2drop ; "
464                    "' linux-.soft1 to .soft1");
465         prom_feval(": linux-.soft2 1 \" .soft2\" $callback 2drop ; "
466                    "' linux-.soft2 to .soft2");
467 }
468
469 extern void paging_init(void);
470
471 void __init setup_arch(char **cmdline_p)
472 {
473         unsigned long highest_paddr;
474         int i;
475
476         /* Initialize PROM console and command line. */
477         *cmdline_p = prom_getbootargs();
478         strcpy(saved_command_line, *cmdline_p);
479
480         printk("ARCH: SUN4U\n");
481
482 #ifdef CONFIG_DUMMY_CONSOLE
483         conswitchp = &dummy_con;
484 #elif defined(CONFIG_PROM_CONSOLE)
485         conswitchp = &prom_con;
486 #endif
487
488 #ifdef CONFIG_SMP
489         i = (unsigned long)&irq_stat[1] - (unsigned long)&irq_stat[0];
490         if ((i == SMP_CACHE_BYTES) || (i == (2 * SMP_CACHE_BYTES))) {
491                 extern unsigned int irqsz_patchme[1];
492                 irqsz_patchme[0] |= ((i == SMP_CACHE_BYTES) ? SMP_CACHE_BYTES_SHIFT : \
493                                                         SMP_CACHE_BYTES_SHIFT + 1);
494                 flushi((long)&irqsz_patchme[0]);
495         } else {
496                 prom_printf("Unexpected size of irq_stat[] elements\n");
497                 prom_halt();
498         }
499 #endif
500         /* Work out if we are starfire early on */
501         check_if_starfire();
502
503         boot_flags_init(*cmdline_p);
504
505         idprom_init();
506         (void) prom_probe_memory();
507
508         /* In paging_init() we tip off this value to see if we need
509          * to change init_mm.pgd to point to the real alias mapping.
510          */
511         phys_base = 0xffffffffffffffffUL;
512         highest_paddr = 0UL;
513         for (i = 0; sp_banks[i].num_bytes != 0; i++) {
514                 unsigned long top;
515
516                 if (sp_banks[i].base_addr < phys_base)
517                         phys_base = sp_banks[i].base_addr;
518                 top = sp_banks[i].base_addr +
519                         sp_banks[i].num_bytes;
520                 if (highest_paddr < top)
521                         highest_paddr = top;
522         }
523         pfn_base = phys_base >> PAGE_SHIFT;
524
525         switch (tlb_type) {
526         default:
527         case spitfire:
528                 kern_base = spitfire_get_itlb_data(sparc64_highest_locked_tlbent());
529                 kern_base &= _PAGE_PADDR_SF;
530                 break;
531
532         case cheetah:
533         case cheetah_plus:
534                 kern_base = cheetah_get_litlb_data(sparc64_highest_locked_tlbent());
535                 kern_base &= _PAGE_PADDR;
536                 break;
537         };
538
539         kern_size = (unsigned long)&_end - (unsigned long)KERNBASE;
540
541         if (!root_flags)
542                 root_mountflags &= ~MS_RDONLY;
543         ROOT_DEV = old_decode_dev(root_dev);
544 #ifdef CONFIG_BLK_DEV_INITRD
545         rd_image_start = ram_flags & RAMDISK_IMAGE_START_MASK;
546         rd_prompt = ((ram_flags & RAMDISK_PROMPT_FLAG) != 0);
547         rd_doload = ((ram_flags & RAMDISK_LOAD_FLAG) != 0);     
548 #endif
549
550         init_task.thread_info->kregs = &fake_swapper_regs;
551
552 #ifdef CONFIG_IP_PNP
553         if (!ic_set_manually) {
554                 int chosen = prom_finddevice ("/chosen");
555                 u32 cl, sv, gw;
556                 
557                 cl = prom_getintdefault (chosen, "client-ip", 0);
558                 sv = prom_getintdefault (chosen, "server-ip", 0);
559                 gw = prom_getintdefault (chosen, "gateway-ip", 0);
560                 if (cl && sv) {
561                         ic_myaddr = cl;
562                         ic_servaddr = sv;
563                         if (gw)
564                                 ic_gateway = gw;
565 #if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_RARP)
566                         ic_proto_enabled = 0;
567 #endif
568                 }
569         }
570 #endif
571
572         paging_init();
573 }
574
575 static int __init set_preferred_console(void)
576 {
577         int idev, odev;
578
579         /* The user has requested a console so this is already set up. */
580         if (serial_console >= 0)
581                 return -EBUSY;
582
583         idev = prom_query_input_device();
584         odev = prom_query_output_device();
585         if (idev == PROMDEV_IKBD && odev == PROMDEV_OSCREEN) {
586                 serial_console = 0;
587         } else if (idev == PROMDEV_ITTYA && odev == PROMDEV_OTTYA) {
588                 serial_console = 1;
589         } else if (idev == PROMDEV_ITTYB && odev == PROMDEV_OTTYB) {
590                 serial_console = 2;
591         } else {
592                 prom_printf("Inconsistent console: "
593                             "input %d, output %d\n",
594                             idev, odev);
595                 prom_halt();
596         }
597
598         if (serial_console)
599                 return add_preferred_console("ttyS", serial_console - 1, NULL);
600
601         return -ENODEV;
602 }
603 console_initcall(set_preferred_console);
604
605 /* BUFFER is PAGE_SIZE bytes long. */
606
607 extern char *sparc_cpu_type;
608 extern char *sparc_fpu_type;
609
610 extern void smp_info(struct seq_file *);
611 extern void smp_bogo(struct seq_file *);
612 extern void mmu_info(struct seq_file *);
613
614 static int show_cpuinfo(struct seq_file *m, void *__unused)
615 {
616         seq_printf(m, 
617                    "cpu\t\t: %s\n"
618                    "fpu\t\t: %s\n"
619                    "promlib\t\t: Version 3 Revision %d\n"
620                    "prom\t\t: %d.%d.%d\n"
621                    "type\t\t: sun4u\n"
622                    "ncpus probed\t: %ld\n"
623                    "ncpus active\t: %ld\n"
624 #ifndef CONFIG_SMP
625                    "Cpu0Bogo\t: %lu.%02lu\n"
626                    "Cpu0ClkTck\t: %016lx\n"
627 #endif
628                    ,
629                    sparc_cpu_type,
630                    sparc_fpu_type,
631                    prom_rev,
632                    prom_prev >> 16,
633                    (prom_prev >> 8) & 0xff,
634                    prom_prev & 0xff,
635                    (long)num_possible_cpus(),
636                    (long)num_online_cpus()
637 #ifndef CONFIG_SMP
638                    , cpu_data(0).udelay_val/(500000/HZ),
639                    (cpu_data(0).udelay_val/(5000/HZ)) % 100,
640                    cpu_data(0).clock_tick
641 #endif
642                 );
643 #ifdef CONFIG_SMP
644         smp_bogo(m);
645 #endif
646         mmu_info(m);
647 #ifdef CONFIG_SMP
648         smp_info(m);
649 #endif
650         return 0;
651 }
652
653 static void *c_start(struct seq_file *m, loff_t *pos)
654 {
655         /* The pointer we are returning is arbitrary,
656          * it just has to be non-NULL and not IS_ERR
657          * in the success case.
658          */
659         return *pos == 0 ? &c_start : NULL;
660 }
661
662 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
663 {
664         ++*pos;
665         return c_start(m, pos);
666 }
667
668 static void c_stop(struct seq_file *m, void *v)
669 {
670 }
671
672 struct seq_operations cpuinfo_op = {
673         .start =c_start,
674         .next = c_next,
675         .stop = c_stop,
676         .show = show_cpuinfo,
677 };
678
679 extern int stop_a_enabled;
680
681 void sun_do_break(void)
682 {
683         if (!stop_a_enabled)
684                 return;
685
686         prom_printf("\n");
687         flush_user_windows();
688
689         prom_cmdline();
690 }
691
692 int serial_console = -1;
693 int stop_a_enabled = 1;
694
695 static int __init topology_init(void)
696 {
697         int i, err;
698
699         err = -ENOMEM;
700         for (i = 0; i < NR_CPUS; i++) {
701                 if (cpu_possible(i)) {
702                         struct cpu *p = kmalloc(sizeof(*p), GFP_KERNEL);
703
704                         if (p) {
705                                 memset(p, 0, sizeof(*p));
706                                 register_cpu(p, i, NULL);
707                                 err = 0;
708                         }
709                 }
710         }
711
712         return err;
713 }
714
715 subsys_initcall(topology_init);