2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
6 * Copyright (c) 2000, BSDi
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcireg.h>
45 #include <machine/pci_cfgreg.h>
53 static uint32_t pci_docfgregread(int bus, int slot, int func, int reg,
55 static int pciereg_cfgread(int bus, unsigned slot, unsigned func,
56 unsigned reg, unsigned bytes);
57 static void pciereg_cfgwrite(int bus, unsigned slot, unsigned func,
58 unsigned reg, int data, unsigned bytes);
59 static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
60 static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
65 static vm_offset_t pcie_base;
66 static int pcie_minbus, pcie_maxbus;
67 static uint32_t pcie_badslots;
68 static struct mtx pcicfg_mtx;
69 MTX_SYSINIT(pcicfg_mtx, &pcicfg_mtx, "pcicfg_mtx", MTX_SPIN);
70 static int mcfg_enable = 1;
71 SYSCTL_INT(_hw_pci, OID_AUTO, mcfg, CTLFLAG_RDTUN, &mcfg_enable, 0,
72 "Enable support for PCI-e memory mapped config access");
75 * Initialise access to PCI configuration space
83 if (cfgmech != CFGMECH_NONE)
88 * Grope around in the PCI config space to see if this is a
89 * chipset that is capable of doing memory-mapped config cycles.
90 * This also implies that it can do PCIe extended config cycles.
93 /* Check for supported chipsets */
94 vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
95 did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
101 /* Intel 7520 or 7320 */
102 pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
103 pcie_cfgregopen(pciebar, 0, 255);
108 /* Intel 915, 925, or 915GM */
109 pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
110 pcie_cfgregopen(pciebar, 0, 255);
119 pci_docfgregread(int bus, int slot, int func, int reg, int bytes)
122 if (cfgmech == CFGMECH_PCIE &&
123 (bus >= pcie_minbus && bus <= pcie_maxbus) &&
124 (bus != 0 || !(1 << slot & pcie_badslots)))
125 return (pciereg_cfgread(bus, slot, func, reg, bytes));
127 return (pcireg_cfgread(bus, slot, func, reg, bytes));
131 * Read configuration space register
134 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
138 if (cfgmech == CFGMECH_NONE)
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_NONE)
168 if (cfgmech == CFGMECH_PCIE &&
169 (bus >= pcie_minbus && bus <= pcie_maxbus) &&
170 (bus != 0 || !(1 << slot & pcie_badslots)))
171 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
173 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
177 * Configuration space access using direct register operations
180 /* enable configuration space accesses and return data port address */
182 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
186 if (bus <= PCI_BUSMAX && slot <= PCI_SLOTMAX && func <= PCI_FUNCMAX &&
187 (unsigned)reg <= PCI_REGMAX && bytes != 3 &&
188 (unsigned)bytes <= 4 && (reg & (bytes - 1)) == 0) {
189 outl(CONF1_ADDR_PORT, (1U << 31) | (bus << 16) | (slot << 11)
190 | (func << 8) | (reg & ~0x03));
191 dataport = CONF1_DATA_PORT + (reg & 0x03);
196 /* disable configuration space accesses */
202 * Do nothing. Writing a 0 to the address port can apparently
203 * confuse some bridges and cause spurious access failures.
208 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
213 mtx_lock_spin(&pcicfg_mtx);
214 port = pci_cfgenable(bus, slot, func, reg, bytes);
229 mtx_unlock_spin(&pcicfg_mtx);
234 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
238 mtx_lock_spin(&pcicfg_mtx);
239 port = pci_cfgenable(bus, slot, func, reg, bytes);
254 mtx_unlock_spin(&pcicfg_mtx);
258 pcie_cfgregopen(uint64_t base, uint8_t minbus, uint8_t maxbus)
270 printf("PCIe: Memory Mapped configuration base @ 0x%lx\n",
273 /* XXX: We should make sure this really fits into the direct map. */
274 pcie_base = (vm_offset_t)pmap_mapdev(base, (maxbus + 1) << 20);
275 pcie_minbus = minbus;
276 pcie_maxbus = maxbus;
277 cfgmech = CFGMECH_PCIE;
280 * On some AMD systems, some of the devices on bus 0 are
281 * inaccessible using memory-mapped PCI config access. Walk
282 * bus 0 looking for such devices. For these devices, we will
283 * fall back to using type 1 config access instead.
285 if (pci_cfgregopen() != 0) {
286 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
287 val1 = pcireg_cfgread(0, slot, 0, 0, 4);
288 if (val1 == 0xffffffff)
291 val2 = pciereg_cfgread(0, slot, 0, 0, 4);
293 pcie_badslots |= (1 << slot);
300 #define PCIE_VADDR(base, reg, bus, slot, func) \
302 ((((bus) & 0xff) << 20) | \
303 (((slot) & 0x1f) << 15) | \
304 (((func) & 0x7) << 12) | \
308 * AMD BIOS And Kernel Developer's Guides for CPU families starting with 10h
309 * have a requirement that all accesses to the memory mapped PCI configuration
310 * space are done using AX class of registers.
311 * Since other vendors do not currently have any contradicting requirements
312 * the AMD access pattern is applied universally.
316 pciereg_cfgread(int bus, unsigned slot, unsigned func, unsigned reg,
322 if (bus < pcie_minbus || bus > pcie_maxbus || slot > PCI_SLOTMAX ||
323 func > PCI_FUNCMAX || reg > PCIE_REGMAX)
326 va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
330 __asm("movl %1, %0" : "=a" (data)
331 : "m" (*(volatile uint32_t *)va));
334 __asm("movzwl %1, %0" : "=a" (data)
335 : "m" (*(volatile uint16_t *)va));
338 __asm("movzbl %1, %0" : "=a" (data)
339 : "m" (*(volatile uint8_t *)va));
347 pciereg_cfgwrite(int bus, unsigned slot, unsigned func, unsigned reg, int data,
352 if (bus < pcie_minbus || bus > pcie_maxbus || slot > PCI_SLOTMAX ||
353 func > PCI_FUNCMAX || reg > PCIE_REGMAX)
356 va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
360 __asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va)
364 __asm("movw %1, %0" : "=m" (*(volatile uint16_t *)va)
365 : "a" ((uint16_t)data));
368 __asm("movb %1, %0" : "=m" (*(volatile uint8_t *)va)
369 : "a" ((uint8_t)data));