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