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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 " PCI_FMT " cannot locate root bridges: "
  84. "%s\n", PCI_ARGS ( pci ), 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 " PCI_FMT " cannot open %s: %s\n",
  96. PCI_ARGS ( pci ), efi_handle_name ( *handle ),
  97. strerror ( rc ) );
  98. continue;
  99. }
  100. if ( u.root->SegmentNumber == PCI_SEG ( pci->busdevfn ) ) {
  101. *root = u.root;
  102. bs->FreePool ( handles );
  103. return 0;
  104. }
  105. bs->CloseProtocol ( *handle,
  106. &efi_pci_root_bridge_io_protocol_guid,
  107. efi_image_handle, *handle );
  108. }
  109. DBGC ( pci, "EFIPCI " PCI_FMT " found no root bridge\n",
  110. PCI_ARGS ( pci ) );
  111. rc = -ENOENT;
  112. bs->FreePool ( handles );
  113. err_locate:
  114. return rc;
  115. }
  116. /**
  117. * Calculate EFI PCI configuration space address
  118. *
  119. * @v pci PCI device
  120. * @v location Encoded offset and width
  121. * @ret address EFI PCI address
  122. */
  123. static unsigned long efipci_address ( struct pci_device *pci,
  124. unsigned long location ) {
  125. return EFI_PCI_ADDRESS ( PCI_BUS ( pci->busdevfn ),
  126. PCI_SLOT ( pci->busdevfn ),
  127. PCI_FUNC ( pci->busdevfn ),
  128. EFIPCI_OFFSET ( location ) );
  129. }
  130. /**
  131. * Read from PCI configuration space
  132. *
  133. * @v pci PCI device
  134. * @v location Encoded offset and width
  135. * @ret value Value
  136. * @ret rc Return status code
  137. */
  138. int efipci_read ( struct pci_device *pci, unsigned long location,
  139. void *value ) {
  140. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  141. EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root;
  142. EFI_HANDLE handle;
  143. EFI_STATUS efirc;
  144. int rc;
  145. /* Identify root bridge */
  146. if ( ( rc = efipci_root ( pci, &handle, &root ) ) != 0 )
  147. goto err_root;
  148. /* Read from configuration space */
  149. if ( ( efirc = root->Pci.Read ( root, EFIPCI_WIDTH ( location ),
  150. efipci_address ( pci, location ), 1,
  151. value ) ) != 0 ) {
  152. rc = -EEFI ( efirc );
  153. DBGC ( pci, "EFIPCI " PCI_FMT " config read from offset %02lx "
  154. "failed: %s\n", PCI_ARGS ( pci ),
  155. EFIPCI_OFFSET ( location ), strerror ( rc ) );
  156. goto err_read;
  157. }
  158. err_read:
  159. bs->CloseProtocol ( handle, &efi_pci_root_bridge_io_protocol_guid,
  160. efi_image_handle, handle );
  161. err_root:
  162. return rc;
  163. }
  164. /**
  165. * Write to PCI configuration space
  166. *
  167. * @v pci PCI device
  168. * @v location Encoded offset and width
  169. * @v value Value
  170. * @ret rc Return status code
  171. */
  172. int efipci_write ( struct pci_device *pci, unsigned long location,
  173. unsigned long value ) {
  174. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  175. EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root;
  176. EFI_HANDLE handle;
  177. EFI_STATUS efirc;
  178. int rc;
  179. /* Identify root bridge */
  180. if ( ( rc = efipci_root ( pci, &handle, &root ) ) != 0 )
  181. goto err_root;
  182. /* Read from configuration space */
  183. if ( ( efirc = root->Pci.Write ( root, EFIPCI_WIDTH ( location ),
  184. efipci_address ( pci, location ), 1,
  185. &value ) ) != 0 ) {
  186. rc = -EEFI ( efirc );
  187. DBGC ( pci, "EFIPCI " PCI_FMT " config write to offset %02lx "
  188. "failed: %s\n", PCI_ARGS ( pci ),
  189. EFIPCI_OFFSET ( location ), strerror ( rc ) );
  190. goto err_write;
  191. }
  192. err_write:
  193. bs->CloseProtocol ( handle, &efi_pci_root_bridge_io_protocol_guid,
  194. efi_image_handle, handle );
  195. err_root:
  196. return rc;
  197. }
  198. PROVIDE_PCIAPI_INLINE ( efi, pci_num_bus );
  199. PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_byte );
  200. PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_word );
  201. PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_dword );
  202. PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_byte );
  203. PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_word );
  204. PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_dword );
  205. /******************************************************************************
  206. *
  207. * EFI PCI device instantiation
  208. *
  209. ******************************************************************************
  210. */
  211. /**
  212. * Open EFI PCI device
  213. *
  214. * @v device EFI device handle
  215. * @v attributes Protocol opening attributes
  216. * @v pci PCI device to fill in
  217. * @ret rc Return status code
  218. */
  219. int efipci_open ( EFI_HANDLE device, UINT32 attributes,
  220. struct pci_device *pci ) {
  221. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  222. union {
  223. EFI_PCI_IO_PROTOCOL *pci_io;
  224. void *interface;
  225. } pci_io;
  226. UINTN pci_segment, pci_bus, pci_dev, pci_fn;
  227. unsigned int busdevfn;
  228. EFI_STATUS efirc;
  229. int rc;
  230. /* See if device is a PCI device */
  231. if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid,
  232. &pci_io.interface, efi_image_handle,
  233. device, attributes ) ) != 0 ) {
  234. rc = -EEFI_PCI ( efirc );
  235. DBGCP ( device, "EFIPCI %s cannot open PCI protocols: %s\n",
  236. efi_handle_name ( device ), strerror ( rc ) );
  237. goto err_open_protocol;
  238. }
  239. /* Get PCI bus:dev.fn address */
  240. if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment,
  241. &pci_bus, &pci_dev,
  242. &pci_fn ) ) != 0 ) {
  243. rc = -EEFI ( efirc );
  244. DBGC ( device, "EFIPCI %s could not get PCI location: %s\n",
  245. efi_handle_name ( device ), strerror ( rc ) );
  246. goto err_get_location;
  247. }
  248. busdevfn = PCI_BUSDEVFN ( pci_segment, pci_bus, pci_dev, pci_fn );
  249. pci_init ( pci, busdevfn );
  250. DBGCP ( device, "EFIPCI " PCI_FMT " is %s\n",
  251. PCI_ARGS ( pci ), efi_handle_name ( device ) );
  252. /* Try to enable I/O cycles, memory cycles, and bus mastering.
  253. * Some platforms will 'helpfully' report errors if these bits
  254. * can't be enabled (for example, if the card doesn't actually
  255. * support I/O cycles). Work around any such platforms by
  256. * enabling bits individually and simply ignoring any errors.
  257. */
  258. pci_io.pci_io->Attributes ( pci_io.pci_io,
  259. EfiPciIoAttributeOperationEnable,
  260. EFI_PCI_IO_ATTRIBUTE_IO, NULL );
  261. pci_io.pci_io->Attributes ( pci_io.pci_io,
  262. EfiPciIoAttributeOperationEnable,
  263. EFI_PCI_IO_ATTRIBUTE_MEMORY, NULL );
  264. pci_io.pci_io->Attributes ( pci_io.pci_io,
  265. EfiPciIoAttributeOperationEnable,
  266. EFI_PCI_IO_ATTRIBUTE_BUS_MASTER, NULL );
  267. /* Populate PCI device */
  268. if ( ( rc = pci_read_config ( pci ) ) != 0 ) {
  269. DBGC ( device, "EFIPCI " PCI_FMT " cannot read PCI "
  270. "configuration: %s\n",
  271. PCI_ARGS ( pci ), 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. DBGC ( device, "EFIPCI " PCI_FMT " (%04x:%04x class %06x) "
  330. "has no driver\n", PCI_ARGS ( &pci ), pci.vendor,
  331. pci.device, pci.class );
  332. return rc;
  333. }
  334. DBGC ( device, "EFIPCI " PCI_FMT " (%04x:%04x class %06x) has driver "
  335. "\"%s\"\n", PCI_ARGS ( &pci ), pci.vendor, pci.device,
  336. pci.class, pci.id->name );
  337. return 0;
  338. }
  339. /**
  340. * Attach driver to device
  341. *
  342. * @v efidev EFI device
  343. * @ret rc Return status code
  344. */
  345. static int efipci_start ( struct efi_device *efidev ) {
  346. EFI_HANDLE device = efidev->device;
  347. struct pci_device *pci;
  348. int rc;
  349. /* Allocate PCI device */
  350. pci = zalloc ( sizeof ( *pci ) );
  351. if ( ! pci ) {
  352. rc = -ENOMEM;
  353. goto err_alloc;
  354. }
  355. /* Open PCI device */
  356. if ( ( rc = efipci_open ( device, ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  357. EFI_OPEN_PROTOCOL_EXCLUSIVE ),
  358. pci ) ) != 0 ) {
  359. DBGC ( device, "EFIPCI %s could not open PCI device: %s\n",
  360. efi_handle_name ( device ), strerror ( rc ) );
  361. DBGC_EFI_OPENERS ( device, device, &efi_pci_io_protocol_guid );
  362. goto err_open;
  363. }
  364. /* Find driver */
  365. if ( ( rc = pci_find_driver ( pci ) ) != 0 ) {
  366. DBGC ( device, "EFIPCI " PCI_FMT " has no driver\n",
  367. PCI_ARGS ( pci ) );
  368. goto err_find_driver;
  369. }
  370. /* Mark PCI device as a child of the EFI device */
  371. pci->dev.parent = &efidev->dev;
  372. list_add ( &pci->dev.siblings, &efidev->dev.children );
  373. /* Probe driver */
  374. if ( ( rc = pci_probe ( pci ) ) != 0 ) {
  375. DBGC ( device, "EFIPCI " PCI_FMT " could not probe driver "
  376. "\"%s\": %s\n", PCI_ARGS ( pci ), pci->id->name,
  377. strerror ( rc ) );
  378. goto err_probe;
  379. }
  380. DBGC ( device, "EFIPCI " PCI_FMT " using driver \"%s\"\n",
  381. PCI_ARGS ( pci ), pci->id->name );
  382. efidev_set_drvdata ( efidev, pci );
  383. return 0;
  384. pci_remove ( pci );
  385. err_probe:
  386. list_del ( &pci->dev.siblings );
  387. err_find_driver:
  388. efipci_close ( device );
  389. err_open:
  390. free ( pci );
  391. err_alloc:
  392. return rc;
  393. }
  394. /**
  395. * Detach driver from device
  396. *
  397. * @v efidev EFI device
  398. */
  399. static void efipci_stop ( struct efi_device *efidev ) {
  400. struct pci_device *pci = efidev_get_drvdata ( efidev );
  401. EFI_HANDLE device = efidev->device;
  402. pci_remove ( pci );
  403. list_del ( &pci->dev.siblings );
  404. efipci_close ( device );
  405. free ( pci );
  406. }
  407. /** EFI PCI driver */
  408. struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
  409. .name = "PCI",
  410. .supported = efipci_supported,
  411. .start = efipci_start,
  412. .stop = efipci_stop,
  413. };