Home | History | Annotate | Line # | Download | only in acpi
acpi_pci_machdep.c revision 1.4
      1 /* $NetBSD: acpi_pci_machdep.c,v 1.4 2018/10/21 11:56:26 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: acpi_pci_machdep.c,v 1.4 2018/10/21 11:56:26 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_mcfg.h>
     56 #include <dev/acpi/acpi_pci.h>
     57 
     58 #include <arm/acpi/acpi_pci_machdep.h>
     59 
     60 #include <arm/pci/pci_msi_machdep.h>
     61 
     62 struct acpi_pci_prt {
     63 	u_int				prt_bus;
     64 	ACPI_HANDLE			prt_handle;
     65 	TAILQ_ENTRY(acpi_pci_prt)	prt_list;
     66 };
     67 
     68 static TAILQ_HEAD(, acpi_pci_prt) acpi_pci_irq_routes =
     69     TAILQ_HEAD_INITIALIZER(acpi_pci_irq_routes);
     70 
     71 static void	acpi_pci_md_attach_hook(device_t, device_t,
     72 				       struct pcibus_attach_args *);
     73 static int	acpi_pci_md_bus_maxdevs(void *, int);
     74 static pcitag_t	acpi_pci_md_make_tag(void *, int, int, int);
     75 static void	acpi_pci_md_decompose_tag(void *, pcitag_t, int *, int *, int *);
     76 static pcireg_t	acpi_pci_md_conf_read(void *, pcitag_t, int);
     77 static void	acpi_pci_md_conf_write(void *, pcitag_t, int, pcireg_t);
     78 static int	acpi_pci_md_conf_hook(void *, int, int, int, pcireg_t);
     79 static void	acpi_pci_md_conf_interrupt(void *, int, int, int, int, int *);
     80 
     81 static int	acpi_pci_md_intr_map(const struct pci_attach_args *,
     82 				    pci_intr_handle_t *);
     83 static const char *acpi_pci_md_intr_string(void *, pci_intr_handle_t,
     84 					  char *, size_t);
     85 static const struct evcnt *acpi_pci_md_intr_evcnt(void *, pci_intr_handle_t);
     86 static int	acpi_pci_md_intr_setattr(void *, pci_intr_handle_t *, int,
     87 					uint64_t);
     88 static void *	acpi_pci_md_intr_establish(void *, pci_intr_handle_t,
     89 					 int, int (*)(void *), void *);
     90 static void	acpi_pci_md_intr_disestablish(void *, void *);
     91 
     92 struct arm32_pci_chipset arm_acpi_pci_chipset = {
     93 	.pc_attach_hook = acpi_pci_md_attach_hook,
     94 	.pc_bus_maxdevs = acpi_pci_md_bus_maxdevs,
     95 	.pc_make_tag = acpi_pci_md_make_tag,
     96 	.pc_decompose_tag = acpi_pci_md_decompose_tag,
     97 	.pc_conf_read = acpi_pci_md_conf_read,
     98 	.pc_conf_write = acpi_pci_md_conf_write,
     99 	.pc_conf_hook = acpi_pci_md_conf_hook,
    100 	.pc_conf_interrupt = acpi_pci_md_conf_interrupt,
    101 
    102 	.pc_intr_map = acpi_pci_md_intr_map,
    103 	.pc_intr_string = acpi_pci_md_intr_string,
    104 	.pc_intr_evcnt = acpi_pci_md_intr_evcnt,
    105 	.pc_intr_setattr = acpi_pci_md_intr_setattr,
    106 	.pc_intr_establish = acpi_pci_md_intr_establish,
    107 	.pc_intr_disestablish = acpi_pci_md_intr_disestablish,
    108 };
    109 
    110 static ACPI_STATUS
    111 acpi_pci_md_pci_link(ACPI_HANDLE handle, int bus)
    112 {
    113 	ACPI_PCI_ROUTING_TABLE *prt;
    114 	ACPI_HANDLE linksrc;
    115 	ACPI_BUFFER buf;
    116 	ACPI_STATUS rv;
    117 	void *linkdev;
    118 
    119 	rv = acpi_get(handle, &buf, AcpiGetIrqRoutingTable);
    120 	if (ACPI_FAILURE(rv))
    121 		return rv;
    122 
    123 	for (char *p = buf.Pointer; ; p += prt->Length) {
    124 		prt = (ACPI_PCI_ROUTING_TABLE *)p;
    125 		if (prt->Length == 0)
    126 			break;
    127 
    128 		const u_int dev = ACPI_HIWORD(prt->Address);
    129 		if (prt->Source[0] != 0) {
    130 			aprint_debug("ACPI: %s dev %u INT%c on lnkdev %s\n",
    131 			    acpi_name(handle), dev, 'A' + (prt->Pin & 3), prt->Source);
    132 			rv = AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &linksrc);
    133 			if (ACPI_FAILURE(rv)) {
    134 				aprint_debug("ACPI: AcpiGetHandle failed for '%s': %s\n",
    135 				    prt->Source, AcpiFormatException(rv));
    136 				continue;
    137 			}
    138 
    139 			linkdev = acpi_pci_link_devbyhandle(linksrc);
    140 			acpi_pci_link_add_reference(linkdev, 0, bus, dev, prt->Pin & 3);
    141 		} else {
    142 			aprint_debug("ACPI: %s dev %u INT%c on globint %d\n",
    143 			    acpi_name(handle), dev, 'A' + (prt->Pin & 3), prt->SourceIndex);
    144 		}
    145 	}
    146 
    147 	return AE_OK;
    148 }
    149 
    150 static void
    151 acpi_pci_md_attach_hook(device_t parent, device_t self,
    152     struct pcibus_attach_args *pba)
    153 {
    154 	struct acpi_pci_context *ap = pba->pba_pc->pc_conf_v;
    155 	struct acpi_pci_prt *prt, *prtp;
    156 	struct acpi_devnode *ad;
    157 	ACPI_HANDLE handle;
    158 	int seg, bus, dev, func;
    159 
    160 	seg = ap->ap_seg;
    161 	handle = NULL;
    162 
    163 	if (pba->pba_bridgetag) {
    164 		/*
    165 		 * Find the PCI address of our parent bridge and look for the
    166 		 * corresponding ACPI device node. If there is no node for this
    167 		 * bus, use the parent bridge routing information.
    168 		 */
    169 		acpi_pci_md_decompose_tag(NULL, *pba->pba_bridgetag, &bus, &dev, &func);
    170 		ad = acpi_pcidev_find(seg, bus, dev, func);
    171 		if (ad != NULL) {
    172 			handle = ad->ad_handle;
    173 		} else {
    174 			/* No routes defined for this bus, copy from parent */
    175 			TAILQ_FOREACH(prtp, &acpi_pci_irq_routes, prt_list)
    176 				if (prtp->prt_bus == bus) {
    177 					handle = prtp->prt_handle;
    178 					break;
    179 				}
    180 		}
    181 	} else {
    182 		/*
    183 		 * Lookup the ACPI device node for the root bus.
    184 		 */
    185 		ad = acpi_pciroot_find(seg, 0);
    186 		if (ad != NULL)
    187 			handle = ad->ad_handle;
    188 	}
    189 
    190 	if (handle != NULL) {
    191 		prt = kmem_alloc(sizeof(*prt), KM_SLEEP);
    192 		prt->prt_bus = pba->pba_bus;
    193 		prt->prt_handle = handle;
    194 		TAILQ_INSERT_TAIL(&acpi_pci_irq_routes, prt, prt_list);
    195 	}
    196 
    197 	acpimcfg_map_bus(self, pba->pba_pc, pba->pba_bus);
    198 
    199 	if (ad != NULL) {
    200 		/*
    201 		 * This is a new ACPI managed bus. Add PCI link references.
    202 		 */
    203 		acpi_pci_md_pci_link(ad->ad_handle, pba->pba_bus);
    204 	}
    205 }
    206 
    207 static int
    208 acpi_pci_md_bus_maxdevs(void *v, int busno)
    209 {
    210 	return 32;
    211 }
    212 
    213 static pcitag_t
    214 acpi_pci_md_make_tag(void *v, int b, int d, int f)
    215 {
    216 	return (b << 16) | (d << 11) | (f << 8);
    217 }
    218 
    219 static void
    220 acpi_pci_md_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
    221 {
    222 	if (bp)
    223 		*bp = (tag >> 16) & 0xff;
    224 	if (dp)
    225 		*dp = (tag >> 11) & 0x1f;
    226 	if (fp)
    227 		*fp = (tag >> 8) & 0x7;
    228 }
    229 
    230 static pcireg_t
    231 acpi_pci_md_conf_read(void *v, pcitag_t tag, int offset)
    232 {
    233 	struct acpi_pci_context * const ap = v;
    234 	pcireg_t val;
    235 
    236 	if (offset < 0 || offset >= PCI_EXTCONF_SIZE)
    237 		return (pcireg_t) -1;
    238 
    239 	acpimcfg_conf_read(&ap->ap_pc, tag, offset, &val);
    240 
    241 	return val;
    242 }
    243 
    244 static void
    245 acpi_pci_md_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
    246 {
    247 	struct acpi_pci_context * const ap = v;
    248 
    249 	if (offset < 0 || offset >= PCI_EXTCONF_SIZE)
    250 		return;
    251 
    252 	acpimcfg_conf_write(&ap->ap_pc, tag, offset, val);
    253 }
    254 
    255 static int
    256 acpi_pci_md_conf_hook(void *v, int b, int d, int f, pcireg_t id)
    257 {
    258 	return PCI_CONF_DEFAULT;
    259 }
    260 
    261 static void
    262 acpi_pci_md_conf_interrupt(void *v, int bus, int dev, int ipin, int sqiz, int *ilinep)
    263 {
    264 }
    265 
    266 static struct acpi_pci_prt *
    267 acpi_pci_md_intr_find_prt(u_int bus)
    268 {
    269 	struct acpi_pci_prt *prt, *prtp;
    270 
    271 	prt = NULL;
    272 	TAILQ_FOREACH(prtp, &acpi_pci_irq_routes, prt_list)
    273 		if (prtp->prt_bus == bus) {
    274 			prt = prtp;
    275 			break;
    276 		}
    277 
    278 	return prt;
    279 }
    280 
    281 static int
    282 acpi_pci_md_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ih)
    283 {
    284 	struct acpi_pci_prt *prt;
    285 	ACPI_PCI_ROUTING_TABLE *tab;
    286 	int line, pol, trig, error;
    287 	ACPI_HANDLE linksrc;
    288 	ACPI_BUFFER buf;
    289 	void *linkdev;
    290 
    291 	if (pa->pa_intrpin == PCI_INTERRUPT_PIN_NONE)
    292 		return EINVAL;
    293 
    294 	prt = acpi_pci_md_intr_find_prt(pa->pa_bus);
    295 	if (prt == NULL)
    296 		return ENXIO;
    297 
    298 	if (ACPI_FAILURE(acpi_get(prt->prt_handle, &buf, AcpiGetIrqRoutingTable)))
    299 		return EIO;
    300 
    301 	error = ENOENT;
    302 	for (char *p = buf.Pointer; ; p += tab->Length) {
    303 		tab = (ACPI_PCI_ROUTING_TABLE *)p;
    304 		if (tab->Length == 0)
    305 			break;
    306 
    307 		if (pa->pa_device == ACPI_HIWORD(tab->Address) &&
    308 		    (pa->pa_intrpin - 1) == (tab->Pin & 3)) {
    309 			if (tab->Source[0] != 0) {
    310 				if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, tab->Source, &linksrc)))
    311 					goto done;
    312 				linkdev = acpi_pci_link_devbyhandle(linksrc);
    313 				*ih = acpi_pci_link_route_interrupt(linkdev, tab->SourceIndex,
    314 				    &line, &pol, &trig);
    315 				error = 0;
    316 				goto done;
    317 			} else {
    318 				*ih = tab->SourceIndex;
    319 				error = 0;
    320 				goto done;
    321 			}
    322 		}
    323 	}
    324 
    325 done:
    326 	ACPI_FREE(buf.Pointer);
    327 	return error;
    328 }
    329 
    330 static const char *
    331 acpi_pci_md_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
    332 {
    333 	const int irq = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
    334 
    335 	if (ih & ARM_PCI_INTR_MSI)
    336 		snprintf(buf, len, "irq %d (MSI)", irq);
    337 	else
    338 		snprintf(buf, len, "irq %d", irq);
    339 
    340 	return buf;
    341 }
    342 
    343 static const struct evcnt *
    344 acpi_pci_md_intr_evcnt(void *v, pci_intr_handle_t ih)
    345 {
    346 	return NULL;
    347 }
    348 
    349 static int
    350 acpi_pci_md_intr_setattr(void *v, pci_intr_handle_t *ih, int attr, uint64_t data)
    351 {
    352 	switch (attr) {
    353 	case PCI_INTR_MPSAFE:
    354 		if (data)
    355 			*ih |= ARM_PCI_INTR_MPSAFE;
    356 		else
    357 			*ih &= ~ARM_PCI_INTR_MPSAFE;
    358 		return 0;
    359 	default:
    360 		return ENODEV;
    361 	}
    362 }
    363 
    364 static void *
    365 acpi_pci_md_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
    366     int (*callback)(void *), void *arg)
    367 {
    368 	struct acpi_pci_context * const ap = v;
    369 
    370 	if (ih & ARM_PCI_INTR_MSI)
    371 		return arm_pci_msi_intr_establish(&ap->ap_pc, ih, ipl, callback, arg);
    372 
    373 	const int irq = (int)__SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
    374 	const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? IST_MPSAFE : 0;
    375 
    376 	return intr_establish(irq, ipl, IST_LEVEL | mpsafe, callback, arg);
    377 }
    378 
    379 static void
    380 acpi_pci_md_intr_disestablish(void *v, void *vih)
    381 {
    382 	intr_disestablish(vih);
    383 }
    384