Introduce flags for reserve_bootmem()
[deliverable/linux.git] / arch / avr32 / kernel / setup.c
CommitLineData
5f97f7f9
HS
1/*
2 * Copyright (C) 2004-2006 Atmel Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/clk.h>
10#include <linux/init.h>
5539f59a 11#include <linux/initrd.h>
5f97f7f9
HS
12#include <linux/sched.h>
13#include <linux/console.h>
14#include <linux/ioport.h>
15#include <linux/bootmem.h>
16#include <linux/fs.h>
17#include <linux/module.h>
5539f59a 18#include <linux/pfn.h>
5f97f7f9
HS
19#include <linux/root_dev.h>
20#include <linux/cpu.h>
10b50b7d 21#include <linux/kernel.h>
5f97f7f9
HS
22
23#include <asm/sections.h>
24#include <asm/processor.h>
25#include <asm/pgtable.h>
26#include <asm/setup.h>
27#include <asm/sysreg.h>
28
29#include <asm/arch/board.h>
30#include <asm/arch/init.h>
31
32extern int root_mountflags;
33
5f97f7f9
HS
34/*
35 * Initialize loops_per_jiffy as 5000000 (500MIPS).
36 * Better make it too large than too small...
37 */
38struct avr32_cpuinfo boot_cpu_data = {
39 .loops_per_jiffy = 5000000
40};
41EXPORT_SYMBOL(boot_cpu_data);
42
bf4352c0 43static char __initdata command_line[COMMAND_LINE_SIZE];
5f97f7f9
HS
44
45/*
d8011768 46 * Standard memory resources
5f97f7f9 47 */
d8011768
HS
48static struct resource __initdata kernel_data = {
49 .name = "Kernel data",
50 .start = 0,
51 .end = 0,
52 .flags = IORESOURCE_MEM,
53};
54static struct resource __initdata kernel_code = {
55 .name = "Kernel code",
56 .start = 0,
57 .end = 0,
58 .flags = IORESOURCE_MEM,
59 .sibling = &kernel_data,
60};
5f97f7f9
HS
61
62/*
d8011768
HS
63 * Available system RAM and reserved regions as singly linked
64 * lists. These lists are traversed using the sibling pointer in
65 * struct resource and are kept sorted at all times.
5f97f7f9 66 */
d8011768
HS
67static struct resource *__initdata system_ram;
68static struct resource *__initdata reserved = &kernel_code;
69
70/*
71 * We need to allocate these before the bootmem allocator is up and
72 * running, so we need this "cache". 32 entries are probably enough
73 * for all but the most insanely complex systems.
74 */
75static struct resource __initdata res_cache[32];
76static unsigned int __initdata res_cache_next_free;
77
78static void __init resource_init(void)
79{
80 struct resource *mem, *res;
81 struct resource *new;
82
83 kernel_code.start = __pa(init_mm.start_code);
84
85 for (mem = system_ram; mem; mem = mem->sibling) {
86 new = alloc_bootmem_low(sizeof(struct resource));
87 memcpy(new, mem, sizeof(struct resource));
88
89 new->sibling = NULL;
90 if (request_resource(&iomem_resource, new))
91 printk(KERN_WARNING "Bad RAM resource %08x-%08x\n",
92 mem->start, mem->end);
93 }
94
95 for (res = reserved; res; res = res->sibling) {
96 new = alloc_bootmem_low(sizeof(struct resource));
97 memcpy(new, res, sizeof(struct resource));
98
99 new->sibling = NULL;
100 if (insert_resource(&iomem_resource, new))
101 printk(KERN_WARNING
102 "Bad reserved resource %s (%08x-%08x)\n",
103 res->name, res->start, res->end);
104 }
105}
106
107static void __init
108add_physical_memory(resource_size_t start, resource_size_t end)
109{
110 struct resource *new, *next, **pprev;
111
112 for (pprev = &system_ram, next = system_ram; next;
113 pprev = &next->sibling, next = next->sibling) {
114 if (end < next->start)
115 break;
116 if (start <= next->end) {
117 printk(KERN_WARNING
118 "Warning: Physical memory map is broken\n");
119 printk(KERN_WARNING
120 "Warning: %08x-%08x overlaps %08x-%08x\n",
121 start, end, next->start, next->end);
122 return;
123 }
124 }
125
126 if (res_cache_next_free >= ARRAY_SIZE(res_cache)) {
127 printk(KERN_WARNING
128 "Warning: Failed to add physical memory %08x-%08x\n",
129 start, end);
130 return;
131 }
132
133 new = &res_cache[res_cache_next_free++];
134 new->start = start;
135 new->end = end;
136 new->name = "System RAM";
137 new->flags = IORESOURCE_MEM;
138
139 *pprev = new;
140}
141
142static int __init
143add_reserved_region(resource_size_t start, resource_size_t end,
144 const char *name)
145{
146 struct resource *new, *next, **pprev;
5f97f7f9 147
d8011768
HS
148 if (end < start)
149 return -EINVAL;
150
151 if (res_cache_next_free >= ARRAY_SIZE(res_cache))
152 return -ENOMEM;
153
154 for (pprev = &reserved, next = reserved; next;
155 pprev = &next->sibling, next = next->sibling) {
156 if (end < next->start)
157 break;
158 if (start <= next->end)
159 return -EBUSY;
160 }
161
162 new = &res_cache[res_cache_next_free++];
163 new->start = start;
164 new->end = end;
165 new->name = name;
166 new->flags = IORESOURCE_MEM;
167
168 *pprev = new;
169
170 return 0;
171}
172
173static unsigned long __init
174find_free_region(const struct resource *mem, resource_size_t size,
175 resource_size_t align)
176{
177 struct resource *res;
178 unsigned long target;
179
180 target = ALIGN(mem->start, align);
181 for (res = reserved; res; res = res->sibling) {
182 if ((target + size) <= res->start)
183 break;
184 if (target <= res->end)
185 target = ALIGN(res->end + 1, align);
186 }
187
188 if ((target + size) > (mem->end + 1))
189 return mem->end + 1;
190
191 return target;
192}
5f97f7f9 193
f9692b95
HS
194static int __init
195alloc_reserved_region(resource_size_t *start, resource_size_t size,
196 resource_size_t align, const char *name)
197{
198 struct resource *mem;
199 resource_size_t target;
200 int ret;
201
202 for (mem = system_ram; mem; mem = mem->sibling) {
203 target = find_free_region(mem, size, align);
204 if (target <= mem->end) {
205 ret = add_reserved_region(target, target + size - 1,
206 name);
207 if (!ret)
208 *start = target;
209 return ret;
210 }
211 }
212
213 return -ENOMEM;
214}
215
5f97f7f9
HS
216/*
217 * Early framebuffer allocation. Works as follows:
218 * - If fbmem_size is zero, nothing will be allocated or reserved.
219 * - If fbmem_start is zero when setup_bootmem() is called,
f9692b95
HS
220 * a block of fbmem_size bytes will be reserved before bootmem
221 * initialization. It will be aligned to the largest page size
222 * that fbmem_size is a multiple of.
5f97f7f9 223 * - If fbmem_start is nonzero, an area of size fbmem_size will be
f9692b95
HS
224 * reserved at the physical address fbmem_start if possible. If
225 * it collides with other reserved memory, a different block of
226 * same size will be allocated, just as if fbmem_start was zero.
5f97f7f9
HS
227 *
228 * Board-specific code may use these variables to set up platform data
229 * for the framebuffer driver if fbmem_size is nonzero.
230 */
d80e2bb1
HS
231resource_size_t __initdata fbmem_start;
232resource_size_t __initdata fbmem_size;
5f97f7f9
HS
233
234/*
235 * "fbmem=xxx[kKmM]" allocates the specified amount of boot memory for
236 * use as framebuffer.
237 *
238 * "fbmem=xxx[kKmM]@yyy[kKmM]" defines a memory region of size xxx and
239 * starting at yyy to be reserved for use as framebuffer.
240 *
241 * The kernel won't verify that the memory region starting at yyy
242 * actually contains usable RAM.
243 */
244static int __init early_parse_fbmem(char *p)
245{
f9692b95
HS
246 int ret;
247 unsigned long align;
248
5f97f7f9 249 fbmem_size = memparse(p, &p);
f9692b95 250 if (*p == '@') {
fe57f84e 251 fbmem_start = memparse(p + 1, &p);
f9692b95
HS
252 ret = add_reserved_region(fbmem_start,
253 fbmem_start + fbmem_size - 1,
254 "Framebuffer");
255 if (ret) {
256 printk(KERN_WARNING
257 "Failed to reserve framebuffer memory\n");
258 fbmem_start = 0;
259 }
260 }
261
262 if (!fbmem_start) {
263 if ((fbmem_size & 0x000fffffUL) == 0)
264 align = 0x100000; /* 1 MiB */
265 else if ((fbmem_size & 0x0000ffffUL) == 0)
266 align = 0x10000; /* 64 KiB */
267 else
268 align = 0x1000; /* 4 KiB */
269
270 ret = alloc_reserved_region(&fbmem_start, fbmem_size,
271 align, "Framebuffer");
272 if (ret) {
273 printk(KERN_WARNING
274 "Failed to allocate framebuffer memory\n");
275 fbmem_size = 0;
276 }
277 }
278
5f97f7f9
HS
279 return 0;
280}
281early_param("fbmem", early_parse_fbmem);
282
5f97f7f9
HS
283static int __init parse_tag_core(struct tag *tag)
284{
285 if (tag->hdr.size > 2) {
286 if ((tag->u.core.flags & 1) == 0)
287 root_mountflags &= ~MS_RDONLY;
288 ROOT_DEV = new_decode_dev(tag->u.core.rootdev);
289 }
290 return 0;
291}
292__tagtable(ATAG_CORE, parse_tag_core);
293
d8011768 294static int __init parse_tag_mem(struct tag *tag)
5f97f7f9 295{
d8011768 296 unsigned long start, end;
5f97f7f9
HS
297
298 /*
299 * Ignore zero-sized entries. If we're running standalone, the
300 * SDRAM code may emit such entries if something goes
301 * wrong...
302 */
303 if (tag->u.mem_range.size == 0)
304 return 0;
305
d8011768
HS
306 start = tag->u.mem_range.addr;
307 end = tag->u.mem_range.addr + tag->u.mem_range.size - 1;
5f97f7f9 308
d8011768
HS
309 add_physical_memory(start, end);
310 return 0;
311}
312__tagtable(ATAG_MEM, parse_tag_mem);
5f97f7f9 313
d8011768
HS
314static int __init parse_tag_rdimg(struct tag *tag)
315{
f3e26984 316#ifdef CONFIG_BLK_DEV_INITRD
d8011768
HS
317 struct tag_mem_range *mem = &tag->u.mem_range;
318 int ret;
319
320 if (initrd_start) {
321 printk(KERN_WARNING
322 "Warning: Only the first initrd image will be used\n");
323 return 0;
5f97f7f9
HS
324 }
325
aa15f637 326 ret = add_reserved_region(mem->addr, mem->addr + mem->size - 1,
d8011768
HS
327 "initrd");
328 if (ret) {
329 printk(KERN_WARNING
330 "Warning: Failed to reserve initrd memory\n");
331 return ret;
332 }
333
334 initrd_start = (unsigned long)__va(mem->addr);
335 initrd_end = initrd_start + mem->size;
336#else
337 printk(KERN_WARNING "RAM disk image present, but "
338 "no initrd support in kernel, ignoring\n");
339#endif
5f97f7f9
HS
340
341 return 0;
342}
d8011768 343__tagtable(ATAG_RDIMG, parse_tag_rdimg);
5f97f7f9 344
d8011768 345static int __init parse_tag_rsvd_mem(struct tag *tag)
5f97f7f9 346{
d8011768
HS
347 struct tag_mem_range *mem = &tag->u.mem_range;
348
349 return add_reserved_region(mem->addr, mem->addr + mem->size - 1,
350 "Reserved");
5f97f7f9 351}
d8011768 352__tagtable(ATAG_RSVD_MEM, parse_tag_rsvd_mem);
5f97f7f9
HS
353
354static int __init parse_tag_cmdline(struct tag *tag)
355{
bf4352c0 356 strlcpy(boot_command_line, tag->u.cmdline.cmdline, COMMAND_LINE_SIZE);
5f97f7f9
HS
357 return 0;
358}
359__tagtable(ATAG_CMDLINE, parse_tag_cmdline);
360
5f97f7f9
HS
361static int __init parse_tag_clock(struct tag *tag)
362{
363 /*
364 * We'll figure out the clocks by peeking at the system
365 * manager regs directly.
366 */
367 return 0;
368}
369__tagtable(ATAG_CLOCK, parse_tag_clock);
370
5f97f7f9
HS
371/*
372 * Scan the tag table for this tag, and call its parse function. The
373 * tag table is built by the linker from all the __tagtable
374 * declarations.
375 */
376static int __init parse_tag(struct tag *tag)
377{
378 extern struct tagtable __tagtable_begin, __tagtable_end;
379 struct tagtable *t;
380
381 for (t = &__tagtable_begin; t < &__tagtable_end; t++)
382 if (tag->hdr.tag == t->tag) {
383 t->parse(tag);
384 break;
385 }
386
387 return t < &__tagtable_end;
388}
389
390/*
391 * Parse all tags in the list we got from the boot loader
392 */
393static void __init parse_tags(struct tag *t)
394{
395 for (; t->hdr.tag != ATAG_NONE; t = tag_next(t))
396 if (!parse_tag(t))
397 printk(KERN_WARNING
398 "Ignoring unrecognised tag 0x%08x\n",
399 t->hdr.tag);
400}
401
5539f59a
HS
402/*
403 * Find a free memory region large enough for storing the
404 * bootmem bitmap.
405 */
406static unsigned long __init
d8011768 407find_bootmap_pfn(const struct resource *mem)
5539f59a
HS
408{
409 unsigned long bootmap_pages, bootmap_len;
d8011768
HS
410 unsigned long node_pages = PFN_UP(mem->end - mem->start + 1);
411 unsigned long bootmap_start;
5539f59a
HS
412
413 bootmap_pages = bootmem_bootmap_pages(node_pages);
414 bootmap_len = bootmap_pages << PAGE_SHIFT;
415
416 /*
417 * Find a large enough region without reserved pages for
418 * storing the bootmem bitmap. We can take advantage of the
419 * fact that all lists have been sorted.
420 *
d8011768
HS
421 * We have to check that we don't collide with any reserved
422 * regions, which includes the kernel image and any RAMDISK
423 * images.
5539f59a 424 */
d8011768 425 bootmap_start = find_free_region(mem, bootmap_len, PAGE_SIZE);
5539f59a 426
d8011768 427 return bootmap_start >> PAGE_SHIFT;
5539f59a
HS
428}
429
d8011768
HS
430#define MAX_LOWMEM HIGHMEM_START
431#define MAX_LOWMEM_PFN PFN_DOWN(MAX_LOWMEM)
432
5539f59a
HS
433static void __init setup_bootmem(void)
434{
435 unsigned bootmap_size;
436 unsigned long first_pfn, bootmap_pfn, pages;
437 unsigned long max_pfn, max_low_pfn;
5539f59a 438 unsigned node = 0;
d8011768 439 struct resource *res;
5539f59a 440
d8011768
HS
441 printk(KERN_INFO "Physical memory:\n");
442 for (res = system_ram; res; res = res->sibling)
443 printk(" %08x-%08x\n", res->start, res->end);
444 printk(KERN_INFO "Reserved memory:\n");
445 for (res = reserved; res; res = res->sibling)
446 printk(" %08x-%08x: %s\n",
447 res->start, res->end, res->name);
5539f59a
HS
448
449 nodes_clear(node_online_map);
450
d8011768 451 if (system_ram->sibling)
5539f59a
HS
452 printk(KERN_WARNING "Only using first memory bank\n");
453
d8011768
HS
454 for (res = system_ram; res; res = NULL) {
455 first_pfn = PFN_UP(res->start);
456 max_low_pfn = max_pfn = PFN_DOWN(res->end + 1);
457 bootmap_pfn = find_bootmap_pfn(res);
5539f59a
HS
458 if (bootmap_pfn > max_pfn)
459 panic("No space for bootmem bitmap!\n");
460
461 if (max_low_pfn > MAX_LOWMEM_PFN) {
462 max_low_pfn = MAX_LOWMEM_PFN;
463#ifndef CONFIG_HIGHMEM
464 /*
465 * Lowmem is memory that can be addressed
466 * directly through P1/P2
467 */
468 printk(KERN_WARNING
469 "Node %u: Only %ld MiB of memory will be used.\n",
470 node, MAX_LOWMEM >> 20);
471 printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
472#else
473#error HIGHMEM is not supported by AVR32 yet
474#endif
475 }
476
477 /* Initialize the boot-time allocator with low memory only. */
478 bootmap_size = init_bootmem_node(NODE_DATA(node), bootmap_pfn,
479 first_pfn, max_low_pfn);
480
5539f59a
HS
481 /*
482 * Register fully available RAM pages with the bootmem
483 * allocator.
484 */
485 pages = max_low_pfn - first_pfn;
486 free_bootmem_node (NODE_DATA(node), PFN_PHYS(first_pfn),
487 PFN_PHYS(pages));
488
d8011768 489 /* Reserve space for the bootmem bitmap... */
5539f59a
HS
490 reserve_bootmem_node(NODE_DATA(node),
491 PFN_PHYS(bootmap_pfn),
72a7fe39
BW
492 bootmap_size,
493 BOOTMEM_DEFAULT);
5539f59a 494
5539f59a 495 /* ...and any other reserved regions. */
d8011768
HS
496 for (res = reserved; res; res = res->sibling) {
497 if (res->start > PFN_PHYS(max_pfn))
5539f59a
HS
498 break;
499
d8011768
HS
500 /*
501 * resource_init will complain about partial
502 * overlaps, so we'll just ignore such
503 * resources for now.
504 */
505 if (res->start >= PFN_PHYS(first_pfn)
506 && res->end < PFN_PHYS(max_pfn))
507 reserve_bootmem_node(
508 NODE_DATA(node), res->start,
72a7fe39
BW
509 res->end - res->start + 1,
510 BOOTMEM_DEFAULT);
5539f59a
HS
511 }
512
513 node_set_online(node);
514 }
515}
516
5f97f7f9
HS
517void __init setup_arch (char **cmdline_p)
518{
519 struct clk *cpu_clk;
520
d8011768
HS
521 init_mm.start_code = (unsigned long)_text;
522 init_mm.end_code = (unsigned long)_etext;
523 init_mm.end_data = (unsigned long)_edata;
524 init_mm.brk = (unsigned long)_end;
525
526 /*
527 * Include .init section to make allocations easier. It will
528 * be removed before the resource is actually requested.
529 */
530 kernel_code.start = __pa(__init_begin);
531 kernel_code.end = __pa(init_mm.end_code - 1);
532 kernel_data.start = __pa(init_mm.end_code);
533 kernel_data.end = __pa(init_mm.brk - 1);
534
5f97f7f9
HS
535 parse_tags(bootloader_tags);
536
537 setup_processor();
538 setup_platform();
c194588d 539 setup_board();
5f97f7f9
HS
540
541 cpu_clk = clk_get(NULL, "cpu");
542 if (IS_ERR(cpu_clk)) {
543 printk(KERN_WARNING "Warning: Unable to get CPU clock\n");
544 } else {
545 unsigned long cpu_hz = clk_get_rate(cpu_clk);
546
547 /*
548 * Well, duh, but it's probably a good idea to
549 * increment the use count.
550 */
551 clk_enable(cpu_clk);
552
553 boot_cpu_data.clk = cpu_clk;
554 boot_cpu_data.loops_per_jiffy = cpu_hz * 4;
555 printk("CPU: Running at %lu.%03lu MHz\n",
556 ((cpu_hz + 500) / 1000) / 1000,
557 ((cpu_hz + 500) / 1000) % 1000);
558 }
559
bf4352c0 560 strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
5f97f7f9
HS
561 *cmdline_p = command_line;
562 parse_early_param();
563
564 setup_bootmem();
565
5f97f7f9
HS
566#ifdef CONFIG_VT
567 conswitchp = &dummy_con;
568#endif
569
570 paging_init();
5f97f7f9
HS
571 resource_init();
572}
This page took 0.199493 seconds and 5 git commands to generate.