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"
36 /// Driver Binding Protocol instance
38 EFI_DRIVER_BINDING_PROTOCOL gXenBusDxeDriverBinding
= {
39 XenBusDxeDriverBindingSupported
,
40 XenBusDxeDriverBindingStart
,
41 XenBusDxeDriverBindingStop
,
48 STATIC EFI_LOCK mMyDeviceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK
);
49 STATIC XENBUS_DEVICE
*mMyDevice
= NULL
;
54 @param ImageHandle Handle that identifies the image to be unloaded.
56 @retval EFI_SUCCESS The image has been unloaded.
57 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
63 IN EFI_HANDLE ImageHandle
68 EFI_HANDLE
*HandleBuffer
;
73 // Retrieve array of all handles in the handle database
75 Status
= gBS
->LocateHandleBuffer (
82 if (EFI_ERROR (Status
)) {
87 // Disconnect the current driver from handles in the handle database
89 for (Index
= 0; Index
< HandleCount
; Index
++) {
90 gBS
->DisconnectController (HandleBuffer
[Index
], gImageHandle
, NULL
);
94 // Free the array of handles
96 FreePool (HandleBuffer
);
100 // Uninstall protocols installed in the driver entry point
102 Status
= gBS
->UninstallMultipleProtocolInterfaces (
104 &gEfiDriverBindingProtocolGuid
, &gXenBusDxeDriverBinding
,
105 &gEfiComponentNameProtocolGuid
, &gXenBusDxeComponentName
,
106 &gEfiComponentName2ProtocolGuid
, &gXenBusDxeComponentName2
,
109 if (EFI_ERROR (Status
)) {
117 This is the declaration of an EFI image entry point. This entry point is
118 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
119 both device drivers and bus drivers.
121 @param ImageHandle The firmware allocated handle for the UEFI image.
122 @param SystemTable A pointer to the EFI System Table.
124 @retval EFI_SUCCESS The operation completed successfully.
125 @retval Others An unexpected error occurred.
129 XenBusDxeDriverEntryPoint (
130 IN EFI_HANDLE ImageHandle
,
131 IN EFI_SYSTEM_TABLE
*SystemTable
137 // Install UEFI Driver Model protocol(s).
139 Status
= EfiLibInstallDriverBindingComponentName2 (
142 &gXenBusDxeDriverBinding
,
144 &gXenBusDxeComponentName
,
145 &gXenBusDxeComponentName2
147 ASSERT_EFI_ERROR (Status
);
155 Tests to see if this driver supports a given controller. If a child device is provided,
156 it further tests to see if this driver supports creating a handle for the specified child device.
158 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
159 @param[in] ControllerHandle The handle of the controller to test. This handle
160 must support a protocol interface that supplies
161 an I/O abstraction to the driver.
162 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
163 parameter is ignored by device drivers, and is optional for bus
164 drivers. For bus drivers, if this parameter is not NULL, then
165 the bus driver must determine if the bus controller specified
166 by ControllerHandle and the child controller specified
167 by RemainingDevicePath are both supported by this
170 @retval EFI_SUCCESS The device specified by ControllerHandle and
171 RemainingDevicePath is supported by the driver specified by This.
172 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
173 RemainingDevicePath is already being managed by the driver
175 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
176 RemainingDevicePath is already being managed by a different
177 driver or an application that requires exclusive access.
178 Currently not implemented.
179 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
180 RemainingDevicePath is not supported by the driver specified by This.
184 XenBusDxeDriverBindingSupported (
185 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
186 IN EFI_HANDLE ControllerHandle
,
187 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
191 EFI_PCI_IO_PROTOCOL
*PciIo
;
194 Status
= gBS
->OpenProtocol (
196 &gEfiPciIoProtocolGuid
,
198 This
->DriverBindingHandle
,
200 EFI_OPEN_PROTOCOL_BY_DRIVER
202 if (EFI_ERROR (Status
)) {
206 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, 0,
207 sizeof Pci
/ sizeof (UINT32
), &Pci
);
209 if (Status
== EFI_SUCCESS
) {
210 if (Pci
.Hdr
.VendorId
== PCI_VENDOR_ID_XEN
&&
211 Pci
.Hdr
.DeviceId
== PCI_DEVICE_ID_XEN_PLATFORM
) {
212 Status
= EFI_SUCCESS
;
214 Status
= EFI_UNSUPPORTED
;
218 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
219 This
->DriverBindingHandle
, ControllerHandle
);
231 XENBUS_DEVICE
*Dev
= Context
;
233 gBS
->DisconnectController(Dev
->ControllerHandle
,
234 Dev
->This
->DriverBindingHandle
, NULL
);
238 Starts a bus controller.
240 The Start() function is designed to be invoked from the EFI boot service ConnectController().
241 As a result, much of the error checking on the parameters to Start() has been moved into this
242 common boot service. It is legal to call Start() from other locations,
243 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
244 1. ControllerHandle must be a valid EFI_HANDLE.
245 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
246 EFI_DEVICE_PATH_PROTOCOL.
247 3. Prior to calling Start(), the Supported() function for the driver specified by This must
248 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
250 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
251 @param[in] ControllerHandle The handle of the controller to start. This handle
252 must support a protocol interface that supplies
253 an I/O abstraction to the driver.
254 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
255 parameter is ignored by device drivers, and is optional for bus
256 drivers. For a bus driver, if this parameter is NULL, then handles
257 for all the children of Controller are created by this driver.
258 If this parameter is not NULL and the first Device Path Node is
259 not the End of Device Path Node, then only the handle for the
260 child device specified by the first Device Path Node of
261 RemainingDevicePath is created by this driver.
262 If the first Device Path Node of RemainingDevicePath is
263 the End of Device Path Node, no child handle is created by this
266 @retval EFI_SUCCESS The device was started.
267 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
268 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
269 @retval EFI_UNSUPPORTED Something is missing on the system that
270 prevent to start the edvice.
271 @retval Others The driver failded to start the device.
276 XenBusDxeDriverBindingStart (
277 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
278 IN EFI_HANDLE ControllerHandle
,
279 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
285 Dev
= AllocateZeroPool (sizeof (*Dev
));
286 Dev
->Signature
= XENBUS_DEVICE_SIGNATURE
;
288 Dev
->ControllerHandle
= ControllerHandle
;
290 EfiAcquireLock (&mMyDeviceLock
);
291 if (mMyDevice
!= NULL
) {
292 EfiReleaseLock (&mMyDeviceLock
);
294 // There is already a XenBus running, only one can be used at a time.
296 Status
= EFI_ALREADY_STARTED
;
300 EfiReleaseLock (&mMyDeviceLock
);
302 Status
= XenHyperpageInit (Dev
);
303 if (EFI_ERROR (Status
)) {
304 DEBUG ((EFI_D_ERROR
, "XenBus: Unable to retrieve the hyperpage.\n"));
305 Status
= EFI_UNSUPPORTED
;
309 Status
= XenGetSharedInfoPage (Dev
);
310 if (EFI_ERROR (Status
)) {
311 DEBUG ((EFI_D_ERROR
, "XenBus: Unable to get the shared info page.\n"));
312 Status
= EFI_UNSUPPORTED
;
316 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_CALLBACK
,
319 &Dev
->ExitBootEvent
);
320 ASSERT_EFI_ERROR (Status
);
330 Stops a bus controller.
332 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
333 As a result, much of the error checking on the parameters to Stop() has been moved
334 into this common boot service. It is legal to call Stop() from other locations,
335 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
336 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
337 same driver's Start() function.
338 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
339 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
340 Start() function, and the Start() function must have called OpenProtocol() on
341 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
343 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
344 @param[in] ControllerHandle A handle to the device being stopped. The handle must
345 support a bus specific I/O protocol for the driver
346 to use to stop the device.
347 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
348 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
349 if NumberOfChildren is 0.
351 @retval EFI_SUCCESS The device was stopped.
352 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
357 XenBusDxeDriverBindingStop (
358 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
359 IN EFI_HANDLE ControllerHandle
,
360 IN UINTN NumberOfChildren
,
361 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
364 XENBUS_DEVICE
*Dev
= mMyDevice
;
366 gBS
->CloseEvent (Dev
->ExitBootEvent
);