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