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