init.c 9.5 KB

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