virtio_mmio.c 19 KB

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