]>
Commit | Line | Data |
---|---|---|
f2162d34 AB |
1 | /** @file\r |
2 | \r | |
3 | Driver for the virtual Xen PCI device\r | |
4 | \r | |
5 | Copyright (C) 2012, Red Hat, Inc.\r | |
694673c9 | 6 | Copyright (c) 2012 - 2016, Intel Corporation. All rights reserved.<BR>\r |
f2162d34 AB |
7 | Copyright (C) 2013, ARM Ltd.\r |
8 | Copyright (C) 2015, Linaro Ltd.\r | |
9 | \r | |
10 | This program and the accompanying materials are licensed and made available\r | |
11 | under the terms and conditions of the BSD License which accompanies this\r | |
12 | distribution. The full text of the license may be found at\r | |
13 | http://opensource.org/licenses/bsd-license.php\r | |
14 | \r | |
15 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r | |
16 | WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
17 | \r | |
18 | **/\r | |
19 | \r | |
20 | #include <IndustryStandard/Acpi.h>\r | |
21 | #include <IndustryStandard/Pci.h>\r | |
22 | #include <Library/BaseMemoryLib.h>\r | |
23 | #include <Library/DebugLib.h>\r | |
24 | #include <Library/MemoryAllocationLib.h>\r | |
25 | #include <Library/UefiBootServicesTableLib.h>\r | |
26 | #include <Library/UefiLib.h>\r | |
27 | \r | |
28 | #include <Protocol/PciIo.h>\r | |
29 | #include <Protocol/XenIo.h>\r | |
30 | \r | |
31 | #define PCI_VENDOR_ID_XEN 0x5853\r | |
32 | #define PCI_DEVICE_ID_XEN_PLATFORM 0x0001\r | |
33 | \r | |
34 | /**\r | |
35 | \r | |
36 | Device probe function for this driver.\r | |
37 | \r | |
38 | The DXE core calls this function for any given device in order to see if the\r | |
39 | driver can drive the device.\r | |
40 | \r | |
41 | @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object\r | |
42 | incorporating this driver (independently of\r | |
43 | any device).\r | |
44 | \r | |
45 | @param[in] DeviceHandle The device to probe.\r | |
46 | \r | |
47 | @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.\r | |
48 | \r | |
49 | \r | |
50 | @retval EFI_SUCCESS The driver supports the device being probed.\r | |
51 | \r | |
52 | @retval EFI_UNSUPPORTED The driver does not support the device being probed.\r | |
53 | \r | |
54 | @return Error codes from the OpenProtocol() boot service or\r | |
55 | the PciIo protocol.\r | |
56 | \r | |
57 | **/\r | |
58 | STATIC\r | |
59 | EFI_STATUS\r | |
60 | EFIAPI\r | |
61 | XenIoPciDeviceBindingSupported (\r | |
62 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
63 | IN EFI_HANDLE DeviceHandle,\r | |
64 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r | |
65 | )\r | |
66 | {\r | |
67 | EFI_STATUS Status;\r | |
68 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
69 | PCI_TYPE00 Pci;\r | |
70 | \r | |
71 | //\r | |
72 | // Attempt to open the device with the PciIo set of interfaces. On success,\r | |
73 | // the protocol is "instantiated" for the PCI device. Covers duplicate open\r | |
74 | // attempts (EFI_ALREADY_STARTED).\r | |
75 | //\r | |
76 | Status = gBS->OpenProtocol (\r | |
77 | DeviceHandle, // candidate device\r | |
78 | &gEfiPciIoProtocolGuid, // for generic PCI access\r | |
79 | (VOID **)&PciIo, // handle to instantiate\r | |
80 | This->DriverBindingHandle, // requestor driver identity\r | |
81 | DeviceHandle, // ControllerHandle, according to\r | |
82 | // the UEFI Driver Model\r | |
83 | EFI_OPEN_PROTOCOL_BY_DRIVER // get exclusive PciIo access to\r | |
84 | // the device; to be released\r | |
85 | );\r | |
86 | if (EFI_ERROR (Status)) {\r | |
87 | return Status;\r | |
88 | }\r | |
89 | \r | |
90 | //\r | |
91 | // Read entire PCI configuration header for more extensive check ahead.\r | |
92 | //\r | |
93 | Status = PciIo->Pci.Read (\r | |
94 | PciIo, // (protocol, device)\r | |
95 | // handle\r | |
96 | EfiPciIoWidthUint32, // access width & copy\r | |
97 | // mode\r | |
98 | 0, // Offset\r | |
99 | sizeof Pci / sizeof (UINT32), // Count\r | |
100 | &Pci // target buffer\r | |
101 | );\r | |
102 | \r | |
103 | if (Status == EFI_SUCCESS) {\r | |
104 | if ((Pci.Hdr.VendorId == PCI_VENDOR_ID_XEN) &&\r | |
105 | (Pci.Hdr.DeviceId == PCI_DEVICE_ID_XEN_PLATFORM)) {\r | |
106 | Status = EFI_SUCCESS;\r | |
107 | } else {\r | |
108 | Status = EFI_UNSUPPORTED;\r | |
109 | }\r | |
110 | }\r | |
111 | \r | |
112 | //\r | |
113 | // We needed PCI IO access only transitorily, to see whether we support the\r | |
114 | // device or not.\r | |
115 | //\r | |
116 | gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,\r | |
117 | This->DriverBindingHandle, DeviceHandle);\r | |
118 | \r | |
119 | return Status;\r | |
120 | }\r | |
121 | \r | |
122 | /**\r | |
123 | \r | |
124 | After we've pronounced support for a specific device in\r | |
125 | DriverBindingSupported(), we start managing said device (passed in by the\r | |
694673c9 | 126 | Driver Execution Environment) with the following service.\r |
f2162d34 AB |
127 | \r |
128 | See DriverBindingSupported() for specification references.\r | |
129 | \r | |
130 | @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object\r | |
131 | incorporating this driver (independently of\r | |
132 | any device).\r | |
133 | \r | |
134 | @param[in] DeviceHandle The supported device to drive.\r | |
135 | \r | |
136 | @param[in] RemainingDevicePath Relevant only for bus drivers, ignored.\r | |
137 | \r | |
138 | \r | |
139 | @retval EFI_SUCCESS The device was started.\r | |
140 | \r | |
141 | @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r | |
142 | \r | |
143 | @return Error codes from the OpenProtocol() boot\r | |
144 | service, the PciIo protocol or the\r | |
145 | InstallProtocolInterface() boot service.\r | |
146 | \r | |
147 | **/\r | |
148 | STATIC\r | |
149 | EFI_STATUS\r | |
150 | EFIAPI\r | |
151 | XenIoPciDeviceBindingStart (\r | |
152 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
153 | IN EFI_HANDLE DeviceHandle,\r | |
154 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r | |
155 | )\r | |
156 | {\r | |
157 | EFI_STATUS Status;\r | |
158 | XENIO_PROTOCOL *XenIo;\r | |
159 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
160 | EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *BarDesc;\r | |
161 | \r | |
162 | XenIo = (XENIO_PROTOCOL *) AllocateZeroPool (sizeof *XenIo);\r | |
163 | if (XenIo == NULL) {\r | |
164 | return EFI_OUT_OF_RESOURCES;\r | |
165 | }\r | |
166 | \r | |
167 | Status = gBS->OpenProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,\r | |
168 | (VOID **)&PciIo, This->DriverBindingHandle,\r | |
169 | DeviceHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);\r | |
170 | if (EFI_ERROR (Status)) {\r | |
171 | goto FreeXenIo;\r | |
172 | }\r | |
173 | \r | |
174 | //\r | |
175 | // The BAR1 of this PCI device is used for shared memory and is supposed to\r | |
176 | // look like MMIO. The address space of the BAR1 will be used to map the\r | |
177 | // Grant Table.\r | |
178 | //\r | |
179 | Status = PciIo->GetBarAttributes (PciIo, PCI_BAR_IDX1, NULL, (VOID**) &BarDesc);\r | |
180 | ASSERT_EFI_ERROR (Status);\r | |
181 | ASSERT (BarDesc->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM);\r | |
182 | \r | |
183 | /* Get a Memory address for mapping the Grant Table. */\r | |
184 | DEBUG ((EFI_D_INFO, "XenIoPci: BAR at %LX\n", BarDesc->AddrRangeMin));\r | |
185 | XenIo->GrantTableAddress = BarDesc->AddrRangeMin;\r | |
186 | FreePool (BarDesc);\r | |
187 | \r | |
188 | Status = gBS->InstallProtocolInterface (&DeviceHandle,\r | |
189 | &gXenIoProtocolGuid, EFI_NATIVE_INTERFACE, XenIo);\r | |
190 | \r | |
191 | if (!EFI_ERROR (Status)) {\r | |
192 | return EFI_SUCCESS;\r | |
193 | }\r | |
194 | \r | |
195 | gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,\r | |
196 | This->DriverBindingHandle, DeviceHandle);\r | |
197 | \r | |
198 | FreeXenIo:\r | |
199 | FreePool (XenIo);\r | |
200 | \r | |
201 | return Status;\r | |
202 | }\r | |
203 | \r | |
204 | /**\r | |
205 | \r | |
206 | Stop driving the XenIo PCI device\r | |
207 | \r | |
208 | @param[in] This The EFI_DRIVER_BINDING_PROTOCOL object\r | |
209 | incorporating this driver (independently of any\r | |
210 | device).\r | |
211 | \r | |
212 | @param[in] DeviceHandle Stop driving this device.\r | |
213 | \r | |
214 | @param[in] NumberOfChildren Since this function belongs to a device driver\r | |
215 | only (as opposed to a bus driver), the caller\r | |
216 | environment sets NumberOfChildren to zero, and\r | |
217 | we ignore it.\r | |
218 | \r | |
219 | @param[in] ChildHandleBuffer Ignored (corresponding to NumberOfChildren).\r | |
220 | \r | |
221 | @retval EFI_SUCCESS Driver instance has been stopped and the PCI\r | |
222 | configuration attributes have been restored.\r | |
223 | \r | |
224 | @return Error codes from the OpenProtocol() or\r | |
225 | CloseProtocol(), UninstallProtocolInterface()\r | |
226 | boot services.\r | |
227 | \r | |
228 | **/\r | |
229 | STATIC\r | |
230 | EFI_STATUS\r | |
231 | EFIAPI\r | |
232 | XenIoPciDeviceBindingStop (\r | |
233 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
234 | IN EFI_HANDLE DeviceHandle,\r | |
235 | IN UINTN NumberOfChildren,\r | |
236 | IN EFI_HANDLE *ChildHandleBuffer\r | |
237 | )\r | |
238 | {\r | |
239 | EFI_STATUS Status;\r | |
240 | XENIO_PROTOCOL *XenIo;\r | |
241 | \r | |
242 | Status = gBS->OpenProtocol (\r | |
243 | DeviceHandle, // candidate device\r | |
244 | &gXenIoProtocolGuid, // retrieve the XenIo iface\r | |
245 | (VOID **)&XenIo, // target pointer\r | |
246 | This->DriverBindingHandle, // requestor driver identity\r | |
247 | DeviceHandle, // requesting lookup for dev.\r | |
248 | EFI_OPEN_PROTOCOL_GET_PROTOCOL // lookup only, no ref. added\r | |
249 | );\r | |
250 | if (EFI_ERROR (Status)) {\r | |
251 | return Status;\r | |
252 | }\r | |
253 | \r | |
254 | //\r | |
255 | // Handle Stop() requests for in-use driver instances gracefully.\r | |
256 | //\r | |
257 | Status = gBS->UninstallProtocolInterface (DeviceHandle,\r | |
258 | &gXenIoProtocolGuid, XenIo);\r | |
259 | if (EFI_ERROR (Status)) {\r | |
260 | return Status;\r | |
261 | }\r | |
262 | \r | |
263 | Status = gBS->CloseProtocol (DeviceHandle, &gEfiPciIoProtocolGuid,\r | |
264 | This->DriverBindingHandle, DeviceHandle);\r | |
265 | \r | |
266 | FreePool (XenIo);\r | |
267 | \r | |
268 | return Status;\r | |
269 | }\r | |
270 | \r | |
271 | \r | |
272 | //\r | |
273 | // The static object that groups the Supported() (ie. probe), Start() and\r | |
274 | // Stop() functions of the driver together. Refer to UEFI Spec 2.3.1 + Errata\r | |
275 | // C, 10.1 EFI Driver Binding Protocol.\r | |
276 | //\r | |
277 | STATIC EFI_DRIVER_BINDING_PROTOCOL gDriverBinding = {\r | |
278 | &XenIoPciDeviceBindingSupported,\r | |
279 | &XenIoPciDeviceBindingStart,\r | |
280 | &XenIoPciDeviceBindingStop,\r | |
281 | 0x10, // Version, must be in [0x10 .. 0xFFFFFFEF] for IHV-developed drivers\r | |
282 | NULL, // ImageHandle, to be overwritten by\r | |
283 | // EfiLibInstallDriverBindingComponentName2() in XenIoPciDeviceEntryPoint()\r | |
284 | NULL // DriverBindingHandle, ditto\r | |
285 | };\r | |
286 | \r | |
287 | \r | |
288 | //\r | |
289 | // The purpose of the following scaffolding (EFI_COMPONENT_NAME_PROTOCOL and\r | |
290 | // EFI_COMPONENT_NAME2_PROTOCOL implementation) is to format the driver's name\r | |
291 | // in English, for display on standard console devices. This is recommended for\r | |
292 | // UEFI drivers that follow the UEFI Driver Model. Refer to the Driver Writer's\r | |
293 | // Guide for UEFI 2.3.1 v1.01, 11 UEFI Driver and Controller Names.\r | |
294 | //\r | |
295 | STATIC\r | |
296 | EFI_UNICODE_STRING_TABLE mDriverNameTable[] = {\r | |
297 | { "eng;en", L"XenIo PCI Driver" },\r | |
298 | { NULL, NULL }\r | |
299 | };\r | |
300 | \r | |
301 | STATIC\r | |
302 | EFI_COMPONENT_NAME_PROTOCOL gComponentName;\r | |
303 | \r | |
304 | EFI_STATUS\r | |
305 | EFIAPI\r | |
306 | XenIoPciGetDriverName (\r | |
307 | IN EFI_COMPONENT_NAME_PROTOCOL *This,\r | |
308 | IN CHAR8 *Language,\r | |
309 | OUT CHAR16 **DriverName\r | |
310 | )\r | |
311 | {\r | |
312 | return LookupUnicodeString2 (\r | |
313 | Language,\r | |
314 | This->SupportedLanguages,\r | |
315 | mDriverNameTable,\r | |
316 | DriverName,\r | |
317 | (BOOLEAN)(This == &gComponentName) // Iso639Language\r | |
318 | );\r | |
319 | }\r | |
320 | \r | |
321 | EFI_STATUS\r | |
322 | EFIAPI\r | |
323 | XenIoPciGetDeviceName (\r | |
324 | IN EFI_COMPONENT_NAME_PROTOCOL *This,\r | |
325 | IN EFI_HANDLE DeviceHandle,\r | |
326 | IN EFI_HANDLE ChildHandle,\r | |
327 | IN CHAR8 *Language,\r | |
328 | OUT CHAR16 **ControllerName\r | |
329 | )\r | |
330 | {\r | |
331 | return EFI_UNSUPPORTED;\r | |
332 | }\r | |
333 | \r | |
334 | STATIC\r | |
335 | EFI_COMPONENT_NAME_PROTOCOL gComponentName = {\r | |
336 | &XenIoPciGetDriverName,\r | |
337 | &XenIoPciGetDeviceName,\r | |
338 | "eng" // SupportedLanguages, ISO 639-2 language codes\r | |
339 | };\r | |
340 | \r | |
341 | STATIC\r | |
342 | EFI_COMPONENT_NAME2_PROTOCOL gComponentName2 = {\r | |
343 | (EFI_COMPONENT_NAME2_GET_DRIVER_NAME) &XenIoPciGetDriverName,\r | |
344 | (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME) &XenIoPciGetDeviceName,\r | |
345 | "en" // SupportedLanguages, RFC 4646 language codes\r | |
346 | };\r | |
347 | \r | |
348 | \r | |
349 | //\r | |
350 | // Entry point of this driver.\r | |
351 | //\r | |
352 | EFI_STATUS\r | |
353 | EFIAPI\r | |
354 | XenIoPciDeviceEntryPoint (\r | |
355 | IN EFI_HANDLE ImageHandle,\r | |
356 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
357 | )\r | |
358 | {\r | |
359 | return EfiLibInstallDriverBindingComponentName2 (\r | |
360 | ImageHandle,\r | |
361 | SystemTable,\r | |
362 | &gDriverBinding,\r | |
363 | ImageHandle,\r | |
364 | &gComponentName,\r | |
365 | &gComponentName2\r | |
366 | );\r | |
367 | }\r |