Commit | Line | Data |
---|---|---|
75f3e8e4 GS |
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 | { | |
80 | mutex_lock(&fw_cfg_dev_lock); | |
81 | iowrite16(fw_cfg_sel_endianness(key), fw_cfg_reg_ctrl); | |
82 | while (pos-- > 0) | |
83 | ioread8(fw_cfg_reg_data); | |
84 | ioread8_rep(fw_cfg_reg_data, buf, count); | |
85 | mutex_unlock(&fw_cfg_dev_lock); | |
86 | } | |
87 | ||
88 | /* clean up fw_cfg device i/o */ | |
89 | static void fw_cfg_io_cleanup(void) | |
90 | { | |
91 | if (fw_cfg_is_mmio) { | |
92 | iounmap(fw_cfg_dev_base); | |
93 | release_mem_region(fw_cfg_p_base, fw_cfg_p_size); | |
94 | } else { | |
95 | ioport_unmap(fw_cfg_dev_base); | |
96 | release_region(fw_cfg_p_base, fw_cfg_p_size); | |
97 | } | |
98 | } | |
99 | ||
100 | /* arch-specific ctrl & data register offsets are not available in ACPI, DT */ | |
101 | #if !(defined(FW_CFG_CTRL_OFF) && defined(FW_CTRL_DATA_OFF)) | |
102 | # if (defined(CONFIG_ARM) || defined(CONFIG_ARM64)) | |
103 | # define FW_CFG_CTRL_OFF 0x08 | |
104 | # define FW_CFG_DATA_OFF 0x00 | |
105 | # elif (defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC32)) /* ppc/mac,sun4m */ | |
106 | # define FW_CFG_CTRL_OFF 0x00 | |
107 | # define FW_CFG_DATA_OFF 0x02 | |
108 | # elif (defined(CONFIG_X86) || defined(CONFIG_SPARC64)) /* x86, sun4u */ | |
109 | # define FW_CFG_CTRL_OFF 0x00 | |
110 | # define FW_CFG_DATA_OFF 0x01 | |
111 | # else | |
112 | # warning "QEMU FW_CFG may not be available on this architecture!" | |
113 | # define FW_CFG_CTRL_OFF 0x00 | |
114 | # define FW_CFG_DATA_OFF 0x01 | |
115 | # endif | |
116 | #endif | |
117 | ||
118 | /* initialize fw_cfg device i/o from platform data */ | |
119 | static int fw_cfg_do_platform_probe(struct platform_device *pdev) | |
120 | { | |
121 | char sig[FW_CFG_SIG_SIZE]; | |
122 | struct resource *range, *ctrl, *data; | |
123 | ||
124 | /* acquire i/o range details */ | |
125 | fw_cfg_is_mmio = false; | |
126 | range = platform_get_resource(pdev, IORESOURCE_IO, 0); | |
127 | if (!range) { | |
128 | fw_cfg_is_mmio = true; | |
129 | range = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
130 | if (!range) | |
131 | return -EINVAL; | |
132 | } | |
133 | fw_cfg_p_base = range->start; | |
134 | fw_cfg_p_size = resource_size(range); | |
135 | ||
136 | if (fw_cfg_is_mmio) { | |
137 | if (!request_mem_region(fw_cfg_p_base, | |
138 | fw_cfg_p_size, "fw_cfg_mem")) | |
139 | return -EBUSY; | |
140 | fw_cfg_dev_base = ioremap(fw_cfg_p_base, fw_cfg_p_size); | |
141 | if (!fw_cfg_dev_base) { | |
142 | release_mem_region(fw_cfg_p_base, fw_cfg_p_size); | |
143 | return -EFAULT; | |
144 | } | |
145 | } else { | |
146 | if (!request_region(fw_cfg_p_base, | |
147 | fw_cfg_p_size, "fw_cfg_io")) | |
148 | return -EBUSY; | |
149 | fw_cfg_dev_base = ioport_map(fw_cfg_p_base, fw_cfg_p_size); | |
150 | if (!fw_cfg_dev_base) { | |
151 | release_region(fw_cfg_p_base, fw_cfg_p_size); | |
152 | return -EFAULT; | |
153 | } | |
154 | } | |
155 | ||
156 | /* were custom register offsets provided (e.g. on the command line)? */ | |
157 | ctrl = platform_get_resource_byname(pdev, IORESOURCE_REG, "ctrl"); | |
158 | data = platform_get_resource_byname(pdev, IORESOURCE_REG, "data"); | |
159 | if (ctrl && data) { | |
160 | fw_cfg_reg_ctrl = fw_cfg_dev_base + ctrl->start; | |
161 | fw_cfg_reg_data = fw_cfg_dev_base + data->start; | |
162 | } else { | |
163 | /* use architecture-specific offsets */ | |
164 | fw_cfg_reg_ctrl = fw_cfg_dev_base + FW_CFG_CTRL_OFF; | |
165 | fw_cfg_reg_data = fw_cfg_dev_base + FW_CFG_DATA_OFF; | |
166 | } | |
167 | ||
168 | /* verify fw_cfg device signature */ | |
169 | fw_cfg_read_blob(FW_CFG_SIGNATURE, sig, 0, FW_CFG_SIG_SIZE); | |
170 | if (memcmp(sig, "QEMU", FW_CFG_SIG_SIZE) != 0) { | |
171 | fw_cfg_io_cleanup(); | |
172 | return -ENODEV; | |
173 | } | |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
178 | /* fw_cfg revision attribute, in /sys/firmware/qemu_fw_cfg top-level dir. */ | |
179 | static u32 fw_cfg_rev; | |
180 | ||
181 | static ssize_t fw_cfg_showrev(struct kobject *k, struct attribute *a, char *buf) | |
182 | { | |
183 | return sprintf(buf, "%u\n", fw_cfg_rev); | |
184 | } | |
185 | ||
186 | static const struct { | |
187 | struct attribute attr; | |
188 | ssize_t (*show)(struct kobject *k, struct attribute *a, char *buf); | |
189 | } fw_cfg_rev_attr = { | |
190 | .attr = { .name = "rev", .mode = S_IRUSR }, | |
191 | .show = fw_cfg_showrev, | |
192 | }; | |
193 | ||
194 | /* fw_cfg_sysfs_entry type */ | |
195 | struct fw_cfg_sysfs_entry { | |
196 | struct kobject kobj; | |
197 | struct fw_cfg_file f; | |
198 | struct list_head list; | |
199 | }; | |
200 | ||
201 | /* get fw_cfg_sysfs_entry from kobject member */ | |
202 | static inline struct fw_cfg_sysfs_entry *to_entry(struct kobject *kobj) | |
203 | { | |
204 | return container_of(kobj, struct fw_cfg_sysfs_entry, kobj); | |
205 | } | |
206 | ||
207 | /* fw_cfg_sysfs_attribute type */ | |
208 | struct fw_cfg_sysfs_attribute { | |
209 | struct attribute attr; | |
210 | ssize_t (*show)(struct fw_cfg_sysfs_entry *entry, char *buf); | |
211 | }; | |
212 | ||
213 | /* get fw_cfg_sysfs_attribute from attribute member */ | |
214 | static inline struct fw_cfg_sysfs_attribute *to_attr(struct attribute *attr) | |
215 | { | |
216 | return container_of(attr, struct fw_cfg_sysfs_attribute, attr); | |
217 | } | |
218 | ||
219 | /* global cache of fw_cfg_sysfs_entry objects */ | |
220 | static LIST_HEAD(fw_cfg_entry_cache); | |
221 | ||
222 | /* kobjects removed lazily by kernel, mutual exclusion needed */ | |
223 | static DEFINE_SPINLOCK(fw_cfg_cache_lock); | |
224 | ||
225 | static inline void fw_cfg_sysfs_cache_enlist(struct fw_cfg_sysfs_entry *entry) | |
226 | { | |
227 | spin_lock(&fw_cfg_cache_lock); | |
228 | list_add_tail(&entry->list, &fw_cfg_entry_cache); | |
229 | spin_unlock(&fw_cfg_cache_lock); | |
230 | } | |
231 | ||
232 | static inline void fw_cfg_sysfs_cache_delist(struct fw_cfg_sysfs_entry *entry) | |
233 | { | |
234 | spin_lock(&fw_cfg_cache_lock); | |
235 | list_del(&entry->list); | |
236 | spin_unlock(&fw_cfg_cache_lock); | |
237 | } | |
238 | ||
239 | static void fw_cfg_sysfs_cache_cleanup(void) | |
240 | { | |
241 | struct fw_cfg_sysfs_entry *entry, *next; | |
242 | ||
243 | list_for_each_entry_safe(entry, next, &fw_cfg_entry_cache, list) { | |
244 | /* will end up invoking fw_cfg_sysfs_cache_delist() | |
245 | * via each object's release() method (i.e. destructor) | |
246 | */ | |
247 | kobject_put(&entry->kobj); | |
248 | } | |
249 | } | |
250 | ||
251 | /* default_attrs: per-entry attributes and show methods */ | |
252 | ||
253 | #define FW_CFG_SYSFS_ATTR(_attr) \ | |
254 | struct fw_cfg_sysfs_attribute fw_cfg_sysfs_attr_##_attr = { \ | |
255 | .attr = { .name = __stringify(_attr), .mode = S_IRUSR }, \ | |
256 | .show = fw_cfg_sysfs_show_##_attr, \ | |
257 | } | |
258 | ||
259 | static ssize_t fw_cfg_sysfs_show_size(struct fw_cfg_sysfs_entry *e, char *buf) | |
260 | { | |
261 | return sprintf(buf, "%u\n", e->f.size); | |
262 | } | |
263 | ||
264 | static ssize_t fw_cfg_sysfs_show_key(struct fw_cfg_sysfs_entry *e, char *buf) | |
265 | { | |
266 | return sprintf(buf, "%u\n", e->f.select); | |
267 | } | |
268 | ||
269 | static ssize_t fw_cfg_sysfs_show_name(struct fw_cfg_sysfs_entry *e, char *buf) | |
270 | { | |
271 | return sprintf(buf, "%s\n", e->f.name); | |
272 | } | |
273 | ||
274 | static FW_CFG_SYSFS_ATTR(size); | |
275 | static FW_CFG_SYSFS_ATTR(key); | |
276 | static FW_CFG_SYSFS_ATTR(name); | |
277 | ||
278 | static struct attribute *fw_cfg_sysfs_entry_attrs[] = { | |
279 | &fw_cfg_sysfs_attr_size.attr, | |
280 | &fw_cfg_sysfs_attr_key.attr, | |
281 | &fw_cfg_sysfs_attr_name.attr, | |
282 | NULL, | |
283 | }; | |
284 | ||
285 | /* sysfs_ops: find fw_cfg_[entry, attribute] and call appropriate show method */ | |
286 | static ssize_t fw_cfg_sysfs_attr_show(struct kobject *kobj, struct attribute *a, | |
287 | char *buf) | |
288 | { | |
289 | struct fw_cfg_sysfs_entry *entry = to_entry(kobj); | |
290 | struct fw_cfg_sysfs_attribute *attr = to_attr(a); | |
291 | ||
292 | return attr->show(entry, buf); | |
293 | } | |
294 | ||
295 | static const struct sysfs_ops fw_cfg_sysfs_attr_ops = { | |
296 | .show = fw_cfg_sysfs_attr_show, | |
297 | }; | |
298 | ||
299 | /* release: destructor, to be called via kobject_put() */ | |
300 | static void fw_cfg_sysfs_release_entry(struct kobject *kobj) | |
301 | { | |
302 | struct fw_cfg_sysfs_entry *entry = to_entry(kobj); | |
303 | ||
304 | fw_cfg_sysfs_cache_delist(entry); | |
305 | kfree(entry); | |
306 | } | |
307 | ||
308 | /* kobj_type: ties together all properties required to register an entry */ | |
309 | static struct kobj_type fw_cfg_sysfs_entry_ktype = { | |
310 | .default_attrs = fw_cfg_sysfs_entry_attrs, | |
311 | .sysfs_ops = &fw_cfg_sysfs_attr_ops, | |
312 | .release = fw_cfg_sysfs_release_entry, | |
313 | }; | |
314 | ||
315 | /* raw-read method and attribute */ | |
316 | static ssize_t fw_cfg_sysfs_read_raw(struct file *filp, struct kobject *kobj, | |
317 | struct bin_attribute *bin_attr, | |
318 | char *buf, loff_t pos, size_t count) | |
319 | { | |
320 | struct fw_cfg_sysfs_entry *entry = to_entry(kobj); | |
321 | ||
322 | if (pos > entry->f.size) | |
323 | return -EINVAL; | |
324 | ||
325 | if (count > entry->f.size - pos) | |
326 | count = entry->f.size - pos; | |
327 | ||
328 | fw_cfg_read_blob(entry->f.select, buf, pos, count); | |
329 | return count; | |
330 | } | |
331 | ||
332 | static struct bin_attribute fw_cfg_sysfs_attr_raw = { | |
333 | .attr = { .name = "raw", .mode = S_IRUSR }, | |
334 | .read = fw_cfg_sysfs_read_raw, | |
335 | }; | |
336 | ||
337 | /* kobjects representing top-level and by_key folders */ | |
338 | static struct kobject *fw_cfg_top_ko; | |
339 | static struct kobject *fw_cfg_sel_ko; | |
340 | ||
341 | /* register an individual fw_cfg file */ | |
342 | static int fw_cfg_register_file(const struct fw_cfg_file *f) | |
343 | { | |
344 | int err; | |
345 | struct fw_cfg_sysfs_entry *entry; | |
346 | ||
347 | /* allocate new entry */ | |
348 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); | |
349 | if (!entry) | |
350 | return -ENOMEM; | |
351 | ||
352 | /* set file entry information */ | |
353 | memcpy(&entry->f, f, sizeof(struct fw_cfg_file)); | |
354 | ||
355 | /* register entry under "/sys/firmware/qemu_fw_cfg/by_key/" */ | |
356 | err = kobject_init_and_add(&entry->kobj, &fw_cfg_sysfs_entry_ktype, | |
357 | fw_cfg_sel_ko, "%d", entry->f.select); | |
358 | if (err) | |
359 | goto err_register; | |
360 | ||
361 | /* add raw binary content access */ | |
362 | err = sysfs_create_bin_file(&entry->kobj, &fw_cfg_sysfs_attr_raw); | |
363 | if (err) | |
364 | goto err_add_raw; | |
365 | ||
366 | /* success, add entry to global cache */ | |
367 | fw_cfg_sysfs_cache_enlist(entry); | |
368 | return 0; | |
369 | ||
370 | err_add_raw: | |
371 | kobject_del(&entry->kobj); | |
372 | err_register: | |
373 | kfree(entry); | |
374 | return err; | |
375 | } | |
376 | ||
377 | /* iterate over all fw_cfg directory entries, registering each one */ | |
378 | static int fw_cfg_register_dir_entries(void) | |
379 | { | |
380 | int ret = 0; | |
381 | u32 count, i; | |
382 | struct fw_cfg_file *dir; | |
383 | size_t dir_size; | |
384 | ||
385 | fw_cfg_read_blob(FW_CFG_FILE_DIR, &count, 0, sizeof(count)); | |
386 | count = be32_to_cpu(count); | |
387 | dir_size = count * sizeof(struct fw_cfg_file); | |
388 | ||
389 | dir = kmalloc(dir_size, GFP_KERNEL); | |
390 | if (!dir) | |
391 | return -ENOMEM; | |
392 | ||
393 | fw_cfg_read_blob(FW_CFG_FILE_DIR, dir, sizeof(count), dir_size); | |
394 | ||
395 | for (i = 0; i < count; i++) { | |
396 | dir[i].size = be32_to_cpu(dir[i].size); | |
397 | dir[i].select = be16_to_cpu(dir[i].select); | |
398 | ret = fw_cfg_register_file(&dir[i]); | |
399 | if (ret) | |
400 | break; | |
401 | } | |
402 | ||
403 | kfree(dir); | |
404 | return ret; | |
405 | } | |
406 | ||
407 | /* unregister top-level or by_key folder */ | |
408 | static inline void fw_cfg_kobj_cleanup(struct kobject *kobj) | |
409 | { | |
410 | kobject_del(kobj); | |
411 | kobject_put(kobj); | |
412 | } | |
413 | ||
414 | static int fw_cfg_sysfs_probe(struct platform_device *pdev) | |
415 | { | |
416 | int err; | |
417 | ||
418 | /* NOTE: If we supported multiple fw_cfg devices, we'd first create | |
419 | * a subdirectory named after e.g. pdev->id, then hang per-device | |
420 | * by_key subdirectories underneath it. However, only | |
421 | * one fw_cfg device exist system-wide, so if one was already found | |
422 | * earlier, we might as well stop here. | |
423 | */ | |
424 | if (fw_cfg_sel_ko) | |
425 | return -EBUSY; | |
426 | ||
427 | /* create by_key subdirectory of /sys/firmware/qemu_fw_cfg/ */ | |
428 | err = -ENOMEM; | |
429 | fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko); | |
430 | if (!fw_cfg_sel_ko) | |
431 | goto err_sel; | |
432 | ||
433 | /* initialize fw_cfg device i/o from platform data */ | |
434 | err = fw_cfg_do_platform_probe(pdev); | |
435 | if (err) | |
436 | goto err_probe; | |
437 | ||
438 | /* get revision number, add matching top-level attribute */ | |
439 | fw_cfg_read_blob(FW_CFG_ID, &fw_cfg_rev, 0, sizeof(fw_cfg_rev)); | |
440 | fw_cfg_rev = le32_to_cpu(fw_cfg_rev); | |
441 | err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr); | |
442 | if (err) | |
443 | goto err_rev; | |
444 | ||
445 | /* process fw_cfg file directory entry, registering each file */ | |
446 | err = fw_cfg_register_dir_entries(); | |
447 | if (err) | |
448 | goto err_dir; | |
449 | ||
450 | /* success */ | |
451 | pr_debug("fw_cfg: loaded.\n"); | |
452 | return 0; | |
453 | ||
454 | err_dir: | |
455 | fw_cfg_sysfs_cache_cleanup(); | |
456 | sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr); | |
457 | err_rev: | |
458 | fw_cfg_io_cleanup(); | |
459 | err_probe: | |
460 | fw_cfg_kobj_cleanup(fw_cfg_sel_ko); | |
461 | err_sel: | |
462 | return err; | |
463 | } | |
464 | ||
465 | static int fw_cfg_sysfs_remove(struct platform_device *pdev) | |
466 | { | |
467 | pr_debug("fw_cfg: unloading.\n"); | |
468 | fw_cfg_sysfs_cache_cleanup(); | |
469 | fw_cfg_kobj_cleanup(fw_cfg_sel_ko); | |
470 | fw_cfg_io_cleanup(); | |
471 | return 0; | |
472 | } | |
473 | ||
474 | static const struct of_device_id fw_cfg_sysfs_mmio_match[] = { | |
475 | { .compatible = "qemu,fw-cfg-mmio", }, | |
476 | {}, | |
477 | }; | |
478 | MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match); | |
479 | ||
480 | #ifdef CONFIG_ACPI | |
481 | static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = { | |
482 | { "QEMU0002", }, | |
483 | {}, | |
484 | }; | |
485 | MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match); | |
486 | #endif | |
487 | ||
488 | static struct platform_driver fw_cfg_sysfs_driver = { | |
489 | .probe = fw_cfg_sysfs_probe, | |
490 | .remove = fw_cfg_sysfs_remove, | |
491 | .driver = { | |
492 | .name = "fw_cfg", | |
493 | .of_match_table = fw_cfg_sysfs_mmio_match, | |
494 | .acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match), | |
495 | }, | |
496 | }; | |
497 | ||
498 | #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE | |
499 | ||
500 | static struct platform_device *fw_cfg_cmdline_dev; | |
501 | ||
502 | /* this probably belongs in e.g. include/linux/types.h, | |
503 | * but right now we are the only ones doing it... | |
504 | */ | |
505 | #ifdef CONFIG_PHYS_ADDR_T_64BIT | |
506 | #define __PHYS_ADDR_PREFIX "ll" | |
507 | #else | |
508 | #define __PHYS_ADDR_PREFIX "" | |
509 | #endif | |
510 | ||
511 | /* use special scanf/printf modifier for phys_addr_t, resource_size_t */ | |
512 | #define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \ | |
513 | ":%" __PHYS_ADDR_PREFIX "i" \ | |
514 | ":%" __PHYS_ADDR_PREFIX "i%n" | |
515 | ||
516 | #define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \ | |
517 | "0x%" __PHYS_ADDR_PREFIX "x" | |
518 | ||
519 | #define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \ | |
520 | ":%" __PHYS_ADDR_PREFIX "u" \ | |
521 | ":%" __PHYS_ADDR_PREFIX "u" | |
522 | ||
523 | static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp) | |
524 | { | |
525 | struct resource res[3] = {}; | |
526 | char *str; | |
527 | phys_addr_t base; | |
528 | resource_size_t size, ctrl_off, data_off; | |
529 | int processed, consumed = 0; | |
530 | ||
531 | /* only one fw_cfg device can exist system-wide, so if one | |
532 | * was processed on the command line already, we might as | |
533 | * well stop here. | |
534 | */ | |
535 | if (fw_cfg_cmdline_dev) { | |
536 | /* avoid leaking previously registered device */ | |
537 | platform_device_unregister(fw_cfg_cmdline_dev); | |
538 | return -EINVAL; | |
539 | } | |
540 | ||
541 | /* consume "<size>" portion of command line argument */ | |
542 | size = memparse(arg, &str); | |
543 | ||
544 | /* get "@<base>[:<ctrl_off>:<data_off>]" chunks */ | |
545 | processed = sscanf(str, PH_ADDR_SCAN_FMT, | |
546 | &base, &consumed, | |
547 | &ctrl_off, &data_off, &consumed); | |
548 | ||
549 | /* sscanf() must process precisely 1 or 3 chunks: | |
550 | * <base> is mandatory, optionally followed by <ctrl_off> | |
551 | * and <data_off>; | |
552 | * there must be no extra characters after the last chunk, | |
553 | * so str[consumed] must be '\0'. | |
554 | */ | |
555 | if (str[consumed] || | |
556 | (processed != 1 && processed != 3)) | |
557 | return -EINVAL; | |
558 | ||
559 | res[0].start = base; | |
560 | res[0].end = base + size - 1; | |
561 | res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM : | |
562 | IORESOURCE_IO; | |
563 | ||
564 | /* insert register offsets, if provided */ | |
565 | if (processed > 1) { | |
566 | res[1].name = "ctrl"; | |
567 | res[1].start = ctrl_off; | |
568 | res[1].flags = IORESOURCE_REG; | |
569 | res[2].name = "data"; | |
570 | res[2].start = data_off; | |
571 | res[2].flags = IORESOURCE_REG; | |
572 | } | |
573 | ||
574 | /* "processed" happens to nicely match the number of resources | |
575 | * we need to pass in to this platform device. | |
576 | */ | |
577 | fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg", | |
578 | PLATFORM_DEVID_NONE, res, processed); | |
579 | if (IS_ERR(fw_cfg_cmdline_dev)) | |
580 | return PTR_ERR(fw_cfg_cmdline_dev); | |
581 | ||
582 | return 0; | |
583 | } | |
584 | ||
585 | static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp) | |
586 | { | |
587 | /* stay silent if device was not configured via the command | |
588 | * line, or if the parameter name (ioport/mmio) doesn't match | |
589 | * the device setting | |
590 | */ | |
591 | if (!fw_cfg_cmdline_dev || | |
592 | (!strcmp(kp->name, "mmio") ^ | |
593 | (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM))) | |
594 | return 0; | |
595 | ||
596 | switch (fw_cfg_cmdline_dev->num_resources) { | |
597 | case 1: | |
598 | return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT, | |
599 | resource_size(&fw_cfg_cmdline_dev->resource[0]), | |
600 | fw_cfg_cmdline_dev->resource[0].start); | |
601 | case 3: | |
602 | return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT, | |
603 | resource_size(&fw_cfg_cmdline_dev->resource[0]), | |
604 | fw_cfg_cmdline_dev->resource[0].start, | |
605 | fw_cfg_cmdline_dev->resource[1].start, | |
606 | fw_cfg_cmdline_dev->resource[2].start); | |
607 | } | |
608 | ||
609 | /* Should never get here */ | |
610 | WARN(1, "Unexpected number of resources: %d\n", | |
611 | fw_cfg_cmdline_dev->num_resources); | |
612 | return 0; | |
613 | } | |
614 | ||
615 | static const struct kernel_param_ops fw_cfg_cmdline_param_ops = { | |
616 | .set = fw_cfg_cmdline_set, | |
617 | .get = fw_cfg_cmdline_get, | |
618 | }; | |
619 | ||
620 | device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR); | |
621 | device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR); | |
622 | ||
623 | #endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */ | |
624 | ||
625 | static int __init fw_cfg_sysfs_init(void) | |
626 | { | |
627 | /* create /sys/firmware/qemu_fw_cfg/ top level directory */ | |
628 | fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj); | |
629 | if (!fw_cfg_top_ko) | |
630 | return -ENOMEM; | |
631 | ||
632 | return platform_driver_register(&fw_cfg_sysfs_driver); | |
633 | } | |
634 | ||
635 | static void __exit fw_cfg_sysfs_exit(void) | |
636 | { | |
637 | platform_driver_unregister(&fw_cfg_sysfs_driver); | |
638 | ||
639 | #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE | |
640 | platform_device_unregister(fw_cfg_cmdline_dev); | |
641 | #endif | |
642 | ||
643 | /* clean up /sys/firmware/qemu_fw_cfg/ */ | |
644 | fw_cfg_kobj_cleanup(fw_cfg_top_ko); | |
645 | } | |
646 | ||
647 | module_init(fw_cfg_sysfs_init); | |
648 | module_exit(fw_cfg_sysfs_exit); |