Home | History | Annotate | Line # | Download | only in cortex
gic_v2m.c revision 1.2
      1 /* $NetBSD: gic_v2m.c,v 1.2 2018/10/30 23:59:47 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #define _INTR_PRIVATE
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: gic_v2m.c,v 1.2 2018/10/30 23:59:47 jmcneill Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/kmem.h>
     39 
     40 #include <dev/pci/pcireg.h>
     41 #include <dev/pci/pcivar.h>
     42 
     43 #include <arm/pic/picvar.h>
     44 #include <arm/cortex/gic_v2m.h>
     45 
     46 static int
     47 gic_v2m_msi_alloc_spi(struct gic_v2m_frame *frame, int count,
     48     const struct pci_attach_args *pa)
     49 {
     50 	int spi, n;
     51 
     52 	for (spi = frame->frame_base;
     53 	     spi < frame->frame_base + frame->frame_count; ) {
     54 		if (frame->frame_pa[spi] == NULL) {
     55 			for (n = 1; n < count; n++)
     56 				if (frame->frame_pa[spi + n] != NULL)
     57 					goto next_spi;
     58 
     59 			for (n = 0; n < count; n++)
     60 				frame->frame_pa[spi + n] = pa;
     61 
     62 			return spi;
     63 		}
     64 next_spi:
     65 		spi += count;
     66 	}
     67 
     68 	return -1;
     69 }
     70 
     71 static void
     72 gic_v2m_msi_free_spi(struct gic_v2m_frame *frame, int spi)
     73 {
     74 	frame->frame_pa[spi] = NULL;
     75 }
     76 
     77 static int
     78 gic_v2m_msi_available_spi(struct gic_v2m_frame *frame)
     79 {
     80 	int spi, n;
     81 
     82 	for (spi = frame->frame_base, n = 0;
     83 	     spi < frame->frame_base + frame->frame_count;
     84 	     spi++) {
     85 		if (frame->frame_pa[spi] == NULL)
     86 			n++;
     87 	}
     88 
     89 	return n;
     90 }
     91 
     92 static void
     93 gic_v2m_msi_enable(struct gic_v2m_frame *frame, int spi)
     94 {
     95 	const struct pci_attach_args *pa = frame->frame_pa[spi];
     96 	pci_chipset_tag_t pc = pa->pa_pc;
     97 	pcitag_t tag = pa->pa_tag;
     98 	pcireg_t ctl;
     99 	int off;
    100 
    101 	if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
    102 		panic("gic_v2m_msi_enable: device is not MSI-capable");
    103 
    104 	const uint64_t addr = frame->frame_reg + GIC_MSI_SETSPI;
    105 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    106 	if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
    107 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO,
    108 		    addr & 0xffffffff);
    109 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI,
    110 		    (addr >> 32) & 0xffffffff);
    111 		pci_conf_write(pc, tag, off + PCI_MSI_MDATA64, spi);
    112 	} else {
    113 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR,
    114 		    addr & 0xffffffff);
    115 		pci_conf_write(pc, tag, off + PCI_MSI_MDATA, spi);
    116 	}
    117 	ctl |= PCI_MSI_CTL_MSI_ENABLE;
    118 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    119 }
    120 
    121 static void
    122 gic_v2m_msi_disable(struct gic_v2m_frame *frame, int spi)
    123 {
    124 	const struct pci_attach_args *pa = frame->frame_pa[spi];
    125 	pci_chipset_tag_t pc = pa->pa_pc;
    126 	pcitag_t tag = pa->pa_tag;
    127 	pcireg_t ctl;
    128 	int off;
    129 
    130 	if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
    131 		panic("gic_v2m_msi_enable: device is not MSI-capable");
    132 
    133 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    134 	ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
    135 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    136 }
    137 
    138 static pci_intr_handle_t *
    139 gic_v2m_msi_alloc(struct arm_pci_msi *msi, int *count,
    140     const struct pci_attach_args *pa, bool exact)
    141 {
    142 	struct gic_v2m_frame * const frame = msi->msi_priv;
    143 	pci_intr_handle_t *vectors;
    144 	int n, off;
    145 
    146 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &off, NULL))
    147 		return NULL;
    148 
    149 	const int avail = gic_v2m_msi_available_spi(frame);
    150 	if (exact && *count > avail)
    151 		return NULL;
    152 
    153 	while (*count > avail) {
    154 		if (avail < *count)
    155 			(*count) >>= 1;
    156 	}
    157 	if (*count == 0)
    158 		return NULL;
    159 
    160 	const int spi_base = gic_v2m_msi_alloc_spi(frame, *count, pa);
    161 	if (spi_base == -1)
    162 		return NULL;
    163 
    164 	vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
    165 	for (n = 0; n < *count; n++) {
    166 		const int spi = spi_base + n;
    167 		vectors[n] = ARM_PCI_INTR_MSI |
    168 		    __SHIFTIN(spi, ARM_PCI_INTR_IRQ) |
    169 		    __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
    170 		    __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
    171 
    172 		gic_v2m_msi_enable(frame, spi);
    173 	}
    174 
    175 	return vectors;
    176 }
    177 
    178 static void *
    179 gic_v2m_msi_intr_establish(struct arm_pci_msi *msi,
    180     pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg)
    181 {
    182 	struct gic_v2m_frame * const frame = msi->msi_priv;
    183 
    184 	const int spi = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
    185 	const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? IST_MPSAFE : 0;
    186 
    187 	return pic_establish_intr(frame->frame_pic, spi, ipl,
    188 	    IST_EDGE | mpsafe, func, arg);
    189 }
    190 
    191 static void
    192 gic_v2m_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
    193     int count)
    194 {
    195 	struct gic_v2m_frame * const frame = msi->msi_priv;
    196 	int n;
    197 
    198 	for (n = 0; n < count; n++) {
    199 		const int spi = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
    200 		gic_v2m_msi_disable(frame, spi);
    201 		gic_v2m_msi_free_spi(frame, spi);
    202 		struct intrsource * const is =
    203 		    frame->frame_pic->pic_sources[spi];
    204 		if (is != NULL)
    205 			pic_disestablish_source(is);
    206 	}
    207 }
    208 
    209 int
    210 gic_v2m_init(struct gic_v2m_frame *frame, device_t dev, uint32_t frame_id)
    211 {
    212 	struct arm_pci_msi *msi = &frame->frame_msi;
    213 
    214 	msi->msi_dev = dev;
    215 	msi->msi_priv = frame;
    216 	msi->msi_alloc = gic_v2m_msi_alloc;
    217 	msi->msi_intr_establish = gic_v2m_msi_intr_establish;
    218 	msi->msi_intr_release = gic_v2m_msi_intr_release;
    219 
    220 	return arm_pci_msi_add(msi);
    221 }
    222