TianoCore EDK2 master
Loading...
Searching...
No Matches
XenIoPciDxe.c
Go to the documentation of this file.
1
17#include <Library/DebugLib.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
54EFIAPI
57 IN EFI_HANDLE DeviceHandle,
58 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
59 )
60{
61 EFI_STATUS Status;
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 {
101 Status = EFI_SUCCESS;
102 } else {
103 Status = EFI_UNSUPPORTED;
104 }
105 }
106
107 //
108 // We needed PCI IO access only transitorily, to see whether we support the
109 // device or not.
110 //
111 gBS->CloseProtocol (
112 DeviceHandle,
113 &gEfiPciIoProtocolGuid,
114 This->DriverBindingHandle,
115 DeviceHandle
116 );
117
118 return Status;
119}
120
147STATIC
149EFIAPI
152 IN EFI_HANDLE DeviceHandle,
153 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
154 )
155{
156 EFI_STATUS Status;
157 XENIO_PROTOCOL *XenIo;
158 EFI_PCI_IO_PROTOCOL *PciIo;
160
161 XenIo = (XENIO_PROTOCOL *)AllocateZeroPool (sizeof *XenIo);
162 if (XenIo == NULL) {
163 return EFI_OUT_OF_RESOURCES;
164 }
165
166 Status = gBS->OpenProtocol (
167 DeviceHandle,
168 &gEfiPciIoProtocolGuid,
169 (VOID **)&PciIo,
170 This->DriverBindingHandle,
171 DeviceHandle,
172 EFI_OPEN_PROTOCOL_BY_DRIVER
173 );
174 if (EFI_ERROR (Status)) {
175 goto FreeXenIo;
176 }
177
178 //
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
181 // Grant Table.
182 //
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);
186
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;
190 FreePool (BarDesc);
191
192 Status = gBS->InstallProtocolInterface (
193 &DeviceHandle,
194 &gXenIoProtocolGuid,
196 XenIo
197 );
198
199 if (!EFI_ERROR (Status)) {
200 return EFI_SUCCESS;
201 }
202
203 gBS->CloseProtocol (
204 DeviceHandle,
205 &gEfiPciIoProtocolGuid,
206 This->DriverBindingHandle,
207 DeviceHandle
208 );
209
210FreeXenIo:
211 FreePool (XenIo);
212
213 return Status;
214}
215
241STATIC
243EFIAPI
246 IN EFI_HANDLE DeviceHandle,
247 IN UINTN NumberOfChildren,
248 IN EFI_HANDLE *ChildHandleBuffer
249 )
250{
251 EFI_STATUS Status;
252 XENIO_PROTOCOL *XenIo;
253
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
261 );
262 if (EFI_ERROR (Status)) {
263 return Status;
264 }
265
266 //
267 // Handle Stop() requests for in-use driver instances gracefully.
268 //
269 Status = gBS->UninstallProtocolInterface (
270 DeviceHandle,
271 &gXenIoProtocolGuid,
272 XenIo
273 );
274 if (EFI_ERROR (Status)) {
275 return Status;
276 }
277
278 Status = gBS->CloseProtocol (
279 DeviceHandle,
280 &gEfiPciIoProtocolGuid,
281 This->DriverBindingHandle,
282 DeviceHandle
283 );
284
285 FreePool (XenIo);
286
287 return Status;
288}
289
290//
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.
294//
295STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding = {
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
303};
304
305//
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.
311//
312STATIC
313EFI_UNICODE_STRING_TABLE mDriverNameTable[] = {
314 { "eng;en", L"XenIo PCI Driver" },
315 { NULL, NULL }
316};
317
318STATIC
319EFI_COMPONENT_NAME_PROTOCOL gComponentName;
320
322EFIAPI
323XenIoPciGetDriverName (
325 IN CHAR8 *Language,
326 OUT CHAR16 **DriverName
327 )
328{
329 return LookupUnicodeString2 (
330 Language,
331 This->SupportedLanguages,
332 mDriverNameTable,
333 DriverName,
334 (BOOLEAN)(This == &gComponentName) // Iso639Language
335 );
336}
337
339EFIAPI
340XenIoPciGetDeviceName (
342 IN EFI_HANDLE DeviceHandle,
343 IN EFI_HANDLE ChildHandle,
344 IN CHAR8 *Language,
345 OUT CHAR16 **ControllerName
346 )
347{
348 return EFI_UNSUPPORTED;
349}
350
351STATIC
352EFI_COMPONENT_NAME_PROTOCOL gComponentName = {
353 &XenIoPciGetDriverName,
354 &XenIoPciGetDeviceName,
355 "eng" // SupportedLanguages, ISO 639-2 language codes
356};
357
358STATIC
359EFI_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
363};
364
365//
366// Entry point of this driver.
367//
369EFIAPI
370XenIoPciDeviceEntryPoint (
371 IN EFI_HANDLE ImageHandle,
372 IN EFI_SYSTEM_TABLE *SystemTable
373 )
374{
376 ImageHandle,
377 SystemTable,
378 &gDriverBinding,
379 ImageHandle,
380 &gComponentName,
381 &gComponentName2
382 );
383}
UINT64 UINTN
PACKED struct @89 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
EFI_STATUS(EFIAPI * EFI_COMPONENT_NAME2_GET_DRIVER_NAME)(IN EFI_COMPONENT_NAME2_PROTOCOL *This, IN CHAR8 *Language, OUT CHAR16 **DriverName)
EFI_STATUS(EFIAPI * EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)(IN EFI_COMPONENT_NAME2_PROTOCOL *This, IN EFI_HANDLE ControllerHandle, IN EFI_HANDLE ChildHandle OPTIONAL, IN CHAR8 *Language, OUT CHAR16 **ControllerName)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
EFI_STATUS EFIAPI LookupUnicodeString2(IN CONST CHAR8 *Language, IN CONST CHAR8 *SupportedLanguages, IN CONST EFI_UNICODE_STRING_TABLE *UnicodeStringTable, OUT CHAR16 **UnicodeString, IN BOOLEAN Iso639Language)
Definition: UefiLib.c:801
EFI_STATUS EFIAPI EfiLibInstallDriverBindingComponentName2(IN CONST EFI_HANDLE ImageHandle, IN CONST EFI_SYSTEM_TABLE *SystemTable, IN EFI_DRIVER_BINDING_PROTOCOL *DriverBinding, IN EFI_HANDLE DriverBindingHandle, IN CONST EFI_COMPONENT_NAME_PROTOCOL *ComponentName OPTIONAL, IN CONST EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2 OPTIONAL)
@ EFI_NATIVE_INTERFACE
Definition: UefiSpec.h:1193
STATIC EFI_STATUS EFIAPI XenIoPciDeviceBindingSupported(IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_HANDLE DeviceHandle, IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath)
Definition: XenIoPciDxe.c:55
STATIC EFI_STATUS EFIAPI XenIoPciDeviceBindingStart(IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_HANDLE DeviceHandle, IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath)
Definition: XenIoPciDxe.c:150
STATIC EFI_STATUS EFIAPI XenIoPciDeviceBindingStop(IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_HANDLE DeviceHandle, IN UINTN NumberOfChildren, IN EFI_HANDLE *ChildHandleBuffer)
Definition: XenIoPciDxe.c:244
EFI_PCI_IO_PROTOCOL_CONFIG Read
Definition: PciIo.h:232