init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * linux/arch/sh/mm/init.c
  3. *
  4. * Copyright (C) 1999 Niibe Yutaka
  5. * Copyright (C) 2002 - 2011 Paul Mundt
  6. *
  7. * Based on linux/arch/i386/mm/init.c:
  8. * Copyright (C) 1995 Linus Torvalds
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/swap.h>
  12. #include <linux/init.h>
  13. #include <linux/gfp.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/percpu.h>
  18. #include <linux/io.h>
  19. #include <linux/memblock.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/export.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/mmzone.h>
  24. #include <asm/kexec.h>
  25. #include <asm/tlb.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/sections.h>
  28. #include <asm/setup.h>
  29. #include <asm/cache.h>
  30. #include <asm/sizes.h>
  31. pgd_t swapper_pg_dir[PTRS_PER_PGD];
  32. void __init generic_mem_init(void)
  33. {
  34. memblock_add(__MEMORY_START, __MEMORY_SIZE);
  35. }
  36. void __init __weak plat_mem_setup(void)
  37. {
  38. /* Nothing to see here, move along. */
  39. }
  40. #ifdef CONFIG_MMU
  41. static pte_t *__get_pte_phys(unsigned long addr)
  42. {
  43. pgd_t *pgd;
  44. pud_t *pud;
  45. pmd_t *pmd;
  46. pgd = pgd_offset_k(addr);
  47. if (pgd_none(*pgd)) {
  48. pgd_ERROR(*pgd);
  49. return NULL;
  50. }
  51. pud = pud_alloc(NULL, pgd, addr);
  52. if (unlikely(!pud)) {
  53. pud_ERROR(*pud);
  54. return NULL;
  55. }
  56. pmd = pmd_alloc(NULL, pud, addr);
  57. if (unlikely(!pmd)) {
  58. pmd_ERROR(*pmd);
  59. return NULL;
  60. }
  61. return pte_offset_kernel(pmd, addr);
  62. }
  63. static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
  64. {
  65. pte_t *pte;
  66. pte = __get_pte_phys(addr);
  67. if (!pte_none(*pte)) {
  68. pte_ERROR(*pte);
  69. return;
  70. }
  71. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
  72. local_flush_tlb_one(get_asid(), addr);
  73. if (pgprot_val(prot) & _PAGE_WIRED)
  74. tlb_wire_entry(NULL, addr, *pte);
  75. }
  76. static void clear_pte_phys(unsigned long addr, pgprot_t prot)
  77. {
  78. pte_t *pte;
  79. pte = __get_pte_phys(addr);
  80. if (pgprot_val(prot) & _PAGE_WIRED)
  81. tlb_unwire_entry();
  82. set_pte(pte, pfn_pte(0, __pgprot(0)));
  83. local_flush_tlb_one(get_asid(), addr);
  84. }
  85. void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
  86. {
  87. unsigned long address = __fix_to_virt(idx);
  88. if (idx >= __end_of_fixed_addresses) {
  89. BUG();
  90. return;
  91. }
  92. set_pte_phys(address, phys, prot);
  93. }
  94. void __clear_fixmap(enum fixed_addresses idx, pgprot_t prot)
  95. {
  96. unsigned long address = __fix_to_virt(idx);
  97. if (idx >= __end_of_fixed_addresses) {
  98. BUG();
  99. return;
  100. }
  101. clear_pte_phys(address, prot);
  102. }
  103. static pmd_t * __init one_md_table_init(pud_t *pud)
  104. {
  105. if (pud_none(*pud)) {
  106. pmd_t *pmd;
  107. pmd = alloc_bootmem_pages(PAGE_SIZE);
  108. pud_populate(&init_mm, pud, pmd);
  109. BUG_ON(pmd != pmd_offset(pud, 0));
  110. }
  111. return pmd_offset(pud, 0);
  112. }
  113. static pte_t * __init one_page_table_init(pmd_t *pmd)
  114. {
  115. if (pmd_none(*pmd)) {
  116. pte_t *pte;
  117. pte = alloc_bootmem_pages(PAGE_SIZE);
  118. pmd_populate_kernel(&init_mm, pmd, pte);
  119. BUG_ON(pte != pte_offset_kernel(pmd, 0));
  120. }
  121. return pte_offset_kernel(pmd, 0);
  122. }
  123. static pte_t * __init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
  124. unsigned long vaddr, pte_t *lastpte)
  125. {
  126. return pte;
  127. }
  128. void __init page_table_range_init(unsigned long start, unsigned long end,
  129. pgd_t *pgd_base)
  130. {
  131. pgd_t *pgd;
  132. pud_t *pud;
  133. pmd_t *pmd;
  134. pte_t *pte = NULL;
  135. int i, j, k;
  136. unsigned long vaddr;
  137. vaddr = start;
  138. i = __pgd_offset(vaddr);
  139. j = __pud_offset(vaddr);
  140. k = __pmd_offset(vaddr);
  141. pgd = pgd_base + i;
  142. for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
  143. pud = (pud_t *)pgd;
  144. for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
  145. pmd = one_md_table_init(pud);
  146. #ifndef __PAGETABLE_PMD_FOLDED
  147. pmd += k;
  148. #endif
  149. for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
  150. pte = page_table_kmap_check(one_page_table_init(pmd),
  151. pmd, vaddr, pte);
  152. vaddr += PMD_SIZE;
  153. }
  154. k = 0;
  155. }
  156. j = 0;
  157. }
  158. }
  159. #endif /* CONFIG_MMU */
  160. void __init allocate_pgdat(unsigned int nid)
  161. {
  162. unsigned long start_pfn, end_pfn;
  163. #ifdef CONFIG_NEED_MULTIPLE_NODES
  164. unsigned long phys;
  165. #endif
  166. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  167. #ifdef CONFIG_NEED_MULTIPLE_NODES
  168. phys = __memblock_alloc_base(sizeof(struct pglist_data),
  169. SMP_CACHE_BYTES, end_pfn << PAGE_SHIFT);
  170. /* Retry with all of system memory */
  171. if (!phys)
  172. phys = __memblock_alloc_base(sizeof(struct pglist_data),
  173. SMP_CACHE_BYTES, memblock_end_of_DRAM());
  174. if (!phys)
  175. panic("Can't allocate pgdat for node %d\n", nid);
  176. NODE_DATA(nid) = __va(phys);
  177. memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
  178. NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
  179. #endif
  180. NODE_DATA(nid)->node_start_pfn = start_pfn;
  181. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  182. }
  183. static void __init bootmem_init_one_node(unsigned int nid)
  184. {
  185. unsigned long total_pages, paddr;
  186. unsigned long end_pfn;
  187. struct pglist_data *p;
  188. p = NODE_DATA(nid);
  189. /* Nothing to do.. */
  190. if (!p->node_spanned_pages)
  191. return;
  192. end_pfn = pgdat_end_pfn(p);
  193. total_pages = bootmem_bootmap_pages(p->node_spanned_pages);
  194. paddr = memblock_alloc(total_pages << PAGE_SHIFT, PAGE_SIZE);
  195. if (!paddr)
  196. panic("Can't allocate bootmap for nid[%d]\n", nid);
  197. init_bootmem_node(p, paddr >> PAGE_SHIFT, p->node_start_pfn, end_pfn);
  198. free_bootmem_with_active_regions(nid, end_pfn);
  199. /*
  200. * XXX Handle initial reservations for the system memory node
  201. * only for the moment, we'll refactor this later for handling
  202. * reservations in other nodes.
  203. */
  204. if (nid == 0) {
  205. struct memblock_region *reg;
  206. /* Reserve the sections we're already using. */
  207. for_each_memblock(reserved, reg) {
  208. reserve_bootmem(reg->base, reg->size, BOOTMEM_DEFAULT);
  209. }
  210. }
  211. sparse_memory_present_with_active_regions(nid);
  212. }
  213. static void __init do_init_bootmem(void)
  214. {
  215. struct memblock_region *reg;
  216. int i;
  217. /* Add active regions with valid PFNs. */
  218. for_each_memblock(memory, reg) {
  219. unsigned long start_pfn, end_pfn;
  220. start_pfn = memblock_region_memory_base_pfn(reg);
  221. end_pfn = memblock_region_memory_end_pfn(reg);
  222. __add_active_range(0, start_pfn, end_pfn);
  223. }
  224. /* All of system RAM sits in node 0 for the non-NUMA case */
  225. allocate_pgdat(0);
  226. node_set_online(0);
  227. plat_mem_setup();
  228. for_each_online_node(i)
  229. bootmem_init_one_node(i);
  230. sparse_init();
  231. }
  232. static void __init early_reserve_mem(void)
  233. {
  234. unsigned long start_pfn;
  235. u32 zero_base = (u32)__MEMORY_START + (u32)PHYSICAL_OFFSET;
  236. u32 start = zero_base + (u32)CONFIG_ZERO_PAGE_OFFSET;
  237. /*
  238. * Partially used pages are not usable - thus
  239. * we are rounding upwards:
  240. */
  241. start_pfn = PFN_UP(__pa(_end));
  242. /*
  243. * Reserve the kernel text and Reserve the bootmem bitmap. We do
  244. * this in two steps (first step was init_bootmem()), because
  245. * this catches the (definitely buggy) case of us accidentally
  246. * initializing the bootmem allocator with an invalid RAM area.
  247. */
  248. memblock_reserve(start, (PFN_PHYS(start_pfn) + PAGE_SIZE - 1) - start);
  249. /*
  250. * Reserve physical pages below CONFIG_ZERO_PAGE_OFFSET.
  251. */
  252. if (CONFIG_ZERO_PAGE_OFFSET != 0)
  253. memblock_reserve(zero_base, CONFIG_ZERO_PAGE_OFFSET);
  254. /*
  255. * Handle additional early reservations
  256. */
  257. check_for_initrd();
  258. reserve_crashkernel();
  259. }
  260. void __init paging_init(void)
  261. {
  262. unsigned long max_zone_pfns[MAX_NR_ZONES];
  263. unsigned long vaddr, end;
  264. int nid;
  265. sh_mv.mv_mem_init();
  266. early_reserve_mem();
  267. /*
  268. * Once the early reservations are out of the way, give the
  269. * platforms a chance to kick out some memory.
  270. */
  271. if (sh_mv.mv_mem_reserve)
  272. sh_mv.mv_mem_reserve();
  273. memblock_enforce_memory_limit(memory_limit);
  274. memblock_allow_resize();
  275. memblock_dump_all();
  276. /*
  277. * Determine low and high memory ranges:
  278. */
  279. max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
  280. min_low_pfn = __MEMORY_START >> PAGE_SHIFT;
  281. nodes_clear(node_online_map);
  282. memory_start = (unsigned long)__va(__MEMORY_START);
  283. memory_end = memory_start + (memory_limit ?: memblock_phys_mem_size());
  284. uncached_init();
  285. pmb_init();
  286. do_init_bootmem();
  287. ioremap_fixed_init();
  288. /* We don't need to map the kernel through the TLB, as
  289. * it is permanatly mapped using P1. So clear the
  290. * entire pgd. */
  291. memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
  292. /* Set an initial value for the MMU.TTB so we don't have to
  293. * check for a null value. */
  294. set_TTB(swapper_pg_dir);
  295. /*
  296. * Populate the relevant portions of swapper_pg_dir so that
  297. * we can use the fixmap entries without calling kmalloc.
  298. * pte's will be filled in by __set_fixmap().
  299. */
  300. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  301. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  302. page_table_range_init(vaddr, end, swapper_pg_dir);
  303. kmap_coherent_init();
  304. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  305. for_each_online_node(nid) {
  306. pg_data_t *pgdat = NODE_DATA(nid);
  307. unsigned long low, start_pfn;
  308. start_pfn = pgdat->bdata->node_min_pfn;
  309. low = pgdat->bdata->node_low_pfn;
  310. if (max_zone_pfns[ZONE_NORMAL] < low)
  311. max_zone_pfns[ZONE_NORMAL] = low;
  312. printk("Node %u: start_pfn = 0x%lx, low = 0x%lx\n",
  313. nid, start_pfn, low);
  314. }
  315. free_area_init_nodes(max_zone_pfns);
  316. }
  317. /*
  318. * Early initialization for any I/O MMUs we might have.
  319. */
  320. static void __init iommu_init(void)
  321. {
  322. no_iommu_init();
  323. }
  324. unsigned int mem_init_done = 0;
  325. void __init mem_init(void)
  326. {
  327. pg_data_t *pgdat;
  328. iommu_init();
  329. high_memory = NULL;
  330. for_each_online_pgdat(pgdat)
  331. high_memory = max_t(void *, high_memory,
  332. __va(pgdat_end_pfn(pgdat) << PAGE_SHIFT));
  333. free_all_bootmem();
  334. /* Set this up early, so we can take care of the zero page */
  335. cpu_cache_init();
  336. /* clear the zero-page */
  337. memset(empty_zero_page, 0, PAGE_SIZE);
  338. __flush_wback_region(empty_zero_page, PAGE_SIZE);
  339. vsyscall_init();
  340. mem_init_print_info(NULL);
  341. pr_info("virtual kernel memory layout:\n"
  342. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  343. #ifdef CONFIG_HIGHMEM
  344. " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  345. #endif
  346. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  347. " lowmem : 0x%08lx - 0x%08lx (%4ld MB) (cached)\n"
  348. #ifdef CONFIG_UNCACHED_MAPPING
  349. " : 0x%08lx - 0x%08lx (%4ld MB) (uncached)\n"
  350. #endif
  351. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  352. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  353. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  354. FIXADDR_START, FIXADDR_TOP,
  355. (FIXADDR_TOP - FIXADDR_START) >> 10,
  356. #ifdef CONFIG_HIGHMEM
  357. PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  358. (LAST_PKMAP*PAGE_SIZE) >> 10,
  359. #endif
  360. (unsigned long)VMALLOC_START, VMALLOC_END,
  361. (VMALLOC_END - VMALLOC_START) >> 20,
  362. (unsigned long)memory_start, (unsigned long)high_memory,
  363. ((unsigned long)high_memory - (unsigned long)memory_start) >> 20,
  364. #ifdef CONFIG_UNCACHED_MAPPING
  365. uncached_start, uncached_end, uncached_size >> 20,
  366. #endif
  367. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  368. ((unsigned long)&__init_end -
  369. (unsigned long)&__init_begin) >> 10,
  370. (unsigned long)&_etext, (unsigned long)&_edata,
  371. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  372. (unsigned long)&_text, (unsigned long)&_etext,
  373. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  374. mem_init_done = 1;
  375. }
  376. void free_initmem(void)
  377. {
  378. free_initmem_default(-1);
  379. }
  380. #ifdef CONFIG_BLK_DEV_INITRD
  381. void free_initrd_mem(unsigned long start, unsigned long end)
  382. {
  383. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  384. }
  385. #endif
  386. #ifdef CONFIG_MEMORY_HOTPLUG
  387. int arch_add_memory(int nid, u64 start, u64 size, bool for_device)
  388. {
  389. pg_data_t *pgdat;
  390. unsigned long start_pfn = PFN_DOWN(start);
  391. unsigned long nr_pages = size >> PAGE_SHIFT;
  392. int ret;
  393. pgdat = NODE_DATA(nid);
  394. /* We only have ZONE_NORMAL, so this is easy.. */
  395. ret = __add_pages(nid, pgdat->node_zones +
  396. zone_for_memory(nid, start, size, ZONE_NORMAL,
  397. for_device),
  398. start_pfn, nr_pages);
  399. if (unlikely(ret))
  400. printk("%s: Failed, __add_pages() == %d\n", __func__, ret);
  401. return ret;
  402. }
  403. EXPORT_SYMBOL_GPL(arch_add_memory);
  404. #ifdef CONFIG_NUMA
  405. int memory_add_physaddr_to_nid(u64 addr)
  406. {
  407. /* Node 0 for now.. */
  408. return 0;
  409. }
  410. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  411. #endif
  412. #ifdef CONFIG_MEMORY_HOTREMOVE
  413. int arch_remove_memory(u64 start, u64 size)
  414. {
  415. unsigned long start_pfn = PFN_DOWN(start);
  416. unsigned long nr_pages = size >> PAGE_SHIFT;
  417. struct zone *zone;
  418. int ret;
  419. zone = page_zone(pfn_to_page(start_pfn));
  420. ret = __remove_pages(zone, start_pfn, nr_pages);
  421. if (unlikely(ret))
  422. pr_warn("%s: Failed, __remove_pages() == %d\n", __func__,
  423. ret);
  424. return ret;
  425. }
  426. #endif
  427. #endif /* CONFIG_MEMORY_HOTPLUG */