2 * The PCI Utilities -- Show Bus Tree
4 * Copyright (c) 1997--2021 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
15 struct bridge host_bridge = { NULL, NULL, NULL, NULL, NULL, 0, ~0, 0, ~0, NULL };
18 find_bus(struct bridge *b, unsigned int domain, unsigned int n)
22 for (bus=b->first_bus; bus; bus=bus->sibling)
23 if (bus->domain == domain && bus->number == n)
29 new_bus(struct bridge *b, unsigned int domain, unsigned int n)
31 struct bus *bus = xmalloc(sizeof(struct bus));
35 bus->first_dev = NULL;
36 bus->last_dev = &bus->first_dev;
37 bus->parent_bridge = b;
39 b->last_bus->sibling = bus;
47 insert_dev(struct device *d, struct bridge *b)
49 struct pci_dev *p = d->dev;
52 if (! (bus = find_bus(b, p->domain, p->bus)))
55 for (c=b->child; c; c=c->next)
56 if (c->domain == (unsigned)p->domain && c->secondary <= p->bus && p->bus <= c->subordinate)
61 bus = new_bus(b, p->domain, p->bus);
63 /* Simple insertion at the end _does_ guarantee the correct order as the
64 * original device list was sorted by (domain, bus, devfn) lexicographically
65 * and all devices on the new list have the same bus number.
68 bus->last_dev = &d->bus_next;
77 struct bridge **last_br, *b;
79 /* Build list of bridges */
81 last_br = &host_bridge.chain;
82 for (d=first_dev; d; d=d->next)
84 struct pci_dev *dd = d->dev;
85 word class = dd->device_class;
86 byte ht = get_conf_byte(d, PCI_HEADER_TYPE) & 0x7f;
87 if ((class >> 8) == PCI_BASE_CLASS_BRIDGE &&
88 (ht == PCI_HEADER_TYPE_BRIDGE || ht == PCI_HEADER_TYPE_CARDBUS))
90 b = xmalloc(sizeof(struct bridge));
91 b->domain = dd->domain;
92 if (ht == PCI_HEADER_TYPE_BRIDGE)
94 b->primary = get_conf_byte(d, PCI_PRIMARY_BUS);
95 b->secondary = get_conf_byte(d, PCI_SECONDARY_BUS);
96 b->subordinate = get_conf_byte(d, PCI_SUBORDINATE_BUS);
100 b->primary = get_conf_byte(d, PCI_CB_PRIMARY_BUS);
101 b->secondary = get_conf_byte(d, PCI_CB_CARD_BUS);
102 b->subordinate = get_conf_byte(d, PCI_CB_SUBORDINATE_BUS);
106 b->next = b->child = NULL;
111 pacc->debug("Tree: bridge %04x:%02x:%02x.%d: %02x -> %02x-%02x\n",
112 dd->domain, dd->bus, dd->dev, dd->func,
113 b->primary, b->secondary, b->subordinate);
118 /* Create a bridge tree */
120 for (b=&host_bridge; b; b=b->chain)
122 struct bridge *c, *best;
124 for (c=&host_bridge; c; c=c->chain)
125 if (c != b && (c == &host_bridge || b->domain == c->domain) &&
126 b->primary >= c->secondary && b->primary <= c->subordinate &&
127 (!best || best->subordinate - best->primary > c->subordinate - c->primary))
131 b->next = best->child;
136 /* Insert secondary bus for each bridge */
138 for (b=&host_bridge; b; b=b->chain)
139 if (!find_bus(b, b->domain, b->secondary))
140 new_bus(b, b->domain, b->secondary);
142 /* Create bus structs and link devices */
144 for (d=first_dev; d; d=d->next)
145 insert_dev(d, &host_bridge);
148 #define LINE_BUF_SIZE 1024
151 print_it(char *line, char *p)
155 if (p >= line + LINE_BUF_SIZE - 1)
156 fputs("...", stdout);
158 for (p=line; *p; p++)
159 if (*p == '+' || *p == '|')
165 static void show_tree_bridge(struct bridge *, char *, char *);
167 static char * FORMAT_CHECK(printf, 3, 4)
168 tree_printf(char *line, char *p, char *fmt, ...)
171 int space = line + LINE_BUF_SIZE - 1 - p;
177 int res = vsnprintf(p, space, fmt, args);
180 /* Ancient C libraries return -1 on overflow and they do not truncate the output properly. */
184 else if (res >= space)
194 show_tree_dev(struct device *d, char *line, char *p)
196 struct pci_dev *q = d->dev;
200 p = tree_printf(line, p, "%02x.%x", q->dev, q->func);
201 for (b=&host_bridge; b; b=b->chain)
204 if (b->secondary == b->subordinate)
205 p = tree_printf(line, p, "-[%02x]-", b->secondary);
207 p = tree_printf(line, p, "-[%02x-%02x]-", b->secondary, b->subordinate);
208 show_tree_bridge(b, line, p);
212 p = tree_printf(line, p, " %s",
213 pci_lookup_name(pacc, namebuf, sizeof(namebuf),
214 PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
215 q->vendor_id, q->device_id));
220 show_tree_bus(struct bus *b, char *line, char *p)
224 else if (!b->first_dev->bus_next)
226 p = tree_printf(line, p, "--");
227 show_tree_dev(b->first_dev, line, p);
231 struct device *d = b->first_dev;
234 char *p2 = tree_printf(line, p, "+-");
235 show_tree_dev(d, line, p2);
238 p = tree_printf(line, p, "\\-");
239 show_tree_dev(d, line, p);
244 show_tree_bridge(struct bridge *b, char *line, char *p)
247 if (!b->first_bus->sibling)
249 if (b == &host_bridge)
250 p = tree_printf(line, p, "[%04x:%02x]-", b->domain, b->first_bus->number);
251 show_tree_bus(b->first_bus, line, p);
255 struct bus *u = b->first_bus;
260 k = tree_printf(line, p, "+-[%04x:%02x]-", u->domain, u->number);
261 show_tree_bus(u, line, k);
264 k = tree_printf(line, p, "\\-[%04x:%02x]-", u->domain, u->number);
265 show_tree_bus(u, line, k);
270 show_forest(struct pci_filter *filter)
272 char line[LINE_BUF_SIZE];
274 show_tree_bridge(&host_bridge, line, line);
278 for (b=&host_bridge; b; b=b->chain)
280 if (b->br_dev && pci_filter_match(filter, b->br_dev->dev))
282 struct pci_dev *d = b->br_dev->dev;
284 p = tree_printf(line, p, "%04x:%02x:", d->domain_16, d->bus);
285 show_tree_dev(b->br_dev, line, p);