virtio_mmio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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 <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. {
  370. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  371. unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
  372. int i, err;
  373. err = request_irq(irq, vm_interrupt, IRQF_SHARED,
  374. dev_name(&vdev->dev), vm_dev);
  375. if (err)
  376. return err;
  377. for (i = 0; i < nvqs; ++i) {
  378. vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
  379. if (IS_ERR(vqs[i])) {
  380. vm_del_vqs(vdev);
  381. return PTR_ERR(vqs[i]);
  382. }
  383. }
  384. return 0;
  385. }
  386. static const char *vm_bus_name(struct virtio_device *vdev)
  387. {
  388. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  389. return vm_dev->pdev->name;
  390. }
  391. static const struct virtio_config_ops virtio_mmio_config_ops = {
  392. .get = vm_get,
  393. .set = vm_set,
  394. .generation = vm_generation,
  395. .get_status = vm_get_status,
  396. .set_status = vm_set_status,
  397. .reset = vm_reset,
  398. .find_vqs = vm_find_vqs,
  399. .del_vqs = vm_del_vqs,
  400. .get_features = vm_get_features,
  401. .finalize_features = vm_finalize_features,
  402. .bus_name = vm_bus_name,
  403. };
  404. /* Platform device */
  405. static int virtio_mmio_probe(struct platform_device *pdev)
  406. {
  407. struct virtio_mmio_device *vm_dev;
  408. struct resource *mem;
  409. unsigned long magic;
  410. int rc;
  411. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  412. if (!mem)
  413. return -EINVAL;
  414. if (!devm_request_mem_region(&pdev->dev, mem->start,
  415. resource_size(mem), pdev->name))
  416. return -EBUSY;
  417. vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
  418. if (!vm_dev)
  419. return -ENOMEM;
  420. vm_dev->vdev.dev.parent = &pdev->dev;
  421. vm_dev->vdev.config = &virtio_mmio_config_ops;
  422. vm_dev->pdev = pdev;
  423. INIT_LIST_HEAD(&vm_dev->virtqueues);
  424. spin_lock_init(&vm_dev->lock);
  425. vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
  426. if (vm_dev->base == NULL)
  427. return -EFAULT;
  428. /* Check magic value */
  429. magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
  430. if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
  431. dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
  432. return -ENODEV;
  433. }
  434. /* Check device version */
  435. vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
  436. if (vm_dev->version < 1 || vm_dev->version > 2) {
  437. dev_err(&pdev->dev, "Version %ld not supported!\n",
  438. vm_dev->version);
  439. return -ENXIO;
  440. }
  441. vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
  442. if (vm_dev->vdev.id.device == 0) {
  443. /*
  444. * virtio-mmio device with an ID 0 is a (dummy) placeholder
  445. * with no function. End probing now with no error reported.
  446. */
  447. return -ENODEV;
  448. }
  449. vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
  450. if (vm_dev->version == 1) {
  451. writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
  452. rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  453. /*
  454. * In the legacy case, ensure our coherently-allocated virtio
  455. * ring will be at an address expressable as a 32-bit PFN.
  456. */
  457. if (!rc)
  458. dma_set_coherent_mask(&pdev->dev,
  459. DMA_BIT_MASK(32 + PAGE_SHIFT));
  460. } else {
  461. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  462. }
  463. if (rc)
  464. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  465. if (rc)
  466. dev_warn(&pdev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
  467. platform_set_drvdata(pdev, vm_dev);
  468. return register_virtio_device(&vm_dev->vdev);
  469. }
  470. static int virtio_mmio_remove(struct platform_device *pdev)
  471. {
  472. struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
  473. unregister_virtio_device(&vm_dev->vdev);
  474. return 0;
  475. }
  476. /* Devices list parameter */
  477. #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
  478. static struct device vm_cmdline_parent = {
  479. .init_name = "virtio-mmio-cmdline",
  480. };
  481. static int vm_cmdline_parent_registered;
  482. static int vm_cmdline_id;
  483. static int vm_cmdline_set(const char *device,
  484. const struct kernel_param *kp)
  485. {
  486. int err;
  487. struct resource resources[2] = {};
  488. char *str;
  489. long long int base, size;
  490. unsigned int irq;
  491. int processed, consumed = 0;
  492. struct platform_device *pdev;
  493. /* Consume "size" part of the command line parameter */
  494. size = memparse(device, &str);
  495. /* Get "@<base>:<irq>[:<id>]" chunks */
  496. processed = sscanf(str, "@%lli:%u%n:%d%n",
  497. &base, &irq, &consumed,
  498. &vm_cmdline_id, &consumed);
  499. /*
  500. * sscanf() must processes at least 2 chunks; also there
  501. * must be no extra characters after the last chunk, so
  502. * str[consumed] must be '\0'
  503. */
  504. if (processed < 2 || str[consumed])
  505. return -EINVAL;
  506. resources[0].flags = IORESOURCE_MEM;
  507. resources[0].start = base;
  508. resources[0].end = base + size - 1;
  509. resources[1].flags = IORESOURCE_IRQ;
  510. resources[1].start = resources[1].end = irq;
  511. if (!vm_cmdline_parent_registered) {
  512. err = device_register(&vm_cmdline_parent);
  513. if (err) {
  514. pr_err("Failed to register parent device!\n");
  515. return err;
  516. }
  517. vm_cmdline_parent_registered = 1;
  518. }
  519. pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
  520. vm_cmdline_id,
  521. (unsigned long long)resources[0].start,
  522. (unsigned long long)resources[0].end,
  523. (int)resources[1].start);
  524. pdev = platform_device_register_resndata(&vm_cmdline_parent,
  525. "virtio-mmio", vm_cmdline_id++,
  526. resources, ARRAY_SIZE(resources), NULL, 0);
  527. if (IS_ERR(pdev))
  528. return PTR_ERR(pdev);
  529. return 0;
  530. }
  531. static int vm_cmdline_get_device(struct device *dev, void *data)
  532. {
  533. char *buffer = data;
  534. unsigned int len = strlen(buffer);
  535. struct platform_device *pdev = to_platform_device(dev);
  536. snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
  537. pdev->resource[0].end - pdev->resource[0].start + 1ULL,
  538. (unsigned long long)pdev->resource[0].start,
  539. (unsigned long long)pdev->resource[1].start,
  540. pdev->id);
  541. return 0;
  542. }
  543. static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
  544. {
  545. buffer[0] = '\0';
  546. device_for_each_child(&vm_cmdline_parent, buffer,
  547. vm_cmdline_get_device);
  548. return strlen(buffer) + 1;
  549. }
  550. static const struct kernel_param_ops vm_cmdline_param_ops = {
  551. .set = vm_cmdline_set,
  552. .get = vm_cmdline_get,
  553. };
  554. device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
  555. static int vm_unregister_cmdline_device(struct device *dev,
  556. void *data)
  557. {
  558. platform_device_unregister(to_platform_device(dev));
  559. return 0;
  560. }
  561. static void vm_unregister_cmdline_devices(void)
  562. {
  563. if (vm_cmdline_parent_registered) {
  564. device_for_each_child(&vm_cmdline_parent, NULL,
  565. vm_unregister_cmdline_device);
  566. device_unregister(&vm_cmdline_parent);
  567. vm_cmdline_parent_registered = 0;
  568. }
  569. }
  570. #else
  571. static void vm_unregister_cmdline_devices(void)
  572. {
  573. }
  574. #endif
  575. /* Platform driver */
  576. static struct of_device_id virtio_mmio_match[] = {
  577. { .compatible = "virtio,mmio", },
  578. {},
  579. };
  580. MODULE_DEVICE_TABLE(of, virtio_mmio_match);
  581. #ifdef CONFIG_ACPI
  582. static const struct acpi_device_id virtio_mmio_acpi_match[] = {
  583. { "LNRO0005", },
  584. { }
  585. };
  586. MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match);
  587. #endif
  588. static struct platform_driver virtio_mmio_driver = {
  589. .probe = virtio_mmio_probe,
  590. .remove = virtio_mmio_remove,
  591. .driver = {
  592. .name = "virtio-mmio",
  593. .of_match_table = virtio_mmio_match,
  594. .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match),
  595. },
  596. };
  597. static int __init virtio_mmio_init(void)
  598. {
  599. return platform_driver_register(&virtio_mmio_driver);
  600. }
  601. static void __exit virtio_mmio_exit(void)
  602. {
  603. platform_driver_unregister(&virtio_mmio_driver);
  604. vm_unregister_cmdline_devices();
  605. }
  606. module_init(virtio_mmio_init);
  607. module_exit(virtio_mmio_exit);
  608. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  609. MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
  610. MODULE_LICENSE("GPL");