1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2016-2022 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 "kernel/gpu/fifo/usermode_api.h"
25 #include "kernel/gpu/fifo/kernel_fifo.h"
26 
27 #include "class/clc661.h" // HOPPER_USERMODE_A
28 #include "class/cl003e.h" // NV01_MEMORY_SYSTEM
29 #include "class/cl003f.h" // NV01_MEMORY_LOCAL_PRIVILEGED
30 
31 NV_STATUS
32 usrmodeConstruct_IMPL
33 (
34     UserModeApi                  *pUserModeApi,
35     CALL_CONTEXT                 *pCallContext,
36     RS_RES_ALLOC_PARAMS_INTERNAL *pParams
37 )
38 {
39     Memory                      *pMemory        = staticCast(pUserModeApi, Memory);
40     OBJGPU                      *pGpu           = pMemory->pGpu;
41     KernelFifo                  *pKernelFifo    = GPU_GET_KERNEL_FIFO(pGpu);
42     NV_HOPPER_USERMODE_A_PARAMS *pAllocParams   = (NV_HOPPER_USERMODE_A_PARAMS*) pParams->pAllocParams;
43     NvU32                        hClass         = pCallContext->pResourceRef->externalClassId;
44     NvBool                       bBar1Mapping   = NV_FALSE;
45     NvBool                       bPrivMapping   = NV_FALSE;
46     MEMORY_DESCRIPTOR           *pMemDesc       = pKernelFifo->pRegVF;
47 
48     // Copy-construction has already been done by the base Memory class
49     if (RS_IS_COPY_CTOR(pParams))
50     {
51         return NV_OK;
52     }
53 
54     //
55     // We check pKernelFifo->pBar1VF because for some reason RM allows HOPPER_USERMODE_A on ADA.
56     // This is a WAR until we root cause.
57     //
58     if (hClass >= HOPPER_USERMODE_A && pAllocParams != NULL && pKernelFifo->pBar1VF != NULL)
59     {
60         bBar1Mapping = pAllocParams->bBar1Mapping;
61         bPrivMapping = pAllocParams->bPriv;
62     }
63 
64     if (pGpu->getProperty(pGpu, PDB_PROP_GPU_COHERENT_CPU_MAPPING) ||
65         pGpu->getProperty(pGpu, PDB_PROP_GPU_IS_ALL_INST_IN_SYSMEM))
66     {
67         bBar1Mapping = NV_FALSE;
68     }
69 
70     NV_CHECK_OR_RETURN(LEVEL_ERROR,
71         !bPrivMapping || bBar1Mapping,
72         NV_ERR_INVALID_PARAMETER);
73 
74     NV_CHECK_OR_RETURN(LEVEL_ERROR,
75         !bPrivMapping || pCallContext->secInfo.privLevel >= RS_PRIV_LEVEL_KERNEL,
76         NV_ERR_INSUFFICIENT_PERMISSIONS);
77 
78     if (bBar1Mapping)
79     {
80         pMemDesc = bPrivMapping ? pKernelFifo->pBar1PrivVF : pKernelFifo->pBar1VF;
81     }
82 
83     NV_CHECK_OK_OR_RETURN(LEVEL_ERROR,
84             memConstructCommon(pMemory,
85                 bBar1Mapping? NV01_MEMORY_SYSTEM : NV01_MEMORY_LOCAL_PRIVILEGED,
86                 0, pMemDesc, 0, NULL, 0, 0, 0, 0, NVOS32_MEM_TAG_NONE, NULL));
87     memdescAddRef(pMemDesc);
88     return NV_OK;
89 }
90 
91 NvBool
92 usrmodeCanCopy_IMPL(UserModeApi *pUserModeApi){
93     return NV_TRUE;
94 }
95