xref: /openbmc/linux/drivers/firmware/qemu_fw_cfg.c (revision 14d1824c768d363f78e4b9e8a98b200aacc29c0f)
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  *
13*14d1824cSMarc-André Lureau  *      [qemu_fw_cfg.]ioport=<size>@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]
1475f3e8e4SGabriel Somlo  * or
15*14d1824cSMarc-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
22*14d1824cSMarc-André Lureau  *      <dma_off> := (optional) offset of dma register
2375f3e8e4SGabriel Somlo  *
2475f3e8e4SGabriel Somlo  * e.g.:
25*14d1824cSMarc-André Lureau  *      qemu_fw_cfg.ioport=12@0x510:0:1:4	(the default on x86)
2675f3e8e4SGabriel Somlo  * or
27*14d1824cSMarc-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>
3175f3e8e4SGabriel Somlo #include <linux/platform_device.h>
3275f3e8e4SGabriel Somlo #include <linux/acpi.h>
3375f3e8e4SGabriel Somlo #include <linux/slab.h>
3475f3e8e4SGabriel Somlo #include <linux/io.h>
3575f3e8e4SGabriel Somlo #include <linux/ioport.h>
361f57bc12SMarc-André Lureau #include <uapi/linux/qemu_fw_cfg.h>
3775f3e8e4SGabriel Somlo 
3875f3e8e4SGabriel Somlo MODULE_AUTHOR("Gabriel L. Somlo <somlo@cmu.edu>");
3975f3e8e4SGabriel Somlo MODULE_DESCRIPTION("QEMU fw_cfg sysfs support");
4075f3e8e4SGabriel Somlo MODULE_LICENSE("GPL");
4175f3e8e4SGabriel Somlo 
4275f3e8e4SGabriel Somlo /* fw_cfg device i/o register addresses */
4375f3e8e4SGabriel Somlo static bool fw_cfg_is_mmio;
4475f3e8e4SGabriel Somlo static phys_addr_t fw_cfg_p_base;
4575f3e8e4SGabriel Somlo static resource_size_t fw_cfg_p_size;
4675f3e8e4SGabriel Somlo static void __iomem *fw_cfg_dev_base;
4775f3e8e4SGabriel Somlo static void __iomem *fw_cfg_reg_ctrl;
4875f3e8e4SGabriel Somlo static void __iomem *fw_cfg_reg_data;
49*14d1824cSMarc-André Lureau static void __iomem *fw_cfg_reg_dma;
5075f3e8e4SGabriel Somlo 
5175f3e8e4SGabriel Somlo /* atomic access to fw_cfg device (potentially slow i/o, so using mutex) */
5275f3e8e4SGabriel Somlo static DEFINE_MUTEX(fw_cfg_dev_lock);
5375f3e8e4SGabriel Somlo 
5475f3e8e4SGabriel Somlo /* pick appropriate endianness for selector key */
558d59d5bdSMarc-André Lureau static void fw_cfg_sel_endianness(u16 key)
5675f3e8e4SGabriel Somlo {
578d59d5bdSMarc-André Lureau 	if (fw_cfg_is_mmio)
588d59d5bdSMarc-André Lureau 		iowrite16be(key, fw_cfg_reg_ctrl);
598d59d5bdSMarc-André Lureau 	else
608d59d5bdSMarc-André Lureau 		iowrite16(key, fw_cfg_reg_ctrl);
6175f3e8e4SGabriel Somlo }
6275f3e8e4SGabriel Somlo 
6375f3e8e4SGabriel Somlo /* read chunk of given fw_cfg blob (caller responsible for sanity-check) */
64b1cc4097SMarc-André Lureau static ssize_t fw_cfg_read_blob(u16 key,
6575f3e8e4SGabriel Somlo 				void *buf, loff_t pos, size_t count)
6675f3e8e4SGabriel Somlo {
67d4f6e272SDan Carpenter 	u32 glk = -1U;
68def7ac80SGabriel Somlo 	acpi_status status;
69def7ac80SGabriel Somlo 
70def7ac80SGabriel Somlo 	/* If we have ACPI, ensure mutual exclusion against any potential
71def7ac80SGabriel Somlo 	 * device access by the firmware, e.g. via AML methods:
72def7ac80SGabriel Somlo 	 */
73def7ac80SGabriel Somlo 	status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
74def7ac80SGabriel Somlo 	if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
75def7ac80SGabriel Somlo 		/* Should never get here */
76def7ac80SGabriel Somlo 		WARN(1, "fw_cfg_read_blob: Failed to lock ACPI!\n");
77def7ac80SGabriel Somlo 		memset(buf, 0, count);
78b1cc4097SMarc-André Lureau 		return -EINVAL;
79def7ac80SGabriel Somlo 	}
80def7ac80SGabriel Somlo 
8175f3e8e4SGabriel Somlo 	mutex_lock(&fw_cfg_dev_lock);
828d59d5bdSMarc-André Lureau 	fw_cfg_sel_endianness(key);
8375f3e8e4SGabriel Somlo 	while (pos-- > 0)
8475f3e8e4SGabriel Somlo 		ioread8(fw_cfg_reg_data);
8575f3e8e4SGabriel Somlo 	ioread8_rep(fw_cfg_reg_data, buf, count);
8675f3e8e4SGabriel Somlo 	mutex_unlock(&fw_cfg_dev_lock);
87def7ac80SGabriel Somlo 
88def7ac80SGabriel Somlo 	acpi_release_global_lock(glk);
89b1cc4097SMarc-André Lureau 	return count;
9075f3e8e4SGabriel Somlo }
9175f3e8e4SGabriel Somlo 
9275f3e8e4SGabriel Somlo /* clean up fw_cfg device i/o */
9375f3e8e4SGabriel Somlo static void fw_cfg_io_cleanup(void)
9475f3e8e4SGabriel Somlo {
9575f3e8e4SGabriel Somlo 	if (fw_cfg_is_mmio) {
9675f3e8e4SGabriel Somlo 		iounmap(fw_cfg_dev_base);
9775f3e8e4SGabriel Somlo 		release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
9875f3e8e4SGabriel Somlo 	} else {
9975f3e8e4SGabriel Somlo 		ioport_unmap(fw_cfg_dev_base);
10075f3e8e4SGabriel Somlo 		release_region(fw_cfg_p_base, fw_cfg_p_size);
10175f3e8e4SGabriel Somlo 	}
10275f3e8e4SGabriel Somlo }
10375f3e8e4SGabriel Somlo 
10475f3e8e4SGabriel Somlo /* arch-specific ctrl & data register offsets are not available in ACPI, DT */
1059b3ec23aSValentin Rothberg #if !(defined(FW_CFG_CTRL_OFF) && defined(FW_CFG_DATA_OFF))
10675f3e8e4SGabriel Somlo # if (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
10775f3e8e4SGabriel Somlo #  define FW_CFG_CTRL_OFF 0x08
10875f3e8e4SGabriel Somlo #  define FW_CFG_DATA_OFF 0x00
109*14d1824cSMarc-André Lureau #  define FW_CFG_DMA_OFF 0x10
11075f3e8e4SGabriel Somlo # elif (defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC32)) /* ppc/mac,sun4m */
11175f3e8e4SGabriel Somlo #  define FW_CFG_CTRL_OFF 0x00
11275f3e8e4SGabriel Somlo #  define FW_CFG_DATA_OFF 0x02
11375f3e8e4SGabriel Somlo # elif (defined(CONFIG_X86) || defined(CONFIG_SPARC64)) /* x86, sun4u */
11475f3e8e4SGabriel Somlo #  define FW_CFG_CTRL_OFF 0x00
11575f3e8e4SGabriel Somlo #  define FW_CFG_DATA_OFF 0x01
116*14d1824cSMarc-André Lureau #  define FW_CFG_DMA_OFF 0x04
11775f3e8e4SGabriel Somlo # else
11800411b7bSGabriel Somlo #  error "QEMU FW_CFG not available on this architecture!"
11975f3e8e4SGabriel Somlo # endif
12075f3e8e4SGabriel Somlo #endif
12175f3e8e4SGabriel Somlo 
12275f3e8e4SGabriel Somlo /* initialize fw_cfg device i/o from platform data */
12375f3e8e4SGabriel Somlo static int fw_cfg_do_platform_probe(struct platform_device *pdev)
12475f3e8e4SGabriel Somlo {
12575f3e8e4SGabriel Somlo 	char sig[FW_CFG_SIG_SIZE];
126*14d1824cSMarc-André Lureau 	struct resource *range, *ctrl, *data, *dma;
12775f3e8e4SGabriel Somlo 
12875f3e8e4SGabriel Somlo 	/* acquire i/o range details */
12975f3e8e4SGabriel Somlo 	fw_cfg_is_mmio = false;
13075f3e8e4SGabriel Somlo 	range = platform_get_resource(pdev, IORESOURCE_IO, 0);
13175f3e8e4SGabriel Somlo 	if (!range) {
13275f3e8e4SGabriel Somlo 		fw_cfg_is_mmio = true;
13375f3e8e4SGabriel Somlo 		range = platform_get_resource(pdev, IORESOURCE_MEM, 0);
13475f3e8e4SGabriel Somlo 		if (!range)
13575f3e8e4SGabriel Somlo 			return -EINVAL;
13675f3e8e4SGabriel Somlo 	}
13775f3e8e4SGabriel Somlo 	fw_cfg_p_base = range->start;
13875f3e8e4SGabriel Somlo 	fw_cfg_p_size = resource_size(range);
13975f3e8e4SGabriel Somlo 
14075f3e8e4SGabriel Somlo 	if (fw_cfg_is_mmio) {
14175f3e8e4SGabriel Somlo 		if (!request_mem_region(fw_cfg_p_base,
14275f3e8e4SGabriel Somlo 					fw_cfg_p_size, "fw_cfg_mem"))
14375f3e8e4SGabriel Somlo 			return -EBUSY;
14475f3e8e4SGabriel Somlo 		fw_cfg_dev_base = ioremap(fw_cfg_p_base, fw_cfg_p_size);
14575f3e8e4SGabriel Somlo 		if (!fw_cfg_dev_base) {
14675f3e8e4SGabriel Somlo 			release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
14775f3e8e4SGabriel Somlo 			return -EFAULT;
14875f3e8e4SGabriel Somlo 		}
14975f3e8e4SGabriel Somlo 	} else {
15075f3e8e4SGabriel Somlo 		if (!request_region(fw_cfg_p_base,
15175f3e8e4SGabriel Somlo 				    fw_cfg_p_size, "fw_cfg_io"))
15275f3e8e4SGabriel Somlo 			return -EBUSY;
15375f3e8e4SGabriel Somlo 		fw_cfg_dev_base = ioport_map(fw_cfg_p_base, fw_cfg_p_size);
15475f3e8e4SGabriel Somlo 		if (!fw_cfg_dev_base) {
15575f3e8e4SGabriel Somlo 			release_region(fw_cfg_p_base, fw_cfg_p_size);
15675f3e8e4SGabriel Somlo 			return -EFAULT;
15775f3e8e4SGabriel Somlo 		}
15875f3e8e4SGabriel Somlo 	}
15975f3e8e4SGabriel Somlo 
16075f3e8e4SGabriel Somlo 	/* were custom register offsets provided (e.g. on the command line)? */
16175f3e8e4SGabriel Somlo 	ctrl = platform_get_resource_byname(pdev, IORESOURCE_REG, "ctrl");
16275f3e8e4SGabriel Somlo 	data = platform_get_resource_byname(pdev, IORESOURCE_REG, "data");
163*14d1824cSMarc-André Lureau 	dma = platform_get_resource_byname(pdev, IORESOURCE_REG, "dma");
16475f3e8e4SGabriel Somlo 	if (ctrl && data) {
16575f3e8e4SGabriel Somlo 		fw_cfg_reg_ctrl = fw_cfg_dev_base + ctrl->start;
16675f3e8e4SGabriel Somlo 		fw_cfg_reg_data = fw_cfg_dev_base + data->start;
16775f3e8e4SGabriel Somlo 	} else {
16875f3e8e4SGabriel Somlo 		/* use architecture-specific offsets */
16975f3e8e4SGabriel Somlo 		fw_cfg_reg_ctrl = fw_cfg_dev_base + FW_CFG_CTRL_OFF;
17075f3e8e4SGabriel Somlo 		fw_cfg_reg_data = fw_cfg_dev_base + FW_CFG_DATA_OFF;
17175f3e8e4SGabriel Somlo 	}
17275f3e8e4SGabriel Somlo 
173*14d1824cSMarc-André Lureau 	if (dma)
174*14d1824cSMarc-André Lureau 		fw_cfg_reg_dma = fw_cfg_dev_base + dma->start;
175*14d1824cSMarc-André Lureau #ifdef FW_CFG_DMA_OFF
176*14d1824cSMarc-André Lureau 	else
177*14d1824cSMarc-André Lureau 		fw_cfg_reg_dma = fw_cfg_dev_base + FW_CFG_DMA_OFF;
178*14d1824cSMarc-André Lureau #endif
179*14d1824cSMarc-André Lureau 
18075f3e8e4SGabriel Somlo 	/* verify fw_cfg device signature */
181b1cc4097SMarc-André Lureau 	if (fw_cfg_read_blob(FW_CFG_SIGNATURE, sig,
182b1cc4097SMarc-André Lureau 				0, FW_CFG_SIG_SIZE) < 0 ||
183b1cc4097SMarc-André Lureau 		memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) != 0) {
18475f3e8e4SGabriel Somlo 		fw_cfg_io_cleanup();
18575f3e8e4SGabriel Somlo 		return -ENODEV;
18675f3e8e4SGabriel Somlo 	}
18775f3e8e4SGabriel Somlo 
18875f3e8e4SGabriel Somlo 	return 0;
18975f3e8e4SGabriel Somlo }
19075f3e8e4SGabriel Somlo 
19175f3e8e4SGabriel Somlo /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */
19275f3e8e4SGabriel Somlo static u32 fw_cfg_rev;
19375f3e8e4SGabriel Somlo 
19475f3e8e4SGabriel Somlo static ssize_t fw_cfg_showrev(struct kobject *k, struct attribute *a, char *buf)
19575f3e8e4SGabriel Somlo {
19675f3e8e4SGabriel Somlo 	return sprintf(buf, "%u\n", fw_cfg_rev);
19775f3e8e4SGabriel Somlo }
19875f3e8e4SGabriel Somlo 
19975f3e8e4SGabriel Somlo static const struct {
20075f3e8e4SGabriel Somlo 	struct attribute attr;
20175f3e8e4SGabriel Somlo 	ssize_t (*show)(struct kobject *k, struct attribute *a, char *buf);
20275f3e8e4SGabriel Somlo } fw_cfg_rev_attr = {
20375f3e8e4SGabriel Somlo 	.attr = { .name = "rev", .mode = S_IRUSR },
20475f3e8e4SGabriel Somlo 	.show = fw_cfg_showrev,
20575f3e8e4SGabriel Somlo };
20675f3e8e4SGabriel Somlo 
20775f3e8e4SGabriel Somlo /* fw_cfg_sysfs_entry type */
20875f3e8e4SGabriel Somlo struct fw_cfg_sysfs_entry {
20975f3e8e4SGabriel Somlo 	struct kobject kobj;
210d174ea7dSMarc-André Lureau 	u32 size;
211d174ea7dSMarc-André Lureau 	u16 select;
212d174ea7dSMarc-André Lureau 	char name[FW_CFG_MAX_FILE_PATH];
21375f3e8e4SGabriel Somlo 	struct list_head list;
21475f3e8e4SGabriel Somlo };
21575f3e8e4SGabriel Somlo 
21675f3e8e4SGabriel Somlo /* get fw_cfg_sysfs_entry from kobject member */
21775f3e8e4SGabriel Somlo static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
21875f3e8e4SGabriel Somlo {
21975f3e8e4SGabriel Somlo 	return container_of(kobj, struct fw_cfg_sysfs_entry, kobj);
22075f3e8e4SGabriel Somlo }
22175f3e8e4SGabriel Somlo 
22275f3e8e4SGabriel Somlo /* fw_cfg_sysfs_attribute type */
22375f3e8e4SGabriel Somlo struct fw_cfg_sysfs_attribute {
22475f3e8e4SGabriel Somlo 	struct attribute attr;
22575f3e8e4SGabriel Somlo 	ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf);
22675f3e8e4SGabriel Somlo };
22775f3e8e4SGabriel Somlo 
22875f3e8e4SGabriel Somlo /* get fw_cfg_sysfs_attribute from attribute member */
22975f3e8e4SGabriel Somlo static inline struct fw_cfg_sysfs_attribute *to_attr(struct attribute *attr)
23075f3e8e4SGabriel Somlo {
23175f3e8e4SGabriel Somlo 	return container_of(attr, struct fw_cfg_sysfs_attribute, attr);
23275f3e8e4SGabriel Somlo }
23375f3e8e4SGabriel Somlo 
23475f3e8e4SGabriel Somlo /* global cache of fw_cfg_sysfs_entry objects */
23575f3e8e4SGabriel Somlo static LIST_HEAD(fw_cfg_entry_cache);
23675f3e8e4SGabriel Somlo 
23775f3e8e4SGabriel Somlo /* kobjects removed lazily by kernel, mutual exclusion needed */
23875f3e8e4SGabriel Somlo static DEFINE_SPINLOCK(fw_cfg_cache_lock);
23975f3e8e4SGabriel Somlo 
24075f3e8e4SGabriel Somlo static inline void fw_cfg_sysfs_cache_enlist(struct fw_cfg_sysfs_entry *entry)
24175f3e8e4SGabriel Somlo {
24275f3e8e4SGabriel Somlo 	spin_lock(&fw_cfg_cache_lock);
24375f3e8e4SGabriel Somlo 	list_add_tail(&entry->list, &fw_cfg_entry_cache);
24475f3e8e4SGabriel Somlo 	spin_unlock(&fw_cfg_cache_lock);
24575f3e8e4SGabriel Somlo }
24675f3e8e4SGabriel Somlo 
24775f3e8e4SGabriel Somlo static inline void fw_cfg_sysfs_cache_delist(struct fw_cfg_sysfs_entry *entry)
24875f3e8e4SGabriel Somlo {
24975f3e8e4SGabriel Somlo 	spin_lock(&fw_cfg_cache_lock);
25075f3e8e4SGabriel Somlo 	list_del(&entry->list);
25175f3e8e4SGabriel Somlo 	spin_unlock(&fw_cfg_cache_lock);
25275f3e8e4SGabriel Somlo }
25375f3e8e4SGabriel Somlo 
25475f3e8e4SGabriel Somlo static void fw_cfg_sysfs_cache_cleanup(void)
25575f3e8e4SGabriel Somlo {
25675f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_entry *entry, *next;
25775f3e8e4SGabriel Somlo 
25875f3e8e4SGabriel Somlo 	list_for_each_entry_safe(entry, next, &fw_cfg_entry_cache, list) {
25975f3e8e4SGabriel Somlo 		/* will end up invoking fw_cfg_sysfs_cache_delist()
26075f3e8e4SGabriel Somlo 		 * via each object's release() method (i.e. destructor)
26175f3e8e4SGabriel Somlo 		 */
26275f3e8e4SGabriel Somlo 		kobject_put(&entry->kobj);
26375f3e8e4SGabriel Somlo 	}
26475f3e8e4SGabriel Somlo }
26575f3e8e4SGabriel Somlo 
26675f3e8e4SGabriel Somlo /* default_attrs: per-entry attributes and show methods */
26775f3e8e4SGabriel Somlo 
26875f3e8e4SGabriel Somlo #define FW_CFG_SYSFS_ATTR(_attr) \
26975f3e8e4SGabriel Somlo struct fw_cfg_sysfs_attribute fw_cfg_sysfs_attr_##_attr = { \
27075f3e8e4SGabriel Somlo 	.attr = { .name = __stringify(_attr), .mode = S_IRUSR }, \
27175f3e8e4SGabriel Somlo 	.show = fw_cfg_sysfs_show_##_attr, \
27275f3e8e4SGabriel Somlo }
27375f3e8e4SGabriel Somlo 
27475f3e8e4SGabriel Somlo static ssize_t fw_cfg_sysfs_show_size(struct fw_cfg_sysfs_entry *e, char *buf)
27575f3e8e4SGabriel Somlo {
276d174ea7dSMarc-André Lureau 	return sprintf(buf, "%u\n", e->size);
27775f3e8e4SGabriel Somlo }
27875f3e8e4SGabriel Somlo 
27975f3e8e4SGabriel Somlo static ssize_t fw_cfg_sysfs_show_key(struct fw_cfg_sysfs_entry *e, char *buf)
28075f3e8e4SGabriel Somlo {
281d174ea7dSMarc-André Lureau 	return sprintf(buf, "%u\n", e->select);
28275f3e8e4SGabriel Somlo }
28375f3e8e4SGabriel Somlo 
28475f3e8e4SGabriel Somlo static ssize_t fw_cfg_sysfs_show_name(struct fw_cfg_sysfs_entry *e, char *buf)
28575f3e8e4SGabriel Somlo {
286d174ea7dSMarc-André Lureau 	return sprintf(buf, "%s\n", e->name);
28775f3e8e4SGabriel Somlo }
28875f3e8e4SGabriel Somlo 
28975f3e8e4SGabriel Somlo static FW_CFG_SYSFS_ATTR(size);
29075f3e8e4SGabriel Somlo static FW_CFG_SYSFS_ATTR(key);
29175f3e8e4SGabriel Somlo static FW_CFG_SYSFS_ATTR(name);
29275f3e8e4SGabriel Somlo 
29375f3e8e4SGabriel Somlo static struct attribute *fw_cfg_sysfs_entry_attrs[] = {
29475f3e8e4SGabriel Somlo 	&fw_cfg_sysfs_attr_size.attr,
29575f3e8e4SGabriel Somlo 	&fw_cfg_sysfs_attr_key.attr,
29675f3e8e4SGabriel Somlo 	&fw_cfg_sysfs_attr_name.attr,
29775f3e8e4SGabriel Somlo 	NULL,
29875f3e8e4SGabriel Somlo };
29975f3e8e4SGabriel Somlo 
30075f3e8e4SGabriel Somlo /* sysfs_ops: find fw_cfg_[entry, attribute] and call appropriate show method */
30175f3e8e4SGabriel Somlo static ssize_t fw_cfg_sysfs_attr_show(struct kobject *kobj, struct attribute *a,
30275f3e8e4SGabriel Somlo 				      char *buf)
30375f3e8e4SGabriel Somlo {
30475f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
30575f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_attribute *attr = to_attr(a);
30675f3e8e4SGabriel Somlo 
30775f3e8e4SGabriel Somlo 	return attr->show(entry, buf);
30875f3e8e4SGabriel Somlo }
30975f3e8e4SGabriel Somlo 
31075f3e8e4SGabriel Somlo static const struct sysfs_ops fw_cfg_sysfs_attr_ops = {
31175f3e8e4SGabriel Somlo 	.show = fw_cfg_sysfs_attr_show,
31275f3e8e4SGabriel Somlo };
31375f3e8e4SGabriel Somlo 
31475f3e8e4SGabriel Somlo /* release: destructor, to be called via kobject_put() */
31575f3e8e4SGabriel Somlo static void fw_cfg_sysfs_release_entry(struct kobject *kobj)
31675f3e8e4SGabriel Somlo {
31775f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
31875f3e8e4SGabriel Somlo 
31975f3e8e4SGabriel Somlo 	fw_cfg_sysfs_cache_delist(entry);
32075f3e8e4SGabriel Somlo 	kfree(entry);
32175f3e8e4SGabriel Somlo }
32275f3e8e4SGabriel Somlo 
32375f3e8e4SGabriel Somlo /* kobj_type: ties together all properties required to register an entry */
32475f3e8e4SGabriel Somlo static struct kobj_type fw_cfg_sysfs_entry_ktype = {
32575f3e8e4SGabriel Somlo 	.default_attrs = fw_cfg_sysfs_entry_attrs,
32675f3e8e4SGabriel Somlo 	.sysfs_ops = &fw_cfg_sysfs_attr_ops,
32775f3e8e4SGabriel Somlo 	.release = fw_cfg_sysfs_release_entry,
32875f3e8e4SGabriel Somlo };
32975f3e8e4SGabriel Somlo 
33075f3e8e4SGabriel Somlo /* raw-read method and attribute */
33175f3e8e4SGabriel Somlo static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj,
33275f3e8e4SGabriel Somlo 				     struct bin_attribute *bin_attr,
33375f3e8e4SGabriel Somlo 				     char *buf, loff_t pos, size_t count)
33475f3e8e4SGabriel Somlo {
33575f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
33675f3e8e4SGabriel Somlo 
337d174ea7dSMarc-André Lureau 	if (pos > entry->size)
33875f3e8e4SGabriel Somlo 		return -EINVAL;
33975f3e8e4SGabriel Somlo 
340d174ea7dSMarc-André Lureau 	if (count > entry->size - pos)
341d174ea7dSMarc-André Lureau 		count = entry->size - pos;
34275f3e8e4SGabriel Somlo 
343b1cc4097SMarc-André Lureau 	return fw_cfg_read_blob(entry->select, buf, pos, count);
34475f3e8e4SGabriel Somlo }
34575f3e8e4SGabriel Somlo 
34675f3e8e4SGabriel Somlo static struct bin_attribute fw_cfg_sysfs_attr_raw = {
34775f3e8e4SGabriel Somlo 	.attr = { .name = "raw", .mode = S_IRUSR },
34875f3e8e4SGabriel Somlo 	.read = fw_cfg_sysfs_read_raw,
34975f3e8e4SGabriel Somlo };
35075f3e8e4SGabriel Somlo 
351246c46ebSGabriel Somlo /*
352246c46ebSGabriel Somlo  * Create a kset subdirectory matching each '/' delimited dirname token
353246c46ebSGabriel Somlo  * in 'name', starting with sysfs kset/folder 'dir'; At the end, create
354246c46ebSGabriel Somlo  * a symlink directed at the given 'target'.
355246c46ebSGabriel Somlo  * NOTE: We do this on a best-effort basis, since 'name' is not guaranteed
356246c46ebSGabriel Somlo  * to be a well-behaved path name. Whenever a symlink vs. kset directory
357246c46ebSGabriel Somlo  * name collision occurs, the kernel will issue big scary warnings while
358246c46ebSGabriel Somlo  * refusing to add the offending link or directory. We follow up with our
359246c46ebSGabriel Somlo  * own, slightly less scary error messages explaining the situation :)
360246c46ebSGabriel Somlo  */
361246c46ebSGabriel Somlo static int fw_cfg_build_symlink(struct kset *dir,
362246c46ebSGabriel Somlo 				struct kobject *target, const char *name)
363246c46ebSGabriel Somlo {
364246c46ebSGabriel Somlo 	int ret;
365246c46ebSGabriel Somlo 	struct kset *subdir;
366246c46ebSGabriel Somlo 	struct kobject *ko;
367246c46ebSGabriel Somlo 	char *name_copy, *p, *tok;
368246c46ebSGabriel Somlo 
369246c46ebSGabriel Somlo 	if (!dir || !target || !name || !*name)
370246c46ebSGabriel Somlo 		return -EINVAL;
371246c46ebSGabriel Somlo 
372246c46ebSGabriel Somlo 	/* clone a copy of name for parsing */
373246c46ebSGabriel Somlo 	name_copy = p = kstrdup(name, GFP_KERNEL);
374246c46ebSGabriel Somlo 	if (!name_copy)
375246c46ebSGabriel Somlo 		return -ENOMEM;
376246c46ebSGabriel Somlo 
377246c46ebSGabriel Somlo 	/* create folders for each dirname token, then symlink for basename */
378246c46ebSGabriel Somlo 	while ((tok = strsep(&p, "/")) && *tok) {
379246c46ebSGabriel Somlo 
380246c46ebSGabriel Somlo 		/* last (basename) token? If so, add symlink here */
381246c46ebSGabriel Somlo 		if (!p || !*p) {
382246c46ebSGabriel Somlo 			ret = sysfs_create_link(&dir->kobj, target, tok);
383246c46ebSGabriel Somlo 			break;
384246c46ebSGabriel Somlo 		}
385246c46ebSGabriel Somlo 
386246c46ebSGabriel Somlo 		/* does the current dir contain an item named after tok ? */
387246c46ebSGabriel Somlo 		ko = kset_find_obj(dir, tok);
388246c46ebSGabriel Somlo 		if (ko) {
389246c46ebSGabriel Somlo 			/* drop reference added by kset_find_obj */
390246c46ebSGabriel Somlo 			kobject_put(ko);
391246c46ebSGabriel Somlo 
392246c46ebSGabriel Somlo 			/* ko MUST be a kset - we're about to use it as one ! */
393246c46ebSGabriel Somlo 			if (ko->ktype != dir->kobj.ktype) {
394246c46ebSGabriel Somlo 				ret = -EINVAL;
395246c46ebSGabriel Somlo 				break;
396246c46ebSGabriel Somlo 			}
397246c46ebSGabriel Somlo 
398246c46ebSGabriel Somlo 			/* descend into already existing subdirectory */
399246c46ebSGabriel Somlo 			dir = to_kset(ko);
400246c46ebSGabriel Somlo 		} else {
401246c46ebSGabriel Somlo 			/* create new subdirectory kset */
402246c46ebSGabriel Somlo 			subdir = kzalloc(sizeof(struct kset), GFP_KERNEL);
403246c46ebSGabriel Somlo 			if (!subdir) {
404246c46ebSGabriel Somlo 				ret = -ENOMEM;
405246c46ebSGabriel Somlo 				break;
406246c46ebSGabriel Somlo 			}
407246c46ebSGabriel Somlo 			subdir->kobj.kset = dir;
408246c46ebSGabriel Somlo 			subdir->kobj.ktype = dir->kobj.ktype;
409246c46ebSGabriel Somlo 			ret = kobject_set_name(&subdir->kobj, "%s", tok);
410246c46ebSGabriel Somlo 			if (ret) {
411246c46ebSGabriel Somlo 				kfree(subdir);
412246c46ebSGabriel Somlo 				break;
413246c46ebSGabriel Somlo 			}
414246c46ebSGabriel Somlo 			ret = kset_register(subdir);
415246c46ebSGabriel Somlo 			if (ret) {
416246c46ebSGabriel Somlo 				kfree(subdir);
417246c46ebSGabriel Somlo 				break;
418246c46ebSGabriel Somlo 			}
419246c46ebSGabriel Somlo 
420246c46ebSGabriel Somlo 			/* descend into newly created subdirectory */
421246c46ebSGabriel Somlo 			dir = subdir;
422246c46ebSGabriel Somlo 		}
423246c46ebSGabriel Somlo 	}
424246c46ebSGabriel Somlo 
425246c46ebSGabriel Somlo 	/* we're done with cloned copy of name */
426246c46ebSGabriel Somlo 	kfree(name_copy);
427246c46ebSGabriel Somlo 	return ret;
428246c46ebSGabriel Somlo }
429246c46ebSGabriel Somlo 
430246c46ebSGabriel Somlo /* recursively unregister fw_cfg/by_name/ kset directory tree */
431246c46ebSGabriel Somlo static void fw_cfg_kset_unregister_recursive(struct kset *kset)
432246c46ebSGabriel Somlo {
433246c46ebSGabriel Somlo 	struct kobject *k, *next;
434246c46ebSGabriel Somlo 
435246c46ebSGabriel Somlo 	list_for_each_entry_safe(k, next, &kset->list, entry)
436246c46ebSGabriel Somlo 		/* all set members are ksets too, but check just in case... */
437246c46ebSGabriel Somlo 		if (k->ktype == kset->kobj.ktype)
438246c46ebSGabriel Somlo 			fw_cfg_kset_unregister_recursive(to_kset(k));
439246c46ebSGabriel Somlo 
440246c46ebSGabriel Somlo 	/* symlinks are cleanly and automatically removed with the directory */
441246c46ebSGabriel Somlo 	kset_unregister(kset);
442246c46ebSGabriel Somlo }
443246c46ebSGabriel Somlo 
444246c46ebSGabriel Somlo /* kobjects & kset representing top-level, by_key, and by_name folders */
44575f3e8e4SGabriel Somlo static struct kobject *fw_cfg_top_ko;
44675f3e8e4SGabriel Somlo static struct kobject *fw_cfg_sel_ko;
447246c46ebSGabriel Somlo static struct kset *fw_cfg_fname_kset;
44875f3e8e4SGabriel Somlo 
44975f3e8e4SGabriel Somlo /* register an individual fw_cfg file */
45075f3e8e4SGabriel Somlo static int fw_cfg_register_file(const struct fw_cfg_file *f)
45175f3e8e4SGabriel Somlo {
45275f3e8e4SGabriel Somlo 	int err;
45375f3e8e4SGabriel Somlo 	struct fw_cfg_sysfs_entry *entry;
45475f3e8e4SGabriel Somlo 
45575f3e8e4SGabriel Somlo 	/* allocate new entry */
45675f3e8e4SGabriel Somlo 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
45775f3e8e4SGabriel Somlo 	if (!entry)
45875f3e8e4SGabriel Somlo 		return -ENOMEM;
45975f3e8e4SGabriel Somlo 
46075f3e8e4SGabriel Somlo 	/* set file entry information */
461d174ea7dSMarc-André Lureau 	entry->size = be32_to_cpu(f->size);
462d174ea7dSMarc-André Lureau 	entry->select = be16_to_cpu(f->select);
463d174ea7dSMarc-André Lureau 	memcpy(entry->name, f->name, FW_CFG_MAX_FILE_PATH);
46475f3e8e4SGabriel Somlo 
46575f3e8e4SGabriel Somlo 	/* register entry under "/sys/firmware/qemu_fw_cfg/by_key/" */
46675f3e8e4SGabriel Somlo 	err = kobject_init_and_add(&entry->kobj, &fw_cfg_sysfs_entry_ktype,
467d174ea7dSMarc-André Lureau 				   fw_cfg_sel_ko, "%d", entry->select);
46875f3e8e4SGabriel Somlo 	if (err)
46975f3e8e4SGabriel Somlo 		goto err_register;
47075f3e8e4SGabriel Somlo 
47175f3e8e4SGabriel Somlo 	/* add raw binary content access */
47275f3e8e4SGabriel Somlo 	err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw);
47375f3e8e4SGabriel Somlo 	if (err)
47475f3e8e4SGabriel Somlo 		goto err_add_raw;
47575f3e8e4SGabriel Somlo 
476246c46ebSGabriel Somlo 	/* try adding "/sys/firmware/qemu_fw_cfg/by_name/" symlink */
477d174ea7dSMarc-André Lureau 	fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->name);
478246c46ebSGabriel Somlo 
47975f3e8e4SGabriel Somlo 	/* success, add entry to global cache */
48075f3e8e4SGabriel Somlo 	fw_cfg_sysfs_cache_enlist(entry);
48175f3e8e4SGabriel Somlo 	return 0;
48275f3e8e4SGabriel Somlo 
48375f3e8e4SGabriel Somlo err_add_raw:
48475f3e8e4SGabriel Somlo 	kobject_del(&entry->kobj);
48575f3e8e4SGabriel Somlo err_register:
48675f3e8e4SGabriel Somlo 	kfree(entry);
48775f3e8e4SGabriel Somlo 	return err;
48875f3e8e4SGabriel Somlo }
48975f3e8e4SGabriel Somlo 
49075f3e8e4SGabriel Somlo /* iterate over all fw_cfg directory entries, registering each one */
49175f3e8e4SGabriel Somlo static int fw_cfg_register_dir_entries(void)
49275f3e8e4SGabriel Somlo {
49375f3e8e4SGabriel Somlo 	int ret = 0;
4943d47a34bSMarc-André Lureau 	__be32 files_count;
49575f3e8e4SGabriel Somlo 	u32 count, i;
49675f3e8e4SGabriel Somlo 	struct fw_cfg_file *dir;
49775f3e8e4SGabriel Somlo 	size_t dir_size;
49875f3e8e4SGabriel Somlo 
499b1cc4097SMarc-André Lureau 	ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, &files_count,
500b1cc4097SMarc-André Lureau 			0, sizeof(files_count));
501b1cc4097SMarc-André Lureau 	if (ret < 0)
502b1cc4097SMarc-André Lureau 		return ret;
503b1cc4097SMarc-André Lureau 
5043d47a34bSMarc-André Lureau 	count = be32_to_cpu(files_count);
50575f3e8e4SGabriel Somlo 	dir_size = count * sizeof(struct fw_cfg_file);
50675f3e8e4SGabriel Somlo 
50775f3e8e4SGabriel Somlo 	dir = kmalloc(dir_size, GFP_KERNEL);
50875f3e8e4SGabriel Somlo 	if (!dir)
50975f3e8e4SGabriel Somlo 		return -ENOMEM;
51075f3e8e4SGabriel Somlo 
511b1cc4097SMarc-André Lureau 	ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, dir,
512b1cc4097SMarc-André Lureau 			sizeof(files_count), dir_size);
513b1cc4097SMarc-André Lureau 	if (ret < 0)
514b1cc4097SMarc-André Lureau 		goto end;
51575f3e8e4SGabriel Somlo 
51675f3e8e4SGabriel Somlo 	for (i = 0; i < count; i++) {
51775f3e8e4SGabriel Somlo 		ret = fw_cfg_register_file(&dir[i]);
51875f3e8e4SGabriel Somlo 		if (ret)
51975f3e8e4SGabriel Somlo 			break;
52075f3e8e4SGabriel Somlo 	}
52175f3e8e4SGabriel Somlo 
522b1cc4097SMarc-André Lureau end:
52375f3e8e4SGabriel Somlo 	kfree(dir);
52475f3e8e4SGabriel Somlo 	return ret;
52575f3e8e4SGabriel Somlo }
52675f3e8e4SGabriel Somlo 
52775f3e8e4SGabriel Somlo /* unregister top-level or by_key folder */
52875f3e8e4SGabriel Somlo static inline void fw_cfg_kobj_cleanup(struct kobject *kobj)
52975f3e8e4SGabriel Somlo {
53075f3e8e4SGabriel Somlo 	kobject_del(kobj);
53175f3e8e4SGabriel Somlo 	kobject_put(kobj);
53275f3e8e4SGabriel Somlo }
53375f3e8e4SGabriel Somlo 
53475f3e8e4SGabriel Somlo static int fw_cfg_sysfs_probe(struct platform_device *pdev)
53575f3e8e4SGabriel Somlo {
53675f3e8e4SGabriel Somlo 	int err;
537f295c8dbSMarc-André Lureau 	__le32 rev;
53875f3e8e4SGabriel Somlo 
53975f3e8e4SGabriel Somlo 	/* NOTE: If we supported multiple fw_cfg devices, we'd first create
54075f3e8e4SGabriel Somlo 	 * a subdirectory named after e.g. pdev->id, then hang per-device
541246c46ebSGabriel Somlo 	 * by_key (and by_name) subdirectories underneath it. However, only
54275f3e8e4SGabriel Somlo 	 * one fw_cfg device exist system-wide, so if one was already found
54375f3e8e4SGabriel Somlo 	 * earlier, we might as well stop here.
54475f3e8e4SGabriel Somlo 	 */
54575f3e8e4SGabriel Somlo 	if (fw_cfg_sel_ko)
54675f3e8e4SGabriel Somlo 		return -EBUSY;
54775f3e8e4SGabriel Somlo 
548246c46ebSGabriel Somlo 	/* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
54975f3e8e4SGabriel Somlo 	err = -ENOMEM;
55075f3e8e4SGabriel Somlo 	fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
55175f3e8e4SGabriel Somlo 	if (!fw_cfg_sel_ko)
55275f3e8e4SGabriel Somlo 		goto err_sel;
553246c46ebSGabriel Somlo 	fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko);
554246c46ebSGabriel Somlo 	if (!fw_cfg_fname_kset)
555246c46ebSGabriel Somlo 		goto err_name;
55675f3e8e4SGabriel Somlo 
55775f3e8e4SGabriel Somlo 	/* initialize fw_cfg device i/o from platform data */
55875f3e8e4SGabriel Somlo 	err = fw_cfg_do_platform_probe(pdev);
55975f3e8e4SGabriel Somlo 	if (err)
56075f3e8e4SGabriel Somlo 		goto err_probe;
56175f3e8e4SGabriel Somlo 
56275f3e8e4SGabriel Somlo 	/* get revision number, add matching top-level attribute */
563b1cc4097SMarc-André Lureau 	err = fw_cfg_read_blob(FW_CFG_ID, &rev, 0, sizeof(rev));
564b1cc4097SMarc-André Lureau 	if (err < 0)
565b1cc4097SMarc-André Lureau 		goto err_probe;
566b1cc4097SMarc-André Lureau 
567f295c8dbSMarc-André Lureau 	fw_cfg_rev = le32_to_cpu(rev);
56875f3e8e4SGabriel Somlo 	err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
56975f3e8e4SGabriel Somlo 	if (err)
57075f3e8e4SGabriel Somlo 		goto err_rev;
57175f3e8e4SGabriel Somlo 
57275f3e8e4SGabriel Somlo 	/* process fw_cfg file directory entry, registering each file */
57375f3e8e4SGabriel Somlo 	err = fw_cfg_register_dir_entries();
57475f3e8e4SGabriel Somlo 	if (err)
57575f3e8e4SGabriel Somlo 		goto err_dir;
57675f3e8e4SGabriel Somlo 
57775f3e8e4SGabriel Somlo 	/* success */
57875f3e8e4SGabriel Somlo 	pr_debug("fw_cfg: loaded.\n");
57975f3e8e4SGabriel Somlo 	return 0;
58075f3e8e4SGabriel Somlo 
58175f3e8e4SGabriel Somlo err_dir:
58275f3e8e4SGabriel Somlo 	fw_cfg_sysfs_cache_cleanup();
58375f3e8e4SGabriel Somlo 	sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
58475f3e8e4SGabriel Somlo err_rev:
58575f3e8e4SGabriel Somlo 	fw_cfg_io_cleanup();
58675f3e8e4SGabriel Somlo err_probe:
587246c46ebSGabriel Somlo 	fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
588246c46ebSGabriel Somlo err_name:
58975f3e8e4SGabriel Somlo 	fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
59075f3e8e4SGabriel Somlo err_sel:
59175f3e8e4SGabriel Somlo 	return err;
59275f3e8e4SGabriel Somlo }
59375f3e8e4SGabriel Somlo 
59475f3e8e4SGabriel Somlo static int fw_cfg_sysfs_remove(struct platform_device *pdev)
59575f3e8e4SGabriel Somlo {
59675f3e8e4SGabriel Somlo 	pr_debug("fw_cfg: unloading.\n");
59775f3e8e4SGabriel Somlo 	fw_cfg_sysfs_cache_cleanup();
59823f1b8d9SMarc-André Lureau 	sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
59923f1b8d9SMarc-André Lureau 	fw_cfg_io_cleanup();
600246c46ebSGabriel Somlo 	fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
60175f3e8e4SGabriel Somlo 	fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
60275f3e8e4SGabriel Somlo 	return 0;
60375f3e8e4SGabriel Somlo }
60475f3e8e4SGabriel Somlo 
60575f3e8e4SGabriel Somlo static const struct of_device_id fw_cfg_sysfs_mmio_match[] = {
60675f3e8e4SGabriel Somlo 	{ .compatible = "qemu,fw-cfg-mmio", },
60775f3e8e4SGabriel Somlo 	{},
60875f3e8e4SGabriel Somlo };
60975f3e8e4SGabriel Somlo MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match);
61075f3e8e4SGabriel Somlo 
61175f3e8e4SGabriel Somlo #ifdef CONFIG_ACPI
61275f3e8e4SGabriel Somlo static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = {
6131f57bc12SMarc-André Lureau 	{ FW_CFG_ACPI_DEVICE_ID, },
61475f3e8e4SGabriel Somlo 	{},
61575f3e8e4SGabriel Somlo };
61675f3e8e4SGabriel Somlo MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match);
61775f3e8e4SGabriel Somlo #endif
61875f3e8e4SGabriel Somlo 
61975f3e8e4SGabriel Somlo static struct platform_driver fw_cfg_sysfs_driver = {
62075f3e8e4SGabriel Somlo 	.probe = fw_cfg_sysfs_probe,
62175f3e8e4SGabriel Somlo 	.remove = fw_cfg_sysfs_remove,
62275f3e8e4SGabriel Somlo 	.driver = {
62375f3e8e4SGabriel Somlo 		.name = "fw_cfg",
62475f3e8e4SGabriel Somlo 		.of_match_table = fw_cfg_sysfs_mmio_match,
62575f3e8e4SGabriel Somlo 		.acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match),
62675f3e8e4SGabriel Somlo 	},
62775f3e8e4SGabriel Somlo };
62875f3e8e4SGabriel Somlo 
62975f3e8e4SGabriel Somlo #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
63075f3e8e4SGabriel Somlo 
63175f3e8e4SGabriel Somlo static struct platform_device *fw_cfg_cmdline_dev;
63275f3e8e4SGabriel Somlo 
63375f3e8e4SGabriel Somlo /* this probably belongs in e.g. include/linux/types.h,
63475f3e8e4SGabriel Somlo  * but right now we are the only ones doing it...
63575f3e8e4SGabriel Somlo  */
63675f3e8e4SGabriel Somlo #ifdef CONFIG_PHYS_ADDR_T_64BIT
63775f3e8e4SGabriel Somlo #define __PHYS_ADDR_PREFIX "ll"
63875f3e8e4SGabriel Somlo #else
63975f3e8e4SGabriel Somlo #define __PHYS_ADDR_PREFIX ""
64075f3e8e4SGabriel Somlo #endif
64175f3e8e4SGabriel Somlo 
64275f3e8e4SGabriel Somlo /* use special scanf/printf modifier for phys_addr_t, resource_size_t */
64375f3e8e4SGabriel Somlo #define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \
64475f3e8e4SGabriel Somlo 			 ":%" __PHYS_ADDR_PREFIX "i" \
645*14d1824cSMarc-André Lureau 			 ":%" __PHYS_ADDR_PREFIX "i%n" \
64675f3e8e4SGabriel Somlo 			 ":%" __PHYS_ADDR_PREFIX "i%n"
64775f3e8e4SGabriel Somlo 
64875f3e8e4SGabriel Somlo #define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \
64975f3e8e4SGabriel Somlo 			 "0x%" __PHYS_ADDR_PREFIX "x"
65075f3e8e4SGabriel Somlo 
65175f3e8e4SGabriel Somlo #define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \
65275f3e8e4SGabriel Somlo 			 ":%" __PHYS_ADDR_PREFIX "u" \
65375f3e8e4SGabriel Somlo 			 ":%" __PHYS_ADDR_PREFIX "u"
65475f3e8e4SGabriel Somlo 
655*14d1824cSMarc-André Lureau #define PH_ADDR_PR_4_FMT PH_ADDR_PR_3_FMT \
656*14d1824cSMarc-André Lureau 			 ":%" __PHYS_ADDR_PREFIX "u"
657*14d1824cSMarc-André Lureau 
65875f3e8e4SGabriel Somlo static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp)
65975f3e8e4SGabriel Somlo {
660*14d1824cSMarc-André Lureau 	struct resource res[4] = {};
66175f3e8e4SGabriel Somlo 	char *str;
66275f3e8e4SGabriel Somlo 	phys_addr_t base;
663*14d1824cSMarc-André Lureau 	resource_size_t size, ctrl_off, data_off, dma_off;
66475f3e8e4SGabriel Somlo 	int processed, consumed = 0;
66575f3e8e4SGabriel Somlo 
66675f3e8e4SGabriel Somlo 	/* only one fw_cfg device can exist system-wide, so if one
66775f3e8e4SGabriel Somlo 	 * was processed on the command line already, we might as
66875f3e8e4SGabriel Somlo 	 * well stop here.
66975f3e8e4SGabriel Somlo 	 */
67075f3e8e4SGabriel Somlo 	if (fw_cfg_cmdline_dev) {
67175f3e8e4SGabriel Somlo 		/* avoid leaking previously registered device */
67275f3e8e4SGabriel Somlo 		platform_device_unregister(fw_cfg_cmdline_dev);
67375f3e8e4SGabriel Somlo 		return -EINVAL;
67475f3e8e4SGabriel Somlo 	}
67575f3e8e4SGabriel Somlo 
67675f3e8e4SGabriel Somlo 	/* consume "<size>" portion of command line argument */
67775f3e8e4SGabriel Somlo 	size = memparse(arg, &str);
67875f3e8e4SGabriel Somlo 
679*14d1824cSMarc-André Lureau 	/* get "@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]" chunks */
68075f3e8e4SGabriel Somlo 	processed = sscanf(str, PH_ADDR_SCAN_FMT,
68175f3e8e4SGabriel Somlo 			   &base, &consumed,
682*14d1824cSMarc-André Lureau 			   &ctrl_off, &data_off, &consumed,
683*14d1824cSMarc-André Lureau 			   &dma_off, &consumed);
68475f3e8e4SGabriel Somlo 
685*14d1824cSMarc-André Lureau 	/* sscanf() must process precisely 1, 3 or 4 chunks:
68675f3e8e4SGabriel Somlo 	 * <base> is mandatory, optionally followed by <ctrl_off>
687*14d1824cSMarc-André Lureau 	 * and <data_off>, and <dma_off>;
68875f3e8e4SGabriel Somlo 	 * there must be no extra characters after the last chunk,
68975f3e8e4SGabriel Somlo 	 * so str[consumed] must be '\0'.
69075f3e8e4SGabriel Somlo 	 */
69175f3e8e4SGabriel Somlo 	if (str[consumed] ||
692*14d1824cSMarc-André Lureau 	    (processed != 1 && processed != 3 && processed != 4))
69375f3e8e4SGabriel Somlo 		return -EINVAL;
69475f3e8e4SGabriel Somlo 
69575f3e8e4SGabriel Somlo 	res[0].start = base;
69675f3e8e4SGabriel Somlo 	res[0].end = base + size - 1;
69775f3e8e4SGabriel Somlo 	res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM :
69875f3e8e4SGabriel Somlo 						   IORESOURCE_IO;
69975f3e8e4SGabriel Somlo 
70075f3e8e4SGabriel Somlo 	/* insert register offsets, if provided */
70175f3e8e4SGabriel Somlo 	if (processed > 1) {
70275f3e8e4SGabriel Somlo 		res[1].name = "ctrl";
70375f3e8e4SGabriel Somlo 		res[1].start = ctrl_off;
70475f3e8e4SGabriel Somlo 		res[1].flags = IORESOURCE_REG;
70575f3e8e4SGabriel Somlo 		res[2].name = "data";
70675f3e8e4SGabriel Somlo 		res[2].start = data_off;
70775f3e8e4SGabriel Somlo 		res[2].flags = IORESOURCE_REG;
70875f3e8e4SGabriel Somlo 	}
709*14d1824cSMarc-André Lureau 	if (processed > 3) {
710*14d1824cSMarc-André Lureau 		res[3].name = "dma";
711*14d1824cSMarc-André Lureau 		res[3].start = dma_off;
712*14d1824cSMarc-André Lureau 		res[3].flags = IORESOURCE_REG;
713*14d1824cSMarc-André Lureau 	}
71475f3e8e4SGabriel Somlo 
71575f3e8e4SGabriel Somlo 	/* "processed" happens to nicely match the number of resources
71675f3e8e4SGabriel Somlo 	 * we need to pass in to this platform device.
71775f3e8e4SGabriel Somlo 	 */
71875f3e8e4SGabriel Somlo 	fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg",
71975f3e8e4SGabriel Somlo 					PLATFORM_DEVID_NONE, res, processed);
72075f3e8e4SGabriel Somlo 
7210a9e63aaSVasyl Gomonovych 	return PTR_ERR_OR_ZERO(fw_cfg_cmdline_dev);
72275f3e8e4SGabriel Somlo }
72375f3e8e4SGabriel Somlo 
72475f3e8e4SGabriel Somlo static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp)
72575f3e8e4SGabriel Somlo {
72675f3e8e4SGabriel Somlo 	/* stay silent if device was not configured via the command
72775f3e8e4SGabriel Somlo 	 * line, or if the parameter name (ioport/mmio) doesn't match
72875f3e8e4SGabriel Somlo 	 * the device setting
72975f3e8e4SGabriel Somlo 	 */
73075f3e8e4SGabriel Somlo 	if (!fw_cfg_cmdline_dev ||
73175f3e8e4SGabriel Somlo 	    (!strcmp(kp->name, "mmio") ^
73275f3e8e4SGabriel Somlo 	     (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM)))
73375f3e8e4SGabriel Somlo 		return 0;
73475f3e8e4SGabriel Somlo 
73575f3e8e4SGabriel Somlo 	switch (fw_cfg_cmdline_dev->num_resources) {
73675f3e8e4SGabriel Somlo 	case 1:
73775f3e8e4SGabriel Somlo 		return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT,
73875f3e8e4SGabriel Somlo 				resource_size(&fw_cfg_cmdline_dev->resource[0]),
73975f3e8e4SGabriel Somlo 				fw_cfg_cmdline_dev->resource[0].start);
74075f3e8e4SGabriel Somlo 	case 3:
74175f3e8e4SGabriel Somlo 		return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT,
74275f3e8e4SGabriel Somlo 				resource_size(&fw_cfg_cmdline_dev->resource[0]),
74375f3e8e4SGabriel Somlo 				fw_cfg_cmdline_dev->resource[0].start,
74475f3e8e4SGabriel Somlo 				fw_cfg_cmdline_dev->resource[1].start,
74575f3e8e4SGabriel Somlo 				fw_cfg_cmdline_dev->resource[2].start);
746*14d1824cSMarc-André Lureau 	case 4:
747*14d1824cSMarc-André Lureau 		return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_4_FMT,
748*14d1824cSMarc-André Lureau 				resource_size(&fw_cfg_cmdline_dev->resource[0]),
749*14d1824cSMarc-André Lureau 				fw_cfg_cmdline_dev->resource[0].start,
750*14d1824cSMarc-André Lureau 				fw_cfg_cmdline_dev->resource[1].start,
751*14d1824cSMarc-André Lureau 				fw_cfg_cmdline_dev->resource[2].start,
752*14d1824cSMarc-André Lureau 				fw_cfg_cmdline_dev->resource[3].start);
75375f3e8e4SGabriel Somlo 	}
75475f3e8e4SGabriel Somlo 
75575f3e8e4SGabriel Somlo 	/* Should never get here */
75675f3e8e4SGabriel Somlo 	WARN(1, "Unexpected number of resources: %d\n",
75775f3e8e4SGabriel Somlo 		fw_cfg_cmdline_dev->num_resources);
75875f3e8e4SGabriel Somlo 	return 0;
75975f3e8e4SGabriel Somlo }
76075f3e8e4SGabriel Somlo 
76175f3e8e4SGabriel Somlo static const struct kernel_param_ops fw_cfg_cmdline_param_ops = {
76275f3e8e4SGabriel Somlo 	.set = fw_cfg_cmdline_set,
76375f3e8e4SGabriel Somlo 	.get = fw_cfg_cmdline_get,
76475f3e8e4SGabriel Somlo };
76575f3e8e4SGabriel Somlo 
76675f3e8e4SGabriel Somlo device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
76775f3e8e4SGabriel Somlo device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
76875f3e8e4SGabriel Somlo 
76975f3e8e4SGabriel Somlo #endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */
77075f3e8e4SGabriel Somlo 
77175f3e8e4SGabriel Somlo static int __init fw_cfg_sysfs_init(void)
77275f3e8e4SGabriel Somlo {
773e8aabc64SMichael S. Tsirkin 	int ret;
774e8aabc64SMichael S. Tsirkin 
77575f3e8e4SGabriel Somlo 	/* create /sys/firmware/qemu_fw_cfg/ top level directory */
77675f3e8e4SGabriel Somlo 	fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj);
77775f3e8e4SGabriel Somlo 	if (!fw_cfg_top_ko)
77875f3e8e4SGabriel Somlo 		return -ENOMEM;
77975f3e8e4SGabriel Somlo 
780e8aabc64SMichael S. Tsirkin 	ret = platform_driver_register(&fw_cfg_sysfs_driver);
781e8aabc64SMichael S. Tsirkin 	if (ret)
782e8aabc64SMichael S. Tsirkin 		fw_cfg_kobj_cleanup(fw_cfg_top_ko);
783e8aabc64SMichael S. Tsirkin 
784e8aabc64SMichael S. Tsirkin 	return ret;
78575f3e8e4SGabriel Somlo }
78675f3e8e4SGabriel Somlo 
78775f3e8e4SGabriel Somlo static void __exit fw_cfg_sysfs_exit(void)
78875f3e8e4SGabriel Somlo {
78975f3e8e4SGabriel Somlo 	platform_driver_unregister(&fw_cfg_sysfs_driver);
79075f3e8e4SGabriel Somlo 
79175f3e8e4SGabriel Somlo #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
79275f3e8e4SGabriel Somlo 	platform_device_unregister(fw_cfg_cmdline_dev);
79375f3e8e4SGabriel Somlo #endif
79475f3e8e4SGabriel Somlo 
79575f3e8e4SGabriel Somlo 	/* clean up /sys/firmware/qemu_fw_cfg/ */
79675f3e8e4SGabriel Somlo 	fw_cfg_kobj_cleanup(fw_cfg_top_ko);
79775f3e8e4SGabriel Somlo }
79875f3e8e4SGabriel Somlo 
79975f3e8e4SGabriel Somlo module_init(fw_cfg_sysfs_init);
80075f3e8e4SGabriel Somlo module_exit(fw_cfg_sysfs_exit);
801