qemu_fw_cfg.c 21 KB

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