virtio_mmio.c 19 KB

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