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