Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * drivers/firmware/qemu_fw_cfg.c
0003  *
0004  * Copyright 2015 Carnegie Mellon University
0005  *
0006  * Expose entries from QEMU's firmware configuration (fw_cfg) device in
0007  * sysfs (read-only, under "/sys/firmware/qemu_fw_cfg/...").
0008  *
0009  * The fw_cfg device may be instantiated via either an ACPI node (on x86
0010  * and select subsets of aarch64), a Device Tree node (on arm), or using
0011  * a kernel module (or command line) parameter with the following syntax:
0012  *
0013  *      [qemu_fw_cfg.]ioport=<size>@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]
0014  * or
0015  *      [qemu_fw_cfg.]mmio=<size>@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]
0016  *
0017  * where:
0018  *      <size>     := size of ioport or mmio range
0019  *      <base>     := physical base address of ioport or mmio range
0020  *      <ctrl_off> := (optional) offset of control register
0021  *      <data_off> := (optional) offset of data register
0022  *      <dma_off> := (optional) offset of dma register
0023  *
0024  * e.g.:
0025  *      qemu_fw_cfg.ioport=12@0x510:0:1:4   (the default on x86)
0026  * or
0027  *      qemu_fw_cfg.mmio=16@0x9020000:8:0:16    (the default on arm)
0028  */
0029 
0030 #include <linux/module.h>
0031 #include <linux/mod_devicetable.h>
0032 #include <linux/platform_device.h>
0033 #include <linux/acpi.h>
0034 #include <linux/slab.h>
0035 #include <linux/io.h>
0036 #include <linux/ioport.h>
0037 #include <uapi/linux/qemu_fw_cfg.h>
0038 #include <linux/delay.h>
0039 #include <linux/crash_dump.h>
0040 #include <linux/crash_core.h>
0041 
0042 MODULE_AUTHOR("Gabriel L. Somlo <somlo@cmu.edu>");
0043 MODULE_DESCRIPTION("QEMU fw_cfg sysfs support");
0044 MODULE_LICENSE("GPL");
0045 
0046 /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */
0047 static u32 fw_cfg_rev;
0048 
0049 /* fw_cfg device i/o register addresses */
0050 static bool fw_cfg_is_mmio;
0051 static phys_addr_t fw_cfg_p_base;
0052 static resource_size_t fw_cfg_p_size;
0053 static void __iomem *fw_cfg_dev_base;
0054 static void __iomem *fw_cfg_reg_ctrl;
0055 static void __iomem *fw_cfg_reg_data;
0056 static void __iomem *fw_cfg_reg_dma;
0057 
0058 /* atomic access to fw_cfg device (potentially slow i/o, so using mutex) */
0059 static DEFINE_MUTEX(fw_cfg_dev_lock);
0060 
0061 /* pick appropriate endianness for selector key */
0062 static void fw_cfg_sel_endianness(u16 key)
0063 {
0064     if (fw_cfg_is_mmio)
0065         iowrite16be(key, fw_cfg_reg_ctrl);
0066     else
0067         iowrite16(key, fw_cfg_reg_ctrl);
0068 }
0069 
0070 #ifdef CONFIG_CRASH_CORE
0071 static inline bool fw_cfg_dma_enabled(void)
0072 {
0073     return (fw_cfg_rev & FW_CFG_VERSION_DMA) && fw_cfg_reg_dma;
0074 }
0075 
0076 /* qemu fw_cfg device is sync today, but spec says it may become async */
0077 static void fw_cfg_wait_for_control(struct fw_cfg_dma_access *d)
0078 {
0079     for (;;) {
0080         u32 ctrl = be32_to_cpu(READ_ONCE(d->control));
0081 
0082         /* do not reorder the read to d->control */
0083         rmb();
0084         if ((ctrl & ~FW_CFG_DMA_CTL_ERROR) == 0)
0085             return;
0086 
0087         cpu_relax();
0088     }
0089 }
0090 
0091 static ssize_t fw_cfg_dma_transfer(void *address, u32 length, u32 control)
0092 {
0093     phys_addr_t dma;
0094     struct fw_cfg_dma_access *d = NULL;
0095     ssize_t ret = length;
0096 
0097     d = kmalloc(sizeof(*d), GFP_KERNEL);
0098     if (!d) {
0099         ret = -ENOMEM;
0100         goto end;
0101     }
0102 
0103     /* fw_cfg device does not need IOMMU protection, so use physical addresses */
0104     *d = (struct fw_cfg_dma_access) {
0105         .address = cpu_to_be64(address ? virt_to_phys(address) : 0),
0106         .length = cpu_to_be32(length),
0107         .control = cpu_to_be32(control)
0108     };
0109 
0110     dma = virt_to_phys(d);
0111 
0112     iowrite32be((u64)dma >> 32, fw_cfg_reg_dma);
0113     /* force memory to sync before notifying device via MMIO */
0114     wmb();
0115     iowrite32be(dma, fw_cfg_reg_dma + 4);
0116 
0117     fw_cfg_wait_for_control(d);
0118 
0119     if (be32_to_cpu(READ_ONCE(d->control)) & FW_CFG_DMA_CTL_ERROR) {
0120         ret = -EIO;
0121     }
0122 
0123 end:
0124     kfree(d);
0125 
0126     return ret;
0127 }
0128 #endif
0129 
0130 /* read chunk of given fw_cfg blob (caller responsible for sanity-check) */
0131 static ssize_t fw_cfg_read_blob(u16 key,
0132                 void *buf, loff_t pos, size_t count)
0133 {
0134     u32 glk = -1U;
0135     acpi_status status;
0136 
0137     /* If we have ACPI, ensure mutual exclusion against any potential
0138      * device access by the firmware, e.g. via AML methods:
0139      */
0140     status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
0141     if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
0142         /* Should never get here */
0143         WARN(1, "fw_cfg_read_blob: Failed to lock ACPI!\n");
0144         memset(buf, 0, count);
0145         return -EINVAL;
0146     }
0147 
0148     mutex_lock(&fw_cfg_dev_lock);
0149     fw_cfg_sel_endianness(key);
0150     while (pos-- > 0)
0151         ioread8(fw_cfg_reg_data);
0152     ioread8_rep(fw_cfg_reg_data, buf, count);
0153     mutex_unlock(&fw_cfg_dev_lock);
0154 
0155     acpi_release_global_lock(glk);
0156     return count;
0157 }
0158 
0159 #ifdef CONFIG_CRASH_CORE
0160 /* write chunk of given fw_cfg blob (caller responsible for sanity-check) */
0161 static ssize_t fw_cfg_write_blob(u16 key,
0162                  void *buf, loff_t pos, size_t count)
0163 {
0164     u32 glk = -1U;
0165     acpi_status status;
0166     ssize_t ret = count;
0167 
0168     /* If we have ACPI, ensure mutual exclusion against any potential
0169      * device access by the firmware, e.g. via AML methods:
0170      */
0171     status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk);
0172     if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) {
0173         /* Should never get here */
0174         WARN(1, "%s: Failed to lock ACPI!\n", __func__);
0175         return -EINVAL;
0176     }
0177 
0178     mutex_lock(&fw_cfg_dev_lock);
0179     if (pos == 0) {
0180         ret = fw_cfg_dma_transfer(buf, count, key << 16
0181                       | FW_CFG_DMA_CTL_SELECT
0182                       | FW_CFG_DMA_CTL_WRITE);
0183     } else {
0184         fw_cfg_sel_endianness(key);
0185         ret = fw_cfg_dma_transfer(NULL, pos, FW_CFG_DMA_CTL_SKIP);
0186         if (ret < 0)
0187             goto end;
0188         ret = fw_cfg_dma_transfer(buf, count, FW_CFG_DMA_CTL_WRITE);
0189     }
0190 
0191 end:
0192     mutex_unlock(&fw_cfg_dev_lock);
0193 
0194     acpi_release_global_lock(glk);
0195 
0196     return ret;
0197 }
0198 #endif /* CONFIG_CRASH_CORE */
0199 
0200 /* clean up fw_cfg device i/o */
0201 static void fw_cfg_io_cleanup(void)
0202 {
0203     if (fw_cfg_is_mmio) {
0204         iounmap(fw_cfg_dev_base);
0205         release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
0206     } else {
0207         ioport_unmap(fw_cfg_dev_base);
0208         release_region(fw_cfg_p_base, fw_cfg_p_size);
0209     }
0210 }
0211 
0212 /* arch-specific ctrl & data register offsets are not available in ACPI, DT */
0213 #if !(defined(FW_CFG_CTRL_OFF) && defined(FW_CFG_DATA_OFF))
0214 # if (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
0215 #  define FW_CFG_CTRL_OFF 0x08
0216 #  define FW_CFG_DATA_OFF 0x00
0217 #  define FW_CFG_DMA_OFF 0x10
0218 # elif defined(CONFIG_PARISC)   /* parisc */
0219 #  define FW_CFG_CTRL_OFF 0x00
0220 #  define FW_CFG_DATA_OFF 0x04
0221 # elif (defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC32)) /* ppc/mac,sun4m */
0222 #  define FW_CFG_CTRL_OFF 0x00
0223 #  define FW_CFG_DATA_OFF 0x02
0224 # elif (defined(CONFIG_X86) || defined(CONFIG_SPARC64)) /* x86, sun4u */
0225 #  define FW_CFG_CTRL_OFF 0x00
0226 #  define FW_CFG_DATA_OFF 0x01
0227 #  define FW_CFG_DMA_OFF 0x04
0228 # else
0229 #  error "QEMU FW_CFG not available on this architecture!"
0230 # endif
0231 #endif
0232 
0233 /* initialize fw_cfg device i/o from platform data */
0234 static int fw_cfg_do_platform_probe(struct platform_device *pdev)
0235 {
0236     char sig[FW_CFG_SIG_SIZE];
0237     struct resource *range, *ctrl, *data, *dma;
0238 
0239     /* acquire i/o range details */
0240     fw_cfg_is_mmio = false;
0241     range = platform_get_resource(pdev, IORESOURCE_IO, 0);
0242     if (!range) {
0243         fw_cfg_is_mmio = true;
0244         range = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0245         if (!range)
0246             return -EINVAL;
0247     }
0248     fw_cfg_p_base = range->start;
0249     fw_cfg_p_size = resource_size(range);
0250 
0251     if (fw_cfg_is_mmio) {
0252         if (!request_mem_region(fw_cfg_p_base,
0253                     fw_cfg_p_size, "fw_cfg_mem"))
0254             return -EBUSY;
0255         fw_cfg_dev_base = ioremap(fw_cfg_p_base, fw_cfg_p_size);
0256         if (!fw_cfg_dev_base) {
0257             release_mem_region(fw_cfg_p_base, fw_cfg_p_size);
0258             return -EFAULT;
0259         }
0260     } else {
0261         if (!request_region(fw_cfg_p_base,
0262                     fw_cfg_p_size, "fw_cfg_io"))
0263             return -EBUSY;
0264         fw_cfg_dev_base = ioport_map(fw_cfg_p_base, fw_cfg_p_size);
0265         if (!fw_cfg_dev_base) {
0266             release_region(fw_cfg_p_base, fw_cfg_p_size);
0267             return -EFAULT;
0268         }
0269     }
0270 
0271     /* were custom register offsets provided (e.g. on the command line)? */
0272     ctrl = platform_get_resource_byname(pdev, IORESOURCE_REG, "ctrl");
0273     data = platform_get_resource_byname(pdev, IORESOURCE_REG, "data");
0274     dma = platform_get_resource_byname(pdev, IORESOURCE_REG, "dma");
0275     if (ctrl && data) {
0276         fw_cfg_reg_ctrl = fw_cfg_dev_base + ctrl->start;
0277         fw_cfg_reg_data = fw_cfg_dev_base + data->start;
0278     } else {
0279         /* use architecture-specific offsets */
0280         fw_cfg_reg_ctrl = fw_cfg_dev_base + FW_CFG_CTRL_OFF;
0281         fw_cfg_reg_data = fw_cfg_dev_base + FW_CFG_DATA_OFF;
0282     }
0283 
0284     if (dma)
0285         fw_cfg_reg_dma = fw_cfg_dev_base + dma->start;
0286 #ifdef FW_CFG_DMA_OFF
0287     else
0288         fw_cfg_reg_dma = fw_cfg_dev_base + FW_CFG_DMA_OFF;
0289 #endif
0290 
0291     /* verify fw_cfg device signature */
0292     if (fw_cfg_read_blob(FW_CFG_SIGNATURE, sig,
0293                 0, FW_CFG_SIG_SIZE) < 0 ||
0294         memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) != 0) {
0295         fw_cfg_io_cleanup();
0296         return -ENODEV;
0297     }
0298 
0299     return 0;
0300 }
0301 
0302 static ssize_t fw_cfg_showrev(struct kobject *k, struct kobj_attribute *a,
0303                   char *buf)
0304 {
0305     return sprintf(buf, "%u\n", fw_cfg_rev);
0306 }
0307 
0308 static const struct kobj_attribute fw_cfg_rev_attr = {
0309     .attr = { .name = "rev", .mode = S_IRUSR },
0310     .show = fw_cfg_showrev,
0311 };
0312 
0313 /* fw_cfg_sysfs_entry type */
0314 struct fw_cfg_sysfs_entry {
0315     struct kobject kobj;
0316     u32 size;
0317     u16 select;
0318     char name[FW_CFG_MAX_FILE_PATH];
0319     struct list_head list;
0320 };
0321 
0322 #ifdef CONFIG_CRASH_CORE
0323 static ssize_t fw_cfg_write_vmcoreinfo(const struct fw_cfg_file *f)
0324 {
0325     static struct fw_cfg_vmcoreinfo *data;
0326     ssize_t ret;
0327 
0328     data = kmalloc(sizeof(struct fw_cfg_vmcoreinfo), GFP_KERNEL);
0329     if (!data)
0330         return -ENOMEM;
0331 
0332     *data = (struct fw_cfg_vmcoreinfo) {
0333         .guest_format = cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF),
0334         .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE),
0335         .paddr = cpu_to_le64(paddr_vmcoreinfo_note())
0336     };
0337     /* spare ourself reading host format support for now since we
0338      * don't know what else to format - host may ignore ours
0339      */
0340     ret = fw_cfg_write_blob(be16_to_cpu(f->select), data,
0341                 0, sizeof(struct fw_cfg_vmcoreinfo));
0342 
0343     kfree(data);
0344     return ret;
0345 }
0346 #endif /* CONFIG_CRASH_CORE */
0347 
0348 /* get fw_cfg_sysfs_entry from kobject member */
0349 static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj)
0350 {
0351     return container_of(kobj, struct fw_cfg_sysfs_entry, kobj);
0352 }
0353 
0354 /* fw_cfg_sysfs_attribute type */
0355 struct fw_cfg_sysfs_attribute {
0356     struct attribute attr;
0357     ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf);
0358 };
0359 
0360 /* get fw_cfg_sysfs_attribute from attribute member */
0361 static inline struct fw_cfg_sysfs_attribute *to_attr(struct attribute *attr)
0362 {
0363     return container_of(attr, struct fw_cfg_sysfs_attribute, attr);
0364 }
0365 
0366 /* global cache of fw_cfg_sysfs_entry objects */
0367 static LIST_HEAD(fw_cfg_entry_cache);
0368 
0369 /* kobjects removed lazily by kernel, mutual exclusion needed */
0370 static DEFINE_SPINLOCK(fw_cfg_cache_lock);
0371 
0372 static inline void fw_cfg_sysfs_cache_enlist(struct fw_cfg_sysfs_entry *entry)
0373 {
0374     spin_lock(&fw_cfg_cache_lock);
0375     list_add_tail(&entry->list, &fw_cfg_entry_cache);
0376     spin_unlock(&fw_cfg_cache_lock);
0377 }
0378 
0379 static inline void fw_cfg_sysfs_cache_delist(struct fw_cfg_sysfs_entry *entry)
0380 {
0381     spin_lock(&fw_cfg_cache_lock);
0382     list_del(&entry->list);
0383     spin_unlock(&fw_cfg_cache_lock);
0384 }
0385 
0386 static void fw_cfg_sysfs_cache_cleanup(void)
0387 {
0388     struct fw_cfg_sysfs_entry *entry, *next;
0389 
0390     list_for_each_entry_safe(entry, next, &fw_cfg_entry_cache, list) {
0391         fw_cfg_sysfs_cache_delist(entry);
0392         kobject_del(&entry->kobj);
0393         kobject_put(&entry->kobj);
0394     }
0395 }
0396 
0397 /* per-entry attributes and show methods */
0398 
0399 #define FW_CFG_SYSFS_ATTR(_attr) \
0400 struct fw_cfg_sysfs_attribute fw_cfg_sysfs_attr_##_attr = { \
0401     .attr = { .name = __stringify(_attr), .mode = S_IRUSR }, \
0402     .show = fw_cfg_sysfs_show_##_attr, \
0403 }
0404 
0405 static ssize_t fw_cfg_sysfs_show_size(struct fw_cfg_sysfs_entry *e, char *buf)
0406 {
0407     return sprintf(buf, "%u\n", e->size);
0408 }
0409 
0410 static ssize_t fw_cfg_sysfs_show_key(struct fw_cfg_sysfs_entry *e, char *buf)
0411 {
0412     return sprintf(buf, "%u\n", e->select);
0413 }
0414 
0415 static ssize_t fw_cfg_sysfs_show_name(struct fw_cfg_sysfs_entry *e, char *buf)
0416 {
0417     return sprintf(buf, "%s\n", e->name);
0418 }
0419 
0420 static FW_CFG_SYSFS_ATTR(size);
0421 static FW_CFG_SYSFS_ATTR(key);
0422 static FW_CFG_SYSFS_ATTR(name);
0423 
0424 static struct attribute *fw_cfg_sysfs_entry_attrs[] = {
0425     &fw_cfg_sysfs_attr_size.attr,
0426     &fw_cfg_sysfs_attr_key.attr,
0427     &fw_cfg_sysfs_attr_name.attr,
0428     NULL,
0429 };
0430 ATTRIBUTE_GROUPS(fw_cfg_sysfs_entry);
0431 
0432 /* sysfs_ops: find fw_cfg_[entry, attribute] and call appropriate show method */
0433 static ssize_t fw_cfg_sysfs_attr_show(struct kobject *kobj, struct attribute *a,
0434                       char *buf)
0435 {
0436     struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
0437     struct fw_cfg_sysfs_attribute *attr = to_attr(a);
0438 
0439     return attr->show(entry, buf);
0440 }
0441 
0442 static const struct sysfs_ops fw_cfg_sysfs_attr_ops = {
0443     .show = fw_cfg_sysfs_attr_show,
0444 };
0445 
0446 /* release: destructor, to be called via kobject_put() */
0447 static void fw_cfg_sysfs_release_entry(struct kobject *kobj)
0448 {
0449     struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
0450 
0451     kfree(entry);
0452 }
0453 
0454 /* kobj_type: ties together all properties required to register an entry */
0455 static struct kobj_type fw_cfg_sysfs_entry_ktype = {
0456     .default_groups = fw_cfg_sysfs_entry_groups,
0457     .sysfs_ops = &fw_cfg_sysfs_attr_ops,
0458     .release = fw_cfg_sysfs_release_entry,
0459 };
0460 
0461 /* raw-read method and attribute */
0462 static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj,
0463                      struct bin_attribute *bin_attr,
0464                      char *buf, loff_t pos, size_t count)
0465 {
0466     struct fw_cfg_sysfs_entry *entry = to_entry(kobj);
0467 
0468     if (pos > entry->size)
0469         return -EINVAL;
0470 
0471     if (count > entry->size - pos)
0472         count = entry->size - pos;
0473 
0474     return fw_cfg_read_blob(entry->select, buf, pos, count);
0475 }
0476 
0477 static struct bin_attribute fw_cfg_sysfs_attr_raw = {
0478     .attr = { .name = "raw", .mode = S_IRUSR },
0479     .read = fw_cfg_sysfs_read_raw,
0480 };
0481 
0482 /*
0483  * Create a kset subdirectory matching each '/' delimited dirname token
0484  * in 'name', starting with sysfs kset/folder 'dir'; At the end, create
0485  * a symlink directed at the given 'target'.
0486  * NOTE: We do this on a best-effort basis, since 'name' is not guaranteed
0487  * to be a well-behaved path name. Whenever a symlink vs. kset directory
0488  * name collision occurs, the kernel will issue big scary warnings while
0489  * refusing to add the offending link or directory. We follow up with our
0490  * own, slightly less scary error messages explaining the situation :)
0491  */
0492 static int fw_cfg_build_symlink(struct kset *dir,
0493                 struct kobject *target, const char *name)
0494 {
0495     int ret;
0496     struct kset *subdir;
0497     struct kobject *ko;
0498     char *name_copy, *p, *tok;
0499 
0500     if (!dir || !target || !name || !*name)
0501         return -EINVAL;
0502 
0503     /* clone a copy of name for parsing */
0504     name_copy = p = kstrdup(name, GFP_KERNEL);
0505     if (!name_copy)
0506         return -ENOMEM;
0507 
0508     /* create folders for each dirname token, then symlink for basename */
0509     while ((tok = strsep(&p, "/")) && *tok) {
0510 
0511         /* last (basename) token? If so, add symlink here */
0512         if (!p || !*p) {
0513             ret = sysfs_create_link(&dir->kobj, target, tok);
0514             break;
0515         }
0516 
0517         /* does the current dir contain an item named after tok ? */
0518         ko = kset_find_obj(dir, tok);
0519         if (ko) {
0520             /* drop reference added by kset_find_obj */
0521             kobject_put(ko);
0522 
0523             /* ko MUST be a kset - we're about to use it as one ! */
0524             if (ko->ktype != dir->kobj.ktype) {
0525                 ret = -EINVAL;
0526                 break;
0527             }
0528 
0529             /* descend into already existing subdirectory */
0530             dir = to_kset(ko);
0531         } else {
0532             /* create new subdirectory kset */
0533             subdir = kzalloc(sizeof(struct kset), GFP_KERNEL);
0534             if (!subdir) {
0535                 ret = -ENOMEM;
0536                 break;
0537             }
0538             subdir->kobj.kset = dir;
0539             subdir->kobj.ktype = dir->kobj.ktype;
0540             ret = kobject_set_name(&subdir->kobj, "%s", tok);
0541             if (ret) {
0542                 kfree(subdir);
0543                 break;
0544             }
0545             ret = kset_register(subdir);
0546             if (ret) {
0547                 kfree(subdir);
0548                 break;
0549             }
0550 
0551             /* descend into newly created subdirectory */
0552             dir = subdir;
0553         }
0554     }
0555 
0556     /* we're done with cloned copy of name */
0557     kfree(name_copy);
0558     return ret;
0559 }
0560 
0561 /* recursively unregister fw_cfg/by_name/ kset directory tree */
0562 static void fw_cfg_kset_unregister_recursive(struct kset *kset)
0563 {
0564     struct kobject *k, *next;
0565 
0566     list_for_each_entry_safe(k, next, &kset->list, entry)
0567         /* all set members are ksets too, but check just in case... */
0568         if (k->ktype == kset->kobj.ktype)
0569             fw_cfg_kset_unregister_recursive(to_kset(k));
0570 
0571     /* symlinks are cleanly and automatically removed with the directory */
0572     kset_unregister(kset);
0573 }
0574 
0575 /* kobjects & kset representing top-level, by_key, and by_name folders */
0576 static struct kobject *fw_cfg_top_ko;
0577 static struct kobject *fw_cfg_sel_ko;
0578 static struct kset *fw_cfg_fname_kset;
0579 
0580 /* register an individual fw_cfg file */
0581 static int fw_cfg_register_file(const struct fw_cfg_file *f)
0582 {
0583     int err;
0584     struct fw_cfg_sysfs_entry *entry;
0585 
0586 #ifdef CONFIG_CRASH_CORE
0587     if (fw_cfg_dma_enabled() &&
0588         strcmp(f->name, FW_CFG_VMCOREINFO_FILENAME) == 0 &&
0589         !is_kdump_kernel()) {
0590         if (fw_cfg_write_vmcoreinfo(f) < 0)
0591             pr_warn("fw_cfg: failed to write vmcoreinfo");
0592     }
0593 #endif
0594 
0595     /* allocate new entry */
0596     entry = kzalloc(sizeof(*entry), GFP_KERNEL);
0597     if (!entry)
0598         return -ENOMEM;
0599 
0600     /* set file entry information */
0601     entry->size = be32_to_cpu(f->size);
0602     entry->select = be16_to_cpu(f->select);
0603     strscpy(entry->name, f->name, FW_CFG_MAX_FILE_PATH);
0604 
0605     /* register entry under "/sys/firmware/qemu_fw_cfg/by_key/" */
0606     err = kobject_init_and_add(&entry->kobj, &fw_cfg_sysfs_entry_ktype,
0607                    fw_cfg_sel_ko, "%d", entry->select);
0608     if (err)
0609         goto err_put_entry;
0610 
0611     /* add raw binary content access */
0612     err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw);
0613     if (err)
0614         goto err_del_entry;
0615 
0616     /* try adding "/sys/firmware/qemu_fw_cfg/by_name/" symlink */
0617     fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->name);
0618 
0619     /* success, add entry to global cache */
0620     fw_cfg_sysfs_cache_enlist(entry);
0621     return 0;
0622 
0623 err_del_entry:
0624     kobject_del(&entry->kobj);
0625 err_put_entry:
0626     kobject_put(&entry->kobj);
0627     return err;
0628 }
0629 
0630 /* iterate over all fw_cfg directory entries, registering each one */
0631 static int fw_cfg_register_dir_entries(void)
0632 {
0633     int ret = 0;
0634     __be32 files_count;
0635     u32 count, i;
0636     struct fw_cfg_file *dir;
0637     size_t dir_size;
0638 
0639     ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, &files_count,
0640             0, sizeof(files_count));
0641     if (ret < 0)
0642         return ret;
0643 
0644     count = be32_to_cpu(files_count);
0645     dir_size = count * sizeof(struct fw_cfg_file);
0646 
0647     dir = kmalloc(dir_size, GFP_KERNEL);
0648     if (!dir)
0649         return -ENOMEM;
0650 
0651     ret = fw_cfg_read_blob(FW_CFG_FILE_DIR, dir,
0652             sizeof(files_count), dir_size);
0653     if (ret < 0)
0654         goto end;
0655 
0656     for (i = 0; i < count; i++) {
0657         ret = fw_cfg_register_file(&dir[i]);
0658         if (ret)
0659             break;
0660     }
0661 
0662 end:
0663     kfree(dir);
0664     return ret;
0665 }
0666 
0667 /* unregister top-level or by_key folder */
0668 static inline void fw_cfg_kobj_cleanup(struct kobject *kobj)
0669 {
0670     kobject_del(kobj);
0671     kobject_put(kobj);
0672 }
0673 
0674 static int fw_cfg_sysfs_probe(struct platform_device *pdev)
0675 {
0676     int err;
0677     __le32 rev;
0678 
0679     /* NOTE: If we supported multiple fw_cfg devices, we'd first create
0680      * a subdirectory named after e.g. pdev->id, then hang per-device
0681      * by_key (and by_name) subdirectories underneath it. However, only
0682      * one fw_cfg device exist system-wide, so if one was already found
0683      * earlier, we might as well stop here.
0684      */
0685     if (fw_cfg_sel_ko)
0686         return -EBUSY;
0687 
0688     /* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */
0689     err = -ENOMEM;
0690     fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko);
0691     if (!fw_cfg_sel_ko)
0692         goto err_sel;
0693     fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko);
0694     if (!fw_cfg_fname_kset)
0695         goto err_name;
0696 
0697     /* initialize fw_cfg device i/o from platform data */
0698     err = fw_cfg_do_platform_probe(pdev);
0699     if (err)
0700         goto err_probe;
0701 
0702     /* get revision number, add matching top-level attribute */
0703     err = fw_cfg_read_blob(FW_CFG_ID, &rev, 0, sizeof(rev));
0704     if (err < 0)
0705         goto err_probe;
0706 
0707     fw_cfg_rev = le32_to_cpu(rev);
0708     err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
0709     if (err)
0710         goto err_rev;
0711 
0712     /* process fw_cfg file directory entry, registering each file */
0713     err = fw_cfg_register_dir_entries();
0714     if (err)
0715         goto err_dir;
0716 
0717     /* success */
0718     pr_debug("fw_cfg: loaded.\n");
0719     return 0;
0720 
0721 err_dir:
0722     fw_cfg_sysfs_cache_cleanup();
0723     sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
0724 err_rev:
0725     fw_cfg_io_cleanup();
0726 err_probe:
0727     fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
0728 err_name:
0729     fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
0730 err_sel:
0731     return err;
0732 }
0733 
0734 static int fw_cfg_sysfs_remove(struct platform_device *pdev)
0735 {
0736     pr_debug("fw_cfg: unloading.\n");
0737     fw_cfg_sysfs_cache_cleanup();
0738     sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr);
0739     fw_cfg_io_cleanup();
0740     fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset);
0741     fw_cfg_kobj_cleanup(fw_cfg_sel_ko);
0742     return 0;
0743 }
0744 
0745 static const struct of_device_id fw_cfg_sysfs_mmio_match[] = {
0746     { .compatible = "qemu,fw-cfg-mmio", },
0747     {},
0748 };
0749 MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match);
0750 
0751 #ifdef CONFIG_ACPI
0752 static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = {
0753     { FW_CFG_ACPI_DEVICE_ID, },
0754     {},
0755 };
0756 MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match);
0757 #endif
0758 
0759 static struct platform_driver fw_cfg_sysfs_driver = {
0760     .probe = fw_cfg_sysfs_probe,
0761     .remove = fw_cfg_sysfs_remove,
0762     .driver = {
0763         .name = "fw_cfg",
0764         .of_match_table = fw_cfg_sysfs_mmio_match,
0765         .acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match),
0766     },
0767 };
0768 
0769 #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
0770 
0771 static struct platform_device *fw_cfg_cmdline_dev;
0772 
0773 /* this probably belongs in e.g. include/linux/types.h,
0774  * but right now we are the only ones doing it...
0775  */
0776 #ifdef CONFIG_PHYS_ADDR_T_64BIT
0777 #define __PHYS_ADDR_PREFIX "ll"
0778 #else
0779 #define __PHYS_ADDR_PREFIX ""
0780 #endif
0781 
0782 /* use special scanf/printf modifier for phys_addr_t, resource_size_t */
0783 #define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \
0784              ":%" __PHYS_ADDR_PREFIX "i" \
0785              ":%" __PHYS_ADDR_PREFIX "i%n" \
0786              ":%" __PHYS_ADDR_PREFIX "i%n"
0787 
0788 #define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \
0789              "0x%" __PHYS_ADDR_PREFIX "x"
0790 
0791 #define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \
0792              ":%" __PHYS_ADDR_PREFIX "u" \
0793              ":%" __PHYS_ADDR_PREFIX "u"
0794 
0795 #define PH_ADDR_PR_4_FMT PH_ADDR_PR_3_FMT \
0796              ":%" __PHYS_ADDR_PREFIX "u"
0797 
0798 static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp)
0799 {
0800     struct resource res[4] = {};
0801     char *str;
0802     phys_addr_t base;
0803     resource_size_t size, ctrl_off, data_off, dma_off;
0804     int processed, consumed = 0;
0805 
0806     /* only one fw_cfg device can exist system-wide, so if one
0807      * was processed on the command line already, we might as
0808      * well stop here.
0809      */
0810     if (fw_cfg_cmdline_dev) {
0811         /* avoid leaking previously registered device */
0812         platform_device_unregister(fw_cfg_cmdline_dev);
0813         return -EINVAL;
0814     }
0815 
0816     /* consume "<size>" portion of command line argument */
0817     size = memparse(arg, &str);
0818 
0819     /* get "@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]" chunks */
0820     processed = sscanf(str, PH_ADDR_SCAN_FMT,
0821                &base, &consumed,
0822                &ctrl_off, &data_off, &consumed,
0823                &dma_off, &consumed);
0824 
0825     /* sscanf() must process precisely 1, 3 or 4 chunks:
0826      * <base> is mandatory, optionally followed by <ctrl_off>
0827      * and <data_off>, and <dma_off>;
0828      * there must be no extra characters after the last chunk,
0829      * so str[consumed] must be '\0'.
0830      */
0831     if (str[consumed] ||
0832         (processed != 1 && processed != 3 && processed != 4))
0833         return -EINVAL;
0834 
0835     res[0].start = base;
0836     res[0].end = base + size - 1;
0837     res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM :
0838                            IORESOURCE_IO;
0839 
0840     /* insert register offsets, if provided */
0841     if (processed > 1) {
0842         res[1].name = "ctrl";
0843         res[1].start = ctrl_off;
0844         res[1].flags = IORESOURCE_REG;
0845         res[2].name = "data";
0846         res[2].start = data_off;
0847         res[2].flags = IORESOURCE_REG;
0848     }
0849     if (processed > 3) {
0850         res[3].name = "dma";
0851         res[3].start = dma_off;
0852         res[3].flags = IORESOURCE_REG;
0853     }
0854 
0855     /* "processed" happens to nicely match the number of resources
0856      * we need to pass in to this platform device.
0857      */
0858     fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg",
0859                     PLATFORM_DEVID_NONE, res, processed);
0860 
0861     return PTR_ERR_OR_ZERO(fw_cfg_cmdline_dev);
0862 }
0863 
0864 static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp)
0865 {
0866     /* stay silent if device was not configured via the command
0867      * line, or if the parameter name (ioport/mmio) doesn't match
0868      * the device setting
0869      */
0870     if (!fw_cfg_cmdline_dev ||
0871         (!strcmp(kp->name, "mmio") ^
0872          (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM)))
0873         return 0;
0874 
0875     switch (fw_cfg_cmdline_dev->num_resources) {
0876     case 1:
0877         return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT,
0878                 resource_size(&fw_cfg_cmdline_dev->resource[0]),
0879                 fw_cfg_cmdline_dev->resource[0].start);
0880     case 3:
0881         return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT,
0882                 resource_size(&fw_cfg_cmdline_dev->resource[0]),
0883                 fw_cfg_cmdline_dev->resource[0].start,
0884                 fw_cfg_cmdline_dev->resource[1].start,
0885                 fw_cfg_cmdline_dev->resource[2].start);
0886     case 4:
0887         return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_4_FMT,
0888                 resource_size(&fw_cfg_cmdline_dev->resource[0]),
0889                 fw_cfg_cmdline_dev->resource[0].start,
0890                 fw_cfg_cmdline_dev->resource[1].start,
0891                 fw_cfg_cmdline_dev->resource[2].start,
0892                 fw_cfg_cmdline_dev->resource[3].start);
0893     }
0894 
0895     /* Should never get here */
0896     WARN(1, "Unexpected number of resources: %d\n",
0897         fw_cfg_cmdline_dev->num_resources);
0898     return 0;
0899 }
0900 
0901 static const struct kernel_param_ops fw_cfg_cmdline_param_ops = {
0902     .set = fw_cfg_cmdline_set,
0903     .get = fw_cfg_cmdline_get,
0904 };
0905 
0906 device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
0907 device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR);
0908 
0909 #endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */
0910 
0911 static int __init fw_cfg_sysfs_init(void)
0912 {
0913     int ret;
0914 
0915     /* create /sys/firmware/qemu_fw_cfg/ top level directory */
0916     fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj);
0917     if (!fw_cfg_top_ko)
0918         return -ENOMEM;
0919 
0920     ret = platform_driver_register(&fw_cfg_sysfs_driver);
0921     if (ret)
0922         fw_cfg_kobj_cleanup(fw_cfg_top_ko);
0923 
0924     return ret;
0925 }
0926 
0927 static void __exit fw_cfg_sysfs_exit(void)
0928 {
0929     platform_driver_unregister(&fw_cfg_sysfs_driver);
0930 
0931 #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE
0932     platform_device_unregister(fw_cfg_cmdline_dev);
0933 #endif
0934 
0935     /* clean up /sys/firmware/qemu_fw_cfg/ */
0936     fw_cfg_kobj_cleanup(fw_cfg_top_ko);
0937 }
0938 
0939 module_init(fw_cfg_sysfs_init);
0940 module_exit(fw_cfg_sysfs_exit);