1 /*
2 * Copyright (c) 2017-2021, 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_vdbox_huc_interface.h
24 //! \brief    MHW interface for constructing HUC commands for the Vdbox engine
25 //! \details  Defines the interfaces for constructing MHW Vdbox HUC commands across all platforms
26 //!
27 
28 #ifndef _MHW_VDBOX_HUC_INTERFACE_H_
29 #define _MHW_VDBOX_HUC_INTERFACE_H_
30 
31 #include "mhw_vdbox.h"
32 #include "mhw_mi.h"
33 #include "mhw_vdbox_huc_def.h"
34 
35 typedef struct _MHW_VDBOX_HUC_STREAM_OBJ_PARAMS
36 {
37     uint32_t                    dwIndStreamInLength;
38     uint32_t                    dwIndStreamInStartAddrOffset;
39     bool                        bHucProcessing;
40     uint32_t                    dwIndStreamOutStartAddrOffset;
41     uint8_t                     bStreamOutEnable;
42     uint8_t                     bStreamInEnable;
43     uint8_t                     bEmulPreventionByteRemoval;
44     uint8_t                     bStartCodeSearchEngine;
45     uint8_t                     bLengthModeEnabled;
46     uint8_t                     ucStartCodeByte2;
47     uint8_t                     ucStartCodeByte1;
48     uint8_t                     ucStartCodeByte0;
49 } MHW_VDBOX_HUC_STREAM_OBJ_PARAMS, *PMHW_VDBOX_HUC_STREAM_OBJ_PARAMS;
50 
51 // structure for HuC IMEM_STATE, DMEM_STATE commands
52 typedef struct _MHW_VDBOX_HUC_IMEM_STATE_PARAMS
53 {
54     uint32_t                    dwKernelWopcmOffset;                            // 32KB-aligned kernel offset in WOPCM
55     uint32_t                    dwKernelDescriptor;                             // kernel descriptor
56     PMOS_RESOURCE               presHucBinaryImageBuffer;
57 } MHW_VDBOX_HUC_IMEM_STATE_PARAMS, *PMHW_VDBOX_HUC_IMEM_STATE_PARAMS;
58 
59 typedef struct _MHW_VDBOX_HUC_DMEM_STATE_PARAMS
60 {
61     uint32_t                    dwDataLength;                                   // length in bytes of the HUC data. Must be in increments of 64B
62     uint32_t                    dwDmemOffset;                                   // DMEM offset in the HuC Kernel. This is different for ViperOS vs GEMS.
63     PMOS_RESOURCE               presHucDataSource;                              // resource for HuC data source
64 } MHW_VDBOX_HUC_DMEM_STATE_PARAMS, *PMHW_VDBOX_HUC_DMEM_STATE_PARAMS;
65 
66 // structure for HuC VIRTUAL_ADDR commands
67 typedef struct _MHW_VDBOX_HUC_VIRTUAL_ADDR_PARAMS
68 {
69     MHW_VDBOX_HUC_REGION_PARAMS  regionParams[16];                                 // region [0~15] for VIRTUAL_ADDR command
70 } MHW_VDBOX_HUC_VIRTUAL_ADDR_PARAMS, *PMHW_VDBOX_HUC_VIRTUAL_ADDR_PARAMS;
71 
72 
73 
74 //!  MHW Vdbox Huc interface
75 /*!
76 This class defines the interfaces for constructing Vdbox Huc commands across all platforms
77 */
78 class MhwVdboxHucInterface
79 {
80 protected:
81 
82     PMOS_INTERFACE              m_osInterface = nullptr; //!< Pointer to OS interface
83     MhwMiInterface             *m_MiInterface = nullptr; //!< Pointer to MI interface
84     MhwCpInterface             *m_cpInterface = nullptr; //!< Pointer to CP interface
85     MEDIA_WA_TABLE              *m_waTable = nullptr; //!< Pointer to WA table
86 
87     static const uint32_t  m_hucStatusHevcS2lFailureMask = 0x8000;  //!< HuC Status HEVC short to long failure mask
88                                                                     //!< bit15: uKernal uOS Status, FW will write 0 if has critical error
89 
90     static const uint32_t  m_hucStatus2ImemLoadedMask = 0x40;     //!< HuC Status 2 IMEM loaded mask
91                                                                   //!< bit 6: Valid IMEM Loaded
92     static const uint32_t  m_hucErrorFlagsMask = 0xFFFE;          //!< HuC error 2 flags mask
93 
94     static const uint32_t  m_hevcVdencHucErrorFlagMask = 0x40000000;  //!< HEVC VDEnc Huc error flag mask, bit 30
95 
96     static const uint32_t  m_vp9VdencHucErrorFlagMask  = 0x40000000;  //!< VP9C VDEnc Huc error flag mask, bit 30 (same as hevc)
97 
98     static const uint32_t  m_avcVdencHucErrorFlagMask  = 0x20000000;  //!< AVC VDEnc Huc error flag mask, bit 29
99 
100     static const uint32_t  m_hucStatusReEncodeMask = 0x80000000;  //! HUC PAK Integration kernel reEncode mask.
101 
102     MmioRegistersHuc       m_mmioRegisters[MHW_VDBOX_NODE_MAX] = { };  //!< HuC mmio registers
103 
104     MHW_MEMORY_OBJECT_CONTROL_PARAMS m_cacheabilitySettings[MOS_CODEC_RESOURCE_USAGE_END_CODEC] = { }; //!< Cacheability settings
105 
106    std::shared_ptr<void> m_hucItfNew = nullptr;
107 
108     //!
109     //! \brief    Constructor
110     //!
111     MhwVdboxHucInterface(
112         PMOS_INTERFACE osInterface,
113         MhwMiInterface *miInterface,
114         MhwCpInterface *cpInterface);
115 
116     //!
117     //! \brief    Add a resource to the command buffer
118     //! \details  Internal function to add either a graphics address of a resource or
119     //!           add the resource to the patch list for the requested buffer
120     //!
121     //! \param    [in] osInterface
122     //!           OS interface
123     //! \param    [in] cmdBuffer
124     //!           Command buffer to which resource is added
125     //! \param    [in] params
126     //!           Parameters necessary to add the resource
127     //!
128     //! \return   MOS_STATUS
129     //!           MOS_STATUS_SUCCESS if success, else fail reason
130     //!
131     MOS_STATUS(*AddResourceToCmd) (
132         PMOS_INTERFACE osInterface,
133         PMOS_COMMAND_BUFFER cmdBuffer,
134         PMHW_RESOURCE_PARAMS params);
135 
136 public:
137     //!
138     //! \brief    Destructor
139     //!
~MhwVdboxHucInterface()140     virtual ~MhwVdboxHucInterface() {}
141 
142     //!
143     //! \brief    Get new HUC interface, temporal solution before switching from
144     //!           old interface to new one
145     //!
146     //! \return   pointer to new HUC interface
147     //!
GetNewHucInterface()148     virtual std::shared_ptr<void> GetNewHucInterface() { return nullptr; }
149 
150     //!
151     //! \brief    Get Huc Status Hevc S2l Failure Mask
152     //!
153     //! \return   [out] uint32_t
154     //!           Mask got.
155     //!
GetHucStatusHevcS2lFailureMask()156     inline uint32_t GetHucStatusHevcS2lFailureMask()
157     {
158         return m_hucStatusHevcS2lFailureMask;
159     }
160 
161     //!
162     //! \brief    Get Huc Status2 Imem Loaded Mask
163     //!
164     //! \return   [out] uint32_t
165     //!           Mask got.
166     //!
GetHucStatus2ImemLoadedMask()167     inline uint32_t GetHucStatus2ImemLoadedMask()
168     {
169         return m_hucStatus2ImemLoadedMask;
170     }
171 
172     //!
173     //! \brief    Get Huc Error Flags Mask
174     //!
175     //! \return   [out] uint32_t
176     //!           Mask got.
177     //!
GetHucErrorFlagsMask()178     inline uint32_t GetHucErrorFlagsMask()
179     {
180         return m_hucErrorFlagsMask;
181     }
182 
183     //!
184     //! \brief    Get AVC VDEnc Huc Error Flag Mask
185     //!
186     //! \return   [out] uint32_t
187     //!           Mask got.
188     //!
GetAvcVdencHucErrorFlagMask()189     inline uint32_t GetAvcVdencHucErrorFlagMask()
190     {
191         return m_avcVdencHucErrorFlagMask;
192     }
193 
194     //!
195     //! \brief    Get HEVC VDEnc Huc Error Flag Mask
196     //!
197     //! \return   [out] uint32_t
198     //!           Mask got.
199     //!
GetHevcVdencHucErrorFlagMask()200     inline uint32_t GetHevcVdencHucErrorFlagMask()
201     {
202         return m_hevcVdencHucErrorFlagMask;
203     }
204 
205     //!
206     //! \brief    Get VP9 VDEnc Huc Error Flag Mask
207     //!
208     //! \return   [out] uint32_t
209     //!           Mask got.
210     //!
GetVp9VdencHucErrorFlagMask()211     inline uint32_t GetVp9VdencHucErrorFlagMask()
212     {
213         return m_vp9VdencHucErrorFlagMask;
214     }
215 
216     //!
217     //! \brief    Get Huc Status ReEncode Mask
218     //!
219     //! \return   [out] uint32_t
220     //!           Mask got.
221     //!
GetHucStatusReEncodeMask()222     inline uint32_t GetHucStatusReEncodeMask()
223     {
224         return m_hucStatusReEncodeMask;
225     }
226 
227     //!
228     //! \brief    Get mmio registers
229     //!
230     //! \param    [in] index
231     //!           mmio registers index.
232     //!
233     //! \return   [out] MmioRegistersHuc*
234     //!           mmio registers got.
235     //!
GetMmioRegisters(MHW_VDBOX_NODE_IND index)236     inline MmioRegistersHuc* GetMmioRegisters(MHW_VDBOX_NODE_IND index)
237     {
238         if (index < MHW_VDBOX_NODE_MAX)
239         {
240             return &m_mmioRegisters[index];
241         }
242         else
243         {
244             MHW_ASSERT("index is out of range!");
245             return &m_mmioRegisters[MHW_VDBOX_NODE_1];
246         }
247     }
248 
249     //!
250     //! \brief    Calculates the maximum size for HUC picture level commands
251     //! \details  Client facing function to calculate the maximum size for HUC picture level commands
252     //! \param    [in] mode
253     //!           Indicate the codec mode
254     //! \param    [out] commandsSize
255     //!           The maximum command buffer size
256     //! \param    [out] patchListSize
257     //!           The maximum command patch list size
258     //! \param    [in] params
259     //!           PM HW Vdbox state command size parameters
260     //! \return   MOS_STATUS
261     //!           MOS_STATUS_SUCCESS if success, else fail reason
262     //!
263     virtual MOS_STATUS GetHucStateCommandSize(
264         uint32_t                        mode,
265         uint32_t                        *commandsSize,
266         uint32_t                        *patchListSize,
267         PMHW_VDBOX_STATE_CMDSIZE_PARAMS params) = 0;
268 
269     //!
270     //! \brief    Calculates maximum size for HUC slice/MB level commands
271     //! \details  Client facing function to calculate maximum size for HUC slice/MB level commands
272     //! \param    [in] mode
273     //!           Indicate the codec mode
274     //! \param    [out] commandsSize
275     //!            The maximum command buffer size
276     //! \param    [out] patchListSize
277     //!           The maximum command patch list size
278     //! \return   MOS_STATUS
279     //!           MOS_STATUS_SUCCESS if success, else fail reason
280     //!
281     virtual MOS_STATUS GetHucPrimitiveCommandSize(
282         uint32_t                        mode,
283         uint32_t                        *commandsSize,
284         uint32_t                        *patchListSize) = 0;
285 
286     //!
287     //! \brief    Adds Huc pipe mode select command in command buffer
288     //!
289     //! \param    [in] cmdBuffer
290     //!           Command buffer to which HW command is added
291     //! \param    [in] params
292     //!           Params structure used to populate the HW command
293     //!
294     //! \return   MOS_STATUS
295     //!           MOS_STATUS_SUCCESS if success, else fail reason
296     //!
297     virtual MOS_STATUS AddHucPipeModeSelectCmd(
298         MOS_COMMAND_BUFFER                  *cmdBuffer,
299         MHW_VDBOX_PIPE_MODE_SELECT_PARAMS   *params) = 0;
300 
301     //!
302     //! \brief    Adds Huc IMEM State command in command buffer
303     //!
304     //! \param    [in] cmdBuffer
305     //!           Command buffer to which HW command is added
306     //! \param    [in] params
307     //!           Params structure used to populate the HW command
308     //!
309     //! \return   MOS_STATUS
310     //!           MOS_STATUS_SUCCESS if success, else fail reason
311     //!
312     virtual MOS_STATUS AddHucImemStateCmd(
313         MOS_COMMAND_BUFFER                  *cmdBuffer,
314         MHW_VDBOX_HUC_IMEM_STATE_PARAMS     *params) = 0;
315 
316     //!
317     //! \brief    Adds Huc DMEM State command in command buffer
318     //!
319     //! \param    [in] cmdBuffer
320     //!           Command buffer to which HW command is added
321     //! \param    [in] params
322     //!           Params structure used to populate the HW command
323     //!
324     //! \return   MOS_STATUS
325     //!           MOS_STATUS_SUCCESS if success, else fail reason
326     //!
327     virtual MOS_STATUS AddHucDmemStateCmd(
328         MOS_COMMAND_BUFFER                  *cmdBuffer,
329         MHW_VDBOX_HUC_DMEM_STATE_PARAMS     *params) = 0;
330 
331     //!
332     //! \brief    Adds Huc Virtual Addr State command in command buffer
333     //!
334     //! \param    [in] cmdBuffer
335     //!           Command buffer to which HW command is added
336     //! \param    [in] params
337     //!           Params structure used to populate the HW command
338     //!
339     //! \return   MOS_STATUS
340     //!           MOS_STATUS_SUCCESS if success, else fail reason
341     //!
342     virtual MOS_STATUS AddHucVirtualAddrStateCmd(
343         MOS_COMMAND_BUFFER                  *cmdBuffer,
344         MHW_VDBOX_HUC_VIRTUAL_ADDR_PARAMS   *params) = 0;
345 
346     //!
347     //! \brief    Adds Huc Indirect Object Base Addr State command in command buffer
348     //!
349     //! \param    [in] cmdBuffer
350     //!           Command buffer to which HW command is added
351     //! \param    [in] params
352     //!           Params structure used to populate the HW command
353     //!
354     //! \return   MOS_STATUS
355     //!           MOS_STATUS_SUCCESS if success, else fail reason
356     //!
357     virtual MOS_STATUS AddHucIndObjBaseAddrStateCmd(
358         MOS_COMMAND_BUFFER                  *cmdBuffer,
359         MHW_VDBOX_IND_OBJ_BASE_ADDR_PARAMS  *params) = 0;
360 
361     //!
362     //! \brief    Adds Huc Stream Object command in command buffer
363     //!
364     //! \param    [in] cmdBuffer
365     //!           Command buffer to which HW command is added
366     //! \param    [in] params
367     //!           Params structure used to populate the HW command
368     //!
369     //! \return   MOS_STATUS
370     //!           MOS_STATUS_SUCCESS if success, else fail reason
371     //!
372     virtual MOS_STATUS AddHucStreamObjectCmd(
373         MOS_COMMAND_BUFFER                  *cmdBuffer,
374         MHW_VDBOX_HUC_STREAM_OBJ_PARAMS     *params) = 0;
375 
376     //!
377     //! \brief    Adds Huc Start command in command buffer
378     //!
379     //! \param    [in] cmdBuffer
380     //!           Command buffer to which HW command is added
381     //! \param    [in] lastStreamObject
382     //!           Set last stream object or not
383     //!
384     //! \return   MOS_STATUS
385     //!           MOS_STATUS_SUCCESS if success, else fail reason
386     //!
387     virtual MOS_STATUS AddHucStartCmd(
388         MOS_COMMAND_BUFFER             *cmdBuffer,
389         bool                            lastStreamObject) = 0;
390 
391     //!
392     //! \brief    Set cacheability settings
393     //!
394     //! \param    [in] cacheabilitySettings
395     //!           Cacheability settings
396     //!
397     //! \return   MOS_STATUS
398     //!           MOS_STATUS_SUCCESS if success, else fail reason
399     //!
SetCacheabilitySettings(MHW_MEMORY_OBJECT_CONTROL_PARAMS cacheabilitySettings[MOS_CODEC_RESOURCE_USAGE_END_CODEC])400     MOS_STATUS SetCacheabilitySettings(
401         MHW_MEMORY_OBJECT_CONTROL_PARAMS cacheabilitySettings[MOS_CODEC_RESOURCE_USAGE_END_CODEC])
402     {
403         MHW_FUNCTION_ENTER;
404 
405         uint32_t size = MOS_CODEC_RESOURCE_USAGE_END_CODEC * sizeof(MHW_MEMORY_OBJECT_CONTROL_PARAMS);
406         MOS_STATUS eStatus = MOS_SecureMemcpy(m_cacheabilitySettings, size,
407             cacheabilitySettings, size);
408 
409         return eStatus;
410     }
411 
412     //!
413     //! \brief    Get huc product family
414     //!
415     //! \return   uint32_t
416     //!           Huc product family.
417     //!
418     virtual uint32_t GetHucProductFamily() = 0;
419 };
420 
421 #endif
422