]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OvmfPkg/XenBusDxe/XenBusDxe.c
MdeModulePkg: Add PciSioSerialDxe driver
[mirror_edk2.git] / OvmfPkg / XenBusDxe / XenBusDxe.c
... / ...
CommitLineData
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 <Library/DebugLib.h>\r
27#include <Library/XenHypercallLib.h>\r
28\r
29#include "XenBusDxe.h"\r
30\r
31#include "GrantTable.h"\r
32#include "XenStore.h"\r
33#include "XenBus.h"\r
34\r
35#include <IndustryStandard/Xen/hvm/params.h>\r
36#include <IndustryStandard/Xen/memory.h>\r
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
51STATIC EFI_LOCK mMyDeviceLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK);\r
52STATIC XENBUS_DEVICE *mMyDevice = NULL;\r
53\r
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
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 EFI_ABORTED Xen hypercalls are not available.\r
169 @retval Others An unexpected error occurred.\r
170**/\r
171EFI_STATUS\r
172EFIAPI\r
173XenBusDxeDriverEntryPoint (\r
174 IN EFI_HANDLE ImageHandle,\r
175 IN EFI_SYSTEM_TABLE *SystemTable\r
176 )\r
177{\r
178 EFI_STATUS Status;\r
179\r
180 if (! XenHypercallIsAvailable ()) {\r
181 return EFI_ABORTED;\r
182 }\r
183\r
184 //\r
185 // Install UEFI Driver Model protocol(s).\r
186 //\r
187 Status = EfiLibInstallDriverBindingComponentName2 (\r
188 ImageHandle,\r
189 SystemTable,\r
190 &gXenBusDxeDriverBinding,\r
191 ImageHandle,\r
192 &gXenBusDxeComponentName,\r
193 &gXenBusDxeComponentName2\r
194 );\r
195 ASSERT_EFI_ERROR (Status);\r
196\r
197\r
198 return Status;\r
199}\r
200\r
201\r
202/**\r
203 Tests to see if this driver supports a given controller. If a child device is provided,\r
204 it further tests to see if this driver supports creating a handle for the specified child device.\r
205\r
206 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
207 @param[in] ControllerHandle The handle of the controller to test. This handle\r
208 must support a protocol interface that supplies\r
209 an I/O abstraction to the driver.\r
210 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
211 parameter is ignored by device drivers, and is optional for bus\r
212 drivers. For bus drivers, if this parameter is not NULL, then\r
213 the bus driver must determine if the bus controller specified\r
214 by ControllerHandle and the child controller specified\r
215 by RemainingDevicePath are both supported by this\r
216 bus driver.\r
217\r
218 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
219 RemainingDevicePath is supported by the driver specified by This.\r
220 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
221 RemainingDevicePath is already being managed by the driver\r
222 specified by This.\r
223 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
224 RemainingDevicePath is already being managed by a different\r
225 driver or an application that requires exclusive access.\r
226 Currently not implemented.\r
227 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
228 RemainingDevicePath is not supported by the driver specified by This.\r
229**/\r
230EFI_STATUS\r
231EFIAPI\r
232XenBusDxeDriverBindingSupported (\r
233 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
234 IN EFI_HANDLE ControllerHandle,\r
235 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
236 )\r
237{\r
238 EFI_STATUS Status;\r
239 XENIO_PROTOCOL *XenIo;\r
240\r
241 Status = gBS->OpenProtocol (\r
242 ControllerHandle,\r
243 &gXenIoProtocolGuid,\r
244 (VOID **)&XenIo,\r
245 This->DriverBindingHandle,\r
246 ControllerHandle,\r
247 EFI_OPEN_PROTOCOL_BY_DRIVER\r
248 );\r
249\r
250 if (EFI_ERROR (Status)) {\r
251 return Status;\r
252 }\r
253\r
254 gBS->CloseProtocol (ControllerHandle, &gXenIoProtocolGuid,\r
255 This->DriverBindingHandle, ControllerHandle);\r
256\r
257 return Status;\r
258}\r
259\r
260VOID\r
261EFIAPI\r
262NotifyExitBoot (\r
263 IN EFI_EVENT Event,\r
264 IN VOID *Context\r
265 )\r
266{\r
267 XENBUS_DEVICE *Dev = Context;\r
268\r
269 gBS->DisconnectController(Dev->ControllerHandle,\r
270 Dev->This->DriverBindingHandle, NULL);\r
271}\r
272\r
273/**\r
274 Starts a bus controller.\r
275\r
276 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
277 As a result, much of the error checking on the parameters to Start() has been moved into this\r
278 common boot service. It is legal to call Start() from other locations,\r
279 but the following calling restrictions must be followed, or the system behavior will not be deterministic.\r
280 1. ControllerHandle must be a valid EFI_HANDLE.\r
281 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
282 EFI_DEVICE_PATH_PROTOCOL.\r
283 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
284 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
285\r
286 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
287 @param[in] ControllerHandle The handle of the controller to start. This handle\r
288 must support a protocol interface that supplies\r
289 an I/O abstraction to the driver.\r
290 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
291 parameter is ignored by device drivers, and is optional for bus\r
292 drivers. For a bus driver, if this parameter is NULL, then handles\r
293 for all the children of Controller are created by this driver.\r
294 If this parameter is not NULL and the first Device Path Node is\r
295 not the End of Device Path Node, then only the handle for the\r
296 child device specified by the first Device Path Node of\r
297 RemainingDevicePath is created by this driver.\r
298 If the first Device Path Node of RemainingDevicePath is\r
299 the End of Device Path Node, no child handle is created by this\r
300 driver.\r
301\r
302 @retval EFI_SUCCESS The device was started.\r
303 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
304 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
305 @retval EFI_UNSUPPORTED Something is missing on the system that\r
306 prevent to start the edvice.\r
307 @retval Others The driver failded to start the device.\r
308\r
309**/\r
310EFI_STATUS\r
311EFIAPI\r
312XenBusDxeDriverBindingStart (\r
313 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
314 IN EFI_HANDLE ControllerHandle,\r
315 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
316 )\r
317{\r
318 EFI_STATUS Status;\r
319 XENBUS_DEVICE *Dev;\r
320 XENIO_PROTOCOL *XenIo;\r
321 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
322\r
323 Status = gBS->OpenProtocol (\r
324 ControllerHandle,\r
325 &gXenIoProtocolGuid,\r
326 (VOID**)&XenIo,\r
327 This->DriverBindingHandle,\r
328 ControllerHandle,\r
329 EFI_OPEN_PROTOCOL_BY_DRIVER\r
330 );\r
331\r
332 if (EFI_ERROR (Status)) {\r
333 return Status;\r
334 }\r
335\r
336 Status = gBS->OpenProtocol (\r
337 ControllerHandle,\r
338 &gEfiDevicePathProtocolGuid,\r
339 (VOID **) &DevicePath,\r
340 This->DriverBindingHandle,\r
341 ControllerHandle,\r
342 EFI_OPEN_PROTOCOL_BY_DRIVER\r
343 );\r
344\r
345 if (EFI_ERROR (Status)) {\r
346 goto ErrorOpenningProtocol;\r
347 }\r
348\r
349 Dev = AllocateZeroPool (sizeof (*Dev));\r
350 Dev->Signature = XENBUS_DEVICE_SIGNATURE;\r
351 Dev->This = This;\r
352 Dev->ControllerHandle = ControllerHandle;\r
353 Dev->XenIo = XenIo;\r
354 Dev->DevicePath = DevicePath;\r
355 InitializeListHead (&Dev->ChildList);\r
356\r
357 EfiAcquireLock (&mMyDeviceLock);\r
358 if (mMyDevice != NULL) {\r
359 EfiReleaseLock (&mMyDeviceLock);\r
360 //\r
361 // There is already a XenBus running, only one can be used at a time.\r
362 //\r
363 Status = EFI_ALREADY_STARTED;\r
364 goto ErrorAllocated;\r
365 }\r
366 mMyDevice = Dev;\r
367 EfiReleaseLock (&mMyDeviceLock);\r
368\r
369 Status = XenGetSharedInfoPage (Dev);\r
370 if (EFI_ERROR (Status)) {\r
371 DEBUG ((EFI_D_ERROR, "XenBus: Unable to get the shared info page.\n"));\r
372 Status = EFI_UNSUPPORTED;\r
373 goto ErrorAllocated;\r
374 }\r
375\r
376 XenGrantTableInit (Dev);\r
377\r
378 Status = XenStoreInit (Dev);\r
379 ASSERT_EFI_ERROR (Status);\r
380\r
381 XenBusEnumerateBus (Dev);\r
382\r
383 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,\r
384 NotifyExitBoot,\r
385 (VOID*) Dev,\r
386 &Dev->ExitBootEvent);\r
387 ASSERT_EFI_ERROR (Status);\r
388\r
389 return EFI_SUCCESS;\r
390\r
391ErrorAllocated:\r
392 FreePool (Dev);\r
393 gBS->CloseProtocol (ControllerHandle, &gEfiDevicePathProtocolGuid,\r
394 This->DriverBindingHandle, ControllerHandle);\r
395ErrorOpenningProtocol:\r
396 gBS->CloseProtocol (ControllerHandle, &gXenIoProtocolGuid,\r
397 This->DriverBindingHandle, ControllerHandle);\r
398 return Status;\r
399}\r
400\r
401/**\r
402 Stops a bus controller.\r
403\r
404 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
405 As a result, much of the error checking on the parameters to Stop() has been moved\r
406 into this common boot service. It is legal to call Stop() from other locations,\r
407 but the following calling restrictions must be followed, or the system behavior will not be deterministic.\r
408 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
409 same driver's Start() function.\r
410 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
411 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
412 Start() function, and the Start() function must have called OpenProtocol() on\r
413 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
414\r
415 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
416 @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
417 support a bus specific I/O protocol for the driver\r
418 to use to stop the device.\r
419 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
420 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
421 if NumberOfChildren is 0.\r
422\r
423 @retval EFI_SUCCESS The device was stopped.\r
424 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
425\r
426**/\r
427EFI_STATUS\r
428EFIAPI\r
429XenBusDxeDriverBindingStop (\r
430 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
431 IN EFI_HANDLE ControllerHandle,\r
432 IN UINTN NumberOfChildren,\r
433 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r
434 )\r
435{\r
436 UINTN Index;\r
437 XENBUS_PROTOCOL *XenBusIo;\r
438 XENBUS_PRIVATE_DATA *ChildData;\r
439 EFI_STATUS Status;\r
440 XENBUS_DEVICE *Dev = mMyDevice;\r
441\r
442 for (Index = 0; Index < NumberOfChildren; Index++) {\r
443 Status = gBS->OpenProtocol (\r
444 ChildHandleBuffer[Index],\r
445 &gXenBusProtocolGuid,\r
446 (VOID **) &XenBusIo,\r
447 This->DriverBindingHandle,\r
448 ControllerHandle,\r
449 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
450 if (EFI_ERROR (Status)) {\r
451 DEBUG ((EFI_D_ERROR, "XenBusDxe: get children protocol failed: %r\n", Status));\r
452 continue;\r
453 }\r
454 ChildData = XENBUS_PRIVATE_DATA_FROM_THIS (XenBusIo);\r
455 Status = gBS->DisconnectController (ChildData->Handle, NULL, NULL);\r
456 if (EFI_ERROR (Status)) {\r
457 DEBUG ((EFI_D_ERROR, "XenBusDxe: error disconnecting child: %r\n",\r
458 Status));\r
459 continue;\r
460 }\r
461\r
462 Status = gBS->UninstallMultipleProtocolInterfaces (\r
463 ChildData->Handle,\r
464 &gEfiDevicePathProtocolGuid, ChildData->DevicePath,\r
465 &gXenBusProtocolGuid, &ChildData->XenBusIo,\r
466 NULL);\r
467 ASSERT_EFI_ERROR (Status);\r
468\r
469 FreePool ((VOID*)ChildData->XenBusIo.Type);\r
470 FreePool ((VOID*)ChildData->XenBusIo.Node);\r
471 FreePool ((VOID*)ChildData->XenBusIo.Backend);\r
472 FreePool (ChildData->DevicePath);\r
473 RemoveEntryList (&ChildData->Link);\r
474 FreePool (ChildData);\r
475 }\r
476 if (NumberOfChildren > 0) {\r
477 return EFI_SUCCESS;\r
478 }\r
479\r
480 gBS->CloseEvent (Dev->ExitBootEvent);\r
481 XenStoreDeinit (Dev);\r
482 XenGrantTableDeinit (Dev);\r
483\r
484 gBS->CloseProtocol (ControllerHandle, &gEfiDevicePathProtocolGuid,\r
485 This->DriverBindingHandle, ControllerHandle);\r
486 gBS->CloseProtocol (ControllerHandle, &gXenIoProtocolGuid,\r
487 This->DriverBindingHandle, ControllerHandle);\r
488\r
489 mMyDevice = NULL;\r
490 FreePool (Dev);\r
491 return EFI_SUCCESS;\r
492}\r