/*-
* Copyright (c) 1997, 1998, 2009 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) 1996 Christopher G. Demetriou. All rights reserved.
* Copyright (c) 1994 Charles M. Hannum. 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. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Charles M. Hannum.
* 4. The name of the author 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 ``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 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.
*/
/*
* Machine-specific functions for PCI autoconfiguration.
*
* On PCs, there are two methods of generating PCI configuration cycles.
* We try to detect the appropriate mechanism for this machine and set
* up a few function pointers to access the correct method directly.
*
* The configuration method can be hard-coded in the config file by
* using `options PCI_CONF_MODE=N', where `N' is the configuration mode
* as defined section 3.6.4.1, `Generating Configuration Cycles'.
*/
if (pin > PCI_INTERRUPT_PIN_MAX) {
aprint_normal("pci_intr_map: bad interrupt pin %d\n", pin);
goto bad;
}
#if NIOAPIC > 0 || NACPICA > 0
KASSERT(rawpin >= PCI_INTERRUPT_PIN_A);
KASSERT(rawpin <= PCI_INTERRUPT_PIN_D);
pci_decompose_tag(pc, pa->pa_tag, &bus, &dev, &func);
if (mp_busses != NULL) {
/*
* Note: PCI_INTERRUPT_PIN_A == 1 where intr_find_mpmapping
* wants pci bus_pin encoding which uses INT_A == 0.
*/
if (intr_find_mpmapping(bus,
(dev << 2) | (rawpin - PCI_INTERRUPT_PIN_A), ihp) == 0) {
if (APIC_IRQ_LEGACY_IRQ(*ihp) == 0)
*ihp |= line;
return 0;
}
/*
* No explicit PCI mapping found. This is not fatal,
* we'll try the ISA (or possibly EISA) mappings next.
*/
}
#endif
/*
* Section 6.2.4, `Miscellaneous Functions', says that 255 means
* `unknown' or `no connection' on a PC. We assume that a device with
* `no connection' either doesn't have an interrupt (in which case the
* pin number should be 0, and would have been noticed above), or
* wasn't configured by the BIOS (in which case we punt, since there's
* no real way we can know how the interrupt lines are mapped in the
* hardware).
*
* XXX
* Since IRQ 0 is only used by the clock, and we can't actually be sure
* that the BIOS did its job, we also recognize that as meaning that
* the BIOS has not configured the device.
*/
if (line == 0 || line == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
aprint_normal("pci_intr_map: no mapping for pin %c (line=%02x)\n",
'@' + pin, line);
goto bad;
} else {
if (line >= NUM_LEGACY_IRQS) {
aprint_normal("pci_intr_map: bad interrupt line %d\n", line);
goto bad;
}
if (line == 2) {
aprint_normal("pci_intr_map: changed line 2 to line 9\n");
line = 9;
}
}
#if NIOAPIC > 0 || NACPICA > 0
if (mp_busses != NULL) {
if (intr_find_mpmapping(mp_isa_bus, line, ihp) == 0) {
if ((*ihp & 0xff) == 0)
*ihp |= line;
return 0;
}
#if NEISA > 0
if (intr_find_mpmapping(mp_eisa_bus, line, ihp) == 0) {
if ((*ihp & 0xff) == 0)
*ihp |= line;
return 0;
}
#endif
aprint_normal("pci_intr_map: bus %d dev %d func %d pin %d; line %d\n",
bus, dev, func, pin, line);
aprint_normal("pci_intr_map: no MP mapping found\n");
}
#endif
/*
* Interrupt handler allocation utility. This function calls each allocation
* function as specified by arguments.
* Currently callee functions are pci_intx_alloc(), pci_msi_alloc_exact(),
* and pci_msix_alloc_exact().
* pa : pci_attach_args
* ihps : interrupt handlers
* counts : The array of number of required interrupt handlers.
* It is overwritten by allocated the number of handlers.
* CAUTION: The size of counts[] must be PCI_INTR_TYPE_SIZE.
* max_type : "max" type of using interrupts. See below.
* e.g.
* If you want to use 5 MSI-X, 1 MSI, or INTx, you use "counts" as
* int counts[PCI_INTR_TYPE_SIZE];
* counts[PCI_INTR_TYPE_MSIX] = 5;
* counts[PCI_INTR_TYPE_MSI] = 1;
* counts[PCI_INTR_TYPE_INTX] = 1;
* error = pci_intr_alloc(pa, ihps, counts, PCI_INTR_TYPE_MSIX);
*
* If you want to use hardware max number MSI-X or 1 MSI,
* and not to use INTx, you use "counts" as
* int counts[PCI_INTR_TYPE_SIZE];
* counts[PCI_INTR_TYPE_MSIX] = -1;
* counts[PCI_INTR_TYPE_MSI] = 1;
* counts[PCI_INTR_TYPE_INTX] = 0;
* error = pci_intr_alloc(pa, ihps, counts, PCI_INTR_TYPE_MSIX);
*
* If you want to use 3 MSI or INTx, you can use "counts" as
* int counts[PCI_INTR_TYPE_SIZE];
* counts[PCI_INTR_TYPE_MSI] = 3;
* counts[PCI_INTR_TYPE_INTX] = 1;
* error = pci_intr_alloc(pa, ihps, counts, PCI_INTR_TYPE_MSI);
*
* If you want to use 1 MSI or INTx (probably most general usage),
* you can simply use this API like
* below
* error = pci_intr_alloc(pa, ihps, NULL, 0);
* ^ ignored
*/
int
pci_intr_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
int *counts, pci_intr_type_t max_type)
{
int error;
int intx_count, msi_count, msix_count;