virtio_mmio.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio memory mapped device driver
  4. *
  5. * Copyright 2011-2014, ARM Ltd.
  6. *
  7. * This module allows virtio devices to be used over a virtual, memory mapped
  8. * platform device.
  9. *
  10. * The guest device(s) may be instantiated in one of three equivalent ways:
  11. *
  12. * 1. Static platform device in board's code, eg.:
  13. *
  14. * static struct platform_device v2m_virtio_device = {
  15. * .name = "virtio-mmio",
  16. * .id = -1,
  17. * .num_resources = 2,
  18. * .resource = (struct resource []) {
  19. * {
  20. * .start = 0x1001e000,
  21. * .end = 0x1001e0ff,
  22. * .flags = IORESOURCE_MEM,
  23. * }, {
  24. * .start = 42 + 32,
  25. * .end = 42 + 32,
  26. * .flags = IORESOURCE_IRQ,
  27. * },
  28. * }
  29. * };
  30. *
  31. * 2. Device Tree node, eg.:
  32. *
  33. * virtio_block@1e000 {
  34. * compatible = "virtio,mmio";
  35. * reg = <0x1e000 0x100>;
  36. * interrupts = <42>;
  37. * }
  38. *
  39. * 3. Kernel module (or command line) parameter. Can be used more than once -
  40. * one device will be created for each one. Syntax:
  41. *
  42. * [virtio_mmio.]device=<size>@<baseaddr>:<irq>[:<id>]
  43. * where:
  44. * <size> := size (can use standard suffixes like K, M or G)
  45. * <baseaddr> := physical base address
  46. * <irq> := interrupt number (as passed to request_irq())
  47. * <id> := (optional) platform device id
  48. * eg.:
  49. * virtio_mmio.device=0x100@0x100b0000:48 \
  50. * virtio_mmio.device=1K@0x1001e000:74
  51. *
  52. * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007
  53. */
  54. #define pr_fmt(fmt) "virtio-mmio: " fmt
  55. #include <linux/acpi.h>
  56. #include <linux/dma-mapping.h>
  57. #include <linux/highmem.h>
  58. #include <linux/interrupt.h>
  59. #include <linux/io.h>
  60. #include <linux/list.h>
  61. #include <linux/module.h>
  62. #include <linux/of.h>
  63. #include <linux/platform_device.h>
  64. #include <linux/pm.h>
  65. #include <linux/slab.h>
  66. #include <linux/spinlock.h>
  67. #include <linux/virtio.h>
  68. #include <linux/virtio_config.h>
  69. #include <uapi/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 list node for the virtqueues list */
  89. struct list_head node;
  90. };
  91. /* Configuration interface */
  92. static u64 vm_get_features(struct virtio_device *vdev)
  93. {
  94. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  95. u64 features;
  96. writel(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
  97. features = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
  98. features <<= 32;
  99. writel(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL);
  100. features |= readl(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES);
  101. return features;
  102. }
  103. static int vm_finalize_features(struct virtio_device *vdev)
  104. {
  105. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  106. /* Give virtio_ring a chance to accept features. */
  107. vring_transport_features(vdev);
  108. /* Make sure there are no mixed devices */
  109. if (vm_dev->version == 2 &&
  110. !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  111. dev_err(&vdev->dev, "New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n");
  112. return -EINVAL;
  113. }
  114. writel(1, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
  115. writel((u32)(vdev->features >> 32),
  116. vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
  117. writel(0, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL);
  118. writel((u32)vdev->features,
  119. vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES);
  120. return 0;
  121. }
  122. static void vm_get(struct virtio_device *vdev, unsigned int offset,
  123. void *buf, unsigned int len)
  124. {
  125. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  126. void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
  127. u8 b;
  128. __le16 w;
  129. __le32 l;
  130. if (vm_dev->version == 1) {
  131. u8 *ptr = buf;
  132. int i;
  133. for (i = 0; i < len; i++)
  134. ptr[i] = readb(base + offset + i);
  135. return;
  136. }
  137. switch (len) {
  138. case 1:
  139. b = readb(base + offset);
  140. memcpy(buf, &b, sizeof b);
  141. break;
  142. case 2:
  143. w = cpu_to_le16(readw(base + offset));
  144. memcpy(buf, &w, sizeof w);
  145. break;
  146. case 4:
  147. l = cpu_to_le32(readl(base + offset));
  148. memcpy(buf, &l, sizeof l);
  149. break;
  150. case 8:
  151. l = cpu_to_le32(readl(base + offset));
  152. memcpy(buf, &l, sizeof l);
  153. l = cpu_to_le32(ioread32(base + offset + sizeof l));
  154. memcpy(buf + sizeof l, &l, sizeof l);
  155. break;
  156. default:
  157. BUG();
  158. }
  159. }
  160. static void vm_set(struct virtio_device *vdev, unsigned int offset,
  161. const void *buf, unsigned int len)
  162. {
  163. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  164. void __iomem *base = vm_dev->base + VIRTIO_MMIO_CONFIG;
  165. u8 b;
  166. __le16 w;
  167. __le32 l;
  168. if (vm_dev->version == 1) {
  169. const u8 *ptr = buf;
  170. int i;
  171. for (i = 0; i < len; i++)
  172. writeb(ptr[i], base + offset + i);
  173. return;
  174. }
  175. switch (len) {
  176. case 1:
  177. memcpy(&b, buf, sizeof b);
  178. writeb(b, base + offset);
  179. break;
  180. case 2:
  181. memcpy(&w, buf, sizeof w);
  182. writew(le16_to_cpu(w), base + offset);
  183. break;
  184. case 4:
  185. memcpy(&l, buf, sizeof l);
  186. writel(le32_to_cpu(l), base + offset);
  187. break;
  188. case 8:
  189. memcpy(&l, buf, sizeof l);
  190. writel(le32_to_cpu(l), base + offset);
  191. memcpy(&l, buf + sizeof l, sizeof l);
  192. writel(le32_to_cpu(l), base + offset + sizeof l);
  193. break;
  194. default:
  195. BUG();
  196. }
  197. }
  198. static u32 vm_generation(struct virtio_device *vdev)
  199. {
  200. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  201. if (vm_dev->version == 1)
  202. return 0;
  203. else
  204. return readl(vm_dev->base + VIRTIO_MMIO_CONFIG_GENERATION);
  205. }
  206. static u8 vm_get_status(struct virtio_device *vdev)
  207. {
  208. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  209. return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
  210. }
  211. static void vm_set_status(struct virtio_device *vdev, u8 status)
  212. {
  213. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  214. /* We should never be setting status to 0. */
  215. BUG_ON(status == 0);
  216. /*
  217. * Per memory-barriers.txt, wmb() is not needed to guarantee
  218. * that the cache coherent memory writes have completed
  219. * before writing to the MMIO region.
  220. */
  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. static bool vm_notify_with_data(struct virtqueue *vq)
  240. {
  241. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
  242. u32 data = vring_notification_data(vq);
  243. writel(data, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
  244. return true;
  245. }
  246. /* Notify all virtqueues on an interrupt. */
  247. static irqreturn_t vm_interrupt(int irq, void *opaque)
  248. {
  249. struct virtio_mmio_device *vm_dev = opaque;
  250. struct virtio_mmio_vq_info *info;
  251. unsigned long status;
  252. unsigned long flags;
  253. irqreturn_t ret = IRQ_NONE;
  254. /* Read and acknowledge interrupts */
  255. status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
  256. writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
  257. if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)) {
  258. virtio_config_changed(&vm_dev->vdev);
  259. ret = IRQ_HANDLED;
  260. }
  261. if (likely(status & VIRTIO_MMIO_INT_VRING)) {
  262. spin_lock_irqsave(&vm_dev->lock, flags);
  263. list_for_each_entry(info, &vm_dev->virtqueues, node)
  264. ret |= vring_interrupt(irq, info->vq);
  265. spin_unlock_irqrestore(&vm_dev->lock, flags);
  266. }
  267. return ret;
  268. }
  269. static void vm_del_vq(struct virtqueue *vq)
  270. {
  271. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
  272. struct virtio_mmio_vq_info *info = vq->priv;
  273. unsigned long flags;
  274. unsigned int index = vq->index;
  275. spin_lock_irqsave(&vm_dev->lock, flags);
  276. list_del(&info->node);
  277. spin_unlock_irqrestore(&vm_dev->lock, flags);
  278. /* Select and deactivate the queue */
  279. writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
  280. if (vm_dev->version == 1) {
  281. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  282. } else {
  283. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
  284. WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
  285. }
  286. vring_del_virtqueue(vq);
  287. kfree(info);
  288. }
  289. static void vm_del_vqs(struct virtio_device *vdev)
  290. {
  291. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  292. struct virtqueue *vq, *n;
  293. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  294. vm_del_vq(vq);
  295. free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
  296. }
  297. static void vm_synchronize_cbs(struct virtio_device *vdev)
  298. {
  299. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  300. synchronize_irq(platform_get_irq(vm_dev->pdev, 0));
  301. }
  302. static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned int index,
  303. void (*callback)(struct virtqueue *vq),
  304. const char *name, bool ctx)
  305. {
  306. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  307. bool (*notify)(struct virtqueue *vq);
  308. struct virtio_mmio_vq_info *info;
  309. struct virtqueue *vq;
  310. unsigned long flags;
  311. unsigned int num;
  312. int err;
  313. if (__virtio_test_bit(vdev, VIRTIO_F_NOTIFICATION_DATA))
  314. notify = vm_notify_with_data;
  315. else
  316. notify = vm_notify;
  317. if (!name)
  318. return NULL;
  319. /* Select the queue we're interested in */
  320. writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
  321. /* Queue shouldn't already be set up. */
  322. if (readl(vm_dev->base + (vm_dev->version == 1 ?
  323. VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) {
  324. err = -ENOENT;
  325. goto error_available;
  326. }
  327. /* Allocate and fill out our active queue description */
  328. info = kmalloc(sizeof(*info), GFP_KERNEL);
  329. if (!info) {
  330. err = -ENOMEM;
  331. goto error_kmalloc;
  332. }
  333. num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
  334. if (num == 0) {
  335. err = -ENOENT;
  336. goto error_new_virtqueue;
  337. }
  338. /* Create the vring */
  339. vq = vring_create_virtqueue(index, num, VIRTIO_MMIO_VRING_ALIGN, vdev,
  340. true, true, ctx, notify, callback, name);
  341. if (!vq) {
  342. err = -ENOMEM;
  343. goto error_new_virtqueue;
  344. }
  345. vq->num_max = num;
  346. /* Activate the queue */
  347. writel(virtqueue_get_vring_size(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
  348. if (vm_dev->version == 1) {
  349. u64 q_pfn = virtqueue_get_desc_addr(vq) >> PAGE_SHIFT;
  350. /*
  351. * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something
  352. * that doesn't fit in 32bit, fail the setup rather than
  353. * pretending to be successful.
  354. */
  355. if (q_pfn >> 32) {
  356. dev_err(&vdev->dev,
  357. "platform bug: legacy virtio-mmio must not be used with RAM above 0x%llxGB\n",
  358. 0x1ULL << (32 + PAGE_SHIFT - 30));
  359. err = -E2BIG;
  360. goto error_bad_pfn;
  361. }
  362. writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
  363. writel(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  364. } else {
  365. u64 addr;
  366. addr = virtqueue_get_desc_addr(vq);
  367. writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW);
  368. writel((u32)(addr >> 32),
  369. vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH);
  370. addr = virtqueue_get_avail_addr(vq);
  371. writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW);
  372. writel((u32)(addr >> 32),
  373. vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH);
  374. addr = virtqueue_get_used_addr(vq);
  375. writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW);
  376. writel((u32)(addr >> 32),
  377. vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH);
  378. writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
  379. }
  380. vq->priv = info;
  381. info->vq = vq;
  382. spin_lock_irqsave(&vm_dev->lock, flags);
  383. list_add(&info->node, &vm_dev->virtqueues);
  384. spin_unlock_irqrestore(&vm_dev->lock, flags);
  385. return vq;
  386. error_bad_pfn:
  387. vring_del_virtqueue(vq);
  388. error_new_virtqueue:
  389. if (vm_dev->version == 1) {
  390. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  391. } else {
  392. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY);
  393. WARN_ON(readl(vm_dev->base + VIRTIO_MMIO_QUEUE_READY));
  394. }
  395. kfree(info);
  396. error_kmalloc:
  397. error_available:
  398. return ERR_PTR(err);
  399. }
  400. static int vm_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  401. struct virtqueue *vqs[],
  402. struct virtqueue_info vqs_info[],
  403. struct irq_affinity *desc)
  404. {
  405. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  406. int irq = platform_get_irq(vm_dev->pdev, 0);
  407. int i, err, queue_idx = 0;
  408. if (irq < 0)
  409. return irq;
  410. err = request_irq(irq, vm_interrupt, IRQF_SHARED,
  411. dev_name(&vdev->dev), vm_dev);
  412. if (err)
  413. return err;
  414. if (of_property_read_bool(vm_dev->pdev->dev.of_node, "wakeup-source"))
  415. enable_irq_wake(irq);
  416. for (i = 0; i < nvqs; ++i) {
  417. struct virtqueue_info *vqi = &vqs_info[i];
  418. if (!vqi->name) {
  419. vqs[i] = NULL;
  420. continue;
  421. }
  422. vqs[i] = vm_setup_vq(vdev, queue_idx++, vqi->callback,
  423. vqi->name, vqi->ctx);
  424. if (IS_ERR(vqs[i])) {
  425. vm_del_vqs(vdev);
  426. return PTR_ERR(vqs[i]);
  427. }
  428. }
  429. return 0;
  430. }
  431. static const char *vm_bus_name(struct virtio_device *vdev)
  432. {
  433. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  434. return vm_dev->pdev->name;
  435. }
  436. static bool vm_get_shm_region(struct virtio_device *vdev,
  437. struct virtio_shm_region *region, u8 id)
  438. {
  439. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  440. u64 len, addr;
  441. /* Select the region we're interested in */
  442. writel(id, vm_dev->base + VIRTIO_MMIO_SHM_SEL);
  443. /* Read the region size */
  444. len = (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_LEN_LOW);
  445. len |= (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_LEN_HIGH) << 32;
  446. region->len = len;
  447. /* Check if region length is -1. If that's the case, the shared memory
  448. * region does not exist and there is no need to proceed further.
  449. */
  450. if (len == ~(u64)0)
  451. return false;
  452. /* Read the region base address */
  453. addr = (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_BASE_LOW);
  454. addr |= (u64) readl(vm_dev->base + VIRTIO_MMIO_SHM_BASE_HIGH) << 32;
  455. region->addr = addr;
  456. return true;
  457. }
  458. static const struct virtio_config_ops virtio_mmio_config_ops = {
  459. .get = vm_get,
  460. .set = vm_set,
  461. .generation = vm_generation,
  462. .get_status = vm_get_status,
  463. .set_status = vm_set_status,
  464. .reset = vm_reset,
  465. .find_vqs = vm_find_vqs,
  466. .del_vqs = vm_del_vqs,
  467. .get_features = vm_get_features,
  468. .finalize_features = vm_finalize_features,
  469. .bus_name = vm_bus_name,
  470. .get_shm_region = vm_get_shm_region,
  471. .synchronize_cbs = vm_synchronize_cbs,
  472. };
  473. #ifdef CONFIG_PM_SLEEP
  474. static int virtio_mmio_freeze(struct device *dev)
  475. {
  476. struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev);
  477. return virtio_device_freeze(&vm_dev->vdev);
  478. }
  479. static int virtio_mmio_restore(struct device *dev)
  480. {
  481. struct virtio_mmio_device *vm_dev = dev_get_drvdata(dev);
  482. if (vm_dev->version == 1)
  483. writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
  484. return virtio_device_restore(&vm_dev->vdev);
  485. }
  486. static const struct dev_pm_ops virtio_mmio_pm_ops = {
  487. SET_SYSTEM_SLEEP_PM_OPS(virtio_mmio_freeze, virtio_mmio_restore)
  488. };
  489. #endif
  490. static void virtio_mmio_release_dev(struct device *_d)
  491. {
  492. struct virtio_device *vdev =
  493. container_of(_d, struct virtio_device, dev);
  494. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  495. kfree(vm_dev);
  496. }
  497. /* Platform device */
  498. static int virtio_mmio_probe(struct platform_device *pdev)
  499. {
  500. struct virtio_mmio_device *vm_dev;
  501. unsigned long magic;
  502. int rc;
  503. vm_dev = kzalloc(sizeof(*vm_dev), GFP_KERNEL);
  504. if (!vm_dev)
  505. return -ENOMEM;
  506. vm_dev->vdev.dev.parent = &pdev->dev;
  507. vm_dev->vdev.dev.release = virtio_mmio_release_dev;
  508. vm_dev->vdev.config = &virtio_mmio_config_ops;
  509. vm_dev->pdev = pdev;
  510. INIT_LIST_HEAD(&vm_dev->virtqueues);
  511. spin_lock_init(&vm_dev->lock);
  512. vm_dev->base = devm_platform_ioremap_resource(pdev, 0);
  513. if (IS_ERR(vm_dev->base)) {
  514. rc = PTR_ERR(vm_dev->base);
  515. goto free_vm_dev;
  516. }
  517. /* Check magic value */
  518. magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
  519. if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
  520. dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
  521. rc = -ENODEV;
  522. goto free_vm_dev;
  523. }
  524. /* Check device version */
  525. vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
  526. if (vm_dev->version < 1 || vm_dev->version > 2) {
  527. dev_err(&pdev->dev, "Version %ld not supported!\n",
  528. vm_dev->version);
  529. rc = -ENXIO;
  530. goto free_vm_dev;
  531. }
  532. vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
  533. if (vm_dev->vdev.id.device == 0) {
  534. /*
  535. * virtio-mmio device with an ID 0 is a (dummy) placeholder
  536. * with no function. End probing now with no error reported.
  537. */
  538. rc = -ENODEV;
  539. goto free_vm_dev;
  540. }
  541. vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
  542. if (vm_dev->version == 1) {
  543. writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
  544. rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  545. /*
  546. * In the legacy case, ensure our coherently-allocated virtio
  547. * ring will be at an address expressable as a 32-bit PFN.
  548. */
  549. if (!rc)
  550. dma_set_coherent_mask(&pdev->dev,
  551. DMA_BIT_MASK(32 + PAGE_SHIFT));
  552. } else {
  553. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  554. }
  555. if (rc)
  556. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  557. if (rc)
  558. dev_warn(&pdev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
  559. platform_set_drvdata(pdev, vm_dev);
  560. rc = register_virtio_device(&vm_dev->vdev);
  561. if (rc)
  562. put_device(&vm_dev->vdev.dev);
  563. return rc;
  564. free_vm_dev:
  565. kfree(vm_dev);
  566. return rc;
  567. }
  568. static void virtio_mmio_remove(struct platform_device *pdev)
  569. {
  570. struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
  571. unregister_virtio_device(&vm_dev->vdev);
  572. }
  573. /* Devices list parameter */
  574. #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
  575. static struct device vm_cmdline_parent = {
  576. .init_name = "virtio-mmio-cmdline",
  577. };
  578. static int vm_cmdline_parent_registered;
  579. static int vm_cmdline_id;
  580. static int vm_cmdline_set(const char *device,
  581. const struct kernel_param *kp)
  582. {
  583. int err;
  584. struct resource resources[2] = {};
  585. char *str;
  586. long long base, size;
  587. unsigned int irq;
  588. int processed, consumed = 0;
  589. struct platform_device *pdev;
  590. /* Consume "size" part of the command line parameter */
  591. size = memparse(device, &str);
  592. /* Get "@<base>:<irq>[:<id>]" chunks */
  593. processed = sscanf(str, "@%lli:%u%n:%d%n",
  594. &base, &irq, &consumed,
  595. &vm_cmdline_id, &consumed);
  596. /*
  597. * sscanf() must process at least 2 chunks; also there
  598. * must be no extra characters after the last chunk, so
  599. * str[consumed] must be '\0'
  600. */
  601. if (processed < 2 || str[consumed] || irq == 0)
  602. return -EINVAL;
  603. resources[0].flags = IORESOURCE_MEM;
  604. resources[0].start = base;
  605. resources[0].end = base + size - 1;
  606. resources[1].flags = IORESOURCE_IRQ;
  607. resources[1].start = resources[1].end = irq;
  608. if (!vm_cmdline_parent_registered) {
  609. err = device_register(&vm_cmdline_parent);
  610. if (err) {
  611. put_device(&vm_cmdline_parent);
  612. pr_err("Failed to register parent device!\n");
  613. return err;
  614. }
  615. vm_cmdline_parent_registered = 1;
  616. }
  617. pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
  618. vm_cmdline_id,
  619. (unsigned long long)resources[0].start,
  620. (unsigned long long)resources[0].end,
  621. (int)resources[1].start);
  622. pdev = platform_device_register_resndata(&vm_cmdline_parent,
  623. "virtio-mmio", vm_cmdline_id++,
  624. resources, ARRAY_SIZE(resources), NULL, 0);
  625. return PTR_ERR_OR_ZERO(pdev);
  626. }
  627. static int vm_cmdline_get_device(struct device *dev, void *data)
  628. {
  629. char *buffer = data;
  630. unsigned int len = strlen(buffer);
  631. struct platform_device *pdev = to_platform_device(dev);
  632. snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
  633. pdev->resource[0].end - pdev->resource[0].start + 1ULL,
  634. (unsigned long long)pdev->resource[0].start,
  635. (unsigned long long)pdev->resource[1].start,
  636. pdev->id);
  637. return 0;
  638. }
  639. static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
  640. {
  641. buffer[0] = '\0';
  642. device_for_each_child(&vm_cmdline_parent, buffer,
  643. vm_cmdline_get_device);
  644. return strlen(buffer) + 1;
  645. }
  646. static const struct kernel_param_ops vm_cmdline_param_ops = {
  647. .set = vm_cmdline_set,
  648. .get = vm_cmdline_get,
  649. };
  650. device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
  651. static int vm_unregister_cmdline_device(struct device *dev,
  652. void *data)
  653. {
  654. platform_device_unregister(to_platform_device(dev));
  655. return 0;
  656. }
  657. static void vm_unregister_cmdline_devices(void)
  658. {
  659. if (vm_cmdline_parent_registered) {
  660. device_for_each_child(&vm_cmdline_parent, NULL,
  661. vm_unregister_cmdline_device);
  662. device_unregister(&vm_cmdline_parent);
  663. vm_cmdline_parent_registered = 0;
  664. }
  665. }
  666. #else
  667. static void vm_unregister_cmdline_devices(void)
  668. {
  669. }
  670. #endif
  671. /* Platform driver */
  672. static const struct of_device_id virtio_mmio_match[] = {
  673. { .compatible = "virtio,mmio", },
  674. {},
  675. };
  676. MODULE_DEVICE_TABLE(of, virtio_mmio_match);
  677. #ifdef CONFIG_ACPI
  678. static const struct acpi_device_id virtio_mmio_acpi_match[] = {
  679. { "LNRO0005", },
  680. { }
  681. };
  682. MODULE_DEVICE_TABLE(acpi, virtio_mmio_acpi_match);
  683. #endif
  684. static struct platform_driver virtio_mmio_driver = {
  685. .probe = virtio_mmio_probe,
  686. .remove_new = virtio_mmio_remove,
  687. .driver = {
  688. .name = "virtio-mmio",
  689. .of_match_table = virtio_mmio_match,
  690. .acpi_match_table = ACPI_PTR(virtio_mmio_acpi_match),
  691. #ifdef CONFIG_PM_SLEEP
  692. .pm = &virtio_mmio_pm_ops,
  693. #endif
  694. },
  695. };
  696. static int __init virtio_mmio_init(void)
  697. {
  698. return platform_driver_register(&virtio_mmio_driver);
  699. }
  700. static void __exit virtio_mmio_exit(void)
  701. {
  702. platform_driver_unregister(&virtio_mmio_driver);
  703. vm_unregister_cmdline_devices();
  704. }
  705. module_init(virtio_mmio_init);
  706. module_exit(virtio_mmio_exit);
  707. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  708. MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
  709. MODULE_LICENSE("GPL");