3 This driver produces Virtio Device Protocol instances for Virtio PCI devices.
5 Copyright (C) 2012, Red Hat, Inc.
6 Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>
7 Copyright (C) 2013, ARM Ltd.
8 Copyright (C) 2017, AMD Inc, All rights reserved.<BR>
10 This program and the accompanying materials are licensed and made available
11 under the terms and conditions of the BSD License which accompanies this
12 distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
16 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/UefiLib.h>
24 #include "VirtioPciDevice.h"
28 Read a word from Region 0 of the device specified by VirtIo Device protocol.
30 The function implements the ReadDevice protocol member of
31 VIRTIO_DEVICE_PROTOCOL.
33 @param[in] This VirtIo Device protocol.
35 @param[in] FieldOffset Source offset.
37 @param[in] FieldSize Source field size, must be in { 1, 2, 4, 8 }.
39 @param[in] BufferSize Number of bytes available in the target buffer. Must
42 @param[out] Buffer Target buffer.
45 @return Status code returned by PciIo->Io.Read().
51 IN VIRTIO_DEVICE_PROTOCOL
*This
,
58 VIRTIO_PCI_DEVICE
*Dev
;
60 Dev
= VIRTIO_PCI_DEVICE_FROM_VIRTIO_DEVICE (This
);
62 return VirtioPciIoRead (Dev
,
63 Dev
->DeviceSpecificConfigurationOffset
+ FieldOffset
,
64 FieldSize
, BufferSize
, Buffer
);
69 Write a word into Region 0 of the device specified by VirtIo Device protocol.
71 @param[in] This VirtIo Device protocol.
73 @param[in] FieldOffset Destination offset.
75 @param[in] FieldSize Destination field size, must be in { 1, 2, 4, 8 }.
77 @param[in] Value Little endian value to write, converted to UINT64.
78 The least significant FieldSize bytes will be used.
81 @return Status code returned by PciIo->Io.Write().
86 VirtioPciDeviceWrite (
87 IN VIRTIO_DEVICE_PROTOCOL
*This
,
93 VIRTIO_PCI_DEVICE
*Dev
;
95 Dev
= VIRTIO_PCI_DEVICE_FROM_VIRTIO_DEVICE (This
);
97 return VirtioPciIoWrite (Dev
,
98 Dev
->DeviceSpecificConfigurationOffset
+ FieldOffset
, FieldSize
, Value
);
103 VirtioPciGetDeviceFeatures (
104 IN VIRTIO_DEVICE_PROTOCOL
*This
,
105 OUT UINT64
*DeviceFeatures
108 VIRTIO_PCI_DEVICE
*Dev
;
112 if (DeviceFeatures
== NULL
) {
113 return EFI_INVALID_PARAMETER
;
116 Dev
= VIRTIO_PCI_DEVICE_FROM_VIRTIO_DEVICE (This
);
118 Status
= VirtioPciIoRead (Dev
, VIRTIO_PCI_OFFSET_DEVICE_FEATURES
,
119 sizeof (UINT32
), sizeof (UINT32
), &Features32
);
120 if (!EFI_ERROR (Status
)) {
121 *DeviceFeatures
= Features32
;
128 VirtioPciGetQueueSize (
129 IN VIRTIO_DEVICE_PROTOCOL
*This
,
130 OUT UINT16
*QueueNumMax
133 VIRTIO_PCI_DEVICE
*Dev
;
135 if (QueueNumMax
== NULL
) {
136 return EFI_INVALID_PARAMETER
;
139 Dev
= VIRTIO_PCI_DEVICE_FROM_VIRTIO_DEVICE (This
);
141 return VirtioPciIoRead (Dev
, VIRTIO_PCI_OFFSET_QUEUE_SIZE
, sizeof (UINT16
),
142 sizeof (UINT16
), QueueNumMax
);
147 VirtioPciGetDeviceStatus (
148 IN VIRTIO_DEVICE_PROTOCOL
*This
,
149 OUT UINT8
*DeviceStatus
152 VIRTIO_PCI_DEVICE
*Dev
;
154 if (DeviceStatus
== NULL
) {
155 return EFI_INVALID_PARAMETER
;
158 Dev
= VIRTIO_PCI_DEVICE_FROM_VIRTIO_DEVICE (This
);
160 return VirtioPciIoRead (Dev
, VIRTIO_PCI_OFFSET_QUEUE_DEVICE_STATUS
,
161 sizeof (UINT8
), sizeof (UINT8
), DeviceStatus
);
166 VirtioPciSetGuestFeatures (
167 IN VIRTIO_DEVICE_PROTOCOL
*This
,
171 VIRTIO_PCI_DEVICE
*Dev
;
173 Dev
= VIRTIO_PCI_DEVICE_FROM_VIRTIO_DEVICE (This
);
175 if (Features
> MAX_UINT32
) {
176 return EFI_UNSUPPORTED
;
178 return VirtioPciIoWrite (Dev
, VIRTIO_PCI_OFFSET_GUEST_FEATURES
,
179 sizeof (UINT32
), Features
);
184 VirtioPciSetQueueAddress (
185 IN VIRTIO_DEVICE_PROTOCOL
*This
,
189 VIRTIO_PCI_DEVICE
*Dev
;
191 Dev
= VIRTIO_PCI_DEVICE_FROM_VIRTIO_DEVICE (This
);
193 return VirtioPciIoWrite (Dev
, VIRTIO_PCI_OFFSET_QUEUE_ADDRESS
, sizeof (UINT32
),
194 (UINT32
)((UINTN
)Ring
->Base
>> EFI_PAGE_SHIFT
));
199 VirtioPciSetQueueSel (
200 IN VIRTIO_DEVICE_PROTOCOL
*This
,
204 VIRTIO_PCI_DEVICE
*Dev
;
206 Dev
= VIRTIO_PCI_DEVICE_FROM_VIRTIO_DEVICE (This
);
208 return VirtioPciIoWrite (Dev
, VIRTIO_PCI_OFFSET_QUEUE_SELECT
, sizeof (UINT16
),
214 VirtioPciSetQueueAlignment (
215 IN VIRTIO_DEVICE_PROTOCOL
*This
,
224 VirtioPciSetPageSize (
225 IN VIRTIO_DEVICE_PROTOCOL
*This
,
229 return (PageSize
== EFI_PAGE_SIZE
) ? EFI_SUCCESS
: EFI_UNSUPPORTED
;
234 VirtioPciSetQueueNotify (
235 IN VIRTIO_DEVICE_PROTOCOL
*This
,
239 VIRTIO_PCI_DEVICE
*Dev
;
241 Dev
= VIRTIO_PCI_DEVICE_FROM_VIRTIO_DEVICE (This
);
243 return VirtioPciIoWrite (Dev
, VIRTIO_PCI_OFFSET_QUEUE_NOTIFY
, sizeof (UINT16
),
249 VirtioPciSetQueueSize (
250 IN VIRTIO_DEVICE_PROTOCOL
*This
,
255 // This function is only applicable in Virtio-MMIO.
256 // (The QueueSize field is read-only in Virtio proper (PCI))
263 VirtioPciSetDeviceStatus (
264 IN VIRTIO_DEVICE_PROTOCOL
*This
,
265 IN UINT8 DeviceStatus
268 VIRTIO_PCI_DEVICE
*Dev
;
270 Dev
= VIRTIO_PCI_DEVICE_FROM_VIRTIO_DEVICE (This
);
272 return VirtioPciIoWrite (Dev
, VIRTIO_PCI_OFFSET_QUEUE_DEVICE_STATUS
,
273 sizeof (UINT8
), DeviceStatus
);
278 VirtioPciAllocateSharedPages (
279 IN VIRTIO_DEVICE_PROTOCOL
*This
,
281 OUT VOID
**HostAddress
286 Buffer
= AllocatePages (NumPages
);
287 if (Buffer
== NULL
) {
288 return EFI_OUT_OF_RESOURCES
;
291 *HostAddress
= Buffer
;
297 VirtioPciFreeSharedPages (
298 IN VIRTIO_DEVICE_PROTOCOL
*This
,
303 FreePages (HostAddress
, NumPages
);
308 VirtioPciMapSharedBuffer (
309 IN VIRTIO_DEVICE_PROTOCOL
*This
,
310 IN VIRTIO_MAP_OPERATION Operation
,
311 IN VOID
*HostAddress
,
312 IN OUT UINTN
*NumberOfBytes
,
313 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
317 *DeviceAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
325 VirtioPciUnmapSharedBuffer (
326 IN VIRTIO_DEVICE_PROTOCOL
*This
,