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