init.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/sh/mm/init.c
  4. *
  5. * Copyright (C) 1999 Niibe Yutaka
  6. * Copyright (C) 2002 - 2011 Paul Mundt
  7. *
  8. * Based on linux/arch/i386/mm/init.c:
  9. * Copyright (C) 1995 Linus Torvalds
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/swap.h>
  13. #include <linux/init.h>
  14. #include <linux/gfp.h>
  15. #include <linux/memblock.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/percpu.h>
  19. #include <linux/io.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/pgalloc.h>
  31. #include <linux/sizes.h>
  32. #include "ioremap.h"
  33. pgd_t swapper_pg_dir[PTRS_PER_PGD];
  34. void __init generic_mem_init(void)
  35. {
  36. memblock_add(__MEMORY_START, __MEMORY_SIZE);
  37. }
  38. void __init __weak plat_mem_setup(void)
  39. {
  40. /* Nothing to see here, move along. */
  41. }
  42. #ifdef CONFIG_MMU
  43. static pte_t *__get_pte_phys(unsigned long addr)
  44. {
  45. pgd_t *pgd;
  46. p4d_t *p4d;
  47. pud_t *pud;
  48. pmd_t *pmd;
  49. pgd = pgd_offset_k(addr);
  50. if (pgd_none(*pgd)) {
  51. pgd_ERROR(*pgd);
  52. return NULL;
  53. }
  54. p4d = p4d_alloc(NULL, pgd, addr);
  55. if (unlikely(!p4d)) {
  56. p4d_ERROR(*p4d);
  57. return NULL;
  58. }
  59. pud = pud_alloc(NULL, p4d, addr);
  60. if (unlikely(!pud)) {
  61. pud_ERROR(*pud);
  62. return NULL;
  63. }
  64. pmd = pmd_alloc(NULL, pud, addr);
  65. if (unlikely(!pmd)) {
  66. pmd_ERROR(*pmd);
  67. return NULL;
  68. }
  69. return pte_offset_kernel(pmd, addr);
  70. }
  71. static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
  72. {
  73. pte_t *pte;
  74. pte = __get_pte_phys(addr);
  75. if (!pte_none(*pte)) {
  76. pte_ERROR(*pte);
  77. return;
  78. }
  79. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
  80. local_flush_tlb_one(get_asid(), addr);
  81. if (pgprot_val(prot) & _PAGE_WIRED)
  82. tlb_wire_entry(NULL, addr, *pte);
  83. }
  84. static void clear_pte_phys(unsigned long addr, pgprot_t prot)
  85. {
  86. pte_t *pte;
  87. pte = __get_pte_phys(addr);
  88. if (pgprot_val(prot) & _PAGE_WIRED)
  89. tlb_unwire_entry();
  90. set_pte(pte, pfn_pte(0, __pgprot(0)));
  91. local_flush_tlb_one(get_asid(), addr);
  92. }
  93. void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
  94. {
  95. unsigned long address = __fix_to_virt(idx);
  96. if (idx >= __end_of_fixed_addresses) {
  97. BUG();
  98. return;
  99. }
  100. set_pte_phys(address, phys, prot);
  101. }
  102. void __clear_fixmap(enum fixed_addresses idx, pgprot_t prot)
  103. {
  104. unsigned long address = __fix_to_virt(idx);
  105. if (idx >= __end_of_fixed_addresses) {
  106. BUG();
  107. return;
  108. }
  109. clear_pte_phys(address, prot);
  110. }
  111. static pmd_t * __init one_md_table_init(pud_t *pud)
  112. {
  113. if (pud_none(*pud)) {
  114. pmd_t *pmd;
  115. pmd = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  116. if (!pmd)
  117. panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
  118. __func__, PAGE_SIZE, PAGE_SIZE);
  119. pud_populate(&init_mm, pud, pmd);
  120. BUG_ON(pmd != pmd_offset(pud, 0));
  121. }
  122. return pmd_offset(pud, 0);
  123. }
  124. static pte_t * __init one_page_table_init(pmd_t *pmd)
  125. {
  126. if (pmd_none(*pmd)) {
  127. pte_t *pte;
  128. pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  129. if (!pte)
  130. panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
  131. __func__, PAGE_SIZE, PAGE_SIZE);
  132. pmd_populate_kernel(&init_mm, pmd, pte);
  133. BUG_ON(pte != pte_offset_kernel(pmd, 0));
  134. }
  135. return pte_offset_kernel(pmd, 0);
  136. }
  137. static pte_t * __init page_table_kmap_check(pte_t *pte, pmd_t *pmd,
  138. unsigned long vaddr, pte_t *lastpte)
  139. {
  140. return pte;
  141. }
  142. void __init page_table_range_init(unsigned long start, unsigned long end,
  143. pgd_t *pgd_base)
  144. {
  145. pgd_t *pgd;
  146. pud_t *pud;
  147. pmd_t *pmd;
  148. pte_t *pte = NULL;
  149. int i, j, k;
  150. unsigned long vaddr;
  151. vaddr = start;
  152. i = pgd_index(vaddr);
  153. j = pud_index(vaddr);
  154. k = pmd_index(vaddr);
  155. pgd = pgd_base + i;
  156. for ( ; (i < PTRS_PER_PGD) && (vaddr != end); pgd++, i++) {
  157. pud = (pud_t *)pgd;
  158. for ( ; (j < PTRS_PER_PUD) && (vaddr != end); pud++, j++) {
  159. pmd = one_md_table_init(pud);
  160. #ifndef __PAGETABLE_PMD_FOLDED
  161. pmd += k;
  162. #endif
  163. for (; (k < PTRS_PER_PMD) && (vaddr != end); pmd++, k++) {
  164. pte = page_table_kmap_check(one_page_table_init(pmd),
  165. pmd, vaddr, pte);
  166. vaddr += PMD_SIZE;
  167. }
  168. k = 0;
  169. }
  170. j = 0;
  171. }
  172. }
  173. #endif /* CONFIG_MMU */
  174. void __init allocate_pgdat(unsigned int nid)
  175. {
  176. unsigned long start_pfn, end_pfn;
  177. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  178. #ifdef CONFIG_NUMA
  179. NODE_DATA(nid) = memblock_alloc_try_nid(
  180. sizeof(struct pglist_data),
  181. SMP_CACHE_BYTES, MEMBLOCK_LOW_LIMIT,
  182. MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  183. if (!NODE_DATA(nid))
  184. panic("Can't allocate pgdat for node %d\n", nid);
  185. #endif
  186. NODE_DATA(nid)->node_start_pfn = start_pfn;
  187. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  188. }
  189. static void __init do_init_bootmem(void)
  190. {
  191. unsigned long start_pfn, end_pfn;
  192. int i;
  193. /* Add active regions with valid PFNs. */
  194. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL)
  195. __add_active_range(0, start_pfn, end_pfn);
  196. /* All of system RAM sits in node 0 for the non-NUMA case */
  197. allocate_pgdat(0);
  198. node_set_online(0);
  199. plat_mem_setup();
  200. sparse_init();
  201. }
  202. static void __init early_reserve_mem(void)
  203. {
  204. unsigned long start_pfn;
  205. u32 zero_base = (u32)__MEMORY_START + (u32)PHYSICAL_OFFSET;
  206. u32 start = zero_base + (u32)CONFIG_ZERO_PAGE_OFFSET;
  207. /*
  208. * Partially used pages are not usable - thus
  209. * we are rounding upwards:
  210. */
  211. start_pfn = PFN_UP(__pa(_end));
  212. /*
  213. * Reserve the kernel text and Reserve the bootmem bitmap. We do
  214. * this in two steps (first step was init_bootmem()), because
  215. * this catches the (definitely buggy) case of us accidentally
  216. * initializing the bootmem allocator with an invalid RAM area.
  217. */
  218. memblock_reserve(start, (PFN_PHYS(start_pfn) + PAGE_SIZE - 1) - start);
  219. /*
  220. * Reserve physical pages below CONFIG_ZERO_PAGE_OFFSET.
  221. */
  222. if (CONFIG_ZERO_PAGE_OFFSET != 0)
  223. memblock_reserve(zero_base, CONFIG_ZERO_PAGE_OFFSET);
  224. /*
  225. * Handle additional early reservations
  226. */
  227. check_for_initrd();
  228. reserve_crashkernel();
  229. }
  230. void __init paging_init(void)
  231. {
  232. unsigned long max_zone_pfns[MAX_NR_ZONES];
  233. unsigned long vaddr, end;
  234. sh_mv.mv_mem_init();
  235. early_reserve_mem();
  236. /*
  237. * Once the early reservations are out of the way, give the
  238. * platforms a chance to kick out some memory.
  239. */
  240. if (sh_mv.mv_mem_reserve)
  241. sh_mv.mv_mem_reserve();
  242. memblock_enforce_memory_limit(memory_limit);
  243. memblock_allow_resize();
  244. memblock_dump_all();
  245. /*
  246. * Determine low and high memory ranges:
  247. */
  248. max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
  249. min_low_pfn = __MEMORY_START >> PAGE_SHIFT;
  250. nodes_clear(node_online_map);
  251. memory_start = (unsigned long)__va(__MEMORY_START);
  252. memory_end = memory_start + (memory_limit ?: memblock_phys_mem_size());
  253. uncached_init();
  254. pmb_init();
  255. do_init_bootmem();
  256. ioremap_fixed_init();
  257. /* We don't need to map the kernel through the TLB, as
  258. * it is permanatly mapped using P1. So clear the
  259. * entire pgd. */
  260. memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
  261. /* Set an initial value for the MMU.TTB so we don't have to
  262. * check for a null value. */
  263. set_TTB(swapper_pg_dir);
  264. /*
  265. * Populate the relevant portions of swapper_pg_dir so that
  266. * we can use the fixmap entries without calling kmalloc.
  267. * pte's will be filled in by __set_fixmap().
  268. */
  269. vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
  270. end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
  271. page_table_range_init(vaddr, end, swapper_pg_dir);
  272. kmap_coherent_init();
  273. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  274. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  275. free_area_init(max_zone_pfns);
  276. }
  277. unsigned int mem_init_done = 0;
  278. void __init mem_init(void)
  279. {
  280. pg_data_t *pgdat;
  281. high_memory = NULL;
  282. for_each_online_pgdat(pgdat)
  283. high_memory = max_t(void *, high_memory,
  284. __va(pgdat_end_pfn(pgdat) << PAGE_SHIFT));
  285. memblock_free_all();
  286. /* Set this up early, so we can take care of the zero page */
  287. cpu_cache_init();
  288. /* clear the zero-page */
  289. memset(empty_zero_page, 0, PAGE_SIZE);
  290. __flush_wback_region(empty_zero_page, PAGE_SIZE);
  291. vsyscall_init();
  292. pr_info("virtual kernel memory layout:\n"
  293. " fixmap : 0x%08lx - 0x%08lx (%4ld kB)\n"
  294. " vmalloc : 0x%08lx - 0x%08lx (%4ld MB)\n"
  295. " lowmem : 0x%08lx - 0x%08lx (%4ld MB) (cached)\n"
  296. #ifdef CONFIG_UNCACHED_MAPPING
  297. " : 0x%08lx - 0x%08lx (%4ld MB) (uncached)\n"
  298. #endif
  299. " .init : 0x%08lx - 0x%08lx (%4ld kB)\n"
  300. " .data : 0x%08lx - 0x%08lx (%4ld kB)\n"
  301. " .text : 0x%08lx - 0x%08lx (%4ld kB)\n",
  302. FIXADDR_START, FIXADDR_TOP,
  303. (FIXADDR_TOP - FIXADDR_START) >> 10,
  304. (unsigned long)VMALLOC_START, VMALLOC_END,
  305. (VMALLOC_END - VMALLOC_START) >> 20,
  306. (unsigned long)memory_start, (unsigned long)high_memory,
  307. ((unsigned long)high_memory - (unsigned long)memory_start) >> 20,
  308. #ifdef CONFIG_UNCACHED_MAPPING
  309. uncached_start, uncached_end, uncached_size >> 20,
  310. #endif
  311. (unsigned long)&__init_begin, (unsigned long)&__init_end,
  312. ((unsigned long)&__init_end -
  313. (unsigned long)&__init_begin) >> 10,
  314. (unsigned long)&_etext, (unsigned long)&_edata,
  315. ((unsigned long)&_edata - (unsigned long)&_etext) >> 10,
  316. (unsigned long)&_text, (unsigned long)&_etext,
  317. ((unsigned long)&_etext - (unsigned long)&_text) >> 10);
  318. mem_init_done = 1;
  319. }
  320. #ifdef CONFIG_MEMORY_HOTPLUG
  321. int arch_add_memory(int nid, u64 start, u64 size,
  322. struct mhp_params *params)
  323. {
  324. unsigned long start_pfn = PFN_DOWN(start);
  325. unsigned long nr_pages = size >> PAGE_SHIFT;
  326. int ret;
  327. if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
  328. return -EINVAL;
  329. /* We only have ZONE_NORMAL, so this is easy.. */
  330. ret = __add_pages(nid, start_pfn, nr_pages, params);
  331. if (unlikely(ret))
  332. printk("%s: Failed, __add_pages() == %d\n", __func__, ret);
  333. return ret;
  334. }
  335. void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
  336. {
  337. unsigned long start_pfn = PFN_DOWN(start);
  338. unsigned long nr_pages = size >> PAGE_SHIFT;
  339. __remove_pages(start_pfn, nr_pages, altmap);
  340. }
  341. #endif /* CONFIG_MEMORY_HOTPLUG */