qemu_fw_cfg.c 25 KB

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