Home | History | Annotate | Line # | Download | only in cortex
gic_v2m.c revision 1.5.4.3
      1 /* $NetBSD: gic_v2m.c,v 1.5.4.3 2020/04/08 14:07:28 martin 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.5.4.3 2020/04/08 14:07:28 martin 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_disable: 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 void
    139 gic_v2m_msix_enable(struct gic_v2m_frame *frame, int spi, int msix_vec,
    140     bus_space_tag_t bst, bus_space_handle_t bsh)
    141 {
    142 	const struct pci_attach_args *pa = frame->frame_pa[spi];
    143 	pci_chipset_tag_t pc = pa->pa_pc;
    144 	pcitag_t tag = pa->pa_tag;
    145 	pcireg_t ctl;
    146 	int off;
    147 
    148 	if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
    149 		panic("gic_v2m_msix_enable: device is not MSI-X-capable");
    150 
    151 	const uint64_t addr = frame->frame_reg + GIC_MSI_SETSPI;
    152 	const uint64_t entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
    153 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO, (uint32_t)addr);
    154 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI, (uint32_t)(addr >> 32));
    155 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_DATA, spi);
    156 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, 0);
    157 
    158 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    159 	ctl |= PCI_MSIX_CTL_ENABLE;
    160 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    161 }
    162 
    163 static void
    164 gic_v2m_msix_disable(struct gic_v2m_frame *frame, int spi)
    165 {
    166 	const struct pci_attach_args *pa = frame->frame_pa[spi];
    167 	pci_chipset_tag_t pc = pa->pa_pc;
    168 	pcitag_t tag = pa->pa_tag;
    169 	pcireg_t ctl;
    170 	int off;
    171 
    172 	if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
    173 		panic("gic_v2m_msix_disable: device is not MSI-X-capable");
    174 
    175 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    176 	ctl &= ~PCI_MSIX_CTL_ENABLE;
    177 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    178 }
    179 
    180 static pci_intr_handle_t *
    181 gic_v2m_msi_alloc(struct arm_pci_msi *msi, int *count,
    182     const struct pci_attach_args *pa, bool exact)
    183 {
    184 	struct gic_v2m_frame * const frame = msi->msi_priv;
    185 	pci_intr_handle_t *vectors;
    186 	int n, off;
    187 
    188 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &off, NULL))
    189 		return NULL;
    190 
    191 	const int avail = gic_v2m_msi_available_spi(frame);
    192 	if (exact && *count > avail)
    193 		return NULL;
    194 
    195 	while (*count > avail) {
    196 		if (avail < *count)
    197 			(*count) >>= 1;
    198 	}
    199 	if (*count == 0)
    200 		return NULL;
    201 
    202 	const int spi_base = gic_v2m_msi_alloc_spi(frame, *count, pa);
    203 	if (spi_base == -1)
    204 		return NULL;
    205 
    206 	vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
    207 	for (n = 0; n < *count; n++) {
    208 		const int spi = spi_base + n;
    209 		vectors[n] = ARM_PCI_INTR_MSI |
    210 		    __SHIFTIN(spi, ARM_PCI_INTR_IRQ) |
    211 		    __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
    212 		    __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
    213 
    214 		gic_v2m_msi_enable(frame, spi);
    215 	}
    216 
    217 	return vectors;
    218 }
    219 
    220 static pci_intr_handle_t *
    221 gic_v2m_msix_alloc(struct arm_pci_msi *msi, u_int *table_indexes, int *count,
    222     const struct pci_attach_args *pa, bool exact)
    223 {
    224 	struct gic_v2m_frame * const frame = msi->msi_priv;
    225 	pci_intr_handle_t *vectors;
    226 	bus_space_tag_t bst;
    227 	bus_space_handle_t bsh;
    228 	bus_size_t bsz;
    229 	uint32_t table_offset, table_size;
    230 	int n, off, bar, error;
    231 	pcireg_t tbl;
    232 
    233 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &off, NULL))
    234 		return NULL;
    235 
    236 	const int avail = gic_v2m_msi_available_spi(frame);
    237 	if (exact && *count > avail)
    238 		return NULL;
    239 
    240 	while (*count > avail) {
    241 		if (avail < *count)
    242 			(*count) >>= 1;
    243 	}
    244 	if (*count == 0)
    245 		return NULL;
    246 
    247 	tbl = pci_conf_read(pa->pa_pc, pa->pa_tag, off + PCI_MSIX_TBLOFFSET);
    248 	bar = PCI_BAR0 + (4 * (tbl & PCI_MSIX_TBLBIR_MASK));
    249 	table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
    250 	table_size = pci_msix_count(pa->pa_pc, pa->pa_tag) * PCI_MSIX_TABLE_ENTRY_SIZE;
    251 	if (table_size == 0)
    252 		return NULL;
    253 
    254 	error = pci_mapreg_submap(pa, bar, pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar),
    255 	    BUS_SPACE_MAP_LINEAR, roundup(table_size, PAGE_SIZE), table_offset,
    256 	    &bst, &bsh, NULL, &bsz);
    257 	if (error)
    258 		return NULL;
    259 
    260 	const int spi_base = gic_v2m_msi_alloc_spi(frame, *count, pa);
    261 	if (spi_base == -1) {
    262 		bus_space_unmap(bst, bsh, bsz);
    263 		return NULL;
    264 	}
    265 
    266 	vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
    267 	for (n = 0; n < *count; n++) {
    268 		const int spi = spi_base + n;
    269 		const int msix_vec = table_indexes ? table_indexes[n] : n;
    270 		vectors[msix_vec] = ARM_PCI_INTR_MSIX |
    271 		    __SHIFTIN(spi, ARM_PCI_INTR_IRQ) |
    272 		    __SHIFTIN(msix_vec, ARM_PCI_INTR_MSI_VEC) |
    273 		    __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
    274 
    275 		gic_v2m_msix_enable(frame, spi, msix_vec, bst, bsh);
    276 	}
    277 
    278 	bus_space_unmap(bst, bsh, bsz);
    279 
    280 	return vectors;
    281 }
    282 
    283 static void *
    284 gic_v2m_msi_intr_establish(struct arm_pci_msi *msi,
    285     pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg, const char *xname)
    286 {
    287 	struct gic_v2m_frame * const frame = msi->msi_priv;
    288 
    289 	const int spi = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
    290 	const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? IST_MPSAFE : 0;
    291 
    292 	return pic_establish_intr(frame->frame_pic, spi, ipl,
    293 	    IST_EDGE | mpsafe, func, arg, xname);
    294 }
    295 
    296 static void
    297 gic_v2m_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
    298     int count)
    299 {
    300 	struct gic_v2m_frame * const frame = msi->msi_priv;
    301 	int n;
    302 
    303 	for (n = 0; n < count; n++) {
    304 		const int spi = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
    305 		if (pih[n] & ARM_PCI_INTR_MSIX)
    306 			gic_v2m_msix_disable(frame, spi);
    307 		if (pih[n] & ARM_PCI_INTR_MSI)
    308 			gic_v2m_msi_disable(frame, spi);
    309 		gic_v2m_msi_free_spi(frame, spi);
    310 		struct intrsource * const is =
    311 		    frame->frame_pic->pic_sources[spi];
    312 		if (is != NULL)
    313 			pic_disestablish_source(is);
    314 	}
    315 }
    316 
    317 int
    318 gic_v2m_init(struct gic_v2m_frame *frame, device_t dev, uint32_t frame_id)
    319 {
    320 	struct arm_pci_msi *msi = &frame->frame_msi;
    321 
    322 	msi->msi_dev = dev;
    323 	msi->msi_priv = frame;
    324 	msi->msi_alloc = gic_v2m_msi_alloc;
    325 	msi->msix_alloc = gic_v2m_msix_alloc;
    326 	msi->msi_intr_establish = gic_v2m_msi_intr_establish;
    327 	msi->msi_intr_release = gic_v2m_msi_intr_release;
    328 
    329 	return arm_pci_msi_add(msi);
    330 }
    331