2 This driver produces XenBus Protocol instances for each Xen PV devices.
4 This XenBus bus driver will first initialize differente services in order to
5 enumerate the ParaVirtualized devices available.
14 Copyright (C) 2014, Citrix Ltd.
16 This program and the accompanying materials
17 are licensed and made available under the terms and conditions of the BSD License
18 which accompanies this distribution. The full text of the license may be found at
19 http://opensource.org/licenses/bsd-license.php
21 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
22 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26 #include <IndustryStandard/Pci.h>
27 #include <IndustryStandard/Acpi.h>
28 #include <Library/DebugLib.h>
30 #include "XenBusDxe.h"
32 #include "XenHypercall.h"
33 #include "GrantTable.h"
37 /// Driver Binding Protocol instance
39 EFI_DRIVER_BINDING_PROTOCOL gXenBusDxeDriverBinding
= {
40 XenBusDxeDriverBindingSupported
,
41 XenBusDxeDriverBindingStart
,
42 XenBusDxeDriverBindingStop
,
49 STATIC EFI_LOCK mMyDeviceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK
);
50 STATIC XENBUS_DEVICE
*mMyDevice
= NULL
;
55 @param ImageHandle Handle that identifies the image to be unloaded.
57 @retval EFI_SUCCESS The image has been unloaded.
58 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
64 IN EFI_HANDLE ImageHandle
69 EFI_HANDLE
*HandleBuffer
;
74 // Retrieve array of all handles in the handle database
76 Status
= gBS
->LocateHandleBuffer (
83 if (EFI_ERROR (Status
)) {
88 // Disconnect the current driver from handles in the handle database
90 for (Index
= 0; Index
< HandleCount
; Index
++) {
91 gBS
->DisconnectController (HandleBuffer
[Index
], gImageHandle
, NULL
);
95 // Free the array of handles
97 FreePool (HandleBuffer
);
101 // Uninstall protocols installed in the driver entry point
103 Status
= gBS
->UninstallMultipleProtocolInterfaces (
105 &gEfiDriverBindingProtocolGuid
, &gXenBusDxeDriverBinding
,
106 &gEfiComponentNameProtocolGuid
, &gXenBusDxeComponentName
,
107 &gEfiComponentName2ProtocolGuid
, &gXenBusDxeComponentName2
,
110 if (EFI_ERROR (Status
)) {
118 This is the declaration of an EFI image entry point. This entry point is
119 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
120 both device drivers and bus drivers.
122 @param ImageHandle The firmware allocated handle for the UEFI image.
123 @param SystemTable A pointer to the EFI System Table.
125 @retval EFI_SUCCESS The operation completed successfully.
126 @retval Others An unexpected error occurred.
130 XenBusDxeDriverEntryPoint (
131 IN EFI_HANDLE ImageHandle
,
132 IN EFI_SYSTEM_TABLE
*SystemTable
138 // Install UEFI Driver Model protocol(s).
140 Status
= EfiLibInstallDriverBindingComponentName2 (
143 &gXenBusDxeDriverBinding
,
145 &gXenBusDxeComponentName
,
146 &gXenBusDxeComponentName2
148 ASSERT_EFI_ERROR (Status
);
156 Tests to see if this driver supports a given controller. If a child device is provided,
157 it further tests to see if this driver supports creating a handle for the specified child device.
159 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
160 @param[in] ControllerHandle The handle of the controller to test. This handle
161 must support a protocol interface that supplies
162 an I/O abstraction to the driver.
163 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
164 parameter is ignored by device drivers, and is optional for bus
165 drivers. For bus drivers, if this parameter is not NULL, then
166 the bus driver must determine if the bus controller specified
167 by ControllerHandle and the child controller specified
168 by RemainingDevicePath are both supported by this
171 @retval EFI_SUCCESS The device specified by ControllerHandle and
172 RemainingDevicePath is supported by the driver specified by This.
173 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
174 RemainingDevicePath is already being managed by the driver
176 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
177 RemainingDevicePath is already being managed by a different
178 driver or an application that requires exclusive access.
179 Currently not implemented.
180 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
181 RemainingDevicePath is not supported by the driver specified by This.
185 XenBusDxeDriverBindingSupported (
186 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
187 IN EFI_HANDLE ControllerHandle
,
188 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
192 EFI_PCI_IO_PROTOCOL
*PciIo
;
195 Status
= gBS
->OpenProtocol (
197 &gEfiPciIoProtocolGuid
,
199 This
->DriverBindingHandle
,
201 EFI_OPEN_PROTOCOL_BY_DRIVER
203 if (EFI_ERROR (Status
)) {
207 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, 0,
208 sizeof Pci
/ sizeof (UINT32
), &Pci
);
210 if (Status
== EFI_SUCCESS
) {
211 if (Pci
.Hdr
.VendorId
== PCI_VENDOR_ID_XEN
&&
212 Pci
.Hdr
.DeviceId
== PCI_DEVICE_ID_XEN_PLATFORM
) {
213 Status
= EFI_SUCCESS
;
215 Status
= EFI_UNSUPPORTED
;
219 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
220 This
->DriverBindingHandle
, ControllerHandle
);
232 XENBUS_DEVICE
*Dev
= Context
;
234 gBS
->DisconnectController(Dev
->ControllerHandle
,
235 Dev
->This
->DriverBindingHandle
, NULL
);
239 Starts a bus controller.
241 The Start() function is designed to be invoked from the EFI boot service ConnectController().
242 As a result, much of the error checking on the parameters to Start() has been moved into this
243 common boot service. It is legal to call Start() from other locations,
244 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
245 1. ControllerHandle must be a valid EFI_HANDLE.
246 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
247 EFI_DEVICE_PATH_PROTOCOL.
248 3. Prior to calling Start(), the Supported() function for the driver specified by This must
249 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
251 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
252 @param[in] ControllerHandle The handle of the controller to start. This handle
253 must support a protocol interface that supplies
254 an I/O abstraction to the driver.
255 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
256 parameter is ignored by device drivers, and is optional for bus
257 drivers. For a bus driver, if this parameter is NULL, then handles
258 for all the children of Controller are created by this driver.
259 If this parameter is not NULL and the first Device Path Node is
260 not the End of Device Path Node, then only the handle for the
261 child device specified by the first Device Path Node of
262 RemainingDevicePath is created by this driver.
263 If the first Device Path Node of RemainingDevicePath is
264 the End of Device Path Node, no child handle is created by this
267 @retval EFI_SUCCESS The device was started.
268 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
269 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
270 @retval EFI_UNSUPPORTED Something is missing on the system that
271 prevent to start the edvice.
272 @retval Others The driver failded to start the device.
277 XenBusDxeDriverBindingStart (
278 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
279 IN EFI_HANDLE ControllerHandle
,
280 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
285 EFI_PCI_IO_PROTOCOL
*PciIo
;
286 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*BarDesc
;
289 Status
= gBS
->OpenProtocol (
291 &gEfiPciIoProtocolGuid
,
293 This
->DriverBindingHandle
,
295 EFI_OPEN_PROTOCOL_BY_DRIVER
297 if (EFI_ERROR (Status
)) {
301 Dev
= AllocateZeroPool (sizeof (*Dev
));
302 Dev
->Signature
= XENBUS_DEVICE_SIGNATURE
;
304 Dev
->ControllerHandle
= ControllerHandle
;
307 EfiAcquireLock (&mMyDeviceLock
);
308 if (mMyDevice
!= NULL
) {
309 EfiReleaseLock (&mMyDeviceLock
);
311 // There is already a XenBus running, only one can be used at a time.
313 Status
= EFI_ALREADY_STARTED
;
317 EfiReleaseLock (&mMyDeviceLock
);
320 // The BAR1 of this PCI device is used for shared memory and is supposed to
321 // look like MMIO. The address space of the BAR1 will be used to map the
324 Status
= PciIo
->GetBarAttributes (PciIo
, PCI_BAR_IDX1
, NULL
, (VOID
**) &BarDesc
);
325 ASSERT_EFI_ERROR (Status
);
326 ASSERT (BarDesc
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
);
328 /* Get a Memory address for mapping the Grant Table. */
329 DEBUG ((EFI_D_INFO
, "XenBus: BAR at %LX\n", BarDesc
->AddrRangeMin
));
330 MmioAddr
= BarDesc
->AddrRangeMin
;
333 Status
= XenHyperpageInit (Dev
);
334 if (EFI_ERROR (Status
)) {
335 DEBUG ((EFI_D_ERROR
, "XenBus: Unable to retrieve the hyperpage.\n"));
336 Status
= EFI_UNSUPPORTED
;
340 Status
= XenGetSharedInfoPage (Dev
);
341 if (EFI_ERROR (Status
)) {
342 DEBUG ((EFI_D_ERROR
, "XenBus: Unable to get the shared info page.\n"));
343 Status
= EFI_UNSUPPORTED
;
347 XenGrantTableInit (Dev
, MmioAddr
);
349 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_CALLBACK
,
352 &Dev
->ExitBootEvent
);
353 ASSERT_EFI_ERROR (Status
);
359 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
360 This
->DriverBindingHandle
, ControllerHandle
);
365 Stops a bus controller.
367 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
368 As a result, much of the error checking on the parameters to Stop() has been moved
369 into this common boot service. It is legal to call Stop() from other locations,
370 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
371 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
372 same driver's Start() function.
373 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
374 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
375 Start() function, and the Start() function must have called OpenProtocol() on
376 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
378 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
379 @param[in] ControllerHandle A handle to the device being stopped. The handle must
380 support a bus specific I/O protocol for the driver
381 to use to stop the device.
382 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
383 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
384 if NumberOfChildren is 0.
386 @retval EFI_SUCCESS The device was stopped.
387 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
392 XenBusDxeDriverBindingStop (
393 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
394 IN EFI_HANDLE ControllerHandle
,
395 IN UINTN NumberOfChildren
,
396 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
399 XENBUS_DEVICE
*Dev
= mMyDevice
;
401 gBS
->CloseEvent (Dev
->ExitBootEvent
);
402 XenGrantTableDeinit (Dev
);
404 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
405 This
->DriverBindingHandle
, ControllerHandle
);