Home | History | Annotate | Line # | Download | only in acpi
acpipchb.c revision 1.14
      1 /* $NetBSD: acpipchb.c,v 1.14 2019/12/28 17:19:43 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 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: acpipchb.c,v 1.14 2019/12/28 17:19:43 jmcneill Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/device.h>
     38 #include <sys/intr.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/extent.h>
     42 #include <sys/queue.h>
     43 #include <sys/mutex.h>
     44 #include <sys/kmem.h>
     45 
     46 #include <machine/cpu.h>
     47 
     48 #include <arm/cpufunc.h>
     49 
     50 #include <dev/pci/pcireg.h>
     51 #include <dev/pci/pcivar.h>
     52 #include <dev/pci/pciconf.h>
     53 
     54 #include <dev/acpi/acpivar.h>
     55 #include <dev/acpi/acpi_pci.h>
     56 #include <dev/acpi/acpi_mcfg.h>
     57 
     58 #include <arm/acpi/acpi_pci_machdep.h>
     59 
     60 #define	PCIHOST_CACHELINE_SIZE		arm_dcache_align
     61 
     62 #define	ACPIPCHB_MAX_RANGES	64	/* XXX arbitrary limit */
     63 
     64 struct acpipchb_bus_range {
     65 	bus_addr_t		min;
     66 	bus_addr_t		max;
     67 	bus_addr_t		offset;
     68 };
     69 
     70 struct acpipchb_bus_space {
     71 	struct bus_space	bs;
     72 
     73 	struct acpipchb_bus_range range[ACPIPCHB_MAX_RANGES];
     74 	int			nrange;
     75 
     76 	int			(*map)(void *, bus_addr_t, bus_size_t,
     77 				       int, bus_space_handle_t *);
     78 
     79 	int			flags;
     80 };
     81 
     82 struct acpipchb_softc {
     83 	device_t		sc_dev;
     84 
     85 	bus_space_tag_t		sc_memt;
     86 
     87 	struct arm32_bus_dma_tag sc_dmat;
     88 	struct acpi_pci_context sc_ap;
     89 
     90 	ACPI_HANDLE		sc_handle;
     91 	ACPI_INTEGER		sc_bus;
     92 
     93 	struct acpipchb_bus_space sc_pcimem_bst;
     94 	struct acpipchb_bus_space sc_pciio_bst;
     95 };
     96 
     97 static int
     98 acpipchb_amazon_graviton_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *data)
     99 {
    100 	struct acpi_pci_context *ap = pc->pc_conf_v;
    101 	int b, d, f;
    102 
    103 	pci_decompose_tag(pc, tag, &b, &d, &f);
    104 
    105 	if (ap->ap_bus == b) {
    106 		if (d > 0 || f > 0) {
    107 			*data = -1;
    108 			return EINVAL;
    109 		}
    110 		*data = bus_space_read_4(ap->ap_bst, ap->ap_conf_bsh, reg);
    111 		return 0;
    112 	}
    113 
    114 	return acpimcfg_conf_read(pc, tag, reg, data);
    115 }
    116 
    117 static int
    118 acpipchb_amazon_graviton_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    119 {
    120 	struct acpi_pci_context *ap = pc->pc_conf_v;
    121 	int b, d, f;
    122 
    123 	pci_decompose_tag(pc, tag, &b, &d, &f);
    124 
    125 	if (ap->ap_bus == b) {
    126 		if (d > 0 || f > 0) {
    127 			return EINVAL;
    128 		}
    129 		bus_space_write_4(ap->ap_bst, ap->ap_conf_bsh, reg, data);
    130 		return 0;
    131 	}
    132 
    133 	return acpimcfg_conf_write(pc, tag, reg, data);
    134 }
    135 
    136 static ACPI_STATUS
    137 acpipchb_amazon_graviton_map(ACPI_HANDLE handle, UINT32 level, void *ctx, void **retval)
    138 {
    139 	struct acpi_pci_context *ap = ctx;
    140 	struct acpi_resources res;
    141 	struct acpi_mem *mem;
    142 	ACPI_HANDLE parent;
    143 	ACPI_STATUS rv;
    144 	int error;
    145 
    146 	rv = AcpiGetParent(handle, &parent);
    147 	if (ACPI_FAILURE(rv))
    148 		return rv;
    149 	if (ap->ap_handle != parent)
    150 		return AE_OK;
    151 
    152 	rv = acpi_resource_parse(ap->ap_dev, handle, "_CRS", &res, &acpi_resource_parse_ops_quiet);
    153 	if (ACPI_FAILURE(rv))
    154 		return rv;
    155 
    156 	mem = acpi_res_mem(&res, 0);
    157 	if (mem == NULL) {
    158 		acpi_resource_cleanup(&res);
    159 		return AE_NOT_FOUND;
    160 	}
    161 
    162 	error = bus_space_map(ap->ap_bst, mem->ar_base, mem->ar_length,
    163 	    _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &ap->ap_conf_bsh);
    164 	if (error != 0)
    165 		return AE_NO_MEMORY;
    166 
    167 	ap->ap_conf_read = acpipchb_amazon_graviton_conf_read;
    168 	ap->ap_conf_write = acpipchb_amazon_graviton_conf_write;
    169 
    170 	return AE_CTRL_TERMINATE;
    171 }
    172 
    173 static void
    174 acpipchb_amazon_graviton_init(struct acpi_pci_context *ap)
    175 {
    176 	ACPI_STATUS rv;
    177 
    178 	rv = AcpiGetDevices(__UNCONST("AMZN0001"), acpipchb_amazon_graviton_map, ap, NULL);
    179 	if (ACPI_FAILURE(rv))
    180 		return;
    181 }
    182 
    183 static const struct acpipchb_quirk {
    184 	const char			q_oemid[ACPI_OEM_ID_SIZE+1];
    185 	const char			q_oemtableid[ACPI_OEM_TABLE_ID_SIZE+1];
    186 	uint32_t			q_oemrevision;
    187 	void				(*q_init)(struct acpi_pci_context *);
    188 } acpipchb_quirks[] = {
    189 	{ "AMAZON",	"GRAVITON",	0,	acpipchb_amazon_graviton_init },
    190 };
    191 
    192 static const struct acpipchb_quirk *
    193 acpipchb_find_quirk(void)
    194 {
    195 	ACPI_STATUS rv;
    196 	ACPI_TABLE_MCFG *mcfg;
    197 	u_int n;
    198 
    199 	rv = AcpiGetTable(ACPI_SIG_MCFG, 0, (ACPI_TABLE_HEADER **)&mcfg);
    200 	if (ACPI_FAILURE(rv))
    201 		return NULL;
    202 
    203 	for (n = 0; n < __arraycount(acpipchb_quirks); n++) {
    204 		const struct acpipchb_quirk *q = &acpipchb_quirks[n];
    205 		if (memcmp(q->q_oemid, mcfg->Header.OemId, ACPI_OEM_ID_SIZE) == 0 &&
    206 		    memcmp(q->q_oemtableid, mcfg->Header.OemTableId, ACPI_OEM_TABLE_ID_SIZE) == 0 &&
    207 		    q->q_oemrevision == mcfg->Header.OemRevision)
    208 			return q;
    209 	}
    210 
    211 	return NULL;
    212 }
    213 
    214 static int	acpipchb_match(device_t, cfdata_t, void *);
    215 static void	acpipchb_attach(device_t, device_t, void *);
    216 
    217 static void	acpipchb_setup_ranges(struct acpipchb_softc *, struct pcibus_attach_args *);
    218 
    219 CFATTACH_DECL_NEW(acpipchb, sizeof(struct acpipchb_softc),
    220 	acpipchb_match, acpipchb_attach, NULL, NULL);
    221 
    222 static const char * const compatible[] = {
    223 	"PNP0A08",
    224 	NULL
    225 };
    226 
    227 static int
    228 acpipchb_match(device_t parent, cfdata_t cf, void *aux)
    229 {
    230 	struct acpi_attach_args *aa = aux;
    231 
    232 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    233 		return 0;
    234 
    235 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
    236 }
    237 
    238 static void
    239 acpipchb_attach(device_t parent, device_t self, void *aux)
    240 {
    241 	struct acpipchb_softc * const sc = device_private(self);
    242 	struct acpi_attach_args *aa = aux;
    243 	struct pcibus_attach_args pba;
    244 	const struct acpipchb_quirk *q;
    245 	ACPI_INTEGER cca, seg;
    246 
    247 	sc->sc_dev = self;
    248 	sc->sc_memt = aa->aa_memt;
    249 	sc->sc_handle = aa->aa_node->ad_handle;
    250 
    251 	if (ACPI_FAILURE(acpi_eval_integer(sc->sc_handle, "_BBN", &sc->sc_bus)))
    252 		sc->sc_bus = 0;
    253 
    254 	if (ACPI_FAILURE(acpi_eval_integer(sc->sc_handle, "_SEG", &seg)))
    255 		seg = 0;
    256 
    257 	if (ACPI_FAILURE(acpi_eval_integer(sc->sc_handle, "_CCA", &cca)))
    258 		cca = 1;
    259 
    260 	aprint_naive("\n");
    261 	aprint_normal(": PCI Express Host Bridge\n");
    262 
    263 	sc->sc_dmat = *aa->aa_dmat;
    264 	if (cca == 0)
    265 		sc->sc_dmat._nranges = 0;
    266 
    267 	sc->sc_ap.ap_dev = self;
    268 	sc->sc_ap.ap_pc = *aa->aa_pc;
    269 	sc->sc_ap.ap_pc.pc_conf_v = &sc->sc_ap;
    270 	sc->sc_ap.ap_seg = seg;
    271 	sc->sc_ap.ap_handle = sc->sc_handle;
    272 	sc->sc_ap.ap_bus = sc->sc_bus;
    273 	sc->sc_ap.ap_bst = sc->sc_memt;
    274 
    275 	q = acpipchb_find_quirk();
    276 	if (q != NULL)
    277 		q->q_init(&sc->sc_ap);
    278 
    279 	if (acpi_pci_ignore_boot_config(sc->sc_handle)) {
    280 		if (acpimcfg_configure_bus(self, &sc->sc_ap.ap_pc, sc->sc_handle, sc->sc_bus, PCIHOST_CACHELINE_SIZE) != 0)
    281 			aprint_error_dev(self, "failed to configure bus\n");
    282 	}
    283 
    284 	memset(&pba, 0, sizeof(pba));
    285 	pba.pba_flags = aa->aa_pciflags & ~(PCI_FLAGS_MEM_OKAY | PCI_FLAGS_IO_OKAY);
    286 	pba.pba_memt = 0;
    287 	pba.pba_iot = 0;
    288 	pba.pba_dmat = &sc->sc_dmat;
    289 #ifdef _PCI_HAVE_DMA64
    290 	pba.pba_dmat64 = &sc->sc_dmat;
    291 #endif
    292 	pba.pba_pc = &sc->sc_ap.ap_pc;
    293 	pba.pba_bus = sc->sc_bus;
    294 
    295 	acpipchb_setup_ranges(sc, &pba);
    296 
    297 	config_found_ia(self, "pcibus", &pba, pcibusprint);
    298 }
    299 
    300 struct acpipchb_setup_ranges_args {
    301 	struct acpipchb_softc *sc;
    302 	struct pcibus_attach_args *pba;
    303 };
    304 
    305 static int
    306 acpipchb_bus_space_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
    307     bus_space_handle_t *bshp)
    308 {
    309 	struct acpipchb_bus_space * const abs = t;
    310 	int i;
    311 
    312 	if (size == 0)
    313 		return ERANGE;
    314 
    315 	if ((abs->flags & PCI_FLAGS_IO_OKAY) != 0) {
    316 		/* Force strongly ordered mapping for all I/O space */
    317 		flag = _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED;
    318 	}
    319 
    320 	for (i = 0; i < abs->nrange; i++) {
    321 		struct acpipchb_bus_range * const range = &abs->range[i];
    322 		if (bpa >= range->min && bpa + size - 1 <= range->max)
    323 			return abs->map(t, bpa + range->offset, size, flag, bshp);
    324 	}
    325 
    326 	return ERANGE;
    327 }
    328 
    329 static ACPI_STATUS
    330 acpipchb_setup_ranges_cb(ACPI_RESOURCE *res, void *ctx)
    331 {
    332 	struct acpipchb_setup_ranges_args * const args = ctx;
    333 	struct acpipchb_softc * const sc = args->sc;
    334 	struct pcibus_attach_args *pba = args->pba;
    335 	struct acpipchb_bus_space *abs;
    336 	struct acpipchb_bus_range *range;
    337 	const char *range_type;
    338 	u_int pci_flags;
    339 
    340 	if (res->Type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
    341 	    res->Type != ACPI_RESOURCE_TYPE_ADDRESS64)
    342 		return AE_OK;
    343 
    344 	switch (res->Data.Address.ResourceType) {
    345 	case ACPI_IO_RANGE:
    346 		abs = &sc->sc_pciio_bst;
    347 		range_type = "I/O";
    348 		pci_flags = PCI_FLAGS_IO_OKAY;
    349 		break;
    350 	case ACPI_MEMORY_RANGE:
    351 		abs = &sc->sc_pcimem_bst;
    352 		range_type = "MEM";
    353 		pci_flags = PCI_FLAGS_MEM_OKAY;
    354 		break;
    355 	default:
    356 		return AE_OK;
    357 	}
    358 
    359 	if (abs->nrange == ACPIPCHB_MAX_RANGES) {
    360 		aprint_error_dev(sc->sc_dev,
    361 		    "maximum number of ranges reached, increase ACPIPCHB_MAX_RANGES\n");
    362 		return AE_LIMIT;
    363 	}
    364 
    365 	range = &abs->range[abs->nrange];
    366 	switch (res->Type) {
    367 	case ACPI_RESOURCE_TYPE_ADDRESS32:
    368 		range->min = res->Data.Address32.Address.Minimum;
    369 		range->max = res->Data.Address32.Address.Maximum;
    370 		range->offset = res->Data.Address32.Address.TranslationOffset;
    371 		break;
    372 	case ACPI_RESOURCE_TYPE_ADDRESS64:
    373 		range->min = res->Data.Address64.Address.Minimum;
    374 		range->max = res->Data.Address64.Address.Maximum;
    375 		range->offset = res->Data.Address64.Address.TranslationOffset;
    376 		break;
    377 	default:
    378 		return AE_OK;
    379 	}
    380 	abs->nrange++;
    381 
    382 	aprint_debug_dev(sc->sc_dev, "PCI %s [%#lx-%#lx] -> %#lx\n", range_type, range->min, range->max, range->offset);
    383 
    384 	if ((pba->pba_flags & pci_flags) == 0) {
    385 		abs->bs = *sc->sc_memt;
    386 		abs->bs.bs_cookie = abs;
    387 		abs->map = abs->bs.bs_map;
    388 		abs->flags = pci_flags;
    389 		abs->bs.bs_map = acpipchb_bus_space_map;
    390 		if ((pci_flags & PCI_FLAGS_IO_OKAY) != 0)
    391 			pba->pba_iot = &abs->bs;
    392 		else if ((pci_flags & PCI_FLAGS_MEM_OKAY) != 0)
    393 			pba->pba_memt = &abs->bs;
    394 		pba->pba_flags |= pci_flags;
    395 	}
    396 
    397 	return AE_OK;
    398 }
    399 
    400 static void
    401 acpipchb_setup_ranges(struct acpipchb_softc *sc, struct pcibus_attach_args *pba)
    402 {
    403 	struct acpipchb_setup_ranges_args args;
    404 
    405 	args.sc = sc;
    406 	args.pba = pba;
    407 
    408 	AcpiWalkResources(sc->sc_handle, "_CRS", acpipchb_setup_ranges_cb, &args);
    409 }
    410