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