]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Pptt/PpttParser.c
ShellPkg/UefiShellAcpiViewCommandLib: Fix PPTT cache attributes validation
[mirror_edk2.git] / ShellPkg / Library / UefiShellAcpiViewCommandLib / Parsers / Pptt / PpttParser.c
1 /** @file
2 PPTT table parser
3
4 Copyright (c) 2019, ARM Limited. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 @par Reference(s):
8 - ACPI 6.2 Specification - Errata A, September 2017
9 **/
10
11 #include <Library/PrintLib.h>
12 #include <Library/UefiLib.h>
13 #include "AcpiParser.h"
14
15 // Local variables
16 STATIC CONST UINT8* ProcessorTopologyStructureType;
17 STATIC CONST UINT8* ProcessorTopologyStructureLength;
18 STATIC CONST UINT32* NumberOfPrivateResources;
19 STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;
20
21 /**
22 An ACPI_PARSER array describing the ACPI PPTT Table.
23 **/
24 STATIC CONST ACPI_PARSER PpttParser[] = {
25 PARSE_ACPI_HEADER (&AcpiHdrInfo)
26 };
27
28 /**
29 This function validates the Cache Type Structure (Type 1) Line size field.
30
31 @param [in] Ptr Pointer to the start of the field data.
32 @param [in] Context Pointer to context specific information e.g. this
33 could be a pointer to the ACPI table header.
34 **/
35 STATIC
36 VOID
37 EFIAPI
38 ValidateCacheLineSize (
39 IN UINT8* Ptr,
40 IN VOID* Context
41 )
42 {
43 #if defined(MDE_CPU_ARM) || defined (MDE_CPU_AARCH64)
44 // Reference: ARM Architecture Reference Manual ARMv8 (D.a)
45 // Section D12.2.25: CCSIDR_EL1, Current Cache Size ID Register
46 // LineSize, bits [2:0]
47 // (Log2(Number of bytes in cache line)) - 4.
48
49 UINT16 LineSize;
50 LineSize = *(UINT16*)Ptr;
51
52 if ((LineSize < 16) || (LineSize > 2048)) {
53 IncrementErrorCount ();
54 Print (
55 L"\nERROR: The cache line size must be between 16 and 2048 bytes"
56 L" on ARM Platforms."
57 );
58 return;
59 }
60
61 if ((LineSize & (LineSize - 1)) != 0) {
62 IncrementErrorCount ();
63 Print (L"\nERROR: The cache line size is not a power of 2.");
64 }
65 #endif
66 }
67
68 /**
69 This function validates the Cache Type Structure (Type 1) Attributes field.
70
71 @param [in] Ptr Pointer to the start of the field data.
72 @param [in] Context Pointer to context specific information e.g. this
73 could be a pointer to the ACPI table header.
74 **/
75 STATIC
76 VOID
77 EFIAPI
78 ValidateCacheAttributes (
79 IN UINT8* Ptr,
80 IN VOID* Context
81 )
82 {
83 // Reference: Advanced Configuration and Power Interface (ACPI) Specification
84 // Version 6.2 Errata A, September 2017
85 // Table 5-153: Cache Type Structure
86 UINT8 Attributes;
87 Attributes = *(UINT8*)Ptr;
88
89 if ((Attributes & 0xE0) != 0) {
90 IncrementErrorCount ();
91 Print (
92 L"\nERROR: Attributes bits [7:5] are reserved and must be zero.",
93 Attributes
94 );
95 return;
96 }
97 }
98
99 /**
100 An ACPI_PARSER array describing the processor topology structure header.
101 **/
102 STATIC CONST ACPI_PARSER ProcessorTopologyStructureHeaderParser[] = {
103 {L"Type", 1, 0, NULL, NULL, (VOID**)&ProcessorTopologyStructureType,
104 NULL, NULL},
105 {L"Length", 1, 1, NULL, NULL, (VOID**)&ProcessorTopologyStructureLength,
106 NULL, NULL},
107 {L"Reserved", 2, 2, NULL, NULL, NULL, NULL, NULL}
108 };
109
110 /**
111 An ACPI_PARSER array describing the Processor Hierarchy Node Structure - Type 0.
112 **/
113 STATIC CONST ACPI_PARSER ProcessorHierarchyNodeStructureParser[] = {
114 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
115 {L"Length", 1, 1, L"%d", NULL, NULL, NULL, NULL},
116 {L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL},
117
118 {L"Flags", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
119 {L"Parent", 4, 8, L"0x%x", NULL, NULL, NULL, NULL},
120 {L"ACPI Processor ID", 4, 12, L"0x%x", NULL, NULL, NULL, NULL},
121 {L"Number of private resources", 4, 16, L"%d", NULL,
122 (VOID**)&NumberOfPrivateResources, NULL, NULL}
123 };
124
125 /**
126 An ACPI_PARSER array describing the Cache Type Structure - Type 1.
127 **/
128 STATIC CONST ACPI_PARSER CacheTypeStructureParser[] = {
129 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
130 {L"Length", 1, 1, L"%d", NULL, NULL, NULL, NULL},
131 {L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL},
132
133 {L"Flags", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
134 {L"Next Level of Cache", 4, 8, L"0x%x", NULL, NULL, NULL, NULL},
135 {L"Size", 4, 12, L"0x%x", NULL, NULL, NULL, NULL},
136 {L"Number of sets", 4, 16, L"%d", NULL, NULL, NULL, NULL},
137 {L"Associativity", 1, 20, L"%d", NULL, NULL, NULL, NULL},
138 {L"Attributes", 1, 21, L"0x%x", NULL, NULL, ValidateCacheAttributes, NULL},
139 {L"Line size", 2, 22, L"%d", NULL, NULL, ValidateCacheLineSize, NULL}
140 };
141
142 /**
143 An ACPI_PARSER array describing the ID Type Structure - Type 2.
144 **/
145 STATIC CONST ACPI_PARSER IdStructureParser[] = {
146 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
147 {L"Length", 1, 1, L"%d", NULL, NULL, NULL, NULL},
148 {L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL},
149
150 {L"VENDOR_ID", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
151 {L"LEVEL_1_ID", 8, 8, L"0x%x", NULL, NULL, NULL, NULL},
152 {L"LEVEL_2_ID", 8, 16, L"0x%x", NULL, NULL, NULL, NULL},
153 {L"MAJOR_REV", 2, 24, L"0x%x", NULL, NULL, NULL, NULL},
154 {L"MINOR_REV", 2, 26, L"0x%x", NULL, NULL, NULL, NULL},
155 {L"SPIN_REV", 2, 28, L"0x%x", NULL, NULL, NULL, NULL},
156 };
157
158 /**
159 This function parses the Processor Hierarchy Node Structure (Type 0).
160
161 @param [in] Ptr Pointer to the start of the Processor Hierarchy Node
162 Structure data.
163 @param [in] Length Length of the Processor Hierarchy Node Structure.
164 **/
165 STATIC
166 VOID
167 DumpProcessorHierarchyNodeStructure (
168 IN UINT8* Ptr,
169 IN UINT8 Length
170 )
171 {
172 UINT32 Offset;
173 UINT8* PrivateResourcePtr;
174 UINT32 Index;
175 CHAR16 Buffer[OUTPUT_FIELD_COLUMN_WIDTH];
176
177 Offset = ParseAcpi (
178 TRUE,
179 2,
180 "Processor Hierarchy Node Structure",
181 Ptr,
182 Length,
183 PARSER_PARAMS (ProcessorHierarchyNodeStructureParser)
184 );
185
186 PrivateResourcePtr = Ptr + Offset;
187 Index = 0;
188 while (Index < *NumberOfPrivateResources) {
189 UnicodeSPrint (
190 Buffer,
191 sizeof (Buffer),
192 L"Private resources [%d]",
193 Index
194 );
195
196 PrintFieldName (4, Buffer);
197 Print (
198 L"0x%x\n",
199 *((UINT32*) PrivateResourcePtr)
200 );
201
202 PrivateResourcePtr += sizeof(UINT32);
203 Index++;
204 }
205 }
206
207 /**
208 This function parses the Cache Type Structure (Type 1).
209
210 @param [in] Ptr Pointer to the start of the Cache Type Structure data.
211 @param [in] Length Length of the Cache Type Structure.
212 **/
213 STATIC
214 VOID
215 DumpCacheTypeStructure (
216 IN UINT8* Ptr,
217 IN UINT8 Length
218 )
219 {
220 ParseAcpi (
221 TRUE,
222 2,
223 "Cache Type Structure",
224 Ptr,
225 Length,
226 PARSER_PARAMS (CacheTypeStructureParser)
227 );
228 }
229
230 /**
231 This function parses the ID Structure (Type 2).
232
233 @param [in] Ptr Pointer to the start of the ID Structure data.
234 @param [in] Length Length of the ID Structure.
235 **/
236 STATIC
237 VOID
238 DumpIDStructure (
239 IN UINT8* Ptr,
240 IN UINT8 Length
241 )
242 {
243 ParseAcpi (
244 TRUE,
245 2,
246 "ID Structure",
247 Ptr,
248 Length,
249 PARSER_PARAMS (IdStructureParser)
250 );
251 }
252
253 /**
254 This function parses the ACPI PPTT table.
255 When trace is enabled this function parses the PPTT table and
256 traces the ACPI table fields.
257
258 This function parses the following processor topology structures:
259 - Processor hierarchy node structure (Type 0)
260 - Cache Type Structure (Type 1)
261 - ID structure (Type 2)
262
263 This function also performs validation of the ACPI table fields.
264
265 @param [in] Trace If TRUE, trace the ACPI fields.
266 @param [in] Ptr Pointer to the start of the buffer.
267 @param [in] AcpiTableLength Length of the ACPI table.
268 @param [in] AcpiTableRevision Revision of the ACPI table.
269 **/
270 VOID
271 EFIAPI
272 ParseAcpiPptt (
273 IN BOOLEAN Trace,
274 IN UINT8* Ptr,
275 IN UINT32 AcpiTableLength,
276 IN UINT8 AcpiTableRevision
277 )
278 {
279 UINT32 Offset;
280 UINT8* ProcessorTopologyStructurePtr;
281
282 if (!Trace) {
283 return;
284 }
285
286 Offset = ParseAcpi (
287 TRUE,
288 0,
289 "PPTT",
290 Ptr,
291 AcpiTableLength,
292 PARSER_PARAMS (PpttParser)
293 );
294 ProcessorTopologyStructurePtr = Ptr + Offset;
295
296 while (Offset < AcpiTableLength) {
297 // Parse Processor Hierarchy Node Structure to obtain Type and Length.
298 ParseAcpi (
299 FALSE,
300 0,
301 NULL,
302 ProcessorTopologyStructurePtr,
303 4, // Length of the processor topology structure header is 4 bytes
304 PARSER_PARAMS (ProcessorTopologyStructureHeaderParser)
305 );
306
307 if ((Offset + (*ProcessorTopologyStructureLength)) > AcpiTableLength) {
308 IncrementErrorCount ();
309 Print (
310 L"ERROR: Invalid processor topology structure length:"
311 L" Type = %d, Length = %d\n",
312 *ProcessorTopologyStructureType,
313 *ProcessorTopologyStructureLength
314 );
315 break;
316 }
317
318 PrintFieldName (2, L"* Structure Offset *");
319 Print (L"0x%x\n", Offset);
320
321 switch (*ProcessorTopologyStructureType) {
322 case EFI_ACPI_6_2_PPTT_TYPE_PROCESSOR:
323 DumpProcessorHierarchyNodeStructure (
324 ProcessorTopologyStructurePtr,
325 *ProcessorTopologyStructureLength
326 );
327 break;
328 case EFI_ACPI_6_2_PPTT_TYPE_CACHE:
329 DumpCacheTypeStructure (
330 ProcessorTopologyStructurePtr,
331 *ProcessorTopologyStructureLength
332 );
333 break;
334 case EFI_ACPI_6_2_PPTT_TYPE_ID:
335 DumpIDStructure (
336 ProcessorTopologyStructurePtr,
337 *ProcessorTopologyStructureLength
338 );
339 break;
340 default:
341 IncrementErrorCount ();
342 Print (
343 L"ERROR: Unknown processor topology structure:"
344 L" Type = %d, Length = %d\n",
345 *ProcessorTopologyStructureType,
346 *ProcessorTopologyStructureLength
347 );
348 }
349
350 ProcessorTopologyStructurePtr += *ProcessorTopologyStructureLength;
351 Offset += *ProcessorTopologyStructureLength;
352 } // while
353 }