qemu_fw_cfg.c 21 KB

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