mem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PowerPC version
  4. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  5. *
  6. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  7. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  8. * Copyright (C) 1996 Paul Mackerras
  9. * PPC44x/36-bit changes by Matt Porter (mporter@mvista.com)
  10. *
  11. * Derived from "arch/i386/mm/init.c"
  12. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  13. */
  14. #include <linux/memblock.h>
  15. #include <linux/highmem.h>
  16. #include <linux/suspend.h>
  17. #include <linux/dma-direct.h>
  18. #include <linux/execmem.h>
  19. #include <linux/vmalloc.h>
  20. #include <asm/swiotlb.h>
  21. #include <asm/machdep.h>
  22. #include <asm/rtas.h>
  23. #include <asm/kasan.h>
  24. #include <asm/svm.h>
  25. #include <asm/mmzone.h>
  26. #include <asm/ftrace.h>
  27. #include <asm/code-patching.h>
  28. #include <asm/setup.h>
  29. #include <asm/fixmap.h>
  30. #include <mm/mmu_decl.h>
  31. unsigned long long memory_limit __initdata;
  32. unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
  33. EXPORT_SYMBOL(empty_zero_page);
  34. pgprot_t __phys_mem_access_prot(unsigned long pfn, unsigned long size,
  35. pgprot_t vma_prot)
  36. {
  37. if (ppc_md.phys_mem_access_prot)
  38. return ppc_md.phys_mem_access_prot(pfn, size, vma_prot);
  39. if (!page_is_ram(pfn))
  40. vma_prot = pgprot_noncached(vma_prot);
  41. return vma_prot;
  42. }
  43. EXPORT_SYMBOL(__phys_mem_access_prot);
  44. #ifdef CONFIG_MEMORY_HOTPLUG
  45. static DEFINE_MUTEX(linear_mapping_mutex);
  46. #ifdef CONFIG_NUMA
  47. int memory_add_physaddr_to_nid(u64 start)
  48. {
  49. return hot_add_scn_to_nid(start);
  50. }
  51. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  52. #endif
  53. int __weak create_section_mapping(unsigned long start, unsigned long end,
  54. int nid, pgprot_t prot)
  55. {
  56. return -ENODEV;
  57. }
  58. int __weak remove_section_mapping(unsigned long start, unsigned long end)
  59. {
  60. return -ENODEV;
  61. }
  62. int __ref arch_create_linear_mapping(int nid, u64 start, u64 size,
  63. struct mhp_params *params)
  64. {
  65. int rc;
  66. start = (unsigned long)__va(start);
  67. mutex_lock(&linear_mapping_mutex);
  68. rc = create_section_mapping(start, start + size, nid,
  69. params->pgprot);
  70. mutex_unlock(&linear_mapping_mutex);
  71. if (rc) {
  72. pr_warn("Unable to create linear mapping for 0x%llx..0x%llx: %d\n",
  73. start, start + size, rc);
  74. return -EFAULT;
  75. }
  76. return 0;
  77. }
  78. void __ref arch_remove_linear_mapping(u64 start, u64 size)
  79. {
  80. int ret;
  81. /* Remove htab bolted mappings for this section of memory */
  82. start = (unsigned long)__va(start);
  83. mutex_lock(&linear_mapping_mutex);
  84. ret = remove_section_mapping(start, start + size);
  85. mutex_unlock(&linear_mapping_mutex);
  86. if (ret)
  87. pr_warn("Unable to remove linear mapping for 0x%llx..0x%llx: %d\n",
  88. start, start + size, ret);
  89. /* Ensure all vmalloc mappings are flushed in case they also
  90. * hit that section of memory
  91. */
  92. vm_unmap_aliases();
  93. }
  94. /*
  95. * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
  96. * updating.
  97. */
  98. static void update_end_of_memory_vars(u64 start, u64 size)
  99. {
  100. unsigned long end_pfn = PFN_UP(start + size);
  101. if (end_pfn > max_pfn) {
  102. max_pfn = end_pfn;
  103. max_low_pfn = end_pfn;
  104. high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
  105. }
  106. }
  107. int __ref add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
  108. struct mhp_params *params)
  109. {
  110. int ret;
  111. ret = __add_pages(nid, start_pfn, nr_pages, params);
  112. if (ret)
  113. return ret;
  114. /* update max_pfn, max_low_pfn and high_memory */
  115. update_end_of_memory_vars(start_pfn << PAGE_SHIFT,
  116. nr_pages << PAGE_SHIFT);
  117. return ret;
  118. }
  119. int __ref arch_add_memory(int nid, u64 start, u64 size,
  120. struct mhp_params *params)
  121. {
  122. unsigned long start_pfn = start >> PAGE_SHIFT;
  123. unsigned long nr_pages = size >> PAGE_SHIFT;
  124. int rc;
  125. rc = arch_create_linear_mapping(nid, start, size, params);
  126. if (rc)
  127. return rc;
  128. rc = add_pages(nid, start_pfn, nr_pages, params);
  129. if (rc)
  130. arch_remove_linear_mapping(start, size);
  131. return rc;
  132. }
  133. void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
  134. {
  135. unsigned long start_pfn = start >> PAGE_SHIFT;
  136. unsigned long nr_pages = size >> PAGE_SHIFT;
  137. __remove_pages(start_pfn, nr_pages, altmap);
  138. arch_remove_linear_mapping(start, size);
  139. }
  140. #endif
  141. #ifndef CONFIG_NUMA
  142. void __init mem_topology_setup(void)
  143. {
  144. max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
  145. min_low_pfn = MEMORY_START >> PAGE_SHIFT;
  146. #ifdef CONFIG_HIGHMEM
  147. max_low_pfn = lowmem_end_addr >> PAGE_SHIFT;
  148. #endif
  149. /* Place all memblock_regions in the same node and merge contiguous
  150. * memblock_regions
  151. */
  152. memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
  153. }
  154. void __init initmem_init(void)
  155. {
  156. sparse_init();
  157. }
  158. /* mark pages that don't exist as nosave */
  159. static int __init mark_nonram_nosave(void)
  160. {
  161. unsigned long spfn, epfn, prev = 0;
  162. int i;
  163. for_each_mem_pfn_range(i, MAX_NUMNODES, &spfn, &epfn, NULL) {
  164. if (prev && prev < spfn)
  165. register_nosave_region(prev, spfn);
  166. prev = epfn;
  167. }
  168. return 0;
  169. }
  170. #else /* CONFIG_NUMA */
  171. static int __init mark_nonram_nosave(void)
  172. {
  173. return 0;
  174. }
  175. #endif
  176. /*
  177. * Zones usage:
  178. *
  179. * We setup ZONE_DMA to be 31-bits on all platforms and ZONE_NORMAL to be
  180. * everything else. GFP_DMA32 page allocations automatically fall back to
  181. * ZONE_DMA.
  182. *
  183. * By using 31-bit unconditionally, we can exploit zone_dma_limit to inform the
  184. * generic DMA mapping code. 32-bit only devices (if not handled by an IOMMU
  185. * anyway) will take a first dip into ZONE_NORMAL and get otherwise served by
  186. * ZONE_DMA.
  187. */
  188. static unsigned long max_zone_pfns[MAX_NR_ZONES];
  189. /*
  190. * paging_init() sets up the page tables - in fact we've already done this.
  191. */
  192. void __init paging_init(void)
  193. {
  194. unsigned long long total_ram = memblock_phys_mem_size();
  195. phys_addr_t top_of_ram = memblock_end_of_DRAM();
  196. int zone_dma_bits;
  197. #ifdef CONFIG_HIGHMEM
  198. unsigned long v = __fix_to_virt(FIX_KMAP_END);
  199. unsigned long end = __fix_to_virt(FIX_KMAP_BEGIN);
  200. for (; v < end; v += PAGE_SIZE)
  201. map_kernel_page(v, 0, __pgprot(0)); /* XXX gross */
  202. map_kernel_page(PKMAP_BASE, 0, __pgprot(0)); /* XXX gross */
  203. pkmap_page_table = virt_to_kpte(PKMAP_BASE);
  204. #endif /* CONFIG_HIGHMEM */
  205. printk(KERN_DEBUG "Top of RAM: 0x%llx, Total RAM: 0x%llx\n",
  206. (unsigned long long)top_of_ram, total_ram);
  207. printk(KERN_DEBUG "Memory hole size: %ldMB\n",
  208. (long int)((top_of_ram - total_ram) >> 20));
  209. /*
  210. * Allow 30-bit DMA for very limited Broadcom wifi chips on many
  211. * powerbooks.
  212. */
  213. if (IS_ENABLED(CONFIG_PPC32))
  214. zone_dma_bits = 30;
  215. else
  216. zone_dma_bits = 31;
  217. zone_dma_limit = DMA_BIT_MASK(zone_dma_bits);
  218. #ifdef CONFIG_ZONE_DMA
  219. max_zone_pfns[ZONE_DMA] = min(max_low_pfn,
  220. 1UL << (zone_dma_bits - PAGE_SHIFT));
  221. #endif
  222. max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
  223. #ifdef CONFIG_HIGHMEM
  224. max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
  225. #endif
  226. free_area_init(max_zone_pfns);
  227. mark_nonram_nosave();
  228. }
  229. void __init mem_init(void)
  230. {
  231. /*
  232. * book3s is limited to 16 page sizes due to encoding this in
  233. * a 4-bit field for slices.
  234. */
  235. BUILD_BUG_ON(MMU_PAGE_COUNT > 16);
  236. #ifdef CONFIG_SWIOTLB
  237. /*
  238. * Some platforms (e.g. 85xx) limit DMA-able memory way below
  239. * 4G. We force memblock to bottom-up mode to ensure that the
  240. * memory allocated in swiotlb_init() is DMA-able.
  241. * As it's the last memblock allocation, no need to reset it
  242. * back to to-down.
  243. */
  244. memblock_set_bottom_up(true);
  245. swiotlb_init(ppc_swiotlb_enable, ppc_swiotlb_flags);
  246. #endif
  247. kasan_late_init();
  248. memblock_free_all();
  249. #ifdef CONFIG_HIGHMEM
  250. {
  251. unsigned long pfn, highmem_mapnr;
  252. highmem_mapnr = lowmem_end_addr >> PAGE_SHIFT;
  253. for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
  254. phys_addr_t paddr = (phys_addr_t)pfn << PAGE_SHIFT;
  255. struct page *page = pfn_to_page(pfn);
  256. if (memblock_is_memory(paddr) && !memblock_is_reserved(paddr))
  257. free_highmem_page(page);
  258. }
  259. }
  260. #endif /* CONFIG_HIGHMEM */
  261. #if defined(CONFIG_PPC_E500) && !defined(CONFIG_SMP)
  262. /*
  263. * If smp is enabled, next_tlbcam_idx is initialized in the cpu up
  264. * functions.... do it here for the non-smp case.
  265. */
  266. per_cpu(next_tlbcam_idx, smp_processor_id()) =
  267. (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) - 1;
  268. #endif
  269. #ifdef CONFIG_PPC32
  270. pr_info("Kernel virtual memory layout:\n");
  271. #ifdef CONFIG_KASAN
  272. pr_info(" * 0x%08lx..0x%08lx : kasan shadow mem\n",
  273. KASAN_SHADOW_START, KASAN_SHADOW_END);
  274. #endif
  275. pr_info(" * 0x%08lx..0x%08lx : fixmap\n", FIXADDR_START, FIXADDR_TOP);
  276. #ifdef CONFIG_HIGHMEM
  277. pr_info(" * 0x%08lx..0x%08lx : highmem PTEs\n",
  278. PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP));
  279. #endif /* CONFIG_HIGHMEM */
  280. if (ioremap_bot != IOREMAP_TOP)
  281. pr_info(" * 0x%08lx..0x%08lx : early ioremap\n",
  282. ioremap_bot, IOREMAP_TOP);
  283. pr_info(" * 0x%08lx..0x%08lx : vmalloc & ioremap\n",
  284. VMALLOC_START, VMALLOC_END);
  285. #ifdef MODULES_VADDR
  286. pr_info(" * 0x%08lx..0x%08lx : modules\n",
  287. MODULES_VADDR, MODULES_END);
  288. #endif
  289. #endif /* CONFIG_PPC32 */
  290. }
  291. void free_initmem(void)
  292. {
  293. ppc_md.progress = ppc_printk_progress;
  294. mark_initmem_nx();
  295. free_initmem_default(POISON_FREE_INITMEM);
  296. ftrace_free_init_tramp();
  297. }
  298. /*
  299. * System memory should not be in /proc/iomem but various tools expect it
  300. * (eg kdump).
  301. */
  302. static int __init add_system_ram_resources(void)
  303. {
  304. phys_addr_t start, end;
  305. u64 i;
  306. for_each_mem_range(i, &start, &end) {
  307. struct resource *res;
  308. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  309. WARN_ON(!res);
  310. if (res) {
  311. res->name = "System RAM";
  312. res->start = start;
  313. /*
  314. * In memblock, end points to the first byte after
  315. * the range while in resourses, end points to the
  316. * last byte in the range.
  317. */
  318. res->end = end - 1;
  319. res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
  320. WARN_ON(request_resource(&iomem_resource, res) < 0);
  321. }
  322. }
  323. return 0;
  324. }
  325. subsys_initcall(add_system_ram_resources);
  326. #ifdef CONFIG_STRICT_DEVMEM
  327. /*
  328. * devmem_is_allowed(): check to see if /dev/mem access to a certain address
  329. * is valid. The argument is a physical page number.
  330. *
  331. * Access has to be given to non-kernel-ram areas as well, these contain the
  332. * PCI mmio resources as well as potential bios/acpi data regions.
  333. */
  334. int devmem_is_allowed(unsigned long pfn)
  335. {
  336. if (page_is_rtas_user_buf(pfn))
  337. return 1;
  338. if (iomem_is_exclusive(PFN_PHYS(pfn)))
  339. return 0;
  340. if (!page_is_ram(pfn))
  341. return 1;
  342. return 0;
  343. }
  344. #endif /* CONFIG_STRICT_DEVMEM */
  345. /*
  346. * This is defined in kernel/resource.c but only powerpc needs to export it, for
  347. * the EHEA driver. Drop this when drivers/net/ethernet/ibm/ehea is removed.
  348. */
  349. EXPORT_SYMBOL_GPL(walk_system_ram_range);
  350. #ifdef CONFIG_EXECMEM
  351. static struct execmem_info execmem_info __ro_after_init;
  352. #if defined(CONFIG_PPC_8xx) || defined(CONFIG_PPC_BOOK3S_603)
  353. static void prealloc_execmem_pgtable(void)
  354. {
  355. unsigned long va;
  356. for (va = ALIGN_DOWN(MODULES_VADDR, PGDIR_SIZE); va < MODULES_END; va += PGDIR_SIZE)
  357. pte_alloc_kernel(pmd_off_k(va), va);
  358. }
  359. #else
  360. static void prealloc_execmem_pgtable(void) { }
  361. #endif
  362. struct execmem_info __init *execmem_arch_setup(void)
  363. {
  364. pgprot_t kprobes_prot = strict_module_rwx_enabled() ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
  365. pgprot_t prot = strict_module_rwx_enabled() ? PAGE_KERNEL : PAGE_KERNEL_EXEC;
  366. unsigned long fallback_start = 0, fallback_end = 0;
  367. unsigned long start, end;
  368. /*
  369. * BOOK3S_32 and 8xx define MODULES_VADDR for text allocations and
  370. * allow allocating data in the entire vmalloc space
  371. */
  372. #ifdef MODULES_VADDR
  373. unsigned long limit = (unsigned long)_etext - SZ_32M;
  374. BUILD_BUG_ON(TASK_SIZE > MODULES_VADDR);
  375. /* First try within 32M limit from _etext to avoid branch trampolines */
  376. if (MODULES_VADDR < PAGE_OFFSET && MODULES_END > limit) {
  377. start = limit;
  378. fallback_start = MODULES_VADDR;
  379. fallback_end = MODULES_END;
  380. } else {
  381. start = MODULES_VADDR;
  382. }
  383. end = MODULES_END;
  384. #else
  385. start = VMALLOC_START;
  386. end = VMALLOC_END;
  387. #endif
  388. prealloc_execmem_pgtable();
  389. execmem_info = (struct execmem_info){
  390. .ranges = {
  391. [EXECMEM_DEFAULT] = {
  392. .start = start,
  393. .end = end,
  394. .pgprot = prot,
  395. .alignment = 1,
  396. .fallback_start = fallback_start,
  397. .fallback_end = fallback_end,
  398. },
  399. [EXECMEM_KPROBES] = {
  400. .start = VMALLOC_START,
  401. .end = VMALLOC_END,
  402. .pgprot = kprobes_prot,
  403. .alignment = 1,
  404. },
  405. [EXECMEM_MODULE_DATA] = {
  406. .start = VMALLOC_START,
  407. .end = VMALLOC_END,
  408. .pgprot = PAGE_KERNEL,
  409. .alignment = 1,
  410. },
  411. },
  412. };
  413. return &execmem_info;
  414. }
  415. #endif /* CONFIG_EXECMEM */