sched: clean up wakeup balancing, move wake_affine()
[wrt350n-kernel.git] / drivers / virtio / virtio_pci.c
blob59a8f73dec730421c3dcff6b52c0c9de59798b43
1 /*
2 * Virtio PCI driver
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
9 * Authors:
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");
31 MODULE_VERSION("1");
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 */
40 void *ioaddr;
42 /* a list of queues so we can dispatch IRQs */
43 spinlock_t lock;
44 struct list_head virtqueues;
47 struct virtio_pci_vq_info
49 /* the actual virtqueue */
50 struct virtqueue *vq;
52 /* the number of entries in the queue */
53 int num;
55 /* the index of the queue */
56 int queue_index;
58 /* the virtual address of the ring queue */
59 void *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 },
68 { 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 = {
77 .parent = NULL,
78 .bus_id = "virtio-pci",
81 /* Unique numbering for devices under the kvm root */
82 static unsigned int dev_index;
84 /* Convert a generic virtio device to our structure */
85 static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
87 return container_of(vdev, struct virtio_pci_device, vdev);
90 /* virtio config->feature() implementation */
91 static bool vp_feature(struct virtio_device *vdev, unsigned bit)
93 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
94 u32 mask;
96 /* Since this function is supposed to have the side effect of
97 * enabling a queried feature, we simulate that by doing a read
98 * from the host feature bitmask and then writing to the guest
99 * feature bitmask */
100 mask = ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
101 if (mask & (1 << bit)) {
102 mask |= (1 << bit);
103 iowrite32(mask, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES);
106 return !!(mask & (1 << bit));
109 /* virtio config->get() implementation */
110 static void vp_get(struct virtio_device *vdev, unsigned offset,
111 void *buf, unsigned len)
113 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
114 void *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
115 u8 *ptr = buf;
116 int i;
118 for (i = 0; i < len; i++)
119 ptr[i] = ioread8(ioaddr + i);
122 /* the config->set() implementation. it's symmetric to the config->get()
123 * implementation */
124 static void vp_set(struct virtio_device *vdev, unsigned offset,
125 const void *buf, unsigned len)
127 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
128 void *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset;
129 const u8 *ptr = buf;
130 int i;
132 for (i = 0; i < len; i++)
133 iowrite8(ptr[i], ioaddr + i);
136 /* config->{get,set}_status() implementations */
137 static u8 vp_get_status(struct virtio_device *vdev)
139 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
140 return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
143 static void vp_set_status(struct virtio_device *vdev, u8 status)
145 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
146 /* We should never be setting status to 0. */
147 BUG_ON(status == 0);
148 return iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
151 static void vp_reset(struct virtio_device *vdev)
153 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
154 /* 0 status means a reset. */
155 return iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
158 /* the notify function used when creating a virt queue */
159 static void vp_notify(struct virtqueue *vq)
161 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
162 struct virtio_pci_vq_info *info = vq->priv;
164 /* we write the queue's selector into the notification register to
165 * signal the other end */
166 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
169 /* A small wrapper to also acknowledge the interrupt when it's handled.
170 * I really need an EIO hook for the vring so I can ack the interrupt once we
171 * know that we'll be handling the IRQ but before we invoke the callback since
172 * the callback may notify the host which results in the host attempting to
173 * raise an interrupt that we would then mask once we acknowledged the
174 * interrupt. */
175 static irqreturn_t vp_interrupt(int irq, void *opaque)
177 struct virtio_pci_device *vp_dev = opaque;
178 struct virtio_pci_vq_info *info;
179 irqreturn_t ret = IRQ_NONE;
180 unsigned long flags;
181 u8 isr;
183 /* reading the ISR has the effect of also clearing it so it's very
184 * important to save off the value. */
185 isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
187 /* It's definitely not us if the ISR was not high */
188 if (!isr)
189 return IRQ_NONE;
191 /* Configuration change? Tell driver if it wants to know. */
192 if (isr & VIRTIO_PCI_ISR_CONFIG) {
193 struct virtio_driver *drv;
194 drv = container_of(vp_dev->vdev.dev.driver,
195 struct virtio_driver, driver);
197 if (drv->config_changed)
198 drv->config_changed(&vp_dev->vdev);
201 spin_lock_irqsave(&vp_dev->lock, flags);
202 list_for_each_entry(info, &vp_dev->virtqueues, node) {
203 if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
204 ret = IRQ_HANDLED;
206 spin_unlock_irqrestore(&vp_dev->lock, flags);
208 return ret;
211 /* the config->find_vq() implementation */
212 static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index,
213 void (*callback)(struct virtqueue *vq))
215 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
216 struct virtio_pci_vq_info *info;
217 struct virtqueue *vq;
218 unsigned long flags;
219 u16 num;
220 int err;
222 /* Select the queue we're interested in */
223 iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
225 /* Check if queue is either not available or already active. */
226 num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
227 if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
228 return ERR_PTR(-ENOENT);
230 /* allocate and fill out our structure the represents an active
231 * queue */
232 info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL);
233 if (!info)
234 return ERR_PTR(-ENOMEM);
236 info->queue_index = index;
237 info->num = num;
239 info->queue = kzalloc(PAGE_ALIGN(vring_size(num,PAGE_SIZE)), GFP_KERNEL);
240 if (info->queue == NULL) {
241 err = -ENOMEM;
242 goto out_info;
245 /* activate the queue */
246 iowrite32(virt_to_phys(info->queue) >> PAGE_SHIFT,
247 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
249 /* create the vring */
250 vq = vring_new_virtqueue(info->num, vdev, info->queue,
251 vp_notify, callback);
252 if (!vq) {
253 err = -ENOMEM;
254 goto out_activate_queue;
257 vq->priv = info;
258 info->vq = vq;
260 spin_lock_irqsave(&vp_dev->lock, flags);
261 list_add(&info->node, &vp_dev->virtqueues);
262 spin_unlock_irqrestore(&vp_dev->lock, flags);
264 return vq;
266 out_activate_queue:
267 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
268 kfree(info->queue);
269 out_info:
270 kfree(info);
271 return ERR_PTR(err);
274 /* the config->del_vq() implementation */
275 static void vp_del_vq(struct virtqueue *vq)
277 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
278 struct virtio_pci_vq_info *info = vq->priv;
279 unsigned long flags;
281 spin_lock_irqsave(&vp_dev->lock, flags);
282 list_del(&info->node);
283 spin_unlock_irqrestore(&vp_dev->lock, flags);
285 vring_del_virtqueue(vq);
287 /* Select and deactivate the queue */
288 iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
289 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
291 kfree(info->queue);
292 kfree(info);
295 static struct virtio_config_ops virtio_pci_config_ops = {
296 .feature = vp_feature,
297 .get = vp_get,
298 .set = vp_set,
299 .get_status = vp_get_status,
300 .set_status = vp_set_status,
301 .reset = vp_reset,
302 .find_vq = vp_find_vq,
303 .del_vq = vp_del_vq,
306 /* the PCI probing function */
307 static int __devinit virtio_pci_probe(struct pci_dev *pci_dev,
308 const struct pci_device_id *id)
310 struct virtio_pci_device *vp_dev;
311 int err;
313 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
314 if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
315 return -ENODEV;
317 if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
318 printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
319 VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
320 return -ENODEV;
323 /* allocate our structure and fill it out */
324 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
325 if (vp_dev == NULL)
326 return -ENOMEM;
328 snprintf(vp_dev->vdev.dev.bus_id, BUS_ID_SIZE, "virtio%d", dev_index);
329 vp_dev->vdev.index = dev_index;
330 dev_index++;
332 vp_dev->vdev.dev.parent = &virtio_pci_root;
333 vp_dev->vdev.config = &virtio_pci_config_ops;
334 vp_dev->pci_dev = pci_dev;
335 INIT_LIST_HEAD(&vp_dev->virtqueues);
336 spin_lock_init(&vp_dev->lock);
338 /* enable the device */
339 err = pci_enable_device(pci_dev);
340 if (err)
341 goto out;
343 err = pci_request_regions(pci_dev, "virtio-pci");
344 if (err)
345 goto out_enable_device;
347 vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
348 if (vp_dev->ioaddr == NULL)
349 goto out_req_regions;
351 pci_set_drvdata(pci_dev, vp_dev);
353 /* we use the subsystem vendor/device id as the virtio vendor/device
354 * id. this allows us to use the same PCI vendor/device id for all
355 * virtio devices and to identify the particular virtio driver by
356 * the subsytem ids */
357 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
358 vp_dev->vdev.id.device = pci_dev->subsystem_device;
360 /* register a handler for the queue with the PCI device's interrupt */
361 err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
362 vp_dev->vdev.dev.bus_id, vp_dev);
363 if (err)
364 goto out_set_drvdata;
366 /* finally register the virtio device */
367 err = register_virtio_device(&vp_dev->vdev);
368 if (err)
369 goto out_req_irq;
371 return 0;
373 out_req_irq:
374 free_irq(pci_dev->irq, vp_dev);
375 out_set_drvdata:
376 pci_set_drvdata(pci_dev, NULL);
377 pci_iounmap(pci_dev, vp_dev->ioaddr);
378 out_req_regions:
379 pci_release_regions(pci_dev);
380 out_enable_device:
381 pci_disable_device(pci_dev);
382 out:
383 kfree(vp_dev);
384 return err;
387 static void __devexit virtio_pci_remove(struct pci_dev *pci_dev)
389 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
391 free_irq(pci_dev->irq, vp_dev);
392 pci_set_drvdata(pci_dev, NULL);
393 pci_iounmap(pci_dev, vp_dev->ioaddr);
394 pci_release_regions(pci_dev);
395 pci_disable_device(pci_dev);
396 kfree(vp_dev);
399 #ifdef CONFIG_PM
400 static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state)
402 pci_save_state(pci_dev);
403 pci_set_power_state(pci_dev, PCI_D3hot);
404 return 0;
407 static int virtio_pci_resume(struct pci_dev *pci_dev)
409 pci_restore_state(pci_dev);
410 pci_set_power_state(pci_dev, PCI_D0);
411 return 0;
413 #endif
415 static struct pci_driver virtio_pci_driver = {
416 .name = "virtio-pci",
417 .id_table = virtio_pci_id_table,
418 .probe = virtio_pci_probe,
419 .remove = virtio_pci_remove,
420 #ifdef CONFIG_PM
421 .suspend = virtio_pci_suspend,
422 .resume = virtio_pci_resume,
423 #endif
426 static int __init virtio_pci_init(void)
428 int err;
430 err = device_register(&virtio_pci_root);
431 if (err)
432 return err;
434 err = pci_register_driver(&virtio_pci_driver);
435 if (err)
436 device_unregister(&virtio_pci_root);
438 return err;
441 module_init(virtio_pci_init);
443 static void __exit virtio_pci_exit(void)
445 device_unregister(&virtio_pci_root);
446 pci_unregister_driver(&virtio_pci_driver);
449 module_exit(virtio_pci_exit);