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