[tcpip] Allow supported address families to be detected at runtime
[ipxe.git] / src / drivers / bus / pcibackup.c
1 /*
2 * Copyright (C) 2009 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 <stdint.h>
27 #include <ipxe/pci.h>
28 #include <ipxe/pcibackup.h>
29
30 /** @file
31 *
32 * PCI configuration space backup and restoration
33 *
34 */
35
36 /**
37 * Check PCI configuration space offset against exclusion list
38 *
39 * @v pci PCI device
40 * @v offset Offset within PCI configuration space
41 * @v exclude PCI configuration space backup exclusion list, or NULL
42 */
43 static int
44 pci_backup_excluded ( struct pci_device *pci, unsigned int offset,
45 const uint8_t *exclude ) {
46
47 if ( ! exclude )
48 return 0;
49 for ( ; *exclude != PCI_CONFIG_BACKUP_EXCLUDE_END ; exclude++ ) {
50 if ( offset == *exclude ) {
51 DBGC ( pci, "PCI %p skipping configuration offset "
52 "%02x\n", pci, offset );
53 return 1;
54 }
55 }
56 return 0;
57 }
58
59 /**
60 * Back up PCI configuration space
61 *
62 * @v pci PCI device
63 * @v backup PCI configuration space backup
64 * @v exclude PCI configuration space backup exclusion list, or NULL
65 */
66 void pci_backup ( struct pci_device *pci, struct pci_config_backup *backup,
67 const uint8_t *exclude ) {
68 unsigned int offset;
69 uint32_t *dword;
70
71 for ( offset = 0, dword = backup->dwords ; offset < 0x100 ;
72 offset += sizeof ( *dword ) , dword++ ) {
73 if ( ! pci_backup_excluded ( pci, offset, exclude ) )
74 pci_read_config_dword ( pci, offset, dword );
75 }
76 }
77
78 /**
79 * Restore PCI configuration space
80 *
81 * @v pci PCI device
82 * @v backup PCI configuration space backup
83 * @v exclude PCI configuration space backup exclusion list, or NULL
84 */
85 void pci_restore ( struct pci_device *pci, struct pci_config_backup *backup,
86 const uint8_t *exclude ) {
87 unsigned int offset;
88 uint32_t *dword;
89
90 for ( offset = 0, dword = backup->dwords ; offset < 0x100 ;
91 offset += sizeof ( *dword ) , dword++ ) {
92 if ( ! pci_backup_excluded ( pci, offset, exclude ) )
93 pci_write_config_dword ( pci, offset, *dword );
94 }
95 }