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