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