blob: e7ea2b3b1d11d051340942f70487a80b738e1dc0 [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;
214 struct fw_cfg_file f;
215 struct list_head list;
216};
217
218/* get fw_cfg_sysfs_entry from kobject member */
219static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
220{
221 return container_of(kobj, struct fw_cfg_sysfs_entry, kobj);
222}
223
224/* fw_cfg_sysfs_attribute type */
225struct fw_cfg_sysfs_attribute {
226 struct attribute attr;
227 ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf);
228};
229
230/* get fw_cfg_sysfs_attribute from attribute member */
231static inline struct fw_cfg_sysfs_attribute *to_attr(struct attribute *attr)
232{
233 return container_of(attr, struct fw_cfg_sysfs_attribute, attr);
234}
235
236/* global cache of fw_cfg_sysfs_entry objects */
237static LIST_HEAD(fw_cfg_entry_cache);
238
239/* kobjects removed lazily by kernel, mutual exclusion needed */
240static DEFINE_SPINLOCK(fw_cfg_cache_lock);
241
242static inline void fw_cfg_sysfs_cache_enlist(struct fw_cfg_sysfs_entry *entry)
243{
244 spin_lock(&fw_cfg_cache_lock);
245 list_add_tail(&entry->list, &fw_cfg_entry_cache);
246 spin_unlock(&fw_cfg_cache_lock);
247}
248
249static inline void fw_cfg_sysfs_cache_delist(struct fw_cfg_sysfs_entry *entry)
250{
251 spin_lock(&fw_cfg_cache_lock);
252 list_del(&entry->list);
253 spin_unlock(&fw_cfg_cache_lock);
254}
255
256static void fw_cfg_sysfs_cache_cleanup(void)
257{
258 struct fw_cfg_sysfs_entry *entry, *next;
259
260 list_for_each_entry_safe(entry, next, &fw_cfg_entry_cache, list) {
261 /* will end up invoking fw_cfg_sysfs_cache_delist()
262 * via each object's release() method (i.e. destructor)
263 */
264 kobject_put(&entry->kobj);
265 }
266}
267
268/* default_attrs: per-entry attributes and show methods */
269
270#define FW_CFG_SYSFS_ATTR(_attr) \
271struct fw_cfg_sysfs_attribute fw_cfg_sysfs_attr_##_attr = { \
272 .attr = { .name = __stringify(_attr), .mode = S_IRUSR }, \
273 .show = fw_cfg_sysfs_show_##_attr, \
274}
275
276static ssize_t fw_cfg_sysfs_show_size(struct fw_cfg_sysfs_entry *e, char *buf)
277{
278 return sprintf(buf, "%u\n", e->f.size);
279}
280
281static ssize_t fw_cfg_sysfs_show_key(struct fw_cfg_sysfs_entry *e, char *buf)
282{
283 return sprintf(buf, "%u\n", e->f.select);
284}
285
286static ssize_t fw_cfg_sysfs_show_name(struct fw_cfg_sysfs_entry *e, char *buf)
287{
288 return sprintf(buf, "%s\n", e->f.name);
289}
290
291static FW_CFG_SYSFS_ATTR(size);
292static FW_CFG_SYSFS_ATTR(key);
293static FW_CFG_SYSFS_ATTR(name);
294
295static struct attribute *fw_cfg_sysfs_entry_attrs[] = {
296 &fw_cfg_sysfs_attr_size.attr,
297 &fw_cfg_sysfs_attr_key.attr,
298 &fw_cfg_sysfs_attr_name.attr,
299 NULL,
300};
301
302/* sysfs_ops: find fw_cfg_[entry, attribute] and call appropriate show method */
303static ssize_t fw_cfg_sysfs_attr_show(struct kobject *kobj, struct attribute *a,
304 char *buf)
305{
306 struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
307 struct fw_cfg_sysfs_attribute *attr = to_attr(a);
308
309 return attr->show(entry, buf);
310}
311
312static const struct sysfs_ops fw_cfg_sysfs_attr_ops = {
313 .show = fw_cfg_sysfs_attr_show,
314};
315
316/* release: destructor, to be called via kobject_put() */
317static void fw_cfg_sysfs_release_entry(struct kobject *kobj)
318{
319 struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
320
321 fw_cfg_sysfs_cache_delist(entry);
322 kfree(entry);
323}
324
325/* kobj_type: ties together all properties required to register an entry */
326static struct kobj_type fw_cfg_sysfs_entry_ktype = {
327 .default_attrs = fw_cfg_sysfs_entry_attrs,
328 .sysfs_ops = &fw_cfg_sysfs_attr_ops,
329 .release = fw_cfg_sysfs_release_entry,
330};
331
332/* raw-read method and attribute */
333static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj,
334 struct bin_attribute *bin_attr,
335 char *buf, loff_t pos, size_t count)
336{
337 struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
338
339 if (pos > entry->f.size)
340 return -EINVAL;
341
342 if (count > entry->f.size - pos)
343 count = entry->f.size - pos;
344
345 fw_cfg_read_blob(entry->f.select, buf, pos, count);
346 return count;
347}
348
349static struct bin_attribute fw_cfg_sysfs_attr_raw = {
350 .attr = { .name = "raw", .mode = S_IRUSR },
351 .read = fw_cfg_sysfs_read_raw,
352};
353
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500354/*
355 * Create a kset subdirectory matching each '/' delimited dirname token
356 * in 'name', starting with sysfs kset/folder 'dir'; At the end, create
357 * a symlink directed at the given 'target'.
358 * NOTE: We do this on a best-effort basis, since 'name' is not guaranteed
359 * to be a well-behaved path name. Whenever a symlink vs. kset directory
360 * name collision occurs, the kernel will issue big scary warnings while
361 * refusing to add the offending link or directory. We follow up with our
362 * own, slightly less scary error messages explaining the situation :)
363 */
364static int fw_cfg_build_symlink(struct kset *dir,
365 struct kobject *target, const char *name)
366{
367 int ret;
368 struct kset *subdir;
369 struct kobject *ko;
370 char *name_copy, *p, *tok;
371
372 if (!dir || !target || !name || !*name)
373 return -EINVAL;
374
375 /* clone a copy of name for parsing */
376 name_copy = p = kstrdup(name, GFP_KERNEL);
377 if (!name_copy)
378 return -ENOMEM;
379
380 /* create folders for each dirname token, then symlink for basename */
381 while ((tok = strsep(&p, "/")) && *tok) {
382
383 /* last (basename) token? If so, add symlink here */
384 if (!p || !*p) {
385 ret = sysfs_create_link(&dir->kobj, target, tok);
386 break;
387 }
388
389 /* does the current dir contain an item named after tok ? */
390 ko = kset_find_obj(dir, tok);
391 if (ko) {
392 /* drop reference added by kset_find_obj */
393 kobject_put(ko);
394
395 /* ko MUST be a kset - we're about to use it as one ! */
396 if (ko->ktype != dir->kobj.ktype) {
397 ret = -EINVAL;
398 break;
399 }
400
401 /* descend into already existing subdirectory */
402 dir = to_kset(ko);
403 } else {
404 /* create new subdirectory kset */
405 subdir = kzalloc(sizeof(struct kset), GFP_KERNEL);
406 if (!subdir) {
407 ret = -ENOMEM;
408 break;
409 }
410 subdir->kobj.kset = dir;
411 subdir->kobj.ktype = dir->kobj.ktype;
412 ret = kobject_set_name(&subdir->kobj, "%s", tok);
413 if (ret) {
414 kfree(subdir);
415 break;
416 }
417 ret = kset_register(subdir);
418 if (ret) {
419 kfree(subdir);
420 break;
421 }
422
423 /* descend into newly created subdirectory */
424 dir = subdir;
425 }
426 }
427
428 /* we're done with cloned copy of name */
429 kfree(name_copy);
430 return ret;
431}
432
433/* recursively unregister fw_cfg/by_name/ kset directory tree */
434static void fw_cfg_kset_unregister_recursive(struct kset *kset)
435{
436 struct kobject *k, *next;
437
438 list_for_each_entry_safe(k, next, &kset->list, entry)
439 /* all set members are ksets too, but check just in case... */
440 if (k->ktype == kset->kobj.ktype)
441 fw_cfg_kset_unregister_recursive(to_kset(k));
442
443 /* symlinks are cleanly and automatically removed with the directory */
444 kset_unregister(kset);
445}
446
447/* kobjects & kset representing top-level, by_key, and by_name folders */
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500448static struct kobject *fw_cfg_top_ko;
449static struct kobject *fw_cfg_sel_ko;
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500450static struct kset *fw_cfg_fname_kset;
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500451
452/* register an individual fw_cfg file */
453static int fw_cfg_register_file(const struct fw_cfg_file *f)
454{
455 int err;
456 struct fw_cfg_sysfs_entry *entry;
457
458 /* allocate new entry */
459 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
460 if (!entry)
461 return -ENOMEM;
462
463 /* set file entry information */
464 memcpy(&entry->f, f, sizeof(struct fw_cfg_file));
465
466 /* register entry under "/sys/firmware/qemu_fw_cfg/by_key/" */
467 err = kobject_init_and_add(&entry->kobj, &fw_cfg_sysfs_entry_ktype,
468 fw_cfg_sel_ko, "%d", entry->f.select);
469 if (err)
470 goto err_register;
471
472 /* add raw binary content access */
473 err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw);
474 if (err)
475 goto err_add_raw;
476
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500477 /* try adding "/sys/firmware/qemu_fw_cfg/by_name/" symlink */
478 fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->f.name);
479
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500480 /* success, add entry to global cache */
481 fw_cfg_sysfs_cache_enlist(entry);
482 return 0;
483
484err_add_raw:
485 kobject_del(&entry->kobj);
486err_register:
487 kfree(entry);
488 return err;
489}
490
491/* iterate over all fw_cfg directory entries, registering each one */
492static int fw_cfg_register_dir_entries(void)
493{
494 int ret = 0;
495 u32 count, i;
496 struct fw_cfg_file *dir;
497 size_t dir_size;
498
499 fw_cfg_read_blob(FW_CFG_FILE_DIR, &count, 0, sizeof(count));
500 count = be32_to_cpu(count);
501 dir_size = count * sizeof(struct fw_cfg_file);
502
503 dir = kmalloc(dir_size, GFP_KERNEL);
504 if (!dir)
505 return -ENOMEM;
506
507 fw_cfg_read_blob(FW_CFG_FILE_DIR, dir, sizeof(count), dir_size);
508
509 for (i = 0; i < count; i++) {
510 dir[i].size = be32_to_cpu(dir[i].size);
511 dir[i].select = be16_to_cpu(dir[i].select);
512 ret = fw_cfg_register_file(&dir[i]);
513 if (ret)
514 break;
515 }
516
517 kfree(dir);
518 return ret;
519}
520
521/* unregister top-level or by_key folder */
522static inline void fw_cfg_kobj_cleanup(struct kobject *kobj)
523{
524 kobject_del(kobj);
525 kobject_put(kobj);
526}
527
528static int fw_cfg_sysfs_probe(struct platform_device *pdev)
529{
530 int err;
531
532 /* NOTE: If we supported multiple fw_cfg devices, we'd first create
533 * a subdirectory named after e.g. pdev->id, then hang per-device
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500534 * by_key (and by_name) subdirectories underneath it. However, only
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500535 * one fw_cfg device exist system-wide, so if one was already found
536 * earlier, we might as well stop here.
537 */
538 if (fw_cfg_sel_ko)
539 return -EBUSY;
540
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500541 /* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500542 err = -ENOMEM;
543 fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
544 if (!fw_cfg_sel_ko)
545 goto err_sel;
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500546 fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko);
547 if (!fw_cfg_fname_kset)
548 goto err_name;
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500549
550 /* initialize fw_cfg device i/o from platform data */
551 err = fw_cfg_do_platform_probe(pdev);
552 if (err)
553 goto err_probe;
554
555 /* get revision number, add matching top-level attribute */
556 fw_cfg_read_blob(FW_CFG_ID, &fw_cfg_rev, 0, sizeof(fw_cfg_rev));
557 fw_cfg_rev = le32_to_cpu(fw_cfg_rev);
558 err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
559 if (err)
560 goto err_rev;
561
562 /* process fw_cfg file directory entry, registering each file */
563 err = fw_cfg_register_dir_entries();
564 if (err)
565 goto err_dir;
566
567 /* success */
568 pr_debug("fw_cfg: loaded.\n");
569 return 0;
570
571err_dir:
572 fw_cfg_sysfs_cache_cleanup();
573 sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
574err_rev:
575 fw_cfg_io_cleanup();
576err_probe:
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500577 fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
578err_name:
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500579 fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
580err_sel:
581 return err;
582}
583
584static int fw_cfg_sysfs_remove(struct platform_device *pdev)
585{
586 pr_debug("fw_cfg: unloading.\n");
587 fw_cfg_sysfs_cache_cleanup();
Marc-André Lureau23f1b8d2017-11-20 10:55:15 +0100588 sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
589 fw_cfg_io_cleanup();
Gabriel Somlo246c46e2016-01-28 09:23:13 -0500590 fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500591 fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500592 return 0;
593}
594
595static const struct of_device_id fw_cfg_sysfs_mmio_match[] = {
596 { .compatible = "qemu,fw-cfg-mmio", },
597 {},
598};
599MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match);
600
601#ifdef CONFIG_ACPI
602static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = {
603 { "QEMU0002", },
604 {},
605};
606MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match);
607#endif
608
609static struct platform_driver fw_cfg_sysfs_driver = {
610 .probe = fw_cfg_sysfs_probe,
611 .remove = fw_cfg_sysfs_remove,
612 .driver = {
613 .name = "fw_cfg",
614 .of_match_table = fw_cfg_sysfs_mmio_match,
615 .acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match),
616 },
617};
618
619#ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
620
621static struct platform_device *fw_cfg_cmdline_dev;
622
623/* this probably belongs in e.g. include/linux/types.h,
624 * but right now we are the only ones doing it...
625 */
626#ifdef CONFIG_PHYS_ADDR_T_64BIT
627#define __PHYS_ADDR_PREFIX "ll"
628#else
629#define __PHYS_ADDR_PREFIX ""
630#endif
631
632/* use special scanf/printf modifier for phys_addr_t, resource_size_t */
633#define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \
634 ":%" __PHYS_ADDR_PREFIX "i" \
635 ":%" __PHYS_ADDR_PREFIX "i%n"
636
637#define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \
638 "0x%" __PHYS_ADDR_PREFIX "x"
639
640#define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \
641 ":%" __PHYS_ADDR_PREFIX "u" \
642 ":%" __PHYS_ADDR_PREFIX "u"
643
644static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp)
645{
646 struct resource res[3] = {};
647 char *str;
648 phys_addr_t base;
649 resource_size_t size, ctrl_off, data_off;
650 int processed, consumed = 0;
651
652 /* only one fw_cfg device can exist system-wide, so if one
653 * was processed on the command line already, we might as
654 * well stop here.
655 */
656 if (fw_cfg_cmdline_dev) {
657 /* avoid leaking previously registered device */
658 platform_device_unregister(fw_cfg_cmdline_dev);
659 return -EINVAL;
660 }
661
662 /* consume "<size>" portion of command line argument */
663 size = memparse(arg, &str);
664
665 /* get "@<base>[:<ctrl_off>:<data_off>]" chunks */
666 processed = sscanf(str, PH_ADDR_SCAN_FMT,
667 &base, &consumed,
668 &ctrl_off, &data_off, &consumed);
669
670 /* sscanf() must process precisely 1 or 3 chunks:
671 * <base> is mandatory, optionally followed by <ctrl_off>
672 * and <data_off>;
673 * there must be no extra characters after the last chunk,
674 * so str[consumed] must be '\0'.
675 */
676 if (str[consumed] ||
677 (processed != 1 && processed != 3))
678 return -EINVAL;
679
680 res[0].start = base;
681 res[0].end = base + size - 1;
682 res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM :
683 IORESOURCE_IO;
684
685 /* insert register offsets, if provided */
686 if (processed > 1) {
687 res[1].name = "ctrl";
688 res[1].start = ctrl_off;
689 res[1].flags = IORESOURCE_REG;
690 res[2].name = "data";
691 res[2].start = data_off;
692 res[2].flags = IORESOURCE_REG;
693 }
694
695 /* "processed" happens to nicely match the number of resources
696 * we need to pass in to this platform device.
697 */
698 fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg",
699 PLATFORM_DEVID_NONE, res, processed);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500700
Vasyl Gomonovych0a9e63a2017-11-28 22:40:27 +0100701 return PTR_ERR_OR_ZERO(fw_cfg_cmdline_dev);
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500702}
703
704static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp)
705{
706 /* stay silent if device was not configured via the command
707 * line, or if the parameter name (ioport/mmio) doesn't match
708 * the device setting
709 */
710 if (!fw_cfg_cmdline_dev ||
711 (!strcmp(kp->name, "mmio") ^
712 (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM)))
713 return 0;
714
715 switch (fw_cfg_cmdline_dev->num_resources) {
716 case 1:
717 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT,
718 resource_size(&fw_cfg_cmdline_dev->resource[0]),
719 fw_cfg_cmdline_dev->resource[0].start);
720 case 3:
721 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT,
722 resource_size(&fw_cfg_cmdline_dev->resource[0]),
723 fw_cfg_cmdline_dev->resource[0].start,
724 fw_cfg_cmdline_dev->resource[1].start,
725 fw_cfg_cmdline_dev->resource[2].start);
726 }
727
728 /* Should never get here */
729 WARN(1, "Unexpected number of resources: %d\n",
730 fw_cfg_cmdline_dev->num_resources);
731 return 0;
732}
733
734static const struct kernel_param_ops fw_cfg_cmdline_param_ops = {
735 .set = fw_cfg_cmdline_set,
736 .get = fw_cfg_cmdline_get,
737};
738
739device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
740device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
741
742#endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */
743
744static int __init fw_cfg_sysfs_init(void)
745{
Michael S. Tsirkine8aabc62016-04-03 15:22:08 +0300746 int ret;
747
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500748 /* create /sys/firmware/qemu_fw_cfg/ top level directory */
749 fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj);
750 if (!fw_cfg_top_ko)
751 return -ENOMEM;
752
Michael S. Tsirkine8aabc62016-04-03 15:22:08 +0300753 ret = platform_driver_register(&fw_cfg_sysfs_driver);
754 if (ret)
755 fw_cfg_kobj_cleanup(fw_cfg_top_ko);
756
757 return ret;
Gabriel Somlo75f3e8e2016-01-28 09:23:11 -0500758}
759
760static void __exit fw_cfg_sysfs_exit(void)
761{
762 platform_driver_unregister(&fw_cfg_sysfs_driver);
763
764#ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
765 platform_device_unregister(fw_cfg_cmdline_dev);
766#endif
767
768 /* clean up /sys/firmware/qemu_fw_cfg/ */
769 fw_cfg_kobj_cleanup(fw_cfg_top_ko);
770}
771
772module_init(fw_cfg_sysfs_init);
773module_exit(fw_cfg_sysfs_exit);