blob: 0eb155fdfb35939fbdb703663be35ad468f977cd [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é Lureauc1d0c3f2017-11-13 20:29:54 +010013 * [qemu_fw_cfg.]ioport=<size>@<base>[:<ctrl_off>:<data_off>]
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -050014 * or
Marc-André Lureauc1d0c3f2017-11-13 20:29:54 +010015 * [qemu_fw_cfg.]mmio=<size>@<base>[:<ctrl_off>:<data_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
22 *
23 * e.g.:
Marc-André Lureauc1d0c3f2017-11-13 20:29:54 +010024 * qemu_fw_cfg.ioport=2@0x510:0:1 (the default on x86)
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -050025 * or
Marc-André Lureauc1d0c3f2017-11-13 20:29:54 +010026 * qemu_fw_cfg.mmio=0xA@0x9020000:8:0 (the default on arm)
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -050027 */
28
29#include <linux/module.h>
30#include <linux/platform_device.h>
31#include <linux/acpi.h>
32#include <linux/slab.h>
33#include <linux/io.h>
34#include <linux/ioport.h>
35
36MODULE_AUTHOR("Gabriel L. Somlo <somlo@cmu.edu>");
37MODULE_DESCRIPTION("QEMU fw_cfg sysfs support");
38MODULE_LICENSE("GPL");
39
40/* selector key values for "well-known" fw_cfg entries */
41#define FW_CFG_SIGNATURE 0x00
42#define FW_CFG_ID 0x01
43#define FW_CFG_FILE_DIR 0x19
44
45/* size in bytes of fw_cfg signature */
46#define FW_CFG_SIG_SIZE 4
47
48/* fw_cfg "file name" is up to 56 characters (including terminating nul) */
49#define FW_CFG_MAX_FILE_PATH 56
50
51/* fw_cfg file directory entry type */
52struct fw_cfg_file {
53 u32 size;
54 u16 select;
55 u16 reserved;
56 char name[FW_CFG_MAX_FILE_PATH];
57};
58
59/* fw_cfg device i/o register addresses */
60static bool fw_cfg_is_mmio;
61static phys_addr_t fw_cfg_p_base;
62static resource_size_t fw_cfg_p_size;
63static void __iomem *fw_cfg_dev_base;
64static void __iomem *fw_cfg_reg_ctrl;
65static void __iomem *fw_cfg_reg_data;
66
67/* atomic access to fw_cfg device (potentially slow i/o, so using mutex) */
68static DEFINE_MUTEX(fw_cfg_dev_lock);
69
70/* pick appropriate endianness for selector key */
Marc-André Lureau8d59d5b2018-02-28 16:06:05 +010071static void fw_cfg_sel_endianness(u16 key)
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -050072{
Marc-André Lureau8d59d5b2018-02-28 16:06:05 +010073 if (fw_cfg_is_mmio)
74 iowrite16be(key, fw_cfg_reg_ctrl);
75 else
76 iowrite16(key, fw_cfg_reg_ctrl);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -050077}
78
79/* read chunk of given fw_cfg blob (caller responsible for sanity-check) */
80static inline void fw_cfg_read_blob(u16 key,
81 void *buf, loff_t pos, size_t count)
82{
Dan Carpenterd4f6e272016-04-14 12:33:37 +030083 u32 glk = -1U;
Gabriel Somlodef7ac82016-03-08 13:30:50 -050084 acpi_status status;
85
86 /* If we have ACPI, ensure mutual exclusion against any potential
87 * device access by the firmware, e.g. via AML methods:
88 */
89 status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
90 if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
91 /* Should never get here */
92 WARN(1, "fw_cfg_read_blob: Failed to lock ACPI!\n");
93 memset(buf, 0, count);
94 return;
95 }
96
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -050097 mutex_lock(&fw_cfg_dev_lock);
Marc-André Lureau8d59d5b2018-02-28 16:06:05 +010098 fw_cfg_sel_endianness(key);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -050099 while (pos-- > 0)
100 ioread8(fw_cfg_reg_data);
101 ioread8_rep(fw_cfg_reg_data, buf, count);
102 mutex_unlock(&fw_cfg_dev_lock);
Gabriel Somlodef7ac82016-03-08 13:30:50 -0500103
104 acpi_release_global_lock(glk);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500105}
106
107/* clean up fw_cfg device i/o */
108static void fw_cfg_io_cleanup(void)
109{
110 if (fw_cfg_is_mmio) {
111 iounmap(fw_cfg_dev_base);
112 release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
113 } else {
114 ioport_unmap(fw_cfg_dev_base);
115 release_region(fw_cfg_p_base, fw_cfg_p_size);
116 }
117}
118
119/* arch-specific ctrl & data register offsets are not available in ACPI, DT */
Valentin Rothberg9b3ec232016-02-11 12:19:03 +0100120#if !(defined(FW_CFG_CTRL_OFF) && defined(FW_CFG_DATA_OFF))
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500121# if (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
122# define FW_CFG_CTRL_OFF 0x08
123# define FW_CFG_DATA_OFF 0x00
124# elif (defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC32)) /* ppc/mac,sun4m */
125# define FW_CFG_CTRL_OFF 0x00
126# define FW_CFG_DATA_OFF 0x02
127# elif (defined(CONFIG_X86) || defined(CONFIG_SPARC64)) /* x86, sun4u */
128# define FW_CFG_CTRL_OFF 0x00
129# define FW_CFG_DATA_OFF 0x01
130# else
Gabriel Somlo00411b72016-02-22 16:18:18 -0500131# error "QEMU FW_CFG not available on this architecture!"
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500132# endif
133#endif
134
135/* initialize fw_cfg device i/o from platform data */
136static int fw_cfg_do_platform_probe(struct platform_device *pdev)
137{
138 char sig[FW_CFG_SIG_SIZE];
139 struct resource *range, *ctrl, *data;
140
141 /* acquire i/o range details */
142 fw_cfg_is_mmio = false;
143 range = platform_get_resource(pdev, IORESOURCE_IO, 0);
144 if (!range) {
145 fw_cfg_is_mmio = true;
146 range = platform_get_resource(pdev, IORESOURCE_MEM, 0);
147 if (!range)
148 return -EINVAL;
149 }
150 fw_cfg_p_base = range->start;
151 fw_cfg_p_size = resource_size(range);
152
153 if (fw_cfg_is_mmio) {
154 if (!request_mem_region(fw_cfg_p_base,
155 fw_cfg_p_size, "fw_cfg_mem"))
156 return -EBUSY;
157 fw_cfg_dev_base = ioremap(fw_cfg_p_base, fw_cfg_p_size);
158 if (!fw_cfg_dev_base) {
159 release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
160 return -EFAULT;
161 }
162 } else {
163 if (!request_region(fw_cfg_p_base,
164 fw_cfg_p_size, "fw_cfg_io"))
165 return -EBUSY;
166 fw_cfg_dev_base = ioport_map(fw_cfg_p_base, fw_cfg_p_size);
167 if (!fw_cfg_dev_base) {
168 release_region(fw_cfg_p_base, fw_cfg_p_size);
169 return -EFAULT;
170 }
171 }
172
173 /* were custom register offsets provided (e.g. on the command line)? */
174 ctrl = platform_get_resource_byname(pdev, IORESOURCE_REG, "ctrl");
175 data = platform_get_resource_byname(pdev, IORESOURCE_REG, "data");
176 if (ctrl && data) {
177 fw_cfg_reg_ctrl = fw_cfg_dev_base + ctrl->start;
178 fw_cfg_reg_data = fw_cfg_dev_base + data->start;
179 } else {
180 /* use architecture-specific offsets */
181 fw_cfg_reg_ctrl = fw_cfg_dev_base + FW_CFG_CTRL_OFF;
182 fw_cfg_reg_data = fw_cfg_dev_base + FW_CFG_DATA_OFF;
183 }
184
185 /* verify fw_cfg device signature */
186 fw_cfg_read_blob(FW_CFG_SIGNATURE, sig, 0, FW_CFG_SIG_SIZE);
187 if (memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) != 0) {
188 fw_cfg_io_cleanup();
189 return -ENODEV;
190 }
191
192 return 0;
193}
194
195/* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */
196static u32 fw_cfg_rev;
197
198static ssize_t fw_cfg_showrev(struct kobject *k, struct attribute *a, char *buf)
199{
200 return sprintf(buf, "%u\n", fw_cfg_rev);
201}
202
203static const struct {
204 struct attribute attr;
205 ssize_t (*show)(struct kobject *k, struct attribute *a, char *buf);
206} fw_cfg_rev_attr = {
207 .attr = { .name = "rev", .mode = S_IRUSR },
208 .show = fw_cfg_showrev,
209};
210
211/* fw_cfg_sysfs_entry type */
212struct fw_cfg_sysfs_entry {
213 struct kobject kobj;
Marc-André Lureaud174ea7d2018-02-28 16:06:06 +0100214 u32 size;
215 u16 select;
216 char name[FW_CFG_MAX_FILE_PATH];
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500217 struct list_head list;
218};
219
220/* get fw_cfg_sysfs_entry from kobject member */
221static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
222{
223 return container_of(kobj, struct fw_cfg_sysfs_entry, kobj);
224}
225
226/* fw_cfg_sysfs_attribute type */
227struct fw_cfg_sysfs_attribute {
228 struct attribute attr;
229 ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf);
230};
231
232/* get fw_cfg_sysfs_attribute from attribute member */
233static inline struct fw_cfg_sysfs_attribute *to_attr(struct attribute *attr)
234{
235 return container_of(attr, struct fw_cfg_sysfs_attribute, attr);
236}
237
238/* global cache of fw_cfg_sysfs_entry objects */
239static LIST_HEAD(fw_cfg_entry_cache);
240
241/* kobjects removed lazily by kernel, mutual exclusion needed */
242static DEFINE_SPINLOCK(fw_cfg_cache_lock);
243
244static inline void fw_cfg_sysfs_cache_enlist(struct fw_cfg_sysfs_entry *entry)
245{
246 spin_lock(&fw_cfg_cache_lock);
247 list_add_tail(&entry->list, &fw_cfg_entry_cache);
248 spin_unlock(&fw_cfg_cache_lock);
249}
250
251static inline void fw_cfg_sysfs_cache_delist(struct fw_cfg_sysfs_entry *entry)
252{
253 spin_lock(&fw_cfg_cache_lock);
254 list_del(&entry->list);
255 spin_unlock(&fw_cfg_cache_lock);
256}
257
258static void fw_cfg_sysfs_cache_cleanup(void)
259{
260 struct fw_cfg_sysfs_entry *entry, *next;
261
262 list_for_each_entry_safe(entry, next, &fw_cfg_entry_cache, list) {
263 /* will end up invoking fw_cfg_sysfs_cache_delist()
264 * via each object's release() method (i.e. destructor)
265 */
266 kobject_put(&entry->kobj);
267 }
268}
269
270/* default_attrs: per-entry attributes and show methods */
271
272#define FW_CFG_SYSFS_ATTR(_attr) \
273struct fw_cfg_sysfs_attribute fw_cfg_sysfs_attr_##_attr = { \
274 .attr = { .name = __stringify(_attr), .mode = S_IRUSR }, \
275 .show = fw_cfg_sysfs_show_##_attr, \
276}
277
278static ssize_t fw_cfg_sysfs_show_size(struct fw_cfg_sysfs_entry *e, char *buf)
279{
Marc-André Lureaud174ea7d2018-02-28 16:06:06 +0100280 return sprintf(buf, "%u\n", e->size);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500281}
282
283static ssize_t fw_cfg_sysfs_show_key(struct fw_cfg_sysfs_entry *e, char *buf)
284{
Marc-André Lureaud174ea7d2018-02-28 16:06:06 +0100285 return sprintf(buf, "%u\n", e->select);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500286}
287
288static ssize_t fw_cfg_sysfs_show_name(struct fw_cfg_sysfs_entry *e, char *buf)
289{
Marc-André Lureaud174ea7d2018-02-28 16:06:06 +0100290 return sprintf(buf, "%s\n", e->name);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500291}
292
293static FW_CFG_SYSFS_ATTR(size);
294static FW_CFG_SYSFS_ATTR(key);
295static FW_CFG_SYSFS_ATTR(name);
296
297static struct attribute *fw_cfg_sysfs_entry_attrs[] = {
298 &fw_cfg_sysfs_attr_size.attr,
299 &fw_cfg_sysfs_attr_key.attr,
300 &fw_cfg_sysfs_attr_name.attr,
301 NULL,
302};
303
304/* sysfs_ops: find fw_cfg_[entry, attribute] and call appropriate show method */
305static ssize_t fw_cfg_sysfs_attr_show(struct kobject *kobj, struct attribute *a,
306 char *buf)
307{
308 struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
309 struct fw_cfg_sysfs_attribute *attr = to_attr(a);
310
311 return attr->show(entry, buf);
312}
313
314static const struct sysfs_ops fw_cfg_sysfs_attr_ops = {
315 .show = fw_cfg_sysfs_attr_show,
316};
317
318/* release: destructor, to be called via kobject_put() */
319static void fw_cfg_sysfs_release_entry(struct kobject *kobj)
320{
321 struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
322
323 fw_cfg_sysfs_cache_delist(entry);
324 kfree(entry);
325}
326
327/* kobj_type: ties together all properties required to register an entry */
328static struct kobj_type fw_cfg_sysfs_entry_ktype = {
329 .default_attrs = fw_cfg_sysfs_entry_attrs,
330 .sysfs_ops = &fw_cfg_sysfs_attr_ops,
331 .release = fw_cfg_sysfs_release_entry,
332};
333
334/* raw-read method and attribute */
335static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj,
336 struct bin_attribute *bin_attr,
337 char *buf, loff_t pos, size_t count)
338{
339 struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
340
Marc-André Lureaud174ea7d2018-02-28 16:06:06 +0100341 if (pos > entry->size)
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500342 return -EINVAL;
343
Marc-André Lureaud174ea7d2018-02-28 16:06:06 +0100344 if (count > entry->size - pos)
345 count = entry->size - pos;
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500346
Marc-André Lureaud174ea7d2018-02-28 16:06:06 +0100347 fw_cfg_read_blob(entry->select, buf, pos, count);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500348 return count;
349}
350
351static struct bin_attribute fw_cfg_sysfs_attr_raw = {
352 .attr = { .name = "raw", .mode = S_IRUSR },
353 .read = fw_cfg_sysfs_read_raw,
354};
355
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500356/*
357 * Create a kset subdirectory matching each '/' delimited dirname token
358 * in 'name', starting with sysfs kset/folder 'dir'; At the end, create
359 * a symlink directed at the given 'target'.
360 * NOTE: We do this on a best-effort basis, since 'name' is not guaranteed
361 * to be a well-behaved path name. Whenever a symlink vs. kset directory
362 * name collision occurs, the kernel will issue big scary warnings while
363 * refusing to add the offending link or directory. We follow up with our
364 * own, slightly less scary error messages explaining the situation :)
365 */
366static int fw_cfg_build_symlink(struct kset *dir,
367 struct kobject *target, const char *name)
368{
369 int ret;
370 struct kset *subdir;
371 struct kobject *ko;
372 char *name_copy, *p, *tok;
373
374 if (!dir || !target || !name || !*name)
375 return -EINVAL;
376
377 /* clone a copy of name for parsing */
378 name_copy = p = kstrdup(name, GFP_KERNEL);
379 if (!name_copy)
380 return -ENOMEM;
381
382 /* create folders for each dirname token, then symlink for basename */
383 while ((tok = strsep(&p, "/")) && *tok) {
384
385 /* last (basename) token? If so, add symlink here */
386 if (!p || !*p) {
387 ret = sysfs_create_link(&dir->kobj, target, tok);
388 break;
389 }
390
391 /* does the current dir contain an item named after tok ? */
392 ko = kset_find_obj(dir, tok);
393 if (ko) {
394 /* drop reference added by kset_find_obj */
395 kobject_put(ko);
396
397 /* ko MUST be a kset - we're about to use it as one ! */
398 if (ko->ktype != dir->kobj.ktype) {
399 ret = -EINVAL;
400 break;
401 }
402
403 /* descend into already existing subdirectory */
404 dir = to_kset(ko);
405 } else {
406 /* create new subdirectory kset */
407 subdir = kzalloc(sizeof(struct kset), GFP_KERNEL);
408 if (!subdir) {
409 ret = -ENOMEM;
410 break;
411 }
412 subdir->kobj.kset = dir;
413 subdir->kobj.ktype = dir->kobj.ktype;
414 ret = kobject_set_name(&subdir->kobj, "%s", tok);
415 if (ret) {
416 kfree(subdir);
417 break;
418 }
419 ret = kset_register(subdir);
420 if (ret) {
421 kfree(subdir);
422 break;
423 }
424
425 /* descend into newly created subdirectory */
426 dir = subdir;
427 }
428 }
429
430 /* we're done with cloned copy of name */
431 kfree(name_copy);
432 return ret;
433}
434
435/* recursively unregister fw_cfg/by_name/ kset directory tree */
436static void fw_cfg_kset_unregister_recursive(struct kset *kset)
437{
438 struct kobject *k, *next;
439
440 list_for_each_entry_safe(k, next, &kset->list, entry)
441 /* all set members are ksets too, but check just in case... */
442 if (k->ktype == kset->kobj.ktype)
443 fw_cfg_kset_unregister_recursive(to_kset(k));
444
445 /* symlinks are cleanly and automatically removed with the directory */
446 kset_unregister(kset);
447}
448
449/* kobjects & kset representing top-level, by_key, and by_name folders */
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500450static struct kobject *fw_cfg_top_ko;
451static struct kobject *fw_cfg_sel_ko;
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500452static struct kset *fw_cfg_fname_kset;
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500453
454/* register an individual fw_cfg file */
455static int fw_cfg_register_file(const struct fw_cfg_file *f)
456{
457 int err;
458 struct fw_cfg_sysfs_entry *entry;
459
460 /* allocate new entry */
461 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
462 if (!entry)
463 return -ENOMEM;
464
465 /* set file entry information */
Marc-André Lureaud174ea7d2018-02-28 16:06:06 +0100466 entry->size = be32_to_cpu(f->size);
467 entry->select = be16_to_cpu(f->select);
468 memcpy(entry->name, f->name, FW_CFG_MAX_FILE_PATH);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500469
470 /* register entry under "/sys/firmware/qemu_fw_cfg/by_key/" */
471 err = kobject_init_and_add(&entry->kobj, &fw_cfg_sysfs_entry_ktype,
Marc-André Lureaud174ea7d2018-02-28 16:06:06 +0100472 fw_cfg_sel_ko, "%d", entry->select);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500473 if (err)
474 goto err_register;
475
476 /* add raw binary content access */
477 err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw);
478 if (err)
479 goto err_add_raw;
480
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500481 /* try adding "/sys/firmware/qemu_fw_cfg/by_name/" symlink */
Marc-André Lureaud174ea7d2018-02-28 16:06:06 +0100482 fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->name);
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500483
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500484 /* success, add entry to global cache */
485 fw_cfg_sysfs_cache_enlist(entry);
486 return 0;
487
488err_add_raw:
489 kobject_del(&entry->kobj);
490err_register:
491 kfree(entry);
492 return err;
493}
494
495/* iterate over all fw_cfg directory entries, registering each one */
496static int fw_cfg_register_dir_entries(void)
497{
498 int ret = 0;
499 u32 count, i;
500 struct fw_cfg_file *dir;
501 size_t dir_size;
502
503 fw_cfg_read_blob(FW_CFG_FILE_DIR, &count, 0, sizeof(count));
504 count = be32_to_cpu(count);
505 dir_size = count * sizeof(struct fw_cfg_file);
506
507 dir = kmalloc(dir_size, GFP_KERNEL);
508 if (!dir)
509 return -ENOMEM;
510
511 fw_cfg_read_blob(FW_CFG_FILE_DIR, dir, sizeof(count), dir_size);
512
513 for (i = 0; i < count; i++) {
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500514 ret = fw_cfg_register_file(&dir[i]);
515 if (ret)
516 break;
517 }
518
519 kfree(dir);
520 return ret;
521}
522
523/* unregister top-level or by_key folder */
524static inline void fw_cfg_kobj_cleanup(struct kobject *kobj)
525{
526 kobject_del(kobj);
527 kobject_put(kobj);
528}
529
530static int fw_cfg_sysfs_probe(struct platform_device *pdev)
531{
532 int err;
Marc-André Lureauf295c8d2018-02-28 16:06:07 +0100533 __le32 rev;
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500534
535 /* NOTE: If we supported multiple fw_cfg devices, we'd first create
536 * a subdirectory named after e.g. pdev->id, then hang per-device
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500537 * by_key (and by_name) subdirectories underneath it. However, only
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500538 * one fw_cfg device exist system-wide, so if one was already found
539 * earlier, we might as well stop here.
540 */
541 if (fw_cfg_sel_ko)
542 return -EBUSY;
543
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500544 /* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500545 err = -ENOMEM;
546 fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
547 if (!fw_cfg_sel_ko)
548 goto err_sel;
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500549 fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko);
550 if (!fw_cfg_fname_kset)
551 goto err_name;
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500552
553 /* initialize fw_cfg device i/o from platform data */
554 err = fw_cfg_do_platform_probe(pdev);
555 if (err)
556 goto err_probe;
557
558 /* get revision number, add matching top-level attribute */
Marc-André Lureauf295c8d2018-02-28 16:06:07 +0100559 fw_cfg_read_blob(FW_CFG_ID, &rev, 0, sizeof(rev));
560 fw_cfg_rev = le32_to_cpu(rev);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500561 err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
562 if (err)
563 goto err_rev;
564
565 /* process fw_cfg file directory entry, registering each file */
566 err = fw_cfg_register_dir_entries();
567 if (err)
568 goto err_dir;
569
570 /* success */
571 pr_debug("fw_cfg: loaded.\n");
572 return 0;
573
574err_dir:
575 fw_cfg_sysfs_cache_cleanup();
576 sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
577err_rev:
578 fw_cfg_io_cleanup();
579err_probe:
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500580 fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
581err_name:
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500582 fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
583err_sel:
584 return err;
585}
586
587static int fw_cfg_sysfs_remove(struct platform_device *pdev)
588{
589 pr_debug("fw_cfg: unloading.\n");
590 fw_cfg_sysfs_cache_cleanup();
Marc-André Lureau23f1b8d2017-11-20 10:55:15 +0100591 sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
592 fw_cfg_io_cleanup();
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500593 fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500594 fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500595 return 0;
596}
597
598static const struct of_device_id fw_cfg_sysfs_mmio_match[] = {
599 { .compatible = "qemu,fw-cfg-mmio", },
600 {},
601};
602MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match);
603
604#ifdef CONFIG_ACPI
605static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = {
606 { "QEMU0002", },
607 {},
608};
609MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match);
610#endif
611
612static struct platform_driver fw_cfg_sysfs_driver = {
613 .probe = fw_cfg_sysfs_probe,
614 .remove = fw_cfg_sysfs_remove,
615 .driver = {
616 .name = "fw_cfg",
617 .of_match_table = fw_cfg_sysfs_mmio_match,
618 .acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match),
619 },
620};
621
622#ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
623
624static struct platform_device *fw_cfg_cmdline_dev;
625
626/* this probably belongs in e.g. include/linux/types.h,
627 * but right now we are the only ones doing it...
628 */
629#ifdef CONFIG_PHYS_ADDR_T_64BIT
630#define __PHYS_ADDR_PREFIX "ll"
631#else
632#define __PHYS_ADDR_PREFIX ""
633#endif
634
635/* use special scanf/printf modifier for phys_addr_t, resource_size_t */
636#define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \
637 ":%" __PHYS_ADDR_PREFIX "i" \
638 ":%" __PHYS_ADDR_PREFIX "i%n"
639
640#define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \
641 "0x%" __PHYS_ADDR_PREFIX "x"
642
643#define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \
644 ":%" __PHYS_ADDR_PREFIX "u" \
645 ":%" __PHYS_ADDR_PREFIX "u"
646
647static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp)
648{
649 struct resource res[3] = {};
650 char *str;
651 phys_addr_t base;
652 resource_size_t size, ctrl_off, data_off;
653 int processed, consumed = 0;
654
655 /* only one fw_cfg device can exist system-wide, so if one
656 * was processed on the command line already, we might as
657 * well stop here.
658 */
659 if (fw_cfg_cmdline_dev) {
660 /* avoid leaking previously registered device */
661 platform_device_unregister(fw_cfg_cmdline_dev);
662 return -EINVAL;
663 }
664
665 /* consume "<size>" portion of command line argument */
666 size = memparse(arg, &str);
667
668 /* get "@<base>[:<ctrl_off>:<data_off>]" chunks */
669 processed = sscanf(str, PH_ADDR_SCAN_FMT,
670 &base, &consumed,
671 &ctrl_off, &data_off, &consumed);
672
673 /* sscanf() must process precisely 1 or 3 chunks:
674 * <base> is mandatory, optionally followed by <ctrl_off>
675 * and <data_off>;
676 * there must be no extra characters after the last chunk,
677 * so str[consumed] must be '\0'.
678 */
679 if (str[consumed] ||
680 (processed != 1 && processed != 3))
681 return -EINVAL;
682
683 res[0].start = base;
684 res[0].end = base + size - 1;
685 res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM :
686 IORESOURCE_IO;
687
688 /* insert register offsets, if provided */
689 if (processed > 1) {
690 res[1].name = "ctrl";
691 res[1].start = ctrl_off;
692 res[1].flags = IORESOURCE_REG;
693 res[2].name = "data";
694 res[2].start = data_off;
695 res[2].flags = IORESOURCE_REG;
696 }
697
698 /* "processed" happens to nicely match the number of resources
699 * we need to pass in to this platform device.
700 */
701 fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg",
702 PLATFORM_DEVID_NONE, res, processed);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500703
Vasyl Gomonovych0a9e63a2017-11-28 22:40:27 +0100704 return PTR_ERR_OR_ZERO(fw_cfg_cmdline_dev);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500705}
706
707static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp)
708{
709 /* stay silent if device was not configured via the command
710 * line, or if the parameter name (ioport/mmio) doesn't match
711 * the device setting
712 */
713 if (!fw_cfg_cmdline_dev ||
714 (!strcmp(kp->name, "mmio") ^
715 (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM)))
716 return 0;
717
718 switch (fw_cfg_cmdline_dev->num_resources) {
719 case 1:
720 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT,
721 resource_size(&fw_cfg_cmdline_dev->resource[0]),
722 fw_cfg_cmdline_dev->resource[0].start);
723 case 3:
724 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT,
725 resource_size(&fw_cfg_cmdline_dev->resource[0]),
726 fw_cfg_cmdline_dev->resource[0].start,
727 fw_cfg_cmdline_dev->resource[1].start,
728 fw_cfg_cmdline_dev->resource[2].start);
729 }
730
731 /* Should never get here */
732 WARN(1, "Unexpected number of resources: %d\n",
733 fw_cfg_cmdline_dev->num_resources);
734 return 0;
735}
736
737static const struct kernel_param_ops fw_cfg_cmdline_param_ops = {
738 .set = fw_cfg_cmdline_set,
739 .get = fw_cfg_cmdline_get,
740};
741
742device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
743device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
744
745#endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */
746
747static int __init fw_cfg_sysfs_init(void)
748{
Michael S. Tsirkine8aabc62016-04-03 15:22:08 +0300749 int ret;
750
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500751 /* create /sys/firmware/qemu_fw_cfg/ top level directory */
752 fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj);
753 if (!fw_cfg_top_ko)
754 return -ENOMEM;
755
Michael S. Tsirkine8aabc62016-04-03 15:22:08 +0300756 ret = platform_driver_register(&fw_cfg_sysfs_driver);
757 if (ret)
758 fw_cfg_kobj_cleanup(fw_cfg_top_ko);
759
760 return ret;
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500761}
762
763static void __exit fw_cfg_sysfs_exit(void)
764{
765 platform_driver_unregister(&fw_cfg_sysfs_driver);
766
767#ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
768 platform_device_unregister(fw_cfg_cmdline_dev);
769#endif
770
771 /* clean up /sys/firmware/qemu_fw_cfg/ */
772 fw_cfg_kobj_cleanup(fw_cfg_top_ko);
773}
774
775module_init(fw_cfg_sysfs_init);
776module_exit(fw_cfg_sysfs_exit);