Initial code release
[external/syslinux.git] / gpxe / src / interface / efi / efi_pci.c
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 FILE_LICENCE ( GPL2_OR_LATER );
20
21 #include <errno.h>
22 #include <gpxe/pci.h>
23 #include <gpxe/efi/efi.h>
24 #include <gpxe/efi/Protocol/PciRootBridgeIo.h>
25
26 /** @file
27  *
28  * gPXE PCI I/O API for EFI
29  *
30  */
31
32 /** PCI root bridge I/O protocol */
33 static EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *efipci;
34 EFI_REQUIRE_PROTOCOL ( EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL, &efipci );
35
36 static unsigned long efipci_address ( struct pci_device *pci,
37                                       unsigned long location ) {
38         return EFI_PCI_ADDRESS ( pci->bus, PCI_SLOT ( pci->devfn ),
39                                  PCI_FUNC ( pci->devfn ),
40                                  EFIPCI_OFFSET ( location ) );
41 }
42
43 int efipci_read ( struct pci_device *pci, unsigned long location,
44                   void *value ) {
45         EFI_STATUS efirc;
46
47         if ( ( efirc = efipci->Pci.Read ( efipci, EFIPCI_WIDTH ( location ),
48                                           efipci_address ( pci, location ), 1,
49                                           value ) ) != 0 ) {
50                 DBG ( "EFIPCI config read from %02x:%02x.%x offset %02lx "
51                       "failed: %s\n", pci->bus, PCI_SLOT ( pci->devfn ),
52                       PCI_FUNC ( pci->devfn ), EFIPCI_OFFSET ( location ),
53                       efi_strerror ( efirc ) );
54                 return -EIO;
55         }
56
57         return 0;
58 }
59
60 int efipci_write ( struct pci_device *pci, unsigned long location,
61                    unsigned long value ) {
62         EFI_STATUS efirc;
63
64         if ( ( efirc = efipci->Pci.Write ( efipci, EFIPCI_WIDTH ( location ),
65                                            efipci_address ( pci, location ), 1,
66                                            &value ) ) != 0 ) {
67                 DBG ( "EFIPCI config write to %02x:%02x.%x offset %02lx "
68                       "failed: %s\n", pci->bus, PCI_SLOT ( pci->devfn ),
69                       PCI_FUNC ( pci->devfn ), EFIPCI_OFFSET ( location ),
70                       efi_strerror ( efirc ) );
71                 return -EIO;
72         }
73
74         return 0;
75 }
76
77 PROVIDE_PCIAPI_INLINE ( efi, pci_max_bus );
78 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_byte );
79 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_word );
80 PROVIDE_PCIAPI_INLINE ( efi, pci_read_config_dword );
81 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_byte );
82 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_word );
83 PROVIDE_PCIAPI_INLINE ( efi, pci_write_config_dword );