Fix Fam14 mainboard whitespace
[coreboot.git] / src / mainboard / asrock / e350m1 / 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
48 /* ACPI table pointers returned by AmdInitLate */
49 VOID *DmiTable          = NULL;
50 VOID *AcpiPstate        = NULL;
51 VOID *AcpiSrat          = NULL;
52 VOID *AcpiSlit          = NULL;
53
54 VOID *AcpiWheaMce       = NULL;
55 VOID *AcpiWheaCmc       = NULL;
56 VOID *AcpiAlib          = NULL;
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         Status = AGESA_SUCCESS;
161         return (UINT32)Status;
162 }
163
164 UINT32
165 agesawrapper_amdinitreset (
166         VOID
167         )
168 {
169         AGESA_STATUS status;
170         AMD_INTERFACE_PARAMS AmdParamStruct;
171         AMD_RESET_PARAMS AmdResetParams;
172
173         LibAmdMemFill (&AmdParamStruct,
174                                         0,
175                                         sizeof (AMD_INTERFACE_PARAMS),
176                                         &(AmdParamStruct.StdHeader));
177
178         LibAmdMemFill (&AmdResetParams,
179                                         0,
180                                         sizeof (AMD_RESET_PARAMS),
181                                         &(AmdResetParams.StdHeader));
182
183         AmdParamStruct.AgesaFunctionName = AMD_INIT_RESET;
184         AmdParamStruct.AllocationMethod = ByHost;
185         AmdParamStruct.NewStructSize = sizeof(AMD_RESET_PARAMS);
186         AmdParamStruct.NewStructPtr = &AmdResetParams;
187         AmdParamStruct.StdHeader.AltImageBasePtr = 0;
188         AmdParamStruct.StdHeader.CalloutPtr = NULL;
189         AmdParamStruct.StdHeader.Func = 0;
190         AmdParamStruct.StdHeader.ImageBasePtr = 0;
191         AmdCreateStruct (&AmdParamStruct);
192         AmdResetParams.HtConfig.Depth = 0;
193
194         status = AmdInitReset ((AMD_RESET_PARAMS *)AmdParamStruct.NewStructPtr);
195         if (status != AGESA_SUCCESS) agesawrapper_amdreadeventlog();
196         AmdReleaseStruct (&AmdParamStruct);
197         return (UINT32)status;
198  }
199
200 UINT32
201 agesawrapper_amdinitearly (
202         VOID
203         )
204 {
205         AGESA_STATUS status;
206         AMD_INTERFACE_PARAMS AmdParamStruct;
207         AMD_EARLY_PARAMS                 *AmdEarlyParamsPtr;
208
209         LibAmdMemFill (&AmdParamStruct,
210                                         0,
211                                         sizeof (AMD_INTERFACE_PARAMS),
212                                         &(AmdParamStruct.StdHeader));
213
214         AmdParamStruct.AgesaFunctionName = AMD_INIT_EARLY;
215         AmdParamStruct.AllocationMethod = PreMemHeap;
216         AmdParamStruct.StdHeader.AltImageBasePtr = 0;
217         AmdParamStruct.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
218         AmdParamStruct.StdHeader.Func = 0;
219         AmdParamStruct.StdHeader.ImageBasePtr = 0;
220         AmdCreateStruct (&AmdParamStruct);
221
222         AmdEarlyParamsPtr = (AMD_EARLY_PARAMS *)AmdParamStruct.NewStructPtr;
223         OemCustomizeInitEarly (AmdEarlyParamsPtr);
224
225         status = AmdInitEarly ((AMD_EARLY_PARAMS *)AmdParamStruct.NewStructPtr);
226         if (status != AGESA_SUCCESS) agesawrapper_amdreadeventlog();
227         AmdReleaseStruct (&AmdParamStruct);
228
229         return (UINT32)status;
230 }
231
232 UINT32
233 agesawrapper_amdinitpost (
234         VOID
235         )
236 {
237         AGESA_STATUS status;
238         UINT16                                  i;
239         UINT32                                  *HeadPtr;
240         AMD_INTERFACE_PARAMS    AmdParamStruct;
241         BIOS_HEAP_MANAGER               *BiosManagerPtr;
242
243         LibAmdMemFill (&AmdParamStruct,
244                                         0,
245                                         sizeof (AMD_INTERFACE_PARAMS),
246                                         &(AmdParamStruct.StdHeader));
247
248         AmdParamStruct.AgesaFunctionName = AMD_INIT_POST;
249         AmdParamStruct.AllocationMethod = PreMemHeap;
250         AmdParamStruct.StdHeader.AltImageBasePtr = 0;
251         AmdParamStruct.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
252         AmdParamStruct.StdHeader.Func = 0;
253         AmdParamStruct.StdHeader.ImageBasePtr = 0;
254
255         AmdCreateStruct (&AmdParamStruct);
256         status = AmdInitPost ((AMD_POST_PARAMS *)AmdParamStruct.NewStructPtr);
257         if (status != AGESA_SUCCESS) agesawrapper_amdreadeventlog();
258         AmdReleaseStruct (&AmdParamStruct);
259
260         /* Initialize heap space */
261         BiosManagerPtr = (BIOS_HEAP_MANAGER *)BIOS_HEAP_START_ADDRESS;
262
263         HeadPtr = (UINT32 *) ((UINT8 *) BiosManagerPtr + sizeof (BIOS_HEAP_MANAGER));
264         for (i = 0; i < ((BIOS_HEAP_SIZE/4) - (sizeof (BIOS_HEAP_MANAGER)/4)); i++)
265         {
266         *HeadPtr = 0x00000000;
267         HeadPtr++;
268         }
269         BiosManagerPtr->StartOfAllocatedNodes = 0;
270         BiosManagerPtr->StartOfFreedNodes = 0;
271
272         return (UINT32)status;
273 }
274
275 UINT32
276 agesawrapper_amdinitenv (
277         VOID
278         )
279 {
280         AGESA_STATUS status;
281         AMD_INTERFACE_PARAMS AmdParamStruct;
282         PCI_ADDR                         PciAddress;
283         UINT32                           PciValue;
284
285         LibAmdMemFill (&AmdParamStruct,
286                                         0,
287                                         sizeof (AMD_INTERFACE_PARAMS),
288                                         &(AmdParamStruct.StdHeader));
289
290         AmdParamStruct.AgesaFunctionName = AMD_INIT_ENV;
291         AmdParamStruct.AllocationMethod = PostMemDram;
292         AmdParamStruct.StdHeader.AltImageBasePtr = 0;
293         AmdParamStruct.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
294         AmdParamStruct.StdHeader.Func = 0;
295         AmdParamStruct.StdHeader.ImageBasePtr = 0;
296         AmdCreateStruct (&AmdParamStruct);
297         status = AmdInitEnv ((AMD_ENV_PARAMS *)AmdParamStruct.NewStructPtr);
298         if (status != AGESA_SUCCESS) agesawrapper_amdreadeventlog();
299         /* Initialize Subordinate Bus Number and Secondary Bus Number
300          * In platform BIOS this address is allocated by PCI enumeration code
301                  Modify D1F0x18
302          */
303         PciAddress.Address.Bus = 0;
304         PciAddress.Address.Device = 1;
305         PciAddress.Address.Function = 0;
306         PciAddress.Address.Register = 0x18;
307         /* Write to D1F0x18 */
308         LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
309         PciValue |= 0x00010100;
310         LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
311
312         /* Initialize GMM Base Address for Legacy Bridge Mode
313         *       Modify B1D5F0x18
314         */
315         PciAddress.Address.Bus = 1;
316         PciAddress.Address.Device = 5;
317         PciAddress.Address.Function = 0;
318         PciAddress.Address.Register = 0x18;
319
320         LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
321         PciValue |= 0x96000000;
322         LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
323
324         /* Initialize FB Base Address for Legacy Bridge Mode
325         * Modify B1D5F0x10
326         */
327         PciAddress.Address.Register = 0x10;
328         LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
329         PciValue |= 0x80000000;
330         LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
331
332         /* Initialize GMM Base Address for Pcie Mode
333         *       Modify B0D1F0x18
334         */
335         PciAddress.Address.Bus = 0;
336         PciAddress.Address.Device = 1;
337         PciAddress.Address.Function = 0;
338         PciAddress.Address.Register = 0x18;
339
340         LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
341         PciValue |= 0x96000000;
342         LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
343
344         /* Initialize FB Base Address for Pcie Mode
345         *       Modify B0D1F0x10
346         */
347         PciAddress.Address.Register = 0x10;
348         LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
349         PciValue |= 0x80000000;
350         LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
351
352         /* Initialize MMIO Base and Limit Address
353         *       Modify B0D1F0x20
354         */
355         PciAddress.Address.Bus = 0;
356         PciAddress.Address.Device = 1;
357         PciAddress.Address.Function = 0;
358         PciAddress.Address.Register = 0x20;
359
360         LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
361         PciValue |= 0x96009600;
362         LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
363
364         /* Initialize MMIO Prefetchable Memory Limit and Base
365         *       Modify B0D1F0x24
366         */
367         PciAddress.Address.Register = 0x24;
368         LibAmdPciRead (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
369         PciValue |= 0x8FF18001;
370         LibAmdPciWrite (AccessWidth32, PciAddress, &PciValue, &AmdParamStruct.StdHeader);
371         AmdReleaseStruct (&AmdParamStruct);
372
373         return (UINT32)status;
374 }
375
376 VOID *
377 agesawrapper_getlateinitptr (
378         int pick
379         )
380 {
381         switch (pick) {
382                 case PICK_DMI:
383                         return DmiTable;
384                 case PICK_PSTATE:
385                         return AcpiPstate;
386                 case PICK_SRAT:
387                         return AcpiSrat;
388                 case PICK_SLIT:
389                         return AcpiSlit;
390                 case PICK_WHEA_MCE:
391                         return AcpiWheaMce;
392                 case PICK_WHEA_CMC:
393                         return AcpiWheaCmc;
394                 case PICK_ALIB:
395                         return AcpiAlib;
396                 default:
397                         return NULL;
398         }
399 }
400
401 UINT32
402 agesawrapper_amdinitmid (
403         VOID
404         )
405 {
406         AGESA_STATUS status;
407         AMD_INTERFACE_PARAMS AmdParamStruct;
408
409         /* Enable MMIO on AMD CPU Address Map Controller */
410         agesawrapper_amdinitcpuio ();
411
412         LibAmdMemFill (&AmdParamStruct,
413                                         0,
414                                         sizeof (AMD_INTERFACE_PARAMS),
415                                         &(AmdParamStruct.StdHeader));
416
417         AmdParamStruct.AgesaFunctionName = AMD_INIT_MID;
418         AmdParamStruct.AllocationMethod = PostMemDram;
419         AmdParamStruct.StdHeader.AltImageBasePtr = 0;
420         AmdParamStruct.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
421         AmdParamStruct.StdHeader.Func = 0;
422         AmdParamStruct.StdHeader.ImageBasePtr = 0;
423
424         AmdCreateStruct (&AmdParamStruct);
425
426         status = AmdInitMid ((AMD_MID_PARAMS *)AmdParamStruct.NewStructPtr);
427         if (status != AGESA_SUCCESS) agesawrapper_amdreadeventlog();
428         AmdReleaseStruct (&AmdParamStruct);
429
430         return (UINT32)status;
431 }
432
433 UINT32
434 agesawrapper_amdinitlate (
435         VOID
436         )
437 {
438         AGESA_STATUS Status;
439         AMD_LATE_PARAMS AmdLateParams;
440
441         LibAmdMemFill (&AmdLateParams,
442                                         0,
443                                         sizeof (AMD_LATE_PARAMS),
444                                         &(AmdLateParams.StdHeader));
445
446         AmdLateParams.StdHeader.AltImageBasePtr = 0;
447         AmdLateParams.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
448         AmdLateParams.StdHeader.Func = 0;
449         AmdLateParams.StdHeader.ImageBasePtr = 0;
450
451         Status = AmdInitLate (&AmdLateParams);
452         if (Status != AGESA_SUCCESS) {
453                 agesawrapper_amdreadeventlog();
454                 ASSERT(Status == AGESA_SUCCESS);
455         }
456
457         DmiTable                = AmdLateParams.DmiTable;
458         AcpiPstate              = AmdLateParams.AcpiPState;
459         AcpiSrat                = AmdLateParams.AcpiSrat;
460         AcpiSlit                = AmdLateParams.AcpiSlit;
461
462         AcpiWheaMce             = AmdLateParams.AcpiWheaMce;
463         AcpiWheaCmc             = AmdLateParams.AcpiWheaCmc;
464         AcpiAlib                = AmdLateParams.AcpiAlib;
465
466         return (UINT32)Status;
467 }
468
469 UINT32
470 agesawrapper_amdlaterunaptask (
471         UINT32 Func,
472         UINT32 Data,
473         VOID *ConfigPtr
474         )
475 {
476         AGESA_STATUS Status;
477         AP_EXE_PARAMS ApExeParams;
478
479         LibAmdMemFill (&ApExeParams,
480                                         0,
481                                         sizeof (AP_EXE_PARAMS),
482                                         &(ApExeParams.StdHeader));
483
484         ApExeParams.StdHeader.AltImageBasePtr = 0;
485         ApExeParams.StdHeader.CalloutPtr = (CALLOUT_ENTRY) &GetBiosCallout;
486         ApExeParams.StdHeader.Func = 0;
487         ApExeParams.StdHeader.ImageBasePtr = 0;
488         ApExeParams.StdHeader.ImageBasePtr = 0;
489         ApExeParams.FunctionNumber = Func;
490         ApExeParams.RelatedDataBlock = ConfigPtr;
491
492         Status = AmdLateRunApTask (&ApExeParams);
493         if (Status != AGESA_SUCCESS) {
494                 agesawrapper_amdreadeventlog();
495                 ASSERT(Status == AGESA_SUCCESS);
496         }
497
498         return (UINT32)Status;
499 }
500
501 UINT32
502 agesawrapper_amdreadeventlog (
503         VOID
504         )
505 {
506         AGESA_STATUS Status;
507         EVENT_PARAMS AmdEventParams;
508
509         LibAmdMemFill (&AmdEventParams,
510                                         0,
511                                         sizeof (EVENT_PARAMS),
512                                         &(AmdEventParams.StdHeader));
513
514         AmdEventParams.StdHeader.AltImageBasePtr = 0;
515         AmdEventParams.StdHeader.CalloutPtr = NULL;
516         AmdEventParams.StdHeader.Func = 0;
517         AmdEventParams.StdHeader.ImageBasePtr = 0;
518         Status = AmdReadEventLog (&AmdEventParams);
519         while (AmdEventParams.EventClass != 0) {
520         printk(BIOS_DEBUG,"\nEventLog:  EventClass = %x, EventInfo = %x.\n",AmdEventParams.EventClass,AmdEventParams.EventInfo);
521         printk(BIOS_DEBUG,"  Param1 = %x, Param2 = %x.\n",AmdEventParams.DataParam1,AmdEventParams.DataParam2);
522         printk(BIOS_DEBUG,"  Param3 = %x, Param4 = %x.\n",AmdEventParams.DataParam3,AmdEventParams.DataParam4);
523         Status = AmdReadEventLog (&AmdEventParams);
524         }
525
526         return (UINT32)Status;
527 }