qemu_fw_cfg.c 22 KB

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