XenIoPciDxe.c 12 KB

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