RK3368 GPU: Rogue N Init.
[firefly-linux-kernel-4.4.55.git] / drivers / staging / imgtec / rogue / pvr_bridge_k.c
1 /*************************************************************************/ /*!
2 @File
3 @Title          PVR Bridge Module (kernel side)
4 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @Description    Receives calls from the user portion of services and
6                 despatches them to functions in the kernel portion.
7 @License        Dual MIT/GPLv2
8
9 The contents of this file are subject to the MIT license as set out below.
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
20
21 Alternatively, the contents of this file may be used under the terms of
22 the GNU General Public License Version 2 ("GPL") in which case the provisions
23 of GPL are applicable instead of those above.
24
25 If you wish to allow use of your version of this file only under the terms of
26 GPL, and not to allow others to use your version of this file under the terms
27 of the MIT license, indicate your decision by deleting the provisions above
28 and replace them with the notice and other provisions required by GPL as set
29 out in the file called "GPL-COPYING" included in this distribution. If you do
30 not delete the provisions above, a recipient may use your version of this file
31 under the terms of either the MIT license or GPL.
32
33 This License is also included in this distribution in the file called
34 "MIT-COPYING".
35
36 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
37 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
39 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
40 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
41 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
42 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 */ /**************************************************************************/
44
45 #include <linux/mm_types.h>
46
47 #include "img_defs.h"
48 #include "pvr_bridge.h"
49 #include "connection_server.h"
50 #include "syscommon.h"
51 #include "pvr_debug.h"
52 #include "pvr_debugfs.h"
53 #include "private_data.h"
54 #include "linkage.h"
55 #include "pmr.h"
56 #include "rgx_bvnc_defs_km.h"
57
58 #include <drm/drmP.h>
59 #include "pvr_drm.h"
60 #include "pvr_drv.h"
61
62 #include "env_connection.h"
63 #include <linux/sched.h>
64
65 /* RGX: */
66 #if defined(SUPPORT_RGX)
67 #include "rgx_bridge.h"
68 #endif
69
70 #include "srvcore.h"
71 #include "common_srvcore_bridge.h"
72
73 #if defined(SUPPORT_DRM_EXT)
74 #define CAST_BRIDGE_CMD_PTR_TO_PTR(ptr) (ptr)
75 #else
76 #define CAST_BRIDGE_CMD_PTR_TO_PTR(ptr) (void *)(uintptr_t)(ptr)
77 #endif
78
79 #if defined(MODULE_TEST)
80 /************************************************************************/
81 // additional includes for services testing
82 /************************************************************************/
83 #include "pvr_test_bridge.h"
84 #include "kern_test.h"
85 /************************************************************************/
86 // end of additional includes
87 /************************************************************************/
88 #endif
89
90 /* WARNING!
91  * The mmap code has its own mutex, to prevent a possible deadlock,
92  * when using gPVRSRVLock.
93  * The Linux kernel takes the mm->mmap_sem before calling the mmap
94  * entry points (PVRMMap, MMapVOpen, MMapVClose), but the ioctl
95  * entry point may take mm->mmap_sem during fault handling, or 
96  * before calling get_user_pages.  If gPVRSRVLock was used in the
97  * mmap entry points, a deadlock could result, due to the ioctl
98  * and mmap code taking the two locks in different orders.
99  * As a corollary to this, the mmap entry points must not call
100  * any driver code that relies on gPVRSRVLock is held.
101  */
102 static DEFINE_MUTEX(g_sMMapMutex);
103
104 #if defined(DEBUG_BRIDGE_KM)
105 static PVR_DEBUGFS_ENTRY_DATA *gpsPVRDebugFSBridgeStatsEntry = NULL;
106 static struct seq_operations gsBridgeStatsReadOps;
107 #endif
108
109 /* These will go when full bridge gen comes in */
110 #if defined(PDUMP)
111 PVRSRV_ERROR InitPDUMPCTRLBridge(void);
112 PVRSRV_ERROR DeinitPDUMPCTRLBridge(void);
113 PVRSRV_ERROR InitPDUMPBridge(void);
114 PVRSRV_ERROR DeinitPDUMPBridge(void);
115 PVRSRV_ERROR InitRGXPDUMPBridge(void);
116 PVRSRV_ERROR DeinitRGXPDUMPBridge(void);
117 #endif
118 #if defined(SUPPORT_DISPLAY_CLASS)
119 PVRSRV_ERROR InitDCBridge(void);
120 PVRSRV_ERROR DeinitDCBridge(void);
121 #endif
122 PVRSRV_ERROR InitMMBridge(void);
123 PVRSRV_ERROR DeinitMMBridge(void);
124 #if !defined(EXCLUDE_CMM_BRIDGE)
125 PVRSRV_ERROR InitCMMBridge(void);
126 PVRSRV_ERROR DeinitCMMBridge(void);
127 #endif
128 PVRSRV_ERROR InitPDUMPMMBridge(void);
129 PVRSRV_ERROR DeinitPDUMPMMBridge(void);
130 PVRSRV_ERROR InitSRVCOREBridge(void);
131 PVRSRV_ERROR DeinitSRVCOREBridge(void);
132 PVRSRV_ERROR InitSYNCBridge(void);
133 PVRSRV_ERROR DeinitSYNCBridge(void);
134
135 #if defined(SUPPORT_SERVER_SYNC)
136 #if defined(SUPPORT_INSECURE_EXPORT)
137 PVRSRV_ERROR InitSYNCEXPORTBridge(void);
138 PVRSRV_ERROR DeinitSYNCEXPORTBridge(void);
139 #endif
140 #if defined(SUPPORT_SECURE_EXPORT)
141 PVRSRV_ERROR InitSYNCSEXPORTBridge(void);
142 PVRSRV_ERROR DeinitSYNCSEXPORTBridge(void);
143 #endif
144 #endif /* defined(SUPPORT_SERVER_SYNC) */
145
146 #if defined (SUPPORT_RGX)
147 #if !defined(SUPPORT_KERNEL_SRVINIT)
148 PVRSRV_ERROR InitRGXINITBridge(void);
149 PVRSRV_ERROR DeinitRGXINITBridge(void);
150 #endif
151 PVRSRV_ERROR InitRGXTA3DBridge(void);
152 PVRSRV_ERROR DeinitRGXTA3DBridge(void);
153 PVRSRV_ERROR InitRGXTQBridge(void);
154 PVRSRV_ERROR DeinitRGXTQBridge(void);
155 PVRSRV_ERROR InitRGXTQ2Bridge(void);
156 PVRSRV_ERROR DeinitRGXTQ2Bridge(void);
157 PVRSRV_ERROR InitRGXCMPBridge(void);
158 PVRSRV_ERROR DeinitRGXCMPBridge(void);
159 #if !defined(EXCLUDE_BREAKPOINT_BRIDGE)
160 PVRSRV_ERROR InitBREAKPOINTBridge(void);
161 PVRSRV_ERROR DeinitBREAKPOINTBridge(void);
162 #endif
163 PVRSRV_ERROR InitDEBUGMISCBridge(void);
164 PVRSRV_ERROR DeinitDEBUGMISCBridge(void);
165 PVRSRV_ERROR InitRGXHWPERFBridge(void);
166 PVRSRV_ERROR DeinitRGXHWPERFBridge(void);
167 PVRSRV_ERROR InitRGXRAYBridge(void);
168 PVRSRV_ERROR DeinitRGXRAYBridge(void);
169 #if !defined(EXCLUDE_REGCONFIG_BRIDGE)
170 PVRSRV_ERROR InitREGCONFIGBridge(void);
171 PVRSRV_ERROR DeinitREGCONFIGBridge(void);
172 #endif
173 PVRSRV_ERROR InitTIMERQUERYBridge(void);
174 PVRSRV_ERROR DeinitTIMERQUERYBridge(void);
175 PVRSRV_ERROR InitRGXKICKSYNCBridge(void);
176 PVRSRV_ERROR DeinitRGXKICKSYNCBridge(void);
177 PVRSRV_ERROR InitRGXSIGNALSBridge(void);
178 PVRSRV_ERROR DeinitRGXSIGNALSBridge(void);
179 #endif /* SUPPORT_RGX */
180 PVRSRV_ERROR InitCACHEBridge(void);
181 PVRSRV_ERROR DeinitCACHEBridge(void);
182 #if defined(SUPPORT_SECURE_EXPORT)
183 PVRSRV_ERROR InitSMMBridge(void);
184 PVRSRV_ERROR DeinitSMMBridge(void);
185 #endif
186 #if !defined(EXCLUDE_HTBUFFER_BRIDGE)
187 PVRSRV_ERROR InitHTBUFFERBridge(void);
188 PVRSRV_ERROR DeinitHTBUFFERBridge(void);
189 #endif
190 PVRSRV_ERROR InitPVRTLBridge(void);
191 PVRSRV_ERROR DeinitPVRTLBridge(void);
192 #if defined(PVR_RI_DEBUG)
193 PVRSRV_ERROR InitRIBridge(void);
194 PVRSRV_ERROR DeinitRIBridge(void);
195 #endif
196 #if defined(SUPPORT_PAGE_FAULT_DEBUG)
197 PVRSRV_ERROR InitDEVICEMEMHISTORYBridge(void);
198 PVRSRV_ERROR DeinitDEVICEMEMHISTORYBridge(void);
199 #endif
200 PVRSRV_ERROR InitDMABUFBridge(void);
201 PVRSRV_ERROR DeinitDMABUFBridge(void);
202 #if defined(SUPPORT_VALIDATION_BRIDGE)
203 PVRSRV_ERROR InitVALIDATIONBridge(void);
204 #endif
205
206 #if defined(PVR_TESTING_UTILS)
207 PVRSRV_ERROR InitTUTILSBridge(void);
208 PVRSRV_ERROR DeinitTUTILSBridge(void);
209 #endif
210 #if defined(PVRSRV_ENABLE_FULL_SYNC_TRACKING)
211 PVRSRV_ERROR InitSYNCTRACKINGBridge(void);
212 PVRSRV_ERROR DeinitSYNCTRACKINGBridge(void);
213 #endif
214 #if defined(SUPPORT_WRAP_EXTMEM)
215 PVRSRV_ERROR InitMMEXTMEMBridge(void);
216 PVRSRV_ERROR DeinitMMEXTMEMBridge(void);
217 #endif
218
219 PVRSRV_ERROR
220 DeviceDepBridgeInit(IMG_UINT64 ui64Features)
221 {
222         PVRSRV_ERROR eError;
223
224         if(ui64Features & RGX_FEATURE_COMPUTE_BIT_MASK)
225         {
226                 eError = InitRGXCMPBridge();
227                 if (eError != PVRSRV_OK)
228                 {
229                         return eError;
230                 }
231         }
232
233
234         if(ui64Features & RGX_FEATURE_SIGNAL_SNOOPING_BIT_MASK)
235         {
236                 eError = InitRGXSIGNALSBridge();
237                 if (eError != PVRSRV_OK)
238                 {
239                         return eError;
240                 }
241         }
242
243         if(ui64Features & RGX_FEATURE_RAY_TRACING_BIT_MASK)
244         {
245                 eError = InitRGXRAYBridge();
246                 if (eError != PVRSRV_OK)
247                 {
248                         return eError;
249                 }
250         }
251
252         if(ui64Features & RGX_FEATURE_FASTRENDER_DM_BIT_MASK)
253         {
254                 eError = InitRGXTQ2Bridge();
255                 if (eError != PVRSRV_OK)
256                 {
257                         return eError;
258                 }
259         }
260
261         return PVRSRV_OK;
262 }
263
264
265 PVRSRV_ERROR
266 DeviceDepBridgeDeInit(IMG_UINT64 ui64Features)
267 {
268         PVRSRV_ERROR eError;
269
270         if(ui64Features & RGX_FEATURE_COMPUTE_BIT_MASK)
271         {
272                 eError = DeinitRGXCMPBridge();
273                 if (eError != PVRSRV_OK)
274                 {
275                         return eError;
276                 }
277         }
278
279
280         if(ui64Features & RGX_FEATURE_SIGNAL_SNOOPING_BIT_MASK)
281         {
282                 eError = DeinitRGXSIGNALSBridge();
283                 if (eError != PVRSRV_OK)
284                 {
285                         return eError;
286                 }
287         }
288
289         if(ui64Features & RGX_FEATURE_RAY_TRACING_BIT_MASK)
290         {
291                 eError = DeinitRGXRAYBridge();
292                 if (eError != PVRSRV_OK)
293                 {
294                         return eError;
295                 }
296         }
297
298         if(ui64Features & RGX_FEATURE_FASTRENDER_DM_BIT_MASK)
299         {
300                 eError = DeinitRGXTQ2Bridge();
301                 if (eError != PVRSRV_OK)
302                 {
303                         return eError;
304                 }
305         }
306
307         return PVRSRV_OK;
308 }
309
310
311
312 PVRSRV_ERROR
313 LinuxBridgeInit(void)
314 {
315         PVRSRV_ERROR eError;
316 #if defined(DEBUG_BRIDGE_KM)
317         IMG_INT iResult;
318
319         iResult = PVRDebugFSCreateEntry("bridge_stats",
320                                         NULL,
321                                         &gsBridgeStatsReadOps,
322                                         NULL,
323                                         NULL,
324                                         NULL,
325                                         &g_BridgeDispatchTable[0],
326                                         &gpsPVRDebugFSBridgeStatsEntry);
327         if (iResult != 0)
328         {
329                 return PVRSRV_ERROR_OUT_OF_MEMORY;
330         }
331 #endif
332
333         BridgeDispatchTableStartOffsetsInit();
334
335         eError = InitSRVCOREBridge();
336         if (eError != PVRSRV_OK)
337         {
338                 return eError;
339         }
340
341         eError = InitSYNCBridge();
342         if (eError != PVRSRV_OK)
343         {
344                 return eError;
345         }
346
347 #if defined(SUPPORT_SERVER_SYNC)
348 #if defined(SUPPORT_INSECURE_EXPORT)
349         eError = InitSYNCEXPORTBridge();
350         if (eError != PVRSRV_OK)
351         {
352                 return eError;
353         }
354 #endif
355 #if defined(SUPPORT_SECURE_EXPORT)
356         eError = InitSYNCSEXPORTBridge();
357         if (eError != PVRSRV_OK)
358         {
359                 return eError;
360         }
361 #endif
362 #endif /* defined(SUPPORT_SERVER_SYNC) */
363
364 #if defined(PDUMP)
365         eError = InitPDUMPCTRLBridge();
366         if (eError != PVRSRV_OK)
367         {
368                 return eError;
369         }
370 #endif
371
372         eError = InitMMBridge();
373         if (eError != PVRSRV_OK)
374         {
375                 return eError;
376         }
377 #if !defined(EXCLUDE_CMM_BRIDGE)
378         eError = InitCMMBridge();
379         if (eError != PVRSRV_OK)
380         {
381                 return eError;
382         }
383 #endif
384
385 #if defined(PDUMP)
386         eError = InitPDUMPMMBridge();
387         if (eError != PVRSRV_OK)
388         {
389                 return eError;
390         }
391         eError = InitPDUMPBridge();
392         if (eError != PVRSRV_OK)
393         {
394                 return eError;
395         }
396 #endif
397
398         eError = InitDMABUFBridge();
399         if (eError != PVRSRV_OK)
400         {
401                 return eError;
402         }
403
404 #if defined(SUPPORT_DISPLAY_CLASS)
405         eError = InitDCBridge();
406         if (eError != PVRSRV_OK)
407         {
408                 return eError;
409         }
410 #endif
411
412         eError = InitCACHEBridge();
413         if (eError != PVRSRV_OK)
414         {
415                 return eError;
416         }
417
418 #if defined(SUPPORT_SECURE_EXPORT)
419         eError = InitSMMBridge();
420         if (eError != PVRSRV_OK)
421         {
422                 return eError;
423         }
424 #endif
425
426 #if !defined(EXCLUDE_HTBUFFER_BRIDGE)
427         eError = InitHTBUFFERBridge();
428         if (eError != PVRSRV_OK)
429         {
430                 return eError;
431         }
432 #endif
433
434         eError = InitPVRTLBridge();
435         if (eError != PVRSRV_OK)
436         {
437                 return eError;
438         }
439
440         #if defined(PVR_RI_DEBUG)
441         eError = InitRIBridge();
442         if (eError != PVRSRV_OK)
443         {
444                 return eError;
445         }
446         #endif
447
448 #if defined(SUPPORT_VALIDATION_BRIDGE)
449         eError = InitVALIDATIONBridge();
450         if (eError != PVRSRV_OK)
451         {
452                 return eError;
453         }
454 #endif
455
456 #if defined(PVR_TESTING_UTILS)
457         eError = InitTUTILSBridge();
458         if (eError != PVRSRV_OK)
459         {
460                 return eError;
461         }
462 #endif
463
464 #if defined(SUPPORT_PAGE_FAULT_DEBUG)
465         eError = InitDEVICEMEMHISTORYBridge();
466         if (eError != PVRSRV_OK)
467         {
468                 return eError;
469         }
470 #endif
471
472
473 #if defined(PVRSRV_ENABLE_FULL_SYNC_TRACKING)
474         eError = InitSYNCTRACKINGBridge();
475         if (eError != PVRSRV_OK)
476         {
477                 return eError;
478         }
479 #endif
480
481         #if defined (SUPPORT_RGX)
482
483         eError = InitRGXTQBridge();
484         if (eError != PVRSRV_OK)
485         {
486                 return eError;
487         }
488
489 #if !defined(SUPPORT_KERNEL_SRVINIT)
490         eError = InitRGXINITBridge();
491         if (eError != PVRSRV_OK)
492         {
493                 return eError;
494         }
495 #endif
496
497         eError = InitRGXTA3DBridge();
498         if (eError != PVRSRV_OK)
499         {
500                 return eError;
501         }
502
503 #if !defined(EXCLUDE_BREAKPOINT_BRIDGE)
504         eError = InitBREAKPOINTBridge();
505         if (eError != PVRSRV_OK)
506         {
507                 return eError;
508         }
509 #endif
510
511         eError = InitDEBUGMISCBridge();
512         if (eError != PVRSRV_OK)
513         {
514                 return eError;
515         }
516
517 #if defined(PDUMP)
518         eError = InitRGXPDUMPBridge();
519         if (eError != PVRSRV_OK)
520         {
521                 return eError;
522         }
523 #endif
524
525         eError = InitRGXHWPERFBridge();
526         if (eError != PVRSRV_OK)
527         {
528                 return eError;
529         }
530
531 #if !defined(EXCLUDE_REGCONFIG_BRIDGE)
532         eError = InitREGCONFIGBridge();
533         if (eError != PVRSRV_OK)
534         {
535                 return eError;
536         }
537 #endif
538
539         eError = InitTIMERQUERYBridge();
540         if (eError != PVRSRV_OK)
541         {
542                 return eError;
543         }
544
545         eError = InitRGXKICKSYNCBridge();
546         if (eError != PVRSRV_OK)
547         {
548                 return eError;
549         }
550
551 #endif /* SUPPORT_RGX */
552
553 #if defined(SUPPORT_WRAP_EXTMEM)
554         eError = InitMMEXTMEMBridge();
555         if (eError != PVRSRV_OK)
556         {
557                 return eError;
558         }
559 #endif
560
561         return eError;
562 }
563
564 PVRSRV_ERROR
565 LinuxBridgeDeInit(void)
566 {
567         PVRSRV_ERROR eError;
568
569 #if defined(SUPPORT_WRAP_EXTMEM)
570         eError = DeinitMMEXTMEMBridge();
571         if (eError != PVRSRV_OK)
572         {
573                 return eError;
574         }
575 #endif
576
577 #if defined(DEBUG_BRIDGE_KM)
578         if (gpsPVRDebugFSBridgeStatsEntry != NULL)
579         {
580                 PVRDebugFSRemoveEntry(&gpsPVRDebugFSBridgeStatsEntry);
581         }
582 #endif
583
584         eError = DeinitSRVCOREBridge();
585         if (eError != PVRSRV_OK)
586         {
587                 return eError;
588         }
589
590         eError = DeinitSYNCBridge();
591         if (eError != PVRSRV_OK)
592         {
593                 return eError;
594         }
595
596 #if defined(SUPPORT_SERVER_SYNC)
597 #if defined(SUPPORT_INSECURE_EXPORT)
598         eError = DeinitSYNCEXPORTBridge();
599         if (eError != PVRSRV_OK)
600         {
601                 return eError;
602         }
603 #endif
604 #if defined(SUPPORT_SECURE_EXPORT)
605         eError = DeinitSYNCSEXPORTBridge();
606         if (eError != PVRSRV_OK)
607         {
608                 return eError;
609         }
610 #endif
611 #endif /* defined(SUPPORT_SERVER_SYNC) */
612
613 #if defined(PDUMP)
614         eError = DeinitPDUMPCTRLBridge();
615         if (eError != PVRSRV_OK)
616         {
617                 return eError;
618         }
619 #endif
620
621         eError = DeinitMMBridge();
622         if (eError != PVRSRV_OK)
623         {
624                 return eError;
625         }
626 #if !defined(EXCLUDE_CMM_BRIDGE)
627         eError = DeinitCMMBridge();
628         if (eError != PVRSRV_OK)
629         {
630                 return eError;
631         }
632 #endif
633
634 #if defined(PDUMP)
635         eError = DeinitPDUMPMMBridge();
636         if (eError != PVRSRV_OK)
637         {
638                 return eError;
639         }
640         eError = DeinitPDUMPBridge();
641         if (eError != PVRSRV_OK)
642         {
643                 return eError;
644         }
645 #endif
646
647         eError = DeinitDMABUFBridge();
648         if (eError != PVRSRV_OK)
649         {
650                 return eError;
651         }
652
653 #if defined(PVR_TESTING_UTILS)
654         eError = DeinitTUTILSBridge();
655         if (eError != PVRSRV_OK)
656         {
657                 return eError;
658         }
659 #endif
660
661 #if defined(SUPPORT_DISPLAY_CLASS)
662         eError = DeinitDCBridge();
663         if (eError != PVRSRV_OK)
664         {
665                 return eError;
666         }
667 #endif
668
669         eError = DeinitCACHEBridge();
670         if (eError != PVRSRV_OK)
671         {
672                 return eError;
673         }
674
675 #if defined(SUPPORT_SECURE_EXPORT)
676         eError = DeinitSMMBridge();
677         if (eError != PVRSRV_OK)
678         {
679                 return eError;
680         }
681 #endif
682
683 #if !defined(EXCLUDE_HTBUFFER_BRIDGE)
684         eError = DeinitHTBUFFERBridge();
685         if (eError != PVRSRV_OK)
686         {
687                 return eError;
688         }
689 #endif
690
691         eError = DeinitPVRTLBridge();
692         if (eError != PVRSRV_OK)
693         {
694                 return eError;
695         }
696
697         #if defined(PVR_RI_DEBUG)
698         eError = DeinitRIBridge();
699         if (eError != PVRSRV_OK)
700         {
701                 return eError;
702         }
703         #endif
704
705 #if defined(SUPPORT_PAGE_FAULT_DEBUG)
706         eError = DeinitDEVICEMEMHISTORYBridge();
707         if (eError != PVRSRV_OK)
708         {
709                 return eError;
710         }
711 #endif
712
713
714 #if defined(PVRSRV_ENABLE_FULL_SYNC_TRACKING)
715         eError = DeinitSYNCTRACKINGBridge();
716         if (eError != PVRSRV_OK)
717         {
718                 return eError;
719         }
720 #endif
721
722         #if defined (SUPPORT_RGX)
723
724         eError = DeinitRGXTQBridge();
725         if (eError != PVRSRV_OK)
726         {
727                 return eError;
728         }
729
730
731 #if !defined(SUPPORT_KERNEL_SRVINIT)
732         eError = DeinitRGXINITBridge();
733         if (eError != PVRSRV_OK)
734         {
735                 return eError;
736         }
737 #endif
738
739         eError = DeinitRGXTA3DBridge();
740         if (eError != PVRSRV_OK)
741         {
742                 return eError;
743         }
744
745 #if !defined(EXCLUDE_BREAKPOINT_BRIDGE)
746         eError = DeinitBREAKPOINTBridge();
747         if (eError != PVRSRV_OK)
748         {
749                 return eError;
750         }
751 #endif
752
753         eError = DeinitDEBUGMISCBridge();
754         if (eError != PVRSRV_OK)
755         {
756                 return eError;
757         }
758
759 #if defined(PDUMP)
760         eError = DeinitRGXPDUMPBridge();
761         if (eError != PVRSRV_OK)
762         {
763                 return eError;
764         }
765 #endif
766
767         eError = DeinitRGXHWPERFBridge();
768         if (eError != PVRSRV_OK)
769         {
770                 return eError;
771         }
772
773 #if !defined(EXCLUDE_REGCONFIG_BRIDGE)
774         eError = DeinitREGCONFIGBridge();
775         if (eError != PVRSRV_OK)
776         {
777                 return eError;
778         }
779 #endif
780
781         eError = DeinitTIMERQUERYBridge();
782         if (eError != PVRSRV_OK)
783         {
784                 return eError;
785         }
786
787         eError = DeinitRGXKICKSYNCBridge();
788         if (eError != PVRSRV_OK)
789         {
790                 return eError;
791         }
792
793 #endif /* SUPPORT_RGX */
794
795         return eError;
796 }
797
798 #if defined(DEBUG_BRIDGE_KM)
799 static void *BridgeStatsSeqStart(struct seq_file *psSeqFile, loff_t *puiPosition)
800 {
801         PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY *psDispatchTable = (PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY *)psSeqFile->private;
802
803         OSAcquireBridgeLock();
804
805         if (psDispatchTable == NULL || (*puiPosition) > BRIDGE_DISPATCH_TABLE_ENTRY_COUNT)
806         {
807                 return NULL;
808         }
809
810         if ((*puiPosition) == 0) 
811         {
812                 return SEQ_START_TOKEN;
813         }
814
815         return &(psDispatchTable[(*puiPosition) - 1]);
816 }
817
818 static void BridgeStatsSeqStop(struct seq_file *psSeqFile, void *pvData)
819 {
820         PVR_UNREFERENCED_PARAMETER(psSeqFile);
821         PVR_UNREFERENCED_PARAMETER(pvData);
822
823         OSReleaseBridgeLock();
824 }
825
826 static void *BridgeStatsSeqNext(struct seq_file *psSeqFile,
827                                void *pvData,
828                                loff_t *puiPosition)
829 {
830         PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY *psDispatchTable = (PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY *)psSeqFile->private;
831         loff_t uiItemAskedFor = *puiPosition; /* puiPosition on entry is the index to return */
832
833         PVR_UNREFERENCED_PARAMETER(pvData);
834
835         /* Is the item asked for (starts at 0) a valid table index? */
836         if (uiItemAskedFor < BRIDGE_DISPATCH_TABLE_ENTRY_COUNT)
837         {
838                 (*puiPosition)++; /* on exit it is the next seq index to ask for */
839                 return &(psDispatchTable[uiItemAskedFor]);
840         }
841
842         /* Now passed the end of the table to indicate stop */
843         return NULL;
844 }
845
846 static int BridgeStatsSeqShow(struct seq_file *psSeqFile, void *pvData)
847 {
848         if (pvData == SEQ_START_TOKEN)
849         {
850                 seq_printf(psSeqFile,
851                            "Total ioctl call count = %u\n"
852                            "Total number of bytes copied via copy_from_user = %u\n"
853                            "Total number of bytes copied via copy_to_user = %u\n"
854                            "Total number of bytes copied via copy_*_user = %u\n\n"
855                            "%3s: %-60s | %-48s | %10s | %20s | %20s | %20s | %20s \n",
856                            g_BridgeGlobalStats.ui32IOCTLCount,
857                            g_BridgeGlobalStats.ui32TotalCopyFromUserBytes,
858                            g_BridgeGlobalStats.ui32TotalCopyToUserBytes,
859                            g_BridgeGlobalStats.ui32TotalCopyFromUserBytes + g_BridgeGlobalStats.ui32TotalCopyToUserBytes,
860                            "#",
861                            "Bridge Name",
862                            "Wrapper Function",
863                            "Call Count",
864                            "copy_from_user (B)",
865                            "copy_to_user (B)",
866                            "Total Time (us)",
867                            "Max Time (us)");
868         }
869         else if (pvData != NULL)
870         {
871                 PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY *psEntry = ( PVRSRV_BRIDGE_DISPATCH_TABLE_ENTRY *)pvData;
872                 IMG_UINT32 ui32Remainder;
873
874                 seq_printf(psSeqFile,
875                            "%3d: %-60s   %-48s   %-10u   %-20u   %-20u   %-20llu   %-20llu\n",
876                            (IMG_UINT32)(((size_t)psEntry-(size_t)g_BridgeDispatchTable)/sizeof(*g_BridgeDispatchTable)),
877                            psEntry->pszIOCName,
878                            (psEntry->pfFunction != NULL) ? psEntry->pszFunctionName : "(null)",
879                            psEntry->ui32CallCount,
880                            psEntry->ui32CopyFromUserTotalBytes,
881                            psEntry->ui32CopyToUserTotalBytes,
882                            (unsigned long long) OSDivide64r64(psEntry->ui64TotalTimeNS, 1000, &ui32Remainder),
883                            (unsigned long long) OSDivide64r64(psEntry->ui64MaxTimeNS, 1000, &ui32Remainder));
884         }
885
886         return 0;
887 }
888
889 static struct seq_operations gsBridgeStatsReadOps =
890 {
891         .start = BridgeStatsSeqStart,
892         .stop = BridgeStatsSeqStop,
893         .next = BridgeStatsSeqNext,
894         .show = BridgeStatsSeqShow,
895 };
896 #endif /* defined(DEBUG_BRIDGE_KM) */
897
898 int
899 PVRSRV_BridgeDispatchKM(struct drm_device __maybe_unused *dev, void *arg, struct drm_file *pDRMFile)
900 {
901         struct drm_pvr_srvkm_cmd *psSrvkmCmd = (struct drm_pvr_srvkm_cmd *) arg;
902         PVRSRV_BRIDGE_PACKAGE sBridgePackageKM = { 0 };
903         CONNECTION_DATA *psConnection = LinuxConnectionFromFile(pDRMFile->filp);
904         PVRSRV_ERROR error;
905
906         if(psConnection == NULL)
907         {
908                 PVR_DPF((PVR_DBG_ERROR, "%s: Connection is closed", __FUNCTION__));
909                 return -EFAULT;
910         }
911
912         if(OSGetDriverSuspended())
913         {
914                 return -EINTR;
915         }
916
917         PVR_ASSERT(psSrvkmCmd != NULL);
918
919         DRM_DEBUG("tgid=%d, tgid_connection=%d, bridge_id=%d, func_id=%d",
920                           task_tgid_nr(current),
921                           ((ENV_CONNECTION_DATA *)PVRSRVConnectionPrivateData(psConnection))->owner,
922                           psSrvkmCmd->bridge_id,
923                           psSrvkmCmd->bridge_func_id);
924
925         sBridgePackageKM.ui32BridgeID = psSrvkmCmd->bridge_id;
926         sBridgePackageKM.ui32FunctionID = psSrvkmCmd->bridge_func_id;
927         sBridgePackageKM.ui32Size = sizeof(sBridgePackageKM);
928         sBridgePackageKM.pvParamIn = CAST_BRIDGE_CMD_PTR_TO_PTR(psSrvkmCmd->in_data_ptr);
929         sBridgePackageKM.ui32InBufferSize = psSrvkmCmd->in_data_size;
930         sBridgePackageKM.pvParamOut = CAST_BRIDGE_CMD_PTR_TO_PTR(psSrvkmCmd->out_data_ptr);
931         sBridgePackageKM.ui32OutBufferSize = psSrvkmCmd->out_data_size;
932
933         error =  BridgedDispatchKM(psConnection, &sBridgePackageKM);
934         return OSPVRSRVToNativeError(error);
935 }
936
937 int
938 PVRSRV_MMap(struct file *pFile, struct vm_area_struct *ps_vma)
939 {
940         CONNECTION_DATA *psConnection = LinuxConnectionFromFile(pFile);
941         IMG_HANDLE hSecurePMRHandle = (IMG_HANDLE)((uintptr_t)ps_vma->vm_pgoff);
942         PMR *psPMR;
943         PVRSRV_ERROR eError;
944
945         if(psConnection == NULL)
946         {
947                 PVR_DPF((PVR_DBG_ERROR, "Invalid connection data"));
948                 return -ENOENT;
949         }
950
951         /*
952          * The bridge lock used here to protect PVRSRVLookupHandle is replaced
953          * by a specific lock considering that the handle functions have now
954          * their own lock. This change was necessary to solve the lockdep issues
955          * related with the PVRSRV_MMap.
956          */
957         mutex_lock(&g_sMMapMutex);
958
959         eError = PVRSRVLookupHandle(psConnection->psHandleBase,
960                                                                 (void **)&psPMR,
961                                                                 hSecurePMRHandle,
962                                                                 PVRSRV_HANDLE_TYPE_PHYSMEM_PMR,
963                                                                 IMG_TRUE);
964         if (eError != PVRSRV_OK)
965         {
966                 goto e0;
967         }
968
969         /* Note: PMRMMapPMR will take a reference on the PMR.
970          * Unref the handle immediately, because we have now done
971          * the required operation on the PMR (whether it succeeded or not)
972          */
973         eError = PMRMMapPMR(psPMR, ps_vma);
974         PVRSRVReleaseHandle(psConnection->psHandleBase, hSecurePMRHandle, PVRSRV_HANDLE_TYPE_PHYSMEM_PMR);
975         if (eError != PVRSRV_OK)
976         {
977                 goto e1;
978         }
979
980         mutex_unlock(&g_sMMapMutex);
981
982         return 0;
983
984 e1:
985         PMRUnrefPMR(psPMR);
986         goto em1;
987 e0:
988         PVR_DPF((PVR_DBG_ERROR, "Error in mmap critical section"));
989 em1:
990         mutex_unlock(&g_sMMapMutex);
991
992         PVR_DPF((PVR_DBG_ERROR, "Unable to translate error %d", eError));
993         PVR_ASSERT(eError != PVRSRV_OK);
994
995         return -ENOENT; // -EAGAIN // or what?
996 }