1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-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 /******************************************************************************
25  *
26  *   File: mig_monitor_session.c
27  *
28  *   Description:
29  *       Upon successful allocation of this class, a client is granted
30  *       permission to query information across the GPU instances
31  *       irrespective of per-instance MIG subscriptions
32  *
33  *****************************************************************************/
34 
35 #define NVOC_MIG_MONITOR_SESSION_H_PRIVATE_ACCESS_ALLOWED
36 
37 #include "core/core.h"
38 #include "kernel/gpu/mig_mgr/mig_monitor_session.h"
39 #include "class/clc640.h"
40 #include "os/os.h"
41 #include "rmapi/client.h"
42 #include "core/system.h"
43 
44 NV_STATUS
migmonitorsessionConstruct_IMPL(MIGMonitorSession * pMIGMonitorSession,CALL_CONTEXT * pCallContext,RS_RES_ALLOC_PARAMS_INTERNAL * pRmAllocParams)45 migmonitorsessionConstruct_IMPL
46 (
47     MIGMonitorSession            *pMIGMonitorSession,
48     CALL_CONTEXT                 *pCallContext,
49     RS_RES_ALLOC_PARAMS_INTERNAL *pRmAllocParams
50 )
51 {
52     NVC640_ALLOCATION_PARAMETERS *pUserParams = pRmAllocParams->pAllocParams;
53     OBJSYS *pSys = SYS_GET_INSTANCE();
54     NvHandle hClient = pCallContext->pClient->hClient;
55     NV_STATUS status;
56 
57     NV_ASSERT_OR_RETURN(RMCFG_FEATURE_KERNEL_RM, NV_ERR_NOT_SUPPORTED);
58 
59     osRmCapInitDescriptor(&pMIGMonitorSession->dupedCapDescriptor);
60 
61     status = osRmCapAcquire(pSys->pOsRmCaps,
62                             NV_RM_CAP_SYS_SMC_MONITOR,
63                             pUserParams->capDescriptor,
64                             &pMIGMonitorSession->dupedCapDescriptor);
65 
66     //
67     // On platforms where capability isn't implemented,
68     // enforce the admin-only check.
69     //
70     if (status == NV_ERR_NOT_SUPPORTED)
71     {
72         if (!rmclientIsAdminByHandle(hClient, pCallContext->secInfo.privLevel))
73         {
74             NV_PRINTF(LEVEL_ERROR, "insufficient permissions\n");
75             return NV_ERR_INSUFFICIENT_PERMISSIONS;
76         }
77     }
78     else if (status != NV_OK)
79     {
80          NV_PRINTF(LEVEL_ERROR, "Capability validation failed\n");
81          return status;
82     }
83 
84     return NV_OK;
85 }
86 
87 void
migmonitorsessionDestruct_IMPL(MIGMonitorSession * pMIGMonitorSession)88 migmonitorsessionDestruct_IMPL
89 (
90     MIGMonitorSession *pMIGMonitorSession
91 )
92 {
93     osRmCapRelease(pMIGMonitorSession->dupedCapDescriptor);
94 }
95 
96