2 * Copyright (c) 1997, Stefan Esser <se@kfreebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@kfreebsd.org>
4 * Copyright (c) 2000, BSDi
5 * Copyright (c) 2004, Scott Long <scottl@kfreebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $FreeBSD: src/sys/i386/pci/pci_cfgreg.c,v 1.124.2.3 2009/05/04 21:04:29 jhb
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/thread2.h>
39 #include <sys/spinlock.h>
40 #include <sys/spinlock2.h>
41 #include <sys/queue.h>
42 #include <bus/pci/pcivar.h>
43 #include <bus/pci/pcireg.h>
44 #include "pci_cfgreg.h"
45 #include <machine/pc/bios.h>
48 #include <vm/vm_param.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_extern.h>
52 #include <machine/pmap.h>
60 static vm_offset_t pcie_base;
61 static int pcie_minbus, pcie_maxbus;
62 static uint32_t pcie_badslots;
64 static struct spinlock pcicfg_spin;
66 static int mcfg_enable = 1;
67 TUNABLE_INT("hw.pci.mcfg", &mcfg_enable);
69 static uint32_t pci_docfgregread(int bus, int slot, int func, int reg, int bytes);
71 static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
72 static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data,
75 static int pciereg_cfgread(int bus, unsigned slot, unsigned func,
76 unsigned reg, unsigned bytes);
77 static void pciereg_cfgwrite(int bus, unsigned slot, unsigned func,
78 unsigned reg, int data, unsigned bytes);
81 * Initialise access to PCI configuration space
86 static int inited = 0;
92 spin_init(&pcicfg_spin, "pcicfg");
95 if (cfgmech != CFGMECH_NONE)
100 * Grope around in the PCI config space to see if this is a
101 * chipset that is capable of doing memory-mapped config cycles.
102 * This also implies that it can do PCIe extended config cycles.
105 /* Check for supported chipsets */
106 vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
107 did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
113 /* Intel 7520 or 7320 */
114 pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
115 pcie_cfgregopen(pciebar, 0, 255);
120 /* Intel 915, 925, or 915GM */
121 pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
122 pcie_cfgregopen(pciebar, 0, 255);
130 pci_docfgregread(int bus, int slot, int func, int reg, int bytes)
132 if (cfgmech == CFGMECH_PCIE &&
133 (bus >= pcie_minbus && bus <= pcie_maxbus) &&
134 (bus != 0 || !(1 << slot & pcie_badslots)))
135 return pciereg_cfgread(bus, slot, func, reg, bytes);
137 return pcireg_cfgread(bus, slot, func, reg, bytes);
141 * Read configuration space register
144 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
147 * Some BIOS writers seem to want to ignore the spec and put
148 * 0 in the intline rather than 255 to indicate none. Some use
149 * numbers in the range 128-254 to indicate something strange and
150 * apparently undocumented anywhere. Assume these are completely
151 * bogus and map them to 255, which means "none".
153 if (reg == PCIR_INTLINE && bytes == 1) {
156 line = pci_docfgregread(bus, slot, func, PCIR_INTLINE, 1);
157 if (line == 0 || line >= 128)
158 return (PCI_INVALID_IRQ);
161 return pci_docfgregread(bus, slot, func, reg, bytes);
165 * Write configuration space register
168 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
170 if (cfgmech == CFGMECH_PCIE &&
171 (bus >= pcie_minbus && bus <= pcie_maxbus) &&
172 (bus != 0 || !(1 << slot & pcie_badslots)))
173 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
175 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
179 * Configuration space access using direct register operations
182 /* enable configuration space accesses and return data port address */
184 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
188 if (bus <= PCI_BUSMAX &&
189 slot <= PCI_SLOTMAX &&
190 func <= PCI_FUNCMAX &&
191 (unsigned)reg <= PCI_REGMAX &&
193 (unsigned)bytes <= 4 &&
194 (reg & (bytes - 1)) == 0) {
195 outl(CONF1_ADDR_PORT, (1 << 31) | (bus << 16) | (slot << 11) |
196 (func << 8) | (reg & ~0x03));
197 dataport = CONF1_DATA_PORT + (reg & 0x03);
202 /* disable configuration space accesses */
207 * Do nothing. Writing a 0 to the address port can apparently
208 * confuse some bridges and cause spurious access failures.
213 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
218 spin_lock(&pcicfg_spin);
219 port = pci_cfgenable(bus, slot, func, reg, bytes);
234 spin_unlock(&pcicfg_spin);
239 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
243 spin_lock(&pcicfg_spin);
244 port = pci_cfgenable(bus, slot, func, reg, bytes);
259 spin_unlock(&pcicfg_spin);
263 pcie_cfgregopen(uint64_t base, uint8_t minbus, uint8_t maxbus)
266 kprintf("PCIe: Memory Mapped configuration base @ 0x%jx, "
267 "bus [%d, %d]\n", (uintmax_t)base, minbus, maxbus);
277 kprintf("PCIe: Using Memory Mapped configuration\n");
279 pcie_base = (vm_offset_t)pmap_mapdev_uncacheable(base,
280 ((unsigned)maxbus + 1) << 20);
281 pcie_minbus = minbus;
282 pcie_maxbus = maxbus;
283 cfgmech = CFGMECH_PCIE;
286 * On some AMD systems, some of the devices on bus 0 are
287 * inaccessible using memory-mapped PCI config access. Walk
288 * bus 0 looking for such devices. For these devices, we will
289 * fall back to using type 1 config access instead.
291 if (pci_cfgregopen() != 0) {
294 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
297 val1 = pcireg_cfgread(0, slot, 0, 0, 4);
298 if (val1 == 0xffffffff)
301 val2 = pciereg_cfgread(0, slot, 0, 0, 4);
303 pcie_badslots |= (1 << slot);
309 #define PCIE_VADDR(base, reg, bus, slot, func) \
311 ((((bus) & 0xff) << 20) | \
312 (((slot) & 0x1f) << 15) | \
313 (((func) & 0x7) << 12) | \
317 pciereg_cfgread(int bus, unsigned slot, unsigned func, unsigned reg,
320 volatile vm_offset_t va;
323 if (bus < pcie_minbus || bus > pcie_maxbus || slot > PCI_SLOTMAX ||
324 func > PCI_FUNCMAX || reg > PCIE_REGMAX)
327 va = PCIE_VADDR(pcie_base, reg, bus, slot, func);
331 data = *(volatile uint32_t *)(va);
334 data = *(volatile uint16_t *)(va);
337 data = *(volatile uint8_t *)(va);
344 pciereg_cfgwrite(int bus, unsigned slot, unsigned func, unsigned reg, int data,
347 volatile vm_offset_t va;
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 *(volatile uint32_t *)(va) = data;
360 *(volatile uint16_t *)(va) = data;
363 *(volatile uint8_t *)(va) = data;