1 // PCI config space access functions.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "malloc.h" // malloc_tmp
9 #include "output.h" // dprintf
10 #include "pci.h" // pci_config_writel
11 #include "pci_regs.h" // PCI_VENDOR_ID
12 #include "romfile.h" // romfile_loadint
13 #include "string.h" // memset
14 #include "util.h" // udelay
15 #include "x86.h" // outl
17 void pci_config_writel(u16 bdf, u32 addr, u32 val)
19 outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
20 outl(val, PORT_PCI_DATA);
23 void pci_config_writew(u16 bdf, u32 addr, u16 val)
25 outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
26 outw(val, PORT_PCI_DATA + (addr & 2));
29 void pci_config_writeb(u16 bdf, u32 addr, u8 val)
31 outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
32 outb(val, PORT_PCI_DATA + (addr & 3));
35 u32 pci_config_readl(u16 bdf, u32 addr)
37 outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
38 return inl(PORT_PCI_DATA);
41 u16 pci_config_readw(u16 bdf, u32 addr)
43 outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
44 return inw(PORT_PCI_DATA + (addr & 2));
47 u8 pci_config_readb(u16 bdf, u32 addr)
49 outl(0x80000000 | (bdf << 8) | (addr & 0xfc), PORT_PCI_CMD);
50 return inb(PORT_PCI_DATA + (addr & 3));
54 pci_config_maskw(u16 bdf, u32 addr, u16 off, u16 on)
56 u16 val = pci_config_readw(bdf, addr);
57 val = (val & ~off) | on;
58 pci_config_writew(bdf, addr, val);
61 // Helper function for foreachbdf() macro - return next device
63 pci_next(int bdf, int bus)
65 if (pci_bdf_to_fn(bdf) == 0
66 && (pci_config_readb(bdf, PCI_HEADER_TYPE) & 0x80) == 0)
67 // Last found device wasn't a multi-function device - skip to
74 if (pci_bdf_to_bus(bdf) != bus)
77 u16 v = pci_config_readw(bdf, PCI_VENDOR_ID);
78 if (v != 0x0000 && v != 0xffff)
82 if (pci_bdf_to_fn(bdf) == 0)
89 struct hlist_head PCIDevices VARVERIFY32INIT;
90 int MaxPCIBus VARFSEG;
92 // Check if PCI is available at all
96 outl(0x80000000, PORT_PCI_CMD);
97 if (inl(PORT_PCI_CMD) != 0x80000000) {
98 dprintf(1, "Detected non-PCI system\n");
104 // Find all PCI devices and populate PCIDevices linked list.
106 pci_probe_devices(void)
108 dprintf(3, "PCI probe\n");
109 struct pci_device *busdevs[256];
110 memset(busdevs, 0, sizeof(busdevs));
111 struct hlist_node **pprev = &PCIDevices.first;
112 int extraroots = romfile_loadint("etc/extra-pci-roots", 0);
113 int bus = -1, lastbus = 0, rootbuses = 0, count=0;
114 while (bus < 0xff && (bus < MaxPCIBus || rootbuses < extraroots)) {
117 foreachbdf(bdf, bus) {
118 // Create new pci_device struct and add to list.
119 struct pci_device *dev = malloc_tmp(sizeof(*dev));
124 memset(dev, 0, sizeof(*dev));
125 hlist_add(&dev->node, pprev);
126 pprev = &dev->node.next;
129 // Find parent device.
131 struct pci_device *parent = busdevs[bus];
140 rootbus = parent->rootbus;
143 // Populate pci_device info.
145 dev->parent = parent;
146 dev->rootbus = rootbus;
147 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
148 dev->vendor = vendev & 0xffff;
149 dev->device = vendev >> 16;
150 u32 classrev = pci_config_readl(bdf, PCI_CLASS_REVISION);
151 dev->class = classrev >> 16;
152 dev->prog_if = classrev >> 8;
153 dev->revision = classrev & 0xff;
154 dev->header_type = pci_config_readb(bdf, PCI_HEADER_TYPE);
155 u8 v = dev->header_type & 0x7f;
156 if (v == PCI_HEADER_TYPE_BRIDGE || v == PCI_HEADER_TYPE_CARDBUS) {
157 u8 secbus = pci_config_readb(bdf, PCI_SECONDARY_BUS);
158 dev->secondary_bus = secbus;
159 if (secbus > bus && !busdevs[secbus])
160 busdevs[secbus] = dev;
161 if (secbus > MaxPCIBus)
164 dprintf(4, "PCI device %02x:%02x.%x (vd=%04x:%04x c=%04x)\n"
165 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
167 , dev->vendor, dev->device, dev->class);
170 dprintf(1, "Found %d PCI devices (max PCI bus is %02x)\n", count, MaxPCIBus);
173 // Search for a device with the specified vendor and device ids.
175 pci_find_device(u16 vendid, u16 devid)
177 struct pci_device *pci;
179 if (pci->vendor == vendid && pci->device == devid)
185 // Search for a device with the specified class id.
187 pci_find_class(u16 classid)
189 struct pci_device *pci;
191 if (pci->class == classid)
197 int pci_init_device(const struct pci_device_id *ids
198 , struct pci_device *pci, void *arg)
200 while (ids->vendid || ids->class_mask) {
201 if ((ids->vendid == PCI_ANY_ID || ids->vendid == pci->vendor) &&
202 (ids->devid == PCI_ANY_ID || ids->devid == pci->device) &&
203 !((ids->class ^ pci->class) & ids->class_mask)) {
214 pci_find_init_device(const struct pci_device_id *ids, void *arg)
216 struct pci_device *pci;
218 if (pci_init_device(ids, pci, arg) == 0)
224 u8 pci_find_capability(struct pci_device *pci, u8 cap_id, u8 cap)
227 u16 status = pci_config_readw(pci->bdf, PCI_STATUS);
229 if (!(status & PCI_STATUS_CAP_LIST))
234 cap = pci_config_readb(pci->bdf, PCI_CAPABILITY_LIST);
237 cap = pci_config_readb(pci->bdf, cap + PCI_CAP_LIST_NEXT);
239 for (i = 0; cap && i <= 0xff; i++) {
240 if (pci_config_readb(pci->bdf, cap + PCI_CAP_LIST_ID) == cap_id)
242 cap = pci_config_readb(pci->bdf, cap + PCI_CAP_LIST_NEXT);
248 /* Test whether bridge support forwarding of transactions
249 * of a specific type.
250 * Note: disables bridge's window registers as a side effect.
252 int pci_bridge_has_region(struct pci_device *pci,
253 enum pci_region_type region_type)
257 switch (region_type) {
258 case PCI_REGION_TYPE_IO:
261 case PCI_REGION_TYPE_PREFMEM:
262 base = PCI_PREF_MEMORY_BASE;
265 /* Regular memory support is mandatory */
269 pci_config_writeb(pci->bdf, base, 0xFF);
271 return pci_config_readb(pci->bdf, base) != 0;
277 u8 v = inb(PORT_PCI_REBOOT) & ~6;
278 outb(v|2, PORT_PCI_REBOOT); /* Request hard reset */
280 outb(v|6, PORT_PCI_REBOOT); /* Actually do the reset */