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/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/vmcore_info.h> 40 41 MODULE_AUTHOR("Gabriel L. Somlo <somlo@cmu.edu>"); 42 MODULE_DESCRIPTION("QEMU fw_cfg sysfs support"); 43 MODULE_LICENSE("GPL"); 44 45 /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */ 46 static u32 fw_cfg_rev; 47 48 /* fw_cfg device i/o register addresses */ 49 static bool fw_cfg_is_mmio; 50 static phys_addr_t fw_cfg_p_base; 51 static resource_size_t fw_cfg_p_size; 52 static void __iomem *fw_cfg_dev_base; 53 static void __iomem *fw_cfg_reg_ctrl; 54 static void __iomem *fw_cfg_reg_data; 55 static void __iomem *fw_cfg_reg_dma; 56 57 /* atomic access to fw_cfg device (potentially slow i/o, so using mutex) */ 58 static DEFINE_MUTEX(fw_cfg_dev_lock); 59 60 /* pick appropriate endianness for selector key */ 61 static 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_VMCORE_INFO 70 static inline bool fw_cfg_dma_enabled(void) 71 { 72 return (fw_cfg_rev & FW_CFG_VERSION_DMA) && fw_cfg_reg_dma; 73 } 74 75 /* qemu fw_cfg device is sync today, but spec says it may become async */ 76 static 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 /* do not reorder the read to d->control */ 82 rmb(); 83 if ((ctrl & ~FW_CFG_DMA_CTL_ERROR) == 0) 84 return; 85 86 cpu_relax(); 87 } 88 } 89 90 static 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_obj(*d); 97 if (!d) { 98 ret = -ENOMEM; 99 goto end; 100 } 101 102 /* fw_cfg device does not need IOMMU protection, so use physical addresses */ 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 /* force memory to sync before notifying device via MMIO */ 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 122 end: 123 kfree(d); 124 125 return ret; 126 } 127 #endif 128 129 /* read chunk of given fw_cfg blob (caller responsible for sanity-check) */ 130 static 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 /* If we have ACPI, ensure mutual exclusion against any potential 137 * device access by the firmware, e.g. via AML methods: 138 */ 139 status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk); 140 if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) { 141 /* Should never get here */ 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_VMCORE_INFO 159 /* write chunk of given fw_cfg blob (caller responsible for sanity-check) */ 160 static 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 /* If we have ACPI, ensure mutual exclusion against any potential 168 * device access by the firmware, e.g. via AML methods: 169 */ 170 status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk); 171 if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) { 172 /* Should never get here */ 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 190 end: 191 mutex_unlock(&fw_cfg_dev_lock); 192 193 acpi_release_global_lock(glk); 194 195 return ret; 196 } 197 #endif /* CONFIG_VMCORE_INFO */ 198 199 /* clean up fw_cfg device i/o */ 200 static 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 /* arch-specific ctrl & data register offsets are not available in ACPI, DT */ 212 #if !(defined(FW_CFG_CTRL_OFF) && defined(FW_CFG_DATA_OFF)) 213 # if (defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_LOONGARCH) || defined(CONFIG_RISCV)) 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_PARISC) /* parisc */ 218 # define FW_CFG_CTRL_OFF 0x00 219 # define FW_CFG_DATA_OFF 0x04 220 # elif (defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC32)) /* ppc/mac,sun4m */ 221 # define FW_CFG_CTRL_OFF 0x00 222 # define FW_CFG_DATA_OFF 0x02 223 # elif (defined(CONFIG_X86) || defined(CONFIG_SPARC64)) /* x86, sun4u */ 224 # define FW_CFG_CTRL_OFF 0x00 225 # define FW_CFG_DATA_OFF 0x01 226 # define FW_CFG_DMA_OFF 0x04 227 # else 228 # error "QEMU FW_CFG not available on this architecture!" 229 # endif 230 #endif 231 232 /* initialize fw_cfg device i/o from platform data */ 233 static int fw_cfg_do_platform_probe(struct platform_device *pdev) 234 { 235 char sig[FW_CFG_SIG_SIZE]; 236 struct resource *range, *ctrl, *data, *dma; 237 238 /* acquire i/o range details */ 239 fw_cfg_is_mmio = false; 240 range = platform_get_resource(pdev, IORESOURCE_IO, 0); 241 if (!range) { 242 fw_cfg_is_mmio = true; 243 range = platform_get_resource(pdev, IORESOURCE_MEM, 0); 244 if (!range) 245 return -EINVAL; 246 } 247 fw_cfg_p_base = range->start; 248 fw_cfg_p_size = resource_size(range); 249 250 if (fw_cfg_is_mmio) { 251 if (!request_mem_region(fw_cfg_p_base, 252 fw_cfg_p_size, "fw_cfg_mem")) 253 return -EBUSY; 254 fw_cfg_dev_base = ioremap(fw_cfg_p_base, fw_cfg_p_size); 255 if (!fw_cfg_dev_base) { 256 release_mem_region(fw_cfg_p_base, fw_cfg_p_size); 257 return -EFAULT; 258 } 259 } else { 260 if (!request_region(fw_cfg_p_base, 261 fw_cfg_p_size, "fw_cfg_io")) 262 return -EBUSY; 263 fw_cfg_dev_base = ioport_map(fw_cfg_p_base, fw_cfg_p_size); 264 if (!fw_cfg_dev_base) { 265 release_region(fw_cfg_p_base, fw_cfg_p_size); 266 return -EFAULT; 267 } 268 } 269 270 /* were custom register offsets provided (e.g. on the command line)? */ 271 ctrl = platform_get_resource_byname(pdev, IORESOURCE_REG, "ctrl"); 272 data = platform_get_resource_byname(pdev, IORESOURCE_REG, "data"); 273 dma = platform_get_resource_byname(pdev, IORESOURCE_REG, "dma"); 274 if (ctrl && data) { 275 fw_cfg_reg_ctrl = fw_cfg_dev_base + ctrl->start; 276 fw_cfg_reg_data = fw_cfg_dev_base + data->start; 277 } else { 278 /* use architecture-specific offsets */ 279 fw_cfg_reg_ctrl = fw_cfg_dev_base + FW_CFG_CTRL_OFF; 280 fw_cfg_reg_data = fw_cfg_dev_base + FW_CFG_DATA_OFF; 281 } 282 283 if (dma) 284 fw_cfg_reg_dma = fw_cfg_dev_base + dma->start; 285 #ifdef FW_CFG_DMA_OFF 286 else 287 fw_cfg_reg_dma = fw_cfg_dev_base + FW_CFG_DMA_OFF; 288 #endif 289 290 /* verify fw_cfg device signature */ 291 if (fw_cfg_read_blob(FW_CFG_SIGNATURE, sig, 292 0, FW_CFG_SIG_SIZE) < 0 || 293 memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) != 0) { 294 fw_cfg_io_cleanup(); 295 return -ENODEV; 296 } 297 298 return 0; 299 } 300 301 static ssize_t fw_cfg_showrev(struct kobject *k, struct kobj_attribute *a, 302 char *buf) 303 { 304 return sprintf(buf, "%u\n", fw_cfg_rev); 305 } 306 307 static const struct kobj_attribute fw_cfg_rev_attr = { 308 .attr = { .name = "rev", .mode = S_IRUSR }, 309 .show = fw_cfg_showrev, 310 }; 311 312 /* fw_cfg_sysfs_entry type */ 313 struct fw_cfg_sysfs_entry { 314 struct kobject kobj; 315 u32 size; 316 u16 select; 317 char name[FW_CFG_MAX_FILE_PATH]; 318 struct list_head list; 319 }; 320 321 #ifdef CONFIG_VMCORE_INFO 322 static ssize_t fw_cfg_write_vmcoreinfo(const struct fw_cfg_file *f) 323 { 324 static struct fw_cfg_vmcoreinfo *data; 325 ssize_t ret; 326 327 data = kmalloc_obj(struct fw_cfg_vmcoreinfo); 328 if (!data) 329 return -ENOMEM; 330 331 *data = (struct fw_cfg_vmcoreinfo) { 332 .guest_format = cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF), 333 .size = cpu_to_le32(VMCOREINFO_NOTE_SIZE), 334 .paddr = cpu_to_le64(paddr_vmcoreinfo_note()) 335 }; 336 /* spare ourself reading host format support for now since we 337 * don't know what else to format - host may ignore ours 338 */ 339 ret = fw_cfg_write_blob(be16_to_cpu(f->select), data, 340 0, sizeof(struct fw_cfg_vmcoreinfo)); 341 342 kfree(data); 343 return ret; 344 } 345 #endif /* CONFIG_VMCORE_INFO */ 346 347 /* get fw_cfg_sysfs_entry from kobject member */ 348 static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj) 349 { 350 return container_of(kobj, struct fw_cfg_sysfs_entry, kobj); 351 } 352 353 /* fw_cfg_sysfs_attribute type */ 354 struct fw_cfg_sysfs_attribute { 355 struct attribute attr; 356 ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf); 357 }; 358 359 /* get fw_cfg_sysfs_attribute from attribute member */ 360 static inline struct fw_cfg_sysfs_attribute *to_attr(struct attribute *attr) 361 { 362 return container_of(attr, struct fw_cfg_sysfs_attribute, attr); 363 } 364 365 /* global cache of fw_cfg_sysfs_entry objects */ 366 static LIST_HEAD(fw_cfg_entry_cache); 367 368 /* kobjects removed lazily by kernel, mutual exclusion needed */ 369 static DEFINE_SPINLOCK(fw_cfg_cache_lock); 370 371 static inline void fw_cfg_sysfs_cache_enlist(struct fw_cfg_sysfs_entry *entry) 372 { 373 spin_lock(&fw_cfg_cache_lock); 374 list_add_tail(&entry->list, &fw_cfg_entry_cache); 375 spin_unlock(&fw_cfg_cache_lock); 376 } 377 378 static inline void fw_cfg_sysfs_cache_delist(struct fw_cfg_sysfs_entry *entry) 379 { 380 spin_lock(&fw_cfg_cache_lock); 381 list_del(&entry->list); 382 spin_unlock(&fw_cfg_cache_lock); 383 } 384 385 static void fw_cfg_sysfs_cache_cleanup(void) 386 { 387 struct fw_cfg_sysfs_entry *entry, *next; 388 389 list_for_each_entry_safe(entry, next, &fw_cfg_entry_cache, list) { 390 fw_cfg_sysfs_cache_delist(entry); 391 kobject_del(&entry->kobj); 392 kobject_put(&entry->kobj); 393 } 394 } 395 396 /* per-entry attributes and show methods */ 397 398 #define FW_CFG_SYSFS_ATTR(_attr) \ 399 struct 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 404 static 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 409 static 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 414 static 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 419 static FW_CFG_SYSFS_ATTR(size); 420 static FW_CFG_SYSFS_ATTR(key); 421 static FW_CFG_SYSFS_ATTR(name); 422 423 static 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 ATTRIBUTE_GROUPS(fw_cfg_sysfs_entry); 430 431 /* sysfs_ops: find fw_cfg_[entry, attribute] and call appropriate show method */ 432 static ssize_t fw_cfg_sysfs_attr_show(struct kobject *kobj, struct attribute *a, 433 char *buf) 434 { 435 struct fw_cfg_sysfs_entry *entry = to_entry(kobj); 436 struct fw_cfg_sysfs_attribute *attr = to_attr(a); 437 438 return attr->show(entry, buf); 439 } 440 441 static const struct sysfs_ops fw_cfg_sysfs_attr_ops = { 442 .show = fw_cfg_sysfs_attr_show, 443 }; 444 445 /* release: destructor, to be called via kobject_put() */ 446 static void fw_cfg_sysfs_release_entry(struct kobject *kobj) 447 { 448 struct fw_cfg_sysfs_entry *entry = to_entry(kobj); 449 450 kfree(entry); 451 } 452 453 /* kobj_type: ties together all properties required to register an entry */ 454 static const struct kobj_type fw_cfg_sysfs_entry_ktype = { 455 .default_groups = fw_cfg_sysfs_entry_groups, 456 .sysfs_ops = &fw_cfg_sysfs_attr_ops, 457 .release = fw_cfg_sysfs_release_entry, 458 }; 459 460 /* raw-read method and attribute */ 461 static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj, 462 const 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 476 static const 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 * Create a kset subdirectory matching each '/' delimited dirname token 483 * in 'name', starting with sysfs kset/folder 'dir'; At the end, create 484 * a symlink directed at the given 'target'. 485 * NOTE: We do this on a best-effort basis, since 'name' is not guaranteed 486 * to be a well-behaved path name. Whenever a symlink vs. kset directory 487 * name collision occurs, the kernel will issue big scary warnings while 488 * refusing to add the offending link or directory. We follow up with our 489 * own, slightly less scary error messages explaining the situation :) 490 */ 491 static 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 /* clone a copy of name for parsing */ 503 name_copy = p = kstrdup(name, GFP_KERNEL); 504 if (!name_copy) 505 return -ENOMEM; 506 507 /* create folders for each dirname token, then symlink for basename */ 508 while ((tok = strsep(&p, "/")) && *tok) { 509 510 /* last (basename) token? If so, add symlink here */ 511 if (!p || !*p) { 512 ret = sysfs_create_link(&dir->kobj, target, tok); 513 break; 514 } 515 516 /* does the current dir contain an item named after tok ? */ 517 ko = kset_find_obj(dir, tok); 518 if (ko) { 519 /* drop reference added by kset_find_obj */ 520 kobject_put(ko); 521 522 /* ko MUST be a kset - we're about to use it as one ! */ 523 if (ko->ktype != dir->kobj.ktype) { 524 ret = -EINVAL; 525 break; 526 } 527 528 /* descend into already existing subdirectory */ 529 dir = to_kset(ko); 530 } else { 531 /* create new subdirectory kset */ 532 subdir = kzalloc_obj(struct kset); 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 /* descend into newly created subdirectory */ 551 dir = subdir; 552 } 553 } 554 555 /* we're done with cloned copy of name */ 556 kfree(name_copy); 557 return ret; 558 } 559 560 /* recursively unregister fw_cfg/by_name/ kset directory tree */ 561 static 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 /* all set members are ksets too, but check just in case... */ 567 if (k->ktype == kset->kobj.ktype) 568 fw_cfg_kset_unregister_recursive(to_kset(k)); 569 570 /* symlinks are cleanly and automatically removed with the directory */ 571 kset_unregister(kset); 572 } 573 574 /* kobjects & kset representing top-level, by_key, and by_name folders */ 575 static struct kobject *fw_cfg_top_ko; 576 static struct kobject *fw_cfg_sel_ko; 577 static struct kset *fw_cfg_fname_kset; 578 579 /* register an individual fw_cfg file */ 580 static 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_VMCORE_INFO 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 /* allocate new entry */ 595 entry = kzalloc_obj(*entry); 596 if (!entry) 597 return -ENOMEM; 598 599 /* set file entry information */ 600 entry->size = be32_to_cpu(f->size); 601 entry->select = be16_to_cpu(f->select); 602 strscpy(entry->name, f->name, FW_CFG_MAX_FILE_PATH); 603 604 /* register entry under "/sys/firmware/qemu_fw_cfg/by_key/" */ 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_put_entry; 609 610 /* add raw binary content access */ 611 err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw); 612 if (err) 613 goto err_del_entry; 614 615 /* try adding "/sys/firmware/qemu_fw_cfg/by_name/" symlink */ 616 fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->name); 617 618 /* success, add entry to global cache */ 619 fw_cfg_sysfs_cache_enlist(entry); 620 return 0; 621 622 err_del_entry: 623 kobject_del(&entry->kobj); 624 err_put_entry: 625 kobject_put(&entry->kobj); 626 return err; 627 } 628 629 /* iterate over all fw_cfg directory entries, registering each one */ 630 static 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 661 end: 662 kfree(dir); 663 return ret; 664 } 665 666 /* unregister top-level or by_key folder */ 667 static inline void fw_cfg_kobj_cleanup(struct kobject *kobj) 668 { 669 kobject_del(kobj); 670 kobject_put(kobj); 671 } 672 673 static int fw_cfg_sysfs_probe(struct platform_device *pdev) 674 { 675 int err; 676 __le32 rev; 677 678 /* NOTE: If we supported multiple fw_cfg devices, we'd first create 679 * a subdirectory named after e.g. pdev->id, then hang per-device 680 * by_key (and by_name) subdirectories underneath it. However, only 681 * one fw_cfg device exist system-wide, so if one was already found 682 * earlier, we might as well stop here. 683 */ 684 if (fw_cfg_sel_ko) 685 return -EBUSY; 686 687 /* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */ 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 /* initialize fw_cfg device i/o from platform data */ 697 err = fw_cfg_do_platform_probe(pdev); 698 if (err) 699 goto err_probe; 700 701 /* get revision number, add matching top-level attribute */ 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 /* process fw_cfg file directory entry, registering each file */ 712 err = fw_cfg_register_dir_entries(); 713 if (err) 714 goto err_dir; 715 716 /* success */ 717 pr_debug("fw_cfg: loaded.\n"); 718 return 0; 719 720 err_dir: 721 fw_cfg_sysfs_cache_cleanup(); 722 sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr); 723 err_rev: 724 fw_cfg_io_cleanup(); 725 err_probe: 726 fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset); 727 err_name: 728 fw_cfg_kobj_cleanup(fw_cfg_sel_ko); 729 err_sel: 730 return err; 731 } 732 733 static void 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 } 742 743 static const struct of_device_id fw_cfg_sysfs_mmio_match[] = { 744 { .compatible = "qemu,fw-cfg-mmio", }, 745 {}, 746 }; 747 MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match); 748 749 #ifdef CONFIG_ACPI 750 static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = { 751 { FW_CFG_ACPI_DEVICE_ID, }, 752 {}, 753 }; 754 MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match); 755 #endif 756 757 static struct platform_driver fw_cfg_sysfs_driver = { 758 .probe = fw_cfg_sysfs_probe, 759 .remove = fw_cfg_sysfs_remove, 760 .driver = { 761 .name = "fw_cfg", 762 .of_match_table = fw_cfg_sysfs_mmio_match, 763 .acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match), 764 }, 765 }; 766 767 #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE 768 769 static struct platform_device *fw_cfg_cmdline_dev; 770 771 /* this probably belongs in e.g. include/linux/types.h, 772 * but right now we are the only ones doing it... 773 */ 774 #ifdef CONFIG_PHYS_ADDR_T_64BIT 775 #define __PHYS_ADDR_PREFIX "ll" 776 #else 777 #define __PHYS_ADDR_PREFIX "" 778 #endif 779 780 /* use special scanf/printf modifier for phys_addr_t, resource_size_t */ 781 #define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \ 782 ":%" __PHYS_ADDR_PREFIX "i" \ 783 ":%" __PHYS_ADDR_PREFIX "i%n" \ 784 ":%" __PHYS_ADDR_PREFIX "i%n" 785 786 #define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \ 787 "0x%" __PHYS_ADDR_PREFIX "x" 788 789 #define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \ 790 ":%" __PHYS_ADDR_PREFIX "u" \ 791 ":%" __PHYS_ADDR_PREFIX "u" 792 793 #define PH_ADDR_PR_4_FMT PH_ADDR_PR_3_FMT \ 794 ":%" __PHYS_ADDR_PREFIX "u" 795 796 static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp) 797 { 798 struct resource res[4] = {}; 799 char *str; 800 phys_addr_t base; 801 resource_size_t size, ctrl_off, data_off, dma_off; 802 int processed, consumed = 0; 803 804 /* only one fw_cfg device can exist system-wide, so if one 805 * was processed on the command line already, we might as 806 * well stop here. 807 */ 808 if (fw_cfg_cmdline_dev) { 809 /* avoid leaking previously registered device */ 810 platform_device_unregister(fw_cfg_cmdline_dev); 811 return -EINVAL; 812 } 813 814 /* consume "<size>" portion of command line argument */ 815 size = memparse(arg, &str); 816 817 /* get "@<base>[:<ctrl_off>:<data_off>[:<dma_off>]]" chunks */ 818 processed = sscanf(str, PH_ADDR_SCAN_FMT, 819 &base, &consumed, 820 &ctrl_off, &data_off, &consumed, 821 &dma_off, &consumed); 822 823 /* sscanf() must process precisely 1, 3 or 4 chunks: 824 * <base> is mandatory, optionally followed by <ctrl_off> 825 * and <data_off>, and <dma_off>; 826 * there must be no extra characters after the last chunk, 827 * so str[consumed] must be '\0'. 828 */ 829 if (str[consumed] || 830 (processed != 1 && processed != 3 && processed != 4)) 831 return -EINVAL; 832 833 res[0].start = base; 834 res[0].end = base + size - 1; 835 res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM : 836 IORESOURCE_IO; 837 838 /* insert register offsets, if provided */ 839 if (processed > 1) { 840 res[1].name = "ctrl"; 841 res[1].start = ctrl_off; 842 res[1].flags = IORESOURCE_REG; 843 res[2].name = "data"; 844 res[2].start = data_off; 845 res[2].flags = IORESOURCE_REG; 846 } 847 if (processed > 3) { 848 res[3].name = "dma"; 849 res[3].start = dma_off; 850 res[3].flags = IORESOURCE_REG; 851 } 852 853 /* "processed" happens to nicely match the number of resources 854 * we need to pass in to this platform device. 855 */ 856 fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg", 857 PLATFORM_DEVID_NONE, res, processed); 858 859 return PTR_ERR_OR_ZERO(fw_cfg_cmdline_dev); 860 } 861 862 static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp) 863 { 864 /* stay silent if device was not configured via the command 865 * line, or if the parameter name (ioport/mmio) doesn't match 866 * the device setting 867 */ 868 if (!fw_cfg_cmdline_dev || 869 (!strcmp(kp->name, "mmio") ^ 870 (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM))) 871 return 0; 872 873 switch (fw_cfg_cmdline_dev->num_resources) { 874 case 1: 875 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT, 876 resource_size(&fw_cfg_cmdline_dev->resource[0]), 877 fw_cfg_cmdline_dev->resource[0].start); 878 case 3: 879 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT, 880 resource_size(&fw_cfg_cmdline_dev->resource[0]), 881 fw_cfg_cmdline_dev->resource[0].start, 882 fw_cfg_cmdline_dev->resource[1].start, 883 fw_cfg_cmdline_dev->resource[2].start); 884 case 4: 885 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_4_FMT, 886 resource_size(&fw_cfg_cmdline_dev->resource[0]), 887 fw_cfg_cmdline_dev->resource[0].start, 888 fw_cfg_cmdline_dev->resource[1].start, 889 fw_cfg_cmdline_dev->resource[2].start, 890 fw_cfg_cmdline_dev->resource[3].start); 891 } 892 893 /* Should never get here */ 894 WARN(1, "Unexpected number of resources: %d\n", 895 fw_cfg_cmdline_dev->num_resources); 896 return 0; 897 } 898 899 static const struct kernel_param_ops fw_cfg_cmdline_param_ops = { 900 .set = fw_cfg_cmdline_set, 901 .get = fw_cfg_cmdline_get, 902 }; 903 904 device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR); 905 device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR); 906 907 #endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */ 908 909 static int __init fw_cfg_sysfs_init(void) 910 { 911 int ret; 912 913 /* create /sys/firmware/qemu_fw_cfg/ top level directory */ 914 fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj); 915 if (!fw_cfg_top_ko) 916 return -ENOMEM; 917 918 ret = platform_driver_register(&fw_cfg_sysfs_driver); 919 if (ret) 920 fw_cfg_kobj_cleanup(fw_cfg_top_ko); 921 922 return ret; 923 } 924 925 static void __exit fw_cfg_sysfs_exit(void) 926 { 927 platform_driver_unregister(&fw_cfg_sysfs_driver); 928 929 #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE 930 platform_device_unregister(fw_cfg_cmdline_dev); 931 #endif 932 933 /* clean up /sys/firmware/qemu_fw_cfg/ */ 934 fw_cfg_kobj_cleanup(fw_cfg_top_ko); 935 } 936 937 module_init(fw_cfg_sysfs_init); 938 module_exit(fw_cfg_sysfs_exit); 939