You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

efi_pci.c 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <ipxe/pci.h>
  23. #include <ipxe/efi/efi.h>
  24. #include <ipxe/efi/efi_pci.h>
  25. #include <ipxe/efi/efi_driver.h>
  26. #include <ipxe/efi/Protocol/PciIo.h>
  27. #include <ipxe/efi/Protocol/PciRootBridgeIo.h>
  28. /** @file
  29. *
  30. * iPXE PCI I/O API for EFI
  31. *
  32. */
  33. /* Disambiguate the various error causes */
  34. #define EINFO_EEFI_PCI \
  35. __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
  36. "Could not open PCI I/O protocol" )
  37. #define EINFO_EEFI_PCI_NOT_PCI \
  38. __einfo_platformify ( EINFO_EEFI_PCI, EFI_UNSUPPORTED, \
  39. "Not a PCI device" )
  40. #define EEFI_PCI_NOT_PCI __einfo_error ( EINFO_EEFI_PCI_NOT_PCI )
  41. #define EINFO_EEFI_PCI_IN_USE \
  42. __einfo_platformify ( EINFO_EEFI_PCI, EFI_ACCESS_DENIED, \
  43. "PCI device already has a driver" )
  44. #define EEFI_PCI_IN_USE __einfo_error ( EINFO_EEFI_PCI_IN_USE )
  45. #define EEFI_PCI( efirc ) \
  46. EPLATFORM ( EINFO_EEFI_PCI, efirc, \
  47. EEFI_PCI_NOT_PCI, EEFI_PCI_IN_USE )
  48. /******************************************************************************
  49. *
  50. * iPXE PCI API
  51. *
  52. ******************************************************************************
  53. */
  54. /** PCI root bridge I/O protocol */
  55. static EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *efipci;
  56. EFI_REQUIRE_PROTOCOL ( EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL, &efipci );
  57. static unsigned long efipci_address ( struct pci_device *pci,
  58. unsigned long location ) {
  59. return EFI_PCI_ADDRESS ( PCI_BUS ( pci->busdevfn ),
  60. PCI_SLOT ( pci->busdevfn ),
  61. PCI_FUNC ( pci->busdevfn ),
  62. EFIPCI_OFFSET ( location ) );
  63. }
  64. int efipci_read ( struct pci_device *pci, unsigned long location,
  65. void *value ) {
  66. EFI_STATUS efirc;
  67. int rc;
  68. if ( ( efirc = efipci->Pci.Read ( efipci, EFIPCI_WIDTH ( location ),
  69. efipci_address ( pci, location ), 1,
  70. value ) ) != 0 ) {
  71. rc = -EEFI ( efirc );
  72. DBG ( "EFIPCI config read from " PCI_FMT " offset %02lx "
  73. "failed: %s\n", PCI_ARGS ( pci ),
  74. EFIPCI_OFFSET ( location ), strerror ( rc ) );
  75. return -EIO;
  76. }
  77. return 0;
  78. }
  79. int efipci_write ( struct pci_device *pci, unsigned long location,
  80. unsigned long value ) {
  81. EFI_STATUS efirc;
  82. int rc;
  83. if ( ( efirc = efipci->Pci.Write ( efipci, EFIPCI_WIDTH ( location ),
  84. efipci_address ( pci, location ), 1,
  85. &value ) ) != 0 ) {
  86. rc = -EEFI ( efirc );
  87. DBG ( "EFIPCI config write to " PCI_FMT " offset %02lx "
  88. "failed: %s\n", PCI_ARGS ( pci ),
  89. EFIPCI_OFFSET ( location ), strerror ( rc ) );
  90. return -EIO;
  91. }
  92. return 0;
  93. }
  94. PROVIDE_PCIAPI_INLINE ( efi, pci_num_bus );
  95. PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_byte );
  96. PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_word );
  97. PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_dword );
  98. PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_byte );
  99. PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_word );
  100. PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_dword );
  101. /******************************************************************************
  102. *
  103. * EFI PCI device instantiation
  104. *
  105. ******************************************************************************
  106. */
  107. /** EFI PCI I/O protocol GUID */
  108. static EFI_GUID efi_pci_io_protocol_guid
  109. = EFI_PCI_IO_PROTOCOL_GUID;
  110. /**
  111. * Open EFI PCI device
  112. *
  113. * @v device EFI device handle
  114. * @v attributes Protocol opening attributes
  115. * @v pci PCI device to fill in
  116. * @ret rc Return status code
  117. */
  118. int efipci_open ( EFI_HANDLE device, UINT32 attributes,
  119. struct pci_device *pci ) {
  120. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  121. union {
  122. EFI_PCI_IO_PROTOCOL *pci_io;
  123. void *interface;
  124. } pci_io;
  125. UINTN pci_segment, pci_bus, pci_dev, pci_fn;
  126. EFI_STATUS efirc;
  127. int rc;
  128. /* See if device is a PCI device */
  129. if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid,
  130. &pci_io.interface, efi_image_handle,
  131. device, attributes ) ) != 0 ) {
  132. rc = -EEFI_PCI ( efirc );
  133. DBGCP ( device, "EFIPCI %p %s cannot open PCI protocols: %s\n",
  134. device, efi_handle_name ( device ), strerror ( rc ) );
  135. goto err_open_protocol;
  136. }
  137. /* Get PCI bus:dev.fn address */
  138. if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment,
  139. &pci_bus, &pci_dev,
  140. &pci_fn ) ) != 0 ) {
  141. rc = -EEFI ( efirc );
  142. DBGC ( device, "EFIPCI %p %s could not get PCI location: %s\n",
  143. device, efi_handle_name ( device ), strerror ( rc ) );
  144. goto err_get_location;
  145. }
  146. DBGC2 ( device, "EFIPCI %p %s is PCI %04lx:%02lx:%02lx.%lx\n", device,
  147. efi_handle_name ( device ), ( ( unsigned long ) pci_segment ),
  148. ( ( unsigned long ) pci_bus ), ( ( unsigned long ) pci_dev ),
  149. ( ( unsigned long ) pci_fn ) );
  150. /* Try to enable I/O cycles, memory cycles, and bus mastering.
  151. * Some platforms will 'helpfully' report errors if these bits
  152. * can't be enabled (for example, if the card doesn't actually
  153. * support I/O cycles). Work around any such platforms by
  154. * enabling bits individually and simply ignoring any errors.
  155. */
  156. pci_io.pci_io->Attributes ( pci_io.pci_io,
  157. EfiPciIoAttributeOperationEnable,
  158. EFI_PCI_IO_ATTRIBUTE_IO, NULL );
  159. pci_io.pci_io->Attributes ( pci_io.pci_io,
  160. EfiPciIoAttributeOperationEnable,
  161. EFI_PCI_IO_ATTRIBUTE_MEMORY, NULL );
  162. pci_io.pci_io->Attributes ( pci_io.pci_io,
  163. EfiPciIoAttributeOperationEnable,
  164. EFI_PCI_IO_ATTRIBUTE_BUS_MASTER, NULL );
  165. /* Populate PCI device */
  166. pci_init ( pci, PCI_BUSDEVFN ( pci_bus, pci_dev, pci_fn ) );
  167. if ( ( rc = pci_read_config ( pci ) ) != 0 ) {
  168. DBGC ( device, "EFIPCI %p %s cannot read PCI configuration: "
  169. "%s\n", device, efi_handle_name ( device ),
  170. strerror ( rc ) );
  171. goto err_pci_read_config;
  172. }
  173. return 0;
  174. err_pci_read_config:
  175. err_get_location:
  176. bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
  177. efi_image_handle, device );
  178. err_open_protocol:
  179. return rc;
  180. }
  181. /**
  182. * Close EFI PCI device
  183. *
  184. * @v device EFI device handle
  185. */
  186. void efipci_close ( EFI_HANDLE device ) {
  187. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  188. bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
  189. efi_image_handle, device );
  190. }
  191. /**
  192. * Get EFI PCI device information
  193. *
  194. * @v device EFI device handle
  195. * @v pci PCI device to fill in
  196. * @ret rc Return status code
  197. */
  198. int efipci_info ( EFI_HANDLE device, struct pci_device *pci ) {
  199. int rc;
  200. /* Open PCI device, if possible */
  201. if ( ( rc = efipci_open ( device, EFI_OPEN_PROTOCOL_GET_PROTOCOL,
  202. pci ) ) != 0 )
  203. return rc;
  204. /* Close PCI device */
  205. efipci_close ( device );
  206. return 0;
  207. }
  208. /******************************************************************************
  209. *
  210. * EFI PCI driver
  211. *
  212. ******************************************************************************
  213. */
  214. /**
  215. * Check to see if driver supports a device
  216. *
  217. * @v device EFI device handle
  218. * @ret rc Return status code
  219. */
  220. static int efipci_supported ( EFI_HANDLE device ) {
  221. struct pci_device pci;
  222. int rc;
  223. /* Get PCI device information */
  224. if ( ( rc = efipci_info ( device, &pci ) ) != 0 )
  225. return rc;
  226. /* Look for a driver */
  227. if ( ( rc = pci_find_driver ( &pci ) ) != 0 ) {
  228. DBGCP ( device, "EFIPCI %p %s has no driver\n",
  229. device, efi_handle_name ( device ) );
  230. return rc;
  231. }
  232. DBGC ( device, "EFIPCI %p %s has driver \"%s\"\n",
  233. device, efi_handle_name ( device ), pci.id->name );
  234. return 0;
  235. }
  236. /**
  237. * Attach driver to device
  238. *
  239. * @v efidev EFI device
  240. * @ret rc Return status code
  241. */
  242. static int efipci_start ( struct efi_device *efidev ) {
  243. EFI_HANDLE device = efidev->device;
  244. struct pci_device *pci;
  245. int rc;
  246. /* Allocate PCI device */
  247. pci = zalloc ( sizeof ( *pci ) );
  248. if ( ! pci ) {
  249. rc = -ENOMEM;
  250. goto err_alloc;
  251. }
  252. /* Open PCI device */
  253. if ( ( rc = efipci_open ( device, ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  254. EFI_OPEN_PROTOCOL_EXCLUSIVE ),
  255. pci ) ) != 0 ) {
  256. DBGC ( device, "EFIPCI %p %s could not open PCI device: %s\n",
  257. device, efi_handle_name ( device ), strerror ( rc ) );
  258. DBGC_EFI_OPENERS ( device, device, &efi_pci_io_protocol_guid );
  259. goto err_open;
  260. }
  261. /* Find driver */
  262. if ( ( rc = pci_find_driver ( pci ) ) != 0 ) {
  263. DBGC ( device, "EFIPCI %p %s has no driver\n",
  264. device, efi_handle_name ( device ) );
  265. goto err_find_driver;
  266. }
  267. /* Mark PCI device as a child of the EFI device */
  268. pci->dev.parent = &efidev->dev;
  269. list_add ( &pci->dev.siblings, &efidev->dev.children );
  270. /* Probe driver */
  271. if ( ( rc = pci_probe ( pci ) ) != 0 ) {
  272. DBGC ( device, "EFIPCI %p %s could not probe driver \"%s\": "
  273. "%s\n", device, efi_handle_name ( device ),
  274. pci->id->name, strerror ( rc ) );
  275. goto err_probe;
  276. }
  277. DBGC ( device, "EFIPCI %p %s using driver \"%s\"\n",
  278. device, efi_handle_name ( device ), pci->id->name );
  279. efidev_set_drvdata ( efidev, pci );
  280. return 0;
  281. pci_remove ( pci );
  282. err_probe:
  283. list_del ( &pci->dev.siblings );
  284. err_find_driver:
  285. efipci_close ( device );
  286. err_open:
  287. free ( pci );
  288. err_alloc:
  289. return rc;
  290. }
  291. /**
  292. * Detach driver from device
  293. *
  294. * @v efidev EFI device
  295. */
  296. static void efipci_stop ( struct efi_device *efidev ) {
  297. struct pci_device *pci = efidev_get_drvdata ( efidev );
  298. EFI_HANDLE device = efidev->device;
  299. pci_remove ( pci );
  300. list_del ( &pci->dev.siblings );
  301. efipci_close ( device );
  302. free ( pci );
  303. }
  304. /** EFI PCI driver */
  305. struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
  306. .name = "PCI",
  307. .supported = efipci_supported,
  308. .start = efipci_start,
  309. .stop = efipci_stop,
  310. };