qemu_fw_cfg.c 25 KB

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