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
26 /////////////////////////////////////////////////////////////////////////////////////
27 /// Constructor for the GmmCachePolicyCommon Class, initializes the CachePolicy
28 /// @param[in] pCachePolicy
29 /////////////////////////////////////////////////////////////////////////////////////
GmmCachePolicyCommon(GMM_CACHE_POLICY_ELEMENT * pCachePolicy,Context * pGmmLibContext)30 GmmLib::GmmCachePolicyCommon::GmmCachePolicyCommon(GMM_CACHE_POLICY_ELEMENT *pCachePolicy, Context *pGmmLibContext)
31 {
32 this->pCachePolicy = pCachePolicy;
33 this->pGmmLibContext = pGmmLibContext;
34 NumPATRegisters = GMM_NUM_PAT_ENTRIES_LEGACY;
35 NumMOCSRegisters = GMM_MAX_NUMBER_MOCS_INDEXES;
36 }
37
38 /////////////////////////////////////////////////////////////////////////////////////
39 /// Returns the wanted memory type for this usage.
40 ///
41 /// @param[in] CachePolicy: cache policy for a usage
42 ///
43 /// @return wanted memory type
44 /////////////////////////////////////////////////////////////////////////////////////
GetWantedMemoryType(GMM_CACHE_POLICY_ELEMENT CachePolicy)45 GMM_GFX_MEMORY_TYPE GmmLib::GmmCachePolicyCommon::GetWantedMemoryType(GMM_CACHE_POLICY_ELEMENT CachePolicy)
46 {
47 GMM_GFX_MEMORY_TYPE WantedMemoryType = GMM_GFX_UC_WITH_FENCE;
48 if(CachePolicy.WT)
49 {
50 WantedMemoryType = GMM_GFX_WT;
51 }
52 else if(!(CachePolicy.LLC || CachePolicy.ELLC))
53 {
54 WantedMemoryType = GMM_GFX_UC_WITH_FENCE;
55 }
56 else
57 {
58 WantedMemoryType = GMM_GFX_WB;
59 }
60 return WantedMemoryType;
61 }
62
63 /////////////////////////////////////////////////////////////////////////////////////
64 /// Generates memory object based on resource usage
65 ///
66 /// @param[in] pResInfo: Resource info for resource , can be null
67 /// @param[in] Usage: Current usage for resource
68 ///
69 /// @return MEMORY_OBJECT_CONTROL_STATE: Populated memory object
70 ///
71 /////////////////////////////////////////////////////////////////////////////////////
CachePolicyGetOriginalMemoryObject(GMM_RESOURCE_INFO * pResInfo)72 MEMORY_OBJECT_CONTROL_STATE GMM_STDCALL GmmLib::GmmCachePolicyCommon::CachePolicyGetOriginalMemoryObject(GMM_RESOURCE_INFO *pResInfo)
73 {
74 MEMORY_OBJECT_CONTROL_STATE MOCS = pGmmLibContext->GetCachePolicyElement(GMM_RESOURCE_USAGE_UNKNOWN).MemoryObjectOverride;
75
76 if(pResInfo)
77 {
78 MOCS = pResInfo->GetMOCS();
79 }
80
81 return MOCS;
82 }
83
84 /////////////////////////////////////////////////////////////////////////////////////
85 /// A simple getter function returning the MOCS (cache policy) for a given
86 /// use Usage of the named resource pResInfo.
87 /// Typically used to populate a SURFACE_STATE for a GPU task.
88 ///
89 /// @param[in] pResInfo: Resource info for resource, can be NULL.
90 /// @param[in] Usage: Current usage for resource.
91 ///
92 /// @return MEMORY_OBJECT_CONTROL_STATE: Gen adjusted MOCS structure (cache
93 /// policy) for the given buffer use.
94 /////////////////////////////////////////////////////////////////////////////////////
CachePolicyGetMemoryObject(GMM_RESOURCE_INFO * pResInfo,GMM_RESOURCE_USAGE_TYPE Usage)95 MEMORY_OBJECT_CONTROL_STATE GMM_STDCALL GmmLib::GmmCachePolicyCommon::CachePolicyGetMemoryObject(GMM_RESOURCE_INFO *pResInfo, GMM_RESOURCE_USAGE_TYPE Usage)
96 {
97 const GMM_CACHE_POLICY_ELEMENT *CachePolicy = NULL;
98 __GMM_ASSERT(pGmmLibContext->GetCachePolicyElement(Usage).Initialized);
99 CachePolicy = pGmmLibContext->GetCachePolicyUsage();
100 // Prevent wrong Usage for XAdapter resources. UMD does not call GetMemoryObject on shader resources but,
101 // when they add it someone could call it without knowing the restriction.
102 if(pResInfo &&
103 pResInfo->GetResFlags().Info.XAdapter &&
104 (Usage != GMM_RESOURCE_USAGE_XADAPTER_SHARED_RESOURCE))
105 {
106 __GMM_ASSERT(false);
107 }
108
109 if(!pResInfo ||
110 (CachePolicy[Usage].Override & CachePolicy[pResInfo->GetCachePolicyUsage()].IDCode) ||
111 (CachePolicy[Usage].Override == ALWAYS_OVERRIDE))
112 {
113 return CachePolicy[Usage].MemoryObjectOverride;
114 }
115 else
116 {
117 return CachePolicy[Usage].MemoryObjectNoOverride;
118 }
119 }
120 /////////////////////////////////////////////////////////////////////////////////////
121 /// A simple getter function returning the PAT (cache policy) for a given
122 /// use Usage of the named resource pResInfo.
123 /// Typically used to populate PPGTT/GGTT.
124 ///
125 /// @param[in] pResInfo: Resource info for resource, can be NULL.
126 /// @param[in] Usage: Current usage for resource.
127 ///
128 /// @return PATIndex
129 /////////////////////////////////////////////////////////////////////////////////////
CachePolicyGetPATIndex(GMM_RESOURCE_INFO * pResInfo,GMM_RESOURCE_USAGE_TYPE Usage,bool * pCompressionEnable,bool IsCpuCacheable)130 uint32_t GMM_STDCALL GmmLib::GmmCachePolicyCommon::CachePolicyGetPATIndex(GMM_RESOURCE_INFO *pResInfo, GMM_RESOURCE_USAGE_TYPE Usage, bool *pCompressionEnable, bool IsCpuCacheable)
131 {
132 GMM_UNREFERENCED_PARAMETER(pResInfo);
133 GMM_UNREFERENCED_PARAMETER(Usage);
134 GMM_UNREFERENCED_PARAMETER(pCompressionEnable);
135 GMM_UNREFERENCED_PARAMETER(IsCpuCacheable);
136
137 return GMM_PAT_ERROR;
138 }
139
140 /////////////////////////////////////////////////////////////////////////////////////
141 /// Generates PTE based on resource usage
142 ///
143 /// @param[in] Usage: type of usage
144 ///
145 /// @return GMM_PTE_CACHE_CONTROL_BITS: Populated PTE
146 /////////////////////////////////////////////////////////////////////////////////////
CachePolicyGetPteType(GMM_RESOURCE_USAGE_TYPE Usage)147 GMM_PTE_CACHE_CONTROL_BITS GMM_STDCALL GmmLib::GmmCachePolicyCommon::CachePolicyGetPteType(GMM_RESOURCE_USAGE_TYPE Usage)
148 {
149 __GMM_ASSERT(pGmmLibContext->GetCachePolicyElement(Usage).Initialized);
150 return pGmmLibContext->GetCachePolicyElement(Usage).PTE;
151 }
152
153 /////////////////////////////////////////////////////////////////////////////////////
154 /// Returns number of PAT entries possible on that platform
155 ///
156 /// @return uint32_t
157 /////////////////////////////////////////////////////////////////////////////////////
CachePolicyGetNumPATRegisters()158 uint32_t GMM_STDCALL GmmLib::GmmCachePolicyCommon::CachePolicyGetNumPATRegisters()
159 {
160 return NumPATRegisters;
161 }
162
163 /////////////////////////////////////////////////////////////////////////////////////
164 /// Returns L1 cache attribute based on resource usage
165 ///
166 /// @return uint32_t
167 /////////////////////////////////////////////////////////////////////////////////////
GetSurfaceStateL1CachePolicy(GMM_RESOURCE_USAGE_TYPE Usage)168 uint32_t GMM_STDCALL GmmLib::GmmCachePolicyCommon::GetSurfaceStateL1CachePolicy(GMM_RESOURCE_USAGE_TYPE Usage)
169 {
170 __GMM_ASSERT(pCachePolicy[Usage].Initialized);
171 return pCachePolicy[Usage].L1CC;
172 }
173