Home | History | Annotate | Line # | Download | only in cortex
gicv3_its.c revision 1.14
      1 /* $NetBSD: gicv3_its.c,v 1.14 2019/06/16 19:19:30 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.14 2019/06/16 19:19:30 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 	struct pci_attach_args *new_pa;
    264 	int n;
    265 
    266 	for (n = 0; n < its->its_pic->pic_maxsources; n++) {
    267 		if (its->its_pa[n] == NULL) {
    268 			new_pa = kmem_alloc(sizeof(*new_pa), KM_SLEEP);
    269 			memcpy(new_pa, pa, sizeof(*new_pa));
    270 			its->its_pa[n] = new_pa;
    271 			return n + its->its_pic->pic_irqbase;
    272 		}
    273 	}
    274 
    275         return -1;
    276 }
    277 
    278 static void
    279 gicv3_its_msi_free_lpi(struct gicv3_its *its, int lpi)
    280 {
    281 	struct pci_attach_args *pa;
    282 
    283 	KASSERT(lpi >= its->its_pic->pic_irqbase);
    284 
    285 	pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    286 	its->its_pa[lpi - its->its_pic->pic_irqbase] = NULL;
    287 	kmem_free(pa, sizeof(*pa));
    288 }
    289 
    290 static uint32_t
    291 gicv3_its_devid(pci_chipset_tag_t pc, pcitag_t tag)
    292 {
    293 	uint32_t devid;
    294 	int b, d, f;
    295 
    296 	pci_decompose_tag(pc, tag, &b, &d, &f);
    297 
    298 	devid = (b << 8) | (d << 3) | f;
    299 
    300 	return pci_get_devid(pc, devid);
    301 }
    302 
    303 static int
    304 gicv3_its_device_map(struct gicv3_its *its, uint32_t devid, u_int count)
    305 {
    306 	struct gicv3_its_device *dev;
    307 	u_int vectors;
    308 
    309 	vectors = MAX(2, count);
    310 	while (!powerof2(vectors))
    311 		vectors++;
    312 
    313 	const uint64_t typer = gits_read_8(its, GITS_TYPER);
    314 	const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
    315 	const u_int itt_entry_size = __SHIFTOUT(typer, GITS_TYPER_ITT_entry_size) + 1;
    316 	const u_int itt_size = roundup(vectors * itt_entry_size, GITS_ITT_ALIGN);
    317 
    318 	LIST_FOREACH(dev, &its->its_devices, dev_list)
    319 		if (dev->dev_id == devid) {
    320 			return itt_size <= dev->dev_size ? 0 : EEXIST;
    321 		}
    322 
    323 	dev = kmem_alloc(sizeof(*dev), KM_SLEEP);
    324 	dev->dev_id = devid;
    325 	dev->dev_size = itt_size;
    326 	gicv3_dma_alloc(its->its_gic, &dev->dev_itt, itt_size, GITS_ITT_ALIGN);
    327 	LIST_INSERT_HEAD(&its->its_devices, dev, dev_list);
    328 
    329 	/*
    330 	 * Map the device to the ITT
    331 	 */
    332 	gits_command_mapd(its, devid, dev->dev_itt.segs[0].ds_addr, id_bits - 1, true);
    333 	gits_wait(its);
    334 
    335 	return 0;
    336 }
    337 
    338 static void
    339 gicv3_its_msi_enable(struct gicv3_its *its, int lpi, int count)
    340 {
    341 	const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    342 	pci_chipset_tag_t pc = pa->pa_pc;
    343 	pcitag_t tag = pa->pa_tag;
    344 	pcireg_t ctl;
    345 	int off;
    346 
    347 	if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
    348 		panic("gicv3_its_msi_enable: device is not MSI-capable");
    349 
    350 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    351 	ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
    352 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    353 
    354 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    355 	ctl &= ~PCI_MSI_CTL_MME_MASK;
    356 	ctl |= __SHIFTIN(ilog2(count), PCI_MSI_CTL_MME_MASK);
    357 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    358 
    359 	const uint64_t addr = its->its_base + GITS_TRANSLATER;
    360 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    361 	if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
    362 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO,
    363 		    addr & 0xffffffff);
    364 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI,
    365 		    (addr >> 32) & 0xffffffff);
    366 		pci_conf_write(pc, tag, off + PCI_MSI_MDATA64, lpi);
    367 	} else {
    368 		pci_conf_write(pc, tag, off + PCI_MSI_MADDR,
    369 		    addr & 0xffffffff);
    370 		pci_conf_write(pc, tag, off + PCI_MSI_MDATA, lpi);
    371 	}
    372 	ctl |= PCI_MSI_CTL_MSI_ENABLE;
    373 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    374 }
    375 
    376 static void
    377 gicv3_its_msi_disable(struct gicv3_its *its, int lpi)
    378 {
    379 	const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    380 	pci_chipset_tag_t pc = pa->pa_pc;
    381 	pcitag_t tag = pa->pa_tag;
    382 	pcireg_t ctl;
    383 	int off;
    384 
    385 	if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
    386 		panic("gicv3_its_msi_enable: device is not MSI-capable");
    387 
    388 	ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
    389 	ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
    390 	pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
    391 }
    392 
    393 static void
    394 gicv3_its_msix_enable(struct gicv3_its *its, int lpi, int msix_vec,
    395     bus_space_tag_t bst, bus_space_handle_t bsh)
    396 {
    397 	const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    398 	pci_chipset_tag_t pc = pa->pa_pc;
    399 	pcitag_t tag = pa->pa_tag;
    400 	pcireg_t ctl;
    401 	int off;
    402 
    403 	if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
    404 		panic("gicv3_its_msix_enable: device is not MSI-X-capable");
    405 
    406 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    407 	ctl &= ~PCI_MSIX_CTL_ENABLE;
    408 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    409 
    410 	const uint64_t addr = its->its_base + GITS_TRANSLATER;
    411 	const uint64_t entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
    412 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO, (uint32_t)addr);
    413 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI, (uint32_t)(addr >> 32));
    414 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_DATA, lpi);
    415 	bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, 0);
    416 
    417 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    418 	ctl |= PCI_MSIX_CTL_ENABLE;
    419 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    420 }
    421 
    422 static void
    423 gicv3_its_msix_disable(struct gicv3_its *its, int lpi)
    424 {
    425 	const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    426 	pci_chipset_tag_t pc = pa->pa_pc;
    427 	pcitag_t tag = pa->pa_tag;
    428 	pcireg_t ctl;
    429 	int off;
    430 
    431 	if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
    432 		panic("gicv3_its_msix_disable: device is not MSI-X-capable");
    433 
    434 	ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
    435 	ctl &= ~PCI_MSIX_CTL_ENABLE;
    436 	pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
    437 }
    438 
    439 static pci_intr_handle_t *
    440 gicv3_its_msi_alloc(struct arm_pci_msi *msi, int *count,
    441     const struct pci_attach_args *pa, bool exact)
    442 {
    443 	struct gicv3_its * const its = msi->msi_priv;
    444 	struct cpu_info * const ci = cpu_lookup(0);
    445 	pci_intr_handle_t *vectors;
    446 	int n, off;
    447 
    448 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &off, NULL))
    449 		return NULL;
    450 
    451 	const uint64_t typer = gits_read_8(its, GITS_TYPER);
    452 	const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
    453 	if (*count == 0 || *count > (1 << id_bits))
    454 		return NULL;
    455 
    456 	const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
    457 
    458 	if (gicv3_its_device_map(its, devid, *count) != 0)
    459 		return NULL;
    460 
    461 	vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
    462 	for (n = 0; n < *count; n++) {
    463 		const int lpi = gicv3_its_msi_alloc_lpi(its, pa);
    464 		vectors[n] = ARM_PCI_INTR_MSI |
    465 		    __SHIFTIN(lpi, ARM_PCI_INTR_IRQ) |
    466 		    __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
    467 		    __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
    468 
    469 		if (n == 0)
    470 			gicv3_its_msi_enable(its, lpi, *count);
    471 
    472 		/*
    473 		 * Record target PE
    474 		 */
    475 		its->its_targets[lpi - its->its_pic->pic_irqbase] = ci;
    476 
    477 		/*
    478 		 * Map event
    479 		 */
    480 		gits_command_mapi(its, devid, lpi, cpu_index(ci));
    481 		gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
    482 	}
    483 	gits_wait(its);
    484 
    485 	return vectors;
    486 }
    487 
    488 static pci_intr_handle_t *
    489 gicv3_its_msix_alloc(struct arm_pci_msi *msi, u_int *table_indexes, int *count,
    490     const struct pci_attach_args *pa, bool exact)
    491 {
    492 	struct gicv3_its * const its = msi->msi_priv;
    493 	struct cpu_info *ci = cpu_lookup(0);
    494 	pci_intr_handle_t *vectors;
    495 	bus_space_tag_t bst;
    496 	bus_space_handle_t bsh;
    497 	bus_size_t bsz;
    498 	uint32_t table_offset, table_size;
    499 	int n, off, bar, error;
    500 	pcireg_t tbl;
    501 
    502 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &off, NULL))
    503 		return NULL;
    504 
    505 	const uint64_t typer = gits_read_8(its, GITS_TYPER);
    506 	const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
    507 	if (*count == 0 || *count > (1 << id_bits))
    508 		return NULL;
    509 
    510 	tbl = pci_conf_read(pa->pa_pc, pa->pa_tag, off + PCI_MSIX_TBLOFFSET);
    511 	bar = PCI_BAR0 + (4 * (tbl & PCI_MSIX_PBABIR_MASK));
    512 	table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
    513 	table_size = pci_msix_count(pa->pa_pc, pa->pa_tag) * PCI_MSIX_TABLE_ENTRY_SIZE;
    514 	if (table_size == 0)
    515 		return NULL;
    516 
    517 	error = pci_mapreg_submap(pa, bar, pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar),
    518 	    BUS_SPACE_MAP_LINEAR, roundup(table_size, PAGE_SIZE), table_offset,
    519 	    &bst, &bsh, NULL, &bsz);
    520 	if (error)
    521 		return NULL;
    522 
    523 	const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
    524 
    525 	if (gicv3_its_device_map(its, devid, *count) != 0) {
    526 		bus_space_unmap(bst, bsh, bsz);
    527 		return NULL;
    528 	}
    529 
    530 	vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
    531 	for (n = 0; n < *count; n++) {
    532 		const int lpi = gicv3_its_msi_alloc_lpi(its, pa);
    533 		const int msix_vec = table_indexes ? table_indexes[n] : n;
    534 		vectors[msix_vec] = ARM_PCI_INTR_MSIX |
    535 		    __SHIFTIN(lpi, ARM_PCI_INTR_IRQ) |
    536 		    __SHIFTIN(msix_vec, ARM_PCI_INTR_MSI_VEC) |
    537 		    __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
    538 
    539 		gicv3_its_msix_enable(its, lpi, msix_vec, bst, bsh);
    540 
    541 		/*
    542 		 * Record target PE
    543 		 */
    544 		its->its_targets[lpi - its->its_pic->pic_irqbase] = ci;
    545 
    546 		/*
    547 		 * Map event
    548 		 */
    549 		gits_command_mapi(its, devid, lpi, cpu_index(ci));
    550 		gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
    551 	}
    552 	gits_wait(its);
    553 
    554 	bus_space_unmap(bst, bsh, bsz);
    555 
    556 	return vectors;
    557 }
    558 
    559 static void *
    560 gicv3_its_msi_intr_establish(struct arm_pci_msi *msi,
    561     pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg, const char *xname)
    562 {
    563 	struct gicv3_its * const its = msi->msi_priv;
    564 	const struct pci_attach_args *pa;
    565 	void *intrh;
    566 
    567 	const int lpi = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
    568 	const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? IST_MPSAFE : 0;
    569 
    570 	intrh = pic_establish_intr(its->its_pic, lpi - its->its_pic->pic_irqbase, ipl,
    571 	    IST_EDGE | mpsafe, func, arg, xname);
    572 	if (intrh == NULL)
    573 		return NULL;
    574 
    575 	/* Invalidate LPI configuration tables */
    576 	pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
    577 	KASSERT(pa != NULL);
    578 	const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
    579 	gits_command_inv(its, devid, lpi);
    580 
    581 	return intrh;
    582 }
    583 
    584 static void
    585 gicv3_its_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
    586     int count)
    587 {
    588 	struct gicv3_its * const its = msi->msi_priv;
    589 	int n;
    590 
    591 	for (n = 0; n < count; n++) {
    592 		const int lpi = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
    593 		KASSERT(lpi >= its->its_pic->pic_irqbase);
    594 		if (pih[n] & ARM_PCI_INTR_MSIX)
    595 			gicv3_its_msix_disable(its, lpi);
    596 		if (pih[n] & ARM_PCI_INTR_MSI)
    597 			gicv3_its_msi_disable(its, lpi);
    598 		gicv3_its_msi_free_lpi(its, lpi);
    599 		its->its_targets[lpi - its->its_pic->pic_irqbase] = NULL;
    600 		struct intrsource * const is =
    601 		    its->its_pic->pic_sources[lpi - its->its_pic->pic_irqbase];
    602 		if (is != NULL)
    603 			pic_disestablish_source(is);
    604 	}
    605 }
    606 
    607 static void
    608 gicv3_its_command_init(struct gicv3_softc *sc, struct gicv3_its *its)
    609 {
    610 	uint64_t cbaser;
    611 
    612 	gicv3_dma_alloc(sc, &its->its_cmd, GITS_COMMANDS_SIZE, GITS_COMMANDS_ALIGN);
    613 
    614 	cbaser = its->its_cmd.segs[0].ds_addr;
    615 	cbaser |= __SHIFTIN(GITS_Cache_NORMAL_NC, GITS_CBASER_InnerCache);
    616 	cbaser |= __SHIFTIN(GITS_Shareability_NS, GITS_CBASER_Shareability);
    617 	cbaser |= __SHIFTIN((its->its_cmd.len / 4096) - 1, GITS_CBASER_Size);
    618 	cbaser |= GITS_CBASER_Valid;
    619 
    620 	gits_write_8(its, GITS_CBASER, cbaser);
    621 	gits_write_8(its, GITS_CWRITER, 0);
    622 }
    623 
    624 static void
    625 gicv3_its_table_init(struct gicv3_softc *sc, struct gicv3_its *its)
    626 {
    627 	u_int table_size, page_size, table_align;
    628 	uint64_t baser;
    629 	int tab;
    630 
    631 	const uint64_t typer = gits_read_8(its, GITS_TYPER);
    632 
    633 	/* devbits and innercache defaults */
    634 	u_int devbits = __SHIFTOUT(typer, GITS_TYPER_Devbits) + 1;
    635 	u_int innercache = GITS_Cache_NORMAL_NC;
    636 
    637 	uint32_t iidr = gits_read_4(its, GITS_IIDR);
    638 	const uint32_t ctx =
    639 	   __SHIFTIN(GITS_IIDR_IMP_CAVIUM, GITS_IIDR_Implementor) |
    640 	   __SHIFTIN(GITS_IIDR_PID_CAVIUM_THUNDERX, GITS_IIDR_ProductID) |
    641 	   __SHIFTIN(0, GITS_IIDR_Variant);
    642 	const uint32_t mask =
    643 	    GITS_IIDR_Implementor |
    644 	    GITS_IIDR_ProductID |
    645 	    GITS_IIDR_Variant;
    646 
    647 	if ((iidr & mask) == ctx) {
    648 		devbits = 20;		/* 8Mb */
    649 		innercache = GITS_Cache_DEVICE_nGnRnE;
    650 		aprint_normal_dev(sc->sc_dev, "Cavium ThunderX errata detected\n");
    651 	}
    652 
    653 	for (tab = 0; tab < 8; tab++) {
    654 		baser = gits_read_8(its, GITS_BASERn(tab));
    655 
    656 		const u_int entry_size = __SHIFTOUT(baser, GITS_BASER_Entry_Size) + 1;
    657 
    658 		switch (__SHIFTOUT(baser, GITS_BASER_Page_Size)) {
    659 		case GITS_Page_Size_4KB:
    660 			page_size = 4096;
    661 			table_align = 4096;
    662 			break;
    663 		case GITS_Page_Size_16KB:
    664 			page_size = 16384;
    665 			table_align = 4096;
    666 			break;
    667 		case GITS_Page_Size_64KB:
    668 		default:
    669 			page_size = 65536;
    670 			table_align = 65536;
    671 			break;
    672 		}
    673 
    674 		switch (__SHIFTOUT(baser, GITS_BASER_Type)) {
    675 		case GITS_Type_Devices:
    676 			/*
    677 			 * Table size scales with the width of the DeviceID.
    678 			 */
    679 			table_size = roundup(entry_size * (1 << devbits), page_size);
    680 			break;
    681 		case GITS_Type_InterruptCollections:
    682 			/*
    683 			 * Allocate space for one interrupt collection per CPU.
    684 			 */
    685 			table_size = roundup(entry_size * MAXCPUS, page_size);
    686 			break;
    687 		default:
    688 			table_size = 0;
    689 			break;
    690 		}
    691 
    692 		if (table_size == 0)
    693 			continue;
    694 
    695 		aprint_normal_dev(sc->sc_dev, "ITS TT%u type %#x size %#x\n", tab, (u_int)__SHIFTOUT(baser, GITS_BASER_Type), table_size);
    696 		gicv3_dma_alloc(sc, &its->its_tab[tab], table_size, table_align);
    697 
    698 		baser &= ~GITS_BASER_Size;
    699 		baser |= __SHIFTIN(table_size / page_size - 1, GITS_BASER_Size);
    700 		baser &= ~GITS_BASER_Physical_Address;
    701 		baser |= its->its_tab[tab].segs[0].ds_addr;
    702 		baser &= ~GITS_BASER_InnerCache;
    703 		baser |= __SHIFTIN(innercache, GITS_BASER_InnerCache);
    704 		baser &= ~GITS_BASER_Shareability;
    705 		baser |= __SHIFTIN(GITS_Shareability_NS, GITS_BASER_Shareability);
    706 		baser |= GITS_BASER_Valid;
    707 
    708 		gits_write_8(its, GITS_BASERn(tab), baser);
    709 	}
    710 }
    711 
    712 static void
    713 gicv3_its_enable(struct gicv3_softc *sc, struct gicv3_its *its)
    714 {
    715 	uint32_t ctlr;
    716 
    717 	ctlr = gits_read_4(its, GITS_CTLR);
    718 	ctlr |= GITS_CTLR_Enabled;
    719 	gits_write_4(its, GITS_CTLR, ctlr);
    720 }
    721 
    722 static void
    723 gicv3_its_cpu_init(void *priv, struct cpu_info *ci)
    724 {
    725 	struct gicv3_its * const its = priv;
    726 	struct gicv3_softc * const sc = its->its_gic;
    727 	const struct pci_attach_args *pa;
    728 	uint64_t rdbase;
    729 	size_t irq;
    730 
    731 	const uint64_t typer = bus_space_read_8(sc->sc_bst, its->its_bsh, GITS_TYPER);
    732 	if (typer & GITS_TYPER_PTA) {
    733 		void *va = bus_space_vaddr(sc->sc_bst, sc->sc_bsh_r[ci->ci_gic_redist]);
    734 		rdbase = vtophys((vaddr_t)va);
    735 	} else {
    736 		rdbase = (uint64_t)sc->sc_processor_id[cpu_index(ci)] << 16;
    737 	}
    738 	its->its_rdbase[cpu_index(ci)] = rdbase;
    739 
    740 	/*
    741 	 * Map collection ID of this CPU's index to this CPU's redistributor.
    742 	 */
    743 	gits_command_mapc(its, cpu_index(ci), rdbase, true);
    744 	gits_command_invall(its, cpu_index(ci));
    745 	gits_wait(its);
    746 
    747 	/*
    748 	 * Update routing for LPIs targetting this CPU
    749 	 */
    750 	for (irq = 0; irq < its->its_pic->pic_maxsources; irq++) {
    751 		if (its->its_targets[irq] != ci)
    752 			continue;
    753 		pa = its->its_pa[irq];
    754 		KASSERT(pa != NULL);
    755 
    756 		const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
    757 		gits_command_movi(its, devid, irq + its->its_pic->pic_irqbase, cpu_index(ci));
    758 		gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
    759 	}
    760 
    761 	its->its_cpuonline[cpu_index(ci)] = true;
    762 }
    763 
    764 static void
    765 gicv3_its_get_affinity(void *priv, size_t irq, kcpuset_t *affinity)
    766 {
    767 	struct gicv3_its * const its = priv;
    768 	struct cpu_info *ci;
    769 
    770 	kcpuset_zero(affinity);
    771 	ci = its->its_targets[irq];
    772 	if (ci)
    773 		kcpuset_set(affinity, cpu_index(ci));
    774 }
    775 
    776 static int
    777 gicv3_its_set_affinity(void *priv, size_t irq, const kcpuset_t *affinity)
    778 {
    779 	struct gicv3_its * const its = priv;
    780 	const struct pci_attach_args *pa;
    781 	struct cpu_info *ci;
    782 
    783 	const int set = kcpuset_countset(affinity);
    784 	if (set != 1)
    785 		return EINVAL;
    786 
    787 	pa = its->its_pa[irq];
    788 	if (pa == NULL)
    789 		return EINVAL;
    790 
    791 	ci = cpu_lookup(kcpuset_ffs(affinity) - 1);
    792 	its->its_targets[irq] = ci;
    793 
    794 	if (its->its_cpuonline[cpu_index(ci)] == true) {
    795 		const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
    796 		gits_command_movi(its, devid, irq + its->its_pic->pic_irqbase, cpu_index(ci));
    797 		gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
    798 	}
    799 
    800 	return 0;
    801 }
    802 
    803 int
    804 gicv3_its_init(struct gicv3_softc *sc, bus_space_handle_t bsh,
    805     uint64_t its_base, uint32_t its_id)
    806 {
    807 	struct gicv3_its *its;
    808 	struct arm_pci_msi *msi;
    809 
    810 	const uint64_t typer = bus_space_read_8(sc->sc_bst, bsh, GITS_TYPER);
    811 	if ((typer & GITS_TYPER_Physical) == 0)
    812 		return ENXIO;
    813 
    814 	its = kmem_alloc(sizeof(*its), KM_SLEEP);
    815 	its->its_id = its_id;
    816 	its->its_bst = sc->sc_bst;
    817 	its->its_bsh = bsh;
    818 	its->its_dmat = sc->sc_dmat;
    819 	its->its_base = its_base;
    820 	its->its_pic = &sc->sc_lpi;
    821 	KASSERT(its->its_pic->pic_maxsources > 0);
    822 	its->its_pa = kmem_zalloc(sizeof(struct pci_attach_args *) * its->its_pic->pic_maxsources, KM_SLEEP);
    823 	its->its_targets = kmem_zalloc(sizeof(struct cpu_info *) * its->its_pic->pic_maxsources, KM_SLEEP);
    824 	its->its_gic = sc;
    825 	its->its_cb.cpu_init = gicv3_its_cpu_init;
    826 	its->its_cb.get_affinity = gicv3_its_get_affinity;
    827 	its->its_cb.set_affinity = gicv3_its_set_affinity;
    828 	its->its_cb.priv = its;
    829 	LIST_INIT(&its->its_devices);
    830 	LIST_INSERT_HEAD(&sc->sc_lpi_callbacks, &its->its_cb, list);
    831 
    832 	gicv3_its_command_init(sc, its);
    833 	gicv3_its_table_init(sc, its);
    834 
    835 	gicv3_its_enable(sc, its);
    836 
    837 	gicv3_its_cpu_init(its, curcpu());
    838 
    839 	msi = &its->its_msi;
    840 	msi->msi_dev = sc->sc_dev;
    841 	msi->msi_priv = its;
    842 	msi->msi_alloc = gicv3_its_msi_alloc;
    843 	msi->msix_alloc = gicv3_its_msix_alloc;
    844 	msi->msi_intr_establish = gicv3_its_msi_intr_establish;
    845 	msi->msi_intr_release = gicv3_its_msi_intr_release;
    846 
    847 	return arm_pci_msi_add(msi);
    848 }
    849