aboutsummaryrefslogtreecommitdiff
path: root/src/vendorcode/amd/agesa/f15tn/Proc/IDS/Debug/IdsDebugPrint.c
blob: 5c3e312158a1cc271fdcb5bd23439801c60c30bf (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/**
 * @file
 *
 * AMD Integrated Debug Print Routines
 *
 * Contains all functions related to IDS Debug Print
 *
 * @xrefitem bom "File Content Label" "Release Content"
 * @e project:      AGESA
 * @e sub-project:  IDS
 * @e \$Revision: 63425 $   @e \$Date: 2011-12-22 11:24:10 -0600 (Thu, 22 Dec 2011) $
 */
/*****************************************************************************
 * Copyright (c) 2008 - 2012, Advanced Micro Devices, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Advanced Micro Devices, Inc. nor the names of
 *       its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************
 */

 /*----------------------------------------------------------------------------------------
 *                             M O D U L E S    U S E D
 *----------------------------------------------------------------------------------------
 */
#include "AGESA.h"
#include "Ids.h"
#include "IdsLib.h"
#include "amdlib.h"
#include "IdsDebugPrint.h"
#include "Filecode.h"
CODE_GROUP (G1_PEICC)
RDATA_GROUP (G1_PEICC)

#define FILECODE PROC_IDS_DEBUG_IDSDEBUGPRINT_FILECODE

//
// Also support coding convention rules for var arg macros
//
#define _INT_SIZE_OF(n) ((sizeof (n) + sizeof (UINTN) - 1) &~(sizeof (UINTN) - 1))
typedef CHAR8 *VA_LIST;
#define VA_START(ap, v) (ap = (VA_LIST) & (v) + _INT_SIZE_OF (v))
#define VA_ARG(ap, t)   (*(t *) ((ap += _INT_SIZE_OF (t)) - _INT_SIZE_OF (t)))
#define VA_END(ap)      (ap = (VA_LIST) 0)

#define LEFT_JUSTIFY    0x01
#define PREFIX_SIGN     0x02
#define PREFIX_BLANK    0x04
#define COMMA_TYPE      0x08
#define LONG_TYPE       0x10
#define PREFIX_ZERO     0x20

#define MAX_LOCAL_BUFFER_SIZE   512
#define BUFFER_OVERFLOW 0xFFFF

/**
 *  Check If any print service is enabled.
 *
 *  @param[in] DebugPrintList    The Pointer to print service list
 *
 *  @retval       TRUE    At least on print service is enabled
 *  @retval       FALSE   All print service is disabled
 *
 **/
STATIC BOOLEAN
AmdIdsDebugPrintCheckSupportAll (
  IN      IDS_DEBUG_PRINT **DebugPrintList
  )
{
  BOOLEAN IsSupported;
  UINTN i;
  IsSupported = FALSE;
  for (i = 0; DebugPrintList[i] != NULL; i++) {
    if (DebugPrintList[i]->support ()) {
      IsSupported = TRUE;
    }
  }
  return IsSupported;
}

/**
 *  Parses flag and width information from theFormat string and returns the next index
 *  into the Format string that needs to be parsed. See file headed for details of Flag and Width.
 *
 *  @param[in]  Format  Current location in the AvSPrint format string.
 *  @param[out] Flags  Returns flags
 *  @param[out] Width  Returns width of element
 *  @param[out] Marker Vararg list that may be partially consumed and returned.
 *
 *  @retval Pointer indexed into the Format string for all the information parsed by this routine.
 *
 **/
STATIC CHAR8 *
GetFlagsAndWidth (
  IN       CHAR8 *Format,
     OUT   UINTN *Flags,
     OUT   UINTN *Width,
  IN OUT   VA_LIST *Marker
  )
{
  UINTN   Count;
  BOOLEAN Done;

  *Flags = 0;
  *Width = 0;
  for (Done = FALSE; !Done; ) {
    Format++;

    switch (*Format) {

    case '-':			/* ' - ' */
      *Flags |= LEFT_JUSTIFY;
      break;
    case '+':			/* ' + ' */
      *Flags |= PREFIX_SIGN;
      break;
    case ' ':
      *Flags |= PREFIX_BLANK;
      break;
    case ',':			/* ', ' */
      *Flags |= COMMA_TYPE;
      break;
    case 'L':
    case 'l':
      *Flags |= LONG_TYPE;
      break;

    case '*':
      *Width = VA_ARG (*Marker, UINTN);
      break;

    case '0':
      *Flags |= PREFIX_ZERO;
      break;

    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
      Count = 0;
      do {
        Count = (Count * 10) + *Format - '0';
        Format++;
      } while ((*Format >= '0')  &&  (*Format <= '9'));
      Format--;
      *Width = Count;
      break;

    default:
      Done = TRUE;
    }
  }
  return Format;
}

CHAR8 STATIC HexStr[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
extern CONST IDS_DEBUG_PRINT* ROMDATA IdsDebugPrint[];

/**
 *
 *  @param[in,out] Value  - Hex value to convert to a string in Buffer.
 *
 *
 */
VOID
GetDebugPrintList (
  IN OUT   CONST IDS_DEBUG_PRINT   ***pIdsDebugPrintListPtr
  )
{
  *pIdsDebugPrintListPtr = &IdsDebugPrint[0];
}

/**
 *
 *  @param[in,out] Buffer Location to place ascii hex string of Value.
 *  @param[in] Value  - Hex value to convert to a string in Buffer.
 *  @param[in] Flags  - Flags to use in printing Hex string, see file header for details.
 *  @param[in] Width  - Width of hex value.
 *  @param[in,out] BufferSize  - Size of input buffer
 *
 *  @retval Number of characters printed.
 **/

STATIC UINTN
ValueToHexStr (
  IN OUT   CHAR8   *Buffer,
  IN       UINT64      Value,
  IN       UINTN       Flags,
  IN       UINTN       Width,
  IN OUT   UINTN       *BufferSize
  )
{
  CHAR8   TempBuffer[30];
  CHAR8   *TempStr;
  CHAR8   Prefix;
  CHAR8   *BufferPtr;
  UINTN   Count;
  UINTN   Index;

  TempStr = TempBuffer;
  BufferPtr = Buffer;
  //
  // Count starts at one since we will null terminate. Each iteration of the
  // loop picks off one nibble. Oh yea TempStr ends up backwards
  //
  Count = 0;
  do {
    *(TempStr++) = HexStr[Value & 0x0f];
    Value >>= 4;
    Count++;
  } while (Value != 0);

  if (Flags & PREFIX_ZERO) {
    Prefix = '0';
  } else if (!(Flags & LEFT_JUSTIFY)) {
    Prefix = ' ';
  } else {
    Prefix = 0x00;
  }
  for (Index = Count; Index < Width; Index++) {
    *(TempStr++) = Prefix;
  }

  //
  // Reverse temp string into Buffer.
  //
  while (TempStr != TempBuffer) {
    (*BufferSize)--;
    if (*BufferSize == 0) {
      return BUFFER_OVERFLOW;
    }
    *(BufferPtr++) = *(--TempStr);
  }

  *BufferPtr = 0;
  return Index;
}

/**
 *  Prints a Value as a decimal number in Buffer
 *
 *  @param[in] Buffer  Location to place ascii decimal number string of Value.
 *  @param[in] Value  Decimal value to convert to a string in Buffer.
 *  @param[in] Flags  Flags to use in printing decimal string, see file header for details.
 *  @param[in,out] BufferSize  Size of input buffer
 *
 *  @retval Number of characters printed.
 *
**/

STATIC UINTN
ValueToString (
  IN OUT   CHAR8   *Buffer,
  IN       INT32       Value,
  IN       UINTN       Flags,
  IN OUT   UINTN       *BufferSize
  )
{
  CHAR8   TempBuffer[30];
  CHAR8   *TempStr;
  CHAR8   *BufferPtr;
  UINTN   Count;
  UINTN   Remainder;

  ASSERT (*BufferSize);
  TempStr = TempBuffer;
  BufferPtr = Buffer;
  Count = 0;

  if (Value < 0) {
    (*BufferSize)--;
    if (*BufferSize == 0) {
      return BUFFER_OVERFLOW;
    }
    *(BufferPtr++) = '-';	/* ' - ' */
    Value = - Value;
    Count++;
  }

  do {
    Remainder = Value % 10;
    Value /= 10;
    *(TempStr++) = (CHAR8)(Remainder + '0');
    Count++;
    if ((Flags & COMMA_TYPE) == COMMA_TYPE) {
      if (Count % 3 == 0) {
        *(TempStr++) = ',';
      }
    }
  } while (Value != 0);

  //
  // Reverse temp string into Buffer.
  //
  while (TempStr != TempBuffer) {
    (*BufferSize)--;
    if (*BufferSize == 0) {
      return BUFFER_OVERFLOW;
    }
    *(BufferPtr++) = *(--TempStr);
  }

  *BufferPtr = 0;
  return Count;
}

/**
 *  Worker function for print string to buffer
 *
 *  @param[in] Flag    - filter flag
 *  @param[in] *Format - format string
 *  @param[in] Marker  - Variable parameter
 *  @param[in] Buffer  - Point to input buffer
 *  @param[in] BufferSize  - Buffer size
 *  @param[out] OutputStringLen  - output string length, include '\0' at the end
 *
 *  @retval    IDS_DEBUG_PRINT_SUCCESS      succeed
 *  @retval    IDS_DEBUG_PRINT_BUFFER_OVERFLOW      input buffer overflow
**/
STATIC IDS_DEBUG_PRINT_STATUS
AmdIdsDebugPrintWorker (
  IN       CONST CHAR8 *Format,
  IN       VA_LIST Marker,
  IN       CHAR8 *Buffer,
  IN       UINTN BufferSize,
     OUT   UINTN *OutputStringLen
  )
{
  UINTN Index;
  UINTN Length;
  UINTN Flags;
  UINTN Width;
  UINT64 Value;
  CHAR8 *AsciiStr;

  //Init the default Value
  Index = 0;
  //
  // Process format string
  //
  for (; (*Format != '\0') && (BufferSize > 0); Format++) {
    if (*Format != '%') {
      Buffer[Index++] = *Format;
      BufferSize--;
    } else {
      Format = GetFlagsAndWidth ((CHAR8 *)Format, &Flags, &Width, &Marker);
      switch (*Format) {
      case 'X':
        Flags |= PREFIX_ZERO;
        Width = sizeof (UINT64) * 2;
	// fall through
      case 'x':
        if ((Flags & LONG_TYPE) == LONG_TYPE) {
          Value = VA_ARG (Marker, UINT64);
        } else {
          Value = VA_ARG (Marker, UINTN);
        }
        Length = ValueToHexStr (&Buffer[Index], Value, Flags, Width, &BufferSize);
        if (Length != BUFFER_OVERFLOW) {
          Index += Length;
        } else {
          return IDS_DEBUG_PRINT_BUFFER_OVERFLOW;
        }
        break;

      case 'd':
        Value = (UINTN)VA_ARG (Marker, UINT32);
        Length = ValueToString (&Buffer[Index], (UINT32)Value, Flags, &BufferSize);
        if (Length != BUFFER_OVERFLOW) {
          Index += Length;
        } else {
          return IDS_DEBUG_PRINT_BUFFER_OVERFLOW;
        }

        break;

      case 's':
      case 'S':
        AsciiStr = (CHAR8 *)VA_ARG (Marker, CHAR8 *);
        while (*AsciiStr != '\0') {
          BufferSize--;
          if (BufferSize == 0) {
            return IDS_DEBUG_PRINT_BUFFER_OVERFLOW;
          }
          Buffer[Index++] = *AsciiStr++;
        }
        break;

      case 'c':
        BufferSize--;
        if (BufferSize == 0) {
          return IDS_DEBUG_PRINT_BUFFER_OVERFLOW;
        }
        Buffer[Index++] = (CHAR8)VA_ARG (Marker, UINTN);
        break;

      case 'v':
        ASSERT (FALSE);   // %v is no longer supported
        break;

      case '%':
        BufferSize--;
        if (BufferSize == 0) {
          return IDS_DEBUG_PRINT_BUFFER_OVERFLOW;
        }
        Buffer[Index++] = *Format;
        break;

      default:
        //
        // if the type is unknown print it to the screen
        //
        BufferSize--;
        if (BufferSize == 0) {
          return IDS_DEBUG_PRINT_BUFFER_OVERFLOW;
        }
        Buffer[Index++] = '%';

        BufferSize--;
        if (BufferSize == 0) {
          return IDS_DEBUG_PRINT_BUFFER_OVERFLOW;
        }
        Buffer[Index++] = *Format;
        break;
      }
    }
  }
  if (BufferSize == 0) {
    return IDS_DEBUG_PRINT_BUFFER_OVERFLOW;
  }
  //Mark the end of word
  Buffer[Index] = 0;
  *OutputStringLen = Index;
  return IDS_DEBUG_PRINT_SUCCESS;
}


/**
 *  Insert Overflow warning to the tail of output
 *
 *  @param[in] Buffer  - Point to input buffer
 *  @param[in] BufferSize  - Buffer size
 *
**/
STATIC VOID
InsertOverflowWarningMessage (
  IN      CHAR8   *Buffer,
  IN      UINTN BufferSize
  )
{
  CHAR8 *Destination;
  CHAR8 WarningString[] = "\n#BUFFER  OVERFLOW#\n";
  AMD_CONFIG_PARAMS StdHeader;

  Destination = Buffer + BufferSize - sizeof (WarningString);
  LibAmdMemCopy (Destination, WarningString, sizeof (WarningString), &StdHeader);
}

/**
 *  Process debug string
 *
 *  @param[in] Flag    - filter flag
 *  @param[in] *Format - format string
 *  @param[in] Marker  - Variable parameter
 *
**/
STATIC VOID
AmdIdsDebugPrintProcess (
  IN      UINT64 Flag,
  IN      CONST CHAR8 *Format,
  IN      VA_LIST Marker
  )
{
  UINT64 Filter;
  CHAR8 LocalBuffer[MAX_LOCAL_BUFFER_SIZE];
  UINTN OutPutStringLen;
  IDS_DEBUG_PRINT **DebugPrintList;
  IDS_DEBUG_PRINT_PRIVATE_DATA debugPrintPrivate;
  UINT8 i;


  GetDebugPrintList ((CONST IDS_DEBUG_PRINT ***)&DebugPrintList);
  if (AmdIdsDebugPrintCheckSupportAll (DebugPrintList)) {
    if (AmdIdsDebugPrintWorker (Format, Marker, &LocalBuffer[0], sizeof (LocalBuffer), &OutPutStringLen) == IDS_DEBUG_PRINT_BUFFER_OVERFLOW) {
      InsertOverflowWarningMessage (&LocalBuffer[0], sizeof (LocalBuffer));
      OutPutStringLen = sizeof (LocalBuffer);
    }

    //init input
    debugPrintPrivate.saveContext = FALSE;

    for (i = 0; DebugPrintList[i] != NULL; i++) {
      if (DebugPrintList[i]->support ()) {
        Filter = IDS_DEBUG_PRINT_MASK;
        //Get Customize filter (Option)
        DebugPrintList[i]->customfilter (&Filter);
        if (Flag & Filter) {
          //Init Private Date (Option)
          DebugPrintList[i]->InitPrivateData (Flag, &debugPrintPrivate);
          //Print Physical Layer
          DebugPrintList[i]->print (&LocalBuffer[0], OutPutStringLen, &debugPrintPrivate);
        }
      }
    }
  }
}

/**
 *  Prints string to debug host like printf in C
 *
 *  @param[in] Flag    - filter flag
 *  @param[in] *Format - format string
 *  @param[in] ... Variable parameter
 *
**/
VOID
AmdIdsDebugPrint (
  IN      UINT64 Flag,
  IN      CONST CHAR8 *Format,
  IN      ...
  )
{
  VA_LIST Marker;
  VA_START (Marker, Format); //init marker to 1st dynamic parameters.
  AmdIdsDebugPrintProcess (Flag, Format, Marker);
  VA_END (Marker);
}

/**
 *  Prints memory debug strings
 *
 *  @param[in] *Format - format string
 *  @param[in] ... Variable parameter
 *
**/
VOID
AmdIdsDebugPrintMem (
  IN      CHAR8 *Format,
  IN      ...
  )
{
  VA_LIST Marker;
  VA_START (Marker, Format); //init marker to 1st dynamic parameters.
  AmdIdsDebugPrintProcess (MEM_FLOW, Format, Marker);
  VA_END (Marker);
}

/**
 *  Prints CPU debug strings
 *
 *  @param[in] *Format - format string
 *  @param[in] ... Variable parameter
 *
**/
VOID
AmdIdsDebugPrintCpu (
  IN      CHAR8 *Format,
  IN      ...
  )
{
  VA_LIST Marker;
  VA_START (Marker, Format); //init marker to 1st dynamic parameters.
  AmdIdsDebugPrintProcess (CPU_TRACE, Format, Marker);
  VA_END (Marker);
}


/**
 *  Prints HT debug strings
 *
 *  @param[in] *Format - format string
 *  @param[in] ... Variable parameter
 *
**/
VOID
AmdIdsDebugPrintHt (
  IN      CHAR8 *Format,
  IN      ...
  )
{
  VA_LIST Marker;
  VA_START (Marker, Format); //init marker to 1st dynamic parameters.
  AmdIdsDebugPrintProcess (HT_TRACE, Format, Marker);
  VA_END (Marker);
}


/**
 *  Prints GNB debug strings
 *
 *  @param[in] *Format - format string
 *  @param[in] ... Variable parameter
 *
**/
VOID
AmdIdsDebugPrintGnb (
  IN      CHAR8 *Format,
  IN      ...
  )
{
  VA_LIST Marker;
  VA_START (Marker, Format); //init marker to 1st dynamic parameters.
  AmdIdsDebugPrintProcess (GNB_TRACE, Format, Marker);
  VA_END (Marker);
}

/**
 *  Prints debug strings in any condition
 *
 *  @param[in] *Format - format string
 *  @param[in] ... Variable parameter
 *
**/
VOID
AmdIdsDebugPrintAll (
  IN      CHAR8 *Format,
  IN      ...
  )
{
  VA_LIST Marker;
  VA_START (Marker, Format); //init marker to 1st dynamic parameters.
  AmdIdsDebugPrintProcess (TRACE_MASK_ALL, Format, Marker);
  VA_END (Marker);
}