1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-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 *   Kernel Pmu Module
27 *   This file contains functions managing PMU core on CPU RM
28 *
29 ****************************************************************************/
30 
31 #include "gpu/pmu/kern_pmu.h"
32 
33 NV_STATUS
34 kpmuConstructEngine_IMPL(OBJGPU *pGpu, KernelPmu *pKernelPmu, ENGDESCRIPTOR engDesc)
35 {
36 
37     return kpmuInitLibosLoggingStructures(pGpu, pKernelPmu);
38 }
39 
40 void
41 kpmuDestruct_IMPL(KernelPmu *pKernelPmu)
42 {
43     OBJGPU *pGpu = ENG_GET_GPU(pKernelPmu);
44 
45     kpmuFreeLibosLoggingStructures(pGpu, pKernelPmu);
46 }
47 
48 /*!
49  * Init libos PMU logging
50  */
51 NV_STATUS
52 kpmuInitLibosLoggingStructures_IMPL
53 (
54     OBJGPU *pGpu,
55     KernelPmu *pKernelPmu
56 )
57 {
58     NV_STATUS nvStatus = NV_OK;
59 
60     if (!IS_GSP_CLIENT(pGpu))
61     {
62         return NV_OK;
63     }
64 
65     NV_ASSERT_OR_RETURN(pKernelPmu->pPrintBuf == NULL, NV_ERR_INVALID_STATE);
66 
67     pKernelPmu->pLogElf = NULL;
68 
69     // Allocate print buffer
70     pKernelPmu->printBufSize = PMU_LOG_BUFFER_MAX_SIZE;
71     pKernelPmu->pPrintBuf = portMemAllocNonPaged(pKernelPmu->printBufSize);
72 
73     // Create PMU log
74     libosLogCreateEx(&pKernelPmu->logDecode, "PMU");
75 
76     // Add PMU log buffer (use a fake "task name" - NVRISCV)
77     libosLogAddLogEx(&pKernelPmu->logDecode, pKernelPmu->pPrintBuf, pKernelPmu->printBufSize,
78                      pGpu->gpuInstance, (gpuGetChipArch(pGpu) >> GPU_ARCH_SHIFT), gpuGetChipImpl(pGpu),
79                      "NVRISCV", NULL);
80 
81     // Finish PMU log init (setting the lossless-print flag and resolve-pointers flag)
82     libosLogInitEx(&pKernelPmu->logDecode, pKernelPmu->pLogElf, NV_TRUE, NV_TRUE, NV_TRUE, pKernelPmu->logElfSize);
83 
84     if (nvStatus != NV_OK)
85         kpmuFreeLibosLoggingStructures(pGpu, pKernelPmu);
86     return nvStatus;
87 }
88 
89 /*!
90  * Free libos PMU logging
91  */
92 void
93 kpmuFreeLibosLoggingStructures_IMPL
94 (
95     OBJGPU *pGpu,
96     KernelPmu *pKernelPmu
97 )
98 {
99     if (!IS_GSP_CLIENT(pGpu))
100     {
101         return;
102     }
103 
104     // Destroy PMU log
105     libosLogDestroy(&pKernelPmu->logDecode);
106     portMemFree(pKernelPmu->pPrintBuf);
107     pKernelPmu->pPrintBuf = NULL;
108     portMemFree(pKernelPmu->pLogElf);
109     pKernelPmu->pLogElf = NULL;
110 }
111 
112 /*!
113  * Log a PMU libos log buffer coming from GSP-RM
114  */
115 void
116 kpmuLogBuf_IMPL
117 (
118     OBJGPU *pGpu,
119     KernelPmu *pKernelPmu,
120     NvU8 *pBuf,
121     NvU32 bufSize
122 )
123 {
124     NV_ASSERT_OR_RETURN_VOID(pKernelPmu->pPrintBuf != NULL);
125 
126     portMemCopy(pKernelPmu->pPrintBuf, pKernelPmu->printBufSize,
127                 pBuf, bufSize);
128     libosExtractLogs(&pKernelPmu->logDecode, NV_FALSE);
129 }
130 
131 NV_STATUS kpmuStateInitLocked_IMPL
132 (
133     OBJGPU *pGpu,
134     KernelPmu *pKernelPmu
135 )
136 {
137 
138     return NV_OK;
139 }
140