init.c 12 KB

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