virtio_mmio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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, bool ctx)
  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, ctx, 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. u64 q_pfn = virtqueue_get_desc_addr(vq) >> PAGE_SHIFT;
  329. /*
  330. * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something
  331. * that doesn't fit in 32bit, fail the setup rather than
  332. * pretending to be successful.
  333. */
  334. if (q_pfn >> 32) {
  335. dev_err(&vdev->dev,
  336. "platform bug: legacy virtio-mmio must not be used with RAM above 0x%llxGB\n",
  337. 0x1ULL << (32 + PAGE_SHIFT - 30));
  338. err = -E2BIG;
  339. goto error_bad_pfn;
  340. }
  341. writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
  342. writel(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  343. } else {
  344. u64 addr;
  345. addr = virtqueue_get_desc_addr(vq);
  346. writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW);
  347. writel((u32)(addr >> 32),
  348. vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH);
  349. addr = virtqueue_get_avail_addr(vq);
  350. writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW);
  351. writel((u32)(addr >> 32),
  352. vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH);
  353. addr = virtqueue_get_used_addr(vq);
  354. writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW);
  355. writel((u32)(addr >> 32),
  356. vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH);
  357. writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
  358. }
  359. vq->priv = info;
  360. info->vq = vq;
  361. spin_lock_irqsave(&vm_dev->lock, flags);
  362. list_add(&info->node, &vm_dev->virtqueues);
  363. spin_unlock_irqrestore(&vm_dev->lock, flags);
  364. return vq;
  365. error_bad_pfn:
  366. vring_del_virtqueue(vq);
  367. error_new_virtqueue:
  368. if (vm_dev->version == 1) {
  369. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  370. } else {
  371. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
  372. WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
  373. }
  374. kfree(info);
  375. error_kmalloc:
  376. error_available:
  377. return ERR_PTR(err);
  378. }
  379. static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  380. struct virtqueue *vqs[],
  381. vq_callback_t *callbacks[],
  382. const char * const names[],
  383. const bool *ctx,
  384. struct irq_affinity *desc)
  385. {
  386. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  387. unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
  388. int i, err;
  389. err = request_irq(irq, vm_interrupt, IRQF_SHARED,
  390. dev_name(&vdev->dev), vm_dev);
  391. if (err)
  392. return err;
  393. for (i = 0; i < nvqs; ++i) {
  394. vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i],
  395. ctx ? ctx[i] : false);
  396. if (IS_ERR(vqs[i])) {
  397. vm_del_vqs(vdev);
  398. return PTR_ERR(vqs[i]);
  399. }
  400. }
  401. return 0;
  402. }
  403. static const char *vm_bus_name(struct virtio_device *vdev)
  404. {
  405. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  406. return vm_dev->pdev->name;
  407. }
  408. static const struct virtio_config_ops virtio_mmio_config_ops = {
  409. .get = vm_get,
  410. .set = vm_set,
  411. .generation = vm_generation,
  412. .get_status = vm_get_status,
  413. .set_status = vm_set_status,
  414. .reset = vm_reset,
  415. .find_vqs = vm_find_vqs,
  416. .del_vqs = vm_del_vqs,
  417. .get_features = vm_get_features,
  418. .finalize_features = vm_finalize_features,
  419. .bus_name = vm_bus_name,
  420. };
  421. static void virtio_mmio_release_dev(struct device *_d)
  422. {
  423. struct virtio_device *vdev =
  424. container_of(_d, struct virtio_device, dev);
  425. struct virtio_mmio_device *vm_dev =
  426. container_of(vdev, struct virtio_mmio_device, vdev);
  427. struct platform_device *pdev = vm_dev->pdev;
  428. devm_kfree(&pdev->dev, vm_dev);
  429. }
  430. /* Platform device */
  431. static int virtio_mmio_probe(struct platform_device *pdev)
  432. {
  433. struct virtio_mmio_device *vm_dev;
  434. struct resource *mem;
  435. unsigned long magic;
  436. int rc;
  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.dev.release = virtio_mmio_release_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. rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  480. /*
  481. * In the legacy case, ensure our coherently-allocated virtio
  482. * ring will be at an address expressable as a 32-bit PFN.
  483. */
  484. if (!rc)
  485. dma_set_coherent_mask(&pdev->dev,
  486. DMA_BIT_MASK(32 + PAGE_SHIFT));
  487. } else {
  488. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  489. }
  490. if (rc)
  491. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  492. if (rc)
  493. dev_warn(&pdev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
  494. platform_set_drvdata(pdev, vm_dev);
  495. rc = register_virtio_device(&vm_dev->vdev);
  496. if (rc)
  497. put_device(&vm_dev->vdev.dev);
  498. return rc;
  499. }
  500. static int virtio_mmio_remove(struct platform_device *pdev)
  501. {
  502. struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
  503. unregister_virtio_device(&vm_dev->vdev);
  504. return 0;
  505. }
  506. /* Devices list parameter */
  507. #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
  508. static struct device vm_cmdline_parent = {
  509. .init_name = "virtio-mmio-cmdline",
  510. };
  511. static int vm_cmdline_parent_registered;
  512. static int vm_cmdline_id;
  513. static int vm_cmdline_set(const char *device,
  514. const struct kernel_param *kp)
  515. {
  516. int err;
  517. struct resource resources[2] = {};
  518. char *str;
  519. long long int base, size;
  520. unsigned int irq;
  521. int processed, consumed = 0;
  522. struct platform_device *pdev;
  523. /* Consume "size" part of the command line parameter */
  524. size = memparse(device, &str);
  525. /* Get "@<base>:<irq>[:<id>]" chunks */
  526. processed = sscanf(str, "@%lli:%u%n:%d%n",
  527. &base, &irq, &consumed,
  528. &vm_cmdline_id, &consumed);
  529. /*
  530. * sscanf() must processes at least 2 chunks; also there
  531. * must be no extra characters after the last chunk, so
  532. * str[consumed] must be '\0'
  533. */
  534. if (processed < 2 || str[consumed])
  535. return -EINVAL;
  536. resources[0].flags = IORESOURCE_MEM;
  537. resources[0].start = base;
  538. resources[0].end = base + size - 1;
  539. resources[1].flags = IORESOURCE_IRQ;
  540. resources[1].start = resources[1].end = irq;
  541. if (!vm_cmdline_parent_registered) {
  542. err = device_register(&vm_cmdline_parent);
  543. if (err) {
  544. pr_err("Failed to register parent device!\n");
  545. return err;
  546. }
  547. vm_cmdline_parent_registered = 1;
  548. }
  549. pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
  550. vm_cmdline_id,
  551. (unsigned long long)resources[0].start,
  552. (unsigned long long)resources[0].end,
  553. (int)resources[1].start);
  554. pdev = platform_device_register_resndata(&vm_cmdline_parent,
  555. "virtio-mmio", vm_cmdline_id++,
  556. resources, ARRAY_SIZE(resources), NULL, 0);
  557. return PTR_ERR_OR_ZERO(pdev);
  558. }
  559. static int vm_cmdline_get_device(struct device *dev, void *data)
  560. {
  561. char *buffer = data;
  562. unsigned int len = strlen(buffer);
  563. struct platform_device *pdev = to_platform_device(dev);
  564. snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
  565. pdev->resource[0].end - pdev->resource[0].start + 1ULL,
  566. (unsigned long long)pdev->resource[0].start,
  567. (unsigned long long)pdev->resource[1].start,
  568. pdev->id);
  569. return 0;
  570. }
  571. static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
  572. {
  573. buffer[0] = '\0';
  574. device_for_each_child(&vm_cmdline_parent, buffer,
  575. vm_cmdline_get_device);
  576. return strlen(buffer) + 1;
  577. }
  578. static const struct kernel_param_ops vm_cmdline_param_ops = {
  579. .set = vm_cmdline_set,
  580. .get = vm_cmdline_get,
  581. };
  582. device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
  583. static int vm_unregister_cmdline_device(struct device *dev,
  584. void *data)
  585. {
  586. platform_device_unregister(to_platform_device(dev));
  587. return 0;
  588. }
  589. static void vm_unregister_cmdline_devices(void)
  590. {
  591. if (vm_cmdline_parent_registered) {
  592. device_for_each_child(&vm_cmdline_parent, NULL,
  593. vm_unregister_cmdline_device);
  594. device_unregister(&vm_cmdline_parent);
  595. vm_cmdline_parent_registered = 0;
  596. }
  597. }
  598. #else
  599. static void vm_unregister_cmdline_devices(void)
  600. {
  601. }
  602. #endif
  603. /* Platform driver */
  604. static const struct of_device_id virtio_mmio_match[] = {
  605. { .compatible = "virtio,mmio", },
  606. {},
  607. };
  608. MODULE_DEVICE_TABLE(of, virtio_mmio_match);
  609. #ifdef CONFIG_ACPI
  610. static const struct acpi_device_id virtio_mmio_acpi_match[] = {
  611. { "LNRO0005", },
  612. { }
  613. };
  614. MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match);
  615. #endif
  616. static struct platform_driver virtio_mmio_driver = {
  617. .probe = virtio_mmio_probe,
  618. .remove = virtio_mmio_remove,
  619. .driver = {
  620. .name = "virtio-mmio",
  621. .of_match_table = virtio_mmio_match,
  622. .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match),
  623. },
  624. };
  625. static int __init virtio_mmio_init(void)
  626. {
  627. return platform_driver_register(&virtio_mmio_driver);
  628. }
  629. static void __exit virtio_mmio_exit(void)
  630. {
  631. platform_driver_unregister(&virtio_mmio_driver);
  632. vm_unregister_cmdline_devices();
  633. }
  634. module_init(virtio_mmio_init);
  635. module_exit(virtio_mmio_exit);
  636. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  637. MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
  638. MODULE_LICENSE("GPL");