Add qemu 2.4.0
[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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <ipxe/pci.h>
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>
30
31 /** @file
32  *
33  * iPXE PCI I/O API for EFI
34  *
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  *
55  * iPXE PCI API
56  *
57  ******************************************************************************
58  */
59
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 );
63
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 ) );
70 }
71
72 int efipci_read ( struct pci_device *pci, unsigned long location,
73                   void *value ) {
74         EFI_STATUS efirc;
75         int rc;
76
77         if ( ! efipci )
78                 return -ENOTSUP;
79
80         if ( ( efirc = efipci->Pci.Read ( efipci, EFIPCI_WIDTH ( location ),
81                                           efipci_address ( pci, location ), 1,
82                                           value ) ) != 0 ) {
83                 rc = -EEFI ( efirc );
84                 DBG ( "EFIPCI config read from " PCI_FMT " offset %02lx "
85                       "failed: %s\n", PCI_ARGS ( pci ),
86                       EFIPCI_OFFSET ( location ), strerror ( rc ) );
87                 return -EIO;
88         }
89
90         return 0;
91 }
92
93 int efipci_write ( struct pci_device *pci, unsigned long location,
94                    unsigned long value ) {
95         EFI_STATUS efirc;
96         int rc;
97
98         if ( ! efipci )
99                 return -ENOTSUP;
100
101         if ( ( efirc = efipci->Pci.Write ( efipci, EFIPCI_WIDTH ( location ),
102                                            efipci_address ( pci, location ), 1,
103                                            &value ) ) != 0 ) {
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 ) );
108                 return -EIO;
109         }
110
111         return 0;
112 }
113
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 );
121
122 /******************************************************************************
123  *
124  * EFI PCI device instantiation
125  *
126  ******************************************************************************
127  */
128
129 /**
130  * Open EFI PCI device
131  *
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
136  */
137 int efipci_open ( EFI_HANDLE device, UINT32 attributes,
138                   struct pci_device *pci ) {
139         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
140         union {
141                 EFI_PCI_IO_PROTOCOL *pci_io;
142                 void *interface;
143         } pci_io;
144         UINTN pci_segment, pci_bus, pci_dev, pci_fn;
145         EFI_STATUS efirc;
146         int rc;
147
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;
156         }
157
158         /* Get PCI bus:dev.fn address */
159         if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment,
160                                                     &pci_bus, &pci_dev,
161                                                     &pci_fn ) ) != 0 ) {
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;
166         }
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 ) );
171
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.
177          */
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 );
187
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 ),
193                        strerror ( rc ) );
194                 goto err_pci_read_config;
195         }
196
197         return 0;
198
199  err_pci_read_config:
200  err_get_location:
201         bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
202                             efi_image_handle, device );
203  err_open_protocol:
204         return rc;
205 }
206
207 /**
208  * Close EFI PCI device
209  *
210  * @v device            EFI device handle
211  */
212 void efipci_close ( EFI_HANDLE device ) {
213         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
214
215         bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
216                             efi_image_handle, device );
217 }
218
219 /**
220  * Get EFI PCI device information
221  *
222  * @v device            EFI device handle
223  * @v pci               PCI device to fill in
224  * @ret rc              Return status code
225  */
226 int efipci_info ( EFI_HANDLE device, struct pci_device *pci ) {
227         int rc;
228
229         /* Open PCI device, if possible */
230         if ( ( rc = efipci_open ( device, EFI_OPEN_PROTOCOL_GET_PROTOCOL,
231                                   pci ) ) != 0 )
232                 return rc;
233
234         /* Close PCI device */
235         efipci_close ( device );
236
237         return 0;
238 }
239
240 /******************************************************************************
241  *
242  * EFI PCI driver
243  *
244  ******************************************************************************
245  */
246
247 /**
248  * Check to see if driver supports a device
249  *
250  * @v device            EFI device handle
251  * @ret rc              Return status code
252  */
253 static int efipci_supported ( EFI_HANDLE device ) {
254         struct pci_device pci;
255         int rc;
256
257         /* Get PCI device information */
258         if ( ( rc = efipci_info ( device, &pci ) ) != 0 )
259                 return rc;
260
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 ) );
265                 return rc;
266         }
267         DBGC ( device, "EFIPCI %p %s has driver \"%s\"\n",
268                device, efi_handle_name ( device ), pci.id->name );
269
270         return 0;
271 }
272
273 /**
274  * Attach driver to device
275  *
276  * @v efidev            EFI device
277  * @ret rc              Return status code
278  */
279 static int efipci_start ( struct efi_device *efidev ) {
280         EFI_HANDLE device = efidev->device;
281         struct pci_device *pci;
282         int rc;
283
284         /* Allocate PCI device */
285         pci = zalloc ( sizeof ( *pci ) );
286         if ( ! pci ) {
287                 rc = -ENOMEM;
288                 goto err_alloc;
289         }
290
291         /* Open PCI device */
292         if ( ( rc = efipci_open ( device, ( EFI_OPEN_PROTOCOL_BY_DRIVER |
293                                             EFI_OPEN_PROTOCOL_EXCLUSIVE ),
294                                   pci ) ) != 0 ) {
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 );
298                 goto err_open;
299         }
300
301         /* Find driver */
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;
306         }
307
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 );
311
312         /* Probe driver */
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 ) );
317                 goto err_probe;
318         }
319         DBGC ( device, "EFIPCI %p %s using driver \"%s\"\n",
320                device, efi_handle_name ( device ), pci->id->name );
321
322         efidev_set_drvdata ( efidev, pci );
323         return 0;
324
325         pci_remove ( pci );
326  err_probe:
327         list_del ( &pci->dev.siblings );
328  err_find_driver:
329         efipci_close ( device );
330  err_open:
331         free ( pci );
332  err_alloc:
333         return rc;
334 }
335
336 /**
337  * Detach driver from device
338  *
339  * @v efidev            EFI device
340   */
341 static void efipci_stop ( struct efi_device *efidev ) {
342         struct pci_device *pci = efidev_get_drvdata ( efidev );
343         EFI_HANDLE device = efidev->device;
344
345         pci_remove ( pci );
346         list_del ( &pci->dev.siblings );
347         efipci_close ( device );
348         free ( pci );
349 }
350
351 /** EFI PCI driver */
352 struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
353         .name = "PCI",
354         .supported = efipci_supported,
355         .start = efipci_start,
356         .stop = efipci_stop,
357 };