2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/efi/efi.h>
26 #include <ipxe/efi/efi_pci.h>
27 #include <ipxe/efi/efi_driver.h>
28 #include <ipxe/efi/Protocol/PciIo.h>
29 #include <ipxe/efi/Protocol/PciRootBridgeIo.h>
33 * iPXE PCI I/O API for EFI
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, \
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 )
53 /******************************************************************************
57 ******************************************************************************
60 /** PCI root bridge I/O protocol */
61 static EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *efipci;
62 EFI_REQUEST_PROTOCOL ( EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL, &efipci );
64 static unsigned long efipci_address ( struct pci_device *pci,
65 unsigned long location ) {
66 return EFI_PCI_ADDRESS ( PCI_BUS ( pci->busdevfn ),
67 PCI_SLOT ( pci->busdevfn ),
68 PCI_FUNC ( pci->busdevfn ),
69 EFIPCI_OFFSET ( location ) );
72 int efipci_read ( struct pci_device *pci, unsigned long location,
80 if ( ( efirc = efipci->Pci.Read ( efipci, EFIPCI_WIDTH ( location ),
81 efipci_address ( pci, location ), 1,
84 DBG ( "EFIPCI config read from " PCI_FMT " offset %02lx "
85 "failed: %s\n", PCI_ARGS ( pci ),
86 EFIPCI_OFFSET ( location ), strerror ( rc ) );
93 int efipci_write ( struct pci_device *pci, unsigned long location,
94 unsigned long value ) {
101 if ( ( efirc = efipci->Pci.Write ( efipci, EFIPCI_WIDTH ( location ),
102 efipci_address ( pci, location ), 1,
104 rc = -EEFI ( efirc );
105 DBG ( "EFIPCI config write to " PCI_FMT " offset %02lx "
106 "failed: %s\n", PCI_ARGS ( pci ),
107 EFIPCI_OFFSET ( location ), strerror ( rc ) );
114 PROVIDE_PCIAPI_INLINE ( efi, pci_num_bus );
115 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_byte );
116 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_word );
117 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_dword );
118 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_byte );
119 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_word );
120 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_dword );
122 /******************************************************************************
124 * EFI PCI device instantiation
126 ******************************************************************************
130 * Open EFI PCI device
132 * @v device EFI device handle
133 * @v attributes Protocol opening attributes
134 * @v pci PCI device to fill in
135 * @ret rc Return status code
137 int efipci_open ( EFI_HANDLE device, UINT32 attributes,
138 struct pci_device *pci ) {
139 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
141 EFI_PCI_IO_PROTOCOL *pci_io;
144 UINTN pci_segment, pci_bus, pci_dev, pci_fn;
148 /* See if device is a PCI device */
149 if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid,
150 &pci_io.interface, efi_image_handle,
151 device, attributes ) ) != 0 ) {
152 rc = -EEFI_PCI ( efirc );
153 DBGCP ( device, "EFIPCI %p %s cannot open PCI protocols: %s\n",
154 device, efi_handle_name ( device ), strerror ( rc ) );
155 goto err_open_protocol;
158 /* Get PCI bus:dev.fn address */
159 if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment,
162 rc = -EEFI ( efirc );
163 DBGC ( device, "EFIPCI %p %s could not get PCI location: %s\n",
164 device, efi_handle_name ( device ), strerror ( rc ) );
165 goto err_get_location;
167 DBGC2 ( device, "EFIPCI %p %s is PCI %04lx:%02lx:%02lx.%lx\n", device,
168 efi_handle_name ( device ), ( ( unsigned long ) pci_segment ),
169 ( ( unsigned long ) pci_bus ), ( ( unsigned long ) pci_dev ),
170 ( ( unsigned long ) pci_fn ) );
172 /* Try to enable I/O cycles, memory cycles, and bus mastering.
173 * Some platforms will 'helpfully' report errors if these bits
174 * can't be enabled (for example, if the card doesn't actually
175 * support I/O cycles). Work around any such platforms by
176 * enabling bits individually and simply ignoring any errors.
178 pci_io.pci_io->Attributes ( pci_io.pci_io,
179 EfiPciIoAttributeOperationEnable,
180 EFI_PCI_IO_ATTRIBUTE_IO, NULL );
181 pci_io.pci_io->Attributes ( pci_io.pci_io,
182 EfiPciIoAttributeOperationEnable,
183 EFI_PCI_IO_ATTRIBUTE_MEMORY, NULL );
184 pci_io.pci_io->Attributes ( pci_io.pci_io,
185 EfiPciIoAttributeOperationEnable,
186 EFI_PCI_IO_ATTRIBUTE_BUS_MASTER, NULL );
188 /* Populate PCI device */
189 pci_init ( pci, PCI_BUSDEVFN ( pci_bus, pci_dev, pci_fn ) );
190 if ( ( rc = pci_read_config ( pci ) ) != 0 ) {
191 DBGC ( device, "EFIPCI %p %s cannot read PCI configuration: "
192 "%s\n", device, efi_handle_name ( device ),
194 goto err_pci_read_config;
201 bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
202 efi_image_handle, device );
208 * Close EFI PCI device
210 * @v device EFI device handle
212 void efipci_close ( EFI_HANDLE device ) {
213 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
215 bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
216 efi_image_handle, device );
220 * Get EFI PCI device information
222 * @v device EFI device handle
223 * @v pci PCI device to fill in
224 * @ret rc Return status code
226 int efipci_info ( EFI_HANDLE device, struct pci_device *pci ) {
229 /* Open PCI device, if possible */
230 if ( ( rc = efipci_open ( device, EFI_OPEN_PROTOCOL_GET_PROTOCOL,
234 /* Close PCI device */
235 efipci_close ( device );
240 /******************************************************************************
244 ******************************************************************************
248 * Check to see if driver supports a device
250 * @v device EFI device handle
251 * @ret rc Return status code
253 static int efipci_supported ( EFI_HANDLE device ) {
254 struct pci_device pci;
257 /* Get PCI device information */
258 if ( ( rc = efipci_info ( device, &pci ) ) != 0 )
261 /* Look for a driver */
262 if ( ( rc = pci_find_driver ( &pci ) ) != 0 ) {
263 DBGCP ( device, "EFIPCI %p %s has no driver\n",
264 device, efi_handle_name ( device ) );
267 DBGC ( device, "EFIPCI %p %s has driver \"%s\"\n",
268 device, efi_handle_name ( device ), pci.id->name );
274 * Attach driver to device
276 * @v efidev EFI device
277 * @ret rc Return status code
279 static int efipci_start ( struct efi_device *efidev ) {
280 EFI_HANDLE device = efidev->device;
281 struct pci_device *pci;
284 /* Allocate PCI device */
285 pci = zalloc ( sizeof ( *pci ) );
291 /* Open PCI device */
292 if ( ( rc = efipci_open ( device, ( EFI_OPEN_PROTOCOL_BY_DRIVER |
293 EFI_OPEN_PROTOCOL_EXCLUSIVE ),
295 DBGC ( device, "EFIPCI %p %s could not open PCI device: %s\n",
296 device, efi_handle_name ( device ), strerror ( rc ) );
297 DBGC_EFI_OPENERS ( device, device, &efi_pci_io_protocol_guid );
302 if ( ( rc = pci_find_driver ( pci ) ) != 0 ) {
303 DBGC ( device, "EFIPCI %p %s has no driver\n",
304 device, efi_handle_name ( device ) );
305 goto err_find_driver;
308 /* Mark PCI device as a child of the EFI device */
309 pci->dev.parent = &efidev->dev;
310 list_add ( &pci->dev.siblings, &efidev->dev.children );
313 if ( ( rc = pci_probe ( pci ) ) != 0 ) {
314 DBGC ( device, "EFIPCI %p %s could not probe driver \"%s\": "
315 "%s\n", device, efi_handle_name ( device ),
316 pci->id->name, strerror ( rc ) );
319 DBGC ( device, "EFIPCI %p %s using driver \"%s\"\n",
320 device, efi_handle_name ( device ), pci->id->name );
322 efidev_set_drvdata ( efidev, pci );
327 list_del ( &pci->dev.siblings );
329 efipci_close ( device );
337 * Detach driver from device
339 * @v efidev EFI device
341 static void efipci_stop ( struct efi_device *efidev ) {
342 struct pci_device *pci = efidev_get_drvdata ( efidev );
343 EFI_HANDLE device = efidev->device;
346 list_del ( &pci->dev.siblings );
347 efipci_close ( device );
351 /** EFI PCI driver */
352 struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
354 .supported = efipci_supported,
355 .start = efipci_start,