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