aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/ice4100/sgx535/system/sgx_intel_ce/sysconfig.c
blob: 08363df026789ea2446d1489a1ef8a4762c52dcd (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
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
/**********************************************************************
 *
 * Copyright (c) 2008-2010 Intel Corporation.
 * Copyright(c) 2008 Imagination Technologies Ltd. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful but, except
 * as otherwise stated in writing, without any warranty; without even the
 * implied warranty of merchantability or fitness for a particular purpose.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * The full GNU General Public License is included in this distribution in
 * the file called "COPYING".
 *
 * Contact Information:
 *     Intel Corporation
 *     2200 Mission College Blvd.
 *     Santa Clara, CA  97052
 *
 ******************************************************************************/

/* This file was based on the Imagination Technologies sample implementation. */

#include "sgxdefs.h"
#include "services_headers.h"
#include "kerneldisplay.h"
#include "oemfuncs.h"
#include "sgxinfo.h"
#include "pdump_km.h"
#include "sgxinfokm.h"
#include "syslocal.h"

#ifdef __linux__
#include "mm.h"
#endif

#ifdef	LDM_PCI
#include "linux/pci.h"
#endif

#ifdef INTEL_D3_PM
#include "graphics_pm.h"
#endif

#include "linux/pci.h"

#define SYS_SGX_CLOCK_SPEED					(400000000)
#define SYS_SGX_HWRECOVERY_TIMEOUT_FREQ		(100)
#define SYS_SGX_PDS_TIMER_FREQ				(1000)
#define SYS_SGX_ACTIVE_POWER_LATENCY_MS		(500)

SYS_DATA *gpsSysData = (SYS_DATA *) NULL;
SYS_DATA gsSysData;

static SYS_SPECIFIC_DATA gsSysSpecificData;

static u32 gui32SGXDeviceID;
static SGX_DEVICE_MAP gsSGXDeviceMap;

#ifdef	LDM_PCI
extern struct pci_dev *gpsPVRLDMDev;
#endif

u32 PVRSRV_BridgeDispatchKM(u32 Ioctl,
			unsigned char * pInBuf,
			u32 InBufLen,
			unsigned char * pOutBuf,
			u32 OutBufLen, u32 * pdwBytesTransferred);

static PVRSRV_ERROR InitSGXPCIDev(SYS_DATA *psSysData)
{
	SYS_SPECIFIC_DATA *psSysSpecData = (SYS_SPECIFIC_DATA *) psSysData->pvSysSpecificData;

	struct pci_dev *psPCIDev = NULL;
	psPCIDev = pci_get_device(SYS_SGX_DEV_VENDOR_ID, SYS_SGX_DEV_DEVICE_ID, NULL);
	if (psPCIDev != NULL)
	{
		if (pci_enable_device(psPCIDev))
			goto init_failed;

		psSysSpecData->psSGXPCIDev = psPCIDev;

		return PVRSRV_OK;
	}

init_failed:
	psSysSpecData->psSGXPCIDev = NULL;
	PVR_DPF((PVR_DBG_ERROR,"InitSGXPCIDev: failed to initialize the sgx PCI device!\n"));
	return PVRSRV_ERROR_GENERIC;
}

static void DeinitSGXPCIDev(SYS_DATA *psSysData)
{
	SYS_SPECIFIC_DATA *psSysSpecData = (SYS_SPECIFIC_DATA *) psSysData->pvSysSpecificData;

	if (psSysSpecData->psSGXPCIDev != NULL)
	{
		pci_disable_device(psSysSpecData->psSGXPCIDev);
	}
}

static PVRSRV_ERROR GetSGXPCIDevIRQ(SYS_DATA *psSysData, u32 *pui32IRQ)
{
	SYS_SPECIFIC_DATA *psSysSpecData = (SYS_SPECIFIC_DATA *) psSysData->pvSysSpecificData;

	if (psSysSpecData->psSGXPCIDev != NULL)
	{
		*pui32IRQ = psSysSpecData->psSGXPCIDev->irq;
		return PVRSRV_OK;
	}

	return PVRSRV_ERROR_GENERIC;
}


static PVRSRV_ERROR SysLocateDevices(SYS_DATA * psSysData)
{
	u32 ui32SGXRegBaseAddr, ui32SGXMemBaseAddr;
	u32 ui32IRQ;

#ifdef INTEL_D3_NO_PCI_ENUM
	ui32SGXRegBaseAddr = 0xDC000000;
	ui32IRQ = 4;
#else

#define SGX_PCI_BUS 1
#define SGX_PCI_DEV 2
#define SGX_PCI_FN  0

#define BAR0 0x10

	ui32SGXRegBaseAddr = OSPCIReadDword(SGX_PCI_BUS,
					    SGX_PCI_DEV, SGX_PCI_FN, BAR0);
	if(GetSGXPCIDevIRQ(psSysData, &ui32IRQ) != PVRSRV_OK)
	{
		PVR_DPF((PVR_DBG_ERROR, "sysLocateDevices: failed to get the pci IRQ!"));
		return PVRSRV_ERROR_INVALID_DEVICE;
	}

#endif

	// This address was only ever used in the addr translation functions. We took
	// that out though, so it isn't used anywhere
#ifdef INTEL_D3_LOCALMEM
	ui32SGXMemBaseAddr = 0x18000000;
#else
	ui32SGXMemBaseAddr = 0;
#endif

	PVR_TRACE(("IRQ: %d", ui32IRQ));

	gsSGXDeviceMap.ui32Flags = 0x0;

	gsSGXDeviceMap.sRegsSysPBase.uiAddr =
	    ui32SGXRegBaseAddr + SYS_SGX_REG_OFFSET;
	//gsSGXDeviceMap.sRegsDevPBase = SysSysPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX, gsSGXDeviceMap.sRegsSysPBase);
	gsSGXDeviceMap.sRegsCpuPBase =
	    SysSysPAddrToCpuPAddr(gsSGXDeviceMap.sRegsSysPBase);
	gsSGXDeviceMap.ui32RegsSize = SYS_SGX_REG_SIZE;

	gsSGXDeviceMap.sLocalMemSysPBase.uiAddr = ui32SGXMemBaseAddr;
	gsSGXDeviceMap.sLocalMemDevPBase =
	    SysSysPAddrToDevPAddr(PVRSRV_DEVICE_TYPE_SGX,
				  gsSGXDeviceMap.sLocalMemSysPBase);
	gsSGXDeviceMap.sLocalMemCpuPBase =
	    SysSysPAddrToCpuPAddr(gsSGXDeviceMap.sLocalMemSysPBase);
	gsSGXDeviceMap.ui32LocalMemSize = SYS_LOCALMEM_FOR_SGX_RESERVE_SIZE;

	gsSGXDeviceMap.ui32IRQ = ui32IRQ;

	return PVRSRV_OK;
}

PVRSRV_ERROR SysInitialise(void)
{
	u32 i;
	PVRSRV_ERROR eError;
	PVRSRV_DEVICE_NODE *psDeviceNode;
	SGX_TIMING_INFORMATION *psTimingInfo;

	gpsSysData = &gsSysData;
	memset(gpsSysData, 0, sizeof(SYS_DATA));

	gpsSysData->pvSysSpecificData = &gsSysSpecificData;
	gsSysSpecificData.ui32SysSpecificData = 0;
#ifdef	LDM_PCI

	PVR_ASSERT(gpsPVRLDMDev != NULL);
	gsSysSpecificData.psPCIDev = gpsPVRLDMDev;
#endif

	eError = OSInitEnvData(&gpsSysData->pvEnvSpecificData);
	if (eError != PVRSRV_OK) {
		PVR_DPF((PVR_DBG_ERROR,
			 "SysInitialise: Failed to setup env structure"));
		SysDeinitialise(gpsSysData);
		gpsSysData = NULL;
		return eError;
	}
	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
			      SYS_SPECIFIC_DATA_ENABLE_ENVDATA);

	psTimingInfo = &gsSGXDeviceMap.sTimingInfo;
	psTimingInfo->ui32CoreClockSpeed = SYS_SGX_CLOCK_SPEED;
	psTimingInfo->ui32HWRecoveryFreq = SYS_SGX_HWRECOVERY_TIMEOUT_FREQ;
#if defined(SUPPORT_ACTIVE_POWER_MANAGEMENT)
	psTimingInfo->bEnableActivePM = 1;
#else
	psTimingInfo->bEnableActivePM = 0;
#endif
	psTimingInfo->ui32ActivePowManLatencyms =
	    SYS_SGX_ACTIVE_POWER_LATENCY_MS;
	psTimingInfo->ui32uKernelFreq = SYS_SGX_PDS_TIMER_FREQ;

	eError = InitSGXPCIDev(gpsSysData);
	if (eError != PVRSRV_OK)
	{
		SysDeinitialise(gpsSysData);
		gpsSysData = NULL;
		return eError;
	}


	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
			      SYS_SPECIFIC_DATA_ENABLE_PCINIT);

	gpsSysData->ui32NumDevices = SYS_DEVICE_COUNT;

	for (i = 0; i < SYS_DEVICE_COUNT; i++) {
		gpsSysData->sDeviceID[i].uiID = i;
		gpsSysData->sDeviceID[i].bInUse = 0;
	}

	gpsSysData->psDeviceNodeList = NULL;
	gpsSysData->psQueueList = NULL;

	eError = SysInitialiseCommon(gpsSysData);
	if (eError != PVRSRV_OK) {
		PVR_DPF((PVR_DBG_ERROR,
			 "SysInitialise: Failed in SysInitialiseCommon"));
		SysDeinitialise(gpsSysData);
		gpsSysData = NULL;
		return eError;
	}

	eError = SysLocateDevices(gpsSysData);
	if (eError != PVRSRV_OK) {
		PVR_DPF((PVR_DBG_ERROR,
			 "SysInitialise: Failed to locate devices"));
		SysDeinitialise(gpsSysData);
		gpsSysData = NULL;
		return eError;
	}
	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
			      SYS_SPECIFIC_DATA_ENABLE_LOCATEDEV);

	eError = SysInitRegisters();
	if (eError != PVRSRV_OK) {
		PVR_DPF((PVR_DBG_ERROR,
			 "SysInitRegisters: Failed to initialise registers"));
		SysDeinitialise(gpsSysData);
		gpsSysData = NULL;
		return eError;
	}
	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
			      SYS_SPECIFIC_DATA_ENABLE_INITREG);

#if defined(READ_TCF_HOST_MEM_SIGNATURE)
	SysTestTCFHostMemSig(gsSGXDeviceMap.sRegsCpuPBase,
			     gsSGXDeviceMap.sLocalMemCpuPBase);
#endif

	eError =
	    PVRSRVRegisterDevice(gpsSysData, SGXRegisterDevice, 0x00000001,
				 &gui32SGXDeviceID);
	if (eError != PVRSRV_OK) {
		PVR_DPF((PVR_DBG_ERROR,
			 "SysInitialise: Failed to register device!"));
		SysDeinitialise(gpsSysData);
		gpsSysData = NULL;
		return eError;
	}
	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
			      SYS_SPECIFIC_DATA_ENABLE_REGDEV);

#ifdef INTEL_D3_LOCALMEM
	gpsSysData->apsLocalDevMemArena[0] = RA_Create("SGXLocalDeviceMemory",
						       gsSGXDeviceMap.
						       sLocalMemSysPBase.uiAddr,
						       gsSGXDeviceMap.
						       ui32LocalMemSize, NULL,
						       HOST_PAGESIZE(), NULL,
						       NULL, NULL, NULL);
#else
	gpsSysData->apsLocalDevMemArena[0] = NULL;
#endif

	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
			      SYS_SPECIFIC_DATA_ENABLE_RA_ARENA);

	psDeviceNode = gpsSysData->psDeviceNodeList;
	while (psDeviceNode) {
		switch (psDeviceNode->sDevId.eDeviceType) {
		case PVRSRV_DEVICE_TYPE_SGX:
			{
				DEVICE_MEMORY_INFO *psDevMemoryInfo;
				DEVICE_MEMORY_HEAP_INFO *psDeviceMemoryHeap;

				psDeviceNode->psLocalDevMemArena =
				    gpsSysData->apsLocalDevMemArena[0];

				psDevMemoryInfo = &psDeviceNode->sDevMemoryInfo;
				psDeviceMemoryHeap =
				    psDevMemoryInfo->psDeviceMemoryHeap;

				for (i = 0; i < psDevMemoryInfo->ui32HeapCount;
				     i++) {
#ifdef INTEL_D3_LOCALMEM
					psDeviceMemoryHeap[i].ui32Attribs |=
					    PVRSRV_BACKINGSTORE_LOCALMEM_CONTIG;
#else
					psDeviceMemoryHeap[i].ui32Attribs |=
					    PVRSRV_BACKINGSTORE_SYSMEM_NONCONTIG;
					//psDeviceMemoryHeap[i].ui32Attribs |= PVRSRV_BACKINGSTORE_SYSMEM_CONTIG;
#endif
					psDeviceMemoryHeap[i].
					    psLocalDevMemArena =
					    gpsSysData->apsLocalDevMemArena[0];

				}
				break;
			}
		default:
			break;
		}

		psDeviceNode = psDeviceNode->psNext;
	}

	PDUMPINIT();
	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
			      SYS_SPECIFIC_DATA_ENABLE_PDUMPINIT);

	eError = PVRSRVInitialiseDevice(gui32SGXDeviceID);
	if (eError != PVRSRV_OK) {
		PVR_DPF((PVR_DBG_ERROR,
			 "SysInitialise: Failed to initialise device!"));
		SysDeinitialise(gpsSysData);
		gpsSysData = NULL;
		return eError;
	}
	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
			      SYS_SPECIFIC_DATA_ENABLE_INITDEV);

#ifdef INTEL_D3_PM
	{
		int pm_rc = graphics_pm_init();
		if (GRAPHICS_PM_OK != pm_rc) {
			eError = PVRSRV_ERROR_INIT_FAILURE;
			PVR_DPF((PVR_DBG_ERROR,
				 "SysInitialise: Failed to initialise power management!"));
			SysDeinitialise(gpsSysData);
			gpsSysData = NULL;
			return eError;
		}
		SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
				      SYS_SPECIFIC_DATA_ENABLE_GRAPHICS_PM);
	}
#endif

	PVR_DPF((PVR_DBG_WARNING, "SysInitialise: OK 0x%x",
		 gsSysSpecificData.ui32SysSpecificData));
	return PVRSRV_OK;
}

PVRSRV_ERROR SysFinalise(void)
{
#if defined(SYS_USING_INTERRUPTS)
	PVRSRV_ERROR eError;

	eError = OSInstallMISR(gpsSysData);
	if (eError != PVRSRV_OK) {
		PVR_DPF((PVR_DBG_ERROR,
			 "OSInstallMISR: Failed to install MISR"));
		SysDeinitialise(gpsSysData);
		gpsSysData = NULL;
		return eError;
	}
	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
			      SYS_SPECIFIC_DATA_ENABLE_MISR);

	eError = OSInstallSystemLISR(gpsSysData, gsSGXDeviceMap.ui32IRQ);
	if (eError != PVRSRV_OK) {
		PVR_DPF((PVR_DBG_ERROR,
			 "OSInstallSystemLISR: Failed to install ISR"));
		OSUninstallMISR(gpsSysData);
		SysDeinitialise(gpsSysData);
		gpsSysData = NULL;
		return eError;
	}
	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
			      SYS_SPECIFIC_DATA_ENABLE_LISR);
	SysEnableInterrupts(gpsSysData);
	SYS_SPECIFIC_DATA_SET(&gsSysSpecificData, SYS_SPECIFIC_DATA_ENABLE_IRQ);
#endif

	gpsSysData->pszVersionString =
	    SysCreateVersionString(gsSGXDeviceMap.sRegsCpuPBase);
	if (!gpsSysData->pszVersionString) {
		PVR_DPF((PVR_DBG_ERROR,
			 "SysFinalise: Failed to create a system version string"));
	} else {
		PVR_DPF((PVR_DBG_WARNING, "SysFinalise: Version string: %s",
			 gpsSysData->pszVersionString));
	}

	return PVRSRV_OK;
}

PVRSRV_ERROR SysDeinitialise(SYS_DATA * psSysData)
{
	SYS_SPECIFIC_DATA *psSysSpecData;
	PVRSRV_ERROR eError;

	if (psSysData == NULL) {
		PVR_DPF((PVR_DBG_ERROR,
			 "SysDeinitialise: Called with NULL SYS_DATA pointer.  Probably called before."));
		return PVRSRV_OK;
	}

	psSysSpecData = (SYS_SPECIFIC_DATA *) psSysData->pvSysSpecificData;

#ifdef INTEL_D3_PM
	if (SYS_SPECIFIC_DATA_TEST
	    (psSysSpecData, SYS_SPECIFIC_DATA_ENABLE_GRAPHICS_PM)) {
		int pm_rc = graphics_pm_deinit();
		if (GRAPHICS_PM_OK != pm_rc) {
			PVR_DPF((PVR_DBG_ERROR,
				 "SysDeinitialise: Failed to deinitialise power management!"));
			/* Continue with deinit even if failed to deinit power management */
		}
	}
#endif

#if defined(SYS_USING_INTERRUPTS)
	if (SYS_SPECIFIC_DATA_TEST(psSysSpecData, SYS_SPECIFIC_DATA_ENABLE_IRQ)) {
		SysDisableInterrupts(psSysData);
	}
	if (SYS_SPECIFIC_DATA_TEST
	    (psSysSpecData, SYS_SPECIFIC_DATA_ENABLE_LISR)) {
		eError = OSUninstallSystemLISR(psSysData);
		if (eError != PVRSRV_OK) {
			PVR_DPF((PVR_DBG_ERROR,
				 "SysDeinitialise: OSUninstallSystemLISR failed"));
			return eError;
		}
	}
	if (SYS_SPECIFIC_DATA_TEST
	    (psSysSpecData, SYS_SPECIFIC_DATA_ENABLE_MISR)) {
		eError = OSUninstallMISR(psSysData);
		if (eError != PVRSRV_OK) {
			PVR_DPF((PVR_DBG_ERROR,
				 "SysDeinitialise: OSUninstallMISR failed"));
			return eError;
		}
	}
#endif
	if (SYS_SPECIFIC_DATA_TEST
	    (psSysSpecData, SYS_SPECIFIC_DATA_ENABLE_INITDEV)) {
		eError = PVRSRVDeinitialiseDevice(gui32SGXDeviceID);
		if (eError != PVRSRV_OK) {
			PVR_DPF((PVR_DBG_ERROR,
				 "SysDeinitialise: failed to de-init the device"));
			return eError;
		}
	}

	if (SYS_SPECIFIC_DATA_TEST
	    (psSysSpecData, SYS_SPECIFIC_DATA_ENABLE_RA_ARENA)) {
#ifdef INTEL_D3_LOCALMEM
		RA_Delete(gpsSysData->apsLocalDevMemArena[0]);
#endif
		gpsSysData->apsLocalDevMemArena[0] = NULL;
	}

	SysDeinitialiseCommon(gpsSysData);

	DeinitSGXPCIDev(gpsSysData);

	if (SYS_SPECIFIC_DATA_TEST
	    (psSysSpecData, SYS_SPECIFIC_DATA_ENABLE_ENVDATA)) {
		eError = OSDeInitEnvData(gpsSysData->pvEnvSpecificData);
		if (eError != PVRSRV_OK) {
			PVR_DPF((PVR_DBG_ERROR,
				 "SysDeinitialise: failed to de-init env structure"));
			return eError;
		}
	}

	if (SYS_SPECIFIC_DATA_TEST
	    (psSysSpecData, SYS_SPECIFIC_DATA_ENABLE_PDUMPINIT)) {
		PDUMPDEINIT();
	}

	psSysSpecData->ui32SysSpecificData = 0;

	gpsSysData = NULL;

	return PVRSRV_OK;
}

PVRSRV_ERROR SysGetDeviceMemoryMap(PVRSRV_DEVICE_TYPE eDeviceType,
				   void **ppvDeviceMap)
{

	switch (eDeviceType) {
	case PVRSRV_DEVICE_TYPE_SGX:
		{
			*ppvDeviceMap = (void *)&gsSGXDeviceMap;
			break;
		}
	default:
		{
			PVR_DPF((PVR_DBG_ERROR,
				 "SysGetDeviceMemoryMap: unsupported device type"));
		}
	}
	return PVRSRV_OK;
}

IMG_DEV_PHYADDR SysCpuPAddrToDevPAddr(PVRSRV_DEVICE_TYPE eDeviceType,
				      IMG_CPU_PHYADDR CpuPAddr)
{
	IMG_DEV_PHYADDR DevPAddr;

	DevPAddr.uiAddr = CpuPAddr.uiAddr;

	return DevPAddr;
}

IMG_CPU_PHYADDR SysSysPAddrToCpuPAddr(IMG_SYS_PHYADDR sys_paddr)
{
	IMG_CPU_PHYADDR cpu_paddr;

	cpu_paddr.uiAddr = sys_paddr.uiAddr;

	return cpu_paddr;
}

IMG_SYS_PHYADDR SysCpuPAddrToSysPAddr(IMG_CPU_PHYADDR cpu_paddr)
{
	IMG_SYS_PHYADDR sys_paddr;

	sys_paddr.uiAddr = cpu_paddr.uiAddr;

	return sys_paddr;
}

IMG_DEV_PHYADDR SysSysPAddrToDevPAddr(PVRSRV_DEVICE_TYPE eDeviceType,
				      IMG_SYS_PHYADDR SysPAddr)
{
	IMG_DEV_PHYADDR DevPAddr;

	DevPAddr.uiAddr = SysPAddr.uiAddr;

	return DevPAddr;
}

IMG_SYS_PHYADDR SysDevPAddrToSysPAddr(PVRSRV_DEVICE_TYPE eDeviceType,
				      IMG_DEV_PHYADDR DevPAddr)
{
	IMG_SYS_PHYADDR SysPAddr;

	SysPAddr.uiAddr = DevPAddr.uiAddr;

	return SysPAddr;
}

void SysRegisterExternalDevice(PVRSRV_DEVICE_NODE * psDeviceNode)
{
}

void SysRemoveExternalDevice(PVRSRV_DEVICE_NODE * psDeviceNode)
{
}

PVRSRV_ERROR SysResetDevice(u32 ui32DeviceIndex)
{
	if (ui32DeviceIndex == gui32SGXDeviceID) {
		SysResetSGX(gpsSysData->pvSOCRegsBase);
	} else {
		return PVRSRV_ERROR_GENERIC;
	}

	return PVRSRV_OK;
}

PVRSRV_ERROR SysOEMFunction(u32 ui32ID,
			    void *pvIn,
			    u32 ulInSize, void *pvOut, u32 ulOutSize)
{
	if (ulInSize || pvIn) ;

	if ((ui32ID == OEM_GET_EXT_FUNCS) &&
	    (ulOutSize == sizeof(PVRSRV_DC_OEM_JTABLE))) {
		PVRSRV_DC_OEM_JTABLE *psOEMJTable =
		    (PVRSRV_DC_OEM_JTABLE *) pvOut;
		psOEMJTable->pfnOEMBridgeDispatch = &PVRSRV_BridgeDispatchKM;
#if !defined(SERVICES4)
		psOEMJTable->pfnOEMReadRegistryString =
		    &PVRSRVReadRegistryString;
		psOEMJTable->pfnOEMWriteRegistryString =
		    &PVRSRVWriteRegistryString;
#endif
		return PVRSRV_OK;
	}

	return PVRSRV_ERROR_INVALID_PARAMS;
}

void SysSaveRestoreArenaLiveSegments(int bSave)
{
	u32 uiBufferSize;
	static void *pvBackupBuffer = NULL;
	static void *hBlockAlloc;
	static u32 uiWriteBufferSize = 0;
	PVRSRV_ERROR eError;

	uiBufferSize = 0;

	if (gpsSysData->apsLocalDevMemArena[0] != NULL) {

		if (PVRSRVSaveRestoreLiveSegments
		    ((void *)gpsSysData->apsLocalDevMemArena[0], NULL,
		     &uiBufferSize, bSave) == PVRSRV_OK) {
			if (uiBufferSize) {
				if (bSave && pvBackupBuffer == NULL) {
					uiWriteBufferSize = uiBufferSize;

					eError =
					    OSAllocPages(PVRSRV_HAP_KERNEL_ONLY
							 | PVRSRV_HAP_CACHED,
							 uiBufferSize,
							 HOST_PAGESIZE(),
							 &pvBackupBuffer,
							 &hBlockAlloc);
					if (eError != PVRSRV_OK) {
						PVR_DPF((PVR_DBG_ERROR,
							 "SysSaveRestoreArenaLiveSegments: OSAllocPages(0x%x) failed:%lu",
							 uiBufferSize, eError));
						return;
					}
				} else {
					PVR_ASSERT(uiWriteBufferSize ==
						   uiBufferSize);
				}

				PVRSRVSaveRestoreLiveSegments((void *)
							      gpsSysData->
							      apsLocalDevMemArena
							      [0],
							      pvBackupBuffer,
							      &uiBufferSize,
							      bSave);

				if (!bSave && pvBackupBuffer) {

					eError =
					    OSFreePages(PVRSRV_HAP_KERNEL_ONLY |
							PVRSRV_HAP_CACHED,
							uiWriteBufferSize,
							pvBackupBuffer,
							hBlockAlloc);
					if (eError != PVRSRV_OK) {
						PVR_DPF((PVR_DBG_ERROR,
							 "SysSaveRestoreArenaLiveSegments: OSFreePages(0x%x) failed:%lu",
							 uiBufferSize, eError));
					}

					pvBackupBuffer = NULL;
				}
			}
		}
	}

}

PVRSRV_ERROR SysMapInRegisters(void)
{
	PVRSRV_DEVICE_NODE *psDeviceNodeList;

	psDeviceNodeList = gpsSysData->psDeviceNodeList;

	while (psDeviceNodeList) {
		PVRSRV_SGXDEV_INFO *psDevInfo =
		    (PVRSRV_SGXDEV_INFO *) psDeviceNodeList->pvDevice;
		if (psDeviceNodeList->sDevId.eDeviceType ==
		    PVRSRV_DEVICE_TYPE_SGX) {

			psDevInfo->pvRegsBaseKM =
			    OSMapPhysToLin(gsSGXDeviceMap.sRegsCpuPBase,
					   gsSGXDeviceMap.ui32RegsSize,
					   PVRSRV_HAP_KERNEL_ONLY |
					   PVRSRV_HAP_UNCACHED, NULL);

			if (!psDevInfo->pvRegsBaseKM) {
				PVR_DPF((PVR_DBG_ERROR,
					 "SysMapInRegisters : Failed to map in regs\n"));
				return PVRSRV_ERROR_BAD_MAPPING;
			}

			psDevInfo->ui32RegSize = gsSGXDeviceMap.ui32RegsSize;
			psDevInfo->sRegsPhysBase = gsSGXDeviceMap.sRegsSysPBase;

#ifdef SGX_FEATURE_2D_HARDWARE

			psDevInfo->s2DSlavePortKM.pvData =
			    OSMapPhysToLin(gsSGXDeviceMap.sSPCpuPBase,
					   gsSGXDeviceMap.ui32SPSize,
					   PVRSRV_HAP_KERNEL_ONLY |
					   PVRSRV_HAP_UNCACHED, NULL);

			if (!psDevInfo->s2DSlavePortKM.pvData) {
				PVR_DPF((PVR_DBG_ERROR,
					 "SysMapInRegisters : Failed to map 2D Slave port region\n"));
				return PVRSRV_ERROR_BAD_MAPPING;
			}

			psDevInfo->s2DSlavePortKM.ui32DataRange =
			    gsSGXDeviceMap.ui32SPSize;
			psDevInfo->s2DSlavePortKM.sPhysBase =
			    gsSGXDeviceMap.sSPSysPBase;
#endif

		}

		psDeviceNodeList = psDeviceNodeList->psNext;
	}

	return PVRSRV_OK;
}

PVRSRV_ERROR SysUnmapRegisters(void)
{
	PVRSRV_DEVICE_NODE *psDeviceNodeList;

	psDeviceNodeList = gpsSysData->psDeviceNodeList;

	while (psDeviceNodeList) {
		PVRSRV_SGXDEV_INFO *psDevInfo =
		    (PVRSRV_SGXDEV_INFO *) psDeviceNodeList->pvDevice;
		if (psDeviceNodeList->sDevId.eDeviceType ==
		    PVRSRV_DEVICE_TYPE_SGX) {

			if (psDevInfo->pvRegsBaseKM) {
				OSUnMapPhysToLin(psDevInfo->pvRegsBaseKM,
						 gsSGXDeviceMap.ui32RegsSize,
						 PVRSRV_HAP_KERNEL_ONLY |
						 PVRSRV_HAP_UNCACHED, NULL);

				psDevInfo->pvRegsBaseKM = NULL;
			}

			psDevInfo->pvRegsBaseKM = NULL;
			psDevInfo->ui32RegSize = 0;
			psDevInfo->sRegsPhysBase.uiAddr = 0;

#ifdef SGX_FEATURE_2D_HARDWARE

			if (psDevInfo->s2DSlavePortKM.pvData) {
				OSUnMapPhysToLin(psDevInfo->s2DSlavePortKM.
						 pvData,
						 gsSGXDeviceMap.ui32SPSize,
						 PVRSRV_HAP_KERNEL_ONLY |
						 PVRSRV_HAP_UNCACHED, NULL);

				psDevInfo->s2DSlavePortKM.pvData = NULL;
			}

			psDevInfo->s2DSlavePortKM.pvData = NULL;
			psDevInfo->s2DSlavePortKM.ui32DataRange = 0;
			psDevInfo->s2DSlavePortKM.sPhysBase.uiAddr = 0;
#endif

		}

		psDeviceNodeList = psDeviceNodeList->psNext;
	}

	return PVRSRV_OK;
}

PVRSRV_ERROR SysSystemPrePowerState(PVRSRV_SYS_POWER_STATE eNewPowerState)
{
	PVRSRV_ERROR eError = PVRSRV_OK;

	if (eNewPowerState != gpsSysData->eCurrentPowerState) {
		if ((eNewPowerState == PVRSRV_SYS_POWER_STATE_D3) &&
		    (gpsSysData->eCurrentPowerState <
		     PVRSRV_SYS_POWER_STATE_D3)) {

#if defined(SYS_USING_INTERRUPTS)
			if (SYS_SPECIFIC_DATA_TEST
			    (&gsSysSpecificData,
			     SYS_SPECIFIC_DATA_ENABLE_IRQ)) {
				SysDisableInterrupts(gpsSysData);

				SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
						      SYS_SPECIFIC_DATA_PM_IRQ_DISABLE);
				SYS_SPECIFIC_DATA_CLEAR(&gsSysSpecificData,
							SYS_SPECIFIC_DATA_ENABLE_IRQ);

			}

			if (SYS_SPECIFIC_DATA_TEST
			    (&gsSysSpecificData,
			     SYS_SPECIFIC_DATA_ENABLE_LISR)) {
				eError = OSUninstallSystemLISR(gpsSysData);
				if (eError != PVRSRV_OK) {
					PVR_DPF((PVR_DBG_ERROR,
						 "SysSystemPrePowerState: OSUninstallSystemLISR failed (%d)",
						 eError));
				}
				SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
						      SYS_SPECIFIC_DATA_PM_UNINSTALL_LISR);
				SYS_SPECIFIC_DATA_CLEAR(&gsSysSpecificData,
							SYS_SPECIFIC_DATA_ENABLE_LISR);
			}
#endif

			SysUnmapRegisters();
		}
	}

	return eError;
}

PVRSRV_ERROR SysSystemPostPowerState(PVRSRV_SYS_POWER_STATE eNewPowerState)
{
	PVRSRV_ERROR eError = PVRSRV_OK;

	if (eNewPowerState != gpsSysData->eCurrentPowerState) {
		if ((gpsSysData->eCurrentPowerState ==
		     PVRSRV_SYS_POWER_STATE_D3)
		    && (eNewPowerState < PVRSRV_SYS_POWER_STATE_D3)) {

			eError = SysLocateDevices(gpsSysData);
			if (eError != PVRSRV_OK) {
				PVR_DPF((PVR_DBG_ERROR,
					 "SysSystemPostPowerState: Failed to locate devices"));
				return eError;
			}

			eError = SysMapInRegisters();
			if (eError != PVRSRV_OK) {
				PVR_DPF((PVR_DBG_ERROR,
					 "SysSystemPostPowerState: Failed to map in registers"));
				return eError;
			}

			eError = SysInitRegisters();
			if (eError != PVRSRV_OK) {
				PVR_DPF((PVR_DBG_ERROR,
					 "SysSystemPostPowerState: Failed to Initialise registers"));
				return eError;
			}
#if defined(SYS_USING_INTERRUPTS)
			if (SYS_SPECIFIC_DATA_TEST
			    (&gsSysSpecificData,
			     SYS_SPECIFIC_DATA_PM_UNINSTALL_LISR)) {
				eError =
				    OSInstallSystemLISR(gpsSysData,
							gsSGXDeviceMap.ui32IRQ);
				if (eError != PVRSRV_OK) {
					PVR_DPF((PVR_DBG_ERROR,
						 "SysSystemPostPowerState: OSInstallSystemLISR failed to install ISR (%d)",
						 eError));
				}
				SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
						      SYS_SPECIFIC_DATA_ENABLE_LISR);
				SYS_SPECIFIC_DATA_CLEAR(&gsSysSpecificData,
							SYS_SPECIFIC_DATA_PM_UNINSTALL_LISR);
			}

			if (SYS_SPECIFIC_DATA_TEST
			    (&gsSysSpecificData,
			     SYS_SPECIFIC_DATA_PM_IRQ_DISABLE)) {
				SysEnableInterrupts(gpsSysData);

				SYS_SPECIFIC_DATA_SET(&gsSysSpecificData,
						      SYS_SPECIFIC_DATA_ENABLE_IRQ);
				SYS_SPECIFIC_DATA_CLEAR(&gsSysSpecificData,
							SYS_SPECIFIC_DATA_PM_IRQ_DISABLE);
			}
#endif
		}
	}

	return eError;
}

PVRSRV_ERROR SysDevicePrePowerState(u32 ui32DeviceIndex,
				    PVRSRV_DEV_POWER_STATE eNewPowerState,
				    PVRSRV_DEV_POWER_STATE eCurrentPowerState)
{
	if (ui32DeviceIndex == gui32SGXDeviceID) {
		if ((eNewPowerState != eCurrentPowerState) &&
		    (eNewPowerState == PVRSRV_DEV_POWER_STATE_IDLE)) {
#ifdef INTEL_D3_PM
			graphics_pm_set_idle();
#endif
			PVR_DPF((PVR_DBG_MESSAGE,
				 "SysDevicePrePowerState: Remove SGX power"));
		}
	}

	return PVRSRV_OK;
}

PVRSRV_ERROR SysDevicePostPowerState(u32 ui32DeviceIndex,
				     PVRSRV_DEV_POWER_STATE eNewPowerState,
				     PVRSRV_DEV_POWER_STATE eCurrentPowerState)
{
	if (ui32DeviceIndex == gui32SGXDeviceID) {
		if ((eNewPowerState != eCurrentPowerState) &&
		    (eCurrentPowerState == PVRSRV_DEV_POWER_STATE_IDLE)) {
#ifdef INTEL_D3_PM
			graphics_pm_set_busy();
#endif
			PVR_DPF((PVR_DBG_MESSAGE,
				 "SysDevicePostPowerState: Restore SGX power"));
		}
	}

	return PVRSRV_OK;
}