1 /*==============================================================================
2 Copyright(c) 2017 Intel Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files(the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and / or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 OTHER DEALINGS IN THE SOFTWARE.
21 ============================================================================*/
22
23 #include "Internal/Common/GmmLibInc.h"
24 #include "External/Common/GmmCachePolicy.h"
25 #include "External/Common/CachePolicy/GmmCachePolicyGen10.h"
26 #include "External/Common/CachePolicy/GmmCachePolicyGen11.h"
27
28 //=============================================================================
29 //
30 // Function: IsSpecialMOCSUsage
31 //
32 // Desc: This function returns special(hw-reserved) MocsIdx based on usage
33 //
34 // Parameters: usage -> Resource usage type
35 // UpdateMOCS -> True if MOCS Table must be updated, ow false
36 //
37 // Return: int32_t
38 //
39 //-----------------------------------------------------------------------------
IsSpecialMOCSUsage(GMM_RESOURCE_USAGE_TYPE Usage,bool & UpdateMOCS)40 int32_t GmmLib::GmmGen11CachePolicy::IsSpecialMOCSUsage(GMM_RESOURCE_USAGE_TYPE Usage, bool &UpdateMOCS)
41 {
42 int32_t MocsIdx = -1;
43 UpdateMOCS = true;
44
45 switch(Usage)
46 {
47 case GMM_RESOURCE_USAGE_MOCS_62:
48 __GMM_ASSERT(pCachePolicy[Usage].L3 == 0); //Architecturally, TR/Aux-TT node isn't L3-cacheable.
49 pCachePolicy[Usage].L3 = 0;
50 MocsIdx = 62;
51 break;
52 case GMM_RESOURCE_USAGE_L3_EVICTION:
53 pCachePolicy[Usage].L3 = 0;
54 MocsIdx = 63;
55 break;
56 default:
57 UpdateMOCS = false;
58 break;
59 }
60
61 return MocsIdx;
62 }
63
64 //=============================================================================
65 //
66 // Function: __GmmGen11InitCachePolicy
67 //
68 // Desc: This function initializes the cache policy
69 //
70 // Parameters: pCachePolicy -> Ptr to array to be populated with the
71 // mapping of usages -> cache settings.
72 //
73 // Return: GMM_STATUS
74 //
75 //-----------------------------------------------------------------------------
InitCachePolicy()76 GMM_STATUS GmmLib::GmmGen11CachePolicy::InitCachePolicy()
77 {
78 __GMM_ASSERTPTR(pCachePolicy, GMM_ERROR);
79
80 #define DEFINE_CACHE_ELEMENT(usage, llc, ellc, l3, wt, age, aom, lecc_scc, l3_scc, scf, sso, cos) DEFINE_CP_ELEMENT(usage, llc, ellc, l3, wt, age, aom, lecc_scc, l3_scc, scf, sso, cos, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
81 #include "GmmGen11CachePolicy.h"
82
83 #define TC_LLC (1)
84 #define TC_ELLC (0) //Is this supported anymore in TargetCache?
85 #define TC_LLC_ELLC (2)
86
87 #define LeCC_UNCACHEABLE (0x1)
88 #define LeCC_WT_CACHEABLE (0x2) //Only used as MemPushWRite disqualifier if set along with eLLC-only
89 #define LeCC_WB_CACHEABLE (0x3)
90
91 #define L3_UNCACHEABLE (0x1)
92 #define L3_WB_CACHEABLE (0x3)
93
94 #define DISABLE_SKIP_CACHING_CONTROL (0x0)
95 #define ENABLE_SKIP_CACHING_CONTROL (0x1)
96
97 #define DISABLE_SELF_SNOOP_OVERRIDE (0x0)
98 #define ENABLE_SELF_SNOOP_OVERRIDE (0x1)
99 #define ENABLE_SELF_SNOOP_ALWAYS (0x3)
100 #define CLASS_SERVICE_ZERO (0x0)
101
102 // Setup Static MOCS Table
103 {
104 this->SetUpMOCSTable();
105 }
106
107 {
108 // Define index of cache element
109 uint32_t Usage = 0;
110 uint32_t CurrentMaxSpecialIndex = GMM_GEN9_MAX_NUMBER_MOCS_INDEXES - 1;
111
112 #if(_WIN32 && (_DEBUG || _RELEASE_INTERNAL))
113 OverrideCachePolicy();
114 #endif
115
116 // Process the cache policy and fill in the look up table
117 for(; Usage < GMM_RESOURCE_USAGE_MAX; Usage++)
118 {
119 bool CachePolicyError = false;
120 bool SpecialMOCS = false;
121 int32_t CPTblIdx = -1;
122 uint32_t j = 0;
123 uint64_t PTEValue = 0;
124 GMM_CACHE_POLICY_TBL_ELEMENT UsageEle = {0};
125
126 CPTblIdx = IsSpecialMOCSUsage((GMM_RESOURCE_USAGE_TYPE)Usage, SpecialMOCS);
127
128 UsageEle.LeCC.Reserved = 0; // Reserved bits zeroe'd, this is so we
129 // we can compare the unioned LeCC.DwordValue.
130 UsageEle.LeCC.SelfSnoop = DISABLE_SELF_SNOOP_OVERRIDE;
131 UsageEle.LeCC.CoS = CLASS_SERVICE_ZERO;
132 UsageEle.LeCC.SCC = 0;
133 UsageEle.LeCC.ESC = 0;
134
135 if(pCachePolicy[Usage].SCF && pGmmLibContext->GetSkuTable().FtrLLCBypass)
136 {
137 UsageEle.LeCC.SCF = pCachePolicy[Usage].SCF;
138 }
139
140 if(pCachePolicy[Usage].SSO & ENABLE_SELF_SNOOP_OVERRIDE)
141 {
142 UsageEle.LeCC.SelfSnoop = pCachePolicy[Usage].SSO & ENABLE_SELF_SNOOP_ALWAYS;
143 }
144 if(pCachePolicy[Usage].CoS)
145 {
146 UsageEle.LeCC.CoS = pCachePolicy[Usage].CoS;
147 }
148 if(pCachePolicy[Usage].LeCC_SCC)
149 {
150 UsageEle.LeCC.SCC = pCachePolicy[Usage].LeCC_SCC;
151 UsageEle.LeCC.ESC = ENABLE_SKIP_CACHING_CONTROL;
152 }
153 UsageEle.LeCC.LRUM = pCachePolicy[Usage].AGE;
154
155 // default to LLC target cache.
156 UsageEle.LeCC.TargetCache = TC_LLC;
157 UsageEle.LeCC.Cacheability = LeCC_WB_CACHEABLE;
158 if(pCachePolicy[Usage].LLC)
159 {
160 UsageEle.LeCC.TargetCache = TC_LLC;
161 }
162 else
163 {
164 UsageEle.LeCC.Cacheability = LeCC_UNCACHEABLE;
165 }
166
167 UsageEle.L3.Reserved = 0; // Reserved bits zeroe'd, this is so we
168 // we can compare the unioned L3.UshortValue.
169 UsageEle.L3.ESC = DISABLE_SKIP_CACHING_CONTROL;
170 UsageEle.L3.SCC = 0;
171 UsageEle.L3.Cacheability = pCachePolicy[Usage].L3 ? L3_WB_CACHEABLE : L3_UNCACHEABLE;
172
173 if(pCachePolicy[Usage].L3_SCC)
174 {
175 UsageEle.L3.ESC = ENABLE_SKIP_CACHING_CONTROL;
176 UsageEle.L3.SCC = (uint16_t)pCachePolicy[Usage].L3_SCC;
177 }
178
179 if(CPTblIdx >= GMM_GEN9_MAX_NUMBER_MOCS_INDEXES)
180 {
181 GMM_CACHE_POLICY_TBL_ELEMENT *TblEle = &pGmmLibContext->GetCachePolicyTlbElement()[CPTblIdx];
182 CurrentMaxSpecialIndex = ((uint32_t)CPTblIdx > CurrentMaxSpecialIndex) ? (uint32_t)CPTblIdx : CurrentMaxSpecialIndex;
183
184 if(SpecialMOCS && //Update if one of special MOCS enums
185 !(TblEle->LeCC.DwordValue == UsageEle.LeCC.DwordValue &&
186 TblEle->L3.UshortValue == UsageEle.L3.UshortValue))
187 {
188 //Assert if being overwritten!
189 __GMM_ASSERT(TblEle->LeCC.DwordValue == 0 &&
190 TblEle->L3.UshortValue == 0);
191 }
192 }
193 else
194 {
195 for(j = 1; j <= CurrentMaxMocsIndex; j++)
196 {
197 GMM_CACHE_POLICY_TBL_ELEMENT *TblEle = &pGmmLibContext->GetCachePolicyTlbElement()[j];
198 if(TblEle->LeCC.DwordValue == UsageEle.LeCC.DwordValue &&
199 TblEle->L3.UshortValue == UsageEle.L3.UshortValue)
200 {
201 CPTblIdx = j;
202 break;
203 }
204 }
205 }
206 // Didn't find the caching settings in one of the already programmed Explicit Mocs lookup table entries
207 // Need to add a new explicit mocs lookup table entry.
208 if(CPTblIdx == -1)
209 {
210
211 #if(_WIN32 && (_DEBUG || _RELEASE_INTERNAL))
212 // If the Cache Policy setting is overriden through regkey,
213 // don't raise an assert/log error. Raising an assert for debug/perf testing isn't really helpful
214 if(pCachePolicy[Usage].IsOverridenByRegkey)
215 {
216 if(CurrentMaxMocsIndex < GMM_GEN9_MAX_NUMBER_MOCS_INDEXES - 1)
217 {
218 GMM_CACHE_POLICY_TBL_ELEMENT *TblEle = &(pGmmLibContext->GetCachePolicyTlbElement()[++CurrentMaxMocsIndex]);
219 CPTblIdx = CurrentMaxMocsIndex;
220
221 TblEle->LeCC.DwordValue = UsageEle.LeCC.DwordValue;
222 TblEle->L3.UshortValue = UsageEle.L3.UshortValue;
223 }
224 else
225 {
226 // Too many unique caching combinations to program the
227 // MOCS lookup table.
228 CachePolicyError = true;
229 GMM_ASSERTDPF(
230 "Cache Policy Init Error: Invalid Cache Programming, too many unique caching combinations"
231 "(we only support GMM_GEN_MAX_NUMBER_MOCS_INDEXES = %d)",
232 GMM_GEN9_MAX_NUMBER_MOCS_INDEXES - 1);
233 // Set cache policy index to uncached.
234 CPTblIdx = 0;
235 }
236 }
237 else
238 #endif
239 {
240 GMM_ASSERTDPF(false, "CRITICAL ERROR: Cache Policy Usage value specified by Client in not defined in Fixed MOCS Table!");
241
242 // Log Error using regkey to indicate the above error
243 #if(_WIN32 && (_DEBUG || _RELEASE_INTERNAL) && __GMM_KMD__)
244 REGISTRY_OVERRIDE_WRITE(Usage, NewMOCSEntryLeCCValue, UsageEle.LeCC.DwordValue);
245 REGISTRY_OVERRIDE_WRITE(Usage, NewMOCSEntryL3Value, UsageEle.L3.UshortValue);
246 #endif
247
248 CachePolicyError = true;
249 GMM_ASSERTDPF(
250 "Cache Policy Init Error: Invalid Cache Programming, too many unique caching combinations"
251 "(we only support GMM_GEN_MAX_NUMBER_MOCS_INDEXES = %d)",
252 CurrentMaxMocsIndex);
253
254 // Set cache policy index to uncached.
255 CPTblIdx = 0;
256 }
257 }
258
259 // PTE entries do not control caching on SKL+ (for legacy context)
260 if(!GetUsagePTEValue(pCachePolicy[Usage], Usage, &PTEValue))
261 {
262 CachePolicyError = true;
263 }
264
265 pCachePolicy[Usage].PTE.DwordValue = PTEValue & 0xFFFFFFFF;
266 pCachePolicy[Usage].PTE.HighDwordValue = 0;
267
268 pCachePolicy[Usage].MemoryObjectOverride.Gen11.Index = CPTblIdx;
269
270 pCachePolicy[Usage].Override = ALWAYS_OVERRIDE;
271
272 if(CachePolicyError)
273 {
274 GMM_ASSERTDPF("Cache Policy Init Error: Invalid Cache Programming - Element %d", Usage);
275 }
276 }
277
278 CurrentMaxSpecialMocsIndex = CurrentMaxSpecialIndex;
279 }
280
281 return GMM_SUCCESS;
282 }
283
284 //=============================================================================
285 //
286 // Function: SetUpMOCSTable
287 //
288 // Desc:
289 //
290 // Parameters:
291 //
292 // Return: GMM_STATUS
293 //
294 //-----------------------------------------------------------------------------
SetUpMOCSTable()295 void GmmLib::GmmGen11CachePolicy::SetUpMOCSTable()
296 {
297 GMM_CACHE_POLICY_TBL_ELEMENT *pCachePolicyTlbElement = &(pGmmLibContext->GetCachePolicyTlbElement()[0]);
298
299 #define GMM_DEFINE_MOCS(Index, L3_ESC, L3_SCC, L3_CC, LeCC_CC, LeCC_TC, LeCC_LRUM, LeCC_AOM, LeCC_ESC, LeCC_SCC, LeCC_PFM, LeCC_SCF, LeCC_CoS, LeCC_SelfSnoop) \
300 { \
301 pCachePolicyTlbElement[Index].L3.ESC = L3_ESC; \
302 pCachePolicyTlbElement[Index].L3.SCC = L3_SCC; \
303 pCachePolicyTlbElement[Index].L3.Cacheability = L3_CC; \
304 pCachePolicyTlbElement[Index].LeCC.Cacheability = LeCC_CC; \
305 pCachePolicyTlbElement[Index].LeCC.TargetCache = LeCC_TC; \
306 pCachePolicyTlbElement[Index].LeCC.LRUM = LeCC_LRUM; \
307 pCachePolicyTlbElement[Index].LeCC.AOM = LeCC_AOM; \
308 pCachePolicyTlbElement[Index].LeCC.ESC = LeCC_ESC; \
309 pCachePolicyTlbElement[Index].LeCC.SCC = LeCC_SCC; \
310 pCachePolicyTlbElement[Index].LeCC.PFM = LeCC_PFM; \
311 pCachePolicyTlbElement[Index].LeCC.SCF = LeCC_SCF; \
312 pCachePolicyTlbElement[Index].LeCC.CoS = LeCC_CoS; \
313 pCachePolicyTlbElement[Index].LeCC.SelfSnoop = LeCC_SelfSnoop; \
314 }
315
316 // clang-format off
317
318 //Default MOCS Table
319 for(int index = 0; index < GMM_MAX_NUMBER_MOCS_INDEXES; index++)
320 { // Index ESC SCC L3CC LeCC TC LRUM DAoM ERSC SCC PFM SCF CoS SSE
321 GMM_DEFINE_MOCS(index , 0 , 0 , 3 , 3 , 1 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
322 }
323 // Explicit MOCS Table
324 // Index ESC SCC L3CC LeCC TC LRUM DAoM ERSC SCC PFM SCF CoS SSE
325 GMM_DEFINE_MOCS( 1 , 0 , 0 , 3 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
326 GMM_DEFINE_MOCS( 2 , 0 , 0 , 3 , 3 , 1 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
327 GMM_DEFINE_MOCS( 3 , 0 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
328 GMM_DEFINE_MOCS( 4 , 0 , 0 , 3 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
329 GMM_DEFINE_MOCS( 5 , 0 , 0 , 1 , 3 , 1 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
330 GMM_DEFINE_MOCS( 6 , 0 , 0 , 1 , 3 , 1 , 1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 , 3 , 1 , 3 , 0 , 0 , 3 , 0 , 0 , 0 , 0 )
345 GMM_DEFINE_MOCS( 21 , 0 , 0 , 3 , 3 , 1 , 3 , 0 , 0 , 1 , 0 , 0 , 0 , 0 )
346 GMM_DEFINE_MOCS( 22 , 0 , 0 , 3 , 3 , 1 , 3 , 0 , 1 , 3 , 0 , 0 , 0 , 0 )
347 GMM_DEFINE_MOCS( 23 , 0 , 0 , 3 , 3 , 1 , 3 , 0 , 1 , 7 , 0 , 0 , 0 , 0 )
348 GMM_DEFINE_MOCS( 62 , 0 , 0 , 1 , 3 , 1 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
349 GMM_DEFINE_MOCS( 63 , 0 , 0 , 1 , 3 , 1 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
350
351 if(pGmmLibContext->GetSkuTable().FtrLLCBypass)
352 {
353 GMM_DEFINE_MOCS( 16 , 0 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 )
354 GMM_DEFINE_MOCS( 17 , 0 , 0 , 3 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 )
355 }
356
357 CurrentMaxMocsIndex = 23;
358
359 // clang-format on
360 #undef GMM_DEFINE_MOCS
361 }
362