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