Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  S390 version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *    Copyright IBM Corp. 1999
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *    Author(s): Hartmut Penner (hp@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  Derived from "arch/i386/mm/init.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *    Copyright (C) 1995  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/swiotlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/pfn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/poison.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/initrd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/cma.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/dma-direct.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <asm/pgalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <asm/ptdump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <asm/dma.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <asm/lowcore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <asm/tlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <asm/ctl_reg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include <asm/sclp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #include <asm/set_memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #include <asm/kasan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include <asm/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #include <asm/uv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #include <linux/virtio_config.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) pgd_t swapper_pg_dir[PTRS_PER_PGD] __section(".bss..swapper_pg_dir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) unsigned long empty_zero_page, zero_page_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) EXPORT_SYMBOL(empty_zero_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) EXPORT_SYMBOL(zero_page_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) bool initmem_freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static void __init setup_zero_pages(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	unsigned int order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	/* Latest machines require a mapping granularity of 512KB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	order = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	/* Limit number of empty zero pages for small memory sizes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	while (order > 2 && (totalram_pages() >> 10) < (1UL << order))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		order--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	empty_zero_page = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (!empty_zero_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		panic("Out of memory in setup_zero_pages");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	page = virt_to_page((void *) empty_zero_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	split_page(page, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	for (i = 1 << order; i > 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		mark_page_reserved(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		page++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	zero_page_mask = ((PAGE_SIZE << order) - 1) & PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * paging_init() sets up the page tables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) void __init paging_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	unsigned long max_zone_pfns[MAX_NR_ZONES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	unsigned long pgd_type, asce_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	psw_t psw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	init_mm.pgd = swapper_pg_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (VMALLOC_END > _REGION2_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		asce_bits = _ASCE_TYPE_REGION2 | _ASCE_TABLE_LENGTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		pgd_type = _REGION2_ENTRY_EMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		asce_bits = _ASCE_TYPE_REGION3 | _ASCE_TABLE_LENGTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		pgd_type = _REGION3_ENTRY_EMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	init_mm.context.asce = (__pa(init_mm.pgd) & PAGE_MASK) | asce_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	S390_lowcore.kernel_asce = init_mm.context.asce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	S390_lowcore.user_asce = S390_lowcore.kernel_asce;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	crst_table_init((unsigned long *) init_mm.pgd, pgd_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	vmem_map_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	kasan_copy_shadow(init_mm.pgd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	/* enable virtual mapping in kernel mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	__ctl_load(S390_lowcore.kernel_asce, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	__ctl_load(S390_lowcore.kernel_asce, 7, 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	__ctl_load(S390_lowcore.kernel_asce, 13, 13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	psw.mask = __extract_psw();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	psw_bits(psw).dat = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	psw_bits(psw).as = PSW_BITS_AS_HOME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	__load_psw_mask(psw.mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	kasan_free_early_identity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	sparse_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	zone_dma_bits = 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	max_zone_pfns[ZONE_DMA] = PFN_DOWN(MAX_DMA_ADDRESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	free_area_init(max_zone_pfns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void mark_rodata_ro(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	unsigned long size = __end_ro_after_init - __start_ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	set_memory_ro((unsigned long)__start_ro_after_init, size >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	pr_info("Write protected read-only-after-init data: %luk\n", size >> 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	debug_checkwx();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int set_memory_encrypted(unsigned long addr, int numpages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	/* make specified pages unshared, (swiotlb, dma_free) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	for (i = 0; i < numpages; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		uv_remove_shared(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		addr += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int set_memory_decrypted(unsigned long addr, int numpages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	/* make specified pages shared (swiotlb, dma_alloca) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	for (i = 0; i < numpages; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		uv_set_shared(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		addr += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* are we a protected virtualization guest? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) bool force_dma_unencrypted(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return is_prot_virt_guest();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #ifdef CONFIG_ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int arch_has_restricted_virtio_memory_access(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return is_prot_virt_guest();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) EXPORT_SYMBOL(arch_has_restricted_virtio_memory_access);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* protected virtualization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static void pv_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (!is_prot_virt_guest())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	/* make sure bounce buffers are shared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	swiotlb_force = SWIOTLB_FORCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	swiotlb_init(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	swiotlb_update_mem_attributes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) void __init mem_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	cpumask_set_cpu(0, &init_mm.context.cpu_attach_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	cpumask_set_cpu(0, mm_cpumask(&init_mm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	set_max_mapnr(max_low_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)         high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	pv_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	/* Setup guest page hinting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	cmma_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	/* this will put all low memory onto the freelists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	memblock_free_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	setup_zero_pages();	/* Setup zeroed pages. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	cmma_init_nodat();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	mem_init_print_info(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) void free_initmem(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	initmem_freed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	__set_memory((unsigned long)_sinittext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		     (unsigned long)(_einittext - _sinittext) >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		     SET_MEMORY_RW | SET_MEMORY_NX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	free_initmem_default(POISON_FREE_INITMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) unsigned long memory_block_size_bytes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	 * Make sure the memory block size is always greater
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	 * or equal than the memory increment size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	return max_t(unsigned long, MIN_MEMORY_BLOCK_SIZE, sclp.rzm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) #ifdef CONFIG_MEMORY_HOTPLUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) #ifdef CONFIG_CMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* Prevent memory blocks which contain cma regions from going offline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct s390_cma_mem_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	unsigned long start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	unsigned long end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static int s390_cma_check_range(struct cma *cma, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct s390_cma_mem_data *mem_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	unsigned long start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	mem_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	start = cma_get_base(cma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	end = start + cma_get_size(cma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (end < mem_data->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (start >= mem_data->end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static int s390_cma_mem_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 				 unsigned long action, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct s390_cma_mem_data mem_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	struct memory_notify *arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	arg = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	mem_data.start = arg->start_pfn << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	mem_data.end = mem_data.start + (arg->nr_pages << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (action == MEM_GOING_OFFLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		rc = cma_for_each_area(s390_cma_check_range, &mem_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return notifier_from_errno(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static struct notifier_block s390_cma_mem_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	.notifier_call = s390_cma_mem_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int __init s390_cma_mem_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	return register_memory_notifier(&s390_cma_mem_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) device_initcall(s390_cma_mem_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) #endif /* CONFIG_CMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int arch_add_memory(int nid, u64 start, u64 size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		    struct mhp_params *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	unsigned long start_pfn = PFN_DOWN(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	unsigned long size_pages = PFN_DOWN(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (WARN_ON_ONCE(params->altmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	rc = vmem_add_mapping(start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	rc = __add_pages(nid, start_pfn, size_pages, params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		vmem_remove_mapping(start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) void arch_remove_memory(int nid, u64 start, u64 size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	unsigned long start_pfn = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	unsigned long nr_pages = size >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	__remove_pages(start_pfn, nr_pages, altmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	vmem_remove_mapping(start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) #endif /* CONFIG_MEMORY_HOTPLUG */