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