qemu_fw_cfg.c 21 KB

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