summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/nvrm/core/common/nvrm_keylist.c
blob: 8471e134366db1d503d3c6e86c28521b2a2fa110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (c) 2008-2009 NVIDIA Corporation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the NVIDIA Corporation nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "nvcommon.h"
#include "nvutil.h"
#include "nvrm_keylist.h"

#define NVRM_KEY_ARRAY_LEN 15

// Key node structure
typedef struct KeyRec
{
    NvU32 KeyID[NVRM_KEY_ARRAY_LEN];
    NvU32 Value[NVRM_KEY_ARRAY_LEN];
    NvU32 Count;
    struct KeyRec *pNextKey;
}Key;

// Pointer to the Key Linked-List
static Key s_InitialKeyList;
static Key* s_pKeyList = NULL;

// Handle to mutex for tread safety
static NvOsMutexHandle s_Mutex = NULL;

// Add a new key node to the existing list
static NvError AddKeyToList(NvU32 KeyID, NvU32 Value);

// Frees the Linked-List
static void FreeKeyList(void);

void NvRmPrivDeInitKeyList(NvRmDeviceHandle hRm);

NvError NvRmPrivInitKeyList(
    NvRmDeviceHandle hRm,
    const NvU32 * InitialValues,
    NvU32 InitialCount);

NvError NvRmPrivInitKeyList(NvRmDeviceHandle hRm, 
                            const NvU32 *InitialValues,
                            NvU32 InitialCount)
{
    NvError Error;
    NvU32 i;

    Error = NvOsMutexCreate(&s_Mutex);
    if (Error!=NvSuccess)
        return Error;

    if (!s_pKeyList)
    {
        s_pKeyList = &s_InitialKeyList;
        s_InitialKeyList.Count = 0;
        s_InitialKeyList.pNextKey = NULL;
        for (i=0; i<InitialCount; i++)
        {
            AddKeyToList(NvOdmKeyListId_ReservedAreaStart + i, 
                InitialValues[i]);
        }
    }

    // Creating the Mutex
    return Error;
}

void NvRmPrivDeInitKeyList(NvRmDeviceHandle hRm)
{
    NvOsMutexLock(s_Mutex);
    FreeKeyList();
    s_pKeyList = NULL;
    NvOsMutexUnlock(s_Mutex);
    NvOsMutexDestroy(s_Mutex);
}

NvU32 NvRmGetKeyValue(NvRmDeviceHandle hRm, NvU32 KeyID)
{
    Key *pList = s_pKeyList;
    NvU32 Value = 0;
    unsigned int i;

    NvOsMutexLock(s_Mutex);
    while (pList)
    {
        for (i=0; i<pList->Count; i++)
        {
            if (pList->KeyID[i] == KeyID)
            {
                Value = pList->Value[i];
                goto cleanup;
            }
        }
        pList = pList->pNextKey;
    }
cleanup:
    NvOsMutexUnlock(s_Mutex);
    // Returning value as 0 since key is not present
    return Value;
}

NvError NvRmSetKeyValuePair(NvRmDeviceHandle hRm, NvU32 KeyID, NvU32 Value)
{
    Key *pList = s_pKeyList;
    NvError e = NvSuccess;
    unsigned int i;

    if (KeyID >= NvOdmKeyListId_ReservedAreaStart &&
        KeyID <= NvOdmKeyListId_ReservedAreaEnd)
        return NvError_NotSupported;

    NvOsMutexLock(s_Mutex);
    // Checking if key already exists
    while (pList)
    {
        for (i=0; i<pList->Count; i++)
        {
            if (pList->KeyID[i] == KeyID)
            {
                pList->Value[i] = Value;
                goto cleanup;
            }
        }
        pList = pList->pNextKey;
    }
    // Adding The new key to the list
    e = AddKeyToList(KeyID, Value);
cleanup:
    NvOsMutexUnlock(s_Mutex);
    return e;
}


NvError AddKeyToList(NvU32 KeyID, NvU32 Value)
{
    Key *pList;

    if (s_pKeyList->Count < NVRM_KEY_ARRAY_LEN)
    {
        s_pKeyList->KeyID[s_pKeyList->Count] = KeyID;
        s_pKeyList->Value[s_pKeyList->Count] = Value;
        s_pKeyList->Count++;
    }
    else
    {
        pList = NvOsAlloc(sizeof(Key));

        if (pList == NULL)
            return NvError_InsufficientMemory;

        pList->KeyID[0] = KeyID;
        pList->Value[0] = Value;
        pList->Count = 1;
        pList->pNextKey = s_pKeyList;
        s_pKeyList = pList;
    }

    return NvSuccess;
}

void FreeKeyList(void)
{
    Key *pTemp = s_pKeyList;
    while (s_pKeyList != &s_InitialKeyList)
    {
        pTemp = s_pKeyList->pNextKey ;
        NvOsFree(s_pKeyList);
        s_pKeyList = pTemp;
    }
}