#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rumpdev_pci.c,v 1.5 2015/05/17 13:51:31 pooka Exp $");
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/atomic.h>
#include <dev/pci/pcivar.h>
#include "pci_user.h"
void
pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
{
}
int
pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
{
return 32;
}
pcitag_t
pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
{
pcitag_t pt;
int *tag;
unsigned csr;
int rv;
CTASSERT(sizeof(pt) >= sizeof(int));
tag = (int *)&pt;
*tag = (bus << 16) | (device << 8) | (function << 0);
rv = rumpcomp_pci_confread(bus, device, function,
PCI_COMMAND_STATUS_REG, &csr);
if (rv == 0 && (csr & PCI_COMMAND_MEM_ENABLE) == 0) {
rumpcomp_pci_confwrite(bus, device, function,
PCI_COMMAND_STATUS_REG, csr | PCI_COMMAND_MEM_ENABLE);
}
return pt;
}
pcireg_t
pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
{
unsigned int rv;
int bus, device, fun;
pci_decompose_tag(pc, tag, &bus, &device, &fun);
rumpcomp_pci_confread(bus, device, fun, reg, &rv);
return rv;
}
void
pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
{
int bus, device, fun;
pci_decompose_tag(pc, tag, &bus, &device, &fun);
rumpcomp_pci_confwrite(bus, device, fun, reg, data);
}
void
pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
int *bp, int *dp, int *fp)
{
int *t = (int *)&tag;
*bp = (*t >> 16) & 0xff;
*dp = (*t >> 8) & 0xff;
*fp = (*t >> 0) & 0xff;
}
int
pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
{
static unsigned int intrhandle;
unsigned cookie;
int rv;
cookie = atomic_inc_uint_nv(&intrhandle);
rv = rumpcomp_pci_irq_map(pa->pa_bus,
pa->pa_device, pa->pa_function, pa->pa_intrline, cookie);
if (rv == 0)
*ih = cookie;
return 0;
}
const char *
pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih,
char *buf, size_t buflen)
{
snprintf(buf, buflen, "pausebreak");
return buf;
}
void *
pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
int level, int (*func)(void *), void *arg)
{
return rumpcomp_pci_irq_establish(ih, func, arg);
}
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:
return 0;
default:
return ENODEV;
}
}
void
pci_intr_disestablish(pci_chipset_tag_t pc, void *not_your_above_ih)
{
panic("%s: unimplemented", __func__);
}
#ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pciidereg.h>
#include <dev/pci/pciidevar.h>
void *
pciide_machdep_compat_intr_establish(device_t dev,
const struct pci_attach_args *pa, int chan,
int (*func)(void *), void *arg)
{
pci_intr_handle_t ih;
struct pci_attach_args mypa = *pa;
mypa.pa_intrline = PCIIDE_COMPAT_IRQ(chan);
if (pci_intr_map(&mypa, &ih) != 0)
return NULL;
return rumpcomp_pci_irq_establish(ih, func, arg);
}
__strong_alias(pciide_machdep_compat_intr_disestablish,pci_intr_disestablish);
#endif