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