/*
* Copyright (c) 2015 Internet Initiative Japan Inc.
* 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.
*
* 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.
*/
/*
* A Pseudo pic for single MSI/MSI-X device.
* The pic and MSI/MSI-X device are distinbuished by "devid". The "devid"
* is managed by below "dev_seqs".
*/
struct msipic {
struct msipic_pci_info mp_i;
int mp_devid; /* The device id for the MSI/MSI-X device. */
char mp_pic_name[MSIPICNAMEBUF]; /* The MSI/MSI-X device's name. */
/*
* This struct managements "devid" to use the same "devid" for the device
* re-attached. If the device's bus number and device number and function
* number are equal, it is assumed re-attached.
*/
struct dev_last_used_seq {
bool ds_using;
int ds_bus;
int ds_dev;
int ds_fun;
};
/* The number of MSI/MSI-X devices supported by system. */
#define NUM_MSI_DEVS 256
/* Record devids to use the same devid when the device is re-attached. */
static struct dev_last_used_seq dev_seqs[NUM_MSI_DEVS];
static int msipic_allocate_common_msi_devid(const struct pci_attach_args *);
static void msipic_release_common_msi_devid(int);
/*
* Return new "devid" for the device attached first.
* Return the same "devid" for the device re-attached after dettached once.
* Return -1 if the number of attached MSI/MSI-X devices is over NUM_MSI_DEVS.
*/
static int
msipic_allocate_common_msi_devid(const struct pci_attach_args *pa)
{
pci_chipset_tag_t pc;
pcitag_t tag;
int bus, dev, fun, i;
KASSERT(mutex_owned(&msipic_list_lock));
pc = pa->pa_pc;
tag = pa->pa_tag;
pci_decompose_tag(pc, tag, &bus, &dev, &fun);
/* if the device was once attached, use same devid */
for (i = 0; i < NUM_MSI_DEVS; i++) {
/* skip host bridge */
if (dev_seqs[i].ds_bus == 0
&& dev_seqs[i].ds_dev == 0
&& dev_seqs[i].ds_fun == 0)
break;
if (dev_seqs[i].ds_bus == bus
&& dev_seqs[i].ds_dev == dev
&& dev_seqs[i].ds_fun == fun) {
dev_seqs[i].ds_using = true;
return i;
}
}
for (i = 0; i < NUM_MSI_DEVS; i++) {
if (dev_seqs[i].ds_using == 0) {
dev_seqs[i].ds_using = true;
dev_seqs[i].ds_bus = bus;
dev_seqs[i].ds_dev = dev;
dev_seqs[i].ds_fun = fun;
return i;
}
}
DPRINTF(("too many MSI devices.\n"));
return -1;
}
/*
* Set the "devid" unused, but keep reserving the "devid" to reuse when
* the device is re-attached.
*/
static void
msipic_release_common_msi_devid(int devid)
{
/*
* Return the msi_pic whose device is already registered.
* If the device is not registered yet, return NULL.
*/
struct pic *
msipic_find_msi_pic(int devid)
{
struct pic *msipic;
if (msipic->mp_i.mp_xen_pirq != NULL) {
KASSERT(msipic->mp_i.mp_veccnt > 0);
#ifdef DIAGNOSTIC
for (int i = 0; i < msipic->mp_i.mp_veccnt; i++) {
KASSERT(msipic->mp_i.mp_xen_pirq[i] == 0);
}
#endif
kmem_free(msipic->mp_i.mp_xen_pirq,
sizeof(*msipic->mp_i.mp_xen_pirq) * msipic->mp_i.mp_veccnt);
}
kmem_free(msipic, sizeof(*msipic));
kmem_free(msi_pic, sizeof(*msi_pic));
}
/*
* The pic is MSI/MSI-X pic or not.
*/
bool
msipic_is_msi_pic(struct pic *pic)
{
return (pic->pic_msipic != NULL);
}
/*
* Return the MSI/MSI-X devid which is unique for each devices.
*/
int
msipic_get_devid(struct pic *pic)
{
KASSERT(msipic_is_msi_pic(pic));
return pic->pic_msipic->mp_devid;
}
/*
* Return the PCI bus/dev/func info for the device.
*/
const struct msipic_pci_info *
msipic_get_pci_info(struct pic *pic)
{
KASSERT(msipic_is_msi_pic(pic));
return &pic->pic_msipic->mp_i;
}
#define MSI_MSICTL_ENABLE 1
#define MSI_MSICTL_DISABLE 0
static void
msi_set_msictl_enablebit(struct pic *pic, int msi_vec, int flag)
{
pci_chipset_tag_t pc;
struct pci_attach_args *pa;
pcitag_t tag;
pcireg_t ctl;
int off, err __diagused;
pc = NULL;
pa = &pic->pic_msipic->mp_pa;
tag = pa->pa_tag;
err = pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL);
KASSERT(err != 0);
/*
* MSI can establish only one vector at once.
* So, use whole device mask bit instead of a vector mask bit.
*/
ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
if (flag == MSI_MSICTL_ENABLE)
ctl |= PCI_MSI_CTL_MSI_ENABLE;
else
ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
/*
* Do not use pic->hwunmask() immediately after pic->delroute().
* It is required to use pic->addroute() before pic->hwunmask().
*/
static void
msi_hwunmask(struct pic *pic, int msi_vec)
{
static void
msi_addroute(struct pic *pic, struct cpu_info *ci,
int unused, int idt_vec, int type)
{
pci_chipset_tag_t pc;
struct pci_attach_args *pa;
pcitag_t tag;
#ifndef XENPV
pcireg_t addr, data;
#endif
pcireg_t ctl;
int off, err __diagused;
pc = NULL;
pa = &pic->pic_msipic->mp_pa;
tag = pa->pa_tag;
err = pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL);
KASSERT(err != 0);
ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
#ifndef XENPV
/*
* See Intel 64 and IA-32 Architectures Software Developer's Manual
* Volume 3 10.11 Message Signalled Interrupts.
*/
/*
* "cpuid" for MSI address is local APIC ID. In NetBSD, the ID is
* the same as ci->ci_cpuid.
*/
addr = LAPIC_MSIADDR_BASE | __SHIFTIN(ci->ci_cpuid,
LAPIC_MSIADDR_DSTID_MASK);
/* If trigger mode is edge, it don't care level for trigger mode. */
data = __SHIFTIN(idt_vec, LAPIC_VECTOR_MASK)
| LAPIC_TRIGMODE_EDGE | LAPIC_DLMODE_FIXED;
/*
* The size of the message data register is 16bit if the extended
* message data is not implemented. If it's 16bit and the per-vector
* masking is not capable, the location of the upper 16bit is out of
* the MSI capability structure's range. The PCI spec says the upper
* 16bit is driven to 0 if the message data register is 16bit. It's the
* spec, so it's OK just to write it regardless of the value of the
* upper 16bit.
*/
if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO, addr);
pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI, 0);
pci_conf_write(pc, tag, off + PCI_MSI_MDATA64, data);
} else {
pci_conf_write(pc, tag, off + PCI_MSI_MADDR, addr);
pci_conf_write(pc, tag, off + PCI_MSI_MDATA, data);
}
#endif /* !XENPV */
ctl |= PCI_MSI_CTL_MSI_ENABLE;
#ifdef XENPV
pci_conf_write16(pc, tag, off + PCI_MSI_CTL + 2, ctl >> 16);
#else
pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
#endif
}
/*
* Do not use pic->hwunmask() immediately after pic->delroute().
* It is required to use pic->addroute() before pic->hwunmask().
*/
static void
msi_delroute(struct pic *pic, struct cpu_info *ci,
int msi_vec, int idt_vec, int type)
{
msi_hwmask(pic, msi_vec);
}
/*
* Template for MSI pic.
* .pic_msipic is set later in construct_msi_pic().
*/
static const struct pic msi_pic_tmpl = {
.pic_type = PIC_MSI,
.pic_vecbase = 0,
.pic_apicid = 0,
.pic_lock = __SIMPLELOCK_UNLOCKED, /* not used for msi_pic */
.pic_hwmask = msi_hwmask,
.pic_hwunmask = msi_hwunmask,
.pic_addroute = msi_addroute,
.pic_delroute = msi_delroute,
.pic_intr_get_devname = x86_intr_get_devname,
.pic_intr_get_assigned = x86_intr_get_assigned,
.pic_intr_get_count = x86_intr_get_count,
};
/*
* Do not use pic->hwunmask() immediately after pic->delroute().
* It is required to use pic->addroute() before pic->hwunmask().
*/
static void
msix_hwunmask(struct pic *pic, int msix_vec)
{
/*
* See Intel 64 and IA-32 Architectures Software Developer's Manual
* Volume 3 10.11 Message Signalled Interrupts.
*/
/*
* "cpuid" for MSI-X address is local APIC ID. In NetBSD, the ID is
* the same as ci->ci_cpuid.
*/
addr = LAPIC_MSIADDR_BASE | __SHIFTIN(ci->ci_cpuid,
LAPIC_MSIADDR_DSTID_MASK);
/* If trigger mode is edge, it don't care level for trigger mode. */
data = __SHIFTIN(idt_vec, LAPIC_VECTOR_MASK)
| LAPIC_TRIGMODE_EDGE | LAPIC_DLMODE_FIXED;
ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
if (ctl & PCI_MSIX_CTL_FUNCMASK) {
ctl &= ~PCI_MSIX_CTL_FUNCMASK;
}
ctl |= PCI_MSIX_CTL_ENABLE;
#ifdef XENPV
pci_conf_write16(pc, tag, off + PCI_MSIX_CTL + 2, ctl >> 16);
#else
pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
#endif
}
/*
* Do not use pic->hwunmask() immediately after pic->delroute().
* It is required to use pic->addroute() before pic->hwunmask().
*/
static void
msix_delroute(struct pic *pic, struct cpu_info *ci,
int msix_vec, int vec, int type)
{
msix_hwmask(pic, msix_vec);
}
/*
* Template for MSI-X pic.
* .pic_msipic is set later in construct_msix_pic().
*/
static const struct pic msix_pic_tmpl = {
.pic_type = PIC_MSIX,
.pic_vecbase = 0,
.pic_apicid = 0,
.pic_lock = __SIMPLELOCK_UNLOCKED, /* not used for msix_pic */
.pic_hwmask = msix_hwmask,
.pic_hwunmask = msix_hwunmask,
.pic_addroute = msix_addroute,
.pic_delroute = msix_delroute,
.pic_intr_get_devname = x86_intr_get_devname,
.pic_intr_get_assigned = x86_intr_get_assigned,
.pic_intr_get_count = x86_intr_get_count,
};