edb6c78
From 03425774d2c19874ad4483ec6b7e13c0146e81f7 Mon Sep 17 00:00:00 2001
edb6c78
From: Al Stone <ahs3@redhat.com>
edb6c78
Date: Fri, 25 Sep 2020 16:56:55 -0600
edb6c78
Subject: [PATCH 25/40] Support HMAT in a big-endian world
edb6c78
edb6c78
Signed-off-by: Al Stone <ahs3@redhat.com>
edb6c78
---
edb6c78
 source/common/dmtbdump1.c  | 70 ++++++++++++++++++++++----------------
edb6c78
 source/compiler/dttable1.c | 24 ++++++++++---
edb6c78
 2 files changed, 59 insertions(+), 35 deletions(-)
edb6c78
edb6c78
Index: acpica-unix2-20200925/source/common/dmtbdump1.c
edb6c78
===================================================================
edb6c78
--- acpica-unix2-20200925.orig/source/common/dmtbdump1.c
edb6c78
+++ acpica-unix2-20200925/source/common/dmtbdump1.c
edb6c78
@@ -1301,37 +1301,45 @@ AcpiDmDumpHmat (
edb6c78
     UINT32                  Length;
edb6c78
     ACPI_DMTABLE_INFO       *InfoTable;
edb6c78
     UINT32                  i, j;
edb6c78
+    UINT32                  TableLength = AcpiUtReadUint32(&Table->Length);
edb6c78
+    UINT16                  HmatStructType;
edb6c78
+    UINT32                  HmatStructLength;
edb6c78
+    UINT32                  InitPDs;
edb6c78
+    UINT32                  TgtPDs;
edb6c78
+    UINT16                  SMBIOSHandles;
edb6c78
 
edb6c78
 
edb6c78
     /* Main table */
edb6c78
 
edb6c78
-    Status = AcpiDmDumpTable (Table->Length, 0, Table, 0, AcpiDmTableInfoHmat);
edb6c78
+    Status = AcpiDmDumpTable (TableLength, 0, Table, 0, AcpiDmTableInfoHmat);
edb6c78
     if (ACPI_FAILURE (Status))
edb6c78
     {
edb6c78
         return;
edb6c78
     }
edb6c78
     Offset = sizeof (ACPI_TABLE_HMAT);
edb6c78
 
edb6c78
-    while (Offset < Table->Length)
edb6c78
+    while (Offset < TableLength)
edb6c78
     {
edb6c78
         AcpiOsPrintf ("\n");
edb6c78
 
edb6c78
         /* Dump HMAT structure header */
edb6c78
 
edb6c78
         HmatStruct = ACPI_ADD_PTR (ACPI_HMAT_STRUCTURE, Table, Offset);
edb6c78
-        if (HmatStruct->Length < sizeof (ACPI_HMAT_STRUCTURE))
edb6c78
+	HmatStructLength = AcpiUtReadUint32(&HmatStruct->Length);
edb6c78
+        if (HmatStructLength < sizeof (ACPI_HMAT_STRUCTURE))
edb6c78
         {
edb6c78
             AcpiOsPrintf ("Invalid HMAT structure length\n");
edb6c78
             return;
edb6c78
         }
edb6c78
-        Status = AcpiDmDumpTable (Table->Length, Offset, HmatStruct,
edb6c78
-            HmatStruct->Length, AcpiDmTableInfoHmatHdr);
edb6c78
+        Status = AcpiDmDumpTable (TableLength, Offset, HmatStruct,
edb6c78
+            HmatStructLength, AcpiDmTableInfoHmatHdr);
edb6c78
         if (ACPI_FAILURE (Status))
edb6c78
         {
edb6c78
             return;
edb6c78
         }
edb6c78
 
edb6c78
-        switch (HmatStruct->Type)
edb6c78
+	HmatStructType = AcpiUtReadUint16(&HmatStruct->Type);
edb6c78
+        switch (HmatStructType)
edb6c78
         {
edb6c78
         case ACPI_HMAT_TYPE_ADDRESS_RANGE:
edb6c78
 
edb6c78
@@ -1354,7 +1362,7 @@ AcpiDmDumpHmat (
edb6c78
         default:
edb6c78
 
edb6c78
             AcpiOsPrintf ("\n**** Unknown HMAT structure type 0x%X\n",
edb6c78
-                HmatStruct->Type);
edb6c78
+                HmatStructType);
edb6c78
 
edb6c78
             /* Attempt to continue */
edb6c78
 
edb6c78
@@ -1363,13 +1371,13 @@ AcpiDmDumpHmat (
edb6c78
 
edb6c78
         /* Dump HMAT structure body */
edb6c78
 
edb6c78
-        if (HmatStruct->Length < Length)
edb6c78
+        if (HmatStructLength < Length)
edb6c78
         {
edb6c78
             AcpiOsPrintf ("Invalid HMAT structure length\n");
edb6c78
             return;
edb6c78
         }
edb6c78
-        Status = AcpiDmDumpTable (Table->Length, Offset, HmatStruct,
edb6c78
-            HmatStruct->Length, InfoTable);
edb6c78
+        Status = AcpiDmDumpTable (TableLength, Offset, HmatStruct,
edb6c78
+            HmatStructLength, InfoTable);
edb6c78
         if (ACPI_FAILURE (Status))
edb6c78
         {
edb6c78
             return;
edb6c78
@@ -1377,7 +1385,7 @@ AcpiDmDumpHmat (
edb6c78
 
edb6c78
         /* Dump HMAT structure additionals */
edb6c78
 
edb6c78
-        switch (HmatStruct->Type)
edb6c78
+        switch (HmatStructType)
edb6c78
         {
edb6c78
         case ACPI_HMAT_TYPE_LOCALITY:
edb6c78
 
edb6c78
@@ -1386,15 +1394,16 @@ AcpiDmDumpHmat (
edb6c78
 
edb6c78
             /* Dump initiator proximity domains */
edb6c78
 
edb6c78
-            if ((UINT32)(HmatStruct->Length - SubtableOffset) <
edb6c78
-                (UINT32)(HmatLocality->NumberOfInitiatorPDs * 4))
edb6c78
+	    InitPDs = AcpiUtReadUint32(&HmatLocality->NumberOfInitiatorPDs);
edb6c78
+            if ((UINT32)(HmatStructLength - SubtableOffset) <
edb6c78
+                (UINT32)(InitPDs * 4))
edb6c78
             {
edb6c78
                 AcpiOsPrintf ("Invalid initiator proximity domain number\n");
edb6c78
                 return;
edb6c78
             }
edb6c78
-            for (i = 0; i < HmatLocality->NumberOfInitiatorPDs; i++)
edb6c78
+            for (i = 0; i < InitPDs; i++)
edb6c78
             {
edb6c78
-                Status = AcpiDmDumpTable (Table->Length, Offset + SubtableOffset,
edb6c78
+                Status = AcpiDmDumpTable (TableLength, Offset + SubtableOffset,
edb6c78
                     ACPI_ADD_PTR (ACPI_HMAT_STRUCTURE, HmatStruct, SubtableOffset),
edb6c78
                     4, AcpiDmTableInfoHmat1a);
edb6c78
                 if (ACPI_FAILURE (Status))
edb6c78
@@ -1407,15 +1416,16 @@ AcpiDmDumpHmat (
edb6c78
 
edb6c78
             /* Dump target proximity domains */
edb6c78
 
edb6c78
-            if ((UINT32)(HmatStruct->Length - SubtableOffset) <
edb6c78
-                (UINT32)(HmatLocality->NumberOfTargetPDs * 4))
edb6c78
+	    TgtPDs = AcpiUtReadUint32(&HmatLocality->NumberOfTargetPDs);
edb6c78
+            if ((UINT32)(HmatStructLength - SubtableOffset) <
edb6c78
+                (UINT32)(TgtPDs * 4))
edb6c78
             {
edb6c78
                 AcpiOsPrintf ("Invalid target proximity domain number\n");
edb6c78
                 return;
edb6c78
             }
edb6c78
-            for (i = 0; i < HmatLocality->NumberOfTargetPDs; i++)
edb6c78
+            for (i = 0; i < TgtPDs; i++)
edb6c78
             {
edb6c78
-                Status = AcpiDmDumpTable (Table->Length, Offset + SubtableOffset,
edb6c78
+                Status = AcpiDmDumpTable (TableLength, Offset + SubtableOffset,
edb6c78
                     ACPI_ADD_PTR (ACPI_HMAT_STRUCTURE, HmatStruct, SubtableOffset),
edb6c78
                     4, AcpiDmTableInfoHmat1b);
edb6c78
                 if (ACPI_FAILURE (Status))
edb6c78
@@ -1428,18 +1438,17 @@ AcpiDmDumpHmat (
edb6c78
 
edb6c78
             /* Dump latency/bandwidth entris */
edb6c78
 
edb6c78
-            if ((UINT32)(HmatStruct->Length - SubtableOffset) <
edb6c78
-                (UINT32)(HmatLocality->NumberOfInitiatorPDs *
edb6c78
-                         HmatLocality->NumberOfTargetPDs * 2))
edb6c78
+            if ((UINT32)(HmatStructLength - SubtableOffset) <
edb6c78
+                (UINT32)(InitPDs * TgtPDs * 2))
edb6c78
             {
edb6c78
                 AcpiOsPrintf ("Invalid latency/bandwidth entry number\n");
edb6c78
                 return;
edb6c78
             }
edb6c78
-            for (i = 0; i < HmatLocality->NumberOfInitiatorPDs; i++)
edb6c78
+            for (i = 0; i < InitPDs; i++)
edb6c78
             {
edb6c78
-                for (j = 0; j < HmatLocality->NumberOfTargetPDs; j++)
edb6c78
+                for (j = 0; j < TgtPDs; j++)
edb6c78
                 {
edb6c78
-                    Status = AcpiDmDumpTable (Table->Length, Offset + SubtableOffset,
edb6c78
+                    Status = AcpiDmDumpTable (TableLength, Offset + SubtableOffset,
edb6c78
                         ACPI_ADD_PTR (ACPI_HMAT_STRUCTURE, HmatStruct, SubtableOffset),
edb6c78
                         2, AcpiDmTableInfoHmat1c);
edb6c78
                     if (ACPI_FAILURE(Status))
edb6c78
@@ -1459,15 +1468,16 @@ AcpiDmDumpHmat (
edb6c78
 
edb6c78
             /* Dump SMBIOS handles */
edb6c78
 
edb6c78
-            if ((UINT32)(HmatStruct->Length - SubtableOffset) <
edb6c78
-                (UINT32)(HmatCache->NumberOfSMBIOSHandles * 2))
edb6c78
+	    SMBIOSHandles = AcpiUtReadUint16(&HmatCache->NumberOfSMBIOSHandles);
edb6c78
+            if ((UINT32)(HmatStructLength - SubtableOffset) <
edb6c78
+                (UINT32)(SMBIOSHandles * 2))
edb6c78
             {
edb6c78
                 AcpiOsPrintf ("Invalid SMBIOS handle number\n");
edb6c78
                 return;
edb6c78
             }
edb6c78
-            for (i = 0; i < HmatCache->NumberOfSMBIOSHandles; i++)
edb6c78
+            for (i = 0; i < SMBIOSHandles; i++)
edb6c78
             {
edb6c78
-                Status = AcpiDmDumpTable (Table->Length, Offset + SubtableOffset,
edb6c78
+                Status = AcpiDmDumpTable (TableLength, Offset + SubtableOffset,
edb6c78
                     ACPI_ADD_PTR (ACPI_HMAT_STRUCTURE, HmatStruct, SubtableOffset),
edb6c78
                     2, AcpiDmTableInfoHmat2a);
edb6c78
                 if (ACPI_FAILURE (Status))
edb6c78
@@ -1487,6 +1497,6 @@ AcpiDmDumpHmat (
edb6c78
 NextSubtable:
edb6c78
         /* Point to next HMAT structure subtable */
edb6c78
 
edb6c78
-        Offset += (HmatStruct->Length);
edb6c78
+        Offset += (HmatStructLength);
edb6c78
     }
edb6c78
 }
edb6c78
Index: acpica-unix2-20200925/source/compiler/dttable1.c
edb6c78
===================================================================
edb6c78
--- acpica-unix2-20200925.orig/source/compiler/dttable1.c
edb6c78
+++ acpica-unix2-20200925/source/compiler/dttable1.c
edb6c78
@@ -1295,6 +1295,8 @@ DtCompileHmat (
edb6c78
     UINT32                  TgtPDNumber;
edb6c78
     UINT64                  EntryNumber;
edb6c78
     UINT16                  SMBIOSHandleNumber;
edb6c78
+    UINT16                  HmatStructType;
edb6c78
+    UINT32                  Length;
edb6c78
 
edb6c78
 
edb6c78
     ParentTable = DtPeekSubtable ();
edb6c78
@@ -1325,7 +1327,8 @@ DtCompileHmat (
edb6c78
 
edb6c78
         /* Compile HMAT structure body */
edb6c78
 
edb6c78
-        switch (HmatStruct->Type)
edb6c78
+	HmatStructType = AcpiUtReadUint16(&HmatStruct->Type);
edb6c78
+        switch (HmatStructType)
edb6c78
         {
edb6c78
         case ACPI_HMAT_TYPE_ADDRESS_RANGE:
edb6c78
 
edb6c78
@@ -1358,7 +1361,7 @@ DtCompileHmat (
edb6c78
 
edb6c78
         /* Compile HMAT structure additionals */
edb6c78
 
edb6c78
-        switch (HmatStruct->Type)
edb6c78
+        switch (HmatStructType)
edb6c78
         {
edb6c78
         case ACPI_HMAT_TYPE_LOCALITY:
edb6c78
 
edb6c78
@@ -1384,7 +1387,7 @@ DtCompileHmat (
edb6c78
                 HmatStruct->Length += Subtable->Length;
edb6c78
                 IntPDNumber++;
edb6c78
             }
edb6c78
-            HmatLocality->NumberOfInitiatorPDs = IntPDNumber;
edb6c78
+            HmatLocality->NumberOfInitiatorPDs = AcpiUtReadUint32(&IntPDNumber);
edb6c78
 
edb6c78
             /* Compile target proximity domain list */
edb6c78
 
edb6c78
@@ -1405,7 +1408,7 @@ DtCompileHmat (
edb6c78
                 HmatStruct->Length += Subtable->Length;
edb6c78
                 TgtPDNumber++;
edb6c78
             }
edb6c78
-            HmatLocality->NumberOfTargetPDs = TgtPDNumber;
edb6c78
+            HmatLocality->NumberOfTargetPDs = AcpiUtReadUint32(&TgtPDNumber);
edb6c78
 
edb6c78
             /* Save start of the entries for reporting errors */
edb6c78
 
edb6c78
@@ -1430,6 +1433,9 @@ DtCompileHmat (
edb6c78
                 HmatStruct->Length += Subtable->Length;
edb6c78
                 EntryNumber++;
edb6c78
             }
edb6c78
+	    
edb6c78
+	    Length = AcpiUtReadUint32(&HmatStruct->Length);
edb6c78
+	    HmatStruct->Length = Length;
edb6c78
 
edb6c78
             /* Validate number of entries */
edb6c78
 
edb6c78
@@ -1464,11 +1470,19 @@ DtCompileHmat (
edb6c78
                 HmatStruct->Length += Subtable->Length;
edb6c78
                 SMBIOSHandleNumber++;
edb6c78
             }
edb6c78
-            HmatCache->NumberOfSMBIOSHandles = SMBIOSHandleNumber;
edb6c78
+            HmatCache->NumberOfSMBIOSHandles = 
edb6c78
+		    AcpiUtReadUint16(&SMBIOSHandleNumber);
edb6c78
+
edb6c78
+	    Length = AcpiUtReadUint32(&HmatStruct->Length);
edb6c78
+	    HmatStruct->Length = Length;
edb6c78
+
edb6c78
             break;
edb6c78
 
edb6c78
         default:
edb6c78
 
edb6c78
+	    Length = AcpiUtReadUint32(&HmatStruct->Length);
edb6c78
+	    HmatStruct->Length = Length;
edb6c78
+
edb6c78
             break;
edb6c78
         }
edb6c78
     }