]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/XenBusDxe/XenBusDxe.c
IntelFsp2Pkg/SplitFspBin.py: Support rebasing 1.x binary.
[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
b26f0cf9 16 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e65e8802
AP
17\r
18**/\r
19\r
e65e8802 20#include <Library/DebugLib.h>\r
cd8ff8fd 21#include <Library/XenHypercallLib.h>\r
e65e8802
AP
22\r
23#include "XenBusDxe.h"\r
24\r
0fd14246 25#include "GrantTable.h"\r
a9090a94 26#include "XenStore.h"\r
86d968e0 27#include "XenBus.h"\r
abcbbb14 28\r
bbc3758a
AB
29#include <IndustryStandard/Xen/hvm/params.h>\r
30#include <IndustryStandard/Xen/memory.h>\r
e65e8802
AP
31\r
32///\r
33/// Driver Binding Protocol instance\r
34///\r
35EFI_DRIVER_BINDING_PROTOCOL gXenBusDxeDriverBinding = {\r
36 XenBusDxeDriverBindingSupported,\r
37 XenBusDxeDriverBindingStart,\r
38 XenBusDxeDriverBindingStop,\r
39 XENBUS_DXE_VERSION,\r
40 NULL,\r
41 NULL\r
42};\r
43\r
44\r
a154f420
AP
45STATIC EFI_LOCK mMyDeviceLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK);\r
46STATIC XENBUS_DEVICE *mMyDevice = NULL;\r
47\r
bbc3758a
AB
48/**\r
49 Map the shared_info_t page into memory.\r
50\r
51 @param Dev A XENBUS_DEVICE instance.\r
52\r
53 @retval EFI_SUCCESS Dev->SharedInfo whill contain a pointer to\r
54 the shared info page\r
55 @retval EFI_LOAD_ERROR The shared info page could not be mapped. The\r
56 hypercall returned an error.\r
57**/\r
58STATIC\r
59EFI_STATUS\r
60XenGetSharedInfoPage (\r
61 IN OUT XENBUS_DEVICE *Dev\r
62 )\r
63{\r
64 xen_add_to_physmap_t Parameter;\r
65\r
66 ASSERT (Dev->SharedInfo == NULL);\r
67\r
68 Parameter.domid = DOMID_SELF;\r
69 Parameter.space = XENMAPSPACE_shared_info;\r
70 Parameter.idx = 0;\r
71\r
72 //\r
73 // using reserved page because the page is not released when Linux is\r
74 // starting because of the add_to_physmap. QEMU might try to access the\r
75 // page, and fail because it have no right to do so (segv).\r
76 //\r
77 Dev->SharedInfo = AllocateReservedPages (1);\r
78 Parameter.gpfn = (UINTN) Dev->SharedInfo >> EFI_PAGE_SHIFT;\r
79 if (XenHypercallMemoryOp (XENMEM_add_to_physmap, &Parameter) != 0) {\r
80 FreePages (Dev->SharedInfo, 1);\r
81 Dev->SharedInfo = NULL;\r
82 return EFI_LOAD_ERROR;\r
83 }\r
84\r
85 return EFI_SUCCESS;\r
86}\r
87\r
e65e8802
AP
88/**\r
89 Unloads an image.\r
90\r
91 @param ImageHandle Handle that identifies the image to be unloaded.\r
92\r
93 @retval EFI_SUCCESS The image has been unloaded.\r
94 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.\r
95\r
96**/\r
97EFI_STATUS\r
98EFIAPI\r
99XenBusDxeUnload (\r
100 IN EFI_HANDLE ImageHandle\r
101 )\r
102{\r
103 EFI_STATUS Status;\r
104\r
105 EFI_HANDLE *HandleBuffer;\r
106 UINTN HandleCount;\r
107 UINTN Index;\r
108\r
109 //\r
110 // Retrieve array of all handles in the handle database\r
111 //\r
112 Status = gBS->LocateHandleBuffer (\r
113 AllHandles,\r
114 NULL,\r
115 NULL,\r
116 &HandleCount,\r
117 &HandleBuffer\r
118 );\r
119 if (EFI_ERROR (Status)) {\r
120 return Status;\r
121 }\r
122\r
123 //\r
124 // Disconnect the current driver from handles in the handle database\r
125 //\r
126 for (Index = 0; Index < HandleCount; Index++) {\r
127 gBS->DisconnectController (HandleBuffer[Index], gImageHandle, NULL);\r
128 }\r
129\r
130 //\r
131 // Free the array of handles\r
132 //\r
133 FreePool (HandleBuffer);\r
134\r
135\r
136 //\r
137 // Uninstall protocols installed in the driver entry point\r
138 //\r
139 Status = gBS->UninstallMultipleProtocolInterfaces (\r
140 ImageHandle,\r
141 &gEfiDriverBindingProtocolGuid, &gXenBusDxeDriverBinding,\r
142 &gEfiComponentNameProtocolGuid, &gXenBusDxeComponentName,\r
143 &gEfiComponentName2ProtocolGuid, &gXenBusDxeComponentName2,\r
144 NULL\r
145 );\r
146 if (EFI_ERROR (Status)) {\r
147 return Status;\r
148 }\r
149\r
150 return EFI_SUCCESS;\r
151}\r
152\r
153/**\r
154 This is the declaration of an EFI image entry point. This entry point is\r
155 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including\r
156 both device drivers and bus drivers.\r
157\r
158 @param ImageHandle The firmware allocated handle for the UEFI image.\r
159 @param SystemTable A pointer to the EFI System Table.\r
160\r
161 @retval EFI_SUCCESS The operation completed successfully.\r
48b3ff04 162 @retval EFI_ABORTED Xen hypercalls are not available.\r
e65e8802
AP
163 @retval Others An unexpected error occurred.\r
164**/\r
165EFI_STATUS\r
166EFIAPI\r
167XenBusDxeDriverEntryPoint (\r
168 IN EFI_HANDLE ImageHandle,\r
169 IN EFI_SYSTEM_TABLE *SystemTable\r
170 )\r
171{\r
172 EFI_STATUS Status;\r
173\r
48b3ff04
LE
174 if (! XenHypercallIsAvailable ()) {\r
175 return EFI_ABORTED;\r
176 }\r
177\r
e65e8802
AP
178 //\r
179 // Install UEFI Driver Model protocol(s).\r
180 //\r
181 Status = EfiLibInstallDriverBindingComponentName2 (\r
182 ImageHandle,\r
183 SystemTable,\r
184 &gXenBusDxeDriverBinding,\r
185 ImageHandle,\r
186 &gXenBusDxeComponentName,\r
187 &gXenBusDxeComponentName2\r
188 );\r
189 ASSERT_EFI_ERROR (Status);\r
190\r
191\r
192 return Status;\r
193}\r
194\r
195\r
196/**\r
197 Tests to see if this driver supports a given controller. If a child device is provided,\r
198 it further tests to see if this driver supports creating a handle for the specified child device.\r
199\r
200 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
201 @param[in] ControllerHandle The handle of the controller to test. This handle\r
202 must support a protocol interface that supplies\r
203 an I/O abstraction to the driver.\r
204 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
205 parameter is ignored by device drivers, and is optional for bus\r
206 drivers. For bus drivers, if this parameter is not NULL, then\r
207 the bus driver must determine if the bus controller specified\r
208 by ControllerHandle and the child controller specified\r
209 by RemainingDevicePath are both supported by this\r
210 bus driver.\r
211\r
212 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
213 RemainingDevicePath is supported by the driver specified by This.\r
214 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
215 RemainingDevicePath is already being managed by the driver\r
216 specified by This.\r
217 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
218 RemainingDevicePath is already being managed by a different\r
219 driver or an application that requires exclusive access.\r
220 Currently not implemented.\r
221 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
222 RemainingDevicePath is not supported by the driver specified by This.\r
223**/\r
224EFI_STATUS\r
225EFIAPI\r
226XenBusDxeDriverBindingSupported (\r
227 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
228 IN EFI_HANDLE ControllerHandle,\r
229 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
230 )\r
231{\r
232 EFI_STATUS Status;\r
d9fdfd85 233 XENIO_PROTOCOL *XenIo;\r
e65e8802
AP
234\r
235 Status = gBS->OpenProtocol (\r
236 ControllerHandle,\r
d9fdfd85
AB
237 &gXenIoProtocolGuid,\r
238 (VOID **)&XenIo,\r
e65e8802
AP
239 This->DriverBindingHandle,\r
240 ControllerHandle,\r
241 EFI_OPEN_PROTOCOL_BY_DRIVER\r
242 );\r
d9fdfd85 243\r
e65e8802
AP
244 if (EFI_ERROR (Status)) {\r
245 return Status;\r
246 }\r
247\r
d9fdfd85 248 gBS->CloseProtocol (ControllerHandle, &gXenIoProtocolGuid,\r
e65e8802
AP
249 This->DriverBindingHandle, ControllerHandle);\r
250\r
251 return Status;\r
252}\r
253\r
a154f420
AP
254VOID\r
255EFIAPI\r
256NotifyExitBoot (\r
257 IN EFI_EVENT Event,\r
258 IN VOID *Context\r
259 )\r
260{\r
261 XENBUS_DEVICE *Dev = Context;\r
262\r
263 gBS->DisconnectController(Dev->ControllerHandle,\r
264 Dev->This->DriverBindingHandle, NULL);\r
265}\r
266\r
e65e8802
AP
267/**\r
268 Starts a bus controller.\r
269\r
270 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
271 As a result, much of the error checking on the parameters to Start() has been moved into this\r
272 common boot service. It is legal to call Start() from other locations,\r
273 but the following calling restrictions must be followed, or the system behavior will not be deterministic.\r
274 1. ControllerHandle must be a valid EFI_HANDLE.\r
275 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
276 EFI_DEVICE_PATH_PROTOCOL.\r
277 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
278 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
279\r
280 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
281 @param[in] ControllerHandle The handle of the controller to start. This handle\r
282 must support a protocol interface that supplies\r
283 an I/O abstraction to the driver.\r
284 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
285 parameter is ignored by device drivers, and is optional for bus\r
286 drivers. For a bus driver, if this parameter is NULL, then handles\r
287 for all the children of Controller are created by this driver.\r
288 If this parameter is not NULL and the first Device Path Node is\r
289 not the End of Device Path Node, then only the handle for the\r
290 child device specified by the first Device Path Node of\r
291 RemainingDevicePath is created by this driver.\r
292 If the first Device Path Node of RemainingDevicePath is\r
293 the End of Device Path Node, no child handle is created by this\r
294 driver.\r
295\r
296 @retval EFI_SUCCESS The device was started.\r
297 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
298 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
abcbbb14
AP
299 @retval EFI_UNSUPPORTED Something is missing on the system that\r
300 prevent to start the edvice.\r
e65e8802
AP
301 @retval Others The driver failded to start the device.\r
302\r
303**/\r
304EFI_STATUS\r
305EFIAPI\r
306XenBusDxeDriverBindingStart (\r
307 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
308 IN EFI_HANDLE ControllerHandle,\r
309 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
310 )\r
311{\r
a154f420
AP
312 EFI_STATUS Status;\r
313 XENBUS_DEVICE *Dev;\r
d9fdfd85 314 XENIO_PROTOCOL *XenIo;\r
86d968e0 315 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
956622c4
AP
316\r
317 Status = gBS->OpenProtocol (\r
318 ControllerHandle,\r
d9fdfd85
AB
319 &gXenIoProtocolGuid,\r
320 (VOID**)&XenIo,\r
956622c4
AP
321 This->DriverBindingHandle,\r
322 ControllerHandle,\r
323 EFI_OPEN_PROTOCOL_BY_DRIVER\r
324 );\r
d9fdfd85 325\r
956622c4
AP
326 if (EFI_ERROR (Status)) {\r
327 return Status;\r
328 }\r
a154f420 329\r
86d968e0
AP
330 Status = gBS->OpenProtocol (\r
331 ControllerHandle,\r
332 &gEfiDevicePathProtocolGuid,\r
333 (VOID **) &DevicePath,\r
334 This->DriverBindingHandle,\r
335 ControllerHandle,\r
336 EFI_OPEN_PROTOCOL_BY_DRIVER\r
337 );\r
338\r
339 if (EFI_ERROR (Status)) {\r
340 goto ErrorOpenningProtocol;\r
341 }\r
342\r
a154f420
AP
343 Dev = AllocateZeroPool (sizeof (*Dev));\r
344 Dev->Signature = XENBUS_DEVICE_SIGNATURE;\r
345 Dev->This = This;\r
346 Dev->ControllerHandle = ControllerHandle;\r
d9fdfd85 347 Dev->XenIo = XenIo;\r
86d968e0
AP
348 Dev->DevicePath = DevicePath;\r
349 InitializeListHead (&Dev->ChildList);\r
a154f420
AP
350\r
351 EfiAcquireLock (&mMyDeviceLock);\r
352 if (mMyDevice != NULL) {\r
353 EfiReleaseLock (&mMyDeviceLock);\r
354 //\r
355 // There is already a XenBus running, only one can be used at a time.\r
356 //\r
357 Status = EFI_ALREADY_STARTED;\r
358 goto ErrorAllocated;\r
359 }\r
360 mMyDevice = Dev;\r
361 EfiReleaseLock (&mMyDeviceLock);\r
362\r
abcbbb14
AP
363 Status = XenGetSharedInfoPage (Dev);\r
364 if (EFI_ERROR (Status)) {\r
365 DEBUG ((EFI_D_ERROR, "XenBus: Unable to get the shared info page.\n"));\r
366 Status = EFI_UNSUPPORTED;\r
367 goto ErrorAllocated;\r
368 }\r
369\r
d9fdfd85 370 XenGrantTableInit (Dev);\r
0fd14246 371\r
a9090a94
AP
372 Status = XenStoreInit (Dev);\r
373 ASSERT_EFI_ERROR (Status);\r
374\r
86d968e0
AP
375 XenBusEnumerateBus (Dev);\r
376\r
a154f420
AP
377 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,\r
378 NotifyExitBoot,\r
379 (VOID*) Dev,\r
380 &Dev->ExitBootEvent);\r
381 ASSERT_EFI_ERROR (Status);\r
382\r
383 return EFI_SUCCESS;\r
384\r
385ErrorAllocated:\r
386 FreePool (Dev);\r
86d968e0
AP
387 gBS->CloseProtocol (ControllerHandle, &gEfiDevicePathProtocolGuid,\r
388 This->DriverBindingHandle, ControllerHandle);\r
389ErrorOpenningProtocol:\r
d9fdfd85 390 gBS->CloseProtocol (ControllerHandle, &gXenIoProtocolGuid,\r
956622c4 391 This->DriverBindingHandle, ControllerHandle);\r
a154f420 392 return Status;\r
e65e8802
AP
393}\r
394\r
395/**\r
396 Stops a bus controller.\r
397\r
398 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
399 As a result, much of the error checking on the parameters to Stop() has been moved\r
400 into this common boot service. It is legal to call Stop() from other locations,\r
401 but the following calling restrictions must be followed, or the system behavior will not be deterministic.\r
402 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
403 same driver's Start() function.\r
404 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
405 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
406 Start() function, and the Start() function must have called OpenProtocol() on\r
407 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
408\r
409 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
410 @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
411 support a bus specific I/O protocol for the driver\r
412 to use to stop the device.\r
413 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
414 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
415 if NumberOfChildren is 0.\r
416\r
417 @retval EFI_SUCCESS The device was stopped.\r
418 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
419\r
420**/\r
421EFI_STATUS\r
422EFIAPI\r
423XenBusDxeDriverBindingStop (\r
424 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
425 IN EFI_HANDLE ControllerHandle,\r
426 IN UINTN NumberOfChildren,\r
427 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r
428 )\r
429{\r
86d968e0
AP
430 UINTN Index;\r
431 XENBUS_PROTOCOL *XenBusIo;\r
432 XENBUS_PRIVATE_DATA *ChildData;\r
433 EFI_STATUS Status;\r
a154f420
AP
434 XENBUS_DEVICE *Dev = mMyDevice;\r
435\r
86d968e0
AP
436 for (Index = 0; Index < NumberOfChildren; Index++) {\r
437 Status = gBS->OpenProtocol (\r
438 ChildHandleBuffer[Index],\r
439 &gXenBusProtocolGuid,\r
440 (VOID **) &XenBusIo,\r
441 This->DriverBindingHandle,\r
442 ControllerHandle,\r
443 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
444 if (EFI_ERROR (Status)) {\r
445 DEBUG ((EFI_D_ERROR, "XenBusDxe: get children protocol failed: %r\n", Status));\r
446 continue;\r
447 }\r
448 ChildData = XENBUS_PRIVATE_DATA_FROM_THIS (XenBusIo);\r
449 Status = gBS->DisconnectController (ChildData->Handle, NULL, NULL);\r
450 if (EFI_ERROR (Status)) {\r
451 DEBUG ((EFI_D_ERROR, "XenBusDxe: error disconnecting child: %r\n",\r
452 Status));\r
453 continue;\r
454 }\r
455\r
456 Status = gBS->UninstallMultipleProtocolInterfaces (\r
457 ChildData->Handle,\r
458 &gEfiDevicePathProtocolGuid, ChildData->DevicePath,\r
459 &gXenBusProtocolGuid, &ChildData->XenBusIo,\r
460 NULL);\r
461 ASSERT_EFI_ERROR (Status);\r
462\r
463 FreePool ((VOID*)ChildData->XenBusIo.Type);\r
464 FreePool ((VOID*)ChildData->XenBusIo.Node);\r
465 FreePool ((VOID*)ChildData->XenBusIo.Backend);\r
466 FreePool (ChildData->DevicePath);\r
467 RemoveEntryList (&ChildData->Link);\r
468 FreePool (ChildData);\r
469 }\r
470 if (NumberOfChildren > 0) {\r
471 return EFI_SUCCESS;\r
472 }\r
473\r
a154f420 474 gBS->CloseEvent (Dev->ExitBootEvent);\r
a9090a94 475 XenStoreDeinit (Dev);\r
0fd14246 476 XenGrantTableDeinit (Dev);\r
a154f420 477\r
86d968e0
AP
478 gBS->CloseProtocol (ControllerHandle, &gEfiDevicePathProtocolGuid,\r
479 This->DriverBindingHandle, ControllerHandle);\r
d9fdfd85 480 gBS->CloseProtocol (ControllerHandle, &gXenIoProtocolGuid,\r
956622c4
AP
481 This->DriverBindingHandle, ControllerHandle);\r
482\r
a154f420
AP
483 mMyDevice = NULL;\r
484 FreePool (Dev);\r
485 return EFI_SUCCESS;\r
e65e8802 486}\r