1
/**********************************************************************
3
* Copyright (C) Imagination Technologies Ltd. All rights reserved.
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
9
* This program is distributed in the hope it will be useful but, except
10
* as otherwise stated in writing, without any warranty; without even the
11
* implied warranty of merchantability or fitness for a particular purpose.
12
* See the GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License along with
15
* this program; if not, write to the Free Software Foundation, Inc.,
16
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
* The full GNU General Public License is included in this distribution in
19
* the file called "COPYING".
21
* Contact Information:
22
* Imagination Technologies Ltd. <gpl-support@imgtec.com>
23
* Home Park Estate, Kings Langley, Herts, WD4 8LZ, UK
25
******************************************************************************/
27
#include "services_headers.h"
32
static IMG_BOOL gbInitServerRunning = IMG_FALSE;
33
static IMG_BOOL gbInitServerRan = IMG_FALSE;
34
static IMG_BOOL gbInitSuccessful = IMG_FALSE;
37
PVRSRV_ERROR PVRSRVSetInitServerState(PVRSRV_INIT_SERVER_STATE eInitServerState, IMG_BOOL bState)
40
switch(eInitServerState)
42
case PVRSRV_INIT_SERVER_RUNNING:
43
gbInitServerRunning = bState;
45
case PVRSRV_INIT_SERVER_RAN:
46
gbInitServerRan = bState;
48
case PVRSRV_INIT_SERVER_SUCCESSFUL:
49
gbInitSuccessful = bState;
52
PVR_DPF((PVR_DBG_ERROR,
53
"PVRSRVSetInitServerState : Unknown state %x", eInitServerState));
54
return PVRSRV_ERROR_UNKNOWN_INIT_SERVER_STATE;
61
IMG_BOOL PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_STATE eInitServerState)
65
switch(eInitServerState)
67
case PVRSRV_INIT_SERVER_RUNNING:
68
bReturnVal = gbInitServerRunning;
70
case PVRSRV_INIT_SERVER_RAN:
71
bReturnVal = gbInitServerRan;
73
case PVRSRV_INIT_SERVER_SUCCESSFUL:
74
bReturnVal = gbInitSuccessful;
77
PVR_DPF((PVR_DBG_ERROR,
78
"PVRSRVGetInitServerState : Unknown state %x", eInitServerState));
79
bReturnVal = IMG_FALSE;
85
static IMG_BOOL _IsSystemStatePowered(PVRSRV_SYS_POWER_STATE eSystemPowerState)
87
return (IMG_BOOL)(eSystemPowerState < PVRSRV_SYS_POWER_STATE_D2);
92
PVRSRV_ERROR PVRSRVPowerLock(IMG_UINT32 ui32CallerID,
93
IMG_BOOL bSystemPowerEvent)
97
IMG_UINT32 ui32Timeout = 1000000;
99
SysAcquireData(&psSysData);
101
eError = OSPowerLockWrap();
102
if (eError != PVRSRV_OK)
109
eError = OSLockResource(&psSysData->sPowerStateChangeResource,
111
if (eError == PVRSRV_OK)
115
else if (ui32CallerID == ISR_ID)
119
eError = PVRSRV_ERROR_RETRY;
125
} while (ui32Timeout > 0);
127
if (eError != PVRSRV_OK)
133
if ((eError == PVRSRV_OK) &&
134
!bSystemPowerEvent &&
135
!_IsSystemStatePowered(psSysData->eCurrentPowerState))
138
PVRSRVPowerUnlock(ui32CallerID);
139
eError = PVRSRV_ERROR_RETRY;
147
IMG_VOID PVRSRVPowerUnlock(IMG_UINT32 ui32CallerID)
149
OSUnlockResource(&gpsSysData->sPowerStateChangeResource, ui32CallerID);
154
static PVRSRV_ERROR PVRSRVDevicePrePowerStateKM_AnyVaCb(PVRSRV_POWER_DEV *psPowerDevice, va_list va)
156
PVRSRV_DEV_POWER_STATE eNewDevicePowerState;
160
IMG_BOOL bAllDevices;
161
IMG_UINT32 ui32DeviceIndex;
162
PVRSRV_DEV_POWER_STATE eNewPowerState;
165
bAllDevices = va_arg(va, IMG_BOOL);
166
ui32DeviceIndex = va_arg(va, IMG_UINT32);
167
eNewPowerState = va_arg(va, PVRSRV_DEV_POWER_STATE);
169
if (bAllDevices || (ui32DeviceIndex == psPowerDevice->ui32DeviceIndex))
171
eNewDevicePowerState = (eNewPowerState == PVRSRV_DEV_POWER_STATE_DEFAULT) ?
172
psPowerDevice->eDefaultPowerState : eNewPowerState;
174
if (psPowerDevice->eCurrentPowerState != eNewDevicePowerState)
176
if (psPowerDevice->pfnPrePower != IMG_NULL)
179
eError = psPowerDevice->pfnPrePower(psPowerDevice->hDevCookie,
180
eNewDevicePowerState,
181
psPowerDevice->eCurrentPowerState);
182
if (eError != PVRSRV_OK)
189
eError = SysDevicePrePowerState(psPowerDevice->ui32DeviceIndex,
190
eNewDevicePowerState,
191
psPowerDevice->eCurrentPowerState);
192
if (eError != PVRSRV_OK)
203
PVRSRV_ERROR PVRSRVDevicePrePowerStateKM(IMG_BOOL bAllDevices,
204
IMG_UINT32 ui32DeviceIndex,
205
PVRSRV_DEV_POWER_STATE eNewPowerState)
210
SysAcquireData(&psSysData);
213
eError = List_PVRSRV_POWER_DEV_PVRSRV_ERROR_Any_va(psSysData->psPowerDeviceList,
214
&PVRSRVDevicePrePowerStateKM_AnyVaCb,
222
static PVRSRV_ERROR PVRSRVDevicePostPowerStateKM_AnyVaCb(PVRSRV_POWER_DEV *psPowerDevice, va_list va)
224
PVRSRV_DEV_POWER_STATE eNewDevicePowerState;
228
IMG_BOOL bAllDevices;
229
IMG_UINT32 ui32DeviceIndex;
230
PVRSRV_DEV_POWER_STATE eNewPowerState;
233
bAllDevices = va_arg(va, IMG_BOOL);
234
ui32DeviceIndex = va_arg(va, IMG_UINT32);
235
eNewPowerState = va_arg(va, PVRSRV_DEV_POWER_STATE);
237
if (bAllDevices || (ui32DeviceIndex == psPowerDevice->ui32DeviceIndex))
239
eNewDevicePowerState = (eNewPowerState == PVRSRV_DEV_POWER_STATE_DEFAULT) ?
240
psPowerDevice->eDefaultPowerState : eNewPowerState;
242
if (psPowerDevice->eCurrentPowerState != eNewDevicePowerState)
245
eError = SysDevicePostPowerState(psPowerDevice->ui32DeviceIndex,
246
eNewDevicePowerState,
247
psPowerDevice->eCurrentPowerState);
248
if (eError != PVRSRV_OK)
253
if (psPowerDevice->pfnPostPower != IMG_NULL)
256
eError = psPowerDevice->pfnPostPower(psPowerDevice->hDevCookie,
257
eNewDevicePowerState,
258
psPowerDevice->eCurrentPowerState);
259
if (eError != PVRSRV_OK)
265
psPowerDevice->eCurrentPowerState = eNewDevicePowerState;
272
PVRSRV_ERROR PVRSRVDevicePostPowerStateKM(IMG_BOOL bAllDevices,
273
IMG_UINT32 ui32DeviceIndex,
274
PVRSRV_DEV_POWER_STATE eNewPowerState)
279
SysAcquireData(&psSysData);
282
eError = List_PVRSRV_POWER_DEV_PVRSRV_ERROR_Any_va(psSysData->psPowerDeviceList,
283
&PVRSRVDevicePostPowerStateKM_AnyVaCb,
293
PVRSRV_ERROR PVRSRVSetDevicePowerStateKM(IMG_UINT32 ui32DeviceIndex,
294
PVRSRV_DEV_POWER_STATE eNewPowerState,
295
IMG_UINT32 ui32CallerID,
296
IMG_BOOL bRetainMutex)
301
SysAcquireData(&psSysData);
303
eError = PVRSRVPowerLock(ui32CallerID, IMG_FALSE);
304
if(eError != PVRSRV_OK)
310
if (eNewPowerState == PVRSRV_DEV_POWER_STATE_DEFAULT)
316
eError = PVRSRVDevicePrePowerStateKM(IMG_FALSE, ui32DeviceIndex, PVRSRV_DEV_POWER_STATE_ON);
317
if(eError != PVRSRV_OK)
322
eError = PVRSRVDevicePostPowerStateKM(IMG_FALSE, ui32DeviceIndex, PVRSRV_DEV_POWER_STATE_ON);
324
if (eError != PVRSRV_OK)
333
eError = PVRSRVDevicePrePowerStateKM(IMG_FALSE, ui32DeviceIndex, eNewPowerState);
334
if(eError != PVRSRV_OK)
336
if (eNewPowerState == PVRSRV_DEV_POWER_STATE_DEFAULT)
343
eError = PVRSRVDevicePostPowerStateKM(IMG_FALSE, ui32DeviceIndex, eNewPowerState);
345
if (eNewPowerState == PVRSRV_DEV_POWER_STATE_DEFAULT)
352
if(eError != PVRSRV_OK)
354
PVR_DPF((PVR_DBG_ERROR,
355
"PVRSRVSetDevicePowerStateKM : Transition to %d FAILED 0x%x", eNewPowerState, eError));
358
if (!bRetainMutex || (eError != PVRSRV_OK))
360
PVRSRVPowerUnlock(ui32CallerID);
368
PVRSRV_ERROR PVRSRVSystemPrePowerStateKM(PVRSRV_SYS_POWER_STATE eNewSysPowerState)
372
PVRSRV_DEV_POWER_STATE eNewDevicePowerState;
374
SysAcquireData(&psSysData);
377
eError = PVRSRVPowerLock(KERNEL_ID, IMG_TRUE);
378
if(eError != PVRSRV_OK)
383
if (_IsSystemStatePowered(eNewSysPowerState) !=
384
_IsSystemStatePowered(psSysData->eCurrentPowerState))
386
if (_IsSystemStatePowered(eNewSysPowerState))
389
eNewDevicePowerState = PVRSRV_DEV_POWER_STATE_DEFAULT;
393
eNewDevicePowerState = PVRSRV_DEV_POWER_STATE_OFF;
397
eError = PVRSRVDevicePrePowerStateKM(IMG_TRUE, 0, eNewDevicePowerState);
398
if (eError != PVRSRV_OK)
404
if (eNewSysPowerState != psSysData->eCurrentPowerState)
407
eError = SysSystemPrePowerState(eNewSysPowerState);
408
if (eError != PVRSRV_OK)
418
PVR_DPF((PVR_DBG_ERROR,
419
"PVRSRVSystemPrePowerStateKM: Transition from %d to %d FAILED 0x%x",
420
psSysData->eCurrentPowerState, eNewSysPowerState, eError));
423
psSysData->eFailedPowerState = eNewSysPowerState;
425
PVRSRVPowerUnlock(KERNEL_ID);
432
PVRSRV_ERROR PVRSRVSystemPostPowerStateKM(PVRSRV_SYS_POWER_STATE eNewSysPowerState)
434
PVRSRV_ERROR eError = PVRSRV_OK;
436
PVRSRV_DEV_POWER_STATE eNewDevicePowerState;
438
SysAcquireData(&psSysData);
440
if (eNewSysPowerState != psSysData->eCurrentPowerState)
443
eError = SysSystemPostPowerState(eNewSysPowerState);
444
if (eError != PVRSRV_OK)
450
if (_IsSystemStatePowered(eNewSysPowerState) !=
451
_IsSystemStatePowered(psSysData->eCurrentPowerState))
453
if (_IsSystemStatePowered(eNewSysPowerState))
456
eNewDevicePowerState = PVRSRV_DEV_POWER_STATE_DEFAULT;
460
eNewDevicePowerState = PVRSRV_DEV_POWER_STATE_OFF;
464
eError = PVRSRVDevicePostPowerStateKM(IMG_TRUE, 0, eNewDevicePowerState);
465
if (eError != PVRSRV_OK)
471
PVR_DPF((PVR_DBG_MESSAGE,
472
"PVRSRVSystemPostPowerStateKM: System Power Transition from %d to %d OK",
473
psSysData->eCurrentPowerState, eNewSysPowerState));
475
psSysData->eCurrentPowerState = eNewSysPowerState;
479
PVRSRVPowerUnlock(KERNEL_ID);
482
if (_IsSystemStatePowered(eNewSysPowerState) &&
483
PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_SUCCESSFUL))
488
PVRSRVScheduleDeviceCallbacks();
496
PVRSRV_ERROR PVRSRVSetPowerStateKM(PVRSRV_SYS_POWER_STATE eNewSysPowerState)
501
SysAcquireData(&psSysData);
503
eError = PVRSRVSystemPrePowerStateKM(eNewSysPowerState);
504
if(eError != PVRSRV_OK)
509
eError = PVRSRVSystemPostPowerStateKM(eNewSysPowerState);
510
if(eError != PVRSRV_OK)
516
psSysData->eFailedPowerState = PVRSRV_SYS_POWER_STATE_Unspecified;
522
PVR_DPF((PVR_DBG_ERROR,
523
"PVRSRVSetPowerStateKM: Transition from %d to %d FAILED 0x%x",
524
psSysData->eCurrentPowerState, eNewSysPowerState, eError));
527
psSysData->eFailedPowerState = eNewSysPowerState;
533
PVRSRV_ERROR PVRSRVRegisterPowerDevice(IMG_UINT32 ui32DeviceIndex,
534
PFN_PRE_POWER pfnPrePower,
535
PFN_POST_POWER pfnPostPower,
536
PFN_PRE_CLOCKSPEED_CHANGE pfnPreClockSpeedChange,
537
PFN_POST_CLOCKSPEED_CHANGE pfnPostClockSpeedChange,
538
IMG_HANDLE hDevCookie,
539
PVRSRV_DEV_POWER_STATE eCurrentPowerState,
540
PVRSRV_DEV_POWER_STATE eDefaultPowerState)
544
PVRSRV_POWER_DEV *psPowerDevice;
546
if (pfnPrePower == IMG_NULL &&
547
pfnPostPower == IMG_NULL)
549
return PVRSRVRemovePowerDevice(ui32DeviceIndex);
552
SysAcquireData(&psSysData);
554
eError = OSAllocMem( PVRSRV_OS_NON_PAGEABLE_HEAP,
555
sizeof(PVRSRV_POWER_DEV),
556
(IMG_VOID **)&psPowerDevice, IMG_NULL,
558
if(eError != PVRSRV_OK)
560
PVR_DPF((PVR_DBG_ERROR,"PVRSRVRegisterPowerDevice: Failed to alloc PVRSRV_POWER_DEV"));
565
psPowerDevice->pfnPrePower = pfnPrePower;
566
psPowerDevice->pfnPostPower = pfnPostPower;
567
psPowerDevice->pfnPreClockSpeedChange = pfnPreClockSpeedChange;
568
psPowerDevice->pfnPostClockSpeedChange = pfnPostClockSpeedChange;
569
psPowerDevice->hDevCookie = hDevCookie;
570
psPowerDevice->ui32DeviceIndex = ui32DeviceIndex;
571
psPowerDevice->eCurrentPowerState = eCurrentPowerState;
572
psPowerDevice->eDefaultPowerState = eDefaultPowerState;
575
List_PVRSRV_POWER_DEV_Insert(&(psSysData->psPowerDeviceList), psPowerDevice);
581
PVRSRV_ERROR PVRSRVRemovePowerDevice (IMG_UINT32 ui32DeviceIndex)
584
PVRSRV_POWER_DEV *psPowerDev;
586
SysAcquireData(&psSysData);
589
psPowerDev = (PVRSRV_POWER_DEV*)
590
List_PVRSRV_POWER_DEV_Any_va(psSysData->psPowerDeviceList,
591
&MatchPowerDeviceIndex_AnyVaCb,
596
List_PVRSRV_POWER_DEV_Remove(psPowerDev);
597
OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(PVRSRV_POWER_DEV), psPowerDev, IMG_NULL);
606
IMG_BOOL PVRSRVIsDevicePowered(IMG_UINT32 ui32DeviceIndex)
609
PVRSRV_POWER_DEV *psPowerDevice;
611
SysAcquireData(&psSysData);
614
if (OSIsResourceLocked(&psSysData->sPowerStateChangeResource, KERNEL_ID) ||
615
OSIsResourceLocked(&psSysData->sPowerStateChangeResource, ISR_ID))
620
psPowerDevice = (PVRSRV_POWER_DEV*)
621
List_PVRSRV_POWER_DEV_Any_va(psSysData->psPowerDeviceList,
622
&MatchPowerDeviceIndex_AnyVaCb,
624
return (psPowerDevice && (psPowerDevice->eCurrentPowerState == PVRSRV_DEV_POWER_STATE_ON))
625
? IMG_TRUE : IMG_FALSE;
629
PVRSRV_ERROR PVRSRVDevicePreClockSpeedChange(IMG_UINT32 ui32DeviceIndex,
630
IMG_BOOL bIdleDevice,
633
PVRSRV_ERROR eError = PVRSRV_OK;
635
PVRSRV_POWER_DEV *psPowerDevice;
637
PVR_UNREFERENCED_PARAMETER(pvInfo);
639
SysAcquireData(&psSysData);
644
eError = PVRSRVPowerLock(KERNEL_ID, IMG_FALSE);
645
if (eError != PVRSRV_OK)
647
PVR_DPF((PVR_DBG_ERROR, "PVRSRVDevicePreClockSpeedChange : failed to acquire lock, error:0x%x", eError));
653
psPowerDevice = (PVRSRV_POWER_DEV*)
654
List_PVRSRV_POWER_DEV_Any_va(psSysData->psPowerDeviceList,
655
&MatchPowerDeviceIndex_AnyVaCb,
658
if (psPowerDevice && psPowerDevice->pfnPostClockSpeedChange)
660
eError = psPowerDevice->pfnPreClockSpeedChange(psPowerDevice->hDevCookie,
662
psPowerDevice->eCurrentPowerState);
663
if (eError != PVRSRV_OK)
665
PVR_DPF((PVR_DBG_ERROR,
666
"PVRSRVDevicePreClockSpeedChange : Device %u failed, error:0x%x",
667
ui32DeviceIndex, eError));
671
if (bIdleDevice && eError != PVRSRV_OK)
673
PVRSRVPowerUnlock(KERNEL_ID);
680
IMG_VOID PVRSRVDevicePostClockSpeedChange(IMG_UINT32 ui32DeviceIndex,
681
IMG_BOOL bIdleDevice,
686
PVRSRV_POWER_DEV *psPowerDevice;
688
PVR_UNREFERENCED_PARAMETER(pvInfo);
690
SysAcquireData(&psSysData);
693
psPowerDevice = (PVRSRV_POWER_DEV*)
694
List_PVRSRV_POWER_DEV_Any_va(psSysData->psPowerDeviceList,
695
&MatchPowerDeviceIndex_AnyVaCb,
698
if (psPowerDevice && psPowerDevice->pfnPostClockSpeedChange)
700
eError = psPowerDevice->pfnPostClockSpeedChange(psPowerDevice->hDevCookie,
702
psPowerDevice->eCurrentPowerState);
703
if (eError != PVRSRV_OK)
705
PVR_DPF((PVR_DBG_ERROR,
706
"PVRSRVDevicePostClockSpeedChange : Device %u failed, error:0x%x",
707
ui32DeviceIndex, eError));
715
PVRSRVPowerUnlock(KERNEL_ID);