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