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>
29 #include <Library/XenHypercallLib.h>
31 #include "XenBusDxe.h"
33 #include "GrantTable.h"
37 #include <IndustryStandard/Xen/hvm/params.h>
38 #include <IndustryStandard/Xen/memory.h>
41 /// Driver Binding Protocol instance
43 EFI_DRIVER_BINDING_PROTOCOL gXenBusDxeDriverBinding
= {
44 XenBusDxeDriverBindingSupported
,
45 XenBusDxeDriverBindingStart
,
46 XenBusDxeDriverBindingStop
,
53 STATIC EFI_LOCK mMyDeviceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK
);
54 STATIC XENBUS_DEVICE
*mMyDevice
= NULL
;
57 Map the shared_info_t page into memory.
59 @param Dev A XENBUS_DEVICE instance.
61 @retval EFI_SUCCESS Dev->SharedInfo whill contain a pointer to
63 @retval EFI_LOAD_ERROR The shared info page could not be mapped. The
64 hypercall returned an error.
68 XenGetSharedInfoPage (
69 IN OUT XENBUS_DEVICE
*Dev
72 xen_add_to_physmap_t Parameter
;
74 ASSERT (Dev
->SharedInfo
== NULL
);
76 Parameter
.domid
= DOMID_SELF
;
77 Parameter
.space
= XENMAPSPACE_shared_info
;
81 // using reserved page because the page is not released when Linux is
82 // starting because of the add_to_physmap. QEMU might try to access the
83 // page, and fail because it have no right to do so (segv).
85 Dev
->SharedInfo
= AllocateReservedPages (1);
86 Parameter
.gpfn
= (UINTN
) Dev
->SharedInfo
>> EFI_PAGE_SHIFT
;
87 if (XenHypercallMemoryOp (XENMEM_add_to_physmap
, &Parameter
) != 0) {
88 FreePages (Dev
->SharedInfo
, 1);
89 Dev
->SharedInfo
= NULL
;
90 return EFI_LOAD_ERROR
;
99 @param ImageHandle Handle that identifies the image to be unloaded.
101 @retval EFI_SUCCESS The image has been unloaded.
102 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
108 IN EFI_HANDLE ImageHandle
113 EFI_HANDLE
*HandleBuffer
;
118 // Retrieve array of all handles in the handle database
120 Status
= gBS
->LocateHandleBuffer (
127 if (EFI_ERROR (Status
)) {
132 // Disconnect the current driver from handles in the handle database
134 for (Index
= 0; Index
< HandleCount
; Index
++) {
135 gBS
->DisconnectController (HandleBuffer
[Index
], gImageHandle
, NULL
);
139 // Free the array of handles
141 FreePool (HandleBuffer
);
145 // Uninstall protocols installed in the driver entry point
147 Status
= gBS
->UninstallMultipleProtocolInterfaces (
149 &gEfiDriverBindingProtocolGuid
, &gXenBusDxeDriverBinding
,
150 &gEfiComponentNameProtocolGuid
, &gXenBusDxeComponentName
,
151 &gEfiComponentName2ProtocolGuid
, &gXenBusDxeComponentName2
,
154 if (EFI_ERROR (Status
)) {
162 This is the declaration of an EFI image entry point. This entry point is
163 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
164 both device drivers and bus drivers.
166 @param ImageHandle The firmware allocated handle for the UEFI image.
167 @param SystemTable A pointer to the EFI System Table.
169 @retval EFI_SUCCESS The operation completed successfully.
170 @retval Others An unexpected error occurred.
174 XenBusDxeDriverEntryPoint (
175 IN EFI_HANDLE ImageHandle
,
176 IN EFI_SYSTEM_TABLE
*SystemTable
182 // Install UEFI Driver Model protocol(s).
184 Status
= EfiLibInstallDriverBindingComponentName2 (
187 &gXenBusDxeDriverBinding
,
189 &gXenBusDxeComponentName
,
190 &gXenBusDxeComponentName2
192 ASSERT_EFI_ERROR (Status
);
200 Tests to see if this driver supports a given controller. If a child device is provided,
201 it further tests to see if this driver supports creating a handle for the specified child device.
203 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
204 @param[in] ControllerHandle The handle of the controller to test. This handle
205 must support a protocol interface that supplies
206 an I/O abstraction to the driver.
207 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
208 parameter is ignored by device drivers, and is optional for bus
209 drivers. For bus drivers, if this parameter is not NULL, then
210 the bus driver must determine if the bus controller specified
211 by ControllerHandle and the child controller specified
212 by RemainingDevicePath are both supported by this
215 @retval EFI_SUCCESS The device specified by ControllerHandle and
216 RemainingDevicePath is supported by the driver specified by This.
217 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
218 RemainingDevicePath is already being managed by the driver
220 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
221 RemainingDevicePath is already being managed by a different
222 driver or an application that requires exclusive access.
223 Currently not implemented.
224 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
225 RemainingDevicePath is not supported by the driver specified by This.
229 XenBusDxeDriverBindingSupported (
230 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
231 IN EFI_HANDLE ControllerHandle
,
232 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
236 EFI_PCI_IO_PROTOCOL
*PciIo
;
239 Status
= gBS
->OpenProtocol (
241 &gEfiPciIoProtocolGuid
,
243 This
->DriverBindingHandle
,
245 EFI_OPEN_PROTOCOL_BY_DRIVER
247 if (EFI_ERROR (Status
)) {
251 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, 0,
252 sizeof Pci
/ sizeof (UINT32
), &Pci
);
254 if (Status
== EFI_SUCCESS
) {
255 if (Pci
.Hdr
.VendorId
== PCI_VENDOR_ID_XEN
&&
256 Pci
.Hdr
.DeviceId
== PCI_DEVICE_ID_XEN_PLATFORM
) {
257 Status
= EFI_SUCCESS
;
259 Status
= EFI_UNSUPPORTED
;
263 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
264 This
->DriverBindingHandle
, ControllerHandle
);
276 XENBUS_DEVICE
*Dev
= Context
;
278 gBS
->DisconnectController(Dev
->ControllerHandle
,
279 Dev
->This
->DriverBindingHandle
, NULL
);
283 Starts a bus controller.
285 The Start() function is designed to be invoked from the EFI boot service ConnectController().
286 As a result, much of the error checking on the parameters to Start() has been moved into this
287 common boot service. It is legal to call Start() from other locations,
288 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
289 1. ControllerHandle must be a valid EFI_HANDLE.
290 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
291 EFI_DEVICE_PATH_PROTOCOL.
292 3. Prior to calling Start(), the Supported() function for the driver specified by This must
293 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
295 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
296 @param[in] ControllerHandle The handle of the controller to start. This handle
297 must support a protocol interface that supplies
298 an I/O abstraction to the driver.
299 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
300 parameter is ignored by device drivers, and is optional for bus
301 drivers. For a bus driver, if this parameter is NULL, then handles
302 for all the children of Controller are created by this driver.
303 If this parameter is not NULL and the first Device Path Node is
304 not the End of Device Path Node, then only the handle for the
305 child device specified by the first Device Path Node of
306 RemainingDevicePath is created by this driver.
307 If the first Device Path Node of RemainingDevicePath is
308 the End of Device Path Node, no child handle is created by this
311 @retval EFI_SUCCESS The device was started.
312 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
313 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
314 @retval EFI_UNSUPPORTED Something is missing on the system that
315 prevent to start the edvice.
316 @retval Others The driver failded to start the device.
321 XenBusDxeDriverBindingStart (
322 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
323 IN EFI_HANDLE ControllerHandle
,
324 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
329 EFI_PCI_IO_PROTOCOL
*PciIo
;
330 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*BarDesc
;
332 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
334 Status
= gBS
->OpenProtocol (
336 &gEfiPciIoProtocolGuid
,
338 This
->DriverBindingHandle
,
340 EFI_OPEN_PROTOCOL_BY_DRIVER
342 if (EFI_ERROR (Status
)) {
346 Status
= gBS
->OpenProtocol (
348 &gEfiDevicePathProtocolGuid
,
349 (VOID
**) &DevicePath
,
350 This
->DriverBindingHandle
,
352 EFI_OPEN_PROTOCOL_BY_DRIVER
355 if (EFI_ERROR (Status
)) {
356 goto ErrorOpenningProtocol
;
359 Dev
= AllocateZeroPool (sizeof (*Dev
));
360 Dev
->Signature
= XENBUS_DEVICE_SIGNATURE
;
362 Dev
->ControllerHandle
= ControllerHandle
;
364 Dev
->DevicePath
= DevicePath
;
365 InitializeListHead (&Dev
->ChildList
);
367 EfiAcquireLock (&mMyDeviceLock
);
368 if (mMyDevice
!= NULL
) {
369 EfiReleaseLock (&mMyDeviceLock
);
371 // There is already a XenBus running, only one can be used at a time.
373 Status
= EFI_ALREADY_STARTED
;
377 EfiReleaseLock (&mMyDeviceLock
);
380 // The BAR1 of this PCI device is used for shared memory and is supposed to
381 // look like MMIO. The address space of the BAR1 will be used to map the
384 Status
= PciIo
->GetBarAttributes (PciIo
, PCI_BAR_IDX1
, NULL
, (VOID
**) &BarDesc
);
385 ASSERT_EFI_ERROR (Status
);
386 ASSERT (BarDesc
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
);
388 /* Get a Memory address for mapping the Grant Table. */
389 DEBUG ((EFI_D_INFO
, "XenBus: BAR at %LX\n", BarDesc
->AddrRangeMin
));
390 MmioAddr
= BarDesc
->AddrRangeMin
;
393 Status
= XenGetSharedInfoPage (Dev
);
394 if (EFI_ERROR (Status
)) {
395 DEBUG ((EFI_D_ERROR
, "XenBus: Unable to get the shared info page.\n"));
396 Status
= EFI_UNSUPPORTED
;
400 XenGrantTableInit (Dev
, MmioAddr
);
402 Status
= XenStoreInit (Dev
);
403 ASSERT_EFI_ERROR (Status
);
405 XenBusEnumerateBus (Dev
);
407 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_CALLBACK
,
410 &Dev
->ExitBootEvent
);
411 ASSERT_EFI_ERROR (Status
);
417 gBS
->CloseProtocol (ControllerHandle
, &gEfiDevicePathProtocolGuid
,
418 This
->DriverBindingHandle
, ControllerHandle
);
419 ErrorOpenningProtocol
:
420 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
421 This
->DriverBindingHandle
, ControllerHandle
);
426 Stops a bus controller.
428 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
429 As a result, much of the error checking on the parameters to Stop() has been moved
430 into this common boot service. It is legal to call Stop() from other locations,
431 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
432 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
433 same driver's Start() function.
434 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
435 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
436 Start() function, and the Start() function must have called OpenProtocol() on
437 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
439 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
440 @param[in] ControllerHandle A handle to the device being stopped. The handle must
441 support a bus specific I/O protocol for the driver
442 to use to stop the device.
443 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
444 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
445 if NumberOfChildren is 0.
447 @retval EFI_SUCCESS The device was stopped.
448 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
453 XenBusDxeDriverBindingStop (
454 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
455 IN EFI_HANDLE ControllerHandle
,
456 IN UINTN NumberOfChildren
,
457 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
461 XENBUS_PROTOCOL
*XenBusIo
;
462 XENBUS_PRIVATE_DATA
*ChildData
;
464 XENBUS_DEVICE
*Dev
= mMyDevice
;
466 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
467 Status
= gBS
->OpenProtocol (
468 ChildHandleBuffer
[Index
],
469 &gXenBusProtocolGuid
,
471 This
->DriverBindingHandle
,
473 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
474 if (EFI_ERROR (Status
)) {
475 DEBUG ((EFI_D_ERROR
, "XenBusDxe: get children protocol failed: %r\n", Status
));
478 ChildData
= XENBUS_PRIVATE_DATA_FROM_THIS (XenBusIo
);
479 Status
= gBS
->DisconnectController (ChildData
->Handle
, NULL
, NULL
);
480 if (EFI_ERROR (Status
)) {
481 DEBUG ((EFI_D_ERROR
, "XenBusDxe: error disconnecting child: %r\n",
486 Status
= gBS
->UninstallMultipleProtocolInterfaces (
488 &gEfiDevicePathProtocolGuid
, ChildData
->DevicePath
,
489 &gXenBusProtocolGuid
, &ChildData
->XenBusIo
,
491 ASSERT_EFI_ERROR (Status
);
493 FreePool ((VOID
*)ChildData
->XenBusIo
.Type
);
494 FreePool ((VOID
*)ChildData
->XenBusIo
.Node
);
495 FreePool ((VOID
*)ChildData
->XenBusIo
.Backend
);
496 FreePool (ChildData
->DevicePath
);
497 RemoveEntryList (&ChildData
->Link
);
498 FreePool (ChildData
);
500 if (NumberOfChildren
> 0) {
504 gBS
->CloseEvent (Dev
->ExitBootEvent
);
505 XenStoreDeinit (Dev
);
506 XenGrantTableDeinit (Dev
);
508 gBS
->CloseProtocol (ControllerHandle
, &gEfiDevicePathProtocolGuid
,
509 This
->DriverBindingHandle
, ControllerHandle
);
510 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
511 This
->DriverBindingHandle
, ControllerHandle
);