2 * The PCI Utilities -- Bus Mapping Mode
4 * Copyright (c) 1997--2008 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
18 struct bus_bridge *next;
19 byte this, dev, func, first, last, bug;
25 struct bus_bridge *bridges, *via;
28 static struct bus_info *bus_info;
31 map_bridge(struct bus_info *bi, struct device *d, int np, int ns, int nl)
33 struct bus_bridge *b = xmalloc(sizeof(struct bus_bridge));
34 struct pci_dev *p = d->dev;
36 b->next = bi->bridges;
38 b->this = get_conf_byte(d, np);
41 b->first = get_conf_byte(d, ns);
42 b->last = get_conf_byte(d, nl);
43 printf("## %02x:%02x.%d is a bridge from %02x to %02x-%02x\n",
44 p->bus, p->dev, p->func, b->this, b->first, b->last);
45 if (b->this != p->bus)
46 printf("!!! Bridge points to invalid primary bus.\n");
47 if (b->first > b->last)
49 printf("!!! Bridge points to invalid bus range.\n");
57 int domain = (filter.domain >= 0 ? filter.domain : 0);
59 int verbose = pacc->debugging;
60 struct bus_info *bi = bus_info + bus;
64 printf("Mapping bus %04x:%02x\n", domain, bus);
65 for (dev = 0; dev < 32; dev++)
66 if (filter.slot < 0 || filter.slot == dev)
69 for (func = 0; func < func_limit; func++)
70 if (filter.func < 0 || filter.func == func)
72 struct pci_dev *p = pci_get_dev(pacc, domain, bus, dev, func);
73 u16 vendor = pci_read_word(p, PCI_VENDOR_ID);
74 if (vendor && vendor != 0xffff)
76 if (!func && (pci_read_byte(p, PCI_HEADER_TYPE) & 0x80))
79 printf("Discovered device %04x:%02x:%02x.%d\n", domain, bus, dev, func);
81 if (d = scan_device(p))
84 switch (get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f)
86 case PCI_HEADER_TYPE_BRIDGE:
87 map_bridge(bi, d, PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS);
89 case PCI_HEADER_TYPE_CARDBUS:
90 map_bridge(bi, d, PCI_CB_PRIMARY_BUS, PCI_CB_CARD_BUS, PCI_CB_SUBORDINATE_BUS);
96 printf("But it was filtered out.\n");
104 do_map_bridges(int bus, int min, int max)
106 struct bus_info *bi = bus_info + bus;
107 struct bus_bridge *b;
110 for (b=bi->bridges; b; b=b->next)
112 if (bus_info[b->first].guestbook)
114 else if (b->first < min || b->last > max)
118 bus_info[b->first].via = b;
119 do_map_bridges(b->first, b->first, b->last);
129 printf("\nSummary of buses:\n\n");
130 for (i=0; i<256; i++)
131 if (bus_info[i].exists && !bus_info[i].guestbook)
132 do_map_bridges(i, 0, 255);
133 for (i=0; i<256; i++)
135 struct bus_info *bi = bus_info + i;
136 struct bus_bridge *b = bi->via;
142 printf("Entered via %02x:%02x.%d\n", b->this, b->dev, b->func);
144 printf("Primary host bus\n");
146 printf("Secondary host bus (?)\n");
148 for (b=bi->bridges; b; b=b->next)
150 printf("\t%02x.%d Bridge to %02x-%02x", b->dev, b->func, b->first, b->last);
154 printf(" <overlap bug>");
157 printf(" <crossing bug>");
168 if (pacc->method == PCI_ACCESS_PROC_BUS_PCI ||
169 pacc->method == PCI_ACCESS_SYS_BUS_PCI ||
170 pacc->method == PCI_ACCESS_WIN32_CFGMGR32 ||
171 pacc->method == PCI_ACCESS_DUMP)
172 printf("WARNING: Bus mapping can be reliable only with direct hardware access enabled.\n\n");
173 bus_info = xmalloc(sizeof(struct bus_info) * 256);
174 memset(bus_info, 0, sizeof(struct bus_info) * 256);
176 do_map_bus(filter.bus);
180 for (bus=0; bus<256; bus++)