[PATCH] mips: add MIPS-specific support for flatmem/discontigmem
[linux-2.6.git] / arch / mips / kernel / setup.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1995 Linus Torvalds
7  * Copyright (C) 1995 Waldorf Electronics
8  * Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 01, 02, 03  Ralf Baechle
9  * Copyright (C) 1996 Stoned Elipot
10  * Copyright (C) 1999 Silicon Graphics, Inc.
11  * Copyright (C) 2000 2001, 2002  Maciej W. Rozycki
12  */
13 #include <linux/config.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/ioport.h>
17 #include <linux/sched.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/stddef.h>
22 #include <linux/string.h>
23 #include <linux/unistd.h>
24 #include <linux/slab.h>
25 #include <linux/user.h>
26 #include <linux/utsname.h>
27 #include <linux/a.out.h>
28 #include <linux/tty.h>
29 #include <linux/bootmem.h>
30 #include <linux/initrd.h>
31 #include <linux/major.h>
32 #include <linux/kdev_t.h>
33 #include <linux/root_dev.h>
34 #include <linux/highmem.h>
35 #include <linux/console.h>
36 #include <linux/mmzone.h>
37
38 #include <asm/addrspace.h>
39 #include <asm/bootinfo.h>
40 #include <asm/cpu.h>
41 #include <asm/sections.h>
42 #include <asm/setup.h>
43 #include <asm/system.h>
44
45 struct cpuinfo_mips cpu_data[NR_CPUS];
46
47 EXPORT_SYMBOL(cpu_data);
48
49 #ifdef CONFIG_VT
50 struct screen_info screen_info;
51 #endif
52
53 /*
54  * Despite it's name this variable is even if we don't have PCI
55  */
56 unsigned int PCI_DMA_BUS_IS_PHYS;
57
58 EXPORT_SYMBOL(PCI_DMA_BUS_IS_PHYS);
59
60 /*
61  * Setup information
62  *
63  * These are initialized so they are in the .data section
64  */
65 unsigned long mips_machtype = MACH_UNKNOWN;
66 unsigned long mips_machgroup = MACH_GROUP_UNKNOWN;
67
68 EXPORT_SYMBOL(mips_machtype);
69 EXPORT_SYMBOL(mips_machgroup);
70
71 struct boot_mem_map boot_mem_map;
72
73 static char command_line[CL_SIZE];
74        char arcs_cmdline[CL_SIZE]=CONFIG_CMDLINE;
75
76 /*
77  * mips_io_port_base is the begin of the address space to which x86 style
78  * I/O ports are mapped.
79  */
80 const unsigned long mips_io_port_base = -1;
81 EXPORT_SYMBOL(mips_io_port_base);
82
83 /*
84  * isa_slot_offset is the address where E(ISA) busaddress 0 is mapped
85  * for the processor.
86  */
87 unsigned long isa_slot_offset;
88 EXPORT_SYMBOL(isa_slot_offset);
89
90 static struct resource code_resource = { .name = "Kernel code", };
91 static struct resource data_resource = { .name = "Kernel data", };
92
93 void __init add_memory_region(phys_t start, phys_t size, long type)
94 {
95         int x = boot_mem_map.nr_map;
96         struct boot_mem_map_entry *prev = boot_mem_map.map + x - 1;
97
98         /*
99          * Try to merge with previous entry if any.  This is far less than
100          * perfect but is sufficient for most real world cases.
101          */
102         if (x && prev->addr + prev->size == start && prev->type == type) {
103                 prev->size += size;
104                 return;
105         }
106
107         if (x == BOOT_MEM_MAP_MAX) {
108                 printk("Ooops! Too many entries in the memory map!\n");
109                 return;
110         }
111
112         boot_mem_map.map[x].addr = start;
113         boot_mem_map.map[x].size = size;
114         boot_mem_map.map[x].type = type;
115         boot_mem_map.nr_map++;
116 }
117
118 static void __init print_memory_map(void)
119 {
120         int i;
121         const int field = 2 * sizeof(unsigned long);
122
123         for (i = 0; i < boot_mem_map.nr_map; i++) {
124                 printk(" memory: %0*Lx @ %0*Lx ",
125                        field, (unsigned long long) boot_mem_map.map[i].size,
126                        field, (unsigned long long) boot_mem_map.map[i].addr);
127
128                 switch (boot_mem_map.map[i].type) {
129                 case BOOT_MEM_RAM:
130                         printk("(usable)\n");
131                         break;
132                 case BOOT_MEM_ROM_DATA:
133                         printk("(ROM data)\n");
134                         break;
135                 case BOOT_MEM_RESERVED:
136                         printk("(reserved)\n");
137                         break;
138                 default:
139                         printk("type %lu\n", boot_mem_map.map[i].type);
140                         break;
141                 }
142         }
143 }
144
145 static inline void parse_cmdline_early(void)
146 {
147         char c = ' ', *to = command_line, *from = saved_command_line;
148         unsigned long start_at, mem_size;
149         int len = 0;
150         int usermem = 0;
151
152         printk("Determined physical RAM map:\n");
153         print_memory_map();
154
155         for (;;) {
156                 /*
157                  * "mem=XXX[kKmM]" defines a memory region from
158                  * 0 to <XXX>, overriding the determined size.
159                  * "mem=XXX[KkmM]@YYY[KkmM]" defines a memory region from
160                  * <YYY> to <YYY>+<XXX>, overriding the determined size.
161                  */
162                 if (c == ' ' && !memcmp(from, "mem=", 4)) {
163                         if (to != command_line)
164                                 to--;
165                         /*
166                          * If a user specifies memory size, we
167                          * blow away any automatically generated
168                          * size.
169                          */
170                         if (usermem == 0) {
171                                 boot_mem_map.nr_map = 0;
172                                 usermem = 1;
173                         }
174                         mem_size = memparse(from + 4, &from);
175                         if (*from == '@')
176                                 start_at = memparse(from + 1, &from);
177                         else
178                                 start_at = 0;
179                         add_memory_region(start_at, mem_size, BOOT_MEM_RAM);
180                 }
181                 c = *(from++);
182                 if (!c)
183                         break;
184                 if (CL_SIZE <= ++len)
185                         break;
186                 *(to++) = c;
187         }
188         *to = '\0';
189
190         if (usermem) {
191                 printk("User-defined physical RAM map:\n");
192                 print_memory_map();
193         }
194 }
195
196 static inline int parse_rd_cmdline(unsigned long* rd_start, unsigned long* rd_end)
197 {
198         /*
199          * "rd_start=0xNNNNNNNN" defines the memory address of an initrd
200          * "rd_size=0xNN" it's size
201          */
202         unsigned long start = 0;
203         unsigned long size = 0;
204         unsigned long end;
205         char cmd_line[CL_SIZE];
206         char *start_str;
207         char *size_str;
208         char *tmp;
209
210         strcpy(cmd_line, command_line);
211         *command_line = 0;
212         tmp = cmd_line;
213         /* Ignore "rd_start=" strings in other parameters. */
214         start_str = strstr(cmd_line, "rd_start=");
215         if (start_str && start_str != cmd_line && *(start_str - 1) != ' ')
216                 start_str = strstr(start_str, " rd_start=");
217         while (start_str) {
218                 if (start_str != cmd_line)
219                         strncat(command_line, tmp, start_str - tmp);
220                 start = memparse(start_str + 9, &start_str);
221                 tmp = start_str + 1;
222                 start_str = strstr(start_str, " rd_start=");
223         }
224         if (*tmp)
225                 strcat(command_line, tmp);
226
227         strcpy(cmd_line, command_line);
228         *command_line = 0;
229         tmp = cmd_line;
230         /* Ignore "rd_size" strings in other parameters. */
231         size_str = strstr(cmd_line, "rd_size=");
232         if (size_str && size_str != cmd_line && *(size_str - 1) != ' ')
233                 size_str = strstr(size_str, " rd_size=");
234         while (size_str) {
235                 if (size_str != cmd_line)
236                         strncat(command_line, tmp, size_str - tmp);
237                 size = memparse(size_str + 8, &size_str);
238                 tmp = size_str + 1;
239                 size_str = strstr(size_str, " rd_size=");
240         }
241         if (*tmp)
242                 strcat(command_line, tmp);
243
244 #ifdef CONFIG_MIPS64
245         /* HACK: Guess if the sign extension was forgotten */
246         if (start > 0x0000000080000000 && start < 0x00000000ffffffff)
247                 start |= 0xffffffff00000000;
248 #endif
249
250         end = start + size;
251         if (start && end) {
252                 *rd_start = start;
253                 *rd_end = end;
254                 return 1;
255         }
256         return 0;
257 }
258
259 #define PFN_UP(x)       (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
260 #define PFN_DOWN(x)     ((x) >> PAGE_SHIFT)
261 #define PFN_PHYS(x)     ((x) << PAGE_SHIFT)
262
263 #define MAXMEM          HIGHMEM_START
264 #define MAXMEM_PFN      PFN_DOWN(MAXMEM)
265
266 static inline void bootmem_init(void)
267 {
268         unsigned long start_pfn;
269         unsigned long reserved_end = (unsigned long)&_end;
270 #ifndef CONFIG_SGI_IP27
271         unsigned long first_usable_pfn;
272         unsigned long bootmap_size;
273         int i;
274 #endif
275 #ifdef CONFIG_BLK_DEV_INITRD
276         int initrd_reserve_bootmem = 0;
277
278         /* Board specific code should have set up initrd_start and initrd_end */
279         ROOT_DEV = Root_RAM0;
280         if (parse_rd_cmdline(&initrd_start, &initrd_end)) {
281                 reserved_end = max(reserved_end, initrd_end);
282                 initrd_reserve_bootmem = 1;
283         } else {
284                 unsigned long tmp;
285                 u32 *initrd_header;
286
287                 tmp = ((reserved_end + PAGE_SIZE-1) & PAGE_MASK) - sizeof(u32) * 2;
288                 if (tmp < reserved_end)
289                         tmp += PAGE_SIZE;
290                 initrd_header = (u32 *)tmp;
291                 if (initrd_header[0] == 0x494E5244) {
292                         initrd_start = (unsigned long)&initrd_header[2];
293                         initrd_end = initrd_start + initrd_header[1];
294                         reserved_end = max(reserved_end, initrd_end);
295                         initrd_reserve_bootmem = 1;
296                 }
297         }
298 #endif  /* CONFIG_BLK_DEV_INITRD */
299
300         /*
301          * Partially used pages are not usable - thus
302          * we are rounding upwards.
303          */
304         start_pfn = PFN_UP(CPHYSADDR(reserved_end));
305
306 #ifndef CONFIG_SGI_IP27
307         /* Find the highest page frame number we have available.  */
308         max_pfn = 0;
309         first_usable_pfn = -1UL;
310         for (i = 0; i < boot_mem_map.nr_map; i++) {
311                 unsigned long start, end;
312
313                 if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
314                         continue;
315
316                 start = PFN_UP(boot_mem_map.map[i].addr);
317                 end = PFN_DOWN(boot_mem_map.map[i].addr
318                       + boot_mem_map.map[i].size);
319
320                 if (start >= end)
321                         continue;
322                 if (end > max_pfn)
323                         max_pfn = end;
324                 if (start < first_usable_pfn) {
325                         if (start > start_pfn) {
326                                 first_usable_pfn = start;
327                         } else if (end > start_pfn) {
328                                 first_usable_pfn = start_pfn;
329                         }
330                 }
331         }
332
333         /*
334          * Determine low and high memory ranges
335          */
336         max_low_pfn = max_pfn;
337         if (max_low_pfn > MAXMEM_PFN) {
338                 max_low_pfn = MAXMEM_PFN;
339 #ifndef CONFIG_HIGHMEM
340                 /* Maximum memory usable is what is directly addressable */
341                 printk(KERN_WARNING "Warning only %ldMB will be used.\n",
342                        MAXMEM >> 20);
343                 printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
344 #endif
345         }
346
347 #ifdef CONFIG_HIGHMEM
348         /*
349          * Crude, we really should make a better attempt at detecting
350          * highstart_pfn
351          */
352         highstart_pfn = highend_pfn = max_pfn;
353         if (max_pfn > MAXMEM_PFN) {
354                 highstart_pfn = MAXMEM_PFN;
355                 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
356                        (highend_pfn - highstart_pfn) >> (20 - PAGE_SHIFT));
357         }
358 #endif
359
360         memory_present(0, first_usable_pfn, max_low_pfn);
361
362         /* Initialize the boot-time allocator with low memory only.  */
363         bootmap_size = init_bootmem(first_usable_pfn, max_low_pfn);
364
365         /*
366          * Register fully available low RAM pages with the bootmem allocator.
367          */
368         for (i = 0; i < boot_mem_map.nr_map; i++) {
369                 unsigned long curr_pfn, last_pfn, size;
370
371                 /*
372                  * Reserve usable memory.
373                  */
374                 if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
375                         continue;
376
377                 /*
378                  * We are rounding up the start address of usable memory:
379                  */
380                 curr_pfn = PFN_UP(boot_mem_map.map[i].addr);
381                 if (curr_pfn >= max_low_pfn)
382                         continue;
383                 if (curr_pfn < start_pfn)
384                         curr_pfn = start_pfn;
385
386                 /*
387                  * ... and at the end of the usable range downwards:
388                  */
389                 last_pfn = PFN_DOWN(boot_mem_map.map[i].addr
390                                     + boot_mem_map.map[i].size);
391
392                 if (last_pfn > max_low_pfn)
393                         last_pfn = max_low_pfn;
394
395                 /*
396                  * Only register lowmem part of lowmem segment with bootmem.
397                  */
398                 size = last_pfn - curr_pfn;
399                 if (curr_pfn > PFN_DOWN(HIGHMEM_START))
400                         continue;
401                 if (curr_pfn + size - 1 > PFN_DOWN(HIGHMEM_START))
402                         size = PFN_DOWN(HIGHMEM_START) - curr_pfn;
403                 if (!size)
404                         continue;
405
406                 /*
407                  * ... finally, did all the rounding and playing
408                  * around just make the area go away?
409                  */
410                 if (last_pfn <= curr_pfn)
411                         continue;
412
413                 /* Register lowmem ranges */
414                 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
415         }
416
417         /* Reserve the bootmap memory.  */
418         reserve_bootmem(PFN_PHYS(first_usable_pfn), bootmap_size);
419 #endif /* CONFIG_SGI_IP27 */
420
421 #ifdef CONFIG_BLK_DEV_INITRD
422         initrd_below_start_ok = 1;
423         if (initrd_start) {
424                 unsigned long initrd_size = ((unsigned char *)initrd_end) - ((unsigned char *)initrd_start);
425                 printk("Initial ramdisk at: 0x%p (%lu bytes)\n",
426                        (void *)initrd_start, initrd_size);
427
428                 if (CPHYSADDR(initrd_end) > PFN_PHYS(max_low_pfn)) {
429                         printk("initrd extends beyond end of memory "
430                                "(0x%0*Lx > 0x%0*Lx)\ndisabling initrd\n",
431                                sizeof(long) * 2,
432                                (unsigned long long)CPHYSADDR(initrd_end),
433                                sizeof(long) * 2,
434                                (unsigned long long)PFN_PHYS(max_low_pfn));
435                         initrd_start = initrd_end = 0;
436                         initrd_reserve_bootmem = 0;
437                 }
438
439                 if (initrd_reserve_bootmem)
440                         reserve_bootmem(CPHYSADDR(initrd_start), initrd_size);
441         }
442 #endif /* CONFIG_BLK_DEV_INITRD  */
443 }
444
445 static inline void resource_init(void)
446 {
447         int i;
448
449 #if defined(CONFIG_MIPS64) && !defined(CONFIG_BUILD_ELF64)
450         /*
451          * The 64bit code in 32bit object format trick can't represent
452          * 64bit wide relocations for linker script symbols.
453          */
454         code_resource.start = CPHYSADDR(&_text);
455         code_resource.end = CPHYSADDR(&_etext) - 1;
456         data_resource.start = CPHYSADDR(&_etext);
457         data_resource.end = CPHYSADDR(&_edata) - 1;
458 #else
459         code_resource.start = virt_to_phys(&_text);
460         code_resource.end = virt_to_phys(&_etext) - 1;
461         data_resource.start = virt_to_phys(&_etext);
462         data_resource.end = virt_to_phys(&_edata) - 1;
463 #endif
464
465         /*
466          * Request address space for all standard RAM.
467          */
468         for (i = 0; i < boot_mem_map.nr_map; i++) {
469                 struct resource *res;
470                 unsigned long start, end;
471
472                 start = boot_mem_map.map[i].addr;
473                 end = boot_mem_map.map[i].addr + boot_mem_map.map[i].size - 1;
474                 if (start >= MAXMEM)
475                         continue;
476                 if (end >= MAXMEM)
477                         end = MAXMEM - 1;
478
479                 res = alloc_bootmem(sizeof(struct resource));
480                 switch (boot_mem_map.map[i].type) {
481                 case BOOT_MEM_RAM:
482                 case BOOT_MEM_ROM_DATA:
483                         res->name = "System RAM";
484                         break;
485                 case BOOT_MEM_RESERVED:
486                 default:
487                         res->name = "reserved";
488                 }
489
490                 res->start = start;
491                 res->end = end;
492
493                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
494                 request_resource(&iomem_resource, res);
495
496                 /*
497                  *  We don't know which RAM region contains kernel data,
498                  *  so we try it repeatedly and let the resource manager
499                  *  test it.
500                  */
501                 request_resource(res, &code_resource);
502                 request_resource(res, &data_resource);
503         }
504 }
505
506 #undef PFN_UP
507 #undef PFN_DOWN
508 #undef PFN_PHYS
509
510 #undef MAXMEM
511 #undef MAXMEM_PFN
512
513 static int __initdata earlyinit_debug;
514
515 static int __init earlyinit_debug_setup(char *str)
516 {
517         earlyinit_debug = 1;
518         return 1;
519 }
520 __setup("earlyinit_debug", earlyinit_debug_setup);
521
522 extern initcall_t __earlyinitcall_start, __earlyinitcall_end;
523
524 static void __init do_earlyinitcalls(void)
525 {
526         initcall_t *call, *start, *end;
527
528         start = &__earlyinitcall_start;
529         end = &__earlyinitcall_end;
530
531         for (call = start; call < end; call++) {
532                 if (earlyinit_debug)
533                         printk("calling earlyinitcall 0x%p\n", *call);
534
535                 (*call)();
536         }
537 }
538
539 void __init setup_arch(char **cmdline_p)
540 {
541         cpu_probe();
542         prom_init();
543         cpu_report();
544
545 #if defined(CONFIG_VT)
546 #if defined(CONFIG_VGA_CONSOLE)
547         conswitchp = &vga_con;
548 #elif defined(CONFIG_DUMMY_CONSOLE)
549         conswitchp = &dummy_con;
550 #endif
551 #endif
552
553         /* call board setup routine */
554         do_earlyinitcalls();
555
556         strlcpy(command_line, arcs_cmdline, sizeof(command_line));
557         strlcpy(saved_command_line, command_line, COMMAND_LINE_SIZE);
558
559         *cmdline_p = command_line;
560
561         parse_cmdline_early();
562         bootmem_init();
563         sparse_init();
564         paging_init();
565         resource_init();
566 }
567
568 int __init fpu_disable(char *s)
569 {
570         cpu_data[0].options &= ~MIPS_CPU_FPU;
571
572         return 1;
573 }
574
575 __setup("nofpu", fpu_disable);