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