Home | History | Annotate | Line # | Download | only in pci
pci_intr_machdep.c revision 1.26.2.1
      1  1.26.2.1     rmind /*	$NetBSD: pci_intr_machdep.c,v 1.26.2.1 2014/05/18 17:45:30 rmind Exp $	*/
      2       1.1    bouyer 
      3       1.1    bouyer /*-
      4      1.13        ad  * Copyright (c) 1997, 1998, 2009 The NetBSD Foundation, Inc.
      5       1.1    bouyer  * All rights reserved.
      6       1.1    bouyer  *
      7       1.1    bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1    bouyer  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9       1.1    bouyer  * NASA Ames Research Center.
     10       1.1    bouyer  *
     11       1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     12       1.1    bouyer  * modification, are permitted provided that the following conditions
     13       1.1    bouyer  * are met:
     14       1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     15       1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     16       1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     18       1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     19       1.1    bouyer  *
     20       1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1    bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1    bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1    bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1    bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1    bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1    bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1    bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1    bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1    bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1    bouyer  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1    bouyer  */
     32       1.1    bouyer 
     33       1.1    bouyer /*
     34       1.1    bouyer  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
     35       1.1    bouyer  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
     36       1.1    bouyer  *
     37       1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     38       1.1    bouyer  * modification, are permitted provided that the following conditions
     39       1.1    bouyer  * are met:
     40       1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     41       1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     42       1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     43       1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     44       1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     45       1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     46       1.1    bouyer  *    must display the following acknowledgement:
     47       1.1    bouyer  *	This product includes software developed by Charles M. Hannum.
     48       1.1    bouyer  * 4. The name of the author may not be used to endorse or promote products
     49       1.1    bouyer  *    derived from this software without specific prior written permission.
     50       1.1    bouyer  *
     51       1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52       1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53       1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54       1.1    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     55       1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     56       1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     57       1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     58       1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59       1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     60       1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61       1.1    bouyer  */
     62       1.1    bouyer 
     63       1.1    bouyer /*
     64       1.1    bouyer  * Machine-specific functions for PCI autoconfiguration.
     65       1.1    bouyer  *
     66       1.1    bouyer  * On PCs, there are two methods of generating PCI configuration cycles.
     67       1.1    bouyer  * We try to detect the appropriate mechanism for this machine and set
     68       1.1    bouyer  * up a few function pointers to access the correct method directly.
     69       1.1    bouyer  *
     70       1.1    bouyer  * The configuration method can be hard-coded in the config file by
     71       1.1    bouyer  * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
     72       1.1    bouyer  * as defined section 3.6.4.1, `Generating Configuration Cycles'.
     73       1.1    bouyer  */
     74       1.1    bouyer 
     75       1.1    bouyer #include <sys/cdefs.h>
     76  1.26.2.1     rmind __KERNEL_RCSID(0, "$NetBSD: pci_intr_machdep.c,v 1.26.2.1 2014/05/18 17:45:30 rmind Exp $");
     77       1.1    bouyer 
     78       1.1    bouyer #include <sys/types.h>
     79       1.1    bouyer #include <sys/param.h>
     80       1.1    bouyer #include <sys/time.h>
     81       1.1    bouyer #include <sys/systm.h>
     82       1.1    bouyer #include <sys/errno.h>
     83       1.1    bouyer #include <sys/device.h>
     84       1.7        ad #include <sys/intr.h>
     85      1.20  drochner #include <sys/malloc.h>
     86       1.1    bouyer 
     87       1.1    bouyer #include <dev/pci/pcivar.h>
     88       1.1    bouyer 
     89       1.1    bouyer #include "ioapic.h"
     90       1.1    bouyer #include "eisa.h"
     91      1.14  jmcneill #include "acpica.h"
     92       1.1    bouyer #include "opt_mpbios.h"
     93       1.2  christos #include "opt_acpi.h"
     94       1.1    bouyer 
     95      1.26    dyoung #include <machine/i82489reg.h>
     96      1.26    dyoung 
     97      1.14  jmcneill #if NIOAPIC > 0 || NACPICA > 0
     98      1.22    dyoung #include <machine/i82093reg.h>
     99       1.1    bouyer #include <machine/i82093var.h>
    100       1.2  christos #include <machine/mpconfig.h>
    101       1.1    bouyer #include <machine/mpbiosvar.h>
    102       1.1    bouyer #include <machine/pic.h>
    103       1.1    bouyer #endif
    104       1.1    bouyer 
    105       1.1    bouyer #ifdef MPBIOS
    106       1.1    bouyer #include <machine/mpbiosvar.h>
    107       1.1    bouyer #endif
    108       1.1    bouyer 
    109      1.14  jmcneill #if NACPICA > 0
    110       1.1    bouyer #include <machine/mpacpi.h>
    111       1.1    bouyer #endif
    112       1.1    bouyer 
    113      1.11        ad #define	MPSAFE_MASK	0x80000000
    114      1.11        ad 
    115       1.1    bouyer int
    116      1.19    dyoung pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
    117       1.1    bouyer {
    118       1.1    bouyer 	int pin = pa->pa_intrpin;
    119       1.1    bouyer 	int line = pa->pa_intrline;
    120      1.23    dyoung 	pci_chipset_tag_t ipc, pc = pa->pa_pc;
    121      1.14  jmcneill #if NIOAPIC > 0 || NACPICA > 0
    122       1.1    bouyer 	int rawpin = pa->pa_rawintrpin;
    123       1.1    bouyer 	int bus, dev, func;
    124       1.1    bouyer #endif
    125       1.1    bouyer 
    126      1.23    dyoung 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    127      1.23    dyoung 		if ((ipc->pc_present & PCI_OVERRIDE_INTR_MAP) == 0)
    128      1.23    dyoung 			continue;
    129      1.23    dyoung 		return (*ipc->pc_ov->ov_intr_map)(ipc->pc_ctx, pa, ihp);
    130      1.16    dyoung 	}
    131      1.15    dyoung 
    132       1.1    bouyer 	if (pin == 0) {
    133       1.1    bouyer 		/* No IRQ used. */
    134       1.1    bouyer 		goto bad;
    135       1.1    bouyer 	}
    136       1.1    bouyer 
    137       1.2  christos 	*ihp = 0;
    138       1.2  christos 
    139       1.1    bouyer 	if (pin > PCI_INTERRUPT_PIN_MAX) {
    140      1.13        ad 		aprint_normal("pci_intr_map: bad interrupt pin %d\n", pin);
    141       1.1    bouyer 		goto bad;
    142       1.1    bouyer 	}
    143       1.1    bouyer 
    144      1.14  jmcneill #if NIOAPIC > 0 || NACPICA > 0
    145      1.24      yamt 	KASSERT(rawpin >= PCI_INTERRUPT_PIN_A);
    146      1.24      yamt 	KASSERT(rawpin <= PCI_INTERRUPT_PIN_D);
    147       1.1    bouyer 	pci_decompose_tag(pc, pa->pa_tag, &bus, &dev, &func);
    148       1.1    bouyer 	if (mp_busses != NULL) {
    149      1.25      yamt 		/*
    150      1.25      yamt 		 * Note: PCI_INTERRUPT_PIN_A == 1 where intr_find_mpmapping
    151      1.25      yamt 		 * wants pci bus_pin encoding which uses INT_A == 0.
    152      1.25      yamt 		 */
    153      1.24      yamt 		if (intr_find_mpmapping(bus,
    154      1.24      yamt 		    (dev << 2) | (rawpin - PCI_INTERRUPT_PIN_A), ihp) == 0) {
    155      1.24      yamt 			if (APIC_IRQ_LEGACY_IRQ(*ihp) == 0)
    156       1.2  christos 				*ihp |= line;
    157       1.1    bouyer 			return 0;
    158       1.1    bouyer 		}
    159       1.1    bouyer 		/*
    160       1.1    bouyer 		 * No explicit PCI mapping found. This is not fatal,
    161       1.1    bouyer 		 * we'll try the ISA (or possibly EISA) mappings next.
    162       1.1    bouyer 		 */
    163       1.1    bouyer 	}
    164       1.1    bouyer #endif
    165       1.1    bouyer 
    166       1.1    bouyer 	/*
    167       1.1    bouyer 	 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
    168       1.1    bouyer 	 * `unknown' or `no connection' on a PC.  We assume that a device with
    169       1.1    bouyer 	 * `no connection' either doesn't have an interrupt (in which case the
    170       1.1    bouyer 	 * pin number should be 0, and would have been noticed above), or
    171       1.1    bouyer 	 * wasn't configured by the BIOS (in which case we punt, since there's
    172       1.1    bouyer 	 * no real way we can know how the interrupt lines are mapped in the
    173       1.1    bouyer 	 * hardware).
    174       1.1    bouyer 	 *
    175       1.1    bouyer 	 * XXX
    176       1.1    bouyer 	 * Since IRQ 0 is only used by the clock, and we can't actually be sure
    177       1.1    bouyer 	 * that the BIOS did its job, we also recognize that as meaning that
    178       1.1    bouyer 	 * the BIOS has not configured the device.
    179       1.1    bouyer 	 */
    180       1.1    bouyer 	if (line == 0 || line == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    181      1.13        ad 		aprint_normal("pci_intr_map: no mapping for pin %c (line=%02x)\n",
    182       1.1    bouyer 		       '@' + pin, line);
    183       1.1    bouyer 		goto bad;
    184       1.1    bouyer 	} else {
    185       1.1    bouyer 		if (line >= NUM_LEGACY_IRQS) {
    186      1.13        ad 			aprint_normal("pci_intr_map: bad interrupt line %d\n", line);
    187       1.1    bouyer 			goto bad;
    188       1.1    bouyer 		}
    189       1.1    bouyer 		if (line == 2) {
    190      1.13        ad 			aprint_normal("pci_intr_map: changed line 2 to line 9\n");
    191       1.1    bouyer 			line = 9;
    192       1.1    bouyer 		}
    193       1.1    bouyer 	}
    194      1.14  jmcneill #if NIOAPIC > 0 || NACPICA > 0
    195       1.1    bouyer 	if (mp_busses != NULL) {
    196       1.1    bouyer 		if (intr_find_mpmapping(mp_isa_bus, line, ihp) == 0) {
    197       1.2  christos 			if ((*ihp & 0xff) == 0)
    198       1.2  christos 				*ihp |= line;
    199       1.1    bouyer 			return 0;
    200       1.1    bouyer 		}
    201       1.1    bouyer #if NEISA > 0
    202       1.1    bouyer 		if (intr_find_mpmapping(mp_eisa_bus, line, ihp) == 0) {
    203       1.2  christos 			if ((*ihp & 0xff) == 0)
    204       1.2  christos 				*ihp |= line;
    205       1.1    bouyer 			return 0;
    206       1.1    bouyer 		}
    207       1.1    bouyer #endif
    208      1.13        ad 		aprint_normal("pci_intr_map: bus %d dev %d func %d pin %d; line %d\n",
    209       1.1    bouyer 		    bus, dev, func, pin, line);
    210      1.13        ad 		aprint_normal("pci_intr_map: no MP mapping found\n");
    211       1.1    bouyer 	}
    212       1.1    bouyer #endif
    213       1.1    bouyer 
    214       1.1    bouyer 	*ihp = line;
    215       1.1    bouyer 	return 0;
    216       1.1    bouyer 
    217       1.1    bouyer bad:
    218       1.1    bouyer 	*ihp = -1;
    219       1.1    bouyer 	return 1;
    220       1.1    bouyer }
    221       1.1    bouyer 
    222       1.1    bouyer const char *
    223  1.26.2.1     rmind pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
    224  1.26.2.1     rmind     size_t len)
    225       1.1    bouyer {
    226      1.23    dyoung 	pci_chipset_tag_t ipc;
    227      1.17    dyoung 
    228      1.23    dyoung 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    229      1.23    dyoung 		if ((ipc->pc_present & PCI_OVERRIDE_INTR_STRING) == 0)
    230      1.23    dyoung 			continue;
    231  1.26.2.1     rmind 		return (*ipc->pc_ov->ov_intr_string)(ipc->pc_ctx, pc, ih,
    232  1.26.2.1     rmind 		    buf, len);
    233      1.16    dyoung 	}
    234      1.15    dyoung 
    235  1.26.2.1     rmind 	return intr_string(ih & ~MPSAFE_MASK, buf, len);
    236       1.1    bouyer }
    237       1.1    bouyer 
    238       1.1    bouyer 
    239       1.1    bouyer const struct evcnt *
    240       1.6  christos pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
    241       1.1    bouyer {
    242      1.23    dyoung 	pci_chipset_tag_t ipc;
    243       1.1    bouyer 
    244      1.23    dyoung 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    245      1.23    dyoung 		if ((ipc->pc_present & PCI_OVERRIDE_INTR_EVCNT) == 0)
    246      1.23    dyoung 			continue;
    247      1.23    dyoung 		return (*ipc->pc_ov->ov_intr_evcnt)(ipc->pc_ctx, pc, ih);
    248      1.16    dyoung 	}
    249      1.15    dyoung 
    250       1.1    bouyer 	/* XXX for now, no evcnt parent reported */
    251       1.1    bouyer 	return NULL;
    252       1.1    bouyer }
    253       1.1    bouyer 
    254      1.11        ad int
    255      1.11        ad pci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
    256      1.11        ad 		 int attr, uint64_t data)
    257      1.11        ad {
    258      1.11        ad 
    259      1.11        ad 	switch (attr) {
    260      1.11        ad 	case PCI_INTR_MPSAFE:
    261      1.11        ad 		if (data) {
    262      1.11        ad 			 *ih |= MPSAFE_MASK;
    263      1.11        ad 		} else {
    264      1.11        ad 			 *ih &= ~MPSAFE_MASK;
    265      1.11        ad 		}
    266      1.11        ad 		/* XXX Set live if already mapped. */
    267      1.11        ad 		return 0;
    268      1.11        ad 	default:
    269      1.11        ad 		return ENODEV;
    270      1.11        ad 	}
    271      1.11        ad }
    272      1.11        ad 
    273       1.1    bouyer void *
    274       1.6  christos pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
    275       1.5  christos     int level, int (*func)(void *), void *arg)
    276       1.1    bouyer {
    277       1.1    bouyer 	int pin, irq;
    278       1.1    bouyer 	struct pic *pic;
    279      1.12  drochner #if NIOAPIC > 0
    280      1.12  drochner 	struct ioapic_softc *ioapic;
    281      1.12  drochner #endif
    282      1.11        ad 	bool mpsafe;
    283      1.23    dyoung 	pci_chipset_tag_t ipc;
    284       1.1    bouyer 
    285      1.23    dyoung 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    286      1.23    dyoung 		if ((ipc->pc_present & PCI_OVERRIDE_INTR_ESTABLISH) == 0)
    287      1.23    dyoung 			continue;
    288      1.23    dyoung 		return (*ipc->pc_ov->ov_intr_establish)(ipc->pc_ctx,
    289      1.23    dyoung 		    pc, ih, level, func, arg);
    290      1.16    dyoung 	}
    291      1.15    dyoung 
    292       1.1    bouyer 	pic = &i8259_pic;
    293      1.11        ad 	pin = irq = (ih & ~MPSAFE_MASK);
    294      1.11        ad 	mpsafe = ((ih & MPSAFE_MASK) != 0);
    295       1.1    bouyer 
    296       1.1    bouyer #if NIOAPIC > 0
    297       1.1    bouyer 	if (ih & APIC_INT_VIA_APIC) {
    298      1.12  drochner 		ioapic = ioapic_find(APIC_IRQ_APIC(ih));
    299      1.12  drochner 		if (ioapic == NULL) {
    300      1.13        ad 			aprint_normal("pci_intr_establish: bad ioapic %d\n",
    301       1.1    bouyer 			    APIC_IRQ_APIC(ih));
    302       1.1    bouyer 			return NULL;
    303       1.1    bouyer 		}
    304      1.12  drochner 		pic = &ioapic->sc_pic;
    305       1.1    bouyer 		pin = APIC_IRQ_PIN(ih);
    306       1.1    bouyer 		irq = APIC_IRQ_LEGACY_IRQ(ih);
    307       1.1    bouyer 		if (irq < 0 || irq >= NUM_LEGACY_IRQS)
    308       1.1    bouyer 			irq = -1;
    309       1.1    bouyer 	}
    310       1.1    bouyer #endif
    311       1.1    bouyer 
    312      1.10        ad 	return intr_establish(irq, pic, pin, IST_LEVEL, level, func, arg,
    313      1.11        ad 	    mpsafe);
    314       1.1    bouyer }
    315       1.1    bouyer 
    316       1.1    bouyer void
    317       1.6  christos pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
    318       1.1    bouyer {
    319      1.23    dyoung 	pci_chipset_tag_t ipc;
    320       1.1    bouyer 
    321      1.23    dyoung 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    322      1.23    dyoung 		if ((ipc->pc_present & PCI_OVERRIDE_INTR_DISESTABLISH) == 0)
    323      1.23    dyoung 			continue;
    324      1.23    dyoung 		(*ipc->pc_ov->ov_intr_disestablish)(ipc->pc_ctx, pc, cookie);
    325      1.23    dyoung 		return;
    326      1.15    dyoung 	}
    327      1.15    dyoung 
    328       1.1    bouyer 	intr_disestablish(cookie);
    329       1.1    bouyer }
    330      1.20  drochner 
    331      1.20  drochner #if NIOAPIC > 0
    332      1.20  drochner /*
    333      1.20  drochner  * experimental support for MSI, does support a single vector,
    334      1.20  drochner  * no MSI-X, 8-bit APIC IDs
    335      1.20  drochner  * (while it doesn't need the ioapic technically, it borrows
    336      1.20  drochner  * from its kernel support)
    337      1.20  drochner  */
    338      1.20  drochner 
    339      1.20  drochner /* dummies, needed by common intr_establish code */
    340      1.20  drochner static void
    341      1.20  drochner msipic_hwmask(struct pic *pic, int pin)
    342      1.20  drochner {
    343      1.20  drochner }
    344      1.20  drochner static void
    345      1.20  drochner msipic_addroute(struct pic *pic, struct cpu_info *ci,
    346      1.20  drochner 		int pin, int vec, int type)
    347      1.20  drochner {
    348      1.20  drochner }
    349      1.20  drochner 
    350      1.20  drochner static struct pic msi_pic = {
    351      1.20  drochner 	.pic_name = "msi",
    352      1.20  drochner 	.pic_type = PIC_SOFT,
    353      1.20  drochner 	.pic_vecbase = 0,
    354      1.20  drochner 	.pic_apicid = 0,
    355      1.20  drochner 	.pic_lock = __SIMPLELOCK_UNLOCKED,
    356      1.20  drochner 	.pic_hwmask = msipic_hwmask,
    357      1.20  drochner 	.pic_hwunmask = msipic_hwmask,
    358      1.20  drochner 	.pic_addroute = msipic_addroute,
    359      1.20  drochner 	.pic_delroute = msipic_addroute,
    360      1.20  drochner 	.pic_edge_stubs = ioapic_edge_stubs,
    361      1.20  drochner };
    362      1.20  drochner 
    363      1.20  drochner struct msi_hdl {
    364      1.20  drochner 	struct intrhand *ih;
    365      1.20  drochner 	pci_chipset_tag_t pc;
    366      1.20  drochner 	pcitag_t tag;
    367      1.20  drochner 	int co;
    368      1.20  drochner };
    369      1.20  drochner 
    370      1.20  drochner void *
    371      1.20  drochner pci_msi_establish(struct pci_attach_args *pa, int level,
    372      1.20  drochner 		  int (*func)(void *), void *arg)
    373      1.20  drochner {
    374      1.20  drochner 	int co;
    375      1.21    dyoung 	struct intrhand *ih;
    376      1.20  drochner 	struct msi_hdl *msih;
    377      1.20  drochner 	struct cpu_info *ci;
    378      1.21    dyoung 	struct intrsource *is;
    379      1.21    dyoung 	pcireg_t reg;
    380      1.20  drochner 
    381      1.20  drochner 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &co, 0))
    382      1.20  drochner 		return NULL;
    383      1.20  drochner 
    384      1.21    dyoung 	ih = intr_establish(-1, &msi_pic, -1, IST_EDGE, level, func, arg, 0);
    385      1.21    dyoung 	if (ih == NULL)
    386      1.20  drochner 		return NULL;
    387      1.20  drochner 
    388      1.20  drochner 	msih = malloc(sizeof(*msih), M_DEVBUF, M_WAITOK);
    389      1.20  drochner 	msih->ih = ih;
    390      1.20  drochner 	msih->pc = pa->pa_pc;
    391      1.20  drochner 	msih->tag = pa->pa_tag;
    392      1.20  drochner 	msih->co = co;
    393      1.20  drochner 
    394      1.21    dyoung 	ci = ih->ih_cpu;
    395      1.21    dyoung 	is = ci->ci_isources[ih->ih_slot];
    396      1.21    dyoung 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, co + PCI_MSI_CTL);
    397      1.21    dyoung 	pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MADDR64_LO,
    398      1.26    dyoung 		       LAPIC_MSIADDR_BASE |
    399      1.26    dyoung 		       __SHIFTIN(ci->ci_cpuid, LAPIC_MSIADDR_DSTID_MASK));
    400      1.21    dyoung 	if (reg & PCI_MSI_CTL_64BIT_ADDR) {
    401      1.21    dyoung 		pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MADDR64_HI,
    402      1.21    dyoung 		    0);
    403      1.22    dyoung 		/* XXX according to the manual, ASSERT is unnecessary if
    404      1.22    dyoung 		 * EDGE
    405      1.22    dyoung 		 */
    406      1.21    dyoung 		pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MDATA64,
    407      1.26    dyoung 		    __SHIFTIN(is->is_idtvec, LAPIC_MSIDATA_VECTOR_MASK) |
    408      1.26    dyoung 		    LAPIC_MSIDATA_TRGMODE_EDGE | LAPIC_MSIDATA_LEVEL_ASSERT |
    409      1.26    dyoung 		    LAPIC_MSIDATA_DM_FIXED);
    410      1.22    dyoung 	} else {
    411      1.22    dyoung 		/* XXX according to the manual, ASSERT is unnecessary if
    412      1.22    dyoung 		 * EDGE
    413      1.22    dyoung 		 */
    414      1.21    dyoung 		pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MDATA,
    415      1.26    dyoung 		    __SHIFTIN(is->is_idtvec, LAPIC_MSIDATA_VECTOR_MASK) |
    416      1.26    dyoung 		    LAPIC_MSIDATA_TRGMODE_EDGE | LAPIC_MSIDATA_LEVEL_ASSERT |
    417      1.26    dyoung 		    LAPIC_MSIDATA_DM_FIXED);
    418      1.22    dyoung 	}
    419      1.21    dyoung 	pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_CTL,
    420      1.21    dyoung 	    PCI_MSI_CTL_MSI_ENABLE);
    421      1.21    dyoung 	return msih;
    422      1.20  drochner }
    423      1.20  drochner 
    424      1.20  drochner void
    425      1.20  drochner pci_msi_disestablish(void *ih)
    426      1.20  drochner {
    427      1.20  drochner 	struct msi_hdl *msih = ih;
    428      1.20  drochner 
    429      1.21    dyoung 	pci_conf_write(msih->pc, msih->tag, msih->co + PCI_MSI_CTL, 0);
    430      1.20  drochner 	intr_disestablish(msih->ih);
    431      1.21    dyoung 	free(msih, M_DEVBUF);
    432      1.20  drochner }
    433      1.20  drochner #endif
    434