init.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (C) 2007-2008 Michal Simek <monstr@monstr.eu>
  3. * Copyright (C) 2006 Atmark Techno, Inc.
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. */
  9. #include <linux/bootmem.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/memblock.h>
  13. #include <linux/mm.h> /* mem_init */
  14. #include <linux/initrd.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/pfn.h>
  17. #include <linux/slab.h>
  18. #include <linux/swap.h>
  19. #include <linux/export.h>
  20. #include <asm/page.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/sections.h>
  24. #include <asm/tlb.h>
  25. #include <asm/fixmap.h>
  26. /* Use for MMU and noMMU because of PCI generic code */
  27. int mem_init_done;
  28. #ifndef CONFIG_MMU
  29. unsigned int __page_offset;
  30. EXPORT_SYMBOL(__page_offset);
  31. #else
  32. static int init_bootmem_done;
  33. #endif /* CONFIG_MMU */
  34. char *klimit = _end;
  35. /*
  36. * Initialize the bootmem system and give it all the memory we
  37. * have available.
  38. */
  39. unsigned long memory_start;
  40. EXPORT_SYMBOL(memory_start);
  41. unsigned long memory_size;
  42. EXPORT_SYMBOL(memory_size);
  43. unsigned long lowmem_size;
  44. /*
  45. * paging_init() sets up the page tables - in fact we've already done this.
  46. */
  47. static void __init paging_init(void)
  48. {
  49. unsigned long zones_size[MAX_NR_ZONES];
  50. #ifdef CONFIG_MMU
  51. int idx;
  52. /* Setup fixmaps */
  53. for (idx = 0; idx < __end_of_fixed_addresses; idx++)
  54. clear_fixmap(idx);
  55. #endif
  56. /* Clean every zones */
  57. memset(zones_size, 0, sizeof(zones_size));
  58. zones_size[ZONE_DMA] = max_pfn;
  59. free_area_init(zones_size);
  60. }
  61. void __init setup_memory(void)
  62. {
  63. unsigned long map_size;
  64. struct memblock_region *reg;
  65. #ifndef CONFIG_MMU
  66. u32 kernel_align_start, kernel_align_size;
  67. /* Find main memory where is the kernel */
  68. for_each_memblock(memory, reg) {
  69. memory_start = (u32)reg->base;
  70. lowmem_size = reg->size;
  71. if ((memory_start <= (u32)_text) &&
  72. ((u32)_text <= (memory_start + lowmem_size - 1))) {
  73. memory_size = lowmem_size;
  74. PAGE_OFFSET = memory_start;
  75. printk(KERN_INFO "%s: Main mem: 0x%x, "
  76. "size 0x%08x\n", __func__, (u32) memory_start,
  77. (u32) memory_size);
  78. break;
  79. }
  80. }
  81. if (!memory_start || !memory_size) {
  82. panic("%s: Missing memory setting 0x%08x, size=0x%08x\n",
  83. __func__, (u32) memory_start, (u32) memory_size);
  84. }
  85. /* reservation of region where is the kernel */
  86. kernel_align_start = PAGE_DOWN((u32)_text);
  87. /* ALIGN can be remove because _end in vmlinux.lds.S is align */
  88. kernel_align_size = PAGE_UP((u32)klimit) - kernel_align_start;
  89. printk(KERN_INFO "%s: kernel addr:0x%08x-0x%08x size=0x%08x\n",
  90. __func__, kernel_align_start, kernel_align_start
  91. + kernel_align_size, kernel_align_size);
  92. memblock_reserve(kernel_align_start, kernel_align_size);
  93. #endif
  94. /*
  95. * Kernel:
  96. * start: base phys address of kernel - page align
  97. * end: base phys address of kernel - page align
  98. *
  99. * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
  100. * max_low_pfn
  101. * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn)
  102. * num_physpages - number of all pages
  103. */
  104. /* memory start is from the kernel end (aligned) to higher addr */
  105. min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */
  106. /* RAM is assumed contiguous */
  107. num_physpages = max_mapnr = memory_size >> PAGE_SHIFT;
  108. max_low_pfn = ((u64)memory_start + (u64)lowmem_size) >> PAGE_SHIFT;
  109. max_pfn = ((u64)memory_start + (u64)memory_size) >> PAGE_SHIFT;
  110. printk(KERN_INFO "%s: max_mapnr: %#lx\n", __func__, max_mapnr);
  111. printk(KERN_INFO "%s: min_low_pfn: %#lx\n", __func__, min_low_pfn);
  112. printk(KERN_INFO "%s: max_low_pfn: %#lx\n", __func__, max_low_pfn);
  113. printk(KERN_INFO "%s: max_pfn: %#lx\n", __func__, max_pfn);
  114. /*
  115. * Find an area to use for the bootmem bitmap.
  116. * We look for the first area which is at least
  117. * 128kB in length (128kB is enough for a bitmap
  118. * for 4GB of memory, using 4kB pages), plus 1 page
  119. * (in case the address isn't page-aligned).
  120. */
  121. map_size = init_bootmem_node(NODE_DATA(0),
  122. PFN_UP(TOPHYS((u32)klimit)), min_low_pfn, max_low_pfn);
  123. memblock_reserve(PFN_UP(TOPHYS((u32)klimit)) << PAGE_SHIFT, map_size);
  124. /* free bootmem is whole main memory */
  125. free_bootmem(memory_start, lowmem_size);
  126. /* reserve allocate blocks */
  127. for_each_memblock(reserved, reg) {
  128. unsigned long top = reg->base + reg->size - 1;
  129. pr_debug("reserved - 0x%08x-0x%08x, %lx, %lx\n",
  130. (u32) reg->base, (u32) reg->size, top,
  131. memory_start + lowmem_size - 1);
  132. if (top <= (memory_start + lowmem_size - 1)) {
  133. reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
  134. } else if (reg->base < (memory_start + lowmem_size - 1)) {
  135. unsigned long trunc_size = memory_start + lowmem_size -
  136. reg->base;
  137. reserve_bootmem(reg->base, trunc_size, BOOTMEM_DEFAULT);
  138. }
  139. }
  140. #ifdef CONFIG_MMU
  141. init_bootmem_done = 1;
  142. #endif
  143. paging_init();
  144. }
  145. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  146. {
  147. unsigned long addr;
  148. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  149. ClearPageReserved(virt_to_page(addr));
  150. init_page_count(virt_to_page(addr));
  151. free_page(addr);
  152. totalram_pages++;
  153. }
  154. printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
  155. }
  156. #ifdef CONFIG_BLK_DEV_INITRD
  157. void free_initrd_mem(unsigned long start, unsigned long end)
  158. {
  159. int pages = 0;
  160. for (; start < end; start += PAGE_SIZE) {
  161. ClearPageReserved(virt_to_page(start));
  162. init_page_count(virt_to_page(start));
  163. free_page(start);
  164. totalram_pages++;
  165. pages++;
  166. }
  167. printk(KERN_NOTICE "Freeing initrd memory: %dk freed\n",
  168. (int)(pages * (PAGE_SIZE / 1024)));
  169. }
  170. #endif
  171. void free_initmem(void)
  172. {
  173. free_init_pages("unused kernel memory",
  174. (unsigned long)(&__init_begin),
  175. (unsigned long)(&__init_end));
  176. }
  177. void __init mem_init(void)
  178. {
  179. high_memory = (void *)__va(memory_start + lowmem_size - 1);
  180. /* this will put all memory onto the freelists */
  181. totalram_pages += free_all_bootmem();
  182. printk(KERN_INFO "Memory: %luk/%luk available\n",
  183. nr_free_pages() << (PAGE_SHIFT-10),
  184. num_physpages << (PAGE_SHIFT-10));
  185. mem_init_done = 1;
  186. }
  187. #ifndef CONFIG_MMU
  188. int page_is_ram(unsigned long pfn)
  189. {
  190. return __range_ok(pfn, 0);
  191. }
  192. #else
  193. int page_is_ram(unsigned long pfn)
  194. {
  195. return pfn < max_low_pfn;
  196. }
  197. /*
  198. * Check for command-line options that affect what MMU_init will do.
  199. */
  200. static void mm_cmdline_setup(void)
  201. {
  202. unsigned long maxmem = 0;
  203. char *p = cmd_line;
  204. /* Look for mem= option on command line */
  205. p = strstr(cmd_line, "mem=");
  206. if (p) {
  207. p += 4;
  208. maxmem = memparse(p, &p);
  209. if (maxmem && memory_size > maxmem) {
  210. memory_size = maxmem;
  211. memblock.memory.regions[0].size = memory_size;
  212. }
  213. }
  214. }
  215. /*
  216. * MMU_init_hw does the chip-specific initialization of the MMU hardware.
  217. */
  218. static void __init mmu_init_hw(void)
  219. {
  220. /*
  221. * The Zone Protection Register (ZPR) defines how protection will
  222. * be applied to every page which is a member of a given zone. At
  223. * present, we utilize only two of the zones.
  224. * The zone index bits (of ZSEL) in the PTE are used for software
  225. * indicators, except the LSB. For user access, zone 1 is used,
  226. * for kernel access, zone 0 is used. We set all but zone 1
  227. * to zero, allowing only kernel access as indicated in the PTE.
  228. * For zone 1, we set a 01 binary (a value of 10 will not work)
  229. * to allow user access as indicated in the PTE. This also allows
  230. * kernel access as indicated in the PTE.
  231. */
  232. __asm__ __volatile__ ("ori r11, r0, 0x10000000;" \
  233. "mts rzpr, r11;"
  234. : : : "r11");
  235. }
  236. /*
  237. * MMU_init sets up the basic memory mappings for the kernel,
  238. * including both RAM and possibly some I/O regions,
  239. * and sets up the page tables and the MMU hardware ready to go.
  240. */
  241. /* called from head.S */
  242. asmlinkage void __init mmu_init(void)
  243. {
  244. unsigned int kstart, ksize;
  245. if (!memblock.reserved.cnt) {
  246. printk(KERN_EMERG "Error memory count\n");
  247. machine_restart(NULL);
  248. }
  249. if ((u32) memblock.memory.regions[0].size < 0x1000000) {
  250. printk(KERN_EMERG "Memory must be greater than 16MB\n");
  251. machine_restart(NULL);
  252. }
  253. /* Find main memory where the kernel is */
  254. memory_start = (u32) memblock.memory.regions[0].base;
  255. lowmem_size = memory_size = (u32) memblock.memory.regions[0].size;
  256. if (lowmem_size > CONFIG_LOWMEM_SIZE) {
  257. lowmem_size = CONFIG_LOWMEM_SIZE;
  258. memory_size = lowmem_size;
  259. }
  260. mm_cmdline_setup(); /* FIXME parse args from command line - not used */
  261. /*
  262. * Map out the kernel text/data/bss from the available physical
  263. * memory.
  264. */
  265. kstart = __pa(CONFIG_KERNEL_START); /* kernel start */
  266. /* kernel size */
  267. ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START));
  268. memblock_reserve(kstart, ksize);
  269. #if defined(CONFIG_BLK_DEV_INITRD)
  270. /* Remove the init RAM disk from the available memory. */
  271. /* if (initrd_start) {
  272. mem_pieces_remove(&phys_avail, __pa(initrd_start),
  273. initrd_end - initrd_start, 1);
  274. }*/
  275. #endif /* CONFIG_BLK_DEV_INITRD */
  276. /* Initialize the MMU hardware */
  277. mmu_init_hw();
  278. /* Map in all of RAM starting at CONFIG_KERNEL_START */
  279. mapin_ram();
  280. /* Extend vmalloc and ioremap area as big as possible */
  281. ioremap_base = ioremap_bot = FIXADDR_START;
  282. /* Initialize the context management stuff */
  283. mmu_context_init();
  284. /* Shortly after that, the entire linear mapping will be available */
  285. /* This will also cause that unflatten device tree will be allocated
  286. * inside 768MB limit */
  287. memblock_set_current_limit(memory_start + lowmem_size - 1);
  288. }
  289. /* This is only called until mem_init is done. */
  290. void __init *early_get_page(void)
  291. {
  292. void *p;
  293. if (init_bootmem_done) {
  294. p = alloc_bootmem_pages(PAGE_SIZE);
  295. } else {
  296. /*
  297. * Mem start + 32MB -> here is limit
  298. * because of mem mapping from head.S
  299. */
  300. p = __va(memblock_alloc_base(PAGE_SIZE, PAGE_SIZE,
  301. memory_start + 0x2000000));
  302. }
  303. return p;
  304. }
  305. #endif /* CONFIG_MMU */
  306. void * __init_refok alloc_maybe_bootmem(size_t size, gfp_t mask)
  307. {
  308. if (mem_init_done)
  309. return kmalloc(size, mask);
  310. else
  311. return alloc_bootmem(size);
  312. }
  313. void * __init_refok zalloc_maybe_bootmem(size_t size, gfp_t mask)
  314. {
  315. void *p;
  316. if (mem_init_done)
  317. p = kzalloc(size, mask);
  318. else {
  319. p = alloc_bootmem(size);
  320. if (p)
  321. memset(p, 0, size);
  322. }
  323. return p;
  324. }