AMD Agesa macro expansion fix
[coreboot.git] / src / vendorcode / amd / agesa / f12 / Proc / CPU / cpuLateInit.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * AMD CPU Late Init API
6  *
7  * Contains code for doing any late CPU initialization.
8  *
9  * @xrefitem bom "File Content Label" "Release Content"
10  * @e project:      AGESA
11  * @e sub-project:  CPU
12  * @e \$Revision: 44324 $   @e \$Date: 2010-12-22 17:16:51 +0800 (Wed, 22 Dec 2010) $
13  *
14  */
15 /*
16  ******************************************************************************
17  *
18  * Copyright (c) 2011, Advanced Micro Devices, Inc.
19  * All rights reserved.
20  * 
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions are met:
23  *     * Redistributions of source code must retain the above copyright
24  *       notice, this list of conditions and the following disclaimer.
25  *     * Redistributions in binary form must reproduce the above copyright
26  *       notice, this list of conditions and the following disclaimer in the
27  *       documentation and/or other materials provided with the distribution.
28  *     * Neither the name of Advanced Micro Devices, Inc. nor the names of 
29  *       its contributors may be used to endorse or promote products derived 
30  *       from this software without specific prior written permission.
31  * 
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35  * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
36  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  ******************************************************************************
43  */
44
45 /*----------------------------------------------------------------------------------------
46  *                            M O D U L E S    U S E D
47  *----------------------------------------------------------------------------------------
48  */
49 #include "AGESA.h"
50 #include "amdlib.h"
51 #include "Ids.h"
52 #include "cpuLateInit.h"
53 #include "cpuRegisters.h"
54 #include "GeneralServices.h"
55 #include "cpuServices.h"
56 #include "Filecode.h"
57 CODE_GROUP (G3_DXE)
58 RDATA_GROUP (G3_DXE)
59
60 #define FILECODE PROC_CPU_CPULATEINIT_FILECODE
61 /*----------------------------------------------------------------------------------------
62  *                   D E F I N I T I O N S    A N D    M A C R O S
63  *----------------------------------------------------------------------------------------
64  */
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  *           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
73  *----------------------------------------------------------------------------------------
74  */
75 VOID
76 DisableCf8ExtCfg (
77   IN       AMD_CONFIG_PARAMS   *StdHeader
78   );
79 /*----------------------------------------------------------------------------------------
80  *                          E X P O R T E D    F U N C T I O N S
81  *----------------------------------------------------------------------------------------
82  */
83
84
85 /*---------------------------------------------------------------------------------------*/
86 /**
87  * Performs CPU related initialization at the late entry point
88  *
89  * This function should be the last function run by the AGESA
90  * CPU module and prepares the processor for the operating system
91  * bootstrap load process.
92  *
93  * @param[in]  StdHeader         Config handle for library and services
94  *
95  * @retval     AGESA_SUCCESS
96  *
97  */
98 AGESA_STATUS
99 AmdCpuLate (
100   IN       AMD_CONFIG_PARAMS   *StdHeader
101   )
102 {
103   DisableCf8ExtCfg (StdHeader);
104   return (AGESA_SUCCESS);
105 }
106
107 /*---------------------------------------------------------------------------------------*/
108 /**
109  * Clear EnableCf8ExtCfg on all socket
110  *
111  * Clear F3x8C bit 14 EnableCf8ExtCfg
112  *
113  * @param[in]  StdHeader         Config handle for library and services
114  *
115  *
116  */
117 VOID
118 DisableCf8ExtCfg (
119   IN       AMD_CONFIG_PARAMS   *StdHeader
120   )
121 {
122   AGESA_STATUS  AgesaStatus;
123   PCI_ADDR PciAddress;
124   UINT32 Socket;
125   UINT32 Module;
126   UINT32 PciData;
127   UINT32 LegacyPciAccess;
128
129   ASSERT (IsBsp (StdHeader, &AgesaStatus));
130
131   for (Socket = 0; Socket < GetPlatformNumberOfSockets (); Socket++) {
132     for (Module = 0; Module < GetPlatformNumberOfModules (); Module++) {
133       if (GetPciAddress (StdHeader, Socket, Module, &PciAddress, &AgesaStatus)) {
134         PciAddress.Address.Function = FUNC_3;
135         PciAddress.Address.Register = NB_CFG_HIGH_REG;
136         LegacyPciAccess = ((1 << 31) + (PciAddress.Address.Register & 0xFC) + (PciAddress.Address.Function << 8) + (PciAddress.Address.Device << 11) + (PciAddress.Address.Bus << 16) + ((PciAddress.Address.Register & 0xF00) << (24 - 8)));
137         // read from PCI register
138         LibAmdIoWrite (AccessWidth32, IOCF8, &LegacyPciAccess, StdHeader);
139         LibAmdIoRead (AccessWidth32, IOCFC, &PciData, StdHeader);
140         // Disable Cf8ExtCfg
141         PciData &= 0xFFFFBFFF;
142         // write to PCI register
143         LibAmdIoWrite (AccessWidth32, IOCF8, &LegacyPciAccess, StdHeader);
144         LibAmdIoWrite (AccessWidth32, IOCFC, &PciData, StdHeader);
145       }
146     }
147   }
148 }
149
150 /*---------------------------------------------------------------------------------------*/
151 /**
152  * Calculate an ACPI style checksum
153  *
154  * Computes the checksum and stores the value to the checksum
155  * field of the passed in ACPI table's header.
156  *
157  * @param[in]  Table             ACPI table to checksum
158  * @param[in]  StdHeader         Config handle for library and services
159  *
160  */
161 VOID
162 ChecksumAcpiTable (
163   IN OUT   ACPI_TABLE_HEADER *Table,
164   IN       AMD_CONFIG_PARAMS *StdHeader
165   )
166 {
167   UINT8  *BuffTempPtr;
168   UINT8  Checksum;
169   UINT32 BufferOffset;
170
171   Table->Checksum = 0;
172   Checksum = 0;
173   BuffTempPtr = (UINT8 *) Table;
174   for (BufferOffset = 0; BufferOffset < Table->TableLength; BufferOffset++) {
175     Checksum = Checksum - *(BuffTempPtr + BufferOffset);
176   }
177
178   Table->Checksum = Checksum;
179 }
180
181 /*---------------------------------------------------------------------------------------*/
182 /**
183  *
184  * Run code on every AP in the system.
185  *
186  * @param[in] ApParams       AP task pointer.
187  * @param[in] StdHeader      Handle to config for library and services
188  *
189  * @return    The most severe AGESA_STATUS returned by an AP.
190  *
191  */
192 AGESA_STATUS
193 RunLateApTaskOnAllAPs (
194   IN       AP_EXE_PARAMS     *ApParams,
195   IN       AMD_CONFIG_PARAMS *StdHeader
196   )
197 {
198   UINT32                  NumberOfSockets;
199   UINT32                  NumberOfCores;
200   UINT8                   Socket;
201   UINT8                   Core;
202   UINT8                   ApicId;
203   UINT32                  BscSocket;
204   UINT32                  Ignored;
205   UINT32                  BscCoreNum;
206   AGESA_STATUS            CalledStatus;
207   AGESA_STATUS            IgnoredStatus;
208   AGESA_STATUS            AgesaStatus;
209
210   ASSERT (IsBsp (StdHeader, &IgnoredStatus));
211
212   AgesaStatus = AGESA_SUCCESS;
213
214   IdentifyCore (StdHeader, &BscSocket, &Ignored, &BscCoreNum, &IgnoredStatus);
215   NumberOfSockets = GetPlatformNumberOfSockets ();
216
217   for (Socket = 0; Socket < NumberOfSockets; Socket++) {
218     if (GetActiveCoresInGivenSocket (Socket, &NumberOfCores, StdHeader)) {
219       for (Core = 0; Core < NumberOfCores; Core++) {
220         if ((Socket != BscSocket) || (Core != BscCoreNum)) {
221           GetApicId (StdHeader, Socket, Core, &ApicId, &IgnoredStatus);
222           AGESA_TESTPOINT (TpIfBeforeRunApFromAllAps, StdHeader);
223           CalledStatus = AgesaRunFcnOnAp ((UINTN) ApicId, ApParams);
224           AGESA_TESTPOINT (TpIfAfterRunApFromAllAps, StdHeader);
225           if (CalledStatus > AgesaStatus) {
226             AgesaStatus = CalledStatus;
227           }
228         }
229       }
230     }
231   }
232   return AgesaStatus;
233 }
234
235
236 /*---------------------------------------------------------------------------------------*/
237 /**
238  *
239  * Run code on core 0 of every socket in the system.
240  *
241  * @param[in] ApParams       AP task pointer.
242  * @param[in] StdHeader      Handle to config for library and services
243  *
244  * @return    The most severe AGESA_STATUS returned by an AP.
245  *
246  */
247 AGESA_STATUS
248 RunLateApTaskOnAllCore0s (
249   IN       AP_EXE_PARAMS     *ApParams,
250   IN       AMD_CONFIG_PARAMS *StdHeader
251   )
252 {
253   UINT32                  NumberOfSockets;
254   UINT8                   Socket;
255   UINT8                   ApicId;
256   UINT32                  BscSocket;
257   UINT32                  IgnoredModule;
258   UINT32                  IgnoredCore;
259   AGESA_STATUS            CalledStatus;
260   AGESA_STATUS            IgnoredStatus;
261   AGESA_STATUS            AgesaStatus;
262
263   ASSERT (IsBsp (StdHeader, &IgnoredStatus));
264
265   AgesaStatus = AGESA_SUCCESS;
266
267   IdentifyCore (StdHeader, &BscSocket, &IgnoredModule, &IgnoredCore, &IgnoredStatus);
268   NumberOfSockets = GetPlatformNumberOfSockets ();
269
270   for (Socket = 0; Socket < NumberOfSockets; Socket++) {
271     if (IsProcessorPresent (Socket, StdHeader)) {
272       if (Socket != BscSocket) {
273         GetApicId (StdHeader, Socket, 0, &ApicId, &IgnoredStatus);
274         AGESA_TESTPOINT (TpIfBeforeRunApFromAllCore0s, StdHeader);
275         CalledStatus = AgesaRunFcnOnAp ((UINTN) ApicId, ApParams);
276         AGESA_TESTPOINT (TpIfAfterRunApFromAllCore0s, StdHeader);
277         if (CalledStatus > AgesaStatus) {
278           AgesaStatus = CalledStatus;
279         }
280       }
281     }
282   }
283   return AgesaStatus;
284 }