/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1999, by UCHIYAMA Yasushi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the developer may NOT be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef PCIBIOS_IRQS_HINT
int pcibios_irqs_hint = PCIBIOS_IRQS_HINT;
#endif
struct pciintr_link_map *pciintr_link_lookup(int);
struct pciintr_link_map *pciintr_link_alloc(struct pcibios_intr_routing *,
int);
struct pcibios_intr_routing *pciintr_pir_lookup(int, int);
static int pciintr_bitmap_count_irq(int, int *);
static int pciintr_bitmap_find_lowest_irq(int, int *);
int pciintr_link_init (void);
#ifdef PCIBIOS_INTR_GUESS
int pciintr_guess_irq(void);
#endif
int pciintr_link_fixup(void);
int pciintr_link_route(uint16_t *);
int pciintr_irq_release(uint16_t *);
int pciintr_header_fixup(pci_chipset_tag_t);
void pciintr_do_header_fixup(pci_chipset_tag_t, pcitag_t, void*);
struct pciintr_link_map *
pciintr_link_alloc(struct pcibios_intr_routing *pir, int pin)
{
int link = pir->linkmap[pin].link, clink, irq;
struct pciintr_link_map *l, *lstart;
if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
/*
* Get the canonical link value for this entry.
*/
if (pciintr_icu_getclink(pciintr_icu_tag, pciintr_icu_handle,
link, &clink) != 0) {
/*
* ICU doesn't understand the link value.
* Just ignore this PIR entry.
*/
#ifdef DIAGNOSTIC
printf("pciintr_link_alloc: bus %d device %d: "
"link 0x%02x invalid\n",
pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link);
#endif
return (NULL);
}
/*
* Check the link value by asking the ICU for the
* canonical link value.
* Also, determine if this PIRQ is mapped to an IRQ.
*/
if (pciintr_icu_get_intr(pciintr_icu_tag, pciintr_icu_handle,
clink, &irq) != 0) {
/*
* ICU doesn't understand the canonical link value.
* Just ignore this PIR entry.
*/
#ifdef DIAGNOSTIC
printf("pciintr_link_alloc: "
"bus %d device %d link 0x%02x: "
"PIRQ 0x%02x invalid\n",
pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link,
clink);
#endif
return (NULL);
}
}
l = malloc(sizeof(*l), M_DEVBUF, M_WAITOK | M_ZERO);
l->link = link;
l->bitmap = pir->linkmap[pin].bitmap;
if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
l->clink = clink;
l->irq = irq; /* maybe X86_PCI_INTERRUPT_LINE_NO_CONNECTION */
} else {
l->clink = link; /* only for PCIBIOSVERBOSE diagnostic */
l->irq = X86_PCI_INTERRUPT_LINE_NO_CONNECTION;
}
struct pcibios_intr_routing *
pciintr_pir_lookup(int bus, int device)
{
struct pcibios_intr_routing *pir;
int entry;
if (pcibios_pir_table == NULL)
return (NULL);
for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
pir = &pcibios_pir_table[entry];
if (pir->bus == bus &&
PIR_DEVFUNC_DEVICE(pir->device) == device)
return (pir);
}
return (NULL);
}
static int
pciintr_bitmap_count_irq(int irq_bitmap, int *irqp)
{
int i, bit, count = 0, irq = X86_PCI_INTERRUPT_LINE_NO_CONNECTION;
if (irq_bitmap != 0) {
for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
if (irq_bitmap & bit) {
irq = i;
count++;
}
}
}
*irqp = irq;
return (count);
}
static int
pciintr_bitmap_find_lowest_irq(int irq_bitmap, int *irqp)
{
int i, bit;
if (irq_bitmap != 0) {
for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
if (irq_bitmap & bit) {
*irqp = i;
return (1); /* found */
}
}
}
return (0); /* not found */
}
int
pciintr_link_init(void)
{
int entry, pin, link;
struct pcibios_intr_routing *pir;
struct pciintr_link_map *l;
if (pcibios_pir_table == NULL) {
/* No PIR table; can't do anything. */
printf("pciintr_link_init: no PIR table\n");
return (1);
}
SIMPLEQ_INIT(&pciintr_link_map_list);
for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
pir = &pcibios_pir_table[entry];
for (pin = 0; pin < PCI_INTERRUPT_PIN_MAX; pin++) {
link = pir->linkmap[pin].link;
if (link == 0) {
/* No connection for this pin. */
continue;
}
/*
* Multiple devices may be wired to the same
* interrupt; check to see if we've seen this
* one already. If not, allocate a new link
* map entry and stuff it in the map.
*/
l = pciintr_link_lookup(link);
if (l == NULL) {
(void) pciintr_link_alloc(pir, pin);
} else if (pir->linkmap[pin].bitmap != l->bitmap) {
/*
* violates PCI IRQ Routing Table Specification
*/
#ifdef DIAGNOSTIC
printf("pciintr_link_init: "
"bus %d device %d link 0x%02x: "
"bad irq bitmap 0x%04x, "
"should be 0x%04x\n",
pir->bus, PIR_DEVFUNC_DEVICE(pir->device),
link, pir->linkmap[pin].bitmap, l->bitmap);
#endif
/* safer value. */
l->bitmap &= pir->linkmap[pin].bitmap;
/* XXX - or, should ignore this entry? */
}
}
}
return (0);
}
#ifdef PCIBIOS_INTR_GUESS
/*
* No compatible PCI ICU found.
* Hopes the BIOS already setup the ICU.
*/
int
pciintr_guess_irq(void)
{
struct pciintr_link_map *l;
int irq, guessed = 0;
/*
* Stage 1: If only one IRQ is available for the link, use it.
*/
SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
continue;
if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
l->irq = irq;
l->fixup_stage = 1;
#ifdef PCIINTR_DEBUG
printf("pciintr_guess_irq (stage 1): "
"guessing PIRQ 0x%02x to be IRQ %d\n",
l->clink, l->irq);
#endif
guessed = 1;
}
}
int
pciintr_link_fixup(void)
{
struct pciintr_link_map *l;
int irq;
uint16_t pciirq = 0;
/*
* First stage: Attempt to connect PIRQs which aren't
* yet connected.
*/
SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
/*
* Interrupt is already connected. Don't do
* anything to it.
* In this case, l->fixup_stage == 0.
*/
pciirq |= 1 << l->irq;
#ifdef PCIINTR_DEBUG
printf("pciintr_link_fixup: PIRQ 0x%02x already "
"connected to IRQ %d\n", l->clink, l->irq);
#endif
continue;
}
/*
* Interrupt isn't connected. Attempt to assign it to an IRQ.
*/
#ifdef PCIINTR_DEBUG
printf("pciintr_link_fixup: PIRQ 0x%02x not connected",
l->clink);
#endif
/*
* Just do the easy case now; we'll defer the harder ones
* to Stage 2.
*/
if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
l->irq = irq;
l->fixup_stage = 1;
pciirq |= 1 << irq;
#ifdef PCIINTR_DEBUG
printf(", assigning IRQ %d", l->irq);
#endif
}
#ifdef PCIINTR_DEBUG
printf("\n");
#endif
}
/*
* Stage 2: Attempt to connect PIRQs which we didn't
* connect in Stage 1.
*/
SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
continue;
if (pciintr_bitmap_find_lowest_irq(l->bitmap & pciirq,
&l->irq)) {
/*
* This IRQ is a valid PCI IRQ already
* connected to another PIRQ, and also an
* IRQ our PIRQ can use; connect it up!
*/
l->fixup_stage = 2;
#ifdef PCIINTR_DEBUG
printf("pciintr_link_fixup (stage 2): "
"assigning IRQ %d to PIRQ 0x%02x\n",
l->irq, l->clink);
#endif
}
}
#ifdef PCIBIOS_IRQS_HINT
/*
* Stage 3: The worst case. I need configuration hint that
* user supplied a mask for the PCI irqs
*/
SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
if (l->irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION)
continue;
if (pciintr_bitmap_find_lowest_irq(
l->bitmap & pcibios_irqs_hint, &l->irq)) {
l->fixup_stage = 3;
#ifdef PCIINTR_DEBUG
printf("pciintr_link_fixup (stage 3): "
"assigning IRQ %d to PIRQ 0x%02x\n",
l->irq, l->clink);
#endif
}
}
#endif /* PCIBIOS_IRQS_HINT */
return (0);
}
int
pciintr_link_route(uint16_t *pciirq)
{
struct pciintr_link_map *l;
int rv = 0;
*pciirq = 0;
SIMPLEQ_FOREACH(l, &pciintr_link_map_list, list) {
if (l->fixup_stage == 0) {
if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
/* Appropriate interrupt was not found. */
#ifdef DIAGNOSTIC
printf("pciintr_link_route: "
"PIRQ 0x%02x: no IRQ, try "
"\"options PCIBIOS_IRQS_HINT=0x%04x\"\n",
l->clink,
/* suggest irq 9/10/11, if possible */
(l->bitmap & 0x0e00) ? (l->bitmap & 0x0e00)
: l->bitmap);
#endif
} else {
/* BIOS setting has no problem */
#ifdef PCIINTR_DEBUG
printf("pciintr_link_route: "
"route of PIRQ 0x%02x -> "
"IRQ %d preserved BIOS setting\n",
l->clink, l->irq);
#endif
*pciirq |= (1 << l->irq);
}
continue; /* nothing to do. */
}
if (pciintr_icu_set_intr(pciintr_icu_tag, pciintr_icu_handle,
l->clink, l->irq) != 0 ||
pciintr_icu_set_trigger(pciintr_icu_tag,
pciintr_icu_handle,
l->irq, IST_LEVEL) != 0) {
printf("pciintr_link_route: route of PIRQ 0x%02x -> "
"IRQ %d failed\n", l->clink, l->irq);
rv = 1;
} else {
/*
* Successfully routed interrupt. Mark this as
* a PCI interrupt.
*/
*pciirq |= (1 << l->irq);
}
}
return (rv);
}
int
pciintr_irq_release(uint16_t *pciirq)
{
int i, bit;
uint16_t bios_pciirq;
int reg;
/* Get bios level/edge setting. */
bios_pciirq = 0;
for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
(void)pciintr_icu_get_trigger(pciintr_icu_tag,
pciintr_icu_handle, i, ®);
if (reg == IST_LEVEL)
bios_pciirq |= bit;
}
#if 0
if (pin == 0) {
/*
* No interrupt used.
*/
return;
}
#endif
pir = pciintr_pir_lookup(bus, device);
if (pir == NULL || (link = pir->linkmap[pin - 1].link) == 0) {
/*
* Interrupt not connected; no
* need to change.
*/
return;
}
l = pciintr_link_lookup(link);
if (l == NULL) {
#ifdef PCIINTR_DEBUG
/*
* No link map entry.
* Probably pciintr_icu_getclink() or pciintr_icu_get_intr()
* was failed.
*/
printf("pciintr_header_fixup: no entry for link 0x%02x "
"(%d:%d:%d:%c)\n", link, bus, device, function,
'@' + pin);
#endif
return;
}
/*
* IRQs 14 and 15 are reserved for PCI IDE interrupts; don't muck
* with them.
*/
if (irq == 14 || irq == 15) {
PCIBIOS_PRINTV((" WARNING: ignored\n"));
return;
}
if (l->irq == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
/* Appropriate interrupt was not found. */
if (pciintr_icu_tag == NULL &&
irq != 0 && irq != X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
/*
* Do not print warning,
* if no compatible PCI ICU found,
* but the irq is already assigned by BIOS.
*/
PCIBIOS_PRINTV(("\n"));
} else {
PCIBIOS_PRINTV((" WARNING: missing IRQ\n"));
}
return;
}
if (l->irq == irq) {
/* don't have to reconfigure */
PCIBIOS_PRINTV((" already assigned\n"));
return;
}
/*
* Attempt to initialize our PCI interrupt router. If
* the PIR Table is present in ROM, use the location
* specified by the PIR Table, and use the compat ID,
* if present. Otherwise, we have to look for the router
* ourselves (the PCI-ISA bridge).
*
* A number of buggy BIOS implementations leave the router
* entry as 000:00:0, which is typically not the correct
* device/function. If the router device address is set to
* this value, and the compatible router entry is undefined
* (zero is the correct value to indicate undefined), then we
* work on the basis it is most likely an error, and search
* the entire device-space of bus 0 (but obviously starting
* with 000:00:0, in case that really is the right one).
*/
if (pcibios_pir_header.signature != 0 &&
(pcibios_pir_header.router_bus != 0 ||
PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc) != 0 ||
PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc) != 0 ||
pcibios_pir_header.compat_router != 0)) {
icutag = pci_make_tag(pc, pcibios_pir_header.router_bus,
PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc),
PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc));
icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
if ((piit = pciintr_icu_lookup(icuid)) == NULL) {
/*
* if we fail to look up an ICU at given
* PCI address, try compat ID next.
*/
icuid = pcibios_pir_header.compat_router;
piit = pciintr_icu_lookup(icuid);
}
} else {
int device, maxdevs = pci_bus_maxdevs(pc, 0);
/*
* Search configuration space for a known interrupt
* router.
*/
for (device = 0; device < maxdevs; device++) {
const struct pci_quirkdata *qd;
int function, nfuncs;
pcireg_t bhlcr;
/* Invalid vendor ID value? */
if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
continue;
/* XXX Not invalid, but we've done this ~forever. */
if (PCI_VENDOR(icuid) == 0)
continue;
for (function = 0; function < nfuncs; function++) {
icutag = pci_make_tag(pc, 0, device, function);
icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
/* Invalid vendor ID value? */
if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
continue;
/* Not invalid, but we've done this ~forever */
if (PCI_VENDOR(icuid) == 0)
continue;
/*
* Invalidate the ICU ID. If we failed to find the
* interrupt router (piit == NULL) we don't want to
* display a spurious device address below containing
* the product information of the last device we
* looked at.
*/
icuid = 0;
found:;
}
/*
* Initialize the PCI ICU.
*/
if ((*piit->piit_init)(pc, iot, icutag, &pciintr_icu_tag,
&pciintr_icu_handle) != 0)
return (-1); /* non-fatal */
/*
* Initialize the PCI interrupt link map.
*/
if (pciintr_link_init()) {
error = -1; /* non-fatal */
goto cleanup;
}
/*
* Fix up the link->IRQ mappings.
*/
if (pciintr_link_fixup() != 0) {
error = -1; /* non-fatal */
goto cleanup;
}
/*
* Now actually program the PCI ICU with the new
* routing information.
*/
if (pciintr_link_route(pciirq) != 0) {
error = 1; /* fatal */
goto cleanup;
}
/*
* Now that we've routed all of the PIRQs, rewrite the PCI
* configuration headers to reflect the new mapping.
*/
if (pciintr_header_fixup(pc) != 0) {
error = 1; /* fatal */
goto cleanup;
}
/*
* Free any unused PCI IRQs for ISA devices.
*/
if (pciintr_irq_release(pciirq) != 0) {
error = -1; /* non-fatal */
goto cleanup;
}
/*
* All done!
*/
cleanup:
if (piit->piit_uninit != NULL)
(*piit->piit_uninit)(pciintr_icu_handle);
return (error);
}