AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / Proc / CPU / Feature / PreserveMailbox.c
1 /* $NoKeywords:$ */
2 /**
3  * @file
4  *
5  * AMD AGESA CPU Preserve Registers used for AP Mailbox.
6  *
7  * Save and Restore the normal feature content of the registers being used for
8  * the AP Mailbox.
9  *
10  * @xrefitem bom "File Content Label" "Release Content"
11  * @e project:      AGESA
12  * @e sub-project:  CPU/Feature
13  * @e \$Revision: 56279 $   @e \$Date: 2011-07-11 13:11:28 -0600 (Mon, 11 Jul 2011) $
14  *
15  */
16 /*
17  ******************************************************************************
18  *
19  * Copyright (C) 2012 Advanced Micro Devices, Inc.
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions are met:
24  *     * Redistributions of source code must retain the above copyright
25  *       notice, this list of conditions and the following disclaimer.
26  *     * Redistributions in binary form must reproduce the above copyright
27  *       notice, this list of conditions and the following disclaimer in the
28  *       documentation and/or other materials provided with the distribution.
29  *     * Neither the name of Advanced Micro Devices, Inc. nor the names of
30  *       its contributors may be used to endorse or promote products derived
31  *       from this software without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
37  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  ******************************************************************************
45  */
46
47 /*----------------------------------------------------------------------------------------
48  *                             M O D U L E S    U S E D
49  *----------------------------------------------------------------------------------------
50  */
51 #include "AGESA.h"
52 #include "amdlib.h"
53 #include "Ids.h"
54 #include "Topology.h"
55 #include "GeneralServices.h"
56 #include "OptionMultiSocket.h"
57 #include "cpuRegisters.h"
58 #include "cpuApicUtilities.h"
59 #include "cpuServices.h"
60 #include "cpuFamilyTranslation.h"
61 #include "cpuFeatures.h"
62 #include "PreserveMailbox.h"
63 #include "heapManager.h"
64 #include "Filecode.h"
65 CODE_GROUP (G1_PEICC)
66 RDATA_GROUP (G2_PEI)
67
68 #define FILECODE PROC_CPU_FEATURE_PRESERVEMAILBOX_FILECODE
69 /*----------------------------------------------------------------------------------------
70  *                   D E F I N I T I O N S    A N D    M A C R O S
71  *----------------------------------------------------------------------------------------
72  */
73
74 /*----------------------------------------------------------------------------------------
75  *                  T Y P E D E F S     A N D     S T R U C T U  R E S
76  *----------------------------------------------------------------------------------------
77  */
78
79 /*----------------------------------------------------------------------------------------
80  *           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
81  *----------------------------------------------------------------------------------------
82  */
83
84 /*----------------------------------------------------------------------------------------
85  *                          E X P O R T E D    F U N C T I O N S
86  *----------------------------------------------------------------------------------------
87  */
88 extern CPU_FAMILY_SUPPORT_TABLE PreserveMailboxFamilyServiceTable;
89
90 /*---------------------------------------------------------------------------------------*/
91 /**
92  *  The contents of the mailbox registers should always be preserved.
93  *
94  * @param[in]    PlatformConfig     Contains the runtime modifiable feature input data.
95  * @param[in]    StdHeader          Config Handle for library, services.
96  *
97  * @retval       TRUE               Always TRUE
98  *
99  */
100 BOOLEAN
101 STATIC
102 IsPreserveAroundMailboxEnabled (
103   IN       PLATFORM_CONFIGURATION *PlatformConfig,
104   IN       AMD_CONFIG_PARAMS      *StdHeader
105   )
106 {
107   return TRUE;
108 }
109
110 /*---------------------------------------------------------------------------------------*/
111 /**
112  *  Save and Restore or Initialize the content of the mailbox registers.
113  *
114  * The registers used for AP mailbox should have the content related to their function
115  * preserved.
116  *
117  * @param[in]    EntryPoint         Timepoint designator.
118  * @param[in]    PlatformConfig     Contains the runtime modifiable feature input data.
119  * @param[in]    StdHeader          Config Handle for library, services.
120  *
121  * @return       AGESA_SUCCESS      Always succeeds.
122  *
123  */
124 AGESA_STATUS
125 STATIC
126 PreserveMailboxes (
127   IN       UINT64                 EntryPoint,
128   IN       PLATFORM_CONFIGURATION *PlatformConfig,
129   IN       AMD_CONFIG_PARAMS      *StdHeader
130   )
131 {
132   PRESERVE_MAILBOX_FAMILY_SERVICES *FamilySpecificServices;
133   UINT32 Socket;
134   UINT32 Module;
135   PCI_ADDR BaseAddress;
136   PCI_ADDR MailboxRegister;
137   PRESERVE_MAILBOX_FAMILY_REGISTER *NextRegister;
138   AGESA_STATUS IgnoredStatus;
139   AGESA_STATUS HeapStatus;
140   UINT32 Value;
141   ALLOCATE_HEAP_PARAMS AllocateParams;
142   LOCATE_HEAP_PTR LocateParams;
143   UINT32 RegisterEntryIndex;
144
145   BaseAddress.AddressValue = ILLEGAL_SBDFO;
146
147   if (EntryPoint == CPU_FEAT_AFTER_COHERENT_DISCOVERY) {
148     // The save step.  Save either the register content or zero (for cold boot, if family specifies that).
149     AllocateParams.BufferHandle = PRESERVE_MAIL_BOX_HANDLE;
150     AllocateParams.RequestedBufferSize = (sizeof (UINT32) * (MAX_PRESERVE_REGISTER_ENTRIES * (MAX_SOCKETS * MAX_DIES)));
151     AllocateParams.Persist = HEAP_SYSTEM_MEM;
152     HeapStatus = HeapAllocateBuffer (&AllocateParams, StdHeader);
153     ASSERT ((HeapStatus == AGESA_SUCCESS) && (AllocateParams.BufferPtr != NULL));
154     LibAmdMemFill (AllocateParams.BufferPtr, 0xFF, AllocateParams.RequestedBufferSize, StdHeader);
155     RegisterEntryIndex = 0;
156     for (Socket = 0; Socket < GetPlatformNumberOfSockets (); Socket++) {
157       for (Module = 0; Module < GetPlatformNumberOfModules (); Module++) {
158         if (GetPciAddress (StdHeader, Socket, Module, &BaseAddress, &IgnoredStatus)) {
159           GetFeatureServicesOfSocket (&PreserveMailboxFamilyServiceTable, Socket, (CONST VOID **)&FamilySpecificServices, StdHeader);
160           ASSERT (FamilySpecificServices != NULL);
161           NextRegister = FamilySpecificServices->RegisterList;
162           while (NextRegister->Register.AddressValue != ILLEGAL_SBDFO) {
163             ASSERT (RegisterEntryIndex <
164                     (MAX_PRESERVE_REGISTER_ENTRIES * GetPlatformNumberOfSockets () * GetPlatformNumberOfModules ()));
165             if (FamilySpecificServices->IsZeroOnCold && (!IsWarmReset (StdHeader))) {
166               Value = 0;
167             } else {
168               MailboxRegister = BaseAddress;
169               MailboxRegister.Address.Function = NextRegister->Register.Address.Function;
170               MailboxRegister.Address.Register = NextRegister->Register.Address.Register;
171               LibAmdPciRead (AccessWidth32, MailboxRegister, &Value, StdHeader);
172               Value &= NextRegister->Mask;
173             }
174             (* (MAILBOX_REGISTER_SAVE_ENTRY) AllocateParams.BufferPtr) [RegisterEntryIndex] = Value;
175             RegisterEntryIndex++;
176             NextRegister++;
177           }
178         }
179       }
180     }
181   } else if ((EntryPoint == CPU_FEAT_INIT_LATE_END) || (EntryPoint == CPU_FEAT_AFTER_RESUME_MTRR_SYNC)) {
182     // The restore step.  Just write out the saved content in the buffer.
183     LocateParams.BufferHandle = PRESERVE_MAIL_BOX_HANDLE;
184     HeapStatus = HeapLocateBuffer (&LocateParams, StdHeader);
185     ASSERT ((HeapStatus == AGESA_SUCCESS) && (LocateParams.BufferPtr != NULL));
186     RegisterEntryIndex = 0;
187     for (Socket = 0; Socket < GetPlatformNumberOfSockets (); Socket++) {
188       for (Module = 0; Module < GetPlatformNumberOfModules (); Module++) {
189         if (GetPciAddress (StdHeader, Socket, Module, &BaseAddress, &IgnoredStatus)) {
190           GetFeatureServicesOfSocket (&PreserveMailboxFamilyServiceTable, Socket, (CONST VOID **)&FamilySpecificServices, StdHeader);
191           NextRegister = FamilySpecificServices->RegisterList;
192           while (NextRegister->Register.AddressValue != ILLEGAL_SBDFO) {
193             ASSERT (RegisterEntryIndex <
194                     (MAX_PRESERVE_REGISTER_ENTRIES * GetPlatformNumberOfSockets () * GetPlatformNumberOfModules ()));
195             MailboxRegister = BaseAddress;
196             MailboxRegister.Address.Function = NextRegister->Register.Address.Function;
197             MailboxRegister.Address.Register = NextRegister->Register.Address.Register;
198             LibAmdPciRead (AccessWidth32, MailboxRegister, &Value, StdHeader);
199             Value = ((Value & ~NextRegister->Mask) | (* (MAILBOX_REGISTER_SAVE_ENTRY) LocateParams.BufferPtr) [RegisterEntryIndex]);
200             LibAmdPciWrite (AccessWidth32, MailboxRegister, &Value, StdHeader);
201             RegisterEntryIndex++;
202             NextRegister++;
203           }
204         }
205       }
206     }
207     HeapStatus = HeapDeallocateBuffer (PRESERVE_MAIL_BOX_HANDLE, StdHeader);
208   }
209   return AGESA_SUCCESS;
210 }
211
212
213 CONST CPU_FEATURE_DESCRIPTOR ROMDATA CpuFeaturePreserveAroundMailbox =
214 {
215   PreserveAroundMailbox,
216   (CPU_FEAT_AFTER_COHERENT_DISCOVERY | CPU_FEAT_INIT_LATE_END | CPU_FEAT_AFTER_RESUME_MTRR_SYNC),
217   IsPreserveAroundMailboxEnabled,
218   PreserveMailboxes
219 };