2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3 * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000, BSDi
5 * Copyright (c) 2004, Scott Long <scottl@freebsd.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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/mutex.h>
41 #include <sys/malloc.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/pci/pcireg.h>
46 #include <machine/pci_cfgreg.h>
47 #include <machine/pc/bios.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_kern.h>
52 #include <vm/vm_extern.h>
54 #include <machine/pmap.h>
57 #include <machine/xbox.h>
60 #define PRVERB(a) do { \
66 struct pcie_cfg_elem {
67 TAILQ_ENTRY(pcie_cfg_elem) elem;
81 static TAILQ_HEAD(pcie_cfg_list, pcie_cfg_elem) pcie_list[MAXCPU];
82 static uint64_t pcie_base;
83 static int pcie_minbus, pcie_maxbus;
84 static uint32_t pcie_badslots;
87 static struct mtx pcicfg_mtx;
88 static int mcfg_enable = 1;
89 SYSCTL_INT(_hw_pci, OID_AUTO, mcfg, CTLFLAG_RDTUN, &mcfg_enable, 0,
90 "Enable support for PCI-e memory mapped config access");
92 static uint32_t pci_docfgregread(int bus, int slot, int func, int reg,
94 static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
95 static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
97 static int pcireg_cfgopen(void);
99 static int pciereg_cfgread(int bus, unsigned slot, unsigned func,
100 unsigned reg, unsigned bytes);
101 static void pciereg_cfgwrite(int bus, unsigned slot, unsigned func,
102 unsigned reg, int data, unsigned bytes);
105 * Some BIOS writers seem to want to ignore the spec and put
106 * 0 in the intline rather than 255 to indicate none. Some use
107 * numbers in the range 128-254 to indicate something strange and
108 * apparently undocumented anywhere. Assume these are completely bogus
109 * and map them to 255, which means "none".
112 pci_i386_map_intline(int line)
114 if (line == 0 || line >= 128)
115 return (PCI_INVALID_IRQ);
121 pcibios_get_version(void)
123 struct bios_regs args;
125 if (PCIbios.ventry == 0) {
126 PRVERB(("pcibios: No call entry point\n"));
129 args.eax = PCIBIOS_BIOS_PRESENT;
130 if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) {
131 PRVERB(("pcibios: BIOS_PRESENT call failed\n"));
134 if (args.edx != 0x20494350) {
135 PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n"));
138 return (args.ebx & 0xffff);
143 * Initialise access to PCI configuration space
151 static int opened = 0;
159 if (cfgmech == CFGMECH_NONE && pcireg_cfgopen() == 0)
162 v = pcibios_get_version();
164 PRVERB(("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8,
166 mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
169 /* $PIR requires PCI BIOS 2.10 or greater. */
173 if (cfgmech == CFGMECH_PCIE)
177 * Grope around in the PCI config space to see if this is a
178 * chipset that is capable of doing memory-mapped config cycles.
179 * This also implies that it can do PCIe extended config cycles.
182 /* Check for supported chipsets */
183 vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
184 did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
190 /* Intel 7520 or 7320 */
191 pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
192 pcie_cfgregopen(pciebar, 0, 255);
197 /* Intel 915, 925, or 915GM */
198 pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
199 pcie_cfgregopen(pciebar, 0, 255);
209 pci_docfgregread(int bus, int slot, int func, int reg, int bytes)
212 if (cfgmech == CFGMECH_PCIE &&
213 (bus >= pcie_minbus && bus <= pcie_maxbus) &&
214 (bus != 0 || !(1 << slot & pcie_badslots)))
215 return (pciereg_cfgread(bus, slot, func, reg, bytes));
217 return (pcireg_cfgread(bus, slot, func, reg, bytes));
221 * Read configuration space register
224 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
229 * Some BIOS writers seem to want to ignore the spec and put
230 * 0 in the intline rather than 255 to indicate none. The rest of
231 * the code uses 255 as an invalid IRQ.
233 if (reg == PCIR_INTLINE && bytes == 1) {
234 line = pci_docfgregread(bus, slot, func, PCIR_INTLINE, 1);
235 return (pci_i386_map_intline(line));
237 return (pci_docfgregread(bus, slot, func, reg, bytes));
241 * Write configuration space register
244 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
247 if (cfgmech == CFGMECH_PCIE &&
248 (bus >= pcie_minbus && bus <= pcie_maxbus) &&
249 (bus != 0 || !(1 << slot & pcie_badslots)))
250 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
252 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
256 * Configuration space access using direct register operations
259 /* enable configuration space accesses and return data port address */
261 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
266 if (arch_i386_is_xbox) {
268 * The Xbox MCPX chipset is a derivative of the nForce 1
269 * chipset. It almost has the same bus layout; some devices
270 * cannot be used, because they have been removed.
274 * Devices 00:00.1 and 00:00.2 used to be memory controllers on
275 * the nForce chipset, but on the Xbox, using them will lockup
278 if (bus == 0 && slot == 0 && (func == 1 || func == 2))
282 * Bus 1 only contains a VGA controller at 01:00.0. When you try
283 * to probe beyond that device, you only get garbage, which
284 * could cause lockups.
286 if (bus == 1 && (slot != 0 || func != 0))
290 * Bus 2 used to contain the AGP controller, but the Xbox MCPX
291 * doesn't have one. Probing it can cause lockups.
298 if (bus <= PCI_BUSMAX
300 && func <= PCI_FUNCMAX
301 && (unsigned)reg <= PCI_REGMAX
303 && (unsigned)bytes <= 4
304 && (reg & (bytes - 1)) == 0) {
308 outl(CONF1_ADDR_PORT, (1U << 31)
309 | (bus << 16) | (slot << 11)
310 | (func << 8) | (reg & ~0x03));
311 dataport = CONF1_DATA_PORT + (reg & 0x03);
314 outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1));
315 outb(CONF2_FORWARD_PORT, bus);
316 dataport = 0xc000 | (slot << 8) | reg;
323 /* disable configuration space accesses */
331 * Do nothing for the config mechanism 1 case.
332 * Writing a 0 to the address port can apparently
333 * confuse some bridges and cause spurious
338 outb(CONF2_ENABLE_PORT, 0);
344 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
349 mtx_lock_spin(&pcicfg_mtx);
350 port = pci_cfgenable(bus, slot, func, reg, bytes);
365 mtx_unlock_spin(&pcicfg_mtx);
370 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
374 mtx_lock_spin(&pcicfg_mtx);
375 port = pci_cfgenable(bus, slot, func, reg, bytes);
390 mtx_unlock_spin(&pcicfg_mtx);
394 /* check whether the configuration mechanism has been correctly identified */
396 pci_cfgcheck(int maxdev)
404 printf("pci_cfgcheck:\tdevice ");
406 for (device = 0; device < maxdev; device++) {
408 printf("%d ", device);
410 port = pci_cfgenable(0, device, 0, 0, 4);
412 if (id == 0 || id == 0xffffffff)
415 port = pci_cfgenable(0, device, 0, 8, 4);
416 class = inl(port) >> 8;
418 printf("[class=%06x] ", class);
419 if (class == 0 || (class & 0xf870ff) != 0)
422 port = pci_cfgenable(0, device, 0, 14, 1);
425 printf("[hdr=%02x] ", header);
426 if ((header & 0x7e) != 0)
430 printf("is there (id=%08x)\n", id);
436 printf("-- nothing found\n");
445 uint32_t mode1res, oldval1;
446 uint8_t mode2res, oldval2;
448 /* Check for type #1 first. */
449 oldval1 = inl(CONF1_ADDR_PORT);
452 printf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08x\n",
459 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK);
461 mode1res = inl(CONF1_ADDR_PORT);
462 outl(CONF1_ADDR_PORT, oldval1);
465 printf("pci_open(1a):\tmode1res=0x%08x (0x%08lx)\n", mode1res,
469 if (pci_cfgcheck(32))
473 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1);
474 mode1res = inl(CONF1_ADDR_PORT);
475 outl(CONF1_ADDR_PORT, oldval1);
478 printf("pci_open(1b):\tmode1res=0x%08x (0x%08lx)\n", mode1res,
481 if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) {
482 if (pci_cfgcheck(32))
486 /* Type #1 didn't work, so try type #2. */
487 oldval2 = inb(CONF2_ENABLE_PORT);
490 printf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n",
494 if ((oldval2 & 0xf0) == 0) {
499 outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK);
500 mode2res = inb(CONF2_ENABLE_PORT);
501 outb(CONF2_ENABLE_PORT, oldval2);
504 printf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n",
505 mode2res, CONF2_ENABLE_CHK);
507 if (mode2res == CONF2_ENABLE_RES) {
509 printf("pci_open(2a):\tnow trying mechanism 2\n");
511 if (pci_cfgcheck(16))
516 /* Nothing worked, so punt. */
517 cfgmech = CFGMECH_NONE;
523 pcie_cfgregopen(uint64_t base, uint8_t minbus, uint8_t maxbus)
525 struct pcie_cfg_list *pcielist;
526 struct pcie_cfg_elem *pcie_array, *elem;
541 if (base >= 0x100000000) {
544 "PCI: Memory Mapped PCI configuration area base 0x%jx too high\n",
551 printf("PCIe: Memory Mapped configuration base @ 0x%jx\n",
555 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)
559 pcie_array = malloc(sizeof(struct pcie_cfg_elem) * PCIE_CACHE,
561 if (pcie_array == NULL)
564 va = kva_alloc(PCIE_CACHE * PAGE_SIZE);
566 free(pcie_array, M_DEVBUF);
571 pcielist = &pcie_list[pc->pc_cpuid];
573 pcielist = &pcie_list[0];
575 TAILQ_INIT(pcielist);
576 for (i = 0; i < PCIE_CACHE; i++) {
577 elem = &pcie_array[i];
578 elem->vapage = va + (i * PAGE_SIZE);
580 TAILQ_INSERT_HEAD(pcielist, elem, elem);
585 pcie_minbus = minbus;
586 pcie_maxbus = maxbus;
587 cfgmech = CFGMECH_PCIE;
591 * On some AMD systems, some of the devices on bus 0 are
592 * inaccessible using memory-mapped PCI config access. Walk
593 * bus 0 looking for such devices. For these devices, we will
594 * fall back to using type 1 config access instead.
596 if (pci_cfgregopen() != 0) {
597 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
598 val1 = pcireg_cfgread(0, slot, 0, 0, 4);
599 if (val1 == 0xffffffff)
602 val2 = pciereg_cfgread(0, slot, 0, 0, 4);
604 pcie_badslots |= (1 << slot);
612 #define PCIE_PADDR(base, reg, bus, slot, func) \
614 ((((bus) & 0xff) << 20) | \
615 (((slot) & 0x1f) << 15) | \
616 (((func) & 0x7) << 12) | \
619 static __inline vm_offset_t
620 pciereg_findaddr(int bus, unsigned slot, unsigned func, unsigned reg)
622 struct pcie_cfg_list *pcielist;
623 struct pcie_cfg_elem *elem;
624 vm_paddr_t pa, papage;
626 pa = PCIE_PADDR(pcie_base, reg, bus, slot, func);
627 papage = pa & ~PAGE_MASK;
630 * Find an element in the cache that matches the physical page desired,
631 * or create a new mapping from the least recently used element.
632 * A very simple LRU algorithm is used here, does it need to be more
635 pcielist = &pcie_list[PCPU_GET(cpuid)];
636 TAILQ_FOREACH(elem, pcielist, elem) {
637 if (elem->papage == papage)
642 elem = TAILQ_LAST(pcielist, pcie_cfg_list);
643 if (elem->papage != 0) {
644 pmap_kremove(elem->vapage);
645 invlpg(elem->vapage);
647 pmap_kenter(elem->vapage, papage);
648 elem->papage = papage;
651 if (elem != TAILQ_FIRST(pcielist)) {
652 TAILQ_REMOVE(pcielist, elem, elem);
653 TAILQ_INSERT_HEAD(pcielist, elem, elem);
655 return (elem->vapage | (pa & PAGE_MASK));
659 * AMD BIOS And Kernel Developer's Guides for CPU families starting with 10h
660 * have a requirement that all accesses to the memory mapped PCI configuration
661 * space are done using AX class of registers.
662 * Since other vendors do not currently have any contradicting requirements
663 * the AMD access pattern is applied universally.
667 pciereg_cfgread(int bus, unsigned slot, unsigned func, unsigned reg,
673 if (bus < pcie_minbus || bus > pcie_maxbus || slot > PCI_SLOTMAX ||
674 func > PCI_FUNCMAX || reg > PCIE_REGMAX)
678 va = pciereg_findaddr(bus, slot, func, reg);
682 __asm("movl %1, %0" : "=a" (data)
683 : "m" (*(volatile uint32_t *)va));
686 __asm("movzwl %1, %0" : "=a" (data)
687 : "m" (*(volatile uint16_t *)va));
690 __asm("movzbl %1, %0" : "=a" (data)
691 : "m" (*(volatile uint8_t *)va));
700 pciereg_cfgwrite(int bus, unsigned slot, unsigned func, unsigned reg, int data,
705 if (bus < pcie_minbus || bus > pcie_maxbus || slot > PCI_SLOTMAX ||
706 func > PCI_FUNCMAX || reg > PCIE_REGMAX)
710 va = pciereg_findaddr(bus, slot, func, reg);
714 __asm("movl %1, %0" : "=m" (*(volatile uint32_t *)va)
718 __asm("movw %1, %0" : "=m" (*(volatile uint16_t *)va)
719 : "a" ((uint16_t)data));
722 __asm("movb %1, %0" : "=m" (*(volatile uint8_t *)va)
723 : "a" ((uint8_t)data));