1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 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 #define NVOC_CRASHCAT_WAYFINDER_H_PRIVATE_ACCESS_ALLOWED
25 #include "crashcat/crashcat_wayfinder.h"
26 #include "crashcat/crashcat_engine.h"
27 #include "crashcat/crashcat_queue.h"
28 #include "crashcat/crashcat_report.h"
29 #include "utils/nvassert.h"
30 #include "nv-crashcat-decoder.h"
31 
crashcatWayfinderSetWFL0_V1(CrashCatWayfinder * pWayfinder,NvU32 wfl0)32 void crashcatWayfinderSetWFL0_V1(CrashCatWayfinder *pWayfinder, NvU32 wfl0)
33 {
34     pWayfinder->v1.wfl0 = wfl0;
35 }
36 
crashcatWayfinderGetReportQueue_V1(CrashCatWayfinder * pWayfinder)37 CrashCatQueue *crashcatWayfinderGetReportQueue_V1(CrashCatWayfinder *pWayfinder)
38 {
39     if (pWayfinder->pQueue != NULL)
40         return pWayfinder->pQueue;
41 
42     //
43     // If we've already decoded WFL1 but don't have a queue, queue control object creation failed
44     // and is unlikely to succeed on subsequent attempts, and we don't want to spam the logs.
45     //
46     if (pWayfinder->v1.wfl1 != 0)
47         return NULL;
48 
49     CrashCatEngine *pEngine = CRASHCAT_GET_ENGINE(pWayfinder);
50     NV_CRASHCAT_SCRATCH_GROUP_ID wfl1Location =
51         crashcatWayfinderL0V1Wfl1Location(pWayfinder->v1.wfl0);
52 
53     // Read the L1 wayfinder to locate the queue
54     const NvU32 *pScratchOffsets = crashcatEngineGetScratchOffsets(pEngine, wfl1Location);
55     if (pScratchOffsets == NULL)
56     {
57         NV_PRINTF(LEVEL_ERROR, "invalid WFL1 scratch location %u\n", wfl1Location);
58         return NULL;
59     }
60 
61     //
62     // In NV_CRASHCAT_WAYFINDER_VERSION_1, the WFL1 contains two 32-bit values specifying the queue
63     // location, so we only need to read two registers. Where the scratch group contains 4
64     // registers, the other two will be used for the queue control.
65     //
66     if ((pScratchOffsets[0] == 0) || (pScratchOffsets[1] == 0))
67     {
68         NV_PRINTF(LEVEL_ERROR, "insufficiently-sized L1 wayfinder scratch location %u\n",
69                   wfl1Location);
70         return NULL;
71     }
72 
73     // Have we already decoded WFL1?
74     if (pWayfinder->v1.wfl1 == 0)
75     {
76         pWayfinder->v1.wfl1 =
77             ((NvU64)crashcatEnginePriRead(pEngine, pScratchOffsets[1]) << 32) |
78                     crashcatEnginePriRead(pEngine, pScratchOffsets[0]);
79     }
80 
81     CrashCatQueueConfig queueConfig;
82 
83     queueConfig.aperture = crashcatWayfinderL1V1QueueAperture(pWayfinder->v1.wfl1);
84     queueConfig.size = crashcatWayfinderL1V1QueueSize(pWayfinder->v1.wfl1);
85     queueConfig.offset = crashcatWayfinderL1V1QueueOffset(pWayfinder->v1.wfl1);
86 
87     if ((pScratchOffsets[2] != 0) && (pScratchOffsets[3] != 0))
88     {
89         //
90         // If the scratch group has at least 4 32-bit registers, the 3rd and 4th are used for queue
91         // put and get, respectively.
92         //
93         queueConfig.putRegOffset = pScratchOffsets[2];
94         queueConfig.getRegOffset = pScratchOffsets[3];
95     }
96     else
97     {
98         //
99         // If there are only 2 scratch registers in the group, the WFL1 is erased and the registers
100         // are reused for queue put and get, respectively.
101         //
102         queueConfig.putRegOffset = pScratchOffsets[0];
103         queueConfig.getRegOffset = pScratchOffsets[1];
104 
105         crashcatEnginePriWrite(pEngine, queueConfig.getRegOffset, 0);
106         crashcatEnginePriWrite(pEngine, queueConfig.putRegOffset, 0);
107 
108         //
109         // Producer watches WFL0 and waits for the _WFL1_LOCATION bits to be set to _NONE
110         // before it will update the queue put pointer.
111         //
112         NvU32 wfl0Offset = crashcatEngineGetWFL0Offset(pEngine);
113         NvU32 wfl0 = FLD_SET_DRF64(_CRASHCAT, _WAYFINDER_L0_V1, _WFL1_LOCATION, _NONE,
114                                    pWayfinder->v1.wfl0);
115         crashcatEnginePriWrite(pEngine, wfl0Offset, wfl0);
116     }
117 
118     // Create the queue control object
119     NV_STATUS status;
120     NV_CHECK_OK_OR_ELSE(status, LEVEL_ERROR,
121         objCreate(&pWayfinder->pQueue, pWayfinder, CrashCatQueue, &queueConfig),
122         return NULL);
123 
124     return pWayfinder->pQueue;
125 }
126