init.c 9.4 KB

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