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 header is BSD licensed so anyone can use the definitions to implement
13 * compatible drivers/servers.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of IBM nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #ifndef _LINUX_VIRTIO_PCI_H
40 #define _LINUX_VIRTIO_PCI_H
42 #include "linux/types.h"
43 #include "linux/virtio_config.h"
45 #ifndef VIRTIO_PCI_NO_LEGACY
47 /* A 32-bit r/o bitmask of the features supported by the host */
48 #define VIRTIO_PCI_HOST_FEATURES 0
50 /* A 32-bit r/w bitmask of features activated by the guest */
51 #define VIRTIO_PCI_GUEST_FEATURES 4
53 /* A 32-bit r/w PFN for the currently selected queue */
54 #define VIRTIO_PCI_QUEUE_PFN 8
56 /* A 16-bit r/o queue size for the currently selected queue */
57 #define VIRTIO_PCI_QUEUE_NUM 12
59 /* A 16-bit r/w queue selector */
60 #define VIRTIO_PCI_QUEUE_SEL 14
62 /* A 16-bit r/w queue notifier */
63 #define VIRTIO_PCI_QUEUE_NOTIFY 16
65 /* An 8-bit device status register. */
66 #define VIRTIO_PCI_STATUS 18
68 /* An 8-bit r/o interrupt status register. Reading the value will return the
69 * current contents of the ISR and will also clear it. This is effectively
70 * a read-and-acknowledge. */
71 #define VIRTIO_PCI_ISR 19
73 /* MSI-X registers: only enabled if MSI-X is enabled. */
74 /* A 16-bit vector for configuration changes. */
75 #define VIRTIO_MSI_CONFIG_VECTOR 20
76 /* A 16-bit vector for selected queue notifications. */
77 #define VIRTIO_MSI_QUEUE_VECTOR 22
79 /* The remaining space is defined by each driver as the per-driver
80 * configuration space */
81 #define VIRTIO_PCI_CONFIG_OFF(msix_enabled) ((msix_enabled) ? 24 : 20)
82 /* Deprecated: please use VIRTIO_PCI_CONFIG_OFF instead */
83 #define VIRTIO_PCI_CONFIG(msix_enabled) VIRTIO_PCI_CONFIG_OFF(msix_enabled)
85 /* How many bits to shift physical queue address written to QUEUE_PFN.
86 * 12 is historical, and due to x86 page size. */
87 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
89 /* The alignment to use between consumer and producer parts of vring.
90 * x86 pagesize again. */
91 #define VIRTIO_PCI_VRING_ALIGN 4096
93 #endif /* VIRTIO_PCI_NO_LEGACY */
95 /* The bit of the ISR which indicates a device configuration change. */
96 #define VIRTIO_PCI_ISR_CONFIG 0x2
97 /* Vector value used to disable MSI for queue */
98 #define VIRTIO_MSI_NO_VECTOR 0xffff
100 /* IDs for different capabilities. Must all exist. */
102 /* Common configuration */
103 #define VIRTIO_PCI_CAP_COMMON_CFG 1
105 #define VIRTIO_PCI_CAP_NOTIFY_CFG 2
107 #define VIRTIO_PCI_CAP_ISR_CFG 3
108 /* Device specific configuration */
109 #define VIRTIO_PCI_CAP_DEVICE_CFG 4
110 /* PCI configuration access */
111 #define VIRTIO_PCI_CAP_PCI_CFG 5
113 /* This is the PCI capability header: */
114 struct virtio_pci_cap
{
115 __u8 cap_vndr
; /* Generic PCI field: PCI_CAPABILITY_ID_VENDOR_SPECIFIC */
116 __u8 cap_next
; /* Generic PCI field: next ptr. */
117 __u8 cap_len
; /* Generic PCI field: capability length */
118 __u8 cfg_type
; /* Identifies the structure. */
119 __u8 bar
; /* Where to find it. */
120 __u8 padding
[3]; /* Pad to full dword. */
121 __le32 offset
; /* Offset within bar. */
122 __le32 length
; /* Length of the structure, in bytes. */
125 struct virtio_pci_notify_cap
{
126 struct virtio_pci_cap cap
;
127 __le32 notify_off_multiplier
; /* Multiplier for queue_notify_off. */
130 /* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
131 struct virtio_pci_common_cfg
{
132 /* About the whole device. */
133 __le32 device_feature_select
; /* read-write */
134 __le32 device_feature
; /* read-only */
135 __le32 guest_feature_select
; /* read-write */
136 __le32 guest_feature
; /* read-write */
137 __le16 msix_config
; /* read-write */
138 __le16 num_queues
; /* read-only */
139 __u8 device_status
; /* read-write */
140 __u8 config_generation
; /* read-only */
142 /* About a specific virtqueue. */
143 __le16 queue_select
; /* read-write */
144 __le16 queue_size
; /* read-write, power of 2. */
145 __le16 queue_msix_vector
; /* read-write */
146 __le16 queue_enable
; /* read-write */
147 __le16 queue_notify_off
; /* read-only */
148 __le32 queue_desc_lo
; /* read-write */
149 __le32 queue_desc_hi
; /* read-write */
150 __le32 queue_avail_lo
; /* read-write */
151 __le32 queue_avail_hi
; /* read-write */
152 __le32 queue_used_lo
; /* read-write */
153 __le32 queue_used_hi
; /* read-write */
156 #define MAX_QUEUES_PER_DEVICE_DEFAULT 8
158 typedef struct virtio_queue_info
160 /* the actual virtqueue */
161 struct virtqueue
*vq
;
162 /* the number of entries in the queue */
164 /* the virtual address of the ring queue */
168 typedef struct virtio_system_ops
{
169 // device register access
170 u8 (*vdev_read_byte
)(ULONG_PTR ulRegister
);
171 u16 (*vdev_read_word
)(ULONG_PTR ulRegister
);
172 u32 (*vdev_read_dword
)(ULONG_PTR ulRegister
);
173 void (*vdev_write_byte
)(ULONG_PTR ulRegister
, u8 bValue
);
174 void (*vdev_write_word
)(ULONG_PTR ulRegister
, u16 wValue
);
175 void (*vdev_write_dword
)(ULONG_PTR ulRegister
, u32 ulValue
);
178 void *(*mem_alloc_contiguous_pages
)(void *context
, size_t size
);
179 void (*mem_free_contiguous_pages
)(void *context
, void *virt
);
180 ULONGLONG (*mem_get_physical_address
)(void *context
, void *virt
);
181 void *(*mem_alloc_nonpaged_block
)(void *context
, size_t size
);
182 void (*mem_free_nonpaged_block
)(void *context
, void *addr
);
184 // PCI config space access
185 int (*pci_read_config_byte
)(void *context
, int where
, u8
*bVal
);
186 int (*pci_read_config_word
)(void *context
, int where
, u16
*wVal
);
187 int (*pci_read_config_dword
)(void *context
, int where
, u32
*dwVal
);
189 // PCI resource handling
190 size_t (*pci_get_resource_len
)(void *context
, int bar
);
191 void *(*pci_map_address_range
)(void *context
, int bar
, size_t offset
, size_t maxlen
);
194 u16 (*vdev_get_msix_vector
)(void *context
, int queue
);
195 void (*vdev_sleep
)(void *context
, unsigned int msecs
);
198 struct virtio_device
;
199 typedef struct virtio_device VirtIODevice
;
201 struct virtio_device_ops
203 // read/write device config and read config generation counter
204 void (*get_config
)(VirtIODevice
*vdev
, unsigned offset
, void *buf
, unsigned len
);
205 void (*set_config
)(VirtIODevice
*vdev
, unsigned offset
, const void *buf
, unsigned len
);
206 u32 (*get_config_generation
)(VirtIODevice
*vdev
);
208 // read/write device status byte and reset the device
209 u8 (*get_status
)(VirtIODevice
*vdev
);
210 void (*set_status
)(VirtIODevice
*vdev
, u8 status
);
211 void (*reset
)(VirtIODevice
*vdev
);
213 // get/set device feature bits
214 u64 (*get_features
)(VirtIODevice
*vdev
);
215 NTSTATUS (*set_features
)(VirtIODevice
*vdev
, u64 features
);
217 // set config/queue MSI interrupt vector, returns the new vector
218 u16 (*set_config_vector
)(VirtIODevice
*vdev
, u16 vector
);
219 u16 (*set_queue_vector
)(struct virtqueue
*vq
, u16 vector
);
221 // query virtual queue size and memory requirements
222 NTSTATUS (*query_queue_alloc
)(VirtIODevice
*vdev
,
223 unsigned index
, unsigned short *pNumEntries
,
224 unsigned long *pRingSize
,
225 unsigned long *pHeapSize
);
227 // allocate and initialize a queue
228 NTSTATUS (*setup_queue
)(struct virtqueue
**queue
,
229 VirtIODevice
*vdev
, VirtIOQueueInfo
*info
,
230 unsigned idx
, u16 msix_vec
);
232 // tear down and deallocate a queue
233 void (*delete_queue
)(VirtIOQueueInfo
*info
);
238 // the I/O port BAR of the PCI device (legacy virtio devices only)
241 // true if the device uses MSI interrupts
244 // true if the VIRTIO_RING_F_EVENT_IDX feature flag has been negotiated
245 bool event_suppression_enabled
;
247 // true if the VIRTIO_F_RING_PACKED feature flag has been negotiated
250 // internal device operations, implemented separately for legacy and modern
251 const struct virtio_device_ops
*device
;
253 // external callbacks implemented separately by different driver model drivers
254 const struct virtio_system_ops
*system
;
256 // opaque context value passed as first argument to virtio_system_ops callbacks
259 // the ISR status field, reading causes the device to de-assert an interrupt
262 // modern virtio device capabilities and related state
263 volatile struct virtio_pci_common_cfg
*common
;
264 volatile unsigned char *config
;
265 volatile unsigned char *notify_base
;
267 u32 notify_offset_multiplier
;
272 // maximum number of virtqueues that fit in the memory block pointed to by info
275 // points to inline_info if not more than MAX_QUEUES_PER_DEVICE_DEFAULT queues
276 // are used, or to an external allocation otherwise
277 VirtIOQueueInfo
*info
;
278 VirtIOQueueInfo inline_info
[MAX_QUEUES_PER_DEVICE_DEFAULT
];
281 /* Driver API: device init and shutdown
282 * DeviceContext is a driver defined opaque value which will be passed to driver
283 * supplied callbacks described in pSystemOps. pSystemOps must be non-NULL and all
284 * its fields must be non-NULL. msix_used is true if and only if the device is
285 * configured with MSI support.
287 NTSTATUS
virtio_device_initialize(VirtIODevice
*vdev
,
288 const VirtIOSystemOps
*pSystemOps
,
291 void virtio_device_shutdown(VirtIODevice
*vdev
);
293 /* Driver API: device status manipulation
294 * virtio_set_status should not be called by new drivers. Device status should only
295 * be getting its bits set with virtio_add_status and reset all back to 0 with
296 * virtio_device_reset. virtio_device_ready is a special version of virtio_add_status
297 * which adds the VIRTIO_CONFIG_S_DRIVER_OK status bit.
299 u8
virtio_get_status(VirtIODevice
*vdev
);
300 void virtio_set_status(VirtIODevice
*vdev
, u8 status
);
301 void virtio_add_status(VirtIODevice
*vdev
, u8 status
);
303 void virtio_device_reset(VirtIODevice
*vdev
);
304 void virtio_device_ready(VirtIODevice
*vdev
);
306 /* Driver API: device feature bitmap manipulation
307 * Features passed to virtio_set_features should be a subset of features offered by
308 * the device as returned from virtio_get_features. virtio_set_features sets the
309 * VIRTIO_CONFIG_S_FEATURES_OK status bit if it is supported by the device.
311 #define virtio_is_feature_enabled(FeaturesList, Feature) (!!((FeaturesList) & (1ULL << (Feature))))
312 #define virtio_feature_enable(FeaturesList, Feature) ((FeaturesList) |= (1ULL << (Feature)))
313 #define virtio_feature_disable(FeaturesList, Feature) ((FeaturesList) &= ~(1ULL << (Feature)))
315 u64
virtio_get_features(VirtIODevice
*dev
);
316 NTSTATUS
virtio_set_features(VirtIODevice
*vdev
, u64 features
);
318 /* Driver API: device configuration access
319 * Both virtio_get_config and virtio_set_config support arbitrary values of the len
320 * parameter. Config items of length 1, 2, and 4 are read/written using one access,
321 * length 8 is broken down to two 4 bytes accesses, and any other length is read or
322 * written byte by byte.
324 void virtio_get_config(VirtIODevice
*vdev
, unsigned offset
,
325 void *buf
, unsigned len
);
326 void virtio_set_config(VirtIODevice
*vdev
, unsigned offset
,
327 void *buf
, unsigned len
);
329 /* Driver API: virtqueue setup
330 * virtio_reserve_queue_memory makes VirtioLib reserve memory for its virtqueue
331 * bookkeeping. Drivers should call this function if they intend to set up queues
332 * one by one with virtio_find_queue. virtio_find_queues (plural) internally takes
333 * care of the reservation and virtio_reserve_queue_memory need not be called.
334 * Note that in addition to queue interrupt vectors, virtio_find_queues also sets
335 * up the device config vector as a convenience.
336 * Drivers should treat the returned struct virtqueue pointers as opaque handles.
338 NTSTATUS
virtio_query_queue_allocation(VirtIODevice
*vdev
, unsigned index
,
339 unsigned short *pNumEntries
,
340 unsigned long *pRingSize
,
341 unsigned long *pHeapSize
);
343 NTSTATUS
virtio_reserve_queue_memory(VirtIODevice
*vdev
, unsigned nvqs
);
345 NTSTATUS
virtio_find_queue(VirtIODevice
*vdev
, unsigned index
,
346 struct virtqueue
**vq
);
347 NTSTATUS
virtio_find_queues(VirtIODevice
*vdev
, unsigned nvqs
,
348 struct virtqueue
*vqs
[]);
350 /* Driver API: virtqueue shutdown
351 * The device must be reset and re-initialized to re-setup queues after they have
354 void virtio_delete_queue(struct virtqueue
*vq
);
355 void virtio_delete_queues(VirtIODevice
*vdev
);
357 /* Driver API: virtqueue query and manipulation
358 * virtio_get_queue_descriptor_size
359 * is useful in situations where the driver has to prepare for the memory allocation
360 * performed by virtio_reserve_queue_memory beforehand.
363 u32
virtio_get_queue_size(struct virtqueue
*vq
);
364 unsigned long virtio_get_indirect_page_capacity();
366 static ULONG FORCEINLINE
virtio_get_queue_descriptor_size()
368 return sizeof(VirtIOQueueInfo
);
371 /* Driver API: interrupt handling
372 * virtio_set_config_vector and virtio_set_queue_vector set the MSI vector used for
373 * device configuration interrupt and queue interrupt, respectively. The driver may
374 * choose to either return the vector from the vdev_get_msix_vector callback (called
375 * as part of queue setup) or call these functions later. Note that setting the vector
376 * may fail which is indicated by the return value of VIRTIO_MSI_NO_VECTOR.
377 * virtio_read_isr_status returns the value of the ISR status register, note that it
378 * is not idempotent, calling the function makes the device de-assert the interrupt.
380 u16
virtio_set_config_vector(VirtIODevice
*vdev
, u16 vector
);
381 u16
virtio_set_queue_vector(struct virtqueue
*vq
, u16 vector
);
383 u8
virtio_read_isr_status(VirtIODevice
*vdev
);
385 /* Driver API: miscellaneous helpers
386 * virtio_get_bar_index returns the corresponding BAR index given its physical address.
387 * This tends to be useful to all drivers since Windows doesn't provide reliable BAR
388 * indices as part of resource enumeration. The function returns -1 on failure.
390 int virtio_get_bar_index(PPCI_COMMON_HEADER pPCIHeader
, PHYSICAL_ADDRESS BasePA
);