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"
38 /// Driver Binding Protocol instance
40 EFI_DRIVER_BINDING_PROTOCOL gXenBusDxeDriverBinding
= {
41 XenBusDxeDriverBindingSupported
,
42 XenBusDxeDriverBindingStart
,
43 XenBusDxeDriverBindingStop
,
50 STATIC EFI_LOCK mMyDeviceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK
);
51 STATIC XENBUS_DEVICE
*mMyDevice
= NULL
;
56 @param ImageHandle Handle that identifies the image to be unloaded.
58 @retval EFI_SUCCESS The image has been unloaded.
59 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
65 IN EFI_HANDLE ImageHandle
70 EFI_HANDLE
*HandleBuffer
;
75 // Retrieve array of all handles in the handle database
77 Status
= gBS
->LocateHandleBuffer (
84 if (EFI_ERROR (Status
)) {
89 // Disconnect the current driver from handles in the handle database
91 for (Index
= 0; Index
< HandleCount
; Index
++) {
92 gBS
->DisconnectController (HandleBuffer
[Index
], gImageHandle
, NULL
);
96 // Free the array of handles
98 FreePool (HandleBuffer
);
102 // Uninstall protocols installed in the driver entry point
104 Status
= gBS
->UninstallMultipleProtocolInterfaces (
106 &gEfiDriverBindingProtocolGuid
, &gXenBusDxeDriverBinding
,
107 &gEfiComponentNameProtocolGuid
, &gXenBusDxeComponentName
,
108 &gEfiComponentName2ProtocolGuid
, &gXenBusDxeComponentName2
,
111 if (EFI_ERROR (Status
)) {
119 This is the declaration of an EFI image entry point. This entry point is
120 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
121 both device drivers and bus drivers.
123 @param ImageHandle The firmware allocated handle for the UEFI image.
124 @param SystemTable A pointer to the EFI System Table.
126 @retval EFI_SUCCESS The operation completed successfully.
127 @retval Others An unexpected error occurred.
131 XenBusDxeDriverEntryPoint (
132 IN EFI_HANDLE ImageHandle
,
133 IN EFI_SYSTEM_TABLE
*SystemTable
139 // Install UEFI Driver Model protocol(s).
141 Status
= EfiLibInstallDriverBindingComponentName2 (
144 &gXenBusDxeDriverBinding
,
146 &gXenBusDxeComponentName
,
147 &gXenBusDxeComponentName2
149 ASSERT_EFI_ERROR (Status
);
157 Tests to see if this driver supports a given controller. If a child device is provided,
158 it further tests to see if this driver supports creating a handle for the specified child device.
160 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
161 @param[in] ControllerHandle The handle of the controller to test. This handle
162 must support a protocol interface that supplies
163 an I/O abstraction to the driver.
164 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
165 parameter is ignored by device drivers, and is optional for bus
166 drivers. For bus drivers, if this parameter is not NULL, then
167 the bus driver must determine if the bus controller specified
168 by ControllerHandle and the child controller specified
169 by RemainingDevicePath are both supported by this
172 @retval EFI_SUCCESS The device specified by ControllerHandle and
173 RemainingDevicePath is supported by the driver specified by This.
174 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
175 RemainingDevicePath is already being managed by the driver
177 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
178 RemainingDevicePath is already being managed by a different
179 driver or an application that requires exclusive access.
180 Currently not implemented.
181 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
182 RemainingDevicePath is not supported by the driver specified by This.
186 XenBusDxeDriverBindingSupported (
187 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
188 IN EFI_HANDLE ControllerHandle
,
189 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
193 EFI_PCI_IO_PROTOCOL
*PciIo
;
196 Status
= gBS
->OpenProtocol (
198 &gEfiPciIoProtocolGuid
,
200 This
->DriverBindingHandle
,
202 EFI_OPEN_PROTOCOL_BY_DRIVER
204 if (EFI_ERROR (Status
)) {
208 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, 0,
209 sizeof Pci
/ sizeof (UINT32
), &Pci
);
211 if (Status
== EFI_SUCCESS
) {
212 if (Pci
.Hdr
.VendorId
== PCI_VENDOR_ID_XEN
&&
213 Pci
.Hdr
.DeviceId
== PCI_DEVICE_ID_XEN_PLATFORM
) {
214 Status
= EFI_SUCCESS
;
216 Status
= EFI_UNSUPPORTED
;
220 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
221 This
->DriverBindingHandle
, ControllerHandle
);
233 XENBUS_DEVICE
*Dev
= Context
;
235 gBS
->DisconnectController(Dev
->ControllerHandle
,
236 Dev
->This
->DriverBindingHandle
, NULL
);
240 Starts a bus controller.
242 The Start() function is designed to be invoked from the EFI boot service ConnectController().
243 As a result, much of the error checking on the parameters to Start() has been moved into this
244 common boot service. It is legal to call Start() from other locations,
245 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
246 1. ControllerHandle must be a valid EFI_HANDLE.
247 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
248 EFI_DEVICE_PATH_PROTOCOL.
249 3. Prior to calling Start(), the Supported() function for the driver specified by This must
250 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
252 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
253 @param[in] ControllerHandle The handle of the controller to start. This handle
254 must support a protocol interface that supplies
255 an I/O abstraction to the driver.
256 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
257 parameter is ignored by device drivers, and is optional for bus
258 drivers. For a bus driver, if this parameter is NULL, then handles
259 for all the children of Controller are created by this driver.
260 If this parameter is not NULL and the first Device Path Node is
261 not the End of Device Path Node, then only the handle for the
262 child device specified by the first Device Path Node of
263 RemainingDevicePath is created by this driver.
264 If the first Device Path Node of RemainingDevicePath is
265 the End of Device Path Node, no child handle is created by this
268 @retval EFI_SUCCESS The device was started.
269 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
270 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
271 @retval EFI_UNSUPPORTED Something is missing on the system that
272 prevent to start the edvice.
273 @retval Others The driver failded to start the device.
278 XenBusDxeDriverBindingStart (
279 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
280 IN EFI_HANDLE ControllerHandle
,
281 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
286 EFI_PCI_IO_PROTOCOL
*PciIo
;
287 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*BarDesc
;
290 Status
= gBS
->OpenProtocol (
292 &gEfiPciIoProtocolGuid
,
294 This
->DriverBindingHandle
,
296 EFI_OPEN_PROTOCOL_BY_DRIVER
298 if (EFI_ERROR (Status
)) {
302 Dev
= AllocateZeroPool (sizeof (*Dev
));
303 Dev
->Signature
= XENBUS_DEVICE_SIGNATURE
;
305 Dev
->ControllerHandle
= ControllerHandle
;
308 EfiAcquireLock (&mMyDeviceLock
);
309 if (mMyDevice
!= NULL
) {
310 EfiReleaseLock (&mMyDeviceLock
);
312 // There is already a XenBus running, only one can be used at a time.
314 Status
= EFI_ALREADY_STARTED
;
318 EfiReleaseLock (&mMyDeviceLock
);
321 // The BAR1 of this PCI device is used for shared memory and is supposed to
322 // look like MMIO. The address space of the BAR1 will be used to map the
325 Status
= PciIo
->GetBarAttributes (PciIo
, PCI_BAR_IDX1
, NULL
, (VOID
**) &BarDesc
);
326 ASSERT_EFI_ERROR (Status
);
327 ASSERT (BarDesc
->ResType
== ACPI_ADDRESS_SPACE_TYPE_MEM
);
329 /* Get a Memory address for mapping the Grant Table. */
330 DEBUG ((EFI_D_INFO
, "XenBus: BAR at %LX\n", BarDesc
->AddrRangeMin
));
331 MmioAddr
= BarDesc
->AddrRangeMin
;
334 Status
= XenHyperpageInit (Dev
);
335 if (EFI_ERROR (Status
)) {
336 DEBUG ((EFI_D_ERROR
, "XenBus: Unable to retrieve the hyperpage.\n"));
337 Status
= EFI_UNSUPPORTED
;
341 Status
= XenGetSharedInfoPage (Dev
);
342 if (EFI_ERROR (Status
)) {
343 DEBUG ((EFI_D_ERROR
, "XenBus: Unable to get the shared info page.\n"));
344 Status
= EFI_UNSUPPORTED
;
348 XenGrantTableInit (Dev
, MmioAddr
);
350 Status
= XenStoreInit (Dev
);
351 ASSERT_EFI_ERROR (Status
);
353 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_CALLBACK
,
356 &Dev
->ExitBootEvent
);
357 ASSERT_EFI_ERROR (Status
);
363 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
364 This
->DriverBindingHandle
, ControllerHandle
);
369 Stops a bus controller.
371 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
372 As a result, much of the error checking on the parameters to Stop() has been moved
373 into this common boot service. It is legal to call Stop() from other locations,
374 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
375 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
376 same driver's Start() function.
377 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
378 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
379 Start() function, and the Start() function must have called OpenProtocol() on
380 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
382 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
383 @param[in] ControllerHandle A handle to the device being stopped. The handle must
384 support a bus specific I/O protocol for the driver
385 to use to stop the device.
386 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
387 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
388 if NumberOfChildren is 0.
390 @retval EFI_SUCCESS The device was stopped.
391 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
396 XenBusDxeDriverBindingStop (
397 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
398 IN EFI_HANDLE ControllerHandle
,
399 IN UINTN NumberOfChildren
,
400 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
403 XENBUS_DEVICE
*Dev
= mMyDevice
;
405 gBS
->CloseEvent (Dev
->ExitBootEvent
);
406 XenStoreDeinit (Dev
);
407 XenGrantTableDeinit (Dev
);
409 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
410 This
->DriverBindingHandle
, ControllerHandle
);