/*
* 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.
*/
/*
* TODO
*
* - PBA (Pending Bit Array) support
* - HyperTransport mapping support
*/
/*
* Establish an MSI handle.
* If multiple MSI handles are required to be established, a device driver
* must call this function for each handle.
*/
void *
x86_pci_msi_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
int level, int (*func)(void *), void *arg, const char *xname)
{
struct pic *pic;
pic = msipic_find_msi_pic(MSI_INT_DEV(ih));
if (pic == NULL) {
DPRINTF(("pci_intr_handler has no msi_pic\n"));
return NULL;
}
/*
* Disestablish an MSI handle.
* If multiple MSI handles are required to be disestablished, a device driver
* must call this function for each handle.
*/
void
x86_pci_msi_disestablish(pci_chipset_tag_t pc, void *cookie)
{
/*
* Establish an MSI-X handle.
* If multiple MSI-X handles are required to be established, a device driver
* must call this function for each handle.
*/
void *
x86_pci_msix_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
int level, int (*func)(void *), void *arg, const char *xname)
{
struct pic *pic;
pic = msipic_find_msi_pic(MSI_INT_DEV(ih));
if (pic == NULL) {
DPRINTF(("pci_intr_handler has no msi_pic\n"));
return NULL;
}
/*
* Disestablish an MSI-X handle.
* If multiple MSI-X handles are required to be disestablished, a device driver
* must call this function for each handle.
*/
void
x86_pci_msix_disestablish(pci_chipset_tag_t pc, void *cookie)
{
pci_msi_common_disestablish(pc, cookie);
}
#ifdef __HAVE_PCI_MSI_MSIX
/*****************************************************************************/
/*
* extern for MI code.
*/
/*
* This function is used by device drivers like pci_intr_map().
*
* "ihps" is the array of vector numbers which MSI used instead of IRQ number.
* "count" must be power of 2.
* "count" can decrease if struct intrsource cannot be allocated.
* if count == 0, return non-zero value.
*/
int
pci_msi_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
int *count)
{
int hw_max;
/* MSI vector count must be power of 2. */
KASSERT(*count > 0);
KASSERT(((*count - 1) & *count) == 0);
hw_max = pci_msi_count(pa->pa_pc, pa->pa_tag);
if (hw_max == 0)
return ENODEV;
if (*count > hw_max) {
DPRINTF(("cut off MSI count to %d\n", hw_max));
*count = hw_max; /* cut off hw_max */
}
return x86_pci_msi_alloc(ihps, count, pa);
}
/*
* This function is used by device drivers like pci_intr_map().
*
* "ihps" is the array of vector numbers which MSI used instead of IRQ number.
* "count" must be power of 2.
* "count" can not decrease.
* If "count" struct intrsources cannot be allocated, return non-zero value.
*/
int
pci_msi_alloc_exact(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
int count)
{
int hw_max;
/* MSI vector count must be power of 2. */
KASSERT(count > 0);
KASSERT(((count - 1) & count) == 0);
hw_max = pci_msi_count(pa->pa_pc, pa->pa_tag);
if (hw_max == 0)
return ENODEV;
if (count > hw_max) {
DPRINTF(("over hardware max MSI count %d\n", hw_max));
return EINVAL;
}
/*
* This function is used by device drivers like pci_intr_map().
*
* "ihps" is the array of vector numbers which MSI-X used instead of IRQ number.
* "count" can decrease if enough struct intrsources cannot be allocated.
* if count == 0, return non-zero value.
*/
int
pci_msix_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
int *count)
{
int hw_max;
KASSERT(*count > 0);
hw_max = pci_msix_count(pa->pa_pc, pa->pa_tag);
if (hw_max == 0)
return ENODEV;
if (*count > hw_max) {
DPRINTF(("cut off MSI-X count to %d\n", hw_max));
*count = hw_max; /* cut off hw_max */
}
return x86_pci_msix_alloc(ihps, count, pa);
}
/*
* This function is used by device drivers like pci_intr_map().
*
* "ihps" is the array of vector numbers which MSI-X used instead of IRQ number.
* "count" can not decrease.
* If "count" struct intrsource cannot be allocated, return non-zero value.
*/
int
pci_msix_alloc_exact(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
int count)
{
int hw_max;
KASSERT(count > 0);
hw_max = pci_msix_count(pa->pa_pc, pa->pa_tag);
if (hw_max == 0)
return ENODEV;
if (count > hw_max) {
DPRINTF(("over hardware max MSI-X count %d\n", hw_max));
return EINVAL;
}
/*
* This function is used by device drivers like pci_intr_map().
* Furthermore, this function can map each handle to an MSI-X table index.
*
* "ihps" is the array of vector numbers which MSI-X used instead of IRQ number.
* "count" can not decrease.
* "map" size must be equal to "count".
* If "count" struct intrsource cannot be allocated, return non-zero value.
* e.g.
* If "map" = { 1, 4, 0 },
* 1st handle is bound to MSI-X index 1
* 2nd handle is bound to MSI-X index 4
* 3rd handle is bound to MSI-X index 0
*/
int
pci_msix_alloc_map(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
u_int *table_indexes, int count)
{
int hw_max, i, j;
KASSERT(count > 0);
hw_max = pci_msix_count(pa->pa_pc, pa->pa_tag);
if (hw_max == 0)
return ENODEV;
if (count > hw_max) {
DPRINTF(("over hardware max MSI-X count %d\n", hw_max));
return EINVAL;
}
/* check not to duplicate table_index */
for (i = 0; i < count; i++) {
u_int basing = table_indexes[i];
KASSERT(table_indexes[i] < PCI_MSIX_MAX_VECTORS);
if (basing >= hw_max) {
DPRINTF(("table index is over hardware max MSI-X index %d\n",
hw_max - 1));
return EINVAL;
}
for (j = i + 1; j < count; j++) {
if (basing == table_indexes[j]) {
DPRINTF(("MSI-X table index duplicated\n"));
return EINVAL;
}
}
}