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