xref: /openbmc/linux/drivers/firmware/qemu_fw_cfg.c (revision 6b698713d4e99e5a64e2d07f0df90545809227b9)
175f3e8e4SGabriel Somlo /*
275f3e8e4SGabriel Somlo  * drivers/firmware/qemu_fw_cfg.c
375f3e8e4SGabriel Somlo  *
475f3e8e4SGabriel Somlo  * Copyright 2015 Carnegie Mellon University
575f3e8e4SGabriel Somlo  *
675f3e8e4SGabriel Somlo  * Expose entries from QEMU's firmware configuration (fw_cfg) device in
775f3e8e4SGabriel Somlo  * sysfs (read-only, under "/sys/firmware/qemu_fw_cfg/...").
875f3e8e4SGabriel Somlo  *
975f3e8e4SGabriel Somlo  * The fw_cfg device may be instantiated via either an ACPI node (on x86
1075f3e8e4SGabriel Somlo  * and select subsets of aarch64), a Device Tree node (on arm), or using
1175f3e8e4SGabriel Somlo  * a kernel module (or command line) parameter with the following syntax:
1275f3e8e4SGabriel Somlo  *
1314d1824cSMarc-André Lureau  *      [qemu_fw_cfg.]ioport=<size>@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]
1475f3e8e4SGabriel Somlo  * or
1514d1824cSMarc-André Lureau  *      [qemu_fw_cfg.]mmio=<size>@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]
1675f3e8e4SGabriel Somlo  *
1775f3e8e4SGabriel Somlo  * where:
1875f3e8e4SGabriel Somlo  *      <size>     := size of ioport or mmio range
1975f3e8e4SGabriel Somlo  *      <base>     := physical base address of ioport or mmio range
2075f3e8e4SGabriel Somlo  *      <ctrl_off> := (optional) offset of control register
2175f3e8e4SGabriel Somlo  *      <data_off> := (optional) offset of data register
2214d1824cSMarc-André Lureau  *      <dma_off> := (optional) offset of dma register
2375f3e8e4SGabriel Somlo  *
2475f3e8e4SGabriel Somlo  * e.g.:
2514d1824cSMarc-André Lureau  *      qemu_fw_cfg.ioport=12@0x510:0:1:4	(the default on x86)
2675f3e8e4SGabriel Somlo  * or
2714d1824cSMarc-André Lureau  *      qemu_fw_cfg.mmio=16@0x9020000:8:0:16	(the default on arm)
2875f3e8e4SGabriel Somlo  */
2975f3e8e4SGabriel Somlo 
3075f3e8e4SGabriel Somlo #include <linux/module.h>
311b7369acSArnd Bergmann #include <linux/mod_devicetable.h>
3275f3e8e4SGabriel Somlo #include <linux/platform_device.h>
3375f3e8e4SGabriel Somlo #include <linux/acpi.h>
3475f3e8e4SGabriel Somlo #include <linux/slab.h>
3575f3e8e4SGabriel Somlo #include <linux/io.h>
3675f3e8e4SGabriel Somlo #include <linux/ioport.h>
371f57bc12SMarc-André Lureau #include <uapi/linux/qemu_fw_cfg.h>
382d6d60a3SMarc-André Lureau #include <linux/delay.h>
392d6d60a3SMarc-André Lureau #include <linux/crash_dump.h>
402d6d60a3SMarc-André Lureau #include <linux/crash_core.h>
4175f3e8e4SGabriel Somlo 
4275f3e8e4SGabriel Somlo MODULE_AUTHOR("Gabriel L. Somlo <somlo@cmu.edu>");
4375f3e8e4SGabriel Somlo MODULE_DESCRIPTION("QEMU fw_cfg sysfs support");
4475f3e8e4SGabriel Somlo MODULE_LICENSE("GPL");
4575f3e8e4SGabriel Somlo 
462d6d60a3SMarc-André Lureau /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */
472d6d60a3SMarc-André Lureau static u32 fw_cfg_rev;
482d6d60a3SMarc-André Lureau 
4975f3e8e4SGabriel Somlo /* fw_cfg device i/o register addresses */
5075f3e8e4SGabriel Somlo static bool fw_cfg_is_mmio;
5175f3e8e4SGabriel Somlo static phys_addr_t fw_cfg_p_base;
5275f3e8e4SGabriel Somlo static resource_size_t fw_cfg_p_size;
5375f3e8e4SGabriel Somlo static void __iomem *fw_cfg_dev_base;
5475f3e8e4SGabriel Somlo static void __iomem *fw_cfg_reg_ctrl;
5575f3e8e4SGabriel Somlo static void __iomem *fw_cfg_reg_data;
5614d1824cSMarc-André Lureau static void __iomem *fw_cfg_reg_dma;
5775f3e8e4SGabriel Somlo 
5875f3e8e4SGabriel Somlo /* atomic access to fw_cfg device (potentially slow i/o, so using mutex) */
5975f3e8e4SGabriel Somlo static DEFINE_MUTEX(fw_cfg_dev_lock);
6075f3e8e4SGabriel Somlo 
6175f3e8e4SGabriel Somlo /* pick appropriate endianness for selector key */
628d59d5bdSMarc-André Lureau static void fw_cfg_sel_endianness(u16 key)
6375f3e8e4SGabriel Somlo {
648d59d5bdSMarc-André Lureau 	if (fw_cfg_is_mmio)
658d59d5bdSMarc-André Lureau 		iowrite16be(key, fw_cfg_reg_ctrl);
668d59d5bdSMarc-André Lureau 	else
678d59d5bdSMarc-André Lureau 		iowrite16(key, fw_cfg_reg_ctrl);
6875f3e8e4SGabriel Somlo }
6975f3e8e4SGabriel Somlo 
702d6d60a3SMarc-André Lureau #ifdef CONFIG_CRASH_CORE
712d6d60a3SMarc-André Lureau static inline bool fw_cfg_dma_enabled(void)
722d6d60a3SMarc-André Lureau {
732d6d60a3SMarc-André Lureau 	return (fw_cfg_rev & FW_CFG_VERSION_DMA) && fw_cfg_reg_dma;
742d6d60a3SMarc-André Lureau }
752d6d60a3SMarc-André Lureau 
762d6d60a3SMarc-André Lureau /* qemu fw_cfg device is sync today, but spec says it may become async */
772d6d60a3SMarc-André Lureau static void fw_cfg_wait_for_control(struct fw_cfg_dma_access *d)
782d6d60a3SMarc-André Lureau {
792d6d60a3SMarc-André Lureau 	for (;;) {
802d6d60a3SMarc-André Lureau 		u32 ctrl = be32_to_cpu(READ_ONCE(d->control));
812d6d60a3SMarc-André Lureau 
822d6d60a3SMarc-André Lureau 		/* do not reorder the read to d->control */
832d6d60a3SMarc-André Lureau 		rmb();
842d6d60a3SMarc-André Lureau 		if ((ctrl & ~FW_CFG_DMA_CTL_ERROR) == 0)
852d6d60a3SMarc-André Lureau 			return;
862d6d60a3SMarc-André Lureau 
872d6d60a3SMarc-André Lureau 		cpu_relax();
882d6d60a3SMarc-André Lureau 	}
892d6d60a3SMarc-André Lureau }
902d6d60a3SMarc-André Lureau 
912d6d60a3SMarc-André Lureau static ssize_t fw_cfg_dma_transfer(void *address, u32 length, u32 control)
922d6d60a3SMarc-André Lureau {
932d6d60a3SMarc-André Lureau 	phys_addr_t dma;
942d6d60a3SMarc-André Lureau 	struct fw_cfg_dma_access *d = NULL;
952d6d60a3SMarc-André Lureau 	ssize_t ret = length;
962d6d60a3SMarc-André Lureau 
972d6d60a3SMarc-André Lureau 	d = kmalloc(sizeof(*d), GFP_KERNEL);
982d6d60a3SMarc-André Lureau 	if (!d) {
992d6d60a3SMarc-André Lureau 		ret = -ENOMEM;
1002d6d60a3SMarc-André Lureau 		goto end;
1012d6d60a3SMarc-André Lureau 	}
1022d6d60a3SMarc-André Lureau 
1032d6d60a3SMarc-André Lureau 	/* fw_cfg device does not need IOMMU protection, so use physical addresses */
1042d6d60a3SMarc-André Lureau 	*d = (struct fw_cfg_dma_access) {
1052d6d60a3SMarc-André Lureau 		.address = cpu_to_be64(address ? virt_to_phys(address) : 0),
1062d6d60a3SMarc-André Lureau 		.length = cpu_to_be32(length),
1072d6d60a3SMarc-André Lureau 		.control = cpu_to_be32(control)
1082d6d60a3SMarc-André Lureau 	};
1092d6d60a3SMarc-André Lureau 
1102d6d60a3SMarc-André Lureau 	dma = virt_to_phys(d);
1112d6d60a3SMarc-André Lureau 
1122d6d60a3SMarc-André Lureau 	iowrite32be((u64)dma >> 32, fw_cfg_reg_dma);
1132d6d60a3SMarc-André Lureau 	/* force memory to sync before notifying device via MMIO */
1142d6d60a3SMarc-André Lureau 	wmb();
1152d6d60a3SMarc-André Lureau 	iowrite32be(dma, fw_cfg_reg_dma + 4);
1162d6d60a3SMarc-André Lureau 
1172d6d60a3SMarc-André Lureau 	fw_cfg_wait_for_control(d);
1182d6d60a3SMarc-André Lureau 
1192d6d60a3SMarc-André Lureau 	if (be32_to_cpu(READ_ONCE(d->control)) & FW_CFG_DMA_CTL_ERROR) {
1202d6d60a3SMarc-André Lureau 		ret = -EIO;
1212d6d60a3SMarc-André Lureau 	}
1222d6d60a3SMarc-André Lureau 
1232d6d60a3SMarc-André Lureau end:
1242d6d60a3SMarc-André Lureau 	kfree(d);
1252d6d60a3SMarc-André Lureau 
1262d6d60a3SMarc-André Lureau 	return ret;
1272d6d60a3SMarc-André Lureau }
1282d6d60a3SMarc-André Lureau #endif
1292d6d60a3SMarc-André Lureau 
13075f3e8e4SGabriel Somlo /* read chunk of given fw_cfg blob (caller responsible for sanity-check) */
131b1cc4097SMarc-André Lureau static ssize_t fw_cfg_read_blob(u16 key,
13275f3e8e4SGabriel Somlo 				void *buf, loff_t pos, size_t count)
13375f3e8e4SGabriel Somlo {
134d4f6e272SDan Carpenter 	u32 glk = -1U;
135def7ac80SGabriel Somlo 	acpi_status status;
136def7ac80SGabriel Somlo 
137def7ac80SGabriel Somlo 	/* If we have ACPI, ensure mutual exclusion against any potential
138def7ac80SGabriel Somlo 	 * device access by the firmware, e.g. via AML methods:
139def7ac80SGabriel Somlo 	 */
140def7ac80SGabriel Somlo 	status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
141def7ac80SGabriel Somlo 	if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
142def7ac80SGabriel Somlo 		/* Should never get here */
143def7ac80SGabriel Somlo 		WARN(1, "fw_cfg_read_blob: Failed to lock ACPI!\n");
144def7ac80SGabriel Somlo 		memset(buf, 0, count);
145b1cc4097SMarc-André Lureau 		return -EINVAL;
146def7ac80SGabriel Somlo 	}
147def7ac80SGabriel Somlo 
14875f3e8e4SGabriel Somlo 	mutex_lock(&fw_cfg_dev_lock);
1498d59d5bdSMarc-André Lureau 	fw_cfg_sel_endianness(key);
15075f3e8e4SGabriel Somlo 	while (pos-- > 0)
15175f3e8e4SGabriel Somlo 		ioread8(fw_cfg_reg_data);
15275f3e8e4SGabriel Somlo 	ioread8_rep(fw_cfg_reg_data, buf, count);
15375f3e8e4SGabriel Somlo 	mutex_unlock(&fw_cfg_dev_lock);
154def7ac80SGabriel Somlo 
155def7ac80SGabriel Somlo 	acpi_release_global_lock(glk);
156b1cc4097SMarc-André Lureau 	return count;
15775f3e8e4SGabriel Somlo }
15875f3e8e4SGabriel Somlo 
1592d6d60a3SMarc-André Lureau #ifdef CONFIG_CRASH_CORE
1602d6d60a3SMarc-André Lureau /* write chunk of given fw_cfg blob (caller responsible for sanity-check) */
1612d6d60a3SMarc-André Lureau static ssize_t fw_cfg_write_blob(u16 key,
1622d6d60a3SMarc-André Lureau 				 void *buf, loff_t pos, size_t count)
1632d6d60a3SMarc-André Lureau {
1642d6d60a3SMarc-André Lureau 	u32 glk = -1U;
1652d6d60a3SMarc-André Lureau 	acpi_status status;
1662d6d60a3SMarc-André Lureau 	ssize_t ret = count;
1672d6d60a3SMarc-André Lureau 
1682d6d60a3SMarc-André Lureau 	/* If we have ACPI, ensure mutual exclusion against any potential
1692d6d60a3SMarc-André Lureau 	 * device access by the firmware, e.g. via AML methods:
1702d6d60a3SMarc-André Lureau 	 */
1712d6d60a3SMarc-André Lureau 	status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
1722d6d60a3SMarc-André Lureau 	if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
1732d6d60a3SMarc-André Lureau 		/* Should never get here */
1742d6d60a3SMarc-André Lureau 		WARN(1, "%s: Failed to lock ACPI!\n", __func__);
1752d6d60a3SMarc-André Lureau 		return -EINVAL;
1762d6d60a3SMarc-André Lureau 	}
1772d6d60a3SMarc-André Lureau 
1782d6d60a3SMarc-André Lureau 	mutex_lock(&fw_cfg_dev_lock);
1792d6d60a3SMarc-André Lureau 	if (pos == 0) {
1802d6d60a3SMarc-André Lureau 		ret = fw_cfg_dma_transfer(buf, count, key << 16
1812d6d60a3SMarc-André Lureau 					  | FW_CFG_DMA_CTL_SELECT
1822d6d60a3SMarc-André Lureau 					  | FW_CFG_DMA_CTL_WRITE);
1832d6d60a3SMarc-André Lureau 	} else {
1842d6d60a3SMarc-André Lureau 		fw_cfg_sel_endianness(key);
1852d6d60a3SMarc-André Lureau 		ret = fw_cfg_dma_transfer(NULL, pos, FW_CFG_DMA_CTL_SKIP);
1862d6d60a3SMarc-André Lureau 		if (ret < 0)
1872d6d60a3SMarc-André Lureau 			goto end;
1882d6d60a3SMarc-André Lureau 		ret = fw_cfg_dma_transfer(buf, count, FW_CFG_DMA_CTL_WRITE);
1892d6d60a3SMarc-André Lureau 	}
1902d6d60a3SMarc-André Lureau 
1912d6d60a3SMarc-André Lureau end:
1922d6d60a3SMarc-André Lureau 	mutex_unlock(&fw_cfg_dev_lock);
1932d6d60a3SMarc-André Lureau 
1942d6d60a3SMarc-André Lureau 	acpi_release_global_lock(glk);
1952d6d60a3SMarc-André Lureau 
1962d6d60a3SMarc-André Lureau 	return ret;
1972d6d60a3SMarc-André Lureau }
1982d6d60a3SMarc-André Lureau #endif /* CONFIG_CRASH_CORE */
1992d6d60a3SMarc-André Lureau 
20075f3e8e4SGabriel Somlo /* clean up fw_cfg device i/o */
20175f3e8e4SGabriel Somlo static void fw_cfg_io_cleanup(void)
20275f3e8e4SGabriel Somlo {
20375f3e8e4SGabriel Somlo 	if (fw_cfg_is_mmio) {
20475f3e8e4SGabriel Somlo 		iounmap(fw_cfg_dev_base);
20575f3e8e4SGabriel Somlo 		release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
20675f3e8e4SGabriel Somlo 	} else {
20775f3e8e4SGabriel Somlo 		ioport_unmap(fw_cfg_dev_base);
20875f3e8e4SGabriel Somlo 		release_region(fw_cfg_p_base, fw_cfg_p_size);
20975f3e8e4SGabriel Somlo 	}
21075f3e8e4SGabriel Somlo }
21175f3e8e4SGabriel Somlo 
21275f3e8e4SGabriel Somlo /* arch-specific ctrl & data register offsets are not available in ACPI, DT */
2139b3ec23aSValentin Rothberg #if !(defined(FW_CFG_CTRL_OFF) && defined(FW_CFG_DATA_OFF))
21475f3e8e4SGabriel Somlo # if (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
21575f3e8e4SGabriel Somlo #  define FW_CFG_CTRL_OFF 0x08
21675f3e8e4SGabriel Somlo #  define FW_CFG_DATA_OFF 0x00
21714d1824cSMarc-André Lureau #  define FW_CFG_DMA_OFF 0x10
218*6b698713SHelge Deller # elif defined(CONFIG_PARISC)	/* parisc */
219*6b698713SHelge Deller #  define FW_CFG_CTRL_OFF 0x00
220*6b698713SHelge Deller #  define FW_CFG_DATA_OFF 0x04
22175f3e8e4SGabriel Somlo # elif (defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC32)) /* ppc/mac,sun4m */
22275f3e8e4SGabriel Somlo #  define FW_CFG_CTRL_OFF 0x00
22375f3e8e4SGabriel Somlo #  define FW_CFG_DATA_OFF 0x02
22475f3e8e4SGabriel Somlo # elif (defined(CONFIG_X86) || defined(CONFIG_SPARC64)) /* x86, sun4u */
22575f3e8e4SGabriel Somlo #  define FW_CFG_CTRL_OFF 0x00
22675f3e8e4SGabriel Somlo #  define FW_CFG_DATA_OFF 0x01
22714d1824cSMarc-André Lureau #  define FW_CFG_DMA_OFF 0x04
22875f3e8e4SGabriel Somlo # else
22900411b7bSGabriel Somlo #  error "QEMU FW_CFG not available on this architecture!"
23075f3e8e4SGabriel Somlo # endif
23175f3e8e4SGabriel Somlo #endif
23275f3e8e4SGabriel Somlo 
23375f3e8e4SGabriel Somlo /* initialize fw_cfg device i/o from platform data */
23475f3e8e4SGabriel Somlo static int fw_cfg_do_platform_probe(struct platform_device *pdev)
23575f3e8e4SGabriel Somlo {
23675f3e8e4SGabriel Somlo 	char sig[FW_CFG_SIG_SIZE];
23714d1824cSMarc-André Lureau 	struct resource *range, *ctrl, *data, *dma;
23875f3e8e4SGabriel Somlo 
23975f3e8e4SGabriel Somlo 	/* acquire i/o range details */
24075f3e8e4SGabriel Somlo 	fw_cfg_is_mmio = false;
24175f3e8e4SGabriel Somlo 	range = platform_get_resource(pdev, IORESOURCE_IO, 0);
24275f3e8e4SGabriel Somlo 	if (!range) {
24375f3e8e4SGabriel Somlo 		fw_cfg_is_mmio = true;
24475f3e8e4SGabriel Somlo 		range = platform_get_resource(pdev, IORESOURCE_MEM, 0);
24575f3e8e4SGabriel Somlo 		if (!range)
24675f3e8e4SGabriel Somlo 			return -EINVAL;
24775f3e8e4SGabriel Somlo 	}
24875f3e8e4SGabriel Somlo 	fw_cfg_p_base = range->start;
24975f3e8e4SGabriel Somlo 	fw_cfg_p_size = resource_size(range);
25075f3e8e4SGabriel Somlo 
25175f3e8e4SGabriel Somlo 	if (fw_cfg_is_mmio) {
25275f3e8e4SGabriel Somlo 		if (!request_mem_region(fw_cfg_p_base,
25375f3e8e4SGabriel Somlo 					fw_cfg_p_size, "fw_cfg_mem"))
25475f3e8e4SGabriel Somlo 			return -EBUSY;
25575f3e8e4SGabriel Somlo 		fw_cfg_dev_base = ioremap(fw_cfg_p_base, fw_cfg_p_size);
25675f3e8e4SGabriel Somlo 		if (!fw_cfg_dev_base) {
25775f3e8e4SGabriel Somlo 			release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
25875f3e8e4SGabriel Somlo 			return -EFAULT;
25975f3e8e4SGabriel Somlo 		}
26075f3e8e4SGabriel Somlo 	} else {
26175f3e8e4SGabriel Somlo 		if (!request_region(fw_cfg_p_base,
26275f3e8e4SGabriel Somlo 				    fw_cfg_p_size, "fw_cfg_io"))
26375f3e8e4SGabriel Somlo 			return -EBUSY;
26475f3e8e4SGabriel Somlo 		fw_cfg_dev_base = ioport_map(fw_cfg_p_base, fw_cfg_p_size);
26575f3e8e4SGabriel Somlo 		if (!fw_cfg_dev_base) {
26675f3e8e4SGabriel Somlo 			release_region(fw_cfg_p_base, fw_cfg_p_size);
26775f3e8e4SGabriel Somlo 			return -EFAULT;
26875f3e8e4SGabriel Somlo 		}
26975f3e8e4SGabriel Somlo 	}
27075f3e8e4SGabriel Somlo 
27175f3e8e4SGabriel Somlo 	/* were custom register offsets provided (e.g. on the command line)? */
27275f3e8e4SGabriel Somlo 	ctrl = platform_get_resource_byname(pdev, IORESOURCE_REG, "ctrl");
27375f3e8e4SGabriel Somlo 	data = platform_get_resource_byname(pdev, IORESOURCE_REG, "data");
27414d1824cSMarc-André Lureau 	dma = platform_get_resource_byname(pdev, IORESOURCE_REG, "dma");
27575f3e8e4SGabriel Somlo 	if (ctrl && data) {
27675f3e8e4SGabriel Somlo 		fw_cfg_reg_ctrl = fw_cfg_dev_base + ctrl->start;
27775f3e8e4SGabriel Somlo 		fw_cfg_reg_data = fw_cfg_dev_base + data->start;
27875f3e8e4SGabriel Somlo 	} else {
27975f3e8e4SGabriel Somlo 		/* use architecture-specific offsets */
28075f3e8e4SGabriel Somlo 		fw_cfg_reg_ctrl = fw_cfg_dev_base + FW_CFG_CTRL_OFF;
28175f3e8e4SGabriel Somlo 		fw_cfg_reg_data = fw_cfg_dev_base + FW_CFG_DATA_OFF;
28275f3e8e4SGabriel Somlo 	}
28375f3e8e4SGabriel Somlo 
28414d1824cSMarc-André Lureau 	if (dma)
28514d1824cSMarc-André Lureau 		fw_cfg_reg_dma = fw_cfg_dev_base + dma->start;
28614d1824cSMarc-André Lureau #ifdef FW_CFG_DMA_OFF
28714d1824cSMarc-André Lureau 	else
28814d1824cSMarc-André Lureau 		fw_cfg_reg_dma = fw_cfg_dev_base + FW_CFG_DMA_OFF;
28914d1824cSMarc-André Lureau #endif
29014d1824cSMarc-André Lureau 
29175f3e8e4SGabriel Somlo 	/* verify fw_cfg device signature */
292b1cc4097SMarc-André Lureau 	if (fw_cfg_read_blob(FW_CFG_SIGNATURE, sig,
293b1cc4097SMarc-André Lureau 				0, FW_CFG_SIG_SIZE) < 0 ||
294b1cc4097SMarc-André Lureau 		memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) != 0) {
29575f3e8e4SGabriel Somlo 		fw_cfg_io_cleanup();
29675f3e8e4SGabriel Somlo 		return -ENODEV;
29775f3e8e4SGabriel Somlo 	}
29875f3e8e4SGabriel Somlo 
29975f3e8e4SGabriel Somlo 	return 0;
30075f3e8e4SGabriel Somlo }
30175f3e8e4SGabriel Somlo 
30275f3e8e4SGabriel Somlo static ssize_t fw_cfg_showrev(struct kobject *k, struct attribute *a, char *buf)
30375f3e8e4SGabriel Somlo {
30475f3e8e4SGabriel Somlo 	return sprintf(buf, "%u\n", fw_cfg_rev);
30575f3e8e4SGabriel Somlo }
30675f3e8e4SGabriel Somlo 
30775f3e8e4SGabriel Somlo static const struct {
30875f3e8e4SGabriel Somlo 	struct attribute attr;
30975f3e8e4SGabriel Somlo 	ssize_t (*show)(struct kobject *k, struct attribute *a, char *buf);
31075f3e8e4SGabriel Somlo } fw_cfg_rev_attr = {
31175f3e8e4SGabriel Somlo 	.attr = { .name = "rev", .mode = S_IRUSR },
31275f3e8e4SGabriel Somlo 	.show = fw_cfg_showrev,
31375f3e8e4SGabriel Somlo };
31475f3e8e4SGabriel Somlo 
31575f3e8e4SGabriel Somlo /* fw_cfg_sysfs_entry type */
31675f3e8e4SGabriel Somlo struct fw_cfg_sysfs_entry {
31775f3e8e4SGabriel Somlo 	struct kobject kobj;
318d174ea7dSMarc-André Lureau 	u32 size;
319d174ea7dSMarc-André Lureau 	u16 select;
320d174ea7dSMarc-André Lureau 	char name[FW_CFG_MAX_FILE_PATH];
32175f3e8e4SGabriel Somlo 	struct list_head list;
32275f3e8e4SGabriel Somlo };
32375f3e8e4SGabriel Somlo 
3242d6d60a3SMarc-André Lureau #ifdef CONFIG_CRASH_CORE
3252d6d60a3SMarc-André Lureau static ssize_t fw_cfg_write_vmcoreinfo(const struct fw_cfg_file *f)
3262d6d60a3SMarc-André Lureau {
3272d6d60a3SMarc-André Lureau 	static struct fw_cfg_vmcoreinfo *data;
3282d6d60a3SMarc-André Lureau 	ssize_t ret;
3292d6d60a3SMarc-André Lureau 
3302d6d60a3SMarc-André Lureau 	data = kmalloc(sizeof(struct fw_cfg_vmcoreinfo), GFP_KERNEL);
3312d6d60a3SMarc-André Lureau 	if (!data)
3322d6d60a3SMarc-André Lureau 		return -ENOMEM;
3332d6d60a3SMarc-André Lureau 
3342d6d60a3SMarc-André Lureau 	*data = (struct fw_cfg_vmcoreinfo) {
3352d6d60a3SMarc-André Lureau 		.guest_format = cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF),
3362d6d60a3SMarc-André Lureau 		.size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
3372d6d60a3SMarc-André Lureau 		.paddr = cpu_to_le64(paddr_vmcoreinfo_note())
3382d6d60a3SMarc-André Lureau 	};
3392d6d60a3SMarc-André Lureau 	/* spare ourself reading host format support for now since we
3402d6d60a3SMarc-André Lureau 	 * don't know what else to format - host may ignore ours
3412d6d60a3SMarc-André Lureau 	 */
3422d6d60a3SMarc-André Lureau 	ret = fw_cfg_write_blob(be16_to_cpu(f->select), data,
3432d6d60a3SMarc-André Lureau 				0, sizeof(struct fw_cfg_vmcoreinfo));
3442d6d60a3SMarc-André Lureau 
3452d6d60a3SMarc-André Lureau 	kfree(data);
3462d6d60a3SMarc-André Lureau 	return ret;
3472d6d60a3SMarc-André Lureau }
3482d6d60a3SMarc-André Lureau #endif /* CONFIG_CRASH_CORE */
3492d6d60a3SMarc-André Lureau 
35075f3e8e4SGabriel Somlo /* get fw_cfg_sysfs_entry from kobject member */
35175f3e8e4SGabriel Somlo static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
35275f3e8e4SGabriel Somlo {
35375f3e8e4SGabriel Somlo 	return container_of(kobj, struct fw_cfg_sysfs_entry, kobj);
35475f3e8e4SGabriel Somlo }
35575f3e8e4SGabriel Somlo 
35675f3e8e4SGabriel Somlo /* fw_cfg_sysfs_attribute type */
35775f3e8e4SGabriel Somlo struct fw_cfg_sysfs_attribute {
35875f3e8e4SGabriel Somlo 	struct attribute attr;
35975f3e8e4SGabriel Somlo 	ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf);
36075f3e8e4SGabriel Somlo };
36175f3e8e4SGabriel Somlo 
36275f3e8e4SGabriel Somlo /* get fw_cfg_sysfs_attribute from attribute member */
36375f3e8e4SGabriel Somlo static inline struct fw_cfg_sysfs_attribute *to_attr(struct attribute *attr)
36475f3e8e4SGabriel Somlo {
36575f3e8e4SGabriel Somlo 	return container_of(attr, struct fw_cfg_sysfs_attribute, attr);
36675f3e8e4SGabriel Somlo }
36775f3e8e4SGabriel Somlo 
36875f3e8e4SGabriel Somlo /* global cache of fw_cfg_sysfs_entry objects */
36975f3e8e4SGabriel Somlo static LIST_HEAD(fw_cfg_entry_cache);
37075f3e8e4SGabriel Somlo 
37175f3e8e4SGabriel Somlo /* kobjects removed lazily by kernel, mutual exclusion needed */
37275f3e8e4SGabriel Somlo static DEFINE_SPINLOCK(fw_cfg_cache_lock);
37375f3e8e4SGabriel Somlo 
37475f3e8e4SGabriel Somlo static inline void fw_cfg_sysfs_cache_enlist(struct fw_cfg_sysfs_entry *entry)
37575f3e8e4SGabriel Somlo {
37675f3e8e4SGabriel Somlo 	spin_lock(&fw_cfg_cache_lock);
37775f3e8e4SGabriel Somlo 	list_add_tail(&entry->list, &fw_cfg_entry_cache);
37875f3e8e4SGabriel Somlo 	spin_unlock(&fw_cfg_cache_lock);
37975f3e8e4SGabriel Somlo }
38075f3e8e4SGabriel Somlo 
38175f3e8e4SGabriel Somlo static inline void fw_cfg_sysfs_cache_delist(struct fw_cfg_sysfs_entry *entry)
38275f3e8e4SGabriel Somlo {
38375f3e8e4SGabriel Somlo 	spin_lock(&fw_cfg_cache_lock);
38475f3e8e4SGabriel Somlo 	list_del(&entry->list);
38575f3e8e4SGabriel Somlo 	spin_unlock(&fw_cfg_cache_lock);
38675f3e8e4SGabriel Somlo }
38775f3e8e4SGabriel Somlo 
38875f3e8e4SGabriel Somlo static void fw_cfg_sysfs_cache_cleanup(void)
38975f3e8e4SGabriel Somlo {
39075f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_entry *entry, *next;
39175f3e8e4SGabriel Somlo 
39275f3e8e4SGabriel Somlo 	list_for_each_entry_safe(entry, next, &fw_cfg_entry_cache, list) {
39375f3e8e4SGabriel Somlo 		/* will end up invoking fw_cfg_sysfs_cache_delist()
39475f3e8e4SGabriel Somlo 		 * via each object's release() method (i.e. destructor)
39575f3e8e4SGabriel Somlo 		 */
39675f3e8e4SGabriel Somlo 		kobject_put(&entry->kobj);
39775f3e8e4SGabriel Somlo 	}
39875f3e8e4SGabriel Somlo }
39975f3e8e4SGabriel Somlo 
40075f3e8e4SGabriel Somlo /* default_attrs: per-entry attributes and show methods */
40175f3e8e4SGabriel Somlo 
40275f3e8e4SGabriel Somlo #define FW_CFG_SYSFS_ATTR(_attr) \
40375f3e8e4SGabriel Somlo struct fw_cfg_sysfs_attribute fw_cfg_sysfs_attr_##_attr = { \
40475f3e8e4SGabriel Somlo 	.attr = { .name = __stringify(_attr), .mode = S_IRUSR }, \
40575f3e8e4SGabriel Somlo 	.show = fw_cfg_sysfs_show_##_attr, \
40675f3e8e4SGabriel Somlo }
40775f3e8e4SGabriel Somlo 
40875f3e8e4SGabriel Somlo static ssize_t fw_cfg_sysfs_show_size(struct fw_cfg_sysfs_entry *e, char *buf)
40975f3e8e4SGabriel Somlo {
410d174ea7dSMarc-André Lureau 	return sprintf(buf, "%u\n", e->size);
41175f3e8e4SGabriel Somlo }
41275f3e8e4SGabriel Somlo 
41375f3e8e4SGabriel Somlo static ssize_t fw_cfg_sysfs_show_key(struct fw_cfg_sysfs_entry *e, char *buf)
41475f3e8e4SGabriel Somlo {
415d174ea7dSMarc-André Lureau 	return sprintf(buf, "%u\n", e->select);
41675f3e8e4SGabriel Somlo }
41775f3e8e4SGabriel Somlo 
41875f3e8e4SGabriel Somlo static ssize_t fw_cfg_sysfs_show_name(struct fw_cfg_sysfs_entry *e, char *buf)
41975f3e8e4SGabriel Somlo {
420d174ea7dSMarc-André Lureau 	return sprintf(buf, "%s\n", e->name);
42175f3e8e4SGabriel Somlo }
42275f3e8e4SGabriel Somlo 
42375f3e8e4SGabriel Somlo static FW_CFG_SYSFS_ATTR(size);
42475f3e8e4SGabriel Somlo static FW_CFG_SYSFS_ATTR(key);
42575f3e8e4SGabriel Somlo static FW_CFG_SYSFS_ATTR(name);
42675f3e8e4SGabriel Somlo 
42775f3e8e4SGabriel Somlo static struct attribute *fw_cfg_sysfs_entry_attrs[] = {
42875f3e8e4SGabriel Somlo 	&fw_cfg_sysfs_attr_size.attr,
42975f3e8e4SGabriel Somlo 	&fw_cfg_sysfs_attr_key.attr,
43075f3e8e4SGabriel Somlo 	&fw_cfg_sysfs_attr_name.attr,
43175f3e8e4SGabriel Somlo 	NULL,
43275f3e8e4SGabriel Somlo };
43375f3e8e4SGabriel Somlo 
43475f3e8e4SGabriel Somlo /* sysfs_ops: find fw_cfg_[entry, attribute] and call appropriate show method */
43575f3e8e4SGabriel Somlo static ssize_t fw_cfg_sysfs_attr_show(struct kobject *kobj, struct attribute *a,
43675f3e8e4SGabriel Somlo 				      char *buf)
43775f3e8e4SGabriel Somlo {
43875f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
43975f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_attribute *attr = to_attr(a);
44075f3e8e4SGabriel Somlo 
44175f3e8e4SGabriel Somlo 	return attr->show(entry, buf);
44275f3e8e4SGabriel Somlo }
44375f3e8e4SGabriel Somlo 
44475f3e8e4SGabriel Somlo static const struct sysfs_ops fw_cfg_sysfs_attr_ops = {
44575f3e8e4SGabriel Somlo 	.show = fw_cfg_sysfs_attr_show,
44675f3e8e4SGabriel Somlo };
44775f3e8e4SGabriel Somlo 
44875f3e8e4SGabriel Somlo /* release: destructor, to be called via kobject_put() */
44975f3e8e4SGabriel Somlo static void fw_cfg_sysfs_release_entry(struct kobject *kobj)
45075f3e8e4SGabriel Somlo {
45175f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
45275f3e8e4SGabriel Somlo 
45375f3e8e4SGabriel Somlo 	fw_cfg_sysfs_cache_delist(entry);
45475f3e8e4SGabriel Somlo 	kfree(entry);
45575f3e8e4SGabriel Somlo }
45675f3e8e4SGabriel Somlo 
45775f3e8e4SGabriel Somlo /* kobj_type: ties together all properties required to register an entry */
45875f3e8e4SGabriel Somlo static struct kobj_type fw_cfg_sysfs_entry_ktype = {
45975f3e8e4SGabriel Somlo 	.default_attrs = fw_cfg_sysfs_entry_attrs,
46075f3e8e4SGabriel Somlo 	.sysfs_ops = &fw_cfg_sysfs_attr_ops,
46175f3e8e4SGabriel Somlo 	.release = fw_cfg_sysfs_release_entry,
46275f3e8e4SGabriel Somlo };
46375f3e8e4SGabriel Somlo 
46475f3e8e4SGabriel Somlo /* raw-read method and attribute */
46575f3e8e4SGabriel Somlo static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj,
46675f3e8e4SGabriel Somlo 				     struct bin_attribute *bin_attr,
46775f3e8e4SGabriel Somlo 				     char *buf, loff_t pos, size_t count)
46875f3e8e4SGabriel Somlo {
46975f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
47075f3e8e4SGabriel Somlo 
471d174ea7dSMarc-André Lureau 	if (pos > entry->size)
47275f3e8e4SGabriel Somlo 		return -EINVAL;
47375f3e8e4SGabriel Somlo 
474d174ea7dSMarc-André Lureau 	if (count > entry->size - pos)
475d174ea7dSMarc-André Lureau 		count = entry->size - pos;
47675f3e8e4SGabriel Somlo 
477b1cc4097SMarc-André Lureau 	return fw_cfg_read_blob(entry->select, buf, pos, count);
47875f3e8e4SGabriel Somlo }
47975f3e8e4SGabriel Somlo 
48075f3e8e4SGabriel Somlo static struct bin_attribute fw_cfg_sysfs_attr_raw = {
48175f3e8e4SGabriel Somlo 	.attr = { .name = "raw", .mode = S_IRUSR },
48275f3e8e4SGabriel Somlo 	.read = fw_cfg_sysfs_read_raw,
48375f3e8e4SGabriel Somlo };
48475f3e8e4SGabriel Somlo 
485246c46ebSGabriel Somlo /*
486246c46ebSGabriel Somlo  * Create a kset subdirectory matching each '/' delimited dirname token
487246c46ebSGabriel Somlo  * in 'name', starting with sysfs kset/folder 'dir'; At the end, create
488246c46ebSGabriel Somlo  * a symlink directed at the given 'target'.
489246c46ebSGabriel Somlo  * NOTE: We do this on a best-effort basis, since 'name' is not guaranteed
490246c46ebSGabriel Somlo  * to be a well-behaved path name. Whenever a symlink vs. kset directory
491246c46ebSGabriel Somlo  * name collision occurs, the kernel will issue big scary warnings while
492246c46ebSGabriel Somlo  * refusing to add the offending link or directory. We follow up with our
493246c46ebSGabriel Somlo  * own, slightly less scary error messages explaining the situation :)
494246c46ebSGabriel Somlo  */
495246c46ebSGabriel Somlo static int fw_cfg_build_symlink(struct kset *dir,
496246c46ebSGabriel Somlo 				struct kobject *target, const char *name)
497246c46ebSGabriel Somlo {
498246c46ebSGabriel Somlo 	int ret;
499246c46ebSGabriel Somlo 	struct kset *subdir;
500246c46ebSGabriel Somlo 	struct kobject *ko;
501246c46ebSGabriel Somlo 	char *name_copy, *p, *tok;
502246c46ebSGabriel Somlo 
503246c46ebSGabriel Somlo 	if (!dir || !target || !name || !*name)
504246c46ebSGabriel Somlo 		return -EINVAL;
505246c46ebSGabriel Somlo 
506246c46ebSGabriel Somlo 	/* clone a copy of name for parsing */
507246c46ebSGabriel Somlo 	name_copy = p = kstrdup(name, GFP_KERNEL);
508246c46ebSGabriel Somlo 	if (!name_copy)
509246c46ebSGabriel Somlo 		return -ENOMEM;
510246c46ebSGabriel Somlo 
511246c46ebSGabriel Somlo 	/* create folders for each dirname token, then symlink for basename */
512246c46ebSGabriel Somlo 	while ((tok = strsep(&p, "/")) && *tok) {
513246c46ebSGabriel Somlo 
514246c46ebSGabriel Somlo 		/* last (basename) token? If so, add symlink here */
515246c46ebSGabriel Somlo 		if (!p || !*p) {
516246c46ebSGabriel Somlo 			ret = sysfs_create_link(&dir->kobj, target, tok);
517246c46ebSGabriel Somlo 			break;
518246c46ebSGabriel Somlo 		}
519246c46ebSGabriel Somlo 
520246c46ebSGabriel Somlo 		/* does the current dir contain an item named after tok ? */
521246c46ebSGabriel Somlo 		ko = kset_find_obj(dir, tok);
522246c46ebSGabriel Somlo 		if (ko) {
523246c46ebSGabriel Somlo 			/* drop reference added by kset_find_obj */
524246c46ebSGabriel Somlo 			kobject_put(ko);
525246c46ebSGabriel Somlo 
526246c46ebSGabriel Somlo 			/* ko MUST be a kset - we're about to use it as one ! */
527246c46ebSGabriel Somlo 			if (ko->ktype != dir->kobj.ktype) {
528246c46ebSGabriel Somlo 				ret = -EINVAL;
529246c46ebSGabriel Somlo 				break;
530246c46ebSGabriel Somlo 			}
531246c46ebSGabriel Somlo 
532246c46ebSGabriel Somlo 			/* descend into already existing subdirectory */
533246c46ebSGabriel Somlo 			dir = to_kset(ko);
534246c46ebSGabriel Somlo 		} else {
535246c46ebSGabriel Somlo 			/* create new subdirectory kset */
536246c46ebSGabriel Somlo 			subdir = kzalloc(sizeof(struct kset), GFP_KERNEL);
537246c46ebSGabriel Somlo 			if (!subdir) {
538246c46ebSGabriel Somlo 				ret = -ENOMEM;
539246c46ebSGabriel Somlo 				break;
540246c46ebSGabriel Somlo 			}
541246c46ebSGabriel Somlo 			subdir->kobj.kset = dir;
542246c46ebSGabriel Somlo 			subdir->kobj.ktype = dir->kobj.ktype;
543246c46ebSGabriel Somlo 			ret = kobject_set_name(&subdir->kobj, "%s", tok);
544246c46ebSGabriel Somlo 			if (ret) {
545246c46ebSGabriel Somlo 				kfree(subdir);
546246c46ebSGabriel Somlo 				break;
547246c46ebSGabriel Somlo 			}
548246c46ebSGabriel Somlo 			ret = kset_register(subdir);
549246c46ebSGabriel Somlo 			if (ret) {
550246c46ebSGabriel Somlo 				kfree(subdir);
551246c46ebSGabriel Somlo 				break;
552246c46ebSGabriel Somlo 			}
553246c46ebSGabriel Somlo 
554246c46ebSGabriel Somlo 			/* descend into newly created subdirectory */
555246c46ebSGabriel Somlo 			dir = subdir;
556246c46ebSGabriel Somlo 		}
557246c46ebSGabriel Somlo 	}
558246c46ebSGabriel Somlo 
559246c46ebSGabriel Somlo 	/* we're done with cloned copy of name */
560246c46ebSGabriel Somlo 	kfree(name_copy);
561246c46ebSGabriel Somlo 	return ret;
562246c46ebSGabriel Somlo }
563246c46ebSGabriel Somlo 
564246c46ebSGabriel Somlo /* recursively unregister fw_cfg/by_name/ kset directory tree */
565246c46ebSGabriel Somlo static void fw_cfg_kset_unregister_recursive(struct kset *kset)
566246c46ebSGabriel Somlo {
567246c46ebSGabriel Somlo 	struct kobject *k, *next;
568246c46ebSGabriel Somlo 
569246c46ebSGabriel Somlo 	list_for_each_entry_safe(k, next, &kset->list, entry)
570246c46ebSGabriel Somlo 		/* all set members are ksets too, but check just in case... */
571246c46ebSGabriel Somlo 		if (k->ktype == kset->kobj.ktype)
572246c46ebSGabriel Somlo 			fw_cfg_kset_unregister_recursive(to_kset(k));
573246c46ebSGabriel Somlo 
574246c46ebSGabriel Somlo 	/* symlinks are cleanly and automatically removed with the directory */
575246c46ebSGabriel Somlo 	kset_unregister(kset);
576246c46ebSGabriel Somlo }
577246c46ebSGabriel Somlo 
578246c46ebSGabriel Somlo /* kobjects & kset representing top-level, by_key, and by_name folders */
57975f3e8e4SGabriel Somlo static struct kobject *fw_cfg_top_ko;
58075f3e8e4SGabriel Somlo static struct kobject *fw_cfg_sel_ko;
581246c46ebSGabriel Somlo static struct kset *fw_cfg_fname_kset;
58275f3e8e4SGabriel Somlo 
58375f3e8e4SGabriel Somlo /* register an individual fw_cfg file */
58475f3e8e4SGabriel Somlo static int fw_cfg_register_file(const struct fw_cfg_file *f)
58575f3e8e4SGabriel Somlo {
58675f3e8e4SGabriel Somlo 	int err;
58775f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_entry *entry;
58875f3e8e4SGabriel Somlo 
5892d6d60a3SMarc-André Lureau #ifdef CONFIG_CRASH_CORE
5902d6d60a3SMarc-André Lureau 	if (fw_cfg_dma_enabled() &&
5912d6d60a3SMarc-André Lureau 		strcmp(f->name, FW_CFG_VMCOREINFO_FILENAME) == 0 &&
5922d6d60a3SMarc-André Lureau 		!is_kdump_kernel()) {
5932d6d60a3SMarc-André Lureau 		if (fw_cfg_write_vmcoreinfo(f) < 0)
5942d6d60a3SMarc-André Lureau 			pr_warn("fw_cfg: failed to write vmcoreinfo");
5952d6d60a3SMarc-André Lureau 	}
5962d6d60a3SMarc-André Lureau #endif
5972d6d60a3SMarc-André Lureau 
59875f3e8e4SGabriel Somlo 	/* allocate new entry */
59975f3e8e4SGabriel Somlo 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
60075f3e8e4SGabriel Somlo 	if (!entry)
60175f3e8e4SGabriel Somlo 		return -ENOMEM;
60275f3e8e4SGabriel Somlo 
60375f3e8e4SGabriel Somlo 	/* set file entry information */
604d174ea7dSMarc-André Lureau 	entry->size = be32_to_cpu(f->size);
605d174ea7dSMarc-André Lureau 	entry->select = be16_to_cpu(f->select);
606d174ea7dSMarc-André Lureau 	memcpy(entry->name, f->name, FW_CFG_MAX_FILE_PATH);
60775f3e8e4SGabriel Somlo 
60875f3e8e4SGabriel Somlo 	/* register entry under "/sys/firmware/qemu_fw_cfg/by_key/" */
60975f3e8e4SGabriel Somlo 	err = kobject_init_and_add(&entry->kobj, &fw_cfg_sysfs_entry_ktype,
610d174ea7dSMarc-André Lureau 				   fw_cfg_sel_ko, "%d", entry->select);
611fe3c6068SQiushi Wu 	if (err) {
612fe3c6068SQiushi Wu 		kobject_put(&entry->kobj);
613fe3c6068SQiushi Wu 		return err;
614fe3c6068SQiushi Wu 	}
61575f3e8e4SGabriel Somlo 
61675f3e8e4SGabriel Somlo 	/* add raw binary content access */
61775f3e8e4SGabriel Somlo 	err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw);
61875f3e8e4SGabriel Somlo 	if (err)
61975f3e8e4SGabriel Somlo 		goto err_add_raw;
62075f3e8e4SGabriel Somlo 
621246c46ebSGabriel Somlo 	/* try adding "/sys/firmware/qemu_fw_cfg/by_name/" symlink */
622d174ea7dSMarc-André Lureau 	fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->name);
623246c46ebSGabriel Somlo 
62475f3e8e4SGabriel Somlo 	/* success, add entry to global cache */
62575f3e8e4SGabriel Somlo 	fw_cfg_sysfs_cache_enlist(entry);
62675f3e8e4SGabriel Somlo 	return 0;
62775f3e8e4SGabriel Somlo 
62875f3e8e4SGabriel Somlo err_add_raw:
62975f3e8e4SGabriel Somlo 	kobject_del(&entry->kobj);
63075f3e8e4SGabriel Somlo 	kfree(entry);
63175f3e8e4SGabriel Somlo 	return err;
63275f3e8e4SGabriel Somlo }
63375f3e8e4SGabriel Somlo 
63475f3e8e4SGabriel Somlo /* iterate over all fw_cfg directory entries, registering each one */
63575f3e8e4SGabriel Somlo static int fw_cfg_register_dir_entries(void)
63675f3e8e4SGabriel Somlo {
63775f3e8e4SGabriel Somlo 	int ret = 0;
6383d47a34bSMarc-André Lureau 	__be32 files_count;
63975f3e8e4SGabriel Somlo 	u32 count, i;
64075f3e8e4SGabriel Somlo 	struct fw_cfg_file *dir;
64175f3e8e4SGabriel Somlo 	size_t dir_size;
64275f3e8e4SGabriel Somlo 
643b1cc4097SMarc-André Lureau 	ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, &files_count,
644b1cc4097SMarc-André Lureau 			0, sizeof(files_count));
645b1cc4097SMarc-André Lureau 	if (ret < 0)
646b1cc4097SMarc-André Lureau 		return ret;
647b1cc4097SMarc-André Lureau 
6483d47a34bSMarc-André Lureau 	count = be32_to_cpu(files_count);
64975f3e8e4SGabriel Somlo 	dir_size = count * sizeof(struct fw_cfg_file);
65075f3e8e4SGabriel Somlo 
65175f3e8e4SGabriel Somlo 	dir = kmalloc(dir_size, GFP_KERNEL);
65275f3e8e4SGabriel Somlo 	if (!dir)
65375f3e8e4SGabriel Somlo 		return -ENOMEM;
65475f3e8e4SGabriel Somlo 
655b1cc4097SMarc-André Lureau 	ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, dir,
656b1cc4097SMarc-André Lureau 			sizeof(files_count), dir_size);
657b1cc4097SMarc-André Lureau 	if (ret < 0)
658b1cc4097SMarc-André Lureau 		goto end;
65975f3e8e4SGabriel Somlo 
66075f3e8e4SGabriel Somlo 	for (i = 0; i < count; i++) {
66175f3e8e4SGabriel Somlo 		ret = fw_cfg_register_file(&dir[i]);
66275f3e8e4SGabriel Somlo 		if (ret)
66375f3e8e4SGabriel Somlo 			break;
66475f3e8e4SGabriel Somlo 	}
66575f3e8e4SGabriel Somlo 
666b1cc4097SMarc-André Lureau end:
66775f3e8e4SGabriel Somlo 	kfree(dir);
66875f3e8e4SGabriel Somlo 	return ret;
66975f3e8e4SGabriel Somlo }
67075f3e8e4SGabriel Somlo 
67175f3e8e4SGabriel Somlo /* unregister top-level or by_key folder */
67275f3e8e4SGabriel Somlo static inline void fw_cfg_kobj_cleanup(struct kobject *kobj)
67375f3e8e4SGabriel Somlo {
67475f3e8e4SGabriel Somlo 	kobject_del(kobj);
67575f3e8e4SGabriel Somlo 	kobject_put(kobj);
67675f3e8e4SGabriel Somlo }
67775f3e8e4SGabriel Somlo 
67875f3e8e4SGabriel Somlo static int fw_cfg_sysfs_probe(struct platform_device *pdev)
67975f3e8e4SGabriel Somlo {
68075f3e8e4SGabriel Somlo 	int err;
681f295c8dbSMarc-André Lureau 	__le32 rev;
68275f3e8e4SGabriel Somlo 
68375f3e8e4SGabriel Somlo 	/* NOTE: If we supported multiple fw_cfg devices, we'd first create
68475f3e8e4SGabriel Somlo 	 * a subdirectory named after e.g. pdev->id, then hang per-device
685246c46ebSGabriel Somlo 	 * by_key (and by_name) subdirectories underneath it. However, only
68675f3e8e4SGabriel Somlo 	 * one fw_cfg device exist system-wide, so if one was already found
68775f3e8e4SGabriel Somlo 	 * earlier, we might as well stop here.
68875f3e8e4SGabriel Somlo 	 */
68975f3e8e4SGabriel Somlo 	if (fw_cfg_sel_ko)
69075f3e8e4SGabriel Somlo 		return -EBUSY;
69175f3e8e4SGabriel Somlo 
692246c46ebSGabriel Somlo 	/* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
69375f3e8e4SGabriel Somlo 	err = -ENOMEM;
69475f3e8e4SGabriel Somlo 	fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
69575f3e8e4SGabriel Somlo 	if (!fw_cfg_sel_ko)
69675f3e8e4SGabriel Somlo 		goto err_sel;
697246c46ebSGabriel Somlo 	fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko);
698246c46ebSGabriel Somlo 	if (!fw_cfg_fname_kset)
699246c46ebSGabriel Somlo 		goto err_name;
70075f3e8e4SGabriel Somlo 
70175f3e8e4SGabriel Somlo 	/* initialize fw_cfg device i/o from platform data */
70275f3e8e4SGabriel Somlo 	err = fw_cfg_do_platform_probe(pdev);
70375f3e8e4SGabriel Somlo 	if (err)
70475f3e8e4SGabriel Somlo 		goto err_probe;
70575f3e8e4SGabriel Somlo 
70675f3e8e4SGabriel Somlo 	/* get revision number, add matching top-level attribute */
707b1cc4097SMarc-André Lureau 	err = fw_cfg_read_blob(FW_CFG_ID, &rev, 0, sizeof(rev));
708b1cc4097SMarc-André Lureau 	if (err < 0)
709b1cc4097SMarc-André Lureau 		goto err_probe;
710b1cc4097SMarc-André Lureau 
711f295c8dbSMarc-André Lureau 	fw_cfg_rev = le32_to_cpu(rev);
71275f3e8e4SGabriel Somlo 	err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
71375f3e8e4SGabriel Somlo 	if (err)
71475f3e8e4SGabriel Somlo 		goto err_rev;
71575f3e8e4SGabriel Somlo 
71675f3e8e4SGabriel Somlo 	/* process fw_cfg file directory entry, registering each file */
71775f3e8e4SGabriel Somlo 	err = fw_cfg_register_dir_entries();
71875f3e8e4SGabriel Somlo 	if (err)
71975f3e8e4SGabriel Somlo 		goto err_dir;
72075f3e8e4SGabriel Somlo 
72175f3e8e4SGabriel Somlo 	/* success */
72275f3e8e4SGabriel Somlo 	pr_debug("fw_cfg: loaded.\n");
72375f3e8e4SGabriel Somlo 	return 0;
72475f3e8e4SGabriel Somlo 
72575f3e8e4SGabriel Somlo err_dir:
72675f3e8e4SGabriel Somlo 	fw_cfg_sysfs_cache_cleanup();
72775f3e8e4SGabriel Somlo 	sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
72875f3e8e4SGabriel Somlo err_rev:
72975f3e8e4SGabriel Somlo 	fw_cfg_io_cleanup();
73075f3e8e4SGabriel Somlo err_probe:
731246c46ebSGabriel Somlo 	fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
732246c46ebSGabriel Somlo err_name:
73375f3e8e4SGabriel Somlo 	fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
73475f3e8e4SGabriel Somlo err_sel:
73575f3e8e4SGabriel Somlo 	return err;
73675f3e8e4SGabriel Somlo }
73775f3e8e4SGabriel Somlo 
73875f3e8e4SGabriel Somlo static int fw_cfg_sysfs_remove(struct platform_device *pdev)
73975f3e8e4SGabriel Somlo {
74075f3e8e4SGabriel Somlo 	pr_debug("fw_cfg: unloading.\n");
74175f3e8e4SGabriel Somlo 	fw_cfg_sysfs_cache_cleanup();
74223f1b8d9SMarc-André Lureau 	sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
74323f1b8d9SMarc-André Lureau 	fw_cfg_io_cleanup();
744246c46ebSGabriel Somlo 	fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
74575f3e8e4SGabriel Somlo 	fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
74675f3e8e4SGabriel Somlo 	return 0;
74775f3e8e4SGabriel Somlo }
74875f3e8e4SGabriel Somlo 
74975f3e8e4SGabriel Somlo static const struct of_device_id fw_cfg_sysfs_mmio_match[] = {
75075f3e8e4SGabriel Somlo 	{ .compatible = "qemu,fw-cfg-mmio", },
75175f3e8e4SGabriel Somlo 	{},
75275f3e8e4SGabriel Somlo };
75375f3e8e4SGabriel Somlo MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match);
75475f3e8e4SGabriel Somlo 
75575f3e8e4SGabriel Somlo #ifdef CONFIG_ACPI
75675f3e8e4SGabriel Somlo static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = {
7571f57bc12SMarc-André Lureau 	{ FW_CFG_ACPI_DEVICE_ID, },
75875f3e8e4SGabriel Somlo 	{},
75975f3e8e4SGabriel Somlo };
76075f3e8e4SGabriel Somlo MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match);
76175f3e8e4SGabriel Somlo #endif
76275f3e8e4SGabriel Somlo 
76375f3e8e4SGabriel Somlo static struct platform_driver fw_cfg_sysfs_driver = {
76475f3e8e4SGabriel Somlo 	.probe = fw_cfg_sysfs_probe,
76575f3e8e4SGabriel Somlo 	.remove = fw_cfg_sysfs_remove,
76675f3e8e4SGabriel Somlo 	.driver = {
76775f3e8e4SGabriel Somlo 		.name = "fw_cfg",
76875f3e8e4SGabriel Somlo 		.of_match_table = fw_cfg_sysfs_mmio_match,
76975f3e8e4SGabriel Somlo 		.acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match),
77075f3e8e4SGabriel Somlo 	},
77175f3e8e4SGabriel Somlo };
77275f3e8e4SGabriel Somlo 
77375f3e8e4SGabriel Somlo #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
77475f3e8e4SGabriel Somlo 
77575f3e8e4SGabriel Somlo static struct platform_device *fw_cfg_cmdline_dev;
77675f3e8e4SGabriel Somlo 
77775f3e8e4SGabriel Somlo /* this probably belongs in e.g. include/linux/types.h,
77875f3e8e4SGabriel Somlo  * but right now we are the only ones doing it...
77975f3e8e4SGabriel Somlo  */
78075f3e8e4SGabriel Somlo #ifdef CONFIG_PHYS_ADDR_T_64BIT
78175f3e8e4SGabriel Somlo #define __PHYS_ADDR_PREFIX "ll"
78275f3e8e4SGabriel Somlo #else
78375f3e8e4SGabriel Somlo #define __PHYS_ADDR_PREFIX ""
78475f3e8e4SGabriel Somlo #endif
78575f3e8e4SGabriel Somlo 
78675f3e8e4SGabriel Somlo /* use special scanf/printf modifier for phys_addr_t, resource_size_t */
78775f3e8e4SGabriel Somlo #define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \
78875f3e8e4SGabriel Somlo 			 ":%" __PHYS_ADDR_PREFIX "i" \
78914d1824cSMarc-André Lureau 			 ":%" __PHYS_ADDR_PREFIX "i%n" \
79075f3e8e4SGabriel Somlo 			 ":%" __PHYS_ADDR_PREFIX "i%n"
79175f3e8e4SGabriel Somlo 
79275f3e8e4SGabriel Somlo #define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \
79375f3e8e4SGabriel Somlo 			 "0x%" __PHYS_ADDR_PREFIX "x"
79475f3e8e4SGabriel Somlo 
79575f3e8e4SGabriel Somlo #define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \
79675f3e8e4SGabriel Somlo 			 ":%" __PHYS_ADDR_PREFIX "u" \
79775f3e8e4SGabriel Somlo 			 ":%" __PHYS_ADDR_PREFIX "u"
79875f3e8e4SGabriel Somlo 
79914d1824cSMarc-André Lureau #define PH_ADDR_PR_4_FMT PH_ADDR_PR_3_FMT \
80014d1824cSMarc-André Lureau 			 ":%" __PHYS_ADDR_PREFIX "u"
80114d1824cSMarc-André Lureau 
80275f3e8e4SGabriel Somlo static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp)
80375f3e8e4SGabriel Somlo {
80414d1824cSMarc-André Lureau 	struct resource res[4] = {};
80575f3e8e4SGabriel Somlo 	char *str;
80675f3e8e4SGabriel Somlo 	phys_addr_t base;
80714d1824cSMarc-André Lureau 	resource_size_t size, ctrl_off, data_off, dma_off;
80875f3e8e4SGabriel Somlo 	int processed, consumed = 0;
80975f3e8e4SGabriel Somlo 
81075f3e8e4SGabriel Somlo 	/* only one fw_cfg device can exist system-wide, so if one
81175f3e8e4SGabriel Somlo 	 * was processed on the command line already, we might as
81275f3e8e4SGabriel Somlo 	 * well stop here.
81375f3e8e4SGabriel Somlo 	 */
81475f3e8e4SGabriel Somlo 	if (fw_cfg_cmdline_dev) {
81575f3e8e4SGabriel Somlo 		/* avoid leaking previously registered device */
81675f3e8e4SGabriel Somlo 		platform_device_unregister(fw_cfg_cmdline_dev);
81775f3e8e4SGabriel Somlo 		return -EINVAL;
81875f3e8e4SGabriel Somlo 	}
81975f3e8e4SGabriel Somlo 
82075f3e8e4SGabriel Somlo 	/* consume "<size>" portion of command line argument */
82175f3e8e4SGabriel Somlo 	size = memparse(arg, &str);
82275f3e8e4SGabriel Somlo 
82314d1824cSMarc-André Lureau 	/* get "@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]" chunks */
82475f3e8e4SGabriel Somlo 	processed = sscanf(str, PH_ADDR_SCAN_FMT,
82575f3e8e4SGabriel Somlo 			   &base, &consumed,
82614d1824cSMarc-André Lureau 			   &ctrl_off, &data_off, &consumed,
82714d1824cSMarc-André Lureau 			   &dma_off, &consumed);
82875f3e8e4SGabriel Somlo 
82914d1824cSMarc-André Lureau 	/* sscanf() must process precisely 1, 3 or 4 chunks:
83075f3e8e4SGabriel Somlo 	 * <base> is mandatory, optionally followed by <ctrl_off>
83114d1824cSMarc-André Lureau 	 * and <data_off>, and <dma_off>;
83275f3e8e4SGabriel Somlo 	 * there must be no extra characters after the last chunk,
83375f3e8e4SGabriel Somlo 	 * so str[consumed] must be '\0'.
83475f3e8e4SGabriel Somlo 	 */
83575f3e8e4SGabriel Somlo 	if (str[consumed] ||
83614d1824cSMarc-André Lureau 	    (processed != 1 && processed != 3 && processed != 4))
83775f3e8e4SGabriel Somlo 		return -EINVAL;
83875f3e8e4SGabriel Somlo 
83975f3e8e4SGabriel Somlo 	res[0].start = base;
84075f3e8e4SGabriel Somlo 	res[0].end = base + size - 1;
84175f3e8e4SGabriel Somlo 	res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM :
84275f3e8e4SGabriel Somlo 						   IORESOURCE_IO;
84375f3e8e4SGabriel Somlo 
84475f3e8e4SGabriel Somlo 	/* insert register offsets, if provided */
84575f3e8e4SGabriel Somlo 	if (processed > 1) {
84675f3e8e4SGabriel Somlo 		res[1].name = "ctrl";
84775f3e8e4SGabriel Somlo 		res[1].start = ctrl_off;
84875f3e8e4SGabriel Somlo 		res[1].flags = IORESOURCE_REG;
84975f3e8e4SGabriel Somlo 		res[2].name = "data";
85075f3e8e4SGabriel Somlo 		res[2].start = data_off;
85175f3e8e4SGabriel Somlo 		res[2].flags = IORESOURCE_REG;
85275f3e8e4SGabriel Somlo 	}
85314d1824cSMarc-André Lureau 	if (processed > 3) {
85414d1824cSMarc-André Lureau 		res[3].name = "dma";
85514d1824cSMarc-André Lureau 		res[3].start = dma_off;
85614d1824cSMarc-André Lureau 		res[3].flags = IORESOURCE_REG;
85714d1824cSMarc-André Lureau 	}
85875f3e8e4SGabriel Somlo 
85975f3e8e4SGabriel Somlo 	/* "processed" happens to nicely match the number of resources
86075f3e8e4SGabriel Somlo 	 * we need to pass in to this platform device.
86175f3e8e4SGabriel Somlo 	 */
86275f3e8e4SGabriel Somlo 	fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg",
86375f3e8e4SGabriel Somlo 					PLATFORM_DEVID_NONE, res, processed);
86475f3e8e4SGabriel Somlo 
8650a9e63aaSVasyl Gomonovych 	return PTR_ERR_OR_ZERO(fw_cfg_cmdline_dev);
86675f3e8e4SGabriel Somlo }
86775f3e8e4SGabriel Somlo 
86875f3e8e4SGabriel Somlo static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp)
86975f3e8e4SGabriel Somlo {
87075f3e8e4SGabriel Somlo 	/* stay silent if device was not configured via the command
87175f3e8e4SGabriel Somlo 	 * line, or if the parameter name (ioport/mmio) doesn't match
87275f3e8e4SGabriel Somlo 	 * the device setting
87375f3e8e4SGabriel Somlo 	 */
87475f3e8e4SGabriel Somlo 	if (!fw_cfg_cmdline_dev ||
87575f3e8e4SGabriel Somlo 	    (!strcmp(kp->name, "mmio") ^
87675f3e8e4SGabriel Somlo 	     (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM)))
87775f3e8e4SGabriel Somlo 		return 0;
87875f3e8e4SGabriel Somlo 
87975f3e8e4SGabriel Somlo 	switch (fw_cfg_cmdline_dev->num_resources) {
88075f3e8e4SGabriel Somlo 	case 1:
88175f3e8e4SGabriel Somlo 		return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT,
88275f3e8e4SGabriel Somlo 				resource_size(&fw_cfg_cmdline_dev->resource[0]),
88375f3e8e4SGabriel Somlo 				fw_cfg_cmdline_dev->resource[0].start);
88475f3e8e4SGabriel Somlo 	case 3:
88575f3e8e4SGabriel Somlo 		return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT,
88675f3e8e4SGabriel Somlo 				resource_size(&fw_cfg_cmdline_dev->resource[0]),
88775f3e8e4SGabriel Somlo 				fw_cfg_cmdline_dev->resource[0].start,
88875f3e8e4SGabriel Somlo 				fw_cfg_cmdline_dev->resource[1].start,
88975f3e8e4SGabriel Somlo 				fw_cfg_cmdline_dev->resource[2].start);
89014d1824cSMarc-André Lureau 	case 4:
89114d1824cSMarc-André Lureau 		return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_4_FMT,
89214d1824cSMarc-André Lureau 				resource_size(&fw_cfg_cmdline_dev->resource[0]),
89314d1824cSMarc-André Lureau 				fw_cfg_cmdline_dev->resource[0].start,
89414d1824cSMarc-André Lureau 				fw_cfg_cmdline_dev->resource[1].start,
89514d1824cSMarc-André Lureau 				fw_cfg_cmdline_dev->resource[2].start,
89614d1824cSMarc-André Lureau 				fw_cfg_cmdline_dev->resource[3].start);
89775f3e8e4SGabriel Somlo 	}
89875f3e8e4SGabriel Somlo 
89975f3e8e4SGabriel Somlo 	/* Should never get here */
90075f3e8e4SGabriel Somlo 	WARN(1, "Unexpected number of resources: %d\n",
90175f3e8e4SGabriel Somlo 		fw_cfg_cmdline_dev->num_resources);
90275f3e8e4SGabriel Somlo 	return 0;
90375f3e8e4SGabriel Somlo }
90475f3e8e4SGabriel Somlo 
90575f3e8e4SGabriel Somlo static const struct kernel_param_ops fw_cfg_cmdline_param_ops = {
90675f3e8e4SGabriel Somlo 	.set = fw_cfg_cmdline_set,
90775f3e8e4SGabriel Somlo 	.get = fw_cfg_cmdline_get,
90875f3e8e4SGabriel Somlo };
90975f3e8e4SGabriel Somlo 
91075f3e8e4SGabriel Somlo device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
91175f3e8e4SGabriel Somlo device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
91275f3e8e4SGabriel Somlo 
91375f3e8e4SGabriel Somlo #endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */
91475f3e8e4SGabriel Somlo 
91575f3e8e4SGabriel Somlo static int __init fw_cfg_sysfs_init(void)
91675f3e8e4SGabriel Somlo {
917e8aabc64SMichael S. Tsirkin 	int ret;
918e8aabc64SMichael S. Tsirkin 
91975f3e8e4SGabriel Somlo 	/* create /sys/firmware/qemu_fw_cfg/ top level directory */
92075f3e8e4SGabriel Somlo 	fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj);
92175f3e8e4SGabriel Somlo 	if (!fw_cfg_top_ko)
92275f3e8e4SGabriel Somlo 		return -ENOMEM;
92375f3e8e4SGabriel Somlo 
924e8aabc64SMichael S. Tsirkin 	ret = platform_driver_register(&fw_cfg_sysfs_driver);
925e8aabc64SMichael S. Tsirkin 	if (ret)
926e8aabc64SMichael S. Tsirkin 		fw_cfg_kobj_cleanup(fw_cfg_top_ko);
927e8aabc64SMichael S. Tsirkin 
928e8aabc64SMichael S. Tsirkin 	return ret;
92975f3e8e4SGabriel Somlo }
93075f3e8e4SGabriel Somlo 
93175f3e8e4SGabriel Somlo static void __exit fw_cfg_sysfs_exit(void)
93275f3e8e4SGabriel Somlo {
93375f3e8e4SGabriel Somlo 	platform_driver_unregister(&fw_cfg_sysfs_driver);
93475f3e8e4SGabriel Somlo 
93575f3e8e4SGabriel Somlo #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
93675f3e8e4SGabriel Somlo 	platform_device_unregister(fw_cfg_cmdline_dev);
93775f3e8e4SGabriel Somlo #endif
93875f3e8e4SGabriel Somlo 
93975f3e8e4SGabriel Somlo 	/* clean up /sys/firmware/qemu_fw_cfg/ */
94075f3e8e4SGabriel Somlo 	fw_cfg_kobj_cleanup(fw_cfg_top_ko);
94175f3e8e4SGabriel Somlo }
94275f3e8e4SGabriel Somlo 
94375f3e8e4SGabriel Somlo module_init(fw_cfg_sysfs_init);
94475f3e8e4SGabriel Somlo module_exit(fw_cfg_sysfs_exit);
945