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 | * | |
c1d0c3f6 | 13 | * [qemu_fw_cfg.]ioport=<size>@<base>[:<ctrl_off>:<data_off>] |
75f3e8e4 | 14 | * or |
c1d0c3f6 | 15 | * [qemu_fw_cfg.]mmio=<size>@<base>[:<ctrl_off>:<data_off>] |
75f3e8e4 GS |
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.: | |
c1d0c3f6 | 24 | * qemu_fw_cfg.ioport=2@0x510:0:1 (the default on x86) |
75f3e8e4 | 25 | * or |
c1d0c3f6 | 26 | * qemu_fw_cfg.mmio=0xA@0x9020000:8:0 (the default on arm) |
75f3e8e4 GS |
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 */ | |
8d59d5bd | 71 | static void fw_cfg_sel_endianness(u16 key) |
75f3e8e4 | 72 | { |
8d59d5bd MAL |
73 | if (fw_cfg_is_mmio) |
74 | iowrite16be(key, fw_cfg_reg_ctrl); | |
75 | else | |
76 | iowrite16(key, fw_cfg_reg_ctrl); | |
75f3e8e4 GS |
77 | } |
78 | ||
79 | /* read chunk of given fw_cfg blob (caller responsible for sanity-check) */ | |
80 | static inline void fw_cfg_read_blob(u16 key, | |
81 | void *buf, loff_t pos, size_t count) | |
82 | { | |
d4f6e272 | 83 | u32 glk = -1U; |
def7ac80 GS |
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 | ||
75f3e8e4 | 97 | mutex_lock(&fw_cfg_dev_lock); |
8d59d5bd | 98 | fw_cfg_sel_endianness(key); |
75f3e8e4 GS |
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); | |
def7ac80 GS |
103 | |
104 | acpi_release_global_lock(glk); | |
75f3e8e4 GS |
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 */ | |
9b3ec23a | 120 | #if !(defined(FW_CFG_CTRL_OFF) && defined(FW_CFG_DATA_OFF)) |
75f3e8e4 GS |
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 | |
00411b7b | 131 | # error "QEMU FW_CFG not available on this architecture!" |
75f3e8e4 GS |
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; | |
d174ea7d MAL |
214 | u32 size; |
215 | u16 select; | |
216 | char name[FW_CFG_MAX_FILE_PATH]; | |
75f3e8e4 GS |
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 | { | |
d174ea7d | 280 | return sprintf(buf, "%u\n", e->size); |
75f3e8e4 GS |
281 | } |
282 | ||
283 | static ssize_t fw_cfg_sysfs_show_key(struct fw_cfg_sysfs_entry *e, char *buf) | |
284 | { | |
d174ea7d | 285 | return sprintf(buf, "%u\n", e->select); |
75f3e8e4 GS |
286 | } |
287 | ||
288 | static ssize_t fw_cfg_sysfs_show_name(struct fw_cfg_sysfs_entry *e, char *buf) | |
289 | { | |
d174ea7d | 290 | return sprintf(buf, "%s\n", e->name); |
75f3e8e4 GS |
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 | ||
d174ea7d | 341 | if (pos > entry->size) |
75f3e8e4 GS |
342 | return -EINVAL; |
343 | ||
d174ea7d MAL |
344 | if (count > entry->size - pos) |
345 | count = entry->size - pos; | |
75f3e8e4 | 346 | |
d174ea7d | 347 | fw_cfg_read_blob(entry->select, buf, pos, count); |
75f3e8e4 GS |
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 | ||
246c46eb GS |
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 */ | |
75f3e8e4 GS |
450 | static struct kobject *fw_cfg_top_ko; |
451 | static struct kobject *fw_cfg_sel_ko; | |
246c46eb | 452 | static struct kset *fw_cfg_fname_kset; |
75f3e8e4 GS |
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 */ | |
d174ea7d MAL |
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); | |
75f3e8e4 GS |
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, | |
d174ea7d | 472 | fw_cfg_sel_ko, "%d", entry->select); |
75f3e8e4 GS |
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 | ||
246c46eb | 481 | /* try adding "/sys/firmware/qemu_fw_cfg/by_name/" symlink */ |
d174ea7d | 482 | fw_cfg_build_symlink(fw_cfg_fname_kset, &entry->kobj, entry->name); |
246c46eb | 483 | |
75f3e8e4 GS |
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 | u32 count, i; | |
500 | struct fw_cfg_file *dir; | |
501 | size_t dir_size; | |
502 | ||
503 | fw_cfg_read_blob(FW_CFG_FILE_DIR, &count, 0, sizeof(count)); | |
504 | count = be32_to_cpu(count); | |
505 | dir_size = count * sizeof(struct fw_cfg_file); | |
506 | ||
507 | dir = kmalloc(dir_size, GFP_KERNEL); | |
508 | if (!dir) | |
509 | return -ENOMEM; | |
510 | ||
511 | fw_cfg_read_blob(FW_CFG_FILE_DIR, dir, sizeof(count), dir_size); | |
512 | ||
513 | for (i = 0; i < count; i++) { | |
75f3e8e4 GS |
514 | ret = fw_cfg_register_file(&dir[i]); |
515 | if (ret) | |
516 | break; | |
517 | } | |
518 | ||
519 | kfree(dir); | |
520 | return ret; | |
521 | } | |
522 | ||
523 | /* unregister top-level or by_key folder */ | |
524 | static inline void fw_cfg_kobj_cleanup(struct kobject *kobj) | |
525 | { | |
526 | kobject_del(kobj); | |
527 | kobject_put(kobj); | |
528 | } | |
529 | ||
530 | static int fw_cfg_sysfs_probe(struct platform_device *pdev) | |
531 | { | |
532 | int err; | |
533 | ||
534 | /* NOTE: If we supported multiple fw_cfg devices, we'd first create | |
535 | * a subdirectory named after e.g. pdev->id, then hang per-device | |
246c46eb | 536 | * by_key (and by_name) subdirectories underneath it. However, only |
75f3e8e4 GS |
537 | * one fw_cfg device exist system-wide, so if one was already found |
538 | * earlier, we might as well stop here. | |
539 | */ | |
540 | if (fw_cfg_sel_ko) | |
541 | return -EBUSY; | |
542 | ||
246c46eb | 543 | /* create by_key and by_name subdirs of /sys/firmware/qemu_fw_cfg/ */ |
75f3e8e4 GS |
544 | err = -ENOMEM; |
545 | fw_cfg_sel_ko = kobject_create_and_add("by_key", fw_cfg_top_ko); | |
546 | if (!fw_cfg_sel_ko) | |
547 | goto err_sel; | |
246c46eb GS |
548 | fw_cfg_fname_kset = kset_create_and_add("by_name", NULL, fw_cfg_top_ko); |
549 | if (!fw_cfg_fname_kset) | |
550 | goto err_name; | |
75f3e8e4 GS |
551 | |
552 | /* initialize fw_cfg device i/o from platform data */ | |
553 | err = fw_cfg_do_platform_probe(pdev); | |
554 | if (err) | |
555 | goto err_probe; | |
556 | ||
557 | /* get revision number, add matching top-level attribute */ | |
558 | fw_cfg_read_blob(FW_CFG_ID, &fw_cfg_rev, 0, sizeof(fw_cfg_rev)); | |
559 | fw_cfg_rev = le32_to_cpu(fw_cfg_rev); | |
560 | err = sysfs_create_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr); | |
561 | if (err) | |
562 | goto err_rev; | |
563 | ||
564 | /* process fw_cfg file directory entry, registering each file */ | |
565 | err = fw_cfg_register_dir_entries(); | |
566 | if (err) | |
567 | goto err_dir; | |
568 | ||
569 | /* success */ | |
570 | pr_debug("fw_cfg: loaded.\n"); | |
571 | return 0; | |
572 | ||
573 | err_dir: | |
574 | fw_cfg_sysfs_cache_cleanup(); | |
575 | sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr); | |
576 | err_rev: | |
577 | fw_cfg_io_cleanup(); | |
578 | err_probe: | |
246c46eb GS |
579 | fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset); |
580 | err_name: | |
75f3e8e4 GS |
581 | fw_cfg_kobj_cleanup(fw_cfg_sel_ko); |
582 | err_sel: | |
583 | return err; | |
584 | } | |
585 | ||
586 | static int fw_cfg_sysfs_remove(struct platform_device *pdev) | |
587 | { | |
588 | pr_debug("fw_cfg: unloading.\n"); | |
589 | fw_cfg_sysfs_cache_cleanup(); | |
23f1b8d9 MAL |
590 | sysfs_remove_file(fw_cfg_top_ko, &fw_cfg_rev_attr.attr); |
591 | fw_cfg_io_cleanup(); | |
246c46eb | 592 | fw_cfg_kset_unregister_recursive(fw_cfg_fname_kset); |
75f3e8e4 | 593 | fw_cfg_kobj_cleanup(fw_cfg_sel_ko); |
75f3e8e4 GS |
594 | return 0; |
595 | } | |
596 | ||
597 | static const struct of_device_id fw_cfg_sysfs_mmio_match[] = { | |
598 | { .compatible = "qemu,fw-cfg-mmio", }, | |
599 | {}, | |
600 | }; | |
601 | MODULE_DEVICE_TABLE(of, fw_cfg_sysfs_mmio_match); | |
602 | ||
603 | #ifdef CONFIG_ACPI | |
604 | static const struct acpi_device_id fw_cfg_sysfs_acpi_match[] = { | |
605 | { "QEMU0002", }, | |
606 | {}, | |
607 | }; | |
608 | MODULE_DEVICE_TABLE(acpi, fw_cfg_sysfs_acpi_match); | |
609 | #endif | |
610 | ||
611 | static struct platform_driver fw_cfg_sysfs_driver = { | |
612 | .probe = fw_cfg_sysfs_probe, | |
613 | .remove = fw_cfg_sysfs_remove, | |
614 | .driver = { | |
615 | .name = "fw_cfg", | |
616 | .of_match_table = fw_cfg_sysfs_mmio_match, | |
617 | .acpi_match_table = ACPI_PTR(fw_cfg_sysfs_acpi_match), | |
618 | }, | |
619 | }; | |
620 | ||
621 | #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE | |
622 | ||
623 | static struct platform_device *fw_cfg_cmdline_dev; | |
624 | ||
625 | /* this probably belongs in e.g. include/linux/types.h, | |
626 | * but right now we are the only ones doing it... | |
627 | */ | |
628 | #ifdef CONFIG_PHYS_ADDR_T_64BIT | |
629 | #define __PHYS_ADDR_PREFIX "ll" | |
630 | #else | |
631 | #define __PHYS_ADDR_PREFIX "" | |
632 | #endif | |
633 | ||
634 | /* use special scanf/printf modifier for phys_addr_t, resource_size_t */ | |
635 | #define PH_ADDR_SCAN_FMT "@%" __PHYS_ADDR_PREFIX "i%n" \ | |
636 | ":%" __PHYS_ADDR_PREFIX "i" \ | |
637 | ":%" __PHYS_ADDR_PREFIX "i%n" | |
638 | ||
639 | #define PH_ADDR_PR_1_FMT "0x%" __PHYS_ADDR_PREFIX "x@" \ | |
640 | "0x%" __PHYS_ADDR_PREFIX "x" | |
641 | ||
642 | #define PH_ADDR_PR_3_FMT PH_ADDR_PR_1_FMT \ | |
643 | ":%" __PHYS_ADDR_PREFIX "u" \ | |
644 | ":%" __PHYS_ADDR_PREFIX "u" | |
645 | ||
646 | static int fw_cfg_cmdline_set(const char *arg, const struct kernel_param *kp) | |
647 | { | |
648 | struct resource res[3] = {}; | |
649 | char *str; | |
650 | phys_addr_t base; | |
651 | resource_size_t size, ctrl_off, data_off; | |
652 | int processed, consumed = 0; | |
653 | ||
654 | /* only one fw_cfg device can exist system-wide, so if one | |
655 | * was processed on the command line already, we might as | |
656 | * well stop here. | |
657 | */ | |
658 | if (fw_cfg_cmdline_dev) { | |
659 | /* avoid leaking previously registered device */ | |
660 | platform_device_unregister(fw_cfg_cmdline_dev); | |
661 | return -EINVAL; | |
662 | } | |
663 | ||
664 | /* consume "<size>" portion of command line argument */ | |
665 | size = memparse(arg, &str); | |
666 | ||
667 | /* get "@<base>[:<ctrl_off>:<data_off>]" chunks */ | |
668 | processed = sscanf(str, PH_ADDR_SCAN_FMT, | |
669 | &base, &consumed, | |
670 | &ctrl_off, &data_off, &consumed); | |
671 | ||
672 | /* sscanf() must process precisely 1 or 3 chunks: | |
673 | * <base> is mandatory, optionally followed by <ctrl_off> | |
674 | * and <data_off>; | |
675 | * there must be no extra characters after the last chunk, | |
676 | * so str[consumed] must be '\0'. | |
677 | */ | |
678 | if (str[consumed] || | |
679 | (processed != 1 && processed != 3)) | |
680 | return -EINVAL; | |
681 | ||
682 | res[0].start = base; | |
683 | res[0].end = base + size - 1; | |
684 | res[0].flags = !strcmp(kp->name, "mmio") ? IORESOURCE_MEM : | |
685 | IORESOURCE_IO; | |
686 | ||
687 | /* insert register offsets, if provided */ | |
688 | if (processed > 1) { | |
689 | res[1].name = "ctrl"; | |
690 | res[1].start = ctrl_off; | |
691 | res[1].flags = IORESOURCE_REG; | |
692 | res[2].name = "data"; | |
693 | res[2].start = data_off; | |
694 | res[2].flags = IORESOURCE_REG; | |
695 | } | |
696 | ||
697 | /* "processed" happens to nicely match the number of resources | |
698 | * we need to pass in to this platform device. | |
699 | */ | |
700 | fw_cfg_cmdline_dev = platform_device_register_simple("fw_cfg", | |
701 | PLATFORM_DEVID_NONE, res, processed); | |
75f3e8e4 | 702 | |
0a9e63aa | 703 | return PTR_ERR_OR_ZERO(fw_cfg_cmdline_dev); |
75f3e8e4 GS |
704 | } |
705 | ||
706 | static int fw_cfg_cmdline_get(char *buf, const struct kernel_param *kp) | |
707 | { | |
708 | /* stay silent if device was not configured via the command | |
709 | * line, or if the parameter name (ioport/mmio) doesn't match | |
710 | * the device setting | |
711 | */ | |
712 | if (!fw_cfg_cmdline_dev || | |
713 | (!strcmp(kp->name, "mmio") ^ | |
714 | (fw_cfg_cmdline_dev->resource[0].flags == IORESOURCE_MEM))) | |
715 | return 0; | |
716 | ||
717 | switch (fw_cfg_cmdline_dev->num_resources) { | |
718 | case 1: | |
719 | return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_1_FMT, | |
720 | resource_size(&fw_cfg_cmdline_dev->resource[0]), | |
721 | fw_cfg_cmdline_dev->resource[0].start); | |
722 | case 3: | |
723 | return snprintf(buf, PAGE_SIZE, PH_ADDR_PR_3_FMT, | |
724 | resource_size(&fw_cfg_cmdline_dev->resource[0]), | |
725 | fw_cfg_cmdline_dev->resource[0].start, | |
726 | fw_cfg_cmdline_dev->resource[1].start, | |
727 | fw_cfg_cmdline_dev->resource[2].start); | |
728 | } | |
729 | ||
730 | /* Should never get here */ | |
731 | WARN(1, "Unexpected number of resources: %d\n", | |
732 | fw_cfg_cmdline_dev->num_resources); | |
733 | return 0; | |
734 | } | |
735 | ||
736 | static const struct kernel_param_ops fw_cfg_cmdline_param_ops = { | |
737 | .set = fw_cfg_cmdline_set, | |
738 | .get = fw_cfg_cmdline_get, | |
739 | }; | |
740 | ||
741 | device_param_cb(ioport, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR); | |
742 | device_param_cb(mmio, &fw_cfg_cmdline_param_ops, NULL, S_IRUSR); | |
743 | ||
744 | #endif /* CONFIG_FW_CFG_SYSFS_CMDLINE */ | |
745 | ||
746 | static int __init fw_cfg_sysfs_init(void) | |
747 | { | |
e8aabc64 MT |
748 | int ret; |
749 | ||
75f3e8e4 GS |
750 | /* create /sys/firmware/qemu_fw_cfg/ top level directory */ |
751 | fw_cfg_top_ko = kobject_create_and_add("qemu_fw_cfg", firmware_kobj); | |
752 | if (!fw_cfg_top_ko) | |
753 | return -ENOMEM; | |
754 | ||
e8aabc64 MT |
755 | ret = platform_driver_register(&fw_cfg_sysfs_driver); |
756 | if (ret) | |
757 | fw_cfg_kobj_cleanup(fw_cfg_top_ko); | |
758 | ||
759 | return ret; | |
75f3e8e4 GS |
760 | } |
761 | ||
762 | static void __exit fw_cfg_sysfs_exit(void) | |
763 | { | |
764 | platform_driver_unregister(&fw_cfg_sysfs_driver); | |
765 | ||
766 | #ifdef CONFIG_FW_CFG_SYSFS_CMDLINE | |
767 | platform_device_unregister(fw_cfg_cmdline_dev); | |
768 | #endif | |
769 | ||
770 | /* clean up /sys/firmware/qemu_fw_cfg/ */ | |
771 | fw_cfg_kobj_cleanup(fw_cfg_top_ko); | |
772 | } | |
773 | ||
774 | module_init(fw_cfg_sysfs_init); | |
775 | module_exit(fw_cfg_sysfs_exit); |