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) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <asm/proto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * Some BIOSes seem to corrupt the low 64k of memory during events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * like suspend/resume and unplugging an HDMI cable.  Reserve all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * remaining free memory in that area and fill it with a distinct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * pattern.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define MAX_SCAN_AREAS	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static int __read_mostly memory_corruption_check = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static unsigned __read_mostly corruption_check_size = 64*1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static unsigned __read_mostly corruption_check_period = 60; /* seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static struct scan_area {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u64 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	u64 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) } scan_areas[MAX_SCAN_AREAS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static int num_scan_areas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static __init int set_corruption_check(char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	if (!arg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		pr_err("memory_corruption_check config string not provided\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	ret = kstrtoul(arg, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	memory_corruption_check = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) early_param("memory_corruption_check", set_corruption_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static __init int set_corruption_check_period(char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (!arg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		pr_err("memory_corruption_check_period config string not provided\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	ret = kstrtoul(arg, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	corruption_check_period = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) early_param("memory_corruption_check_period", set_corruption_check_period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static __init int set_corruption_check_size(char *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	unsigned size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (!arg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		pr_err("memory_corruption_check_size config string not provided\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	size = memparse(arg, &end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (*end == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		corruption_check_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return (size == corruption_check_size) ? 0 : -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) early_param("memory_corruption_check_size", set_corruption_check_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) void __init setup_bios_corruption_check(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	phys_addr_t start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u64 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (memory_corruption_check == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		memory_corruption_check =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) #ifdef CONFIG_X86_BOOTPARAM_MEMORY_CORRUPTION_CHECK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (corruption_check_size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		memory_corruption_check = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (!memory_corruption_check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	corruption_check_size = round_up(corruption_check_size, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 				NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		start = clamp_t(phys_addr_t, round_up(start, PAGE_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 				PAGE_SIZE, corruption_check_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		end = clamp_t(phys_addr_t, round_down(end, PAGE_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			      PAGE_SIZE, corruption_check_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		if (start >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		memblock_reserve(start, end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		scan_areas[num_scan_areas].addr = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		scan_areas[num_scan_areas].size = end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		/* Assume we've already mapped this early memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		memset(__va(start), 0, end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		if (++num_scan_areas >= MAX_SCAN_AREAS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (num_scan_areas)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		pr_info("Scanning %d areas for low memory corruption\n", num_scan_areas);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static void check_for_bios_corruption(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	int corruption = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (!memory_corruption_check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	for (i = 0; i < num_scan_areas; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		unsigned long *addr = __va(scan_areas[i].addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		unsigned long size = scan_areas[i].size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		for (; size; addr++, size -= sizeof(unsigned long)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			if (!*addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			pr_err("Corrupted low memory at %p (%lx phys) = %08lx\n", addr, __pa(addr), *addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			corruption = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			*addr = 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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	WARN_ONCE(corruption, KERN_ERR "Memory corruption detected in low memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static void check_corruption(struct work_struct *dummy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static DECLARE_DELAYED_WORK(bios_check_work, check_corruption);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static void check_corruption(struct work_struct *dummy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	check_for_bios_corruption();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	schedule_delayed_work(&bios_check_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		round_jiffies_relative(corruption_check_period*HZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static int start_periodic_check_for_corruption(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (!num_scan_areas || !memory_corruption_check || corruption_check_period == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	pr_info("Scanning for low memory corruption every %d seconds\n", corruption_check_period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	/* First time we run the checks right away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	schedule_delayed_work(&bios_check_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) device_initcall(start_periodic_check_for_corruption);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)