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