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/init.h>
14#include <linux/ioport.h>
15#include <linux/module.h>
16#include <linux/screen_info.h>
17#include <linux/bootmem.h>
18#include <linux/initrd.h>
19#include <linux/root_dev.h>
20#include <linux/highmem.h>
21#include <linux/console.h>
22#include <linux/pfn.h>
23
24#include <asm/addrspace.h>
25#include <asm/bootinfo.h>
26#include <asm/cache.h>
27#include <asm/cpu.h>
28#include <asm/sections.h>
29#include <asm/setup.h>
30#include <asm/system.h>
31
32struct cpuinfo_mips cpu_data[NR_CPUS] __read_mostly;
33
34EXPORT_SYMBOL(cpu_data);
35
36#ifdef CONFIG_VT
37struct screen_info screen_info;
38#endif
39
40/*
41 * Despite it's name this variable is even if we don't have PCI
42 */
43unsigned int PCI_DMA_BUS_IS_PHYS;
44
45EXPORT_SYMBOL(PCI_DMA_BUS_IS_PHYS);
46
47/*
48 * Setup information
49 *
50 * These are initialized so they are in the .data section
51 */
52unsigned long mips_machtype __read_mostly = MACH_UNKNOWN;
53unsigned long mips_machgroup __read_mostly = MACH_GROUP_UNKNOWN;
54
55EXPORT_SYMBOL(mips_machtype);
56EXPORT_SYMBOL(mips_machgroup);
57
58struct boot_mem_map boot_mem_map;
59
60static char command_line[CL_SIZE];
61       char arcs_cmdline[CL_SIZE]=CONFIG_CMDLINE;
62
63/*
64 * mips_io_port_base is the begin of the address space to which x86 style
65 * I/O ports are mapped.
66 */
67const unsigned long mips_io_port_base __read_mostly = -1;
68EXPORT_SYMBOL(mips_io_port_base);
69
70/*
71 * isa_slot_offset is the address where E(ISA) busaddress 0 is mapped
72 * for the processor.
73 */
74unsigned long isa_slot_offset;
75EXPORT_SYMBOL(isa_slot_offset);
76
77static struct resource code_resource = { .name = "Kernel code", };
78static struct resource data_resource = { .name = "Kernel data", };
79
80void __init add_memory_region(phys_t start, phys_t size, long type)
81{
82	int x = boot_mem_map.nr_map;
83	struct boot_mem_map_entry *prev = boot_mem_map.map + x - 1;
84
85	/* Sanity check */
86	if (start + size < start) {
87		printk("Trying to add an invalid memory region, skipped\n");
88		return;
89	}
90
91	/*
92	 * Try to merge with previous entry if any.  This is far less than
93	 * perfect but is sufficient for most real world cases.
94	 */
95	if (x && prev->addr + prev->size == start && prev->type == type) {
96		prev->size += size;
97		return;
98	}
99
100	if (x == BOOT_MEM_MAP_MAX) {
101		printk("Ooops! Too many entries in the memory map!\n");
102		return;
103	}
104
105	boot_mem_map.map[x].addr = start;
106	boot_mem_map.map[x].size = size;
107	boot_mem_map.map[x].type = type;
108	boot_mem_map.nr_map++;
109}
110
111static void __init print_memory_map(void)
112{
113	int i;
114	const int field = 2 * sizeof(unsigned long);
115
116	for (i = 0; i < boot_mem_map.nr_map; i++) {
117		printk(" memory: %0*Lx @ %0*Lx ",
118		       field, (unsigned long long) boot_mem_map.map[i].size,
119		       field, (unsigned long long) boot_mem_map.map[i].addr);
120
121		switch (boot_mem_map.map[i].type) {
122		case BOOT_MEM_RAM:
123			printk("(usable)\n");
124			break;
125		case BOOT_MEM_ROM_DATA:
126			printk("(ROM data)\n");
127			break;
128		case BOOT_MEM_RESERVED:
129			printk("(reserved)\n");
130			break;
131		default:
132			printk("type %lu\n", boot_mem_map.map[i].type);
133			break;
134		}
135	}
136}
137
138/*
139 * Manage initrd
140 */
141#ifdef CONFIG_BLK_DEV_INITRD
142
143static int __init rd_start_early(char *p)
144{
145	unsigned long start = memparse(p, &p);
146
147#ifdef CONFIG_64BIT
148	/* Guess if the sign extension was forgotten by bootloader */
149	if (start < XKPHYS)
150		start = (int)start;
151#endif
152	initrd_start = start;
153	initrd_end += start;
154	return 0;
155}
156early_param("rd_start", rd_start_early);
157
158static int __init rd_size_early(char *p)
159{
160	initrd_end += memparse(p, &p);
161	return 0;
162}
163early_param("rd_size", rd_size_early);
164
165/* it returns the next free pfn after initrd */
166static unsigned long __init init_initrd(void)
167{
168	unsigned long end;
169	u32 *initrd_header;
170
171	/*
172	 * Board specific code or command line parser should have
173	 * already set up initrd_start and initrd_end. In these cases
174	 * perfom sanity checks and use them if all looks good.
175	 */
176	if (initrd_start && initrd_end > initrd_start)
177		goto sanitize;
178
179	/*
180	 * See if initrd has been added to the kernel image by
181	 * arch/mips/boot/addinitrd.c. In that case a header is
182	 * prepended to initrd and is made up by 8 bytes. The fisrt
183	 * word is a magic number and the second one is the size of
184	 * initrd.  Initrd start must be page aligned in any cases.
185	 */
186	initrd_header = __va(PAGE_ALIGN(__pa_symbol(&_end) + 8)) - 8;
187	if (initrd_header[0] != 0x494E5244)
188		goto disable;
189	initrd_start = (unsigned long)(initrd_header + 2);
190	initrd_end = initrd_start + initrd_header[1];
191
192sanitize:
193	if (initrd_start & ~PAGE_MASK) {
194		printk(KERN_ERR "initrd start must be page aligned\n");
195		goto disable;
196	}
197	if (initrd_start < PAGE_OFFSET) {
198		printk(KERN_ERR "initrd start < PAGE_OFFSET\n");
199		goto disable;
200	}
201
202	/*
203	 * Sanitize initrd addresses. For example firmware
204	 * can't guess if they need to pass them through
205	 * 64-bits values if the kernel has been built in pure
206	 * 32-bit. We need also to switch from KSEG0 to XKPHYS
207	 * addresses now, so the code can now safely use __pa().
208	 */
209	end = __pa(initrd_end);
210	initrd_end = (unsigned long)__va(end);
211	initrd_start = (unsigned long)__va(__pa(initrd_start));
212
213	ROOT_DEV = Root_RAM0;
214	return PFN_UP(end);
215disable:
216	initrd_start = 0;
217	initrd_end = 0;
218	return 0;
219}
220
221static void __init finalize_initrd(void)
222{
223	unsigned long size = initrd_end - initrd_start;
224
225	if (size == 0) {
226		printk(KERN_INFO "Initrd not found or empty");
227		goto disable;
228	}
229	if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
230		printk("Initrd extends beyond end of memory");
231		goto disable;
232	}
233
234	reserve_bootmem(__pa(initrd_start), size);
235	initrd_below_start_ok = 1;
236
237	printk(KERN_INFO "Initial ramdisk at: 0x%lx (%lu bytes)\n",
238	       initrd_start, size);
239	return;
240disable:
241	printk(" - disabling initrd\n");
242	initrd_start = 0;
243	initrd_end = 0;
244}
245
246#else  /* !CONFIG_BLK_DEV_INITRD */
247
248static unsigned long __init init_initrd(void)
249{
250	return 0;
251}
252
253#define finalize_initrd()	do {} while (0)
254
255#endif
256
257/*
258 * Initialize the bootmem allocator. It also setup initrd related data
259 * if needed.
260 */
261#ifdef CONFIG_SGI_IP27
262
263static void __init bootmem_init(void)
264{
265	init_initrd();
266	finalize_initrd();
267}
268
269#else  /* !CONFIG_SGI_IP27 */
270
271static void __init bootmem_init(void)
272{
273	unsigned long reserved_end;
274	unsigned long mapstart = ~0UL;
275	unsigned long bootmap_size;
276	int i;
277
278	/*
279	 * Init any data related to initrd. It's a nop if INITRD is
280	 * not selected. Once that done we can determine the low bound
281	 * of usable memory.
282	 */
283	reserved_end = max(init_initrd(), PFN_UP(__pa_symbol(&_end)));
284
285	/*
286	 * max_low_pfn is not a number of pages. The number of pages
287	 * of the system is given by 'max_low_pfn - min_low_pfn'.
288	 */
289	min_low_pfn = ~0UL;
290	max_low_pfn = 0;
291
292	/*
293	 * Find the highest page frame number we have available.
294	 */
295	for (i = 0; i < boot_mem_map.nr_map; i++) {
296		unsigned long start, end;
297
298		if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
299			continue;
300
301		start = PFN_UP(boot_mem_map.map[i].addr);
302		end = PFN_DOWN(boot_mem_map.map[i].addr
303				+ boot_mem_map.map[i].size);
304
305		if (end > max_low_pfn)
306			max_low_pfn = end;
307		if (start < min_low_pfn)
308			min_low_pfn = start;
309		if (end <= reserved_end)
310			continue;
311		if (start >= mapstart)
312			continue;
313		mapstart = max(reserved_end, start);
314	}
315
316	if (min_low_pfn >= max_low_pfn)
317		panic("Incorrect memory mapping !!!");
318	if (min_low_pfn > ARCH_PFN_OFFSET) {
319		printk(KERN_INFO
320		       "Wasting %lu bytes for tracking %lu unused pages\n",
321		       (min_low_pfn - ARCH_PFN_OFFSET) * sizeof(struct page),
322		       min_low_pfn - ARCH_PFN_OFFSET);
323	} else if (min_low_pfn < ARCH_PFN_OFFSET) {
324		printk(KERN_INFO
325		       "%lu free pages won't be used\n",
326		       ARCH_PFN_OFFSET - min_low_pfn);
327	}
328	min_low_pfn = ARCH_PFN_OFFSET;
329
330	/*
331	 * Determine low and high memory ranges
332	 */
333	if (max_low_pfn > PFN_DOWN(HIGHMEM_START)) {
334#ifdef CONFIG_HIGHMEM
335		highstart_pfn = PFN_DOWN(HIGHMEM_START);
336		highend_pfn = max_low_pfn;
337#endif
338		max_low_pfn = PFN_DOWN(HIGHMEM_START);
339	}
340
341	/*
342	 * Initialize the boot-time allocator with low memory only.
343	 */
344	bootmap_size = init_bootmem_node(NODE_DATA(0), mapstart,
345					 min_low_pfn, max_low_pfn);
346
347
348#ifdef CONFIG_SPARSEMEM
349	for (i = 0; i < boot_mem_map.nr_map; i++) {
350		unsigned long start, end;
351
352		start = PFN_UP(boot_mem_map.map[i].addr);
353		end = PFN_DOWN(boot_mem_map.map[i].addr
354				+ boot_mem_map.map[i].size);
355
356		if (start <= min_low_pfn)
357			start = min_low_pfn;
358		if (start >= end)
359			continue;
360
361#ifndef CONFIG_HIGHMEM
362		if (end > max_low_pfn)
363			end = max_low_pfn;
364
365		/*
366		 * ... finally, is the area going away?
367		 */
368		if (end <= start)
369			continue;
370#endif /* CONFIG_HIGHMEM */
371
372		add_active_range(0, start, end);
373	}
374#endif /* CONFIG_SPARSEMEM */
375	/*
376	 * Register fully available low RAM pages with the bootmem allocator.
377	 */
378	for (i = 0; i < boot_mem_map.nr_map; i++) {
379		unsigned long start, end, size;
380
381		/*
382	 	 * Reserve usable memory.
383	 	 */
384		if (boot_mem_map.map[i].type != BOOT_MEM_RAM)
385			continue;
386
387		start = PFN_UP(boot_mem_map.map[i].addr);
388		end   = PFN_DOWN(boot_mem_map.map[i].addr
389				    + boot_mem_map.map[i].size);
390		/*
391		 * We are rounding up the start address of usable memory
392		 * and at the end of the usable range downwards.
393		 */
394		if (start >= max_low_pfn)
395			continue;
396		if (start < reserved_end)
397			start = reserved_end;
398		if (end > max_low_pfn)
399			end = max_low_pfn;
400
401		/*
402		 * ... finally, is the area going away?
403		 */
404		if (end <= start)
405			continue;
406		size = end - start;
407
408		/* Register lowmem ranges */
409		free_bootmem(PFN_PHYS(start), size << PAGE_SHIFT);
410
411		/*
412		 * Reserve the bootmap memory.
413		 */
414		if ((mapstart >= start) && bootmap_size <= size)
415			reserve_bootmem(PFN_PHYS(mapstart), bootmap_size);
416
417		memory_present(0, start, end);
418	}
419
420
421	/*
422	 * Reserve initrd memory if needed.
423	 */
424	finalize_initrd();
425}
426
427#endif	/* CONFIG_SGI_IP27 */
428
429/*
430 * arch_mem_init - initialize memory managment subsystem
431 *
432 *  o plat_mem_setup() detects the memory configuration and will record detected
433 *    memory areas using add_memory_region.
434 *
435 * At this stage the memory configuration of the system is known to the
436 * kernel but generic memory managment system is still entirely uninitialized.
437 *
438 *  o bootmem_init()
439 *  o sparse_init()
440 *  o paging_init()
441 *
442 * At this stage the bootmem allocator is ready to use.
443 *
444 * NOTE: historically plat_mem_setup did the entire platform initialization.
445 *       This was rather impractical because it meant plat_mem_setup had to
446 * get away without any kind of memory allocator.  To keep old code from
447 * breaking plat_setup was just renamed to plat_setup and a second platform
448 * initialization hook for anything else was introduced.
449 */
450
451static int usermem __initdata = 0;
452
453static int __init early_parse_mem(char *p)
454{
455	unsigned long start, size;
456
457	/*
458	 * If a user specifies memory size, we
459	 * blow away any automatically generated
460	 * size.
461	 */
462	if (usermem == 0) {
463		boot_mem_map.nr_map = 0;
464		usermem = 1;
465 	}
466	start = 0;
467	size = memparse(p, &p);
468	if (*p == '@')
469		start = memparse(p + 1, &p);
470
471	add_memory_region(start, size, BOOT_MEM_RAM);
472	return 0;
473}
474early_param("mem", early_parse_mem);
475
476static void __init arch_mem_init(char **cmdline_p)
477{
478	extern void plat_mem_setup(void);
479
480	/* call board setup routine */
481	plat_mem_setup();
482
483	printk("Determined physical RAM map:\n");
484	print_memory_map();
485
486	strlcpy(command_line, arcs_cmdline, sizeof(command_line));
487	strlcpy(boot_command_line, command_line, COMMAND_LINE_SIZE);
488
489	*cmdline_p = command_line;
490
491	parse_early_param();
492
493	if (usermem) {
494		printk("User-defined physical RAM map:\n");
495		print_memory_map();
496	}
497
498	bootmem_init();
499#ifdef CONFIG_SPARSEMEM
500        sparse_memory_present_with_active_regions(MAX_NUMNODES);
501#endif
502	sparse_init();
503	paging_init();
504}
505
506static void __init resource_init(void)
507{
508	int i;
509
510	if (UNCAC_BASE != IO_BASE)
511		return;
512
513	code_resource.start = __pa_symbol(&_text);
514	code_resource.end = __pa_symbol(&_etext) - 1;
515	data_resource.start = __pa_symbol(&_etext);
516	data_resource.end = __pa_symbol(&_edata) - 1;
517
518	/*
519	 * Request address space for all standard RAM.
520	 */
521	for (i = 0; i < boot_mem_map.nr_map; i++) {
522		struct resource *res;
523		unsigned long start, end;
524
525		start = boot_mem_map.map[i].addr;
526		end = boot_mem_map.map[i].addr + boot_mem_map.map[i].size - 1;
527		if (start >= HIGHMEM_START)
528			continue;
529		if (end >= HIGHMEM_START)
530			end = HIGHMEM_START - 1;
531
532		res = alloc_bootmem(sizeof(struct resource));
533		switch (boot_mem_map.map[i].type) {
534		case BOOT_MEM_RAM:
535		case BOOT_MEM_ROM_DATA:
536			res->name = "System RAM";
537			break;
538		case BOOT_MEM_RESERVED:
539		default:
540			res->name = "reserved";
541		}
542
543		res->start = start;
544		res->end = end;
545
546		res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
547		request_resource(&iomem_resource, res);
548
549		/*
550		 *  We don't know which RAM region contains kernel data,
551		 *  so we try it repeatedly and let the resource manager
552		 *  test it.
553		 */
554		request_resource(res, &code_resource);
555		request_resource(res, &data_resource);
556	}
557}
558
559void __init setup_arch(char **cmdline_p)
560{
561	cpu_probe();
562	prom_init();
563
564#ifdef CONFIG_EARLY_PRINTK
565	{
566		extern void setup_early_printk(void);
567
568		setup_early_printk();
569	}
570#endif
571	cpu_report();
572
573#if defined(CONFIG_VT)
574#if defined(CONFIG_VGA_CONSOLE)
575	conswitchp = &vga_con;
576#elif defined(CONFIG_DUMMY_CONSOLE)
577	conswitchp = &dummy_con;
578#endif
579#endif
580
581	arch_mem_init(cmdline_p);
582
583	resource_init();
584#ifdef CONFIG_SMP
585	plat_smp_setup();
586#endif
587}
588
589static int __init fpu_disable(char *s)
590{
591	int i;
592
593	for (i = 0; i < NR_CPUS; i++)
594		cpu_data[i].options &= ~MIPS_CPU_FPU;
595
596	return 1;
597}
598
599__setup("nofpu", fpu_disable);
600
601static int __init dsp_disable(char *s)
602{
603	cpu_data[0].ases &= ~MIPS_ASE_DSP;
604
605	return 1;
606}
607
608__setup("nodsp", dsp_disable);
609
610unsigned long kernelsp[NR_CPUS];
611unsigned long fw_arg0, fw_arg1, fw_arg2, fw_arg3;
612