init.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. * Copyright (C) 2019 Western Digital Corporation or its affiliates.
  5. * Copyright (C) 2020 FORTH-ICS/CARV
  6. * Nick Kossifidis <mick@ics.forth.gr>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/mm.h>
  10. #include <linux/memblock.h>
  11. #include <linux/initrd.h>
  12. #include <linux/swap.h>
  13. #include <linux/sizes.h>
  14. #include <linux/of_fdt.h>
  15. #include <linux/of_reserved_mem.h>
  16. #include <linux/libfdt.h>
  17. #include <linux/set_memory.h>
  18. #include <linux/crash_dump.h>
  19. #include <asm/fixmap.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/sections.h>
  22. #include <asm/soc.h>
  23. #include <asm/io.h>
  24. #include <asm/ptdump.h>
  25. #include "../kernel/head.h"
  26. unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
  27. __page_aligned_bss;
  28. EXPORT_SYMBOL(empty_zero_page);
  29. extern char _start[];
  30. #define DTB_EARLY_BASE_VA PGDIR_SIZE
  31. void *dtb_early_va __initdata;
  32. uintptr_t dtb_early_pa __initdata;
  33. struct pt_alloc_ops {
  34. pte_t *(*get_pte_virt)(phys_addr_t pa);
  35. phys_addr_t (*alloc_pte)(uintptr_t va);
  36. #ifndef __PAGETABLE_PMD_FOLDED
  37. pmd_t *(*get_pmd_virt)(phys_addr_t pa);
  38. phys_addr_t (*alloc_pmd)(uintptr_t va);
  39. #endif
  40. };
  41. static void __init zone_sizes_init(void)
  42. {
  43. unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
  44. #ifdef CONFIG_ZONE_DMA32
  45. max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G,
  46. (unsigned long) PFN_PHYS(max_low_pfn)));
  47. #endif
  48. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  49. free_area_init(max_zone_pfns);
  50. }
  51. static void setup_zero_page(void)
  52. {
  53. memset((void *)empty_zero_page, 0, PAGE_SIZE);
  54. }
  55. #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
  56. static inline void print_mlk(char *name, unsigned long b, unsigned long t)
  57. {
  58. pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t,
  59. (((t) - (b)) >> 10));
  60. }
  61. static inline void print_mlm(char *name, unsigned long b, unsigned long t)
  62. {
  63. pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t,
  64. (((t) - (b)) >> 20));
  65. }
  66. static void print_vm_layout(void)
  67. {
  68. pr_notice("Virtual kernel memory layout:\n");
  69. print_mlk("fixmap", (unsigned long)FIXADDR_START,
  70. (unsigned long)FIXADDR_TOP);
  71. print_mlm("pci io", (unsigned long)PCI_IO_START,
  72. (unsigned long)PCI_IO_END);
  73. print_mlm("vmemmap", (unsigned long)VMEMMAP_START,
  74. (unsigned long)VMEMMAP_END);
  75. print_mlm("vmalloc", (unsigned long)VMALLOC_START,
  76. (unsigned long)VMALLOC_END);
  77. print_mlm("lowmem", (unsigned long)PAGE_OFFSET,
  78. (unsigned long)high_memory);
  79. }
  80. #else
  81. static void print_vm_layout(void) { }
  82. #endif /* CONFIG_DEBUG_VM */
  83. void __init mem_init(void)
  84. {
  85. #ifdef CONFIG_FLATMEM
  86. BUG_ON(!mem_map);
  87. #endif /* CONFIG_FLATMEM */
  88. high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
  89. memblock_free_all();
  90. mem_init_print_info(NULL);
  91. print_vm_layout();
  92. }
  93. #ifdef CONFIG_BLK_DEV_INITRD
  94. static void __init setup_initrd(void)
  95. {
  96. phys_addr_t start;
  97. unsigned long size;
  98. /* Ignore the virtul address computed during device tree parsing */
  99. initrd_start = initrd_end = 0;
  100. if (!phys_initrd_size)
  101. return;
  102. /*
  103. * Round the memory region to page boundaries as per free_initrd_mem()
  104. * This allows us to detect whether the pages overlapping the initrd
  105. * are in use, but more importantly, reserves the entire set of pages
  106. * as we don't want these pages allocated for other purposes.
  107. */
  108. start = round_down(phys_initrd_start, PAGE_SIZE);
  109. size = phys_initrd_size + (phys_initrd_start - start);
  110. size = round_up(size, PAGE_SIZE);
  111. if (!memblock_is_region_memory(start, size)) {
  112. pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region",
  113. (u64)start, size);
  114. goto disable;
  115. }
  116. if (memblock_is_region_reserved(start, size)) {
  117. pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n",
  118. (u64)start, size);
  119. goto disable;
  120. }
  121. memblock_reserve(start, size);
  122. /* Now convert initrd to virtual addresses */
  123. initrd_start = (unsigned long)__va(phys_initrd_start);
  124. initrd_end = initrd_start + phys_initrd_size;
  125. initrd_below_start_ok = 1;
  126. pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
  127. (void *)(initrd_start), size);
  128. return;
  129. disable:
  130. pr_cont(" - disabling initrd\n");
  131. initrd_start = 0;
  132. initrd_end = 0;
  133. }
  134. #endif /* CONFIG_BLK_DEV_INITRD */
  135. void __init setup_bootmem(void)
  136. {
  137. phys_addr_t mem_start = 0;
  138. phys_addr_t start, dram_end, end = 0;
  139. phys_addr_t vmlinux_end = __pa_symbol(&_end);
  140. phys_addr_t vmlinux_start = __pa_symbol(&_start);
  141. phys_addr_t max_mapped_addr = __pa(~(ulong)0);
  142. u64 i;
  143. /* Find the memory region containing the kernel */
  144. for_each_mem_range(i, &start, &end) {
  145. phys_addr_t size = end - start;
  146. if (!mem_start)
  147. mem_start = start;
  148. if (start <= vmlinux_start && vmlinux_end <= end)
  149. BUG_ON(size == 0);
  150. }
  151. /*
  152. * The maximal physical memory size is -PAGE_OFFSET.
  153. * Make sure that any memory beyond mem_start + (-PAGE_OFFSET) is removed
  154. * as it is unusable by kernel.
  155. */
  156. memblock_enforce_memory_limit(-PAGE_OFFSET);
  157. /* Reserve from the start of the kernel to the end of the kernel */
  158. memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
  159. dram_end = memblock_end_of_DRAM();
  160. /*
  161. * memblock allocator is not aware of the fact that last 4K bytes of
  162. * the addressable memory can not be mapped because of IS_ERR_VALUE
  163. * macro. Make sure that last 4k bytes are not usable by memblock
  164. * if end of dram is equal to maximum addressable memory.
  165. */
  166. if (max_mapped_addr == (dram_end - 1))
  167. memblock_set_current_limit(max_mapped_addr - 4096);
  168. max_pfn = PFN_DOWN(dram_end);
  169. max_low_pfn = max_pfn;
  170. set_max_mapnr(max_low_pfn);
  171. #ifdef CONFIG_BLK_DEV_INITRD
  172. setup_initrd();
  173. #endif /* CONFIG_BLK_DEV_INITRD */
  174. /*
  175. * Avoid using early_init_fdt_reserve_self() since __pa() does
  176. * not work for DTB pointers that are fixmap addresses
  177. */
  178. memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
  179. early_init_fdt_scan_reserved_mem();
  180. memblock_allow_resize();
  181. memblock_dump_all();
  182. }
  183. #ifdef CONFIG_MMU
  184. static struct pt_alloc_ops pt_ops;
  185. unsigned long va_pa_offset;
  186. EXPORT_SYMBOL(va_pa_offset);
  187. unsigned long pfn_base;
  188. EXPORT_SYMBOL(pfn_base);
  189. pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
  190. pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
  191. pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
  192. pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
  193. void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
  194. {
  195. unsigned long addr = __fix_to_virt(idx);
  196. pte_t *ptep;
  197. BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
  198. ptep = &fixmap_pte[pte_index(addr)];
  199. if (pgprot_val(prot))
  200. set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
  201. else
  202. pte_clear(&init_mm, addr, ptep);
  203. local_flush_tlb_page(addr);
  204. }
  205. static inline pte_t *__init get_pte_virt_early(phys_addr_t pa)
  206. {
  207. return (pte_t *)((uintptr_t)pa);
  208. }
  209. static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa)
  210. {
  211. clear_fixmap(FIX_PTE);
  212. return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
  213. }
  214. static inline pte_t *get_pte_virt_late(phys_addr_t pa)
  215. {
  216. return (pte_t *) __va(pa);
  217. }
  218. static inline phys_addr_t __init alloc_pte_early(uintptr_t va)
  219. {
  220. /*
  221. * We only create PMD or PGD early mappings so we
  222. * should never reach here with MMU disabled.
  223. */
  224. BUG();
  225. }
  226. static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va)
  227. {
  228. return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
  229. }
  230. static phys_addr_t alloc_pte_late(uintptr_t va)
  231. {
  232. unsigned long vaddr;
  233. vaddr = __get_free_page(GFP_KERNEL);
  234. if (!vaddr || !pgtable_pte_page_ctor(virt_to_page(vaddr)))
  235. BUG();
  236. return __pa(vaddr);
  237. }
  238. static void __init create_pte_mapping(pte_t *ptep,
  239. uintptr_t va, phys_addr_t pa,
  240. phys_addr_t sz, pgprot_t prot)
  241. {
  242. uintptr_t pte_idx = pte_index(va);
  243. BUG_ON(sz != PAGE_SIZE);
  244. if (pte_none(ptep[pte_idx]))
  245. ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot);
  246. }
  247. #ifndef __PAGETABLE_PMD_FOLDED
  248. pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
  249. pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
  250. pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
  251. pmd_t early_dtb_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
  252. static pmd_t *__init get_pmd_virt_early(phys_addr_t pa)
  253. {
  254. /* Before MMU is enabled */
  255. return (pmd_t *)((uintptr_t)pa);
  256. }
  257. static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa)
  258. {
  259. clear_fixmap(FIX_PMD);
  260. return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
  261. }
  262. static pmd_t *get_pmd_virt_late(phys_addr_t pa)
  263. {
  264. return (pmd_t *) __va(pa);
  265. }
  266. static phys_addr_t __init alloc_pmd_early(uintptr_t va)
  267. {
  268. BUG_ON((va - PAGE_OFFSET) >> PGDIR_SHIFT);
  269. return (uintptr_t)early_pmd;
  270. }
  271. static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va)
  272. {
  273. return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
  274. }
  275. static phys_addr_t alloc_pmd_late(uintptr_t va)
  276. {
  277. unsigned long vaddr;
  278. vaddr = __get_free_page(GFP_KERNEL);
  279. BUG_ON(!vaddr);
  280. return __pa(vaddr);
  281. }
  282. static void __init create_pmd_mapping(pmd_t *pmdp,
  283. uintptr_t va, phys_addr_t pa,
  284. phys_addr_t sz, pgprot_t prot)
  285. {
  286. pte_t *ptep;
  287. phys_addr_t pte_phys;
  288. uintptr_t pmd_idx = pmd_index(va);
  289. if (sz == PMD_SIZE) {
  290. if (pmd_none(pmdp[pmd_idx]))
  291. pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot);
  292. return;
  293. }
  294. if (pmd_none(pmdp[pmd_idx])) {
  295. pte_phys = pt_ops.alloc_pte(va);
  296. pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
  297. ptep = pt_ops.get_pte_virt(pte_phys);
  298. memset(ptep, 0, PAGE_SIZE);
  299. } else {
  300. pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx]));
  301. ptep = pt_ops.get_pte_virt(pte_phys);
  302. }
  303. create_pte_mapping(ptep, va, pa, sz, prot);
  304. }
  305. #define pgd_next_t pmd_t
  306. #define alloc_pgd_next(__va) pt_ops.alloc_pmd(__va)
  307. #define get_pgd_next_virt(__pa) pt_ops.get_pmd_virt(__pa)
  308. #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
  309. create_pmd_mapping(__nextp, __va, __pa, __sz, __prot)
  310. #define fixmap_pgd_next fixmap_pmd
  311. #else
  312. #define pgd_next_t pte_t
  313. #define alloc_pgd_next(__va) pt_ops.alloc_pte(__va)
  314. #define get_pgd_next_virt(__pa) pt_ops.get_pte_virt(__pa)
  315. #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
  316. create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
  317. #define fixmap_pgd_next fixmap_pte
  318. #endif
  319. void __init create_pgd_mapping(pgd_t *pgdp,
  320. uintptr_t va, phys_addr_t pa,
  321. phys_addr_t sz, pgprot_t prot)
  322. {
  323. pgd_next_t *nextp;
  324. phys_addr_t next_phys;
  325. uintptr_t pgd_idx = pgd_index(va);
  326. if (sz == PGDIR_SIZE) {
  327. if (pgd_val(pgdp[pgd_idx]) == 0)
  328. pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot);
  329. return;
  330. }
  331. if (pgd_val(pgdp[pgd_idx]) == 0) {
  332. next_phys = alloc_pgd_next(va);
  333. pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
  334. nextp = get_pgd_next_virt(next_phys);
  335. memset(nextp, 0, PAGE_SIZE);
  336. } else {
  337. next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx]));
  338. nextp = get_pgd_next_virt(next_phys);
  339. }
  340. create_pgd_next_mapping(nextp, va, pa, sz, prot);
  341. }
  342. static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
  343. {
  344. /* Upgrade to PMD_SIZE mappings whenever possible */
  345. if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1)))
  346. return PAGE_SIZE;
  347. return PMD_SIZE;
  348. }
  349. /*
  350. * setup_vm() is called from head.S with MMU-off.
  351. *
  352. * Following requirements should be honoured for setup_vm() to work
  353. * correctly:
  354. * 1) It should use PC-relative addressing for accessing kernel symbols.
  355. * To achieve this we always use GCC cmodel=medany.
  356. * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
  357. * so disable compiler instrumentation when FTRACE is enabled.
  358. *
  359. * Currently, the above requirements are honoured by using custom CFLAGS
  360. * for init.o in mm/Makefile.
  361. */
  362. #ifndef __riscv_cmodel_medany
  363. #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
  364. #endif
  365. asmlinkage void __init setup_vm(uintptr_t dtb_pa)
  366. {
  367. uintptr_t va, pa, end_va;
  368. uintptr_t load_pa = (uintptr_t)(&_start);
  369. uintptr_t load_sz = (uintptr_t)(&_end) - load_pa;
  370. uintptr_t map_size;
  371. #ifndef __PAGETABLE_PMD_FOLDED
  372. pmd_t fix_bmap_spmd, fix_bmap_epmd;
  373. #endif
  374. va_pa_offset = PAGE_OFFSET - load_pa;
  375. pfn_base = PFN_DOWN(load_pa);
  376. /*
  377. * Enforce boot alignment requirements of RV32 and
  378. * RV64 by only allowing PMD or PGD mappings.
  379. */
  380. map_size = PMD_SIZE;
  381. /* Sanity check alignment and size */
  382. BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
  383. BUG_ON((load_pa % map_size) != 0);
  384. pt_ops.alloc_pte = alloc_pte_early;
  385. pt_ops.get_pte_virt = get_pte_virt_early;
  386. #ifndef __PAGETABLE_PMD_FOLDED
  387. pt_ops.alloc_pmd = alloc_pmd_early;
  388. pt_ops.get_pmd_virt = get_pmd_virt_early;
  389. #endif
  390. /* Setup early PGD for fixmap */
  391. create_pgd_mapping(early_pg_dir, FIXADDR_START,
  392. (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
  393. #ifndef __PAGETABLE_PMD_FOLDED
  394. /* Setup fixmap PMD */
  395. create_pmd_mapping(fixmap_pmd, FIXADDR_START,
  396. (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
  397. /* Setup trampoline PGD and PMD */
  398. create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
  399. (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE);
  400. create_pmd_mapping(trampoline_pmd, PAGE_OFFSET,
  401. load_pa, PMD_SIZE, PAGE_KERNEL_EXEC);
  402. #else
  403. /* Setup trampoline PGD */
  404. create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
  405. load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC);
  406. #endif
  407. /*
  408. * Setup early PGD covering entire kernel which will allows
  409. * us to reach paging_init(). We map all memory banks later
  410. * in setup_vm_final() below.
  411. */
  412. end_va = PAGE_OFFSET + load_sz;
  413. for (va = PAGE_OFFSET; va < end_va; va += map_size)
  414. create_pgd_mapping(early_pg_dir, va,
  415. load_pa + (va - PAGE_OFFSET),
  416. map_size, PAGE_KERNEL_EXEC);
  417. #ifndef __PAGETABLE_PMD_FOLDED
  418. /* Setup early PMD for DTB */
  419. create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,
  420. (uintptr_t)early_dtb_pmd, PGDIR_SIZE, PAGE_TABLE);
  421. /* Create two consecutive PMD mappings for FDT early scan */
  422. pa = dtb_pa & ~(PMD_SIZE - 1);
  423. create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA,
  424. pa, PMD_SIZE, PAGE_KERNEL);
  425. create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA + PMD_SIZE,
  426. pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL);
  427. dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PMD_SIZE - 1));
  428. #else
  429. /* Create two consecutive PGD mappings for FDT early scan */
  430. pa = dtb_pa & ~(PGDIR_SIZE - 1);
  431. create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,
  432. pa, PGDIR_SIZE, PAGE_KERNEL);
  433. create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA + PGDIR_SIZE,
  434. pa + PGDIR_SIZE, PGDIR_SIZE, PAGE_KERNEL);
  435. dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PGDIR_SIZE - 1));
  436. #endif
  437. dtb_early_pa = dtb_pa;
  438. /*
  439. * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
  440. * range can not span multiple pmds.
  441. */
  442. BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
  443. != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
  444. #ifndef __PAGETABLE_PMD_FOLDED
  445. /*
  446. * Early ioremap fixmap is already created as it lies within first 2MB
  447. * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END
  448. * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn
  449. * the user if not.
  450. */
  451. fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];
  452. fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];
  453. if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {
  454. WARN_ON(1);
  455. pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
  456. pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));
  457. pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
  458. fix_to_virt(FIX_BTMAP_BEGIN));
  459. pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
  460. fix_to_virt(FIX_BTMAP_END));
  461. pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
  462. pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
  463. }
  464. #endif
  465. }
  466. static void __init setup_vm_final(void)
  467. {
  468. uintptr_t va, map_size;
  469. phys_addr_t pa, start, end;
  470. u64 i;
  471. /**
  472. * MMU is enabled at this point. But page table setup is not complete yet.
  473. * fixmap page table alloc functions should be used at this point
  474. */
  475. pt_ops.alloc_pte = alloc_pte_fixmap;
  476. pt_ops.get_pte_virt = get_pte_virt_fixmap;
  477. #ifndef __PAGETABLE_PMD_FOLDED
  478. pt_ops.alloc_pmd = alloc_pmd_fixmap;
  479. pt_ops.get_pmd_virt = get_pmd_virt_fixmap;
  480. #endif
  481. /* Setup swapper PGD for fixmap */
  482. create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
  483. __pa_symbol(fixmap_pgd_next),
  484. PGDIR_SIZE, PAGE_TABLE);
  485. /* Map all memory banks */
  486. for_each_mem_range(i, &start, &end) {
  487. if (start >= end)
  488. break;
  489. if (start <= __pa(PAGE_OFFSET) &&
  490. __pa(PAGE_OFFSET) < end)
  491. start = __pa(PAGE_OFFSET);
  492. map_size = best_map_size(start, end - start);
  493. for (pa = start; pa < end; pa += map_size) {
  494. va = (uintptr_t)__va(pa);
  495. create_pgd_mapping(swapper_pg_dir, va, pa,
  496. map_size, PAGE_KERNEL_EXEC);
  497. }
  498. }
  499. /* Clear fixmap PTE and PMD mappings */
  500. clear_fixmap(FIX_PTE);
  501. clear_fixmap(FIX_PMD);
  502. /* Move to swapper page table */
  503. csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
  504. local_flush_tlb_all();
  505. /* generic page allocation functions must be used to setup page table */
  506. pt_ops.alloc_pte = alloc_pte_late;
  507. pt_ops.get_pte_virt = get_pte_virt_late;
  508. #ifndef __PAGETABLE_PMD_FOLDED
  509. pt_ops.alloc_pmd = alloc_pmd_late;
  510. pt_ops.get_pmd_virt = get_pmd_virt_late;
  511. #endif
  512. }
  513. #else
  514. asmlinkage void __init setup_vm(uintptr_t dtb_pa)
  515. {
  516. #ifdef CONFIG_BUILTIN_DTB
  517. dtb_early_va = soc_lookup_builtin_dtb();
  518. if (!dtb_early_va) {
  519. /* Fallback to first available DTS */
  520. dtb_early_va = (void *) __dtb_start;
  521. }
  522. #else
  523. dtb_early_va = (void *)dtb_pa;
  524. #endif
  525. dtb_early_pa = dtb_pa;
  526. }
  527. static inline void setup_vm_final(void)
  528. {
  529. }
  530. #endif /* CONFIG_MMU */
  531. #ifdef CONFIG_STRICT_KERNEL_RWX
  532. void mark_rodata_ro(void)
  533. {
  534. unsigned long text_start = (unsigned long)_text;
  535. unsigned long text_end = (unsigned long)_etext;
  536. unsigned long rodata_start = (unsigned long)__start_rodata;
  537. unsigned long data_start = (unsigned long)_data;
  538. unsigned long max_low = (unsigned long)(__va(PFN_PHYS(max_low_pfn)));
  539. set_memory_ro(text_start, (text_end - text_start) >> PAGE_SHIFT);
  540. set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
  541. set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
  542. set_memory_nx(data_start, (max_low - data_start) >> PAGE_SHIFT);
  543. debug_checkwx();
  544. }
  545. #endif
  546. #ifdef CONFIG_KEXEC_CORE
  547. /*
  548. * reserve_crashkernel() - reserves memory for crash kernel
  549. *
  550. * This function reserves memory area given in "crashkernel=" kernel command
  551. * line parameter. The memory reserved is used by dump capture kernel when
  552. * primary kernel is crashing.
  553. */
  554. static void __init reserve_crashkernel(void)
  555. {
  556. unsigned long long crash_base = 0;
  557. unsigned long long crash_size = 0;
  558. unsigned long search_start = memblock_start_of_DRAM();
  559. unsigned long search_end = memblock_end_of_DRAM();
  560. int ret = 0;
  561. /*
  562. * Don't reserve a region for a crash kernel on a crash kernel
  563. * since it doesn't make much sense and we have limited memory
  564. * resources.
  565. */
  566. #ifdef CONFIG_CRASH_DUMP
  567. if (is_kdump_kernel()) {
  568. pr_info("crashkernel: ignoring reservation request\n");
  569. return;
  570. }
  571. #endif
  572. ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
  573. &crash_size, &crash_base);
  574. if (ret || !crash_size)
  575. return;
  576. crash_size = PAGE_ALIGN(crash_size);
  577. if (crash_base == 0) {
  578. /*
  579. * Current riscv boot protocol requires 2MB alignment for
  580. * RV64 and 4MB alignment for RV32 (hugepage size)
  581. */
  582. crash_base = memblock_find_in_range(search_start, search_end,
  583. crash_size, PMD_SIZE);
  584. if (crash_base == 0) {
  585. pr_warn("crashkernel: couldn't allocate %lldKB\n",
  586. crash_size >> 10);
  587. return;
  588. }
  589. } else {
  590. /* User specifies base address explicitly. */
  591. if (!memblock_is_region_memory(crash_base, crash_size)) {
  592. pr_warn("crashkernel: requested region is not memory\n");
  593. return;
  594. }
  595. if (memblock_is_region_reserved(crash_base, crash_size)) {
  596. pr_warn("crashkernel: requested region is reserved\n");
  597. return;
  598. }
  599. if (!IS_ALIGNED(crash_base, PMD_SIZE)) {
  600. pr_warn("crashkernel: requested region is misaligned\n");
  601. return;
  602. }
  603. }
  604. memblock_reserve(crash_base, crash_size);
  605. pr_info("crashkernel: reserved 0x%016llx - 0x%016llx (%lld MB)\n",
  606. crash_base, crash_base + crash_size, crash_size >> 20);
  607. crashk_res.start = crash_base;
  608. crashk_res.end = crash_base + crash_size - 1;
  609. }
  610. #endif /* CONFIG_KEXEC_CORE */
  611. #ifdef CONFIG_CRASH_DUMP
  612. /*
  613. * We keep track of the ELF core header of the crashed
  614. * kernel with a reserved-memory region with compatible
  615. * string "linux,elfcorehdr". Here we register a callback
  616. * to populate elfcorehdr_addr/size when this region is
  617. * present. Note that this region will be marked as
  618. * reserved once we call early_init_fdt_scan_reserved_mem()
  619. * later on.
  620. */
  621. static int elfcore_hdr_setup(struct reserved_mem *rmem)
  622. {
  623. elfcorehdr_addr = rmem->base;
  624. elfcorehdr_size = rmem->size;
  625. return 0;
  626. }
  627. RESERVEDMEM_OF_DECLARE(elfcorehdr, "linux,elfcorehdr", elfcore_hdr_setup);
  628. #endif
  629. void __init paging_init(void)
  630. {
  631. setup_vm_final();
  632. sparse_init();
  633. setup_zero_page();
  634. zone_sizes_init();
  635. #ifdef CONFIG_KEXEC_CORE
  636. reserve_crashkernel();
  637. #endif
  638. }
  639. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  640. int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
  641. struct vmem_altmap *altmap)
  642. {
  643. return vmemmap_populate_basepages(start, end, node, NULL);
  644. }
  645. #endif