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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <ipxe/pci.h>
  27. #include <ipxe/efi/efi.h>
  28. #include <ipxe/efi/efi_pci.h>
  29. #include <ipxe/efi/efi_driver.h>
  30. #include <ipxe/efi/Protocol/PciIo.h>
  31. #include <ipxe/efi/Protocol/PciRootBridgeIo.h>
  32. /** @file
  33. *
  34. * iPXE PCI I/O API for EFI
  35. *
  36. */
  37. /* Disambiguate the various error causes */
  38. #define EINFO_EEFI_PCI \
  39. __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
  40. "Could not open PCI I/O protocol" )
  41. #define EINFO_EEFI_PCI_NOT_PCI \
  42. __einfo_platformify ( EINFO_EEFI_PCI, EFI_UNSUPPORTED, \
  43. "Not a PCI device" )
  44. #define EEFI_PCI_NOT_PCI __einfo_error ( EINFO_EEFI_PCI_NOT_PCI )
  45. #define EINFO_EEFI_PCI_IN_USE \
  46. __einfo_platformify ( EINFO_EEFI_PCI, EFI_ACCESS_DENIED, \
  47. "PCI device already has a driver" )
  48. #define EEFI_PCI_IN_USE __einfo_error ( EINFO_EEFI_PCI_IN_USE )
  49. #define EEFI_PCI( efirc ) \
  50. EPLATFORM ( EINFO_EEFI_PCI, efirc, \
  51. EEFI_PCI_NOT_PCI, EEFI_PCI_IN_USE )
  52. /******************************************************************************
  53. *
  54. * iPXE PCI API
  55. *
  56. ******************************************************************************
  57. */
  58. /**
  59. * Locate EFI PCI root bridge I/O protocol
  60. *
  61. * @v pci PCI device
  62. * @ret handle EFI PCI root bridge handle
  63. * @ret root EFI PCI root bridge I/O protocol, or NULL if not found
  64. * @ret rc Return status code
  65. */
  66. static int efipci_root ( struct pci_device *pci, EFI_HANDLE *handle,
  67. EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL **root ) {
  68. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  69. EFI_HANDLE *handles;
  70. UINTN num_handles;
  71. union {
  72. void *interface;
  73. EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root;
  74. } u;
  75. EFI_STATUS efirc;
  76. UINTN i;
  77. int rc;
  78. /* Enumerate all handles */
  79. if ( ( efirc = bs->LocateHandleBuffer ( ByProtocol,
  80. &efi_pci_root_bridge_io_protocol_guid,
  81. NULL, &num_handles, &handles ) ) != 0 ) {
  82. rc = -EEFI ( efirc );
  83. DBGC ( pci, "EFIPCI cannot locate root bridges: %s\n",
  84. strerror ( rc ) );
  85. goto err_locate;
  86. }
  87. /* Look for matching root bridge I/O protocol */
  88. for ( i = 0 ; i < num_handles ; i++ ) {
  89. *handle = handles[i];
  90. if ( ( efirc = bs->OpenProtocol ( *handle,
  91. &efi_pci_root_bridge_io_protocol_guid,
  92. &u.interface, efi_image_handle, *handle,
  93. EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
  94. rc = -EEFI ( efirc );
  95. DBGC ( pci, "EFIPCI cannot open %s: %s\n",
  96. efi_handle_name ( *handle ), strerror ( rc ) );
  97. continue;
  98. }
  99. if ( u.root->SegmentNumber == PCI_SEG ( pci->busdevfn ) ) {
  100. *root = u.root;
  101. bs->FreePool ( handles );
  102. return 0;
  103. }
  104. bs->CloseProtocol ( *handle,
  105. &efi_pci_root_bridge_io_protocol_guid,
  106. efi_image_handle, *handle );
  107. }
  108. DBGC ( pci, "EFIPCI found no root bridge for " PCI_FMT "\n",
  109. PCI_ARGS ( pci ) );
  110. rc = -ENOENT;
  111. bs->FreePool ( handles );
  112. err_locate:
  113. return rc;
  114. }
  115. /**
  116. * Calculate EFI PCI configuration space address
  117. *
  118. * @v pci PCI device
  119. * @v location Encoded offset and width
  120. * @ret address EFI PCI address
  121. */
  122. static unsigned long efipci_address ( struct pci_device *pci,
  123. unsigned long location ) {
  124. return EFI_PCI_ADDRESS ( PCI_BUS ( pci->busdevfn ),
  125. PCI_SLOT ( pci->busdevfn ),
  126. PCI_FUNC ( pci->busdevfn ),
  127. EFIPCI_OFFSET ( location ) );
  128. }
  129. /**
  130. * Read from PCI configuration space
  131. *
  132. * @v pci PCI device
  133. * @v location Encoded offset and width
  134. * @ret value Value
  135. * @ret rc Return status code
  136. */
  137. int efipci_read ( struct pci_device *pci, unsigned long location,
  138. void *value ) {
  139. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  140. EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root;
  141. EFI_HANDLE handle;
  142. EFI_STATUS efirc;
  143. int rc;
  144. /* Identify root bridge */
  145. if ( ( rc = efipci_root ( pci, &handle, &root ) ) != 0 )
  146. goto err_root;
  147. /* Read from configuration space */
  148. if ( ( efirc = root->Pci.Read ( root, EFIPCI_WIDTH ( location ),
  149. efipci_address ( pci, location ), 1,
  150. value ) ) != 0 ) {
  151. rc = -EEFI ( efirc );
  152. DBG ( "EFIPCI config read from " PCI_FMT " offset %02lx "
  153. "failed: %s\n", PCI_ARGS ( pci ),
  154. EFIPCI_OFFSET ( location ), strerror ( rc ) );
  155. goto err_read;
  156. }
  157. err_read:
  158. bs->CloseProtocol ( handle, &efi_pci_root_bridge_io_protocol_guid,
  159. efi_image_handle, handle );
  160. err_root:
  161. return rc;
  162. }
  163. /**
  164. * Write to PCI configuration space
  165. *
  166. * @v pci PCI device
  167. * @v location Encoded offset and width
  168. * @v value Value
  169. * @ret rc Return status code
  170. */
  171. int efipci_write ( struct pci_device *pci, unsigned long location,
  172. unsigned long value ) {
  173. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  174. EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root;
  175. EFI_HANDLE handle;
  176. EFI_STATUS efirc;
  177. int rc;
  178. /* Identify root bridge */
  179. if ( ( rc = efipci_root ( pci, &handle, &root ) ) != 0 )
  180. goto err_root;
  181. /* Read from configuration space */
  182. if ( ( efirc = root->Pci.Write ( root, EFIPCI_WIDTH ( location ),
  183. efipci_address ( pci, location ), 1,
  184. &value ) ) != 0 ) {
  185. rc = -EEFI ( efirc );
  186. DBG ( "EFIPCI config write to " PCI_FMT " offset %02lx "
  187. "failed: %s\n", PCI_ARGS ( pci ),
  188. EFIPCI_OFFSET ( location ), strerror ( rc ) );
  189. goto err_write;
  190. }
  191. err_write:
  192. bs->CloseProtocol ( handle, &efi_pci_root_bridge_io_protocol_guid,
  193. efi_image_handle, handle );
  194. err_root:
  195. return rc;
  196. }
  197. PROVIDE_PCIAPI_INLINE ( efi, pci_num_bus );
  198. PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_byte );
  199. PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_word );
  200. PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_dword );
  201. PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_byte );
  202. PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_word );
  203. PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_dword );
  204. /******************************************************************************
  205. *
  206. * EFI PCI device instantiation
  207. *
  208. ******************************************************************************
  209. */
  210. /**
  211. * Open EFI PCI device
  212. *
  213. * @v device EFI device handle
  214. * @v attributes Protocol opening attributes
  215. * @v pci PCI device to fill in
  216. * @ret rc Return status code
  217. */
  218. int efipci_open ( EFI_HANDLE device, UINT32 attributes,
  219. struct pci_device *pci ) {
  220. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  221. union {
  222. EFI_PCI_IO_PROTOCOL *pci_io;
  223. void *interface;
  224. } pci_io;
  225. UINTN pci_segment, pci_bus, pci_dev, pci_fn;
  226. unsigned int busdevfn;
  227. EFI_STATUS efirc;
  228. int rc;
  229. /* See if device is a PCI device */
  230. if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid,
  231. &pci_io.interface, efi_image_handle,
  232. device, attributes ) ) != 0 ) {
  233. rc = -EEFI_PCI ( efirc );
  234. DBGCP ( device, "EFIPCI %s cannot open PCI protocols: %s\n",
  235. efi_handle_name ( device ), strerror ( rc ) );
  236. goto err_open_protocol;
  237. }
  238. /* Get PCI bus:dev.fn address */
  239. if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment,
  240. &pci_bus, &pci_dev,
  241. &pci_fn ) ) != 0 ) {
  242. rc = -EEFI ( efirc );
  243. DBGC ( device, "EFIPCI %s could not get PCI location: %s\n",
  244. efi_handle_name ( device ), strerror ( rc ) );
  245. goto err_get_location;
  246. }
  247. DBGC2 ( device, "EFIPCI %s is PCI %04lx:%02lx:%02lx.%lx\n",
  248. efi_handle_name ( device ), ( ( unsigned long ) pci_segment ),
  249. ( ( unsigned long ) pci_bus ), ( ( unsigned long ) pci_dev ),
  250. ( ( unsigned long ) pci_fn ) );
  251. /* Try to enable I/O cycles, memory cycles, and bus mastering.
  252. * Some platforms will 'helpfully' report errors if these bits
  253. * can't be enabled (for example, if the card doesn't actually
  254. * support I/O cycles). Work around any such platforms by
  255. * enabling bits individually and simply ignoring any errors.
  256. */
  257. pci_io.pci_io->Attributes ( pci_io.pci_io,
  258. EfiPciIoAttributeOperationEnable,
  259. EFI_PCI_IO_ATTRIBUTE_IO, NULL );
  260. pci_io.pci_io->Attributes ( pci_io.pci_io,
  261. EfiPciIoAttributeOperationEnable,
  262. EFI_PCI_IO_ATTRIBUTE_MEMORY, NULL );
  263. pci_io.pci_io->Attributes ( pci_io.pci_io,
  264. EfiPciIoAttributeOperationEnable,
  265. EFI_PCI_IO_ATTRIBUTE_BUS_MASTER, NULL );
  266. /* Populate PCI device */
  267. busdevfn = PCI_BUSDEVFN ( pci_segment, pci_bus, pci_dev, pci_fn );
  268. pci_init ( pci, busdevfn );
  269. if ( ( rc = pci_read_config ( pci ) ) != 0 ) {
  270. DBGC ( device, "EFIPCI %s cannot read PCI configuration: %s\n",
  271. efi_handle_name ( device ), strerror ( rc ) );
  272. goto err_pci_read_config;
  273. }
  274. return 0;
  275. err_pci_read_config:
  276. err_get_location:
  277. bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
  278. efi_image_handle, device );
  279. err_open_protocol:
  280. return rc;
  281. }
  282. /**
  283. * Close EFI PCI device
  284. *
  285. * @v device EFI device handle
  286. */
  287. void efipci_close ( EFI_HANDLE device ) {
  288. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  289. bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
  290. efi_image_handle, device );
  291. }
  292. /**
  293. * Get EFI PCI device information
  294. *
  295. * @v device EFI device handle
  296. * @v pci PCI device to fill in
  297. * @ret rc Return status code
  298. */
  299. int efipci_info ( EFI_HANDLE device, struct pci_device *pci ) {
  300. int rc;
  301. /* Open PCI device, if possible */
  302. if ( ( rc = efipci_open ( device, EFI_OPEN_PROTOCOL_GET_PROTOCOL,
  303. pci ) ) != 0 )
  304. return rc;
  305. /* Close PCI device */
  306. efipci_close ( device );
  307. return 0;
  308. }
  309. /******************************************************************************
  310. *
  311. * EFI PCI driver
  312. *
  313. ******************************************************************************
  314. */
  315. /**
  316. * Check to see if driver supports a device
  317. *
  318. * @v device EFI device handle
  319. * @ret rc Return status code
  320. */
  321. static int efipci_supported ( EFI_HANDLE device ) {
  322. struct pci_device pci;
  323. int rc;
  324. /* Get PCI device information */
  325. if ( ( rc = efipci_info ( device, &pci ) ) != 0 )
  326. return rc;
  327. /* Look for a driver */
  328. if ( ( rc = pci_find_driver ( &pci ) ) != 0 ) {
  329. DBGCP ( device, "EFIPCI %s has no driver\n",
  330. efi_handle_name ( device ) );
  331. return rc;
  332. }
  333. DBGC ( device, "EFIPCI %s has driver \"%s\"\n",
  334. efi_handle_name ( device ), pci.id->name );
  335. return 0;
  336. }
  337. /**
  338. * Attach driver to device
  339. *
  340. * @v efidev EFI device
  341. * @ret rc Return status code
  342. */
  343. static int efipci_start ( struct efi_device *efidev ) {
  344. EFI_HANDLE device = efidev->device;
  345. struct pci_device *pci;
  346. int rc;
  347. /* Allocate PCI device */
  348. pci = zalloc ( sizeof ( *pci ) );
  349. if ( ! pci ) {
  350. rc = -ENOMEM;
  351. goto err_alloc;
  352. }
  353. /* Open PCI device */
  354. if ( ( rc = efipci_open ( device, ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  355. EFI_OPEN_PROTOCOL_EXCLUSIVE ),
  356. pci ) ) != 0 ) {
  357. DBGC ( device, "EFIPCI %s could not open PCI device: %s\n",
  358. efi_handle_name ( device ), strerror ( rc ) );
  359. DBGC_EFI_OPENERS ( device, device, &efi_pci_io_protocol_guid );
  360. goto err_open;
  361. }
  362. /* Find driver */
  363. if ( ( rc = pci_find_driver ( pci ) ) != 0 ) {
  364. DBGC ( device, "EFIPCI %s has no driver\n",
  365. efi_handle_name ( device ) );
  366. goto err_find_driver;
  367. }
  368. /* Mark PCI device as a child of the EFI device */
  369. pci->dev.parent = &efidev->dev;
  370. list_add ( &pci->dev.siblings, &efidev->dev.children );
  371. /* Probe driver */
  372. if ( ( rc = pci_probe ( pci ) ) != 0 ) {
  373. DBGC ( device, "EFIPCI %s could not probe driver \"%s\": %s\n",
  374. efi_handle_name ( device ), pci->id->name,
  375. strerror ( rc ) );
  376. goto err_probe;
  377. }
  378. DBGC ( device, "EFIPCI %s using driver \"%s\"\n",
  379. efi_handle_name ( device ), pci->id->name );
  380. efidev_set_drvdata ( efidev, pci );
  381. return 0;
  382. pci_remove ( pci );
  383. err_probe:
  384. list_del ( &pci->dev.siblings );
  385. err_find_driver:
  386. efipci_close ( device );
  387. err_open:
  388. free ( pci );
  389. err_alloc:
  390. return rc;
  391. }
  392. /**
  393. * Detach driver from device
  394. *
  395. * @v efidev EFI device
  396. */
  397. static void efipci_stop ( struct efi_device *efidev ) {
  398. struct pci_device *pci = efidev_get_drvdata ( efidev );
  399. EFI_HANDLE device = efidev->device;
  400. pci_remove ( pci );
  401. list_del ( &pci->dev.siblings );
  402. efipci_close ( device );
  403. free ( pci );
  404. }
  405. /** EFI PCI driver */
  406. struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
  407. .name = "PCI",
  408. .supported = efipci_supported,
  409. .start = efipci_start,
  410. .stop = efipci_stop,
  411. };