qemu_fw_cfg.c 21 KB

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