2 * The PCI Utilities -- Show Bus Tree
4 * Copyright (c) 1997--2020 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);
149 print_it(char *line, char *p)
154 for (p=line; *p; p++)
155 if (*p == '+' || *p == '|')
161 static void show_tree_bridge(struct bridge *, char *, char *);
163 #define LINE_BUF_SIZE 1024
165 static char * FORMAT_CHECK(printf, 3, 4)
166 tree_printf(char *line, char *p, char *fmt, ...)
169 char *end = line + LINE_BUF_SIZE - 2;
175 int res = vsnprintf(p, end - p, fmt, args);
178 /* Ancient C libraries return -1 on overflow */
189 show_tree_dev(struct device *d, char *line, char *p)
191 struct pci_dev *q = d->dev;
195 p = tree_printf(line, p, "%02x.%x", q->dev, q->func);
196 for (b=&host_bridge; b; b=b->chain)
199 if (b->secondary == b->subordinate)
200 p = tree_printf(line, p, "-[%02x]-", b->secondary);
202 p = tree_printf(line, p, "-[%02x-%02x]-", b->secondary, b->subordinate);
203 show_tree_bridge(b, line, p);
207 p = tree_printf(line, p, " %s",
208 pci_lookup_name(pacc, namebuf, sizeof(namebuf),
209 PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
210 q->vendor_id, q->device_id));
215 show_tree_bus(struct bus *b, char *line, char *p)
219 else if (!b->first_dev->bus_next)
221 p = tree_printf(line, p, "--");
222 show_tree_dev(b->first_dev, line, p);
226 struct device *d = b->first_dev;
229 char *p2 = tree_printf(line, p, "+-");
230 show_tree_dev(d, line, p2);
233 p = tree_printf(line, p, "\\-");
234 show_tree_dev(d, line, p);
239 show_tree_bridge(struct bridge *b, char *line, char *p)
242 if (!b->first_bus->sibling)
244 if (b == &host_bridge)
245 p = tree_printf(line, p, "[%04x:%02x]-", b->domain, b->first_bus->number);
246 show_tree_bus(b->first_bus, line, p);
250 struct bus *u = b->first_bus;
255 k = tree_printf(line, p, "+-[%04x:%02x]-", u->domain, u->number);
256 show_tree_bus(u, line, k);
259 k = tree_printf(line, p, "\\-[%04x:%02x]-", u->domain, u->number);
260 show_tree_bus(u, line, k);
265 show_forest(struct pci_filter *filter)
267 char line[LINE_BUF_SIZE];
269 show_tree_bridge(&host_bridge, line, line);
273 for (b=&host_bridge; b; b=b->chain)
275 if (b->br_dev && pci_filter_match(filter, b->br_dev->dev))
277 struct pci_dev *d = b->br_dev->dev;
279 p = tree_printf(line, p, "%04x:%02x:", d->domain_16, d->bus);
280 show_tree_dev(b->br_dev, line, p);