xref: /aosp_15_r20/external/intel-media-driver/media_softlet/agnostic/common/hw/mhw_impl.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2021-2022, 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 //! \file     mhw_impl.h
24 //! \brief    MHW impl common defines
25 //! \details
26 //!
27 
28 #ifndef __MHW_IMPL_H__
29 #define __MHW_IMPL_H__
30 
31 #include "mhw_itf.h"
32 #include "mhw_utilities.h"
33 #include "media_class_trace.h"
34 
35 //   [Macro Prefixes]                 |   [Macro Suffixes]
36 //   No Prefix: for external use      |   _T   : type
37 //   _        : for internal use      |   _F   : function name
38 //   __       : intermediate macros   |   _M   : class member name
39 //              only used by other    |   _DECL: declaration
40 //              macros                |   _DEF : definition
41 
42 #define __MHW_CMD_T(CMD) CMD##_CMD  // MHW command type
43 
44 // MHW command parameters and data
45 #define __MHW_CMDINFO_T(CMD) std::pair<_MHW_PAR_T(CMD), typename cmd_t::__MHW_CMD_T(CMD)>
46 
47 #define __MHW_CMDINFO_M(CMD) m_##CMD##_Info
48 
49 #define __MHW_GETPAR_DEF(CMD)                     \
50     __MHW_GETPAR_DECL(CMD) override               \
51     {                                             \
52         return this->__MHW_CMDINFO_M(CMD)->first; \
53     }
54 
55 #define __MHW_GETSIZE_DEF(CMD)                           \
56     __MHW_GETSIZE_DECL(CMD) override                     \
57     {                                                    \
58         return sizeof(typename cmd_t::__MHW_CMD_T(CMD)); \
59     }
60 
61 #if MHW_HWCMDPARSER_ENABLED
62 #define MHW_HWCMDPARSER_INITCMDNAME(CMD) this->m_currentCmdName = #CMD
63 #else
64 #define MHW_HWCMDPARSER_INITCMDNAME(CMD)
65 #endif
66 
67 #define __MHW_ADDCMD_DEF(CMD)                                             \
68     __MHW_ADDCMD_DECL(CMD) override                                       \
69     {                                                                     \
70         MHW_FUNCTION_ENTER;                                               \
71         MHW_HWCMDPARSER_INITCMDNAME(CMD);                                 \
72         return this->AddCmd(cmdBuf,                                       \
73             batchBuf,                                                     \
74             this->__MHW_CMDINFO_M(CMD)->second,                           \
75             [=]() -> MOS_STATUS { return this->__MHW_SETCMD_F(CMD)(); }); \
76     }
77 
78 #if __cplusplus < 201402L
79 #define __MHW_CMDINFO_DEF(CMD) std::unique_ptr<__MHW_CMDINFO_T(CMD)> \
80     __MHW_CMDINFO_M(CMD) = std::unique_ptr<__MHW_CMDINFO_T(CMD)>(new __MHW_CMDINFO_T(CMD)())
81 #else
82 #define __MHW_CMDINFO_DEF(CMD) std::unique_ptr<__MHW_CMDINFO_T(CMD)> \
83     __MHW_CMDINFO_M(CMD) = std::make_unique<__MHW_CMDINFO_T(CMD)>()
84 #endif
85 
86 #define _MHW_CMD_ALL_DEF_FOR_IMPL(CMD) \
87 public:                                \
88     __MHW_GETPAR_DEF(CMD);             \
89     __MHW_GETSIZE_DEF(CMD);            \
90     __MHW_ADDCMD_DEF(CMD)              \
91 protected:                             \
92     __MHW_CMDINFO_DEF(CMD)
93 
94 #define _MHW_SETCMD_OVERRIDE_DECL(CMD) __MHW_SETCMD_DECL(CMD) override
95 
96 #define _MHW_SETCMD_CALLBASE(CMD)                            \
97     MHW_FUNCTION_ENTER;                                      \
98     const auto &params = this->__MHW_CMDINFO_M(CMD)->first;  \
99     auto &      cmd    = this->__MHW_CMDINFO_M(CMD)->second; \
100     MHW_CHK_STATUS_RETURN(base_t::__MHW_SETCMD_F(CMD)())
101 
102 // DWORD location of a command field
103 #define _MHW_CMD_DW_LOCATION(field) \
104     static_cast<uint32_t>((reinterpret_cast<uint32_t *>(&(cmd.field)) - reinterpret_cast<uint32_t *>(&cmd)))
105 
106 #define _MHW_CMD_ASSIGN_FIELD(dw, field, value) cmd.dw.field = (value)
107 #define PATCH_LIST_COMMAND(x)  (x##_NUMBER_OF_ADDRESSES)
108 
109 namespace mhw
110 {
111 class Impl
112 {
113 protected:
114 #if !_MEDIA_RESERVED
115     template <typename T, typename = void>
116     struct HasExtSettings : std::false_type
117     {
118     };
119 
120     template <typename T>
121     struct HasExtSettings<
122         T,
123         decltype(static_cast<T *>(nullptr)->extSettings, void())>
124         : std::true_type
125     {
126     };
127 
128     template <
129         typename P,
130         typename std::enable_if<HasExtSettings<P>::value, bool>::type = true>
131     MOS_STATUS ApplyExtSettings(const P &params, uint32_t *cmd)
132     {
133         for (const auto &func : params.extSettings)
134         {
135             MHW_CHK_STATUS_RETURN(func(cmd));
136         }
137         return MOS_STATUS_SUCCESS;
138     }
139 
140     template <
141         typename P,
142         typename std::enable_if<!HasExtSettings<P>::value, bool>::type = true>
143     MOS_STATUS ApplyExtSettings(const P &, uint32_t *)
144     {
145         return MOS_STATUS_SUCCESS;
146     }
147 #endif  // !_MEDIA_RESERVED
148 
149     static int32_t Clip3(int32_t x, int32_t y, int32_t z)
150     {
151         int32_t ret = 0;
152 
153         if (z < x)
154         {
155             ret = x;
156         }
157         else if (z > y)
158         {
159             ret = y;
160         }
161         else
162         {
163             ret = z;
164         }
165 
166         return ret;
167     }
168 
169     static bool MmcEnabled(MOS_MEMCOMP_STATE state)
170     {
171         return state == MOS_MEMCOMP_RC || state == MOS_MEMCOMP_MC;
172     }
173 
174     static bool MmcRcEnabled(MOS_MEMCOMP_STATE state)
175     {
176         return state == MOS_MEMCOMP_RC;
177     }
178 
179     static uint32_t GetHwTileType(MOS_TILE_TYPE tileType, MOS_TILE_MODE_GMM tileModeGMM, bool gmmTileEnabled)
180     {
181         uint32_t tileMode = 0;
182 
183         if (gmmTileEnabled)
184         {
185             return tileModeGMM;
186         }
187 
188         switch (tileType)
189         {
190         case MOS_TILE_LINEAR:
191             tileMode = 0;
192             break;
193         case MOS_TILE_YS:
194             tileMode = 1;
195             break;
196         case MOS_TILE_X:
197             tileMode = 2;
198             break;
199         default:
200             tileMode = 3;
201             break;
202         }
203 
204         return tileMode;
205     }
206 
207     static uint16_t Fp32_to_Fp16(float value)
208     {
209         //FP32 sign 1 bit, exponent 8 bits, fraction 23 bits
210         //FP16 sign 1 bit, exponent 5 bits, fraction 10 bits
211         uint32_t bits     = *((uint32_t *)&value);
212         uint16_t sign     = (bits >> 31) & 0x1;
213         uint16_t exponent = (bits >> 23) & 0xFF;
214         uint32_t fraction = bits & 0x7FFFFF;
215         int16_t  exp_fp16;
216         if (exponent == 0xFF)
217         {
218             exp_fp16 = 0x1F;
219         }
220         else if (exponent == 0)
221         {
222             exp_fp16 = 0;
223         }
224         else
225         {
226             exp_fp16 = exponent - 127 + 15;
227         }
228         uint16_t fraction_fp16 = (fraction + 0x1000) >> 13;
229         uint16_t result        = (sign << 15) | (exp_fp16 << 10) | fraction_fp16;
230         if (0 == sign)
231         {
232             result = MOS_MIN(result, 0x7bff);  //Infinity:0x7c00
233         }
234         else
235         {
236             result = MOS_MIN(result, 0xfbff);  //-Infinity:0xfc00
237         }
238         return result;
239     }
240 
241     Impl(PMOS_INTERFACE osItf)
242     {
243         MHW_FUNCTION_ENTER;
244 
245         MHW_CHK_NULL_NO_STATUS_RETURN(osItf);
246 
247         m_osItf          = osItf;
248         m_userSettingPtr = osItf->pfnGetUserSettingInstance(osItf);
249         if (m_osItf->bUsesGfxAddress)
250         {
251             AddResourceToCmd = Mhw_AddResourceToCmd_GfxAddress;
252         }
253         else
254         {
255             AddResourceToCmd = Mhw_AddResourceToCmd_PatchList;
256         }
257     }
258 
259     virtual ~Impl()
260     {
261         MHW_FUNCTION_ENTER;
262     }
263 
264     template <typename Cmd, typename CmdSetting>
265     MOS_STATUS AddCmd(PMOS_COMMAND_BUFFER cmdBuf,
266         PMHW_BATCH_BUFFER                 batchBuf,
267         Cmd &                             cmd,
268         const CmdSetting &                setting)
269     {
270         this->m_currentCmdBuf   = cmdBuf;
271         this->m_currentBatchBuf = batchBuf;
272 
273         // set MHW cmd
274         cmd = {};
275         MHW_CHK_STATUS_RETURN(setting());
276 
277         // call MHW cmd parser
278     #if MHW_HWCMDPARSER_ENABLED
279         auto instance = mhw::HwcmdParser::GetInstance();
280         if (instance)
281         {
282             instance->ParseCmd(this->m_currentCmdName,
283                 reinterpret_cast<uint32_t *>(&cmd),
284                 sizeof(cmd) / sizeof(uint32_t));
285         }
286     #endif
287 
288         // add cmd to cmd buffer
289         return Mhw_AddCommandCmdOrBB(m_osItf, cmdBuf, batchBuf, &cmd, sizeof(cmd));
290     }
291 
292 protected:
293     MOS_STATUS(*AddResourceToCmd)
294     (PMOS_INTERFACE osItf, PMOS_COMMAND_BUFFER cmdBuf, PMHW_RESOURCE_PARAMS params) = nullptr;
295 
296     PMOS_INTERFACE              m_osItf           = nullptr;
297     MediaUserSettingSharedPtr   m_userSettingPtr  = nullptr;
298     PMOS_COMMAND_BUFFER         m_currentCmdBuf   = nullptr;
299     PMHW_BATCH_BUFFER           m_currentBatchBuf = nullptr;
300 
301 #if MHW_HWCMDPARSER_ENABLED
302     std::string m_currentCmdName;
303 #endif  // MHW_HWCMDPARSER_ENABLED
304 MEDIA_CLASS_DEFINE_END(mhw__Impl)
305 };
306 }  // namespace mhw
307 
308 #endif  // __MHW_IMPL_H__
309