RK3368 GPU: Rogue N Init.
[firefly-linux-kernel-4.4.55.git] / drivers / staging / imgtec / rogue / rgx_compat_bvnc.c
1 /*************************************************************************/ /*!
2 @File           rgx_compact_bvnc.c
3 @Title          BVNC compatibility check utilities
4 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @Description    Utility functions used for packing BNC and V.
6 @License        Dual MIT/GPLv2
7
8 The contents of this file are subject to the MIT license as set out below.
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 Alternatively, the contents of this file may be used under the terms of
21 the GNU General Public License Version 2 ("GPL") in which case the provisions
22 of GPL are applicable instead of those above.
23
24 If you wish to allow use of your version of this file only under the terms of
25 GPL, and not to allow others to use your version of this file under the terms
26 of the MIT license, indicate your decision by deleting the provisions above
27 and replace them with the notice and other provisions required by GPL as set
28 out in the file called "GPL-COPYING" included in this distribution. If you do
29 not delete the provisions above, a recipient may use your version of this file
30 under the terms of either the MIT license or GPL.
31
32 This License is also included in this distribution in the file called
33 "MIT-COPYING".
34
35 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
36 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
37 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
38 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
39 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
40 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
41 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 */ /**************************************************************************/
43
44 #include "rgx_compat_bvnc.h"
45 #if defined(RGX_FIRMWARE)
46 #include "rgxfw_utils.h"
47 #elif !defined(RGX_BUILD_BINARY)
48 #include "pvr_debug.h"
49 #endif
50
51 #if defined(RGX_FIRMWARE)
52 #define PVR_COMPAT_ASSERT RGXFW_ASSERT
53 #elif !defined(RGX_BUILD_BINARY)
54 #define PVR_COMPAT_ASSERT PVR_ASSERT
55 #else
56 #include <assert.h>
57 #define PVR_COMPAT_ASSERT assert
58 #endif
59
60 /**************************************************************************//**
61  * C library strlen function.
62  *****************************************************************************/
63 static INLINE IMG_UINT32 OSStringLength(const IMG_CHAR* pszInput)
64 {
65         const IMG_CHAR* pszTemp = pszInput;
66
67         while (*pszTemp)
68                 pszTemp++;
69
70         return (pszTemp - pszInput);
71 }
72
73 /**************************************************************************//**
74  * Utility function for packing BNC
75  *****************************************************************************/
76 static INLINE IMG_UINT64 rgx_bnc_pack(IMG_UINT32 ui32B, IMG_UINT32 ui32N,
77                                                                                                                 IMG_UINT32 ui32C)
78 {
79         /*
80          * Test for input B, N and C exceeding max bit width.
81          */
82         PVR_COMPAT_ASSERT((ui32B & (~(RGX_BVNC_PACK_MASK_B >> RGX_BVNC_PACK_SHIFT_B))) == 0);
83         PVR_COMPAT_ASSERT((ui32N & (~(RGX_BVNC_PACK_MASK_N >> RGX_BVNC_PACK_SHIFT_N))) == 0);
84         PVR_COMPAT_ASSERT((ui32C & (~(RGX_BVNC_PACK_MASK_C >> RGX_BVNC_PACK_SHIFT_C))) == 0);
85
86         return (((IMG_UINT64)ui32B << RGX_BVNC_PACK_SHIFT_B) |
87                         ((IMG_UINT64)ui32N << RGX_BVNC_PACK_SHIFT_N) |
88                         ((IMG_UINT64)ui32C << RGX_BVNC_PACK_SHIFT_C));
89 }
90
91 /**************************************************************************//**
92  * Utility function for packing BNC and V to be used by compatibility check.
93  * BNC is packed into 48 bit format.
94  * If the array pointed to by pszV is a string that is shorter than 
95  * ui32OutVMaxLen characters, null characters are appended to the copy in the
96  * array pointed to by pszOutV, until 'ui32OutVMaxLen' characters in all have
97  * been written.
98  *
99  * @param:      pui64OutBNC       Output containing packed BNC.
100  * @param       pszOutV           Output containing version string.
101  * @param       ui32OutVMaxLen    Max characters that can be written to 
102                                   pszOutV (excluding terminating null character)
103  * @param       ui32B             Input 'B' value
104  * @param       pszV              Input 'V' string
105  * @param       ui32N             Input 'N' value
106  * @param       ui32C             Input 'C' value
107  * @return      None
108  *****************************************************************************/
109 void rgx_bvnc_packed(IMG_UINT64 *pui64OutBNC, IMG_CHAR *pszOutV, IMG_UINT32 ui32OutVMaxLen,
110                                          IMG_UINT32 ui32B, IMG_CHAR *pszV, IMG_UINT32 ui32N, IMG_UINT32 ui32C)
111 {
112         *pui64OutBNC = rgx_bnc_pack(ui32B, ui32N, ui32C);
113
114         if (!pszOutV)
115                 return;
116
117         if (pszV)
118         {
119                 /*
120                  * Assert can fail for two reasons
121                  * 1. Caller is passing invalid 'V' string or
122                  * 2. Dest buffer does not have enough memory allocated for max 'V' size.
123                  */
124                 PVR_COMPAT_ASSERT(OSStringLength(pszV) <= ui32OutVMaxLen);
125
126                 
127                 for (; ui32OutVMaxLen > 0 && *pszV != '\0'; --ui32OutVMaxLen)
128                 {
129                         /* When copying the V, omit any characters as these would cause
130                          * the compatibility check against the V read from HW to fail
131                          */
132                         if (*pszV && (*pszV >= '0') && (*pszV <='9'))
133                         {
134                                 *pszOutV++ = *pszV++;
135                         }
136                         else
137                         {
138                                 pszV++;
139                         }
140                 }
141         }
142
143         do
144         {
145                 *pszOutV++ = '\0';
146         }while(ui32OutVMaxLen-- > 0);
147 }
148
149 /**************************************************************************//**
150  * Utility function for packing BNC and V to be used by compatibility check.
151  * Input B,N and C is packed into 48 bit format.
152  * Input V is converted into string. If number of characters required to
153  * represent 16 bit wide version number is less than ui32OutVMaxLen, than null
154  * characters are appended to pszOutV, until ui32OutVMaxLen characters in all 
155  * have been written.
156  *
157  * @param:      pui64OutBNC       Output containing packed BNC.
158  * @param       pszOutV           Output containing version string.
159  * @param       ui32OutVMaxLen    Max characters that can be written to 
160                                   pszOutV (excluding terminating null character)
161  * @param       ui32B             Input 'B' value (16 bit wide)
162  * @param       ui32V             Input 'V' value (16 bit wide)
163  * @param       ui32N             Input 'N' value (16 bit wide)
164  * @param       ui32C             Input 'C' value (16 bit wide)
165  * @return     .None
166  *****************************************************************************/
167 void rgx_bvnc_pack_hw(IMG_UINT64 *pui64OutBNC, IMG_CHAR *pszOutV, IMG_UINT32 ui32OutVMaxLen,
168                                           IMG_UINT32 ui32B, IMG_UINT32 ui32V, IMG_UINT32 ui32N, IMG_UINT32 ui32C)
169 {
170         /*
171          * Allocate space for max digits required to represent 16 bit wide version
172          * number (including NULL terminating character).
173          */
174         IMG_CHAR aszBuf[6];
175         IMG_CHAR *pszPointer = aszBuf;
176
177         *pui64OutBNC = rgx_bnc_pack(ui32B, ui32N, ui32C);
178
179         if (!pszOutV)
180                 return;
181
182         /*
183          * Function only supports 16 bits wide version number.
184          */
185         PVR_COMPAT_ASSERT((ui32V & ~0xFFFF) == 0);
186
187         if (ui32V > 9999)
188                 pszPointer+=5;
189         else if (ui32V > 999)
190                 pszPointer+=4;
191         else if (ui32V > 99)
192                 pszPointer+=3;
193         else if (ui32V > 9)
194                 pszPointer+=2;
195         else
196                 pszPointer+=1;
197         
198         *pszPointer-- = '\0';
199         *pszPointer = '0';
200         
201         while (ui32V > 0)
202         {
203                 *pszPointer-- = (ui32V % 10) + '0';
204                 ui32V /= 10;
205         }
206
207         for (pszPointer = aszBuf; ui32OutVMaxLen > 0 && *pszPointer != '\0'; --ui32OutVMaxLen)
208                 *pszOutV++ = *pszPointer++;
209
210         /*
211          * Append NULL characters.
212          */
213         do
214         {
215                 *pszOutV++ = '\0';
216         }while(ui32OutVMaxLen-- > 0);
217 }
218