Module Name:    src
Committed By:   jmcneill
Date:           Sun Sep 13 21:41:17 UTC 2020

Modified Files:
        src/sys/arch/arm/acpi: acpi_iort.c acpi_pci_n1sdp.c acpi_platform.c
            acpi_table.c

Log Message:
Make Arm MD ACPI code big endian friendly.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/arm/acpi/acpi_iort.c
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/arm/acpi/acpi_pci_n1sdp.c
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/arm/acpi/acpi_platform.c
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/arm/acpi/acpi_table.c

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_iort.c
diff -u src/sys/arch/arm/acpi/acpi_iort.c:1.3 src/sys/arch/arm/acpi/acpi_iort.c:1.4
--- src/sys/arch/arm/acpi/acpi_iort.c:1.3	Thu Feb 13 00:02:21 2020
+++ src/sys/arch/arm/acpi/acpi_iort.c	Sun Sep 13 21:41:17 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: acpi_iort.c,v 1.3 2020/02/13 00:02:21 jmcneill Exp $ */
+/* $NetBSD: acpi_iort.c,v 1.4 2020/09/13 21:41:17 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_iort.c,v 1.3 2020/02/13 00:02:21 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_iort.c,v 1.4 2020/09/13 21:41:17 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -47,15 +47,15 @@ acpi_iort_id_map(ACPI_IORT_NODE *node, u
 	ACPI_IORT_ID_MAPPING *map;
 	uint32_t offset, n;
 
-	offset = node->MappingOffset;
-	for (n = 0; n < node->MappingCount; n++) {
+	offset = le32toh(node->MappingOffset);
+	for (n = 0; n < le32toh(node->MappingCount); n++) {
 		map = ACPI_ADD_PTR(ACPI_IORT_ID_MAPPING, node, offset);
-		if (map->Flags & ACPI_IORT_ID_SINGLE_MAPPING) {
-			*id = map->OutputBase;
+		if (le32toh(map->Flags) & ACPI_IORT_ID_SINGLE_MAPPING) {
+			*id = le32toh(map->OutputBase);
 			return map;
 		}
-		if (*id >= map->InputBase && *id <= map->InputBase + map->IdCount) {
-			*id = *id - map->InputBase + map->OutputBase;
+		if (*id >= le32toh(map->InputBase) && *id <= le32toh(map->InputBase) + le32toh(map->IdCount)) {
+			*id = *id - le32toh(map->InputBase) + le32toh(map->OutputBase);
 			return map;
 		}
 		offset += sizeof(ACPI_IORT_ID_MAPPING);
@@ -73,7 +73,7 @@ acpi_iort_find_ref(ACPI_TABLE_IORT *iort
 	if (map == NULL)
 		return NULL;
 
-	return ACPI_ADD_PTR(ACPI_IORT_NODE, iort, map->OutputReference);
+	return ACPI_ADD_PTR(ACPI_IORT_NODE, iort, le32toh(map->OutputReference));
 }
 
 uint32_t
@@ -89,12 +89,12 @@ acpi_iort_pci_root_map(u_int seg, uint32
 	if (ACPI_FAILURE(rv))
 		return devid;
 
-	offset = iort->NodeOffset;
-	for (n = 0; n < iort->NodeCount; n++) {
+	offset = le32toh(iort->NodeOffset);
+	for (n = 0; n < le32toh(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) {
+			if (le32toh(root->PciSegmentNumber) == seg) {
 				const uint32_t odevid = devid;
 				do {
 					node = acpi_iort_find_ref(iort, node, &devid);
@@ -103,7 +103,7 @@ acpi_iort_pci_root_map(u_int seg, uint32
 				return devid;
 			}
 		}
-		offset += node->Length;
+		offset += le16toh(node->Length);
 	}
 
 	return devid;
@@ -123,28 +123,28 @@ acpi_iort_its_id_map(u_int seg, uint32_t
 	if (ACPI_FAILURE(rv))
 		return 0;
 
-	offset = iort->NodeOffset;
-	for (n = 0; n < iort->NodeCount; n++) {
+	offset = le32toh(iort->NodeOffset);
+	for (n = 0; n < le32toh(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) {
+			if (le32toh(root->PciSegmentNumber) == seg) {
 				const uint32_t odevid = devid;
 				do {
 					node = acpi_iort_find_ref(iort, node, &devid);
 					if (node != NULL && node->Type == ACPI_IORT_NODE_ITS_GROUP) {
 						its_group = (ACPI_IORT_ITS_GROUP *)node->NodeData;
-						if (its_group->ItsCount == 0)
+						if (le32toh(its_group->ItsCount) == 0)
 							return 0;
 						aprint_debug("ACPI: IORT mapped devid %#x -> ITS %#x\n",
-						    odevid, its_group->Identifiers[0]);
-						return its_group->Identifiers[0];
+						    odevid, le32toh(its_group->Identifiers[0]));
+						return le32toh(its_group->Identifiers[0]);
 					}
 				} while (node != NULL);
 				return 0;
 			}
 		}
-		offset += node->Length;
+		offset += le16toh(node->Length);
 	}
 
 	return 0;

Index: src/sys/arch/arm/acpi/acpi_pci_n1sdp.c
diff -u src/sys/arch/arm/acpi/acpi_pci_n1sdp.c:1.4 src/sys/arch/arm/acpi/acpi_pci_n1sdp.c:1.5
--- src/sys/arch/arm/acpi/acpi_pci_n1sdp.c:1.4	Wed Jun 17 06:45:09 2020
+++ src/sys/arch/arm/acpi/acpi_pci_n1sdp.c	Sun Sep 13 21:41:17 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: acpi_pci_n1sdp.c,v 1.4 2020/06/17 06:45:09 thorpej Exp $ */
+/* $NetBSD: acpi_pci_n1sdp.c,v 1.5 2020/09/13 21:41:17 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_pci_n1sdp.c,v 1.4 2020/06/17 06:45:09 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_pci_n1sdp.c,v 1.5 2020/09/13 21:41:17 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -173,7 +173,7 @@ acpi_pci_n1sdp_init(struct acpi_pci_cont
 		aprint_debug_dev(ap->ap_dev, "N1SDP: RC @ 0x%08x, %d devices\n",
 		    n1sdp_data[ap->ap_seg]->rc_base_addr, n1sdp_data[ap->ap_seg]->nr_bdfs);
 		for (n = 0; n < n1sdp_data[ap->ap_seg]->nr_bdfs; n++) {
-			const uint32_t bdf = n1sdp_data[ap->ap_seg]->valid_bdfs[n];
+			const uint32_t bdf = le32toh(n1sdp_data[ap->ap_seg]->valid_bdfs[n]);
 			const int b = (bdf >> N1SDP_BUS_SHIFT) & 0xff;
 			const int d = (bdf >> N1SDP_DEV_SHIFT) & 0x1f;
 			const int f = (bdf >> N1SDP_FUNC_SHIFT) & 0x7;

Index: src/sys/arch/arm/acpi/acpi_platform.c
diff -u src/sys/arch/arm/acpi/acpi_platform.c:1.18 src/sys/arch/arm/acpi/acpi_platform.c:1.19
--- src/sys/arch/arm/acpi/acpi_platform.c:1.18	Sun Sep 22 18:31:59 2019
+++ src/sys/arch/arm/acpi/acpi_platform.c	Sun Sep 13 21:41:17 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: acpi_platform.c,v 1.18 2019/09/22 18:31:59 jmcneill Exp $ */
+/* $NetBSD: acpi_platform.c,v 1.19 2020/09/13 21:41:17 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
 #include "opt_multiprocessor.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.18 2019/09/22 18:31:59 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.19 2020/09/13 21:41:17 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -163,7 +163,7 @@ acpi_platform_startup(void)
 		}
 
 		if (spcr->SerialPort.SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
-		    spcr->SerialPort.Address != 0) {
+		    le64toh(spcr->SerialPort.Address) != 0) {
 			switch (spcr->InterfaceType) {
 #if NPLCOM > 0
 			case ACPI_DBG2_ARM_PL011:
@@ -171,7 +171,7 @@ acpi_platform_startup(void)
 			case ACPI_DBG2_ARM_SBSA_GENERIC:
 				plcom_console.pi_type = PLCOM_TYPE_PL011;
 				plcom_console.pi_iot = &arm_generic_bs_tag;
-				plcom_console.pi_iobase = spcr->SerialPort.Address;
+				plcom_console.pi_iobase = le64toh(spcr->SerialPort.Address);
 				plcom_console.pi_size = PL011COM_UART_SIZE;
 				plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
 
@@ -182,15 +182,15 @@ acpi_platform_startup(void)
 			case ACPI_DBG2_16550_COMPATIBLE:
 			case ACPI_DBG2_16550_SUBSET:
 				if (ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8) {
-					comcnattach(&arm_generic_bs_tag, spcr->SerialPort.Address, baud_rate, -1,
+					comcnattach(&arm_generic_bs_tag, le64toh(spcr->SerialPort.Address), baud_rate, -1,
 					    COM_TYPE_NORMAL, TTYDEF_CFLAG);
 				} else {
-					comcnattach(&arm_generic_a4x_bs_tag, spcr->SerialPort.Address, baud_rate, -1,
+					comcnattach(&arm_generic_a4x_bs_tag, le64toh(spcr->SerialPort.Address), baud_rate, -1,
 					    COM_TYPE_NORMAL, TTYDEF_CFLAG);
 				}
 				break;
 			case ACPI_DBG2_BCM2835:
-				comcnattach(&arm_generic_a4x_bs_tag, spcr->SerialPort.Address + 0x40, baud_rate, -1,
+				comcnattach(&arm_generic_a4x_bs_tag, le64toh(spcr->SerialPort.Address) + 0x40, baud_rate, -1,
 				    COM_TYPE_BCMAUXUART, TTYDEF_CFLAG);
 				cn_set_magic("+++++");
 				break;
@@ -207,8 +207,8 @@ acpi_platform_startup(void)
 	 * Initialize PSCI 0.2+ if implemented
 	 */
 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
-		if (fadt->ArmBootFlags & ACPI_FADT_PSCI_COMPLIANT) {
-			if (fadt->ArmBootFlags & ACPI_FADT_PSCI_USE_HVC) {
+		if (le16toh(fadt->ArmBootFlags) & ACPI_FADT_PSCI_COMPLIANT) {
+			if (le16toh(fadt->ArmBootFlags) & ACPI_FADT_PSCI_USE_HVC) {
 				psci_init(psci_call_hvc);
 			} else {
 				psci_init(psci_call_smc);
@@ -222,7 +222,7 @@ acpi_platform_startup(void)
 	 * Count CPUs
 	 */
 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
-		char *end = (char *)madt + madt->Header.Length;
+		char *end = (char *)madt + le32toh(madt->Header.Length);
 		char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
 		while (where < end) {
 			ACPI_SUBTABLE_HEADER *subtable = (ACPI_SUBTABLE_HEADER *)where;
@@ -269,7 +269,7 @@ acpi_platform_device_register(device_t s
 
 		if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY)
 			goto spcr_unmap;
-		if (spcr->SerialPort.Address == 0)
+		if (le64toh(spcr->SerialPort.Address) == 0)
 			goto spcr_unmap;
 		if (spcr->InterfaceType != ACPI_DBG2_16550_COMPATIBLE &&
 		    spcr->InterfaceType != ACPI_DBG2_16550_SUBSET)
@@ -300,7 +300,7 @@ acpi_platform_device_register(device_t s
 			if (mem == NULL)
 				goto crs_cleanup;
 
-			if (mem->ar_base == spcr->SerialPort.Address)
+			if (mem->ar_base == le64toh(spcr->SerialPort.Address))
 				prop_dictionary_set_bool(prop, "force_console", true);
 
 crs_cleanup:

Index: src/sys/arch/arm/acpi/acpi_table.c
diff -u src/sys/arch/arm/acpi/acpi_table.c:1.1 src/sys/arch/arm/acpi/acpi_table.c:1.2
--- src/sys/arch/arm/acpi/acpi_table.c:1.1	Fri Oct 12 22:15:04 2018
+++ src/sys/arch/arm/acpi/acpi_table.c	Sun Sep 13 21:41:17 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: acpi_table.c,v 1.1 2018/10/12 22:15:04 jmcneill Exp $ */
+/* $NetBSD: acpi_table.c,v 1.2 2020/09/13 21:41:17 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_table.c,v 1.1 2018/10/12 22:15:04 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_table.c,v 1.2 2020/09/13 21:41:17 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -61,7 +61,8 @@ acpi_table_map(ACPI_PHYSICAL_ADDRESS pa,
 	rv = acpi_md_OsMapMemory(pa, sizeof(*header), (void **)&header);
 	if (ACPI_FAILURE(rv))
 		return rv;
-	length = header->Length;
+	length = le32toh(header->Length);
+
 	acpi_md_OsUnmapMemory(header, sizeof(*header));
 
 	return acpi_md_OsMapMemory(pa, length, hdrp);
@@ -70,7 +71,7 @@ acpi_table_map(ACPI_PHYSICAL_ADDRESS pa,
 void
 acpi_table_unmap(ACPI_TABLE_HEADER *hdrp)
 {
-	acpi_md_OsUnmapMemory(hdrp, hdrp->Length);
+	acpi_md_OsUnmapMemory(hdrp, le32toh(hdrp->Length));
 }
 
 ACPI_STATUS
@@ -88,7 +89,7 @@ acpi_table_find(const char *sig, void **
 	if (ACPI_FAILURE(rv))
 		return rv;
 	if (memcmp(rsdp->Signature, ACPI_SIG_RSDP, sizeof(rsdp->Signature)) == 0)
-		pa = rsdp->XsdtPhysicalAddress;
+		pa = le64toh(rsdp->XsdtPhysicalAddress);
 	acpi_md_OsUnmapMemory(rsdp, sizeof(*rsdp));
 	if (pa == 0)
 		return AE_NOT_FOUND;
@@ -97,13 +98,14 @@ acpi_table_find(const char *sig, void **
 	rv = acpi_table_map(pa, (void **)&xsdt);
 	if (ACPI_FAILURE(rv))
 		return rv;
-	const u_int entries = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) / ACPI_XSDT_ENTRY_SIZE;
+	const u_int entries = (le32toh(xsdt->Header.Length) - sizeof(ACPI_TABLE_HEADER)) / ACPI_XSDT_ENTRY_SIZE;
 	for (u_int n = 0; n < entries; n++) {
-		rv = acpi_table_map(xsdt->TableOffsetEntry[n], (void **)&header);
+		rv = acpi_table_map(le64toh(xsdt->TableOffsetEntry[n]), (void **)&header);
 		if (ACPI_FAILURE(rv))
 			continue;
 		if (memcmp(header->Signature, sig, sizeof(header->Signature)) == 0) {
 			acpi_table_unmap((ACPI_TABLE_HEADER *)xsdt);
+
 			*hdrp = header;
 			return AE_OK;
 		}

Reply via email to