Add constants for fast path resume copying
[coreboot.git] / src / vendorcode / amd / cimx / sb800 / DISPATCHER.c
1 /**
2  * @file
3  *
4  *  Function dispatcher.
5  *
6  *
7  *
8  * @xrefitem bom "File Content Label" "Release Content"
9  * @e project:      CIMx-SB
10  * @e sub-project:
11  * @e \$Revision:$   @e \$Date:$
12  *
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 /*----------------------------------------------------------------------------------------
47  *                             M O D U L E S    U S E D
48  *----------------------------------------------------------------------------------------
49  */
50
51 #include "SBPLATFORM.h"
52 #include "cbtypes.h"
53
54 /*----------------------------------------------------------------------------------------
55  *           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
56  *----------------------------------------------------------------------------------------
57 */
58
59
60 //
61 // Declaration of local functions
62 //
63
64 VOID  saveConfigPointer (IN AMDSBCFG* pConfig);
65 VOID* VerifyImage (IN UINT64 Signature, IN VOID* ImagePtr);
66 VOID* LocateImage (IN UINT64 Signature);
67
68 /*----------------------------------------------------------------------------------------
69  *                  T Y P E D E F S     A N D     S T R U C T U  R E S
70  *----------------------------------------------------------------------------------------
71  */
72
73 /*----------------------------------------------------------------------------------------
74  *                          E X P O R T E D    F U N C T I O N S
75  *----------------------------------------------------------------------------------------
76  */
77
78 /**
79  * AmdSbDispatcher - Dispatch Southbridge function
80  *
81  *
82  *
83  * @param[in] pConfig   Southbridge configuration structure pointer.
84  *
85  */
86 AGESA_STATUS
87 AmdSbDispatcher (
88   IN       VOID *pConfig
89   )
90 {
91   AGESA_STATUS Status;
92
93 #ifdef B1_IMAGE
94   VOID *pAltImagePtr;
95   CIM_IMAGE_ENTRY   AltImageEntry;
96 #endif
97
98   UINT64  tdValue;
99   tdValue = 0x32314130384253ULL;
100
101 #ifdef B1_IMAGE
102   pAltImagePtr = NULL;
103 #endif
104   Status = AGESA_UNSUPPORTED;
105
106 #ifdef B1_IMAGE
107   if ((UINT32) (UINTN) (((AMD_CONFIG_PARAMS*)pConfig)->AltImageBasePtr) != 0xffffffff ) {
108     if ( ((AMD_CONFIG_PARAMS*)pConfig)->AltImageBasePtr ) {
109       pAltImagePtr = VerifyImage ( tdValue, (VOID*) (UINTN) ((AMD_CONFIG_PARAMS*)pConfig)->AltImageBasePtr);
110     }
111     if ( pAltImagePtr == NULL ) {
112       pAltImagePtr = LocateImage ( tdValue );
113     }
114     if ( pAltImagePtr != NULL ) {
115       ((AMD_CONFIG_PARAMS*)pConfig)->ImageBasePtr = (UINT32) (UINTN) pAltImagePtr;
116       AltImageEntry = (CIM_IMAGE_ENTRY) (UINTN) ((UINT32) (UINTN) pAltImagePtr + (UINT32) (((AMD_IMAGE_HEADER*) (UINTN) pAltImagePtr)->EntryPointAddress));
117       (*AltImageEntry) (pConfig);
118       return Status;
119     }
120   }
121 #endif
122   saveConfigPointer (pConfig);
123
124   if ( ((AMD_CONFIG_PARAMS*)pConfig)->Func == SB_POWERON_INIT ) {
125     sbPowerOnInit ((AMDSBCFG*) pConfig);
126   }
127
128 #ifndef B1_IMAGE
129   if ( ((AMD_CONFIG_PARAMS*)pConfig)->Func == SB_BEFORE_PCI_INIT ) {
130     sbBeforePciInit ((AMDSBCFG*)pConfig);
131   }
132
133   if ( ((AMD_CONFIG_PARAMS*)pConfig)->Func == SB_AFTER_PCI_INIT ) {
134     sbAfterPciInit ((AMDSBCFG*)pConfig);
135   }
136
137   if ( ((AMD_CONFIG_PARAMS*)pConfig)->Func == SB_MID_POST_INIT ) {
138     sbMidPostInit ((AMDSBCFG*)pConfig);
139   }
140
141   if ( ((AMD_CONFIG_PARAMS*)pConfig)->Func == SB_LATE_POST_INIT ) {
142     sbLatePost ((AMDSBCFG*)pConfig);
143   }
144
145   if ( ((AMD_CONFIG_PARAMS*)pConfig)->Func == SB_BEFORE_PCI_RESTORE_INIT ) {
146     sbBeforePciRestoreInit ((AMDSBCFG*)pConfig);
147   }
148
149   if ( ((AMD_CONFIG_PARAMS*)pConfig)->Func == SB_AFTER_PCI_RESTORE_INIT ) {
150     sbAfterPciRestoreInit ((AMDSBCFG*)pConfig);
151   }
152
153   if ( ((AMD_CONFIG_PARAMS*)pConfig)->Func == SB_SMM_SERVICE ) {
154     sbSmmService ((AMDSBCFG*)pConfig);
155   }
156
157   if ( ((AMD_CONFIG_PARAMS*)pConfig)->Func == SB_SMM_ACPION ) {
158     sbSmmAcpiOn ((AMDSBCFG*)pConfig);
159   }
160
161   if ( ((AMD_CONFIG_PARAMS*)pConfig)->Func == SB_EC_FANCONTROL ) {
162     sbECfancontrolservice((AMDSBCFG*)pConfig);;
163   }
164 #endif
165   return Status;
166 }
167
168 /**
169  * LocateImage - Locate Southbridge CIMx module
170  *
171  *
172  *
173  * @param[in] Signature   Southbridge CIMx image signature.
174  *
175  */
176 VOID*
177 LocateImage (
178   IN       UINT64 Signature
179   )
180 {
181   VOID  *Result;
182   UINT32   ImagePtr;
183   ImagePtr = 0xffffffff - (IMAGE_ALIGN - 1);
184
185   while ( ImagePtr >= (0xfffffff - (NUM_IMAGE_LOCATION * IMAGE_ALIGN - 1)) ) {
186 #ifdef x64
187     12346789
188 #else
189     Result = VerifyImage (Signature, (VOID*) ImagePtr);
190 #endif
191     if ( Result != NULL ) {
192       return Result;
193     }
194     ImagePtr -= IMAGE_ALIGN;
195   }
196   return NULL;
197 }
198
199 /**
200  * VerifyImage - Verify Southbridge CIMx module
201  *
202  *
203  * @param[in] Signature   Southbridge CIMx image signature.
204  * @param[in] ImagePtr    Southbridge CIMx image address.
205  *
206  */
207 VOID*
208 VerifyImage (
209   IN       UINT64 Signature,
210   IN       VOID* ImagePtr
211   )
212 {
213   UINT16 *TempImagePtr;
214   UINT16 Sum;
215   UINT32  i;
216   Sum = 0;
217   if ( (*((UINT32*)ImagePtr) == Int32FromChar('$', 'A', 'M', 'D') && ((CIMFILEHEADER*)ImagePtr)->CreatorID == Signature) ) {
218     //GetImage Image size
219     TempImagePtr = (UINT16*)ImagePtr;
220     for ( i = 0; i < (((CIMFILEHEADER*)ImagePtr)->ImageSize); i += 2 ) {
221       Sum = Sum + *TempImagePtr;
222       TempImagePtr++;
223     }
224     if ( Sum == 0 ) {
225       return ImagePtr;
226     }
227   }
228   return NULL;
229 }
230
231 /**
232  * saveConfigPointer - Verify Southbridge CIMx module
233  *
234  *
235  * @param[in] pConfig   Southbridge configuration structure pointer.
236  *
237  */
238 VOID
239 saveConfigPointer (
240   IN       AMDSBCFG* pConfig
241   )
242 {
243   UINT8   dbReg;
244   UINT8   i;
245   UINT32   ddValue;
246
247   ddValue =  (UINT32) (UINTN)pConfig;
248   dbReg = SB_ECMOS_REG08;
249
250   for ( i = 0; i <= 3; i++ ) {
251     WriteIO (SB_IOMAP_REG72, AccWidthUint8, &dbReg);
252     WriteIO (SB_IOMAP_REG73, AccWidthUint8, (UINT8*)&ddValue);
253     ddValue = (ddValue >> 8);
254     dbReg++;
255   }
256 }