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>] 14 * or 15 * [qemu_fw_cfg.]mmio=<size>@<base>[:<ctrl_off>:<data_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 * 23 * e.g.: 24 * qemu_fw_cfg.ioport=2@0x510:0:1 (the default on x86) 25 * or 26 * qemu_fw_cfg.mmio=0xA@0x9020000:8:0 (the default on arm) 27 */ 28 29 #include <linux/module.h> 30 #include <linux/platform_device.h> 31 #include <linux/acpi.h> 32 #include <linux/slab.h> 33 #include <linux/io.h> 34 #include <linux/ioport.h> 35 36 MODULE_AUTHOR("Gabriel L. Somlo <somlo@cmu.edu>"); 37 MODULE_DESCRIPTION("QEMU fw_cfg sysfs support"); 38 MODULE_LICENSE("GPL"); 39 40 /* selector key values for "well-known" fw_cfg entries */ 41 #define FW_CFG_SIGNATURE 0x00 42 #define FW_CFG_ID 0x01 43 #define FW_CFG_FILE_DIR 0x19 44 45 /* size in bytes of fw_cfg signature */ 46 #define FW_CFG_SIG_SIZE 4 47 48 /* fw_cfg "file name" is up to 56 characters (including terminating nul) */ 49 #define FW_CFG_MAX_FILE_PATH 56 50 51 /* fw_cfg file directory entry type */ 52 struct fw_cfg_file { 53 u32 size; 54 u16 select; 55 u16 reserved; 56 char name[FW_CFG_MAX_FILE_PATH]; 57 }; 58 59 /* fw_cfg device i/o register addresses */ 60 static bool fw_cfg_is_mmio; 61 static phys_addr_t fw_cfg_p_base; 62 static resource_size_t fw_cfg_p_size; 63 static void __iomem *fw_cfg_dev_base; 64 static void __iomem *fw_cfg_reg_ctrl; 65 static void __iomem *fw_cfg_reg_data; 66 67 /* atomic access to fw_cfg device (potentially slow i/o, so using mutex) */ 68 static DEFINE_MUTEX(fw_cfg_dev_lock); 69 70 /* pick appropriate endianness for selector key */ 71 static void fw_cfg_sel_endianness(u16 key) 72 { 73 if (fw_cfg_is_mmio) 74 iowrite16be(key, fw_cfg_reg_ctrl); 75 else 76 iowrite16(key, fw_cfg_reg_ctrl); 77 } 78 79 /* read chunk of given fw_cfg blob (caller responsible for sanity-check) */ 80 static void fw_cfg_read_blob(u16 key, 81 void *buf, loff_t pos, size_t count) 82 { 83 u32 glk = -1U; 84 acpi_status status; 85 86 /* If we have ACPI, ensure mutual exclusion against any potential 87 * device access by the firmware, e.g. via AML methods: 88 */ 89 status = acpi_acquire_global_lock(ACPI_WAIT_FOREVER, &glk); 90 if (ACPI_FAILURE(status) && status != AE_NOT_CONFIGURED) { 91 /* Should never get here */ 92 WARN(1, "fw_cfg_read_blob: Failed to lock ACPI!\n"); 93 memset(buf, 0, count); 94 return; 95 } 96 97 mutex_lock(&fw_cfg_dev_lock); 98 fw_cfg_sel_endianness(key); 99 while (pos-- > 0) 100 ioread8(fw_cfg_reg_data); 101 ioread8_rep(fw_cfg_reg_data, buf, count); 102 mutex_unlock(&fw_cfg_dev_lock); 103 104 acpi_release_global_lock(glk); 105 } 106 107 /* clean up fw_cfg device i/o */ 108 static void fw_cfg_io_cleanup(void) 109 { 110 if (fw_cfg_is_mmio) { 111 iounmap(fw_cfg_dev_base); 112 release_mem_region(fw_cfg_p_base, fw_cfg_p_size); 113 } else { 114 ioport_unmap(fw_cfg_dev_base); 115 release_region(fw_cfg_p_base, fw_cfg_p_size); 116 } 117 } 118 119 /* arch-specific ctrl & data register offsets are not available in ACPI, DT */ 120 #if !(defined(FW_CFG_CTRL_OFF) && defined(FW_CFG_DATA_OFF)) 121 # if (defined(CONFIG_ARM) || defined(CONFIG_ARM64)) 122 # define FW_CFG_CTRL_OFF 0x08 123 # define FW_CFG_DATA_OFF 0x00 124 # elif (defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC32)) /* ppc/mac,sun4m */ 125 # define FW_CFG_CTRL_OFF 0x00 126 # define FW_CFG_DATA_OFF 0x02 127 # elif (defined(CONFIG_X86) || defined(CONFIG_SPARC64)) /* x86, sun4u */ 128 # define FW_CFG_CTRL_OFF 0x00 129 # define FW_CFG_DATA_OFF 0x01 130 # else 131 # error "QEMU FW_CFG not available on this architecture!" 132 # endif 133 #endif 134 135 /* initialize fw_cfg device i/o from platform data */ 136 static int fw_cfg_do_platform_probe(struct platform_device *pdev) 137 { 138 char sig[FW_CFG_SIG_SIZE]; 139 struct resource *range, *ctrl, *data; 140 141 /* acquire i/o range details */ 142 fw_cfg_is_mmio = false; 143 range = platform_get_resource(pdev, IORESOURCE_IO, 0); 144 if (!range) { 145 fw_cfg_is_mmio = true; 146 range = platform_get_resource(pdev, IORESOURCE_MEM, 0); 147 if (!range) 148 return -EINVAL; 149 } 150 fw_cfg_p_base = range->start; 151 fw_cfg_p_size = resource_size(range); 152 153 if (fw_cfg_is_mmio) { 154 if (!request_mem_region(fw_cfg_p_base, 155 fw_cfg_p_size, "fw_cfg_mem")) 156 return -EBUSY; 157 fw_cfg_dev_base = ioremap(fw_cfg_p_base, fw_cfg_p_size); 158 if (!fw_cfg_dev_base) { 159 release_mem_region(fw_cfg_p_base, fw_cfg_p_size); 160 return -EFAULT; 161 } 162 } else { 163 if (!request_region(fw_cfg_p_base, 164 fw_cfg_p_size, "fw_cfg_io")) 165 return -EBUSY; 166 fw_cfg_dev_base = ioport_map(fw_cfg_p_base, fw_cfg_p_size); 167 if (!fw_cfg_dev_base) { 168 release_region(fw_cfg_p_base, fw_cfg_p_size); 169 return -EFAULT; 170 } 171 } 172 173 /* were custom register offsets provided (e.g. on the command line)? */ 174 ctrl = platform_get_resource_byname(pdev, IORESOURCE_REG, "ctrl"); 175 data = platform_get_resource_byname(pdev, IORESOURCE_REG, "data"); 176 if (ctrl && data) { 177 fw_cfg_reg_ctrl = fw_cfg_dev_base + ctrl->start; 178 fw_cfg_reg_data = fw_cfg_dev_base + data->start; 179 } else { 180 /* use architecture-specific offsets */ 181 fw_cfg_reg_ctrl = fw_cfg_dev_base + FW_CFG_CTRL_OFF; 182 fw_cfg_reg_data = fw_cfg_dev_base + FW_CFG_DATA_OFF; 183 } 184 185 /* verify fw_cfg device signature */ 186 fw_cfg_read_blob(FW_CFG_SIGNATURE, sig, 0, FW_CFG_SIG_SIZE); 187 if (memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) != 0) { 188 fw_cfg_io_cleanup(); 189 return -ENODEV; 190 } 191 192 return 0; 193 } 194 195 /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */ 196 static u32 fw_cfg_rev; 197 198 static ssize_t fw_cfg_showrev(struct kobject *k, struct attribute *a, char *buf) 199 { 200 return sprintf(buf, "%u\n", fw_cfg_rev); 201 } 202 203 static const struct { 204 struct attribute attr; 205 ssize_t (*show)(struct kobject *k, struct attribute *a, char *buf); 206 } fw_cfg_rev_attr = { 207 .attr = { .name = "rev", .mode = S_IRUSR }, 208 .show = fw_cfg_showrev, 209 }; 210 211 /* fw_cfg_sysfs_entry type */ 212 struct fw_cfg_sysfs_entry { 213 struct kobject kobj; 214 u32 size; 215 u16 select; 216 char name[FW_CFG_MAX_FILE_PATH]; 217 struct list_head list; 218 }; 219 220 /* get fw_cfg_sysfs_entry from kobject member */ 221 static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj) 222 { 223 return container_of(kobj, struct fw_cfg_sysfs_entry, kobj); 224 } 225 226 /* fw_cfg_sysfs_attribute type */ 227 struct fw_cfg_sysfs_attribute { 228 struct attribute attr; 229 ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf); 230 }; 231 232 /* get fw_cfg_sysfs_attribute from attribute member */ 233 static inline struct fw_cfg_sysfs_attribute *to_attr(struct attribute *attr) 234 { 235 return container_of(attr, struct fw_cfg_sysfs_attribute, attr); 236 } 237 238 /* global cache of fw_cfg_sysfs_entry objects */ 239 static LIST_HEAD(fw_cfg_entry_cache); 240 241 /* kobjects removed lazily by kernel, mutual exclusion needed */ 242 static DEFINE_SPINLOCK(fw_cfg_cache_lock); 243 244 static inline void fw_cfg_sysfs_cache_enlist(struct fw_cfg_sysfs_entry *entry) 245 { 246 spin_lock(&fw_cfg_cache_lock); 247 list_add_tail(&entry->list, &fw_cfg_entry_cache); 248 spin_unlock(&fw_cfg_cache_lock); 249 } 250 251 static inline void fw_cfg_sysfs_cache_delist(struct fw_cfg_sysfs_entry *entry) 252 { 253 spin_lock(&fw_cfg_cache_lock); 254 list_del(&entry->list); 255 spin_unlock(&fw_cfg_cache_lock); 256 } 257 258 static void fw_cfg_sysfs_cache_cleanup(void) 259 { 260 struct fw_cfg_sysfs_entry *entry, *next; 261 262 list_for_each_entry_safe(entry, next, &fw_cfg_entry_cache, list) { 263 /* will end up invoking fw_cfg_sysfs_cache_delist() 264 * via each object's release() method (i.e. destructor) 265 */ 266 kobject_put(&entry->kobj); 267 } 268 } 269 270 /* default_attrs: per-entry attributes and show methods */ 271 272 #define FW_CFG_SYSFS_ATTR(_attr) \ 273 struct fw_cfg_sysfs_attribute fw_cfg_sysfs_attr_##_attr = { \ 274 .attr = { .name = __stringify(_attr), .mode = S_IRUSR }, \ 275 .show = fw_cfg_sysfs_show_##_attr, \ 276 } 277 278 static ssize_t fw_cfg_sysfs_show_size(struct fw_cfg_sysfs_entry *e, char *buf) 279 { 280 return sprintf(buf, "%u\n", e->size); 281 } 282 283 static ssize_t fw_cfg_sysfs_show_key(struct fw_cfg_sysfs_entry *e, char *buf) 284 { 285 return sprintf(buf, "%u\n", e->select); 286 } 287 288 static ssize_t fw_cfg_sysfs_show_name(struct fw_cfg_sysfs_entry *e, char *buf) 289 { 290 return sprintf(buf, "%s\n", e->name); 291 } 292 293 static FW_CFG_SYSFS_ATTR(size); 294 static FW_CFG_SYSFS_ATTR(key); 295 static FW_CFG_SYSFS_ATTR(name); 296 297 static struct attribute *fw_cfg_sysfs_entry_attrs[] = { 298 &fw_cfg_sysfs_attr_size.attr, 299 &fw_cfg_sysfs_attr_key.attr, 300 &fw_cfg_sysfs_attr_name.attr, 301 NULL, 302 }; 303 304 /* sysfs_ops: find fw_cfg_[entry, attribute] and call appropriate show method */ 305 static ssize_t fw_cfg_sysfs_attr_show(struct kobject *kobj, struct attribute *a, 306 char *buf) 307 { 308 struct fw_cfg_sysfs_entry *entry = to_entry(kobj); 309 struct fw_cfg_sysfs_attribute *attr = to_attr(a); 310 311 return attr->show(entry, buf); 312 } 313 314 static const struct sysfs_ops fw_cfg_sysfs_attr_ops = { 315 .show = fw_cfg_sysfs_attr_show, 316 }; 317 318 /* release: destructor, to be called via kobject_put() */ 319 static void fw_cfg_sysfs_release_entry(struct kobject *kobj) 320 { 321 struct fw_cfg_sysfs_entry *entry = to_entry(kobj); 322 323 fw_cfg_sysfs_cache_delist(entry); 324 kfree(entry); 325 } 326 327 /* kobj_type: ties together all properties required to register an entry */ 328 static struct kobj_type fw_cfg_sysfs_entry_ktype = { 329 .default_attrs = fw_cfg_sysfs_entry_attrs, 330 .sysfs_ops = &fw_cfg_sysfs_attr_ops, 331 .release = fw_cfg_sysfs_release_entry, 332 }; 333 334 /* raw-read method and attribute */ 335 static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj, 336 struct bin_attribute *bin_attr, 337 char *buf, loff_t pos, size_t count) 338 { 339 struct fw_cfg_sysfs_entry *entry = to_entry(kobj); 340 341 if (pos > entry->size) 342 return -EINVAL; 343 344 if (count > entry->size - pos) 345 count = entry->size - pos; 346 347 fw_cfg_read_blob(entry->select, buf, pos, count); 348 return count; 349 } 350 351 static struct bin_attribute fw_cfg_sysfs_attr_raw = { 352 .attr = { .name = "raw", .mode = S_IRUSR }, 353 .read = fw_cfg_sysfs_read_raw, 354 }; 355 356 /* 357 * Create a kset subdirectory matching each '/' delimited dirname token 358 * in 'name', starting with sysfs kset/folder 'dir'; At the end, create 359 * a symlink directed at the given 'target'. 360 * NOTE: We do this on a best-effort basis, since 'name' is not guaranteed 361 * to be a well-behaved path name. Whenever a symlink vs. kset directory 362 * name collision occurs, the kernel will issue big scary warnings while 363 * refusing to add the offending link or directory. We follow up with our 364 * own, slightly less scary error messages explaining the situation :) 365 */ 366 static int fw_cfg_build_symlink(struct kset *dir, 367 struct kobject *target, const char *name) 368 { 369 int ret; 370 struct kset *subdir; 371 struct kobject *ko; 372 char *name_copy, *p, *tok; 373 374 if (!dir || !target || !name || !*name) 375 return -EINVAL; 376 377 /* clone a copy of name for parsing */ 378 name_copy = p = kstrdup(name, GFP_KERNEL); 379 if (!name_copy) 380 return -ENOMEM; 381 382 /* create folders for each dirname token, then symlink for basename */ 383 while ((tok = strsep(&p, "/")) && *tok) { 384 385 /* last (basename) token? If so, add symlink here */ 386 if (!p || !*p) { 387 ret = sysfs_create_link(&dir->kobj, target, tok); 388 break; 389 } 390 391 /* does the current dir contain an item named after tok ? */ 392 ko = kset_find_obj(dir, tok); 393 if (ko) { 394 /* drop reference added by kset_find_obj */ 395 kobject_put(ko); 396 397 /* ko MUST be a kset - we're about to use it as one ! */ 398 if (ko->ktype != dir->kobj.ktype) { 399 ret = -EINVAL; 400 break; 401 } 402 403 /* descend into already existing subdirectory */ 404 dir = to_kset(ko); 405 } else { 406 /* create new subdirectory kset */ 407 subdir = kzalloc(sizeof(struct kset), GFP_KERNEL); 408 if (!subdir) { 409 ret = -ENOMEM; 410 break; 411 } 412 subdir->kobj.kset = dir; 413 subdir->kobj.ktype = dir->kobj.ktype; 414 ret = kobject_set_name(&subdir->kobj, "%s", tok); 415 if (ret) { 416 kfree(subdir); 417 break; 418 } 419 ret = kset_register(subdir); 420 if (ret) { 421 kfree(subdir); 422 break; 423 } 424 425 /* descend into newly created subdirectory */ 426 dir = subdir; 427 } 428 } 429 430 /* we're done with cloned copy of name */ 431 kfree(name_copy); 432 return ret; 433 } 434 435 /* recursively unregister fw_cfg/by_name/ kset directory tree */ 436 static void fw_cfg_kset_unregister_recursive(struct kset *kset) 437 { 438 struct kobject *k, *next; 439 440 list_for_each_entry_safe(k, next, &kset->list, entry) 441 /* all set members are ksets too, but check just in case... */ 442 if (k->ktype == kset->kobj.ktype) 443 fw_cfg_kset_unregister_recursive(to_kset(k)); 444 445 /* symlinks are cleanly and automatically removed with the directory */ 446 kset_unregister(kset); 447 } 448 449 /* kobjects & kset representing top-level, by_key, and by_name folders */ 450 static struct kobject *fw_cfg_top_ko; 451 static struct kobject *fw_cfg_sel_ko; 452 static struct kset *fw_cfg_fname_kset; 453 454 /* register an individual fw_cfg file */ 455 static int fw_cfg_register_file(const struct fw_cfg_file *f) 456 { 457 int err; 458 struct fw_cfg_sysfs_entry *entry; 459 460 /* allocate new entry */ 461 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 462 if (!entry) 463 return -ENOMEM; 464 465 /* set file entry information */ 466 entry->size = be32_to_cpu(f->size); 467 entry->select = be16_to_cpu(f->select); 468 memcpy(entry->name, f->name, FW_CFG_MAX_FILE_PATH); 469 470 /* register entry under "/sys/firmware/qemu_fw_cfg/by_key/" */ 471 err = kobject_init_and_add(&entry->kobj, &fw_cfg_sysfs_entry_ktype, 472 fw_cfg_sel_ko, "%d", entry->select); 473 if (err) 474 goto err_register; 475 476 /* add raw binary content access */ 477 err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw); 478 if (err) 479 goto err_add_raw; 480 481 /* try adding "/sys/firmware/qemu_fw_cfg/by_name/" symlink */ 482 fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->name); 483 484 /* success, add entry to global cache */ 485 fw_cfg_sysfs_cache_enlist(entry); 486 return 0; 487 488 err_add_raw: 489 kobject_del(&entry->kobj); 490 err_register: 491 kfree(entry); 492 return err; 493 } 494 495 /* iterate over all fw_cfg directory entries, registering each one */ 496 static int fw_cfg_register_dir_entries(void) 497 { 498 int ret = 0; 499 __be32 files_count; 500 u32 count, i; 501 struct fw_cfg_file *dir; 502 size_t dir_size; 503 504 fw_cfg_read_blob(FW_CFG_FILE_DIR, &files_count, 0, sizeof(files_count)); 505 count = be32_to_cpu(files_count); 506 dir_size = count * sizeof(struct fw_cfg_file); 507 508 dir = kmalloc(dir_size, GFP_KERNEL); 509 if (!dir) 510 return -ENOMEM; 511 512 fw_cfg_read_blob(FW_CFG_FILE_DIR, dir, sizeof(files_count), dir_size); 513 514 for (i = 0; i < count; i++) { 515 ret = fw_cfg_register_file(&dir[i]); 516 if (ret) 517 break; 518 } 519 520 kfree(dir); 521 return ret; 522 } 523 524 /* unregister top-level or by_key folder */ 525 static inline void fw_cfg_kobj_cleanup(struct kobject *kobj) 526 { 527 kobject_del(kobj); 528 kobject_put(kobj); 529 } 530 531 static int fw_cfg_sysfs_probe(struct platform_device *pdev) 532 { 533 int err; 534 __le32 rev; 535 536 /* NOTE: If we supported multiple fw_cfg devices, we'd first create 537 * a subdirectory named after e.g. pdev->id, then hang per-device 538 * by_key (and by_name) subdirectories underneath it. However, only 539 * one fw_cfg device exist system-wide, so if one was already found 540 * earlier, we might as well stop here. 541 */ 542 if (fw_cfg_sel_ko) 543 return -EBUSY; 544 545 /* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */ 546 err = -ENOMEM; 547 fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko); 548 if (!fw_cfg_sel_ko) 549 goto err_sel; 550 fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko); 551 if (!fw_cfg_fname_kset) 552 goto err_name; 553 554 /* initialize fw_cfg device i/o from platform data */ 555 err = fw_cfg_do_platform_probe(pdev); 556 if (err) 557 goto err_probe; 558 559 /* get revision number, add matching top-level attribute */ 560 fw_cfg_read_blob(FW_CFG_ID, &rev, 0, sizeof(rev)); 561 fw_cfg_rev = le32_to_cpu(rev); 562 err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr); 563 if (err) 564 goto err_rev; 565 566 /* process fw_cfg file directory entry, registering each file */ 567 err = fw_cfg_register_dir_entries(); 568 if (err) 569 goto err_dir; 570 571 /* success */ 572 pr_debug("fw_cfg: loaded.\n"); 573 return 0; 574 575 err_dir: 576 fw_cfg_sysfs_cache_cleanup(); 577 sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr); 578 err_rev: 579 fw_cfg_io_cleanup(); 580 err_probe: 581 fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset); 582 err_name: 583 fw_cfg_kobj_cleanup(fw_cfg_sel_ko); 584 err_sel: 585 return err; 586 } 587 588 static int fw_cfg_sysfs_remove(struct platform_device *pdev) 589 { 590 pr_debug("fw_cfg: unloading.\n"); 591 fw_cfg_sysfs_cache_cleanup(); 592 sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr); 593 fw_cfg_io_cleanup(); 594 fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset); 595 fw_cfg_kobj_cleanup(fw_cfg_sel_ko); 596 return 0; 597 } 598 599 static const struct of_device_id fw_cfg_sysfs_mmio_match[] = { 600 { .compatible = "qemu,fw-cfg-mmio", }, 601 {}, 602 }; 603 MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match); 604 605 #ifdef CONFIG_ACPI 606 static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = { 607 { "QEMU0002", }, 608 {}, 609 }; 610 MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match); 611 #endif 612 613 static struct platform_driver fw_cfg_sysfs_driver = { 614 .probe = fw_cfg_sysfs_probe, 615 .remove = fw_cfg_sysfs_remove, 616 .driver = { 617 .name = "fw_cfg", 618 .of_match_table = fw_cfg_sysfs_mmio_match, 619 .acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match), 620 }, 621 }; 622 623 #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE 624 625 static struct platform_device *fw_cfg_cmdline_dev; 626 627 /* this probably belongs in e.g. include/linux/types.h, 628 * but right now we are the only ones doing it... 629 */ 630 #ifdef CONFIG_PHYS_ADDR_T_64BIT 631 #define __PHYS_ADDR_PREFIX "ll" 632 #else 633 #define __PHYS_ADDR_PREFIX "" 634 #endif 635 636 /* use special scanf/printf modifier for phys_addr_t, resource_size_t */ 637 #define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \ 638 ":%" __PHYS_ADDR_PREFIX "i" \ 639 ":%" __PHYS_ADDR_PREFIX "i%n" 640 641 #define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \ 642 "0x%" __PHYS_ADDR_PREFIX "x" 643 644 #define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \ 645 ":%" __PHYS_ADDR_PREFIX "u" \ 646 ":%" __PHYS_ADDR_PREFIX "u" 647 648 static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp) 649 { 650 struct resource res[3] = {}; 651 char *str; 652 phys_addr_t base; 653 resource_size_t size, ctrl_off, data_off; 654 int processed, consumed = 0; 655 656 /* only one fw_cfg device can exist system-wide, so if one 657 * was processed on the command line already, we might as 658 * well stop here. 659 */ 660 if (fw_cfg_cmdline_dev) { 661 /* avoid leaking previously registered device */ 662 platform_device_unregister(fw_cfg_cmdline_dev); 663 return -EINVAL; 664 } 665 666 /* consume "<size>" portion of command line argument */ 667 size = memparse(arg, &str); 668 669 /* get "@<base>[:<ctrl_off>:<data_off>]" chunks */ 670 processed = sscanf(str, PH_ADDR_SCAN_FMT, 671 &base, &consumed, 672 &ctrl_off, &data_off, &consumed); 673 674 /* sscanf() must process precisely 1 or 3 chunks: 675 * <base> is mandatory, optionally followed by <ctrl_off> 676 * and <data_off>; 677 * there must be no extra characters after the last chunk, 678 * so str[consumed] must be '\0'. 679 */ 680 if (str[consumed] || 681 (processed != 1 && processed != 3)) 682 return -EINVAL; 683 684 res[0].start = base; 685 res[0].end = base + size - 1; 686 res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM : 687 IORESOURCE_IO; 688 689 /* insert register offsets, if provided */ 690 if (processed > 1) { 691 res[1].name = "ctrl"; 692 res[1].start = ctrl_off; 693 res[1].flags = IORESOURCE_REG; 694 res[2].name = "data"; 695 res[2].start = data_off; 696 res[2].flags = IORESOURCE_REG; 697 } 698 699 /* "processed" happens to nicely match the number of resources 700 * we need to pass in to this platform device. 701 */ 702 fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg", 703 PLATFORM_DEVID_NONE, res, processed); 704 705 return PTR_ERR_OR_ZERO(fw_cfg_cmdline_dev); 706 } 707 708 static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp) 709 { 710 /* stay silent if device was not configured via the command 711 * line, or if the parameter name (ioport/mmio) doesn't match 712 * the device setting 713 */ 714 if (!fw_cfg_cmdline_dev || 715 (!strcmp(kp->name, "mmio") ^ 716 (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM))) 717 return 0; 718 719 switch (fw_cfg_cmdline_dev->num_resources) { 720 case 1: 721 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT, 722 resource_size(&fw_cfg_cmdline_dev->resource[0]), 723 fw_cfg_cmdline_dev->resource[0].start); 724 case 3: 725 return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT, 726 resource_size(&fw_cfg_cmdline_dev->resource[0]), 727 fw_cfg_cmdline_dev->resource[0].start, 728 fw_cfg_cmdline_dev->resource[1].start, 729 fw_cfg_cmdline_dev->resource[2].start); 730 } 731 732 /* Should never get here */ 733 WARN(1, "Unexpected number of resources: %d\n", 734 fw_cfg_cmdline_dev->num_resources); 735 return 0; 736 } 737 738 static const struct kernel_param_ops fw_cfg_cmdline_param_ops = { 739 .set = fw_cfg_cmdline_set, 740 .get = fw_cfg_cmdline_get, 741 }; 742 743 device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR); 744 device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR); 745 746 #endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */ 747 748 static int __init fw_cfg_sysfs_init(void) 749 { 750 int ret; 751 752 /* create /sys/firmware/qemu_fw_cfg/ top level directory */ 753 fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj); 754 if (!fw_cfg_top_ko) 755 return -ENOMEM; 756 757 ret = platform_driver_register(&fw_cfg_sysfs_driver); 758 if (ret) 759 fw_cfg_kobj_cleanup(fw_cfg_top_ko); 760 761 return ret; 762 } 763 764 static void __exit fw_cfg_sysfs_exit(void) 765 { 766 platform_driver_unregister(&fw_cfg_sysfs_driver); 767 768 #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE 769 platform_device_unregister(fw_cfg_cmdline_dev); 770 #endif 771 772 /* clean up /sys/firmware/qemu_fw_cfg/ */ 773 fw_cfg_kobj_cleanup(fw_cfg_top_ko); 774 } 775 776 module_init(fw_cfg_sysfs_init); 777 module_exit(fw_cfg_sysfs_exit); 778