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/mutex.h>
37 #include <dev/pci/pcivar.h>
38 #include <dev/pci/pcireg.h>
41 #include <machine/pci_cfgreg.h>
49 static int pciereg_cfgread(int bus, unsigned slot, unsigned func,
50 unsigned reg, unsigned bytes);
51 static void pciereg_cfgwrite(int bus, unsigned slot, unsigned func,
52 unsigned reg, int data, unsigned bytes);
53 static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
54 static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
57 static vm_offset_t pcie_base;
58 static int pcie_minbus, pcie_maxbus;
59 static struct mtx pcicfg_mtx;
62 * Initialise access to PCI configuration space
70 if (cfgmech != CFGMECH_NONE)
72 mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
76 * Grope around in the PCI config space to see if this is a
77 * chipset that is capable of doing memory-mapped config cycles.
78 * This also implies that it can do PCIe extended config cycles.
81 /* Check for supported chipsets */
82 vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
83 did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
89 /* Intel 7520 or 7320 */
90 pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
91 pcie_cfgregopen(pciebar, 0, 255);
96 /* Intel 915, 925, or 915GM */
97 pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
98 pcie_cfgregopen(pciebar, 0, 255);
107 * Read configuration space register
110 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
115 * Some BIOS writers seem to want to ignore the spec and put
116 * 0 in the intline rather than 255 to indicate none. Some use
117 * numbers in the range 128-254 to indicate something strange and
118 * apparently undocumented anywhere. Assume these are completely bogus
119 * and map them to 255, which the rest of the PCI code recognizes as
122 if (reg == PCIR_INTLINE && bytes == 1) {
123 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
124 if (line == 0 || line >= 128)
125 line = PCI_INVALID_IRQ;
128 return (pcireg_cfgread(bus, slot, func, reg, bytes));
132 * Write configuration space register
135 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
138 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
142 * Configuration space access using direct register operations
145 /* enable configuration space accesses and return data port address */
147 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
151 if (bus <= PCI_BUSMAX && slot < 32 && func <= PCI_FUNCMAX &&
152 reg <= PCI_REGMAX && bytes != 3 && (unsigned) bytes <= 4 &&
153 (reg & (bytes - 1)) == 0) {
154 outl(CONF1_ADDR_PORT, (1 << 31) | (bus << 16) | (slot << 11)
155 | (func << 8) | (reg & ~0x03));
156 dataport = CONF1_DATA_PORT + (reg & 0x03);
161 /* disable configuration space accesses */
167 * Do nothing. Writing a 0 to the address port can apparently
168 * confuse some bridges and cause spurious access failures.
173 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
178 if (cfgmech == CFGMECH_PCIE) {
179 data = pciereg_cfgread(bus, slot, func, reg, bytes);
183 mtx_lock_spin(&pcicfg_mtx);
184 port = pci_cfgenable(bus, slot, func, reg, bytes);
199 mtx_unlock_spin(&pcicfg_mtx);
204 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
208 if (cfgmech == CFGMECH_PCIE) {
209 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
213 mtx_lock_spin(&pcicfg_mtx);
214 port = pci_cfgenable(bus, slot, func, reg, bytes);
229 mtx_unlock_spin(&pcicfg_mtx);
233 pcie_cfgregopen(uint64_t base, uint8_t minbus, uint8_t maxbus)
240 printf("PCIe: Memory Mapped configuration base @ 0x%lx\n",
243 /* XXX: We should make sure this really fits into the direct map. */
244 pcie_base = (vm_offset_t)pmap_mapdev(base, (maxbus + 1) << 20);
245 pcie_minbus = minbus;
246 pcie_maxbus = maxbus;
247 cfgmech = CFGMECH_PCIE;
251 #define PCIE_VADDR(base, reg, bus, slot, func) \
253 ((((bus) & 0xff) << 20) | \
254 (((slot) & 0x1f) << 15) | \
255 (((func) & 0x7) << 12) | \
259 pciereg_cfgread(int bus, unsigned slot, unsigned func, unsigned reg,
262 volatile vm_offset_t va;
265 if (bus < pcie_minbus || bus > pcie_maxbus || slot >= 32 ||
266 func > PCI_FUNCMAX || reg >= 0x1000)
269 va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
273 data = *(volatile uint32_t *)(va);
276 data = *(volatile uint16_t *)(va);
279 data = *(volatile uint8_t *)(va);
287 pciereg_cfgwrite(int bus, unsigned slot, unsigned func, unsigned reg, int data,
290 volatile vm_offset_t va;
292 if (bus < pcie_minbus || bus > pcie_maxbus || slot >= 32 ||
293 func > PCI_FUNCMAX || reg >= 0x1000)
296 va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
300 *(volatile uint32_t *)(va) = data;
303 *(volatile uint16_t *)(va) = data;
306 *(volatile uint8_t *)(va) = data;