1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/module.h>
31#include <linux/platform_device.h>
32#include <linux/acpi.h>
33#include <linux/slab.h>
34#include <linux/io.h>
35#include <linux/ioport.h>
36#include <uapi/linux/qemu_fw_cfg.h>
37#include <linux/delay.h>
38#include <linux/crash_dump.h>
39#include <linux/crash_core.h>
40
41MODULE_AUTHOR("Gabriel L. Somlo <somlo@cmu.edu>");
42MODULE_DESCRIPTION("QEMU fw_cfg sysfs support");
43MODULE_LICENSE("GPL");
44
45
46static u32 fw_cfg_rev;
47
48
49static bool fw_cfg_is_mmio;
50static phys_addr_t fw_cfg_p_base;
51static resource_size_t fw_cfg_p_size;
52static void __iomem *fw_cfg_dev_base;
53static void __iomem *fw_cfg_reg_ctrl;
54static void __iomem *fw_cfg_reg_data;
55static void __iomem *fw_cfg_reg_dma;
56
57
58static DEFINE_MUTEX(fw_cfg_dev_lock);
59
60
61static void fw_cfg_sel_endianness(u16 key)
62{
63 if (fw_cfg_is_mmio)
64 iowrite16be(key, fw_cfg_reg_ctrl);
65 else
66 iowrite16(key, fw_cfg_reg_ctrl);
67}
68
69#ifdef CONFIG_CRASH_CORE
70static inline bool fw_cfg_dma_enabled(void)
71{
72 return (fw_cfg_rev & FW_CFG_VERSION_DMA) && fw_cfg_reg_dma;
73}
74
75
76static void fw_cfg_wait_for_control(struct fw_cfg_dma_access *d)
77{
78 for (;;) {
79 u32 ctrl = be32_to_cpu(READ_ONCE(d->control));
80
81
82 rmb();
83 if ((ctrl & ~FW_CFG_DMA_CTL_ERROR) == 0)
84 return;
85
86 cpu_relax();
87 }
88}
89
90static ssize_t fw_cfg_dma_transfer(void *address, u32 length, u32 control)
91{
92 phys_addr_t dma;
93 struct fw_cfg_dma_access *d = NULL;
94 ssize_t ret = length;
95
96 d = kmalloc(sizeof(*d), GFP_KERNEL);
97 if (!d) {
98 ret = -ENOMEM;
99 goto end;
100 }
101
102
103 *d = (struct fw_cfg_dma_access) {
104 .address = cpu_to_be64(address ? virt_to_phys(address) : 0),
105 .length = cpu_to_be32(length),
106 .control = cpu_to_be32(control)
107 };
108
109 dma = virt_to_phys(d);
110
111 iowrite32be((u64)dma >> 32, fw_cfg_reg_dma);
112
113 wmb();
114 iowrite32be(dma, fw_cfg_reg_dma + 4);
115
116 fw_cfg_wait_for_control(d);
117
118 if (be32_to_cpu(READ_ONCE(d->control)) & FW_CFG_DMA_CTL_ERROR) {
119 ret = -EIO;
120 }
121
122end:
123 kfree(d);
124
125 return ret;
126}
127#endif
128
129
130static ssize_t fw_cfg_read_blob(u16 key,
131 void *buf, loff_t pos, size_t count)
132{
133 u32 glk = -1U;
134 acpi_status status;
135
136
137
138
139 status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
140 if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
141
142 WARN(1, "fw_cfg_read_blob: Failed to lock ACPI!\n");
143 memset(buf, 0, count);
144 return -EINVAL;
145 }
146
147 mutex_lock(&fw_cfg_dev_lock);
148 fw_cfg_sel_endianness(key);
149 while (pos-- > 0)
150 ioread8(fw_cfg_reg_data);
151 ioread8_rep(fw_cfg_reg_data, buf, count);
152 mutex_unlock(&fw_cfg_dev_lock);
153
154 acpi_release_global_lock(glk);
155 return count;
156}
157
158#ifdef CONFIG_CRASH_CORE
159
160static ssize_t fw_cfg_write_blob(u16 key,
161 void *buf, loff_t pos, size_t count)
162{
163 u32 glk = -1U;
164 acpi_status status;
165 ssize_t ret = count;
166
167
168
169
170 status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
171 if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
172
173 WARN(1, "%s: Failed to lock ACPI!\n", __func__);
174 return -EINVAL;
175 }
176
177 mutex_lock(&fw_cfg_dev_lock);
178 if (pos == 0) {
179 ret = fw_cfg_dma_transfer(buf, count, key << 16
180 | FW_CFG_DMA_CTL_SELECT
181 | FW_CFG_DMA_CTL_WRITE);
182 } else {
183 fw_cfg_sel_endianness(key);
184 ret = fw_cfg_dma_transfer(NULL, pos, FW_CFG_DMA_CTL_SKIP);
185 if (ret < 0)
186 goto end;
187 ret = fw_cfg_dma_transfer(buf, count, FW_CFG_DMA_CTL_WRITE);
188 }
189
190end:
191 mutex_unlock(&fw_cfg_dev_lock);
192
193 acpi_release_global_lock(glk);
194
195 return ret;
196}
197#endif
198
199
200static void fw_cfg_io_cleanup(void)
201{
202 if (fw_cfg_is_mmio) {
203 iounmap(fw_cfg_dev_base);
204 release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
205 } else {
206 ioport_unmap(fw_cfg_dev_base);
207 release_region(fw_cfg_p_base, fw_cfg_p_size);
208 }
209}
210
211
212#if !(defined(FW_CFG_CTRL_OFF) && defined(FW_CFG_DATA_OFF))
213# if (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
214# define FW_CFG_CTRL_OFF 0x08
215# define FW_CFG_DATA_OFF 0x00
216# define FW_CFG_DMA_OFF 0x10
217# elif (defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC32))
218# define FW_CFG_CTRL_OFF 0x00
219# define FW_CFG_DATA_OFF 0x02
220# elif (defined(CONFIG_X86) || defined(CONFIG_SPARC64))
221# define FW_CFG_CTRL_OFF 0x00
222# define FW_CFG_DATA_OFF 0x01
223# define FW_CFG_DMA_OFF 0x04
224# else
225# error "QEMU FW_CFG not available on this architecture!"
226# endif
227#endif
228
229
230static int fw_cfg_do_platform_probe(struct platform_device *pdev)
231{
232 char sig[FW_CFG_SIG_SIZE];
233 struct resource *range, *ctrl, *data, *dma;
234
235
236 fw_cfg_is_mmio = false;
237 range = platform_get_resource(pdev, IORESOURCE_IO, 0);
238 if (!range) {
239 fw_cfg_is_mmio = true;
240 range = platform_get_resource(pdev, IORESOURCE_MEM, 0);
241 if (!range)
242 return -EINVAL;
243 }
244 fw_cfg_p_base = range->start;
245 fw_cfg_p_size = resource_size(range);
246
247 if (fw_cfg_is_mmio) {
248 if (!request_mem_region(fw_cfg_p_base,
249 fw_cfg_p_size, "fw_cfg_mem"))
250 return -EBUSY;
251 fw_cfg_dev_base = ioremap(fw_cfg_p_base, fw_cfg_p_size);
252 if (!fw_cfg_dev_base) {
253 release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
254 return -EFAULT;
255 }
256 } else {
257 if (!request_region(fw_cfg_p_base,
258 fw_cfg_p_size, "fw_cfg_io"))
259 return -EBUSY;
260 fw_cfg_dev_base = ioport_map(fw_cfg_p_base, fw_cfg_p_size);
261 if (!fw_cfg_dev_base) {
262 release_region(fw_cfg_p_base, fw_cfg_p_size);
263 return -EFAULT;
264 }
265 }
266
267
268 ctrl = platform_get_resource_byname(pdev, IORESOURCE_REG, "ctrl");
269 data = platform_get_resource_byname(pdev, IORESOURCE_REG, "data");
270 dma = platform_get_resource_byname(pdev, IORESOURCE_REG, "dma");
271 if (ctrl && data) {
272 fw_cfg_reg_ctrl = fw_cfg_dev_base + ctrl->start;
273 fw_cfg_reg_data = fw_cfg_dev_base + data->start;
274 } else {
275
276 fw_cfg_reg_ctrl = fw_cfg_dev_base + FW_CFG_CTRL_OFF;
277 fw_cfg_reg_data = fw_cfg_dev_base + FW_CFG_DATA_OFF;
278 }
279
280 if (dma)
281 fw_cfg_reg_dma = fw_cfg_dev_base + dma->start;
282#ifdef FW_CFG_DMA_OFF
283 else
284 fw_cfg_reg_dma = fw_cfg_dev_base + FW_CFG_DMA_OFF;
285#endif
286
287
288 if (fw_cfg_read_blob(FW_CFG_SIGNATURE, sig,
289 0, FW_CFG_SIG_SIZE) < 0 ||
290 memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) != 0) {
291 fw_cfg_io_cleanup();
292 return -ENODEV;
293 }
294
295 return 0;
296}
297
298static ssize_t fw_cfg_showrev(struct kobject *k, struct attribute *a, char *buf)
299{
300 return sprintf(buf, "%u\n", fw_cfg_rev);
301}
302
303static const struct {
304 struct attribute attr;
305 ssize_t (*show)(struct kobject *k, struct attribute *a, char *buf);
306} fw_cfg_rev_attr = {
307 .attr = { .name = "rev", .mode = S_IRUSR },
308 .show = fw_cfg_showrev,
309};
310
311
312struct fw_cfg_sysfs_entry {
313 struct kobject kobj;
314 u32 size;
315 u16 select;
316 char name[FW_CFG_MAX_FILE_PATH];
317 struct list_head list;
318};
319
320#ifdef CONFIG_CRASH_CORE
321static ssize_t fw_cfg_write_vmcoreinfo(const struct fw_cfg_file *f)
322{
323 static struct fw_cfg_vmcoreinfo *data;
324 ssize_t ret;
325
326 data = kmalloc(sizeof(struct fw_cfg_vmcoreinfo), GFP_KERNEL);
327 if (!data)
328 return -ENOMEM;
329
330 *data = (struct fw_cfg_vmcoreinfo) {
331 .guest_format = cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF),
332 .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
333 .paddr = cpu_to_le64(paddr_vmcoreinfo_note())
334 };
335
336
337
338 ret = fw_cfg_write_blob(be16_to_cpu(f->select), data,
339 0, sizeof(struct fw_cfg_vmcoreinfo));
340
341 kfree(data);
342 return ret;
343}
344#endif
345
346
347static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
348{
349 return container_of(kobj, struct fw_cfg_sysfs_entry, kobj);
350}
351
352
353struct fw_cfg_sysfs_attribute {
354 struct attribute attr;
355 ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf);
356};
357
358
359static inline struct fw_cfg_sysfs_attribute *to_attr(struct attribute *attr)
360{
361 return container_of(attr, struct fw_cfg_sysfs_attribute, attr);
362}
363
364
365static LIST_HEAD(fw_cfg_entry_cache);
366
367
368static DEFINE_SPINLOCK(fw_cfg_cache_lock);
369
370static inline void fw_cfg_sysfs_cache_enlist(struct fw_cfg_sysfs_entry *entry)
371{
372 spin_lock(&fw_cfg_cache_lock);
373 list_add_tail(&entry->list, &fw_cfg_entry_cache);
374 spin_unlock(&fw_cfg_cache_lock);
375}
376
377static inline void fw_cfg_sysfs_cache_delist(struct fw_cfg_sysfs_entry *entry)
378{
379 spin_lock(&fw_cfg_cache_lock);
380 list_del(&entry->list);
381 spin_unlock(&fw_cfg_cache_lock);
382}
383
384static void fw_cfg_sysfs_cache_cleanup(void)
385{
386 struct fw_cfg_sysfs_entry *entry, *next;
387
388 list_for_each_entry_safe(entry, next, &fw_cfg_entry_cache, list) {
389
390
391
392 kobject_put(&entry->kobj);
393 }
394}
395
396
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{
406 return sprintf(buf, "%u\n", e->size);
407}
408
409static ssize_t fw_cfg_sysfs_show_key(struct fw_cfg_sysfs_entry *e, char *buf)
410{
411 return sprintf(buf, "%u\n", e->select);
412}
413
414static ssize_t fw_cfg_sysfs_show_name(struct fw_cfg_sysfs_entry *e, char *buf)
415{
416 return sprintf(buf, "%s\n", e->name);
417}
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
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
445static void fw_cfg_sysfs_release_entry(struct kobject *kobj)
446{
447 struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
448
449 fw_cfg_sysfs_cache_delist(entry);
450 kfree(entry);
451}
452
453
454static struct kobj_type fw_cfg_sysfs_entry_ktype = {
455 .default_attrs = fw_cfg_sysfs_entry_attrs,
456 .sysfs_ops = &fw_cfg_sysfs_attr_ops,
457 .release = fw_cfg_sysfs_release_entry,
458};
459
460
461static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj,
462 struct bin_attribute *bin_attr,
463 char *buf, loff_t pos, size_t count)
464{
465 struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
466
467 if (pos > entry->size)
468 return -EINVAL;
469
470 if (count > entry->size - pos)
471 count = entry->size - pos;
472
473 return fw_cfg_read_blob(entry->select, buf, pos, count);
474}
475
476static struct bin_attribute fw_cfg_sysfs_attr_raw = {
477 .attr = { .name = "raw", .mode = S_IRUSR },
478 .read = fw_cfg_sysfs_read_raw,
479};
480
481
482
483
484
485
486
487
488
489
490
491static int fw_cfg_build_symlink(struct kset *dir,
492 struct kobject *target, const char *name)
493{
494 int ret;
495 struct kset *subdir;
496 struct kobject *ko;
497 char *name_copy, *p, *tok;
498
499 if (!dir || !target || !name || !*name)
500 return -EINVAL;
501
502
503 name_copy = p = kstrdup(name, GFP_KERNEL);
504 if (!name_copy)
505 return -ENOMEM;
506
507
508 while ((tok = strsep(&p, "/")) && *tok) {
509
510
511 if (!p || !*p) {
512 ret = sysfs_create_link(&dir->kobj, target, tok);
513 break;
514 }
515
516
517 ko = kset_find_obj(dir, tok);
518 if (ko) {
519
520 kobject_put(ko);
521
522
523 if (ko->ktype != dir->kobj.ktype) {
524 ret = -EINVAL;
525 break;
526 }
527
528
529 dir = to_kset(ko);
530 } else {
531
532 subdir = kzalloc(sizeof(struct kset), GFP_KERNEL);
533 if (!subdir) {
534 ret = -ENOMEM;
535 break;
536 }
537 subdir->kobj.kset = dir;
538 subdir->kobj.ktype = dir->kobj.ktype;
539 ret = kobject_set_name(&subdir->kobj, "%s", tok);
540 if (ret) {
541 kfree(subdir);
542 break;
543 }
544 ret = kset_register(subdir);
545 if (ret) {
546 kfree(subdir);
547 break;
548 }
549
550
551 dir = subdir;
552 }
553 }
554
555
556 kfree(name_copy);
557 return ret;
558}
559
560
561static void fw_cfg_kset_unregister_recursive(struct kset *kset)
562{
563 struct kobject *k, *next;
564
565 list_for_each_entry_safe(k, next, &kset->list, entry)
566
567 if (k->ktype == kset->kobj.ktype)
568 fw_cfg_kset_unregister_recursive(to_kset(k));
569
570
571 kset_unregister(kset);
572}
573
574
575static struct kobject *fw_cfg_top_ko;
576static struct kobject *fw_cfg_sel_ko;
577static struct kset *fw_cfg_fname_kset;
578
579
580static int fw_cfg_register_file(const struct fw_cfg_file *f)
581{
582 int err;
583 struct fw_cfg_sysfs_entry *entry;
584
585#ifdef CONFIG_CRASH_CORE
586 if (fw_cfg_dma_enabled() &&
587 strcmp(f->name, FW_CFG_VMCOREINFO_FILENAME) == 0 &&
588 !is_kdump_kernel()) {
589 if (fw_cfg_write_vmcoreinfo(f) < 0)
590 pr_warn("fw_cfg: failed to write vmcoreinfo");
591 }
592#endif
593
594
595 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
596 if (!entry)
597 return -ENOMEM;
598
599
600 entry->size = be32_to_cpu(f->size);
601 entry->select = be16_to_cpu(f->select);
602 memcpy(entry->name, f->name, FW_CFG_MAX_FILE_PATH);
603
604
605 err = kobject_init_and_add(&entry->kobj, &fw_cfg_sysfs_entry_ktype,
606 fw_cfg_sel_ko, "%d", entry->select);
607 if (err)
608 goto err_register;
609
610
611 err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw);
612 if (err)
613 goto err_add_raw;
614
615
616 fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->name);
617
618
619 fw_cfg_sysfs_cache_enlist(entry);
620 return 0;
621
622err_add_raw:
623 kobject_del(&entry->kobj);
624err_register:
625 kfree(entry);
626 return err;
627}
628
629
630static int fw_cfg_register_dir_entries(void)
631{
632 int ret = 0;
633 __be32 files_count;
634 u32 count, i;
635 struct fw_cfg_file *dir;
636 size_t dir_size;
637
638 ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, &files_count,
639 0, sizeof(files_count));
640 if (ret < 0)
641 return ret;
642
643 count = be32_to_cpu(files_count);
644 dir_size = count * sizeof(struct fw_cfg_file);
645
646 dir = kmalloc(dir_size, GFP_KERNEL);
647 if (!dir)
648 return -ENOMEM;
649
650 ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, dir,
651 sizeof(files_count), dir_size);
652 if (ret < 0)
653 goto end;
654
655 for (i = 0; i < count; i++) {
656 ret = fw_cfg_register_file(&dir[i]);
657 if (ret)
658 break;
659 }
660
661end:
662 kfree(dir);
663 return ret;
664}
665
666
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;
676 __le32 rev;
677
678
679
680
681
682
683
684 if (fw_cfg_sel_ko)
685 return -EBUSY;
686
687
688 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;
692 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;
695
696
697 err = fw_cfg_do_platform_probe(pdev);
698 if (err)
699 goto err_probe;
700
701
702 err = fw_cfg_read_blob(FW_CFG_ID, &rev, 0, sizeof(rev));
703 if (err < 0)
704 goto err_probe;
705
706 fw_cfg_rev = le32_to_cpu(rev);
707 err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
708 if (err)
709 goto err_rev;
710
711
712 err = fw_cfg_register_dir_entries();
713 if (err)
714 goto err_dir;
715
716
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:
726 fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
727err_name:
728 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();
737 sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
738 fw_cfg_io_cleanup();
739 fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
740 fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
741 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[] = {
752 { FW_CFG_ACPI_DEVICE_ID, },
753 {},
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
773
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
782#define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \
783 ":%" __PHYS_ADDR_PREFIX "i" \
784 ":%" __PHYS_ADDR_PREFIX "i%n" \
785 ":%" __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
794#define PH_ADDR_PR_4_FMT PH_ADDR_PR_3_FMT \
795 ":%" __PHYS_ADDR_PREFIX "u"
796
797static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp)
798{
799 struct resource res[4] = {};
800 char *str;
801 phys_addr_t base;
802 resource_size_t size, ctrl_off, data_off, dma_off;
803 int processed, consumed = 0;
804
805
806
807
808
809 if (fw_cfg_cmdline_dev) {
810
811 platform_device_unregister(fw_cfg_cmdline_dev);
812 return -EINVAL;
813 }
814
815
816 size = memparse(arg, &str);
817
818
819 processed = sscanf(str, PH_ADDR_SCAN_FMT,
820 &base, &consumed,
821 &ctrl_off, &data_off, &consumed,
822 &dma_off, &consumed);
823
824
825
826
827
828
829
830 if (str[consumed] ||
831 (processed != 1 && processed != 3 && processed != 4))
832 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
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 }
848 if (processed > 3) {
849 res[3].name = "dma";
850 res[3].start = dma_off;
851 res[3].flags = IORESOURCE_REG;
852 }
853
854
855
856
857 fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg",
858 PLATFORM_DEVID_NONE, res, processed);
859 if (IS_ERR(fw_cfg_cmdline_dev))
860 return PTR_ERR(fw_cfg_cmdline_dev);
861
862 return 0;
863}
864
865static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp)
866{
867
868
869
870
871 if (!fw_cfg_cmdline_dev ||
872 (!strcmp(kp->name, "mmio") ^
873 (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM)))
874 return 0;
875
876 switch (fw_cfg_cmdline_dev->num_resources) {
877 case 1:
878 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT,
879 resource_size(&fw_cfg_cmdline_dev->resource[0]),
880 fw_cfg_cmdline_dev->resource[0].start);
881 case 3:
882 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT,
883 resource_size(&fw_cfg_cmdline_dev->resource[0]),
884 fw_cfg_cmdline_dev->resource[0].start,
885 fw_cfg_cmdline_dev->resource[1].start,
886 fw_cfg_cmdline_dev->resource[2].start);
887 case 4:
888 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_4_FMT,
889 resource_size(&fw_cfg_cmdline_dev->resource[0]),
890 fw_cfg_cmdline_dev->resource[0].start,
891 fw_cfg_cmdline_dev->resource[1].start,
892 fw_cfg_cmdline_dev->resource[2].start,
893 fw_cfg_cmdline_dev->resource[3].start);
894 }
895
896
897 WARN(1, "Unexpected number of resources: %d\n",
898 fw_cfg_cmdline_dev->num_resources);
899 return 0;
900}
901
902static const struct kernel_param_ops fw_cfg_cmdline_param_ops = {
903 .set = fw_cfg_cmdline_set,
904 .get = fw_cfg_cmdline_get,
905};
906
907device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
908device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
909
910#endif
911
912static int __init fw_cfg_sysfs_init(void)
913{
914 int ret;
915
916
917 fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj);
918 if (!fw_cfg_top_ko)
919 return -ENOMEM;
920
921 ret = platform_driver_register(&fw_cfg_sysfs_driver);
922 if (ret)
923 fw_cfg_kobj_cleanup(fw_cfg_top_ko);
924
925 return ret;
926}
927
928static void __exit fw_cfg_sysfs_exit(void)
929{
930 platform_driver_unregister(&fw_cfg_sysfs_driver);
931
932#ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
933 platform_device_unregister(fw_cfg_cmdline_dev);
934#endif
935
936
937 fw_cfg_kobj_cleanup(fw_cfg_top_ko);
938}
939
940module_init(fw_cfg_sysfs_init);
941module_exit(fw_cfg_sysfs_exit);
942