Module Name:    src
Committed By:   jmcneill
Date:           Sat Dec  8 15:04:40 UTC 2018

Modified Files:
        src/sys/arch/arm/acpi: acpi_pci_machdep.c files.acpi
        src/sys/arch/arm/cortex: gicv3_its.c
        src/sys/arch/arm/include: pci_machdep.h
Added Files:
        src/sys/arch/arm/acpi: acpi_iort.c acpi_iort.h

Log Message:
Add support for decoding PCI ID mappings using IO remapping tables (IORT).


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/arm/acpi/acpi_iort.c \
    src/sys/arch/arm/acpi/acpi_iort.h
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/arm/acpi/acpi_pci_machdep.c
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/arm/acpi/files.acpi
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/arm/cortex/gicv3_its.c
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/arm/include/pci_machdep.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/arm/acpi/acpi_pci_machdep.c
diff -u src/sys/arch/arm/acpi/acpi_pci_machdep.c:1.8 src/sys/arch/arm/acpi/acpi_pci_machdep.c:1.9
--- src/sys/arch/arm/acpi/acpi_pci_machdep.c:1.8	Fri Nov 16 15:06:21 2018
+++ src/sys/arch/arm/acpi/acpi_pci_machdep.c	Sat Dec  8 15:04:40 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: acpi_pci_machdep.c,v 1.8 2018/11/16 15:06:21 jmcneill Exp $ */
+/* $NetBSD: acpi_pci_machdep.c,v 1.9 2018/12/08 15:04:40 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_pci_machdep.c,v 1.8 2018/11/16 15:06:21 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_pci_machdep.c,v 1.9 2018/12/08 15:04:40 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -55,6 +55,7 @@ __KERNEL_RCSID(0, "$NetBSD: acpi_pci_mac
 #include <dev/acpi/acpi_mcfg.h>
 #include <dev/acpi/acpi_pci.h>
 
+#include <arm/acpi/acpi_iort.h>
 #include <arm/acpi/acpi_pci_machdep.h>
 
 #include <arm/pci/pci_msi_machdep.h>
@@ -75,6 +76,7 @@ static int	acpi_pci_md_bus_maxdevs(void 
 static pcitag_t	acpi_pci_md_make_tag(void *, int, int, int);
 static void	acpi_pci_md_decompose_tag(void *, pcitag_t, int *, int *, int *);
 static u_int	acpi_pci_md_get_segment(void *);
+static uint32_t	acpi_pci_md_get_devid(void *, uint32_t);
 static pcireg_t	acpi_pci_md_conf_read(void *, pcitag_t, int);
 static void	acpi_pci_md_conf_write(void *, pcitag_t, int, pcireg_t);
 static int	acpi_pci_md_conf_hook(void *, int, int, int, pcireg_t);
@@ -98,6 +100,7 @@ struct arm32_pci_chipset arm_acpi_pci_ch
 	.pc_make_tag = acpi_pci_md_make_tag,
 	.pc_decompose_tag = acpi_pci_md_decompose_tag,
 	.pc_get_segment = acpi_pci_md_get_segment,
+	.pc_get_devid = acpi_pci_md_get_devid,
 	.pc_conf_read = acpi_pci_md_conf_read,
 	.pc_conf_write = acpi_pci_md_conf_write,
 	.pc_conf_hook = acpi_pci_md_conf_hook,
@@ -240,6 +243,14 @@ acpi_pci_md_get_segment(void *v)
 	return ap->ap_seg;
 }
 
+static uint32_t
+acpi_pci_md_get_devid(void *v, uint32_t devid)
+{
+	struct acpi_pci_context * const ap = v;
+
+	return acpi_iort_pci_root_map(ap->ap_seg, devid);
+}
+
 static pcireg_t
 acpi_pci_md_conf_read(void *v, pcitag_t tag, int offset)
 {

Index: src/sys/arch/arm/acpi/files.acpi
diff -u src/sys/arch/arm/acpi/files.acpi:1.5 src/sys/arch/arm/acpi/files.acpi:1.6
--- src/sys/arch/arm/acpi/files.acpi:1.5	Mon Nov 12 12:56:05 2018
+++ src/sys/arch/arm/acpi/files.acpi	Sat Dec  8 15:04:40 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: files.acpi,v 1.5 2018/11/12 12:56:05 jmcneill Exp $
+#	$NetBSD: files.acpi,v 1.6 2018/12/08 15:04:40 jmcneill Exp $
 #
 # Configuration info for ACPI compliant ARM boards.
 #
@@ -10,6 +10,7 @@ defflag	opt_pcifixup.h		ACPI_PCI_FIXUP
 
 include "dev/acpi/files.acpi"
 
+file	arch/arm/acpi/acpi_iort.c		acpi
 file	arch/arm/acpi/acpi_machdep.c		acpi
 file	arch/arm/acpi/acpi_pci_machdep.c	acpi & pci
 file	arch/arm/acpi/acpi_platform.c		acpi

Index: src/sys/arch/arm/cortex/gicv3_its.c
diff -u src/sys/arch/arm/cortex/gicv3_its.c:1.9 src/sys/arch/arm/cortex/gicv3_its.c:1.10
--- src/sys/arch/arm/cortex/gicv3_its.c:1.9	Wed Nov 28 22:54:11 2018
+++ src/sys/arch/arm/cortex/gicv3_its.c	Sat Dec  8 15:04:40 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: gicv3_its.c,v 1.9 2018/11/28 22:54:11 jmcneill Exp $ */
+/* $NetBSD: gicv3_its.c,v 1.10 2018/12/08 15:04:40 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
 #define _INTR_PRIVATE
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: gicv3_its.c,v 1.9 2018/11/28 22:54:11 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: gicv3_its.c,v 1.10 2018/12/08 15:04:40 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/kmem.h>
@@ -282,11 +282,14 @@ gicv3_its_msi_free_lpi(struct gicv3_its 
 static uint32_t
 gicv3_its_devid(pci_chipset_tag_t pc, pcitag_t tag)
 {
+	uint32_t devid;
 	int b, d, f;
 
 	pci_decompose_tag(pc, tag, &b, &d, &f);
 
-	return (b << 8) | (d << 3) | f;
+	devid = (b << 8) | (d << 3) | f;
+
+	return pci_get_devid(pc, devid);
 }
 
 static int

Index: src/sys/arch/arm/include/pci_machdep.h
diff -u src/sys/arch/arm/include/pci_machdep.h:1.16 src/sys/arch/arm/include/pci_machdep.h:1.17
--- src/sys/arch/arm/include/pci_machdep.h:1.16	Fri Nov 16 15:06:22 2018
+++ src/sys/arch/arm/include/pci_machdep.h	Sat Dec  8 15:04:40 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pci_machdep.h,v 1.16 2018/11/16 15:06:22 jmcneill Exp $	*/
+/*	$NetBSD: pci_machdep.h,v 1.17 2018/12/08 15:04:40 jmcneill Exp $	*/
 
 /*
  * Modified for arm32 by Mark Brinicombe
@@ -92,6 +92,7 @@ struct arm32_pci_chipset {
 	void		(*pc_decompose_tag)(void *, pcitag_t, int *,
 			    int *, int *);
 	u_int		(*pc_get_segment)(void *);
+	uint32_t	(*pc_get_devid)(void *, uint32_t);
 	pcireg_t	(*pc_conf_read)(void *, pcitag_t, int);
 	void		(*pc_conf_write)(void *, pcitag_t, int, pcireg_t);
 
@@ -148,6 +149,8 @@ struct arm32_pci_chipset {
     (*(c)->pc_decompose_tag)((c)->pc_conf_v, (t), (bp), (dp), (fp))
 #define pci_get_segment(c)						\
     ((c)->pc_get_segment ? (*(c)->pc_get_segment)((c)->pc_conf_v) : 0)
+#define pci_get_devid(c, d)						\
+    ((c)->pc_get_devid ? (*(c)->pc_get_devid)((c)->pc_conf_v, (d)) : (d))
 #define	pci_conf_read(c, t, r)						\
     (*(c)->pc_conf_read)((c)->pc_conf_v, (t), (r))
 #define	pci_conf_write(c, t, r, v)					\

Added files:

Index: src/sys/arch/arm/acpi/acpi_iort.c
diff -u /dev/null src/sys/arch/arm/acpi/acpi_iort.c:1.1
--- /dev/null	Sat Dec  8 15:04:40 2018
+++ src/sys/arch/arm/acpi/acpi_iort.c	Sat Dec  8 15:04:40 2018
@@ -0,0 +1,110 @@
+/* $NetBSD: acpi_iort.c,v 1.1 2018/12/08 15:04:40 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jared McNeill <[email protected]>.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: acpi_iort.c,v 1.1 2018/12/08 15:04:40 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/cpu.h>
+
+#include <dev/acpi/acpireg.h>
+#include <dev/acpi/acpivar.h>
+
+#include <arm/acpi/acpi_iort.h>
+
+static ACPI_IORT_ID_MAPPING *
+acpi_iort_id_map(ACPI_IORT_NODE *node, uint32_t *id)
+{
+	ACPI_IORT_ID_MAPPING *map;
+	uint32_t offset, n;
+
+	offset = node->MappingOffset;
+	for (n = 0; n < node->MappingCount; n++) {
+		map = ACPI_ADD_PTR(ACPI_IORT_ID_MAPPING, node, offset);
+		if (map->Flags & ACPI_IORT_ID_SINGLE_MAPPING) {
+			*id = map->InputBase;
+			return map;
+		}
+		if (*id >= map->InputBase && *id <= map->InputBase + map->IdCount) {
+			*id = *id - map->InputBase + map->OutputBase;
+			return map;
+		}
+		offset += sizeof(ACPI_IORT_ID_MAPPING);
+	}
+
+	return NULL;
+}
+
+static ACPI_IORT_NODE *
+acpi_iort_find_ref(ACPI_TABLE_IORT *iort, ACPI_IORT_NODE *node, uint32_t *id)
+{
+	ACPI_IORT_ID_MAPPING *map;
+
+	map = acpi_iort_id_map(node, id);
+	if (map == NULL)
+		return NULL;
+
+	return ACPI_ADD_PTR(ACPI_IORT_NODE, iort, map->OutputReference);
+}
+
+uint32_t
+acpi_iort_pci_root_map(u_int seg, uint32_t devid)
+{
+	ACPI_TABLE_IORT *iort;
+	ACPI_IORT_NODE *node;
+	ACPI_IORT_ROOT_COMPLEX *root;
+	uint32_t offset, n;
+	ACPI_STATUS rv;
+
+	rv = AcpiGetTable(ACPI_SIG_IORT, 0, (ACPI_TABLE_HEADER **)&iort);
+	if (ACPI_FAILURE(rv))
+		return devid;
+
+	offset = iort->NodeOffset;
+	for (n = 0; n < iort->NodeCount; n++) {
+		node = ACPI_ADD_PTR(ACPI_IORT_NODE, iort, offset);
+		if (node->Type == ACPI_IORT_NODE_PCI_ROOT_COMPLEX) {
+			root = (ACPI_IORT_ROOT_COMPLEX *)node->NodeData;
+			if (root->PciSegmentNumber == seg) {
+				const uint32_t odevid = devid;
+				do {
+					node = acpi_iort_find_ref(iort, node, &devid);
+				} while (node != NULL);
+				aprint_debug("ACPI: IORT remapped devid %#x -> %#x\n", odevid, devid);
+				return devid;
+			}
+		}
+		offset += node->Length;
+	}
+
+	return devid;
+}
Index: src/sys/arch/arm/acpi/acpi_iort.h
diff -u /dev/null src/sys/arch/arm/acpi/acpi_iort.h:1.1
--- /dev/null	Sat Dec  8 15:04:40 2018
+++ src/sys/arch/arm/acpi/acpi_iort.h	Sat Dec  8 15:04:40 2018
@@ -0,0 +1,37 @@
+/* $NetBSD: acpi_iort.h,v 1.1 2018/12/08 15:04:40 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jared McNeill <[email protected]>.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _ARM_ACPI_ACPI_IORT_H
+#define _ARM_ACPI_ACPI_IORT_H
+
+uint32_t	acpi_iort_pci_root_map(u_int, uint32_t);
+
+#endif /* !_ARM_ACPI_ACPI_IORT_H */

Reply via email to