#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pci_intr_machdep.c,v 1.51 2020/08/01 12:36:35 jdolecek Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#include <sys/cpu.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/kmem.h>
#include "ioapic.h"
#include "eisa.h"
#include "acpica.h"
#include "opt_mpbios.h"
#include "opt_acpi.h"
#include "opt_pci.h"
#include <dev/pci/pcivar.h>
#include <machine/i82489reg.h>
#if NIOAPIC > 0 || NACPICA > 0
#include <machine/i82093reg.h>
#include <machine/i82093var.h>
#include <machine/mpconfig.h>
#include <machine/mpbiosvar.h>
#include <machine/pic.h>
#include <x86/pci/pci_msi_machdep.h>
#else
#include <machine/i82093var.h>
#endif
#ifdef MPBIOS
#include <machine/mpbiosvar.h>
#endif
#if NACPICA > 0
#include <machine/mpacpi.h>
#endif
int
pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
{
pci_intr_pin_t pin = pa->pa_intrpin;
pci_intr_line_t line = pa->pa_intrline;
pci_chipset_tag_t ipc, pc = pa->pa_pc;
#if NIOAPIC > 0 || NACPICA > 0
pci_intr_pin_t rawpin = pa->pa_rawintrpin;
int bus, dev, func;
#endif
for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
if ((ipc->pc_present & PCI_OVERRIDE_INTR_MAP) == 0)
continue;
return (*ipc->pc_ov->ov_intr_map)(ipc->pc_ctx, pa, ihp);
}
if (pin == 0) {
goto bad;
}
*ihp = 0;
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) {
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;
}
}
#endif
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
*ihp = line;
return 0;
bad:
*ihp = -1;
return 1;
}
const char *
pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
size_t len)
{
pci_chipset_tag_t ipc;
for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
if ((ipc->pc_present & PCI_OVERRIDE_INTR_STRING) == 0)
continue;
return (*ipc->pc_ov->ov_intr_string)(ipc->pc_ctx, pc, ih,
buf, len);
}
#if defined(__HAVE_PCI_MSI_MSIX)
if (INT_VIA_MSI(ih))
return x86_pci_msi_string(pc, ih, buf, len);
#endif
return intr_string(ih & ~MPSAFE_MASK, buf, len);
}
const struct evcnt *
pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
{
pci_chipset_tag_t ipc;
for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
if ((ipc->pc_present & PCI_OVERRIDE_INTR_EVCNT) == 0)
continue;
return (*ipc->pc_ov->ov_intr_evcnt)(ipc->pc_ctx, pc, ih);
}
return NULL;
}
int
pci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
int attr, uint64_t data)
{
switch (attr) {
case PCI_INTR_MPSAFE:
if (data) {
*ih |= MPSAFE_MASK;
} else {
*ih &= ~MPSAFE_MASK;
}
return 0;
default:
return ENODEV;
}
}
static int
pci_intr_find_intx_irq(pci_intr_handle_t ih, int *irq, struct pic **pic,
int *pin)
{
KASSERT(irq != NULL);
KASSERT(pic != NULL);
KASSERT(pin != NULL);
*pic = &i8259_pic;
*pin = *irq = APIC_IRQ_LEGACY_IRQ(ih);
#if NIOAPIC > 0
if (ih & APIC_INT_VIA_APIC) {
struct ioapic_softc *ioapic;
ioapic = ioapic_find(APIC_IRQ_APIC(ih));
if (ioapic == NULL)
return ENOENT;
*pic = &ioapic->sc_pic;
*pin = APIC_IRQ_PIN(ih);
*irq = APIC_IRQ_LEGACY_IRQ(ih);
if (*irq < 0 || *irq >= NUM_LEGACY_IRQS)
*irq = -1;
}
#endif
return 0;
}
static void *
pci_intr_establish_xname_internal(pci_chipset_tag_t pc, pci_intr_handle_t ih,
int level, int (*func)(void *), void *arg, const char *xname)
{
int pin, irq;
struct pic *pic;
bool mpsafe;
pci_chipset_tag_t ipc;
for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
if ((ipc->pc_present & PCI_OVERRIDE_INTR_ESTABLISH) == 0)
continue;
return (*ipc->pc_ov->ov_intr_establish)(ipc->pc_ctx,
pc, ih, level, func, arg);
}
#ifdef __HAVE_PCI_MSI_MSIX
if (INT_VIA_MSI(ih)) {
if (MSI_INT_IS_MSIX(ih))
return x86_pci_msix_establish(pc, ih, level, func, arg,
xname);
else
return x86_pci_msi_establish(pc, ih, level, func, arg,
xname);
}
#endif
if (pci_intr_find_intx_irq(ih, &irq, &pic, &pin)) {
aprint_normal("%s: bad pic %d\n", __func__,
APIC_IRQ_APIC(ih));
return NULL;
}
mpsafe = ((ih & MPSAFE_MASK) != 0);
return intr_establish_xname(irq, pic, pin, IST_LEVEL, level, func, arg,
mpsafe, xname);
}
void *
pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
int level, int (*func)(void *), void *arg)
{
return pci_intr_establish_xname_internal(pc, ih, level, func, arg, "unknown");
}
void *
pci_intr_establish_xname(pci_chipset_tag_t pc, pci_intr_handle_t ih,
int level, int (*func)(void *), void *arg, const char *xname)
{
return pci_intr_establish_xname_internal(pc, ih, level, func, arg, xname);
}
void
pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
{
pci_chipset_tag_t ipc;
for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
if ((ipc->pc_present & PCI_OVERRIDE_INTR_DISESTABLISH) == 0)
continue;
(*ipc->pc_ov->ov_intr_disestablish)(ipc->pc_ctx, pc, cookie);
return;
}
intr_disestablish(cookie);
}
#if NIOAPIC > 0
#ifdef __HAVE_PCI_MSI_MSIX
pci_intr_type_t
pci_intr_type(pci_chipset_tag_t pc, pci_intr_handle_t ih)
{
if (INT_VIA_MSI(ih)) {
if (MSI_INT_IS_MSIX(ih))
return PCI_INTR_TYPE_MSIX;
else
return PCI_INTR_TYPE_MSI;
} else {
return PCI_INTR_TYPE_INTX;
}
}
static const char *
x86_pci_intx_create_intrid(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
size_t len)
{
#if !defined(XENPV)
int pin, irq;
struct pic *pic;
KASSERT(!INT_VIA_MSI(ih));
pic = &i8259_pic;
pin = irq = APIC_IRQ_LEGACY_IRQ(ih);
if (pci_intr_find_intx_irq(ih, &irq, &pic, &pin)) {
aprint_normal("%s: bad pic %d\n", __func__,
APIC_IRQ_APIC(ih));
return NULL;
}
return intr_create_intrid(irq, pic, pin, buf, len);
#else
return pci_intr_string(pc, ih, buf, len);
#endif
}
static void
x86_pci_intx_release(pci_chipset_tag_t pc, pci_intr_handle_t *pih)
{
char intrstr_buf[INTRIDBUF];
const char *intrstr;
intrstr = pci_intr_string(NULL, *pih, intrstr_buf, sizeof(intrstr_buf));
mutex_enter(&cpu_lock);
intr_free_io_intrsource(intrstr);
mutex_exit(&cpu_lock);
kmem_free(pih, sizeof(*pih));
}
int
pci_intx_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **pih)
{
struct intrsource *isp;
pci_intr_handle_t *handle;
int error;
char intrstr_buf[INTRIDBUF];
const char *intrstr;
handle = kmem_zalloc(sizeof(*handle), KM_SLEEP);
if (pci_intr_map(pa, handle) != 0) {
aprint_normal("cannot set up pci_intr_handle_t\n");
error = EINVAL;
goto error;
}
intrstr = x86_pci_intx_create_intrid(pa->pa_pc, *handle, intrstr_buf,
sizeof(intrstr_buf));
mutex_enter(&cpu_lock);
isp = intr_allocate_io_intrsource(intrstr);
mutex_exit(&cpu_lock);
if (isp == NULL) {
aprint_normal("can't allocate io_intersource\n");
error = ENOMEM;
goto error;
}
*pih = handle;
return 0;
error:
kmem_free(handle, sizeof(*handle));
return error;
}
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;
intx_count = msi_count = msix_count = 0;
if (counts == NULL) {
msix_count = 1;
msi_count = 1;
intx_count = 1;
} else {
switch (max_type) {
case PCI_INTR_TYPE_MSIX:
msix_count = counts[PCI_INTR_TYPE_MSIX];
case PCI_INTR_TYPE_MSI:
msi_count = counts[PCI_INTR_TYPE_MSI];
case PCI_INTR_TYPE_INTX:
intx_count = counts[PCI_INTR_TYPE_INTX];
break;
default:
return EINVAL;
}
}
if (counts != NULL)
memset(counts, 0, sizeof(counts[0]) * PCI_INTR_TYPE_SIZE);
error = EINVAL;
if (msix_count == -1)
msix_count = pci_msix_count(pa->pa_pc, pa->pa_tag);
if (msix_count > 0) {
error = pci_msix_alloc_exact(pa, ihps, msix_count);
if (error == 0) {
if (counts != NULL)
counts[PCI_INTR_TYPE_MSIX] = msix_count;
goto out;
}
}
if (msi_count == -1)
msi_count = pci_msi_count(pa->pa_pc, pa->pa_tag);
if (msi_count > 0) {
error = pci_msi_alloc_exact(pa, ihps, msi_count);
if (error == 0) {
if (counts != NULL)
counts[PCI_INTR_TYPE_MSI] = msi_count;
goto out;
}
}
if (intx_count != 0) {
error = pci_intx_alloc(pa, ihps);
if (error == 0) {
if (counts != NULL)
counts[PCI_INTR_TYPE_INTX] = 1;
}
}
out:
return error;
}
void
pci_intr_release(pci_chipset_tag_t pc, pci_intr_handle_t *pih, int count)
{
if (pih == NULL)
return;
if (INT_VIA_MSI(*pih)) {
if (MSI_INT_IS_MSIX(*pih))
return x86_pci_msix_release(pc, pih, count);
else
return x86_pci_msi_release(pc, pih, count);
} else {
KASSERT(count == 1);
return x86_pci_intx_release(pc, pih);
}
}
#endif
#endif