init.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #endif
  179. NODE_DATA(nid)->node_start_pfn = start_pfn;
  180. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  181. }
  182. static void __init do_init_bootmem(void)
  183. {
  184. struct memblock_region *reg;
  185. /* Add active regions with valid PFNs. */
  186. for_each_memblock(memory, reg) {
  187. unsigned long start_pfn, end_pfn;
  188. start_pfn = memblock_region_memory_base_pfn(reg);
  189. end_pfn = memblock_region_memory_end_pfn(reg);
  190. __add_active_range(0, start_pfn, end_pfn);
  191. }
  192. /* All of system RAM sits in node 0 for the non-NUMA case */
  193. allocate_pgdat(0);
  194. node_set_online(0);
  195. plat_mem_setup();
  196. for_each_memblock(memory, reg) {
  197. int nid = memblock_get_region_node(reg);
  198. memory_present(nid, memblock_region_memory_base_pfn(reg),
  199. memblock_region_memory_end_pfn(reg));
  200. }
  201. sparse_init();
  202. }
  203. static void __init early_reserve_mem(void)
  204. {
  205. unsigned long start_pfn;
  206. u32 zero_base = (u32)__MEMORY_START + (u32)PHYSICAL_OFFSET;
  207. u32 start = zero_base + (u32)CONFIG_ZERO_PAGE_OFFSET;
  208. /*
  209. * Partially used pages are not usable - thus
  210. * we are rounding upwards:
  211. */
  212. start_pfn = PFN_UP(__pa(_end));
  213. /*
  214. * Reserve the kernel text and Reserve the bootmem bitmap. We do
  215. * this in two steps (first step was init_bootmem()), because
  216. * this catches the (definitely buggy) case of us accidentally
  217. * initializing the bootmem allocator with an invalid RAM area.
  218. */
  219. memblock_reserve(start, (PFN_PHYS(start_pfn) + PAGE_SIZE - 1) - start);
  220. /*
  221. * Reserve physical pages below CONFIG_ZERO_PAGE_OFFSET.
  222. */
  223. if (CONFIG_ZERO_PAGE_OFFSET != 0)
  224. memblock_reserve(zero_base, CONFIG_ZERO_PAGE_OFFSET);
  225. /*
  226. * Handle additional early reservations
  227. */
  228. check_for_initrd();
  229. reserve_crashkernel();
  230. }
  231. void __init paging_init(void)
  232. {
  233. unsigned long max_zone_pfns[MAX_NR_ZONES];
  234. unsigned long vaddr, end;
  235. sh_mv.mv_mem_init();
  236. early_reserve_mem();
  237. /*
  238. * Once the early reservations are out of the way, give the
  239. * platforms a chance to kick out some memory.
  240. */
  241. if (sh_mv.mv_mem_reserve)
  242. sh_mv.mv_mem_reserve();
  243. memblock_enforce_memory_limit(memory_limit);
  244. memblock_allow_resize();
  245. memblock_dump_all();
  246. /*
  247. * Determine low and high memory ranges:
  248. */
  249. max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
  250. min_low_pfn = __MEMORY_START >> PAGE_SHIFT;
  251. nodes_clear(node_online_map);
  252. memory_start = (unsigned long)__va(__MEMORY_START);
  253. memory_end = memory_start + (memory_limit ?: memblock_phys_mem_size());
  254. uncached_init();
  255. pmb_init();
  256. do_init_bootmem();
  257. ioremap_fixed_init();
  258. /* We don't need to map the kernel through the TLB, as
  259. * it is permanatly mapped using P1. So clear the
  260. * entire pgd. */
  261. memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
  262. /* Set an initial value for the MMU.TTB so we don't have to
  263. * check for a null value. */
  264. set_TTB(swapper_pg_dir);
  265. /*
  266. * Populate the relevant portions of swapper_pg_dir so that
  267. * we can use the fixmap entries without calling kmalloc.
  268. * pte's will be filled in by __set_fixmap().
  269. */
  270. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  271. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  272. page_table_range_init(vaddr, end, swapper_pg_dir);
  273. kmap_coherent_init();
  274. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  275. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  276. free_area_init_nodes(max_zone_pfns);
  277. }
  278. unsigned int mem_init_done = 0;
  279. void __init mem_init(void)
  280. {
  281. pg_data_t *pgdat;
  282. high_memory = NULL;
  283. for_each_online_pgdat(pgdat)
  284. high_memory = max_t(void *, high_memory,
  285. __va(pgdat_end_pfn(pgdat) << PAGE_SHIFT));
  286. free_all_bootmem();
  287. /* Set this up early, so we can take care of the zero page */
  288. cpu_cache_init();
  289. /* clear the zero-page */
  290. memset(empty_zero_page, 0, PAGE_SIZE);
  291. __flush_wback_region(empty_zero_page, PAGE_SIZE);
  292. vsyscall_init();
  293. mem_init_print_info(NULL);
  294. pr_info("virtual kernel memory layout:\n"
  295. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  296. #ifdef CONFIG_HIGHMEM
  297. " pkmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  298. #endif
  299. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  300. " lowmem : 0x%08lx - 0x%08lx (%4ld MB) (cached)\n"
  301. #ifdef CONFIG_UNCACHED_MAPPING
  302. " : 0x%08lx - 0x%08lx (%4ld MB) (uncached)\n"
  303. #endif
  304. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  305. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  306. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  307. FIXADDR_START, FIXADDR_TOP,
  308. (FIXADDR_TOP - FIXADDR_START) >> 10,
  309. #ifdef CONFIG_HIGHMEM
  310. PKMAP_BASE, PKMAP_BASE+LAST_PKMAP*PAGE_SIZE,
  311. (LAST_PKMAP*PAGE_SIZE) >> 10,
  312. #endif
  313. (unsigned long)VMALLOC_START, VMALLOC_END,
  314. (VMALLOC_END - VMALLOC_START) >> 20,
  315. (unsigned long)memory_start, (unsigned long)high_memory,
  316. ((unsigned long)high_memory - (unsigned long)memory_start) >> 20,
  317. #ifdef CONFIG_UNCACHED_MAPPING
  318. uncached_start, uncached_end, uncached_size >> 20,
  319. #endif
  320. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  321. ((unsigned long)&__init_end -
  322. (unsigned long)&__init_begin) >> 10,
  323. (unsigned long)&_etext, (unsigned long)&_edata,
  324. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  325. (unsigned long)&_text, (unsigned long)&_etext,
  326. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  327. mem_init_done = 1;
  328. }
  329. void free_initmem(void)
  330. {
  331. free_initmem_default(-1);
  332. }
  333. #ifdef CONFIG_BLK_DEV_INITRD
  334. void free_initrd_mem(unsigned long start, unsigned long end)
  335. {
  336. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  337. }
  338. #endif
  339. #ifdef CONFIG_MEMORY_HOTPLUG
  340. int arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap,
  341. bool want_memblock)
  342. {
  343. unsigned long start_pfn = PFN_DOWN(start);
  344. unsigned long nr_pages = size >> PAGE_SHIFT;
  345. int ret;
  346. /* We only have ZONE_NORMAL, so this is easy.. */
  347. ret = __add_pages(nid, start_pfn, nr_pages, altmap, want_memblock);
  348. if (unlikely(ret))
  349. printk("%s: Failed, __add_pages() == %d\n", __func__, ret);
  350. return ret;
  351. }
  352. #ifdef CONFIG_NUMA
  353. int memory_add_physaddr_to_nid(u64 addr)
  354. {
  355. /* Node 0 for now.. */
  356. return 0;
  357. }
  358. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  359. #endif
  360. void arch_remove_memory(int nid, u64 start, u64 size,
  361. struct vmem_altmap *altmap)
  362. {
  363. unsigned long start_pfn = PFN_DOWN(start);
  364. unsigned long nr_pages = size >> PAGE_SHIFT;
  365. __remove_pages(start_pfn, nr_pages, altmap);
  366. }
  367. #endif /* CONFIG_MEMORY_HOTPLUG */