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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 #include <ipxe/efi/efi.h>
30 #include <ipxe/efi/efi_pci.h>
31 #include <ipxe/efi/efi_driver.h>
32 #include <ipxe/efi/Protocol/PciIo.h>
33 #include <ipxe/efi/Protocol/PciRootBridgeIo.h>
37 * iPXE PCI I/O API for EFI
41 /* Disambiguate the various error causes */
42 #define EINFO_EEFI_PCI \
43 __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
44 "Could not open PCI I/O protocol" )
45 #define EINFO_EEFI_PCI_NOT_PCI \
46 __einfo_platformify ( EINFO_EEFI_PCI, EFI_UNSUPPORTED, \
48 #define EEFI_PCI_NOT_PCI __einfo_error ( EINFO_EEFI_PCI_NOT_PCI )
49 #define EINFO_EEFI_PCI_IN_USE \
50 __einfo_platformify ( EINFO_EEFI_PCI, EFI_ACCESS_DENIED, \
51 "PCI device already has a driver" )
52 #define EEFI_PCI_IN_USE __einfo_error ( EINFO_EEFI_PCI_IN_USE )
53 #define EEFI_PCI( efirc ) \
54 EPLATFORM ( EINFO_EEFI_PCI, efirc, \
55 EEFI_PCI_NOT_PCI, EEFI_PCI_IN_USE )
57 /******************************************************************************
61 ******************************************************************************
64 /** PCI root bridge I/O protocol */
65 static EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *efipci;
66 EFI_REQUEST_PROTOCOL ( EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL, &efipci );
68 static unsigned long efipci_address ( struct pci_device *pci,
69 unsigned long location ) {
70 return EFI_PCI_ADDRESS ( PCI_BUS ( pci->busdevfn ),
71 PCI_SLOT ( pci->busdevfn ),
72 PCI_FUNC ( pci->busdevfn ),
73 EFIPCI_OFFSET ( location ) );
76 int efipci_read ( struct pci_device *pci, unsigned long location,
84 if ( ( efirc = efipci->Pci.Read ( efipci, EFIPCI_WIDTH ( location ),
85 efipci_address ( pci, location ), 1,
88 DBG ( "EFIPCI config read from " PCI_FMT " offset %02lx "
89 "failed: %s\n", PCI_ARGS ( pci ),
90 EFIPCI_OFFSET ( location ), strerror ( rc ) );
97 int efipci_write ( struct pci_device *pci, unsigned long location,
98 unsigned long value ) {
105 if ( ( efirc = efipci->Pci.Write ( efipci, EFIPCI_WIDTH ( location ),
106 efipci_address ( pci, location ), 1,
108 rc = -EEFI ( efirc );
109 DBG ( "EFIPCI config write to " PCI_FMT " offset %02lx "
110 "failed: %s\n", PCI_ARGS ( pci ),
111 EFIPCI_OFFSET ( location ), strerror ( rc ) );
118 PROVIDE_PCIAPI_INLINE ( efi, pci_num_bus );
119 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_byte );
120 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_word );
121 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_dword );
122 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_byte );
123 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_word );
124 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_dword );
126 /******************************************************************************
128 * EFI PCI device instantiation
130 ******************************************************************************
134 * Open EFI PCI device
136 * @v device EFI device handle
137 * @v attributes Protocol opening attributes
138 * @v pci PCI device to fill in
139 * @ret rc Return status code
141 int efipci_open ( EFI_HANDLE device, UINT32 attributes,
142 struct pci_device *pci ) {
143 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
145 EFI_PCI_IO_PROTOCOL *pci_io;
148 UINTN pci_segment, pci_bus, pci_dev, pci_fn;
152 /* See if device is a PCI device */
153 if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid,
154 &pci_io.interface, efi_image_handle,
155 device, attributes ) ) != 0 ) {
156 rc = -EEFI_PCI ( efirc );
157 DBGCP ( device, "EFIPCI %p %s cannot open PCI protocols: %s\n",
158 device, efi_handle_name ( device ), strerror ( rc ) );
159 goto err_open_protocol;
162 /* Get PCI bus:dev.fn address */
163 if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment,
166 rc = -EEFI ( efirc );
167 DBGC ( device, "EFIPCI %p %s could not get PCI location: %s\n",
168 device, efi_handle_name ( device ), strerror ( rc ) );
169 goto err_get_location;
171 DBGC2 ( device, "EFIPCI %p %s is PCI %04lx:%02lx:%02lx.%lx\n", device,
172 efi_handle_name ( device ), ( ( unsigned long ) pci_segment ),
173 ( ( unsigned long ) pci_bus ), ( ( unsigned long ) pci_dev ),
174 ( ( unsigned long ) pci_fn ) );
176 /* Try to enable I/O cycles, memory cycles, and bus mastering.
177 * Some platforms will 'helpfully' report errors if these bits
178 * can't be enabled (for example, if the card doesn't actually
179 * support I/O cycles). Work around any such platforms by
180 * enabling bits individually and simply ignoring any errors.
182 pci_io.pci_io->Attributes ( pci_io.pci_io,
183 EfiPciIoAttributeOperationEnable,
184 EFI_PCI_IO_ATTRIBUTE_IO, NULL );
185 pci_io.pci_io->Attributes ( pci_io.pci_io,
186 EfiPciIoAttributeOperationEnable,
187 EFI_PCI_IO_ATTRIBUTE_MEMORY, NULL );
188 pci_io.pci_io->Attributes ( pci_io.pci_io,
189 EfiPciIoAttributeOperationEnable,
190 EFI_PCI_IO_ATTRIBUTE_BUS_MASTER, NULL );
192 /* Populate PCI device */
193 pci_init ( pci, PCI_BUSDEVFN ( pci_bus, pci_dev, pci_fn ) );
194 if ( ( rc = pci_read_config ( pci ) ) != 0 ) {
195 DBGC ( device, "EFIPCI %p %s cannot read PCI configuration: "
196 "%s\n", device, efi_handle_name ( device ),
198 goto err_pci_read_config;
205 bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
206 efi_image_handle, device );
212 * Close EFI PCI device
214 * @v device EFI device handle
216 void efipci_close ( EFI_HANDLE device ) {
217 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
219 bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
220 efi_image_handle, device );
224 * Get EFI PCI device information
226 * @v device EFI device handle
227 * @v pci PCI device to fill in
228 * @ret rc Return status code
230 int efipci_info ( EFI_HANDLE device, struct pci_device *pci ) {
233 /* Open PCI device, if possible */
234 if ( ( rc = efipci_open ( device, EFI_OPEN_PROTOCOL_GET_PROTOCOL,
238 /* Close PCI device */
239 efipci_close ( device );
244 /******************************************************************************
248 ******************************************************************************
252 * Check to see if driver supports a device
254 * @v device EFI device handle
255 * @ret rc Return status code
257 static int efipci_supported ( EFI_HANDLE device ) {
258 struct pci_device pci;
261 /* Get PCI device information */
262 if ( ( rc = efipci_info ( device, &pci ) ) != 0 )
265 /* Look for a driver */
266 if ( ( rc = pci_find_driver ( &pci ) ) != 0 ) {
267 DBGCP ( device, "EFIPCI %p %s has no driver\n",
268 device, efi_handle_name ( device ) );
271 DBGC ( device, "EFIPCI %p %s has driver \"%s\"\n",
272 device, efi_handle_name ( device ), pci.id->name );
278 * Attach driver to device
280 * @v efidev EFI device
281 * @ret rc Return status code
283 static int efipci_start ( struct efi_device *efidev ) {
284 EFI_HANDLE device = efidev->device;
285 struct pci_device *pci;
288 /* Allocate PCI device */
289 pci = zalloc ( sizeof ( *pci ) );
295 /* Open PCI device */
296 if ( ( rc = efipci_open ( device, ( EFI_OPEN_PROTOCOL_BY_DRIVER |
297 EFI_OPEN_PROTOCOL_EXCLUSIVE ),
299 DBGC ( device, "EFIPCI %p %s could not open PCI device: %s\n",
300 device, efi_handle_name ( device ), strerror ( rc ) );
301 DBGC_EFI_OPENERS ( device, device, &efi_pci_io_protocol_guid );
306 if ( ( rc = pci_find_driver ( pci ) ) != 0 ) {
307 DBGC ( device, "EFIPCI %p %s has no driver\n",
308 device, efi_handle_name ( device ) );
309 goto err_find_driver;
312 /* Mark PCI device as a child of the EFI device */
313 pci->dev.parent = &efidev->dev;
314 list_add ( &pci->dev.siblings, &efidev->dev.children );
317 if ( ( rc = pci_probe ( pci ) ) != 0 ) {
318 DBGC ( device, "EFIPCI %p %s could not probe driver \"%s\": "
319 "%s\n", device, efi_handle_name ( device ),
320 pci->id->name, strerror ( rc ) );
323 DBGC ( device, "EFIPCI %p %s using driver \"%s\"\n",
324 device, efi_handle_name ( device ), pci->id->name );
326 efidev_set_drvdata ( efidev, pci );
331 list_del ( &pci->dev.siblings );
333 efipci_close ( device );
341 * Detach driver from device
343 * @v efidev EFI device
345 static void efipci_stop ( struct efi_device *efidev ) {
346 struct pci_device *pci = efidev_get_drvdata ( efidev );
347 EFI_HANDLE device = efidev->device;
350 list_del ( &pci->dev.siblings );
351 efipci_close ( device );
355 /** EFI PCI driver */
356 struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
358 .supported = efipci_supported,
359 .start = efipci_start,