virtio_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. .parent = NULL,
  65. .bus_id = "virtio-pci",
  66. };
  67. /* Unique numbering for devices under the kvm root */
  68. static unsigned int dev_index;
  69. /* Convert a generic virtio device to our structure */
  70. static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
  71. {
  72. return container_of(vdev, struct virtio_pci_device, vdev);
  73. }
  74. /* virtio config->get_features() implementation */
  75. static u32 vp_get_features(struct virtio_device *vdev)
  76. {
  77. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  78. /* When someone needs more than 32 feature bits, we'll need to
  79. * steal a bit to indicate that the rest are somewhere else. */
  80. return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
  81. }
  82. /* virtio config->set_features() implementation */
  83. static void vp_set_features(struct virtio_device *vdev, u32 features)
  84. {
  85. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  86. iowrite32(features, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES);
  87. }
  88. /* virtio config->get() implementation */
  89. static void vp_get(struct virtio_device *vdev, unsigned offset,
  90. void *buf, unsigned len)
  91. {
  92. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  93. void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
  94. u8 *ptr = buf;
  95. int i;
  96. for (i = 0; i < len; i++)
  97. ptr[i] = ioread8(ioaddr + i);
  98. }
  99. /* the config->set() implementation. it's symmetric to the config->get()
  100. * implementation */
  101. static void vp_set(struct virtio_device *vdev, unsigned offset,
  102. const void *buf, unsigned len)
  103. {
  104. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  105. void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
  106. const u8 *ptr = buf;
  107. int i;
  108. for (i = 0; i < len; i++)
  109. iowrite8(ptr[i], ioaddr + i);
  110. }
  111. /* config->{get,set}_status() implementations */
  112. static u8 vp_get_status(struct virtio_device *vdev)
  113. {
  114. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  115. return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  116. }
  117. static void vp_set_status(struct virtio_device *vdev, u8 status)
  118. {
  119. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  120. /* We should never be setting status to 0. */
  121. BUG_ON(status == 0);
  122. iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  123. }
  124. static void vp_reset(struct virtio_device *vdev)
  125. {
  126. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  127. /* 0 status means a reset. */
  128. iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
  129. }
  130. /* the notify function used when creating a virt queue */
  131. static void vp_notify(struct virtqueue *vq)
  132. {
  133. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  134. struct virtio_pci_vq_info *info = vq->priv;
  135. /* we write the queue's selector into the notification register to
  136. * signal the other end */
  137. iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
  138. }
  139. /* A small wrapper to also acknowledge the interrupt when it's handled.
  140. * I really need an EIO hook for the vring so I can ack the interrupt once we
  141. * know that we'll be handling the IRQ but before we invoke the callback since
  142. * the callback may notify the host which results in the host attempting to
  143. * raise an interrupt that we would then mask once we acknowledged the
  144. * interrupt. */
  145. static irqreturn_t vp_interrupt(int irq, void *opaque)
  146. {
  147. struct virtio_pci_device *vp_dev = opaque;
  148. struct virtio_pci_vq_info *info;
  149. irqreturn_t ret = IRQ_NONE;
  150. unsigned long flags;
  151. u8 isr;
  152. /* reading the ISR has the effect of also clearing it so it's very
  153. * important to save off the value. */
  154. isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
  155. /* It's definitely not us if the ISR was not high */
  156. if (!isr)
  157. return IRQ_NONE;
  158. /* Configuration change? Tell driver if it wants to know. */
  159. if (isr & VIRTIO_PCI_ISR_CONFIG) {
  160. struct virtio_driver *drv;
  161. drv = container_of(vp_dev->vdev.dev.driver,
  162. struct virtio_driver, driver);
  163. if (drv->config_changed)
  164. drv->config_changed(&vp_dev->vdev);
  165. }
  166. spin_lock_irqsave(&vp_dev->lock, flags);
  167. list_for_each_entry(info, &vp_dev->virtqueues, node) {
  168. if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
  169. ret = IRQ_HANDLED;
  170. }
  171. spin_unlock_irqrestore(&vp_dev->lock, flags);
  172. return ret;
  173. }
  174. /* the config->find_vq() implementation */
  175. static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index,
  176. void (*callback)(struct virtqueue *vq))
  177. {
  178. struct virtio_pci_device *vp_dev = to_vp_device(vdev);
  179. struct virtio_pci_vq_info *info;
  180. struct virtqueue *vq;
  181. unsigned long flags;
  182. u16 num;
  183. int err;
  184. /* Select the queue we're interested in */
  185. iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  186. /* Check if queue is either not available or already active. */
  187. num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
  188. if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
  189. return ERR_PTR(-ENOENT);
  190. /* allocate and fill out our structure the represents an active
  191. * queue */
  192. info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
  193. if (!info)
  194. return ERR_PTR(-ENOMEM);
  195. info->queue_index = index;
  196. info->num = num;
  197. info->queue = kzalloc(PAGE_ALIGN(vring_size(num,PAGE_SIZE)), GFP_KERNEL);
  198. if (info->queue == NULL) {
  199. err = -ENOMEM;
  200. goto out_info;
  201. }
  202. /* activate the queue */
  203. iowrite32(virt_to_phys(info->queue) >> PAGE_SHIFT,
  204. vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  205. /* create the vring */
  206. vq = vring_new_virtqueue(info->num, vdev, info->queue,
  207. vp_notify, callback);
  208. if (!vq) {
  209. err = -ENOMEM;
  210. goto out_activate_queue;
  211. }
  212. vq->priv = info;
  213. info->vq = vq;
  214. spin_lock_irqsave(&vp_dev->lock, flags);
  215. list_add(&info->node, &vp_dev->virtqueues);
  216. spin_unlock_irqrestore(&vp_dev->lock, flags);
  217. return vq;
  218. out_activate_queue:
  219. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  220. kfree(info->queue);
  221. out_info:
  222. kfree(info);
  223. return ERR_PTR(err);
  224. }
  225. /* the config->del_vq() implementation */
  226. static void vp_del_vq(struct virtqueue *vq)
  227. {
  228. struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
  229. struct virtio_pci_vq_info *info = vq->priv;
  230. unsigned long flags;
  231. spin_lock_irqsave(&vp_dev->lock, flags);
  232. list_del(&info->node);
  233. spin_unlock_irqrestore(&vp_dev->lock, flags);
  234. vring_del_virtqueue(vq);
  235. /* Select and deactivate the queue */
  236. iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  237. iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  238. kfree(info->queue);
  239. kfree(info);
  240. }
  241. static struct virtio_config_ops virtio_pci_config_ops = {
  242. .get = vp_get,
  243. .set = vp_set,
  244. .get_status = vp_get_status,
  245. .set_status = vp_set_status,
  246. .reset = vp_reset,
  247. .find_vq = vp_find_vq,
  248. .del_vq = vp_del_vq,
  249. .get_features = vp_get_features,
  250. .set_features = vp_set_features,
  251. };
  252. /* the PCI probing function */
  253. static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
  254. const struct pci_device_id *id)
  255. {
  256. struct virtio_pci_device *vp_dev;
  257. int err;
  258. /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
  259. if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
  260. return -ENODEV;
  261. if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
  262. printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
  263. VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
  264. return -ENODEV;
  265. }
  266. /* allocate our structure and fill it out */
  267. vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
  268. if (vp_dev == NULL)
  269. return -ENOMEM;
  270. snprintf(vp_dev->vdev.dev.bus_id, BUS_ID_SIZE, "virtio%d", dev_index);
  271. vp_dev->vdev.index = dev_index;
  272. dev_index++;
  273. vp_dev->vdev.dev.parent = &virtio_pci_root;
  274. vp_dev->vdev.config = &virtio_pci_config_ops;
  275. vp_dev->pci_dev = pci_dev;
  276. INIT_LIST_HEAD(&vp_dev->virtqueues);
  277. spin_lock_init(&vp_dev->lock);
  278. /* enable the device */
  279. err = pci_enable_device(pci_dev);
  280. if (err)
  281. goto out;
  282. err = pci_request_regions(pci_dev, "virtio-pci");
  283. if (err)
  284. goto out_enable_device;
  285. vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
  286. if (vp_dev->ioaddr == NULL)
  287. goto out_req_regions;
  288. pci_set_drvdata(pci_dev, vp_dev);
  289. /* we use the subsystem vendor/device id as the virtio vendor/device
  290. * id. this allows us to use the same PCI vendor/device id for all
  291. * virtio devices and to identify the particular virtio driver by
  292. * the subsytem ids */
  293. vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
  294. vp_dev->vdev.id.device = pci_dev->subsystem_device;
  295. /* register a handler for the queue with the PCI device's interrupt */
  296. err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
  297. vp_dev->vdev.dev.bus_id, vp_dev);
  298. if (err)
  299. goto out_set_drvdata;
  300. /* finally register the virtio device */
  301. err = register_virtio_device(&vp_dev->vdev);
  302. if (err)
  303. goto out_req_irq;
  304. return 0;
  305. out_req_irq:
  306. free_irq(pci_dev->irq, vp_dev);
  307. out_set_drvdata:
  308. pci_set_drvdata(pci_dev, NULL);
  309. pci_iounmap(pci_dev, vp_dev->ioaddr);
  310. out_req_regions:
  311. pci_release_regions(pci_dev);
  312. out_enable_device:
  313. pci_disable_device(pci_dev);
  314. out:
  315. kfree(vp_dev);
  316. return err;
  317. }
  318. static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
  319. {
  320. struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
  321. unregister_virtio_device(&vp_dev->vdev);
  322. free_irq(pci_dev->irq, vp_dev);
  323. pci_set_drvdata(pci_dev, NULL);
  324. pci_iounmap(pci_dev, vp_dev->ioaddr);
  325. pci_release_regions(pci_dev);
  326. pci_disable_device(pci_dev);
  327. kfree(vp_dev);
  328. }
  329. #ifdef CONFIG_PM
  330. static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
  331. {
  332. pci_save_state(pci_dev);
  333. pci_set_power_state(pci_dev, PCI_D3hot);
  334. return 0;
  335. }
  336. static int virtio_pci_resume(struct pci_dev *pci_dev)
  337. {
  338. pci_restore_state(pci_dev);
  339. pci_set_power_state(pci_dev, PCI_D0);
  340. return 0;
  341. }
  342. #endif
  343. static struct pci_driver virtio_pci_driver = {
  344. .name = "virtio-pci",
  345. .id_table = virtio_pci_id_table,
  346. .probe = virtio_pci_probe,
  347. .remove = virtio_pci_remove,
  348. #ifdef CONFIG_PM
  349. .suspend = virtio_pci_suspend,
  350. .resume = virtio_pci_resume,
  351. #endif
  352. };
  353. static int __init virtio_pci_init(void)
  354. {
  355. int err;
  356. err = device_register(&virtio_pci_root);
  357. if (err)
  358. return err;
  359. err = pci_register_driver(&virtio_pci_driver);
  360. if (err)
  361. device_unregister(&virtio_pci_root);
  362. return err;
  363. }
  364. module_init(virtio_pci_init);
  365. static void __exit virtio_pci_exit(void)
  366. {
  367. device_unregister(&virtio_pci_root);
  368. pci_unregister_driver(&virtio_pci_driver);
  369. }
  370. module_exit(virtio_pci_exit);