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