Home | History | Annotate | Line # | Download | only in cortex
gicv3_its.c revision 1.29
      1 /* $NetBSD: gicv3_its.c,v 1.29 2020/12/11 21:40:50 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: gicv3_its.c,v 1.29 2020/12/11 21:40:50 jmcneill Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/kmem.h>
     39 #include <sys/bus.h>
     40 #include <sys/cpu.h>
     41 #include <sys/bitops.h>
     42 
     43 #include <uvm/uvm.h>
     44 
     45 #include <dev/pci/pcireg.h>
     46 #include <dev/pci/pcivar.h>
     47 
     48 #include <arm/pic/picvar.h>
     49 #include <arm/cortex/gicv3_its.h>
     50 
     51 /*
     52  * ITS translation table sizes
     53  */
     54 #define	GITS_COMMANDS_SIZE	0x1000
     55 #define	GITS_COMMANDS_ALIGN	0x10000
     56 
     57 #define	GITS_ITT_ALIGN		0x100
     58 
     59 /*
     60  * IIDR values used for errata
     61  */
     62 #define GITS_IIDR_PID_CAVIUM_THUNDERX	0xa1
     63 #define GITS_IIDR_IMP_CAVIUM		0x34c
     64 #define	GITS_IIDR_CAVIUM_ERRATA_MASK	(GITS_IIDR_Implementor|GITS_IIDR_ProductID|GITS_IIDR_Variant)
     65 #define	GITS_IIDR_CAVIUM_ERRATA_VALUE							\
     66 		(__SHIFTIN(GITS_IIDR_IMP_CAVIUM, GITS_IIDR_Implementor) |		\
     67 		 __SHIFTIN(GITS_IIDR_PID_CAVIUM_THUNDERX, GITS_IIDR_ProductID) |	\
     68 		 __SHIFTIN(0, GITS_IIDR_Variant))
     69 
     70 static const char * gits_cache_type[] = {
     71 	[GITS_Cache_DEVICE_nGnRnE]	= "Device-nGnRnE",
     72 	[GITS_Cache_NORMAL_NC]		= "Non-cacheable",
     73 	[GITS_Cache_NORMAL_RA_WT]	= "Cacheable RA WT",
     74 	[GITS_Cache_NORMAL_RA_WB]	= "Cacheable RA WB",
     75 	[GITS_Cache_NORMAL_WA_WT]	= "Cacheable WA WT",
     76 	[GITS_Cache_NORMAL_WA_WB]	= "Cacheable WA WB",
     77 	[GITS_Cache_NORMAL_RA_WA_WT]	= "Cacheable RA WA WT",
     78 	[GITS_Cache_NORMAL_RA_WA_WB]	= "Cacheable RA WA WB",
     79 };
     80 
     81 static const char * gits_share_type[] = {
     82 	[GITS_Shareability_NS]		= "Non-shareable",
     83 	[GITS_Shareability_IS]		= "Inner shareable",
     84 	[GITS_Shareability_OS]		= "Outer shareable",
     85 	[3]				= "(Reserved)",
     86 };
     87 
     88 static inline uint32_t
     89 gits_read_4(struct gicv3_its *its, bus_size_t reg)
     90 {
     91 	return bus_space_read_4(its->its_bst, its->its_bsh, reg);
     92 }
     93 
     94 static inline void
     95 gits_write_4(struct gicv3_its *its, bus_size_t reg, uint32_t val)
     96 {
     97 	bus_space_write_4(its->its_bst, its->its_bsh, reg, val);
     98 }
     99 
    100 static inline uint64_t
    101 gits_read_8(struct gicv3_its *its, bus_size_t reg)
    102 {
    103 	return bus_space_read_8(its->its_bst, its->its_bsh, reg);
    104 }
    105 
    106 static inline void
    107 gits_write_8(struct gicv3_its *its, bus_size_t reg, uint64_t val)
    108 {
    109 	bus_space_write_8(its->its_bst, its->its_bsh, reg, val);
    110 }
    111 
    112 static inline void
    113 gits_command(struct gicv3_its *its, const struct gicv3_its_command *cmd)
    114 {
    115 	uint64_t cwriter;
    116 	u_int woff;
    117 
    118 	cwriter = gits_read_8(its, GITS_CWRITER);
    119 	woff = cwriter & GITS_CWRITER_Offset;
    120 
    121 #if _BYTE_ORDER == _BIG_ENDIAN
    122 	uint64_t *dw = (uint64_t *)(its->its_cmd.base + woff);
    123 	for (int i = 0; i < __arraycount(cmd->dw); i++)
    124 		dw[i] = htole64(cmd->dw[i]);
    125 #else
    126 	memcpy(its->its_cmd.base + woff, cmd->dw, sizeof(cmd->dw));
    127 #endif
    128 	bus_dmamap_sync(its->its_dmat, its->its_cmd.map, woff, sizeof(cmd->dw), BUS_DMASYNC_PREWRITE);
    129 
    130 	woff += sizeof(cmd->dw);
    131 	if (woff == its->its_cmd.len)
    132 		woff = 0;
    133 
    134 	gits_write_8(its, GITS_CWRITER, woff);
    135 }
    136 
    137 static inline void
    138 gits_command_mapc(struct gicv3_its *its, uint16_t icid, uint64_t rdbase, bool v)
    139 {
    140 	struct gicv3_its_command cmd;
    141 
    142 	KASSERT((rdbase & 0xffff) == 0);
    143 
    144 	/*
    145 	 * Map a collection table entry (ICID) to the target redistributor (RDbase).
    146 	 */
    147 	memset(&cmd, 0, sizeof(cmd));
    148 	cmd.dw[0] = GITS_CMD_MAPC;
    149 	cmd.dw[2] = icid;
    150 	if (v) {
    151 		cmd.dw[2] |= rdbase;
    152 		cmd.dw[2] |= __BIT(63);
    153 	}
    154 
    155 	gits_command(its, &cmd);
    156 }
    157 
    158 static inline void
    159 gits_command_mapd(struct gicv3_its *its, uint32_t deviceid, uint64_t itt_addr, u_int size, bool v)
    160 {
    161 	struct gicv3_its_command cmd;
    162 
    163 	KASSERT((itt_addr & 0xff) == 0);
    164 
    165 	/*
    166 	 * Map a device table entry (DeviceID) to its associated ITT (ITT_addr).
    167 	 */
    168 	memset(&cmd, 0, sizeof(cmd));
    169 	cmd.dw[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
    170 	cmd.dw[1] = size;
    171 	if (v) {
    172 		cmd.dw[2] = itt_addr | __BIT(63);
    173 	}
    174 
    175 	gits_command(its, &cmd);
    176 }
    177 
    178 static inline void
    179 gits_command_mapti(struct gicv3_its *its, uint32_t deviceid, uint32_t eventid, uint32_t pintid, uint16_t icid)
    180 {
    181 	struct gicv3_its_command cmd;
    182 
    183 	/*
    184 	 * Map the event defined by EventID and DeviceID to its associated ITE, defined by ICID and pINTID
    185 	 * in the ITT associated with DeviceID.
    186 	 */
    187 	memset(&cmd, 0, sizeof(cmd));
    188 	cmd.dw[0] = GITS_CMD_MAPTI | ((uint64_t)deviceid << 32);
    189 	cmd.dw[1] = eventid | ((uint64_t)pintid << 32);
    190 	cmd.dw[2] = icid;
    191 
    192 	gits_command(its, &cmd);
    193 }
    194 
    195 static inline void
    196 gits_command_movi(struct gicv3_its *its, uint32_t deviceid, uint32_t eventid, uint16_t icid)
    197 {
    198 	struct gicv3_its_command cmd;
    199 
    200 	/*
    201 	 * Update the ICID field in the ITT entry for the event defined by DeviceID and
    202 	 * EventID.
    203 	 */
    204 	memset(&cmd, 0, sizeof(cmd));
    205 	cmd.dw[0] = GITS_CMD_MOVI | ((uint64_t)deviceid << 32);
    206 	cmd.dw[1] = eventid;
    207 	cmd.dw[2] = icid;
    208 
    209 	gits_command(its, &cmd);
    210 }
    211 
    212 static inline void
    213 gits_command_inv(struct gicv3_its *its, uint32_t deviceid, uint32_t eventid)
    214 {
    215 	struct gicv3_its_command cmd;
    216 
    217 	/*
    218 	 * Ensure any caching in the redistributors associated with the specified
    219 	 * EventID is consistent with the LPI configuration tables.
    220 	 */
    221 	memset(&cmd, 0, sizeof(cmd));
    222 	cmd.dw[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
    223 	cmd.dw[1] = eventid;
    224 
    225 	gits_command(its, &cmd);
    226 }
    227 
    228 static inline void
    229 gits_command_invall(struct gicv3_its *its, uint16_t icid)
    230 {
    231 	struct gicv3_its_command cmd;
    232 
    233 	/*
    234 	 * Ensure any caching associated with this ICID is consistent with LPI
    235 	 * configuration tables for all redistributors.
    236 	 */
    237 	memset(&cmd, 0, sizeof(cmd));
    238 	cmd.dw[0] = GITS_CMD_INVALL;
    239 	cmd.dw[2] = icid;
    240 
    241 	gits_command(its, &cmd);
    242 }
    243 
    244 static inline void
    245 gits_command_sync(struct gicv3_its *its, uint64_t rdbase)
    246 {
    247 	struct gicv3_its_command cmd;
    248 
    249 	KASSERT((rdbase & 0xffff) == 0);
    250 
    251 	/*
    252 	 * Ensure all outstanding ITS operations associated with physical interrupts
    253 	 * for the specified redistributor (RDbase) are globally observed before
    254 	 * further ITS commands are executed.
    255 	 */
    256 	memset(&cmd, 0, sizeof(cmd));
    257 	cmd.dw[0] = GITS_CMD_SYNC;
    258 	cmd.dw[2] = rdbase;
    259 
    260 	gits_command(its, &cmd);
    261 }
    262 
    263 static inline int
    264 gits_wait(struct gicv3_its *its)
    265 {
    266 	u_int woff, roff;
    267 	int retry = 100000;
    268 
    269 	/*
    270 	 * The ITS command queue is empty when CWRITER and CREADR specify the
    271 	 * same base address offset value.
    272 	 */
    273 	for (retry = 1000; retry > 0; retry--) {
    274 		woff = gits_read_8(its, GITS_CWRITER) & GITS_CWRITER_Offset;
    275 		roff = gits_read_8(its, GITS_CREADR) & GITS_CREADR_Offset;
    276 		if (woff == roff)
    277 			break;
    278 		delay(100);
    279 	}
    280 	if (retry == 0) {
    281 		device_printf(its->its_gic->sc_dev, "ITS command queue timeout\n");
    282 		return ETIMEDOUT;
    283 	}
    284 
    285 	return 0;
    286 }
    287 
    288 static int
    289 gicv3_its_msi_alloc_lpi(struct gicv3_its *its,
    290     const struct pci_attach_args *pa)
    291 {
    292 	struct pci_attach_args *new_pa;
    293 	vmem_addr_t n;
    294 
    295 	KASSERT(its->its_gic->sc_lpi_pool != NULL);
    296 
    297 	if (vmem_alloc(its->its_gic->sc_lpi_pool, 1, VM_INSTANTFIT|VM_SLEEP, &n) != 0)
    298 		return -1;
    299 
    300 	KASSERT(its->its_pa[n] == NULL);
    301 
    302 	new_pa = kmem_alloc(sizeof(*new_pa), KM_SLEEP);
    303 	memcpy(new_pa, pa, sizeof(*new_pa));
    304 	its->its_pa[n] = new_pa;
    305 	return n + its->its_pic->pic_irqbase;
    306 }
    307 
    308 static void
    309 gicv3_its_msi_free_lpi(struct gicv3_its *its, int lpi)
    310 {
    311 	struct pci_attach_args *pa;
    312 
    313 	KASSERT(its->its_gic->sc_lpi_pool != NULL);
    314 	KASSERT(lpi >= its->its_pic->pic_irqbase);
    315 
    316 	pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    317 	its->its_pa[lpi - its->its_pic->pic_irqbase] = NULL;
    318 	kmem_free(pa, sizeof(*pa));
    319 
    320 	vmem_free(its->its_gic->sc_lpi_pool, lpi - its->its_pic->pic_irqbase, 1);
    321 }
    322 
    323 static uint32_t
    324 gicv3_its_devid(pci_chipset_tag_t pc, pcitag_t tag)
    325 {
    326 	uint32_t devid;
    327 	int b, d, f;
    328 
    329 	pci_decompose_tag(pc, tag, &b, &d, &f);
    330 
    331 	devid = (b << 8) | (d << 3) | f;
    332 
    333 	return pci_get_devid(pc, devid);
    334 }
    335 
    336 static int
    337 gicv3_its_device_map(struct gicv3_its *its, uint32_t devid, u_int count)
    338 {
    339 	struct gicv3_its_device *dev;
    340 	u_int vectors;
    341 
    342 	vectors = MAX(2, count);
    343 	while (!powerof2(vectors))
    344 		vectors++;
    345 
    346 	const uint64_t typer = gits_read_8(its, GITS_TYPER);
    347 	const u_int itt_entry_size = __SHIFTOUT(typer, GITS_TYPER_ITT_entry_size) + 1;
    348 	const u_int itt_size = roundup(vectors * itt_entry_size, GITS_ITT_ALIGN);
    349 
    350 	LIST_FOREACH(dev, &its->its_devices, dev_list)
    351 		if (dev->dev_id == devid) {
    352 			return itt_size <= dev->dev_size ? 0 : EEXIST;
    353 		}
    354 
    355 	dev = kmem_alloc(sizeof(*dev), KM_SLEEP);
    356 	dev->dev_id = devid;
    357 	dev->dev_size = itt_size;
    358 	gicv3_dma_alloc(its->its_gic, &dev->dev_itt, itt_size, GITS_ITT_ALIGN);
    359 	LIST_INSERT_HEAD(&its->its_devices, dev, dev_list);
    360 
    361 	/*
    362 	 * Map the device to the ITT
    363 	 */
    364 	const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
    365 	gits_command_mapd(its, devid, dev->dev_itt.segs[0].ds_addr, id_bits - 1, true);
    366 	gits_wait(its);
    367 
    368 	return 0;
    369 }
    370 
    371 static void
    372 gicv3_its_msi_enable(struct gicv3_its *its, int lpi, int count)
    373 {
    374 	const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    375 	pci_chipset_tag_t pc = pa->pa_pc;
    376 	pcitag_t tag = pa->pa_tag;
    377 	pcireg_t ctl;
    378 	int off;
    379 
    380 	if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
    381 		panic("gicv3_its_msi_enable: device is not MSI-capable");
    382 
    383 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    384 	ctl &= ~PCI_MSI_CTL_MME_MASK;
    385 	ctl |= __SHIFTIN(ilog2(count), PCI_MSI_CTL_MME_MASK);
    386 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    387 
    388 	const uint64_t addr = its->its_base + GITS_TRANSLATER;
    389 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    390 	if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
    391 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO,
    392 		    addr & 0xffffffff);
    393 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI,
    394 		    (addr >> 32) & 0xffffffff);
    395 		pci_conf_write(pc, tag, off + PCI_MSI_MDATA64,
    396 		    lpi - its->its_pic->pic_irqbase);
    397 	} else {
    398 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR,
    399 		    addr & 0xffffffff);
    400 		pci_conf_write(pc, tag, off + PCI_MSI_MDATA,
    401 		    lpi - its->its_pic->pic_irqbase);
    402 	}
    403 	ctl |= PCI_MSI_CTL_MSI_ENABLE;
    404 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    405 }
    406 
    407 static void
    408 gicv3_its_msi_disable(struct gicv3_its *its, int lpi)
    409 {
    410 	const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    411 	pci_chipset_tag_t pc = pa->pa_pc;
    412 	pcitag_t tag = pa->pa_tag;
    413 	pcireg_t ctl;
    414 	int off;
    415 
    416 	if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
    417 		panic("gicv3_its_msi_enable: device is not MSI-capable");
    418 
    419 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    420 	ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
    421 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    422 }
    423 
    424 static void
    425 gicv3_its_msix_enable(struct gicv3_its *its, int lpi, int msix_vec,
    426     bus_space_tag_t bst, bus_space_handle_t bsh)
    427 {
    428 	const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    429 	pci_chipset_tag_t pc = pa->pa_pc;
    430 	pcitag_t tag = pa->pa_tag;
    431 	pcireg_t ctl;
    432 	uint32_t val;
    433 	int off;
    434 
    435 	if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
    436 		panic("gicv3_its_msix_enable: device is not MSI-X-capable");
    437 
    438 	const uint64_t addr = its->its_base + GITS_TRANSLATER;
    439 	const uint64_t entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
    440 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO, (uint32_t)addr);
    441 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI, (uint32_t)(addr >> 32));
    442 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_DATA, lpi - its->its_pic->pic_irqbase);
    443 	val = bus_space_read_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL);
    444 	val &= ~PCI_MSIX_VECTCTL_MASK;
    445 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, val);
    446 
    447 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    448 	ctl |= PCI_MSIX_CTL_ENABLE;
    449 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    450 }
    451 
    452 static void
    453 gicv3_its_msix_disable(struct gicv3_its *its, int lpi)
    454 {
    455 	const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    456 	pci_chipset_tag_t pc = pa->pa_pc;
    457 	pcitag_t tag = pa->pa_tag;
    458 	pcireg_t ctl;
    459 	int off;
    460 
    461 	if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
    462 		panic("gicv3_its_msix_disable: device is not MSI-X-capable");
    463 
    464 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    465 	ctl &= ~PCI_MSIX_CTL_ENABLE;
    466 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    467 }
    468 
    469 static pci_intr_handle_t *
    470 gicv3_its_msi_alloc(struct arm_pci_msi *msi, int *count,
    471     const struct pci_attach_args *pa, bool exact)
    472 {
    473 	struct gicv3_its * const its = msi->msi_priv;
    474 	struct cpu_info * const ci = cpu_lookup(0);
    475 	pci_intr_handle_t *vectors;
    476 	int n, off;
    477 
    478 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &off, NULL))
    479 		return NULL;
    480 
    481 	const uint64_t typer = gits_read_8(its, GITS_TYPER);
    482 	const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
    483 	if (*count == 0 || *count > (1 << id_bits))
    484 		return NULL;
    485 
    486 	const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
    487 
    488 	if (gicv3_its_device_map(its, devid, *count) != 0)
    489 		return NULL;
    490 
    491 	vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
    492 	for (n = 0; n < *count; n++) {
    493 		const int lpi = gicv3_its_msi_alloc_lpi(its, pa);
    494 		vectors[n] = ARM_PCI_INTR_MSI |
    495 		    __SHIFTIN(lpi, ARM_PCI_INTR_IRQ) |
    496 		    __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
    497 		    __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
    498 
    499 		if (n == 0)
    500 			gicv3_its_msi_enable(its, lpi, *count);
    501 
    502 		/*
    503 		 * Record target PE
    504 		 */
    505 		its->its_targets[lpi - its->its_pic->pic_irqbase] = ci;
    506 
    507 		/*
    508 		 * Map event
    509 		 */
    510 		gits_command_mapti(its, devid, lpi - its->its_pic->pic_irqbase, lpi, cpu_index(ci));
    511 		gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
    512 	}
    513 	gits_wait(its);
    514 
    515 	return vectors;
    516 }
    517 
    518 static pci_intr_handle_t *
    519 gicv3_its_msix_alloc(struct arm_pci_msi *msi, u_int *table_indexes, int *count,
    520     const struct pci_attach_args *pa, bool exact)
    521 {
    522 	struct gicv3_its * const its = msi->msi_priv;
    523 	struct cpu_info *ci = cpu_lookup(0);
    524 	pci_intr_handle_t *vectors;
    525 	bus_space_tag_t bst;
    526 	bus_space_handle_t bsh;
    527 	bus_size_t bsz;
    528 	uint32_t table_offset, table_size;
    529 	int n, off, bar, error;
    530 	pcireg_t tbl;
    531 
    532 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &off, NULL))
    533 		return NULL;
    534 
    535 	const uint64_t typer = gits_read_8(its, GITS_TYPER);
    536 	const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
    537 	if (*count == 0 || *count > (1 << id_bits))
    538 		return NULL;
    539 
    540 	tbl = pci_conf_read(pa->pa_pc, pa->pa_tag, off + PCI_MSIX_TBLOFFSET);
    541 	bar = PCI_BAR0 + (4 * (tbl & PCI_MSIX_TBLBIR_MASK));
    542 	table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
    543 	table_size = pci_msix_count(pa->pa_pc, pa->pa_tag) * PCI_MSIX_TABLE_ENTRY_SIZE;
    544 	if (table_size == 0)
    545 		return NULL;
    546 
    547 	error = pci_mapreg_submap(pa, bar, pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar),
    548 	    BUS_SPACE_MAP_LINEAR, roundup(table_size, PAGE_SIZE), table_offset,
    549 	    &bst, &bsh, NULL, &bsz);
    550 	if (error)
    551 		return NULL;
    552 
    553 	const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
    554 
    555 	if (gicv3_its_device_map(its, devid, *count) != 0) {
    556 		bus_space_unmap(bst, bsh, bsz);
    557 		return NULL;
    558 	}
    559 
    560 	vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
    561 	for (n = 0; n < *count; n++) {
    562 		const int lpi = gicv3_its_msi_alloc_lpi(its, pa);
    563 		const int msix_vec = table_indexes ? table_indexes[n] : n;
    564 		vectors[msix_vec] = ARM_PCI_INTR_MSIX |
    565 		    __SHIFTIN(lpi, ARM_PCI_INTR_IRQ) |
    566 		    __SHIFTIN(msix_vec, ARM_PCI_INTR_MSI_VEC) |
    567 		    __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
    568 
    569 		gicv3_its_msix_enable(its, lpi, msix_vec, bst, bsh);
    570 
    571 		/*
    572 		 * Record target PE
    573 		 */
    574 		its->its_targets[lpi - its->its_pic->pic_irqbase] = ci;
    575 
    576 		/*
    577 		 * Map event
    578 		 */
    579 		gits_command_mapti(its, devid, lpi - its->its_pic->pic_irqbase, lpi, cpu_index(ci));
    580 		gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
    581 	}
    582 	gits_wait(its);
    583 
    584 	bus_space_unmap(bst, bsh, bsz);
    585 
    586 	return vectors;
    587 }
    588 
    589 static void *
    590 gicv3_its_msi_intr_establish(struct arm_pci_msi *msi,
    591     pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg, const char *xname)
    592 {
    593 	struct gicv3_its * const its = msi->msi_priv;
    594 	const struct pci_attach_args *pa;
    595 	void *intrh;
    596 
    597 	const int lpi = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
    598 	const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? IST_MPSAFE : 0;
    599 
    600 	intrh = pic_establish_intr(its->its_pic, lpi - its->its_pic->pic_irqbase, ipl,
    601 	    IST_EDGE | mpsafe, func, arg, xname);
    602 	if (intrh == NULL)
    603 		return NULL;
    604 
    605 	/* Invalidate LPI configuration tables */
    606 	pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    607 	KASSERT(pa != NULL);
    608 	const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
    609 	gits_command_inv(its, devid, lpi - its->its_pic->pic_irqbase);
    610 
    611 	return intrh;
    612 }
    613 
    614 static void
    615 gicv3_its_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
    616     int count)
    617 {
    618 	struct gicv3_its * const its = msi->msi_priv;
    619 	int n;
    620 
    621 	for (n = 0; n < count; n++) {
    622 		const int lpi = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
    623 		KASSERT(lpi >= its->its_pic->pic_irqbase);
    624 		if (pih[n] & ARM_PCI_INTR_MSIX)
    625 			gicv3_its_msix_disable(its, lpi);
    626 		if (pih[n] & ARM_PCI_INTR_MSI)
    627 			gicv3_its_msi_disable(its, lpi);
    628 		gicv3_its_msi_free_lpi(its, lpi);
    629 		its->its_targets[lpi - its->its_pic->pic_irqbase] = NULL;
    630 		struct intrsource * const is =
    631 		    its->its_pic->pic_sources[lpi - its->its_pic->pic_irqbase];
    632 		if (is != NULL)
    633 			pic_disestablish_source(is);
    634 	}
    635 }
    636 
    637 static void
    638 gicv3_its_command_init(struct gicv3_softc *sc, struct gicv3_its *its)
    639 {
    640 	uint64_t cbaser;
    641 
    642 	gicv3_dma_alloc(sc, &its->its_cmd, GITS_COMMANDS_SIZE, GITS_COMMANDS_ALIGN);
    643 
    644 	cbaser = its->its_cmd.segs[0].ds_addr;
    645 	cbaser |= __SHIFTIN(GITS_Cache_NORMAL_NC, GITS_CBASER_InnerCache);
    646 	cbaser |= __SHIFTIN(GITS_Shareability_NS, GITS_CBASER_Shareability);
    647 	cbaser |= __SHIFTIN((its->its_cmd.len / 4096) - 1, GITS_CBASER_Size);
    648 	cbaser |= GITS_CBASER_Valid;
    649 
    650 	gits_write_8(its, GITS_CBASER, cbaser);
    651 	gits_write_8(its, GITS_CWRITER, 0);
    652 }
    653 
    654 static void
    655 gicv3_its_table_params(struct gicv3_softc *sc, struct gicv3_its *its,
    656     u_int *devbits, u_int *innercache, u_int *share)
    657 {
    658 
    659 	const uint64_t typer = gits_read_8(its, GITS_TYPER);
    660 	const uint32_t iidr = gits_read_4(its, GITS_IIDR);
    661 
    662 	/* Default values */
    663 	*devbits = __SHIFTOUT(typer, GITS_TYPER_Devbits) + 1;
    664 	*innercache = GITS_Cache_NORMAL_WA_WB;
    665 	*share = GITS_Shareability_IS;
    666 
    667 	/* Cavium ThunderX errata */
    668 	if ((iidr & GITS_IIDR_CAVIUM_ERRATA_MASK) == GITS_IIDR_CAVIUM_ERRATA_VALUE) {
    669 		*devbits = 20;		/* 8Mb */
    670 		*innercache = GITS_Cache_DEVICE_nGnRnE;
    671 		aprint_normal_dev(sc->sc_dev, "Cavium ThunderX errata detected\n");
    672 	}
    673 }
    674 
    675 static void
    676 gicv3_its_table_init(struct gicv3_softc *sc, struct gicv3_its *its)
    677 {
    678 	u_int table_size, page_size, table_align;
    679 	u_int devbits, innercache, share;
    680 	const char *table_type;
    681 	uint64_t baser;
    682 	int tab;
    683 
    684 	gicv3_its_table_params(sc, its, &devbits, &innercache, &share);
    685 
    686 	for (tab = 0; tab < 8; tab++) {
    687 		baser = gits_read_8(its, GITS_BASERn(tab));
    688 
    689 		const u_int entry_size = __SHIFTOUT(baser, GITS_BASER_Entry_Size) + 1;
    690 
    691 		switch (__SHIFTOUT(baser, GITS_BASER_Page_Size)) {
    692 		case GITS_Page_Size_4KB:
    693 			page_size = 4096;
    694 			table_align = 4096;
    695 			break;
    696 		case GITS_Page_Size_16KB:
    697 			page_size = 16384;
    698 			table_align = 4096;
    699 			break;
    700 		case GITS_Page_Size_64KB:
    701 		default:
    702 			page_size = 65536;
    703 			table_align = 65536;
    704 			break;
    705 		}
    706 
    707 		switch (__SHIFTOUT(baser, GITS_BASER_Type)) {
    708 		case GITS_Type_Devices:
    709 			/*
    710 			 * Table size scales with the width of the DeviceID.
    711 			 */
    712 			table_size = roundup(entry_size * (1 << devbits), page_size);
    713 			table_type = "Devices";
    714 			break;
    715 		case GITS_Type_InterruptCollections:
    716 			/*
    717 			 * Allocate space for one interrupt collection per CPU.
    718 			 */
    719 			table_size = roundup(entry_size * MAXCPUS, page_size);
    720 			table_type = "Collections";
    721 			break;
    722 		default:
    723 			table_size = 0;
    724 			break;
    725 		}
    726 
    727 		if (table_size == 0)
    728 			continue;
    729 
    730 		gicv3_dma_alloc(sc, &its->its_tab[tab], table_size, table_align);
    731 
    732 		baser &= ~GITS_BASER_Size;
    733 		baser |= __SHIFTIN(table_size / page_size - 1, GITS_BASER_Size);
    734 		baser &= ~GITS_BASER_Physical_Address;
    735 		baser |= its->its_tab[tab].segs[0].ds_addr;
    736 		baser &= ~GITS_BASER_InnerCache;
    737 		baser |= __SHIFTIN(innercache, GITS_BASER_InnerCache);
    738 		baser &= ~GITS_BASER_Shareability;
    739 		baser |= __SHIFTIN(share, GITS_BASER_Shareability);
    740 		baser |= GITS_BASER_Valid;
    741 
    742 		gits_write_8(its, GITS_BASERn(tab), baser);
    743 
    744 		baser = gits_read_8(its, GITS_BASERn(tab));
    745 		if (__SHIFTOUT(baser, GITS_BASER_Shareability) == GITS_Shareability_NS) {
    746 			baser &= ~GITS_BASER_InnerCache;
    747 			baser |= __SHIFTIN(GITS_Cache_NORMAL_NC, GITS_BASER_InnerCache);
    748 
    749 			gits_write_8(its, GITS_BASERn(tab), baser);
    750 		}
    751 
    752 		baser = gits_read_8(its, GITS_BASERn(tab));
    753 		aprint_normal_dev(sc->sc_dev, "ITS [#%d] %s table @ %#lx/%#x, %s, %s\n",
    754 		    tab, table_type, its->its_tab[tab].segs[0].ds_addr, table_size,
    755 		    gits_cache_type[__SHIFTOUT(baser, GITS_BASER_InnerCache)],
    756 		    gits_share_type[__SHIFTOUT(baser, GITS_BASER_Shareability)]);
    757 	}
    758 }
    759 
    760 static void
    761 gicv3_its_enable(struct gicv3_softc *sc, struct gicv3_its *its)
    762 {
    763 	uint32_t ctlr;
    764 
    765 	ctlr = gits_read_4(its, GITS_CTLR);
    766 	ctlr |= GITS_CTLR_Enabled;
    767 	gits_write_4(its, GITS_CTLR, ctlr);
    768 }
    769 
    770 static void
    771 gicv3_its_cpu_init(void *priv, struct cpu_info *ci)
    772 {
    773 	struct gicv3_its * const its = priv;
    774 	struct gicv3_softc * const sc = its->its_gic;
    775 	const struct pci_attach_args *pa;
    776 	uint64_t rdbase;
    777 	size_t irq;
    778 
    779 	const uint64_t typer = bus_space_read_8(sc->sc_bst, its->its_bsh, GITS_TYPER);
    780 	if (typer & GITS_TYPER_PTA) {
    781 		void *va = bus_space_vaddr(sc->sc_bst, sc->sc_bsh_r[ci->ci_gic_redist]);
    782 		rdbase = vtophys((vaddr_t)va);
    783 	} else {
    784 		rdbase = (uint64_t)sc->sc_processor_id[cpu_index(ci)] << 16;
    785 	}
    786 	its->its_rdbase[cpu_index(ci)] = rdbase;
    787 
    788 	/*
    789 	 * Map collection ID of this CPU's index to this CPU's redistributor.
    790 	 */
    791 	gits_command_mapc(its, cpu_index(ci), rdbase, true);
    792 	gits_command_invall(its, cpu_index(ci));
    793 	gits_wait(its);
    794 
    795 	/*
    796 	 * Update routing for LPIs targetting this CPU
    797 	 */
    798 	for (irq = 0; irq < its->its_pic->pic_maxsources; irq++) {
    799 		if (its->its_targets[irq] != ci)
    800 			continue;
    801 		pa = its->its_pa[irq];
    802 		KASSERT(pa != NULL);
    803 
    804 		const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
    805 		gits_command_movi(its, devid, irq, cpu_index(ci));
    806 		gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
    807 	}
    808 
    809 	its->its_cpuonline[cpu_index(ci)] = true;
    810 }
    811 
    812 static void
    813 gicv3_its_get_affinity(void *priv, size_t irq, kcpuset_t *affinity)
    814 {
    815 	struct gicv3_its * const its = priv;
    816 	struct cpu_info *ci;
    817 
    818 	ci = its->its_targets[irq];
    819 	if (ci)
    820 		kcpuset_set(affinity, cpu_index(ci));
    821 }
    822 
    823 static int
    824 gicv3_its_set_affinity(void *priv, size_t irq, const kcpuset_t *affinity)
    825 {
    826 	struct gicv3_its * const its = priv;
    827 	const struct pci_attach_args *pa;
    828 	struct cpu_info *ci;
    829 
    830 	const int set = kcpuset_countset(affinity);
    831 	if (set != 1)
    832 		return EINVAL;
    833 
    834 	pa = its->its_pa[irq];
    835 	if (pa == NULL)
    836 		return EPASSTHROUGH;
    837 
    838 	ci = cpu_lookup(kcpuset_ffs(affinity) - 1);
    839 	its->its_targets[irq] = ci;
    840 
    841 	if (its->its_cpuonline[cpu_index(ci)] == true) {
    842 		const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
    843 		gits_command_movi(its, devid, irq, cpu_index(ci));
    844 		gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
    845 	}
    846 
    847 	return 0;
    848 }
    849 
    850 int
    851 gicv3_its_init(struct gicv3_softc *sc, bus_space_handle_t bsh,
    852     uint64_t its_base, uint32_t its_id)
    853 {
    854 	struct gicv3_its *its;
    855 	struct arm_pci_msi *msi;
    856 
    857 	const uint64_t typer = bus_space_read_8(sc->sc_bst, bsh, GITS_TYPER);
    858 	if ((typer & GITS_TYPER_Physical) == 0)
    859 		return ENXIO;
    860 
    861 	its = kmem_zalloc(sizeof(*its), KM_SLEEP);
    862 	its->its_id = its_id;
    863 	its->its_bst = sc->sc_bst;
    864 	its->its_bsh = bsh;
    865 	its->its_dmat = sc->sc_dmat;
    866 	its->its_base = its_base;
    867 	its->its_pic = &sc->sc_lpi;
    868 	snprintf(its->its_pic->pic_name, sizeof(its->its_pic->pic_name), "gicv3-its");
    869 	KASSERT(its->its_pic->pic_maxsources > 0);
    870 	its->its_pa = kmem_zalloc(sizeof(struct pci_attach_args *) * its->its_pic->pic_maxsources, KM_SLEEP);
    871 	its->its_targets = kmem_zalloc(sizeof(struct cpu_info *) * its->its_pic->pic_maxsources, KM_SLEEP);
    872 	its->its_gic = sc;
    873 	its->its_cb.cpu_init = gicv3_its_cpu_init;
    874 	its->its_cb.get_affinity = gicv3_its_get_affinity;
    875 	its->its_cb.set_affinity = gicv3_its_set_affinity;
    876 	its->its_cb.priv = its;
    877 	LIST_INIT(&its->its_devices);
    878 	LIST_INSERT_HEAD(&sc->sc_lpi_callbacks, &its->its_cb, list);
    879 
    880 	gicv3_its_command_init(sc, its);
    881 	gicv3_its_table_init(sc, its);
    882 
    883 	gicv3_its_enable(sc, its);
    884 
    885 	gicv3_its_cpu_init(its, curcpu());
    886 
    887 	msi = &its->its_msi;
    888 	msi->msi_id = its_id;
    889 	msi->msi_dev = sc->sc_dev;
    890 	msi->msi_priv = its;
    891 	msi->msi_alloc = gicv3_its_msi_alloc;
    892 	msi->msix_alloc = gicv3_its_msix_alloc;
    893 	msi->msi_intr_establish = gicv3_its_msi_intr_establish;
    894 	msi->msi_intr_release = gicv3_its_msi_intr_release;
    895 
    896 	return arm_pci_msi_add(msi);
    897 }
    898