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 #ifndef KERNEL_CE_PRIVATE_H
25 #define KERNEL_CE_PRIVATE_H
26 
27 #include "gpu/gpu.h"
28 #include "gpu_mgr/gpu_mgr.h"
29 #include "kernel/gpu/mig_mgr/kernel_mig_manager.h"
30 
31 #define CE_GET_LOWEST_AVAILABLE_IDX(x) \
32     (x == 0 || x == 0xFFFFFFFF) ? 0xFFFFFFFF : portUtilCountTrailingZeros32(x)
33 
34 /*!
35  * @brief Obtain relative CE index.
36  *
37  * @param rmEngineType RM_ENGINE_TYPE_ for this CE, or partition-local engine type.
38  * @param ceIdx CE index in 0..GPU_MAX_CES-1
39  *
40  * @return NV_OK if the conversion is successful.
41  */
42 static NV_INLINE
ceIndexFromType(OBJGPU * pGpu,Device * pDevice,RM_ENGINE_TYPE rmEngineType,NvU32 * ceIdx)43 NV_STATUS ceIndexFromType(OBJGPU *pGpu, Device *pDevice, RM_ENGINE_TYPE rmEngineType, NvU32 *ceIdx)
44 {
45     NV_STATUS status = NV_OK;
46     RM_ENGINE_TYPE localRmEngType = rmEngineType;
47 
48     *ceIdx = GPU_MAX_CES;
49 
50     //
51     // If MIG is enabled, client passes a logical engineId w.r.t its own partition
52     // we need to convert this logical Id to a physical engine Id as we use it
53     // to set runlistId
54     //
55     if (IS_MIG_IN_USE(pGpu))
56     {
57         KernelMIGManager *pKernelMIGManager = GPU_GET_KERNEL_MIG_MANAGER(pGpu);
58         MIG_INSTANCE_REF ref;
59         NvBool bEnginePresent;
60 
61         status = kmigmgrGetInstanceRefFromDevice(pGpu, pKernelMIGManager, pDevice, &ref);
62         if (status != NV_OK)
63             return status;
64 
65         bEnginePresent = kmigmgrIsLocalEngineInInstance(pGpu, pKernelMIGManager, rmEngineType, ref);
66         if (!bEnginePresent)
67             return NV_ERR_INVALID_ARGUMENT;
68 
69         status = kmigmgrGetLocalToGlobalEngineType(pGpu, pKernelMIGManager, ref, rmEngineType, &localRmEngType);
70         if (status != NV_OK)
71             return status;
72     }
73 
74     if (!RM_ENGINE_TYPE_IS_COPY(localRmEngType))
75     {
76         return NV_ERR_INVALID_ARGUMENT;
77     }
78 
79     *ceIdx = RM_ENGINE_TYPE_COPY_IDX(localRmEngType);
80     return status;
81 }
82 
83 #endif // KERNEL_CE_PRIVATE_H
84