1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 1993-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "os/os.h"
25 #include "kernel/gpu/mig_mgr/kernel_mig_manager.h"
26 #include "kernel/gpu/fifo/kernel_channel.h"
27 #include "kernel/gpu/nvdec/kernel_nvdec_ctx.h"
28 #include "kernel/gpu/device/device.h"
29 
30 #include "class/cla0b0.h" // NVA0B0_VIDEO_DECODER
31 #include "class/clb0b0.h" // NVB0B0_VIDEO_DECODER
32 #include "class/clb6b0.h" // NVB6B0_VIDEO_DECODER
33 #include "class/clb8b0.h" // NVB8B0_VIDEO_DECODER
34 #include "class/clc1b0.h" // NVC1B0_VIDEO_DECODER
35 #include "class/clc2b0.h" // NVC2B0_VIDEO_DECODER
36 #include "class/clc3b0.h" // NVC3B0_VIDEO_DECODER
37 #include "class/clc4b0.h" // NVC4B0_VIDEO_DECODER
38 #include "class/clc6b0.h" // NVC6B0_VIDEO_DECODER
39 #include "class/clc7b0.h" // NVC7B0_VIDEO_DECODER
40 #include "class/clc9b0.h" // NVC9B0_VIDEO_DECODER
41 
42 /*
43  * This function returns an engine descriptor corresponding to the class
44  * and engine instance passed in.
45  *
46  * @params[in] externalClassId  Id of class being allocated
47  * @params[in] pAllocParams     void pointer containing creation parameters.
48  *
49  * @returns
50  * ENG_INVALID, for unknown engine. Returns the right engine descriptor otherwise.
51  */
52 ENGDESCRIPTOR
nvdecGetEngineDescFromAllocParams(OBJGPU * pGpu,NvU32 externalClassId,void * pAllocParams)53 nvdecGetEngineDescFromAllocParams
54 (
55     OBJGPU  *pGpu,
56     NvU32    externalClassId,
57     void    *pAllocParams
58 )
59 {
60     CALL_CONTEXT *pCallContext = resservGetTlsCallContext();
61     NvU32                        engineInstance    = 0;
62     NV_BSP_ALLOCATION_PARAMETERS *pNvdecAllocParams = pAllocParams;
63 
64     NV_ASSERT_OR_RETURN((pNvdecAllocParams != NULL), ENG_INVALID);
65 
66     if (pNvdecAllocParams->size != sizeof(NV_BSP_ALLOCATION_PARAMETERS))
67     {
68         NV_PRINTF(LEVEL_ERROR, "createParams size mismatch (rm = 0x%x / client = 0x%x)\n",
69                   (NvU32) sizeof(NV_BSP_ALLOCATION_PARAMETERS),
70                   pNvdecAllocParams->size);
71         DBG_BREAKPOINT();
72         return ENG_INVALID;
73     }
74 
75     switch (externalClassId)
76     {
77         case NVA0B0_VIDEO_DECODER:
78         case NVB0B0_VIDEO_DECODER:
79         case NVB6B0_VIDEO_DECODER:
80         case NVC1B0_VIDEO_DECODER:
81         case NVC2B0_VIDEO_DECODER:
82         case NVC3B0_VIDEO_DECODER:
83             engineInstance = 0;
84             break;
85         case NVC4B0_VIDEO_DECODER:
86         case NVC6B0_VIDEO_DECODER:
87         case NVC7B0_VIDEO_DECODER:
88         case NVC9B0_VIDEO_DECODER:
89         case NVB8B0_VIDEO_DECODER:
90             engineInstance = pNvdecAllocParams->engineInstance;
91             break;
92         default:
93             return ENG_INVALID;
94     }
95 
96     if (IS_MIG_IN_USE(pGpu))
97     {
98         KernelMIGManager *pKernelMIGManager = GPU_GET_KERNEL_MIG_MANAGER(pGpu);
99         MIG_INSTANCE_REF ref;
100         RM_ENGINE_TYPE rmEngineType;
101         RsResourceRef *pDeviceRef = NULL;
102 
103         NV_ASSERT_OK(
104             refFindAncestorOfType(pCallContext->pResourceRef,
105                                   classId(Device), &pDeviceRef));
106 
107         if (pDeviceRef == NULL)
108             return ENG_INVALID;
109 
110         NV_ASSERT_OK(
111             kmigmgrGetInstanceRefFromDevice(pGpu, pKernelMIGManager,
112                                             dynamicCast(pDeviceRef->pResource, Device),
113                                             &ref));
114 
115         NV_ASSERT_OK(
116             kmigmgrGetLocalToGlobalEngineType(pGpu, pKernelMIGManager, ref,
117                                               RM_ENGINE_TYPE_NVDEC(engineInstance),
118                                               &rmEngineType));
119         return ENG_NVDEC(RM_ENGINE_TYPE_NVDEC_IDX(rmEngineType));
120     }
121 
122     // Get the right class as per engine instance.
123     return ENG_NVDEC(engineInstance);
124 }
125