virtio_mmio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * Virtio memory mapped device driver
  3. *
  4. * Copyright 2011-2014, ARM Ltd.
  5. *
  6. * This module allows virtio devices to be used over a virtual, memory mapped
  7. * platform device.
  8. *
  9. * The guest device(s) may be instantiated in one of three equivalent ways:
  10. *
  11. * 1. Static platform device in board's code, eg.:
  12. *
  13. * static struct platform_device v2m_virtio_device = {
  14. * .name = "virtio-mmio",
  15. * .id = -1,
  16. * .num_resources = 2,
  17. * .resource = (struct resource []) {
  18. * {
  19. * .start = 0x1001e000,
  20. * .end = 0x1001e0ff,
  21. * .flags = IORESOURCE_MEM,
  22. * }, {
  23. * .start = 42 + 32,
  24. * .end = 42 + 32,
  25. * .flags = IORESOURCE_IRQ,
  26. * },
  27. * }
  28. * };
  29. *
  30. * 2. Device Tree node, eg.:
  31. *
  32. * virtio_block@1e000 {
  33. * compatible = "virtio,mmio";
  34. * reg = <0x1e000 0x100>;
  35. * interrupts = <42>;
  36. * }
  37. *
  38. * 3. Kernel module (or command line) parameter. Can be used more than once -
  39. * one device will be created for each one. Syntax:
  40. *
  41. * [virtio_mmio.]device=<size>@<baseaddr>:<irq>[:<id>]
  42. * where:
  43. * <size> := size (can use standard suffixes like K, M or G)
  44. * <baseaddr> := physical base address
  45. * <irq> := interrupt number (as passed to request_irq())
  46. * <id> := (optional) platform device id
  47. * eg.:
  48. * virtio_mmio.device=0x100@0x100b0000:48 \
  49. * virtio_mmio.device=1K@0x1001e000:74
  50. *
  51. *
  52. *
  53. * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007
  54. *
  55. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  56. * See the COPYING file in the top-level directory.
  57. */
  58. #define pr_fmt(fmt) "virtio-mmio: " fmt
  59. #include <linux/acpi.h>
  60. #include <linux/highmem.h>
  61. #include <linux/interrupt.h>
  62. #include <linux/io.h>
  63. #include <linux/list.h>
  64. #include <linux/module.h>
  65. #include <linux/platform_device.h>
  66. #include <linux/slab.h>
  67. #include <linux/spinlock.h>
  68. #include <linux/virtio.h>
  69. #include <linux/virtio_config.h>
  70. #include <linux/virtio_mmio.h>
  71. #include <linux/virtio_ring.h>
  72. /* The alignment to use between consumer and producer parts of vring.
  73. * Currently hardcoded to the page size. */
  74. #define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE
  75. #define to_virtio_mmio_device(_plat_dev) \
  76. container_of(_plat_dev, struct virtio_mmio_device, vdev)
  77. struct virtio_mmio_device {
  78. struct virtio_device vdev;
  79. struct platform_device *pdev;
  80. void __iomem *base;
  81. unsigned long version;
  82. /* a list of queues so we can dispatch IRQs */
  83. spinlock_t lock;
  84. struct list_head virtqueues;
  85. };
  86. struct virtio_mmio_vq_info {
  87. /* the actual virtqueue */
  88. struct virtqueue *vq;
  89. /* the list node for the virtqueues list */
  90. struct list_head node;
  91. };
  92. /* Configuration interface */
  93. static u64 vm_get_features(struct virtio_device *vdev)
  94. {
  95. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  96. u64 features;
  97. writel(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
  98. features = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
  99. features <<= 32;
  100. writel(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
  101. features |= readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
  102. return features;
  103. }
  104. static int vm_finalize_features(struct virtio_device *vdev)
  105. {
  106. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  107. /* Give virtio_ring a chance to accept features. */
  108. vring_transport_features(vdev);
  109. /* Make sure there is are no mixed devices */
  110. if (vm_dev->version == 2 &&
  111. !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  112. dev_err(&vdev->dev, "New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n");
  113. return -EINVAL;
  114. }
  115. writel(1, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
  116. writel((u32)(vdev->features >> 32),
  117. vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
  118. writel(0, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
  119. writel((u32)vdev->features,
  120. vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
  121. return 0;
  122. }
  123. static void vm_get(struct virtio_device *vdev, unsigned offset,
  124. void *buf, unsigned len)
  125. {
  126. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  127. void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
  128. u8 b;
  129. __le16 w;
  130. __le32 l;
  131. if (vm_dev->version == 1) {
  132. u8 *ptr = buf;
  133. int i;
  134. for (i = 0; i < len; i++)
  135. ptr[i] = readb(base + offset + i);
  136. return;
  137. }
  138. switch (len) {
  139. case 1:
  140. b = readb(base + offset);
  141. memcpy(buf, &b, sizeof b);
  142. break;
  143. case 2:
  144. w = cpu_to_le16(readw(base + offset));
  145. memcpy(buf, &w, sizeof w);
  146. break;
  147. case 4:
  148. l = cpu_to_le32(readl(base + offset));
  149. memcpy(buf, &l, sizeof l);
  150. break;
  151. case 8:
  152. l = cpu_to_le32(readl(base + offset));
  153. memcpy(buf, &l, sizeof l);
  154. l = cpu_to_le32(ioread32(base + offset + sizeof l));
  155. memcpy(buf + sizeof l, &l, sizeof l);
  156. break;
  157. default:
  158. BUG();
  159. }
  160. }
  161. static void vm_set(struct virtio_device *vdev, unsigned offset,
  162. const void *buf, unsigned len)
  163. {
  164. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  165. void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
  166. u8 b;
  167. __le16 w;
  168. __le32 l;
  169. if (vm_dev->version == 1) {
  170. const u8 *ptr = buf;
  171. int i;
  172. for (i = 0; i < len; i++)
  173. writeb(ptr[i], base + offset + i);
  174. return;
  175. }
  176. switch (len) {
  177. case 1:
  178. memcpy(&b, buf, sizeof b);
  179. writeb(b, base + offset);
  180. break;
  181. case 2:
  182. memcpy(&w, buf, sizeof w);
  183. writew(le16_to_cpu(w), base + offset);
  184. break;
  185. case 4:
  186. memcpy(&l, buf, sizeof l);
  187. writel(le32_to_cpu(l), base + offset);
  188. break;
  189. case 8:
  190. memcpy(&l, buf, sizeof l);
  191. writel(le32_to_cpu(l), base + offset);
  192. memcpy(&l, buf + sizeof l, sizeof l);
  193. writel(le32_to_cpu(l), base + offset + sizeof l);
  194. break;
  195. default:
  196. BUG();
  197. }
  198. }
  199. static u32 vm_generation(struct virtio_device *vdev)
  200. {
  201. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  202. if (vm_dev->version == 1)
  203. return 0;
  204. else
  205. return readl(vm_dev->base + VIRTIO_MMIO_CONFIG_GENERATION);
  206. }
  207. static u8 vm_get_status(struct virtio_device *vdev)
  208. {
  209. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  210. return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
  211. }
  212. static void vm_set_status(struct virtio_device *vdev, u8 status)
  213. {
  214. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  215. /* We should never be setting status to 0. */
  216. BUG_ON(status == 0);
  217. writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
  218. }
  219. static void vm_reset(struct virtio_device *vdev)
  220. {
  221. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  222. /* 0 status means a reset. */
  223. writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
  224. }
  225. /* Transport interface */
  226. /* the notify function used when creating a virt queue */
  227. static bool vm_notify(struct virtqueue *vq)
  228. {
  229. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
  230. /* We write the queue's selector into the notification register to
  231. * signal the other end */
  232. writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
  233. return true;
  234. }
  235. /* Notify all virtqueues on an interrupt. */
  236. static irqreturn_t vm_interrupt(int irq, void *opaque)
  237. {
  238. struct virtio_mmio_device *vm_dev = opaque;
  239. struct virtio_mmio_vq_info *info;
  240. unsigned long status;
  241. unsigned long flags;
  242. irqreturn_t ret = IRQ_NONE;
  243. /* Read and acknowledge interrupts */
  244. status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
  245. writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
  246. if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) {
  247. virtio_config_changed(&vm_dev->vdev);
  248. ret = IRQ_HANDLED;
  249. }
  250. if (likely(status & VIRTIO_MMIO_INT_VRING)) {
  251. spin_lock_irqsave(&vm_dev->lock, flags);
  252. list_for_each_entry(info, &vm_dev->virtqueues, node)
  253. ret |= vring_interrupt(irq, info->vq);
  254. spin_unlock_irqrestore(&vm_dev->lock, flags);
  255. }
  256. return ret;
  257. }
  258. static void vm_del_vq(struct virtqueue *vq)
  259. {
  260. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
  261. struct virtio_mmio_vq_info *info = vq->priv;
  262. unsigned long flags;
  263. unsigned int index = vq->index;
  264. spin_lock_irqsave(&vm_dev->lock, flags);
  265. list_del(&info->node);
  266. spin_unlock_irqrestore(&vm_dev->lock, flags);
  267. /* Select and deactivate the queue */
  268. writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
  269. if (vm_dev->version == 1) {
  270. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  271. } else {
  272. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
  273. WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
  274. }
  275. vring_del_virtqueue(vq);
  276. kfree(info);
  277. }
  278. static void vm_del_vqs(struct virtio_device *vdev)
  279. {
  280. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  281. struct virtqueue *vq, *n;
  282. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  283. vm_del_vq(vq);
  284. free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
  285. }
  286. static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
  287. void (*callback)(struct virtqueue *vq),
  288. const char *name)
  289. {
  290. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  291. struct virtio_mmio_vq_info *info;
  292. struct virtqueue *vq;
  293. unsigned long flags;
  294. unsigned int num;
  295. int err;
  296. if (!name)
  297. return NULL;
  298. /* Select the queue we're interested in */
  299. writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
  300. /* Queue shouldn't already be set up. */
  301. if (readl(vm_dev->base + (vm_dev->version == 1 ?
  302. VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) {
  303. err = -ENOENT;
  304. goto error_available;
  305. }
  306. /* Allocate and fill out our active queue description */
  307. info = kmalloc(sizeof(*info), GFP_KERNEL);
  308. if (!info) {
  309. err = -ENOMEM;
  310. goto error_kmalloc;
  311. }
  312. num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
  313. if (num == 0) {
  314. err = -ENOENT;
  315. goto error_new_virtqueue;
  316. }
  317. /* Create the vring */
  318. vq = vring_create_virtqueue(index, num, VIRTIO_MMIO_VRING_ALIGN, vdev,
  319. true, true, vm_notify, callback, name);
  320. if (!vq) {
  321. err = -ENOMEM;
  322. goto error_new_virtqueue;
  323. }
  324. /* Activate the queue */
  325. writel(virtqueue_get_vring_size(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
  326. if (vm_dev->version == 1) {
  327. writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
  328. writel(virtqueue_get_desc_addr(vq) >> PAGE_SHIFT,
  329. vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  330. } else {
  331. u64 addr;
  332. addr = virtqueue_get_desc_addr(vq);
  333. writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW);
  334. writel((u32)(addr >> 32),
  335. vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH);
  336. addr = virtqueue_get_avail_addr(vq);
  337. writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW);
  338. writel((u32)(addr >> 32),
  339. vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH);
  340. addr = virtqueue_get_used_addr(vq);
  341. writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW);
  342. writel((u32)(addr >> 32),
  343. vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH);
  344. writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
  345. }
  346. vq->priv = info;
  347. info->vq = vq;
  348. spin_lock_irqsave(&vm_dev->lock, flags);
  349. list_add(&info->node, &vm_dev->virtqueues);
  350. spin_unlock_irqrestore(&vm_dev->lock, flags);
  351. return vq;
  352. error_new_virtqueue:
  353. if (vm_dev->version == 1) {
  354. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  355. } else {
  356. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
  357. WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
  358. }
  359. kfree(info);
  360. error_kmalloc:
  361. error_available:
  362. return ERR_PTR(err);
  363. }
  364. static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  365. struct virtqueue *vqs[],
  366. vq_callback_t *callbacks[],
  367. const char * const names[])
  368. {
  369. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  370. unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
  371. int i, err;
  372. err = request_irq(irq, vm_interrupt, IRQF_SHARED,
  373. dev_name(&vdev->dev), vm_dev);
  374. if (err)
  375. return err;
  376. for (i = 0; i < nvqs; ++i) {
  377. vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
  378. if (IS_ERR(vqs[i])) {
  379. vm_del_vqs(vdev);
  380. return PTR_ERR(vqs[i]);
  381. }
  382. }
  383. return 0;
  384. }
  385. static const char *vm_bus_name(struct virtio_device *vdev)
  386. {
  387. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  388. return vm_dev->pdev->name;
  389. }
  390. static const struct virtio_config_ops virtio_mmio_config_ops = {
  391. .get = vm_get,
  392. .set = vm_set,
  393. .generation = vm_generation,
  394. .get_status = vm_get_status,
  395. .set_status = vm_set_status,
  396. .reset = vm_reset,
  397. .find_vqs = vm_find_vqs,
  398. .del_vqs = vm_del_vqs,
  399. .get_features = vm_get_features,
  400. .finalize_features = vm_finalize_features,
  401. .bus_name = vm_bus_name,
  402. };
  403. static void virtio_mmio_release_dev_empty(struct device *_d) {}
  404. /* Platform device */
  405. static int virtio_mmio_probe(struct platform_device *pdev)
  406. {
  407. struct virtio_mmio_device *vm_dev;
  408. struct resource *mem;
  409. unsigned long magic;
  410. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  411. if (!mem)
  412. return -EINVAL;
  413. if (!devm_request_mem_region(&pdev->dev, mem->start,
  414. resource_size(mem), pdev->name))
  415. return -EBUSY;
  416. vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
  417. if (!vm_dev)
  418. return -ENOMEM;
  419. vm_dev->vdev.dev.parent = &pdev->dev;
  420. vm_dev->vdev.dev.release = virtio_mmio_release_dev_empty;
  421. vm_dev->vdev.config = &virtio_mmio_config_ops;
  422. vm_dev->pdev = pdev;
  423. INIT_LIST_HEAD(&vm_dev->virtqueues);
  424. spin_lock_init(&vm_dev->lock);
  425. vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
  426. if (vm_dev->base == NULL)
  427. return -EFAULT;
  428. /* Check magic value */
  429. magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
  430. if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
  431. dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
  432. return -ENODEV;
  433. }
  434. /* Check device version */
  435. vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
  436. if (vm_dev->version < 1 || vm_dev->version > 2) {
  437. dev_err(&pdev->dev, "Version %ld not supported!\n",
  438. vm_dev->version);
  439. return -ENXIO;
  440. }
  441. vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
  442. if (vm_dev->vdev.id.device == 0) {
  443. /*
  444. * virtio-mmio device with an ID 0 is a (dummy) placeholder
  445. * with no function. End probing now with no error reported.
  446. */
  447. return -ENODEV;
  448. }
  449. vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
  450. if (vm_dev->version == 1)
  451. writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
  452. platform_set_drvdata(pdev, vm_dev);
  453. return register_virtio_device(&vm_dev->vdev);
  454. }
  455. static int virtio_mmio_remove(struct platform_device *pdev)
  456. {
  457. struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
  458. unregister_virtio_device(&vm_dev->vdev);
  459. return 0;
  460. }
  461. /* Devices list parameter */
  462. #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
  463. static struct device vm_cmdline_parent = {
  464. .init_name = "virtio-mmio-cmdline",
  465. };
  466. static int vm_cmdline_parent_registered;
  467. static int vm_cmdline_id;
  468. static int vm_cmdline_set(const char *device,
  469. const struct kernel_param *kp)
  470. {
  471. int err;
  472. struct resource resources[2] = {};
  473. char *str;
  474. long long int base, size;
  475. unsigned int irq;
  476. int processed, consumed = 0;
  477. struct platform_device *pdev;
  478. /* Consume "size" part of the command line parameter */
  479. size = memparse(device, &str);
  480. /* Get "@<base>:<irq>[:<id>]" chunks */
  481. processed = sscanf(str, "@%lli:%u%n:%d%n",
  482. &base, &irq, &consumed,
  483. &vm_cmdline_id, &consumed);
  484. /*
  485. * sscanf() must processes at least 2 chunks; also there
  486. * must be no extra characters after the last chunk, so
  487. * str[consumed] must be '\0'
  488. */
  489. if (processed < 2 || str[consumed])
  490. return -EINVAL;
  491. resources[0].flags = IORESOURCE_MEM;
  492. resources[0].start = base;
  493. resources[0].end = base + size - 1;
  494. resources[1].flags = IORESOURCE_IRQ;
  495. resources[1].start = resources[1].end = irq;
  496. if (!vm_cmdline_parent_registered) {
  497. err = device_register(&vm_cmdline_parent);
  498. if (err) {
  499. pr_err("Failed to register parent device!\n");
  500. return err;
  501. }
  502. vm_cmdline_parent_registered = 1;
  503. }
  504. pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
  505. vm_cmdline_id,
  506. (unsigned long long)resources[0].start,
  507. (unsigned long long)resources[0].end,
  508. (int)resources[1].start);
  509. pdev = platform_device_register_resndata(&vm_cmdline_parent,
  510. "virtio-mmio", vm_cmdline_id++,
  511. resources, ARRAY_SIZE(resources), NULL, 0);
  512. if (IS_ERR(pdev))
  513. return PTR_ERR(pdev);
  514. return 0;
  515. }
  516. static int vm_cmdline_get_device(struct device *dev, void *data)
  517. {
  518. char *buffer = data;
  519. unsigned int len = strlen(buffer);
  520. struct platform_device *pdev = to_platform_device(dev);
  521. snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
  522. pdev->resource[0].end - pdev->resource[0].start + 1ULL,
  523. (unsigned long long)pdev->resource[0].start,
  524. (unsigned long long)pdev->resource[1].start,
  525. pdev->id);
  526. return 0;
  527. }
  528. static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
  529. {
  530. buffer[0] = '\0';
  531. device_for_each_child(&vm_cmdline_parent, buffer,
  532. vm_cmdline_get_device);
  533. return strlen(buffer) + 1;
  534. }
  535. static const struct kernel_param_ops vm_cmdline_param_ops = {
  536. .set = vm_cmdline_set,
  537. .get = vm_cmdline_get,
  538. };
  539. device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
  540. static int vm_unregister_cmdline_device(struct device *dev,
  541. void *data)
  542. {
  543. platform_device_unregister(to_platform_device(dev));
  544. return 0;
  545. }
  546. static void vm_unregister_cmdline_devices(void)
  547. {
  548. if (vm_cmdline_parent_registered) {
  549. device_for_each_child(&vm_cmdline_parent, NULL,
  550. vm_unregister_cmdline_device);
  551. device_unregister(&vm_cmdline_parent);
  552. vm_cmdline_parent_registered = 0;
  553. }
  554. }
  555. #else
  556. static void vm_unregister_cmdline_devices(void)
  557. {
  558. }
  559. #endif
  560. /* Platform driver */
  561. static struct of_device_id virtio_mmio_match[] = {
  562. { .compatible = "virtio,mmio", },
  563. {},
  564. };
  565. MODULE_DEVICE_TABLE(of, virtio_mmio_match);
  566. #ifdef CONFIG_ACPI
  567. static const struct acpi_device_id virtio_mmio_acpi_match[] = {
  568. { "LNRO0005", },
  569. { }
  570. };
  571. MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match);
  572. #endif
  573. static struct platform_driver virtio_mmio_driver = {
  574. .probe = virtio_mmio_probe,
  575. .remove = virtio_mmio_remove,
  576. .driver = {
  577. .name = "virtio-mmio",
  578. .of_match_table = virtio_mmio_match,
  579. .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match),
  580. },
  581. };
  582. static int __init virtio_mmio_init(void)
  583. {
  584. return platform_driver_register(&virtio_mmio_driver);
  585. }
  586. static void __exit virtio_mmio_exit(void)
  587. {
  588. platform_driver_unregister(&virtio_mmio_driver);
  589. vm_unregister_cmdline_devices();
  590. }
  591. module_init(virtio_mmio_init);
  592. module_exit(virtio_mmio_exit);
  593. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  594. MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
  595. MODULE_LICENSE("GPL");