aboutsummaryrefslogtreecommitdiffstats
path: root/src/drivers/bus/pcibridge.c
blob: d2763faf9ce83e3109ba49789f7a9c10fc71a0e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2022 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stddef.h>
#include <stdint.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/pci.h>
#include <ipxe/pcibridge.h>

/** @file
 *
 * PCI-to-PCI bridge
 *
 */

/** List of all PCI bridges */
static LIST_HEAD ( pcibridges );

/**
 * Find bridge attached to a PCI device
 *
 * @v pci		PCI device
 * @ret bridge		PCI bridge, or NULL
 */
struct pci_bridge * pcibridge_find ( struct pci_device *pci ) {
	unsigned int bus = PCI_BUS ( pci->busdevfn );
	struct pci_bridge *bridge;

	/* Find matching bridge */
	list_for_each_entry ( bridge, &pcibridges, list ) {
		if ( bus == bridge->secondary )
			return bridge;
	}

	return NULL;
}

/**
 * Probe PCI device
 *
 * @v pci		PCI device
 * @ret rc		Return status code
 */
static int pcibridge_probe ( struct pci_device *pci ) {
	struct pci_bridge *bridge;
	uint16_t base;
	uint16_t limit;
	int rc;

	/* Allocate and initialise structure */
	bridge = zalloc ( sizeof ( *bridge ) );
	if ( ! bridge ) {
		rc = -ENOMEM;
		goto err_alloc;
	}
	bridge->pci = pci;

	/* Read configuration */
	pci_read_config_dword ( pci, PCI_PRIMARY, &bridge->buses );
	cpu_to_le32s ( &buses );
	pci_read_config_word ( pci, PCI_MEM_BASE, &base );
	bridge->membase = ( ( base & ~PCI_MEM_MASK ) << 16 );
	pci_read_config_word ( pci, PCI_MEM_LIMIT, &limit );
	bridge->memlimit = ( ( ( ( limit | PCI_MEM_MASK ) + 1 ) << 16 ) - 1 );
	DBGC ( bridge, "BRIDGE " PCI_FMT " bus %02x to [%02x,%02x) mem "
	       "[%08x,%08x)\n", PCI_ARGS ( pci ), bridge->primary,
	       bridge->secondary, bridge->subordinate, bridge->membase,
	       bridge->memlimit );

	/* Add to list of PCI bridges */
	list_add ( &bridge->list, &pcibridges );

	pci_set_drvdata ( pci, bridge );
	return 0;

	free ( bridge );
 err_alloc:
	return rc;
}

/**
 * Remove PCI device
 *
 * @v pci		PCI device
 */
static void pcibridge_remove ( struct pci_device *pci ) {
	struct pci_bridge *bridge = pci_get_drvdata ( pci );

	/* Remove from list of bridges */
	list_del ( &bridge->list );

	/* Free device */
	free ( bridge );
}

/** Bridge PCI device IDs */
static struct pci_device_id pcibridge_ids[] = {
	PCI_ROM ( 0xffff, 0xffff, "bridge", "Bridge", 0 ),
};

/** Bridge PCI driver */
struct pci_driver pcibridge_driver __pci_driver = {
	.ids = pcibridge_ids,
	.id_count = ( sizeof ( pcibridge_ids ) / sizeof ( pcibridge_ids[0] ) ),
	.class = PCI_CLASS_ID ( PCI_CLASS_BRIDGE, PCI_CLASS_BRIDGE_PCI,
				PCI_ANY_ID ),
	.probe = pcibridge_probe,
	.remove = pcibridge_remove,
};