]> Git Repo - linux.git/blob - drivers/firmware/qemu_fw_cfg.c
Merge tag 'for-linus' of git://github.com/openrisc/linux
[linux.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  *      [qemu_fw_cfg.]ioport=<size>@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]
14  * or
15  *      [qemu_fw_cfg.]mmio=<size>@<base>[:<ctrl_off>:<data_off>[:<dma_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  *      <dma_off> := (optional) offset of dma register
23  *
24  * e.g.:
25  *      qemu_fw_cfg.ioport=12@0x510:0:1:4       (the default on x86)
26  * or
27  *      qemu_fw_cfg.mmio=16@0x9020000:8:0:16    (the default on arm)
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
42 MODULE_AUTHOR("Gabriel L. Somlo <[email protected]>");
43 MODULE_DESCRIPTION("QEMU fw_cfg sysfs support");
44 MODULE_LICENSE("GPL");
45
46 /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */
47 static u32 fw_cfg_rev;
48
49 /* fw_cfg device i/o register addresses */
50 static bool fw_cfg_is_mmio;
51 static phys_addr_t fw_cfg_p_base;
52 static resource_size_t fw_cfg_p_size;
53 static void __iomem *fw_cfg_dev_base;
54 static void __iomem *fw_cfg_reg_ctrl;
55 static void __iomem *fw_cfg_reg_data;
56 static void __iomem *fw_cfg_reg_dma;
57
58 /* atomic access to fw_cfg device (potentially slow i/o, so using mutex) */
59 static DEFINE_MUTEX(fw_cfg_dev_lock);
60
61 /* pick appropriate endianness for selector key */
62 static 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
71 static inline bool fw_cfg_dma_enabled(void)
72 {
73         return (fw_cfg_rev & FW_CFG_VERSION_DMA) && fw_cfg_reg_dma;
74 }
75
76 /* qemu fw_cfg device is sync today, but spec says it may become async */
77 static 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                 /* do not reorder the read to d->control */
83                 rmb();
84                 if ((ctrl & ~FW_CFG_DMA_CTL_ERROR) == 0)
85                         return;
86
87                 cpu_relax();
88         }
89 }
90
91 static 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         /* fw_cfg device does not need IOMMU protection, so use physical addresses */
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         /* force memory to sync before notifying device via MMIO */
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
123 end:
124         kfree(d);
125
126         return ret;
127 }
128 #endif
129
130 /* read chunk of given fw_cfg blob (caller responsible for sanity-check) */
131 static 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         /* If we have ACPI, ensure mutual exclusion against any potential
138          * device access by the firmware, e.g. via AML methods:
139          */
140         status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
141         if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
142                 /* Should never get here */
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 /* write chunk of given fw_cfg blob (caller responsible for sanity-check) */
161 static 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         /* If we have ACPI, ensure mutual exclusion against any potential
169          * device access by the firmware, e.g. via AML methods:
170          */
171         status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
172         if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
173                 /* Should never get here */
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
191 end:
192         mutex_unlock(&fw_cfg_dev_lock);
193
194         acpi_release_global_lock(glk);
195
196         return ret;
197 }
198 #endif /* CONFIG_CRASH_CORE */
199
200 /* clean up fw_cfg device i/o */
201 static 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 /* arch-specific ctrl & data register offsets are not available in ACPI, DT */
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)   /* parisc */
219 #  define FW_CFG_CTRL_OFF 0x00
220 #  define FW_CFG_DATA_OFF 0x04
221 # elif (defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC32)) /* ppc/mac,sun4m */
222 #  define FW_CFG_CTRL_OFF 0x00
223 #  define FW_CFG_DATA_OFF 0x02
224 # elif (defined(CONFIG_X86) || defined(CONFIG_SPARC64)) /* x86, sun4u */
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 /* initialize fw_cfg device i/o from platform data */
234 static 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         /* acquire i/o range details */
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         /* were custom register offsets provided (e.g. on the command line)? */
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                 /* use architecture-specific offsets */
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         /* verify fw_cfg device signature */
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
302 static 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
308 static const struct kobj_attribute fw_cfg_rev_attr = {
309         .attr = { .name = "rev", .mode = S_IRUSR },
310         .show = fw_cfg_showrev,
311 };
312
313 /* fw_cfg_sysfs_entry type */
314 struct 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
323 static 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         /* spare ourself reading host format support for now since we
338          * don't know what else to format - host may ignore ours
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 /* CONFIG_CRASH_CORE */
347
348 /* get fw_cfg_sysfs_entry from kobject member */
349 static 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 /* fw_cfg_sysfs_attribute type */
355 struct fw_cfg_sysfs_attribute {
356         struct attribute attr;
357         ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf);
358 };
359
360 /* get fw_cfg_sysfs_attribute from attribute member */
361 static 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 /* global cache of fw_cfg_sysfs_entry objects */
367 static LIST_HEAD(fw_cfg_entry_cache);
368
369 /* kobjects removed lazily by kernel, mutual exclusion needed */
370 static DEFINE_SPINLOCK(fw_cfg_cache_lock);
371
372 static 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
379 static 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
386 static 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                 /* will end up invoking fw_cfg_sysfs_cache_delist()
392                  * via each object's release() method (i.e. destructor)
393                  */
394                 kobject_put(&entry->kobj);
395         }
396 }
397
398 /* per-entry attributes and show methods */
399
400 #define FW_CFG_SYSFS_ATTR(_attr) \
401 struct 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
406 static 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
411 static 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
416 static 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
421 static FW_CFG_SYSFS_ATTR(size);
422 static FW_CFG_SYSFS_ATTR(key);
423 static FW_CFG_SYSFS_ATTR(name);
424
425 static 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 ATTRIBUTE_GROUPS(fw_cfg_sysfs_entry);
432
433 /* sysfs_ops: find fw_cfg_[entry, attribute] and call appropriate show method */
434 static ssize_t fw_cfg_sysfs_attr_show(struct kobject *kobj, struct attribute *a,
435                                       char *buf)
436 {
437         struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
438         struct fw_cfg_sysfs_attribute *attr = to_attr(a);
439
440         return attr->show(entry, buf);
441 }
442
443 static const struct sysfs_ops fw_cfg_sysfs_attr_ops = {
444         .show = fw_cfg_sysfs_attr_show,
445 };
446
447 /* release: destructor, to be called via kobject_put() */
448 static void fw_cfg_sysfs_release_entry(struct kobject *kobj)
449 {
450         struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
451
452         fw_cfg_sysfs_cache_delist(entry);
453         kfree(entry);
454 }
455
456 /* kobj_type: ties together all properties required to register an entry */
457 static struct kobj_type fw_cfg_sysfs_entry_ktype = {
458         .default_groups = fw_cfg_sysfs_entry_groups,
459         .sysfs_ops = &fw_cfg_sysfs_attr_ops,
460         .release = fw_cfg_sysfs_release_entry,
461 };
462
463 /* raw-read method and attribute */
464 static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj,
465                                      struct bin_attribute *bin_attr,
466                                      char *buf, loff_t pos, size_t count)
467 {
468         struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
469
470         if (pos > entry->size)
471                 return -EINVAL;
472
473         if (count > entry->size - pos)
474                 count = entry->size - pos;
475
476         return fw_cfg_read_blob(entry->select, buf, pos, count);
477 }
478
479 static struct bin_attribute fw_cfg_sysfs_attr_raw = {
480         .attr = { .name = "raw", .mode = S_IRUSR },
481         .read = fw_cfg_sysfs_read_raw,
482 };
483
484 /*
485  * Create a kset subdirectory matching each '/' delimited dirname token
486  * in 'name', starting with sysfs kset/folder 'dir'; At the end, create
487  * a symlink directed at the given 'target'.
488  * NOTE: We do this on a best-effort basis, since 'name' is not guaranteed
489  * to be a well-behaved path name. Whenever a symlink vs. kset directory
490  * name collision occurs, the kernel will issue big scary warnings while
491  * refusing to add the offending link or directory. We follow up with our
492  * own, slightly less scary error messages explaining the situation :)
493  */
494 static int fw_cfg_build_symlink(struct kset *dir,
495                                 struct kobject *target, const char *name)
496 {
497         int ret;
498         struct kset *subdir;
499         struct kobject *ko;
500         char *name_copy, *p, *tok;
501
502         if (!dir || !target || !name || !*name)
503                 return -EINVAL;
504
505         /* clone a copy of name for parsing */
506         name_copy = p = kstrdup(name, GFP_KERNEL);
507         if (!name_copy)
508                 return -ENOMEM;
509
510         /* create folders for each dirname token, then symlink for basename */
511         while ((tok = strsep(&p, "/")) && *tok) {
512
513                 /* last (basename) token? If so, add symlink here */
514                 if (!p || !*p) {
515                         ret = sysfs_create_link(&dir->kobj, target, tok);
516                         break;
517                 }
518
519                 /* does the current dir contain an item named after tok ? */
520                 ko = kset_find_obj(dir, tok);
521                 if (ko) {
522                         /* drop reference added by kset_find_obj */
523                         kobject_put(ko);
524
525                         /* ko MUST be a kset - we're about to use it as one ! */
526                         if (ko->ktype != dir->kobj.ktype) {
527                                 ret = -EINVAL;
528                                 break;
529                         }
530
531                         /* descend into already existing subdirectory */
532                         dir = to_kset(ko);
533                 } else {
534                         /* create new subdirectory kset */
535                         subdir = kzalloc(sizeof(struct kset), GFP_KERNEL);
536                         if (!subdir) {
537                                 ret = -ENOMEM;
538                                 break;
539                         }
540                         subdir->kobj.kset = dir;
541                         subdir->kobj.ktype = dir->kobj.ktype;
542                         ret = kobject_set_name(&subdir->kobj, "%s", tok);
543                         if (ret) {
544                                 kfree(subdir);
545                                 break;
546                         }
547                         ret = kset_register(subdir);
548                         if (ret) {
549                                 kfree(subdir);
550                                 break;
551                         }
552
553                         /* descend into newly created subdirectory */
554                         dir = subdir;
555                 }
556         }
557
558         /* we're done with cloned copy of name */
559         kfree(name_copy);
560         return ret;
561 }
562
563 /* recursively unregister fw_cfg/by_name/ kset directory tree */
564 static void fw_cfg_kset_unregister_recursive(struct kset *kset)
565 {
566         struct kobject *k, *next;
567
568         list_for_each_entry_safe(k, next, &kset->list, entry)
569                 /* all set members are ksets too, but check just in case... */
570                 if (k->ktype == kset->kobj.ktype)
571                         fw_cfg_kset_unregister_recursive(to_kset(k));
572
573         /* symlinks are cleanly and automatically removed with the directory */
574         kset_unregister(kset);
575 }
576
577 /* kobjects & kset representing top-level, by_key, and by_name folders */
578 static struct kobject *fw_cfg_top_ko;
579 static struct kobject *fw_cfg_sel_ko;
580 static struct kset *fw_cfg_fname_kset;
581
582 /* register an individual fw_cfg file */
583 static int fw_cfg_register_file(const struct fw_cfg_file *f)
584 {
585         int err;
586         struct fw_cfg_sysfs_entry *entry;
587
588 #ifdef CONFIG_CRASH_CORE
589         if (fw_cfg_dma_enabled() &&
590                 strcmp(f->name, FW_CFG_VMCOREINFO_FILENAME) == 0 &&
591                 !is_kdump_kernel()) {
592                 if (fw_cfg_write_vmcoreinfo(f) < 0)
593                         pr_warn("fw_cfg: failed to write vmcoreinfo");
594         }
595 #endif
596
597         /* allocate new entry */
598         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
599         if (!entry)
600                 return -ENOMEM;
601
602         /* set file entry information */
603         entry->size = be32_to_cpu(f->size);
604         entry->select = be16_to_cpu(f->select);
605         memcpy(entry->name, f->name, FW_CFG_MAX_FILE_PATH);
606
607         /* register entry under "/sys/firmware/qemu_fw_cfg/by_key/" */
608         err = kobject_init_and_add(&entry->kobj, &fw_cfg_sysfs_entry_ktype,
609                                    fw_cfg_sel_ko, "%d", entry->select);
610         if (err) {
611                 kobject_put(&entry->kobj);
612                 return err;
613         }
614
615         /* add raw binary content access */
616         err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw);
617         if (err)
618                 goto err_add_raw;
619
620         /* try adding "/sys/firmware/qemu_fw_cfg/by_name/" symlink */
621         fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->name);
622
623         /* success, add entry to global cache */
624         fw_cfg_sysfs_cache_enlist(entry);
625         return 0;
626
627 err_add_raw:
628         kobject_del(&entry->kobj);
629         kfree(entry);
630         return err;
631 }
632
633 /* iterate over all fw_cfg directory entries, registering each one */
634 static int fw_cfg_register_dir_entries(void)
635 {
636         int ret = 0;
637         __be32 files_count;
638         u32 count, i;
639         struct fw_cfg_file *dir;
640         size_t dir_size;
641
642         ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, &files_count,
643                         0, sizeof(files_count));
644         if (ret < 0)
645                 return ret;
646
647         count = be32_to_cpu(files_count);
648         dir_size = count * sizeof(struct fw_cfg_file);
649
650         dir = kmalloc(dir_size, GFP_KERNEL);
651         if (!dir)
652                 return -ENOMEM;
653
654         ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, dir,
655                         sizeof(files_count), dir_size);
656         if (ret < 0)
657                 goto end;
658
659         for (i = 0; i < count; i++) {
660                 ret = fw_cfg_register_file(&dir[i]);
661                 if (ret)
662                         break;
663         }
664
665 end:
666         kfree(dir);
667         return ret;
668 }
669
670 /* unregister top-level or by_key folder */
671 static inline void fw_cfg_kobj_cleanup(struct kobject *kobj)
672 {
673         kobject_del(kobj);
674         kobject_put(kobj);
675 }
676
677 static int fw_cfg_sysfs_probe(struct platform_device *pdev)
678 {
679         int err;
680         __le32 rev;
681
682         /* NOTE: If we supported multiple fw_cfg devices, we'd first create
683          * a subdirectory named after e.g. pdev->id, then hang per-device
684          * by_key (and by_name) subdirectories underneath it. However, only
685          * one fw_cfg device exist system-wide, so if one was already found
686          * earlier, we might as well stop here.
687          */
688         if (fw_cfg_sel_ko)
689                 return -EBUSY;
690
691         /* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
692         err = -ENOMEM;
693         fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
694         if (!fw_cfg_sel_ko)
695                 goto err_sel;
696         fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko);
697         if (!fw_cfg_fname_kset)
698                 goto err_name;
699
700         /* initialize fw_cfg device i/o from platform data */
701         err = fw_cfg_do_platform_probe(pdev);
702         if (err)
703                 goto err_probe;
704
705         /* get revision number, add matching top-level attribute */
706         err = fw_cfg_read_blob(FW_CFG_ID, &rev, 0, sizeof(rev));
707         if (err < 0)
708                 goto err_probe;
709
710         fw_cfg_rev = le32_to_cpu(rev);
711         err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
712         if (err)
713                 goto err_rev;
714
715         /* process fw_cfg file directory entry, registering each file */
716         err = fw_cfg_register_dir_entries();
717         if (err)
718                 goto err_dir;
719
720         /* success */
721         pr_debug("fw_cfg: loaded.\n");
722         return 0;
723
724 err_dir:
725         fw_cfg_sysfs_cache_cleanup();
726         sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
727 err_rev:
728         fw_cfg_io_cleanup();
729 err_probe:
730         fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
731 err_name:
732         fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
733 err_sel:
734         return err;
735 }
736
737 static int fw_cfg_sysfs_remove(struct platform_device *pdev)
738 {
739         pr_debug("fw_cfg: unloading.\n");
740         fw_cfg_sysfs_cache_cleanup();
741         sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
742         fw_cfg_io_cleanup();
743         fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
744         fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
745         return 0;
746 }
747
748 static const struct of_device_id fw_cfg_sysfs_mmio_match[] = {
749         { .compatible = "qemu,fw-cfg-mmio", },
750         {},
751 };
752 MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match);
753
754 #ifdef CONFIG_ACPI
755 static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = {
756         { FW_CFG_ACPI_DEVICE_ID, },
757         {},
758 };
759 MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match);
760 #endif
761
762 static struct platform_driver fw_cfg_sysfs_driver = {
763         .probe = fw_cfg_sysfs_probe,
764         .remove = fw_cfg_sysfs_remove,
765         .driver = {
766                 .name = "fw_cfg",
767                 .of_match_table = fw_cfg_sysfs_mmio_match,
768                 .acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match),
769         },
770 };
771
772 #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
773
774 static struct platform_device *fw_cfg_cmdline_dev;
775
776 /* this probably belongs in e.g. include/linux/types.h,
777  * but right now we are the only ones doing it...
778  */
779 #ifdef CONFIG_PHYS_ADDR_T_64BIT
780 #define __PHYS_ADDR_PREFIX "ll"
781 #else
782 #define __PHYS_ADDR_PREFIX ""
783 #endif
784
785 /* use special scanf/printf modifier for phys_addr_t, resource_size_t */
786 #define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \
787                          ":%" __PHYS_ADDR_PREFIX "i" \
788                          ":%" __PHYS_ADDR_PREFIX "i%n" \
789                          ":%" __PHYS_ADDR_PREFIX "i%n"
790
791 #define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \
792                          "0x%" __PHYS_ADDR_PREFIX "x"
793
794 #define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \
795                          ":%" __PHYS_ADDR_PREFIX "u" \
796                          ":%" __PHYS_ADDR_PREFIX "u"
797
798 #define PH_ADDR_PR_4_FMT PH_ADDR_PR_3_FMT \
799                          ":%" __PHYS_ADDR_PREFIX "u"
800
801 static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp)
802 {
803         struct resource res[4] = {};
804         char *str;
805         phys_addr_t base;
806         resource_size_t size, ctrl_off, data_off, dma_off;
807         int processed, consumed = 0;
808
809         /* only one fw_cfg device can exist system-wide, so if one
810          * was processed on the command line already, we might as
811          * well stop here.
812          */
813         if (fw_cfg_cmdline_dev) {
814                 /* avoid leaking previously registered device */
815                 platform_device_unregister(fw_cfg_cmdline_dev);
816                 return -EINVAL;
817         }
818
819         /* consume "<size>" portion of command line argument */
820         size = memparse(arg, &str);
821
822         /* get "@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]" chunks */
823         processed = sscanf(str, PH_ADDR_SCAN_FMT,
824                            &base, &consumed,
825                            &ctrl_off, &data_off, &consumed,
826                            &dma_off, &consumed);
827
828         /* sscanf() must process precisely 1, 3 or 4 chunks:
829          * <base> is mandatory, optionally followed by <ctrl_off>
830          * and <data_off>, and <dma_off>;
831          * there must be no extra characters after the last chunk,
832          * so str[consumed] must be '\0'.
833          */
834         if (str[consumed] ||
835             (processed != 1 && processed != 3 && processed != 4))
836                 return -EINVAL;
837
838         res[0].start = base;
839         res[0].end = base + size - 1;
840         res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM :
841                                                    IORESOURCE_IO;
842
843         /* insert register offsets, if provided */
844         if (processed > 1) {
845                 res[1].name = "ctrl";
846                 res[1].start = ctrl_off;
847                 res[1].flags = IORESOURCE_REG;
848                 res[2].name = "data";
849                 res[2].start = data_off;
850                 res[2].flags = IORESOURCE_REG;
851         }
852         if (processed > 3) {
853                 res[3].name = "dma";
854                 res[3].start = dma_off;
855                 res[3].flags = IORESOURCE_REG;
856         }
857
858         /* "processed" happens to nicely match the number of resources
859          * we need to pass in to this platform device.
860          */
861         fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg",
862                                         PLATFORM_DEVID_NONE, res, processed);
863
864         return PTR_ERR_OR_ZERO(fw_cfg_cmdline_dev);
865 }
866
867 static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp)
868 {
869         /* stay silent if device was not configured via the command
870          * line, or if the parameter name (ioport/mmio) doesn't match
871          * the device setting
872          */
873         if (!fw_cfg_cmdline_dev ||
874             (!strcmp(kp->name, "mmio") ^
875              (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM)))
876                 return 0;
877
878         switch (fw_cfg_cmdline_dev->num_resources) {
879         case 1:
880                 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT,
881                                 resource_size(&fw_cfg_cmdline_dev->resource[0]),
882                                 fw_cfg_cmdline_dev->resource[0].start);
883         case 3:
884                 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT,
885                                 resource_size(&fw_cfg_cmdline_dev->resource[0]),
886                                 fw_cfg_cmdline_dev->resource[0].start,
887                                 fw_cfg_cmdline_dev->resource[1].start,
888                                 fw_cfg_cmdline_dev->resource[2].start);
889         case 4:
890                 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_4_FMT,
891                                 resource_size(&fw_cfg_cmdline_dev->resource[0]),
892                                 fw_cfg_cmdline_dev->resource[0].start,
893                                 fw_cfg_cmdline_dev->resource[1].start,
894                                 fw_cfg_cmdline_dev->resource[2].start,
895                                 fw_cfg_cmdline_dev->resource[3].start);
896         }
897
898         /* Should never get here */
899         WARN(1, "Unexpected number of resources: %d\n",
900                 fw_cfg_cmdline_dev->num_resources);
901         return 0;
902 }
903
904 static const struct kernel_param_ops fw_cfg_cmdline_param_ops = {
905         .set = fw_cfg_cmdline_set,
906         .get = fw_cfg_cmdline_get,
907 };
908
909 device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
910 device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
911
912 #endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */
913
914 static int __init fw_cfg_sysfs_init(void)
915 {
916         int ret;
917
918         /* create /sys/firmware/qemu_fw_cfg/ top level directory */
919         fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj);
920         if (!fw_cfg_top_ko)
921                 return -ENOMEM;
922
923         ret = platform_driver_register(&fw_cfg_sysfs_driver);
924         if (ret)
925                 fw_cfg_kobj_cleanup(fw_cfg_top_ko);
926
927         return ret;
928 }
929
930 static void __exit fw_cfg_sysfs_exit(void)
931 {
932         platform_driver_unregister(&fw_cfg_sysfs_driver);
933
934 #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
935         platform_device_unregister(fw_cfg_cmdline_dev);
936 #endif
937
938         /* clean up /sys/firmware/qemu_fw_cfg/ */
939         fw_cfg_kobj_cleanup(fw_cfg_top_ko);
940 }
941
942 module_init(fw_cfg_sysfs_init);
943 module_exit(fw_cfg_sysfs_exit);
This page took 0.129562 seconds and 4 git commands to generate.