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 <sys/sysctl.h>
39 #include <dev/pci/pcivar.h>
40 #include <dev/pci/pcireg.h>
43 #include <machine/pci_cfgreg.h>
51 static uint32_t pci_docfgregread(int bus, int slot, int func, int reg,
53 static int pciereg_cfgread(int bus, unsigned slot, unsigned func,
54 unsigned reg, unsigned bytes);
55 static void pciereg_cfgwrite(int bus, unsigned slot, unsigned func,
56 unsigned reg, int data, unsigned bytes);
57 static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
58 static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
63 static vm_offset_t pcie_base;
64 static int pcie_minbus, pcie_maxbus;
65 static uint32_t pcie_badslots;
66 static struct mtx pcicfg_mtx;
67 static int mcfg_enable = 1;
68 SYSCTL_INT(_hw_pci, OID_AUTO, mcfg, CTLFLAG_RDTUN, &mcfg_enable, 0,
69 "Enable support for PCI-e memory mapped config access");
72 * Initialise access to PCI configuration space
82 mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
86 if (cfgmech != CFGMECH_NONE)
91 * Grope around in the PCI config space to see if this is a
92 * chipset that is capable of doing memory-mapped config cycles.
93 * This also implies that it can do PCIe extended config cycles.
96 /* Check for supported chipsets */
97 vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
98 did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
104 /* Intel 7520 or 7320 */
105 pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
106 pcie_cfgregopen(pciebar, 0, 255);
111 /* Intel 915, 925, or 915GM */
112 pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
113 pcie_cfgregopen(pciebar, 0, 255);
122 pci_docfgregread(int bus, int slot, int func, int reg, int bytes)
125 if (cfgmech == CFGMECH_PCIE &&
126 (bus >= pcie_minbus && bus <= pcie_maxbus) &&
127 (bus != 0 || !(1 << slot & pcie_badslots)))
128 return (pciereg_cfgread(bus, slot, func, reg, bytes));
130 return (pcireg_cfgread(bus, slot, func, reg, bytes));
134 * Read configuration space register
137 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
142 * Some BIOS writers seem to want to ignore the spec and put
143 * 0 in the intline rather than 255 to indicate none. Some use
144 * numbers in the range 128-254 to indicate something strange and
145 * apparently undocumented anywhere. Assume these are completely bogus
146 * and map them to 255, which the rest of the PCI code recognizes as
149 if (reg == PCIR_INTLINE && bytes == 1) {
150 line = pci_docfgregread(bus, slot, func, PCIR_INTLINE, 1);
151 if (line == 0 || line >= 128)
152 line = PCI_INVALID_IRQ;
155 return (pci_docfgregread(bus, slot, func, reg, bytes));
159 * Write configuration space register
162 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
165 if (cfgmech == CFGMECH_PCIE &&
166 (bus >= pcie_minbus && bus <= pcie_maxbus) &&
167 (bus != 0 || !(1 << slot & pcie_badslots)))
168 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
170 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
174 * Configuration space access using direct register operations
177 /* enable configuration space accesses and return data port address */
179 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
183 if (bus <= PCI_BUSMAX && slot <= PCI_SLOTMAX && func <= PCI_FUNCMAX &&
184 (unsigned)reg <= PCI_REGMAX && bytes != 3 &&
185 (unsigned)bytes <= 4 && (reg & (bytes - 1)) == 0) {
186 outl(CONF1_ADDR_PORT, (1U << 31) | (bus << 16) | (slot << 11)
187 | (func << 8) | (reg & ~0x03));
188 dataport = CONF1_DATA_PORT + (reg & 0x03);
193 /* disable configuration space accesses */
199 * Do nothing. Writing a 0 to the address port can apparently
200 * confuse some bridges and cause spurious access failures.
205 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
210 mtx_lock_spin(&pcicfg_mtx);
211 port = pci_cfgenable(bus, slot, func, reg, bytes);
226 mtx_unlock_spin(&pcicfg_mtx);
231 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
235 mtx_lock_spin(&pcicfg_mtx);
236 port = pci_cfgenable(bus, slot, func, reg, bytes);
251 mtx_unlock_spin(&pcicfg_mtx);
255 pcie_cfgregopen(uint64_t base, uint8_t minbus, uint8_t maxbus)
267 printf("PCIe: Memory Mapped configuration base @ 0x%lx\n",
270 /* XXX: We should make sure this really fits into the direct map. */
271 pcie_base = (vm_offset_t)pmap_mapdev(base, (maxbus + 1) << 20);
272 pcie_minbus = minbus;
273 pcie_maxbus = maxbus;
274 cfgmech = CFGMECH_PCIE;
277 * On some AMD systems, some of the devices on bus 0 are
278 * inaccessible using memory-mapped PCI config access. Walk
279 * bus 0 looking for such devices. For these devices, we will
280 * fall back to using type 1 config access instead.
282 if (pci_cfgregopen() != 0) {
283 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
284 val1 = pcireg_cfgread(0, slot, 0, 0, 4);
285 if (val1 == 0xffffffff)
288 val2 = pciereg_cfgread(0, slot, 0, 0, 4);
290 pcie_badslots |= (1 << slot);
297 #define PCIE_VADDR(base, reg, bus, slot, func) \
299 ((((bus) & 0xff) << 20) | \
300 (((slot) & 0x1f) << 15) | \
301 (((func) & 0x7) << 12) | \
305 * AMD BIOS And Kernel Developer's Guides for CPU families starting with 10h
306 * have a requirement that all accesses to the memory mapped PCI configuration
307 * space are done using AX class of registers.
308 * Since other vendors do not currently have any contradicting requirements
309 * the AMD access pattern is applied universally.
313 pciereg_cfgread(int bus, unsigned slot, unsigned func, unsigned reg,
319 if (bus < pcie_minbus || bus > pcie_maxbus || slot > PCI_SLOTMAX ||
320 func > PCI_FUNCMAX || reg > PCIE_REGMAX)
323 va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
327 __asm("movl %1, %0" : "=a" (data)
328 : "m" (*(volatile uint32_t *)va));
331 __asm("movzwl %1, %0" : "=a" (data)
332 : "m" (*(volatile uint16_t *)va));
335 __asm("movzbl %1, %0" : "=a" (data)
336 : "m" (*(volatile uint8_t *)va));
344 pciereg_cfgwrite(int bus, unsigned slot, unsigned func, unsigned reg, int data,
349 if (bus < pcie_minbus || bus > pcie_maxbus || slot > PCI_SLOTMAX ||
350 func > PCI_FUNCMAX || reg > PCIE_REGMAX)
353 va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
357 __asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va)
361 __asm("movw %1, %0" : "=m" (*(volatile uint16_t *)va)
362 : "a" ((uint16_t)data));
365 __asm("movb %1, %0" : "=m" (*(volatile uint8_t *)va)
366 : "a" ((uint8_t)data));