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 #pragma once
23 
24 // GmmConst.h needed for GMM_MAX_NUMBER_MOCS_INDEXES
25 #include "GmmConst.h"
26 #include "../../../Platform/GmmPlatforms.h"
27 
28 #ifdef _WIN32
29 #define GMM_MUTEX_HANDLE    HANDLE
30 #else
31 #include <pthread.h>
32 #define GMM_MUTEX_HANDLE    pthread_mutex_t
33 #endif
34 
35 // Set packing alignment
36 #pragma pack(push, 8)
37 
38 //===========================================================================
39 // Forward Declaration: Defined in GmmResourceInfoExt.h
40 //---------------------------------------------------------------------------
41 struct GMM_CONTEXT_REC;
42 typedef struct GMM_CONTEXT_REC GMM_CONTEXT;
43 
44 //===========================================================================
45 // typedef:
46 //      GMM_UMD_CONTEXT
47 //
48 // Description:
49 //      Struct defines user mode GMM context.
50 //----------------------------------------------------------------------------
51 typedef struct GMM_UMD_CONTEXT_REC
52 {
53     uint32_t   TBD1;
54     uint32_t   TBD2;
55     uint32_t   TBD3;
56 } GMM_UMD_CONTEXT;
57 
58 
59 #if (!defined(__GMM_KMD__) && !defined(GMM_UNIFIED_LIB))
60 #include "GmmClientContext.h"
61 #endif
62 
63 //===========================================================================
64 // typedef:
65 //      GMM_GLOBAL_CONTEXT
66 //
67 // Description:
68 //     Struct contains contexts for user mode and kernel mode. It also contains
69 //     platform information. This struct is initialized in user mode with
70 //     GmmInitGlobalContext().
71 //
72 //----------------------------------------------------------------------------
73 
74 #ifdef __cplusplus
75 #include "GmmMemAllocator.hpp"
76 
77 namespace GmmLib
78 {
79     class NON_PAGED_SECTION Context : public GmmMemAllocator
80     {
81     private:
82 #if(!defined(__GMM_KMD__) && !GMM_LIB_DLL_MA)
83     	static int32_t                   RefCount;
84 #endif
85         GMM_CLIENT                       ClientType;
86         GMM_PLATFORM_INFO_CLASS*         pPlatformInfo;
87 
88         GMM_TEXTURE_CALC*                pTextureCalc;
89         SKU_FEATURE_TABLE                SkuTable;
90         WA_TABLE                         WaTable;
91         GT_SYSTEM_INFO                   GtSysInfo;
92 
93     #if(defined(__GMM_KMD__))
94         GMM_GTT_CONTEXT              GttContext;
95     #endif
96 
97         GMM_CONTEXT                      *pGmmKmdContext;
98         GMM_UMD_CONTEXT                  *pGmmUmdContext;
99         void                             *pKmdHwDev;
100         void                             *pUmdAdapter;
101 
102         GMM_CACHE_POLICY_ELEMENT         CachePolicy[GMM_RESOURCE_USAGE_MAX];
103         GMM_CACHE_POLICY_TBL_ELEMENT     CachePolicyTbl[GMM_MAX_NUMBER_MOCS_INDEXES];
104         GMM_CACHE_POLICY                 *pGmmCachePolicy;
105 
106     #if(defined(__GMM_KMD__))
107         uint64_t           IA32ePATTable;
108         GMM_PRIVATE_PAT     PrivatePATTable[GMM_NUM_PAT_ENTRIES];
109         int32_t                PrivatePATTableMemoryType[GMM_NUM_GFX_PAT_TYPES];
110     #endif
111 
112         // Padding Percentage limit on 64KB paged resource
113         uint32_t               AllowedPaddingFor64KbPagesPercentage;
114         uint64_t              InternalGpuVaMax;
115         uint32_t               AllowedPaddingFor64KBTileSurf;
116 #ifdef GMM_LIB_DLL
117         // Mutex Object used for synchronization of ProcessSingleton Context
118         static GMM_MUTEX_HANDLE           SingletonContextSyncMutex;
119 #endif
120 
121     public :
122         //Constructors and destructors
123         Context();
124         ~Context();
125 
126 #if(!defined(__GMM_KMD__) && (!GMM_LIB_DLL_MA))
IncrementRefCount()127         static int32_t IncrementRefCount()  // Returns the current RefCount and then increment it
128         {
129 #if defined(_WIN32)
130             return(InterlockedIncrement((LONG *)&RefCount) - 1);  //InterLockedIncrement() returns incremented value
131 #elif defined(__GNUC__)
132             return(__sync_fetch_and_add(&RefCount, 1));
133 #endif
134         }
135 
DecrementRefCount()136         static int32_t DecrementRefCount()
137         {
138             int CurrentValue = 0;
139             int TargetValue = 0;
140             do
141             {
142                 CurrentValue = RefCount;
143                 if (CurrentValue > 0)
144                 {
145                     TargetValue = CurrentValue - 1;
146                 }
147                 else
148                 {
149                     break;
150                 }
151 #if defined(_WIN32)
152             } while (!(InterlockedCompareExchange((LONG *)&RefCount, TargetValue, CurrentValue) == CurrentValue));
153 #elif defined(__GNUC__)
154             } while (!__sync_bool_compare_and_swap(&RefCount, CurrentValue, TargetValue));
155 #endif
156 
157             return TargetValue;
158         }
159 #endif
160         GMM_STATUS GMM_STDCALL InitContext(
161                                     const PLATFORM& Platform,
162                                     const SKU_FEATURE_TABLE* pSkuTable,
163                                     const WA_TABLE* pWaTable,
164                                     const GT_SYSTEM_INFO* pGtSysInfo,
165                                     GMM_CLIENT ClientType);
166 
167         void GMM_STDCALL DestroyContext();
168 
169 #if (!defined(__GMM_KMD__) && !defined(GMM_UNIFIED_LIB))
170         GMM_CLIENT_CONTEXT *pGmmGlobalClientContext;
171 #endif
172 
173 
174         //Inline functions
175         /////////////////////////////////////////////////////////////////////////
176         /// Returns the client type e.g. DX, OCL, OGL etc.
177         /// @return   client type
178         /////////////////////////////////////////////////////////////////////////
GetClientType()179         GMM_INLINE GMM_CLIENT  GMM_STDCALL  GetClientType()
180         {
181             return (ClientType);
182         }
183 
184         /////////////////////////////////////////////////////////////////////////
185         /// Returns the PlatformInfo
186         /// @return   PlatformInfo
187         /////////////////////////////////////////////////////////////////////////
GetPlatformInfo()188         GMM_INLINE GMM_PLATFORM_INFO&  GMM_STDCALL  GetPlatformInfo()
189         {
190             return (const_cast<GMM_PLATFORM_INFO&>(pPlatformInfo->GetData()));
191         }
192 
193         /////////////////////////////////////////////////////////////////////////
194         /// Returns the cache policy element array ptr
195         /// @return   const cache policy elment ptr
196         /////////////////////////////////////////////////////////////////////////
GetCachePolicyUsage()197         GMM_INLINE GMM_CACHE_POLICY_ELEMENT*  GMM_STDCALL GetCachePolicyUsage()
198         {
199             return (&CachePolicy[0]);
200         }
201 
202         /////////////////////////////////////////////////////////////////////////
203         /// Returns the cache policy tlb element array ptr
204         /// @return   const cache policy elment ptr
205         /////////////////////////////////////////////////////////////////////////
GetCachePolicyTlbElement()206         GMM_INLINE GMM_CACHE_POLICY_TBL_ELEMENT*  GMM_STDCALL GetCachePolicyTlbElement()
207         {
208             return (&CachePolicyTbl[0]);
209         }
210 
211         /////////////////////////////////////////////////////////////////////////
212         /// Returns the texture calculation object ptr
213         /// @return   TextureCalc ptr
214         /////////////////////////////////////////////////////////////////////////
GetTextureCalc()215         GMM_INLINE GMM_TEXTURE_CALC* GMM_STDCALL GetTextureCalc()
216         {
217             return (pTextureCalc);
218         }
219 
220         /////////////////////////////////////////////////////////////////////////
221         /// Returns the platform info class object ptr
222         /// @return   PlatformInfo class object ptr
223         /////////////////////////////////////////////////////////////////////////
GetPlatformInfoObj()224         GMM_INLINE GMM_PLATFORM_INFO_CLASS* GMM_STDCALL GetPlatformInfoObj()
225         {
226             return (pPlatformInfo);
227         }
228 
229         /////////////////////////////////////////////////////////////////////////
230         /// Returns the cache policy object ptr
231         /// @return   TextureCalc ptr
232         /////////////////////////////////////////////////////////////////////////
GetCachePolicyObj()233         GMM_INLINE GMM_CACHE_POLICY* GMM_STDCALL GetCachePolicyObj()
234         {
235             return (pGmmCachePolicy);
236         }
237 
238         /////////////////////////////////////////////////////////////////////////
239         /// Returns the sku table ptr
240         /// @return   const SkuTable ptr
241         /////////////////////////////////////////////////////////////////////////
GetSkuTable()242         GMM_INLINE const SKU_FEATURE_TABLE& GMM_STDCALL GetSkuTable()
243         {
244             return (SkuTable);
245         }
246 
247         /////////////////////////////////////////////////////////////////////////
248         /// Returns the Wa table ptr
249         /// @return    WaTable ptr
250         /////////////////////////////////////////////////////////////////////////
GetWaTable()251         GMM_INLINE const WA_TABLE& GMM_STDCALL GetWaTable()
252         {
253             return (WaTable);
254         }
255 
256         /////////////////////////////////////////////////////////////////////////
257         /// Returns the GT system info ptr
258         /// @return   const GtSysInfo ptr
259         /////////////////////////////////////////////////////////////////////////
GetGtSysInfoPtr()260         GMM_INLINE const GT_SYSTEM_INFO* GMM_STDCALL GetGtSysInfoPtr()
261         {
262             return (&GtSysInfo);
263         }
264 
265         /////////////////////////////////////////////////////////////////////////
266         /// Returns the GT system info ptr
267         /// @return   GtSysInfo ptr
268         /////////////////////////////////////////////////////////////////////////
GetGtSysInfo()269         GMM_INLINE GT_SYSTEM_INFO* GMM_STDCALL GetGtSysInfo()
270         {
271             return (&GtSysInfo);
272         }
273 
274         /////////////////////////////////////////////////////////////////////////
275         /// Returns Cache policy element for a given usage type
276         /// @return   cache policy element
277         ////////////////////////////////////////////////////////////////////////
GetCachePolicyElement(GMM_RESOURCE_USAGE_TYPE Usage)278         GMM_INLINE GMM_CACHE_POLICY_ELEMENT GetCachePolicyElement(GMM_RESOURCE_USAGE_TYPE Usage)
279         {
280             return (CachePolicy[Usage]);
281         }
282 
283         /////////////////////////////////////////////////////////////////////////
284         /// Get padding percentage limit for 64kb pages
285         /////////////////////////////////////////////////////////////////////////
GetAllowedPaddingFor64KbPagesPercentage()286         uint32_t GetAllowedPaddingFor64KbPagesPercentage()
287         {
288             return (AllowedPaddingFor64KbPagesPercentage);
289         }
290 
GetInternalGpuVaRangeLimit()291         uint64_t& GetInternalGpuVaRangeLimit()
292         {
293             return InternalGpuVaMax;
294         }
295          /////////////////////////////////////////////////////////////////////////
296         /// Get padding  limit for 64k pages
297         /////////////////////////////////////////////////////////////////////////
GetAllowedPaddingFor64KBTileSurf()298         uint32_t GetAllowedPaddingFor64KBTileSurf()
299         {
300             return (AllowedPaddingFor64KBTileSurf);
301         }
302 
303         /////////////////////////////////////////////////////////////////////////
304         /// Set padding  limit for 64k pages
305         /////////////////////////////////////////////////////////////////////////
306 
SetAllowedPaddingFor64KBTileSurf(uint32_t Value)307         GMM_INLINE void SetAllowedPaddingFor64KBTileSurf(uint32_t Value)
308         {
309             AllowedPaddingFor64KBTileSurf = Value;
310         }
311 
312     #ifdef GMM_LIB_DLL
313         ADAPTER_BDF             sBdf;
314         #ifdef _WIN32
315             // ProcessHeapVA Singleton HeapObj
316             GMM_HEAP            *pHeapObj;
317             uint32_t            ProcessHeapCounter;
318             // ProcessVA Partition Address space
319             GMM_GFX_PARTITIONING ProcessVA;
320             uint32_t            ProcessVACounter;
321 
322             /////////////////////////////////////////////////////////////////////////
323             /// Get ProcessHeapVA Singleton HeapObj
324             /////////////////////////////////////////////////////////////////////////
325             GMM_HEAP* GetSharedHeapObject();
326 
327             /////////////////////////////////////////////////////////////////////////
328             /// Set ProcessHeapVA Singleton HeapObj
329             /////////////////////////////////////////////////////////////////////////
330             uint32_t SetSharedHeapObject(GMM_HEAP **pProcessHeapObj);
331 
332             /////////////////////////////////////////////////////////////////////////
333             /// Get or Sets ProcessGfxPartition VA
334             /////////////////////////////////////////////////////////////////////////
335             void GetProcessGfxPartition(GMM_GFX_PARTITIONING* pProcessVA);
336 
337             /////////////////////////////////////////////////////////////////////////
338             /// Get or Sets ProcessGfxPartition VA
339             /////////////////////////////////////////////////////////////////////////
340             void SetProcessGfxPartition(GMM_GFX_PARTITIONING* pProcessVA);
341 
342             /////////////////////////////////////////////////////////////////////////
343             /// Destroy Sync Mutex created for Singleton Context access
344             /////////////////////////////////////////////////////////////////////////
DestroySingletonContextSyncMutex()345             static void   DestroySingletonContextSyncMutex()
346             {
347                 if (SingletonContextSyncMutex)
348                 {
349                     ::CloseHandle(SingletonContextSyncMutex);
350                     SingletonContextSyncMutex = NULL;
351                 }
352             }
353 
354             /////////////////////////////////////////////////////////////////////////
355             /// Acquire Sync Mutex for Singleton Context access
356             /////////////////////////////////////////////////////////////////////////
LockSingletonContextSyncMutex()357             static GMM_STATUS   LockSingletonContextSyncMutex()
358             {
359                 if (SingletonContextSyncMutex)
360                 {
361                     while (WAIT_OBJECT_0 != ::WaitForSingleObject(SingletonContextSyncMutex, INFINITE));
362                     return GMM_SUCCESS;
363                 }
364                 else
365                 {
366                     return GMM_ERROR;
367                 }
368             }
369 
370             /////////////////////////////////////////////////////////////////////////
371             /// Release Sync Mutex for Singleton Context access
372             /////////////////////////////////////////////////////////////////////////
UnlockSingletonContextSyncMutex()373             static GMM_STATUS   UnlockSingletonContextSyncMutex()
374             {
375                 if (SingletonContextSyncMutex)
376                 {
377                     ::ReleaseMutex(SingletonContextSyncMutex);
378                     return GMM_SUCCESS;
379                 }
380                 else
381                 {
382                     return GMM_ERROR;
383                 }
384             }
385 
386         #else // Non Win OS
387 
388             /////////////////////////////////////////////////////////////////////////
389             /// Destroy Sync Mutex created for Singleton Context access
390             /////////////////////////////////////////////////////////////////////////
DestroySingletonContextSyncMutex()391             static void   DestroySingletonContextSyncMutex()
392             {
393                 pthread_mutex_destroy(&SingletonContextSyncMutex);
394             }
395 
396             /////////////////////////////////////////////////////////////////////////
397             /// Acquire Sync Mutex for Singleton Context access
398             /////////////////////////////////////////////////////////////////////////
LockSingletonContextSyncMutex()399             static GMM_STATUS   LockSingletonContextSyncMutex()
400             {
401                 pthread_mutex_lock(&SingletonContextSyncMutex);
402                 return GMM_SUCCESS;
403             }
404 
405             /////////////////////////////////////////////////////////////////////////
406             /// Release Sync Mutex for Singleton Context access
407             /////////////////////////////////////////////////////////////////////////
UnlockSingletonContextSyncMutex()408             static GMM_STATUS   UnlockSingletonContextSyncMutex()
409             {
410                 pthread_mutex_unlock(&SingletonContextSyncMutex);
411                 return GMM_SUCCESS;
412             }
413 
414         #endif // _WIN32
415 
416     #endif // GMM_LIB_DLL
417 
418         // KMD specific inline functions
419     #ifdef __GMM_KMD__
420 
421         /////////////////////////////////////////////////////////////////////////
422         /// Returns private PAT table memory type for a given PAT type
423         /// @return   PAT Memory type
424         /////////////////////////////////////////////////////////////////////////
GetPrivatePATTableMemoryType(GMM_GFX_PAT_TYPE PatType)425         GMM_INLINE int32_t  GMM_STDCALL GetPrivatePATTableMemoryType(GMM_GFX_PAT_TYPE PatType)
426         {
427             return (PrivatePATTableMemoryType[PatType]);
428         }
429 
430         /////////////////////////////////////////////////////////////////////////
431         /// Returns private PAT table memory type array ptr
432         /// @return   PAT Memory type array ptr
433         /////////////////////////////////////////////////////////////////////////
GetPrivatePATTableMemoryType()434         GMM_INLINE int32_t*  GMM_STDCALL GetPrivatePATTableMemoryType()
435         {
436             return (PrivatePATTableMemoryType);
437         }
438 
439 
440         /////////////////////////////////////////////////////////////////////////
441         /// Returns private PAT table array ptr
442         /// @return   PAT array ptr
443         /////////////////////////////////////////////////////////////////////////
GetPrivatePATTable()444         GMM_INLINE GMM_PRIVATE_PAT* GMM_STDCALL  GetPrivatePATTable()
445         {
446             return (PrivatePATTable);
447         }
448 
449         /////////////////////////////////////////////////////////////////////////
450         /// Returns private PAT table entry for a given PAT index
451         /// @return   PAT entry
452         /////////////////////////////////////////////////////////////////////////
GetPrivatePATEntry(uint32_t PatIndex)453         GMM_INLINE GMM_PRIVATE_PAT GMM_STDCALL  GetPrivatePATEntry(uint32_t  PatIndex)
454         {
455             return (PrivatePATTable[PatIndex]);
456         }
457 
458         /////////////////////////////////////////////////////////////////////////
459         /// Returns gmm kmd context ptr
460         /// @return   GmmKmdContext ptr
461         /////////////////////////////////////////////////////////////////////////
GetGmmKmdContext()462         GMM_INLINE GMM_CONTEXT* GetGmmKmdContext()
463         {
464             return (pGmmKmdContext);
465         }
466 
467         /////////////////////////////////////////////////////////////////////////
468         /// Sets gmm kmd context ptr
469         /// @return   GmmKmdContext ptr
470         /////////////////////////////////////////////////////////////////////////
SetGmmKmdContext(GMM_CONTEXT * pGmmKmdContext)471         GMM_INLINE void SetGmmKmdContext(GMM_CONTEXT *pGmmKmdContext)
472         {
473             this->pGmmKmdContext = pGmmKmdContext;
474         }
475 
476         /////////////////////////////////////////////////////////////////////////
477         /// Returns gtt context ptr
478         /// @return   GttContext ptr
479         /////////////////////////////////////////////////////////////////////////
GetGttContext()480         GMM_INLINE GMM_GTT_CONTEXT* GetGttContext()
481         {
482             return (&GttContext);
483         }
484 
485         /////////////////////////////////////////////////////////////////////////
486         /// Returns Cache policy tbl element for a given usage type
487         /// @return   cache policy tbl element
488         ////////////////////////////////////////////////////////////////////////
GetCachePolicyTblElement(GMM_RESOURCE_USAGE_TYPE Usage)489         GMM_INLINE GMM_CACHE_POLICY_TBL_ELEMENT GetCachePolicyTblElement(GMM_RESOURCE_USAGE_TYPE Usage)
490         {
491             return (CachePolicyTbl[Usage]);
492         }
493 
494         /////////////////////////////////////////////////////////////////////////
495         /// Resets  the sku Table after  GmmInitContext() could have changed them
496         /// since original latching
497         ////////////////////////////////////////////////////////////////////////
SetSkuTable(SKU_FEATURE_TABLE SkuTable)498         GMM_INLINE void SetSkuTable(SKU_FEATURE_TABLE SkuTable)
499         {
500             this->SkuTable = SkuTable;
501         }
502 
503         /////////////////////////////////////////////////////////////////////////
504         /// Resets  the Wa Table after  GmmInitContext() could have changed them
505         /// since original latching
506         ////////////////////////////////////////////////////////////////////////
SetWaTable(WA_TABLE WaTable)507         GMM_INLINE void SetWaTable(WA_TABLE WaTable)
508         {
509             this->WaTable = WaTable;
510         }
511 
512     #if(_DEBUG || _RELEASE_INTERNAL)
513 
514         /////////////////////////////////////////////////////////////////////////
515         /// Returns the override platform info class object ptr
516         /// @return   PlatformInfo class object ptr
517         /////////////////////////////////////////////////////////////////////////
GetOverridePlatformInfoObj()518         GMM_INLINE GMM_PLATFORM_INFO_CLASS* GMM_STDCALL GetOverridePlatformInfoObj()
519         {
520             return (Override.pPlatformInfo);
521         }
522 
523         /////////////////////////////////////////////////////////////////////////
524         /// Returns the override Platform info ptr to kmd
525         /// @return   override PlatformInfo ref
526         ////////////////////////////////////////////////////////////////////////
GetOverridePlatformInfo()527         GMM_INLINE GMM_PLATFORM_INFO& GMM_STDCALL GetOverridePlatformInfo()
528         {
529             return (const_cast<GMM_PLATFORM_INFO&>(Override.pPlatformInfo->GetData()));
530         }
531 
532         /////////////////////////////////////////////////////////////////////////
533         /// Set the override platform info calc ptr
534         ////////////////////////////////////////////////////////////////////////
SetOverridePlatformInfoObj(GMM_PLATFORM_INFO_CLASS * pPlatformInfoObj)535         GMM_INLINE void SetOverridePlatformInfoObj(GMM_PLATFORM_INFO_CLASS *pPlatformInfoObj)
536         {
537             Override.pPlatformInfo = pPlatformInfoObj;
538         }
539 
540         /////////////////////////////////////////////////////////////////////////
541         /// Returns the override Texture calc ptr to kmd
542         /// @return   override Texture calc ptr
543         ////////////////////////////////////////////////////////////////////////
GetOverrideTextureCalc()544         GMM_INLINE GMM_TEXTURE_CALC* GetOverrideTextureCalc()
545         {
546             return (Override.pTextureCalc);
547         }
548 
549         /////////////////////////////////////////////////////////////////////////
550         /// Set the override Texture calc ptr
551         ////////////////////////////////////////////////////////////////////////
SetOverrideTextureCalc(GMM_TEXTURE_CALC * pTextureCalc)552         GMM_INLINE void SetOverrideTextureCalc(GMM_TEXTURE_CALC *pTextureCalc)
553         {
554             Override.pTextureCalc = pTextureCalc;
555         }
556     #endif
557 
558     #endif
559 
560     GMM_CACHE_POLICY* GMM_STDCALL CreateCachePolicyCommon();
561     GMM_TEXTURE_CALC* GMM_STDCALL CreateTextureCalc(PLATFORM Platform, bool Override);
562     GMM_PLATFORM_INFO_CLASS *GMM_STDCALL CreatePlatformInfo(PLATFORM Platform, bool Override);
563 
564     private:
565         void GMM_STDCALL OverrideSkuWa();
566     };
567 
568 // Max number of Multi-Adapters allowed in the system
569 // ToDO: Make this dynamic allocation instead of static Array
570 #define MAX_NUM_ADAPTERS      3
571 //===========================================================================
572 // typedef:
573 //      _GMM_ADAPTER_INFO_
574 //
575 // Description:
576 //      Struct holds Adapter level information.
577 //----------------------------------------------------------------------------
578 typedef struct _GMM_ADAPTER_INFO_
579 {
580     Context             *pGmmLibContext;                    // Gmm UMD Lib Context which is process Singleton
581     int32_t             RefCount;                           // Ref Count for the number of Gmm UMD Lib process Singleton Context created per Process
582     GMM_MUTEX_HANDLE    SyncMutex;                          // SyncMutex to protect access of Gmm UMD Lib process Singleton Context
583     ADAPTER_BDF         sBdf;                               // Adpater's Bus, Device and Function info for which Gmm UMD Lib process Singleton Context is created
584 }GMM_ADAPTER_INFO;
585 
586 ////////////////////////////////////////////////////////////////////////////////////
587 /// Multi Adpater Context to hold data related to Multiple Adapters in the system
588 /// Contains functions and members that are needed to support Multi-Adapter.
589 ///////////////////////////////////////////////////////////////////////////////////
590     class NON_PAGED_SECTION GmmMultiAdapterContext : public GmmMemAllocator
591     {
592     private:
593         GMM_ADAPTER_INFO                AdapterInfo[MAX_NUM_ADAPTERS];
594         GMM_MUTEX_HANDLE                MAContextSyncMutex;         // SyncMutex to protect access of GmmMultiAdpaterContext
595         uint32_t                        NumAdapters;
596 	void*                           pCpuReserveBase;
597 	uint64_t                        CpuReserveSize;
598 
599     public:
600         //Constructors and destructors
601         GmmMultiAdapterContext();
602         ~GmmMultiAdapterContext();
603         /* Function prototypes */
604         /* Fucntions that update MultiAdapterContext members*/
605         uint32_t GMM_STDCALL            GetAdapterIndex(ADAPTER_BDF sBdf);
606         GMM_STATUS GMM_STDCALL          IntializeAdapterInfo(ADAPTER_BDF sBdf);
607         void GMM_STDCALL                ReleaseAdapterInfo(ADAPTER_BDF sBdf);
608         Context* GMM_STDCALL            GetAdapterLibContext(ADAPTER_BDF sBdf);
609         void GMM_STDCALL                SetAdapterLibContext(ADAPTER_BDF sBdf, Context* pGmmLibContext);
610         GMM_STATUS GMM_STDCALL          LockMAContextSyncMutex();
611         GMM_STATUS GMM_STDCALL          UnLockMAContextSyncMutex();
612         uint32_t GMM_STDCALL            GetNumAdapters();
613         /* Fucntions that update AdapterInfo*/
614         int32_t GMM_STDCALL             IncrementRefCount(ADAPTER_BDF sBdf);
615         int32_t GMM_STDCALL             DecrementRefCount(ADAPTER_BDF sBdf);
616         GMM_STATUS GMM_STDCALL          LockSingletonContextSyncMutex(ADAPTER_BDF sBdf);
617         GMM_STATUS GMM_STDCALL          UnlockSingletonContextSyncMutex(ADAPTER_BDF sBdf);
618     }; // GmmMultiAdapterContext
619 
620 } //namespace
621 
622 typedef GmmLib::Context GMM_GLOBAL_CONTEXT, GMM_LIB_CONTEXT;
623 typedef GmmLib::GmmMultiAdapterContext GMM_MA_LIB_CONTEXT;
624 
625 #else
626 struct GmmLibContext;
627 typedef struct GmmLibContext GMM_GLOBAL_CONTEXT, GMM_LIB_CONTEXT;
628 #endif
629 
630 
631 #ifdef __GMM_KMD__
632 void GMM_STDCALL GmmLinkKmdContextToGlobalInfo(GMM_GLOBAL_CONTEXT *pGmmLibContext, GMM_CONTEXT *pGmmKmdContext);
633 #endif /*__GMM_KMD__*/
634 
635 
636 //Declare all  GMM global context C interfaces.
637 #ifdef __cplusplus
638 extern "C" {
639 #endif /*__cplusplus*/
640 
641     const GMM_PLATFORM_INFO* GMM_STDCALL GmmGetPlatformInfo(GMM_GLOBAL_CONTEXT *pGmmLibContext);
642     const GMM_CACHE_POLICY_ELEMENT* GmmGetCachePolicyUsage(GMM_GLOBAL_CONTEXT *pGmmLibContext);
643           GMM_TEXTURE_CALC*         GmmGetTextureCalc(GMM_GLOBAL_CONTEXT *pGmmLibContext);
644     const SKU_FEATURE_TABLE*        GmmGetSkuTable(GMM_GLOBAL_CONTEXT *pGmmLibContext);
645     const WA_TABLE*                 GmmGetWaTable(GMM_GLOBAL_CONTEXT *pGmmLibContext);
646     const GT_SYSTEM_INFO*           GmmGetGtSysInfo(GMM_GLOBAL_CONTEXT *pGmmLibContext);
647 
648 #ifdef __GMM_KMD__
649     int32_t             GmmGetPrivatePATTableMemoryType(GMM_GLOBAL_CONTEXT *pGmmLibContext, GMM_GFX_PAT_TYPE PatType);
650     GMM_PRIVATE_PAT     GmmGetPrivatePATEntry(GMM_GLOBAL_CONTEXT *pGmmLibContext, uint32_t  PatIndex);
651     GMM_CONTEXT*        GmmGetGmmKmdContext(GMM_GLOBAL_CONTEXT *pGmmLibContext);
652     GMM_GTT_CONTEXT*    GmmGetGttContext(GMM_GLOBAL_CONTEXT *pGmmLibContext);
653     GMM_CACHE_POLICY_TBL_ELEMENT GmmGetCachePolicyTblElement(GMM_GLOBAL_CONTEXT *pGmmLibContext, GMM_RESOURCE_USAGE_TYPE Usage);
654     GMM_CACHE_POLICY_ELEMENT GmmGetCachePolicyElement(GMM_GLOBAL_CONTEXT *pGmmLibContext, GMM_RESOURCE_USAGE_TYPE Usage);
655     void                GmmSetSkuTable(GMM_GLOBAL_CONTEXT *pGmmLibContext, SKU_FEATURE_TABLE SkuTable);
656     void                GmmSetWaTable(GMM_GLOBAL_CONTEXT *pGmmLibContext, WA_TABLE WaTable);
657     GMM_PLATFORM_INFO*  GmmKmdGetPlatformInfo(GMM_GLOBAL_CONTEXT *pGmmLibContext);
658 
659 #if(_DEBUG || _RELEASE_INTERNAL)
660     const GMM_PLATFORM_INFO* GmmGetOverridePlatformInfo(GMM_GLOBAL_CONTEXT *pGmmLibContext);
661     GMM_TEXTURE_CALC*   GmmGetOverrideTextureCalc(GMM_GLOBAL_CONTEXT *pGmmLibContext);
662 #endif
663 
664 #endif
665 
666 #ifdef __cplusplus
667 }
668 #endif /*__cplusplus*/
669 
670     #define GMM_OVERRIDE_PLATFORM_INFO(pTexInfo,pGmmLibContext)    (GmmGetPlatformInfo(pGmmLibContext))
671     #define GMM_OVERRIDE_TEXTURE_CALC(pTexInfo,pGmmLibContext)     (GmmGetTextureCalc(pGmmLibContext))
672 
673 #ifdef __GMM_KMD__
674     #define GMM_OVERRIDE_EXPORTED_PLATFORM_INFO(pTexInfo)    GMM_OVERRIDE_PLATFORM_INFO(pTexInfo)
675     #define GMM_IS_PLANAR(Format)                            GmmIsPlanar(Format)
676 #else
677     #define GMM_OVERRIDE_EXPORTED_PLATFORM_INFO(pTexInfo,pGmmLibContext)    (&((GmmClientContext*)pClientContext)->GetPlatformInfo())
678     #define GMM_IS_PLANAR(Format)                            (pClientContext->IsPlanar(Format))
679 #endif
680 
681 // Reset packing alignment to project default
682 #pragma pack(pop)
683