2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * Based in part on pci.c from Etherboot 5.4, by Ken Yap and David
5 * Munro, in turn based on the Linux kernel's PCI implementation.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <gpxe/tables.h>
28 #include <gpxe/device.h>
37 static struct pci_driver pci_drivers[0]
38 __table_start ( struct pci_driver, pci_drivers );
39 static struct pci_driver pci_drivers_end[0]
40 __table_end ( struct pci_driver, pci_drivers );
42 static void pcibus_remove ( struct root_device *rootdev );
48 * @v reg PCI register number
49 * @ret bar Base address register
51 * Reads the specified PCI base address register, including the flags
52 * portion. 64-bit BARs will be handled automatically. If the value
53 * of the 64-bit BAR exceeds the size of an unsigned long (i.e. if the
54 * high dword is non-zero on a 32-bit platform), then the value
55 * returned will be zero plus the flags for a 64-bit BAR. Unreachable
56 * 64-bit BARs are therefore returned as uninitialised 64-bit BARs.
58 static unsigned long pci_bar ( struct pci_device *pci, unsigned int reg ) {
62 pci_read_config_dword ( pci, reg, &low );
63 if ( ( low & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK) )
64 == (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64) ){
65 pci_read_config_dword ( pci, reg + 4, &high );
67 if ( sizeof ( unsigned long ) > sizeof ( uint32_t ) ) {
68 return ( ( ( uint64_t ) high << 32 ) | low );
70 DBG ( "Unhandled 64-bit BAR %08lx%08lx\n",
72 return PCI_BASE_ADDRESS_MEM_TYPE_64;
80 * Find the start of a PCI BAR
83 * @v reg PCI register number
84 * @ret start BAR start address
86 * Reads the specified PCI base address register, and returns the
87 * address portion of the BAR (i.e. without the flags).
89 * If the address exceeds the size of an unsigned long (i.e. if a
90 * 64-bit BAR has a non-zero high dword on a 32-bit machine), the
91 * return value will be zero.
93 unsigned long pci_bar_start ( struct pci_device *pci, unsigned int reg ) {
96 bar = pci_bar ( pci, reg );
97 if ( (bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY ){
98 return ( bar & PCI_BASE_ADDRESS_MEM_MASK );
100 return ( bar & PCI_BASE_ADDRESS_IO_MASK );
105 * Read membase and ioaddr for a PCI device
109 * This scans through all PCI BARs on the specified device. The first
110 * valid memory BAR is recorded as pci_device::membase, and the first
111 * valid IO BAR is recorded as pci_device::ioaddr.
113 * 64-bit BARs are handled automatically. On a 32-bit platform, if a
114 * 64-bit BAR has a non-zero high dword, it will be regarded as
117 static void pci_read_bases ( struct pci_device *pci ) {
121 for ( reg = PCI_BASE_ADDRESS_0; reg <= PCI_BASE_ADDRESS_5; reg += 4 ) {
122 bar = pci_bar ( pci, reg );
123 if ( bar & PCI_BASE_ADDRESS_SPACE_IO ) {
126 ( bar & PCI_BASE_ADDRESS_IO_MASK );
128 if ( ! pci->membase )
130 ( bar & PCI_BASE_ADDRESS_MEM_MASK );
131 /* Skip next BAR if 64-bit */
132 if ( bar & PCI_BASE_ADDRESS_MEM_TYPE_64 )
143 * Set device to be a busmaster in case BIOS neglected to do so. Also
144 * adjust PCI latency timer to a reasonable value, 32.
146 void adjust_pci_device ( struct pci_device *pci ) {
147 unsigned short new_command, pci_command;
148 unsigned char pci_latency;
150 pci_read_config_word ( pci, PCI_COMMAND, &pci_command );
151 new_command = pci_command | PCI_COMMAND_MASTER | PCI_COMMAND_IO;
152 if ( pci_command != new_command ) {
153 DBG ( "PCI BIOS has not enabled device %02x:%02x.%x! "
154 "Updating PCI command %04x->%04x\n", pci->bus,
155 PCI_SLOT ( pci->devfn ), PCI_FUNC ( pci->devfn ),
156 pci_command, new_command );
157 pci_write_config_word ( pci, PCI_COMMAND, new_command );
160 pci_read_config_byte ( pci, PCI_LATENCY_TIMER, &pci_latency);
161 if ( pci_latency < 32 ) {
162 DBG ( "PCI device %02x:%02x.%x latency timer is unreasonably "
163 "low at %d. Setting to 32.\n", pci->bus,
164 PCI_SLOT ( pci->devfn ), PCI_FUNC ( pci->devfn ),
166 pci_write_config_byte ( pci, PCI_LATENCY_TIMER, 32);
174 * @ret rc Return status code
176 * Searches for a driver for the PCI device. If a driver is found,
177 * its probe() routine is called.
179 static int pci_probe ( struct pci_device *pci ) {
180 struct pci_driver *driver;
181 struct pci_device_id *id;
185 DBG ( "Adding PCI device %02x:%02x.%x (%04x:%04x mem %lx io %lx "
186 "irq %d)\n", pci->bus, PCI_SLOT ( pci->devfn ),
187 PCI_FUNC ( pci->devfn ), pci->vendor, pci->device,
188 pci->membase, pci->ioaddr, pci->irq );
190 for ( driver = pci_drivers ; driver < pci_drivers_end ; driver++ ) {
191 for ( i = 0 ; i < driver->id_count ; i++ ) {
192 id = &driver->ids[i];
193 if ( ( id->vendor != PCI_ANY_ID ) &&
194 ( id->vendor != pci->vendor ) )
196 if ( ( id->device != PCI_ANY_ID ) &&
197 ( id->device != pci->device ) )
199 pci->driver = driver;
200 pci->driver_name = id->name;
201 DBG ( "...using driver %s\n", pci->driver_name );
202 if ( ( rc = driver->probe ( pci, id ) ) != 0 ) {
203 DBG ( "......probe failed\n" );
210 DBG ( "...no driver found\n" );
215 * Remove a PCI device
219 static void pci_remove ( struct pci_device *pci ) {
220 pci->driver->remove ( pci );
221 DBG ( "Removed PCI device %02x:%02x.%x\n", pci->bus,
222 PCI_SLOT ( pci->devfn ), PCI_FUNC ( pci->devfn ) );
228 * @v rootdev PCI bus root device
230 * Scans the PCI bus for devices and registers all devices it can
233 static int pcibus_probe ( struct root_device *rootdev ) {
234 struct pci_device *pci = NULL;
235 unsigned int max_bus;
242 max_bus = pci_max_bus();
243 for ( bus = 0 ; bus <= max_bus ; bus++ ) {
244 for ( devfn = 0 ; devfn <= 0xff ; devfn++ ) {
246 /* Allocate struct pci_device */
248 pci = malloc ( sizeof ( *pci ) );
253 memset ( pci, 0, sizeof ( *pci ) );
257 /* Skip all but the first function on
258 * non-multifunction cards
260 if ( PCI_FUNC ( devfn ) == 0 ) {
261 pci_read_config_byte ( pci, PCI_HEADER_TYPE,
263 } else if ( ! ( hdrtype & 0x80 ) ) {
267 /* Check for physical device presence */
268 pci_read_config_dword ( pci, PCI_VENDOR_ID, &tmp );
269 if ( ( tmp == 0xffffffff ) || ( tmp == 0 ) )
272 /* Populate struct pci_device */
273 pci->vendor = ( tmp & 0xffff );
274 pci->device = ( tmp >> 16 );
275 pci_read_config_dword ( pci, PCI_REVISION, &tmp );
276 pci->class = ( tmp >> 8 );
277 pci_read_config_byte ( pci, PCI_INTERRUPT_LINE,
279 pci_read_bases ( pci );
281 /* Add to device hierarchy */
282 snprintf ( pci->dev.name, sizeof ( pci->dev.name ),
283 "PCI%02x:%02x.%x", bus,
284 PCI_SLOT ( devfn ), PCI_FUNC ( devfn ) );
285 pci->dev.desc.bus_type = BUS_TYPE_PCI;
286 pci->dev.desc.pci.busdevfn = PCI_BUSDEVFN (bus, devfn);
287 pci->dev.desc.pci.vendor = pci->vendor;
288 pci->dev.desc.pci.device = pci->device;
289 pci->dev.parent = &rootdev->dev;
290 list_add ( &pci->dev.siblings, &rootdev->dev.children);
291 INIT_LIST_HEAD ( &pci->dev.children );
293 /* Look for a driver */
294 if ( pci_probe ( pci ) == 0 ) {
295 /* pcidev registered, we can drop our ref */
298 /* Not registered; re-use struct pci_device */
299 list_del ( &pci->dev.siblings );
309 pcibus_remove ( rootdev );
314 * Remove PCI root bus
316 * @v rootdev PCI bus root device
318 static void pcibus_remove ( struct root_device *rootdev ) {
319 struct pci_device *pci;
320 struct pci_device *tmp;
322 list_for_each_entry_safe ( pci, tmp, &rootdev->dev.children,
325 list_del ( &pci->dev.siblings );
330 /** PCI bus root device driver */
331 static struct root_driver pci_root_driver = {
332 .probe = pcibus_probe,
333 .remove = pcibus_remove,
336 /** PCI bus root device */
337 struct root_device pci_root_device __root_device = {
338 .dev = { .name = "PCI" },
339 .driver = &pci_root_driver,