xref: /freebsd/sys/i386/pci/pci_cfgreg.c (revision af3d516f55be795f88c75c5aa0f927a41f486825)
1ac19f918SStefan Eßer /*
25bec6157SStefan Eßer  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
312a02d6eSMike Smith  * Copyright (c) 2000, Michael Smith <msmith@freebsd.org>
412a02d6eSMike Smith  * Copyright (c) 2000, BSDi
55bec6157SStefan Eßer  * All rights reserved.
65bec6157SStefan Eßer  *
75bec6157SStefan Eßer  * Redistribution and use in source and binary forms, with or without
85bec6157SStefan Eßer  * modification, are permitted provided that the following conditions
95bec6157SStefan Eßer  * are met:
105bec6157SStefan Eßer  * 1. Redistributions of source code must retain the above copyright
115bec6157SStefan Eßer  *    notice unmodified, this list of conditions, and the following
125bec6157SStefan Eßer  *    disclaimer.
135bec6157SStefan Eßer  * 2. Redistributions in binary form must reproduce the above copyright
145bec6157SStefan Eßer  *    notice, this list of conditions and the following disclaimer in the
155bec6157SStefan Eßer  *    documentation and/or other materials provided with the distribution.
165bec6157SStefan Eßer  *
175bec6157SStefan Eßer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
185bec6157SStefan Eßer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
195bec6157SStefan Eßer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
205bec6157SStefan Eßer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
215bec6157SStefan Eßer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
225bec6157SStefan Eßer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235bec6157SStefan Eßer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245bec6157SStefan Eßer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255bec6157SStefan Eßer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
265bec6157SStefan Eßer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275bec6157SStefan Eßer  *
28c3aac50fSPeter Wemm  * $FreeBSD$
295bec6157SStefan Eßer  *
30ac19f918SStefan Eßer  */
31ac19f918SStefan Eßer 
3212a02d6eSMike Smith #include <sys/param.h>		/* XXX trim includes */
335bec6157SStefan Eßer #include <sys/systm.h>
348dc26439SPeter Wemm #include <sys/bus.h>
358dc26439SPeter Wemm #include <sys/kernel.h>
362a50a6d7SMike Smith #include <sys/module.h>
37280b4748SPeter Wemm #include <sys/malloc.h>
38af3d516fSPeter Wemm #include <sys/lock.h>
39af3d516fSPeter Wemm #include <sys/mutex.h>
4054c9005fSWarner Losh #include <vm/vm.h>
4154c9005fSWarner Losh #include <vm/pmap.h>
4254c9005fSWarner Losh #include <machine/md_var.h>
43e300f53cSWarner Losh #include <dev/pci/pcivar.h>
44e300f53cSWarner Losh #include <dev/pci/pcireg.h>
452a50a6d7SMike Smith #include <isa/isavar.h>
4612a02d6eSMike Smith #include <machine/pci_cfgreg.h>
47300451c4SMike Smith #include <machine/segments.h>
48300451c4SMike Smith #include <machine/pc/bios.h>
49300451c4SMike Smith 
50bb0d0a8eSMike Smith #ifdef APIC_IO
51bb0d0a8eSMike Smith #include <machine/smp.h>
52bb0d0a8eSMike Smith #endif /* APIC_IO */
53bb0d0a8eSMike Smith 
5421c3015aSDoug Rabson #include "pcib_if.h"
5521c3015aSDoug Rabson 
568ff25e97SJohn Baldwin #define PRVERB(a) do {							\
578ff25e97SJohn Baldwin 	if (bootverbose)						\
588ff25e97SJohn Baldwin 		printf a ;						\
598ff25e97SJohn Baldwin } while(0)
60d626906bSWarner Losh 
615bec6157SStefan Eßer static int cfgmech;
625bec6157SStefan Eßer static int devmax;
63300451c4SMike Smith 
648ab96fd8SJohn Baldwin static int	pci_cfgintr_valid(struct PIR_entry *pe, int pin, int irq);
65099d058bSMike Smith static int	pci_cfgintr_unique(struct PIR_entry *pe, int pin);
66099d058bSMike Smith static int	pci_cfgintr_linked(struct PIR_entry *pe, int pin);
67099d058bSMike Smith static int	pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin);
68099d058bSMike Smith static int	pci_cfgintr_virgin(struct PIR_entry *pe, int pin);
69099d058bSMike Smith 
70fbabd7beSJohn Baldwin static void	pci_print_irqmask(u_int16_t irqs);
71fbabd7beSJohn Baldwin static void	pci_print_route_table(struct PIR_table *prt, int size);
7212a02d6eSMike Smith static int	pcireg_cfgread(int bus, int slot, int func, int reg, int bytes);
7312a02d6eSMike Smith static void	pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes);
74300451c4SMike Smith static int	pcireg_cfgopen(void);
75300451c4SMike Smith 
76099d058bSMike Smith static struct PIR_table *pci_route_table;
7754c9005fSWarner Losh static int pci_route_count;
7854c9005fSWarner Losh 
79af3d516fSPeter Wemm static struct mtx pcicfg_mtx;
80af3d516fSPeter Wemm 
818ce1ab3aSWarner Losh /*
828ce1ab3aSWarner Losh  * Some BIOS writers seem to want to ignore the spec and put
838ce1ab3aSWarner Losh  * 0 in the intline rather than 255 to indicate none.  Some use
848ce1ab3aSWarner Losh  * numbers in the range 128-254 to indicate something strange and
858ce1ab3aSWarner Losh  * apparently undocumented anywhere.  Assume these are completely bogus
868ce1ab3aSWarner Losh  * and map them to 255, which means "none".
878ce1ab3aSWarner Losh  */
888ce1ab3aSWarner Losh static __inline__ int
898ce1ab3aSWarner Losh pci_i386_map_intline(int line)
908ce1ab3aSWarner Losh {
918ce1ab3aSWarner Losh 	if (line == 0 || line >= 128)
92e300f53cSWarner Losh 		return (PCI_INVALID_IRQ);
938ce1ab3aSWarner Losh 	return (line);
948ce1ab3aSWarner Losh }
958ce1ab3aSWarner Losh 
96d626906bSWarner Losh static u_int16_t
97d626906bSWarner Losh pcibios_get_version(void)
98d626906bSWarner Losh {
99d626906bSWarner Losh 	struct bios_regs args;
100d626906bSWarner Losh 
1015264a94fSJohn Baldwin 	if (PCIbios.ventry == 0) {
102d626906bSWarner Losh 		PRVERB(("pcibios: No call entry point\n"));
103d626906bSWarner Losh 		return (0);
104d626906bSWarner Losh 	}
105d626906bSWarner Losh 	args.eax = PCIBIOS_BIOS_PRESENT;
106d626906bSWarner Losh 	if (bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL))) {
107d626906bSWarner Losh 		PRVERB(("pcibios: BIOS_PRESENT call failed\n"));
108d626906bSWarner Losh 		return (0);
109d626906bSWarner Losh 	}
110d626906bSWarner Losh 	if (args.edx != 0x20494350) {
111d626906bSWarner Losh 		PRVERB(("pcibios: BIOS_PRESENT didn't return 'PCI ' in edx\n"));
112d626906bSWarner Losh 		return (0);
113d626906bSWarner Losh 	}
114d626906bSWarner Losh 	return (args.ebx & 0xffff);
115d626906bSWarner Losh }
116d626906bSWarner Losh 
11712a02d6eSMike Smith /*
11812a02d6eSMike Smith  * Initialise access to PCI configuration space
11912a02d6eSMike Smith  */
12012a02d6eSMike Smith int
12112a02d6eSMike Smith pci_cfgregopen(void)
12221c3015aSDoug Rabson {
12312a02d6eSMike Smith 	static int		opened = 0;
12454c9005fSWarner Losh 	u_long			sigaddr;
12554c9005fSWarner Losh 	static struct PIR_table	*pt;
126af3d516fSPeter Wemm 	u_int16_t		v;
12754c9005fSWarner Losh 	u_int8_t		ck, *cv;
12854c9005fSWarner Losh 	int			i;
12921c3015aSDoug Rabson 
13012a02d6eSMike Smith 	if (opened)
13112a02d6eSMike Smith 		return(1);
132300451c4SMike Smith 
133af3d516fSPeter Wemm 	if (pcireg_cfgopen() == 0)
134300451c4SMike Smith 		return(0);
13554c9005fSWarner Losh 
136af3d516fSPeter Wemm 	v = pcibios_get_version();
137af3d516fSPeter Wemm 	if (v > 0)
138af3d516fSPeter Wemm 		printf("pcibios: BIOS version %x.%02x\n", (v & 0xff00) >> 8,
139af3d516fSPeter Wemm 		    v & 0xff);
140af3d516fSPeter Wemm 
14154c9005fSWarner Losh 	/*
14254c9005fSWarner Losh 	 * Look for the interrupt routing table.
143a8c18609SWarner Losh 	 *
144a8c18609SWarner Losh 	 * We use PCI BIOS's PIR table if it's available $PIR is the
145a8c18609SWarner Losh 	 * standard way to do this.  Sadly, some machines are not
146a8c18609SWarner Losh 	 * standards conforming and have _PIR instead.  We shrug and cope
147a8c18609SWarner Losh 	 * by looking for both.
14854c9005fSWarner Losh 	 */
149a8c18609SWarner Losh 	if (pcibios_get_version() >= 0x0210 && pt == NULL) {
150a8c18609SWarner Losh 		sigaddr = bios_sigsearch(0, "$PIR", 4, 16, 0);
151a8c18609SWarner Losh 		if (sigaddr == 0)
152a8c18609SWarner Losh 			sigaddr = bios_sigsearch(0, "_PIR", 4, 16, 0);
153a8c18609SWarner Losh 		if (sigaddr != 0) {
154e300f53cSWarner Losh 			pt = (struct PIR_table *)(uintptr_t)
155e300f53cSWarner Losh 			    BIOS_PADDRTOVADDR(sigaddr);
156a8c18609SWarner Losh 			for (cv = (u_int8_t *)pt, ck = 0, i = 0;
157a8c18609SWarner Losh 			     i < (pt->pt_header.ph_length); i++) {
15854c9005fSWarner Losh 				ck += cv[i];
15954c9005fSWarner Losh 			}
160fefe985dSJohn Baldwin 			if (ck == 0 && pt->pt_header.ph_length >
161fefe985dSJohn Baldwin 			    sizeof(struct PIR_header)) {
162099d058bSMike Smith 				pci_route_table = pt;
163a8c18609SWarner Losh 				pci_route_count = (pt->pt_header.ph_length -
164e300f53cSWarner Losh 				    sizeof(struct PIR_header)) /
165e300f53cSWarner Losh 				    sizeof(struct PIR_entry);
166a8c18609SWarner Losh 				printf("Using $PIR table, %d entries at %p\n",
167a8c18609SWarner Losh 				    pci_route_count, pci_route_table);
168facfd6e8SJohn Baldwin 				if (bootverbose)
169facfd6e8SJohn Baldwin 					pci_print_route_table(pci_route_table,
170facfd6e8SJohn Baldwin 					    pci_route_count);
17154c9005fSWarner Losh 			}
17254c9005fSWarner Losh 		}
173a8c18609SWarner Losh 	}
174af3d516fSPeter Wemm 	mtx_init(&pcicfg_mtx, "pcicfg", NULL, MTX_SPIN);
17512a02d6eSMike Smith 	opened = 1;
176300451c4SMike Smith 	return(1);
177300451c4SMike Smith }
178300451c4SMike Smith 
17912a02d6eSMike Smith /*
18012a02d6eSMike Smith  * Read configuration space register
18112a02d6eSMike Smith  */
182bb0d0a8eSMike Smith u_int32_t
183bb0d0a8eSMike Smith pci_cfgregread(int bus, int slot, int func, int reg, int bytes)
184bb0d0a8eSMike Smith {
185e300f53cSWarner Losh 	uint32_t line;
186bb0d0a8eSMike Smith #ifdef APIC_IO
187e300f53cSWarner Losh 	uint32_t pin;
188e300f53cSWarner Losh 
189bb0d0a8eSMike Smith 	/*
190e300f53cSWarner Losh 	 * If we are using the APIC, the contents of the intline
191e300f53cSWarner Losh 	 * register will probably be wrong (since they are set up for
192e300f53cSWarner Losh 	 * use with the PIC.  Rather than rewrite these registers
193e300f53cSWarner Losh 	 * (maybe that would be smarter) we trap attempts to read them
194e300f53cSWarner Losh 	 * and translate to our private vector numbers.
195bb0d0a8eSMike Smith 	 */
196bb0d0a8eSMike Smith 	if ((reg == PCIR_INTLINE) && (bytes == 1)) {
197bb0d0a8eSMike Smith 
198af3d516fSPeter Wemm 		pin = pcireg_cfgread(bus, slot, func, PCIR_INTPIN, 1);
199af3d516fSPeter Wemm 		line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
200bb0d0a8eSMike Smith 
201bb0d0a8eSMike Smith 		if (pin != 0) {
202bb0d0a8eSMike Smith 			int airq;
203bb0d0a8eSMike Smith 
204bb0d0a8eSMike Smith 			airq = pci_apic_irq(bus, slot, pin);
205bb0d0a8eSMike Smith 			if (airq >= 0) {
206bb0d0a8eSMike Smith 				/* PCI specific entry found in MP table */
207bb0d0a8eSMike Smith 				if (airq != line)
208bb0d0a8eSMike Smith 					undirect_pci_irq(line);
209bb0d0a8eSMike Smith 				return(airq);
210bb0d0a8eSMike Smith 			} else {
211bb0d0a8eSMike Smith 				/*
212e300f53cSWarner Losh 				 * PCI interrupts might be redirected
213e300f53cSWarner Losh 				 * to the ISA bus according to some MP
214e300f53cSWarner Losh 				 * tables. Use the same methods as
215e300f53cSWarner Losh 				 * used by the ISA devices devices to
216e300f53cSWarner Losh 				 * find the proper IOAPIC int pin.
217bb0d0a8eSMike Smith 				 */
218bb0d0a8eSMike Smith 				airq = isa_apic_irq(line);
219bb0d0a8eSMike Smith 				if ((airq >= 0) && (airq != line)) {
220bb0d0a8eSMike Smith 					/* XXX: undirect_pci_irq() ? */
221bb0d0a8eSMike Smith 					undirect_isa_irq(line);
222bb0d0a8eSMike Smith 					return(airq);
223bb0d0a8eSMike Smith 				}
224bb0d0a8eSMike Smith 			}
225bb0d0a8eSMike Smith 		}
226bb0d0a8eSMike Smith 		return(line);
227bb0d0a8eSMike Smith 	}
228d5ccecfaSWarner Losh #else
229d5ccecfaSWarner Losh 	/*
230d5ccecfaSWarner Losh 	 * Some BIOS writers seem to want to ignore the spec and put
231d5ccecfaSWarner Losh 	 * 0 in the intline rather than 255 to indicate none.  The rest of
232d5ccecfaSWarner Losh 	 * the code uses 255 as an invalid IRQ.
233d5ccecfaSWarner Losh 	 */
234d5ccecfaSWarner Losh 	if (reg == PCIR_INTLINE && bytes == 1) {
235af3d516fSPeter Wemm 		line = pcireg_cfgread(bus, slot, func, PCIR_INTLINE, 1);
2368ce1ab3aSWarner Losh 		return pci_i386_map_intline(line);
237d5ccecfaSWarner Losh 	}
238bb0d0a8eSMike Smith #endif /* APIC_IO */
239af3d516fSPeter Wemm 	return(pcireg_cfgread(bus, slot, func, reg, bytes));
240bb0d0a8eSMike Smith }
241bb0d0a8eSMike Smith 
24212a02d6eSMike Smith /*
24312a02d6eSMike Smith  * Write configuration space register
24412a02d6eSMike Smith  */
24512a02d6eSMike Smith void
24612a02d6eSMike Smith pci_cfgregwrite(int bus, int slot, int func, int reg, u_int32_t data, int bytes)
24712a02d6eSMike Smith {
248af3d516fSPeter Wemm 
249cb8e4332SPoul-Henning Kamp 	pcireg_cfgwrite(bus, slot, func, reg, data, bytes);
25012a02d6eSMike Smith }
25112a02d6eSMike Smith 
25212a02d6eSMike Smith /*
25354c9005fSWarner Losh  * Route a PCI interrupt
25454c9005fSWarner Losh  */
25554c9005fSWarner Losh int
2568ab96fd8SJohn Baldwin pci_cfgintr(int bus, int device, int pin, int oldirq)
25754c9005fSWarner Losh {
25854c9005fSWarner Losh 	struct PIR_entry	*pe;
2599d558634SMike Smith 	int			i, irq;
2609d558634SMike Smith 	struct bios_regs	args;
261d626906bSWarner Losh 	u_int16_t		v;
262d3b6477aSWarner Losh  	int already = 0;
263ce494452SWarner Losh  	int errok = 0;
26454c9005fSWarner Losh 
265d626906bSWarner Losh 	v = pcibios_get_version();
266d626906bSWarner Losh 	if (v < 0x0210) {
267d626906bSWarner Losh 		PRVERB((
268d626906bSWarner Losh 		"pci_cfgintr: BIOS %x.%02x doesn't support interrupt routing\n",
269d626906bSWarner Losh 		    (v & 0xff00) >> 8, v & 0xff));
270e300f53cSWarner Losh 		return (PCI_INVALID_IRQ);
271d626906bSWarner Losh 	}
272a3793252SWarner Losh 	if ((bus < 0) || (bus > 255) || (device < 0) || (device > 255) ||
273a3793252SWarner Losh 	    (pin < 1) || (pin > 4))
274e300f53cSWarner Losh 		return(PCI_INVALID_IRQ);
27554c9005fSWarner Losh 
27654c9005fSWarner Losh 	/*
27754c9005fSWarner Losh 	 * Scan the entry table for a contender
27854c9005fSWarner Losh 	 */
279e300f53cSWarner Losh 	for (i = 0, pe = &pci_route_table->pt_entry[0]; i < pci_route_count;
280e300f53cSWarner Losh 	     i++, pe++) {
28154c9005fSWarner Losh 		if ((bus != pe->pe_bus) || (device != pe->pe_device))
28254c9005fSWarner Losh 			continue;
2838ab96fd8SJohn Baldwin 		/*
2848ab96fd8SJohn Baldwin 		 * A link of 0 means that this intpin is not connected to
2858ab96fd8SJohn Baldwin 		 * any other device's interrupt pins and is not connected to
2868ab96fd8SJohn Baldwin 		 * any of the Interrupt Router's interrupt pins, so we can't
2878ab96fd8SJohn Baldwin 		 * route it.
2888ab96fd8SJohn Baldwin 		 */
2898ab96fd8SJohn Baldwin 		if (pe->pe_intpin[pin - 1].link == 0)
2908ab96fd8SJohn Baldwin 			continue;
291099d058bSMike Smith 
2928ab96fd8SJohn Baldwin 		if (pci_cfgintr_valid(pe, pin, oldirq)) {
2938ab96fd8SJohn Baldwin 			printf("pci_cfgintr: %d:%d INT%c BIOS irq %d\n", bus,
2948ab96fd8SJohn Baldwin 			    device, 'A' + pin - 1, oldirq);
2958ab96fd8SJohn Baldwin 			return (oldirq);
2968ab96fd8SJohn Baldwin 		}
297ce494452SWarner Losh 
298ce494452SWarner Losh 		/*
299ce494452SWarner Losh 		 * We try to find a linked interrupt, then we look to see
300ce494452SWarner Losh 		 * if the interrupt is uniquely routed, then we look for
301ce494452SWarner Losh 		 * a virgin interrupt.  The virgin interrupt should return
302ce494452SWarner Losh 		 * an interrupt we can route, but if that fails, maybe we
303ce494452SWarner Losh 		 * should try harder to route a different interrupt.
304ce494452SWarner Losh 		 * However, experience has shown that that's rarely the
305ce494452SWarner Losh 		 * failure mode we see.
306ce494452SWarner Losh 		 */
307099d058bSMike Smith 		irq = pci_cfgintr_linked(pe, pin);
308ce494452SWarner Losh 		if (irq != PCI_INVALID_IRQ)
309ce494452SWarner Losh 			already = 1;
310ce494452SWarner Losh 		if (irq == PCI_INVALID_IRQ) {
311d626906bSWarner Losh 			irq = pci_cfgintr_unique(pe, pin);
312ce494452SWarner Losh 			if (irq != PCI_INVALID_IRQ)
313ce494452SWarner Losh 				errok = 1;
314ce494452SWarner Losh 		}
315e300f53cSWarner Losh 		if (irq == PCI_INVALID_IRQ)
316099d058bSMike Smith 			irq = pci_cfgintr_virgin(pe, pin);
317e300f53cSWarner Losh 		if (irq == PCI_INVALID_IRQ)
31854c9005fSWarner Losh 			break;
319099d058bSMike Smith 
3209d558634SMike Smith 		/*
321ce494452SWarner Losh 		 * Ask the BIOS to route the interrupt.  If we picked an
322ce494452SWarner Losh 		 * interrupt that failed, we should really try other
323ce494452SWarner Losh 		 * choices that the BIOS offers us.
324ce494452SWarner Losh 		 *
325ce494452SWarner Losh 		 * For uniquely routed interrupts, we need to try
326ce494452SWarner Losh 		 * to route them on some machines.  Yet other machines
327ce494452SWarner Losh 		 * fail to route, so we have to pretend that in that
328ce494452SWarner Losh 		 * case it worked.  Isn't pc hardware fun?
329ce494452SWarner Losh 		 *
330ce494452SWarner Losh 		 * NOTE: if we want to whack hardware to do this, then
331ce494452SWarner Losh 		 * I think the right way to do that would be to have
332ce494452SWarner Losh 		 * bridge drivers that do this.  I'm not sure that the
333ce494452SWarner Losh 		 * $PIR table would be valid for those interrupt
334ce494452SWarner Losh 		 * routers.
3359d558634SMike Smith 		 */
3369d558634SMike Smith 		args.eax = PCIBIOS_ROUTE_INTERRUPT;
3379d558634SMike Smith 		args.ebx = (bus << 8) | (device << 3);
338e300f53cSWarner Losh 		/* pin value is 0xa - 0xd */
339e300f53cSWarner Losh 		args.ecx = (irq << 8) | (0xa + pin - 1);
340e300f53cSWarner Losh 		if (!already &&
341ce494452SWarner Losh 		    bios32(&args, PCIbios.ventry, GSEL(GCODE_SEL, SEL_KPL)) &&
342ce494452SWarner Losh 		    !errok) {
343d3b6477aSWarner Losh 			PRVERB(("pci_cfgintr: ROUTE_INTERRUPT failed.\n"));
344e300f53cSWarner Losh 			return(PCI_INVALID_IRQ);
345d626906bSWarner Losh 		}
346e300f53cSWarner Losh 		printf("pci_cfgintr: %d:%d INT%c routed to irq %d\n", bus,
347e300f53cSWarner Losh 		    device, 'A' + pin - 1, irq);
3489d558634SMike Smith 		return(irq);
34954c9005fSWarner Losh 	}
350099d058bSMike Smith 
351e300f53cSWarner Losh 	PRVERB(("pci_cfgintr: can't route an interrupt to %d:%d INT%c\n", bus,
352e300f53cSWarner Losh 	    device, 'A' + pin - 1));
353e300f53cSWarner Losh 	return(PCI_INVALID_IRQ);
354099d058bSMike Smith }
355099d058bSMike Smith 
356099d058bSMike Smith /*
3578ab96fd8SJohn Baldwin  * Check to see if an existing IRQ setting is valid.
3588ab96fd8SJohn Baldwin  */
3598ab96fd8SJohn Baldwin static int
3608ab96fd8SJohn Baldwin pci_cfgintr_valid(struct PIR_entry *pe, int pin, int irq)
3618ab96fd8SJohn Baldwin {
3628ab96fd8SJohn Baldwin 	uint32_t irqmask;
3638ab96fd8SJohn Baldwin 
3648ab96fd8SJohn Baldwin 	if (!PCI_INTERRUPT_VALID(irq))
3658ab96fd8SJohn Baldwin 		return (0);
3668ab96fd8SJohn Baldwin 	irqmask = pe->pe_intpin[pin - 1].irqs;
3678ab96fd8SJohn Baldwin 	if (irqmask & (1 << irq)) {
3688ab96fd8SJohn Baldwin 		PRVERB(("pci_cfgintr_valid: BIOS irq %d is valid\n", irq));
3698ab96fd8SJohn Baldwin 		return (1);
3708ab96fd8SJohn Baldwin 	}
3718ab96fd8SJohn Baldwin 	return (0);
3728ab96fd8SJohn Baldwin }
3738ab96fd8SJohn Baldwin 
3748ab96fd8SJohn Baldwin /*
375099d058bSMike Smith  * Look to see if the routing table claims this pin is uniquely routed.
376099d058bSMike Smith  */
377099d058bSMike Smith static int
378099d058bSMike Smith pci_cfgintr_unique(struct PIR_entry *pe, int pin)
379099d058bSMike Smith {
380099d058bSMike Smith 	int		irq;
381d5ccecfaSWarner Losh 	uint32_t	irqmask;
382099d058bSMike Smith 
383d5ccecfaSWarner Losh 	irqmask = pe->pe_intpin[pin - 1].irqs;
384d5ccecfaSWarner Losh 	if (irqmask != 0 && powerof2(irqmask)) {
385d5ccecfaSWarner Losh 		irq = ffs(irqmask) - 1;
386d626906bSWarner Losh 		PRVERB(("pci_cfgintr_unique: hard-routed to irq %d\n", irq));
387099d058bSMike Smith 		return(irq);
388099d058bSMike Smith 	}
389e300f53cSWarner Losh 	return(PCI_INVALID_IRQ);
390099d058bSMike Smith }
391099d058bSMike Smith 
392099d058bSMike Smith /*
393099d058bSMike Smith  * Look for another device which shares the same link byte and
394099d058bSMike Smith  * already has a unique IRQ, or which has had one routed already.
395099d058bSMike Smith  */
396099d058bSMike Smith static int
397099d058bSMike Smith pci_cfgintr_linked(struct PIR_entry *pe, int pin)
398099d058bSMike Smith {
399099d058bSMike Smith 	struct PIR_entry	*oe;
400099d058bSMike Smith 	struct PIR_intpin	*pi;
401099d058bSMike Smith 	int			i, j, irq;
402099d058bSMike Smith 
403099d058bSMike Smith 	/*
404099d058bSMike Smith 	 * Scan table slots.
405099d058bSMike Smith 	 */
406e300f53cSWarner Losh 	for (i = 0, oe = &pci_route_table->pt_entry[0]; i < pci_route_count;
407e300f53cSWarner Losh 	     i++, oe++) {
408099d058bSMike Smith 		/* scan interrupt pins */
409099d058bSMike Smith 		for (j = 0, pi = &oe->pe_intpin[0]; j < 4; j++, pi++) {
410099d058bSMike Smith 
411e300f53cSWarner Losh 			/* don't look at the entry we're trying to match */
412099d058bSMike Smith 			if ((pe == oe) && (i == (pin - 1)))
413099d058bSMike Smith 				continue;
414099d058bSMike Smith 			/* compare link bytes */
415099d058bSMike Smith 			if (pi->link != pe->pe_intpin[pin - 1].link)
416099d058bSMike Smith 				continue;
417099d058bSMike Smith 			/* link destination mapped to a unique interrupt? */
418d5ccecfaSWarner Losh 			if (pi->irqs != 0 && powerof2(pi->irqs)) {
419099d058bSMike Smith 				irq = ffs(pi->irqs) - 1;
420d626906bSWarner Losh 				PRVERB(("pci_cfgintr_linked: linked (%x) to hard-routed irq %d\n",
421d626906bSWarner Losh 				    pi->link, irq));
422099d058bSMike Smith 				return(irq);
423099d058bSMike Smith 			}
424099d058bSMike Smith 
425e300f53cSWarner Losh 			/*
426e300f53cSWarner Losh 			 * look for the real PCI device that matches this
427e300f53cSWarner Losh 			 * table entry
428e300f53cSWarner Losh 			 */
429e300f53cSWarner Losh 			irq = pci_cfgintr_search(pe, oe->pe_bus, oe->pe_device,
430e300f53cSWarner Losh 			    j, pin);
431e300f53cSWarner Losh 			if (irq != PCI_INVALID_IRQ)
432099d058bSMike Smith 				return(irq);
433099d058bSMike Smith 		}
434099d058bSMike Smith 	}
435e300f53cSWarner Losh 	return(PCI_INVALID_IRQ);
436099d058bSMike Smith }
437099d058bSMike Smith 
438099d058bSMike Smith /*
439099d058bSMike Smith  * Scan for the real PCI device at (bus)/(device) using intpin (matchpin) and
440099d058bSMike Smith  * see if it has already been assigned an interrupt.
441099d058bSMike Smith  */
442099d058bSMike Smith static int
443099d058bSMike Smith pci_cfgintr_search(struct PIR_entry *pe, int bus, int device, int matchpin, int pin)
444099d058bSMike Smith {
445099d058bSMike Smith 	devclass_t		pci_devclass;
446099d058bSMike Smith 	device_t		*pci_devices;
447099d058bSMike Smith 	int			pci_count;
448099d058bSMike Smith 	device_t		*pci_children;
449099d058bSMike Smith 	int			pci_childcount;
450099d058bSMike Smith 	device_t		*busp, *childp;
451099d058bSMike Smith 	int			i, j, irq;
452099d058bSMike Smith 
453099d058bSMike Smith 	/*
454099d058bSMike Smith 	 * Find all the PCI busses.
455099d058bSMike Smith 	 */
456099d058bSMike Smith 	pci_count = 0;
457099d058bSMike Smith 	if ((pci_devclass = devclass_find("pci")) != NULL)
458099d058bSMike Smith 		devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
459099d058bSMike Smith 
460099d058bSMike Smith 	/*
461099d058bSMike Smith 	 * Scan all the PCI busses/devices looking for this one.
462099d058bSMike Smith 	 */
463e300f53cSWarner Losh 	irq = PCI_INVALID_IRQ;
464e300f53cSWarner Losh 	for (i = 0, busp = pci_devices; (i < pci_count) && (irq == PCI_INVALID_IRQ);
465e300f53cSWarner Losh 	     i++, busp++) {
466099d058bSMike Smith 		pci_childcount = 0;
467099d058bSMike Smith 		device_get_children(*busp, &pci_children, &pci_childcount);
468099d058bSMike Smith 
469e300f53cSWarner Losh 		for (j = 0, childp = pci_children; j < pci_childcount; j++,
470e300f53cSWarner Losh 		    childp++) {
471099d058bSMike Smith 			if ((pci_get_bus(*childp) == bus) &&
472099d058bSMike Smith 			    (pci_get_slot(*childp) == device) &&
4731cf5f555SWarner Losh 			    (pci_get_intpin(*childp) == matchpin)) {
4748ce1ab3aSWarner Losh 				irq = pci_i386_map_intline(pci_get_irq(*childp));
475e300f53cSWarner Losh 				if (irq != PCI_INVALID_IRQ)
476d626906bSWarner Losh 					PRVERB(("pci_cfgintr_search: linked (%x) to configured irq %d at %d:%d:%d\n",
47785fab963SMike Smith 					    pe->pe_intpin[pin - 1].link, irq,
478e300f53cSWarner Losh 					    pci_get_bus(*childp),
479e300f53cSWarner Losh 					    pci_get_slot(*childp),
480e300f53cSWarner Losh 					    pci_get_function(*childp)));
4816c9eb5f3SMike Smith 				break;
4826c9eb5f3SMike Smith 			}
4836c9eb5f3SMike Smith 		}
4846c9eb5f3SMike Smith 		if (pci_children != NULL)
4856c9eb5f3SMike Smith 			free(pci_children, M_TEMP);
4866c9eb5f3SMike Smith 	}
4876c9eb5f3SMike Smith 	if (pci_devices != NULL)
4886c9eb5f3SMike Smith 		free(pci_devices, M_TEMP);
489099d058bSMike Smith 	return(irq);
490099d058bSMike Smith }
491099d058bSMike Smith 
492099d058bSMike Smith /*
493099d058bSMike Smith  * Pick a suitable IRQ from those listed as routable to this device.
494099d058bSMike Smith  */
495099d058bSMike Smith static int
496099d058bSMike Smith pci_cfgintr_virgin(struct PIR_entry *pe, int pin)
497099d058bSMike Smith {
498099d058bSMike Smith 	int		irq, ibit;
499099d058bSMike Smith 
500e300f53cSWarner Losh 	/*
501e300f53cSWarner Losh 	 * first scan the set of PCI-only interrupts and see if any of these
502e300f53cSWarner Losh 	 * are routable
503e300f53cSWarner Losh 	 */
504099d058bSMike Smith 	for (irq = 0; irq < 16; irq++) {
505099d058bSMike Smith 		ibit = (1 << irq);
506099d058bSMike Smith 
507099d058bSMike Smith 		/* can we use this interrupt? */
508099d058bSMike Smith 		if ((pci_route_table->pt_header.ph_pci_irqs & ibit) &&
509099d058bSMike Smith 		    (pe->pe_intpin[pin - 1].irqs & ibit)) {
510d626906bSWarner Losh 			PRVERB(("pci_cfgintr_virgin: using routable PCI-only interrupt %d\n", irq));
511099d058bSMike Smith 			return(irq);
512099d058bSMike Smith 		}
513099d058bSMike Smith 	}
514099d058bSMike Smith 
515099d058bSMike Smith 	/* life is tough, so just pick an interrupt */
516099d058bSMike Smith 	for (irq = 0; irq < 16; irq++) {
517099d058bSMike Smith 		ibit = (1 << irq);
518099d058bSMike Smith 		if (pe->pe_intpin[pin - 1].irqs & ibit) {
519d626906bSWarner Losh 			PRVERB(("pci_cfgintr_virgin: using routable interrupt %d\n", irq));
520099d058bSMike Smith 			return(irq);
521099d058bSMike Smith 		}
522099d058bSMike Smith 	}
523e300f53cSWarner Losh 	return(PCI_INVALID_IRQ);
52454c9005fSWarner Losh }
52554c9005fSWarner Losh 
526fbabd7beSJohn Baldwin static void
527fbabd7beSJohn Baldwin pci_print_irqmask(u_int16_t irqs)
528fbabd7beSJohn Baldwin {
529fbabd7beSJohn Baldwin 	int i, first;
530fbabd7beSJohn Baldwin 
531fbabd7beSJohn Baldwin 	if (irqs == 0) {
532fbabd7beSJohn Baldwin 		printf("none");
533fbabd7beSJohn Baldwin 		return;
534fbabd7beSJohn Baldwin 	}
535fbabd7beSJohn Baldwin 	first = 1;
536fbabd7beSJohn Baldwin 	for (i = 0; i < 16; i++, irqs >>= 1)
537fbabd7beSJohn Baldwin 		if (irqs & 1) {
538fbabd7beSJohn Baldwin 			if (!first)
539fbabd7beSJohn Baldwin 				printf(" ");
540fbabd7beSJohn Baldwin 			else
541fbabd7beSJohn Baldwin 				first = 0;
542fbabd7beSJohn Baldwin 			printf("%d", i);
543fbabd7beSJohn Baldwin 		}
544fbabd7beSJohn Baldwin }
545fbabd7beSJohn Baldwin 
546fbabd7beSJohn Baldwin /*
547fbabd7beSJohn Baldwin  * Dump the contents of a PCI BIOS Interrupt Routing Table to the console.
548fbabd7beSJohn Baldwin  */
549fbabd7beSJohn Baldwin static void
550fbabd7beSJohn Baldwin pci_print_route_table(struct PIR_table *prt, int size)
551fbabd7beSJohn Baldwin {
552fbabd7beSJohn Baldwin 	struct PIR_entry *entry;
553fbabd7beSJohn Baldwin 	struct PIR_intpin *intpin;
554fbabd7beSJohn Baldwin 	int i, pin;
555fbabd7beSJohn Baldwin 
556fbabd7beSJohn Baldwin 	printf("PCI-Only Interrupts: ");
557fbabd7beSJohn Baldwin 	pci_print_irqmask(prt->pt_header.ph_pci_irqs);
558fbabd7beSJohn Baldwin 	printf("\nLocation  Bus Device Pin  Link  IRQs\n");
559fbabd7beSJohn Baldwin 	entry = &prt->pt_entry[0];
560fbabd7beSJohn Baldwin 	for (i = 0; i < size; i++, entry++) {
561fbabd7beSJohn Baldwin 		intpin = &entry->pe_intpin[0];
562fbabd7beSJohn Baldwin 		for (pin = 0; pin < 4; pin++, intpin++)
563fbabd7beSJohn Baldwin 			if (intpin->link != 0) {
564fbabd7beSJohn Baldwin 				if (entry->pe_slot == 0)
565fbabd7beSJohn Baldwin 					printf("embedded ");
566fbabd7beSJohn Baldwin 				else
567fbabd7beSJohn Baldwin 					printf("slot %-3d ", entry->pe_slot);
568fbabd7beSJohn Baldwin 				printf(" %3d  %3d    %c   0x%02x  ",
569fbabd7beSJohn Baldwin 				    entry->pe_bus, entry->pe_device,
570fbabd7beSJohn Baldwin 				    'A' + pin, intpin->link);
571fbabd7beSJohn Baldwin 				pci_print_irqmask(intpin->irqs);
572fbabd7beSJohn Baldwin 				printf("\n");
573fbabd7beSJohn Baldwin 			}
574fbabd7beSJohn Baldwin 	}
575fbabd7beSJohn Baldwin }
57654c9005fSWarner Losh 
57754c9005fSWarner Losh /*
578c3ba1376SJohn Baldwin  * See if any interrupts for a given PCI bus are routed in the PIR.  Don't
579c3ba1376SJohn Baldwin  * even bother looking if the BIOS doesn't support routing anyways.
580c3ba1376SJohn Baldwin  */
581c3ba1376SJohn Baldwin int
582c3ba1376SJohn Baldwin pci_probe_route_table(int bus)
583c3ba1376SJohn Baldwin {
584c3ba1376SJohn Baldwin 	int i;
585c3ba1376SJohn Baldwin 	u_int16_t v;
586c3ba1376SJohn Baldwin 
587c3ba1376SJohn Baldwin 	v = pcibios_get_version();
588c3ba1376SJohn Baldwin 	if (v < 0x0210)
589c3ba1376SJohn Baldwin 		return (0);
590c3ba1376SJohn Baldwin 	for (i = 0; i < pci_route_count; i++)
591c3ba1376SJohn Baldwin 		if (pci_route_table->pt_entry[i].pe_bus == bus)
592c3ba1376SJohn Baldwin 			return (1);
593c3ba1376SJohn Baldwin 	return (0);
594c3ba1376SJohn Baldwin }
595c3ba1376SJohn Baldwin 
59612a02d6eSMike Smith /*
59712a02d6eSMike Smith  * Configuration space access using direct register operations
59812a02d6eSMike Smith  */
599ac19f918SStefan Eßer 
6005bec6157SStefan Eßer /* enable configuration space accesses and return data port address */
601a3adc4f8SStefan Eßer static int
6025bec6157SStefan Eßer pci_cfgenable(unsigned bus, unsigned slot, unsigned func, int reg, int bytes)
6035bec6157SStefan Eßer {
6045bec6157SStefan Eßer 	int dataport = 0;
6055bec6157SStefan Eßer 
6065bec6157SStefan Eßer 	if (bus <= PCI_BUSMAX
6075bec6157SStefan Eßer 	    && slot < devmax
6085bec6157SStefan Eßer 	    && func <= PCI_FUNCMAX
6095bec6157SStefan Eßer 	    && reg <= PCI_REGMAX
6105bec6157SStefan Eßer 	    && bytes != 3
6115bec6157SStefan Eßer 	    && (unsigned) bytes <= 4
6125bec6157SStefan Eßer 	    && (reg & (bytes - 1)) == 0) {
6135bec6157SStefan Eßer 		switch (cfgmech) {
6145bec6157SStefan Eßer 		case 1:
615b3daa02eSStefan Eßer 			outl(CONF1_ADDR_PORT, (1 << 31)
616b3daa02eSStefan Eßer 			    | (bus << 16) | (slot << 11)
617b3daa02eSStefan Eßer 			    | (func << 8) | (reg & ~0x03));
618b3daa02eSStefan Eßer 			dataport = CONF1_DATA_PORT + (reg & 0x03);
6195bec6157SStefan Eßer 			break;
6205bec6157SStefan Eßer 		case 2:
6215bec6157SStefan Eßer 			outb(CONF2_ENABLE_PORT, 0xf0 | (func << 1));
6225bec6157SStefan Eßer 			outb(CONF2_FORWARD_PORT, bus);
6235bec6157SStefan Eßer 			dataport = 0xc000 | (slot << 8) | reg;
6245bec6157SStefan Eßer 			break;
6255bec6157SStefan Eßer 		}
6265bec6157SStefan Eßer 	}
6275bec6157SStefan Eßer 	return (dataport);
6285bec6157SStefan Eßer }
6295bec6157SStefan Eßer 
6305bec6157SStefan Eßer /* disable configuration space accesses */
6315bec6157SStefan Eßer static void
6325bec6157SStefan Eßer pci_cfgdisable(void)
6335bec6157SStefan Eßer {
6345bec6157SStefan Eßer 	switch (cfgmech) {
6355bec6157SStefan Eßer 	case 1:
6365bec6157SStefan Eßer 		outl(CONF1_ADDR_PORT, 0);
6375bec6157SStefan Eßer 		break;
6385bec6157SStefan Eßer 	case 2:
6395bec6157SStefan Eßer 		outb(CONF2_ENABLE_PORT, 0);
6405bec6157SStefan Eßer 		outb(CONF2_FORWARD_PORT, 0);
6415bec6157SStefan Eßer 		break;
6425bec6157SStefan Eßer 	}
6435bec6157SStefan Eßer }
6445bec6157SStefan Eßer 
645300451c4SMike Smith static int
64621c3015aSDoug Rabson pcireg_cfgread(int bus, int slot, int func, int reg, int bytes)
6475bec6157SStefan Eßer {
6485bec6157SStefan Eßer 	int data = -1;
6495bec6157SStefan Eßer 	int port;
6505bec6157SStefan Eßer 
651af3d516fSPeter Wemm 	mtx_lock_spin(&pcicfg_mtx);
65221c3015aSDoug Rabson 	port = pci_cfgenable(bus, slot, func, reg, bytes);
6535bec6157SStefan Eßer 	if (port != 0) {
6545bec6157SStefan Eßer 		switch (bytes) {
6555bec6157SStefan Eßer 		case 1:
6565bec6157SStefan Eßer 			data = inb(port);
6575bec6157SStefan Eßer 			break;
6585bec6157SStefan Eßer 		case 2:
6595bec6157SStefan Eßer 			data = inw(port);
6605bec6157SStefan Eßer 			break;
6615bec6157SStefan Eßer 		case 4:
6625bec6157SStefan Eßer 			data = inl(port);
6635bec6157SStefan Eßer 			break;
6645bec6157SStefan Eßer 		}
6655bec6157SStefan Eßer 		pci_cfgdisable();
6665bec6157SStefan Eßer 	}
667af3d516fSPeter Wemm 	mtx_unlock_spin(&pcicfg_mtx);
6685bec6157SStefan Eßer 	return (data);
6695bec6157SStefan Eßer }
6705bec6157SStefan Eßer 
671300451c4SMike Smith static void
67221c3015aSDoug Rabson pcireg_cfgwrite(int bus, int slot, int func, int reg, int data, int bytes)
6735bec6157SStefan Eßer {
6745bec6157SStefan Eßer 	int port;
6755bec6157SStefan Eßer 
676af3d516fSPeter Wemm 	mtx_lock_spin(&pcicfg_mtx);
67721c3015aSDoug Rabson 	port = pci_cfgenable(bus, slot, func, reg, bytes);
6785bec6157SStefan Eßer 	if (port != 0) {
6795bec6157SStefan Eßer 		switch (bytes) {
6805bec6157SStefan Eßer 		case 1:
6815bec6157SStefan Eßer 			outb(port, data);
6825bec6157SStefan Eßer 			break;
6835bec6157SStefan Eßer 		case 2:
6845bec6157SStefan Eßer 			outw(port, data);
6855bec6157SStefan Eßer 			break;
6865bec6157SStefan Eßer 		case 4:
6875bec6157SStefan Eßer 			outl(port, data);
6885bec6157SStefan Eßer 			break;
6895bec6157SStefan Eßer 		}
6905bec6157SStefan Eßer 		pci_cfgdisable();
6915bec6157SStefan Eßer 	}
692af3d516fSPeter Wemm 	mtx_unlock_spin(&pcicfg_mtx);
6935bec6157SStefan Eßer }
6945bec6157SStefan Eßer 
69512a02d6eSMike Smith /* check whether the configuration mechanism has been correctly identified */
6965bec6157SStefan Eßer static int
6975bec6157SStefan Eßer pci_cfgcheck(int maxdev)
698a3adc4f8SStefan Eßer {
699984de797SWarner Losh 	uint32_t id, class;
700984de797SWarner Losh 	uint8_t header;
701984de797SWarner Losh 	uint8_t device;
702af3d516fSPeter Wemm 	int port;
703a3adc4f8SStefan Eßer 
7045bec6157SStefan Eßer 	if (bootverbose)
7055bec6157SStefan Eßer 		printf("pci_cfgcheck:\tdevice ");
70677b57314SStefan Eßer 
7075bec6157SStefan Eßer 	for (device = 0; device < maxdev; device++) {
708c7483249SStefan Eßer 		if (bootverbose)
709c7483249SStefan Eßer 			printf("%d ", device);
7105bec6157SStefan Eßer 
711af3d516fSPeter Wemm 		port = pci_cfgenable(0, device, 0, 0, 4);
712af3d516fSPeter Wemm 		id = inl(port);
713984de797SWarner Losh 		if (id == 0 || id == 0xffffffff)
71481cf5d7aSStefan Eßer 			continue;
71581cf5d7aSStefan Eßer 
716af3d516fSPeter Wemm 		port = pci_cfgenable(0, device, 0, 8, 4);
717af3d516fSPeter Wemm 		class = inl(port) >> 8;
71881cf5d7aSStefan Eßer 		if (bootverbose)
7195bec6157SStefan Eßer 			printf("[class=%06x] ", class);
7208277ac25SStefan Eßer 		if (class == 0 || (class & 0xf870ff) != 0)
72181cf5d7aSStefan Eßer 			continue;
72281cf5d7aSStefan Eßer 
723af3d516fSPeter Wemm 		port = pci_cfgenable(0, device, 0, 14, 1);
724af3d516fSPeter Wemm 		header = inb(port);
72581cf5d7aSStefan Eßer 		if (bootverbose)
7265bec6157SStefan Eßer 			printf("[hdr=%02x] ", header);
7275bec6157SStefan Eßer 		if ((header & 0x7e) != 0)
72881cf5d7aSStefan Eßer 			continue;
72981cf5d7aSStefan Eßer 
7305bec6157SStefan Eßer 		if (bootverbose)
7315bec6157SStefan Eßer 			printf("is there (id=%08x)\n", id);
7325bec6157SStefan Eßer 
7335bec6157SStefan Eßer 		pci_cfgdisable();
7345bec6157SStefan Eßer 		return (1);
735a3adc4f8SStefan Eßer 	}
736c7483249SStefan Eßer 	if (bootverbose)
737c7483249SStefan Eßer 		printf("-- nothing found\n");
7385bec6157SStefan Eßer 
7395bec6157SStefan Eßer 	pci_cfgdisable();
7405bec6157SStefan Eßer 	return (0);
741a3adc4f8SStefan Eßer }
742d7ea35fcSStefan Eßer 
7438dc26439SPeter Wemm static int
744300451c4SMike Smith pcireg_cfgopen(void)
745ac19f918SStefan Eßer {
746984de797SWarner Losh 	uint32_t mode1res, oldval1;
747984de797SWarner Losh 	uint8_t mode2res, oldval2;
7480847c06dSStefan Eßer 
749287911bdSStefan Eßer 	oldval1 = inl(CONF1_ADDR_PORT);
750a3adc4f8SStefan Eßer 
75177b57314SStefan Eßer 	if (bootverbose) {
752984de797SWarner Losh 		printf("pci_open(1):\tmode 1 addr port (0x0cf8) is 0x%08x\n",
7535bec6157SStefan Eßer 		    oldval1);
754a3adc4f8SStefan Eßer 	}
755a3adc4f8SStefan Eßer 
7560e2f699bSStefan Eßer 	if ((oldval1 & CONF1_ENABLE_MSK) == 0) {
757287911bdSStefan Eßer 
7585bec6157SStefan Eßer 		cfgmech = 1;
7595bec6157SStefan Eßer 		devmax = 32;
76077b57314SStefan Eßer 
76177b57314SStefan Eßer 		outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK);
76277b57314SStefan Eßer 		outb(CONF1_ADDR_PORT + 3, 0);
76377b57314SStefan Eßer 		mode1res = inl(CONF1_ADDR_PORT);
764287911bdSStefan Eßer 		outl(CONF1_ADDR_PORT, oldval1);
76577b57314SStefan Eßer 
76677b57314SStefan Eßer 		if (bootverbose)
767984de797SWarner Losh 			printf("pci_open(1a):\tmode1res=0x%08x (0x%08lx)\n",
76877b57314SStefan Eßer 			    mode1res, CONF1_ENABLE_CHK);
76977b57314SStefan Eßer 
77077b57314SStefan Eßer 		if (mode1res) {
7715bec6157SStefan Eßer 			if (pci_cfgcheck(32))
7725bec6157SStefan Eßer 				return (cfgmech);
7735bec6157SStefan Eßer 		}
77477b57314SStefan Eßer 
77577b57314SStefan Eßer 		outl(CONF1_ADDR_PORT, CONF1_ENABLE_CHK1);
77677b57314SStefan Eßer 		mode1res = inl(CONF1_ADDR_PORT);
777287911bdSStefan Eßer 		outl(CONF1_ADDR_PORT, oldval1);
77877b57314SStefan Eßer 
77977b57314SStefan Eßer 		if (bootverbose)
780984de797SWarner Losh 			printf("pci_open(1b):\tmode1res=0x%08x (0x%08lx)\n",
78177b57314SStefan Eßer 			    mode1res, CONF1_ENABLE_CHK1);
78277b57314SStefan Eßer 
783c7483249SStefan Eßer 		if ((mode1res & CONF1_ENABLE_MSK1) == CONF1_ENABLE_RES1) {
7845bec6157SStefan Eßer 			if (pci_cfgcheck(32))
7855bec6157SStefan Eßer 				return (cfgmech);
786287911bdSStefan Eßer 		}
7875bec6157SStefan Eßer 	}
78877b57314SStefan Eßer 
789287911bdSStefan Eßer 	oldval2 = inb(CONF2_ENABLE_PORT);
790287911bdSStefan Eßer 
791287911bdSStefan Eßer 	if (bootverbose) {
7925bec6157SStefan Eßer 		printf("pci_open(2):\tmode 2 enable port (0x0cf8) is 0x%02x\n",
7935bec6157SStefan Eßer 		    oldval2);
794287911bdSStefan Eßer 	}
795287911bdSStefan Eßer 
796287911bdSStefan Eßer 	if ((oldval2 & 0xf0) == 0) {
797c7483249SStefan Eßer 
7985bec6157SStefan Eßer 		cfgmech = 2;
7995bec6157SStefan Eßer 		devmax = 16;
80077b57314SStefan Eßer 
801287911bdSStefan Eßer 		outb(CONF2_ENABLE_PORT, CONF2_ENABLE_CHK);
802287911bdSStefan Eßer 		mode2res = inb(CONF2_ENABLE_PORT);
803287911bdSStefan Eßer 		outb(CONF2_ENABLE_PORT, oldval2);
804287911bdSStefan Eßer 
805287911bdSStefan Eßer 		if (bootverbose)
8065bec6157SStefan Eßer 			printf("pci_open(2a):\tmode2res=0x%02x (0x%02x)\n",
807287911bdSStefan Eßer 			    mode2res, CONF2_ENABLE_CHK);
808287911bdSStefan Eßer 
809287911bdSStefan Eßer 		if (mode2res == CONF2_ENABLE_RES) {
810287911bdSStefan Eßer 			if (bootverbose)
8115bec6157SStefan Eßer 				printf("pci_open(2a):\tnow trying mechanism 2\n");
812287911bdSStefan Eßer 
8135bec6157SStefan Eßer 			if (pci_cfgcheck(16))
8145bec6157SStefan Eßer 				return (cfgmech);
815287911bdSStefan Eßer 		}
816287911bdSStefan Eßer 	}
81777b57314SStefan Eßer 
8185bec6157SStefan Eßer 	cfgmech = 0;
8195bec6157SStefan Eßer 	devmax = 0;
8205bec6157SStefan Eßer 	return (cfgmech);
821ac19f918SStefan Eßer }
8228dc26439SPeter Wemm 
823