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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * drivers/firmware/qemu_fw_cfg.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright 2015 Carnegie Mellon University
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Expose entries from QEMU's firmware configuration (fw_cfg) device in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * sysfs (read-only, under "/sys/firmware/qemu_fw_cfg/...").
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * The fw_cfg device may be instantiated via either an ACPI node (on x86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * and select subsets of aarch64), a Device Tree node (on arm), or using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * a kernel module (or command line) parameter with the following syntax:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *      [qemu_fw_cfg.]ioport=<size>@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *      [qemu_fw_cfg.]mmio=<size>@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * where:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *      <size>     := size of ioport or mmio range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *      <base>     := physical base address of ioport or mmio range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *      <ctrl_off> := (optional) offset of control register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *      <data_off> := (optional) offset of data register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *      <dma_off> := (optional) offset of dma register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * e.g.:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *      qemu_fw_cfg.ioport=12@0x510:0:1:4	(the default on x86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *      qemu_fw_cfg.mmio=16@0x9020000:8:0:16	(the default on arm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/mod_devicetable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <uapi/linux/qemu_fw_cfg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/crash_dump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <linux/crash_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) MODULE_AUTHOR("Gabriel L. Somlo <somlo@cmu.edu>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) MODULE_DESCRIPTION("QEMU fw_cfg sysfs support");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static u32 fw_cfg_rev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) /* fw_cfg device i/o register addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static bool fw_cfg_is_mmio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static phys_addr_t fw_cfg_p_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static resource_size_t fw_cfg_p_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static void __iomem *fw_cfg_dev_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static void __iomem *fw_cfg_reg_ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static void __iomem *fw_cfg_reg_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static void __iomem *fw_cfg_reg_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) /* atomic access to fw_cfg device (potentially slow i/o, so using mutex) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static DEFINE_MUTEX(fw_cfg_dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) /* pick appropriate endianness for selector key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static void fw_cfg_sel_endianness(u16 key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (fw_cfg_is_mmio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		iowrite16be(key, fw_cfg_reg_ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		iowrite16(key, fw_cfg_reg_ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #ifdef CONFIG_CRASH_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static inline bool fw_cfg_dma_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	return (fw_cfg_rev & FW_CFG_VERSION_DMA) && fw_cfg_reg_dma;
^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) /* qemu fw_cfg device is sync today, but spec says it may become async */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static void fw_cfg_wait_for_control(struct fw_cfg_dma_access *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		u32 ctrl = be32_to_cpu(READ_ONCE(d->control));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		/* do not reorder the read to d->control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		if ((ctrl & ~FW_CFG_DMA_CTL_ERROR) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		cpu_relax();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static ssize_t fw_cfg_dma_transfer(void *address, u32 length, u32 control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	phys_addr_t dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct fw_cfg_dma_access *d = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	ssize_t ret = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	d = kmalloc(sizeof(*d), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (!d) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		goto end;
^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) 	/* fw_cfg device does not need IOMMU protection, so use physical addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	*d = (struct fw_cfg_dma_access) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		.address = cpu_to_be64(address ? virt_to_phys(address) : 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		.length = cpu_to_be32(length),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		.control = cpu_to_be32(control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	dma = virt_to_phys(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	iowrite32be((u64)dma >> 32, fw_cfg_reg_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	/* force memory to sync before notifying device via MMIO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	iowrite32be(dma, fw_cfg_reg_dma + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	fw_cfg_wait_for_control(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (be32_to_cpu(READ_ONCE(d->control)) & FW_CFG_DMA_CTL_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	kfree(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* read chunk of given fw_cfg blob (caller responsible for sanity-check) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static ssize_t fw_cfg_read_blob(u16 key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 				void *buf, loff_t pos, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	u32 glk = -1U;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	/* If we have ACPI, ensure mutual exclusion against any potential
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 * device access by the firmware, e.g. via AML methods:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		/* Should never get here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		WARN(1, "fw_cfg_read_blob: Failed to lock ACPI!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		memset(buf, 0, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	mutex_lock(&fw_cfg_dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	fw_cfg_sel_endianness(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	while (pos-- > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		ioread8(fw_cfg_reg_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	ioread8_rep(fw_cfg_reg_data, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	mutex_unlock(&fw_cfg_dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	acpi_release_global_lock(glk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #ifdef CONFIG_CRASH_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* write chunk of given fw_cfg blob (caller responsible for sanity-check) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static ssize_t fw_cfg_write_blob(u16 key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				 void *buf, loff_t pos, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	u32 glk = -1U;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	ssize_t ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	/* If we have ACPI, ensure mutual exclusion against any potential
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 * device access by the firmware, e.g. via AML methods:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		/* Should never get here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		WARN(1, "%s: Failed to lock ACPI!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	mutex_lock(&fw_cfg_dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (pos == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		ret = fw_cfg_dma_transfer(buf, count, key << 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 					  | FW_CFG_DMA_CTL_SELECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 					  | FW_CFG_DMA_CTL_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		fw_cfg_sel_endianness(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		ret = fw_cfg_dma_transfer(NULL, pos, FW_CFG_DMA_CTL_SKIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		ret = fw_cfg_dma_transfer(buf, count, FW_CFG_DMA_CTL_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	mutex_unlock(&fw_cfg_dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	acpi_release_global_lock(glk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #endif /* CONFIG_CRASH_CORE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* clean up fw_cfg device i/o */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static void fw_cfg_io_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (fw_cfg_is_mmio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		iounmap(fw_cfg_dev_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		ioport_unmap(fw_cfg_dev_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		release_region(fw_cfg_p_base, fw_cfg_p_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /* arch-specific ctrl & data register offsets are not available in ACPI, DT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) #if !(defined(FW_CFG_CTRL_OFF) && defined(FW_CFG_DATA_OFF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) # if (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) #  define FW_CFG_CTRL_OFF 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) #  define FW_CFG_DATA_OFF 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) #  define FW_CFG_DMA_OFF 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) # elif defined(CONFIG_PARISC)	/* parisc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) #  define FW_CFG_CTRL_OFF 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) #  define FW_CFG_DATA_OFF 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) # elif (defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC32)) /* ppc/mac,sun4m */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) #  define FW_CFG_CTRL_OFF 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) #  define FW_CFG_DATA_OFF 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) # elif (defined(CONFIG_X86) || defined(CONFIG_SPARC64)) /* x86, sun4u */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) #  define FW_CFG_CTRL_OFF 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) #  define FW_CFG_DATA_OFF 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) #  define FW_CFG_DMA_OFF 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) # else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) #  error "QEMU FW_CFG not available on this architecture!"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) # endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* initialize fw_cfg device i/o from platform data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static int fw_cfg_do_platform_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	char sig[FW_CFG_SIG_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct resource *range, *ctrl, *data, *dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	/* acquire i/o range details */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	fw_cfg_is_mmio = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	range = platform_get_resource(pdev, IORESOURCE_IO, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (!range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		fw_cfg_is_mmio = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		range = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		if (!range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	fw_cfg_p_base = range->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	fw_cfg_p_size = resource_size(range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (fw_cfg_is_mmio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		if (!request_mem_region(fw_cfg_p_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 					fw_cfg_p_size, "fw_cfg_mem"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		fw_cfg_dev_base = ioremap(fw_cfg_p_base, fw_cfg_p_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		if (!fw_cfg_dev_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (!request_region(fw_cfg_p_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 				    fw_cfg_p_size, "fw_cfg_io"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		fw_cfg_dev_base = ioport_map(fw_cfg_p_base, fw_cfg_p_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		if (!fw_cfg_dev_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			release_region(fw_cfg_p_base, fw_cfg_p_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	/* were custom register offsets provided (e.g. on the command line)? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	ctrl = platform_get_resource_byname(pdev, IORESOURCE_REG, "ctrl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	data = platform_get_resource_byname(pdev, IORESOURCE_REG, "data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	dma = platform_get_resource_byname(pdev, IORESOURCE_REG, "dma");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (ctrl && data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		fw_cfg_reg_ctrl = fw_cfg_dev_base + ctrl->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		fw_cfg_reg_data = fw_cfg_dev_base + data->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		/* use architecture-specific offsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		fw_cfg_reg_ctrl = fw_cfg_dev_base + FW_CFG_CTRL_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		fw_cfg_reg_data = fw_cfg_dev_base + FW_CFG_DATA_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (dma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		fw_cfg_reg_dma = fw_cfg_dev_base + dma->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) #ifdef FW_CFG_DMA_OFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		fw_cfg_reg_dma = fw_cfg_dev_base + FW_CFG_DMA_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	/* verify fw_cfg device signature */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (fw_cfg_read_blob(FW_CFG_SIGNATURE, sig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 				0, FW_CFG_SIG_SIZE) < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		fw_cfg_io_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static ssize_t fw_cfg_showrev(struct kobject *k, struct kobj_attribute *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			      char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	return sprintf(buf, "%u\n", fw_cfg_rev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static const struct kobj_attribute fw_cfg_rev_attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	.attr = { .name = "rev", .mode = S_IRUSR },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	.show = fw_cfg_showrev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* fw_cfg_sysfs_entry type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct fw_cfg_sysfs_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct kobject kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	u16 select;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	char name[FW_CFG_MAX_FILE_PATH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) #ifdef CONFIG_CRASH_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static ssize_t fw_cfg_write_vmcoreinfo(const struct fw_cfg_file *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	static struct fw_cfg_vmcoreinfo *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	data = kmalloc(sizeof(struct fw_cfg_vmcoreinfo), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	*data = (struct fw_cfg_vmcoreinfo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		.guest_format = cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		.size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		.paddr = cpu_to_le64(paddr_vmcoreinfo_note())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	/* spare ourself reading host format support for now since we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	 * don't know what else to format - host may ignore ours
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	ret = fw_cfg_write_blob(be16_to_cpu(f->select), data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 				0, sizeof(struct fw_cfg_vmcoreinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) #endif /* CONFIG_CRASH_CORE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /* get fw_cfg_sysfs_entry from kobject member */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	return container_of(kobj, struct fw_cfg_sysfs_entry, kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) /* fw_cfg_sysfs_attribute type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct fw_cfg_sysfs_attribute {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	struct attribute attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /* get fw_cfg_sysfs_attribute from attribute member */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static inline struct fw_cfg_sysfs_attribute *to_attr(struct attribute *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	return container_of(attr, struct fw_cfg_sysfs_attribute, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /* global cache of fw_cfg_sysfs_entry objects */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static LIST_HEAD(fw_cfg_entry_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) /* kobjects removed lazily by kernel, mutual exclusion needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static DEFINE_SPINLOCK(fw_cfg_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static inline void fw_cfg_sysfs_cache_enlist(struct fw_cfg_sysfs_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	spin_lock(&fw_cfg_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	list_add_tail(&entry->list, &fw_cfg_entry_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	spin_unlock(&fw_cfg_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static inline void fw_cfg_sysfs_cache_delist(struct fw_cfg_sysfs_entry *entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	spin_lock(&fw_cfg_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	list_del(&entry->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	spin_unlock(&fw_cfg_cache_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static void fw_cfg_sysfs_cache_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	struct fw_cfg_sysfs_entry *entry, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	list_for_each_entry_safe(entry, next, &fw_cfg_entry_cache, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		fw_cfg_sysfs_cache_delist(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		kobject_put(&entry->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /* default_attrs: per-entry attributes and show methods */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) #define FW_CFG_SYSFS_ATTR(_attr) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct fw_cfg_sysfs_attribute fw_cfg_sysfs_attr_##_attr = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	.attr = { .name = __stringify(_attr), .mode = S_IRUSR }, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	.show = fw_cfg_sysfs_show_##_attr, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static ssize_t fw_cfg_sysfs_show_size(struct fw_cfg_sysfs_entry *e, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	return sprintf(buf, "%u\n", e->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static ssize_t fw_cfg_sysfs_show_key(struct fw_cfg_sysfs_entry *e, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	return sprintf(buf, "%u\n", e->select);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) static ssize_t fw_cfg_sysfs_show_name(struct fw_cfg_sysfs_entry *e, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	return sprintf(buf, "%s\n", e->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static FW_CFG_SYSFS_ATTR(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static FW_CFG_SYSFS_ATTR(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static FW_CFG_SYSFS_ATTR(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static struct attribute *fw_cfg_sysfs_entry_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	&fw_cfg_sysfs_attr_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	&fw_cfg_sysfs_attr_key.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	&fw_cfg_sysfs_attr_name.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) /* sysfs_ops: find fw_cfg_[entry, attribute] and call appropriate show method */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) static ssize_t fw_cfg_sysfs_attr_show(struct kobject *kobj, struct attribute *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 				      char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	struct fw_cfg_sysfs_attribute *attr = to_attr(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	return attr->show(entry, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static const struct sysfs_ops fw_cfg_sysfs_attr_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	.show = fw_cfg_sysfs_attr_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /* release: destructor, to be called via kobject_put() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) static void fw_cfg_sysfs_release_entry(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /* kobj_type: ties together all properties required to register an entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static struct kobj_type fw_cfg_sysfs_entry_ktype = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	.default_attrs = fw_cfg_sysfs_entry_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	.sysfs_ops = &fw_cfg_sysfs_attr_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	.release = fw_cfg_sysfs_release_entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /* raw-read method and attribute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 				     struct bin_attribute *bin_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 				     char *buf, loff_t pos, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (pos > entry->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (count > entry->size - pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		count = entry->size - pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	return fw_cfg_read_blob(entry->select, buf, pos, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) static struct bin_attribute fw_cfg_sysfs_attr_raw = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	.attr = { .name = "raw", .mode = S_IRUSR },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	.read = fw_cfg_sysfs_read_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)  * Create a kset subdirectory matching each '/' delimited dirname token
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)  * in 'name', starting with sysfs kset/folder 'dir'; At the end, create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)  * a symlink directed at the given 'target'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)  * NOTE: We do this on a best-effort basis, since 'name' is not guaranteed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)  * to be a well-behaved path name. Whenever a symlink vs. kset directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)  * name collision occurs, the kernel will issue big scary warnings while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)  * refusing to add the offending link or directory. We follow up with our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)  * own, slightly less scary error messages explaining the situation :)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static int fw_cfg_build_symlink(struct kset *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 				struct kobject *target, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	struct kset *subdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	struct kobject *ko;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	char *name_copy, *p, *tok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	if (!dir || !target || !name || !*name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	/* clone a copy of name for parsing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	name_copy = p = kstrdup(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	if (!name_copy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	/* create folders for each dirname token, then symlink for basename */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	while ((tok = strsep(&p, "/")) && *tok) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		/* last (basename) token? If so, add symlink here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		if (!p || !*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 			ret = sysfs_create_link(&dir->kobj, target, tok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		/* does the current dir contain an item named after tok ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		ko = kset_find_obj(dir, tok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		if (ko) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			/* drop reference added by kset_find_obj */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			kobject_put(ko);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 			/* ko MUST be a kset - we're about to use it as one ! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			if (ko->ktype != dir->kobj.ktype) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 				ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 			/* descend into already existing subdirectory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			dir = to_kset(ko);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			/* create new subdirectory kset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 			subdir = kzalloc(sizeof(struct kset), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			if (!subdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 				ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			subdir->kobj.kset = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 			subdir->kobj.ktype = dir->kobj.ktype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			ret = kobject_set_name(&subdir->kobj, "%s", tok);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 				kfree(subdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			ret = kset_register(subdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 				kfree(subdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 			/* descend into newly created subdirectory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			dir = subdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	/* we're done with cloned copy of name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	kfree(name_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) /* recursively unregister fw_cfg/by_name/ kset directory tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) static void fw_cfg_kset_unregister_recursive(struct kset *kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	struct kobject *k, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	list_for_each_entry_safe(k, next, &kset->list, entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		/* all set members are ksets too, but check just in case... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		if (k->ktype == kset->kobj.ktype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			fw_cfg_kset_unregister_recursive(to_kset(k));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	/* symlinks are cleanly and automatically removed with the directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	kset_unregister(kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) /* kobjects & kset representing top-level, by_key, and by_name folders */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static struct kobject *fw_cfg_top_ko;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) static struct kobject *fw_cfg_sel_ko;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static struct kset *fw_cfg_fname_kset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /* register an individual fw_cfg file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static int fw_cfg_register_file(const struct fw_cfg_file *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	struct fw_cfg_sysfs_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) #ifdef CONFIG_CRASH_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	if (fw_cfg_dma_enabled() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		strcmp(f->name, FW_CFG_VMCOREINFO_FILENAME) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		!is_kdump_kernel()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		if (fw_cfg_write_vmcoreinfo(f) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			pr_warn("fw_cfg: failed to write vmcoreinfo");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	/* allocate new entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	if (!entry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	/* set file entry information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	entry->size = be32_to_cpu(f->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	entry->select = be16_to_cpu(f->select);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	strscpy(entry->name, f->name, FW_CFG_MAX_FILE_PATH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	/* register entry under "/sys/firmware/qemu_fw_cfg/by_key/" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	err = kobject_init_and_add(&entry->kobj, &fw_cfg_sysfs_entry_ktype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 				   fw_cfg_sel_ko, "%d", entry->select);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		goto err_put_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	/* add raw binary content access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		goto err_del_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	/* try adding "/sys/firmware/qemu_fw_cfg/by_name/" symlink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	/* success, add entry to global cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	fw_cfg_sysfs_cache_enlist(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) err_del_entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	kobject_del(&entry->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) err_put_entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	kobject_put(&entry->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) /* iterate over all fw_cfg directory entries, registering each one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) static int fw_cfg_register_dir_entries(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	__be32 files_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	u32 count, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	struct fw_cfg_file *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	size_t dir_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, &files_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			0, sizeof(files_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	count = be32_to_cpu(files_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	dir_size = count * sizeof(struct fw_cfg_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	dir = kmalloc(dir_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	if (!dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 			sizeof(files_count), dir_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 		ret = fw_cfg_register_file(&dir[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	kfree(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) /* unregister top-level or by_key folder */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) static inline void fw_cfg_kobj_cleanup(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	kobject_del(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) static int fw_cfg_sysfs_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	__le32 rev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	/* NOTE: If we supported multiple fw_cfg devices, we'd first create
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	 * a subdirectory named after e.g. pdev->id, then hang per-device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	 * by_key (and by_name) subdirectories underneath it. However, only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	 * one fw_cfg device exist system-wide, so if one was already found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	 * earlier, we might as well stop here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	if (fw_cfg_sel_ko)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	/* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	if (!fw_cfg_sel_ko)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		goto err_sel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	if (!fw_cfg_fname_kset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 		goto err_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	/* initialize fw_cfg device i/o from platform data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	err = fw_cfg_do_platform_probe(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		goto err_probe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	/* get revision number, add matching top-level attribute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	err = fw_cfg_read_blob(FW_CFG_ID, &rev, 0, sizeof(rev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		goto err_probe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	fw_cfg_rev = le32_to_cpu(rev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		goto err_rev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	/* process fw_cfg file directory entry, registering each file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	err = fw_cfg_register_dir_entries();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		goto err_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	/* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	pr_debug("fw_cfg: loaded.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) err_dir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	fw_cfg_sysfs_cache_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) err_rev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	fw_cfg_io_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) err_probe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) err_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) err_sel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) static int fw_cfg_sysfs_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	pr_debug("fw_cfg: unloading.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	fw_cfg_sysfs_cache_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	fw_cfg_io_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) static const struct of_device_id fw_cfg_sysfs_mmio_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	{ .compatible = "qemu,fw-cfg-mmio", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	{ FW_CFG_ACPI_DEVICE_ID, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) static struct platform_driver fw_cfg_sysfs_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	.probe = fw_cfg_sysfs_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	.remove = fw_cfg_sysfs_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		.name = "fw_cfg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		.of_match_table = fw_cfg_sysfs_mmio_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		.acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) static struct platform_device *fw_cfg_cmdline_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) /* this probably belongs in e.g. include/linux/types.h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)  * but right now we are the only ones doing it...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) #ifdef CONFIG_PHYS_ADDR_T_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) #define __PHYS_ADDR_PREFIX "ll"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) #define __PHYS_ADDR_PREFIX ""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) /* use special scanf/printf modifier for phys_addr_t, resource_size_t */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) #define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 			 ":%" __PHYS_ADDR_PREFIX "i" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 			 ":%" __PHYS_ADDR_PREFIX "i%n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 			 ":%" __PHYS_ADDR_PREFIX "i%n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) #define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 			 "0x%" __PHYS_ADDR_PREFIX "x"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) #define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 			 ":%" __PHYS_ADDR_PREFIX "u" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 			 ":%" __PHYS_ADDR_PREFIX "u"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) #define PH_ADDR_PR_4_FMT PH_ADDR_PR_3_FMT \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 			 ":%" __PHYS_ADDR_PREFIX "u"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	struct resource res[4] = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	phys_addr_t base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	resource_size_t size, ctrl_off, data_off, dma_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	int processed, consumed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	/* only one fw_cfg device can exist system-wide, so if one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	 * was processed on the command line already, we might as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	 * well stop here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	if (fw_cfg_cmdline_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 		/* avoid leaking previously registered device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 		platform_device_unregister(fw_cfg_cmdline_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	/* consume "<size>" portion of command line argument */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	size = memparse(arg, &str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	/* get "@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]" chunks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	processed = sscanf(str, PH_ADDR_SCAN_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 			   &base, &consumed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 			   &ctrl_off, &data_off, &consumed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 			   &dma_off, &consumed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	/* sscanf() must process precisely 1, 3 or 4 chunks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	 * <base> is mandatory, optionally followed by <ctrl_off>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	 * and <data_off>, and <dma_off>;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	 * there must be no extra characters after the last chunk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	 * so str[consumed] must be '\0'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	if (str[consumed] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	    (processed != 1 && processed != 3 && processed != 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 	res[0].start = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	res[0].end = base + size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 	res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 						   IORESOURCE_IO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	/* insert register offsets, if provided */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 	if (processed > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 		res[1].name = "ctrl";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 		res[1].start = ctrl_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 		res[1].flags = IORESOURCE_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 		res[2].name = "data";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 		res[2].start = data_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 		res[2].flags = IORESOURCE_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	if (processed > 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 		res[3].name = "dma";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 		res[3].start = dma_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 		res[3].flags = IORESOURCE_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 	/* "processed" happens to nicely match the number of resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 	 * we need to pass in to this platform device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 	fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 					PLATFORM_DEVID_NONE, res, processed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	return PTR_ERR_OR_ZERO(fw_cfg_cmdline_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	/* stay silent if device was not configured via the command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	 * line, or if the parameter name (ioport/mmio) doesn't match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 	 * the device setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 	if (!fw_cfg_cmdline_dev ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	    (!strcmp(kp->name, "mmio") ^
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 	     (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	switch (fw_cfg_cmdline_dev->num_resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 		return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 				resource_size(&fw_cfg_cmdline_dev->resource[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 				fw_cfg_cmdline_dev->resource[0].start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 		return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 				resource_size(&fw_cfg_cmdline_dev->resource[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 				fw_cfg_cmdline_dev->resource[0].start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 				fw_cfg_cmdline_dev->resource[1].start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 				fw_cfg_cmdline_dev->resource[2].start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 		return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_4_FMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 				resource_size(&fw_cfg_cmdline_dev->resource[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 				fw_cfg_cmdline_dev->resource[0].start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 				fw_cfg_cmdline_dev->resource[1].start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 				fw_cfg_cmdline_dev->resource[2].start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 				fw_cfg_cmdline_dev->resource[3].start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	/* Should never get here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 	WARN(1, "Unexpected number of resources: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 		fw_cfg_cmdline_dev->num_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) static const struct kernel_param_ops fw_cfg_cmdline_param_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	.set = fw_cfg_cmdline_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	.get = fw_cfg_cmdline_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) #endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) static int __init fw_cfg_sysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	/* create /sys/firmware/qemu_fw_cfg/ top level directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 	if (!fw_cfg_top_ko)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 	ret = platform_driver_register(&fw_cfg_sysfs_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 		fw_cfg_kobj_cleanup(fw_cfg_top_ko);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) static void __exit fw_cfg_sysfs_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 	platform_driver_unregister(&fw_cfg_sysfs_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	platform_device_unregister(fw_cfg_cmdline_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 	/* clean up /sys/firmware/qemu_fw_cfg/ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	fw_cfg_kobj_cleanup(fw_cfg_top_ko);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) module_init(fw_cfg_sysfs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) module_exit(fw_cfg_sysfs_exit);