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.
7 * Copyright IBM Corp. 2007
10 * Anthony Liguori <aliguori@us.ibm.com>
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.
17 #include <linux/module.h>
18 #include <linux/list.h>
19 #include <linux/pci.h>
20 #include <linux/interrupt.h>
21 #include <linux/virtio.h>
22 #include <linux/virtio_config.h>
23 #include <linux/virtio_ring.h>
24 #include <linux/virtio_pci.h>
25 #include <linux/highmem.h>
26 #include <linux/spinlock.h>
28 MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
29 MODULE_DESCRIPTION("virtio-pci");
30 MODULE_LICENSE("GPL");
33 /* Our device structure */
34 struct virtio_pci_device
36 struct virtio_device vdev;
37 struct pci_dev *pci_dev;
39 /* the IO mapping for the PCI config space */
42 /* a list of queues so we can dispatch IRQs */
44 struct list_head virtqueues;
47 struct virtio_pci_vq_info
49 /* the actual virtqueue */
52 /* the number of entries in the queue */
55 /* the index of the queue */
58 /* the virtual address of the ring queue */
61 /* the list node for the virtqueues list */
62 struct list_head node;
65 /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
66 static struct pci_device_id virtio_pci_id_table[] = {
67 { 0x1af4, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
71 MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
73 /* A PCI device has it's own struct device and so does a virtio device so
74 * we create a place for the virtio devices to show up in sysfs. I think it
75 * would make more sense for virtio to not insist on having it's own device. */
76 static struct device virtio_pci_root = {
78 .init_name = "virtio-pci",
81 /* Convert a generic virtio device to our structure */
82 static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
84 return container_of(vdev, struct virtio_pci_device, vdev);
87 /* virtio config->get_features() implementation */
88 static u32 vp_get_features(struct virtio_device *vdev)
90 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
92 /* When someone needs more than 32 feature bits, we'll need to
93 * steal a bit to indicate that the rest are somewhere else. */
94 return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
97 /* virtio config->finalize_features() implementation */
98 static void vp_finalize_features(struct virtio_device *vdev)
100 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
102 /* Give virtio_ring a chance to accept features. */
103 vring_transport_features(vdev);
105 /* We only support 32 feature bits. */
106 BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1);
107 iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES);
110 /* virtio config->get() implementation */
111 static void vp_get(struct virtio_device *vdev, unsigned offset,
112 void *buf, unsigned len)
114 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
115 void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
119 for (i = 0; i < len; i++)
120 ptr[i] = ioread8(ioaddr + i);
123 /* the config->set() implementation. it's symmetric to the config->get()
125 static void vp_set(struct virtio_device *vdev, unsigned offset,
126 const void *buf, unsigned len)
128 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
129 void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
133 for (i = 0; i < len; i++)
134 iowrite8(ptr[i], ioaddr + i);
137 /* config->{get,set}_status() implementations */
138 static u8 vp_get_status(struct virtio_device *vdev)
140 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
141 return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
144 static void vp_set_status(struct virtio_device *vdev, u8 status)
146 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
147 /* We should never be setting status to 0. */
149 iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
152 static void vp_reset(struct virtio_device *vdev)
154 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
155 /* 0 status means a reset. */
156 iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
159 /* the notify function used when creating a virt queue */
160 static void vp_notify(struct virtqueue *vq)
162 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
163 struct virtio_pci_vq_info *info = vq->priv;
165 /* we write the queue's selector into the notification register to
166 * signal the other end */
167 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
170 /* A small wrapper to also acknowledge the interrupt when it's handled.
171 * I really need an EIO hook for the vring so I can ack the interrupt once we
172 * know that we'll be handling the IRQ but before we invoke the callback since
173 * the callback may notify the host which results in the host attempting to
174 * raise an interrupt that we would then mask once we acknowledged the
176 static irqreturn_t vp_interrupt(int irq, void *opaque)
178 struct virtio_pci_device *vp_dev = opaque;
179 struct virtio_pci_vq_info *info;
180 irqreturn_t ret = IRQ_NONE;
184 /* reading the ISR has the effect of also clearing it so it's very
185 * important to save off the value. */
186 isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
188 /* It's definitely not us if the ISR was not high */
192 /* Configuration change? Tell driver if it wants to know. */
193 if (isr & VIRTIO_PCI_ISR_CONFIG) {
194 struct virtio_driver *drv;
195 drv = container_of(vp_dev->vdev.dev.driver,
196 struct virtio_driver, driver);
198 if (drv->config_changed)
199 drv->config_changed(&vp_dev->vdev);
202 spin_lock_irqsave(&vp_dev->lock, flags);
203 list_for_each_entry(info, &vp_dev->virtqueues, node) {
204 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
207 spin_unlock_irqrestore(&vp_dev->lock, flags);
212 /* the config->find_vq() implementation */
213 static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index,
214 void (*callback)(struct virtqueue *vq))
216 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
217 struct virtio_pci_vq_info *info;
218 struct virtqueue *vq;
219 unsigned long flags, size;
223 /* Select the queue we're interested in */
224 iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
226 /* Check if queue is either not available or already active. */
227 num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
228 if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
229 return ERR_PTR(-ENOENT);
231 /* allocate and fill out our structure the represents an active
233 info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
235 return ERR_PTR(-ENOMEM);
237 info->queue_index = index;
240 size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
241 info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
242 if (info->queue == NULL) {
247 /* activate the queue */
248 iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
249 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
251 /* create the vring */
252 vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN,
253 vdev, info->queue, vp_notify, callback);
256 goto out_activate_queue;
262 spin_lock_irqsave(&vp_dev->lock, flags);
263 list_add(&info->node, &vp_dev->virtqueues);
264 spin_unlock_irqrestore(&vp_dev->lock, flags);
269 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
270 free_pages_exact(info->queue, size);
276 /* the config->del_vq() implementation */
277 static void vp_del_vq(struct virtqueue *vq)
279 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
280 struct virtio_pci_vq_info *info = vq->priv;
281 unsigned long flags, size;
283 spin_lock_irqsave(&vp_dev->lock, flags);
284 list_del(&info->node);
285 spin_unlock_irqrestore(&vp_dev->lock, flags);
287 vring_del_virtqueue(vq);
289 /* Select and deactivate the queue */
290 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
291 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
293 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
294 free_pages_exact(info->queue, size);
298 static struct virtio_config_ops virtio_pci_config_ops = {
301 .get_status = vp_get_status,
302 .set_status = vp_set_status,
304 .find_vq = vp_find_vq,
306 .get_features = vp_get_features,
307 .finalize_features = vp_finalize_features,
310 static void virtio_pci_release_dev(struct device *_d)
312 struct virtio_device *dev = container_of(_d, struct virtio_device, dev);
313 struct virtio_pci_device *vp_dev = to_vp_device(dev);
314 struct pci_dev *pci_dev = vp_dev->pci_dev;
316 free_irq(pci_dev->irq, vp_dev);
317 pci_set_drvdata(pci_dev, NULL);
318 pci_iounmap(pci_dev, vp_dev->ioaddr);
319 pci_release_regions(pci_dev);
320 pci_disable_device(pci_dev);
324 /* the PCI probing function */
325 static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
326 const struct pci_device_id *id)
328 struct virtio_pci_device *vp_dev;
331 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
332 if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
335 if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
336 printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
337 VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
341 /* allocate our structure and fill it out */
342 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
346 vp_dev->vdev.dev.parent = &virtio_pci_root;
347 vp_dev->vdev.dev.release = virtio_pci_release_dev;
348 vp_dev->vdev.config = &virtio_pci_config_ops;
349 vp_dev->pci_dev = pci_dev;
350 INIT_LIST_HEAD(&vp_dev->virtqueues);
351 spin_lock_init(&vp_dev->lock);
353 /* enable the device */
354 err = pci_enable_device(pci_dev);
358 err = pci_request_regions(pci_dev, "virtio-pci");
360 goto out_enable_device;
362 vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
363 if (vp_dev->ioaddr == NULL)
364 goto out_req_regions;
366 pci_set_drvdata(pci_dev, vp_dev);
368 /* we use the subsystem vendor/device id as the virtio vendor/device
369 * id. this allows us to use the same PCI vendor/device id for all
370 * virtio devices and to identify the particular virtio driver by
371 * the subsytem ids */
372 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
373 vp_dev->vdev.id.device = pci_dev->subsystem_device;
375 /* register a handler for the queue with the PCI device's interrupt */
376 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
377 dev_name(&vp_dev->vdev.dev), vp_dev);
379 goto out_set_drvdata;
381 /* finally register the virtio device */
382 err = register_virtio_device(&vp_dev->vdev);
389 free_irq(pci_dev->irq, vp_dev);
391 pci_set_drvdata(pci_dev, NULL);
392 pci_iounmap(pci_dev, vp_dev->ioaddr);
394 pci_release_regions(pci_dev);
396 pci_disable_device(pci_dev);
402 static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
404 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
406 unregister_virtio_device(&vp_dev->vdev);
410 static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
412 pci_save_state(pci_dev);
413 pci_set_power_state(pci_dev, PCI_D3hot);
417 static int virtio_pci_resume(struct pci_dev *pci_dev)
419 pci_restore_state(pci_dev);
420 pci_set_power_state(pci_dev, PCI_D0);
425 static struct pci_driver virtio_pci_driver = {
426 .name = "virtio-pci",
427 .id_table = virtio_pci_id_table,
428 .probe = virtio_pci_probe,
429 .remove = virtio_pci_remove,
431 .suspend = virtio_pci_suspend,
432 .resume = virtio_pci_resume,
436 static int __init virtio_pci_init(void)
440 err = device_register(&virtio_pci_root);
444 err = pci_register_driver(&virtio_pci_driver);
446 device_unregister(&virtio_pci_root);
451 module_init(virtio_pci_init);
453 static void __exit virtio_pci_exit(void)
455 device_unregister(&virtio_pci_root);
456 pci_unregister_driver(&virtio_pci_driver);
459 module_exit(virtio_pci_exit);