xref: /openbmc/linux/arch/powerpc/mm/mem.c (revision c88773dc)
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/export.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/gfp.h>
21 #include <linux/types.h>
22 #include <linux/mm.h>
23 #include <linux/stddef.h>
24 #include <linux/init.h>
25 #include <linux/memblock.h>
26 #include <linux/highmem.h>
27 #include <linux/initrd.h>
28 #include <linux/pagemap.h>
29 #include <linux/suspend.h>
30 #include <linux/hugetlb.h>
31 #include <linux/slab.h>
32 #include <linux/vmalloc.h>
33 #include <linux/memremap.h>
34 #include <linux/dma-direct.h>
35 #include <linux/kprobes.h>
36 
37 #include <asm/prom.h>
38 #include <asm/io.h>
39 #include <asm/mmu_context.h>
40 #include <asm/mmu.h>
41 #include <asm/smp.h>
42 #include <asm/machdep.h>
43 #include <asm/btext.h>
44 #include <asm/tlb.h>
45 #include <asm/sections.h>
46 #include <asm/sparsemem.h>
47 #include <asm/vdso.h>
48 #include <asm/fixmap.h>
49 #include <asm/swiotlb.h>
50 #include <asm/rtas.h>
51 #include <asm/kasan.h>
52 
53 #include <mm/mmu_decl.h>
54 
55 #ifndef CPU_FTR_COHERENT_ICACHE
56 #define CPU_FTR_COHERENT_ICACHE	0	/* XXX for now */
57 #define CPU_FTR_NOEXECUTE	0
58 #endif
59 
60 unsigned long long memory_limit;
61 bool init_mem_is_free;
62 
63 #ifdef CONFIG_HIGHMEM
64 pte_t *kmap_pte;
65 EXPORT_SYMBOL(kmap_pte);
66 #endif
67 
68 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
69 			      unsigned long size, pgprot_t vma_prot)
70 {
71 	if (ppc_md.phys_mem_access_prot)
72 		return ppc_md.phys_mem_access_prot(file, pfn, size, vma_prot);
73 
74 	if (!page_is_ram(pfn))
75 		vma_prot = pgprot_noncached(vma_prot);
76 
77 	return vma_prot;
78 }
79 EXPORT_SYMBOL(phys_mem_access_prot);
80 
81 #ifdef CONFIG_MEMORY_HOTPLUG
82 
83 #ifdef CONFIG_NUMA
84 int memory_add_physaddr_to_nid(u64 start)
85 {
86 	return hot_add_scn_to_nid(start);
87 }
88 #endif
89 
90 int __weak create_section_mapping(unsigned long start, unsigned long end,
91 				  int nid, pgprot_t prot)
92 {
93 	return -ENODEV;
94 }
95 
96 int __weak remove_section_mapping(unsigned long start, unsigned long end)
97 {
98 	return -ENODEV;
99 }
100 
101 #define FLUSH_CHUNK_SIZE SZ_1G
102 /**
103  * flush_dcache_range_chunked(): Write any modified data cache blocks out to
104  * memory and invalidate them, in chunks of up to FLUSH_CHUNK_SIZE
105  * Does not invalidate the corresponding instruction cache blocks.
106  *
107  * @start: the start address
108  * @stop: the stop address (exclusive)
109  * @chunk: the max size of the chunks
110  */
111 static void flush_dcache_range_chunked(unsigned long start, unsigned long stop,
112 				       unsigned long chunk)
113 {
114 	unsigned long i;
115 
116 	for (i = start; i < stop; i += chunk) {
117 		flush_dcache_range(i, min(stop, i + chunk));
118 		cond_resched();
119 	}
120 }
121 
122 int __ref arch_add_memory(int nid, u64 start, u64 size,
123 			  struct mhp_params *params)
124 {
125 	unsigned long start_pfn = start >> PAGE_SHIFT;
126 	unsigned long nr_pages = size >> PAGE_SHIFT;
127 	int rc;
128 
129 	start = (unsigned long)__va(start);
130 	rc = create_section_mapping(start, start + size, nid,
131 				    params->pgprot);
132 	if (rc) {
133 		pr_warn("Unable to create mapping for hot added memory 0x%llx..0x%llx: %d\n",
134 			start, start + size, rc);
135 		return -EFAULT;
136 	}
137 
138 	return __add_pages(nid, start_pfn, nr_pages, params);
139 }
140 
141 void __ref arch_remove_memory(int nid, u64 start, u64 size,
142 			     struct vmem_altmap *altmap)
143 {
144 	unsigned long start_pfn = start >> PAGE_SHIFT;
145 	unsigned long nr_pages = size >> PAGE_SHIFT;
146 	int ret;
147 
148 	__remove_pages(start_pfn, nr_pages, altmap);
149 
150 	/* Remove htab bolted mappings for this section of memory */
151 	start = (unsigned long)__va(start);
152 	flush_dcache_range_chunked(start, start + size, FLUSH_CHUNK_SIZE);
153 
154 	ret = remove_section_mapping(start, start + size);
155 	WARN_ON_ONCE(ret);
156 
157 	/* Ensure all vmalloc mappings are flushed in case they also
158 	 * hit that section of memory
159 	 */
160 	vm_unmap_aliases();
161 }
162 #endif
163 
164 #ifndef CONFIG_NEED_MULTIPLE_NODES
165 void __init mem_topology_setup(void)
166 {
167 	max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
168 	min_low_pfn = MEMORY_START >> PAGE_SHIFT;
169 #ifdef CONFIG_HIGHMEM
170 	max_low_pfn = lowmem_end_addr >> PAGE_SHIFT;
171 #endif
172 
173 	/* Place all memblock_regions in the same node and merge contiguous
174 	 * memblock_regions
175 	 */
176 	memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0);
177 }
178 
179 void __init initmem_init(void)
180 {
181 	sparse_init();
182 }
183 
184 /* mark pages that don't exist as nosave */
185 static int __init mark_nonram_nosave(void)
186 {
187 	unsigned long spfn, epfn, prev = 0;
188 	int i;
189 
190 	for_each_mem_pfn_range(i, MAX_NUMNODES, &spfn, &epfn, NULL) {
191 		if (prev && prev < spfn)
192 			register_nosave_region(prev, spfn);
193 
194 		prev = epfn;
195 	}
196 
197 	return 0;
198 }
199 #else /* CONFIG_NEED_MULTIPLE_NODES */
200 static int __init mark_nonram_nosave(void)
201 {
202 	return 0;
203 }
204 #endif
205 
206 /*
207  * Zones usage:
208  *
209  * We setup ZONE_DMA to be 31-bits on all platforms and ZONE_NORMAL to be
210  * everything else. GFP_DMA32 page allocations automatically fall back to
211  * ZONE_DMA.
212  *
213  * By using 31-bit unconditionally, we can exploit zone_dma_bits to inform the
214  * generic DMA mapping code.  32-bit only devices (if not handled by an IOMMU
215  * anyway) will take a first dip into ZONE_NORMAL and get otherwise served by
216  * ZONE_DMA.
217  */
218 static unsigned long max_zone_pfns[MAX_NR_ZONES];
219 
220 /*
221  * paging_init() sets up the page tables - in fact we've already done this.
222  */
223 void __init paging_init(void)
224 {
225 	unsigned long long total_ram = memblock_phys_mem_size();
226 	phys_addr_t top_of_ram = memblock_end_of_DRAM();
227 
228 #ifdef CONFIG_HIGHMEM
229 	unsigned long v = __fix_to_virt(FIX_KMAP_END);
230 	unsigned long end = __fix_to_virt(FIX_KMAP_BEGIN);
231 
232 	for (; v < end; v += PAGE_SIZE)
233 		map_kernel_page(v, 0, __pgprot(0)); /* XXX gross */
234 
235 	map_kernel_page(PKMAP_BASE, 0, __pgprot(0));	/* XXX gross */
236 	pkmap_page_table = virt_to_kpte(PKMAP_BASE);
237 
238 	kmap_pte = virt_to_kpte(__fix_to_virt(FIX_KMAP_BEGIN));
239 #endif /* CONFIG_HIGHMEM */
240 
241 	printk(KERN_DEBUG "Top of RAM: 0x%llx, Total RAM: 0x%llx\n",
242 	       (unsigned long long)top_of_ram, total_ram);
243 	printk(KERN_DEBUG "Memory hole size: %ldMB\n",
244 	       (long int)((top_of_ram - total_ram) >> 20));
245 
246 	/*
247 	 * Allow 30-bit DMA for very limited Broadcom wifi chips on many
248 	 * powerbooks.
249 	 */
250 	if (IS_ENABLED(CONFIG_PPC32))
251 		zone_dma_bits = 30;
252 	else
253 		zone_dma_bits = 31;
254 
255 #ifdef CONFIG_ZONE_DMA
256 	max_zone_pfns[ZONE_DMA]	= min(max_low_pfn,
257 				      1UL << (zone_dma_bits - PAGE_SHIFT));
258 #endif
259 	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
260 #ifdef CONFIG_HIGHMEM
261 	max_zone_pfns[ZONE_HIGHMEM] = max_pfn;
262 #endif
263 
264 	free_area_init(max_zone_pfns);
265 
266 	mark_nonram_nosave();
267 }
268 
269 void __init mem_init(void)
270 {
271 	/*
272 	 * book3s is limited to 16 page sizes due to encoding this in
273 	 * a 4-bit field for slices.
274 	 */
275 	BUILD_BUG_ON(MMU_PAGE_COUNT > 16);
276 
277 #ifdef CONFIG_SWIOTLB
278 	/*
279 	 * Some platforms (e.g. 85xx) limit DMA-able memory way below
280 	 * 4G. We force memblock to bottom-up mode to ensure that the
281 	 * memory allocated in swiotlb_init() is DMA-able.
282 	 * As it's the last memblock allocation, no need to reset it
283 	 * back to to-down.
284 	 */
285 	memblock_set_bottom_up(true);
286 	swiotlb_init(0);
287 #endif
288 
289 	high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
290 	set_max_mapnr(max_pfn);
291 
292 	kasan_late_init();
293 
294 	memblock_free_all();
295 
296 #ifdef CONFIG_HIGHMEM
297 	{
298 		unsigned long pfn, highmem_mapnr;
299 
300 		highmem_mapnr = lowmem_end_addr >> PAGE_SHIFT;
301 		for (pfn = highmem_mapnr; pfn < max_mapnr; ++pfn) {
302 			phys_addr_t paddr = (phys_addr_t)pfn << PAGE_SHIFT;
303 			struct page *page = pfn_to_page(pfn);
304 			if (!memblock_is_reserved(paddr))
305 				free_highmem_page(page);
306 		}
307 	}
308 #endif /* CONFIG_HIGHMEM */
309 
310 #if defined(CONFIG_PPC_FSL_BOOK3E) && !defined(CONFIG_SMP)
311 	/*
312 	 * If smp is enabled, next_tlbcam_idx is initialized in the cpu up
313 	 * functions.... do it here for the non-smp case.
314 	 */
315 	per_cpu(next_tlbcam_idx, smp_processor_id()) =
316 		(mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) - 1;
317 #endif
318 
319 	mem_init_print_info(NULL);
320 #ifdef CONFIG_PPC32
321 	pr_info("Kernel virtual memory layout:\n");
322 #ifdef CONFIG_KASAN
323 	pr_info("  * 0x%08lx..0x%08lx  : kasan shadow mem\n",
324 		KASAN_SHADOW_START, KASAN_SHADOW_END);
325 #endif
326 	pr_info("  * 0x%08lx..0x%08lx  : fixmap\n", FIXADDR_START, FIXADDR_TOP);
327 #ifdef CONFIG_HIGHMEM
328 	pr_info("  * 0x%08lx..0x%08lx  : highmem PTEs\n",
329 		PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP));
330 #endif /* CONFIG_HIGHMEM */
331 	if (ioremap_bot != IOREMAP_TOP)
332 		pr_info("  * 0x%08lx..0x%08lx  : early ioremap\n",
333 			ioremap_bot, IOREMAP_TOP);
334 	pr_info("  * 0x%08lx..0x%08lx  : vmalloc & ioremap\n",
335 		VMALLOC_START, VMALLOC_END);
336 #endif /* CONFIG_PPC32 */
337 }
338 
339 void free_initmem(void)
340 {
341 	ppc_md.progress = ppc_printk_progress;
342 	mark_initmem_nx();
343 	init_mem_is_free = true;
344 	free_initmem_default(POISON_FREE_INITMEM);
345 }
346 
347 /**
348  * flush_coherent_icache() - if a CPU has a coherent icache, flush it
349  * @addr: The base address to use (can be any valid address, the whole cache will be flushed)
350  * Return true if the cache was flushed, false otherwise
351  */
352 static inline bool flush_coherent_icache(unsigned long addr)
353 {
354 	/*
355 	 * For a snooping icache, we still need a dummy icbi to purge all the
356 	 * prefetched instructions from the ifetch buffers. We also need a sync
357 	 * before the icbi to order the the actual stores to memory that might
358 	 * have modified instructions with the icbi.
359 	 */
360 	if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
361 		mb(); /* sync */
362 		allow_read_from_user((const void __user *)addr, L1_CACHE_BYTES);
363 		icbi((void *)addr);
364 		prevent_read_from_user((const void __user *)addr, L1_CACHE_BYTES);
365 		mb(); /* sync */
366 		isync();
367 		return true;
368 	}
369 
370 	return false;
371 }
372 
373 /**
374  * invalidate_icache_range() - Flush the icache by issuing icbi across an address range
375  * @start: the start address
376  * @stop: the stop address (exclusive)
377  */
378 static void invalidate_icache_range(unsigned long start, unsigned long stop)
379 {
380 	unsigned long shift = l1_icache_shift();
381 	unsigned long bytes = l1_icache_bytes();
382 	char *addr = (char *)(start & ~(bytes - 1));
383 	unsigned long size = stop - (unsigned long)addr + (bytes - 1);
384 	unsigned long i;
385 
386 	for (i = 0; i < size >> shift; i++, addr += bytes)
387 		icbi(addr);
388 
389 	mb(); /* sync */
390 	isync();
391 }
392 
393 /**
394  * flush_icache_range: Write any modified data cache blocks out to memory
395  * and invalidate the corresponding blocks in the instruction cache
396  *
397  * Generic code will call this after writing memory, before executing from it.
398  *
399  * @start: the start address
400  * @stop: the stop address (exclusive)
401  */
402 void flush_icache_range(unsigned long start, unsigned long stop)
403 {
404 	if (flush_coherent_icache(start))
405 		return;
406 
407 	clean_dcache_range(start, stop);
408 
409 	if (IS_ENABLED(CONFIG_44x)) {
410 		/*
411 		 * Flash invalidate on 44x because we are passed kmapped
412 		 * addresses and this doesn't work for userspace pages due to
413 		 * the virtually tagged icache.
414 		 */
415 		iccci((void *)start);
416 		mb(); /* sync */
417 		isync();
418 	} else
419 		invalidate_icache_range(start, stop);
420 }
421 EXPORT_SYMBOL(flush_icache_range);
422 
423 #if !defined(CONFIG_PPC_8xx) && !defined(CONFIG_PPC64)
424 /**
425  * flush_dcache_icache_phys() - Flush a page by it's physical address
426  * @physaddr: the physical address of the page
427  */
428 static void flush_dcache_icache_phys(unsigned long physaddr)
429 {
430 	unsigned long bytes = l1_dcache_bytes();
431 	unsigned long nb = PAGE_SIZE / bytes;
432 	unsigned long addr = physaddr & PAGE_MASK;
433 	unsigned long msr, msr0;
434 	unsigned long loop1 = addr, loop2 = addr;
435 
436 	msr0 = mfmsr();
437 	msr = msr0 & ~MSR_DR;
438 	/*
439 	 * This must remain as ASM to prevent potential memory accesses
440 	 * while the data MMU is disabled
441 	 */
442 	asm volatile(
443 		"   mtctr %2;\n"
444 		"   mtmsr %3;\n"
445 		"   isync;\n"
446 		"0: dcbst   0, %0;\n"
447 		"   addi    %0, %0, %4;\n"
448 		"   bdnz    0b;\n"
449 		"   sync;\n"
450 		"   mtctr %2;\n"
451 		"1: icbi    0, %1;\n"
452 		"   addi    %1, %1, %4;\n"
453 		"   bdnz    1b;\n"
454 		"   sync;\n"
455 		"   mtmsr %5;\n"
456 		"   isync;\n"
457 		: "+&r" (loop1), "+&r" (loop2)
458 		: "r" (nb), "r" (msr), "i" (bytes), "r" (msr0)
459 		: "ctr", "memory");
460 }
461 NOKPROBE_SYMBOL(flush_dcache_icache_phys)
462 #endif // !defined(CONFIG_PPC_8xx) && !defined(CONFIG_PPC64)
463 
464 /*
465  * This is called when a page has been modified by the kernel.
466  * It just marks the page as not i-cache clean.  We do the i-cache
467  * flush later when the page is given to a user process, if necessary.
468  */
469 void flush_dcache_page(struct page *page)
470 {
471 	if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
472 		return;
473 	/* avoid an atomic op if possible */
474 	if (test_bit(PG_arch_1, &page->flags))
475 		clear_bit(PG_arch_1, &page->flags);
476 }
477 EXPORT_SYMBOL(flush_dcache_page);
478 
479 void flush_dcache_icache_page(struct page *page)
480 {
481 #ifdef CONFIG_HUGETLB_PAGE
482 	if (PageCompound(page)) {
483 		flush_dcache_icache_hugepage(page);
484 		return;
485 	}
486 #endif
487 #if defined(CONFIG_PPC_8xx) || defined(CONFIG_PPC64)
488 	/* On 8xx there is no need to kmap since highmem is not supported */
489 	__flush_dcache_icache(page_address(page));
490 #else
491 	if (IS_ENABLED(CONFIG_BOOKE) || sizeof(phys_addr_t) > sizeof(void *)) {
492 		void *start = kmap_atomic(page);
493 		__flush_dcache_icache(start);
494 		kunmap_atomic(start);
495 	} else {
496 		unsigned long addr = page_to_pfn(page) << PAGE_SHIFT;
497 
498 		if (flush_coherent_icache(addr))
499 			return;
500 		flush_dcache_icache_phys(addr);
501 	}
502 #endif
503 }
504 EXPORT_SYMBOL(flush_dcache_icache_page);
505 
506 /**
507  * __flush_dcache_icache(): Flush a particular page from the data cache to RAM.
508  * Note: this is necessary because the instruction cache does *not*
509  * snoop from the data cache.
510  *
511  * @page: the address of the page to flush
512  */
513 void __flush_dcache_icache(void *p)
514 {
515 	unsigned long addr = (unsigned long)p;
516 
517 	if (flush_coherent_icache(addr))
518 		return;
519 
520 	clean_dcache_range(addr, addr + PAGE_SIZE);
521 
522 	/*
523 	 * We don't flush the icache on 44x. Those have a virtual icache and we
524 	 * don't have access to the virtual address here (it's not the page
525 	 * vaddr but where it's mapped in user space). The flushing of the
526 	 * icache on these is handled elsewhere, when a change in the address
527 	 * space occurs, before returning to user space.
528 	 */
529 
530 	if (cpu_has_feature(MMU_FTR_TYPE_44x))
531 		return;
532 
533 	invalidate_icache_range(addr, addr + PAGE_SIZE);
534 }
535 
536 void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
537 {
538 	clear_page(page);
539 
540 	/*
541 	 * We shouldn't have to do this, but some versions of glibc
542 	 * require it (ld.so assumes zero filled pages are icache clean)
543 	 * - Anton
544 	 */
545 	flush_dcache_page(pg);
546 }
547 EXPORT_SYMBOL(clear_user_page);
548 
549 void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
550 		    struct page *pg)
551 {
552 	copy_page(vto, vfrom);
553 
554 	/*
555 	 * We should be able to use the following optimisation, however
556 	 * there are two problems.
557 	 * Firstly a bug in some versions of binutils meant PLT sections
558 	 * were not marked executable.
559 	 * Secondly the first word in the GOT section is blrl, used
560 	 * to establish the GOT address. Until recently the GOT was
561 	 * not marked executable.
562 	 * - Anton
563 	 */
564 #if 0
565 	if (!vma->vm_file && ((vma->vm_flags & VM_EXEC) == 0))
566 		return;
567 #endif
568 
569 	flush_dcache_page(pg);
570 }
571 
572 void flush_icache_user_page(struct vm_area_struct *vma, struct page *page,
573 			     unsigned long addr, int len)
574 {
575 	unsigned long maddr;
576 
577 	maddr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
578 	flush_icache_range(maddr, maddr + len);
579 	kunmap(page);
580 }
581 
582 /*
583  * System memory should not be in /proc/iomem but various tools expect it
584  * (eg kdump).
585  */
586 static int __init add_system_ram_resources(void)
587 {
588 	phys_addr_t start, end;
589 	u64 i;
590 
591 	for_each_mem_range(i, &start, &end) {
592 		struct resource *res;
593 
594 		res = kzalloc(sizeof(struct resource), GFP_KERNEL);
595 		WARN_ON(!res);
596 
597 		if (res) {
598 			res->name = "System RAM";
599 			res->start = start;
600 			/*
601 			 * In memblock, end points to the first byte after
602 			 * the range while in resourses, end points to the
603 			 * last byte in the range.
604 			 */
605 			res->end = end - 1;
606 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
607 			WARN_ON(request_resource(&iomem_resource, res) < 0);
608 		}
609 	}
610 
611 	return 0;
612 }
613 subsys_initcall(add_system_ram_resources);
614 
615 #ifdef CONFIG_STRICT_DEVMEM
616 /*
617  * devmem_is_allowed(): check to see if /dev/mem access to a certain address
618  * is valid. The argument is a physical page number.
619  *
620  * Access has to be given to non-kernel-ram areas as well, these contain the
621  * PCI mmio resources as well as potential bios/acpi data regions.
622  */
623 int devmem_is_allowed(unsigned long pfn)
624 {
625 	if (page_is_rtas_user_buf(pfn))
626 		return 1;
627 	if (iomem_is_exclusive(PFN_PHYS(pfn)))
628 		return 0;
629 	if (!page_is_ram(pfn))
630 		return 1;
631 	return 0;
632 }
633 #endif /* CONFIG_STRICT_DEVMEM */
634 
635 /*
636  * This is defined in kernel/resource.c but only powerpc needs to export it, for
637  * the EHEA driver. Drop this when drivers/net/ethernet/ibm/ehea is removed.
638  */
639 EXPORT_SYMBOL_GPL(walk_system_ram_range);
640