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