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