RK3368 GPU version Rogue M 1.28
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / rogue_m / services / server / include / lists.h
1 /*************************************************************************/ /*!
2 @File
3 @Title          Linked list shared functions templates.
4 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @Description    Definition of the linked list function templates.
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 #ifndef __LISTS_UTILS__
45 #define __LISTS_UTILS__
46
47 /* instruct QAC to ignore warnings about the following custom formatted macros */
48 /* PRQA S 0881,3410 ++ */
49 #include <stdarg.h>
50 #include "img_types.h"
51 #include "device.h"
52 #include "power.h"
53
54 /*
55  - USAGE -
56
57  The list functions work with any structure that provides the fields psNext and
58  ppsThis. In order to make a function available for a given type, it is required
59  to use the funcion template macro that creates the actual code.
60
61  There are 4 main types of functions:
62  - INSERT       : given a pointer to the head pointer of the list and a pointer to
63                           the node, inserts it as the new head.
64  - REMOVE       : given a pointer to a node, removes it from its list.
65  - FOR EACH     : apply a function over all the elements of a list.
66  - ANY          : apply a function over the elements of a list, until one of them
67                           return a non null value, and then returns it.
68
69  The two last functions can have a variable argument form, with allows to pass
70  additional parameters to the callback function. In order to do this, the
71  callback function must take two arguments, the first is the current node and
72  the second is a list of variable arguments (va_list).
73
74  The ANY functions have also another for wich specifies the return type of the
75  callback function and the default value returned by the callback function.
76
77 */
78
79 /*************************************************************************/ /*!
80 @Function       List_##TYPE##_ForEach
81 @Description    Apply a callback function to all the elements of a list.
82 @Input          psHead        The head of the list to be processed.
83 @Input          pfnCallBack   The function to be applied to each element of the list.
84 */ /**************************************************************************/
85 #define DECLARE_LIST_FOR_EACH(TYPE) \
86 IMG_VOID List_##TYPE##_ForEach(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode))
87
88 #define IMPLEMENT_LIST_FOR_EACH(TYPE) \
89 IMG_VOID List_##TYPE##_ForEach(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode))\
90 {\
91         while(psHead)\
92         {\
93                 pfnCallBack(psHead);\
94                 psHead = psHead->psNext;\
95         }\
96 }
97
98 /*************************************************************************/ /*!
99 @Function       List_##TYPE##_ForEachSafe
100 @Description    Apply a callback function to all the elements of a list. Do it
101                 in a safe way that handles the fact that a node might remove itself
102                 from the list during the iteration.
103 @Input          psHead        The head of the list to be processed.
104 @Input          pfnCallBack   The function to be applied to each element of the list.
105 */ /**************************************************************************/
106 #define DECLARE_LIST_FOR_EACH_SAFE(TYPE) \
107 IMG_VOID List_##TYPE##_ForEachSafe(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode))
108
109 #define IMPLEMENT_LIST_FOR_EACH_SAFE(TYPE) \
110 IMG_VOID List_##TYPE##_ForEachSafe(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode))\
111 {\
112         TYPE *psNext;\
113 \
114         while(psHead)\
115         {\
116                 psNext = psHead->psNext; \
117                 pfnCallBack(psHead);\
118                 psHead = psNext;\
119         }\
120 }
121
122
123 #define DECLARE_LIST_FOR_EACH_VA(TYPE) \
124 IMG_VOID List_##TYPE##_ForEach_va(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode, va_list va), ...)
125
126 #define IMPLEMENT_LIST_FOR_EACH_VA(TYPE) \
127 IMG_VOID List_##TYPE##_ForEach_va(TYPE *psHead, IMG_VOID(*pfnCallBack)(TYPE* psNode, va_list va), ...) \
128 {\
129         va_list ap;\
130         while(psHead)\
131         {\
132                 va_start(ap, pfnCallBack);\
133                 pfnCallBack(psHead, ap);\
134                 psHead = psHead->psNext;\
135                 va_end(ap);\
136         }\
137 }
138
139
140 /*************************************************************************/ /*!
141 @Function       List_##TYPE##_Any
142 @Description    Applies a callback function to the elements of a list until the function
143                 returns a non null value, then returns it.
144 @Input          psHead        The head of the list to be processed.
145 @Input          pfnCallBack   The function to be applied to each element of the list.
146 @Return         The first non null value returned by the callback function.
147 */ /**************************************************************************/
148 #define DECLARE_LIST_ANY(TYPE) \
149 IMG_VOID* List_##TYPE##_Any(TYPE *psHead, IMG_VOID* (*pfnCallBack)(TYPE* psNode))
150
151 #define IMPLEMENT_LIST_ANY(TYPE) \
152 IMG_VOID* List_##TYPE##_Any(TYPE *psHead, IMG_VOID* (*pfnCallBack)(TYPE* psNode))\
153 { \
154         IMG_VOID *pResult;\
155         TYPE *psNextNode;\
156         pResult = IMG_NULL;\
157         psNextNode = psHead;\
158         while(psHead && !pResult)\
159         {\
160                 psNextNode = psNextNode->psNext;\
161                 pResult = pfnCallBack(psHead);\
162                 psHead = psNextNode;\
163         }\
164         return pResult;\
165 }
166
167
168 /*with variable arguments, that will be passed as a va_list to the callback function*/
169
170 #define DECLARE_LIST_ANY_VA(TYPE) \
171 IMG_VOID* List_##TYPE##_Any_va(TYPE *psHead, IMG_VOID*(*pfnCallBack)(TYPE* psNode, va_list va), ...)
172
173 #define IMPLEMENT_LIST_ANY_VA(TYPE) \
174 IMG_VOID* List_##TYPE##_Any_va(TYPE *psHead, IMG_VOID*(*pfnCallBack)(TYPE* psNode, va_list va), ...)\
175 {\
176         va_list ap;\
177         TYPE *psNextNode;\
178         IMG_VOID* pResult = IMG_NULL;\
179         while(psHead && !pResult)\
180         {\
181                 psNextNode = psHead->psNext;\
182                 va_start(ap, pfnCallBack);\
183                 pResult = pfnCallBack(psHead, ap);\
184                 va_end(ap);\
185                 psHead = psNextNode;\
186         }\
187         return pResult;\
188 }
189
190 /*those ones are for extra type safety, so there's no need to use castings for the results*/
191
192 #define DECLARE_LIST_ANY_2(TYPE, RTYPE, CONTINUE) \
193 RTYPE List_##TYPE##_##RTYPE##_Any(TYPE *psHead, RTYPE (*pfnCallBack)(TYPE* psNode))
194
195 #define IMPLEMENT_LIST_ANY_2(TYPE, RTYPE, CONTINUE) \
196 RTYPE List_##TYPE##_##RTYPE##_Any(TYPE *psHead, RTYPE (*pfnCallBack)(TYPE* psNode))\
197 { \
198         RTYPE result;\
199         TYPE *psNextNode;\
200         result = CONTINUE;\
201         psNextNode = psHead;\
202         while(psHead && result == CONTINUE)\
203         {\
204                 psNextNode = psNextNode->psNext;\
205                 result = pfnCallBack(psHead);\
206                 psHead = psNextNode;\
207         }\
208         return result;\
209 }
210
211
212 #define DECLARE_LIST_ANY_VA_2(TYPE, RTYPE, CONTINUE) \
213 RTYPE List_##TYPE##_##RTYPE##_Any_va(TYPE *psHead, RTYPE(*pfnCallBack)(TYPE* psNode, va_list va), ...)
214
215 #define IMPLEMENT_LIST_ANY_VA_2(TYPE, RTYPE, CONTINUE) \
216 RTYPE List_##TYPE##_##RTYPE##_Any_va(TYPE *psHead, RTYPE(*pfnCallBack)(TYPE* psNode, va_list va), ...)\
217 {\
218         va_list ap;\
219         TYPE *psNextNode;\
220         RTYPE result = CONTINUE;\
221         while(psHead && result == CONTINUE)\
222         {\
223                 psNextNode = psHead->psNext;\
224                 va_start(ap, pfnCallBack);\
225                 result = pfnCallBack(psHead, ap);\
226                 va_end(ap);\
227                 psHead = psNextNode;\
228         }\
229         return result;\
230 }
231
232
233 /*************************************************************************/ /*!
234 @Function       List_##TYPE##_Remove
235 @Description    Removes a given node from the list.
236 @Input          psNode      The pointer to the node to be removed.
237 */ /**************************************************************************/
238 #define DECLARE_LIST_REMOVE(TYPE) \
239 IMG_VOID List_##TYPE##_Remove(TYPE *psNode)
240
241 #define IMPLEMENT_LIST_REMOVE(TYPE) \
242 IMG_VOID List_##TYPE##_Remove(TYPE *psNode)\
243 {\
244         (*psNode->ppsThis)=psNode->psNext;\
245         if(psNode->psNext)\
246         {\
247                 psNode->psNext->ppsThis = psNode->ppsThis;\
248         }\
249 }
250
251 /*************************************************************************/ /*!
252 @Function       List_##TYPE##_Insert
253 @Description    Inserts a given node at the beginnning of the list.
254 @Input          psHead   The pointer to the pointer to the head node.
255 @Input          psNode   The pointer to the node to be inserted.
256 */ /**************************************************************************/
257 #define DECLARE_LIST_INSERT(TYPE) \
258 IMG_VOID List_##TYPE##_Insert(TYPE **ppsHead, TYPE *psNewNode)
259
260 #define IMPLEMENT_LIST_INSERT(TYPE) \
261 IMG_VOID List_##TYPE##_Insert(TYPE **ppsHead, TYPE *psNewNode)\
262 {\
263         psNewNode->ppsThis = ppsHead;\
264         psNewNode->psNext = *ppsHead;\
265         *ppsHead = psNewNode;\
266         if(psNewNode->psNext)\
267         {\
268                 psNewNode->psNext->ppsThis = &(psNewNode->psNext);\
269         }\
270 }
271
272 /*************************************************************************/ /*!
273 @Function       List_##TYPE##_Reverse
274 @Description    Reverse a list in place
275 @Input          ppsHead    The pointer to the pointer to the head node.
276 */ /**************************************************************************/
277 #define DECLARE_LIST_REVERSE(TYPE) \
278 IMG_VOID List_##TYPE##_Reverse(TYPE **ppsHead)
279
280 #define IMPLEMENT_LIST_REVERSE(TYPE) \
281 IMG_VOID List_##TYPE##_Reverse(TYPE **ppsHead)\
282 {\
283     TYPE *psTmpNode1; \
284     TYPE *psTmpNode2; \
285     TYPE *psCurNode; \
286         psTmpNode1 = IMG_NULL; \
287         psCurNode = *ppsHead; \
288         while(psCurNode) { \
289         psTmpNode2 = psCurNode->psNext; \
290         psCurNode->psNext = psTmpNode1; \
291                 psTmpNode1 = psCurNode; \
292                 psCurNode = psTmpNode2; \
293                 if(psCurNode) \
294                 { \
295                         psTmpNode1->ppsThis = &(psCurNode->psNext); \
296                 } \
297                 else \
298                 { \
299                         psTmpNode1->ppsThis = ppsHead;          \
300                 } \
301         } \
302         *ppsHead = psTmpNode1; \
303 }
304
305 #define IS_LAST_ELEMENT(x) ((x)->psNext == IMG_NULL)
306
307
308 DECLARE_LIST_ANY(PVRSRV_DEVICE_NODE);
309 DECLARE_LIST_ANY_2(PVRSRV_DEVICE_NODE, PVRSRV_ERROR, PVRSRV_OK);
310 DECLARE_LIST_ANY_VA(PVRSRV_DEVICE_NODE);
311 DECLARE_LIST_ANY_VA_2(PVRSRV_DEVICE_NODE, PVRSRV_ERROR, PVRSRV_OK);
312 DECLARE_LIST_FOR_EACH(PVRSRV_DEVICE_NODE);
313 DECLARE_LIST_FOR_EACH_VA(PVRSRV_DEVICE_NODE);
314 DECLARE_LIST_INSERT(PVRSRV_DEVICE_NODE);
315 DECLARE_LIST_REMOVE(PVRSRV_DEVICE_NODE);
316
317 DECLARE_LIST_ANY_VA(PVRSRV_POWER_DEV);
318 DECLARE_LIST_ANY_VA_2(PVRSRV_POWER_DEV, PVRSRV_ERROR, PVRSRV_OK);
319 DECLARE_LIST_INSERT(PVRSRV_POWER_DEV);
320 DECLARE_LIST_REMOVE(PVRSRV_POWER_DEV);
321
322 #undef DECLARE_LIST_ANY_2
323 #undef DECLARE_LIST_ANY_VA
324 #undef DECLARE_LIST_ANY_VA_2
325 #undef DECLARE_LIST_FOR_EACH
326 #undef DECLARE_LIST_FOR_EACH_VA
327 #undef DECLARE_LIST_INSERT
328 #undef DECLARE_LIST_REMOVE
329
330 IMG_VOID* MatchDeviceKM_AnyVaCb(PVRSRV_DEVICE_NODE* psDeviceNode, va_list va);
331 IMG_VOID* MatchPowerDeviceIndex_AnyVaCb(PVRSRV_POWER_DEV *psPowerDev, va_list va);
332
333 #endif
334
335 /* re-enable warnings */
336 /* PRQA S 0881,3410 -- */