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)  * Export Runtime Configuration Interface Table Version 2 (RCI2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * to sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2019 Dell Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * by Narendra K <Narendra.K@dell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * System firmware advertises the address of the RCI2 Table via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * an EFI Configuration Table entry. This code retrieves the RCI2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * table from the address and exports it to sysfs as a binary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * attribute 'rci2' under /sys/firmware/efi/tables directory.
^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) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define RCI_SIGNATURE	"_RC_"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct rci2_table_global_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	u16 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	u16 resvd0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	u16 hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u8 rci2_sig[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	u16 resvd1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	u32 resvd2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	u32 resvd3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	u8 major_rev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	u8 minor_rev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	u16 num_of_structs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	u32 rci2_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	u16 rci2_chksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static u8 *rci2_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static u32 rci2_table_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) unsigned long rci2_table_phys __ro_after_init = EFI_INVALID_TABLE_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			      struct bin_attribute *attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 			      loff_t pos, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	memcpy(buf, attr->private + pos, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static BIN_ATTR(rci2, S_IRUSR, raw_table_read, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static u16 checksum(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	u8 len_is_odd = rci2_table_len % 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	u32 chksum_len = rci2_table_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u16 *base = (u16 *)rci2_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u8 buf[2] = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	u32 offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	u16 chksum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (len_is_odd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		chksum_len -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	while (offset < chksum_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		chksum += *base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		offset += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		base++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (len_is_odd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		buf[0] = *(u8 *)base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		chksum += *(u16 *)(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	return chksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) static int __init efi_rci2_sysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct kobject *tables_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (rci2_table_phys == EFI_INVALID_TABLE_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	rci2_base = memremap(rci2_table_phys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			     sizeof(struct rci2_table_global_hdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			     MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (!rci2_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		pr_debug("RCI2 table init failed - could not map RCI2 table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (strncmp(rci2_base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		    offsetof(struct rci2_table_global_hdr, rci2_sig),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		    RCI_SIGNATURE, 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		pr_debug("RCI2 table init failed - incorrect signature\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		goto err_unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	rci2_table_len = *(u32 *)(rci2_base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 				  offsetof(struct rci2_table_global_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				  rci2_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	memunmap(rci2_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (!rci2_table_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		pr_debug("RCI2 table init failed - incorrect table length\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	rci2_base = memremap(rci2_table_phys, rci2_table_len, MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (!rci2_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		pr_debug("RCI2 table - could not map RCI2 table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (checksum() != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		pr_debug("RCI2 table - incorrect checksum\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		goto err_unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	tables_kobj = kobject_create_and_add("tables", efi_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!tables_kobj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		pr_debug("RCI2 table - tables_kobj creation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		goto err_unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	bin_attr_rci2.size = rci2_table_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	bin_attr_rci2.private = rci2_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	ret = sysfs_create_bin_file(tables_kobj, &bin_attr_rci2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		pr_debug("RCI2 table - rci2 sysfs bin file creation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		kobject_del(tables_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		kobject_put(tables_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		goto err_unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)  err_unmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	memunmap(rci2_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	pr_debug("RCI2 table - sysfs initialization failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) late_initcall(efi_rci2_sysfs_init);