AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / CPU / cpuEventLog.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * AMD Event (Error) Log APIs, and related functions.
6  *
7  * Contains code that records and returns the events and errors.
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project:      AGESA
11  * @e sub-project:  CPU
12  * @e \$Revision: 56279 $   @e \$Date: 2011-07-11 13:11:28 -0600 (Mon, 11 Jul 2011) $
13  *
14  */
15 /*****************************************************************************
16  *
17  * Copyright (C) 2012 Advanced Micro Devices, Inc.
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions are met:
22  *     * Redistributions of source code must retain the above copyright
23  *       notice, this list of conditions and the following disclaimer.
24  *     * Redistributions in binary form must reproduce the above copyright
25  *       notice, this list of conditions and the following disclaimer in the
26  *       documentation and/or other materials provided with the distribution.
27  *     * Neither the name of Advanced Micro Devices, Inc. nor the names of
28  *       its contributors may be used to endorse or promote products derived
29  *       from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34  * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  *
42  ******************************************************************************
43  */
44
45 /*----------------------------------------------------------------------------------------
46  *                             M O D U L E S    U S E D
47  *----------------------------------------------------------------------------------------
48  */
49
50 #include "AGESA.h"
51 #include "amdlib.h"
52 #include "heapManager.h"
53 #include "GeneralServices.h"
54 #include "Ids.h"
55 #include "Filecode.h"
56 CODE_GROUP (G1_PEICC)
57 RDATA_GROUP (G2_PEI)
58
59 #define FILECODE PROC_CPU_CPUEVENTLOG_FILECODE
60 /*----------------------------------------------------------------------------------------
61  *                   D E F I N I T I O N S    A N D    M A C R O S
62  *----------------------------------------------------------------------------------------
63  */
64 #define TOTAL_EVENT_LOG_BUFFERS  16
65
66 /*----------------------------------------------------------------------------------------
67  *                  T Y P E D E F S     A N D     S T R U C T U  R E S
68  *----------------------------------------------------------------------------------------
69  */
70
71 /**
72  * A wrapper for each Event Log entry.
73  */
74 typedef struct {
75   UINT16      Count;         ///< Entry number
76   AGESA_EVENT AgesaEvent;    ///< The entry itself.
77 } AGESA_EVENT_STRUCT;
78
79 /**
80  * The Event Log.
81  */
82 typedef struct {
83   UINT16        ReadWriteFlag;   ///< Read Write flag.
84   UINT16        Count;           ///< The total number of active entries.
85   UINT16        ReadRecordPtr;   ///< The next entry to read.
86   UINT16        WriteRecordPtr;  ///< The next entry to write.
87   AGESA_EVENT_STRUCT AgesaEventStruct[TOTAL_EVENT_LOG_BUFFERS];   ///< The entries.
88 } AGESA_STRUCT_BUFFER;
89
90 /*----------------------------------------------------------------------------------------
91  *           P R O T O T Y P E S     O F     L O C A L     F U  N C T I O N S
92  *----------------------------------------------------------------------------------------
93  */
94 VOID
95 STATIC
96 GetEventLogHeapPointer (
97      OUT  AGESA_STRUCT_BUFFER **EventLog,
98   IN      AMD_CONFIG_PARAMS *StdHeader
99   );
100
101 /*---------------------------------------------------------------------------------------*/
102 /**
103  * External AGESA interface to read an Event from the Event Log.
104  *
105  * This is the implementation of the external AGESA interface entry, as a thin wrapper
106  * around the internal log services.
107  *
108  * @param[in]  Event           The event class, id, and any associated data.
109  *
110  * @retval     AGESA_SUCCESS   Always Succeeds.
111  */
112 AGESA_STATUS
113 AmdReadEventLog (
114   IN       EVENT_PARAMS *Event
115   )
116 {
117   AGESA_EVENT  LogEvent;
118   AGESA_STATUS Status;
119
120   AGESA_TESTPOINT (TpIfAmdReadEventLogEntry, &Event->StdHeader);
121
122   ASSERT (Event != NULL);
123   Event->StdHeader.HeapBasePtr = HeapGetBaseAddress (&Event->StdHeader);
124   Status = GetEventLog (&LogEvent, &Event->StdHeader);
125
126   Event->EventClass = LogEvent.EventClass;
127   Event->EventInfo = LogEvent.EventInfo;
128   Event->DataParam1 = LogEvent.DataParam1;
129   Event->DataParam2 = LogEvent.DataParam2;
130   Event->DataParam3 = LogEvent.DataParam3;
131   Event->DataParam4 = LogEvent.DataParam4;
132
133   AGESA_TESTPOINT (TpIfAmdReadEventLogExit, &Event->StdHeader);
134   return Status;
135 }
136
137
138 /*---------------------------------------------------------------------------------------*/
139 /**
140  *
141  * This function prepares the Event Log for use.
142  *
143  * Allocate the memory for an event log on the heap.  Set the read pointer, write pointer,
144  * and count to reflect the log is empty.
145  *
146  * @param[in]  StdHeader      Our configuration, for passing to services.
147  *
148  * @retval      AGESA_SUCCESS     The event log is initialized.
149  * @retval      AGESA_ERROR       Allocate Heap Buffer returned an error.
150  *
151  */
152 AGESA_STATUS
153 EventLogInitialization (
154   IN       AMD_CONFIG_PARAMS *StdHeader
155   )
156 {
157   ALLOCATE_HEAP_PARAMS  AllocateHeapParams;
158   AGESA_STRUCT_BUFFER   *AgesaEventAlloc;
159   AGESA_STATUS          Status;
160
161   AllocateHeapParams.BufferHandle = EVENT_LOG_BUFFER_HANDLE;
162   AllocateHeapParams.RequestedBufferSize = sizeof (AGESA_STRUCT_BUFFER);
163   AllocateHeapParams.Persist = HEAP_SYSTEM_MEM;
164   Status = HeapAllocateBuffer (&AllocateHeapParams, StdHeader);
165   AgesaEventAlloc = (AGESA_STRUCT_BUFFER *) AllocateHeapParams.BufferPtr;
166   AgesaEventAlloc->Count = 0;
167   AgesaEventAlloc->ReadRecordPtr = 0;
168   AgesaEventAlloc->WriteRecordPtr = 0;
169   AgesaEventAlloc->ReadWriteFlag = 1;
170
171   return Status;
172 }
173
174
175 /*---------------------------------------------------------------------------------------*/
176 /**
177  *
178  * This function logs AGESA events into the event log.
179  *
180  * It will put the information in a circular buffer consisting of 16 such log
181  * entries. If the buffer gets full, then the next event log entry will be written
182  * over the oldest event log entry.
183  *
184  * @param[in]   EventClass   The severity of the event, its associated AGESA_STATUS.
185  * @param[in]   EventInfo    Uniquely identifies the event.
186  * @param[in]   DataParam1   Event specific additional data
187  * @param[in]   DataParam2   Event specific additional data
188  * @param[in]   DataParam3   Event specific additional data
189  * @param[in]   DataParam4   Event specific additional data
190  * @param[in]   StdHeader    Header for library and services
191  *
192  */
193 VOID
194 PutEventLog (
195   IN       AGESA_STATUS EventClass,
196   IN       UINT32 EventInfo,
197   IN       UINT32 DataParam1,
198   IN       UINT32 DataParam2,
199   IN       UINT32 DataParam3,
200   IN       UINT32 DataParam4,
201   IN       AMD_CONFIG_PARAMS *StdHeader
202   )
203 {
204   UINT16 Index;
205   AGESA_STRUCT_BUFFER *AgesaEventAlloc;
206
207   IDS_HDT_CONSOLE (MAIN_FLOW, "\n * %s Event: %08x Data: %x, %x, %x, %x\n\n",
208                     (EventClass == AGESA_FATAL)       ? "FATAL"       :
209                     (EventClass == AGESA_CRITICAL)    ? "CRITICAL"    :
210                     (EventClass == AGESA_ERROR)       ? "ERROR"       :
211                     (EventClass == AGESA_WARNING)     ? "WARNING"     :
212                     (EventClass == AGESA_ALERT)       ? "ALERT"       :
213                     (EventClass == AGESA_BOUNDS_CHK)  ? "BOUNDS_CHK"  :
214                     (EventClass == AGESA_UNSUPPORTED) ? "UNSUPPORTED" :
215                     "SUCCESS", EventInfo, DataParam1, DataParam2, DataParam3, DataParam4);
216
217   AgesaEventAlloc = NULL;
218   GetEventLogHeapPointer (&AgesaEventAlloc, StdHeader);
219   ASSERT (AgesaEventAlloc != NULL);
220   Index = AgesaEventAlloc->WriteRecordPtr;
221
222   // Add the new event log data into a circular buffer
223   AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.EventClass = EventClass;
224   AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.EventInfo  = EventInfo;
225   AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.DataParam1 = DataParam1;
226   AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.DataParam2 = DataParam2;
227   AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.DataParam3 = DataParam3;
228   AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.DataParam4 = DataParam4;
229
230   if ((AgesaEventAlloc->WriteRecordPtr == AgesaEventAlloc->ReadRecordPtr) &&
231              (AgesaEventAlloc->ReadWriteFlag == 0)) {
232     AgesaEventAlloc->WriteRecordPtr += 1;
233     AgesaEventAlloc->ReadRecordPtr += 1;
234     if (AgesaEventAlloc->WriteRecordPtr == TOTAL_EVENT_LOG_BUFFERS) {
235       AgesaEventAlloc->WriteRecordPtr = 0;
236       AgesaEventAlloc->ReadRecordPtr  = 0;
237     }
238   } else {
239     AgesaEventAlloc->WriteRecordPtr += 1;
240     if (AgesaEventAlloc->WriteRecordPtr == TOTAL_EVENT_LOG_BUFFERS) {
241       AgesaEventAlloc->WriteRecordPtr = 0;
242     }
243     AgesaEventAlloc->ReadWriteFlag = 0;
244   }
245   AgesaEventAlloc->Count = AgesaEventAlloc->Count + 1;
246
247   if (AgesaEventAlloc->Count <= TOTAL_EVENT_LOG_BUFFERS) {
248     AgesaEventAlloc->AgesaEventStruct[Index].Count = Index;
249   }
250 }
251
252
253 /*---------------------------------------------------------------------------------------*/
254 /**
255  *
256  * This function gets event logs from the circular buffer.
257  *
258  * It will read the oldest entry from the circular buffer and place that information to the structure
259  * pointed to by the parameter. The read pointers will be incremented to remove the entry from buffer
260  * so that a subsequent call will return the next entry from the buffer. If the buffer is empty the
261  * returned log event will have EventInfo zero, which is not a valid event id.
262  *
263  * @param[out]  EventRecord    The next log event.
264  * @param[in]   StdHeader    Header for library and services
265  *
266  * @retval      AGESA_SUCCESS     Always succeeds.
267  *
268  */
269 AGESA_STATUS
270 GetEventLog (
271      OUT   AGESA_EVENT *EventRecord,
272   IN       AMD_CONFIG_PARAMS *StdHeader
273   )
274 {
275   UINT16 Index;
276   AGESA_STRUCT_BUFFER *AgesaEventAlloc;
277
278   AgesaEventAlloc = NULL;
279
280   GetEventLogHeapPointer (&AgesaEventAlloc, StdHeader);
281   ASSERT (AgesaEventAlloc != NULL);
282
283   if ((AgesaEventAlloc->ReadRecordPtr == AgesaEventAlloc->WriteRecordPtr) &&
284       (AgesaEventAlloc->ReadWriteFlag == 1)) {
285     // EventInfo == zero, means no more data.
286     LibAmdMemFill (EventRecord, 0, sizeof (AGESA_EVENT), StdHeader);
287   } else {
288     Index = AgesaEventAlloc->ReadRecordPtr;
289     EventRecord->EventClass = AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.EventClass;
290     EventRecord->EventInfo  = AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.EventInfo;
291     EventRecord->DataParam1 = AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.DataParam1;
292     EventRecord->DataParam2 = AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.DataParam2;
293     EventRecord->DataParam3 = AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.DataParam3;
294     EventRecord->DataParam4 = AgesaEventAlloc->AgesaEventStruct[Index].AgesaEvent.DataParam4;
295     if (AgesaEventAlloc->ReadRecordPtr == (TOTAL_EVENT_LOG_BUFFERS - 1)) {
296       AgesaEventAlloc->ReadRecordPtr = 0;
297     } else {
298       AgesaEventAlloc->ReadRecordPtr = AgesaEventAlloc->ReadRecordPtr + 1;
299     }
300     if (AgesaEventAlloc->ReadRecordPtr == AgesaEventAlloc->WriteRecordPtr) {
301       AgesaEventAlloc->ReadWriteFlag = 1;
302     }
303   }
304   return (AGESA_SUCCESS);
305 }
306
307 /*---------------------------------------------------------------------------------------*/
308 /**
309  *
310  * This function gets event logs from the circular buffer without flushing the entry.
311  *
312  * It will read the desired entry from the circular buffer and place that information to the structure
313  * pointed to by the parameter. The read pointers will not be incremented to remove the entry from the
314  * buffer. If the buffer is empty, or the desired entry does not exist, FALSE will be returned.
315  *
316  * @param[out]  EventRecord  The next log event.
317  * @param[in]   Index        Zero-based unread entry index
318  * @param[in]   StdHeader    Header for library and services
319  *
320  * @retval      TRUE         Entry exists
321  * @retval      FALSE        Entry does not exist
322  *
323  */
324 BOOLEAN
325 PeekEventLog (
326      OUT   AGESA_EVENT *EventRecord,
327   IN       UINT16 Index,
328   IN       AMD_CONFIG_PARAMS *StdHeader
329   )
330 {
331   UINT16 ActualIndex;
332   UINT16 UnreadEntries;
333   AGESA_STRUCT_BUFFER *AgesaEventAlloc;
334
335   AgesaEventAlloc = NULL;
336
337   GetEventLogHeapPointer (&AgesaEventAlloc, StdHeader);
338   ASSERT (AgesaEventAlloc != NULL);
339
340   if ((AgesaEventAlloc->ReadRecordPtr == AgesaEventAlloc->WriteRecordPtr) &&
341       (AgesaEventAlloc->ReadWriteFlag == 1)) {
342     // EventInfo == zero, means no more data.
343     return FALSE;
344   }
345   if (AgesaEventAlloc->ReadRecordPtr < AgesaEventAlloc->WriteRecordPtr) {
346     UnreadEntries = AgesaEventAlloc->WriteRecordPtr - AgesaEventAlloc->ReadRecordPtr;
347   } else {
348     UnreadEntries = TOTAL_EVENT_LOG_BUFFERS - (AgesaEventAlloc->ReadRecordPtr - AgesaEventAlloc->WriteRecordPtr);
349   }
350   if (Index >= UnreadEntries) {
351     return FALSE;
352   }
353   ActualIndex = Index + AgesaEventAlloc->ReadRecordPtr;
354   if (ActualIndex >= TOTAL_EVENT_LOG_BUFFERS) {
355     ActualIndex -= TOTAL_EVENT_LOG_BUFFERS;
356   }
357
358   EventRecord->EventClass = AgesaEventAlloc->AgesaEventStruct[ActualIndex].AgesaEvent.EventClass;
359   EventRecord->EventInfo  = AgesaEventAlloc->AgesaEventStruct[ActualIndex].AgesaEvent.EventInfo;
360   EventRecord->DataParam1 = AgesaEventAlloc->AgesaEventStruct[ActualIndex].AgesaEvent.DataParam1;
361   EventRecord->DataParam2 = AgesaEventAlloc->AgesaEventStruct[ActualIndex].AgesaEvent.DataParam2;
362   EventRecord->DataParam3 = AgesaEventAlloc->AgesaEventStruct[ActualIndex].AgesaEvent.DataParam3;
363   EventRecord->DataParam4 = AgesaEventAlloc->AgesaEventStruct[ActualIndex].AgesaEvent.DataParam4;
364
365   return TRUE;
366 }
367
368
369 /*---------------------------------------------------------------------------------------*/
370 /**
371  *
372  * This function gets the Event Log pointer.
373  *
374  * It will locate the Event Log on the heap using the heap locate service.  If the Event
375  * Log is not located, NULL is returned.
376  *
377  * @param[out]  EventLog  Pointer to the Event Log, or NULL.
378  * @param[in]   StdHeader Our Configuration, for passing to services.
379  *
380  */
381 VOID
382 STATIC
383 GetEventLogHeapPointer (
384      OUT   AGESA_STRUCT_BUFFER **EventLog,
385   IN       AMD_CONFIG_PARAMS *StdHeader
386   )
387 {
388   LOCATE_HEAP_PTR LocateHeapStruct;
389
390   LocateHeapStruct.BufferHandle = EVENT_LOG_BUFFER_HANDLE;
391   LocateHeapStruct.BufferPtr = NULL;
392   if ((HeapLocateBuffer (&LocateHeapStruct, StdHeader)) == AGESA_SUCCESS) {
393     *EventLog = (AGESA_STRUCT_BUFFER *)LocateHeapStruct.BufferPtr;
394   } else {
395     *EventLog = NULL;
396   }
397 }