1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2016-2021 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 "core/core.h"
25 #include "gpu/gpu.h"
26 #include "os/os.h"
27 #include "gpu/mem_mgr/mem_scrub.h"
28 #include "gpu/mem_mgr/mem_mgr.h"
29 #include "gpu/mem_mgr/heap.h"
30 #include "gpu/mem_mgr/mem_desc.h"
31 #include "kernel/gpu/mig_mgr/kernel_mig_manager.h"
32 #include "kernel/gpu/fifo/kernel_fifo.h"
33 
34 /*!
35  * Performs initialization that is dependant on work done in gpuStateLoad() such
36  * as channel initialization.
37  */
38 NV_STATUS
39 memmgrScrubHandlePostSchedulingEnable_GP100
40 (
41     OBJGPU        *pGpu,
42     MemoryManager *pMemoryManager
43 )
44 {
45     Heap            *pHeap     = GPU_GET_HEAP(pGpu);
46     NvBool           bIsMIGEnabled = IS_MIG_ENABLED(pGpu);
47     KernelMIGManager *pKernelMIGManager = GPU_GET_KERNEL_MIG_MANAGER(pGpu);
48     NvBool bIsVgpuLegacyPolicy = (pKernelMIGManager != NULL) && kmigmgrUseLegacyVgpuPolicy(pGpu, pKernelMIGManager);
49 
50     //
51     // Disabling scrub on free for SLI, waiting on the bug:1915380
52     // Bug: 2997744, skipping the top level scrubber since partitions are not created.
53     //
54     if (!IsSLIEnabled(pGpu) &&
55          memmgrIsScrubOnFreeEnabled(pMemoryManager) &&
56          memmgrIsPmaInitialized(pMemoryManager) &&
57          !(bIsMIGEnabled && IS_VIRTUAL(pGpu) && !bIsVgpuLegacyPolicy))
58     {
59         NV_ASSERT_OK_OR_RETURN(scrubberConstruct(pGpu, pHeap));
60     }
61 
62     return NV_OK;
63 }
64 
65 /*!
66  * Performs cleanup on resources that need to be freed before StateUnload routes
67  * are called
68  */
69 NV_STATUS
70 memmgrScrubHandlePreSchedulingDisable_GP100
71 (
72     OBJGPU        *pGpu,
73     MemoryManager *pMemoryManager
74 )
75 {
76     Heap             *pHeap               = GPU_GET_HEAP(pGpu);
77     OBJMEMSCRUB      *pMemscrub           = NULL;
78     NvBool            bIsMIGEnabled       = IS_MIG_ENABLED(pGpu);
79     NvBool            bIsMIGInUse         = IS_MIG_IN_USE(pGpu);
80     KernelMIGManager *pKernelMIGManager   = GPU_GET_KERNEL_MIG_MANAGER(pGpu);
81     NvBool            bIsVgpuLegacyPolicy = (pKernelMIGManager != NULL) && kmigmgrUseLegacyVgpuPolicy(pGpu, pKernelMIGManager);
82 
83     if (!pHeap)
84         return NV_ERR_GENERIC;
85 
86     //
87     // Top level scrubber was allocated with MIG disabled, it must be destroyed
88     // with MIG disabled as well
89     //
90     if (bIsMIGInUse && !(IS_VIRTUAL(pGpu) && bIsVgpuLegacyPolicy))
91         return NV_WARN_MORE_PROCESSING_REQUIRED;
92 
93     pMemscrub = pHeap->pmaObject.pScrubObj;
94 
95     // Bug: 2997744, skipping the top level scrubber since GPU instances are not created.
96     if (!IsSLIEnabled(pGpu) &&
97          memmgrIsScrubOnFreeEnabled(pMemoryManager) &&
98          memmgrIsPmaInitialized(pMemoryManager) &&
99          !(bIsMIGEnabled && IS_VIRTUAL(pGpu) && !bIsVgpuLegacyPolicy))
100     {
101         scrubberDestruct(pGpu, pHeap, pMemscrub);
102         pHeap->pmaObject.pScrubObj = NULL;
103     }
104 
105     return NV_OK;
106 }
107