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  /* Map buffer */
528  if ( ( rc = efipci_dma_map ( dma, map, virt_to_phys ( addr ),
529  ( pages * EFI_PAGE_SIZE ),
530  DMA_BI ) ) != 0 )
531  goto err_map;
532 
533  /* Increment allocation count (for debugging) */
534  if ( DBG_LOG )
535  dma->allocated++;
536 
537  return addr;
538 
539  efipci_dma_unmap ( dma, map );
540  err_map:
541  pci_io->FreeBuffer ( pci_io, pages, addr );
542  err_alloc:
543  return NULL;
544 }
545 
546 /**
547  * Unmap and free DMA-coherent buffer
548  *
549  * @v dma DMA device
550  * @v map DMA mapping
551  * @v addr Buffer address
552  * @v len Length of buffer
553  */
554 static void efipci_dma_free ( struct dma_device *dma, struct dma_mapping *map,
555  void *addr, size_t len ) {
556  struct efi_pci_device *efipci =
557  container_of ( dma, struct efi_pci_device, pci.dma );
558  EFI_PCI_IO_PROTOCOL *pci_io = efipci->io;
559  unsigned int pages;
560 
561  /* Calculate number of pages */
562  pages = ( ( len + EFI_PAGE_SIZE - 1 ) / EFI_PAGE_SIZE );
563 
564  /* Unmap buffer */
565  efipci_dma_unmap ( dma, map );
566 
567  /* Free buffer */
568  pci_io->FreeBuffer ( pci_io, pages, addr );
569 
570  /* Decrement allocation count (for debugging) */
571  if ( DBG_LOG )
572  dma->allocated--;
573 }
574 
575 /**
576  * Allocate and map DMA-coherent buffer from external (user) memory
577  *
578  * @v dma DMA device
579  * @v map DMA mapping to fill in
580  * @v len Length of buffer
581  * @v align Physical alignment
582  * @ret addr Buffer address, or NULL on error
583  */
585  struct dma_mapping *map,
586  size_t len, size_t align ) {
587  void *addr;
588 
590  return virt_to_user ( addr );
591 }
592 
593 /**
594  * Unmap and free DMA-coherent buffer from external (user) memory
595  *
596  * @v dma DMA device
597  * @v map DMA mapping
598  * @v addr Buffer address
599  * @v len Length of buffer
600  */
601 static void efipci_dma_ufree ( struct dma_device *dma, struct dma_mapping *map,
602  userptr_t addr, size_t len ) {
603 
604  efipci_dma_free ( dma, map, user_to_virt ( addr, 0 ), len );
605 }
606 
607 /**
608  * Set addressable space mask
609  *
610  * @v dma DMA device
611  * @v mask Addressable space mask
612  */
613 static void efipci_dma_set_mask ( struct dma_device *dma, physaddr_t mask ) {
614  struct efi_pci_device *efipci =
615  container_of ( dma, struct efi_pci_device, pci.dma );
616  struct pci_device *pci = &efipci->pci;
617  EFI_PCI_IO_PROTOCOL *pci_io = efipci->io;
619  UINT64 attrs;
620  int is64;
621  EFI_STATUS efirc;
622  int rc;
623 
624  /* Set dual address cycle attribute for 64-bit capable devices */
625  is64 = ( ( ( ( uint64_t ) mask ) + 1 ) == 0 );
629  if ( ( efirc = pci_io->Attributes ( pci_io, op, attrs, NULL ) ) != 0 ) {
630  rc = -EEFI ( efirc );
631  DBGC ( pci, "EFIPCI " PCI_FMT " could not %sable DAC: %s\n",
632  PCI_ARGS ( pci ), ( is64 ? "en" : "dis" ),
633  strerror ( rc ) );
634  /* Ignore failure: errors will manifest in mapping attempts */
635  return;
636  }
637 }
638 
639 /** EFI PCI DMA operations */
641  .map = efipci_dma_map,
642  .unmap = efipci_dma_unmap,
643  .alloc = efipci_dma_alloc,
644  .free = efipci_dma_free,
645  .umalloc = efipci_dma_umalloc,
646  .ufree = efipci_dma_ufree,
647  .set_mask = efipci_dma_set_mask,
648 };
649 
650 /******************************************************************************
651  *
652  * EFI PCI device instantiation
653  *
654  ******************************************************************************
655  */
656 
657 /**
658  * Open EFI PCI device
659  *
660  * @v device EFI device handle
661  * @v attributes Protocol opening attributes
662  * @v efipci EFI PCI device to fill in
663  * @ret rc Return status code
664  */
666  struct efi_pci_device *efipci ) {
668  union {
669  EFI_PCI_IO_PROTOCOL *pci_io;
670  void *interface;
671  } pci_io;
672  UINTN pci_segment, pci_bus, pci_dev, pci_fn;
673  unsigned int busdevfn;
674  EFI_STATUS efirc;
675  int rc;
676 
677  /* See if device is a PCI device */
678  if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid,
679  &pci_io.interface, efi_image_handle,
680  device, attributes ) ) != 0 ) {
681  rc = -EEFI_PCI ( efirc );
682  DBGCP ( device, "EFIPCI %s cannot open PCI protocols: %s\n",
683  efi_handle_name ( device ), strerror ( rc ) );
684  goto err_open_protocol;
685  }
686  efipci->io = pci_io.pci_io;
687 
688  /* Get PCI bus:dev.fn address */
689  if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment,
690  &pci_bus, &pci_dev,
691  &pci_fn ) ) != 0 ) {
692  rc = -EEFI ( efirc );
693  DBGC ( device, "EFIPCI %s could not get PCI location: %s\n",
694  efi_handle_name ( device ), strerror ( rc ) );
695  goto err_get_location;
696  }
697  busdevfn = PCI_BUSDEVFN ( pci_segment, pci_bus, pci_dev, pci_fn );
698  pci_init ( &efipci->pci, busdevfn );
699  dma_init ( &efipci->pci.dma, &efipci_dma_operations );
700  DBGCP ( device, "EFIPCI " PCI_FMT " is %s\n",
701  PCI_ARGS ( &efipci->pci ), efi_handle_name ( device ) );
702 
703  /* Try to enable I/O cycles, memory cycles, and bus mastering.
704  * Some platforms will 'helpfully' report errors if these bits
705  * can't be enabled (for example, if the card doesn't actually
706  * support I/O cycles). Work around any such platforms by
707  * enabling bits individually and simply ignoring any errors.
708  */
709  pci_io.pci_io->Attributes ( pci_io.pci_io,
712  pci_io.pci_io->Attributes ( pci_io.pci_io,
715  pci_io.pci_io->Attributes ( pci_io.pci_io,
718 
719  /* Populate PCI device */
720  if ( ( rc = pci_read_config ( &efipci->pci ) ) != 0 ) {
721  DBGC ( device, "EFIPCI " PCI_FMT " cannot read PCI "
722  "configuration: %s\n",
723  PCI_ARGS ( &efipci->pci ), strerror ( rc ) );
724  goto err_pci_read_config;
725  }
726 
727  return 0;
728 
729  err_pci_read_config:
730  err_get_location:
733  err_open_protocol:
734  return rc;
735 }
736 
737 /**
738  * Close EFI PCI device
739  *
740  * @v device EFI device handle
741  */
744 
747 }
748 
749 /**
750  * Get EFI PCI device information
751  *
752  * @v device EFI device handle
753  * @v efipci EFI PCI device to fill in
754  * @ret rc Return status code
755  */
756 int efipci_info ( EFI_HANDLE device, struct efi_pci_device *efipci ) {
757  int rc;
758 
759  /* Open PCI device, if possible */
761  efipci ) ) != 0 )
762  return rc;
763 
764  /* Close PCI device */
765  efipci_close ( device );
766 
767  return 0;
768 }
769 
770 /******************************************************************************
771  *
772  * EFI PCI driver
773  *
774  ******************************************************************************
775  */
776 
777 /**
778  * Check to see if driver supports a device
779  *
780  * @v device EFI device handle
781  * @ret rc Return status code
782  */
784  struct efi_pci_device efipci;
785  int rc;
786 
787  /* Get PCI device information */
788  if ( ( rc = efipci_info ( device, &efipci ) ) != 0 )
789  return rc;
790 
791  /* Look for a driver */
792  if ( ( rc = pci_find_driver ( &efipci.pci ) ) != 0 ) {
793  DBGC ( device, "EFIPCI " PCI_FMT " (%04x:%04x class %06x) "
794  "has no driver\n", PCI_ARGS ( &efipci.pci ),
795  efipci.pci.vendor, efipci.pci.device,
796  efipci.pci.class );
797  return rc;
798  }
799  DBGC ( device, "EFIPCI " PCI_FMT " (%04x:%04x class %06x) has driver "
800  "\"%s\"\n", PCI_ARGS ( &efipci.pci ), efipci.pci.vendor,
801  efipci.pci.device, efipci.pci.class, efipci.pci.id->name );
802 
803  return 0;
804 }
805 
806 /**
807  * Attach driver to device
808  *
809  * @v efidev EFI device
810  * @ret rc Return status code
811  */
812 static int efipci_start ( struct efi_device *efidev ) {
813  EFI_HANDLE device = efidev->device;
814  struct efi_pci_device *efipci;
815  int rc;
816 
817  /* Allocate PCI device */
818  efipci = zalloc ( sizeof ( *efipci ) );
819  if ( ! efipci ) {
820  rc = -ENOMEM;
821  goto err_alloc;
822  }
823 
824  /* Open PCI device */
827  efipci ) ) != 0 ) {
828  DBGC ( device, "EFIPCI %s could not open PCI device: %s\n",
829  efi_handle_name ( device ), strerror ( rc ) );
831  goto err_open;
832  }
833 
834  /* Find driver */
835  if ( ( rc = pci_find_driver ( &efipci->pci ) ) != 0 ) {
836  DBGC ( device, "EFIPCI " PCI_FMT " has no driver\n",
837  PCI_ARGS ( &efipci->pci ) );
838  goto err_find_driver;
839  }
840 
841  /* Mark PCI device as a child of the EFI device */
842  efipci->pci.dev.parent = &efidev->dev;
843  list_add ( &efipci->pci.dev.siblings, &efidev->dev.children );
844 
845  /* Probe driver */
846  if ( ( rc = pci_probe ( &efipci->pci ) ) != 0 ) {
847  DBGC ( device, "EFIPCI " PCI_FMT " could not probe driver "
848  "\"%s\": %s\n", PCI_ARGS ( &efipci->pci ),
849  efipci->pci.id->name, strerror ( rc ) );
850  goto err_probe;
851  }
852  DBGC ( device, "EFIPCI " PCI_FMT " using driver \"%s\"\n",
853  PCI_ARGS ( &efipci->pci ), efipci->pci.id->name );
854 
855  efidev_set_drvdata ( efidev, efipci );
856  return 0;
857 
858  pci_remove ( &efipci->pci );
859  err_probe:
860  list_del ( &efipci->pci.dev.siblings );
861  err_find_driver:
862  efipci_close ( device );
863  err_open:
864  free ( efipci );
865  err_alloc:
866  return rc;
867 }
868 
869 /**
870  * Detach driver from device
871  *
872  * @v efidev EFI device
873  */
874 static void efipci_stop ( struct efi_device *efidev ) {
875  struct efi_pci_device *efipci = efidev_get_drvdata ( efidev );
876  EFI_HANDLE device = efidev->device;
877 
878  pci_remove ( &efipci->pci );
879  list_del ( &efipci->pci.dev.siblings );
880  assert ( efipci->pci.dma.mapped == 0 );
881  assert ( efipci->pci.dma.allocated == 0 );
882  efipci_close ( device );
883  free ( efipci );
884 }
885 
886 /** EFI PCI driver */
887 struct efi_driver efipci_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
888  .name = "PCI",
889  .supported = efipci_supported,
890  .start = efipci_start,
891  .stop = efipci_stop,
892 };
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:264
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:262
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:195
#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:756
int pci_find_driver(struct pci_device *pci)
Find driver for PCI device.
Definition: pci.c:277
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:212
#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:812
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:665
#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
#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 PCI_BUSDEVFN(segment, bus, slot, func)
Definition: pci.h:265
#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
PROVIDE_PCIAPI_INLINE(efi, pci_num_bus)
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:193
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
#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
static __always_inline void unsigned long bus_addr
Definition: linux_pci.h:150
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:210
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:601
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:584
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static void efipci_stop(struct efi_device *efidev)
Detach driver from device.
Definition: efi_pci.c:874
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
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:293
EFI_HANDLE efi_image_handle
Image handle passed to entry point.
Definition: efi_init.c:33
#define PCI_SLOT(busdevfn)
Definition: pci.h:263
PCI bus.
struct list_head siblings
Devices on the same bus.
Definition: device.h:81
A PCI device.
Definition: pci.h:191
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:177
#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:331
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:783
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:157
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:208
EFI API.
Enable the attributes specified by the bits that are set in Attributes for this PCI controller.
Definition: PciIo.h:119
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:216
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
int pci_num_bus(void)
Determine number of PCI buses within system.
#define EEFI_PCI(efirc)
Definition: efi_pci.c:54
void efipci_close(EFI_HANDLE device)
Close EFI PCI device.
Definition: efi_pci.c:742
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:296
struct pci_device_id * id
Driver device ID.
Definition: pci.h:226
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:640
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:613
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:319
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
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:261
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:554
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
int pci_probe(struct pci_device *pci)
Probe a PCI device.
Definition: pci.c:309
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
#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