]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/XenIoPciDxe/XenIoPciDxe.c
OvmfPkg/OvmfXen: use RealTimeClockRuntimeDxe from EmbeddedPkg
[mirror_edk2.git] / OvmfPkg / XenIoPciDxe / XenIoPciDxe.c
1 /** @file
2
3 Driver for the virtual Xen PCI device
4
5 Copyright (C) 2012, Red Hat, Inc.
6 Copyright (c) 2012 - 2016, Intel Corporation. All rights reserved.<BR>
7 Copyright (C) 2013, ARM Ltd.
8 Copyright (C) 2015, Linaro Ltd.
9
10 SPDX-License-Identifier: BSD-2-Clause-Patent
11
12 **/
13
14 #include <IndustryStandard/Acpi.h>
15 #include <IndustryStandard/Pci.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/UefiLib.h>
21
22 #include <Protocol/PciIo.h>
23 #include <Protocol/XenIo.h>
24
25 #define PCI_VENDOR_ID_XEN 0x5853
26 #define PCI_DEVICE_ID_XEN_PLATFORM 0x0001
27
28 /**
29
30 Device probe function for this driver.
31
32 The DXE core calls this function for any given device in order to see if the
33 driver can drive the device.
34
35 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
36 incorporating this driver (independently of
37 any device).
38
39 @param[in] DeviceHandle The device to probe.
40
41 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
42
43
44 @retval EFI_SUCCESS The driver supports the device being probed.
45
46 @retval EFI_UNSUPPORTED The driver does not support the device being probed.
47
48 @return Error codes from the OpenProtocol() boot service or
49 the PciIo protocol.
50
51 **/
52 STATIC
53 EFI_STATUS
54 EFIAPI
55 XenIoPciDeviceBindingSupported (
56 IN EFI_DRIVER_BINDING_PROTOCOL *This,
57 IN EFI_HANDLE DeviceHandle,
58 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
59 )
60 {
61 EFI_STATUS Status;
62 EFI_PCI_IO_PROTOCOL *PciIo;
63 PCI_TYPE00 Pci;
64
65 //
66 // Attempt to open the device with the PciIo set of interfaces. On success,
67 // the protocol is "instantiated" for the PCI device. Covers duplicate open
68 // attempts (EFI_ALREADY_STARTED).
69 //
70 Status = gBS->OpenProtocol (
71 DeviceHandle, // candidate device
72 &gEfiPciIoProtocolGuid, // for generic PCI access
73 (VOID **)&PciIo, // handle to instantiate
74 This->DriverBindingHandle, // requestor driver identity
75 DeviceHandle, // ControllerHandle, according to
76 // the UEFI Driver Model
77 EFI_OPEN_PROTOCOL_BY_DRIVER // get exclusive PciIo access to
78 // the device; to be released
79 );
80 if (EFI_ERROR (Status)) {
81 return Status;
82 }
83
84 //
85 // Read entire PCI configuration header for more extensive check ahead.
86 //
87 Status = PciIo->Pci.Read (
88 PciIo, // (protocol, device)
89 // handle
90 EfiPciIoWidthUint32, // access width & copy
91 // mode
92 0, // Offset
93 sizeof Pci / sizeof (UINT32), // Count
94 &Pci // target buffer
95 );
96
97 if (Status == EFI_SUCCESS) {
98 if ((Pci.Hdr.VendorId == PCI_VENDOR_ID_XEN) &&
99 (Pci.Hdr.DeviceId == PCI_DEVICE_ID_XEN_PLATFORM)) {
100 Status = EFI_SUCCESS;
101 } else {
102 Status = EFI_UNSUPPORTED;
103 }
104 }
105
106 //
107 // We needed PCI IO access only transitorily, to see whether we support the
108 // device or not.
109 //
110 gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
111 This->DriverBindingHandle, DeviceHandle);
112
113 return Status;
114 }
115
116 /**
117
118 After we've pronounced support for a specific device in
119 DriverBindingSupported(), we start managing said device (passed in by the
120 Driver Execution Environment) with the following service.
121
122 See DriverBindingSupported() for specification references.
123
124 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
125 incorporating this driver (independently of
126 any device).
127
128 @param[in] DeviceHandle The supported device to drive.
129
130 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
131
132
133 @retval EFI_SUCCESS The device was started.
134
135 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
136
137 @return Error codes from the OpenProtocol() boot
138 service, the PciIo protocol or the
139 InstallProtocolInterface() boot service.
140
141 **/
142 STATIC
143 EFI_STATUS
144 EFIAPI
145 XenIoPciDeviceBindingStart (
146 IN EFI_DRIVER_BINDING_PROTOCOL *This,
147 IN EFI_HANDLE DeviceHandle,
148 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
149 )
150 {
151 EFI_STATUS Status;
152 XENIO_PROTOCOL *XenIo;
153 EFI_PCI_IO_PROTOCOL *PciIo;
154 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *BarDesc;
155
156 XenIo = (XENIO_PROTOCOL *) AllocateZeroPool (sizeof *XenIo);
157 if (XenIo == NULL) {
158 return EFI_OUT_OF_RESOURCES;
159 }
160
161 Status = gBS->OpenProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
162 (VOID **)&PciIo, This->DriverBindingHandle,
163 DeviceHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);
164 if (EFI_ERROR (Status)) {
165 goto FreeXenIo;
166 }
167
168 //
169 // The BAR1 of this PCI device is used for shared memory and is supposed to
170 // look like MMIO. The address space of the BAR1 will be used to map the
171 // Grant Table.
172 //
173 Status = PciIo->GetBarAttributes (PciIo, PCI_BAR_IDX1, NULL, (VOID**) &BarDesc);
174 ASSERT_EFI_ERROR (Status);
175 ASSERT (BarDesc->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM);
176
177 /* Get a Memory address for mapping the Grant Table. */
178 DEBUG ((EFI_D_INFO, "XenIoPci: BAR at %LX\n", BarDesc->AddrRangeMin));
179 XenIo->GrantTableAddress = BarDesc->AddrRangeMin;
180 FreePool (BarDesc);
181
182 Status = gBS->InstallProtocolInterface (&DeviceHandle,
183 &gXenIoProtocolGuid, EFI_NATIVE_INTERFACE, XenIo);
184
185 if (!EFI_ERROR (Status)) {
186 return EFI_SUCCESS;
187 }
188
189 gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
190 This->DriverBindingHandle, DeviceHandle);
191
192 FreeXenIo:
193 FreePool (XenIo);
194
195 return Status;
196 }
197
198 /**
199
200 Stop driving the XenIo PCI device
201
202 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
203 incorporating this driver (independently of any
204 device).
205
206 @param[in] DeviceHandle Stop driving this device.
207
208 @param[in] NumberOfChildren Since this function belongs to a device driver
209 only (as opposed to a bus driver), the caller
210 environment sets NumberOfChildren to zero, and
211 we ignore it.
212
213 @param[in] ChildHandleBuffer Ignored (corresponding to NumberOfChildren).
214
215 @retval EFI_SUCCESS Driver instance has been stopped and the PCI
216 configuration attributes have been restored.
217
218 @return Error codes from the OpenProtocol() or
219 CloseProtocol(), UninstallProtocolInterface()
220 boot services.
221
222 **/
223 STATIC
224 EFI_STATUS
225 EFIAPI
226 XenIoPciDeviceBindingStop (
227 IN EFI_DRIVER_BINDING_PROTOCOL *This,
228 IN EFI_HANDLE DeviceHandle,
229 IN UINTN NumberOfChildren,
230 IN EFI_HANDLE *ChildHandleBuffer
231 )
232 {
233 EFI_STATUS Status;
234 XENIO_PROTOCOL *XenIo;
235
236 Status = gBS->OpenProtocol (
237 DeviceHandle, // candidate device
238 &gXenIoProtocolGuid, // retrieve the XenIo iface
239 (VOID **)&XenIo, // target pointer
240 This->DriverBindingHandle, // requestor driver identity
241 DeviceHandle, // requesting lookup for dev.
242 EFI_OPEN_PROTOCOL_GET_PROTOCOL // lookup only, no ref. added
243 );
244 if (EFI_ERROR (Status)) {
245 return Status;
246 }
247
248 //
249 // Handle Stop() requests for in-use driver instances gracefully.
250 //
251 Status = gBS->UninstallProtocolInterface (DeviceHandle,
252 &gXenIoProtocolGuid, XenIo);
253 if (EFI_ERROR (Status)) {
254 return Status;
255 }
256
257 Status = gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,
258 This->DriverBindingHandle, DeviceHandle);
259
260 FreePool (XenIo);
261
262 return Status;
263 }
264
265
266 //
267 // The static object that groups the Supported() (ie. probe), Start() and
268 // Stop() functions of the driver together. Refer to UEFI Spec 2.3.1 + Errata
269 // C, 10.1 EFI Driver Binding Protocol.
270 //
271 STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding = {
272 &XenIoPciDeviceBindingSupported,
273 &XenIoPciDeviceBindingStart,
274 &XenIoPciDeviceBindingStop,
275 0x10, // Version, must be in [0x10 .. 0xFFFFFFEF] for IHV-developed drivers
276 NULL, // ImageHandle, to be overwritten by
277 // EfiLibInstallDriverBindingComponentName2() in XenIoPciDeviceEntryPoint()
278 NULL // DriverBindingHandle, ditto
279 };
280
281
282 //
283 // The purpose of the following scaffolding (EFI_COMPONENT_NAME_PROTOCOL and
284 // EFI_COMPONENT_NAME2_PROTOCOL implementation) is to format the driver's name
285 // in English, for display on standard console devices. This is recommended for
286 // UEFI drivers that follow the UEFI Driver Model. Refer to the Driver Writer's
287 // Guide for UEFI 2.3.1 v1.01, 11 UEFI Driver and Controller Names.
288 //
289 STATIC
290 EFI_UNICODE_STRING_TABLE mDriverNameTable[] = {
291 { "eng;en", L"XenIo PCI Driver" },
292 { NULL, NULL }
293 };
294
295 STATIC
296 EFI_COMPONENT_NAME_PROTOCOL gComponentName;
297
298 EFI_STATUS
299 EFIAPI
300 XenIoPciGetDriverName (
301 IN EFI_COMPONENT_NAME_PROTOCOL *This,
302 IN CHAR8 *Language,
303 OUT CHAR16 **DriverName
304 )
305 {
306 return LookupUnicodeString2 (
307 Language,
308 This->SupportedLanguages,
309 mDriverNameTable,
310 DriverName,
311 (BOOLEAN)(This == &gComponentName) // Iso639Language
312 );
313 }
314
315 EFI_STATUS
316 EFIAPI
317 XenIoPciGetDeviceName (
318 IN EFI_COMPONENT_NAME_PROTOCOL *This,
319 IN EFI_HANDLE DeviceHandle,
320 IN EFI_HANDLE ChildHandle,
321 IN CHAR8 *Language,
322 OUT CHAR16 **ControllerName
323 )
324 {
325 return EFI_UNSUPPORTED;
326 }
327
328 STATIC
329 EFI_COMPONENT_NAME_PROTOCOL gComponentName = {
330 &XenIoPciGetDriverName,
331 &XenIoPciGetDeviceName,
332 "eng" // SupportedLanguages, ISO 639-2 language codes
333 };
334
335 STATIC
336 EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {
337 (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) &XenIoPciGetDriverName,
338 (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) &XenIoPciGetDeviceName,
339 "en" // SupportedLanguages, RFC 4646 language codes
340 };
341
342
343 //
344 // Entry point of this driver.
345 //
346 EFI_STATUS
347 EFIAPI
348 XenIoPciDeviceEntryPoint (
349 IN EFI_HANDLE ImageHandle,
350 IN EFI_SYSTEM_TABLE *SystemTable
351 )
352 {
353 return EfiLibInstallDriverBindingComponentName2 (
354 ImageHandle,
355 SystemTable,
356 &gDriverBinding,
357 ImageHandle,
358 &gComponentName,
359 &gComponentName2
360 );
361 }