Home | History | Annotate | Line # | Download | only in pci
pci_intr_fixup.c revision 1.49
      1  1.49    dyoung /*	$NetBSD: pci_intr_fixup.c,v 1.49 2011/07/01 17:37:26 dyoung Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4   1.1   thorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1   thorpej  * NASA Ames Research Center.
     10   1.1   thorpej  *
     11   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.1   thorpej  * modification, are permitted provided that the following conditions
     13   1.1   thorpej  * are met:
     14   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.1   thorpej  *
     20   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1   thorpej  */
     32   1.1   thorpej 
     33   1.1   thorpej /*
     34   1.1   thorpej  * Copyright (c) 1999, by UCHIYAMA Yasushi
     35   1.1   thorpej  * All rights reserved.
     36   1.1   thorpej  *
     37   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     38   1.1   thorpej  * modification, are permitted provided that the following conditions
     39   1.1   thorpej  * are met:
     40   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     41   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     42   1.1   thorpej  * 2. The name of the developer may NOT be used to endorse or promote products
     43   1.1   thorpej  *    derived from this software without specific prior written permission.
     44   1.1   thorpej  *
     45   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     46   1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47   1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48   1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     49   1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50   1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51   1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52   1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53   1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54   1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55   1.1   thorpej  * SUCH DAMAGE.
     56   1.1   thorpej  */
     57   1.1   thorpej 
     58   1.1   thorpej /*
     59   1.1   thorpej  * PCI Interrupt Router support.
     60   1.1   thorpej  */
     61  1.18     lukem 
     62  1.18     lukem #include <sys/cdefs.h>
     63  1.49    dyoung __KERNEL_RCSID(0, "$NetBSD: pci_intr_fixup.c,v 1.49 2011/07/01 17:37:26 dyoung Exp $");
     64   1.1   thorpej 
     65   1.1   thorpej #include "opt_pcibios.h"
     66  1.32    sekiya #include "opt_pcifixup.h"
     67   1.1   thorpej 
     68   1.1   thorpej #include <sys/param.h>
     69   1.1   thorpej #include <sys/systm.h>
     70   1.1   thorpej #include <sys/kernel.h>
     71   1.1   thorpej #include <sys/malloc.h>
     72   1.1   thorpej #include <sys/queue.h>
     73   1.1   thorpej #include <sys/device.h>
     74   1.1   thorpej 
     75  1.49    dyoung #include <sys/bus.h>
     76   1.1   thorpej #include <machine/intr.h>
     77   1.1   thorpej 
     78   1.1   thorpej #include <dev/pci/pcireg.h>
     79   1.1   thorpej #include <dev/pci/pcivar.h>
     80   1.1   thorpej #include <dev/pci/pcidevs.h>
     81   1.1   thorpej 
     82   1.1   thorpej #include <i386/pci/pci_intr_fixup.h>
     83   1.1   thorpej #include <i386/pci/pcibios.h>
     84   1.1   thorpej 
     85   1.1   thorpej struct pciintr_link_map {
     86   1.1   thorpej 	int link;
     87   1.1   thorpej 	int clink;
     88   1.1   thorpej 	int irq;
     89  1.35     perry 	uint16_t bitmap;
     90   1.1   thorpej 	int fixup_stage;
     91   1.1   thorpej 	SIMPLEQ_ENTRY(pciintr_link_map) list;
     92   1.1   thorpej };
     93   1.1   thorpej 
     94  1.19      onoe pciintr_icu_tag_t pciintr_icu_tag;
     95   1.1   thorpej pciintr_icu_handle_t pciintr_icu_handle;
     96   1.1   thorpej 
     97   1.8      soda #ifdef PCIBIOS_IRQS_HINT
     98   1.8      soda int pcibios_irqs_hint = PCIBIOS_IRQS_HINT;
     99   1.8      soda #endif
    100   1.8      soda 
    101  1.29     kochi struct pciintr_link_map *pciintr_link_lookup(int);
    102  1.29     kochi struct pciintr_link_map *pciintr_link_alloc(struct pcibios_intr_routing *,
    103  1.29     kochi 	int);
    104  1.29     kochi struct pcibios_intr_routing *pciintr_pir_lookup(int, int);
    105  1.29     kochi static int pciintr_bitmap_count_irq(int, int *);
    106  1.29     kochi static int pciintr_bitmap_find_lowest_irq(int, int *);
    107  1.29     kochi int	pciintr_link_init (void);
    108  1.10      soda #ifdef PCIBIOS_INTR_GUESS
    109  1.29     kochi int	pciintr_guess_irq(void);
    110  1.10      soda #endif
    111  1.29     kochi int	pciintr_link_fixup(void);
    112  1.35     perry int	pciintr_link_route(uint16_t *);
    113  1.35     perry int	pciintr_irq_release(uint16_t *);
    114  1.29     kochi int	pciintr_header_fixup(pci_chipset_tag_t);
    115  1.29     kochi void	pciintr_do_header_fixup(pci_chipset_tag_t, pcitag_t, void*);
    116   1.1   thorpej 
    117   1.1   thorpej SIMPLEQ_HEAD(, pciintr_link_map) pciintr_link_map_list;
    118   1.1   thorpej 
    119   1.1   thorpej const struct pciintr_icu_table {
    120   1.1   thorpej 	pci_vendor_id_t	piit_vendor;
    121   1.1   thorpej 	pci_product_id_t piit_product;
    122  1.29     kochi 	int (*piit_init)(pci_chipset_tag_t,
    123  1.29     kochi 	    bus_space_tag_t, pcitag_t, pciintr_icu_tag_t *,
    124  1.29     kochi 	    pciintr_icu_handle_t *);
    125  1.42  jmcneill 	void (*piit_uninit)(pciintr_icu_handle_t);
    126   1.1   thorpej } pciintr_icu_table[] = {
    127   1.1   thorpej 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371MX,
    128  1.42  jmcneill 	  piix_init, piix_uninit },
    129   1.1   thorpej 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371AB_ISA,
    130  1.42  jmcneill 	  piix_init, piix_uninit },
    131   1.1   thorpej 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371FB_ISA,
    132  1.42  jmcneill 	  piix_init, piix_uninit },
    133   1.1   thorpej 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82371SB_ISA,
    134  1.42  jmcneill 	  piix_init, piix_uninit },
    135  1.37     kochi 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82440MX_ISA,
    136  1.42  jmcneill 	  piix_init, piix_uninit },
    137  1.28     kochi 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801AA_LPC,
    138  1.42  jmcneill 	  piix_init, piix_uninit },	/* ICH */
    139  1.28     kochi 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801AB_LPC,
    140  1.42  jmcneill 	  piix_init, piix_uninit },	/* ICH0 */
    141  1.16      haya 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801BA_LPC,
    142  1.43  christos 	  ich_init, NULL },			/* ICH2 */
    143  1.19      onoe 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801BAM_LPC,
    144  1.43  christos 	  ich_init, NULL },			/* ICH2M */
    145  1.28     kochi 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801CA_LPC,
    146  1.43  christos 	  ich_init, NULL },			/* ICH3S */
    147  1.28     kochi 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801CAM_LPC,
    148  1.43  christos 	  ich_init, NULL },			/* ICH3M */
    149  1.21   kanaoka 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801DB_LPC,
    150  1.43  christos 	  ich_init, NULL },			/* ICH4 */
    151  1.48   msaitoh 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801DBM_LPC,
    152  1.43  christos 	  ich_init, NULL },			/* ICH4M */
    153  1.25    dyoung 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801EB_LPC,
    154  1.43  christos 	  ich_init, NULL },			/* ICH5 */
    155  1.37     kochi 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801FB_LPC,
    156  1.43  christos 	  ich_init, NULL },			/* ICH6/ICH6R */
    157  1.34    rpaulo 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801FBM_LPC,
    158  1.43  christos 	  ich_init, NULL },			/* ICH6M */
    159  1.37     kochi 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801G_LPC,
    160  1.43  christos 	  ich_init, NULL },			/* ICH7/ICH7R */
    161  1.37     kochi 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801GBM_LPC,
    162  1.43  christos 	  ich_init, NULL },			/* ICH7-M */
    163  1.37     kochi 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801GHM_LPC,
    164  1.43  christos 	  ich_init, NULL },			/* ICH7DH/ICH7-M DH */
    165   1.1   thorpej 
    166   1.1   thorpej 	{ PCI_VENDOR_OPTI,	PCI_PRODUCT_OPTI_82C558,
    167  1.43  christos 	  opti82c558_init, NULL },
    168   1.1   thorpej 	{ PCI_VENDOR_OPTI,	PCI_PRODUCT_OPTI_82C700,
    169  1.43  christos 	  opti82c700_init, NULL },
    170   1.1   thorpej 
    171   1.1   thorpej 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT82C586_ISA,
    172  1.43  christos 	  via82c586_init, NULL },
    173  1.24     perry 	{ PCI_VENDOR_VIATECH,   PCI_PRODUCT_VIATECH_VT82C596A,
    174  1.43  christos 	  via82c586_init, NULL },
    175  1.11   aymeric 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT82C686A_ISA,
    176  1.43  christos 	  via82c586_init, NULL },
    177   1.1   thorpej 
    178  1.36   xtraeme 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8231,
    179  1.43  christos 	  via8231_init, NULL },
    180  1.38  christos 	{ PCI_VENDOR_VIATECH,   PCI_PRODUCT_VIATECH_VT8233,
    181  1.43  christos 	  via82c586_init, NULL },
    182  1.36   xtraeme 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8233A,
    183  1.43  christos 	  via8231_init, NULL },
    184  1.36   xtraeme 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8235,
    185  1.43  christos 	  via8231_init, NULL },
    186  1.36   xtraeme 	{ PCI_VENDOR_VIATECH,	PCI_PRODUCT_VIATECH_VT8237,
    187  1.43  christos 	  via8231_init, NULL },
    188  1.36   xtraeme 
    189  1.38  christos 
    190   1.1   thorpej 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_85C503,
    191  1.43  christos 	  sis85c503_init, NULL },
    192  1.39   xtraeme 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_962,
    193  1.43  christos 	  sis85c503_init, NULL },
    194  1.39   xtraeme 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_963,
    195  1.43  christos 	  sis85c503_init, NULL },
    196  1.12       uch 
    197  1.12       uch 	{ PCI_VENDOR_AMD,	PCI_PRODUCT_AMD_PBC756_PMC,
    198  1.43  christos 	  amd756_init, NULL },
    199  1.40   xtraeme 	{ PCI_VENDOR_AMD,	PCI_PRODUCT_AMD_PBC766_PMC,
    200  1.43  christos 	  amd756_init, NULL },
    201  1.39   xtraeme 	{ PCI_VENDOR_AMD,	PCI_PRODUCT_AMD_PBC768_PMC,
    202  1.43  christos 	  amd756_init, NULL },
    203  1.17      haya 
    204  1.39   xtraeme 	{ PCI_VENDOR_ALI,	PCI_PRODUCT_ALI_M1533,
    205  1.43  christos 	  ali1543_init, NULL },
    206  1.17      haya 	{ PCI_VENDOR_ALI,	PCI_PRODUCT_ALI_M1543,
    207  1.43  christos 	  ali1543_init, NULL },
    208   1.1   thorpej 
    209   1.1   thorpej 	{ 0,			0,
    210  1.43  christos 	  NULL, NULL },
    211   1.1   thorpej };
    212   1.1   thorpej 
    213  1.29     kochi const struct pciintr_icu_table *pciintr_icu_lookup(pcireg_t);
    214   1.1   thorpej 
    215   1.1   thorpej const struct pciintr_icu_table *
    216  1.29     kochi pciintr_icu_lookup(pcireg_t id)
    217   1.1   thorpej {
    218   1.1   thorpej 	const struct pciintr_icu_table *piit;
    219   1.1   thorpej 
    220   1.1   thorpej 	for (piit = pciintr_icu_table;
    221   1.1   thorpej 	     piit->piit_init != NULL;
    222   1.1   thorpej 	     piit++) {
    223   1.1   thorpej 		if (PCI_VENDOR(id) == piit->piit_vendor &&
    224   1.1   thorpej 		    PCI_PRODUCT(id) == piit->piit_product)
    225   1.1   thorpej 			return (piit);
    226   1.1   thorpej 	}
    227   1.1   thorpej 
    228   1.1   thorpej 	return (NULL);
    229   1.1   thorpej }
    230   1.1   thorpej 
    231   1.1   thorpej struct pciintr_link_map *
    232  1.29     kochi pciintr_link_lookup(int link)
    233   1.1   thorpej {
    234   1.1   thorpej 	struct pciintr_link_map *l;
    235   1.1   thorpej 
    236  1.20     lukem 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    237   1.1   thorpej 		if (l->link == link)
    238   1.1   thorpej 			return (l);
    239   1.1   thorpej 	}
    240   1.1   thorpej 
    241   1.1   thorpej 	return (NULL);
    242   1.1   thorpej }
    243   1.1   thorpej 
    244   1.1   thorpej struct pciintr_link_map *
    245  1.29     kochi pciintr_link_alloc(struct pcibios_intr_routing *pir, int pin)
    246   1.1   thorpej {
    247   1.7      soda 	int link = pir->linkmap[pin].link, clink, irq;
    248   1.1   thorpej 	struct pciintr_link_map *l, *lstart;
    249   1.1   thorpej 
    250  1.10      soda 	if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
    251   1.7      soda 		/*
    252  1.10      soda 		 * Get the canonical link value for this entry.
    253   1.7      soda 		 */
    254  1.10      soda 		if (pciintr_icu_getclink(pciintr_icu_tag, pciintr_icu_handle,
    255  1.10      soda 		    link, &clink) != 0) {
    256  1.10      soda 			/*
    257  1.10      soda 			 * ICU doesn't understand the link value.
    258  1.10      soda 			 * Just ignore this PIR entry.
    259  1.10      soda 			 */
    260   1.7      soda #ifdef DIAGNOSTIC
    261  1.10      soda 			printf("pciintr_link_alloc: bus %d device %d: "
    262  1.10      soda 			    "link 0x%02x invalid\n",
    263  1.10      soda 			    pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link);
    264   1.7      soda #endif
    265  1.10      soda 			return (NULL);
    266  1.10      soda 		}
    267   1.7      soda 
    268   1.7      soda 		/*
    269  1.10      soda 		 * Check the link value by asking the ICU for the
    270  1.10      soda 		 * canonical link value.
    271  1.10      soda 		 * Also, determine if this PIRQ is mapped to an IRQ.
    272   1.7      soda 		 */
    273  1.10      soda 		if (pciintr_icu_get_intr(pciintr_icu_tag, pciintr_icu_handle,
    274  1.10      soda 		    clink, &irq) != 0) {
    275  1.10      soda 			/*
    276  1.10      soda 			 * ICU doesn't understand the canonical link value.
    277  1.10      soda 			 * Just ignore this PIR entry.
    278  1.10      soda 			 */
    279   1.7      soda #ifdef DIAGNOSTIC
    280  1.10      soda 			printf("pciintr_link_alloc: "
    281  1.10      soda 			    "bus %d device %d link 0x%02x: "
    282  1.10      soda 			    "PIRQ 0x%02x invalid\n",
    283  1.10      soda 			    pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link,
    284  1.10      soda 			    clink);
    285   1.7      soda #endif
    286  1.10      soda 			return (NULL);
    287  1.10      soda 		}
    288   1.7      soda 	}
    289   1.7      soda 
    290   1.1   thorpej 	l = malloc(sizeof(*l), M_DEVBUF, M_NOWAIT);
    291   1.1   thorpej 	if (l == NULL)
    292   1.1   thorpej 		panic("pciintr_link_alloc");
    293   1.1   thorpej 
    294   1.1   thorpej 	memset(l, 0, sizeof(*l));
    295   1.1   thorpej 
    296   1.7      soda 	l->link = link;
    297   1.1   thorpej 	l->bitmap = pir->linkmap[pin].bitmap;
    298  1.10      soda 	if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
    299  1.10      soda 		l->clink = clink;
    300  1.23      fvdl 		l->irq = irq; /* maybe X86_PCI_INTERRUPT_LINE_NO_CONNECTION */
    301  1.10      soda 	} else {
    302  1.10      soda 		l->clink = link; /* only for PCIBIOSVERBOSE diagnostic */
    303  1.23      fvdl 		l->irq = X86_PCI_INTERRUPT_LINE_NO_CONNECTION;
    304  1.10      soda 	}
    305   1.1   thorpej 
    306   1.1   thorpej 	lstart = SIMPLEQ_FIRST(&pciintr_link_map_list);
    307   1.1   thorpej 	if (lstart == NULL || lstart->link < l->link)
    308   1.1   thorpej 		SIMPLEQ_INSERT_TAIL(&pciintr_link_map_list, l, list);
    309   1.1   thorpej 	else
    310   1.1   thorpej 		SIMPLEQ_INSERT_HEAD(&pciintr_link_map_list, l, list);
    311   1.1   thorpej 
    312   1.1   thorpej 	return (l);
    313   1.1   thorpej }
    314   1.1   thorpej 
    315   1.1   thorpej struct pcibios_intr_routing *
    316  1.29     kochi pciintr_pir_lookup(int bus, int device)
    317   1.1   thorpej {
    318   1.1   thorpej 	struct pcibios_intr_routing *pir;
    319   1.1   thorpej 	int entry;
    320   1.1   thorpej 
    321   1.1   thorpej 	if (pcibios_pir_table == NULL)
    322   1.1   thorpej 		return (NULL);
    323   1.1   thorpej 
    324   1.1   thorpej 	for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
    325   1.1   thorpej 		pir = &pcibios_pir_table[entry];
    326   1.7      soda 		if (pir->bus == bus &&
    327   1.7      soda 		    PIR_DEVFUNC_DEVICE(pir->device) == device)
    328   1.1   thorpej 			return (pir);
    329   1.1   thorpej 	}
    330   1.1   thorpej 
    331   1.1   thorpej 	return (NULL);
    332   1.1   thorpej }
    333   1.1   thorpej 
    334   1.7      soda static int
    335  1.29     kochi pciintr_bitmap_count_irq(int irq_bitmap, int *irqp)
    336   1.7      soda {
    337  1.23      fvdl 	int i, bit, count = 0, irq = X86_PCI_INTERRUPT_LINE_NO_CONNECTION;
    338   1.7      soda 
    339   1.7      soda 	if (irq_bitmap != 0) {
    340   1.7      soda 		for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
    341   1.7      soda 			if (irq_bitmap & bit) {
    342   1.7      soda 				irq = i;
    343   1.7      soda 				count++;
    344   1.7      soda 			}
    345   1.7      soda 		}
    346   1.7      soda 	}
    347   1.7      soda 	*irqp = irq;
    348   1.7      soda 	return (count);
    349   1.7      soda }
    350   1.7      soda 
    351   1.7      soda static int
    352  1.29     kochi pciintr_bitmap_find_lowest_irq(int irq_bitmap, int *irqp)
    353   1.7      soda {
    354   1.7      soda 	int i, bit;
    355   1.7      soda 
    356   1.7      soda 	if (irq_bitmap != 0) {
    357   1.7      soda 		for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
    358   1.7      soda 			if (irq_bitmap & bit) {
    359   1.7      soda 				*irqp = i;
    360   1.7      soda 				return (1); /* found */
    361   1.7      soda 			}
    362   1.7      soda 		}
    363   1.7      soda 	}
    364   1.7      soda 	return (0); /* not found */
    365   1.7      soda }
    366   1.7      soda 
    367   1.1   thorpej int
    368  1.31     perry pciintr_link_init(void)
    369   1.1   thorpej {
    370  1.10      soda 	int entry, pin, link;
    371   1.1   thorpej 	struct pcibios_intr_routing *pir;
    372   1.1   thorpej 	struct pciintr_link_map *l;
    373   1.1   thorpej 
    374   1.1   thorpej 	if (pcibios_pir_table == NULL) {
    375   1.1   thorpej 		/* No PIR table; can't do anything. */
    376   1.1   thorpej 		printf("pciintr_link_init: no PIR table\n");
    377   1.1   thorpej 		return (1);
    378   1.1   thorpej 	}
    379   1.1   thorpej 
    380   1.1   thorpej 	SIMPLEQ_INIT(&pciintr_link_map_list);
    381   1.1   thorpej 
    382   1.1   thorpej 	for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
    383   1.1   thorpej 		pir = &pcibios_pir_table[entry];
    384   1.7      soda 		for (pin = 0; pin < PCI_INTERRUPT_PIN_MAX; pin++) {
    385   1.1   thorpej 			link = pir->linkmap[pin].link;
    386   1.1   thorpej 			if (link == 0) {
    387   1.1   thorpej 				/* No connection for this pin. */
    388   1.1   thorpej 				continue;
    389   1.1   thorpej 			}
    390   1.1   thorpej 			/*
    391   1.1   thorpej 			 * Multiple devices may be wired to the same
    392   1.1   thorpej 			 * interrupt; check to see if we've seen this
    393   1.1   thorpej 			 * one already.  If not, allocate a new link
    394   1.1   thorpej 			 * map entry and stuff it in the map.
    395   1.1   thorpej 			 */
    396   1.7      soda 			l = pciintr_link_lookup(link);
    397   1.7      soda 			if (l == NULL) {
    398   1.1   thorpej 				(void) pciintr_link_alloc(pir, pin);
    399   1.7      soda 			} else if (pir->linkmap[pin].bitmap != l->bitmap) {
    400   1.7      soda 				/*
    401   1.7      soda 				 * violates PCI IRQ Routing Table Specification
    402   1.7      soda 				 */
    403   1.7      soda #ifdef DIAGNOSTIC
    404   1.7      soda 				printf("pciintr_link_init: "
    405   1.7      soda 				    "bus %d device %d link 0x%02x: "
    406   1.7      soda 				    "bad irq bitmap 0x%04x, "
    407   1.7      soda 				    "should be 0x%04x\n",
    408   1.7      soda 				    pir->bus, PIR_DEVFUNC_DEVICE(pir->device),
    409   1.7      soda 				    link, pir->linkmap[pin].bitmap, l->bitmap);
    410   1.7      soda #endif
    411   1.7      soda 				/* safer value. */
    412   1.7      soda 				l->bitmap &= pir->linkmap[pin].bitmap;
    413   1.7      soda 				/* XXX - or, should ignore this entry? */
    414   1.7      soda 			}
    415   1.1   thorpej 		}
    416   1.1   thorpej 	}
    417   1.1   thorpej 
    418  1.10      soda 	return (0);
    419  1.10      soda }
    420  1.10      soda 
    421  1.10      soda #ifdef PCIBIOS_INTR_GUESS
    422  1.10      soda /*
    423  1.10      soda  * No compatible PCI ICU found.
    424  1.10      soda  * Hopes the BIOS already setup the ICU.
    425  1.10      soda  */
    426  1.10      soda int
    427  1.31     perry pciintr_guess_irq(void)
    428  1.10      soda {
    429  1.10      soda 	struct pciintr_link_map *l;
    430  1.10      soda 	int irq, guessed = 0;
    431  1.10      soda 
    432  1.10      soda 	/*
    433  1.10      soda 	 * Stage 1: If only one IRQ is available for the link, use it.
    434  1.10      soda 	 */
    435  1.20     lukem 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    436  1.23      fvdl 		if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
    437  1.10      soda 			continue;
    438  1.10      soda 		if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
    439  1.10      soda 			l->irq = irq;
    440  1.10      soda 			l->fixup_stage = 1;
    441  1.10      soda #ifdef PCIINTR_DEBUG
    442  1.10      soda 			printf("pciintr_guess_irq (stage 1): "
    443  1.10      soda 			    "guessing PIRQ 0x%02x to be IRQ %d\n",
    444  1.10      soda 			    l->clink, l->irq);
    445  1.10      soda #endif
    446  1.10      soda 			guessed = 1;
    447  1.10      soda 		}
    448  1.10      soda 	}
    449  1.10      soda 
    450  1.10      soda 	return (guessed ? 0 : -1);
    451   1.1   thorpej }
    452  1.10      soda #endif /* PCIBIOS_INTR_GUESS */
    453   1.1   thorpej 
    454   1.1   thorpej int
    455  1.31     perry pciintr_link_fixup(void)
    456   1.1   thorpej {
    457   1.1   thorpej 	struct pciintr_link_map *l;
    458   1.7      soda 	int irq;
    459  1.35     perry 	uint16_t pciirq = 0;
    460   1.1   thorpej 
    461   1.1   thorpej 	/*
    462   1.1   thorpej 	 * First stage: Attempt to connect PIRQs which aren't
    463   1.1   thorpej 	 * yet connected.
    464   1.1   thorpej 	 */
    465  1.20     lukem 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    466  1.23      fvdl 		if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    467   1.1   thorpej 			/*
    468   1.7      soda 			 * Interrupt is already connected.  Don't do
    469   1.7      soda 			 * anything to it.
    470   1.7      soda 			 * In this case, l->fixup_stage == 0.
    471   1.1   thorpej 			 */
    472   1.7      soda 			pciirq |= 1 << l->irq;
    473   1.1   thorpej #ifdef PCIINTR_DEBUG
    474   1.7      soda 			printf("pciintr_link_fixup: PIRQ 0x%02x already "
    475   1.7      soda 			    "connected to IRQ %d\n", l->clink, l->irq);
    476   1.1   thorpej #endif
    477   1.1   thorpej 			continue;
    478   1.1   thorpej 		}
    479   1.1   thorpej 		/*
    480   1.7      soda 		 * Interrupt isn't connected.  Attempt to assign it to an IRQ.
    481   1.1   thorpej 		 */
    482   1.1   thorpej #ifdef PCIINTR_DEBUG
    483   1.7      soda 		printf("pciintr_link_fixup: PIRQ 0x%02x not connected",
    484   1.7      soda 		    l->clink);
    485   1.1   thorpej #endif
    486   1.7      soda 		/*
    487   1.7      soda 		 * Just do the easy case now; we'll defer the harder ones
    488   1.7      soda 		 * to Stage 2.
    489   1.7      soda 		 */
    490   1.7      soda 		if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
    491   1.1   thorpej 			l->irq = irq;
    492   1.7      soda 			l->fixup_stage = 1;
    493   1.1   thorpej 			pciirq |= 1 << irq;
    494   1.1   thorpej #ifdef PCIINTR_DEBUG
    495   1.7      soda 			printf(", assigning IRQ %d", l->irq);
    496   1.1   thorpej #endif
    497   1.1   thorpej 		}
    498   1.7      soda #ifdef PCIINTR_DEBUG
    499   1.7      soda 		printf("\n");
    500   1.7      soda #endif
    501   1.1   thorpej 	}
    502   1.4  augustss 
    503   1.1   thorpej 	/*
    504   1.1   thorpej 	 * Stage 2: Attempt to connect PIRQs which we didn't
    505   1.1   thorpej 	 * connect in Stage 1.
    506   1.1   thorpej 	 */
    507  1.20     lukem 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    508  1.23      fvdl 		if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
    509   1.5       uch 			continue;
    510   1.7      soda 		if (pciintr_bitmap_find_lowest_irq(l->bitmap & pciirq,
    511   1.7      soda 		    &l->irq)) {
    512   1.7      soda 			/*
    513   1.7      soda 			 * This IRQ is a valid PCI IRQ already
    514   1.7      soda 			 * connected to another PIRQ, and also an
    515   1.7      soda 			 * IRQ our PIRQ can use; connect it up!
    516   1.7      soda 			 */
    517   1.7      soda 			l->fixup_stage = 2;
    518   1.1   thorpej #ifdef PCIINTR_DEBUG
    519   1.7      soda 			printf("pciintr_link_fixup (stage 2): "
    520   1.7      soda 			       "assigning IRQ %d to PIRQ 0x%02x\n",
    521   1.7      soda 			       l->irq, l->clink);
    522   1.1   thorpej #endif
    523   1.1   thorpej 		}
    524   1.1   thorpej 	}
    525   1.1   thorpej 
    526   1.5       uch #ifdef PCIBIOS_IRQS_HINT
    527   1.1   thorpej 	/*
    528   1.5       uch 	 * Stage 3: The worst case. I need configuration hint that
    529   1.5       uch 	 * user supplied a mask for the PCI irqs
    530   1.1   thorpej 	 */
    531  1.20     lukem 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    532  1.23      fvdl 		if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
    533   1.5       uch 			continue;
    534   1.7      soda 		if (pciintr_bitmap_find_lowest_irq(
    535   1.8      soda 		    l->bitmap & pcibios_irqs_hint, &l->irq)) {
    536   1.7      soda 			l->fixup_stage = 3;
    537   1.5       uch #ifdef PCIINTR_DEBUG
    538   1.7      soda 			printf("pciintr_link_fixup (stage 3): "
    539   1.7      soda 			       "assigning IRQ %d to PIRQ 0x%02x\n",
    540   1.7      soda 			       l->irq, l->clink);
    541   1.5       uch #endif
    542   1.5       uch 		}
    543   1.5       uch 	}
    544   1.5       uch #endif /* PCIBIOS_IRQS_HINT */
    545   1.1   thorpej 
    546   1.1   thorpej 	return (0);
    547   1.1   thorpej }
    548   1.1   thorpej 
    549   1.1   thorpej int
    550  1.35     perry pciintr_link_route(uint16_t *pciirq)
    551   1.1   thorpej {
    552   1.1   thorpej 	struct pciintr_link_map *l;
    553   1.1   thorpej 	int rv = 0;
    554   1.1   thorpej 
    555   1.1   thorpej 	*pciirq = 0;
    556   1.1   thorpej 
    557  1.20     lukem 	SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
    558   1.7      soda 		if (l->fixup_stage == 0) {
    559  1.23      fvdl 			if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    560   1.7      soda 				/* Appropriate interrupt was not found. */
    561   1.7      soda #ifdef DIAGNOSTIC
    562   1.7      soda 				printf("pciintr_link_route: "
    563   1.7      soda 				    "PIRQ 0x%02x: no IRQ, try "
    564   1.7      soda 				    "\"options PCIBIOS_IRQS_HINT=0x%04x\"\n",
    565   1.7      soda 				    l->clink,
    566   1.7      soda 				    /* suggest irq 9/10/11, if possible */
    567   1.7      soda 				    (l->bitmap & 0x0e00) ? (l->bitmap & 0x0e00)
    568   1.7      soda 				    : l->bitmap);
    569   1.7      soda #endif
    570   1.7      soda 			} else {
    571   1.7      soda 				/* BIOS setting has no problem */
    572   1.7      soda #ifdef PCIINTR_DEBUG
    573   1.7      soda 				printf("pciintr_link_route: "
    574   1.7      soda 				    "route of PIRQ 0x%02x -> "
    575   1.7      soda 				    "IRQ %d preserved BIOS setting\n",
    576   1.7      soda 				    l->clink, l->irq);
    577   1.7      soda #endif
    578   1.7      soda 				*pciirq |= (1 << l->irq);
    579   1.7      soda 			}
    580   1.7      soda 			continue; /* nothing to do. */
    581   1.7      soda 		}
    582   1.7      soda 
    583   1.1   thorpej 		if (pciintr_icu_set_intr(pciintr_icu_tag, pciintr_icu_handle,
    584   1.1   thorpej 					 l->clink, l->irq) != 0 ||
    585   1.7      soda 		    pciintr_icu_set_trigger(pciintr_icu_tag,
    586   1.7      soda 					    pciintr_icu_handle,
    587   1.1   thorpej 					    l->irq, IST_LEVEL) != 0) {
    588   1.7      soda 			printf("pciintr_link_route: route of PIRQ 0x%02x -> "
    589   1.7      soda 			    "IRQ %d failed\n", l->clink, l->irq);
    590   1.1   thorpej 			rv = 1;
    591   1.1   thorpej 		} else {
    592   1.1   thorpej 			/*
    593   1.1   thorpej 			 * Succssfully routed interrupt.  Mark this as
    594   1.1   thorpej 			 * a PCI interrupt.
    595   1.1   thorpej 			 */
    596   1.1   thorpej 			*pciirq |= (1 << l->irq);
    597   1.1   thorpej 		}
    598   1.1   thorpej 	}
    599   1.1   thorpej 
    600   1.1   thorpej 	return (rv);
    601   1.1   thorpej }
    602   1.1   thorpej 
    603   1.1   thorpej int
    604  1.35     perry pciintr_irq_release(uint16_t *pciirq)
    605   1.1   thorpej {
    606   1.7      soda 	int i, bit;
    607  1.35     perry 	uint16_t bios_pciirq;
    608  1.30  christos 	int reg;
    609   1.1   thorpej 
    610  1.30  christos #ifdef PCIINTR_DEBUG
    611  1.30  christos 	printf("pciintr_irq_release: fixup pciirq level/edge map 0x%04x\n",
    612  1.30  christos 	    *pciirq);
    613  1.30  christos #endif
    614  1.30  christos 
    615  1.30  christos 	/* Get bios level/edge setting. */
    616  1.30  christos 	bios_pciirq = 0;
    617  1.30  christos 	for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
    618  1.30  christos 		(void)pciintr_icu_get_trigger(pciintr_icu_tag,
    619  1.30  christos 		    pciintr_icu_handle, i, &reg);
    620  1.30  christos 		if (reg == IST_LEVEL)
    621  1.30  christos 			bios_pciirq |= bit;
    622  1.30  christos 	}
    623  1.30  christos 
    624  1.30  christos #ifdef PCIINTR_DEBUG
    625  1.30  christos 	printf("pciintr_irq_release: bios  pciirq level/edge map 0x%04x\n",
    626  1.30  christos 	    bios_pciirq);
    627  1.30  christos #endif /* PCIINTR_DEBUG */
    628  1.30  christos 
    629  1.30  christos 	/* fixup final level/edge setting. */
    630  1.30  christos 	*pciirq |= bios_pciirq;
    631   1.7      soda 	for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
    632   1.7      soda 		if ((*pciirq & bit) == 0)
    633  1.30  christos 			reg = IST_EDGE;
    634  1.30  christos 		else
    635  1.30  christos 			reg = IST_LEVEL;
    636  1.30  christos 		(void) pciintr_icu_set_trigger(pciintr_icu_tag,
    637  1.30  christos 			    pciintr_icu_handle, i, reg);
    638  1.30  christos 
    639   1.1   thorpej 	}
    640   1.1   thorpej 
    641  1.30  christos #ifdef PCIINTR_DEBUG
    642  1.30  christos 	printf("pciintr_irq_release: final pciirq level/edge map 0x%04x\n",
    643  1.30  christos 	    *pciirq);
    644  1.30  christos #endif /* PCIINTR_DEBUG */
    645  1.30  christos 
    646   1.1   thorpej 	return (0);
    647   1.1   thorpej }
    648   1.1   thorpej 
    649   1.1   thorpej int
    650  1.29     kochi pciintr_header_fixup(pci_chipset_tag_t pc)
    651   1.1   thorpej {
    652   1.7      soda 	PCIBIOS_PRINTV(("------------------------------------------\n"));
    653   1.7      soda 	PCIBIOS_PRINTV(("  device vendor product pin PIRQ IRQ stage\n"));
    654   1.7      soda 	PCIBIOS_PRINTV(("------------------------------------------\n"));
    655  1.14       mcr 	pci_device_foreach(pc, pcibios_max_bus, pciintr_do_header_fixup, NULL);
    656   1.7      soda 	PCIBIOS_PRINTV(("------------------------------------------\n"));
    657   1.1   thorpej 
    658   1.5       uch 	return (0);
    659   1.5       uch }
    660   1.1   thorpej 
    661   1.5       uch void
    662  1.44  christos pciintr_do_header_fixup(pci_chipset_tag_t pc, pcitag_t tag,
    663  1.45  christos     void *context)
    664   1.5       uch {
    665   1.5       uch 	struct pcibios_intr_routing *pir;
    666   1.5       uch 	struct pciintr_link_map *l;
    667   1.5       uch 	int pin, irq, link;
    668   1.5       uch 	int bus, device, function;
    669   1.5       uch 	pcireg_t intr, id;
    670   1.5       uch 
    671   1.5       uch 	pci_decompose_tag(pc, tag, &bus, &device, &function);
    672   1.5       uch 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    673   1.5       uch 
    674   1.5       uch 	intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    675   1.5       uch 	pin = PCI_INTERRUPT_PIN(intr);
    676   1.5       uch 	irq = PCI_INTERRUPT_LINE(intr);
    677   1.1   thorpej 
    678  1.14       mcr #if 0
    679   1.5       uch 	if (pin == 0) {
    680   1.5       uch 		/*
    681   1.5       uch 		 * No interrupt used.
    682   1.5       uch 		 */
    683   1.5       uch 		return;
    684   1.5       uch 	}
    685  1.14       mcr #endif
    686   1.1   thorpej 
    687   1.5       uch 	pir = pciintr_pir_lookup(bus, device);
    688   1.5       uch 	if (pir == NULL || (link = pir->linkmap[pin - 1].link) == 0) {
    689   1.5       uch 		/*
    690   1.5       uch 		 * Interrupt not connected; no
    691   1.5       uch 		 * need to change.
    692   1.5       uch 		 */
    693   1.5       uch 		return;
    694   1.5       uch 	}
    695   1.1   thorpej 
    696   1.7      soda 	l = pciintr_link_lookup(link);
    697   1.5       uch 	if (l == NULL) {
    698   1.7      soda #ifdef PCIINTR_DEBUG
    699   1.5       uch 		/*
    700   1.7      soda 		 * No link map entry.
    701   1.7      soda 		 * Probably pciintr_icu_getclink() or pciintr_icu_get_intr()
    702   1.7      soda 		 * was failed.
    703   1.5       uch 		 */
    704   1.5       uch 		printf("pciintr_header_fixup: no entry for link 0x%02x "
    705   1.5       uch 		       "(%d:%d:%d:%c)\n", link, bus, device, function,
    706   1.5       uch 		       '@' + pin);
    707   1.7      soda #endif
    708   1.5       uch 		return;
    709   1.1   thorpej 	}
    710   1.7      soda 
    711   1.7      soda #ifdef PCIBIOSVERBOSE
    712   1.7      soda 	if (pcibiosverbose) {
    713  1.46       uwe 		PCIBIOS_PRINTV(("%03d:%02d:%d 0x%04x 0x%04x   %c  0x%02x",
    714   1.7      soda 		    bus, device, function, PCI_VENDOR(id), PCI_PRODUCT(id),
    715  1.46       uwe 		    '@' + pin, l->clink));
    716  1.23      fvdl 		if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
    717  1.46       uwe 			PCIBIOS_PRINTV(("   -"));
    718   1.7      soda 		else
    719  1.46       uwe 			PCIBIOS_PRINTV((" %3d", l->irq));
    720  1.46       uwe 		PCIBIOS_PRINTV(("  %d   ", l->fixup_stage));
    721   1.7      soda 	}
    722   1.7      soda #endif
    723   1.5       uch 
    724   1.5       uch 	/*
    725   1.5       uch 	 * IRQs 14 and 15 are reserved for PCI IDE interrupts; don't muck
    726   1.5       uch 	 * with them.
    727   1.5       uch 	 */
    728   1.7      soda 	if (irq == 14 || irq == 15) {
    729   1.7      soda 		PCIBIOS_PRINTV((" WARNING: ignored\n"));
    730   1.7      soda 		return;
    731   1.7      soda 	}
    732   1.7      soda 
    733  1.23      fvdl 	if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    734   1.7      soda 		/* Appropriate interrupt was not found. */
    735  1.10      soda 		if (pciintr_icu_tag == NULL &&
    736  1.23      fvdl 		    irq != 0 && irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    737  1.10      soda 			/*
    738  1.10      soda 			 * Do not print warning,
    739  1.10      soda 			 * if no compatible PCI ICU found,
    740  1.10      soda 			 * but the irq is already assigned by BIOS.
    741  1.10      soda 			 */
    742  1.10      soda 			PCIBIOS_PRINTV(("\n"));
    743  1.10      soda 		} else {
    744  1.10      soda 			PCIBIOS_PRINTV((" WARNING: missing IRQ\n"));
    745  1.10      soda 		}
    746   1.5       uch 		return;
    747   1.7      soda 	}
    748   1.7      soda 
    749   1.7      soda 	if (l->irq == irq) {
    750   1.7      soda 		/* don't have to reconfigure */
    751   1.7      soda 		PCIBIOS_PRINTV((" already assigned\n"));
    752   1.7      soda 		return;
    753   1.7      soda 	}
    754   1.1   thorpej 
    755  1.23      fvdl 	if (irq == 0 || irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
    756   1.7      soda 		PCIBIOS_PRINTV((" fixed up\n"));
    757   1.7      soda 	} else {
    758   1.7      soda 		/* routed by BIOS, but inconsistent */
    759  1.32    sekiya #ifdef PCI_INTR_FIXUP_FORCE
    760   1.7      soda 		/* believe PCI IRQ Routing table */
    761   1.9      soda 		PCIBIOS_PRINTV((" WARNING: overriding irq %d\n", irq));
    762   1.7      soda #else
    763  1.10      soda 		/* believe PCI Interrupt Configuration Register (default) */
    764   1.9      soda 		PCIBIOS_PRINTV((" WARNING: preserving irq %d\n", irq));
    765   1.7      soda 		return;
    766   1.1   thorpej #endif
    767   1.7      soda 	}
    768   1.1   thorpej 
    769   1.5       uch 	intr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
    770   1.5       uch 	intr |= (l->irq << PCI_INTERRUPT_LINE_SHIFT);
    771   1.5       uch 	pci_conf_write(pc, tag, PCI_INTERRUPT_REG, intr);
    772   1.1   thorpej }
    773   1.1   thorpej 
    774   1.1   thorpej int
    775  1.35     perry pci_intr_fixup(pci_chipset_tag_t pc, bus_space_tag_t iot, uint16_t *pciirq)
    776   1.1   thorpej {
    777   1.1   thorpej 	const struct pciintr_icu_table *piit = NULL;
    778   1.1   thorpej 	pcitag_t icutag;
    779   1.1   thorpej 	pcireg_t icuid;
    780  1.42  jmcneill 	int error = 0;
    781   1.1   thorpej 
    782   1.1   thorpej 	/*
    783   1.1   thorpej 	 * Attempt to initialize our PCI interrupt router.  If
    784   1.1   thorpej 	 * the PIR Table is present in ROM, use the location
    785   1.1   thorpej 	 * specified by the PIR Table, and use the compat ID,
    786   1.1   thorpej 	 * if present.  Otherwise, we have to look for the router
    787   1.1   thorpej 	 * ourselves (the PCI-ISA bridge).
    788  1.13   kanaoka 	 *
    789  1.13   kanaoka 	 * A number of buggy BIOS implementations leave the router
    790  1.13   kanaoka 	 * entry as 000:00:0, which is typically not the correct
    791  1.13   kanaoka 	 * device/function.  If the router device address is set to
    792  1.13   kanaoka 	 * this value, and the compatible router entry is undefined
    793  1.13   kanaoka 	 * (zero is the correct value to indicate undefined), then we
    794  1.13   kanaoka 	 * work on the basis it is most likely an error, and search
    795  1.13   kanaoka 	 * the entire device-space of bus 0 (but obviously starting
    796  1.13   kanaoka 	 * with 000:00:0, in case that really is the right one).
    797   1.1   thorpej 	 */
    798  1.13   kanaoka 	if (pcibios_pir_header.signature != 0 &&
    799  1.13   kanaoka 	    (pcibios_pir_header.router_bus != 0 ||
    800  1.13   kanaoka 	     PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc) != 0 ||
    801  1.13   kanaoka 	     PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc) != 0 ||
    802  1.13   kanaoka 	     pcibios_pir_header.compat_router != 0)) {
    803   1.1   thorpej 		icutag = pci_make_tag(pc, pcibios_pir_header.router_bus,
    804   1.7      soda 		    PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc),
    805   1.7      soda 		    PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc));
    806  1.28     kochi 		icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    807  1.28     kochi 		if ((piit = pciintr_icu_lookup(icuid)) == NULL) {
    808   1.1   thorpej 			/*
    809  1.28     kochi 			 * if we fail to look up an ICU at given
    810  1.28     kochi 			 * PCI address, try compat ID next.
    811   1.1   thorpej 			 */
    812  1.28     kochi 			icuid = pcibios_pir_header.compat_router;
    813  1.28     kochi 			piit = pciintr_icu_lookup(icuid);
    814   1.1   thorpej 		}
    815   1.1   thorpej 	} else {
    816   1.1   thorpej 		int device, maxdevs = pci_bus_maxdevs(pc, 0);
    817   1.1   thorpej 
    818   1.1   thorpej 		/*
    819   1.1   thorpej 		 * Search configuration space for a known interrupt
    820   1.1   thorpej 		 * router.
    821   1.1   thorpej 		 */
    822   1.1   thorpej 		for (device = 0; device < maxdevs; device++) {
    823  1.13   kanaoka 			const struct pci_quirkdata *qd;
    824  1.13   kanaoka 			int function, nfuncs;
    825  1.13   kanaoka 			pcireg_t bhlcr;
    826  1.13   kanaoka 
    827   1.1   thorpej 			icutag = pci_make_tag(pc, 0, device, 0);
    828   1.1   thorpej 			icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    829   1.1   thorpej 
    830   1.1   thorpej 			/* Invalid vendor ID value? */
    831   1.1   thorpej 			if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
    832   1.1   thorpej 				continue;
    833   1.1   thorpej 			/* XXX Not invalid, but we've done this ~forever. */
    834   1.1   thorpej 			if (PCI_VENDOR(icuid) == 0)
    835   1.1   thorpej 				continue;
    836   1.1   thorpej 
    837  1.13   kanaoka 			qd = pci_lookup_quirkdata(PCI_VENDOR(icuid),
    838  1.13   kanaoka 			    PCI_PRODUCT(icuid));
    839  1.13   kanaoka 
    840  1.13   kanaoka 			bhlcr = pci_conf_read(pc, icutag, PCI_BHLC_REG);
    841  1.13   kanaoka 			if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
    842  1.13   kanaoka 			    (qd != NULL &&
    843  1.13   kanaoka 			     (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
    844  1.13   kanaoka 				nfuncs = 8;
    845  1.13   kanaoka 			else
    846  1.13   kanaoka 				nfuncs = 1;
    847  1.13   kanaoka 
    848  1.13   kanaoka 			for (function = 0; function < nfuncs; function++) {
    849  1.13   kanaoka 				icutag = pci_make_tag(pc, 0, device, function);
    850  1.13   kanaoka 				icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
    851  1.13   kanaoka 
    852  1.13   kanaoka 				/* Invalid vendor ID value? */
    853  1.13   kanaoka 				if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
    854  1.13   kanaoka 					continue;
    855  1.13   kanaoka 				/* Not invalid, but we've done this ~forever */
    856  1.13   kanaoka 				if (PCI_VENDOR(icuid) == 0)
    857  1.13   kanaoka 					continue;
    858  1.13   kanaoka 
    859  1.13   kanaoka 				piit = pciintr_icu_lookup(icuid);
    860  1.13   kanaoka 				if (piit != NULL)
    861  1.13   kanaoka 					goto found;
    862  1.13   kanaoka 			}
    863   1.1   thorpej 		}
    864  1.13   kanaoka 
    865  1.13   kanaoka 		/*
    866  1.13   kanaoka 		 * Invalidate the ICU ID.  If we failed to find the
    867  1.13   kanaoka 		 * interrupt router (piit == NULL) we don't want to
    868  1.13   kanaoka 		 * display a spurious device address below containing
    869  1.13   kanaoka 		 * the product information of the last device we
    870  1.13   kanaoka 		 * looked at.
    871  1.13   kanaoka 		 */
    872  1.13   kanaoka 		icuid = 0;
    873  1.15       mrg found:;
    874   1.1   thorpej 	}
    875   1.1   thorpej 
    876   1.1   thorpej 	if (piit == NULL) {
    877  1.10      soda 		printf("pci_intr_fixup: no compatible PCI ICU found");
    878  1.10      soda 		if (pcibios_pir_header.signature != 0 && icuid != 0)
    879  1.10      soda 			printf(": ICU vendor 0x%04x product 0x%04x",
    880  1.10      soda 			    PCI_VENDOR(icuid), PCI_PRODUCT(icuid));
    881  1.10      soda 		printf("\n");
    882  1.10      soda #ifdef PCIBIOS_INTR_GUESS
    883  1.10      soda 		if (pciintr_link_init())
    884  1.10      soda 			return (-1);	/* non-fatal */
    885  1.10      soda 		if (pciintr_guess_irq())
    886  1.10      soda 			return (-1);	/* non-fatal */
    887  1.10      soda 		if (pciintr_header_fixup(pc))
    888  1.10      soda 			return (1);	/* fatal */
    889  1.10      soda 		return (0);		/* success! */
    890  1.10      soda #else
    891   1.1   thorpej 		return (-1);		/* non-fatal */
    892  1.10      soda #endif
    893   1.1   thorpej 	}
    894   1.1   thorpej 
    895   1.1   thorpej 	/*
    896   1.1   thorpej 	 * Initialize the PCI ICU.
    897   1.1   thorpej 	 */
    898  1.42  jmcneill 	if ((*piit->piit_init)(pc, iot, icutag, &pciintr_icu_tag,
    899  1.42  jmcneill 	    &pciintr_icu_handle) != 0)
    900   1.1   thorpej 		return (-1);		/* non-fatal */
    901   1.1   thorpej 
    902   1.1   thorpej 	/*
    903   1.1   thorpej 	 * Initialize the PCI interrupt link map.
    904   1.1   thorpej 	 */
    905  1.42  jmcneill 	if (pciintr_link_init()) {
    906  1.42  jmcneill 		error = -1;		/* non-fatal */
    907  1.42  jmcneill 		goto cleanup;
    908  1.42  jmcneill 	}
    909   1.1   thorpej 
    910   1.1   thorpej 	/*
    911   1.1   thorpej 	 * Fix up the link->IRQ mappings.
    912   1.1   thorpej 	 */
    913  1.42  jmcneill 	if (pciintr_link_fixup() != 0) {
    914  1.42  jmcneill 		error = -1;		/* non-fatal */
    915  1.42  jmcneill 		goto cleanup;
    916  1.42  jmcneill 	}
    917   1.1   thorpej 
    918   1.1   thorpej 	/*
    919   1.1   thorpej 	 * Now actually program the PCI ICU with the new
    920   1.1   thorpej 	 * routing information.
    921   1.1   thorpej 	 */
    922  1.42  jmcneill 	if (pciintr_link_route(pciirq) != 0) {
    923  1.42  jmcneill 		error = 1;		/* fatal */
    924  1.42  jmcneill 		goto cleanup;
    925  1.42  jmcneill 	}
    926   1.1   thorpej 
    927   1.1   thorpej 	/*
    928   1.1   thorpej 	 * Now that we've routed all of the PIRQs, rewrite the PCI
    929   1.1   thorpej 	 * configuration headers to reflect the new mapping.
    930   1.1   thorpej 	 */
    931  1.42  jmcneill 	if (pciintr_header_fixup(pc) != 0) {
    932  1.42  jmcneill 		error = 1;		/* fatal */
    933  1.42  jmcneill 		goto cleanup;
    934  1.42  jmcneill 	}
    935   1.1   thorpej 
    936   1.1   thorpej 	/*
    937   1.1   thorpej 	 * Free any unused PCI IRQs for ISA devices.
    938   1.1   thorpej 	 */
    939  1.42  jmcneill 	if (pciintr_irq_release(pciirq) != 0) {
    940  1.42  jmcneill 		error = -1;		/* non-fatal */
    941  1.42  jmcneill 		goto cleanup;
    942  1.42  jmcneill 	}
    943   1.1   thorpej 
    944   1.1   thorpej 	/*
    945   1.1   thorpej 	 * All done!
    946   1.1   thorpej 	 */
    947  1.42  jmcneill cleanup:
    948  1.42  jmcneill 	if (piit->piit_uninit != NULL)
    949  1.42  jmcneill 		(*piit->piit_uninit)(pciintr_icu_handle);
    950  1.42  jmcneill 	return (error);
    951   1.1   thorpej }
    952