iPXE
efi_pci.c
Go to the documentation of this file.
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 <string.h>
28 #include <errno.h>
29 #include <ipxe/pci.h>
30 #include <ipxe/acpi.h>
31 #include <ipxe/efi/efi.h>
32 #include <ipxe/efi/efi_pci.h>
33 #include <ipxe/efi/efi_driver.h>
36 
37 /** @file
38  *
39  * iPXE PCI I/O API for EFI
40  *
41  */
42 
43 /* Disambiguate the various error causes */
44 #define EINFO_EEFI_PCI \
45  __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
46  "Could not open PCI I/O protocol" )
47 #define EINFO_EEFI_PCI_NOT_PCI \
48  __einfo_platformify ( EINFO_EEFI_PCI, EFI_UNSUPPORTED, \
49  "Not a PCI device" )
50 #define EEFI_PCI_NOT_PCI __einfo_error ( EINFO_EEFI_PCI_NOT_PCI )
51 #define EINFO_EEFI_PCI_IN_USE \
52  __einfo_platformify ( EINFO_EEFI_PCI, EFI_ACCESS_DENIED, \
53  "PCI device already has a driver" )
54 #define EEFI_PCI_IN_USE __einfo_error ( EINFO_EEFI_PCI_IN_USE )
55 #define EEFI_PCI( efirc ) \
56  EPLATFORM ( EINFO_EEFI_PCI, efirc, \
57  EEFI_PCI_NOT_PCI, EEFI_PCI_IN_USE )
58 
59 /******************************************************************************
60  *
61  * iPXE PCI API
62  *
63  ******************************************************************************
64  */
65 
66 /**
67  * Find closest bus:dev.fn address range within a root bridge
68  *
69  * @v pci Starting PCI device
70  * @v handle EFI PCI root bridge handle
71  * @v range PCI bus:dev.fn address range to fill in
72  * @ret rc Return status code
73  */
74 static int efipci_discover_one ( struct pci_device *pci, EFI_HANDLE handle,
75  struct pci_range *range ) {
77  union {
78  union acpi_resource *res;
79  void *raw;
80  } acpi;
81  uint32_t best = 0;
85  unsigned int tag;
86  EFI_STATUS efirc;
87  int rc;
88 
89  /* Return empty range on error */
90  range->start = 0;
91  range->count = 0;
92 
93  /* Open root bridge I/O protocol */
95  &root ) ) != 0 ) {
96  DBGC ( pci, "EFIPCI " PCI_FMT " cannot open %s: %s\n",
97  PCI_ARGS ( pci ), efi_handle_name ( handle ),
98  strerror ( rc ) );
99  return rc;
100  }
101 
102  /* Get ACPI resource descriptors */
103  if ( ( efirc = root->Configuration ( root, &acpi.raw ) ) != 0 ) {
104  rc = -EEFI ( efirc );
105  DBGC ( pci, "EFIPCI " PCI_FMT " cannot get configuration for "
106  "%s: %s\n", PCI_ARGS ( pci ),
107  efi_handle_name ( handle ), strerror ( rc ) );
108  return rc;
109  }
110 
111  /* Parse resource descriptors */
112  for ( ; ( ( tag = acpi_resource_tag ( acpi.res ) ) !=
114  acpi.res = acpi_resource_next ( acpi.res ) ) {
115 
116  /* Ignore anything other than a bus number range descriptor */
118  continue;
119  if ( acpi.res->qword.type != ACPI_ADDRESS_TYPE_BUS )
120  continue;
121 
122  /* Get range for this descriptor */
123  start = PCI_BUSDEVFN ( root->SegmentNumber,
124  le64_to_cpu ( acpi.res->qword.min ),
125  0, 0 );
126  count = PCI_BUSDEVFN ( 0, le64_to_cpu ( acpi.res->qword.len ),
127  0, 0 );
128  DBGC2 ( pci, "EFIPCI " PCI_FMT " found %04x:[%02x-%02x] via "
129  "%s\n", PCI_ARGS ( pci ), root->SegmentNumber,
130  PCI_BUS ( start ), PCI_BUS ( start + count - 1 ),
131  efi_handle_name ( handle ) );
132 
133  /* Check for a matching or new closest range */
134  index = ( pci->busdevfn - start );
135  if ( ( index < count ) || ( index > best ) ) {
136  range->start = start;
137  range->count = count;
138  best = index;
139  }
140 
141  /* Stop if this range contains the target bus:dev.fn address */
142  if ( index < count )
143  break;
144  }
145 
146  /* If no range descriptors were seen, assume that the root
147  * bridge has a single bus.
148  */
149  if ( ! range->count ) {
150  range->start = PCI_BUSDEVFN ( root->SegmentNumber, 0, 0, 0 );
151  range->count = PCI_BUSDEVFN ( 0, 1, 0, 0 );
152  }
153 
154  return 0;
155 }
156 
157 /**
158  * Find closest bus:dev.fn address range within any root bridge
159  *
160  * @v pci Starting PCI device
161  * @v range PCI bus:dev.fn address range to fill in
162  * @v handle PCI root bridge I/O handle to fill in
163  * @ret rc Return status code
164  */
165 static int efipci_discover_any ( struct pci_device *pci,
166  struct pci_range *range,
167  EFI_HANDLE *handle ) {
169  uint32_t best = 0;
170  uint32_t index;
171  struct pci_range tmp;
172  EFI_HANDLE *handles;
173  UINTN num_handles;
174  UINTN i;
175  EFI_STATUS efirc;
176  int rc;
177 
178  /* Return an empty range and no handle on error */
179  range->start = 0;
180  range->count = 0;
181  *handle = NULL;
182 
183  /* Enumerate all root bridge I/O protocol handles */
184  if ( ( efirc = bs->LocateHandleBuffer ( ByProtocol,
186  NULL, &num_handles, &handles ) ) != 0 ) {
187  rc = -EEFI ( efirc );
188  DBGC ( pci, "EFIPCI " PCI_FMT " cannot locate root bridges: "
189  "%s\n", PCI_ARGS ( pci ), strerror ( rc ) );
190  goto err_locate;
191  }
192 
193  /* Iterate over all root bridge I/O protocols */
194  for ( i = 0 ; i < num_handles ; i++ ) {
195 
196  /* Get matching or closest range for this root bridge */
197  if ( ( rc = efipci_discover_one ( pci, handles[i],
198  &tmp ) ) != 0 )
199  continue;
200 
201  /* Check for a matching or new closest range */
202  index = ( pci->busdevfn - tmp.start );
203  if ( ( index < tmp.count ) || ( index > best ) ) {
204  range->start = tmp.start;
205  range->count = tmp.count;
206  best = index;
207  }
208 
209  /* Stop if this range contains the target bus:dev.fn address */
210  if ( index < tmp.count ) {
211  *handle = handles[i];
212  break;
213  }
214  }
215 
216  /* Check for a range containing the target bus:dev.fn address */
217  if ( ! *handle ) {
218  rc = -ENOENT;
219  goto err_range;
220  }
221 
222  /* Success */
223  rc = 0;
224 
225  err_range:
226  bs->FreePool ( handles );
227  err_locate:
228  return rc;
229 }
230 
231 /**
232  * Find next PCI bus:dev.fn address range in system
233  *
234  * @v busdevfn Starting PCI bus:dev.fn address
235  * @v range PCI bus:dev.fn address range to fill in
236  */
237 static void efipci_discover ( uint32_t busdevfn, struct pci_range *range ) {
238  struct pci_device pci;
240 
241  /* Find range */
242  memset ( &pci, 0, sizeof ( pci ) );
243  pci_init ( &pci, busdevfn );
244  efipci_discover_any ( &pci, range, &handle );
245 }
246 
247 /**
248  * Open EFI PCI root bridge I/O protocol for ephemeral use
249  *
250  * @v pci PCI device
251  * @ret handle EFI PCI root bridge handle
252  * @ret root EFI PCI root bridge I/O protocol, or NULL if not found
253  * @ret rc Return status code
254  */
255 static int efipci_root_open ( struct pci_device *pci, EFI_HANDLE *handle,
257  struct pci_range tmp;
258  int rc;
259 
260  /* Find matching root bridge I/O protocol handle */
261  if ( ( rc = efipci_discover_any ( pci, &tmp, handle ) ) != 0 )
262  return rc;
263 
264  /* Open PCI root bridge I/O protocol */
266  root ) ) != 0 ) {
267  DBGC ( pci, "EFIPCI " PCI_FMT " cannot open %s: %s\n",
268  PCI_ARGS ( pci ), efi_handle_name ( *handle ),
269  strerror ( rc ) );
270  return rc;
271  }
272 
273  return 0;
274 }
275 
276 /**
277  * Calculate EFI PCI configuration space address
278  *
279  * @v pci PCI device
280  * @v location Encoded offset and width
281  * @ret address EFI PCI address
282  */
283 static unsigned long efipci_address ( struct pci_device *pci,
284  unsigned long location ) {
285 
286  return EFI_PCI_ADDRESS ( PCI_BUS ( pci->busdevfn ),
287  PCI_SLOT ( pci->busdevfn ),
288  PCI_FUNC ( pci->busdevfn ),
289  EFIPCI_OFFSET ( location ) );
290 }
291 
292 /**
293  * Read from PCI configuration space
294  *
295  * @v pci PCI device
296  * @v location Encoded offset and width
297  * @ret value Value
298  * @ret rc Return status code
299  */
300 int efipci_read ( struct pci_device *pci, unsigned long location,
301  void *value ) {
304  EFI_STATUS efirc;
305  int rc;
306 
307  /* Open root bridge */
308  if ( ( rc = efipci_root_open ( pci, &handle, &root ) ) != 0 )
309  return rc;
310 
311  /* Read from configuration space */
312  if ( ( efirc = root->Pci.Read ( root, EFIPCI_WIDTH ( location ),
313  efipci_address ( pci, location ), 1,
314  value ) ) != 0 ) {
315  rc = -EEFI ( efirc );
316  DBGC ( pci, "EFIPCI " PCI_FMT " config read from offset %02lx "
317  "failed: %s\n", PCI_ARGS ( pci ),
318  EFIPCI_OFFSET ( location ), strerror ( rc ) );
319  return rc;
320  }
321 
322  return 0;
323 }
324 
325 /**
326  * Write to PCI configuration space
327  *
328  * @v pci PCI device
329  * @v location Encoded offset and width
330  * @v value Value
331  * @ret rc Return status code
332  */
333 int efipci_write ( struct pci_device *pci, unsigned long location,
334  unsigned long value ) {
337  EFI_STATUS efirc;
338  int rc;
339 
340  /* Open root bridge */
341  if ( ( rc = efipci_root_open ( pci, &handle, &root ) ) != 0 )
342  return rc;
343 
344  /* Read from configuration space */
345  if ( ( efirc = root->Pci.Write ( root, EFIPCI_WIDTH ( location ),
346  efipci_address ( pci, location ), 1,
347  &value ) ) != 0 ) {
348  rc = -EEFI ( efirc );
349  DBGC ( pci, "EFIPCI " PCI_FMT " config write to offset %02lx "
350  "failed: %s\n", PCI_ARGS ( pci ),
351  EFIPCI_OFFSET ( location ), strerror ( rc ) );
352  return rc;
353  }
354 
355  return 0;
356 }
357 
358 /**
359  * Map PCI bus address as an I/O address
360  *
361  * @v bus_addr PCI bus address
362  * @v len Length of region
363  * @ret io_addr I/O address, or NULL on error
364  */
365 void * efipci_ioremap ( struct pci_device *pci, unsigned long bus_addr,
366  size_t len ) {
367  union {
368  union acpi_resource *res;
369  void *raw;
370  } u;
373  unsigned int tag;
375  uint64_t start;
376  uint64_t end;
377  EFI_STATUS efirc;
378  int rc;
379 
380  /* Open root bridge */
381  if ( ( rc = efipci_root_open ( pci, &handle, &root ) ) != 0 )
382  goto err_root;
383 
384  /* Get ACPI resource descriptors */
385  if ( ( efirc = root->Configuration ( root, &u.raw ) ) != 0 ) {
386  rc = -EEFI ( efirc );
387  DBGC ( pci, "EFIPCI " PCI_FMT " cannot get configuration: "
388  "%s\n", PCI_ARGS ( pci ), strerror ( rc ) );
389  goto err_config;
390  }
391 
392  /* Parse resource descriptors */
393  for ( ; ( ( tag = acpi_resource_tag ( u.res ) ) != ACPI_END_RESOURCE ) ;
394  u.res = acpi_resource_next ( u.res ) ) {
395 
396  /* Ignore anything other than a memory range descriptor */
398  continue;
399  if ( u.res->qword.type != ACPI_ADDRESS_TYPE_MEM )
400  continue;
401 
402  /* Ignore descriptors that do not cover this memory range */
403  offset = le64_to_cpu ( u.res->qword.offset );
404  start = ( offset + le64_to_cpu ( u.res->qword.min ) );
405  end = ( start + le64_to_cpu ( u.res->qword.len ) );
406  DBGC2 ( pci, "EFIPCI " PCI_FMT " found range [%08llx,%08llx) "
407  "-> [%08llx,%08llx)\n", PCI_ARGS ( pci ), start, end,
408  ( start - offset ), ( end - offset ) );
409  if ( ( bus_addr < start ) || ( ( bus_addr + len ) > end ) )
410  continue;
411 
412  /* Use this address space descriptor */
413  DBGC2 ( pci, "EFIPCI " PCI_FMT " %08lx+%zx -> ",
414  PCI_ARGS ( pci ), bus_addr, len );
415  bus_addr -= offset;
416  DBGC2 ( pci, "%08lx\n", bus_addr );
417  break;
418  }
419  if ( tag == ACPI_END_RESOURCE ) {
420  DBGC ( pci, "EFIPCI " PCI_FMT " %08lx+%zx is not within "
421  "root bridge address space\n",
422  PCI_ARGS ( pci ), bus_addr, len );
423  }
424 
425  err_config:
426  err_root:
427  return ioremap ( bus_addr, len );
428 }
429 
440 
441 /******************************************************************************
442  *
443  * EFI PCI DMA mappings
444  *
445  ******************************************************************************
446  */
447 
448 /**
449  * Map buffer for DMA
450  *
451  * @v dma DMA device
452  * @v map DMA mapping to fill in
453  * @v addr Buffer address
454  * @v len Length of buffer
455  * @v flags Mapping flags
456  * @ret rc Return status code
457  */
458 static int efipci_dma_map ( struct dma_device *dma, struct dma_mapping *map,
459  void *addr, size_t len, int flags ) {
460  struct efi_pci_device *efipci =
461  container_of ( dma, struct efi_pci_device, pci.dma );
462  struct pci_device *pci = &efipci->pci;
463  EFI_PCI_IO_PROTOCOL *pci_io = efipci->io;
466  UINTN count;
467  VOID *mapping;
469  EFI_STATUS efirc;
470  int rc;
471 
472  /* Sanity check */
473  assert ( map->dma == NULL );
474  assert ( map->offset == 0 );
475  assert ( map->token == NULL );
476 
477  /* Determine operation */
478  switch ( flags ) {
479  case DMA_TX:
481  break;
482  case DMA_RX:
484  break;
485  default:
487  break;
488  }
489 
490  /* Map buffer (if non-zero length) */
491  phys = virt_to_phys ( addr );
492  count = len;
493  if ( len ) {
494  if ( ( efirc = pci_io->Map ( pci_io, op, addr, &count, &bus,
495  &mapping ) ) != 0 ) {
496  rc = -EEFI ( efirc );
497  DBGC ( pci, "EFIPCI " PCI_FMT " cannot map %p+%zx: "
498  "%s\n", PCI_ARGS ( pci ), addr, len,
499  strerror ( rc ) );
500  goto err_map;
501  }
502  } else {
503  bus = phys;
504  mapping = NULL;
505  }
506 
507  /* Check that full length was mapped. The UEFI specification
508  * allows for multiple mappings to be required, but even the
509  * EDK2 PCI device drivers will fail if a platform ever
510  * requires this.
511  */
512  if ( count != len ) {
513  DBGC ( pci, "EFIPCI " PCI_FMT " attempted split mapping for "
514  "%p+%zx\n", PCI_ARGS ( pci ), addr, len );
515  rc = -ENOTSUP;
516  goto err_len;
517  }
518 
519  /* Populate mapping */
520  map->dma = dma;
521  map->offset = ( bus - phys );
522  map->token = mapping;
523 
524  /* Increment mapping count (for debugging) */
525  if ( DBG_LOG )
526  dma->mapped++;
527 
528  return 0;
529 
530  err_len:
531  pci_io->Unmap ( pci_io, mapping );
532  err_map:
533  return rc;
534 }
535 
536 /**
537  * Unmap buffer
538  *
539  * @v dma DMA device
540  * @v map DMA mapping
541  * @v len Used length
542  */
543 static void efipci_dma_unmap ( struct dma_device *dma,
544  struct dma_mapping *map, size_t len __unused ) {
545  struct efi_pci_device *efipci =
546  container_of ( dma, struct efi_pci_device, pci.dma );
547  EFI_PCI_IO_PROTOCOL *pci_io = efipci->io;
548 
549  /* Unmap buffer (if non-zero length) */
550  if ( map->token )
551  pci_io->Unmap ( pci_io, map->token );
552 
553  /* Clear mapping */
554  map->dma = NULL;
555  map->offset = 0;
556  map->token = NULL;
557 
558  /* Decrement mapping count (for debugging) */
559  if ( DBG_LOG )
560  dma->mapped--;
561 }
562 
563 /**
564  * Allocate and map DMA-coherent buffer
565  *
566  * @v dma DMA device
567  * @v map DMA mapping to fill in
568  * @v len Length of buffer
569  * @v align Physical alignment
570  * @ret addr Buffer address, or NULL on error
571  */
572 static void * efipci_dma_alloc ( struct dma_device *dma,
573  struct dma_mapping *map,
574  size_t len, size_t align __unused ) {
575  struct efi_pci_device *efipci =
576  container_of ( dma, struct efi_pci_device, pci.dma );
577  struct pci_device *pci = &efipci->pci;
578  EFI_PCI_IO_PROTOCOL *pci_io = efipci->io;
579  unsigned int pages;
580  VOID *addr;
581  EFI_STATUS efirc;
582  int rc;
583 
584  /* Calculate number of pages */
585  pages = ( ( len + EFI_PAGE_SIZE - 1 ) / EFI_PAGE_SIZE );
586 
587  /* Allocate (page-aligned) buffer */
588  if ( ( efirc = pci_io->AllocateBuffer ( pci_io, AllocateAnyPages,
589  EfiBootServicesData, pages,
590  &addr, 0 ) ) != 0 ) {
591  rc = -EEFI ( efirc );
592  DBGC ( pci, "EFIPCI " PCI_FMT " could not allocate %zd bytes: "
593  "%s\n", PCI_ARGS ( pci ), len, strerror ( rc ) );
594  goto err_alloc;
595  }
596 
597  /* Clear buffer */
598  memset ( addr, 0, ( pages * EFI_PAGE_SIZE ) );
599 
600  /* Map buffer */
601  if ( ( rc = efipci_dma_map ( dma, map, addr, ( pages * EFI_PAGE_SIZE ),
602  DMA_BI ) ) != 0 )
603  goto err_map;
604 
605  /* Increment allocation count (for debugging) */
606  if ( DBG_LOG )
607  dma->allocated++;
608 
609  return addr;
610 
611  efipci_dma_unmap ( dma, map, len );
612  err_map:
613  pci_io->FreeBuffer ( pci_io, pages, addr );
614  err_alloc:
615  return NULL;
616 }
617 
618 /**
619  * Unmap and free DMA-coherent buffer
620  *
621  * @v dma DMA device
622  * @v map DMA mapping
623  * @v addr Buffer address
624  * @v len Length of buffer
625  */
626 static void efipci_dma_free ( struct dma_device *dma, struct dma_mapping *map,
627  void *addr, size_t len ) {
628  struct efi_pci_device *efipci =
629  container_of ( dma, struct efi_pci_device, pci.dma );
630  EFI_PCI_IO_PROTOCOL *pci_io = efipci->io;
631  unsigned int pages;
632 
633  /* Calculate number of pages */
634  pages = ( ( len + EFI_PAGE_SIZE - 1 ) / EFI_PAGE_SIZE );
635 
636  /* Unmap buffer */
637  efipci_dma_unmap ( dma, map, len );
638 
639  /* Free buffer */
640  pci_io->FreeBuffer ( pci_io, pages, addr );
641 
642  /* Decrement allocation count (for debugging) */
643  if ( DBG_LOG )
644  dma->allocated--;
645 }
646 
647 /**
648  * Set addressable space mask
649  *
650  * @v dma DMA device
651  * @v mask Addressable space mask
652  */
653 static void efipci_dma_set_mask ( struct dma_device *dma, physaddr_t mask ) {
654  struct efi_pci_device *efipci =
655  container_of ( dma, struct efi_pci_device, pci.dma );
656  struct pci_device *pci = &efipci->pci;
657  EFI_PCI_IO_PROTOCOL *pci_io = efipci->io;
659  UINT64 attrs;
660  int is64;
661  EFI_STATUS efirc;
662  int rc;
663 
664  /* Set dual address cycle attribute for 64-bit capable devices */
665  is64 = ( ( ( ( uint64_t ) mask ) + 1 ) == 0 );
669  if ( ( efirc = pci_io->Attributes ( pci_io, op, attrs, NULL ) ) != 0 ) {
670  rc = -EEFI ( efirc );
671  DBGC ( pci, "EFIPCI " PCI_FMT " could not %sable DAC: %s\n",
672  PCI_ARGS ( pci ), ( is64 ? "en" : "dis" ),
673  strerror ( rc ) );
674  /* Ignore failure: errors will manifest in mapping attempts */
675  return;
676  }
677 }
678 
679 /** EFI PCI DMA operations */
681  .map = efipci_dma_map,
682  .unmap = efipci_dma_unmap,
683  .alloc = efipci_dma_alloc,
684  .free = efipci_dma_free,
685  .umalloc = efipci_dma_alloc,
686  .ufree = efipci_dma_free,
687  .set_mask = efipci_dma_set_mask,
688 };
689 
690 /******************************************************************************
691  *
692  * EFI PCI device instantiation
693  *
694  ******************************************************************************
695  */
696 
697 /**
698  * Get EFI PCI device information
699  *
700  * @v device EFI device handle
701  * @v efipci EFI PCI device to fill in
702  * @ret rc Return status code
703  */
704 int efipci_info ( EFI_HANDLE device, struct efi_pci_device *efipci ) {
705  EFI_PCI_IO_PROTOCOL *pci_io;
706  UINTN pci_segment, pci_bus, pci_dev, pci_fn;
707  unsigned int busdevfn;
708  EFI_STATUS efirc;
709  int rc;
710 
711  /* See if device is a PCI device */
713  &pci_io ) ) != 0 ) {
714  DBGCP ( device, "EFIPCI %s cannot open PCI protocols: %s\n",
715  efi_handle_name ( device ), strerror ( rc ) );
716  return rc;
717  }
718  efipci->io = pci_io;
719 
720  /* Get PCI bus:dev.fn address */
721  if ( ( efirc = pci_io->GetLocation ( pci_io, &pci_segment, &pci_bus,
722  &pci_dev, &pci_fn ) ) != 0 ) {
723  rc = -EEFI ( efirc );
724  DBGC ( device, "EFIPCI %s could not get PCI location: %s\n",
725  efi_handle_name ( device ), strerror ( rc ) );
726  return rc;
727  }
728  busdevfn = PCI_BUSDEVFN ( pci_segment, pci_bus, pci_dev, pci_fn );
729  pci_init ( &efipci->pci, busdevfn );
730  dma_init ( &efipci->pci.dma, &efipci_dma_operations );
731  DBGCP ( device, "EFIPCI " PCI_FMT " is %s\n",
732  PCI_ARGS ( &efipci->pci ), efi_handle_name ( device ) );
733 
734  /* Try to enable I/O cycles, memory cycles, and bus mastering.
735  * Some platforms will 'helpfully' report errors if these bits
736  * can't be enabled (for example, if the card doesn't actually
737  * support I/O cycles). Work around any such platforms by
738  * enabling bits individually and simply ignoring any errors.
739  */
746 
747  /* Populate PCI device */
748  if ( ( rc = pci_read_config ( &efipci->pci ) ) != 0 ) {
749  DBGC ( device, "EFIPCI " PCI_FMT " cannot read PCI "
750  "configuration: %s\n",
751  PCI_ARGS ( &efipci->pci ), strerror ( rc ) );
752  return rc;
753  }
754 
755  return 0;
756 }
757 
758 /******************************************************************************
759  *
760  * EFI PCI driver
761  *
762  ******************************************************************************
763  */
764 
765 /**
766  * Check to see if driver supports a device
767  *
768  * @v device EFI device handle
769  * @ret rc Return status code
770  */
772  struct efi_pci_device efipci;
773  uint8_t hdrtype;
774  int rc;
775 
776  /* Get PCI device information */
777  if ( ( rc = efipci_info ( device, &efipci ) ) != 0 )
778  return rc;
779 
780  /* Do not attempt to drive bridges */
781  hdrtype = efipci.pci.hdrtype;
782  if ( ( hdrtype & PCI_HEADER_TYPE_MASK ) != PCI_HEADER_TYPE_NORMAL ) {
783  DBGC ( device, "EFIPCI " PCI_FMT " type %02x is not type %02x\n",
784  PCI_ARGS ( &efipci.pci ), hdrtype,
786  return -ENOTTY;
787  }
788 
789  /* Look for a driver */
790  if ( ( rc = pci_find_driver ( &efipci.pci ) ) != 0 ) {
791  DBGC ( device, "EFIPCI " PCI_FMT " (%04x:%04x class %06x) "
792  "has no driver\n", PCI_ARGS ( &efipci.pci ),
793  efipci.pci.vendor, efipci.pci.device,
794  efipci.pci.class );
795  return rc;
796  }
797  DBGC ( device, "EFIPCI " PCI_FMT " (%04x:%04x class %06x) has driver "
798  "\"%s\"\n", PCI_ARGS ( &efipci.pci ), efipci.pci.vendor,
799  efipci.pci.device, efipci.pci.class, efipci.pci.id->name );
800 
801  return 0;
802 }
803 
804 /**
805  * Exclude existing drivers
806  *
807  * @v device EFI device handle
808  * @ret rc Return status code
809  */
812  int rc;
813 
814  /* Exclude existing PCI I/O protocol drivers */
815  if ( ( rc = efi_driver_exclude ( device, protocol ) ) != 0 ) {
816  DBGC ( device, "EFIPCI %s could not exclude drivers: %s\n",
817  efi_handle_name ( device ), strerror ( rc ) );
818  return rc;
819  }
820 
821  return 0;
822 }
823 
824 /**
825  * Attach driver to device
826  *
827  * @v efidev EFI device
828  * @ret rc Return status code
829  */
830 static int efipci_start ( struct efi_device *efidev ) {
831  EFI_HANDLE device = efidev->device;
832  struct efi_pci_device *efipci;
833  int rc;
834 
835  /* Allocate PCI device */
836  efipci = zalloc ( sizeof ( *efipci ) );
837  if ( ! efipci ) {
838  rc = -ENOMEM;
839  goto err_alloc;
840  }
841 
842  /* Get PCI device information */
843  if ( ( rc = efipci_info ( device, efipci ) ) != 0 )
844  goto err_info;
845 
846  /* Open PCI I/O protocol */
848  &efipci->io ) ) != 0 ) {
849  DBGC ( device, "EFIPCI %s could not open PCI device: %s\n",
850  efi_handle_name ( device ), strerror ( rc ) );
852  goto err_open;
853  }
854 
855  /* Find driver */
856  if ( ( rc = pci_find_driver ( &efipci->pci ) ) != 0 ) {
857  DBGC ( device, "EFIPCI " PCI_FMT " has no driver\n",
858  PCI_ARGS ( &efipci->pci ) );
859  goto err_find_driver;
860  }
861 
862  /* Mark PCI device as a child of the EFI device */
863  efipci->pci.dev.parent = &efidev->dev;
864  list_add ( &efipci->pci.dev.siblings, &efidev->dev.children );
865 
866  /* Probe driver */
867  if ( ( rc = pci_probe ( &efipci->pci ) ) != 0 ) {
868  DBGC ( device, "EFIPCI " PCI_FMT " could not probe driver "
869  "\"%s\": %s\n", PCI_ARGS ( &efipci->pci ),
870  efipci->pci.id->name, strerror ( rc ) );
871  goto err_probe;
872  }
873  DBGC ( device, "EFIPCI " PCI_FMT " using driver \"%s\"\n",
874  PCI_ARGS ( &efipci->pci ), efipci->pci.id->name );
875 
876  efidev_set_drvdata ( efidev, efipci );
877  return 0;
878 
879  pci_remove ( &efipci->pci );
880  err_probe:
881  list_del ( &efipci->pci.dev.siblings );
882  err_find_driver:
884  err_open:
885  err_info:
886  free ( efipci );
887  err_alloc:
888  return rc;
889 }
890 
891 /**
892  * Detach driver from device
893  *
894  * @v efidev EFI device
895  */
896 static void efipci_stop ( struct efi_device *efidev ) {
897  struct efi_pci_device *efipci = efidev_get_drvdata ( efidev );
898  EFI_HANDLE device = efidev->device;
899 
900  pci_remove ( &efipci->pci );
901  list_del ( &efipci->pci.dev.siblings );
902  assert ( efipci->pci.dma.mapped == 0 );
903  assert ( efipci->pci.dma.allocated == 0 );
905  free ( efipci );
906 }
907 
908 /** EFI PCI driver */
909 struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_HARDWARE ) = {
910  .name = "PCI",
911  .supported = efipci_supported,
912  .exclude = efipci_exclude,
913  .start = efipci_start,
914  .stop = efipci_stop,
915 };
#define PCI_FUNC(busdevfn)
Definition: pci.h:285
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
#define EFI_PAGE_SIZE
Definition: UefiBaseType.h:187
uint32_t start
Starting bus:dev.fn address.
Definition: pci_io.h:24
#define PCI_BUS(busdevfn)
Definition: pci.h:283
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DMA_TX
Device will read data from host memory.
Definition: dma.h:134
#define EFI_PCI_ADDRESS(bus, dev, func, reg)
struct dma_device dma
DMA device.
Definition: pci.h:214
static void efipci_discover(uint32_t busdevfn, struct pci_range *range)
Find next PCI bus:dev.fn address range in system.
Definition: efi_pci.c:237
struct pci_range range
PCI bus:dev.fn address range.
Definition: pcicloud.c:40
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:174
int efipci_info(EFI_HANDLE device, struct efi_pci_device *efipci)
Get EFI PCI device information.
Definition: efi_pci.c:704
static int efipci_dma_map(struct dma_device *dma, struct dma_mapping *map, void *addr, size_t len, int flags)
Map buffer for DMA.
Definition: efi_pci.c:458
int pci_find_driver(struct pci_device *pci)
Find driver for PCI device.
Definition: pci.c:378
EFI driver interface.
struct dma_device * dma
DMA device (if unmapping is required)
Definition: dma.h:41
struct pci_device pci
PCI device.
Definition: efi_pci.h:23
struct stp_switch root
Root switch.
Definition: stp.h:26
uint32_t class
Device class.
Definition: pci.h:231
int pci_can_probe(struct pci_device *pci)
Check if PCI bus probing is allowed.
#define list_add(new, head)
Add a new entry to the head of a list.
Definition: list.h:69
128 bit buffer containing a unique identifier value.
Definition: Base.h:215
Error codes.
EFI_GUID efi_pci_io_protocol_guid
PCI I/O protocol GUID.
Definition: efi_guid.c:312
static int efipci_root_open(struct pci_device *pci, EFI_HANDLE *handle, EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL **root)
Open EFI PCI root bridge I/O protocol for ephemeral use.
Definition: efi_pci.c:255
A read operation from system memory by a bus master.
Definition: PciIo.h:83
static int efipci_start(struct efi_device *efidev)
Attach driver to device.
Definition: efi_pci.c:830
int pci_write_config_word(struct pci_device *pci, unsigned int where, uint16_t value)
Write 16-bit word to PCI configuration space.
#define ACPI_QWORD_ADDRESS_SPACE_RESOURCE
An ACPI QWORD address space resource descriptor.
Definition: acpi.h:76
#define DMA_RX
Device will write data to host memory.
Definition: dma.h:137
#define PCI_HEADER_TYPE_MASK
Header type mask.
Definition: pci.h:57
EFI_PCI_IO_PROTOCOL_MAP Map
Definition: PciIo.h:525
#define DBGC(...)
Definition: compiler.h:505
long index
Definition: bigint.h:62
#define ENOENT
No such file or directory.
Definition: errno.h:514
unsigned long long uint64_t
Definition: stdint.h:13
EFI_PCI_IO_PROTOCOL_ALLOCATE_BUFFER AllocateBuffer
Definition: PciIo.h:527
int efipci_write(struct pci_device *pci, unsigned long location, unsigned long value)
Write to PCI configuration space.
Definition: efi_pci.c:333
#define EFIPCI_OFFSET(_location)
Definition: efi_pci_api.h:25
int pci_read_config_word(struct pci_device *pci, unsigned int where, uint16_t *value)
Read 16-bit word from PCI configuration space.
#define DBGC_EFI_OPENERS(...)
Definition: efi.h:344
union @18 u
EFI_HANDLE device
EFI device handle.
Definition: efi_driver.h:21
#define DMA_BI
Device will both read data from and write data to host memory.
Definition: dma.h:140
UINT64 EFI_PHYSICAL_ADDRESS
64-bit physical memory address.
Definition: UefiBaseType.h:52
struct device * parent
Bus device.
Definition: device.h:88
static __always_inline void unsigned long bus_addr
Definition: pcibios.h:155
struct device dev
Generic device.
Definition: pci.h:212
static union acpi_resource * acpi_resource_next(union acpi_resource *res)
Get next ACPI resource descriptor.
Definition: acpi.h:168
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
uint32_t start
Starting offset.
Definition: netvsc.h:12
unsigned long tmp
Definition: linux_pci.h:64
static unsigned int acpi_resource_tag(union acpi_resource *res)
Get ACPI resource tag.
Definition: acpi.h:120
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
uint16_t busdevfn
PCI bus:dev.fn address.
Definition: ena.h:28
#define ENOMEM
Not enough space.
Definition: errno.h:534
A hardware device.
Definition: device.h:76
#define EFI_PCI_IO_ATTRIBUTE_MEMORY
Enable the Memory decode bit in the PCI Config Header.
Definition: PciIo.h:60
static signed char phys[4]
Definition: epic100.c:88
PROVIDE_PCIAPI_RUNTIME(efi, PCIAPI_PRIORITY_EFI)
void * efipci_ioremap(struct pci_device *pci, unsigned long bus_addr, size_t len)
Map PCI bus address as an I/O address.
Definition: efi_pci.c:365
uint16_t device
Device ID.
Definition: pci.h:229
#define PCI_HEADER_TYPE_NORMAL
Normal header.
Definition: pci.h:54
PROVIDE_PCIAPI(efi, pci_discover, efipci_discover)
static int efipci_discover_any(struct pci_device *pci, struct pci_range *range, EFI_HANDLE *handle)
Find closest bus:dev.fn address range within any root bridge.
Definition: efi_pci.c:165
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
void efi_close_by_driver(EFI_HANDLE handle, EFI_GUID *protocol)
Close protocol opened for persistent use by a driver.
Definition: efi_open.c:278
int pci_read_config_dword(struct pci_device *pci, unsigned int where, uint32_t *value)
Read 32-bit dword from PCI configuration space.
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static EFI_ACPI_TABLE_PROTOCOL * acpi
ACPI table protocol protocol.
Definition: efi_block.c:66
#define efi_open_by_driver(handle, protocol, interface)
Open protocol for persistent use by a driver.
Definition: efi.h:473
ring len
Length.
Definition: dwmac.h:231
EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION
Definition: PciIo.h:101
A PCI bus:dev.fn address range.
Definition: pci_io.h:22
EFI_PCI_IO_PROTOCOL_ATTRIBUTES Attributes
Definition: PciIo.h:531
EFI_PCI_IO_PROTOCOL_GET_LOCATION GetLocation
Definition: PciIo.h:530
static unsigned int count
Number of entries.
Definition: dwmac.h:225
#define EFI_PCI_IO_ATTRIBUTE_BUS_MASTER
Enable the DMA bit in the PCI Config Header.
Definition: PciIo.h:61
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static void efipci_stop(struct efi_device *efidev)
Detach driver from device.
Definition: efi_pci.c:896
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:652
#define ACPI_ADDRESS_TYPE_BUS
A bus number address space type.
Definition: acpi.h:42
static int efipci_exclude(EFI_HANDLE device)
Exclude existing drivers.
Definition: efi_pci.c:810
#define PCI_BUSDEVFN(segment, bus, slot, func)
Definition: pci_io.h:29
EFI_PCI_IO_PROTOCOL_UNMAP Unmap
Definition: PciIo.h:526
uint8_t flags
Flags.
Definition: ena.h:18
uint8_t hdrtype
Header type.
Definition: pci.h:235
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
ACPI data structures.
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
physaddr_t offset
Address offset.
Definition: dma.h:39
int pci_write_config_byte(struct pci_device *pci, unsigned int where, uint8_t value)
Write byte to PCI configuration space.
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:661
EFI Boot Services Table.
Definition: UefiSpec.h:1930
#define PCI_FMT
PCI device debug message format.
Definition: pci.h:311
#define PCI_SLOT(busdevfn)
Definition: pci.h:284
PCI bus.
struct list_head siblings
Devices on the same bus.
Definition: device.h:84
A PCI device.
Definition: pci.h:210
static void * efidev_get_drvdata(struct efi_device *efidev)
Get EFI driver-private data.
Definition: efi_driver.h:94
uint32_t addr
Buffer address.
Definition: dwmac.h:20
int pci_read_config(struct pci_device *pci)
Read PCI device configuration.
Definition: pci.c:268
#define efi_open(handle, protocol, interface)
Open protocol for ephemeral use.
Definition: efi.h:443
int efi_driver_exclude(EFI_HANDLE device, EFI_GUID *protocol)
Try to disconnect an existing EFI driver.
Definition: efi_driver.c:437
#define EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
Clear for PCI controllers that can not genrate a DAC.
Definition: PciIo.h:66
void pci_remove(struct pci_device *pci)
Remove a PCI device.
Definition: pci.c:432
unsigned char uint8_t
Definition: stdint.h:10
#define ACPI_END_RESOURCE
An ACPI end resource descriptor.
Definition: acpi.h:54
struct efi_driver efipci_driver __efi_driver(EFI_DRIVER_HARDWARE)
EFI PCI driver.
static int efipci_supported(EFI_HANDLE device)
Check to see if driver supports a device.
Definition: efi_pci.c:771
UINT64 UINTN
Unsigned value of native width.
DMA operations.
Definition: dma.h:59
An EFI device.
Definition: efi_driver.h:17
Disable the attributes specified by the bits that are set in Attributes for this PCI controller.
Definition: PciIo.h:117
Provides both read and write access to system memory by both the processor and a bus master.
Definition: PciIo.h:92
#define EFI_PCI_IO_ATTRIBUTE_IO
Enable the I/O decode bit in the PCI Config Header.
Definition: PciIo.h:59
#define VOID
Undeclared type.
Definition: Base.h:271
unsigned int uint32_t
Definition: stdint.h:12
An ACPI resource descriptor.
Definition: acpi.h:101
unsigned long long UINT64
Definition: ProcessorBind.h:96
const char * name
Name.
Definition: pci.h:176
unsigned int count
Number of bus:dev.fn addresses within this range.
Definition: pci_io.h:26
EFI_FREE_POOL FreePool
Definition: UefiSpec.h:1949
EFI_PCI_IO_PROTOCOL_OPERATION
Definition: PciIo.h:79
uint16_t vendor
Vendor ID.
Definition: pci.h:227
EFI API.
PROVIDE_PCIAPI_INLINE(efi, pci_can_probe)
Enable the attributes specified by the bits that are set in Attributes for this PCI controller.
Definition: PciIo.h:113
#define PCIAPI_PRIORITY_EFI
EFI PCI I/O protocols.
Definition: pci_io.h:195
void pci_discover(uint32_t busdevfn, struct pci_range *range)
Find next PCI bus:dev.fn address range in system.
EFI_PCI_IO_PROTOCOL * io
PCI I/O protocol.
Definition: efi_pci.h:25
#define EFI_DRIVER_HARDWARE
Hardware drivers.
Definition: efi_driver.h:72
Provides the basic Memory, I/O, PCI configuration, and DMA interfaces that are used to abstract acces...
unsigned int mapped
Total number of mappings (for debugging)
Definition: dma.h:53
unsigned long physaddr_t
Definition: stdint.h:20
An EFI driver.
Definition: efi_driver.h:33
An EFI PCI device.
Definition: efi_pci.h:21
uint32_t busdevfn
Segment, bus, device, and function (bus:dev.fn) number.
Definition: pci.h:237
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
unsigned int allocated
Total number of allocations (for debugging)
Definition: dma.h:55
static __always_inline void dma_init(struct dma_device *dma, struct dma_operations *op)
Initialise DMA device.
Definition: dma.h:453
#define EFIPCI_WIDTH(_location)
Definition: efi_pci_api.h:26
static __always_inline int struct dma_mapping * map
Definition: dma.h:183
int(* map)(struct dma_device *dma, struct dma_mapping *map, void *addr, size_t len, int flags)
Map buffer for DMA.
Definition: dma.h:70
#define DBGC2(...)
Definition: compiler.h:522
#define ENOTTY
Inappropriate I/O control operation.
Definition: errno.h:594
#define ACPI_ADDRESS_TYPE_MEM
A memory address space type.
Definition: acpi.h:36
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
struct list_head children
Devices attached to this device.
Definition: device.h:86
PCI Root Bridge I/O protocol as defined in the UEFI 2.0 specification.
void * token
Platform mapping token.
Definition: dma.h:43
EFI driver interface.
#define PCI_ARGS(pci)
PCI device debug message arguments.
Definition: pci.h:314
struct pci_device_id * id
Driver device ID.
Definition: pci.h:247
EFI_GUID efi_pci_root_bridge_io_protocol_guid
PCI root bridge I/O protocol GUID.
Definition: efi_guid.c:316
int pci_write_config_dword(struct pci_device *pci, unsigned int where, uint32_t value)
Write 32-bit dword to PCI configuration space.
static int efipci_discover_one(struct pci_device *pci, EFI_HANDLE handle, struct pci_range *range)
Find closest bus:dev.fn address range within a root bridge.
Definition: efi_pci.c:74
Allocate any available range of pages that satisfies the request.
Definition: UefiSpec.h:35
const char * name
Name.
Definition: efi_driver.h:35
uint32_t end
Ending offset.
Definition: netvsc.h:18
#define DBGCP(...)
Definition: compiler.h:539
__be32 raw[7]
Definition: CIB_PRM.h:28
Retrieve the set of handles from the handle database that support a specified protocol.
Definition: UefiSpec.h:1530
EFI_SYSTEM_TABLE * efi_systab
static struct dma_operations efipci_dma_operations
EFI PCI DMA operations.
Definition: efi_pci.c:680
uint16_t protocol
Protocol ID.
Definition: stp.h:18
A write operation from system memory by a bus master.
Definition: PciIo.h:87
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
static void efipci_dma_set_mask(struct dma_device *dma, physaddr_t mask)
Set addressable space mask.
Definition: efi_pci.c:653
The data portions of a loaded Boot Serves Driver, and the default data allocation type used by a Boot...
EFI_PCI_IO_PROTOCOL_FREE_BUFFER FreeBuffer
Definition: PciIo.h:528
static unsigned long efipci_address(struct pci_device *pci, unsigned long location)
Calculate EFI PCI configuration space address.
Definition: efi_pci.c:283
static void pci_init(struct pci_device *pci, unsigned int busdevfn)
Initialise PCI device.
Definition: pci.h:340
void * ioremap(unsigned long bus_addr, size_t len)
Map bus address as an I/O address.
void * pci_ioremap(struct pci_device *pci, unsigned long bus_addr, size_t len)
Map PCI bus address as an I/O address.
EFI PCI I/O Protocol provides the basic Memory, I/O, PCI configuration, and DMA interfaces that a dri...
static void efipci_dma_unmap(struct dma_device *dma, struct dma_mapping *map, size_t len __unused)
Unmap buffer.
Definition: efi_pci.c:543
#define le64_to_cpu(value)
Definition: byteswap.h:114
static void efidev_set_drvdata(struct efi_device *efidev, void *priv)
Set EFI driver-private data.
Definition: efi_driver.h:83
The EFI_PCI_IO_PROTOCOL provides the basic Memory, I/O, PCI configuration, and DMA interfaces used to...
Definition: PciIo.h:518
uint64_t tag
Identity tag.
Definition: edd.h:30
struct device dev
Generic device.
Definition: efi_driver.h:19
#define DBG_LOG
Definition: compiler.h:317
A DMA mapping.
Definition: dma.h:32
uint16_t handle
Handle.
Definition: smbios.h:16
static void efipci_dma_free(struct dma_device *dma, struct dma_mapping *map, void *addr, size_t len)
Unmap and free DMA-coherent buffer.
Definition: efi_pci.c:626
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
int pci_probe(struct pci_device *pci)
Probe a PCI device.
Definition: pci.c:410
String functions.
uint8_t bus
Bus.
Definition: edd.h:14
physaddr_t dma(struct dma_mapping *map, void *addr)
Get DMA address from virtual address.
static void * efipci_dma_alloc(struct dma_device *dma, struct dma_mapping *map, size_t len, size_t align __unused)
Allocate and map DMA-coherent buffer.
Definition: efi_pci.c:572
Definition: efi.h:61
int efipci_read(struct pci_device *pci, unsigned long location, void *value)
Read from PCI configuration space.
Definition: efi_pci.c:300
EFI_LOCATE_HANDLE_BUFFER LocateHandleBuffer
Definition: UefiSpec.h:2007
A DMA-capable device.
Definition: dma.h:47
void * memset(void *dest, int character, size_t len) __nonnull
int pci_read_config_byte(struct pci_device *pci, unsigned int where, uint8_t *value)
Read byte from PCI configuration space.