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