mem.c 10 KB

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