1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2014-2020 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 "nvstatus.h"
25
26 #if !defined(NV_PRINTF_STRING_SECTION)
27 #if defined(NVRM) && NVOS_IS_LIBOS
28 #include "libos_log.h"
29 #define NV_PRINTF_STRING_SECTION LIBOS_SECTION_LOGGING
30 #else // defined(NVRM) && NVOS_IS_LIBOS
31 #define NV_PRINTF_STRING_SECTION
32 #endif // defined(NVRM) && NVOS_IS_LIBOS
33 #endif // !defined(NV_PRINTF_STRING_SECTION)
34
35 /*
36 * Include nvstatuscodes.h twice. Once for creating constant strings in the
37 * the NV_PRINTF_STRING_SECTION section of the executable, and once to build
38 * the g_StatusCodeList table.
39 */
40 #undef NV_STATUS_CODE
41 #undef SDK_NVSTATUSCODES_H
42 #define NV_STATUS_CODE( name, code, string ) static NV_PRINTF_STRING_SECTION \
43 const char rm_pvt_##name##_str[] = string " [" #name "]";
44 #include "nvstatuscodes.h"
45
46 #undef NV_STATUS_CODE
47 #undef SDK_NVSTATUSCODES_H
48 #define NV_STATUS_CODE( name, code, string ) { name, rm_pvt_##name##_str },
49 static struct NvStatusCodeString
50 {
51 NV_STATUS statusCode;
52 const char *statusString;
53 } g_StatusCodeList[] = {
54 #include "nvstatuscodes.h"
55 { 0xffffffff, "Unknown error code!" } // Some compilers don't like the trailing ','
56 };
57 #undef NV_STATUS_CODE
58
59 /*!
60 * @brief Given an NV_STATUS code, returns the corresponding status string.
61 *
62 * @param[in] nvStatusIn NV_STATUS code for which the string is required
63 *
64 * @returns Corresponding status string from the nvstatuscodes.h
65 *
66 * TODO: Bug 200025711: convert this to an array-indexed lookup, instead of a linear search
67 *
68 */
nvstatusToString(NV_STATUS nvStatusIn)69 const char *nvstatusToString(NV_STATUS nvStatusIn)
70 {
71 static NV_PRINTF_STRING_SECTION const char rm_pvt_UNKNOWN_str[] = "Unknown error code!";
72 NvU32 i;
73 NvU32 n = ((NvU32)(sizeof(g_StatusCodeList))/(NvU32)(sizeof(g_StatusCodeList[0])));
74 for (i = 0; i < n; i++)
75 {
76 if (g_StatusCodeList[i].statusCode == nvStatusIn)
77 {
78 return g_StatusCodeList[i].statusString;
79 }
80 }
81
82 return rm_pvt_UNKNOWN_str;
83 }
84