1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2014-2016 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 #include "nvport/nvport.h"
24 
25 typedef struct _PORT_STATE
26 {
27     NvU32 initCount;
28 } PORT_STATE;
29 static PORT_STATE portState;
30 
31 // RISC-V implementation of atomics requires initialization
32 // Disable initCount atomic operations for RISC-V builds
33 #if PORT_IS_MODULE_SUPPORTED(atomic) && !NVCPU_IS_RISCV64
34 #define PORT_DEC(x) portAtomicDecrementS32((volatile NvS32 *)&x)
35 #define PORT_INC(x) portAtomicIncrementS32((volatile NvS32 *)&x)
36 #else
37 #define PORT_DEC(x) --x
38 #define PORT_INC(x) ++x
39 #endif
40 
41 
42 /// @todo Add better way to initialize all modules
portInitialize(void)43 NV_STATUS portInitialize(void)
44 {
45     if (PORT_INC(portState.initCount) == 1)
46     {
47 #if PORT_IS_MODULE_SUPPORTED(debug)
48         portDbgInitialize();
49 #endif
50 #if PORT_IS_MODULE_SUPPORTED(atomic)
51         portAtomicInit();
52 #endif
53 #if PORT_IS_MODULE_SUPPORTED(sync)
54         portSyncInitialize();
55 #endif
56 #if PORT_IS_MODULE_SUPPORTED(memory)
57         portMemInitialize();
58 #endif
59 #if PORT_IS_MODULE_SUPPORTED(crypto)
60         portCryptoInitialize();
61 #endif
62 #if PORT_IS_MODULE_SUPPORTED(cpu)
63         portCpuInitialize();
64 #endif
65     }
66     return NV_OK;
67 }
68 
portShutdown(void)69 void portShutdown(void)
70 {
71     if (PORT_DEC(portState.initCount) == 0)
72     {
73 #if PORT_IS_MODULE_SUPPORTED(cpu)
74         portCpuShutdown();
75 #endif
76 #if PORT_IS_MODULE_SUPPORTED(crypto)
77         portCryptoShutdown();
78 #endif
79 #if PORT_IS_MODULE_SUPPORTED(memory)
80 #if (!defined(DEBUG) || defined(NV_MODS)) && !NVCPU_IS_RISCV64
81         portMemShutdown(NV_TRUE);
82 #else
83         portMemShutdown(NV_FALSE);
84 #endif
85 #endif
86 #if PORT_IS_MODULE_SUPPORTED(sync)
87         portSyncShutdown();
88 #endif
89 #if PORT_IS_MODULE_SUPPORTED(debug)
90         portDbgShutdown();
91 #endif
92     }
93 }
94 
portIsInitialized(void)95 NvBool portIsInitialized(void)
96 {
97     return portState.initCount > 0;
98 }
99