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
,
45 STATIC EFI_LOCK mMyDeviceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK
);
46 STATIC XENBUS_DEVICE
*mMyDevice
= NULL
;
49 Map the shared_info_t page into memory.
51 @param Dev A XENBUS_DEVICE instance.
53 @retval EFI_SUCCESS Dev->SharedInfo whill contain a pointer to
55 @retval EFI_LOAD_ERROR The shared info page could not be mapped. The
56 hypercall returned an error.
60 XenGetSharedInfoPage (
61 IN OUT XENBUS_DEVICE
*Dev
64 xen_add_to_physmap_t Parameter
;
66 ASSERT (Dev
->SharedInfo
== NULL
);
68 Parameter
.domid
= DOMID_SELF
;
69 Parameter
.space
= XENMAPSPACE_shared_info
;
73 // using reserved page because the page is not released when Linux is
74 // starting because of the add_to_physmap. QEMU might try to access the
75 // page, and fail because it have no right to do so (segv).
77 Dev
->SharedInfo
= AllocateReservedPages (1);
78 Parameter
.gpfn
= (UINTN
) Dev
->SharedInfo
>> EFI_PAGE_SHIFT
;
79 if (XenHypercallMemoryOp (XENMEM_add_to_physmap
, &Parameter
) != 0) {
80 FreePages (Dev
->SharedInfo
, 1);
81 Dev
->SharedInfo
= NULL
;
82 return EFI_LOAD_ERROR
;
91 @param ImageHandle Handle that identifies the image to be unloaded.
93 @retval EFI_SUCCESS The image has been unloaded.
94 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
100 IN EFI_HANDLE ImageHandle
105 EFI_HANDLE
*HandleBuffer
;
110 // Retrieve array of all handles in the handle database
112 Status
= gBS
->LocateHandleBuffer (
119 if (EFI_ERROR (Status
)) {
124 // Disconnect the current driver from handles in the handle database
126 for (Index
= 0; Index
< HandleCount
; Index
++) {
127 gBS
->DisconnectController (HandleBuffer
[Index
], gImageHandle
, NULL
);
131 // Free the array of handles
133 FreePool (HandleBuffer
);
137 // Uninstall protocols installed in the driver entry point
139 Status
= gBS
->UninstallMultipleProtocolInterfaces (
141 &gEfiDriverBindingProtocolGuid
, &gXenBusDxeDriverBinding
,
142 &gEfiComponentNameProtocolGuid
, &gXenBusDxeComponentName
,
143 &gEfiComponentName2ProtocolGuid
, &gXenBusDxeComponentName2
,
146 if (EFI_ERROR (Status
)) {
154 This is the declaration of an EFI image entry point. This entry point is
155 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
156 both device drivers and bus drivers.
158 @param ImageHandle The firmware allocated handle for the UEFI image.
159 @param SystemTable A pointer to the EFI System Table.
161 @retval EFI_SUCCESS The operation completed successfully.
162 @retval EFI_ABORTED Xen hypercalls are not available.
163 @retval Others An unexpected error occurred.
167 XenBusDxeDriverEntryPoint (
168 IN EFI_HANDLE ImageHandle
,
169 IN EFI_SYSTEM_TABLE
*SystemTable
174 if (! XenHypercallIsAvailable ()) {
179 // Install UEFI Driver Model protocol(s).
181 Status
= EfiLibInstallDriverBindingComponentName2 (
184 &gXenBusDxeDriverBinding
,
186 &gXenBusDxeComponentName
,
187 &gXenBusDxeComponentName2
189 ASSERT_EFI_ERROR (Status
);
197 Tests to see if this driver supports a given controller. If a child device is provided,
198 it further tests to see if this driver supports creating a handle for the specified child device.
200 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
201 @param[in] ControllerHandle The handle of the controller to test. This handle
202 must support a protocol interface that supplies
203 an I/O abstraction to the driver.
204 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
205 parameter is ignored by device drivers, and is optional for bus
206 drivers. For bus drivers, if this parameter is not NULL, then
207 the bus driver must determine if the bus controller specified
208 by ControllerHandle and the child controller specified
209 by RemainingDevicePath are both supported by this
212 @retval EFI_SUCCESS The device specified by ControllerHandle and
213 RemainingDevicePath is supported by the driver specified by This.
214 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
215 RemainingDevicePath is already being managed by the driver
217 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
218 RemainingDevicePath is already being managed by a different
219 driver or an application that requires exclusive access.
220 Currently not implemented.
221 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
222 RemainingDevicePath is not supported by the driver specified by This.
226 XenBusDxeDriverBindingSupported (
227 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
228 IN EFI_HANDLE ControllerHandle
,
229 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
233 XENIO_PROTOCOL
*XenIo
;
235 Status
= gBS
->OpenProtocol (
239 This
->DriverBindingHandle
,
241 EFI_OPEN_PROTOCOL_BY_DRIVER
244 if (EFI_ERROR (Status
)) {
248 gBS
->CloseProtocol (ControllerHandle
, &gXenIoProtocolGuid
,
249 This
->DriverBindingHandle
, ControllerHandle
);
261 XENBUS_DEVICE
*Dev
= Context
;
263 gBS
->DisconnectController(Dev
->ControllerHandle
,
264 Dev
->This
->DriverBindingHandle
, NULL
);
268 Starts a bus controller.
270 The Start() function is designed to be invoked from the EFI boot service ConnectController().
271 As a result, much of the error checking on the parameters to Start() has been moved into this
272 common boot service. It is legal to call Start() from other locations,
273 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
274 1. ControllerHandle must be a valid EFI_HANDLE.
275 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
276 EFI_DEVICE_PATH_PROTOCOL.
277 3. Prior to calling Start(), the Supported() function for the driver specified by This must
278 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
280 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
281 @param[in] ControllerHandle The handle of the controller to start. This handle
282 must support a protocol interface that supplies
283 an I/O abstraction to the driver.
284 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
285 parameter is ignored by device drivers, and is optional for bus
286 drivers. For a bus driver, if this parameter is NULL, then handles
287 for all the children of Controller are created by this driver.
288 If this parameter is not NULL and the first Device Path Node is
289 not the End of Device Path Node, then only the handle for the
290 child device specified by the first Device Path Node of
291 RemainingDevicePath is created by this driver.
292 If the first Device Path Node of RemainingDevicePath is
293 the End of Device Path Node, no child handle is created by this
296 @retval EFI_SUCCESS The device was started.
297 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
298 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
299 @retval EFI_UNSUPPORTED Something is missing on the system that
300 prevent to start the device.
301 @retval Others The driver failed to start the device.
306 XenBusDxeDriverBindingStart (
307 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
308 IN EFI_HANDLE ControllerHandle
,
309 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
314 XENIO_PROTOCOL
*XenIo
;
315 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
317 Status
= gBS
->OpenProtocol (
321 This
->DriverBindingHandle
,
323 EFI_OPEN_PROTOCOL_BY_DRIVER
326 if (EFI_ERROR (Status
)) {
330 Status
= gBS
->OpenProtocol (
332 &gEfiDevicePathProtocolGuid
,
333 (VOID
**) &DevicePath
,
334 This
->DriverBindingHandle
,
336 EFI_OPEN_PROTOCOL_BY_DRIVER
339 if (EFI_ERROR (Status
)) {
340 goto ErrorOpenningProtocol
;
343 Dev
= AllocateZeroPool (sizeof (*Dev
));
344 Dev
->Signature
= XENBUS_DEVICE_SIGNATURE
;
346 Dev
->ControllerHandle
= ControllerHandle
;
348 Dev
->DevicePath
= DevicePath
;
349 InitializeListHead (&Dev
->ChildList
);
351 EfiAcquireLock (&mMyDeviceLock
);
352 if (mMyDevice
!= NULL
) {
353 EfiReleaseLock (&mMyDeviceLock
);
355 // There is already a XenBus running, only one can be used at a time.
357 Status
= EFI_ALREADY_STARTED
;
361 EfiReleaseLock (&mMyDeviceLock
);
363 Status
= XenGetSharedInfoPage (Dev
);
364 if (EFI_ERROR (Status
)) {
365 DEBUG ((DEBUG_ERROR
, "XenBus: Unable to get the shared info page.\n"));
366 Status
= EFI_UNSUPPORTED
;
370 XenGrantTableInit (Dev
);
372 Status
= XenStoreInit (Dev
);
373 ASSERT_EFI_ERROR (Status
);
375 XenBusEnumerateBus (Dev
);
377 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_CALLBACK
,
380 &Dev
->ExitBootEvent
);
381 ASSERT_EFI_ERROR (Status
);
387 gBS
->CloseProtocol (ControllerHandle
, &gEfiDevicePathProtocolGuid
,
388 This
->DriverBindingHandle
, ControllerHandle
);
389 ErrorOpenningProtocol
:
390 gBS
->CloseProtocol (ControllerHandle
, &gXenIoProtocolGuid
,
391 This
->DriverBindingHandle
, ControllerHandle
);
396 Stops a bus controller.
398 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
399 As a result, much of the error checking on the parameters to Stop() has been moved
400 into this common boot service. It is legal to call Stop() from other locations,
401 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
402 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
403 same driver's Start() function.
404 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
405 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
406 Start() function, and the Start() function must have called OpenProtocol() on
407 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
409 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
410 @param[in] ControllerHandle A handle to the device being stopped. The handle must
411 support a bus specific I/O protocol for the driver
412 to use to stop the device.
413 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
414 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
415 if NumberOfChildren is 0.
417 @retval EFI_SUCCESS The device was stopped.
418 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
423 XenBusDxeDriverBindingStop (
424 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
425 IN EFI_HANDLE ControllerHandle
,
426 IN UINTN NumberOfChildren
,
427 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
431 XENBUS_PROTOCOL
*XenBusIo
;
432 XENBUS_PRIVATE_DATA
*ChildData
;
434 XENBUS_DEVICE
*Dev
= mMyDevice
;
436 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
437 Status
= gBS
->OpenProtocol (
438 ChildHandleBuffer
[Index
],
439 &gXenBusProtocolGuid
,
441 This
->DriverBindingHandle
,
443 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
444 if (EFI_ERROR (Status
)) {
445 DEBUG ((DEBUG_ERROR
, "XenBusDxe: get children protocol failed: %r\n", Status
));
448 ChildData
= XENBUS_PRIVATE_DATA_FROM_THIS (XenBusIo
);
450 Status
= gBS
->CloseProtocol (Dev
->ControllerHandle
, &gXenIoProtocolGuid
,
451 Dev
->This
->DriverBindingHandle
, ChildData
->Handle
);
452 ASSERT_EFI_ERROR (Status
);
454 Status
= gBS
->UninstallMultipleProtocolInterfaces (
456 &gEfiDevicePathProtocolGuid
, ChildData
->DevicePath
,
457 &gXenBusProtocolGuid
, &ChildData
->XenBusIo
,
459 ASSERT_EFI_ERROR (Status
);
461 FreePool ((VOID
*)ChildData
->XenBusIo
.Type
);
462 FreePool ((VOID
*)ChildData
->XenBusIo
.Node
);
463 FreePool ((VOID
*)ChildData
->XenBusIo
.Backend
);
464 FreePool (ChildData
->DevicePath
);
465 RemoveEntryList (&ChildData
->Link
);
466 FreePool (ChildData
);
468 if (NumberOfChildren
> 0) {
472 gBS
->CloseEvent (Dev
->ExitBootEvent
);
473 XenStoreDeinit (Dev
);
474 XenGrantTableDeinit (Dev
);
476 gBS
->CloseProtocol (ControllerHandle
, &gEfiDevicePathProtocolGuid
,
477 This
->DriverBindingHandle
, ControllerHandle
);
478 gBS
->CloseProtocol (ControllerHandle
, &gXenIoProtocolGuid
,
479 This
->DriverBindingHandle
, ControllerHandle
);