virtio_mmio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * Virtio memory mapped device driver
  3. *
  4. * Copyright 2011, ARM Ltd.
  5. *
  6. * This module allows virtio devices to be used over a virtual, memory mapped
  7. * platform device.
  8. *
  9. * Registers layout (all 32-bit wide):
  10. *
  11. * offset d. name description
  12. * ------ -- ---------------- -----------------
  13. *
  14. * 0x000 R MagicValue Magic value "virt"
  15. * 0x004 R Version Device version (current max. 1)
  16. * 0x008 R DeviceID Virtio device ID
  17. * 0x00c R VendorID Virtio vendor ID
  18. *
  19. * 0x010 R HostFeatures Features supported by the host
  20. * 0x014 W HostFeaturesSel Set of host features to access via HostFeatures
  21. *
  22. * 0x020 W GuestFeatures Features activated by the guest
  23. * 0x024 W GuestFeaturesSel Set of activated features to set via GuestFeatures
  24. * 0x028 W GuestPageSize Size of guest's memory page in bytes
  25. *
  26. * 0x030 W QueueSel Queue selector
  27. * 0x034 R QueueNumMax Maximum size of the currently selected queue
  28. * 0x038 W QueueNum Queue size for the currently selected queue
  29. * 0x03c W QueueAlign Used Ring alignment for the current queue
  30. * 0x040 RW QueuePFN PFN for the currently selected queue
  31. *
  32. * 0x050 W QueueNotify Queue notifier
  33. * 0x060 R InterruptStatus Interrupt status register
  34. * 0x060 W InterruptACK Interrupt acknowledge register
  35. * 0x070 RW Status Device status register
  36. *
  37. * 0x100+ RW Device-specific configuration space
  38. *
  39. * Based on Virtio PCI driver by Anthony Liguori, copyright IBM Corp. 2007
  40. *
  41. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  42. * See the COPYING file in the top-level directory.
  43. */
  44. #include <linux/highmem.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/io.h>
  47. #include <linux/list.h>
  48. #include <linux/module.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/slab.h>
  51. #include <linux/spinlock.h>
  52. #include <linux/virtio.h>
  53. #include <linux/virtio_config.h>
  54. #include <linux/virtio_mmio.h>
  55. #include <linux/virtio_ring.h>
  56. /* The alignment to use between consumer and producer parts of vring.
  57. * Currently hardcoded to the page size. */
  58. #define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE
  59. #define to_virtio_mmio_device(_plat_dev) \
  60. container_of(_plat_dev, struct virtio_mmio_device, vdev)
  61. struct virtio_mmio_device {
  62. struct virtio_device vdev;
  63. struct platform_device *pdev;
  64. void __iomem *base;
  65. unsigned long version;
  66. /* a list of queues so we can dispatch IRQs */
  67. spinlock_t lock;
  68. struct list_head virtqueues;
  69. };
  70. struct virtio_mmio_vq_info {
  71. /* the actual virtqueue */
  72. struct virtqueue *vq;
  73. /* the number of entries in the queue */
  74. unsigned int num;
  75. /* the index of the queue */
  76. int queue_index;
  77. /* the virtual address of the ring queue */
  78. void *queue;
  79. /* the list node for the virtqueues list */
  80. struct list_head node;
  81. };
  82. /* Configuration interface */
  83. static u32 vm_get_features(struct virtio_device *vdev)
  84. {
  85. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  86. /* TODO: Features > 32 bits */
  87. writel(0, vm_dev->base + VIRTIO_MMIO_HOST_FEATURES_SEL);
  88. return readl(vm_dev->base + VIRTIO_MMIO_HOST_FEATURES);
  89. }
  90. static void vm_finalize_features(struct virtio_device *vdev)
  91. {
  92. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  93. int i;
  94. /* Give virtio_ring a chance to accept features. */
  95. vring_transport_features(vdev);
  96. for (i = 0; i < ARRAY_SIZE(vdev->features); i++) {
  97. writel(i, vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES_SEL);
  98. writel(vdev->features[i],
  99. vm_dev->base + VIRTIO_MMIO_GUEST_FEATURES);
  100. }
  101. }
  102. static void vm_get(struct virtio_device *vdev, unsigned offset,
  103. void *buf, unsigned len)
  104. {
  105. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  106. u8 *ptr = buf;
  107. int i;
  108. for (i = 0; i < len; i++)
  109. ptr[i] = readb(vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
  110. }
  111. static void vm_set(struct virtio_device *vdev, unsigned offset,
  112. const void *buf, unsigned len)
  113. {
  114. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  115. const u8 *ptr = buf;
  116. int i;
  117. for (i = 0; i < len; i++)
  118. writeb(ptr[i], vm_dev->base + VIRTIO_MMIO_CONFIG + offset + i);
  119. }
  120. static u8 vm_get_status(struct virtio_device *vdev)
  121. {
  122. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  123. return readl(vm_dev->base + VIRTIO_MMIO_STATUS) & 0xff;
  124. }
  125. static void vm_set_status(struct virtio_device *vdev, u8 status)
  126. {
  127. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  128. /* We should never be setting status to 0. */
  129. BUG_ON(status == 0);
  130. writel(status, vm_dev->base + VIRTIO_MMIO_STATUS);
  131. }
  132. static void vm_reset(struct virtio_device *vdev)
  133. {
  134. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  135. /* 0 status means a reset. */
  136. writel(0, vm_dev->base + VIRTIO_MMIO_STATUS);
  137. }
  138. /* Transport interface */
  139. /* the notify function used when creating a virt queue */
  140. static void vm_notify(struct virtqueue *vq)
  141. {
  142. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
  143. struct virtio_mmio_vq_info *info = vq->priv;
  144. /* We write the queue's selector into the notification register to
  145. * signal the other end */
  146. writel(info->queue_index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
  147. }
  148. /* Notify all virtqueues on an interrupt. */
  149. static irqreturn_t vm_interrupt(int irq, void *opaque)
  150. {
  151. struct virtio_mmio_device *vm_dev = opaque;
  152. struct virtio_mmio_vq_info *info;
  153. struct virtio_driver *vdrv = container_of(vm_dev->vdev.dev.driver,
  154. struct virtio_driver, driver);
  155. unsigned long status;
  156. unsigned long flags;
  157. irqreturn_t ret = IRQ_NONE;
  158. /* Read and acknowledge interrupts */
  159. status = readl(vm_dev->base + VIRTIO_MMIO_INTERRUPT_STATUS);
  160. writel(status, vm_dev->base + VIRTIO_MMIO_INTERRUPT_ACK);
  161. if (unlikely(status & VIRTIO_MMIO_INT_CONFIG)
  162. && vdrv && vdrv->config_changed) {
  163. vdrv->config_changed(&vm_dev->vdev);
  164. ret = IRQ_HANDLED;
  165. }
  166. if (likely(status & VIRTIO_MMIO_INT_VRING)) {
  167. spin_lock_irqsave(&vm_dev->lock, flags);
  168. list_for_each_entry(info, &vm_dev->virtqueues, node)
  169. ret |= vring_interrupt(irq, info->vq);
  170. spin_unlock_irqrestore(&vm_dev->lock, flags);
  171. }
  172. return ret;
  173. }
  174. static void vm_del_vq(struct virtqueue *vq)
  175. {
  176. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
  177. struct virtio_mmio_vq_info *info = vq->priv;
  178. unsigned long flags, size;
  179. spin_lock_irqsave(&vm_dev->lock, flags);
  180. list_del(&info->node);
  181. spin_unlock_irqrestore(&vm_dev->lock, flags);
  182. vring_del_virtqueue(vq);
  183. /* Select and deactivate the queue */
  184. writel(info->queue_index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
  185. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  186. size = PAGE_ALIGN(vring_size(info->num, VIRTIO_MMIO_VRING_ALIGN));
  187. free_pages_exact(info->queue, size);
  188. kfree(info);
  189. }
  190. static void vm_del_vqs(struct virtio_device *vdev)
  191. {
  192. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  193. struct virtqueue *vq, *n;
  194. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  195. vm_del_vq(vq);
  196. free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
  197. }
  198. static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
  199. void (*callback)(struct virtqueue *vq),
  200. const char *name)
  201. {
  202. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  203. struct virtio_mmio_vq_info *info;
  204. struct virtqueue *vq;
  205. unsigned long flags, size;
  206. int err;
  207. /* Select the queue we're interested in */
  208. writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
  209. /* Queue shouldn't already be set up. */
  210. if (readl(vm_dev->base + VIRTIO_MMIO_QUEUE_PFN)) {
  211. err = -ENOENT;
  212. goto error_available;
  213. }
  214. /* Allocate and fill out our active queue description */
  215. info = kmalloc(sizeof(*info), GFP_KERNEL);
  216. if (!info) {
  217. err = -ENOMEM;
  218. goto error_kmalloc;
  219. }
  220. info->queue_index = index;
  221. /* Allocate pages for the queue - start with a queue as big as
  222. * possible (limited by maximum size allowed by device), drop down
  223. * to a minimal size, just big enough to fit descriptor table
  224. * and two rings (which makes it "alignment_size * 2")
  225. */
  226. info->num = readl(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX);
  227. while (1) {
  228. size = PAGE_ALIGN(vring_size(info->num,
  229. VIRTIO_MMIO_VRING_ALIGN));
  230. /* Already smallest possible allocation? */
  231. if (size <= VIRTIO_MMIO_VRING_ALIGN * 2) {
  232. err = -ENOMEM;
  233. goto error_alloc_pages;
  234. }
  235. info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  236. if (info->queue)
  237. break;
  238. info->num /= 2;
  239. }
  240. /* Activate the queue */
  241. writel(info->num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM);
  242. writel(VIRTIO_MMIO_VRING_ALIGN,
  243. vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN);
  244. writel(virt_to_phys(info->queue) >> PAGE_SHIFT,
  245. vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  246. /* Create the vring */
  247. vq = vring_new_virtqueue(info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
  248. true, info->queue, vm_notify, callback, name);
  249. if (!vq) {
  250. err = -ENOMEM;
  251. goto error_new_virtqueue;
  252. }
  253. vq->priv = info;
  254. info->vq = vq;
  255. spin_lock_irqsave(&vm_dev->lock, flags);
  256. list_add(&info->node, &vm_dev->virtqueues);
  257. spin_unlock_irqrestore(&vm_dev->lock, flags);
  258. return vq;
  259. error_new_virtqueue:
  260. writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
  261. free_pages_exact(info->queue, size);
  262. error_alloc_pages:
  263. kfree(info);
  264. error_kmalloc:
  265. error_available:
  266. return ERR_PTR(err);
  267. }
  268. static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  269. struct virtqueue *vqs[],
  270. vq_callback_t *callbacks[],
  271. const char *names[])
  272. {
  273. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  274. unsigned int irq = platform_get_irq(vm_dev->pdev, 0);
  275. int i, err;
  276. err = request_irq(irq, vm_interrupt, IRQF_SHARED,
  277. dev_name(&vdev->dev), vm_dev);
  278. if (err)
  279. return err;
  280. for (i = 0; i < nvqs; ++i) {
  281. vqs[i] = vm_setup_vq(vdev, i, callbacks[i], names[i]);
  282. if (IS_ERR(vqs[i])) {
  283. vm_del_vqs(vdev);
  284. return PTR_ERR(vqs[i]);
  285. }
  286. }
  287. return 0;
  288. }
  289. static const char *vm_bus_name(struct virtio_device *vdev)
  290. {
  291. struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
  292. return vm_dev->pdev->name;
  293. }
  294. static struct virtio_config_ops virtio_mmio_config_ops = {
  295. .get = vm_get,
  296. .set = vm_set,
  297. .get_status = vm_get_status,
  298. .set_status = vm_set_status,
  299. .reset = vm_reset,
  300. .find_vqs = vm_find_vqs,
  301. .del_vqs = vm_del_vqs,
  302. .get_features = vm_get_features,
  303. .finalize_features = vm_finalize_features,
  304. .bus_name = vm_bus_name,
  305. };
  306. /* Platform device */
  307. static int __devinit virtio_mmio_probe(struct platform_device *pdev)
  308. {
  309. struct virtio_mmio_device *vm_dev;
  310. struct resource *mem;
  311. unsigned long magic;
  312. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  313. if (!mem)
  314. return -EINVAL;
  315. if (!devm_request_mem_region(&pdev->dev, mem->start,
  316. resource_size(mem), pdev->name))
  317. return -EBUSY;
  318. vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
  319. if (!vm_dev)
  320. return -ENOMEM;
  321. vm_dev->vdev.dev.parent = &pdev->dev;
  322. vm_dev->vdev.config = &virtio_mmio_config_ops;
  323. vm_dev->pdev = pdev;
  324. INIT_LIST_HEAD(&vm_dev->virtqueues);
  325. spin_lock_init(&vm_dev->lock);
  326. vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
  327. if (vm_dev->base == NULL)
  328. return -EFAULT;
  329. /* Check magic value */
  330. magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
  331. if (memcmp(&magic, "virt", 4) != 0) {
  332. dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
  333. return -ENODEV;
  334. }
  335. /* Check device version */
  336. vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
  337. if (vm_dev->version != 1) {
  338. dev_err(&pdev->dev, "Version %ld not supported!\n",
  339. vm_dev->version);
  340. return -ENXIO;
  341. }
  342. vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
  343. vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);
  344. writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE);
  345. platform_set_drvdata(pdev, vm_dev);
  346. return register_virtio_device(&vm_dev->vdev);
  347. }
  348. static int __devexit virtio_mmio_remove(struct platform_device *pdev)
  349. {
  350. struct virtio_mmio_device *vm_dev = platform_get_drvdata(pdev);
  351. unregister_virtio_device(&vm_dev->vdev);
  352. return 0;
  353. }
  354. /* Platform driver */
  355. static struct of_device_id virtio_mmio_match[] = {
  356. { .compatible = "virtio,mmio", },
  357. {},
  358. };
  359. MODULE_DEVICE_TABLE(of, virtio_mmio_match);
  360. static struct platform_driver virtio_mmio_driver = {
  361. .probe = virtio_mmio_probe,
  362. .remove = __devexit_p(virtio_mmio_remove),
  363. .driver = {
  364. .name = "virtio-mmio",
  365. .owner = THIS_MODULE,
  366. .of_match_table = virtio_mmio_match,
  367. },
  368. };
  369. static int __init virtio_mmio_init(void)
  370. {
  371. return platform_driver_register(&virtio_mmio_driver);
  372. }
  373. static void __exit virtio_mmio_exit(void)
  374. {
  375. platform_driver_unregister(&virtio_mmio_driver);
  376. }
  377. module_init(virtio_mmio_init);
  378. module_exit(virtio_mmio_exit);
  379. MODULE_AUTHOR("Pawel Moll <pawel.moll@arm.com>");
  380. MODULE_DESCRIPTION("Platform bus driver for memory mapped virtio devices");
  381. MODULE_LICENSE("GPL");