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/mutex.h>
40 #include <sys/malloc.h>
41 #include <sys/queue.h>
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
44 #include <machine/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>
55 #include <machine/xbox.h>
58 #define PRVERB(a) do { \
64 struct pcie_cfg_elem {
65 TAILQ_ENTRY(pcie_cfg_elem) elem;
77 static TAILQ_HEAD(pcie_cfg_list, pcie_cfg_elem) pcie_list[MAXCPU];
78 static uint32_t pciebar;
81 static struct mtx pcicfg_mtx;
83 static int pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
84 static void pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
85 static int pcireg_cfgopen(void);
87 static int pciereg_cfgopen(void);
88 static int pciereg_cfgread(int bus, int slot, int func, int reg,
90 static void pciereg_cfgwrite(int bus, int slot, int func, int reg,
94 * Some BIOS writers seem to want to ignore the spec and put
95 * 0 in the intline rather than 255 to indicate none. Some use
96 * numbers in the range 128-254 to indicate something strange and
97 * apparently undocumented anywhere. Assume these are completely bogus
98 * and map them to 255, which means "none".
101 pci_i386_map_intline(int line)
103 if (line == 0 || line >= 128)
104 return (PCI_INVALID_IRQ);
109 pcibios_get_version(void)
111 struct bios_regs args;
113 if (PCIbios.ventry == 0) {
114 PRVERB(("pcibios: No call entry point\n"));
117 args.eax = PCIBIOS_BIOS_PRESENT;
118 if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) {
119 PRVERB(("pcibios: BIOS_PRESENT call failed\n"));
122 if (args.edx != 0x20494350) {
123 PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n"));
126 return (args.ebx & 0xffff);
130 * Initialise access to PCI configuration space
135 static int opened = 0;
142 if (pcireg_cfgopen() == 0)
145 v = pcibios_get_version();
147 PRVERB(("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8,
149 mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
152 /* $PIR requires PCI BIOS 2.10 or greater. */
157 * Grope around in the PCI config space to see if this is a
158 * chipset that is capable of doing memory-mapped config cycles.
159 * This also implies that it can do PCIe extended config cycles.
162 /* Check for supported chipsets */
163 vid = pci_cfgregread(0, 0, 0, PCIR_VENDOR, 2);
164 did = pci_cfgregread(0, 0, 0, PCIR_DEVICE, 2);
166 if (did == 0x3590 || did == 0x3592) {
167 /* Intel 7520 or 7320 */
168 pciebar = pci_cfgregread(0, 0, 0, 0xce, 2) << 16;
170 } else if (did == 0x2580 || did == 0x2584) {
171 /* Intel 915 or 925 */
172 pciebar = pci_cfgregread(0, 0, 0, 0x48, 4);
181 * Read configuration space register
184 pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
189 * Some BIOS writers seem to want to ignore the spec and put
190 * 0 in the intline rather than 255 to indicate none. The rest of
191 * the code uses 255 as an invalid IRQ.
193 if (reg == PCIR_INTLINE && bytes == 1) {
194 line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
195 return (pci_i386_map_intline(line));
197 return (pcireg_cfgread(bus, slot, func, reg, bytes));
201 * Write configuration space register
204 pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
207 pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
211 * Configuration space access using direct register operations
214 /* enable configuration space accesses and return data port address */
216 pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
221 if (arch_i386_is_xbox) {
223 * The Xbox MCPX chipset is a derivative of the nForce 1
224 * chipset. It almost has the same bus layout; some devices
225 * cannot be used, because they have been removed.
229 * Devices 00:00.1 and 00:00.2 used to be memory controllers on
230 * the nForce chipset, but on the Xbox, using them will lockup
233 if (bus == 0 && slot == 0 && (func == 1 || func == 2))
237 * Bus 1 only contains a VGA controller at 01:00.0. When you try
238 * to probe beyond that device, you only get garbage, which
239 * could cause lockups.
241 if (bus == 1 && (slot != 0 || func != 0))
245 * Bus 2 used to contain the AGP controller, but the Xbox MCPX
246 * doesn't have one. Probing it can cause lockups.
253 if (bus <= PCI_BUSMAX
255 && func <= PCI_FUNCMAX
258 && (unsigned) bytes <= 4
259 && (reg & (bytes - 1)) == 0) {
262 outl(CONF1_ADDR_PORT, (1 << 31)
263 | (bus << 16) | (slot << 11)
264 | (func << 8) | (reg & ~0x03));
265 dataport = CONF1_DATA_PORT + (reg & 0x03);
268 outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1));
269 outb(CONF2_FORWARD_PORT, bus);
270 dataport = 0xc000 | (slot << 8) | reg;
277 /* disable configuration space accesses */
284 * Do nothing for the config mechanism 1 case.
285 * Writing a 0 to the address port can apparently
286 * confuse some bridges and cause spurious
291 outb(CONF2_ENABLE_PORT, 0);
297 pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
302 if (cfgmech == CFGMECH_PCIE) {
303 data = pciereg_cfgread(bus, slot, func, reg, bytes);
307 mtx_lock_spin(&pcicfg_mtx);
308 port = pci_cfgenable(bus, slot, func, reg, bytes);
323 mtx_unlock_spin(&pcicfg_mtx);
328 pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
332 if (cfgmech == CFGMECH_PCIE) {
333 pciereg_cfgwrite(bus, slot, func, reg, data, bytes);
337 mtx_lock_spin(&pcicfg_mtx);
338 port = pci_cfgenable(bus, slot, func, reg, bytes);
353 mtx_unlock_spin(&pcicfg_mtx);
356 /* check whether the configuration mechanism has been correctly identified */
358 pci_cfgcheck(int maxdev)
366 printf("pci_cfgcheck:\tdevice ");
368 for (device = 0; device < maxdev; device++) {
370 printf("%d ", device);
372 port = pci_cfgenable(0, device, 0, 0, 4);
374 if (id == 0 || id == 0xffffffff)
377 port = pci_cfgenable(0, device, 0, 8, 4);
378 class = inl(port) >> 8;
380 printf("[class=%06x] ", class);
381 if (class == 0 || (class & 0xf870ff) != 0)
384 port = pci_cfgenable(0, device, 0, 14, 1);
387 printf("[hdr=%02x] ", header);
388 if ((header & 0x7e) != 0)
392 printf("is there (id=%08x)\n", id);
398 printf("-- nothing found\n");
407 uint32_t mode1res, oldval1;
408 uint8_t mode2res, oldval2;
410 /* Check for type #1 first. */
411 oldval1 = inl(CONF1_ADDR_PORT);
414 printf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08x\n",
421 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK);
423 mode1res = inl(CONF1_ADDR_PORT);
424 outl(CONF1_ADDR_PORT, oldval1);
427 printf("pci_open(1a):\tmode1res=0x%08x (0x%08lx)\n", mode1res,
431 if (pci_cfgcheck(32))
435 outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1);
436 mode1res = inl(CONF1_ADDR_PORT);
437 outl(CONF1_ADDR_PORT, oldval1);
440 printf("pci_open(1b):\tmode1res=0x%08x (0x%08lx)\n", mode1res,
443 if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) {
444 if (pci_cfgcheck(32))
448 /* Type #1 didn't work, so try type #2. */
449 oldval2 = inb(CONF2_ENABLE_PORT);
452 printf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n",
456 if ((oldval2 & 0xf0) == 0) {
461 outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK);
462 mode2res = inb(CONF2_ENABLE_PORT);
463 outb(CONF2_ENABLE_PORT, oldval2);
466 printf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n",
467 mode2res, CONF2_ENABLE_CHK);
469 if (mode2res == CONF2_ENABLE_RES) {
471 printf("pci_open(2a):\tnow trying mechanism 2\n");
473 if (pci_cfgcheck(16))
478 /* Nothing worked, so punt. */
479 cfgmech = CFGMECH_NONE;
485 pciereg_cfgopen(void)
487 struct pcie_cfg_list *pcielist;
488 struct pcie_cfg_elem *pcie_array, *elem;
496 printf("Setting up PCIe mappings for BAR 0x%x\n", pciebar);
499 SLIST_FOREACH(pc, &cpuhead, pc_allcpu)
503 pcie_array = malloc(sizeof(struct pcie_cfg_elem) * PCIE_CACHE,
505 if (pcie_array == NULL)
508 va = kmem_alloc_nofault(kernel_map, PCIE_CACHE * PAGE_SIZE);
510 free(pcie_array, M_DEVBUF);
515 pcielist = &pcie_list[pc->pc_cpuid];
517 pcielist = &pcie_list[0];
519 TAILQ_INIT(pcielist);
520 for (i = 0; i < PCIE_CACHE; i++) {
521 elem = &pcie_array[i];
522 elem->vapage = va + (i * PAGE_SIZE);
524 TAILQ_INSERT_HEAD(pcielist, elem, elem);
529 cfgmech = CFGMECH_PCIE;
534 #define PCIE_PADDR(bar, reg, bus, slot, func) \
536 (((bus) & 0xff) << 20) | \
537 (((slot) & 0x1f) << 15) | \
538 (((func) & 0x7) << 12) | \
542 * Find an element in the cache that matches the physical page desired, or
543 * create a new mapping from the least recently used element.
544 * A very simple LRU algorithm is used here, does it need to be more
547 static __inline struct pcie_cfg_elem *
548 pciereg_findelem(vm_paddr_t papage)
550 struct pcie_cfg_list *pcielist;
551 struct pcie_cfg_elem *elem;
553 pcielist = &pcie_list[PCPU_GET(cpuid)];
554 TAILQ_FOREACH(elem, pcielist, elem) {
555 if (elem->papage == papage)
560 elem = TAILQ_LAST(pcielist, pcie_cfg_list);
561 if (elem->papage != 0) {
562 pmap_kremove(elem->vapage);
563 invlpg(elem->vapage);
565 pmap_kenter(elem->vapage, papage);
566 elem->papage = papage;
569 if (elem != TAILQ_FIRST(pcielist)) {
570 TAILQ_REMOVE(pcielist, elem, elem);
571 TAILQ_INSERT_HEAD(pcielist, elem, elem);
577 pciereg_cfgread(int bus, int slot, int func, int reg, int bytes)
579 struct pcie_cfg_elem *elem;
580 volatile vm_offset_t va;
581 vm_paddr_t pa, papage;
585 pa = PCIE_PADDR(pciebar, reg, bus, slot, func);
586 papage = pa & ~PAGE_MASK;
587 elem = pciereg_findelem(papage);
588 va = elem->vapage | (pa & PAGE_MASK);
592 data = *(volatile uint32_t *)(va);
595 data = *(volatile uint16_t *)(va);
598 data = *(volatile uint8_t *)(va);
601 panic("pciereg_cfgread: invalid width");
609 pciereg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
611 struct pcie_cfg_elem *elem;
612 volatile vm_offset_t va;
613 vm_paddr_t pa, papage;
616 pa = PCIE_PADDR(pciebar, reg, bus, slot, func);
617 papage = pa & ~PAGE_MASK;
618 elem = pciereg_findelem(papage);
619 va = elem->vapage | (pa & PAGE_MASK);
623 *(volatile uint32_t *)(va) = data;
626 *(volatile uint16_t *)(va) = data;
629 *(volatile uint8_t *)(va) = data;
632 panic("pciereg_cfgwrite: invalid width");