virtio_mmio.c 21 KB

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