virtio_mmio.c 19 KB

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