2 Intel Processor Trace feature.
4 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "CpuCommonFeatures.h"
18 /// This macro define the max entries in the Topa table.
19 /// Each entry in the table contains some attribute bits, a pointer to an output region, and the size of the region.
20 /// The last entry in the table may hold a pointer to the next table. This pointer can either point to the top of the
21 /// current table (for circular array) or to the base of another table.
22 /// At least 2 entries are needed because the list of entries must
23 /// be terminated by an entry with the END bit set to 1, so 2
24 /// entries are required to use a single valid entry.
26 #define MAX_TOPA_ENTRY_COUNT 2
30 /// Processor trace output scheme selection.
33 RtitOutputSchemeSingleRange
= 0,
38 BOOLEAN ProcTraceSupported
;
39 BOOLEAN TopaSupported
;
40 BOOLEAN SingleRangeSupported
;
41 } PROC_TRACE_PROCESSOR_DATA
;
44 UINT32 NumberOfProcessors
;
46 UINT8 ProcTraceOutputScheme
;
47 UINT32 ProcTraceMemSize
;
49 UINTN
*ThreadMemRegionTable
;
50 UINTN AllocatedThreads
;
53 UINTN TopaMemArrayCount
;
55 PROC_TRACE_PROCESSOR_DATA
*ProcessorData
;
59 RTIT_TOPA_TABLE_ENTRY TopaEntry
[MAX_TOPA_ENTRY_COUNT
];
60 } PROC_TRACE_TOPA_TABLE
;
63 Prepares for the data used by CPU feature detection and initialization.
65 @param[in] NumberOfProcessors The number of CPUs in the platform.
67 @return Pointer to a buffer of CPU related configuration data.
69 @note This service could be called by BSP only.
73 ProcTraceGetConfigData (
74 IN UINTN NumberOfProcessors
77 PROC_TRACE_DATA
*ConfigData
;
79 ConfigData
= AllocateZeroPool (sizeof (PROC_TRACE_DATA
) + sizeof (PROC_TRACE_PROCESSOR_DATA
) * NumberOfProcessors
);
80 ASSERT (ConfigData
!= NULL
);
81 ConfigData
->ProcessorData
= (PROC_TRACE_PROCESSOR_DATA
*) ((UINT8
*) ConfigData
+ sizeof (PROC_TRACE_DATA
));
83 ConfigData
->NumberOfProcessors
= (UINT32
) NumberOfProcessors
;
84 ConfigData
->ProcTraceMemSize
= PcdGet32 (PcdCpuProcTraceMemSize
);
85 ConfigData
->ProcTraceOutputScheme
= PcdGet8 (PcdCpuProcTraceOutputScheme
);
91 Detects if Intel Processor Trace feature supported on current
94 @param[in] ProcessorNumber The index of the CPU executing this function.
95 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
96 structure for the CPU executing this function.
97 @param[in] ConfigData A pointer to the configuration buffer returned
98 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
99 CPU_FEATURE_GET_CONFIG_DATA was not provided in
100 RegisterCpuFeature().
102 @retval TRUE Processor Trace feature is supported.
103 @retval FALSE Processor Trace feature is not supported.
105 @note This service could be called by BSP/APs.
110 IN UINTN ProcessorNumber
,
111 IN REGISTER_CPU_FEATURE_INFORMATION
*CpuInfo
,
112 IN VOID
*ConfigData OPTIONAL
115 PROC_TRACE_DATA
*ProcTraceData
;
116 CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_EBX Ebx
;
117 CPUID_INTEL_PROCESSOR_TRACE_MAIN_LEAF_ECX Ecx
;
120 // Check if ProcTraceMemorySize option is enabled (0xFF means disable by user)
122 ProcTraceData
= (PROC_TRACE_DATA
*) ConfigData
;
123 if ((ProcTraceData
->ProcTraceMemSize
> RtitTopaMemorySize128M
) ||
124 (ProcTraceData
->ProcTraceOutputScheme
> RtitOutputSchemeToPA
)) {
129 // Check if Processor Trace is supported
131 AsmCpuidEx (CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS
, 0, NULL
, &Ebx
.Uint32
, NULL
, NULL
);
132 ProcTraceData
->ProcessorData
[ProcessorNumber
].ProcTraceSupported
= (BOOLEAN
) (Ebx
.Bits
.IntelProcessorTrace
== 1);
133 if (!ProcTraceData
->ProcessorData
[ProcessorNumber
].ProcTraceSupported
) {
137 AsmCpuidEx (CPUID_INTEL_PROCESSOR_TRACE
, CPUID_INTEL_PROCESSOR_TRACE_MAIN_LEAF
, NULL
, NULL
, &Ecx
.Uint32
, NULL
);
138 ProcTraceData
->ProcessorData
[ProcessorNumber
].TopaSupported
= (BOOLEAN
) (Ecx
.Bits
.RTIT
== 1);
139 ProcTraceData
->ProcessorData
[ProcessorNumber
].SingleRangeSupported
= (BOOLEAN
) (Ecx
.Bits
.SingleRangeOutput
== 1);
140 if ((ProcTraceData
->ProcessorData
[ProcessorNumber
].TopaSupported
&& (ProcTraceData
->ProcTraceOutputScheme
== RtitOutputSchemeToPA
)) ||
141 (ProcTraceData
->ProcessorData
[ProcessorNumber
].SingleRangeSupported
&& (ProcTraceData
->ProcTraceOutputScheme
== RtitOutputSchemeSingleRange
))) {
149 Initializes Intel Processor Trace feature to specific state.
151 @param[in] ProcessorNumber The index of the CPU executing this function.
152 @param[in] CpuInfo A pointer to the REGISTER_CPU_FEATURE_INFORMATION
153 structure for the CPU executing this function.
154 @param[in] ConfigData A pointer to the configuration buffer returned
155 by CPU_FEATURE_GET_CONFIG_DATA. NULL if
156 CPU_FEATURE_GET_CONFIG_DATA was not provided in
157 RegisterCpuFeature().
158 @param[in] State If TRUE, then the Processor Trace feature must be
160 If FALSE, then the Processor Trace feature must be
163 @retval RETURN_SUCCESS Intel Processor Trace feature is initialized.
168 ProcTraceInitialize (
169 IN UINTN ProcessorNumber
,
170 IN REGISTER_CPU_FEATURE_INFORMATION
*CpuInfo
,
171 IN VOID
*ConfigData
, OPTIONAL
175 UINT32 MemRegionSize
;
178 UINTN MemRegionBaseAddr
;
179 UINTN
*ThreadMemRegionTable
;
181 UINTN TopaTableBaseAddr
;
182 UINTN AlignedAddress
;
184 PROC_TRACE_TOPA_TABLE
*TopaTable
;
185 PROC_TRACE_DATA
*ProcTraceData
;
187 MSR_IA32_RTIT_CTL_REGISTER CtrlReg
;
188 MSR_IA32_RTIT_STATUS_REGISTER StatusReg
;
189 MSR_IA32_RTIT_OUTPUT_BASE_REGISTER OutputBaseReg
;
190 MSR_IA32_RTIT_OUTPUT_MASK_PTRS_REGISTER OutputMaskPtrsReg
;
191 RTIT_TOPA_TABLE_ENTRY
*TopaEntryPtr
;
193 ProcTraceData
= (PROC_TRACE_DATA
*) ConfigData
;
195 MemRegionBaseAddr
= 0;
198 if (ProcTraceData
->ThreadMemRegionTable
== NULL
) {
200 DEBUG ((DEBUG_INFO
, "Initialize Processor Trace\n"));
204 /// Refer to PROC_TRACE_MEM_SIZE Table for Size Encoding
206 MemRegionSize
= (UINT32
) (1 << (ProcTraceData
->ProcTraceMemSize
+ 12));
208 DEBUG ((DEBUG_INFO
, "ProcTrace: MemSize requested: 0x%X \n", MemRegionSize
));
212 // Clear MSR_IA32_RTIT_CTL[0] and IA32_RTIT_STS only if MSR_IA32_RTIT_CTL[0]==1b
214 CtrlReg
.Uint64
= AsmReadMsr64 (MSR_IA32_RTIT_CTL
);
215 if (CtrlReg
.Bits
.TraceEn
!= 0) {
217 /// Clear bit 0 in MSR IA32_RTIT_CTL (570)
219 CtrlReg
.Bits
.TraceEn
= 0;
220 CPU_REGISTER_TABLE_WRITE64 (
228 /// Clear MSR IA32_RTIT_STS (571h) to all zeros
230 StatusReg
.Uint64
= 0x0;
231 CPU_REGISTER_TABLE_WRITE64 (
234 MSR_IA32_RTIT_STATUS
,
241 // Let BSP allocate and create the necessary memory region (Aligned to the size of
242 // the memory region from setup option(ProcTraceMemSize) which is an integral multiple of 4kB)
243 // for the all the enabled threads for storing Processor Trace debug data. Then Configure the trace
244 // address base in MSR, IA32_RTIT_OUTPUT_BASE (560h) bits 47:12. Note that all regions must be
245 // aligned based on their size, not just 4K. Thus a 2M region must have bits 20:12 clear.
247 ThreadMemRegionTable
= (UINTN
*) AllocatePool (ProcTraceData
->NumberOfProcessors
* sizeof (UINTN
*));
248 if (ThreadMemRegionTable
== NULL
) {
249 DEBUG ((DEBUG_ERROR
, "Allocate ProcTrace ThreadMemRegionTable Failed\n"));
250 return RETURN_OUT_OF_RESOURCES
;
252 ProcTraceData
->ThreadMemRegionTable
= ThreadMemRegionTable
;
254 for (Index
= 0; Index
< ProcTraceData
->NumberOfProcessors
; Index
++, ProcTraceData
->AllocatedThreads
++) {
255 Pages
= EFI_SIZE_TO_PAGES (MemRegionSize
);
256 Alignment
= MemRegionSize
;
257 AlignedAddress
= (UINTN
) AllocateAlignedReservedPages (Pages
, Alignment
);
258 if (AlignedAddress
== 0) {
259 DEBUG ((DEBUG_ERROR
, "ProcTrace: Out of mem, allocated only for %d threads\n", ProcTraceData
->AllocatedThreads
));
262 // Could not allocate for BSP even
264 FreePool ((VOID
*) ThreadMemRegionTable
);
265 ThreadMemRegionTable
= NULL
;
266 return RETURN_OUT_OF_RESOURCES
;
271 ThreadMemRegionTable
[Index
] = AlignedAddress
;
272 DEBUG ((DEBUG_INFO
, "ProcTrace: PT MemRegionBaseAddr(aligned) for thread %d: 0x%llX \n", Index
, (UINT64
) ThreadMemRegionTable
[Index
]));
275 DEBUG ((DEBUG_INFO
, "ProcTrace: Allocated PT mem for %d thread \n", ProcTraceData
->AllocatedThreads
));
276 MemRegionBaseAddr
= ThreadMemRegionTable
[0];
278 if (ProcessorNumber
< ProcTraceData
->AllocatedThreads
) {
279 MemRegionBaseAddr
= ProcTraceData
->ThreadMemRegionTable
[ProcessorNumber
];
281 return RETURN_SUCCESS
;
286 /// Check Processor Trace output scheme: Single Range output or ToPA table
290 // Single Range output scheme
292 if (ProcTraceData
->ProcessorData
[ProcessorNumber
].SingleRangeSupported
&&
293 (ProcTraceData
->ProcTraceOutputScheme
== RtitOutputSchemeSingleRange
)) {
295 DEBUG ((DEBUG_INFO
, "ProcTrace: Enabling Single Range Output scheme \n"));
299 // Clear MSR IA32_RTIT_CTL (0x570) ToPA (Bit 8)
301 CtrlReg
.Uint64
= AsmReadMsr64 (MSR_IA32_RTIT_CTL
);
302 CtrlReg
.Bits
.ToPA
= 0;
303 CPU_REGISTER_TABLE_WRITE64 (
311 // Program MSR IA32_RTIT_OUTPUT_BASE (0x560) bits[63:7] with the allocated Memory Region
313 OutputBaseReg
.Bits
.Base
= (MemRegionBaseAddr
>> 7) & 0x01FFFFFF;
314 OutputBaseReg
.Bits
.BaseHi
= RShiftU64 ((UINT64
) MemRegionBaseAddr
, 32) & 0xFFFFFFFF;
315 CPU_REGISTER_TABLE_WRITE64 (
318 MSR_IA32_RTIT_OUTPUT_BASE
,
323 // Program the Mask bits for the Memory Region to MSR IA32_RTIT_OUTPUT_MASK_PTRS (561h)
325 OutputMaskPtrsReg
.Bits
.MaskOrTableOffset
= ((MemRegionSize
- 1) >> 7) & 0x01FFFFFF;
326 OutputMaskPtrsReg
.Bits
.OutputOffset
= RShiftU64 ((UINT64
) (MemRegionSize
- 1), 32) & 0xFFFFFFFF;
327 CPU_REGISTER_TABLE_WRITE64 (
330 MSR_IA32_RTIT_OUTPUT_MASK_PTRS
,
331 OutputMaskPtrsReg
.Uint64
336 // ToPA(Table of physical address) scheme
338 if (ProcTraceData
->ProcessorData
[ProcessorNumber
].TopaSupported
&&
339 (ProcTraceData
->ProcTraceOutputScheme
== RtitOutputSchemeToPA
)) {
341 // Create ToPA structure aligned at 4KB for each logical thread
342 // with at least 2 entries by 8 bytes size each. The first entry
343 // should have the trace output base address in bits 47:12, 6:9
344 // for Size, bits 4,2 and 0 must be cleared. The second entry
345 // should have the base address of the table location in bits
346 // 47:12, bits 4 and 2 must be cleared and bit 0 must be set.
349 DEBUG ((DEBUG_INFO
, "ProcTrace: Enabling ToPA scheme \n"));
351 // Let BSP allocate ToPA table mem for all threads
353 TopaMemArray
= (UINTN
*) AllocatePool (ProcTraceData
->AllocatedThreads
* sizeof (UINTN
*));
354 if (TopaMemArray
== NULL
) {
355 DEBUG ((DEBUG_ERROR
, "ProcTrace: Allocate mem for ToPA Failed\n"));
356 return RETURN_OUT_OF_RESOURCES
;
358 ProcTraceData
->TopaMemArray
= TopaMemArray
;
360 for (Index
= 0; Index
< ProcTraceData
->AllocatedThreads
; Index
++) {
361 Pages
= EFI_SIZE_TO_PAGES (sizeof (PROC_TRACE_TOPA_TABLE
));
363 AlignedAddress
= (UINTN
) AllocateAlignedReservedPages (Pages
, Alignment
);
364 if (AlignedAddress
== 0) {
365 if (Index
< ProcTraceData
->AllocatedThreads
) {
366 ProcTraceData
->AllocatedThreads
= Index
;
368 DEBUG ((DEBUG_ERROR
, "ProcTrace: Out of mem, allocating ToPA mem only for %d threads\n", ProcTraceData
->AllocatedThreads
));
371 // Could not allocate for BSP
373 FreePool ((VOID
*) TopaMemArray
);
375 return RETURN_OUT_OF_RESOURCES
;
380 TopaMemArray
[Index
] = AlignedAddress
;
381 DEBUG ((DEBUG_INFO
, "ProcTrace: Topa table address(aligned) for thread %d is 0x%llX \n", Index
, (UINT64
) TopaMemArray
[Index
]));
384 DEBUG ((DEBUG_INFO
, "ProcTrace: Allocated ToPA mem for %d thread \n", ProcTraceData
->AllocatedThreads
));
386 // BSP gets the first block
388 TopaTableBaseAddr
= TopaMemArray
[0];
391 // Count for currently executing AP.
393 if (ProcessorNumber
< ProcTraceData
->AllocatedThreads
) {
394 TopaTableBaseAddr
= ProcTraceData
->TopaMemArray
[ProcessorNumber
];
396 return RETURN_SUCCESS
;
400 TopaTable
= (PROC_TRACE_TOPA_TABLE
*) TopaTableBaseAddr
;
401 TopaEntryPtr
= &TopaTable
->TopaEntry
[0];
402 TopaEntryPtr
->Bits
.Base
= (MemRegionBaseAddr
>> 12) & 0x000FFFFF;
403 TopaEntryPtr
->Bits
.BaseHi
= RShiftU64 ((UINT64
) MemRegionBaseAddr
, 32) & 0xFFFFFFFF;
404 TopaEntryPtr
->Bits
.Size
= ProcTraceData
->ProcTraceMemSize
;
405 TopaEntryPtr
->Bits
.END
= 0;
407 TopaEntryPtr
= &TopaTable
->TopaEntry
[1];
408 TopaEntryPtr
->Bits
.Base
= (TopaTableBaseAddr
>> 12) & 0x000FFFFF;
409 TopaEntryPtr
->Bits
.BaseHi
= RShiftU64 ((UINT64
) TopaTableBaseAddr
, 32) & 0xFFFFFFFF;
410 TopaEntryPtr
->Bits
.END
= 1;
413 // Program the MSR IA32_RTIT_OUTPUT_BASE (0x560) bits[63:7] with ToPA base
415 OutputBaseReg
.Bits
.Base
= (TopaTableBaseAddr
>> 7) & 0x01FFFFFF;
416 OutputBaseReg
.Bits
.BaseHi
= RShiftU64 ((UINT64
) TopaTableBaseAddr
, 32) & 0xFFFFFFFF;
417 CPU_REGISTER_TABLE_WRITE64 (
420 MSR_IA32_RTIT_OUTPUT_BASE
,
425 // Set the MSR IA32_RTIT_OUTPUT_MASK (0x561) bits[63:7] to 0
427 OutputMaskPtrsReg
.Bits
.MaskOrTableOffset
= 0;
428 OutputMaskPtrsReg
.Bits
.OutputOffset
= 0;
429 CPU_REGISTER_TABLE_WRITE64 (
432 MSR_IA32_RTIT_OUTPUT_MASK_PTRS
,
433 OutputMaskPtrsReg
.Uint64
436 // Enable ToPA output scheme by enabling MSR IA32_RTIT_CTL (0x570) ToPA (Bit 8)
438 CtrlReg
.Uint64
= AsmReadMsr64 (MSR_IA32_RTIT_CTL
);
439 CtrlReg
.Bits
.ToPA
= 1;
440 CPU_REGISTER_TABLE_WRITE64 (
449 /// Enable the Processor Trace feature from MSR IA32_RTIT_CTL (570h)
451 CtrlReg
.Uint64
= AsmReadMsr64 (MSR_IA32_RTIT_CTL
);
453 CtrlReg
.Bits
.User
= 1;
454 CtrlReg
.Bits
.BranchEn
= 1;
456 CtrlReg
.Bits
.TraceEn
= 0;
458 CtrlReg
.Bits
.TraceEn
= 1;
460 CPU_REGISTER_TABLE_WRITE64 (
467 return RETURN_SUCCESS
;