]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/XenBusDxe/XenBusDxe.c
f3c74e1fbe00337d8654408a246c5bce98073935
[mirror_edk2.git] / OvmfPkg / XenBusDxe / XenBusDxe.c
1 /** @file
2 This driver produces XenBus Protocol instances for each Xen PV devices.
3
4 This XenBus bus driver will first initialize differente services in order to
5 enumerate the ParaVirtualized devices available.
6
7 Those services are:
8 - HyperCall
9 - Event Channel
10 - Grant Table
11 - XenStore
12 - XenBus
13
14 Copyright (C) 2014, Citrix Ltd.
15
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
20
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.
23
24 **/
25
26 #include <IndustryStandard/Pci.h>
27 #include <IndustryStandard/Acpi.h>
28 #include <Library/DebugLib.h>
29
30 #include "XenBusDxe.h"
31
32
33 ///
34 /// Driver Binding Protocol instance
35 ///
36 EFI_DRIVER_BINDING_PROTOCOL gXenBusDxeDriverBinding = {
37 XenBusDxeDriverBindingSupported,
38 XenBusDxeDriverBindingStart,
39 XenBusDxeDriverBindingStop,
40 XENBUS_DXE_VERSION,
41 NULL,
42 NULL
43 };
44
45
46 STATIC EFI_LOCK mMyDeviceLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK);
47 STATIC XENBUS_DEVICE *mMyDevice = NULL;
48
49 /**
50 Unloads an image.
51
52 @param ImageHandle Handle that identifies the image to be unloaded.
53
54 @retval EFI_SUCCESS The image has been unloaded.
55 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
56
57 **/
58 EFI_STATUS
59 EFIAPI
60 XenBusDxeUnload (
61 IN EFI_HANDLE ImageHandle
62 )
63 {
64 EFI_STATUS Status;
65
66 EFI_HANDLE *HandleBuffer;
67 UINTN HandleCount;
68 UINTN Index;
69
70 //
71 // Retrieve array of all handles in the handle database
72 //
73 Status = gBS->LocateHandleBuffer (
74 AllHandles,
75 NULL,
76 NULL,
77 &HandleCount,
78 &HandleBuffer
79 );
80 if (EFI_ERROR (Status)) {
81 return Status;
82 }
83
84 //
85 // Disconnect the current driver from handles in the handle database
86 //
87 for (Index = 0; Index < HandleCount; Index++) {
88 gBS->DisconnectController (HandleBuffer[Index], gImageHandle, NULL);
89 }
90
91 //
92 // Free the array of handles
93 //
94 FreePool (HandleBuffer);
95
96
97 //
98 // Uninstall protocols installed in the driver entry point
99 //
100 Status = gBS->UninstallMultipleProtocolInterfaces (
101 ImageHandle,
102 &gEfiDriverBindingProtocolGuid, &gXenBusDxeDriverBinding,
103 &gEfiComponentNameProtocolGuid, &gXenBusDxeComponentName,
104 &gEfiComponentName2ProtocolGuid, &gXenBusDxeComponentName2,
105 NULL
106 );
107 if (EFI_ERROR (Status)) {
108 return Status;
109 }
110
111 return EFI_SUCCESS;
112 }
113
114 /**
115 This is the declaration of an EFI image entry point. This entry point is
116 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
117 both device drivers and bus drivers.
118
119 @param ImageHandle The firmware allocated handle for the UEFI image.
120 @param SystemTable A pointer to the EFI System Table.
121
122 @retval EFI_SUCCESS The operation completed successfully.
123 @retval Others An unexpected error occurred.
124 **/
125 EFI_STATUS
126 EFIAPI
127 XenBusDxeDriverEntryPoint (
128 IN EFI_HANDLE ImageHandle,
129 IN EFI_SYSTEM_TABLE *SystemTable
130 )
131 {
132 EFI_STATUS Status;
133
134 //
135 // Install UEFI Driver Model protocol(s).
136 //
137 Status = EfiLibInstallDriverBindingComponentName2 (
138 ImageHandle,
139 SystemTable,
140 &gXenBusDxeDriverBinding,
141 ImageHandle,
142 &gXenBusDxeComponentName,
143 &gXenBusDxeComponentName2
144 );
145 ASSERT_EFI_ERROR (Status);
146
147
148 return Status;
149 }
150
151
152 /**
153 Tests to see if this driver supports a given controller. If a child device is provided,
154 it further tests to see if this driver supports creating a handle for the specified child device.
155
156 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
157 @param[in] ControllerHandle The handle of the controller to test. This handle
158 must support a protocol interface that supplies
159 an I/O abstraction to the driver.
160 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
161 parameter is ignored by device drivers, and is optional for bus
162 drivers. For bus drivers, if this parameter is not NULL, then
163 the bus driver must determine if the bus controller specified
164 by ControllerHandle and the child controller specified
165 by RemainingDevicePath are both supported by this
166 bus driver.
167
168 @retval EFI_SUCCESS The device specified by ControllerHandle and
169 RemainingDevicePath is supported by the driver specified by This.
170 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
171 RemainingDevicePath is already being managed by the driver
172 specified by This.
173 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
174 RemainingDevicePath is already being managed by a different
175 driver or an application that requires exclusive access.
176 Currently not implemented.
177 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
178 RemainingDevicePath is not supported by the driver specified by This.
179 **/
180 EFI_STATUS
181 EFIAPI
182 XenBusDxeDriverBindingSupported (
183 IN EFI_DRIVER_BINDING_PROTOCOL *This,
184 IN EFI_HANDLE ControllerHandle,
185 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
186 )
187 {
188 EFI_STATUS Status;
189 EFI_PCI_IO_PROTOCOL *PciIo;
190 PCI_TYPE00 Pci;
191
192 Status = gBS->OpenProtocol (
193 ControllerHandle,
194 &gEfiPciIoProtocolGuid,
195 (VOID **)&PciIo,
196 This->DriverBindingHandle,
197 ControllerHandle,
198 EFI_OPEN_PROTOCOL_BY_DRIVER
199 );
200 if (EFI_ERROR (Status)) {
201 return Status;
202 }
203
204 Status = PciIo->Pci.Read (PciIo, EfiPciIoWidthUint32, 0,
205 sizeof Pci / sizeof (UINT32), &Pci);
206
207 if (Status == EFI_SUCCESS) {
208 if (Pci.Hdr.VendorId == PCI_VENDOR_ID_XEN &&
209 Pci.Hdr.DeviceId == PCI_DEVICE_ID_XEN_PLATFORM) {
210 Status = EFI_SUCCESS;
211 } else {
212 Status = EFI_UNSUPPORTED;
213 }
214 }
215
216 gBS->CloseProtocol (ControllerHandle, &gEfiPciIoProtocolGuid,
217 This->DriverBindingHandle, ControllerHandle);
218
219 return Status;
220 }
221
222 VOID
223 EFIAPI
224 NotifyExitBoot (
225 IN EFI_EVENT Event,
226 IN VOID *Context
227 )
228 {
229 XENBUS_DEVICE *Dev = Context;
230
231 gBS->DisconnectController(Dev->ControllerHandle,
232 Dev->This->DriverBindingHandle, NULL);
233 }
234
235 /**
236 Starts a bus controller.
237
238 The Start() function is designed to be invoked from the EFI boot service ConnectController().
239 As a result, much of the error checking on the parameters to Start() has been moved into this
240 common boot service. It is legal to call Start() from other locations,
241 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
242 1. ControllerHandle must be a valid EFI_HANDLE.
243 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
244 EFI_DEVICE_PATH_PROTOCOL.
245 3. Prior to calling Start(), the Supported() function for the driver specified by This must
246 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
247
248 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
249 @param[in] ControllerHandle The handle of the controller to start. This handle
250 must support a protocol interface that supplies
251 an I/O abstraction to the driver.
252 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
253 parameter is ignored by device drivers, and is optional for bus
254 drivers. For a bus driver, if this parameter is NULL, then handles
255 for all the children of Controller are created by this driver.
256 If this parameter is not NULL and the first Device Path Node is
257 not the End of Device Path Node, then only the handle for the
258 child device specified by the first Device Path Node of
259 RemainingDevicePath is created by this driver.
260 If the first Device Path Node of RemainingDevicePath is
261 the End of Device Path Node, no child handle is created by this
262 driver.
263
264 @retval EFI_SUCCESS The device was started.
265 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
266 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
267 @retval Others The driver failded to start the device.
268
269 **/
270 EFI_STATUS
271 EFIAPI
272 XenBusDxeDriverBindingStart (
273 IN EFI_DRIVER_BINDING_PROTOCOL *This,
274 IN EFI_HANDLE ControllerHandle,
275 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
276 )
277 {
278 EFI_STATUS Status;
279 XENBUS_DEVICE *Dev;
280
281 Dev = AllocateZeroPool (sizeof (*Dev));
282 Dev->Signature = XENBUS_DEVICE_SIGNATURE;
283 Dev->This = This;
284 Dev->ControllerHandle = ControllerHandle;
285
286 EfiAcquireLock (&mMyDeviceLock);
287 if (mMyDevice != NULL) {
288 EfiReleaseLock (&mMyDeviceLock);
289 //
290 // There is already a XenBus running, only one can be used at a time.
291 //
292 Status = EFI_ALREADY_STARTED;
293 goto ErrorAllocated;
294 }
295 mMyDevice = Dev;
296 EfiReleaseLock (&mMyDeviceLock);
297
298 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
299 NotifyExitBoot,
300 (VOID*) Dev,
301 &Dev->ExitBootEvent);
302 ASSERT_EFI_ERROR (Status);
303
304 return EFI_SUCCESS;
305
306 ErrorAllocated:
307 FreePool (Dev);
308 return Status;
309 }
310
311 /**
312 Stops a bus controller.
313
314 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
315 As a result, much of the error checking on the parameters to Stop() has been moved
316 into this common boot service. It is legal to call Stop() from other locations,
317 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
318 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
319 same driver's Start() function.
320 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
321 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
322 Start() function, and the Start() function must have called OpenProtocol() on
323 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
324
325 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
326 @param[in] ControllerHandle A handle to the device being stopped. The handle must
327 support a bus specific I/O protocol for the driver
328 to use to stop the device.
329 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
330 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
331 if NumberOfChildren is 0.
332
333 @retval EFI_SUCCESS The device was stopped.
334 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
335
336 **/
337 EFI_STATUS
338 EFIAPI
339 XenBusDxeDriverBindingStop (
340 IN EFI_DRIVER_BINDING_PROTOCOL *This,
341 IN EFI_HANDLE ControllerHandle,
342 IN UINTN NumberOfChildren,
343 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
344 )
345 {
346 XENBUS_DEVICE *Dev = mMyDevice;
347
348 gBS->CloseEvent (Dev->ExitBootEvent);
349
350 mMyDevice = NULL;
351 FreePool (Dev);
352 return EFI_SUCCESS;
353 }