init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. #ifdef CONFIG_HIGHMEM
  45. pte_t *kmap_pte;
  46. EXPORT_SYMBOL(kmap_pte);
  47. pgprot_t kmap_prot;
  48. EXPORT_SYMBOL(kmap_prot);
  49. static inline pte_t *virt_to_kpte(unsigned long vaddr)
  50. {
  51. return pte_offset_kernel(pmd_offset(pgd_offset_k(vaddr),
  52. vaddr), vaddr);
  53. }
  54. static void __init highmem_init(void)
  55. {
  56. pr_debug("%x\n", (u32)PKMAP_BASE);
  57. map_page(PKMAP_BASE, 0, 0); /* XXX gross */
  58. pkmap_page_table = virt_to_kpte(PKMAP_BASE);
  59. kmap_pte = virt_to_kpte(__fix_to_virt(FIX_KMAP_BEGIN));
  60. kmap_prot = PAGE_KERNEL;
  61. }
  62. static unsigned long highmem_setup(void)
  63. {
  64. unsigned long pfn;
  65. unsigned long reservedpages = 0;
  66. for (pfn = max_low_pfn; pfn < max_pfn; ++pfn) {
  67. struct page *page = pfn_to_page(pfn);
  68. /* FIXME not sure about */
  69. if (memblock_is_reserved(pfn << PAGE_SHIFT))
  70. continue;
  71. ClearPageReserved(page);
  72. init_page_count(page);
  73. __free_page(page);
  74. totalhigh_pages++;
  75. reservedpages++;
  76. }
  77. totalram_pages += totalhigh_pages;
  78. printk(KERN_INFO "High memory: %luk\n",
  79. totalhigh_pages << (PAGE_SHIFT-10));
  80. return reservedpages;
  81. }
  82. #endif /* CONFIG_HIGHMEM */
  83. /*
  84. * paging_init() sets up the page tables - in fact we've already done this.
  85. */
  86. static void __init paging_init(void)
  87. {
  88. unsigned long zones_size[MAX_NR_ZONES];
  89. #ifdef CONFIG_MMU
  90. int idx;
  91. /* Setup fixmaps */
  92. for (idx = 0; idx < __end_of_fixed_addresses; idx++)
  93. clear_fixmap(idx);
  94. #endif
  95. /* Clean every zones */
  96. memset(zones_size, 0, sizeof(zones_size));
  97. #ifdef CONFIG_HIGHMEM
  98. highmem_init();
  99. zones_size[ZONE_DMA] = max_low_pfn;
  100. zones_size[ZONE_HIGHMEM] = max_pfn;
  101. #else
  102. zones_size[ZONE_DMA] = max_pfn;
  103. #endif
  104. /* We don't have holes in memory map */
  105. free_area_init_nodes(zones_size);
  106. }
  107. void __init setup_memory(void)
  108. {
  109. unsigned long map_size;
  110. struct memblock_region *reg;
  111. #ifndef CONFIG_MMU
  112. u32 kernel_align_start, kernel_align_size;
  113. /* Find main memory where is the kernel */
  114. for_each_memblock(memory, reg) {
  115. memory_start = (u32)reg->base;
  116. lowmem_size = reg->size;
  117. if ((memory_start <= (u32)_text) &&
  118. ((u32)_text <= (memory_start + lowmem_size - 1))) {
  119. memory_size = lowmem_size;
  120. PAGE_OFFSET = memory_start;
  121. printk(KERN_INFO "%s: Main mem: 0x%x, "
  122. "size 0x%08x\n", __func__, (u32) memory_start,
  123. (u32) memory_size);
  124. break;
  125. }
  126. }
  127. if (!memory_start || !memory_size) {
  128. panic("%s: Missing memory setting 0x%08x, size=0x%08x\n",
  129. __func__, (u32) memory_start, (u32) memory_size);
  130. }
  131. /* reservation of region where is the kernel */
  132. kernel_align_start = PAGE_DOWN((u32)_text);
  133. /* ALIGN can be remove because _end in vmlinux.lds.S is align */
  134. kernel_align_size = PAGE_UP((u32)klimit) - kernel_align_start;
  135. printk(KERN_INFO "%s: kernel addr:0x%08x-0x%08x size=0x%08x\n",
  136. __func__, kernel_align_start, kernel_align_start
  137. + kernel_align_size, kernel_align_size);
  138. memblock_reserve(kernel_align_start, kernel_align_size);
  139. #endif
  140. /*
  141. * Kernel:
  142. * start: base phys address of kernel - page align
  143. * end: base phys address of kernel - page align
  144. *
  145. * min_low_pfn - the first page (mm/bootmem.c - node_boot_start)
  146. * max_low_pfn
  147. * max_mapnr - the first unused page (mm/bootmem.c - node_low_pfn)
  148. * num_physpages - number of all pages
  149. */
  150. /* memory start is from the kernel end (aligned) to higher addr */
  151. min_low_pfn = memory_start >> PAGE_SHIFT; /* minimum for allocation */
  152. /* RAM is assumed contiguous */
  153. num_physpages = max_mapnr = memory_size >> PAGE_SHIFT;
  154. max_low_pfn = ((u64)memory_start + (u64)lowmem_size) >> PAGE_SHIFT;
  155. max_pfn = ((u64)memory_start + (u64)memory_size) >> PAGE_SHIFT;
  156. printk(KERN_INFO "%s: max_mapnr: %#lx\n", __func__, max_mapnr);
  157. printk(KERN_INFO "%s: min_low_pfn: %#lx\n", __func__, min_low_pfn);
  158. printk(KERN_INFO "%s: max_low_pfn: %#lx\n", __func__, max_low_pfn);
  159. printk(KERN_INFO "%s: max_pfn: %#lx\n", __func__, max_pfn);
  160. /*
  161. * Find an area to use for the bootmem bitmap.
  162. * We look for the first area which is at least
  163. * 128kB in length (128kB is enough for a bitmap
  164. * for 4GB of memory, using 4kB pages), plus 1 page
  165. * (in case the address isn't page-aligned).
  166. */
  167. map_size = init_bootmem_node(NODE_DATA(0),
  168. PFN_UP(TOPHYS((u32)klimit)), min_low_pfn, max_low_pfn);
  169. memblock_reserve(PFN_UP(TOPHYS((u32)klimit)) << PAGE_SHIFT, map_size);
  170. /* Add active regions with valid PFNs */
  171. for_each_memblock(memory, reg) {
  172. unsigned long start_pfn, end_pfn;
  173. start_pfn = memblock_region_memory_base_pfn(reg);
  174. end_pfn = memblock_region_memory_end_pfn(reg);
  175. memblock_set_node(start_pfn << PAGE_SHIFT,
  176. (end_pfn - start_pfn) << PAGE_SHIFT, 0);
  177. }
  178. /* free bootmem is whole main memory */
  179. free_bootmem_with_active_regions(0, max_low_pfn);
  180. /* reserve allocate blocks */
  181. for_each_memblock(reserved, reg) {
  182. unsigned long top = reg->base + reg->size - 1;
  183. pr_debug("reserved - 0x%08x-0x%08x, %lx, %lx\n",
  184. (u32) reg->base, (u32) reg->size, top,
  185. memory_start + lowmem_size - 1);
  186. if (top <= (memory_start + lowmem_size - 1)) {
  187. reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
  188. } else if (reg->base < (memory_start + lowmem_size - 1)) {
  189. unsigned long trunc_size = memory_start + lowmem_size -
  190. reg->base;
  191. reserve_bootmem(reg->base, trunc_size, BOOTMEM_DEFAULT);
  192. }
  193. }
  194. /* XXX need to clip this if using highmem? */
  195. sparse_memory_present_with_active_regions(0);
  196. #ifdef CONFIG_MMU
  197. init_bootmem_done = 1;
  198. #endif
  199. paging_init();
  200. }
  201. void free_init_pages(char *what, unsigned long begin, unsigned long end)
  202. {
  203. unsigned long addr;
  204. for (addr = begin; addr < end; addr += PAGE_SIZE) {
  205. ClearPageReserved(virt_to_page(addr));
  206. init_page_count(virt_to_page(addr));
  207. free_page(addr);
  208. totalram_pages++;
  209. }
  210. printk(KERN_INFO "Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
  211. }
  212. #ifdef CONFIG_BLK_DEV_INITRD
  213. void free_initrd_mem(unsigned long start, unsigned long end)
  214. {
  215. int pages = 0;
  216. for (; start < end; start += PAGE_SIZE) {
  217. ClearPageReserved(virt_to_page(start));
  218. init_page_count(virt_to_page(start));
  219. free_page(start);
  220. totalram_pages++;
  221. pages++;
  222. }
  223. printk(KERN_NOTICE "Freeing initrd memory: %dk freed\n",
  224. (int)(pages * (PAGE_SIZE / 1024)));
  225. }
  226. #endif
  227. void free_initmem(void)
  228. {
  229. free_init_pages("unused kernel memory",
  230. (unsigned long)(&__init_begin),
  231. (unsigned long)(&__init_end));
  232. }
  233. void __init mem_init(void)
  234. {
  235. pg_data_t *pgdat;
  236. unsigned long reservedpages = 0, codesize, initsize, datasize, bsssize;
  237. high_memory = (void *)__va(memory_start + lowmem_size - 1);
  238. /* this will put all memory onto the freelists */
  239. totalram_pages += free_all_bootmem();
  240. for_each_online_pgdat(pgdat) {
  241. unsigned long i;
  242. struct page *page;
  243. for (i = 0; i < pgdat->node_spanned_pages; i++) {
  244. if (!pfn_valid(pgdat->node_start_pfn + i))
  245. continue;
  246. page = pgdat_page_nr(pgdat, i);
  247. if (PageReserved(page))
  248. reservedpages++;
  249. }
  250. }
  251. #ifdef CONFIG_HIGHMEM
  252. reservedpages -= highmem_setup();
  253. #endif
  254. codesize = (unsigned long)&_sdata - (unsigned long)&_stext;
  255. datasize = (unsigned long)&_edata - (unsigned long)&_sdata;
  256. initsize = (unsigned long)&__init_end - (unsigned long)&__init_begin;
  257. bsssize = (unsigned long)&__bss_stop - (unsigned long)&__bss_start;
  258. pr_info("Memory: %luk/%luk available (%luk kernel code, "
  259. "%luk reserved, %luk data, %luk bss, %luk init)\n",
  260. nr_free_pages() << (PAGE_SHIFT-10),
  261. num_physpages << (PAGE_SHIFT-10),
  262. codesize >> 10,
  263. reservedpages << (PAGE_SHIFT-10),
  264. datasize >> 10,
  265. bsssize >> 10,
  266. initsize >> 10);
  267. #ifdef CONFIG_MMU
  268. pr_info("Kernel virtual memory layout:\n");
  269. pr_info(" * 0x%08lx..0x%08lx : fixmap\n", FIXADDR_START, FIXADDR_TOP);
  270. #ifdef CONFIG_HIGHMEM
  271. pr_info(" * 0x%08lx..0x%08lx : highmem PTEs\n",
  272. PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP));
  273. #endif /* CONFIG_HIGHMEM */
  274. pr_info(" * 0x%08lx..0x%08lx : early ioremap\n",
  275. ioremap_bot, ioremap_base);
  276. pr_info(" * 0x%08lx..0x%08lx : vmalloc & ioremap\n",
  277. (unsigned long)VMALLOC_START, VMALLOC_END);
  278. #endif
  279. mem_init_done = 1;
  280. }
  281. #ifndef CONFIG_MMU
  282. int page_is_ram(unsigned long pfn)
  283. {
  284. return __range_ok(pfn, 0);
  285. }
  286. #else
  287. int page_is_ram(unsigned long pfn)
  288. {
  289. return pfn < max_low_pfn;
  290. }
  291. /*
  292. * Check for command-line options that affect what MMU_init will do.
  293. */
  294. static void mm_cmdline_setup(void)
  295. {
  296. unsigned long maxmem = 0;
  297. char *p = cmd_line;
  298. /* Look for mem= option on command line */
  299. p = strstr(cmd_line, "mem=");
  300. if (p) {
  301. p += 4;
  302. maxmem = memparse(p, &p);
  303. if (maxmem && memory_size > maxmem) {
  304. memory_size = maxmem;
  305. memblock.memory.regions[0].size = memory_size;
  306. }
  307. }
  308. }
  309. /*
  310. * MMU_init_hw does the chip-specific initialization of the MMU hardware.
  311. */
  312. static void __init mmu_init_hw(void)
  313. {
  314. /*
  315. * The Zone Protection Register (ZPR) defines how protection will
  316. * be applied to every page which is a member of a given zone. At
  317. * present, we utilize only two of the zones.
  318. * The zone index bits (of ZSEL) in the PTE are used for software
  319. * indicators, except the LSB. For user access, zone 1 is used,
  320. * for kernel access, zone 0 is used. We set all but zone 1
  321. * to zero, allowing only kernel access as indicated in the PTE.
  322. * For zone 1, we set a 01 binary (a value of 10 will not work)
  323. * to allow user access as indicated in the PTE. This also allows
  324. * kernel access as indicated in the PTE.
  325. */
  326. __asm__ __volatile__ ("ori r11, r0, 0x10000000;" \
  327. "mts rzpr, r11;"
  328. : : : "r11");
  329. }
  330. /*
  331. * MMU_init sets up the basic memory mappings for the kernel,
  332. * including both RAM and possibly some I/O regions,
  333. * and sets up the page tables and the MMU hardware ready to go.
  334. */
  335. /* called from head.S */
  336. asmlinkage void __init mmu_init(void)
  337. {
  338. unsigned int kstart, ksize;
  339. if (!memblock.reserved.cnt) {
  340. printk(KERN_EMERG "Error memory count\n");
  341. machine_restart(NULL);
  342. }
  343. if ((u32) memblock.memory.regions[0].size < 0x400000) {
  344. printk(KERN_EMERG "Memory must be greater than 4MB\n");
  345. machine_restart(NULL);
  346. }
  347. if ((u32) memblock.memory.regions[0].size < kernel_tlb) {
  348. printk(KERN_EMERG "Kernel size is greater than memory node\n");
  349. machine_restart(NULL);
  350. }
  351. /* Find main memory where the kernel is */
  352. memory_start = (u32) memblock.memory.regions[0].base;
  353. lowmem_size = memory_size = (u32) memblock.memory.regions[0].size;
  354. if (lowmem_size > CONFIG_LOWMEM_SIZE) {
  355. lowmem_size = CONFIG_LOWMEM_SIZE;
  356. #ifndef CONFIG_HIGHMEM
  357. memory_size = lowmem_size;
  358. #endif
  359. }
  360. mm_cmdline_setup(); /* FIXME parse args from command line - not used */
  361. /*
  362. * Map out the kernel text/data/bss from the available physical
  363. * memory.
  364. */
  365. kstart = __pa(CONFIG_KERNEL_START); /* kernel start */
  366. /* kernel size */
  367. ksize = PAGE_ALIGN(((u32)_end - (u32)CONFIG_KERNEL_START));
  368. memblock_reserve(kstart, ksize);
  369. #if defined(CONFIG_BLK_DEV_INITRD)
  370. /* Remove the init RAM disk from the available memory. */
  371. /* if (initrd_start) {
  372. mem_pieces_remove(&phys_avail, __pa(initrd_start),
  373. initrd_end - initrd_start, 1);
  374. }*/
  375. #endif /* CONFIG_BLK_DEV_INITRD */
  376. /* Initialize the MMU hardware */
  377. mmu_init_hw();
  378. /* Map in all of RAM starting at CONFIG_KERNEL_START */
  379. mapin_ram();
  380. /* Extend vmalloc and ioremap area as big as possible */
  381. #ifdef CONFIG_HIGHMEM
  382. ioremap_base = ioremap_bot = PKMAP_BASE;
  383. #else
  384. ioremap_base = ioremap_bot = FIXADDR_START;
  385. #endif
  386. /* Initialize the context management stuff */
  387. mmu_context_init();
  388. /* Shortly after that, the entire linear mapping will be available */
  389. /* This will also cause that unflatten device tree will be allocated
  390. * inside 768MB limit */
  391. memblock_set_current_limit(memory_start + lowmem_size - 1);
  392. }
  393. /* This is only called until mem_init is done. */
  394. void __init *early_get_page(void)
  395. {
  396. void *p;
  397. if (init_bootmem_done) {
  398. p = alloc_bootmem_pages(PAGE_SIZE);
  399. } else {
  400. /*
  401. * Mem start + kernel_tlb -> here is limit
  402. * because of mem mapping from head.S
  403. */
  404. p = __va(memblock_alloc_base(PAGE_SIZE, PAGE_SIZE,
  405. memory_start + kernel_tlb));
  406. }
  407. return p;
  408. }
  409. #endif /* CONFIG_MMU */
  410. void * __init_refok alloc_maybe_bootmem(size_t size, gfp_t mask)
  411. {
  412. if (mem_init_done)
  413. return kmalloc(size, mask);
  414. else
  415. return alloc_bootmem(size);
  416. }
  417. void * __init_refok zalloc_maybe_bootmem(size_t size, gfp_t mask)
  418. {
  419. void *p;
  420. if (mem_init_done)
  421. p = kzalloc(size, mask);
  422. else {
  423. p = alloc_bootmem(size);
  424. if (p)
  425. memset(p, 0, size);
  426. }
  427. return p;
  428. }