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