2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000, BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/mutex.h>
38 #include <dev/pci/pcivar.h>
39 #include <dev/pci/pcireg.h>
42 #include <machine/pci_cfgreg.h>
50 static uint32_t pci_docfgregread(int bus, int slot, int func, int reg,
52 static int pciereg_cfgread(int bus, unsigned slot, unsigned func,
53 unsigned reg, unsigned bytes);
54 static void pciereg_cfgwrite(int bus, unsigned slot, unsigned func,
55 unsigned reg, int data, unsigned bytes);
56 static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
57 static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
60 static vm_offset_t pcie_base;
61 static int pcie_minbus, pcie_maxbus;
62 static uint32_t pcie_badslots;
63 static struct mtx pcicfg_mtx;
64 static int mcfg_enable = 1;
65 TUNABLE_INT("hw.pci.mcfg", &mcfg_enable);
68 * Initialise access to PCI configuration space
78 mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
82 if (cfgmech != CFGMECH_NONE)
87 * Grope around in the PCI config space to see if this is a
88 * chipset that is capable of doing memory-mapped config cycles.
89 * This also implies that it can do PCIe extended config cycles.
92 /* Check for supported chipsets */
93 vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
94 did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
100 /* Intel 7520 or 7320 */
101 pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
102 pcie_cfgregopen(pciebar, 0, 255);
107 /* Intel 915, 925, or 915GM */
108 pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
109 pcie_cfgregopen(pciebar, 0, 255);
118 pci_docfgregread(int bus, int slot, int func, int reg, int bytes)
121 if (cfgmech == CFGMECH_PCIE &&
122 (bus != 0 || !(1 << slot & pcie_badslots)))
123 return (pciereg_cfgread(bus, slot, func, reg, bytes));
125 return (pcireg_cfgread(bus, slot, func, reg, bytes));
129 * Read configuration space register
132 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
137 * Some BIOS writers seem to want to ignore the spec and put
138 * 0 in the intline rather than 255 to indicate none. Some use
139 * numbers in the range 128-254 to indicate something strange and
140 * apparently undocumented anywhere. Assume these are completely bogus
141 * and map them to 255, which the rest of the PCI code recognizes as
144 if (reg == PCIR_INTLINE && bytes == 1) {
145 line = pci_docfgregread(bus, slot, func, PCIR_INTLINE, 1);
146 if (line == 0 || line >= 128)
147 line = PCI_INVALID_IRQ;
150 return (pci_docfgregread(bus, slot, func, reg, bytes));
154 * Write configuration space register
157 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
160 if (cfgmech == CFGMECH_PCIE &&
161 (bus != 0 || !(1 << slot & pcie_badslots)))
162 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
164 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
168 * Configuration space access using direct register operations
171 /* enable configuration space accesses and return data port address */
173 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
177 if (bus <= PCI_BUSMAX && slot < 32 && func <= PCI_FUNCMAX &&
178 reg <= PCI_REGMAX && bytes != 3 && (unsigned) bytes <= 4 &&
179 (reg & (bytes - 1)) == 0) {
180 outl(CONF1_ADDR_PORT, (1 << 31) | (bus << 16) | (slot << 11)
181 | (func << 8) | (reg & ~0x03));
182 dataport = CONF1_DATA_PORT + (reg & 0x03);
187 /* disable configuration space accesses */
193 * Do nothing. Writing a 0 to the address port can apparently
194 * confuse some bridges and cause spurious access failures.
199 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
204 mtx_lock_spin(&pcicfg_mtx);
205 port = pci_cfgenable(bus, slot, func, reg, bytes);
220 mtx_unlock_spin(&pcicfg_mtx);
225 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
229 mtx_lock_spin(&pcicfg_mtx);
230 port = pci_cfgenable(bus, slot, func, reg, bytes);
245 mtx_unlock_spin(&pcicfg_mtx);
249 pcie_cfgregopen(uint64_t base, uint8_t minbus, uint8_t maxbus)
261 printf("PCIe: Memory Mapped configuration base @ 0x%lx\n",
264 /* XXX: We should make sure this really fits into the direct map. */
265 pcie_base = (vm_offset_t)pmap_mapdev(base, (maxbus + 1) << 20);
266 pcie_minbus = minbus;
267 pcie_maxbus = maxbus;
268 cfgmech = CFGMECH_PCIE;
271 * On some AMD systems, some of the devices on bus 0 are
272 * inaccessible using memory-mapped PCI config access. Walk
273 * bus 0 looking for such devices. For these devices, we will
274 * fall back to using type 1 config access instead.
276 if (pci_cfgregopen() != 0) {
277 for (slot = 0; slot < 32; slot++) {
278 val1 = pcireg_cfgread(0, slot, 0, 0, 4);
279 if (val1 == 0xffffffff)
282 val2 = pciereg_cfgread(0, slot, 0, 0, 4);
284 pcie_badslots |= (1 << slot);
291 #define PCIE_VADDR(base, reg, bus, slot, func) \
293 ((((bus) & 0xff) << 20) | \
294 (((slot) & 0x1f) << 15) | \
295 (((func) & 0x7) << 12) | \
299 pciereg_cfgread(int bus, unsigned slot, unsigned func, unsigned reg,
302 volatile vm_offset_t va;
305 if (bus < pcie_minbus || bus > pcie_maxbus || slot >= 32 ||
306 func > PCI_FUNCMAX || reg >= 0x1000)
309 va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
313 data = *(volatile uint32_t *)(va);
316 data = *(volatile uint16_t *)(va);
319 data = *(volatile uint8_t *)(va);
327 pciereg_cfgwrite(int bus, unsigned slot, unsigned func, unsigned reg, int data,
330 volatile vm_offset_t va;
332 if (bus < pcie_minbus || bus > pcie_maxbus || slot >= 32 ||
333 func > PCI_FUNCMAX || reg >= 0x1000)
336 va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
340 *(volatile uint32_t *)(va) = data;
343 *(volatile uint16_t *)(va) = data;
346 *(volatile uint8_t *)(va) = data;