virtio_pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Virtio PCI driver
  3. *
  4. * This module allows virtio devices to be used over a virtual PCI device.
  5. * This can be used with QEMU based VMMs like KVM or Xen.
  6. *
  7. * Copyright IBM Corp. 2007
  8. *
  9. * Authors:
  10. * Anthony Liguori <aliguori@us.ibm.com>
  11. *
  12. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  13. * See the COPYING file in the top-level directory.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/list.h>
  18. #include <linux/pci.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/virtio.h>
  21. #include <linux/virtio_config.h>
  22. #include <linux/virtio_ring.h>
  23. #include <linux/virtio_pci.h>
  24. #include <linux/highmem.h>
  25. #include <linux/spinlock.h>
  26. MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
  27. MODULE_DESCRIPTION("virtio-pci");
  28. MODULE_LICENSE("GPL");
  29. MODULE_VERSION("1");
  30. /* Our device structure */
  31. struct virtio_pci_device
  32. {
  33. struct virtio_device vdev;
  34. struct pci_dev *pci_dev;
  35. /* the IO mapping for the PCI config space */
  36. void __iomem *ioaddr;
  37. /* a list of queues so we can dispatch IRQs */
  38. spinlock_t lock;
  39. struct list_head virtqueues;
  40. };
  41. struct virtio_pci_vq_info
  42. {
  43. /* the actual virtqueue */
  44. struct virtqueue *vq;
  45. /* the number of entries in the queue */
  46. int num;
  47. /* the index of the queue */
  48. int queue_index;
  49. /* the virtual address of the ring queue */
  50. void *queue;
  51. /* the list node for the virtqueues list */
  52. struct list_head node;
  53. };
  54. /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
  55. static struct pci_device_id virtio_pci_id_table[] = {
  56. { 0x1af4, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  57. { 0 },
  58. };
  59. MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
  60. /* A PCI device has it's own struct device and so does a virtio device so
  61. * we create a place for the virtio devices to show up in sysfs. I think it
  62. * would make more sense for virtio to not insist on having it's own device. */
  63. static struct device *virtio_pci_root;
  64. /* Convert a generic virtio device to our structure */
  65. static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
  66. {
  67. return container_of(vdev, struct virtio_pci_device, vdev);
  68. }
  69. /* virtio config->get_features() implementation */
  70. static u32 vp_get_features(struct virtio_device *vdev)
  71. {
  72. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  73. /* When someone needs more than 32 feature bits, we'll need to
  74. * steal a bit to indicate that the rest are somewhere else. */
  75. return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
  76. }
  77. /* virtio config->finalize_features() implementation */
  78. static void vp_finalize_features(struct virtio_device *vdev)
  79. {
  80. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  81. /* Give virtio_ring a chance to accept features. */
  82. vring_transport_features(vdev);
  83. /* We only support 32 feature bits. */
  84. BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
  85. iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
  86. }
  87. /* virtio config->get() implementation */
  88. static void vp_get(struct virtio_device *vdev, unsigned offset,
  89. void *buf, unsigned len)
  90. {
  91. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  92. void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
  93. u8 *ptr = buf;
  94. int i;
  95. for (i = 0; i < len; i++)
  96. ptr[i] = ioread8(ioaddr + i);
  97. }
  98. /* the config->set() implementation. it's symmetric to the config->get()
  99. * implementation */
  100. static void vp_set(struct virtio_device *vdev, unsigned offset,
  101. const void *buf, unsigned len)
  102. {
  103. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  104. void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
  105. const u8 *ptr = buf;
  106. int i;
  107. for (i = 0; i < len; i++)
  108. iowrite8(ptr[i], ioaddr + i);
  109. }
  110. /* config->{get,set}_status() implementations */
  111. static u8 vp_get_status(struct virtio_device *vdev)
  112. {
  113. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  114. return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  115. }
  116. static void vp_set_status(struct virtio_device *vdev, u8 status)
  117. {
  118. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  119. /* We should never be setting status to 0. */
  120. BUG_ON(status == 0);
  121. iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  122. }
  123. static void vp_reset(struct virtio_device *vdev)
  124. {
  125. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  126. /* 0 status means a reset. */
  127. iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  128. }
  129. /* the notify function used when creating a virt queue */
  130. static void vp_notify(struct virtqueue *vq)
  131. {
  132. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  133. struct virtio_pci_vq_info *info = vq->priv;
  134. /* we write the queue's selector into the notification register to
  135. * signal the other end */
  136. iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
  137. }
  138. /* Handle a configuration change: Tell driver if it wants to know. */
  139. static irqreturn_t vp_config_changed(int irq, void *opaque)
  140. {
  141. struct virtio_pci_device *vp_dev = opaque;
  142. struct virtio_driver *drv;
  143. drv = container_of(vp_dev->vdev.dev.driver,
  144. struct virtio_driver, driver);
  145. if (drv && drv->config_changed)
  146. drv->config_changed(&vp_dev->vdev);
  147. return IRQ_HANDLED;
  148. }
  149. /* Notify all virtqueues on an interrupt. */
  150. static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
  151. {
  152. struct virtio_pci_device *vp_dev = opaque;
  153. struct virtio_pci_vq_info *info;
  154. irqreturn_t ret = IRQ_NONE;
  155. unsigned long flags;
  156. spin_lock_irqsave(&vp_dev->lock, flags);
  157. list_for_each_entry(info, &vp_dev->virtqueues, node) {
  158. if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
  159. ret = IRQ_HANDLED;
  160. }
  161. spin_unlock_irqrestore(&vp_dev->lock, flags);
  162. return ret;
  163. }
  164. /* A small wrapper to also acknowledge the interrupt when it's handled.
  165. * I really need an EIO hook for the vring so I can ack the interrupt once we
  166. * know that we'll be handling the IRQ but before we invoke the callback since
  167. * the callback may notify the host which results in the host attempting to
  168. * raise an interrupt that we would then mask once we acknowledged the
  169. * interrupt. */
  170. static irqreturn_t vp_interrupt(int irq, void *opaque)
  171. {
  172. struct virtio_pci_device *vp_dev = opaque;
  173. u8 isr;
  174. /* reading the ISR has the effect of also clearing it so it's very
  175. * important to save off the value. */
  176. isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  177. /* It's definitely not us if the ISR was not high */
  178. if (!isr)
  179. return IRQ_NONE;
  180. /* Configuration change? Tell driver if it wants to know. */
  181. if (isr & VIRTIO_PCI_ISR_CONFIG)
  182. vp_config_changed(irq, opaque);
  183. return vp_vring_interrupt(irq, opaque);
  184. }
  185. /* the config->find_vq() implementation */
  186. static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index,
  187. void (*callback)(struct virtqueue *vq),
  188. const char *name)
  189. {
  190. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  191. struct virtio_pci_vq_info *info;
  192. struct virtqueue *vq;
  193. unsigned long flags, size;
  194. u16 num;
  195. int err;
  196. /* Select the queue we're interested in */
  197. iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  198. /* Check if queue is either not available or already active. */
  199. num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
  200. if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
  201. return ERR_PTR(-ENOENT);
  202. /* allocate and fill out our structure the represents an active
  203. * queue */
  204. info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
  205. if (!info)
  206. return ERR_PTR(-ENOMEM);
  207. info->queue_index = index;
  208. info->num = num;
  209. size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
  210. info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
  211. if (info->queue == NULL) {
  212. err = -ENOMEM;
  213. goto out_info;
  214. }
  215. /* activate the queue */
  216. iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
  217. vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  218. /* create the vring */
  219. vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN,
  220. vdev, info->queue, vp_notify, callback, name);
  221. if (!vq) {
  222. err = -ENOMEM;
  223. goto out_activate_queue;
  224. }
  225. vq->priv = info;
  226. info->vq = vq;
  227. spin_lock_irqsave(&vp_dev->lock, flags);
  228. list_add(&info->node, &vp_dev->virtqueues);
  229. spin_unlock_irqrestore(&vp_dev->lock, flags);
  230. return vq;
  231. out_activate_queue:
  232. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  233. free_pages_exact(info->queue, size);
  234. out_info:
  235. kfree(info);
  236. return ERR_PTR(err);
  237. }
  238. /* the config->del_vq() implementation */
  239. static void vp_del_vq(struct virtqueue *vq)
  240. {
  241. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  242. struct virtio_pci_vq_info *info = vq->priv;
  243. unsigned long size;
  244. vring_del_virtqueue(vq);
  245. /* Select and deactivate the queue */
  246. iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  247. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  248. size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
  249. free_pages_exact(info->queue, size);
  250. kfree(info);
  251. }
  252. static void vp_del_vqs(struct virtio_device *vdev)
  253. {
  254. struct virtqueue *vq, *n;
  255. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  256. vp_del_vq(vq);
  257. }
  258. static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  259. struct virtqueue *vqs[],
  260. vq_callback_t *callbacks[],
  261. const char *names[])
  262. {
  263. int i;
  264. for (i = 0; i < nvqs; ++i) {
  265. vqs[i] = vp_find_vq(vdev, i, callbacks[i], names[i]);
  266. if (IS_ERR(vqs[i]))
  267. goto error;
  268. }
  269. return 0;
  270. error:
  271. vp_del_vqs(vdev);
  272. return PTR_ERR(vqs[i]);
  273. }
  274. static struct virtio_config_ops virtio_pci_config_ops = {
  275. .get = vp_get,
  276. .set = vp_set,
  277. .get_status = vp_get_status,
  278. .set_status = vp_set_status,
  279. .reset = vp_reset,
  280. .find_vqs = vp_find_vqs,
  281. .del_vqs = vp_del_vqs,
  282. .get_features = vp_get_features,
  283. .finalize_features = vp_finalize_features,
  284. };
  285. static void virtio_pci_release_dev(struct device *_d)
  286. {
  287. struct virtio_device *dev = container_of(_d, struct virtio_device, dev);
  288. struct virtio_pci_device *vp_dev = to_vp_device(dev);
  289. struct pci_dev *pci_dev = vp_dev->pci_dev;
  290. free_irq(pci_dev->irq, vp_dev);
  291. pci_set_drvdata(pci_dev, NULL);
  292. pci_iounmap(pci_dev, vp_dev->ioaddr);
  293. pci_release_regions(pci_dev);
  294. pci_disable_device(pci_dev);
  295. kfree(vp_dev);
  296. }
  297. /* the PCI probing function */
  298. static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
  299. const struct pci_device_id *id)
  300. {
  301. struct virtio_pci_device *vp_dev;
  302. int err;
  303. /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
  304. if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
  305. return -ENODEV;
  306. if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
  307. printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
  308. VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
  309. return -ENODEV;
  310. }
  311. /* allocate our structure and fill it out */
  312. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  313. if (vp_dev == NULL)
  314. return -ENOMEM;
  315. vp_dev->vdev.dev.parent = virtio_pci_root;
  316. vp_dev->vdev.dev.release = virtio_pci_release_dev;
  317. vp_dev->vdev.config = &virtio_pci_config_ops;
  318. vp_dev->pci_dev = pci_dev;
  319. INIT_LIST_HEAD(&vp_dev->virtqueues);
  320. spin_lock_init(&vp_dev->lock);
  321. /* enable the device */
  322. err = pci_enable_device(pci_dev);
  323. if (err)
  324. goto out;
  325. err = pci_request_regions(pci_dev, "virtio-pci");
  326. if (err)
  327. goto out_enable_device;
  328. vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
  329. if (vp_dev->ioaddr == NULL)
  330. goto out_req_regions;
  331. pci_set_drvdata(pci_dev, vp_dev);
  332. /* we use the subsystem vendor/device id as the virtio vendor/device
  333. * id. this allows us to use the same PCI vendor/device id for all
  334. * virtio devices and to identify the particular virtio driver by
  335. * the subsytem ids */
  336. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  337. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  338. /* register a handler for the queue with the PCI device's interrupt */
  339. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
  340. dev_name(&vp_dev->vdev.dev), vp_dev);
  341. if (err)
  342. goto out_set_drvdata;
  343. /* finally register the virtio device */
  344. err = register_virtio_device(&vp_dev->vdev);
  345. if (err)
  346. goto out_req_irq;
  347. return 0;
  348. out_req_irq:
  349. free_irq(pci_dev->irq, vp_dev);
  350. out_set_drvdata:
  351. pci_set_drvdata(pci_dev, NULL);
  352. pci_iounmap(pci_dev, vp_dev->ioaddr);
  353. out_req_regions:
  354. pci_release_regions(pci_dev);
  355. out_enable_device:
  356. pci_disable_device(pci_dev);
  357. out:
  358. kfree(vp_dev);
  359. return err;
  360. }
  361. static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
  362. {
  363. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  364. unregister_virtio_device(&vp_dev->vdev);
  365. }
  366. #ifdef CONFIG_PM
  367. static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  368. {
  369. pci_save_state(pci_dev);
  370. pci_set_power_state(pci_dev, PCI_D3hot);
  371. return 0;
  372. }
  373. static int virtio_pci_resume(struct pci_dev *pci_dev)
  374. {
  375. pci_restore_state(pci_dev);
  376. pci_set_power_state(pci_dev, PCI_D0);
  377. return 0;
  378. }
  379. #endif
  380. static struct pci_driver virtio_pci_driver = {
  381. .name = "virtio-pci",
  382. .id_table = virtio_pci_id_table,
  383. .probe = virtio_pci_probe,
  384. .remove = virtio_pci_remove,
  385. #ifdef CONFIG_PM
  386. .suspend = virtio_pci_suspend,
  387. .resume = virtio_pci_resume,
  388. #endif
  389. };
  390. static int __init virtio_pci_init(void)
  391. {
  392. int err;
  393. virtio_pci_root = root_device_register("virtio-pci");
  394. if (IS_ERR(virtio_pci_root))
  395. return PTR_ERR(virtio_pci_root);
  396. err = pci_register_driver(&virtio_pci_driver);
  397. if (err)
  398. device_unregister(virtio_pci_root);
  399. return err;
  400. }
  401. module_init(virtio_pci_init);
  402. static void __exit virtio_pci_exit(void)
  403. {
  404. pci_unregister_driver(&virtio_pci_driver);
  405. root_device_unregister(virtio_pci_root);
  406. }
  407. module_exit(virtio_pci_exit);