]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Include/Protocol/VirtioDevice.h
OvmfPkg/VirtioLib: take VirtIo instance in VirtioRingInit/VirtioRingUninit
[mirror_edk2.git] / OvmfPkg / Include / Protocol / VirtioDevice.h
CommitLineData
fc4d1ce5
OM
1/** @file\r
2 Virtio Device\r
3\r
4 DISCLAIMER: the VIRTIO_DEVICE_PROTOCOL introduced here is a work in progress,\r
5 and should not be used outside of the EDK II tree.\r
6\r
7 Copyright (c) 2013, ARM Ltd. All rights reserved.<BR>\r
87811526 8 Copyright (c) 2017, AMD Inc, All rights reserved.<BR>\r
fc4d1ce5
OM
9\r
10 This program and the accompanying materials are licensed and made available\r
11 under the terms and conditions of the BSD License which accompanies this\r
12 distribution. The full text of the license may be found at\r
13 http://opensource.org/licenses/bsd-license.php\r
14\r
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
16 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
17\r
18**/\r
19\r
20#ifndef __VIRTIO_DEVICE_H__\r
21#define __VIRTIO_DEVICE_H__\r
22\r
07af4eee
LE
23#include <IndustryStandard/Virtio.h>\r
24\r
a136bc3c
BS
25//\r
26// VirtIo Specification Revision: Major[31:24].Minor[23:16].Revision[15:0]\r
27//\r
fc4d1ce5
OM
28#define VIRTIO_SPEC_REVISION(major,minor,revision) \\r
29 ((((major) & 0xFF) << 24) | (((minor) & 0xFF) << 16) | ((revision) & 0xFFFF))\r
30\r
31#define VIRTIO_DEVICE_PROTOCOL_GUID { \\r
32 0xfa920010, 0x6785, 0x4941, {0xb6, 0xec, 0x49, 0x8c, 0x57, 0x9f, 0x16, 0x0a }\\r
33 }\r
34\r
35typedef struct _VIRTIO_DEVICE_PROTOCOL VIRTIO_DEVICE_PROTOCOL;\r
36\r
87811526
BS
37//\r
38// VIRTIO Operation for VIRTIO_MAP_SHARED\r
39//\r
40typedef enum {\r
41 //\r
42 // A read operation from system memory by a bus master\r
43 //\r
44 VirtioOperationBusMasterRead,\r
45 //\r
46 // A write operation to system memory by a bus master\r
47 //\r
48 VirtioOperationBusMasterWrite,\r
49 //\r
50 // Provides both read and write access to system memory by both the\r
51 // processor and a bus master\r
52 //\r
53 VirtioOperationBusMasterCommonBuffer,\r
54} VIRTIO_MAP_OPERATION;\r
55\r
fc4d1ce5
OM
56/**\r
57\r
58 Read a word from the device-specific I/O region of the Virtio Header.\r
59\r
60 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
61\r
62 @param[in] FieldOffset Source offset.\r
63\r
64 @param[in] FieldSize Source field size in bytes, must be in {1, 2, 4, 8}.\r
65\r
66 @param[in] BufferSize Number of bytes available in the target buffer. Must\r
67 equal FieldSize.\r
68\r
69 @param[out] Buffer Target buffer.\r
70\r
71 @retval EFI_SUCCESS The data was read successfully.\r
72 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
73 provided address offset and read size.\r
74 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
75 lack of resources.\r
76 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
77\r
78**/\r
79typedef\r
80EFI_STATUS\r
81(EFIAPI *VIRTIO_DEVICE_READ) (\r
82 IN VIRTIO_DEVICE_PROTOCOL *This,\r
83 IN UINTN FieldOffset,\r
84 IN UINTN FieldSize,\r
85 IN UINTN BufferSize,\r
86 OUT VOID *Buffer\r
87 );\r
88\r
89/**\r
90\r
91 Write a word to the device-specific I/O region of the Virtio Header.\r
92\r
93 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
94\r
95 @param[in] FieldOffset Destination offset.\r
96\r
97 @param[in] FieldSize Destination field size in bytes,\r
98 must be in {1, 2, 4, 8}.\r
99\r
100 @param[out] Value Value to write.\r
101\r
102 @retval EFI_SUCCESS The data was written successfully.\r
103 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
104 provided address offset and write size.\r
105 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
106 lack of resources.\r
107 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
108\r
109**/\r
110typedef\r
111EFI_STATUS\r
112(EFIAPI *VIRTIO_DEVICE_WRITE) (\r
113 IN VIRTIO_DEVICE_PROTOCOL *This,\r
114 IN UINTN FieldOffset,\r
115 IN UINTN FieldSize,\r
116 IN UINT64 Value\r
117 );\r
118\r
119/**\r
120 Read the device features field from the Virtio Header.\r
121\r
122 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
123\r
bc8fde6f 124 @param[out] DeviceFeatures The device features field.\r
fc4d1ce5
OM
125\r
126 @retval EFI_SUCCESS The data was read successfully.\r
127 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
128 provided address offset and read size.\r
129 @retval EFI_INVALID_PARAMETER DeviceFeatures is NULL\r
130**/\r
131typedef\r
132EFI_STATUS\r
133(EFIAPI *VIRTIO_GET_DEVICE_FEATURES) (\r
134 IN VIRTIO_DEVICE_PROTOCOL *This,\r
bc8fde6f 135 OUT UINT64 *DeviceFeatures\r
fc4d1ce5
OM
136 );\r
137\r
138/**\r
139 Write the guest features field in the Virtio Header.\r
140\r
141 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
142\r
bc8fde6f 143 @param[in] Features The guest features field\r
fc4d1ce5
OM
144\r
145**/\r
146typedef\r
147EFI_STATUS\r
148(EFIAPI *VIRTIO_SET_GUEST_FEATURES) (\r
149 IN VIRTIO_DEVICE_PROTOCOL *This,\r
bc8fde6f 150 IN UINT64 Features\r
fc4d1ce5
OM
151 );\r
152\r
fc4d1ce5 153/**\r
07af4eee 154 Write the queue address field(s) in the Virtio Header.\r
fc4d1ce5
OM
155\r
156 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
157\r
07af4eee
LE
158 @param[in] Ring The initialized VRING object to take the\r
159 addresses from.\r
fc4d1ce5
OM
160\r
161 @retval EFI_SUCCESS The data was written successfully.\r
162 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
163 provided address offset and write size.\r
164**/\r
165typedef\r
166EFI_STATUS\r
167(EFIAPI *VIRTIO_SET_QUEUE_ADDRESS) (\r
168 IN VIRTIO_DEVICE_PROTOCOL *This,\r
07af4eee 169 IN VRING *Ring\r
fc4d1ce5
OM
170 );\r
171\r
172/**\r
173\r
174 Write the queue select field in the Virtio Header.\r
175\r
176 Writing to the queue select field sets the index of the queue to which\r
177 operations such as SetQueueAlign and GetQueueNumMax apply.\r
178\r
179 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
180\r
181 @param[in] Index The index of the queue to select\r
182\r
183 @retval EFI_SUCCESS The data was written successfully.\r
184 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
185 provided address offset and write size.\r
186**/\r
187typedef\r
188EFI_STATUS\r
189(EFIAPI *VIRTIO_SET_QUEUE_SEL) (\r
190 IN VIRTIO_DEVICE_PROTOCOL *This,\r
191 IN UINT16 Index\r
192 );\r
193\r
194/**\r
195\r
196 Write the queue notify field in the Virtio Header.\r
197\r
198 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
199\r
200 @param[in] Address The 32-bit Queue Notify field\r
201\r
202 @retval EFI_SUCCESS The data was written successfully.\r
203 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
204 provided address offset and write size.\r
205**/\r
206typedef\r
207EFI_STATUS\r
208(EFIAPI *VIRTIO_SET_QUEUE_NOTIFY) (\r
209 IN VIRTIO_DEVICE_PROTOCOL *This,\r
210 IN UINT16 Index\r
211 );\r
212\r
213/**\r
214 Write the queue alignment field in the Virtio Header.\r
215\r
216 The queue to which the alignment applies is selected by the Queue Select\r
217 field.\r
218\r
219 Note: This operation is not implemented by the VirtIo over PCI. The PCI\r
220 implementation of this protocol returns EFI_SUCCESS.\r
221\r
222 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
223\r
224 @param[in] Alignment The alignment boundary of the Used Ring in bytes.\r
225 Must be a power of 2.\r
226\r
227 @retval EFI_SUCCESS The data was written successfully.\r
228 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
229 provided address offset and write size.\r
230**/\r
231typedef\r
232EFI_STATUS\r
233(EFIAPI *VIRTIO_SET_QUEUE_ALIGN) (\r
234 IN VIRTIO_DEVICE_PROTOCOL *This,\r
235 IN UINT32 Alignment\r
236 );\r
237\r
238/**\r
239 Write the guest page size.\r
240\r
241 Note: This operation is not implemented by the VirtIo over PCI. The PCI\r
242 implementation of this protocol returns EFI_SUCCESS.\r
243\r
244 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
245\r
246 @param[in] PageSize Size of the Guest page in bytes.\r
247 Must be a power of 2.\r
248\r
249 @retval EFI_SUCCESS The data was written successfully.\r
250 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
251 provided address offset and write size.\r
252**/\r
253typedef\r
254EFI_STATUS\r
255(EFIAPI *VIRTIO_SET_PAGE_SIZE) (\r
256 IN VIRTIO_DEVICE_PROTOCOL *This,\r
257 IN UINT32 PageSize\r
258 );\r
259\r
260/**\r
261\r
262 Get the size of the virtqueue selected by the queue select field.\r
263\r
264 See Virtio spec Section 2.3\r
265\r
266 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
267\r
268 @param[out] QueueNumMax The size of the virtqueue in bytes.\r
269 Always a power of 2.\r
270\r
271 @retval EFI_SUCCESS The data was read successfully.\r
272 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
273 provided address offset and read size.\r
274 @retval EFI_INVALID_PARAMETER QueueNumMax is NULL\r
275**/\r
276typedef\r
277EFI_STATUS\r
278(EFIAPI *VIRTIO_GET_QUEUE_NUM_MAX) (\r
279 IN VIRTIO_DEVICE_PROTOCOL *This,\r
280 OUT UINT16 *QueueNumMax\r
281 );\r
282\r
283/**\r
284\r
285 Write to the QueueNum field in the Virtio Header.\r
286\r
287 This function only applies to Virtio-MMIO and may be a stub for other\r
288 implementations. See Virtio Spec appendix X.\r
289\r
290 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
291\r
292 @param[in] QueueSize The number of elements in the queue.\r
293\r
294 @retval EFI_SUCCESS The data was written successfully.\r
295 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
296 provided address offset and write size.\r
297**/\r
298typedef\r
299EFI_STATUS\r
300(EFIAPI *VIRTIO_SET_QUEUE_NUM) (\r
301 IN VIRTIO_DEVICE_PROTOCOL *This,\r
302 IN UINT16 QueueSize\r
303 );\r
304\r
305/**\r
306\r
307 Get the DeviceStatus field from the Virtio Header.\r
308\r
309 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
310\r
311 @param[out] DeviceStatus The 8-bit value for the Device status field\r
312\r
313 @retval EFI_SUCCESS The data was read successfully.\r
314 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
315 provided address offset and read size.\r
316 @retval EFI_INVALID_PARAMETER DeviceStatus is NULL\r
317**/\r
318typedef\r
319EFI_STATUS\r
320(EFIAPI *VIRTIO_GET_DEVICE_STATUS) (\r
321 IN VIRTIO_DEVICE_PROTOCOL *This,\r
322 OUT UINT8 *DeviceStatus\r
323 );\r
324\r
325/**\r
326\r
327 Write the DeviceStatus field in the Virtio Header.\r
328\r
329 @param[in] This This instance of VIRTIO_DEVICE_PROTOCOL\r
330\r
331 @param[in] DeviceStatus The 8-bit value for the Device status field\r
332\r
333 @retval EFI_SUCCESS The data was written successfully.\r
334 @retval EFI_UNSUPPORTED The underlying IO device doesn't support the\r
335 provided address offset and write size.\r
336**/\r
337typedef\r
338EFI_STATUS\r
339(EFIAPI *VIRTIO_SET_DEVICE_STATUS) (\r
340 IN VIRTIO_DEVICE_PROTOCOL *This,\r
341 IN UINT8 DeviceStatus\r
342 );\r
343\r
87811526
BS
344/**\r
345\r
346 Allocates pages that are suitable for an VirtioOperationBusMasterCommonBuffer\r
347 mapping. This means that the buffer allocated by this function supports\r
348 simultaneous access by both the processor and the bus master. The device\r
349 address that the bus master uses to access the buffer must be retrieved with\r
350 a call to VIRTIO_MAP_SHARED.\r
351\r
352 @param[in] This The protocol instance pointer.\r
353\r
354 @param[in] Pages The number of pages to allocate.\r
355\r
356 @param[in,out] HostAddress A pointer to store the system memory base\r
357 address of the allocated range.\r
358\r
359 @retval EFI_SUCCESS The requested memory pages were allocated.\r
360 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
361\r
362**/\r
363typedef\r
364EFI_STATUS\r
365(EFIAPI *VIRTIO_ALLOCATE_SHARED)(\r
366 IN VIRTIO_DEVICE_PROTOCOL *This,\r
367 IN UINTN Pages,\r
368 IN OUT VOID **HostAddress\r
369 );\r
370\r
371/**\r
372 Frees memory that was allocated with VIRTIO_ALLOCATE_SHARED.\r
373\r
374 @param[in] This The protocol instance pointer.\r
375\r
376 @param[in] Pages The number of pages to free.\r
377\r
378 @param[in] HostAddress The system memory base address of the allocated\r
379 range.\r
380\r
381**/\r
382typedef\r
383VOID\r
384(EFIAPI *VIRTIO_FREE_SHARED)(\r
385 IN VIRTIO_DEVICE_PROTOCOL *This,\r
386 IN UINTN Pages,\r
387 IN VOID *HostAddress\r
388 );\r
389\r
390/**\r
391 Provides the virtio device address required to access system memory from a\r
392 DMA bus master.\r
393\r
394 The interface follows the same usage pattern as defined in UEFI spec 2.6\r
395 (Section 13.2 PCI Root Bridge I/O Protocol)\r
396\r
397 @param[in] This The protocol instance pointer.\r
398\r
399 @param[in] Operation Indicates if the bus master is going to\r
400 read or write to system memory.\r
401\r
402 @param[in] HostAddress The system memory address to map to shared\r
403 buffer address.\r
404\r
405 @param[in,out] NumberOfBytes On input the number of bytes to map.\r
406 On output the number of bytes that were\r
407 mapped.\r
408\r
409 @param[out] DeviceAddress The resulting shared map address for the\r
410 bus master to access the hosts HostAddress.\r
411\r
412 @param[out] Mapping A resulting token to pass to\r
413 VIRTIO_UNMAP_SHARED.\r
414\r
415 @retval EFI_SUCCESS The range was mapped for the returned\r
416 NumberOfBytes.\r
417 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a\r
418 common buffer.\r
419 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
420 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to\r
421 a lack of resources.\r
422 @retval EFI_DEVICE_ERROR The system hardware could not map the\r
423 requested address.\r
424**/\r
425\r
426typedef\r
427EFI_STATUS\r
428(EFIAPI *VIRTIO_MAP_SHARED) (\r
429 IN VIRTIO_DEVICE_PROTOCOL *This,\r
430 IN VIRTIO_MAP_OPERATION Operation,\r
431 IN VOID *HostAddress,\r
432 IN OUT UINTN *NumberOfBytes,\r
433 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
434 OUT VOID **Mapping\r
435 );\r
436\r
437/**\r
438 Completes the VIRTIO_MAP_SHARED operation and releases any corresponding\r
439 resources.\r
440\r
441 @param[in] This The protocol instance pointer.\r
442\r
443 @param[in] Mapping The mapping token returned from\r
444 VIRTIO_MAP_SHARED.\r
445\r
446 @retval EFI_SUCCESS The range was unmapped.\r
447 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by\r
448 VIRTIO_MAP_SHARED. Passing an invalid Mapping\r
449 token can cause undefined behavior.\r
450 @retval EFI_DEVICE_ERROR The data was not committed to the target\r
451 system memory.\r
452**/\r
453typedef\r
454EFI_STATUS\r
455(EFIAPI *VIRTIO_UNMAP_SHARED)(\r
456 IN VIRTIO_DEVICE_PROTOCOL *This,\r
457 IN VOID *Mapping\r
458 );\r
fc4d1ce5
OM
459\r
460///\r
461/// This protocol provides an abstraction over the VirtIo transport layer\r
462///\r
463/// DISCLAIMER: this protocol is a work in progress, and should not be used\r
464/// outside of the EDK II tree.\r
465///\r
466struct _VIRTIO_DEVICE_PROTOCOL {\r
a136bc3c
BS
467 //\r
468 // VirtIo Specification Revision encoded with VIRTIO_SPEC_REVISION()\r
469 //\r
fc4d1ce5 470 UINT32 Revision;\r
a136bc3c
BS
471 //\r
472 // From the Virtio Spec\r
473 //\r
fc4d1ce5
OM
474 INT32 SubSystemDeviceId;\r
475\r
476 VIRTIO_GET_DEVICE_FEATURES GetDeviceFeatures;\r
477 VIRTIO_SET_GUEST_FEATURES SetGuestFeatures;\r
478\r
fc4d1ce5
OM
479 VIRTIO_SET_QUEUE_ADDRESS SetQueueAddress;\r
480\r
481 VIRTIO_SET_QUEUE_SEL SetQueueSel;\r
482\r
483 VIRTIO_SET_QUEUE_NOTIFY SetQueueNotify;\r
484\r
485 VIRTIO_SET_QUEUE_ALIGN SetQueueAlign;\r
486 VIRTIO_SET_PAGE_SIZE SetPageSize;\r
487\r
488 VIRTIO_GET_QUEUE_NUM_MAX GetQueueNumMax;\r
489 VIRTIO_SET_QUEUE_NUM SetQueueNum;\r
490\r
491 VIRTIO_GET_DEVICE_STATUS GetDeviceStatus;\r
492 VIRTIO_SET_DEVICE_STATUS SetDeviceStatus;\r
493\r
a136bc3c 494 //\r
fc4d1ce5 495 // Functions to read/write Device Specific headers\r
a136bc3c 496 //\r
fc4d1ce5
OM
497 VIRTIO_DEVICE_WRITE WriteDevice;\r
498 VIRTIO_DEVICE_READ ReadDevice;\r
87811526
BS
499\r
500 //\r
501 // Functions to allocate, free, map and unmap shared buffer\r
502 //\r
503 VIRTIO_ALLOCATE_SHARED AllocateSharedPages;\r
504 VIRTIO_FREE_SHARED FreeSharedPages;\r
505 VIRTIO_MAP_SHARED MapSharedBuffer;\r
506 VIRTIO_UNMAP_SHARED UnmapSharedBuffer;\r
fc4d1ce5
OM
507};\r
508\r
509extern EFI_GUID gVirtioDeviceProtocolGuid;\r
510\r
511#endif\r