Fix memory allocation problem in amdInitLate. Disabled until further debug.
[coreboot.git] / src / mainboard / amd / persimmon / agesawrapper.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2011 Advanced Micro Devices, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 /*----------------------------------------------------------------------------------------
21  *                             M O D U L E S    U S E D
22  *----------------------------------------------------------------------------------------
23  */
24  
25 #include <stdint.h>
26 #include <string.h>
27 #include "agesawrapper.h"
28 #include "BiosCallOuts.h"
29 #include "cpuRegisters.h"
30 #include "cpuCacheInit.h"
31 #include "cpuApicUtilities.h"
32 #include "cpuEarlyInit.h"
33 #include "cpuLateInit.h"
34 #include "Dispatcher.h"
35 #include "cpuCacheInit.h"
36 #include "amdlib.h"
37 #include "PlatformGnbPcieComplex.h"
38 #include "Filecode.h"
39
40 #define FILECODE UNASSIGNED_FILE_FILECODE
41
42 /*----------------------------------------------------------------------------------------
43  *                   D E F I N I T I O N S    A N D    M A C R O S
44  *----------------------------------------------------------------------------------------
45  */
46
47 /* ACPI table pointers returned by AmdInitLate */
48 VOID *DmiTable    = NULL;
49 VOID *AcpiPstate  = NULL;
50 VOID *AcpiSrat    = NULL;
51 VOID *AcpiSlit    = NULL;
52
53 VOID *AcpiWheaMce = NULL;
54 VOID *AcpiWheaCmc = NULL;
55 VOID *AcpiAlib    = NULL; 
56  
57
58 /*----------------------------------------------------------------------------------------
59  *                  T Y P E D E F S     A N D     S T R U C T U  R E S
60  *----------------------------------------------------------------------------------------
61  */
62
63 /*----------------------------------------------------------------------------------------
64  *           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
65  *----------------------------------------------------------------------------------------
66  */
67  
68 /*----------------------------------------------------------------------------------------
69  *                          E X P O R T E D    F U N C T I O N S
70  *----------------------------------------------------------------------------------------
71  */
72  
73 /*---------------------------------------------------------------------------------------
74  *                          L O C A L    F U N C T I O N S
75  *---------------------------------------------------------------------------------------
76  */
77 UINT32 
78 agesawrapper_amdinitcpuio (
79   VOID
80   )
81 {
82   AGESA_STATUS                  Status;
83   UINT64                        MsrReg;
84   UINT32                        PciData;
85   PCI_ADDR                      PciAddress;
86   AMD_CONFIG_PARAMS             StdHeader;
87   
88   /* Enable legacy video routing: D18F1xF4 VGA Enable */
89   PciAddress.AddressValue = MAKE_SBDFO (0, 0, 0x18, 1, 0xF4);
90   PciData = 1;
91   LibAmdPciWrite(AccessWidth32, PciAddress, &PciData, &StdHeader); 
92
93   /* The platform BIOS needs to ensure the memory ranges of SB800 legacy
94    * devices (TPM, HPET, BIOS RAM, Watchdog Timer, I/O APIC and ACPI) are
95    * set to non-posted regions.
96    */
97   PciAddress.AddressValue = MAKE_SBDFO (0, 0, 0x18, 1, 0x84);
98   PciData = 0x00FEDF00; // last address before processor local APIC at FEE00000
99   PciData |= 1 << 7;    // set NP (non-posted) bit
100   LibAmdPciWrite(AccessWidth32, PciAddress, &PciData, &StdHeader); 
101   PciAddress.AddressValue = MAKE_SBDFO (0, 0, 0x18, 1, 0x80);
102   PciData = (0xFED00000 >> 8) | 3; // lowest NP address is HPET at FED00000
103   LibAmdPciWrite(AccessWidth32, PciAddress, &PciData, &StdHeader); 
104    
105   /* Map the remaining PCI hole as posted MMIO */
106   PciAddress.AddressValue = MAKE_SBDFO (0, 0, 0x18, 1, 0x8C);
107   PciData = 0x00FECF00; // last address before non-posted range
108   LibAmdPciWrite(AccessWidth32, PciAddress, &PciData, &StdHeader); 
109   LibAmdMsrRead (0xC001001A, &MsrReg, &StdHeader);
110   MsrReg = (MsrReg >> 8) | 3;
111   PciAddress.AddressValue = MAKE_SBDFO (0, 0, 0x18, 1, 0x88);
112   PciData = (UINT32)MsrReg;
113   LibAmdPciWrite(AccessWidth32, PciAddress, &PciData, &StdHeader); 
114    
115   /* Send all IO (0000-FFFF) to southbridge. */
116   PciAddress.AddressValue = MAKE_SBDFO (0, 0, 0x18, 1, 0xC4);
117   PciData = 0x0000F000;
118   LibAmdPciWrite(AccessWidth32, PciAddress, &PciData, &StdHeader);
119   PciAddress.AddressValue = MAKE_SBDFO (0, 0, 0x18, 1, 0xC0);
120   PciData = 0x00000003;
121   LibAmdPciWrite(AccessWidth32, PciAddress, &PciData, &StdHeader);
122   Status = AGESA_SUCCESS;
123   return (UINT32)Status;
124 }
125  
126 UINT32 
127 agesawrapper_amdinitmmio (
128   VOID
129   )
130 {
131   AGESA_STATUS                  Status;
132   UINT64                        MsrReg;
133   UINT32                        PciData;
134   PCI_ADDR                      PciAddress;
135   AMD_CONFIG_PARAMS             StdHeader;
136   
137   /*
138    Set the MMIO Configuration Base Address and Bus Range onto MMIO configuration base
139    Address MSR register.
140   */
141
142   MsrReg = CONFIG_MMCONF_BASE_ADDRESS | (LibAmdBitScanReverse (CONFIG_MMCONF_BUS_NUMBER) << 2) | 1;
143   LibAmdMsrWrite (0xC0010058, &MsrReg, &StdHeader);
144   
145   /*
146    Set the NB_CFG MSR register. Enable CF8 extended configuration cycles.
147   */
148   LibAmdMsrRead (0xC001001F, &MsrReg, &StdHeader);
149   MsrReg = MsrReg | 0x0000400000000000;
150   LibAmdMsrWrite (0xC001001F, &MsrReg, &StdHeader);
151   
152   /* Set Ontario Link Data */
153   PciAddress.AddressValue = MAKE_SBDFO (0, 0, 0, 0, 0xE0);
154   PciData = 0x01308002;
155   LibAmdPciWrite(AccessWidth32, PciAddress, &PciData, &StdHeader); 
156   PciAddress.AddressValue = MAKE_SBDFO (0, 0, 0, 0, 0xE4);
157   PciData = (AMD_APU_SSID<<0x10)|AMD_APU_SVID;
158   LibAmdPciWrite(AccessWidth32, PciAddress, &PciData, &StdHeader); 
159   
160
161   /* Set ROM cache onto WP to decrease post time */
162   MsrReg = (0x0100000000 - CONFIG_ROM_SIZE) | 5;
163   LibAmdMsrWrite (0x20C, &MsrReg, &StdHeader);
164   MsrReg = (0x1000000000 - CONFIG_ROM_SIZE) | 0x800;
165   LibAmdMsrWrite (0x20D, &MsrReg, &StdHeader);
166
167   Status = AGESA_SUCCESS;
168   return (UINT32)Status;
169 }
170
171 UINT32 
172 agesawrapper_amdinitreset (
173   VOID
174   )
175 {
176   AGESA_STATUS status;
177   AMD_INTERFACE_PARAMS AmdParamStruct;
178   AMD_RESET_PARAMS AmdResetParams;
179   
180   LibAmdMemFill (&AmdParamStruct,
181                  0,
182                  sizeof (AMD_INTERFACE_PARAMS),
183                  &(AmdParamStruct.StdHeader));
184
185
186   LibAmdMemFill (&AmdResetParams,
187                  0,
188                  sizeof (AMD_RESET_PARAMS),
189                  &(AmdResetParams.StdHeader));
190
191   AmdParamStruct.AgesaFunctionName = AMD_INIT_RESET;
192   AmdParamStruct.AllocationMethod = ByHost;
193   AmdParamStruct.NewStructSize = sizeof(AMD_RESET_PARAMS);
194   AmdParamStruct.NewStructPtr = &AmdResetParams;
195   AmdParamStruct.StdHeader.AltImageBasePtr = 0;
196   AmdParamStruct.StdHeader.CalloutPtr = NULL;
197   AmdParamStruct.StdHeader.Func = 0;
198   AmdParamStruct.StdHeader.ImageBasePtr = 0;
199   AmdCreateStruct (&AmdParamStruct);
200   AmdResetParams.HtConfig.Depth = 0;
201   
202   status = AmdInitReset ((AMD_RESET_PARAMS *)AmdParamStruct.NewStructPtr);
203   if (status != AGESA_SUCCESS) agesawrapper_amdreadeventlog();
204   AmdReleaseStruct (&AmdParamStruct);
205   return (UINT32)status;
206  }  
207   
208 UINT32 
209 agesawrapper_amdinitearly (
210   VOID
211   )
212 {
213   AGESA_STATUS status;
214   AMD_INTERFACE_PARAMS AmdParamStruct;
215   AMD_EARLY_PARAMS     *AmdEarlyParamsPtr;
216   
217   LibAmdMemFill (&AmdParamStruct,
218                  0,
219                  sizeof (AMD_INTERFACE_PARAMS),
220                  &(AmdParamStruct.StdHeader));
221
222   AmdParamStruct.AgesaFunctionName = AMD_INIT_EARLY;
223   AmdParamStruct.AllocationMethod = PreMemHeap;
224   AmdParamStruct.StdHeader.AltImageBasePtr = 0;
225   AmdParamStruct.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
226   AmdParamStruct.StdHeader.Func = 0;
227   AmdParamStruct.StdHeader.ImageBasePtr = 0;
228   AmdCreateStruct (&AmdParamStruct);
229   
230   AmdEarlyParamsPtr = (AMD_EARLY_PARAMS *)AmdParamStruct.NewStructPtr;
231   OemCustomizeInitEarly (AmdEarlyParamsPtr);
232   
233   status = AmdInitEarly ((AMD_EARLY_PARAMS *)AmdParamStruct.NewStructPtr);
234   if (status != AGESA_SUCCESS) agesawrapper_amdreadeventlog();
235   AmdReleaseStruct (&AmdParamStruct);
236
237   return (UINT32)status;
238 }
239
240 UINT32 
241 agesawrapper_amdinitpost (
242   VOID
243   )
244 {
245   AGESA_STATUS status;
246   UINT16                  i;
247   UINT32          *HeadPtr;
248   AMD_INTERFACE_PARAMS  AmdParamStruct;
249   BIOS_HEAP_MANAGER    *BiosManagerPtr;
250
251   LibAmdMemFill (&AmdParamStruct,
252                  0,
253                  sizeof (AMD_INTERFACE_PARAMS),
254                  &(AmdParamStruct.StdHeader));
255
256   AmdParamStruct.AgesaFunctionName = AMD_INIT_POST;
257   AmdParamStruct.AllocationMethod = PreMemHeap;
258   AmdParamStruct.StdHeader.AltImageBasePtr = 0;
259   AmdParamStruct.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
260   AmdParamStruct.StdHeader.Func = 0;
261   AmdParamStruct.StdHeader.ImageBasePtr = 0;
262
263   AmdCreateStruct (&AmdParamStruct);
264   status = AmdInitPost ((AMD_POST_PARAMS *)AmdParamStruct.NewStructPtr);
265   if (status != AGESA_SUCCESS) agesawrapper_amdreadeventlog();
266   AmdReleaseStruct (&AmdParamStruct);
267   /* Initialize heap space */
268   BiosManagerPtr = (BIOS_HEAP_MANAGER *)BIOS_HEAP_START_ADDRESS;
269
270   HeadPtr = (UINT32 *) ((UINT8 *) BiosManagerPtr + sizeof (BIOS_HEAP_MANAGER));
271   for (i = 0; i < ((BIOS_HEAP_SIZE/4) - (sizeof (BIOS_HEAP_MANAGER)/4)); i++)
272   {
273     *HeadPtr = 0x00000000;
274     HeadPtr++;
275   }
276   BiosManagerPtr->StartOfAllocatedNodes = 0;
277   BiosManagerPtr->StartOfFreedNodes = 0;
278
279   return (UINT32)status;
280 }
281
282 UINT32 
283 agesawrapper_amdinitenv (
284   VOID
285   )
286 {
287   AGESA_STATUS status;
288   AMD_INTERFACE_PARAMS AmdParamStruct;
289   PCI_ADDR             PciAddress;
290   UINT32               PciValue;
291
292   LibAmdMemFill (&AmdParamStruct,
293                  0,
294                  sizeof (AMD_INTERFACE_PARAMS),
295                  &(AmdParamStruct.StdHeader));
296
297   AmdParamStruct.AgesaFunctionName = AMD_INIT_ENV;
298   AmdParamStruct.AllocationMethod = PostMemDram;
299   AmdParamStruct.StdHeader.AltImageBasePtr = 0;
300   AmdParamStruct.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
301   AmdParamStruct.StdHeader.Func = 0;
302   AmdParamStruct.StdHeader.ImageBasePtr = 0;
303   AmdCreateStruct (&AmdParamStruct);
304   status = AmdInitEnv ((AMD_ENV_PARAMS *)AmdParamStruct.NewStructPtr);
305   if (status != AGESA_SUCCESS) agesawrapper_amdreadeventlog();
306   /* Initialize Subordinate Bus Number and Secondary Bus Number
307    * In platform BIOS this address is allocated by PCI enumeration code
308      Modify D1F0x18
309    */  
310   PciAddress.Address.Bus = 0;
311   PciAddress.Address.Device = 1;
312   PciAddress.Address.Function = 0;
313   PciAddress.Address.Register = 0x18;
314   /* Write to D1F0x18 */
315   LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
316   PciValue |= 0x00010100;
317   LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
318
319   /* Initialize GMM Base Address for Legacy Bridge Mode
320   *  Modify B1D5F0x18
321   */
322   PciAddress.Address.Bus = 1;
323   PciAddress.Address.Device = 5;
324   PciAddress.Address.Function = 0;
325   PciAddress.Address.Register = 0x18;
326
327   LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
328   PciValue |= 0x96000000;
329   LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
330
331   /* Initialize FB Base Address for Legacy Bridge Mode
332   * Modify B1D5F0x10
333   */
334   PciAddress.Address.Register = 0x10;
335   LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
336   PciValue |= 0x80000000;
337   LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
338
339   /* Initialize GMM Base Address for Pcie Mode
340   *  Modify B0D1F0x18
341   */
342   PciAddress.Address.Bus = 0;
343   PciAddress.Address.Device = 1;
344   PciAddress.Address.Function = 0;
345   PciAddress.Address.Register = 0x18;
346
347   LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
348   PciValue |= 0x96000000;
349   LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
350
351   /* Initialize FB Base Address for Pcie Mode
352   *  Modify B0D1F0x10
353   */
354   PciAddress.Address.Register = 0x10;
355   LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
356   PciValue |= 0x80000000;
357   LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
358
359
360   /* Initialize MMIO Base and Limit Address
361   *  Modify B0D1F0x20
362   */
363   PciAddress.Address.Bus = 0;
364   PciAddress.Address.Device = 1;
365   PciAddress.Address.Function = 0;
366   PciAddress.Address.Register = 0x20;
367
368   LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
369   PciValue |= 0x96009600;
370   LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
371
372   /* Initialize MMIO Prefetchable Memory Limit and Base
373   *  Modify B0D1F0x24
374   */
375   PciAddress.Address.Register = 0x24;
376   LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
377   PciValue |= 0x8FF18001;
378   LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
379   AmdReleaseStruct (&AmdParamStruct);
380
381   return (UINT32)status;
382 }
383
384 VOID *
385 agesawrapper_getlateinitptr (
386   int pick
387   )
388 {
389   switch (pick) {
390     case PICK_DMI:
391       return DmiTable;
392     case PICK_PSTATE:
393       return AcpiPstate;
394     case PICK_SRAT:
395       return AcpiSrat;
396     case PICK_SLIT:
397       return AcpiSlit;
398     case PICK_WHEA_MCE:
399       return AcpiWheaMce;
400     case PICK_WHEA_CMC:
401       return AcpiWheaCmc;
402     case PICK_ALIB:
403       return AcpiAlib;
404     default:
405       return NULL;
406   }
407 }
408
409 UINT32 
410 agesawrapper_amdinitmid (
411   VOID
412   )
413 {
414   AGESA_STATUS status;
415   AMD_INTERFACE_PARAMS AmdParamStruct;
416   
417   /* Enable MMIO on AMD CPU Address Map Controller */
418   agesawrapper_amdinitcpuio ();
419   
420   LibAmdMemFill (&AmdParamStruct,
421                  0,
422                  sizeof (AMD_INTERFACE_PARAMS),
423                  &(AmdParamStruct.StdHeader));
424
425   AmdParamStruct.AgesaFunctionName = AMD_INIT_MID;
426   AmdParamStruct.AllocationMethod = PostMemDram;
427   AmdParamStruct.StdHeader.AltImageBasePtr = 0;
428   AmdParamStruct.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
429   AmdParamStruct.StdHeader.Func = 0;
430   AmdParamStruct.StdHeader.ImageBasePtr = 0;
431
432   AmdCreateStruct (&AmdParamStruct);
433
434   status = AmdInitMid ((AMD_MID_PARAMS *)AmdParamStruct.NewStructPtr);
435   if (status != AGESA_SUCCESS) agesawrapper_amdreadeventlog();
436   AmdReleaseStruct (&AmdParamStruct);
437
438   return (UINT32)status;
439 }
440
441 UINT32 
442 agesawrapper_amdinitlate (
443   VOID
444   )
445 {
446   AGESA_STATUS Status;
447   AMD_INTERFACE_PARAMS AmdParamStruct = {0};
448   AMD_LATE_PARAMS *AmdLateParams;
449
450   return 0; // this causes bad ACPI SSDT, need to debug
451
452   AmdParamStruct.AgesaFunctionName = AMD_INIT_LATE;
453   AmdParamStruct.AllocationMethod = PostMemDram;
454   AmdParamStruct.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
455   AmdCreateStruct (&AmdParamStruct);
456   AmdLateParams = (AMD_LATE_PARAMS *)AmdParamStruct.NewStructPtr;
457   Status = AmdInitLate (AmdLateParams);
458   if (Status != AGESA_SUCCESS) {
459     agesawrapper_amdreadeventlog();
460     ASSERT(Status == AGESA_SUCCESS);
461   }
462
463   DmiTable    = AmdLateParams->DmiTable;
464   AcpiPstate  = AmdLateParams->AcpiPState;
465   AcpiSrat    = AmdLateParams->AcpiSrat;
466   AcpiSlit    = AmdLateParams->AcpiSlit;
467
468   AcpiWheaMce = AmdLateParams->AcpiWheaMce;
469   AcpiWheaCmc = AmdLateParams->AcpiWheaCmc;
470   AcpiAlib    = AmdLateParams->AcpiAlib;
471
472   AmdReleaseStruct (&AmdParamStruct);
473   return (UINT32)Status;
474 }
475
476 UINT32 
477 agesawrapper_amdlaterunaptask (
478   UINT32 Data, 
479   VOID *ConfigPtr
480   )
481 {
482   AGESA_STATUS Status;
483   AMD_LATE_PARAMS AmdLateParams;
484
485   LibAmdMemFill (&AmdLateParams,
486                  0,
487                  sizeof (AMD_LATE_PARAMS),
488                  &(AmdLateParams.StdHeader));
489
490   AmdLateParams.StdHeader.AltImageBasePtr = 0;
491   AmdLateParams.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
492   AmdLateParams.StdHeader.Func = 0;
493   AmdLateParams.StdHeader.ImageBasePtr = 0;
494
495   Status = AmdLateRunApTask (&AmdLateParams);
496   if (Status != AGESA_SUCCESS) {
497     agesawrapper_amdreadeventlog();
498     ASSERT(Status == AGESA_SUCCESS);
499   }
500
501   DmiTable       = AmdLateParams.DmiTable;
502   AcpiPstate     = AmdLateParams.AcpiPState;
503   AcpiSrat       = AmdLateParams.AcpiSrat;
504   AcpiSlit       = AmdLateParams.AcpiSlit;
505
506   AcpiWheaMce    = AmdLateParams.AcpiWheaMce;
507   AcpiWheaCmc    = AmdLateParams.AcpiWheaCmc;
508   AcpiAlib       = AmdLateParams.AcpiAlib;
509
510   return (UINT32)Status;
511 }
512
513 UINT32 
514 agesawrapper_amdreadeventlog (
515   VOID
516   )
517 {
518   AGESA_STATUS Status;
519   EVENT_PARAMS AmdEventParams;
520
521   LibAmdMemFill (&AmdEventParams,
522                  0,
523                  sizeof (EVENT_PARAMS),
524                  &(AmdEventParams.StdHeader));
525
526   AmdEventParams.StdHeader.AltImageBasePtr = 0;
527   AmdEventParams.StdHeader.CalloutPtr = NULL;
528   AmdEventParams.StdHeader.Func = 0;
529   AmdEventParams.StdHeader.ImageBasePtr = 0;
530   Status = AmdReadEventLog (&AmdEventParams);
531   while (AmdEventParams.EventClass != 0) {
532     printk(BIOS_DEBUG,"\nEventLog:  EventClass = %x, EventInfo = %x.\n",AmdEventParams.EventClass,AmdEventParams.EventInfo);
533     printk(BIOS_DEBUG,"  Param1 = %x, Param2 = %x.\n",AmdEventParams.DataParam1,AmdEventParams.DataParam2);
534     printk(BIOS_DEBUG,"  Param3 = %x, Param4 = %x.\n",AmdEventParams.DataParam3,AmdEventParams.DataParam4);
535     Status = AmdReadEventLog (&AmdEventParams);
536   }
537
538   return (UINT32)Status;
539 }