init.c 9.4 KB

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