init.c 9.2 KB

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