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 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.
20 #include <IndustryStandard/Acpi.h>
21 #include <IndustryStandard/Pci.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/UefiLib.h>
28 #include <Protocol/PciIo.h>
29 #include <Protocol/XenIo.h>
31 #define PCI_VENDOR_ID_XEN 0x5853
32 #define PCI_DEVICE_ID_XEN_PLATFORM 0x0001
36 Device probe function for this driver.
38 The DXE core calls this function for any given device in order to see if the
39 driver can drive the device.
41 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
42 incorporating this driver (independently of
45 @param[in] DeviceHandle The device to probe.
47 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
50 @retval EFI_SUCCESS The driver supports the device being probed.
52 @retval EFI_UNSUPPORTED The driver does not support the device being probed.
54 @return Error codes from the OpenProtocol() boot service or
61 XenIoPciDeviceBindingSupported (
62 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
63 IN EFI_HANDLE DeviceHandle
,
64 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
68 EFI_PCI_IO_PROTOCOL
*PciIo
;
72 // Attempt to open the device with the PciIo set of interfaces. On success,
73 // the protocol is "instantiated" for the PCI device. Covers duplicate open
74 // attempts (EFI_ALREADY_STARTED).
76 Status
= gBS
->OpenProtocol (
77 DeviceHandle
, // candidate device
78 &gEfiPciIoProtocolGuid
, // for generic PCI access
79 (VOID
**)&PciIo
, // handle to instantiate
80 This
->DriverBindingHandle
, // requestor driver identity
81 DeviceHandle
, // ControllerHandle, according to
82 // the UEFI Driver Model
83 EFI_OPEN_PROTOCOL_BY_DRIVER
// get exclusive PciIo access to
84 // the device; to be released
86 if (EFI_ERROR (Status
)) {
91 // Read entire PCI configuration header for more extensive check ahead.
93 Status
= PciIo
->Pci
.Read (
94 PciIo
, // (protocol, device)
96 EfiPciIoWidthUint32
, // access width & copy
99 sizeof Pci
/ sizeof (UINT32
), // Count
100 &Pci
// target buffer
103 if (Status
== EFI_SUCCESS
) {
104 if ((Pci
.Hdr
.VendorId
== PCI_VENDOR_ID_XEN
) &&
105 (Pci
.Hdr
.DeviceId
== PCI_DEVICE_ID_XEN_PLATFORM
)) {
106 Status
= EFI_SUCCESS
;
108 Status
= EFI_UNSUPPORTED
;
113 // We needed PCI IO access only transitorily, to see whether we support the
116 gBS
->CloseProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
,
117 This
->DriverBindingHandle
, DeviceHandle
);
124 After we've pronounced support for a specific device in
125 DriverBindingSupported(), we start managing said device (passed in by the
126 Driver Execution Environment) with the following service.
128 See DriverBindingSupported() for specification references.
130 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
131 incorporating this driver (independently of
134 @param[in] DeviceHandle The supported device to drive.
136 @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.
139 @retval EFI_SUCCESS The device was started.
141 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
143 @return Error codes from the OpenProtocol() boot
144 service, the PciIo protocol or the
145 InstallProtocolInterface() boot service.
151 XenIoPciDeviceBindingStart (
152 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
153 IN EFI_HANDLE DeviceHandle
,
154 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
158 XENIO_PROTOCOL
*XenIo
;
159 EFI_PCI_IO_PROTOCOL
*PciIo
;
160 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*BarDesc
;
162 XenIo
= (XENIO_PROTOCOL
*) AllocateZeroPool (sizeof *XenIo
);
164 return EFI_OUT_OF_RESOURCES
;
167 Status
= gBS
->OpenProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
,
168 (VOID
**)&PciIo
, This
->DriverBindingHandle
,
169 DeviceHandle
, EFI_OPEN_PROTOCOL_BY_DRIVER
);
170 if (EFI_ERROR (Status
)) {
175 // The BAR1 of this PCI device is used for shared memory and is supposed to
176 // look like MMIO. The address space of the BAR1 will be used to map the
179 Status
= PciIo
->GetBarAttributes (PciIo
, PCI_BAR_IDX1
, NULL
, (VOID
**) &BarDesc
);
180 ASSERT_EFI_ERROR (Status
);
181 ASSERT (BarDesc
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
);
183 /* Get a Memory address for mapping the Grant Table. */
184 DEBUG ((EFI_D_INFO
, "XenIoPci: BAR at %LX\n", BarDesc
->AddrRangeMin
));
185 XenIo
->GrantTableAddress
= BarDesc
->AddrRangeMin
;
188 Status
= gBS
->InstallProtocolInterface (&DeviceHandle
,
189 &gXenIoProtocolGuid
, EFI_NATIVE_INTERFACE
, XenIo
);
191 if (!EFI_ERROR (Status
)) {
195 gBS
->CloseProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
,
196 This
->DriverBindingHandle
, DeviceHandle
);
206 Stop driving the XenIo PCI device
208 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object
209 incorporating this driver (independently of any
212 @param[in] DeviceHandle Stop driving this device.
214 @param[in] NumberOfChildren Since this function belongs to a device driver
215 only (as opposed to a bus driver), the caller
216 environment sets NumberOfChildren to zero, and
219 @param[in] ChildHandleBuffer Ignored (corresponding to NumberOfChildren).
221 @retval EFI_SUCCESS Driver instance has been stopped and the PCI
222 configuration attributes have been restored.
224 @return Error codes from the OpenProtocol() or
225 CloseProtocol(), UninstallProtocolInterface()
232 XenIoPciDeviceBindingStop (
233 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
234 IN EFI_HANDLE DeviceHandle
,
235 IN UINTN NumberOfChildren
,
236 IN EFI_HANDLE
*ChildHandleBuffer
240 XENIO_PROTOCOL
*XenIo
;
242 Status
= gBS
->OpenProtocol (
243 DeviceHandle
, // candidate device
244 &gXenIoProtocolGuid
, // retrieve the XenIo iface
245 (VOID
**)&XenIo
, // target pointer
246 This
->DriverBindingHandle
, // requestor driver identity
247 DeviceHandle
, // requesting lookup for dev.
248 EFI_OPEN_PROTOCOL_GET_PROTOCOL
// lookup only, no ref. added
250 if (EFI_ERROR (Status
)) {
255 // Handle Stop() requests for in-use driver instances gracefully.
257 Status
= gBS
->UninstallProtocolInterface (DeviceHandle
,
258 &gXenIoProtocolGuid
, XenIo
);
259 if (EFI_ERROR (Status
)) {
263 Status
= gBS
->CloseProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
,
264 This
->DriverBindingHandle
, DeviceHandle
);
273 // The static object that groups the Supported() (ie. probe), Start() and
274 // Stop() functions of the driver together. Refer to UEFI Spec 2.3.1 + Errata
275 // C, 10.1 EFI Driver Binding Protocol.
277 STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding
= {
278 &XenIoPciDeviceBindingSupported
,
279 &XenIoPciDeviceBindingStart
,
280 &XenIoPciDeviceBindingStop
,
281 0x10, // Version, must be in [0x10 .. 0xFFFFFFEF] for IHV-developed drivers
282 NULL
, // ImageHandle, to be overwritten by
283 // EfiLibInstallDriverBindingComponentName2() in XenIoPciDeviceEntryPoint()
284 NULL
// DriverBindingHandle, ditto
289 // The purpose of the following scaffolding (EFI_COMPONENT_NAME_PROTOCOL and
290 // EFI_COMPONENT_NAME2_PROTOCOL implementation) is to format the driver's name
291 // in English, for display on standard console devices. This is recommended for
292 // UEFI drivers that follow the UEFI Driver Model. Refer to the Driver Writer's
293 // Guide for UEFI 2.3.1 v1.01, 11 UEFI Driver and Controller Names.
296 EFI_UNICODE_STRING_TABLE mDriverNameTable
[] = {
297 { "eng;en", L
"XenIo PCI Driver" },
302 EFI_COMPONENT_NAME_PROTOCOL gComponentName
;
306 XenIoPciGetDriverName (
307 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
309 OUT CHAR16
**DriverName
312 return LookupUnicodeString2 (
314 This
->SupportedLanguages
,
317 (BOOLEAN
)(This
== &gComponentName
) // Iso639Language
323 XenIoPciGetDeviceName (
324 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
325 IN EFI_HANDLE DeviceHandle
,
326 IN EFI_HANDLE ChildHandle
,
328 OUT CHAR16
**ControllerName
331 return EFI_UNSUPPORTED
;
335 EFI_COMPONENT_NAME_PROTOCOL gComponentName
= {
336 &XenIoPciGetDriverName
,
337 &XenIoPciGetDeviceName
,
338 "eng" // SupportedLanguages, ISO 639-2 language codes
342 EFI_COMPONENT_NAME2_PROTOCOL gComponentName2
= {
343 (EFI_COMPONENT_NAME2_GET_DRIVER_NAME
) &XenIoPciGetDriverName
,
344 (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME
) &XenIoPciGetDeviceName
,
345 "en" // SupportedLanguages, RFC 4646 language codes
350 // Entry point of this driver.
354 XenIoPciDeviceEntryPoint (
355 IN EFI_HANDLE ImageHandle
,
356 IN EFI_SYSTEM_TABLE
*SystemTable
359 return EfiLibInstallDriverBindingComponentName2 (