xref: /openbsd-src/sys/arch/amd64/pci/pci_machdep.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: pci_machdep.c,v 1.65 2016/06/02 21:01:51 kettenis Exp $	*/
2 /*	$NetBSD: pci_machdep.c,v 1.3 2003/05/07 21:33:58 fvdl Exp $	*/
3 
4 /*-
5  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
36  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by Charles M. Hannum.
49  * 4. The name of the author may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 /*
65  * Machine-specific functions for PCI autoconfiguration.
66  */
67 
68 #include <sys/types.h>
69 #include <sys/param.h>
70 #include <sys/time.h>
71 #include <sys/systm.h>
72 #include <sys/errno.h>
73 #include <sys/device.h>
74 #include <sys/extent.h>
75 #include <sys/malloc.h>
76 
77 #include <uvm/uvm_extern.h>
78 
79 #include <machine/bus.h>
80 
81 #include <machine/pio.h>
82 #include <machine/intr.h>
83 #include <machine/biosvar.h>
84 
85 #include <dev/isa/isareg.h>
86 #include <dev/pci/pcivar.h>
87 #include <dev/pci/pcireg.h>
88 #include <dev/pci/pcidevs.h>
89 #include <dev/pci/ppbreg.h>
90 
91 #include "ioapic.h"
92 
93 #if NIOAPIC > 0
94 #include <machine/i82093var.h>
95 #include <machine/mpbiosvar.h>
96 #endif
97 
98 /*
99  * Memory Mapped Configuration space access.
100  *
101  * Since mapping the whole configuration space will cost us up to
102  * 256MB of kernel virtual memory, we use seperate mappings per bus.
103  * The mappings are created on-demand, such that we only use kernel
104  * virtual memory for busses that are actually present.
105  */
106 bus_addr_t pci_mcfg_addr;
107 int pci_mcfg_min_bus, pci_mcfg_max_bus;
108 bus_space_tag_t pci_mcfgt = X86_BUS_SPACE_MEM;
109 bus_space_handle_t pci_mcfgh[256];
110 void pci_mcfg_map_bus(int);
111 
112 struct mutex pci_conf_lock = MUTEX_INITIALIZER(IPL_HIGH);
113 
114 #define	PCI_CONF_LOCK()						\
115 do {									\
116 	mtx_enter(&pci_conf_lock);					\
117 } while (0)
118 
119 #define	PCI_CONF_UNLOCK()						\
120 do {									\
121 	mtx_leave(&pci_conf_lock);					\
122 } while (0)
123 
124 #define	PCI_MODE1_ENABLE	0x80000000UL
125 #define	PCI_MODE1_ADDRESS_REG	0x0cf8
126 #define	PCI_MODE1_DATA_REG	0x0cfc
127 
128 /*
129  * PCI doesn't have any special needs; just use the generic versions
130  * of these functions.
131  */
132 struct bus_dma_tag pci_bus_dma_tag = {
133 	NULL,			/* _may_bounce */
134 	_bus_dmamap_create,
135 	_bus_dmamap_destroy,
136 	_bus_dmamap_load,
137 	_bus_dmamap_load_mbuf,
138 	_bus_dmamap_load_uio,
139 	_bus_dmamap_load_raw,
140 	_bus_dmamap_unload,
141 	_bus_dmamap_sync,
142 	_bus_dmamem_alloc,
143 	_bus_dmamem_alloc_range,
144 	_bus_dmamem_free,
145 	_bus_dmamem_map,
146 	_bus_dmamem_unmap,
147 	_bus_dmamem_mmap,
148 };
149 
150 void
151 pci_attach_hook(struct device *parent, struct device *self,
152     struct pcibus_attach_args *pba)
153 {
154 	pci_chipset_tag_t pc = pba->pba_pc;
155 	pcitag_t tag;
156 	pcireg_t id, class;
157 
158 	if (pba->pba_bus != 0)
159 		return;
160 
161 	/*
162 	 * In order to decide whether the system supports MSI we look
163 	 * at the host bridge, which should be device 0 function 0 on
164 	 * bus 0.  It is better to not enable MSI on systems that
165 	 * support it than the other way around, so be conservative
166 	 * here.  So we don't enable MSI if we don't find a host
167 	 * bridge there.  We also deliberately don't enable MSI on
168 	 * chipsets from low-end manifacturers like VIA and SiS.
169 	 */
170 	tag = pci_make_tag(pc, 0, 0, 0);
171 	id = pci_conf_read(pc, tag, PCI_ID_REG);
172 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
173 
174 	if (PCI_CLASS(class) != PCI_CLASS_BRIDGE ||
175 	    PCI_SUBCLASS(class) != PCI_SUBCLASS_BRIDGE_HOST)
176 		return;
177 
178 	switch (PCI_VENDOR(id)) {
179 	case PCI_VENDOR_INTEL:
180 		/*
181 		 * In the wonderful world of virtualization you can
182 		 * have the latest 64-bit AMD multicore CPU behind a
183 		 * prehistoric Intel host bridge.  Give them what they
184 		 * deserve.
185 		 */
186 		switch (PCI_PRODUCT(id)) {
187 		case PCI_PRODUCT_INTEL_82441FX:	/* QEMU */
188 		case PCI_PRODUCT_INTEL_82443BX:	/* VMWare */
189 			break;
190 		default:
191 			pba->pba_flags |= PCI_FLAGS_MSI_ENABLED;
192 			break;
193 		}
194 		break;
195 	case PCI_VENDOR_NVIDIA:
196 	case PCI_VENDOR_AMD:
197 		pba->pba_flags |= PCI_FLAGS_MSI_ENABLED;
198 		break;
199 	}
200 
201 	/*
202 	 * Don't enable MSI on a HyperTransport bus.  In order to
203 	 * determine that bus 0 is a HyperTransport bus, we look at
204 	 * device 24 function 0, which is the HyperTransport
205 	 * host/primary interface integrated on most 64-bit AMD CPUs.
206 	 * If that device has a HyperTransport capability, bus 0 must
207 	 * be a HyperTransport bus and we disable MSI.
208 	 */
209 	tag = pci_make_tag(pc, 0, 24, 0);
210 	if (pci_get_capability(pc, tag, PCI_CAP_HT, NULL, NULL))
211 		pba->pba_flags &= ~PCI_FLAGS_MSI_ENABLED;
212 }
213 
214 int
215 pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
216 {
217 	return (32);
218 }
219 
220 pcitag_t
221 pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
222 {
223 	if (bus >= 256 || device >= 32 || function >= 8)
224 		panic("pci_make_tag: bad request");
225 
226 	return (PCI_MODE1_ENABLE |
227 	    (bus << 16) | (device << 11) | (function << 8));
228 }
229 
230 void
231 pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp, int *fp)
232 {
233 	if (bp != NULL)
234 		*bp = (tag >> 16) & 0xff;
235 	if (dp != NULL)
236 		*dp = (tag >> 11) & 0x1f;
237 	if (fp != NULL)
238 		*fp = (tag >> 8) & 0x7;
239 }
240 
241 int
242 pci_conf_size(pci_chipset_tag_t pc, pcitag_t tag)
243 {
244 	int bus;
245 
246 	if (pci_mcfg_addr) {
247 		pci_decompose_tag(pc, tag, &bus, NULL, NULL);
248 		if (bus >= pci_mcfg_min_bus && bus <= pci_mcfg_max_bus)
249 			return PCIE_CONFIG_SPACE_SIZE;
250 	}
251 
252 	return PCI_CONFIG_SPACE_SIZE;
253 }
254 
255 void
256 pci_mcfg_map_bus(int bus)
257 {
258 	if (pci_mcfgh[bus])
259 		return;
260 
261 	if (bus_space_map(pci_mcfgt, pci_mcfg_addr + (bus << 20), 1 << 20,
262 	    0, &pci_mcfgh[bus]))
263 		panic("pci_conf_read: cannot map mcfg space");
264 }
265 
266 pcireg_t
267 pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
268 {
269 	pcireg_t data;
270 	int bus;
271 
272 	KASSERT((reg & 0x3) == 0);
273 
274 	if (pci_mcfg_addr && reg >= PCI_CONFIG_SPACE_SIZE) {
275 		pci_decompose_tag(pc, tag, &bus, NULL, NULL);
276 		if (bus >= pci_mcfg_min_bus && bus <= pci_mcfg_max_bus) {
277 			pci_mcfg_map_bus(bus);
278 			data = bus_space_read_4(pci_mcfgt, pci_mcfgh[bus],
279 			    (tag & 0x000ff00) << 4 | reg);
280 			return data;
281 		}
282 	}
283 
284 	PCI_CONF_LOCK();
285 	outl(PCI_MODE1_ADDRESS_REG, tag | reg);
286 	data = inl(PCI_MODE1_DATA_REG);
287 	outl(PCI_MODE1_ADDRESS_REG, 0);
288 	PCI_CONF_UNLOCK();
289 
290 	return data;
291 }
292 
293 void
294 pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
295 {
296 	int bus;
297 
298 	KASSERT((reg & 0x3) == 0);
299 
300 	if (pci_mcfg_addr && reg >= PCI_CONFIG_SPACE_SIZE) {
301 		pci_decompose_tag(pc, tag, &bus, NULL, NULL);
302 		if (bus >= pci_mcfg_min_bus && bus <= pci_mcfg_max_bus) {
303 			pci_mcfg_map_bus(bus);
304 			bus_space_write_4(pci_mcfgt, pci_mcfgh[bus],
305 			    (tag & 0x000ff00) << 4 | reg, data);
306 			return;
307 		}
308 	}
309 
310 	PCI_CONF_LOCK();
311 	outl(PCI_MODE1_ADDRESS_REG, tag | reg);
312 	outl(PCI_MODE1_DATA_REG, data);
313 	outl(PCI_MODE1_ADDRESS_REG, 0);
314 	PCI_CONF_UNLOCK();
315 }
316 
317 void msi_hwmask(struct pic *, int);
318 void msi_hwunmask(struct pic *, int);
319 void msi_addroute(struct pic *, struct cpu_info *, int, int, int);
320 void msi_delroute(struct pic *, struct cpu_info *, int, int, int);
321 
322 struct pic msi_pic = {
323 	{0, {NULL}, NULL, 0, "msi", NULL, 0, 0},
324 	PIC_MSI,
325 #ifdef MULTIPROCESSOR
326 	{},
327 #endif
328 	msi_hwmask,
329 	msi_hwunmask,
330 	msi_addroute,
331 	msi_delroute,
332 	NULL,
333 	ioapic_edge_stubs
334 };
335 
336 void
337 msi_hwmask(struct pic *pic, int pin)
338 {
339 }
340 
341 void
342 msi_hwunmask(struct pic *pic, int pin)
343 {
344 }
345 
346 void
347 msi_addroute(struct pic *pic, struct cpu_info *ci, int pin, int vec, int type)
348 {
349 	pci_chipset_tag_t pc = NULL; /* XXX */
350 	pcitag_t tag = pin;
351 	pcireg_t reg, addr;
352 	int off;
353 
354 	if (pci_get_capability(pc, tag, PCI_CAP_MSI, &off, &reg) == 0)
355 		panic("%s: no msi capability", __func__);
356 
357 	addr = 0xfee00000UL | (ci->ci_apicid << 12);
358 
359 	if (reg & PCI_MSI_MC_C64) {
360 		pci_conf_write(pc, tag, off + PCI_MSI_MA, addr);
361 		pci_conf_write(pc, tag, off + PCI_MSI_MAU32, 0);
362 		pci_conf_write(pc, tag, off + PCI_MSI_MD64, vec);
363 	} else {
364 		pci_conf_write(pc, tag, off + PCI_MSI_MA, addr);
365 		pci_conf_write(pc, tag, off + PCI_MSI_MD32, vec);
366 	}
367 	pci_conf_write(pc, tag, off, reg | PCI_MSI_MC_MSIE);
368 }
369 
370 void
371 msi_delroute(struct pic *pic, struct cpu_info *ci, int pin, int vec, int type)
372 {
373 	pci_chipset_tag_t pc = NULL; /* XXX */
374 	pcitag_t tag = pin;
375 	pcireg_t reg;
376 	int off;
377 
378 	if (pci_get_capability(pc, tag, PCI_CAP_MSI, &off, &reg))
379 		pci_conf_write(pc, tag, off, reg & ~PCI_MSI_MC_MSIE);
380 }
381 
382 int
383 pci_intr_map_msi(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
384 {
385 	pci_chipset_tag_t pc = pa->pa_pc;
386 	pcitag_t tag = pa->pa_tag;
387 
388 	if ((pa->pa_flags & PCI_FLAGS_MSI_ENABLED) == 0 || mp_busses == NULL ||
389 	    pci_get_capability(pc, tag, PCI_CAP_MSI, NULL, NULL) == 0)
390 		return 1;
391 
392 	ihp->tag = tag;
393 	ihp->line = APIC_INT_VIA_MSG;
394 	ihp->pin = 0;
395 	return 0;
396 }
397 
398 void msix_hwmask(struct pic *, int);
399 void msix_hwunmask(struct pic *, int);
400 void msix_addroute(struct pic *, struct cpu_info *, int, int, int);
401 void msix_delroute(struct pic *, struct cpu_info *, int, int, int);
402 
403 struct pic msix_pic = {
404 	{0, {NULL}, NULL, 0, "msix", NULL, 0, 0},
405 	PIC_MSI,
406 #ifdef MULTIPROCESSOR
407 	{},
408 #endif
409 	msix_hwmask,
410 	msix_hwunmask,
411 	msix_addroute,
412 	msix_delroute,
413 	NULL,
414 	ioapic_edge_stubs
415 };
416 
417 /*
418  * We pack the MSI-X vector number into the lower 8 bits of the PCI
419  * tag and use that as the MSI-X "PIC" pin number.  This allows us to
420  * address 256 MSI-X vectors which ought to be enough for anybody.
421  */
422 #define PCI_MSIX_VEC_MASK	0xff
423 #define PCI_MSIX_VEC(pin)	((pin) & PCI_MSIX_VEC_MASK)
424 #define PCI_MSIX_TAG(pin)	((pin) & ~PCI_MSIX_VEC_MASK)
425 #define PCI_MSIX_PIN(tag, vec)	((tag) | (vec))
426 
427 void
428 msix_hwmask(struct pic *pic, int pin)
429 {
430 }
431 
432 void
433 msix_hwunmask(struct pic *pic, int pin)
434 {
435 }
436 
437 void
438 msix_addroute(struct pic *pic, struct cpu_info *ci, int pin, int vec, int type)
439 {
440 	pci_chipset_tag_t pc = NULL; /* XXX */
441 	bus_space_tag_t memt = X86_BUS_SPACE_MEM; /* XXX */
442 	bus_space_handle_t memh;
443 	bus_addr_t base;
444 	pcitag_t tag = PCI_MSIX_TAG(pin);
445 	int entry = PCI_MSIX_VEC(pin);
446 	pcireg_t reg, addr, table;
447 	uint32_t ctrl;
448 	int bir, offset;
449 	int off, tblsz;
450 
451 	if (pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, &reg) == 0)
452 		panic("%s: no msix capability", __func__);
453 
454 	addr = 0xfee00000UL | (ci->ci_apicid << 12);
455 
456 	table = pci_conf_read(pc, tag, off + PCI_MSIX_TABLE);
457 	bir = (table & PCI_MSIX_TABLE_BIR);
458 	offset = (table & PCI_MSIX_TABLE_OFF);
459 	tblsz = PCI_MSIX_MC_TBLSZ(reg) + 1;
460 
461 	bir = PCI_MAPREG_START + bir * 4;
462 	if (pci_mem_find(pc, tag, bir, &base, NULL, NULL) ||
463 	    _bus_space_map(memt, base + offset, tblsz * 16, 0, &memh))
464 		panic("%s: cannot map registers", __func__);
465 
466 	bus_space_write_8(memt, memh, PCI_MSIX_MA(entry), addr);
467 	bus_space_write_4(memt, memh, PCI_MSIX_MD(entry), vec);
468 	bus_space_barrier(memt, memh, PCI_MSIX_MA(entry), 16,
469 	    BUS_SPACE_BARRIER_WRITE);
470 	ctrl = bus_space_read_4(memt, memh, PCI_MSIX_VC(entry));
471 	bus_space_write_4(memt, memh, PCI_MSIX_VC(entry),
472 	    ctrl & ~PCI_MSIX_VC_MASK);
473 
474 	_bus_space_unmap(memt, memh, tblsz * 16, NULL);
475 
476 	pci_conf_write(pc, tag, off, reg | PCI_MSIX_MC_MSIXE);
477 }
478 
479 void
480 msix_delroute(struct pic *pic, struct cpu_info *ci, int pin, int vec, int type)
481 {
482 	pci_chipset_tag_t pc = NULL; /* XXX */
483 	bus_space_tag_t memt = X86_BUS_SPACE_MEM; /* XXX */
484 	bus_space_handle_t memh;
485 	bus_addr_t base;
486 	pcitag_t tag = PCI_MSIX_TAG(pin);
487 	int entry = PCI_MSIX_VEC(pin);
488 	pcireg_t reg, table;
489 	uint32_t ctrl;
490 	int bir, offset;
491 	int off, tblsz;
492 
493 	if (pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, &reg) == 0)
494 		return;
495 
496 	table = pci_conf_read(pc, tag, off + PCI_MSIX_TABLE);
497 	bir = (table & PCI_MSIX_TABLE_BIR);
498 	offset = (table & PCI_MSIX_TABLE_OFF);
499 	tblsz = PCI_MSIX_MC_TBLSZ(reg) + 1;
500 
501 	bir = PCI_MAPREG_START + bir * 4;
502 	if (pci_mem_find(pc, tag, bir, &base, NULL, NULL) ||
503 	    _bus_space_map(memt, base + offset, tblsz * 16, 0, &memh))
504 		panic("%s: cannot map registers", __func__);
505 
506 	ctrl = bus_space_read_4(memt, memh, PCI_MSIX_VC(entry));
507 	bus_space_write_4(memt, memh, PCI_MSIX_VC(entry),
508 	    ctrl | PCI_MSIX_VC_MASK);
509 
510 	_bus_space_unmap(memt, memh, tblsz * 16, NULL);
511 }
512 
513 int
514 pci_intr_map_msix(struct pci_attach_args *pa, int vec, pci_intr_handle_t *ihp)
515 {
516 	pci_chipset_tag_t pc = pa->pa_pc;
517 	pcitag_t tag = pa->pa_tag;
518 	pcireg_t reg;
519 
520 	KASSERT(PCI_MSIX_VEC(vec) == vec);
521 
522 	if ((pa->pa_flags & PCI_FLAGS_MSI_ENABLED) == 0 || mp_busses == NULL ||
523 	    pci_get_capability(pc, tag, PCI_CAP_MSIX, NULL, &reg) == 0)
524 		return 1;
525 
526 	if (vec > PCI_MSIX_MC_TBLSZ(reg))
527 		return 1;
528 
529 	ihp->tag = PCI_MSIX_PIN(tag, vec);
530 	ihp->line = APIC_INT_VIA_MSGX;
531 	ihp->pin = 0;
532 	return 0;
533 }
534 
535 int
536 pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
537 {
538 	int pin = pa->pa_rawintrpin;
539 	int line = pa->pa_intrline;
540 #if NIOAPIC > 0
541 	struct mp_intr_map *mip;
542 	int bus, dev, func;
543 #endif
544 
545 	if (pin == 0) {
546 		/* No IRQ used. */
547 		goto bad;
548 	}
549 
550 	if (pin > PCI_INTERRUPT_PIN_MAX) {
551 		printf("pci_intr_map: bad interrupt pin %d\n", pin);
552 		goto bad;
553 	}
554 
555 	ihp->tag = pa->pa_tag;
556 	ihp->line = line;
557 	ihp->pin = pin;
558 
559 #if NIOAPIC > 0
560 	pci_decompose_tag(pa->pa_pc, pa->pa_tag, &bus, &dev, &func);
561 
562 	if (mp_busses != NULL) {
563 		int mpspec_pin = (dev << 2) | (pin - 1);
564 
565 		if (bus < mp_nbusses) {
566 			for (mip = mp_busses[bus].mb_intrs;
567 			     mip != NULL; mip = mip->next) {
568 				if (&mp_busses[bus] == mp_isa_bus ||
569 				    &mp_busses[bus] == mp_eisa_bus)
570 					continue;
571 				if (mip->bus_pin == mpspec_pin) {
572 					ihp->line = mip->ioapic_ih | line;
573 					return 0;
574 				}
575 			}
576 		}
577 
578 		if (pa->pa_bridgetag) {
579 			int swizpin = PPB_INTERRUPT_SWIZZLE(pin, dev);
580 			if (pa->pa_bridgeih[swizpin - 1].line != -1) {
581 				ihp->line = pa->pa_bridgeih[swizpin - 1].line;
582 				ihp->line |= line;
583 				return 0;
584 			}
585 		}
586 		/*
587 		 * No explicit PCI mapping found. This is not fatal,
588 		 * we'll try the ISA (or possibly EISA) mappings next.
589 		 */
590 	}
591 #endif
592 
593 	/*
594 	 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
595 	 * `unknown' or `no connection' on a PC.  We assume that a device with
596 	 * `no connection' either doesn't have an interrupt (in which case the
597 	 * pin number should be 0, and would have been noticed above), or
598 	 * wasn't configured by the BIOS (in which case we punt, since there's
599 	 * no real way we can know how the interrupt lines are mapped in the
600 	 * hardware).
601 	 *
602 	 * XXX
603 	 * Since IRQ 0 is only used by the clock, and we can't actually be sure
604 	 * that the BIOS did its job, we also recognize that as meaning that
605 	 * the BIOS has not configured the device.
606 	 */
607 	if (line == 0 || line == X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
608 		goto bad;
609 
610 	if (line >= NUM_LEGACY_IRQS) {
611 		printf("pci_intr_map: bad interrupt line %d\n", line);
612 		goto bad;
613 	}
614 	if (line == 2) {
615 		printf("pci_intr_map: changed line 2 to line 9\n");
616 		line = 9;
617 	}
618 
619 #if NIOAPIC > 0
620 	if (mp_busses != NULL) {
621 		if (mip == NULL && mp_isa_bus) {
622 			for (mip = mp_isa_bus->mb_intrs; mip != NULL;
623 			    mip = mip->next) {
624 				if (mip->bus_pin == line) {
625 					ihp->line = mip->ioapic_ih | line;
626 					return 0;
627 				}
628 			}
629 		}
630 #if NEISA > 0
631 		if (mip == NULL && mp_eisa_bus) {
632 			for (mip = mp_eisa_bus->mb_intrs;  mip != NULL;
633 			    mip = mip->next) {
634 				if (mip->bus_pin == line) {
635 					ihp->line = mip->ioapic_ih | line;
636 					return 0;
637 				}
638 			}
639 		}
640 #endif
641 		if (mip == NULL) {
642 			printf("pci_intr_map: "
643 			    "bus %d dev %d func %d pin %d; line %d\n",
644 			    bus, dev, func, pin, line);
645 			printf("pci_intr_map: no MP mapping found\n");
646 		}
647 	}
648 #endif
649 
650 	return 0;
651 
652 bad:
653 	ihp->line = -1;
654 	return 1;
655 }
656 
657 const char *
658 pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih)
659 {
660 	static char irqstr[64];
661 
662 	if (ih.line == 0)
663 		panic("pci_intr_string: bogus handle 0x%x", ih.line);
664 
665 	if (ih.line & APIC_INT_VIA_MSG)
666 		return ("msi");
667 	if (ih.line & APIC_INT_VIA_MSGX)
668 		return ("msix");
669 
670 #if NIOAPIC > 0
671 	if (ih.line & APIC_INT_VIA_APIC)
672 		snprintf(irqstr, sizeof(irqstr), "apic %d int %d",
673 		    APIC_IRQ_APIC(ih.line), APIC_IRQ_PIN(ih.line));
674 	else
675 		snprintf(irqstr, sizeof(irqstr), "irq %d",
676 		    pci_intr_line(pc, ih));
677 #else
678 	snprintf(irqstr, sizeof(irqstr), "irq %d", pci_intr_line(pc, ih));
679 #endif
680 	return (irqstr);
681 }
682 
683 #include "acpiprt.h"
684 #if NACPIPRT > 0
685 void	acpiprt_route_interrupt(int bus, int dev, int pin);
686 #endif
687 
688 void *
689 pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
690     int (*func)(void *), void *arg, const char *what)
691 {
692 	int pin, irq;
693 	int bus, dev;
694 	pcitag_t tag = ih.tag;
695 	struct pic *pic;
696 
697 	if (ih.line & APIC_INT_VIA_MSG) {
698 		return intr_establish(-1, &msi_pic, tag, IST_PULSE, level,
699 		    func, arg, what);
700 	}
701 	if (ih.line & APIC_INT_VIA_MSGX) {
702 		return intr_establish(-1, &msix_pic, tag, IST_PULSE, level,
703 		    func, arg, what);
704 	}
705 
706 	pci_decompose_tag(pc, ih.tag, &bus, &dev, NULL);
707 #if NACPIPRT > 0
708 	acpiprt_route_interrupt(bus, dev, ih.pin);
709 #endif
710 
711 	pic = &i8259_pic;
712 	pin = irq = ih.line;
713 
714 #if NIOAPIC > 0
715 	if (ih.line & APIC_INT_VIA_APIC) {
716 		pic = (struct pic *)ioapic_find(APIC_IRQ_APIC(ih.line));
717 		if (pic == NULL) {
718 			printf("pci_intr_establish: bad ioapic %d\n",
719 			    APIC_IRQ_APIC(ih.line));
720 			return NULL;
721 		}
722 		pin = APIC_IRQ_PIN(ih.line);
723 		irq = APIC_IRQ_LEGACY_IRQ(ih.line);
724 		if (irq < 0 || irq >= NUM_LEGACY_IRQS)
725 			irq = -1;
726 	}
727 #endif
728 
729 	return intr_establish(irq, pic, pin, IST_LEVEL, level, func, arg, what);
730 }
731 
732 void
733 pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
734 {
735 	intr_disestablish(cookie);
736 }
737 
738 struct extent *pciio_ex;
739 struct extent *pcimem_ex;
740 struct extent *pcibus_ex;
741 
742 void
743 pci_init_extents(void)
744 {
745 	bios_memmap_t *bmp;
746 	u_int64_t size;
747 
748 	if (pciio_ex == NULL) {
749 		/*
750 		 * We only have 64K of addressable I/O space.
751 		 * However, since BARs may contain garbage, we cover
752 		 * the full 32-bit address space defined by PCI of
753 		 * which we only make the first 64K available.
754 		 */
755 		pciio_ex = extent_create("pciio", 0, 0xffffffff, M_DEVBUF,
756 		    NULL, 0, EX_NOWAIT | EX_FILLED);
757 		if (pciio_ex == NULL)
758 			return;
759 		extent_free(pciio_ex, 0, 0x10000, EX_NOWAIT);
760 	}
761 
762 	if (pcimem_ex == NULL) {
763 		/*
764 		 * Cover the 36-bit address space addressable by PAE
765 		 * here.  As long as vendors continue to support
766 		 * 32-bit operating systems, we should never see BARs
767 		 * outside that region.
768 		 *
769 		 * Dell 13G servers have important devices outside the
770 		 * 36-bit address space.  Until we can extract the address
771 		 * ranges from ACPI, expand the allowed range to suit.
772 		 */
773 		pcimem_ex = extent_create("pcimem", 0, 0xffffffffffffffffUL,
774 		    M_DEVBUF, NULL, 0, EX_NOWAIT);
775 		if (pcimem_ex == NULL)
776 			return;
777 		extent_alloc_region(pcimem_ex, 0x40000000000UL,
778 		    0xfffffc0000000000UL, EX_NOWAIT);
779 
780 		for (bmp = bios_memmap; bmp->type != BIOS_MAP_END; bmp++) {
781 			/*
782 			 * Ignore address space beyond 4G.
783 			 */
784 			if (bmp->addr >= 0x100000000ULL)
785 				continue;
786 			size = bmp->size;
787 			if (bmp->addr + size >= 0x100000000ULL)
788 				size = 0x100000000ULL - bmp->addr;
789 
790 			/* Ignore zero-sized regions. */
791 			if (size == 0)
792 				continue;
793 
794 			if (extent_alloc_region(pcimem_ex, bmp->addr, size,
795 			    EX_NOWAIT))
796 				printf("memory map conflict 0x%llx/0x%llx\n",
797 				    bmp->addr, bmp->size);
798 		}
799 
800 		/* Take out the video buffer area and BIOS areas. */
801 		extent_alloc_region(pcimem_ex, IOM_BEGIN, IOM_SIZE,
802 		    EX_CONFLICTOK | EX_NOWAIT);
803 	}
804 
805 	if (pcibus_ex == NULL) {
806 		pcibus_ex = extent_create("pcibus", 0, 0xff, M_DEVBUF,
807 		    NULL, 0, EX_NOWAIT);
808 	}
809 }
810 
811 #include "acpi.h"
812 #if NACPI > 0
813 void acpi_pci_match(struct device *, struct pci_attach_args *);
814 pcireg_t acpi_pci_min_powerstate(pci_chipset_tag_t, pcitag_t);
815 void acpi_pci_set_powerstate(pci_chipset_tag_t, pcitag_t, int, int);
816 #endif
817 
818 void
819 pci_dev_postattach(struct device *dev, struct pci_attach_args *pa)
820 {
821 #if NACPI > 0
822 	acpi_pci_match(dev, pa);
823 #endif
824 }
825 
826 pcireg_t
827 pci_min_powerstate(pci_chipset_tag_t pc, pcitag_t tag)
828 {
829 #if NACPI > 0
830 	return acpi_pci_min_powerstate(pc, tag);
831 #else
832 	return pci_get_powerstate(pc, tag);
833 #endif
834 }
835 
836 void
837 pci_set_powerstate_md(pci_chipset_tag_t pc, pcitag_t tag, int state, int pre)
838 {
839 #if NACPI > 0
840 	acpi_pci_set_powerstate(pc, tag, state, pre);
841 #endif
842 }
843