1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-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 "core/core.h"
25 #include "gpu/gpu.h"
26 #include "gpu/mem_mgr/mem_mgr.h"
27 #include "gpu/mmu/kern_gmmu.h"
28 #include "gpu/bus/kern_bus.h"
29 #include "gpu/gsp/gsp_static_config.h"
30 #include "vgpu/vgpu_events.h"
31 #include <ctrl/ctrl2080/ctrl2080fb.h>
32 #include "gpu/mem_mgr/fermi_dma.h"
33 #include "nvoc/prelude.h"
34 
35 /*!
36  * @brief Initialize FB regions from static info obtained from GSP FW. Also,
37  *        initialize region table related RAM fields.
38  */
39 NV_STATUS
memmgrInitBaseFbRegions_FWCLIENT(OBJGPU * pGpu,MemoryManager * pMemoryManager)40 memmgrInitBaseFbRegions_FWCLIENT
41 (
42     OBJGPU        *pGpu,
43     MemoryManager *pMemoryManager
44 )
45 {
46     NV2080_CTRL_CMD_FB_GET_FB_REGION_INFO_PARAMS *pFbRegionInfoParams;
47     NV2080_CTRL_CMD_FB_GET_FB_REGION_FB_REGION_INFO *pFbRegionInfo;
48     GspStaticConfigInfo *pGSCI = GPU_GET_GSP_STATIC_INFO(pGpu);
49     NvU64 bias;
50     NvU32 i;
51 
52     // sanity checks
53     if (pGSCI == NULL)
54     {
55         NV_PRINTF(LEVEL_ERROR, "Missing static info.\n");
56 
57         return NV_ERR_INVALID_STATE;
58     }
59 
60     pFbRegionInfoParams = &pGSCI->fbRegionInfoParams;
61     if (pFbRegionInfoParams->numFBRegions == 0)
62     {
63         NV_PRINTF(LEVEL_ERROR,
64                   "Missing FB region table in GSP Init arguments.\n");
65 
66         return NV_ERR_INVALID_PARAMETER;
67     }
68 
69     if (pFbRegionInfoParams->numFBRegions > MAX_FB_REGIONS)
70     {
71         NV_PRINTF(LEVEL_ERROR,
72                   "Static info struct has more FB regions (%u) than FB supports (%u).\n",
73                   pFbRegionInfoParams->numFBRegions, MAX_FB_REGIONS);
74 
75         return NV_ERR_INVALID_PARAMETER;
76     }
77 
78     pMemoryManager->Ram.reservedMemSize = 0;
79     pMemoryManager->Ram.fbUsableMemSize = 0;
80 
81     // Copy FB regions from static info structure
82     for (i = 0; i < pFbRegionInfoParams->numFBRegions; i++)
83     {
84         pFbRegionInfo = &pFbRegionInfoParams->fbRegion[i];
85         pMemoryManager->Ram.fbRegion[i].base               = pFbRegionInfo->base;
86         pMemoryManager->Ram.fbRegion[i].limit              = pFbRegionInfo->limit;
87         pMemoryManager->Ram.fbRegion[i].bProtected         = pFbRegionInfo->bProtected;
88         pMemoryManager->Ram.fbRegion[i].bInternalHeap      = NV_FALSE;
89         pMemoryManager->Ram.fbRegion[i].performance        = pFbRegionInfo->performance;
90         pMemoryManager->Ram.fbRegion[i].bSupportCompressed = pFbRegionInfo->supportCompressed;
91         pMemoryManager->Ram.fbRegion[i].bSupportISO        = pFbRegionInfo->supportISO;
92         pMemoryManager->Ram.fbRegion[i].rsvdSize           = pFbRegionInfo->reserved;
93 
94         if (pFbRegionInfo->reserved)
95         {
96             pMemoryManager->Ram.fbRegion[i].bRsvdRegion = NV_TRUE;
97             pMemoryManager->Ram.reservedMemSize += pMemoryManager->Ram.fbRegion[i].rsvdSize;
98         }
99         else
100         {
101             pMemoryManager->Ram.fbRegion[i].bRsvdRegion = NV_FALSE;
102             pMemoryManager->Ram.fbUsableMemSize += (pMemoryManager->Ram.fbRegion[i].limit -
103                                             pMemoryManager->Ram.fbRegion[i].base + 1);
104         }
105     }
106     pMemoryManager->Ram.numFBRegions = pFbRegionInfoParams->numFBRegions;
107 
108     // Round up to the closest megabyte.
109     bias = (1 << 20) - 1;
110     //
111     // fbTotalMemSizeMb was set to fbUsableMemSize. However, in RM-offload,
112     // GSP-RM reserves some FB regions for its own usage, thus fbUsableMemSize
113     // won't represent the exact FB size. Instead, we are taking the FB size
114     // from the static info provided by GSP-RM.
115     //
116     pMemoryManager->Ram.fbTotalMemSizeMb  = (pGSCI->fb_length + bias) >> 20;
117     pMemoryManager->Ram.fbAddrSpaceSizeMb =
118         (pMemoryManager->Ram.fbRegion[pFbRegionInfoParams->numFBRegions - 1].limit + bias) >> 20;
119 
120     NV_ASSERT(pMemoryManager->Ram.fbAddrSpaceSizeMb >= pMemoryManager->Ram.fbTotalMemSizeMb);
121 
122     // Dump some stats, region table is dumped in memsysStateLoad
123     NV_PRINTF(LEVEL_INFO, "FB Memory from Static info:\n");
124     NV_PRINTF(LEVEL_INFO, "Reserved Memory=0x%llx, Usable Memory=0x%llx\n",
125               pMemoryManager->Ram.reservedMemSize, pMemoryManager->Ram.fbUsableMemSize);
126     NV_PRINTF(LEVEL_INFO, "fbTotalMemSizeMb=0x%llx, fbAddrSpaceSizeMb=0x%llx\n",
127               pMemoryManager->Ram.fbTotalMemSizeMb, pMemoryManager->Ram.fbAddrSpaceSizeMb);
128 
129     return NV_OK;
130 }
131