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