FreeBSD kernel amd64 PCI device code
pci_cfgreg.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
6 * Copyright (c) 2000, BSDi
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
14 * disclaimer.
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.
18 *
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.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/bus.h>
37#include <sys/lock.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>
43#include <vm/vm.h>
44#include <vm/pmap.h>
45#include <machine/pci_cfgreg.h>
46
47static uint32_t pci_docfgregread(int bus, int slot, int func, int reg,
48 int bytes);
49static int pciereg_cfgread(int bus, unsigned slot, unsigned func,
50 unsigned reg, unsigned bytes);
51static void pciereg_cfgwrite(int bus, unsigned slot, unsigned func,
52 unsigned reg, int data, unsigned bytes);
53static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
54static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
55
56SYSCTL_DECL(_hw_pci);
57
58/*
59 * For amd64 we assume that type 1 I/O port-based access always works.
60 * If an ACPI MCFG table exists, pcie_cfgregopen() will be called to
61 * switch to memory-mapped access.
62 */
63int cfgmech = CFGMECH_1;
64
65static vm_offset_t pcie_base;
67static uint32_t pcie_badslots;
68static struct mtx pcicfg_mtx;
69MTX_SYSINIT(pcicfg_mtx, &pcicfg_mtx, "pcicfg_mtx", MTX_SPIN);
70static int mcfg_enable = 1;
71SYSCTL_INT(_hw_pci, OID_AUTO, mcfg, CTLFLAG_RDTUN, &mcfg_enable, 0,
72 "Enable support for PCI-e memory mapped config access");
73
74int
76{
77
78 return (1);
79}
80
81static uint32_t
82pci_docfgregread(int bus, int slot, int func, int reg, int bytes)
83{
84
85 if (cfgmech == CFGMECH_PCIE &&
86 (bus >= pcie_minbus && bus <= pcie_maxbus) &&
87 (bus != 0 || !(1 << slot & pcie_badslots)))
88 return (pciereg_cfgread(bus, slot, func, reg, bytes));
89 else
90 return (pcireg_cfgread(bus, slot, func, reg, bytes));
91}
92
93/*
94 * Read configuration space register
95 */
96u_int32_t
97pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
98{
99 uint32_t line;
100
101 /*
102 * Some BIOS writers seem to want to ignore the spec and put
103 * 0 in the intline rather than 255 to indicate none. Some use
104 * numbers in the range 128-254 to indicate something strange and
105 * apparently undocumented anywhere. Assume these are completely bogus
106 * and map them to 255, which the rest of the PCI code recognizes as
107 * as an invalid IRQ.
108 */
109 if (reg == PCIR_INTLINE && bytes == 1) {
111 if (line == 0 || line >= 128)
112 line = PCI_INVALID_IRQ;
113 return (line);
114 }
115 return (pci_docfgregread(bus, slot, func, reg, bytes));
116}
117
118/*
119 * Write configuration space register
120 */
121void
122pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
123{
124
125 if (cfgmech == CFGMECH_PCIE &&
126 (bus >= pcie_minbus && bus <= pcie_maxbus) &&
127 (bus != 0 || !(1 << slot & pcie_badslots)))
128 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
129 else
130 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
131}
132
133/*
134 * Configuration space access using direct register operations
135 */
136
137/* enable configuration space accesses and return data port address */
138static int
139pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
140{
141 int dataport = 0;
142
143 if (bus <= PCI_BUSMAX && slot <= PCI_SLOTMAX && func <= PCI_FUNCMAX &&
144 (unsigned)reg <= PCI_REGMAX && bytes != 3 &&
145 (unsigned)bytes <= 4 && (reg & (bytes - 1)) == 0) {
146 outl(CONF1_ADDR_PORT, (1U << 31) | (bus << 16) | (slot << 11)
147 | (func << 8) | (reg & ~0x03));
148 dataport = CONF1_DATA_PORT + (reg & 0x03);
149 }
150 return (dataport);
151}
152
153/* disable configuration space accesses */
154static void
156{
157
158 /*
159 * Do nothing. Writing a 0 to the address port can apparently
160 * confuse some bridges and cause spurious access failures.
161 */
162}
163
164static int
165pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
166{
167 int data = -1;
168 int port;
169
170 mtx_lock_spin(&pcicfg_mtx);
171 port = pci_cfgenable(bus, slot, func, reg, bytes);
172 if (port != 0) {
173 switch (bytes) {
174 case 1:
175 data = inb(port);
176 break;
177 case 2:
178 data = inw(port);
179 break;
180 case 4:
181 data = inl(port);
182 break;
183 }
185 }
186 mtx_unlock_spin(&pcicfg_mtx);
187 return (data);
188}
189
190static void
191pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
192{
193 int port;
194
195 mtx_lock_spin(&pcicfg_mtx);
196 port = pci_cfgenable(bus, slot, func, reg, bytes);
197 if (port != 0) {
198 switch (bytes) {
199 case 1:
200 outb(port, data);
201 break;
202 case 2:
203 outw(port, data);
204 break;
205 case 4:
206 outl(port, data);
207 break;
208 }
210 }
211 mtx_unlock_spin(&pcicfg_mtx);
212}
213
214int
215pcie_cfgregopen(uint64_t base, uint8_t minbus, uint8_t maxbus)
216{
217 uint32_t val1, val2;
218 int slot;
219
220 if (!mcfg_enable)
221 return (0);
222
223 if (minbus != 0)
224 return (0);
225
226 if (bootverbose)
227 printf("PCIe: Memory Mapped configuration base @ 0x%lx\n",
228 base);
229
230 /* XXX: We should make sure this really fits into the direct map. */
231 pcie_base = (vm_offset_t)pmap_mapdev_pciecfg(base, (maxbus + 1) << 20);
232 pcie_minbus = minbus;
233 pcie_maxbus = maxbus;
234 cfgmech = CFGMECH_PCIE;
235
236 /*
237 * On some AMD systems, some of the devices on bus 0 are
238 * inaccessible using memory-mapped PCI config access. Walk
239 * bus 0 looking for such devices. For these devices, we will
240 * fall back to using type 1 config access instead.
241 */
242 if (pci_cfgregopen() != 0) {
243 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
244 val1 = pcireg_cfgread(0, slot, 0, 0, 4);
245 if (val1 == 0xffffffff)
246 continue;
247
248 val2 = pciereg_cfgread(0, slot, 0, 0, 4);
249 if (val2 != val1)
250 pcie_badslots |= (1 << slot);
251 }
252 }
253
254 return (1);
255}
256
257#define PCIE_VADDR(base, reg, bus, slot, func) \
258 ((base) + \
259 ((((bus) & 0xff) << 20) | \
260 (((slot) & 0x1f) << 15) | \
261 (((func) & 0x7) << 12) | \
262 ((reg) & 0xfff)))
263
264/*
265 * AMD BIOS And Kernel Developer's Guides for CPU families starting with 10h
266 * have a requirement that all accesses to the memory mapped PCI configuration
267 * space are done using AX class of registers.
268 * Since other vendors do not currently have any contradicting requirements
269 * the AMD access pattern is applied universally.
270 */
271
272static int
273pciereg_cfgread(int bus, unsigned slot, unsigned func, unsigned reg,
274 unsigned bytes)
275{
276 vm_offset_t va;
277 int data = -1;
278
279 if (bus < pcie_minbus || bus > pcie_maxbus || slot > PCI_SLOTMAX ||
281 return (-1);
282
284
285 switch (bytes) {
286 case 4:
287 __asm("movl %1, %0" : "=a" (data)
288 : "m" (*(volatile uint32_t *)va));
289 break;
290 case 2:
291 __asm("movzwl %1, %0" : "=a" (data)
292 : "m" (*(volatile uint16_t *)va));
293 break;
294 case 1:
295 __asm("movzbl %1, %0" : "=a" (data)
296 : "m" (*(volatile uint8_t *)va));
297 break;
298 }
299
300 return (data);
301}
302
303static void
304pciereg_cfgwrite(int bus, unsigned slot, unsigned func, unsigned reg, int data,
305 unsigned bytes)
306{
307 vm_offset_t va;
308
309 if (bus < pcie_minbus || bus > pcie_maxbus || slot > PCI_SLOTMAX ||
311 return;
312
314
315 switch (bytes) {
316 case 4:
317 __asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va)
318 : "a" (data));
319 break;
320 case 2:
321 __asm("movw %1, %0" : "=m" (*(volatile uint16_t *)va)
322 : "a" ((uint16_t)data));
323 break;
324 case 1:
325 __asm("movb %1, %0" : "=m" (*(volatile uint8_t *)va)
326 : "a" ((uint8_t)data));
327 break;
328 }
329}
u_int32_t pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
Definition: pci_cfgreg.c:97
static int pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
Definition: pci_cfgreg.c:139
static int pcie_maxbus
Definition: pci_cfgreg.c:66
MTX_SYSINIT(pcicfg_mtx, &pcicfg_mtx, "pcicfg_mtx", MTX_SPIN)
static int mcfg_enable
Definition: pci_cfgreg.c:70
static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
Definition: pci_cfgreg.c:165
static uint32_t pci_docfgregread(int bus, int slot, int func, int reg, int bytes)
Definition: pci_cfgreg.c:82
static int pciereg_cfgread(int bus, unsigned slot, unsigned func, unsigned reg, unsigned bytes)
Definition: pci_cfgreg.c:273
static struct mtx pcicfg_mtx
Definition: pci_cfgreg.c:68
static int pcie_minbus
Definition: pci_cfgreg.c:66
static uint32_t pcie_badslots
Definition: pci_cfgreg.c:67
#define PCIE_VADDR(base, reg, bus, slot, func)
Definition: pci_cfgreg.c:257
__FBSDID("$FreeBSD$")
SYSCTL_INT(_hw_pci, OID_AUTO, mcfg, CTLFLAG_RDTUN, &mcfg_enable, 0, "Enable support for PCI-e memory mapped config access")
void pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
Definition: pci_cfgreg.c:122
SYSCTL_DECL(_hw_pci)
int cfgmech
Definition: pci_cfgreg.c:63
static vm_offset_t pcie_base
Definition: pci_cfgreg.c:65
int pci_cfgregopen(void)
Definition: pci_cfgreg.c:75
static void pciereg_cfgwrite(int bus, unsigned slot, unsigned func, unsigned reg, int data, unsigned bytes)
Definition: pci_cfgreg.c:304
static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
Definition: pci_cfgreg.c:191
int pcie_cfgregopen(uint64_t base, uint8_t minbus, uint8_t maxbus)
Definition: pci_cfgreg.c:215
static void pci_cfgdisable(void)
Definition: pci_cfgreg.c:155
u_int reg
Definition: pci_dw_if.m:42
uint16_t data
Definition: pci_if.m:198
u_int func
Definition: pcib_if.m:81
u_int bus
Definition: pcib_if.m:79
u_int slot
Definition: pcib_if.m:80
#define PCIE_REGMAX
Definition: pcireg.h:49
#define PCIR_INTLINE
Definition: pcireg.h:251
#define PCI_BUSMAX
Definition: pcireg.h:45
#define PCI_REGMAX
Definition: pcireg.h:48
#define PCI_SLOTMAX
Definition: pcireg.h:46
#define PCI_FUNCMAX
Definition: pcireg.h:47