2 * Virtio PCI driver - legacy device support
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
8 * Copyright Red Hat, Inc. 2014
11 * Anthony Liguori <aliguori@us.ibm.com>
12 * Rusty Russell <rusty@rustcorp.com.au>
13 * Michael S. Tsirkin <mst@redhat.com>
15 * This work is licensed under the terms of the GNU GPL, version 2 or later.
16 * See the COPYING file in the top-level directory.
20 #include "virtio_pci_common.h"
22 /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
23 static const struct pci_device_id virtio_pci_id_table
[] = {
24 { PCI_DEVICE(0x1af4, PCI_ANY_ID
) },
28 MODULE_DEVICE_TABLE(pci
, virtio_pci_id_table
);
30 /* virtio config->get_features() implementation */
31 static u64
vp_get_features(struct virtio_device
*vdev
)
33 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
35 /* When someone needs more than 32 feature bits, we'll need to
36 * steal a bit to indicate that the rest are somewhere else. */
37 return ioread32(vp_dev
->ioaddr
+ VIRTIO_PCI_HOST_FEATURES
);
40 /* virtio config->finalize_features() implementation */
41 static int vp_finalize_features(struct virtio_device
*vdev
)
43 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
45 /* Give virtio_ring a chance to accept features. */
46 vring_transport_features(vdev
);
48 /* Make sure we don't have any features > 32 bits! */
49 BUG_ON((u32
)vdev
->features
!= vdev
->features
);
51 /* We only support 32 feature bits. */
52 iowrite32(vdev
->features
, vp_dev
->ioaddr
+ VIRTIO_PCI_GUEST_FEATURES
);
57 /* virtio config->get() implementation */
58 static void vp_get(struct virtio_device
*vdev
, unsigned offset
,
59 void *buf
, unsigned len
)
61 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
62 void __iomem
*ioaddr
= vp_dev
->ioaddr
+
63 VIRTIO_PCI_CONFIG(vp_dev
) + offset
;
67 for (i
= 0; i
< len
; i
++)
68 ptr
[i
] = ioread8(ioaddr
+ i
);
71 /* the config->set() implementation. it's symmetric to the config->get()
73 static void vp_set(struct virtio_device
*vdev
, unsigned offset
,
74 const void *buf
, unsigned len
)
76 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
77 void __iomem
*ioaddr
= vp_dev
->ioaddr
+
78 VIRTIO_PCI_CONFIG(vp_dev
) + offset
;
82 for (i
= 0; i
< len
; i
++)
83 iowrite8(ptr
[i
], ioaddr
+ i
);
86 /* config->{get,set}_status() implementations */
87 static u8
vp_get_status(struct virtio_device
*vdev
)
89 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
90 return ioread8(vp_dev
->ioaddr
+ VIRTIO_PCI_STATUS
);
93 static void vp_set_status(struct virtio_device
*vdev
, u8 status
)
95 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
96 /* We should never be setting status to 0. */
98 iowrite8(status
, vp_dev
->ioaddr
+ VIRTIO_PCI_STATUS
);
101 static void vp_reset(struct virtio_device
*vdev
)
103 struct virtio_pci_device
*vp_dev
= to_vp_device(vdev
);
104 /* 0 status means a reset. */
105 iowrite8(0, vp_dev
->ioaddr
+ VIRTIO_PCI_STATUS
);
106 /* Flush out the status write, and flush in device writes,
107 * including MSi-X interrupts, if any. */
108 ioread8(vp_dev
->ioaddr
+ VIRTIO_PCI_STATUS
);
109 /* Flush pending VQ/configuration callbacks. */
110 vp_synchronize_vectors(vdev
);
113 static u16
vp_config_vector(struct virtio_pci_device
*vp_dev
, u16 vector
)
115 /* Setup the vector used for configuration events */
116 iowrite16(vector
, vp_dev
->ioaddr
+ VIRTIO_MSI_CONFIG_VECTOR
);
117 /* Verify we had enough resources to assign the vector */
118 /* Will also flush the write out to device */
119 return ioread16(vp_dev
->ioaddr
+ VIRTIO_MSI_CONFIG_VECTOR
);
122 static struct virtqueue
*setup_vq(struct virtio_pci_device
*vp_dev
,
123 struct virtio_pci_vq_info
*info
,
125 void (*callback
)(struct virtqueue
*vq
),
129 struct virtqueue
*vq
;
134 /* Select the queue we're interested in */
135 iowrite16(index
, vp_dev
->ioaddr
+ VIRTIO_PCI_QUEUE_SEL
);
137 /* Check if queue is either not available or already active. */
138 num
= ioread16(vp_dev
->ioaddr
+ VIRTIO_PCI_QUEUE_NUM
);
139 if (!num
|| ioread32(vp_dev
->ioaddr
+ VIRTIO_PCI_QUEUE_PFN
))
140 return ERR_PTR(-ENOENT
);
143 info
->msix_vector
= msix_vec
;
145 size
= PAGE_ALIGN(vring_size(num
, VIRTIO_PCI_VRING_ALIGN
));
146 info
->queue
= alloc_pages_exact(size
, GFP_KERNEL
|__GFP_ZERO
);
147 if (info
->queue
== NULL
)
148 return ERR_PTR(-ENOMEM
);
150 /* activate the queue */
151 iowrite32(virt_to_phys(info
->queue
) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT
,
152 vp_dev
->ioaddr
+ VIRTIO_PCI_QUEUE_PFN
);
154 /* create the vring */
155 vq
= vring_new_virtqueue(index
, info
->num
,
156 VIRTIO_PCI_VRING_ALIGN
, &vp_dev
->vdev
,
157 true, info
->queue
, vp_notify
, callback
, name
);
160 goto out_activate_queue
;
163 vq
->priv
= (void __force
*)vp_dev
->ioaddr
+ VIRTIO_PCI_QUEUE_NOTIFY
;
165 if (msix_vec
!= VIRTIO_MSI_NO_VECTOR
) {
166 iowrite16(msix_vec
, vp_dev
->ioaddr
+ VIRTIO_MSI_QUEUE_VECTOR
);
167 msix_vec
= ioread16(vp_dev
->ioaddr
+ VIRTIO_MSI_QUEUE_VECTOR
);
168 if (msix_vec
== VIRTIO_MSI_NO_VECTOR
) {
177 vring_del_virtqueue(vq
);
179 iowrite32(0, vp_dev
->ioaddr
+ VIRTIO_PCI_QUEUE_PFN
);
180 free_pages_exact(info
->queue
, size
);
184 static void del_vq(struct virtio_pci_vq_info
*info
)
186 struct virtqueue
*vq
= info
->vq
;
187 struct virtio_pci_device
*vp_dev
= to_vp_device(vq
->vdev
);
190 iowrite16(vq
->index
, vp_dev
->ioaddr
+ VIRTIO_PCI_QUEUE_SEL
);
192 if (vp_dev
->msix_enabled
) {
193 iowrite16(VIRTIO_MSI_NO_VECTOR
,
194 vp_dev
->ioaddr
+ VIRTIO_MSI_QUEUE_VECTOR
);
195 /* Flush the write out to device */
196 ioread8(vp_dev
->ioaddr
+ VIRTIO_PCI_ISR
);
199 vring_del_virtqueue(vq
);
201 /* Select and deactivate the queue */
202 iowrite32(0, vp_dev
->ioaddr
+ VIRTIO_PCI_QUEUE_PFN
);
204 size
= PAGE_ALIGN(vring_size(info
->num
, VIRTIO_PCI_VRING_ALIGN
));
205 free_pages_exact(info
->queue
, size
);
208 static const struct virtio_config_ops virtio_pci_config_ops
= {
211 .get_status
= vp_get_status
,
212 .set_status
= vp_set_status
,
214 .find_vqs
= vp_find_vqs
,
215 .del_vqs
= vp_del_vqs
,
216 .get_features
= vp_get_features
,
217 .finalize_features
= vp_finalize_features
,
218 .bus_name
= vp_bus_name
,
219 .set_vq_affinity
= vp_set_vq_affinity
,
222 /* the PCI probing function */
223 static int virtio_pci_probe(struct pci_dev
*pci_dev
,
224 const struct pci_device_id
*id
)
226 struct virtio_pci_device
*vp_dev
;
229 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
230 if (pci_dev
->device
< 0x1000 || pci_dev
->device
> 0x103f)
233 if (pci_dev
->revision
!= VIRTIO_PCI_ABI_VERSION
) {
234 printk(KERN_ERR
"virtio_pci: expected ABI version %d, got %d\n",
235 VIRTIO_PCI_ABI_VERSION
, pci_dev
->revision
);
239 /* allocate our structure and fill it out */
240 vp_dev
= kzalloc(sizeof(struct virtio_pci_device
), GFP_KERNEL
);
244 vp_dev
->vdev
.dev
.parent
= &pci_dev
->dev
;
245 vp_dev
->vdev
.dev
.release
= virtio_pci_release_dev
;
246 vp_dev
->vdev
.config
= &virtio_pci_config_ops
;
247 vp_dev
->pci_dev
= pci_dev
;
248 INIT_LIST_HEAD(&vp_dev
->virtqueues
);
249 spin_lock_init(&vp_dev
->lock
);
251 /* Disable MSI/MSIX to bring device to a known good state. */
252 pci_msi_off(pci_dev
);
254 /* enable the device */
255 err
= pci_enable_device(pci_dev
);
259 err
= pci_request_regions(pci_dev
, "virtio-pci");
261 goto out_enable_device
;
263 vp_dev
->ioaddr
= pci_iomap(pci_dev
, 0, 0);
264 if (vp_dev
->ioaddr
== NULL
) {
266 goto out_req_regions
;
269 vp_dev
->isr
= vp_dev
->ioaddr
+ VIRTIO_PCI_ISR
;
271 pci_set_drvdata(pci_dev
, vp_dev
);
272 pci_set_master(pci_dev
);
274 /* we use the subsystem vendor/device id as the virtio vendor/device
275 * id. this allows us to use the same PCI vendor/device id for all
276 * virtio devices and to identify the particular virtio driver by
277 * the subsystem ids */
278 vp_dev
->vdev
.id
.vendor
= pci_dev
->subsystem_vendor
;
279 vp_dev
->vdev
.id
.device
= pci_dev
->subsystem_device
;
281 vp_dev
->config_vector
= vp_config_vector
;
282 vp_dev
->setup_vq
= setup_vq
;
283 vp_dev
->del_vq
= del_vq
;
285 /* finally register the virtio device */
286 err
= register_virtio_device(&vp_dev
->vdev
);
288 goto out_set_drvdata
;
293 pci_iounmap(pci_dev
, vp_dev
->ioaddr
);
295 pci_release_regions(pci_dev
);
297 pci_disable_device(pci_dev
);
303 static void virtio_pci_remove(struct pci_dev
*pci_dev
)
305 struct virtio_pci_device
*vp_dev
= pci_get_drvdata(pci_dev
);
307 unregister_virtio_device(&vp_dev
->vdev
);
309 vp_del_vqs(&vp_dev
->vdev
);
310 pci_iounmap(pci_dev
, vp_dev
->ioaddr
);
311 pci_release_regions(pci_dev
);
312 pci_disable_device(pci_dev
);
316 static struct pci_driver virtio_pci_driver
= {
317 .name
= "virtio-pci",
318 .id_table
= virtio_pci_id_table
,
319 .probe
= virtio_pci_probe
,
320 .remove
= virtio_pci_remove
,
321 #ifdef CONFIG_PM_SLEEP
322 .driver
.pm
= &virtio_pci_pm_ops
,
326 module_pci_driver(virtio_pci_driver
);