qemu_fw_cfg.c 25 KB

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