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
))
101 Status
= EFI_SUCCESS
;
103 Status
= EFI_UNSUPPORTED
;
108 // We needed PCI IO access only transitorily, to see whether we support the
113 &gEfiPciIoProtocolGuid
,
114 This
->DriverBindingHandle
,
123 After we've pronounced support for a specific device in
124 DriverBindingSupported(), we start managing said device (passed in by the
125 Driver Execution Environment) with the following service.
127 See DriverBindingSupported() for specification references.
129 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
130 incorporating this driver (independently of
133 @param[in] DeviceHandle The supported device to drive.
135 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
138 @retval EFI_SUCCESS The device was started.
140 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
142 @return Error codes from the OpenProtocol() boot
143 service, the PciIo protocol or the
144 InstallProtocolInterface() boot service.
150 XenIoPciDeviceBindingStart (
151 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
152 IN EFI_HANDLE DeviceHandle
,
153 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
157 XENIO_PROTOCOL
*XenIo
;
158 EFI_PCI_IO_PROTOCOL
*PciIo
;
159 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*BarDesc
;
161 XenIo
= (XENIO_PROTOCOL
*)AllocateZeroPool (sizeof *XenIo
);
163 return EFI_OUT_OF_RESOURCES
;
166 Status
= gBS
->OpenProtocol (
168 &gEfiPciIoProtocolGuid
,
170 This
->DriverBindingHandle
,
172 EFI_OPEN_PROTOCOL_BY_DRIVER
174 if (EFI_ERROR (Status
)) {
179 // The BAR1 of this PCI device is used for shared memory and is supposed to
180 // look like MMIO. The address space of the BAR1 will be used to map the
183 Status
= PciIo
->GetBarAttributes (PciIo
, PCI_BAR_IDX1
, NULL
, (VOID
**)&BarDesc
);
184 ASSERT_EFI_ERROR (Status
);
185 ASSERT (BarDesc
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
);
187 /* Get a Memory address for mapping the Grant Table. */
188 DEBUG ((DEBUG_INFO
, "XenIoPci: BAR at %LX\n", BarDesc
->AddrRangeMin
));
189 XenIo
->GrantTableAddress
= BarDesc
->AddrRangeMin
;
192 Status
= gBS
->InstallProtocolInterface (
195 EFI_NATIVE_INTERFACE
,
199 if (!EFI_ERROR (Status
)) {
205 &gEfiPciIoProtocolGuid
,
206 This
->DriverBindingHandle
,
218 Stop driving the XenIo PCI device
220 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
221 incorporating this driver (independently of any
224 @param[in] DeviceHandle Stop driving this device.
226 @param[in] NumberOfChildren Since this function belongs to a device driver
227 only (as opposed to a bus driver), the caller
228 environment sets NumberOfChildren to zero, and
231 @param[in] ChildHandleBuffer Ignored (corresponding to NumberOfChildren).
233 @retval EFI_SUCCESS Driver instance has been stopped and the PCI
234 configuration attributes have been restored.
236 @return Error codes from the OpenProtocol() or
237 CloseProtocol(), UninstallProtocolInterface()
244 XenIoPciDeviceBindingStop (
245 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
246 IN EFI_HANDLE DeviceHandle
,
247 IN UINTN NumberOfChildren
,
248 IN EFI_HANDLE
*ChildHandleBuffer
252 XENIO_PROTOCOL
*XenIo
;
254 Status
= gBS
->OpenProtocol (
255 DeviceHandle
, // candidate device
256 &gXenIoProtocolGuid
, // retrieve the XenIo iface
257 (VOID
**)&XenIo
, // target pointer
258 This
->DriverBindingHandle
, // requestor driver identity
259 DeviceHandle
, // requesting lookup for dev.
260 EFI_OPEN_PROTOCOL_GET_PROTOCOL
// lookup only, no ref. added
262 if (EFI_ERROR (Status
)) {
267 // Handle Stop() requests for in-use driver instances gracefully.
269 Status
= gBS
->UninstallProtocolInterface (
274 if (EFI_ERROR (Status
)) {
278 Status
= gBS
->CloseProtocol (
280 &gEfiPciIoProtocolGuid
,
281 This
->DriverBindingHandle
,
291 // The static object that groups the Supported() (ie. probe), Start() and
292 // Stop() functions of the driver together. Refer to UEFI Spec 2.3.1 + Errata
293 // C, 10.1 EFI Driver Binding Protocol.
295 STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding
= {
296 &XenIoPciDeviceBindingSupported
,
297 &XenIoPciDeviceBindingStart
,
298 &XenIoPciDeviceBindingStop
,
299 0x10, // Version, must be in [0x10 .. 0xFFFFFFEF] for IHV-developed drivers
300 NULL
, // ImageHandle, to be overwritten by
301 // EfiLibInstallDriverBindingComponentName2() in XenIoPciDeviceEntryPoint()
302 NULL
// DriverBindingHandle, ditto
306 // The purpose of the following scaffolding (EFI_COMPONENT_NAME_PROTOCOL and
307 // EFI_COMPONENT_NAME2_PROTOCOL implementation) is to format the driver's name
308 // in English, for display on standard console devices. This is recommended for
309 // UEFI drivers that follow the UEFI Driver Model. Refer to the Driver Writer's
310 // Guide for UEFI 2.3.1 v1.01, 11 UEFI Driver and Controller Names.
313 EFI_UNICODE_STRING_TABLE mDriverNameTable
[] = {
314 { "eng;en", L
"XenIo PCI Driver" },
319 EFI_COMPONENT_NAME_PROTOCOL gComponentName
;
323 XenIoPciGetDriverName (
324 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
326 OUT CHAR16
**DriverName
329 return LookupUnicodeString2 (
331 This
->SupportedLanguages
,
334 (BOOLEAN
)(This
== &gComponentName
) // Iso639Language
340 XenIoPciGetDeviceName (
341 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
342 IN EFI_HANDLE DeviceHandle
,
343 IN EFI_HANDLE ChildHandle
,
345 OUT CHAR16
**ControllerName
348 return EFI_UNSUPPORTED
;
352 EFI_COMPONENT_NAME_PROTOCOL gComponentName
= {
353 &XenIoPciGetDriverName
,
354 &XenIoPciGetDeviceName
,
355 "eng" // SupportedLanguages, ISO 639-2 language codes
359 EFI_COMPONENT_NAME2_PROTOCOL gComponentName2
= {
360 (EFI_COMPONENT_NAME2_GET_DRIVER_NAME
)&XenIoPciGetDriverName
,
361 (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME
)&XenIoPciGetDeviceName
,
362 "en" // SupportedLanguages, RFC 4646 language codes
366 // Entry point of this driver.
370 XenIoPciDeviceEntryPoint (
371 IN EFI_HANDLE ImageHandle
,
372 IN EFI_SYSTEM_TABLE
*SystemTable
375 return EfiLibInstallDriverBindingComponentName2 (