summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDevice.c
blob: fac3242257a2e0e126b112b0919c3e0bb0df8ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/** @file

  This driver produces Virtio Device Protocol instances for Virtio Mmio devices.

  Copyright (C) 2013, ARM Ltd.
  Copyright (C) 2017, AMD Inc. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>

#include "VirtioMmioDevice.h"

STATIC CONST VIRTIO_DEVICE_PROTOCOL  mMmioDeviceProtocolTemplate = {
  0,                                       // Revision
  0,                                       // SubSystemDeviceId
  VirtioMmioGetDeviceFeatures,             // GetDeviceFeatures
  VirtioMmioSetGuestFeatures,              // SetGuestFeatures
  VirtioMmioSetQueueAddress,               // SetQueueAddress
  VirtioMmioSetQueueSel,                   // SetQueueSel
  VirtioMmioSetQueueNotify,                // SetQueueNotify
  VirtioMmioSetQueueAlignment,             // SetQueueAlign
  VirtioMmioSetPageSize,                   // SetPageSize
  VirtioMmioGetQueueSize,                  // GetQueueNumMax
  VirtioMmioSetQueueSize,                  // SetQueueNum
  VirtioMmioGetDeviceStatus,               // GetDeviceStatus
  VirtioMmioSetDeviceStatus,               // SetDeviceStatus
  VirtioMmioDeviceWrite,                   // WriteDevice
  VirtioMmioDeviceRead,                    // ReadDevice
  VirtioMmioAllocateSharedPages,           // AllocateSharedPages
  VirtioMmioFreeSharedPages,               // FreeSharedPages
  VirtioMmioMapSharedBuffer,               // MapSharedBuffer
  VirtioMmioUnmapSharedBuffer              // UnmapSharedBuffer
};

/**

  Initialize the VirtIo MMIO Device

  @param[in] BaseAddress   Base Address of the VirtIo MMIO Device

  @param[in, out] Device   The driver instance to configure.

  @retval EFI_SUCCESS      Setup complete.

  @retval EFI_UNSUPPORTED  The driver is not a VirtIo MMIO device.

**/
STATIC
EFI_STATUS
EFIAPI
VirtioMmioInit (
  IN PHYSICAL_ADDRESS        BaseAddress,
  IN OUT VIRTIO_MMIO_DEVICE  *Device
  )
{
  UINT32  MagicValue;

  //
  // Initialize VirtIo Mmio Device
  //
  CopyMem (
    &Device->VirtioDevice,
    &mMmioDeviceProtocolTemplate,
    sizeof (VIRTIO_DEVICE_PROTOCOL)
    );
  Device->BaseAddress                    = BaseAddress;
  Device->VirtioDevice.SubSystemDeviceId =
    MmioRead32 (BaseAddress + VIRTIO_MMIO_OFFSET_DEVICE_ID);

  //
  // Double-check MMIO-specific values
  //
  MagicValue = VIRTIO_CFG_READ (Device, VIRTIO_MMIO_OFFSET_MAGIC);
  if (MagicValue != VIRTIO_MMIO_MAGIC) {
    return EFI_UNSUPPORTED;
  }

  Device->Version = VIRTIO_CFG_READ (Device, VIRTIO_MMIO_OFFSET_VERSION);
  switch (Device->Version) {
    case VIRTIO_MMIO_DEVICE_VERSION_0_95:
      DEBUG ((
        DEBUG_INFO,
        "%a virtio 0.9.5, id %d\n",
        __func__,
        Device->VirtioDevice.SubSystemDeviceId
        ));
      Device->VirtioDevice.Revision = VIRTIO_SPEC_REVISION (0, 9, 5);
      break;
    case VIRTIO_MMIO_DEVICE_VERSION_1_00:
      DEBUG ((
        DEBUG_INFO,
        "%a virtio 1.0, id %d\n",
        __func__,
        Device->VirtioDevice.SubSystemDeviceId
        ));
      Device->VirtioDevice.Revision = VIRTIO_SPEC_REVISION (1, 0, 0);
      break;
    default:
      return EFI_UNSUPPORTED;
  }

  return EFI_SUCCESS;
}

/**

  Uninitialize the internals of a virtio-mmio device that has been successfully
  set up with VirtioMmioInit().

  @param[in, out]  Device  The device to clean up.

**/
STATIC
VOID
EFIAPI
VirtioMmioUninit (
  IN VIRTIO_MMIO_DEVICE  *Device
  )
{
  //
  // Note: This function mirrors VirtioMmioInit() that does not allocate any
  //       resources - there's nothing to free here.
  //
}

EFI_STATUS
VirtioMmioInstallDevice (
  IN PHYSICAL_ADDRESS  BaseAddress,
  IN EFI_HANDLE        Handle
  )
{
  EFI_STATUS          Status;
  VIRTIO_MMIO_DEVICE  *VirtIo;

  if (!BaseAddress) {
    return EFI_INVALID_PARAMETER;
  }

  if (Handle == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // Allocate VIRTIO_MMIO_DEVICE
  //
  VirtIo = AllocateZeroPool (sizeof (VIRTIO_MMIO_DEVICE));
  if (VirtIo == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  VirtIo->Signature = VIRTIO_MMIO_DEVICE_SIGNATURE;

  Status = VirtioMmioInit (BaseAddress, VirtIo);
  if (EFI_ERROR (Status)) {
    goto FreeVirtioMem;
  }

  //
  // Install VIRTIO_DEVICE_PROTOCOL to Handle
  //
  Status = gBS->InstallProtocolInterface (
                  &Handle,
                  &gVirtioDeviceProtocolGuid,
                  EFI_NATIVE_INTERFACE,
                  &VirtIo->VirtioDevice
                  );
  if (EFI_ERROR (Status)) {
    goto UninitVirtio;
  }

  return EFI_SUCCESS;

UninitVirtio:
  VirtioMmioUninit (VirtIo);

FreeVirtioMem:
  FreePool (VirtIo);
  return Status;
}

EFI_STATUS
VirtioMmioUninstallDevice (
  IN EFI_HANDLE  DeviceHandle
  )
{
  VIRTIO_DEVICE_PROTOCOL  *VirtioDevice;
  VIRTIO_MMIO_DEVICE      *MmioDevice;
  EFI_STATUS              Status;

  Status = gBS->OpenProtocol (
                  DeviceHandle,                  // candidate device
                  &gVirtioDeviceProtocolGuid,    // retrieve the VirtIo iface
                  (VOID **)&VirtioDevice,        // target pointer
                  DeviceHandle,                  // requestor driver identity
                  DeviceHandle,                  // requesting lookup for dev.
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL // lookup only, no ref. added
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // Get the MMIO device from the VirtIo Device instance
  //
  MmioDevice = VIRTIO_MMIO_DEVICE_FROM_VIRTIO_DEVICE (VirtioDevice);

  //
  // Uninstall the protocol interface
  //
  Status = gBS->UninstallProtocolInterface (
                  DeviceHandle,
                  &gVirtioDeviceProtocolGuid,
                  &MmioDevice->VirtioDevice
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // Uninitialize the VirtIo Device
  //
  VirtioMmioUninit (MmioDevice);
  FreePool (MmioDevice);

  return EFI_SUCCESS;
}