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