Commit | Line | Data |
---|---|---|
3343660d AL |
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 | ||
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> | |
27 | ||
28 | MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>"); | |
29 | MODULE_DESCRIPTION("virtio-pci"); | |
30 | MODULE_LICENSE("GPL"); | |
31 | MODULE_VERSION("1"); | |
32 | ||
33 | /* Our device structure */ | |
34 | struct virtio_pci_device | |
35 | { | |
36 | struct virtio_device vdev; | |
37 | struct pci_dev *pci_dev; | |
38 | ||
39 | /* the IO mapping for the PCI config space */ | |
97968358 | 40 | void __iomem *ioaddr; |
3343660d AL |
41 | |
42 | /* a list of queues so we can dispatch IRQs */ | |
43 | spinlock_t lock; | |
44 | struct list_head virtqueues; | |
45 | }; | |
46 | ||
47 | struct virtio_pci_vq_info | |
48 | { | |
49 | /* the actual virtqueue */ | |
50 | struct virtqueue *vq; | |
51 | ||
52 | /* the number of entries in the queue */ | |
53 | int num; | |
54 | ||
55 | /* the index of the queue */ | |
56 | int queue_index; | |
57 | ||
58 | /* the virtual address of the ring queue */ | |
59 | void *queue; | |
60 | ||
61 | /* the list node for the virtqueues list */ | |
62 | struct list_head node; | |
63 | }; | |
64 | ||
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 }, | |
69 | }; | |
70 | ||
71 | MODULE_DEVICE_TABLE(pci, virtio_pci_id_table); | |
72 | ||
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", | |
79 | }; | |
80 | ||
3343660d AL |
81 | /* Convert a generic virtio device to our structure */ |
82 | static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev) | |
83 | { | |
84 | return container_of(vdev, struct virtio_pci_device, vdev); | |
85 | } | |
86 | ||
c45a6816 RR |
87 | /* virtio config->get_features() implementation */ |
88 | static u32 vp_get_features(struct virtio_device *vdev) | |
89 | { | |
90 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
91 | ||
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); | |
95 | } | |
96 | ||
97 | /* virtio config->set_features() implementation */ | |
98 | static void vp_set_features(struct virtio_device *vdev, u32 features) | |
3343660d AL |
99 | { |
100 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
3343660d | 101 | |
c45a6816 | 102 | iowrite32(features, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES); |
3343660d AL |
103 | } |
104 | ||
105 | /* virtio config->get() implementation */ | |
106 | static void vp_get(struct virtio_device *vdev, unsigned offset, | |
107 | void *buf, unsigned len) | |
108 | { | |
109 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
97968358 | 110 | void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset; |
3343660d AL |
111 | u8 *ptr = buf; |
112 | int i; | |
113 | ||
114 | for (i = 0; i < len; i++) | |
115 | ptr[i] = ioread8(ioaddr + i); | |
116 | } | |
117 | ||
118 | /* the config->set() implementation. it's symmetric to the config->get() | |
119 | * implementation */ | |
120 | static void vp_set(struct virtio_device *vdev, unsigned offset, | |
121 | const void *buf, unsigned len) | |
122 | { | |
123 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
97968358 | 124 | void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset; |
3343660d AL |
125 | const u8 *ptr = buf; |
126 | int i; | |
127 | ||
128 | for (i = 0; i < len; i++) | |
129 | iowrite8(ptr[i], ioaddr + i); | |
130 | } | |
131 | ||
132 | /* config->{get,set}_status() implementations */ | |
133 | static u8 vp_get_status(struct virtio_device *vdev) | |
134 | { | |
135 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
136 | return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS); | |
137 | } | |
138 | ||
139 | static void vp_set_status(struct virtio_device *vdev, u8 status) | |
140 | { | |
141 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
142 | /* We should never be setting status to 0. */ | |
143 | BUG_ON(status == 0); | |
597d56e4 | 144 | iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
3343660d AL |
145 | } |
146 | ||
147 | static void vp_reset(struct virtio_device *vdev) | |
148 | { | |
149 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
150 | /* 0 status means a reset. */ | |
597d56e4 | 151 | iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
3343660d AL |
152 | } |
153 | ||
154 | /* the notify function used when creating a virt queue */ | |
155 | static void vp_notify(struct virtqueue *vq) | |
156 | { | |
157 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); | |
158 | struct virtio_pci_vq_info *info = vq->priv; | |
159 | ||
160 | /* we write the queue's selector into the notification register to | |
161 | * signal the other end */ | |
162 | iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY); | |
163 | } | |
164 | ||
165 | /* A small wrapper to also acknowledge the interrupt when it's handled. | |
166 | * I really need an EIO hook for the vring so I can ack the interrupt once we | |
167 | * know that we'll be handling the IRQ but before we invoke the callback since | |
168 | * the callback may notify the host which results in the host attempting to | |
169 | * raise an interrupt that we would then mask once we acknowledged the | |
170 | * interrupt. */ | |
171 | static irqreturn_t vp_interrupt(int irq, void *opaque) | |
172 | { | |
173 | struct virtio_pci_device *vp_dev = opaque; | |
174 | struct virtio_pci_vq_info *info; | |
175 | irqreturn_t ret = IRQ_NONE; | |
27ebe308 | 176 | unsigned long flags; |
3343660d AL |
177 | u8 isr; |
178 | ||
179 | /* reading the ISR has the effect of also clearing it so it's very | |
180 | * important to save off the value. */ | |
181 | isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR); | |
182 | ||
183 | /* It's definitely not us if the ISR was not high */ | |
184 | if (!isr) | |
185 | return IRQ_NONE; | |
186 | ||
187 | /* Configuration change? Tell driver if it wants to know. */ | |
188 | if (isr & VIRTIO_PCI_ISR_CONFIG) { | |
189 | struct virtio_driver *drv; | |
190 | drv = container_of(vp_dev->vdev.dev.driver, | |
191 | struct virtio_driver, driver); | |
192 | ||
193 | if (drv->config_changed) | |
194 | drv->config_changed(&vp_dev->vdev); | |
195 | } | |
196 | ||
27ebe308 | 197 | spin_lock_irqsave(&vp_dev->lock, flags); |
3343660d AL |
198 | list_for_each_entry(info, &vp_dev->virtqueues, node) { |
199 | if (vring_interrupt(irq, info->vq) == IRQ_HANDLED) | |
200 | ret = IRQ_HANDLED; | |
201 | } | |
27ebe308 | 202 | spin_unlock_irqrestore(&vp_dev->lock, flags); |
3343660d AL |
203 | |
204 | return ret; | |
205 | } | |
206 | ||
207 | /* the config->find_vq() implementation */ | |
208 | static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index, | |
209 | void (*callback)(struct virtqueue *vq)) | |
210 | { | |
211 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); | |
212 | struct virtio_pci_vq_info *info; | |
213 | struct virtqueue *vq; | |
27ebe308 | 214 | unsigned long flags; |
3343660d AL |
215 | u16 num; |
216 | int err; | |
217 | ||
218 | /* Select the queue we're interested in */ | |
219 | iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); | |
220 | ||
221 | /* Check if queue is either not available or already active. */ | |
222 | num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM); | |
223 | if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN)) | |
224 | return ERR_PTR(-ENOENT); | |
225 | ||
226 | /* allocate and fill out our structure the represents an active | |
227 | * queue */ | |
228 | info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL); | |
229 | if (!info) | |
230 | return ERR_PTR(-ENOMEM); | |
231 | ||
232 | info->queue_index = index; | |
233 | info->num = num; | |
234 | ||
235 | info->queue = kzalloc(PAGE_ALIGN(vring_size(num,PAGE_SIZE)), GFP_KERNEL); | |
236 | if (info->queue == NULL) { | |
237 | err = -ENOMEM; | |
238 | goto out_info; | |
239 | } | |
240 | ||
241 | /* activate the queue */ | |
242 | iowrite32(virt_to_phys(info->queue) >> PAGE_SHIFT, | |
243 | vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); | |
244 | ||
245 | /* create the vring */ | |
246 | vq = vring_new_virtqueue(info->num, vdev, info->queue, | |
247 | vp_notify, callback); | |
248 | if (!vq) { | |
249 | err = -ENOMEM; | |
250 | goto out_activate_queue; | |
251 | } | |
252 | ||
253 | vq->priv = info; | |
254 | info->vq = vq; | |
255 | ||
27ebe308 | 256 | spin_lock_irqsave(&vp_dev->lock, flags); |
3343660d | 257 | list_add(&info->node, &vp_dev->virtqueues); |
27ebe308 | 258 | spin_unlock_irqrestore(&vp_dev->lock, flags); |
3343660d AL |
259 | |
260 | return vq; | |
261 | ||
262 | out_activate_queue: | |
263 | iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); | |
264 | kfree(info->queue); | |
265 | out_info: | |
266 | kfree(info); | |
267 | return ERR_PTR(err); | |
268 | } | |
269 | ||
270 | /* the config->del_vq() implementation */ | |
271 | static void vp_del_vq(struct virtqueue *vq) | |
272 | { | |
273 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); | |
274 | struct virtio_pci_vq_info *info = vq->priv; | |
27ebe308 | 275 | unsigned long flags; |
3343660d | 276 | |
27ebe308 | 277 | spin_lock_irqsave(&vp_dev->lock, flags); |
3343660d | 278 | list_del(&info->node); |
27ebe308 | 279 | spin_unlock_irqrestore(&vp_dev->lock, flags); |
3343660d AL |
280 | |
281 | vring_del_virtqueue(vq); | |
282 | ||
283 | /* Select and deactivate the queue */ | |
284 | iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); | |
285 | iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); | |
286 | ||
287 | kfree(info->queue); | |
288 | kfree(info); | |
289 | } | |
290 | ||
291 | static struct virtio_config_ops virtio_pci_config_ops = { | |
3343660d AL |
292 | .get = vp_get, |
293 | .set = vp_set, | |
294 | .get_status = vp_get_status, | |
295 | .set_status = vp_set_status, | |
296 | .reset = vp_reset, | |
297 | .find_vq = vp_find_vq, | |
298 | .del_vq = vp_del_vq, | |
c45a6816 RR |
299 | .get_features = vp_get_features, |
300 | .set_features = vp_set_features, | |
3343660d AL |
301 | }; |
302 | ||
303 | /* the PCI probing function */ | |
304 | static int __devinit virtio_pci_probe(struct pci_dev *pci_dev, | |
305 | const struct pci_device_id *id) | |
306 | { | |
307 | struct virtio_pci_device *vp_dev; | |
308 | int err; | |
309 | ||
310 | /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */ | |
311 | if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f) | |
312 | return -ENODEV; | |
313 | ||
55a7c066 AL |
314 | if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) { |
315 | printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n", | |
316 | VIRTIO_PCI_ABI_VERSION, pci_dev->revision); | |
317 | return -ENODEV; | |
318 | } | |
319 | ||
3343660d AL |
320 | /* allocate our structure and fill it out */ |
321 | vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL); | |
322 | if (vp_dev == NULL) | |
323 | return -ENOMEM; | |
324 | ||
3343660d AL |
325 | vp_dev->vdev.dev.parent = &virtio_pci_root; |
326 | vp_dev->vdev.config = &virtio_pci_config_ops; | |
327 | vp_dev->pci_dev = pci_dev; | |
328 | INIT_LIST_HEAD(&vp_dev->virtqueues); | |
329 | spin_lock_init(&vp_dev->lock); | |
330 | ||
331 | /* enable the device */ | |
332 | err = pci_enable_device(pci_dev); | |
333 | if (err) | |
334 | goto out; | |
335 | ||
336 | err = pci_request_regions(pci_dev, "virtio-pci"); | |
337 | if (err) | |
338 | goto out_enable_device; | |
339 | ||
340 | vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0); | |
341 | if (vp_dev->ioaddr == NULL) | |
342 | goto out_req_regions; | |
343 | ||
344 | pci_set_drvdata(pci_dev, vp_dev); | |
345 | ||
346 | /* we use the subsystem vendor/device id as the virtio vendor/device | |
347 | * id. this allows us to use the same PCI vendor/device id for all | |
348 | * virtio devices and to identify the particular virtio driver by | |
349 | * the subsytem ids */ | |
350 | vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor; | |
351 | vp_dev->vdev.id.device = pci_dev->subsystem_device; | |
352 | ||
353 | /* register a handler for the queue with the PCI device's interrupt */ | |
354 | err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED, | |
355 | vp_dev->vdev.dev.bus_id, vp_dev); | |
356 | if (err) | |
357 | goto out_set_drvdata; | |
358 | ||
359 | /* finally register the virtio device */ | |
360 | err = register_virtio_device(&vp_dev->vdev); | |
361 | if (err) | |
362 | goto out_req_irq; | |
363 | ||
364 | return 0; | |
365 | ||
366 | out_req_irq: | |
367 | free_irq(pci_dev->irq, vp_dev); | |
368 | out_set_drvdata: | |
369 | pci_set_drvdata(pci_dev, NULL); | |
370 | pci_iounmap(pci_dev, vp_dev->ioaddr); | |
371 | out_req_regions: | |
372 | pci_release_regions(pci_dev); | |
373 | out_enable_device: | |
374 | pci_disable_device(pci_dev); | |
375 | out: | |
376 | kfree(vp_dev); | |
377 | return err; | |
378 | } | |
379 | ||
380 | static void __devexit virtio_pci_remove(struct pci_dev *pci_dev) | |
381 | { | |
382 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); | |
383 | ||
bd6c2690 | 384 | unregister_virtio_device(&vp_dev->vdev); |
3343660d AL |
385 | free_irq(pci_dev->irq, vp_dev); |
386 | pci_set_drvdata(pci_dev, NULL); | |
387 | pci_iounmap(pci_dev, vp_dev->ioaddr); | |
388 | pci_release_regions(pci_dev); | |
389 | pci_disable_device(pci_dev); | |
390 | kfree(vp_dev); | |
391 | } | |
392 | ||
393 | #ifdef CONFIG_PM | |
394 | static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state) | |
395 | { | |
396 | pci_save_state(pci_dev); | |
397 | pci_set_power_state(pci_dev, PCI_D3hot); | |
398 | return 0; | |
399 | } | |
400 | ||
401 | static int virtio_pci_resume(struct pci_dev *pci_dev) | |
402 | { | |
403 | pci_restore_state(pci_dev); | |
404 | pci_set_power_state(pci_dev, PCI_D0); | |
405 | return 0; | |
406 | } | |
407 | #endif | |
408 | ||
409 | static struct pci_driver virtio_pci_driver = { | |
410 | .name = "virtio-pci", | |
411 | .id_table = virtio_pci_id_table, | |
412 | .probe = virtio_pci_probe, | |
413 | .remove = virtio_pci_remove, | |
414 | #ifdef CONFIG_PM | |
415 | .suspend = virtio_pci_suspend, | |
416 | .resume = virtio_pci_resume, | |
417 | #endif | |
418 | }; | |
419 | ||
420 | static int __init virtio_pci_init(void) | |
421 | { | |
422 | int err; | |
423 | ||
424 | err = device_register(&virtio_pci_root); | |
425 | if (err) | |
426 | return err; | |
427 | ||
428 | err = pci_register_driver(&virtio_pci_driver); | |
429 | if (err) | |
430 | device_unregister(&virtio_pci_root); | |
431 | ||
432 | return err; | |
433 | } | |
434 | ||
435 | module_init(virtio_pci_init); | |
436 | ||
437 | static void __exit virtio_pci_exit(void) | |
438 | { | |
439 | device_unregister(&virtio_pci_root); | |
440 | pci_unregister_driver(&virtio_pci_driver); | |
441 | } | |
442 | ||
443 | module_exit(virtio_pci_exit); |