2 This driver produces XenBus Protocol instances for each Xen PV devices.
4 This XenBus bus driver will first initialize different services in order to
5 enumerate the ParaVirtualized devices available.
14 Copyright (C) 2014, Citrix Ltd.
16 SPDX-License-Identifier: BSD-2-Clause-Patent
20 #include <Library/DebugLib.h>
21 #include <Library/XenHypercallLib.h>
23 #include "XenBusDxe.h"
25 #include "GrantTable.h"
29 #include <IndustryStandard/Xen/hvm/params.h>
30 #include <IndustryStandard/Xen/memory.h>
33 /// Driver Binding Protocol instance
35 EFI_DRIVER_BINDING_PROTOCOL gXenBusDxeDriverBinding
= {
36 XenBusDxeDriverBindingSupported
,
37 XenBusDxeDriverBindingStart
,
38 XenBusDxeDriverBindingStop
,
44 STATIC EFI_LOCK mMyDeviceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK
);
45 STATIC XENBUS_DEVICE
*mMyDevice
= NULL
;
48 Map the shared_info_t page into memory.
50 @param Dev A XENBUS_DEVICE instance.
52 @retval EFI_SUCCESS Dev->SharedInfo whill contain a pointer to
54 @retval EFI_LOAD_ERROR The shared info page could not be mapped. The
55 hypercall returned an error.
59 XenGetSharedInfoPage (
60 IN OUT XENBUS_DEVICE
*Dev
63 xen_add_to_physmap_t Parameter
;
65 ASSERT (Dev
->SharedInfo
== NULL
);
67 Parameter
.domid
= DOMID_SELF
;
68 Parameter
.space
= XENMAPSPACE_shared_info
;
72 // using reserved page because the page is not released when Linux is
73 // starting because of the add_to_physmap. QEMU might try to access the
74 // page, and fail because it have no right to do so (segv).
76 Dev
->SharedInfo
= AllocateReservedPages (1);
77 Parameter
.gpfn
= (UINTN
)Dev
->SharedInfo
>> EFI_PAGE_SHIFT
;
78 if (XenHypercallMemoryOp (XENMEM_add_to_physmap
, &Parameter
) != 0) {
79 FreePages (Dev
->SharedInfo
, 1);
80 Dev
->SharedInfo
= NULL
;
81 return EFI_LOAD_ERROR
;
90 @param ImageHandle Handle that identifies the image to be unloaded.
92 @retval EFI_SUCCESS The image has been unloaded.
93 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
99 IN EFI_HANDLE ImageHandle
104 EFI_HANDLE
*HandleBuffer
;
109 // Retrieve array of all handles in the handle database
111 Status
= gBS
->LocateHandleBuffer (
118 if (EFI_ERROR (Status
)) {
123 // Disconnect the current driver from handles in the handle database
125 for (Index
= 0; Index
< HandleCount
; Index
++) {
126 gBS
->DisconnectController (HandleBuffer
[Index
], gImageHandle
, NULL
);
130 // Free the array of handles
132 FreePool (HandleBuffer
);
135 // Uninstall protocols installed in the driver entry point
137 Status
= gBS
->UninstallMultipleProtocolInterfaces (
139 &gEfiDriverBindingProtocolGuid
,
140 &gXenBusDxeDriverBinding
,
141 &gEfiComponentNameProtocolGuid
,
142 &gXenBusDxeComponentName
,
143 &gEfiComponentName2ProtocolGuid
,
144 &gXenBusDxeComponentName2
,
147 if (EFI_ERROR (Status
)) {
155 This is the declaration of an EFI image entry point. This entry point is
156 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
157 both device drivers and bus drivers.
159 @param ImageHandle The firmware allocated handle for the UEFI image.
160 @param SystemTable A pointer to the EFI System Table.
162 @retval EFI_SUCCESS The operation completed successfully.
163 @retval EFI_ABORTED Xen hypercalls are not available.
164 @retval Others An unexpected error occurred.
168 XenBusDxeDriverEntryPoint (
169 IN EFI_HANDLE ImageHandle
,
170 IN EFI_SYSTEM_TABLE
*SystemTable
175 if (!XenHypercallIsAvailable ()) {
180 // Install UEFI Driver Model protocol(s).
182 Status
= EfiLibInstallDriverBindingComponentName2 (
185 &gXenBusDxeDriverBinding
,
187 &gXenBusDxeComponentName
,
188 &gXenBusDxeComponentName2
190 ASSERT_EFI_ERROR (Status
);
196 Tests to see if this driver supports a given controller. If a child device is provided,
197 it further tests to see if this driver supports creating a handle for the specified child device.
199 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
200 @param[in] ControllerHandle The handle of the controller to test. This handle
201 must support a protocol interface that supplies
202 an I/O abstraction to the driver.
203 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
204 parameter is ignored by device drivers, and is optional for bus
205 drivers. For bus drivers, if this parameter is not NULL, then
206 the bus driver must determine if the bus controller specified
207 by ControllerHandle and the child controller specified
208 by RemainingDevicePath are both supported by this
211 @retval EFI_SUCCESS The device specified by ControllerHandle and
212 RemainingDevicePath is supported by the driver specified by This.
213 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
214 RemainingDevicePath is already being managed by the driver
216 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
217 RemainingDevicePath is already being managed by a different
218 driver or an application that requires exclusive access.
219 Currently not implemented.
220 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
221 RemainingDevicePath is not supported by the driver specified by This.
225 XenBusDxeDriverBindingSupported (
226 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
227 IN EFI_HANDLE ControllerHandle
,
228 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
232 XENIO_PROTOCOL
*XenIo
;
234 Status
= gBS
->OpenProtocol (
238 This
->DriverBindingHandle
,
240 EFI_OPEN_PROTOCOL_BY_DRIVER
243 if (EFI_ERROR (Status
)) {
250 This
->DriverBindingHandle
,
264 XENBUS_DEVICE
*Dev
= Context
;
266 gBS
->DisconnectController (
267 Dev
->ControllerHandle
,
268 Dev
->This
->DriverBindingHandle
,
274 Starts a bus controller.
276 The Start() function is designed to be invoked from the EFI boot service ConnectController().
277 As a result, much of the error checking on the parameters to Start() has been moved into this
278 common boot service. It is legal to call Start() from other locations,
279 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
280 1. ControllerHandle must be a valid EFI_HANDLE.
281 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
282 EFI_DEVICE_PATH_PROTOCOL.
283 3. Prior to calling Start(), the Supported() function for the driver specified by This must
284 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
286 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
287 @param[in] ControllerHandle The handle of the controller to start. This handle
288 must support a protocol interface that supplies
289 an I/O abstraction to the driver.
290 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
291 parameter is ignored by device drivers, and is optional for bus
292 drivers. For a bus driver, if this parameter is NULL, then handles
293 for all the children of Controller are created by this driver.
294 If this parameter is not NULL and the first Device Path Node is
295 not the End of Device Path Node, then only the handle for the
296 child device specified by the first Device Path Node of
297 RemainingDevicePath is created by this driver.
298 If the first Device Path Node of RemainingDevicePath is
299 the End of Device Path Node, no child handle is created by this
302 @retval EFI_SUCCESS The device was started.
303 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
304 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
305 @retval EFI_UNSUPPORTED Something is missing on the system that
306 prevent to start the device.
307 @retval Others The driver failed to start the device.
312 XenBusDxeDriverBindingStart (
313 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
314 IN EFI_HANDLE ControllerHandle
,
315 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
320 XENIO_PROTOCOL
*XenIo
;
321 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
323 Status
= gBS
->OpenProtocol (
327 This
->DriverBindingHandle
,
329 EFI_OPEN_PROTOCOL_BY_DRIVER
332 if (EFI_ERROR (Status
)) {
336 Status
= gBS
->OpenProtocol (
338 &gEfiDevicePathProtocolGuid
,
339 (VOID
**)&DevicePath
,
340 This
->DriverBindingHandle
,
342 EFI_OPEN_PROTOCOL_BY_DRIVER
345 if (EFI_ERROR (Status
)) {
346 goto ErrorOpenningProtocol
;
349 Dev
= AllocateZeroPool (sizeof (*Dev
));
350 Dev
->Signature
= XENBUS_DEVICE_SIGNATURE
;
352 Dev
->ControllerHandle
= ControllerHandle
;
354 Dev
->DevicePath
= DevicePath
;
355 InitializeListHead (&Dev
->ChildList
);
357 EfiAcquireLock (&mMyDeviceLock
);
358 if (mMyDevice
!= NULL
) {
359 EfiReleaseLock (&mMyDeviceLock
);
361 // There is already a XenBus running, only one can be used at a time.
363 Status
= EFI_ALREADY_STARTED
;
368 EfiReleaseLock (&mMyDeviceLock
);
370 Status
= XenGetSharedInfoPage (Dev
);
371 if (EFI_ERROR (Status
)) {
372 DEBUG ((DEBUG_ERROR
, "XenBus: Unable to get the shared info page.\n"));
373 Status
= EFI_UNSUPPORTED
;
377 XenGrantTableInit (Dev
);
379 Status
= XenStoreInit (Dev
);
380 ASSERT_EFI_ERROR (Status
);
382 XenBusEnumerateBus (Dev
);
384 Status
= gBS
->CreateEvent (
385 EVT_SIGNAL_EXIT_BOOT_SERVICES
,
391 ASSERT_EFI_ERROR (Status
);
399 &gEfiDevicePathProtocolGuid
,
400 This
->DriverBindingHandle
,
403 ErrorOpenningProtocol
:
407 This
->DriverBindingHandle
,
414 Stops a bus controller.
416 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
417 As a result, much of the error checking on the parameters to Stop() has been moved
418 into this common boot service. It is legal to call Stop() from other locations,
419 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
420 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
421 same driver's Start() function.
422 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
423 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
424 Start() function, and the Start() function must have called OpenProtocol() on
425 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
427 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
428 @param[in] ControllerHandle A handle to the device being stopped. The handle must
429 support a bus specific I/O protocol for the driver
430 to use to stop the device.
431 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
432 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
433 if NumberOfChildren is 0.
435 @retval EFI_SUCCESS The device was stopped.
436 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
441 XenBusDxeDriverBindingStop (
442 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
443 IN EFI_HANDLE ControllerHandle
,
444 IN UINTN NumberOfChildren
,
445 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
449 XENBUS_PROTOCOL
*XenBusIo
;
450 XENBUS_PRIVATE_DATA
*ChildData
;
452 XENBUS_DEVICE
*Dev
= mMyDevice
;
454 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
455 Status
= gBS
->OpenProtocol (
456 ChildHandleBuffer
[Index
],
457 &gXenBusProtocolGuid
,
459 This
->DriverBindingHandle
,
461 EFI_OPEN_PROTOCOL_GET_PROTOCOL
463 if (EFI_ERROR (Status
)) {
464 DEBUG ((DEBUG_ERROR
, "XenBusDxe: get children protocol failed: %r\n", Status
));
468 ChildData
= XENBUS_PRIVATE_DATA_FROM_THIS (XenBusIo
);
470 Status
= gBS
->CloseProtocol (
471 Dev
->ControllerHandle
,
473 Dev
->This
->DriverBindingHandle
,
476 ASSERT_EFI_ERROR (Status
);
478 Status
= gBS
->UninstallMultipleProtocolInterfaces (
480 &gEfiDevicePathProtocolGuid
,
481 ChildData
->DevicePath
,
482 &gXenBusProtocolGuid
,
483 &ChildData
->XenBusIo
,
486 ASSERT_EFI_ERROR (Status
);
488 FreePool ((VOID
*)ChildData
->XenBusIo
.Type
);
489 FreePool ((VOID
*)ChildData
->XenBusIo
.Node
);
490 FreePool ((VOID
*)ChildData
->XenBusIo
.Backend
);
491 FreePool (ChildData
->DevicePath
);
492 RemoveEntryList (&ChildData
->Link
);
493 FreePool (ChildData
);
496 if (NumberOfChildren
> 0) {
500 gBS
->CloseEvent (Dev
->ExitBootEvent
);
501 XenStoreDeinit (Dev
);
502 XenGrantTableDeinit (Dev
);
506 &gEfiDevicePathProtocolGuid
,
507 This
->DriverBindingHandle
,
513 This
->DriverBindingHandle
,