1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2022-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 /* ------------------------ Includes ---------------------------------------- */
25 #include "os/os.h"
26 #include "gpu/bif/kernel_bif.h"
27 
28 #include "published/ada/ad102/dev_bus.h"
29 #include "published/ada/ad102/dev_bus_addendum.h"
30 
31 static NvBool _kbifPreOsCheckErotGrantAllowed_AD102(OBJGPU *pGpu, void *pVoid);
32 
33 /*!
34  * Signals preOs to have eRoT hand over control of EEPROM to RM
35  *
36  * @param[in]     pGpu       OBJGPU pointer
37  * @param[in]     pKernelBif KernelBif pointer
38  *
39  * @returns NV_OK if RM has control of the EEPROM
40  * @returns NV_ERR_TIMEOUT if preOs fails to hand over control of the EEPROM
41  *
42  */
43 NV_STATUS
44 kbifPreOsGlobalErotGrantRequest_AD102
45 (
46     OBJGPU    *pGpu,
47     KernelBif *pKernelBif
48 )
49 {
50     NV_STATUS status = NV_OK;
51     NvU32 reg = GPU_REG_RD32(pGpu, NV_PBUS_SW_GLOBAL_EROT_GRANT);
52 
53     // Invalid value suggests that there is no ERoT
54     if (FLD_TEST_DRF(_PBUS, _SW_GLOBAL_EROT_GRANT, _VALID, _NO, reg))
55     {
56         return status;
57     }
58 
59     // Check if grant has already been allowed
60     if (_kbifPreOsCheckErotGrantAllowed_AD102(pGpu, NULL))
61     {
62         return status;
63     }
64 
65     reg = FLD_SET_DRF(_PBUS, _SW_GLOBAL_EROT_GRANT, _REQUEST, _SET, reg);
66     GPU_REG_WR32(pGpu, NV_PBUS_SW_GLOBAL_EROT_GRANT, reg);
67 
68     status = gpuTimeoutCondWait(pGpu, _kbifPreOsCheckErotGrantAllowed_AD102, NULL, NULL);
69     if (status != NV_OK)
70     {
71         NV_PRINTF(LEVEL_ERROR, "Timed out waiting for preOs to grant access to EEPROM\n");
72     }
73 
74     return status;
75 }
76 
77 static NvBool
78 _kbifPreOsCheckErotGrantAllowed_AD102
79 (
80     OBJGPU *pGpu,
81     void   *pVoid
82 )
83 {
84     NvU32 reg = GPU_REG_RD32(pGpu, NV_PBUS_SW_GLOBAL_EROT_GRANT);
85 
86     return FLD_TEST_DRF(_PBUS, _SW_GLOBAL_EROT_GRANT, _ALLOW, _YES, reg);
87 }
88 
89