3 Driver for the virtual Xen PCI device
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.
10 SPDX-License-Identifier: BSD-2-Clause-Patent
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>
22 #include <Protocol/PciIo.h>
23 #include <Protocol/XenIo.h>
25 #define PCI_VENDOR_ID_XEN 0x5853
26 #define PCI_DEVICE_ID_XEN_PLATFORM 0x0001
30 Device probe function for this driver.
32 The DXE core calls this function for any given device in order to see if the
33 driver can drive the device.
35 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
36 incorporating this driver (independently of
39 @param[in] DeviceHandle The device to probe.
41 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
44 @retval EFI_SUCCESS The driver supports the device being probed.
46 @retval EFI_UNSUPPORTED The driver does not support the device being probed.
48 @return Error codes from the OpenProtocol() boot service or
55 XenIoPciDeviceBindingSupported (
56 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
57 IN EFI_HANDLE DeviceHandle
,
58 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
62 EFI_PCI_IO_PROTOCOL
*PciIo
;
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).
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
80 if (EFI_ERROR (Status
)) {
85 // Read entire PCI configuration header for more extensive check ahead.
87 Status
= PciIo
->Pci
.Read (
88 PciIo
, // (protocol, device)
90 EfiPciIoWidthUint32
, // access width & copy
93 sizeof Pci
/ sizeof (UINT32
), // Count
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
;
102 Status
= EFI_UNSUPPORTED
;
107 // We needed PCI IO access only transitorily, to see whether we support the
110 gBS
->CloseProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
,
111 This
->DriverBindingHandle
, DeviceHandle
);
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.
122 See DriverBindingSupported() for specification references.
124 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
125 incorporating this driver (independently of
128 @param[in] DeviceHandle The supported device to drive.
130 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
133 @retval EFI_SUCCESS The device was started.
135 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
137 @return Error codes from the OpenProtocol() boot
138 service, the PciIo protocol or the
139 InstallProtocolInterface() boot service.
145 XenIoPciDeviceBindingStart (
146 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
147 IN EFI_HANDLE DeviceHandle
,
148 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
152 XENIO_PROTOCOL
*XenIo
;
153 EFI_PCI_IO_PROTOCOL
*PciIo
;
154 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*BarDesc
;
156 XenIo
= (XENIO_PROTOCOL
*) AllocateZeroPool (sizeof *XenIo
);
158 return EFI_OUT_OF_RESOURCES
;
161 Status
= gBS
->OpenProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
,
162 (VOID
**)&PciIo
, This
->DriverBindingHandle
,
163 DeviceHandle
, EFI_OPEN_PROTOCOL_BY_DRIVER
);
164 if (EFI_ERROR (Status
)) {
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
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
);
177 /* Get a Memory address for mapping the Grant Table. */
178 DEBUG ((DEBUG_INFO
, "XenIoPci: BAR at %LX\n", BarDesc
->AddrRangeMin
));
179 XenIo
->GrantTableAddress
= BarDesc
->AddrRangeMin
;
182 Status
= gBS
->InstallProtocolInterface (&DeviceHandle
,
183 &gXenIoProtocolGuid
, EFI_NATIVE_INTERFACE
, XenIo
);
185 if (!EFI_ERROR (Status
)) {
189 gBS
->CloseProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
,
190 This
->DriverBindingHandle
, DeviceHandle
);
200 Stop driving the XenIo PCI device
202 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
203 incorporating this driver (independently of any
206 @param[in] DeviceHandle Stop driving this device.
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
213 @param[in] ChildHandleBuffer Ignored (corresponding to NumberOfChildren).
215 @retval EFI_SUCCESS Driver instance has been stopped and the PCI
216 configuration attributes have been restored.
218 @return Error codes from the OpenProtocol() or
219 CloseProtocol(), UninstallProtocolInterface()
226 XenIoPciDeviceBindingStop (
227 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
228 IN EFI_HANDLE DeviceHandle
,
229 IN UINTN NumberOfChildren
,
230 IN EFI_HANDLE
*ChildHandleBuffer
234 XENIO_PROTOCOL
*XenIo
;
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
244 if (EFI_ERROR (Status
)) {
249 // Handle Stop() requests for in-use driver instances gracefully.
251 Status
= gBS
->UninstallProtocolInterface (DeviceHandle
,
252 &gXenIoProtocolGuid
, XenIo
);
253 if (EFI_ERROR (Status
)) {
257 Status
= gBS
->CloseProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
,
258 This
->DriverBindingHandle
, DeviceHandle
);
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.
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
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.
290 EFI_UNICODE_STRING_TABLE mDriverNameTable
[] = {
291 { "eng;en", L
"XenIo PCI Driver" },
296 EFI_COMPONENT_NAME_PROTOCOL gComponentName
;
300 XenIoPciGetDriverName (
301 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
303 OUT CHAR16
**DriverName
306 return LookupUnicodeString2 (
308 This
->SupportedLanguages
,
311 (BOOLEAN
)(This
== &gComponentName
) // Iso639Language
317 XenIoPciGetDeviceName (
318 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
319 IN EFI_HANDLE DeviceHandle
,
320 IN EFI_HANDLE ChildHandle
,
322 OUT CHAR16
**ControllerName
325 return EFI_UNSUPPORTED
;
329 EFI_COMPONENT_NAME_PROTOCOL gComponentName
= {
330 &XenIoPciGetDriverName
,
331 &XenIoPciGetDeviceName
,
332 "eng" // SupportedLanguages, ISO 639-2 language codes
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
344 // Entry point of this driver.
348 XenIoPciDeviceEntryPoint (
349 IN EFI_HANDLE ImageHandle
,
350 IN EFI_SYSTEM_TABLE
*SystemTable
353 return EfiLibInstallDriverBindingComponentName2 (