summaryrefslogtreecommitdiff
path: root/Silicon/AMD/Styx/AcpiTables/Dsdt.c
blob: 360a446f7631afa4912fdde750e3f190515d10b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/** @file

  C language wrapper to build DSDT generated data.

  Copyright (c) 2014 - 2016, AMD Inc. All rights reserved.<BR>

  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <AmdStyxAcpiLib.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/PcdLib.h>

#include <Dsdt.hex>
#include <Dsdt.offset.h>


UINTN
ShiftLeftByteToUlong (
  IN UINT8 Byte,
  IN UINTN Shift
  )
{
  UINTN Data;

  Data = (UINTN)Byte;
  Data <<= Shift;
  return Data;
}

UINTN
AmlGetPkgLength (
  IN UINT8 *Buffer,
  OUT UINTN *PkgLength
  )
{
  UINTN Bytes, Length;

  Bytes = (UINTN)((Buffer[0] >> 6) & 0x3) + 1;
  switch (Bytes) {
    case 1:
      Length = (UINTN)Buffer[0];
      break;

    case 2:
      Length = ShiftLeftByteToUlong(Buffer[1], 4) +
                (UINTN)(Buffer[0] & 0x0F);
      break;

    case 3:
      Length = ShiftLeftByteToUlong(Buffer[2], 12) +
                ShiftLeftByteToUlong(Buffer[1], 4) +
                 (UINTN)(Buffer[0] & 0x0F);
      break;

    default: /* 4 bytes */
      Length = ShiftLeftByteToUlong(Buffer[3], 20) +
                ShiftLeftByteToUlong(Buffer[2], 12) +
                 ShiftLeftByteToUlong(Buffer[1], 4) +
                  (UINTN)(Buffer[0] & 0x0F);
      break;
  }

  *PkgLength = Length;
  return Bytes;
}

UINT8 *
AmlSearchStringPackage (
  IN UINT8 *Buffer,
  IN UINTN Length,
  IN CHAR8 *String
  )
{
  UINTN StrLength;

  StrLength = AsciiStrLen (String) + 1;
  if (Length > StrLength ) {
    Length -= StrLength;
    while (AsciiStrCmp((CHAR8 *)Buffer, String) != 0 && Length) {
      --Length;
      ++Buffer;
    }
    if (Length) {
      return &Buffer[StrLength];
    }
  }
  return NULL;
}

VOID
OverrideMacAddr (
  IN UINT8 *DSD_Data,
  IN UINT64 MacAddr
  )
{
  UINT8 *MacAddrPkg;
  UINTN Bytes, Length, Index = 0;

  // AML encoding: PackageOp
  if (DSD_Data[0] == 0x12) {
    // AML encoding: PkgLength
    Bytes = AmlGetPkgLength (&DSD_Data[1], &Length);

    // Search for "mac-address" property
    MacAddrPkg = AmlSearchStringPackage (&DSD_Data[Bytes + 1],
                                        Length - Bytes,
                                        "mac-address");
    if (MacAddrPkg &&
        MacAddrPkg[0] == 0x12 &&        // PackageOp
        MacAddrPkg[1] == 0x0E &&        // PkgLength
        MacAddrPkg[2] == 0x06) {        // NumElements (element must have a BytePrefix)

      MacAddrPkg += 3;
      do {
        MacAddrPkg[0] = 0x0A;           // BytePrefix
        MacAddrPkg[1] = (UINT8)(MacAddr & 0xFF);
        MacAddrPkg += 2;
        MacAddr >>= 8;
      } while (++Index < 6);
    }
  }
}

VOID
OverrideStatus (
  IN UINT8 *DSD_Data,
  IN BOOLEAN Enable
  )
{
  if (Enable) {
    // AML encoding: ReturnOp + BytePrefix
    if (DSD_Data[1] == 0xA4 && DSD_Data[2] == 0x0A) {
      DSD_Data[3] = 0x0F;
    }
  } else {
    // AML encoding: ReturnOp
    if (DSD_Data[1] == 0xA4) {
      // AML encoding: BytePrefix?
      if (DSD_Data[2] == 0x0A) {
        DSD_Data[3] = 0x00;
      } else {
        DSD_Data[2] = 0x00;
      }
    }
  }
}

EFI_ACPI_DESCRIPTION_HEADER *
DsdtHeader (
  VOID
  )
{
  AML_OFFSET_TABLE_ENTRY *Table;
  BOOLEAN                EnableOnB1;
  UINT32                 CpuId = PcdGet32 (PcdSocCpuId);

  // Enable features on Styx-B1 or later
  EnableOnB1 = (CpuId & 0xFF0) && (CpuId & 0x00F);

  Table = &DSDT_SEATTLE__OffsetTable[0];
  while (Table->Pathname) {
    if (AsciiStrCmp(Table->Pathname, "_SB_.ETH0._DSD") == 0) {
      OverrideMacAddr ((UINT8 *)&AmlCode[Table->Offset], PcdGet64 (PcdEthMacA));
    }
    else if (AsciiStrCmp(Table->Pathname, "_SB_.ETH1._DSD") == 0) {
      OverrideMacAddr ((UINT8 *)&AmlCode[Table->Offset], PcdGet64 (PcdEthMacB));
    }
    else if (AsciiStrCmp(Table->Pathname, "_SB_.AHC1._STA") == 0) {
      OverrideStatus ((UINT8 *)&AmlCode[Table->Offset],
        EnableOnB1 && FixedPcdGet8(PcdSata1PortCount) > 0);
    }
    else if (AsciiStrCmp(Table->Pathname, "_SB_.GIO2._STA") == 0) {
      OverrideStatus ((UINT8 *)&AmlCode[Table->Offset], EnableOnB1);
    }
    else if (AsciiStrCmp(Table->Pathname, "_SB_.GIO3._STA") == 0) {
      OverrideStatus ((UINT8 *)&AmlCode[Table->Offset], EnableOnB1);
    }

    ++Table;
  }

  return (EFI_ACPI_DESCRIPTION_HEADER *) &AmlCode[0];
}