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