These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / interface / efi / efi_pci.c
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
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <ipxe/pci.h>
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>
34
35 /** @file
36  *
37  * iPXE PCI I/O API for EFI
38  *
39  */
40
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,          \
47                               "Not a PCI device" )
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 )
56
57 /******************************************************************************
58  *
59  * iPXE PCI API
60  *
61  ******************************************************************************
62  */
63
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 );
67
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 ) );
74 }
75
76 int efipci_read ( struct pci_device *pci, unsigned long location,
77                   void *value ) {
78         EFI_STATUS efirc;
79         int rc;
80
81         if ( ! efipci )
82                 return -ENOTSUP;
83
84         if ( ( efirc = efipci->Pci.Read ( efipci, EFIPCI_WIDTH ( location ),
85                                           efipci_address ( pci, location ), 1,
86                                           value ) ) != 0 ) {
87                 rc = -EEFI ( efirc );
88                 DBG ( "EFIPCI config read from " PCI_FMT " offset %02lx "
89                       "failed: %s\n", PCI_ARGS ( pci ),
90                       EFIPCI_OFFSET ( location ), strerror ( rc ) );
91                 return -EIO;
92         }
93
94         return 0;
95 }
96
97 int efipci_write ( struct pci_device *pci, unsigned long location,
98                    unsigned long value ) {
99         EFI_STATUS efirc;
100         int rc;
101
102         if ( ! efipci )
103                 return -ENOTSUP;
104
105         if ( ( efirc = efipci->Pci.Write ( efipci, EFIPCI_WIDTH ( location ),
106                                            efipci_address ( pci, location ), 1,
107                                            &value ) ) != 0 ) {
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 ) );
112                 return -EIO;
113         }
114
115         return 0;
116 }
117
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 );
125
126 /******************************************************************************
127  *
128  * EFI PCI device instantiation
129  *
130  ******************************************************************************
131  */
132
133 /**
134  * Open EFI PCI device
135  *
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
140  */
141 int efipci_open ( EFI_HANDLE device, UINT32 attributes,
142                   struct pci_device *pci ) {
143         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
144         union {
145                 EFI_PCI_IO_PROTOCOL *pci_io;
146                 void *interface;
147         } pci_io;
148         UINTN pci_segment, pci_bus, pci_dev, pci_fn;
149         EFI_STATUS efirc;
150         int rc;
151
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;
160         }
161
162         /* Get PCI bus:dev.fn address */
163         if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment,
164                                                     &pci_bus, &pci_dev,
165                                                     &pci_fn ) ) != 0 ) {
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;
170         }
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 ) );
175
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.
181          */
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 );
191
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 ),
197                        strerror ( rc ) );
198                 goto err_pci_read_config;
199         }
200
201         return 0;
202
203  err_pci_read_config:
204  err_get_location:
205         bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
206                             efi_image_handle, device );
207  err_open_protocol:
208         return rc;
209 }
210
211 /**
212  * Close EFI PCI device
213  *
214  * @v device            EFI device handle
215  */
216 void efipci_close ( EFI_HANDLE device ) {
217         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
218
219         bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
220                             efi_image_handle, device );
221 }
222
223 /**
224  * Get EFI PCI device information
225  *
226  * @v device            EFI device handle
227  * @v pci               PCI device to fill in
228  * @ret rc              Return status code
229  */
230 int efipci_info ( EFI_HANDLE device, struct pci_device *pci ) {
231         int rc;
232
233         /* Open PCI device, if possible */
234         if ( ( rc = efipci_open ( device, EFI_OPEN_PROTOCOL_GET_PROTOCOL,
235                                   pci ) ) != 0 )
236                 return rc;
237
238         /* Close PCI device */
239         efipci_close ( device );
240
241         return 0;
242 }
243
244 /******************************************************************************
245  *
246  * EFI PCI driver
247  *
248  ******************************************************************************
249  */
250
251 /**
252  * Check to see if driver supports a device
253  *
254  * @v device            EFI device handle
255  * @ret rc              Return status code
256  */
257 static int efipci_supported ( EFI_HANDLE device ) {
258         struct pci_device pci;
259         int rc;
260
261         /* Get PCI device information */
262         if ( ( rc = efipci_info ( device, &pci ) ) != 0 )
263                 return rc;
264
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 ) );
269                 return rc;
270         }
271         DBGC ( device, "EFIPCI %p %s has driver \"%s\"\n",
272                device, efi_handle_name ( device ), pci.id->name );
273
274         return 0;
275 }
276
277 /**
278  * Attach driver to device
279  *
280  * @v efidev            EFI device
281  * @ret rc              Return status code
282  */
283 static int efipci_start ( struct efi_device *efidev ) {
284         EFI_HANDLE device = efidev->device;
285         struct pci_device *pci;
286         int rc;
287
288         /* Allocate PCI device */
289         pci = zalloc ( sizeof ( *pci ) );
290         if ( ! pci ) {
291                 rc = -ENOMEM;
292                 goto err_alloc;
293         }
294
295         /* Open PCI device */
296         if ( ( rc = efipci_open ( device, ( EFI_OPEN_PROTOCOL_BY_DRIVER |
297                                             EFI_OPEN_PROTOCOL_EXCLUSIVE ),
298                                   pci ) ) != 0 ) {
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 );
302                 goto err_open;
303         }
304
305         /* Find driver */
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;
310         }
311
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 );
315
316         /* Probe driver */
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 ) );
321                 goto err_probe;
322         }
323         DBGC ( device, "EFIPCI %p %s using driver \"%s\"\n",
324                device, efi_handle_name ( device ), pci->id->name );
325
326         efidev_set_drvdata ( efidev, pci );
327         return 0;
328
329         pci_remove ( pci );
330  err_probe:
331         list_del ( &pci->dev.siblings );
332  err_find_driver:
333         efipci_close ( device );
334  err_open:
335         free ( pci );
336  err_alloc:
337         return rc;
338 }
339
340 /**
341  * Detach driver from device
342  *
343  * @v efidev            EFI device
344   */
345 static void efipci_stop ( struct efi_device *efidev ) {
346         struct pci_device *pci = efidev_get_drvdata ( efidev );
347         EFI_HANDLE device = efidev->device;
348
349         pci_remove ( pci );
350         list_del ( &pci->dev.siblings );
351         efipci_close ( device );
352         free ( pci );
353 }
354
355 /** EFI PCI driver */
356 struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
357         .name = "PCI",
358         .supported = efipci_supported,
359         .start = efipci_start,
360         .stop = efipci_stop,
361 };