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