1 /*==============================================================================
2 Copyright(c) 2019 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 Description: This file contains the class definitions for GmmPageTableMgr
23              for user-mode PageTable management, that is common for both
24              Linux and Windows.
25 
26 ============================================================================*/
27 
28 #pragma once
29 #include "GmmHw.h"
30 
31 #ifndef _WIN32
32 #include <pthread.h>
33 #endif
34 
35 typedef enum _GMM_ENGINE_TYPE
36 {
37     ENGINE_TYPE_RCS = 0,            //RCS
38     ENGINE_TYPE_COMPUTE = 1,        //Compute-CS
39     ENGINE_TYPE_BCS,                //BLT
40     ENGINE_TYPE_VD0,
41     ENGINE_TYPE_VD1,
42     ENGINE_TYPE_VE0
43     //Add all engines supporting  AUX-TT
44 } GMM_ENGINE_TYPE;
45 
46 typedef enum TT_Flags
47 {
48     AUXTT = 1,              //Indicate TT request for AUX i.e. e2e compression
49 } TT_TYPE;
50 
51 
52 #if !(defined(__GMM_KMD__))
53 // Shared Structure for both Windows and Linux
54 typedef struct __GMM_DDI_UPDATEAUXTABLE
55 {
56     GMM_UMD_SYNCCONTEXT * UmdContext;     // [in]  pointer to thread-specific data, specifying BBQHandle/Fence etc
57     GMM_RESOURCE_INFO * BaseResInfo;      // [in]  GmmResourceInfo ptr for compressed resource
58     GMM_RESOURCE_INFO * AuxResInfo;       // [in]  GmmResourceInfo ptr for separate Auxiliary resource
59     GMM_GFX_ADDRESS BaseGpuVA;            // [in]  GPUVA where compressed resource has been mapped
60     GMM_GFX_ADDRESS AuxSurfVA;            // [in]  GPUVA where separate Auxiliary resource has been mapped
61     uint8_t Map;                          // [in]  specifies if resource is being mapped or unmapped
62     uint8_t DoNotWait;                    // [in]  specifies if PageTable update be done on CPU (true) or GPU (false)
63 }GMM_DDI_UPDATEAUXTABLE;
64 
65 #ifdef __cplusplus
66 #include "GmmMemAllocator.hpp"
67 
68 namespace GmmLib
69 {
70     class SyncInfoLin {                       //dummy class
71     public:
72         HANDLE BBQueueHandle;
73         uint64_t BBFence;
SyncInfoLin()74         SyncInfoLin() : BBQueueHandle(NULL), BBFence(0) {}
SyncInfoLin(HANDLE Handle,uint64_t Fence)75         SyncInfoLin(HANDLE Handle, uint64_t Fence) : BBQueueHandle(Handle), BBFence(Fence) {}
76     };
77     typedef class SyncInfoLin  SyncInfo;
78 
79 
80      //Forward class declarations
81      class AuxTable;
82      class GmmPageTablePool;
83      typedef class GmmPageTablePool GMM_PAGETABLEPool;
84 
85      typedef enum POOL_TYPE_REC
86      {
87          POOL_TYPE_TRTTL1  = 0,
88          POOL_TYPE_TRTTL2  = 1,
89          POOL_TYPE_AUXTTL1 = 2,
90          POOL_TYPE_AUXTTL2 = 3,
91      } POOL_TYPE;
92 
93     //////////////////////////////////////////////////////////////////////////////////////////////
94     /// Contains functions and members for GMM_PAGETABLE_MGR, clients must place its pointer in
95     /// their device object. Clients call GmmLib to initialize the instance and use it for mapping
96     /// /unmapping on GmmLib managed page tables (TR-TT for SparseResources, AUX-TT for compression)
97     //////////////////////////////////////////////////////////////////////////////////////////////
98     class GMM_LIB_API NON_PAGED_SECTION GmmPageTableMgr :
99                      public GmmMemAllocator
100     {
101     private:
102         GMM_ENGINE_TYPE EngType;             //PageTable managed @ device-level (specifies engine associated with the device)
103 
104         AuxTable* AuxTTObj;                  //Auxiliary Translation Table obj
105 
106         GMM_PAGETABLEPool *pPool;            //Common page table pool
107         uint32_t NumNodePoolElements;
108         GmmClientContext    *pClientContext;    ///< ClientContext of the client creating this Object
109 
110          //OS-specific defn
111 #ifndef _WIN32
112 	pthread_mutex_t PoolLock;
113 #endif
114     public:
115         GMM_DEVICE_CALLBACKS DeviceCb;       //OS-specific defn: Will be used by Clients to send as input arguments for TR-TT APIs
116         GMM_DEVICE_CALLBACKS_INT DeviceCbInt;       //OS-specific defn: Will be used internally GMM lib
117         GMM_TRANSLATIONTABLE_CALLBACKS TTCb; //OS-specific defn
118         HANDLE hCsr;  // OCL per-device command stream receiver handle for aubcapture
119     public:
120         GmmPageTableMgr();
121         GmmPageTableMgr(GMM_DEVICE_CALLBACKS_INT *, uint32_t TTFlags, GmmClientContext  *pClientContextIn); // Allocates memory for indicate TT’s root-tables, initializes common node-pool
122 
123 
124         //GMM_VIRTUAL GMM_GFX_ADDRESS GetTRL3TableAddr();
125         GMM_VIRTUAL GMM_GFX_ADDRESS GetAuxL3TableAddr();
126 
127         //Update TT root table address in context-image
128         GMM_VIRTUAL GMM_STATUS InitContextAuxTableRegister(HANDLE initialBBHandle, GMM_ENGINE_TYPE engType); //Clients call it to update Aux-Table pointer in context-image, engType reqd. if @ context level
129 
130         //Aux TT management API
131         GMM_VIRTUAL GMM_STATUS UpdateAuxTable(const GMM_DDI_UPDATEAUXTABLE*);      //new API for updating Aux-Table to point to correct 16B-chunk
132                                                                        //for given host page VA  when base/Aux surf is mapped/unmapped
133         GMM_VIRTUAL void __ReleaseUnusedPool(GMM_UMD_SYNCCONTEXT *UmdContext);
134         GMM_VIRTUAL GMM_PAGETABLEPool * __GetFreePoolNode(uint32_t * FreePoolNodeIdx, POOL_TYPE PoolType);
135 
136 
137 #ifndef _WIN32
138         //returns number of BOs for indicated TTs = NumNodePoolElements+1 BOs for root table and pools
139         GMM_VIRTUAL int GetNumOfPageTableBOs(uint8_t TTFlags);
140         //returns BO* list for indicated TT in client allocated memory
141         GMM_VIRTUAL int GetPageTableBOList(uint8_t TTFlags, void* BOList);
142 #endif
143 
144         //Destructor
145         GMM_VIRTUAL ~GmmPageTableMgr();                         //Clean-up page table structures
146 
147         // Inline Functions
148         /////////////////////////////////////////////////////////////////////////////////////
149         /// Returns GmmClientContext associated with this PageTableMgr
150         /// @return ::GmmClientContext
151         /////////////////////////////////////////////////////////////////////////////////////
GetClientContext()152         GMM_INLINE_VIRTUAL GmmClientContext* GetClientContext()
153         {
154             return pClientContext;
155         }
156 
157     private:
158         GMM_PAGETABLEPool * __AllocateNodePool(uint32_t AddrAlignment, POOL_TYPE Type);
159 
GetLibContext()160         GMM_INLINE GMM_LIB_CONTEXT *GetLibContext()
161         {
162             return pClientContext->GetLibContext();
163         }
164 
165     };
166 
167 
168 }
169 #endif  // #ifdef __cplusplus
170 #endif
171