setup.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * arch/v850/kernel/setup.c -- Arch-dependent initialization functions
  3. *
  4. * Copyright (C) 2001,02,03,05 NEC Electronics Corporation
  5. * Copyright (C) 2001,02,03,05 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * Written by Miles Bader <miles@gnu.org>
  12. */
  13. #include <linux/mm.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/swap.h> /* we don't have swap, but for nr_free_pages */
  16. #include <linux/irq.h>
  17. #include <linux/reboot.h>
  18. #include <linux/personality.h>
  19. #include <linux/major.h>
  20. #include <linux/root_dev.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/init.h>
  23. #include <asm/irq.h>
  24. #include <asm/setup.h>
  25. #include "mach.h"
  26. /* These symbols are all defined in the linker map to delineate various
  27. statically allocated regions of memory. */
  28. extern char _intv_start, _intv_end;
  29. /* `kram' is only used if the kernel uses part of normal user RAM. */
  30. extern char _kram_start __attribute__ ((__weak__));
  31. extern char _kram_end __attribute__ ((__weak__));
  32. extern char _init_start, _init_end;
  33. extern char _bootmap;
  34. extern char _stext, _etext, _sdata, _edata, _sbss, _ebss;
  35. /* Many platforms use an embedded root image. */
  36. extern char _root_fs_image_start __attribute__ ((__weak__));
  37. extern char _root_fs_image_end __attribute__ ((__weak__));
  38. char command_line[COMMAND_LINE_SIZE];
  39. /* Memory not used by the kernel. */
  40. static unsigned long total_ram_pages;
  41. /* System RAM. */
  42. static unsigned long ram_start = 0, ram_len = 0;
  43. #define ADDR_TO_PAGE_UP(x) ((((unsigned long)x) + PAGE_SIZE-1) >> PAGE_SHIFT)
  44. #define ADDR_TO_PAGE(x) (((unsigned long)x) >> PAGE_SHIFT)
  45. #define PAGE_TO_ADDR(x) (((unsigned long)x) << PAGE_SHIFT)
  46. static void init_mem_alloc (unsigned long ram_start, unsigned long ram_len);
  47. void set_mem_root (void *addr, size_t len, char *cmd_line);
  48. void __init setup_arch (char **cmdline)
  49. {
  50. /* Keep a copy of command line */
  51. *cmdline = command_line;
  52. memcpy (saved_command_line, command_line, COMMAND_LINE_SIZE);
  53. saved_command_line[COMMAND_LINE_SIZE - 1] = '\0';
  54. console_verbose ();
  55. init_mm.start_code = (unsigned long) &_stext;
  56. init_mm.end_code = (unsigned long) &_etext;
  57. init_mm.end_data = (unsigned long) &_edata;
  58. init_mm.brk = (unsigned long) &_kram_end;
  59. /* Find out what mem this machine has. */
  60. mach_get_physical_ram (&ram_start, &ram_len);
  61. /* ... and tell the kernel about it. */
  62. init_mem_alloc (ram_start, ram_len);
  63. printk (KERN_INFO "CPU: %s\nPlatform: %s\n",
  64. CPU_MODEL_LONG, PLATFORM_LONG);
  65. /* do machine-specific setups. */
  66. mach_setup (cmdline);
  67. #ifdef CONFIG_MTD
  68. if (!ROOT_DEV && &_root_fs_image_end > &_root_fs_image_start)
  69. set_mem_root (&_root_fs_image_start,
  70. &_root_fs_image_end - &_root_fs_image_start,
  71. *cmdline);
  72. #endif
  73. }
  74. void __init trap_init (void)
  75. {
  76. }
  77. #ifdef CONFIG_MTD
  78. /* From drivers/mtd/devices/slram.c */
  79. #define SLRAM_BLK_SZ 0x4000
  80. /* Set the root filesystem to be the given memory region.
  81. Some parameter may be appended to CMD_LINE. */
  82. void set_mem_root (void *addr, size_t len, char *cmd_line)
  83. {
  84. /* Some sort of idiocy in MTD means we must supply a length that's
  85. a multiple of SLRAM_BLK_SZ. We just round up the real length,
  86. as the file system shouldn't attempt to access anything beyond
  87. the end of the image anyway. */
  88. len = (((len - 1) + SLRAM_BLK_SZ) / SLRAM_BLK_SZ) * SLRAM_BLK_SZ;
  89. /* The only way to pass info to the MTD slram driver is via
  90. the command line. */
  91. if (*cmd_line) {
  92. cmd_line += strlen (cmd_line);
  93. *cmd_line++ = ' ';
  94. }
  95. sprintf (cmd_line, "slram=root,0x%x,+0x%x", (u32)addr, (u32)len);
  96. ROOT_DEV = MKDEV (MTD_BLOCK_MAJOR, 0);
  97. }
  98. #endif
  99. static void irq_nop (unsigned irq) { }
  100. static unsigned irq_zero (unsigned irq) { return 0; }
  101. static void nmi_end (unsigned irq)
  102. {
  103. if (irq != IRQ_NMI (0)) {
  104. printk (KERN_CRIT "NMI %d is unrecoverable; restarting...",
  105. irq - IRQ_NMI (0));
  106. machine_restart (0);
  107. }
  108. }
  109. static struct hw_interrupt_type nmi_irq_type = {
  110. "NMI",
  111. irq_zero, /* startup */
  112. irq_nop, /* shutdown */
  113. irq_nop, /* enable */
  114. irq_nop, /* disable */
  115. irq_nop, /* ack */
  116. nmi_end, /* end */
  117. };
  118. void __init init_IRQ (void)
  119. {
  120. init_irq_handlers (0, NUM_MACH_IRQS, 1, 0);
  121. init_irq_handlers (IRQ_NMI (0), NUM_NMIS, 1, &nmi_irq_type);
  122. mach_init_irqs ();
  123. }
  124. void __init mem_init (void)
  125. {
  126. max_mapnr = MAP_NR (ram_start + ram_len);
  127. num_physpages = ADDR_TO_PAGE (ram_len);
  128. total_ram_pages = free_all_bootmem ();
  129. printk (KERN_INFO
  130. "Memory: %luK/%luK available"
  131. " (%luK kernel code, %luK data)\n",
  132. PAGE_TO_ADDR (nr_free_pages()) / 1024,
  133. ram_len / 1024,
  134. ((unsigned long)&_etext - (unsigned long)&_stext) / 1024,
  135. ((unsigned long)&_ebss - (unsigned long)&_sdata) / 1024);
  136. }
  137. void free_initmem (void)
  138. {
  139. unsigned long ram_end = ram_start + ram_len;
  140. unsigned long start = PAGE_ALIGN ((unsigned long)(&_init_start));
  141. if (start >= ram_start && start < ram_end) {
  142. unsigned long addr;
  143. unsigned long end = PAGE_ALIGN ((unsigned long)(&_init_end));
  144. if (end > ram_end)
  145. end = ram_end;
  146. printk("Freeing unused kernel memory: %ldK freed\n",
  147. (end - start) / 1024);
  148. for (addr = start; addr < end; addr += PAGE_SIZE) {
  149. struct page *page = virt_to_page (addr);
  150. ClearPageReserved (page);
  151. set_page_count (page, 1);
  152. __free_page (page);
  153. total_ram_pages++;
  154. }
  155. }
  156. }
  157. /* Initialize the `bootmem allocator'. RAM_START and RAM_LEN identify
  158. what RAM may be used. */
  159. static void __init
  160. init_bootmem_alloc (unsigned long ram_start, unsigned long ram_len)
  161. {
  162. /* The part of the kernel that's in the same managed RAM space
  163. used for general allocation. */
  164. unsigned long kram_start = (unsigned long)&_kram_start;
  165. unsigned long kram_end = (unsigned long)&_kram_end;
  166. /* End of the managed RAM space. */
  167. unsigned long ram_end = ram_start + ram_len;
  168. /* Address range of the interrupt vector table. */
  169. unsigned long intv_start = (unsigned long)&_intv_start;
  170. unsigned long intv_end = (unsigned long)&_intv_end;
  171. /* True if the interrupt vectors are in the managed RAM area. */
  172. int intv_in_ram = (intv_end > ram_start && intv_start < ram_end);
  173. /* True if the interrupt vectors are inside the kernel's RAM. */
  174. int intv_in_kram = (intv_end > kram_start && intv_start < kram_end);
  175. /* A pointer to an optional function that reserves platform-specific
  176. memory regions. We declare the pointer `volatile' to avoid gcc
  177. turning the call into a static call (the problem is that since
  178. it's a weak symbol, a static call may end up trying to reference
  179. the location 0x0, which is not always reachable). */
  180. void (*volatile mrb) (void) = mach_reserve_bootmem;
  181. /* The bootmem allocator's allocation bitmap. */
  182. unsigned long bootmap = (unsigned long)&_bootmap;
  183. unsigned long bootmap_len;
  184. /* Round bootmap location up to next page. */
  185. bootmap = PAGE_TO_ADDR (ADDR_TO_PAGE_UP (bootmap));
  186. /* Initialize bootmem allocator. */
  187. bootmap_len = init_bootmem_node (NODE_DATA (0),
  188. ADDR_TO_PAGE (bootmap),
  189. ADDR_TO_PAGE (PAGE_OFFSET),
  190. ADDR_TO_PAGE (ram_end));
  191. /* Now make the RAM actually allocatable (it starts out `reserved'). */
  192. free_bootmem (ram_start, ram_len);
  193. if (kram_end > kram_start)
  194. /* Reserve the RAM part of the kernel's address space, so it
  195. doesn't get allocated. */
  196. reserve_bootmem (kram_start, kram_end - kram_start);
  197. if (intv_in_ram && !intv_in_kram)
  198. /* Reserve the interrupt vector space. */
  199. reserve_bootmem (intv_start, intv_end - intv_start);
  200. if (bootmap >= ram_start && bootmap < ram_end)
  201. /* Reserve the bootmap space. */
  202. reserve_bootmem (bootmap, bootmap_len);
  203. /* Reserve the memory used by the root filesystem image if it's
  204. in RAM. */
  205. if (&_root_fs_image_end > &_root_fs_image_start
  206. && (unsigned long)&_root_fs_image_start >= ram_start
  207. && (unsigned long)&_root_fs_image_start < ram_end)
  208. reserve_bootmem ((unsigned long)&_root_fs_image_start,
  209. &_root_fs_image_end - &_root_fs_image_start);
  210. /* Let the platform-dependent code reserve some too. */
  211. if (mrb)
  212. (*mrb) ();
  213. }
  214. /* Tell the kernel about what RAM it may use for memory allocation. */
  215. static void __init
  216. init_mem_alloc (unsigned long ram_start, unsigned long ram_len)
  217. {
  218. unsigned i;
  219. unsigned long zones_size[MAX_NR_ZONES];
  220. init_bootmem_alloc (ram_start, ram_len);
  221. for (i = 0; i < MAX_NR_ZONES; i++)
  222. zones_size[i] = 0;
  223. /* We stuff all the memory into one area, which includes the
  224. initial gap from PAGE_OFFSET to ram_start. */
  225. zones_size[ZONE_DMA]
  226. = ADDR_TO_PAGE (ram_len + (ram_start - PAGE_OFFSET));
  227. /* The allocator is very picky about the address of the first
  228. allocatable page -- it must be at least as aligned as the
  229. maximum allocation -- so try to detect cases where it will get
  230. confused and signal them at compile time (this is a common
  231. problem when porting to a new platform with ). There is a
  232. similar runtime check in free_area_init_core. */
  233. #if ((PAGE_OFFSET >> PAGE_SHIFT) & ((1UL << (MAX_ORDER - 1)) - 1))
  234. #error MAX_ORDER is too large for given PAGE_OFFSET (use CONFIG_FORCE_MAX_ZONEORDER to change it)
  235. #endif
  236. NODE_DATA(0)->node_mem_map = NULL;
  237. free_area_init_node (0, NODE_DATA(0), zones_size,
  238. ADDR_TO_PAGE (PAGE_OFFSET), 0);
  239. }
  240. /* Taken from m68knommu */
  241. void show_mem(void)
  242. {
  243. unsigned long i;
  244. int free = 0, total = 0, reserved = 0, shared = 0;
  245. int cached = 0;
  246. printk(KERN_INFO "\nMem-info:\n");
  247. show_free_areas();
  248. i = max_mapnr;
  249. while (i-- > 0) {
  250. total++;
  251. if (PageReserved(mem_map+i))
  252. reserved++;
  253. else if (PageSwapCache(mem_map+i))
  254. cached++;
  255. else if (!page_count(mem_map+i))
  256. free++;
  257. else
  258. shared += page_count(mem_map+i) - 1;
  259. }
  260. printk(KERN_INFO "%d pages of RAM\n",total);
  261. printk(KERN_INFO "%d free pages\n",free);
  262. printk(KERN_INFO "%d reserved pages\n",reserved);
  263. printk(KERN_INFO "%d pages shared\n",shared);
  264. printk(KERN_INFO "%d pages swap cached\n",cached);
  265. }