summaryrefslogtreecommitdiff
path: root/drivers/external_drivers/intel_media/display/tng/drv/ospm/pwr_mgmt.c
blob: faf18943f22f917f0cb7f1625c6b36e756dd37c4 (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
944
945
946
947
948
949
950
/**************************************************************************
 * Copyright (c) 2012, Intel Corporation.
 * All Rights Reserved.

 * 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 (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * 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.  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.
 *
 * Authors:
 *    Hitesh K. Patel <hitesh.k.patel@intel.com>
 */


#include <linux/spinlock.h>
#include <asm/intel-mid.h>
#include <asm/intel_scu_ipc.h>

#include <linux/pm_runtime.h>

#include "psb_drv.h"
#include "pmu_tng.h"
#include "tng_wa.h"
#include "pwr_mgmt.h"
#include "gfx_rtpm.h"
#ifdef CONFIG_MOOREFIELD
#include "gfx_ospm_ann.h"
#else
#include "gfx_ospm.h"
#endif
#include "dc_ospm.h"
#include "dc_maxfifo.h"
#include "video_ospm.h"

struct _ospm_data_ *g_ospm_data;
struct drm_device *gpDrmDevice;

/* island, state, ref_count, init_func, power_func */
#ifdef CONFIG_MOOREFIELD
struct ospm_power_island island_list[] = {
	{OSPM_DISPLAY_A, OSPM_POWER_OFF, {0}, ospm_disp_a_init, NULL},
	{OSPM_DISPLAY_B, OSPM_POWER_OFF, {0}, ospm_disp_b_init, NULL},
	{OSPM_DISPLAY_C, OSPM_POWER_OFF, {0}, ospm_disp_c_init, NULL},
	{OSPM_DISPLAY_MIO, OSPM_POWER_OFF, {0}, ospm_mio_init, NULL},
	{OSPM_DISPLAY_HDMI, OSPM_POWER_OFF, {0}, ospm_hdmi_init, NULL},
	{OSPM_GRAPHICS_ISLAND, OSPM_POWER_OFF, {0}, ospm_rscd_init, NULL},
	{OSPM_SIDEKICK_ISLAND, OSPM_POWER_OFF, {0}, ospm_sidekick_init, NULL},
	{OSPM_SLC_ISLAND, OSPM_POWER_OFF, {0}, ospm_slc_init, NULL},
	{OSPM_SLC_LDO_ISLAND, OSPM_POWER_OFF, {0}, ospm_slc_ldo_init, NULL},
#ifdef ENABLE_TNG_VID_VSP
	{OSPM_VIDEO_VPP_ISLAND, OSPM_POWER_OFF, {0}, ospm_vsp_init, NULL},
	{OSPM_VIDEO_DEC_ISLAND, OSPM_POWER_OFF, {0}, ospm_ved_init, NULL},
	{OSPM_VIDEO_ENC_ISLAND, OSPM_POWER_OFF, {0}, ospm_vec_init, NULL},
#endif
};
#else
struct ospm_power_island island_list[] = {
	{OSPM_DISPLAY_A, OSPM_POWER_OFF, {0}, ospm_disp_a_init, NULL},
	{OSPM_DISPLAY_B, OSPM_POWER_OFF, {0}, ospm_disp_b_init, NULL},
	{OSPM_DISPLAY_C, OSPM_POWER_OFF, {0}, ospm_disp_c_init, NULL},
	{OSPM_DISPLAY_MIO, OSPM_POWER_OFF, {0}, ospm_mio_init, NULL},
	{OSPM_DISPLAY_HDMI, OSPM_POWER_OFF, {0}, ospm_hdmi_init, NULL},
	{OSPM_GRAPHICS_ISLAND, OSPM_POWER_OFF, {0}, ospm_gfx_init, NULL},
	{OSPM_SLC_ISLAND, OSPM_POWER_OFF, {0}, ospm_slc_init, NULL},
#ifdef ENABLE_TNG_VID_VSP
	{OSPM_VIDEO_VPP_ISLAND, OSPM_POWER_OFF, {0}, ospm_vsp_init, NULL},
	{OSPM_VIDEO_DEC_ISLAND, OSPM_POWER_OFF, {0}, ospm_ved_init, NULL},
	{OSPM_VIDEO_ENC_ISLAND, OSPM_POWER_OFF, {0}, ospm_vec_init, NULL},
#endif
};
#endif

#define KEY_TABLE_SIZE 32
#define KEY_MASK 0x1F
static u32 key_table[KEY_TABLE_SIZE];
static struct mutex islands_safe;

/**
 * in_atomic_or_interrupt() - Return non-zero if in atomic context.
 * Problems with this code:
 * - Function in_atomic is not guaranteed to detect the atomic state entered
 *   by acquisition of a spinlock (and indeed does so only if CONFIG_PREEMPT).
 *   For a discussion on the use of in_atomic and why is it considered (in
 *   general) problematic, see: http://lwn.net/Articles/274695/
 * - Therefore, scripts/checkpatch.pl will complain about use of function
 *   in_atomic in non-core kernel.  For this reason, the several uses of
 *   in_atomic in this file were centralized here (so only one warning).
 *
 * Note: The test herein was originally:
 *   in_atomic() || in_interrupt()
 * but the test for in_interrupt() is redundant with the in_atomic test.
 */
#if !defined CONFIG_PREEMPT
#error Function in_atomic (in general) requires CONFIG_PREEMPT
#endif

#undef OSPM_DEBUG_INFO
#ifdef OSPM_DEBUG_INFO
const char *get_island_name(u32 hw_island)
{
	const char *pstr;

	switch (hw_island) {
	case OSPM_DISPLAY_A:
		pstr = "DISP A ";
		break;
	case OSPM_DISPLAY_B:
		pstr = "DISP B ";
		break;
	case OSPM_DISPLAY_C:
		pstr = "DISP C ";
		break;
	case OSPM_DISPLAY_MIO:
		pstr = "MIO    ";
		break;
	case OSPM_DISPLAY_HDMI:
		pstr = "HDMI   ";
		break;
	case OSPM_VIDEO_VPP_ISLAND:
		pstr = "VSP    ";
		break;
	case OSPM_VIDEO_DEC_ISLAND:
		pstr = "VED    ";
		break;
	case OSPM_VIDEO_ENC_ISLAND:
		pstr = "VEC    ";
		break;
	case OSPM_GRAPHICS_ISLAND:
		pstr = "GFX    ";
		break;
	default:
		pstr = "(unknown hw_island)";
		break;
	}

	return pstr;
}

static void dump_ref_count(u32 hw_island)
{
	int i = 0;
	int ref_value = 0;
	struct ospm_power_island *p_island = NULL;

	PSB_DEBUG_PM("*** power island refrence count. ***\n");

	for (i = 0; i < ARRAY_SIZE(island_list); i++) {
		if (hw_island & island_list[i].island) {
			p_island = &island_list[i];
			ref_value = atomic_read(&p_island->ref_count);
			printk(KERN_ALERT
				"*** %s: %d\n",
				get_island_name(island_list[i].island),
				ref_value);
		}
	}

	OSPM_DPF("%s: ************************************\n");
}
#endif	/* OSPM_DEBUG_INFO */

/**
 * ospm_suspend_pci
 *
 * Description: Suspend the pci device saving state and disabling
 * as necessary.
 */
static void ospm_suspend_pci(struct drm_device *dev)
{
	struct pci_dev *pdev = dev->pdev;
	struct drm_psb_private *dev_priv = dev->dev_private;
	int bsm, vbt, bgsm;

	PSB_DEBUG_PM("%s\n", __func__);

	pci_read_config_dword(pdev, 0x5C, &bsm);
	dev_priv->saveBSM = bsm;
	pci_read_config_dword(pdev, 0xFC, &vbt);
	dev_priv->saveVBT = vbt;
	pci_read_config_dword(pdev, 0x70, &bgsm);
	dev_priv->saveBGSM = bgsm;
	pci_read_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, &dev_priv->msi_addr);
	pci_read_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, &dev_priv->msi_data);
	pci_save_state(pdev);
	pci_set_power_state(pdev, PCI_D3hot);
}

/**
 * ospm_resume_pci
 *
 * Description: Resume the pci device restoring state and enabling
 * as necessary.
 */
static void ospm_resume_pci(struct drm_device *dev)
{
	struct pci_dev *pdev = dev->pdev;
	struct drm_psb_private *dev_priv = dev->dev_private;
	int mmadr, ret = 0;

	PSB_DEBUG_PM("%s\n", __func__);

	if (dev_priv->saveBGSM != 0)
		pci_write_config_dword(pdev, 0x70, dev_priv->saveBGSM);

	if (dev_priv->saveBSM != 0)
		pci_write_config_dword(pdev, 0x5c, dev_priv->saveBSM);

	if (dev_priv->saveVBT != 0)
		pci_write_config_dword(pdev, 0xFC, dev_priv->saveVBT);

	/* retoring MSI address and data in PCIx space */
	if (dev_priv->msi_addr != 0)
		pci_write_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC,
				dev_priv->msi_addr);

	if (dev_priv->msi_data != 0)
		pci_write_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC,
				dev_priv->msi_data);

	ret = pci_enable_device(pdev);

	/* FixMe, Change should be removed Once bz 115181 is fixed */
	pci_read_config_dword(pdev, 0x10, &mmadr);
	if (mmadr == 0) {
		pr_err("GFX OSPM : Bad PCI config\n");
		BUG();
	}

	if (ret != 0)
		PSB_DEBUG_PM("pci_enable_device failed: %d\n", ret);
}

void rtpm_suspend_pci(void)
{
	ospm_suspend_pci(g_ospm_data->dev);
}

void rtpm_resume_pci(void)
{
	ospm_resume_pci(g_ospm_data->dev);
}


/**
 * get_island_ptr
 *
 * get pointer to the island
 * use it to get array item for setting dependency
 *
 * Although island values are defined as bit mask values,
 * this function only supports having a single bit set
 * in this parameter.
 */
struct ospm_power_island *get_island_ptr(u32 hw_island)
{
	struct ospm_power_island *p_island = NULL;
	int i = 0;

	/* got through islands array to find the island */
	while ((i < ARRAY_SIZE(island_list) && (!p_island))) {
		/* do we have the island? */
		if (hw_island & island_list[i].island) {
			/* Found it */
			p_island = &island_list[i];
			break;
		}

		i++;
	}

	if (i == ARRAY_SIZE(island_list))
		PSB_DEBUG_PM("island %x not found\n", hw_island);

	return p_island;
}

static bool power_down_island(struct ospm_power_island *p_island);

/**
 * power_up_island
 *
 * Description: Power up the island and all of it's dependent islands
 */
static bool power_up_island(struct ospm_power_island *p_island)
{
	bool ret = true;

	/* handle the dependency first */
	if (p_island->p_dependency) {
		/* Power up dependent island */
		ret = power_up_island(p_island->p_dependency);
		if (!ret)
			return ret;
	}

	/* if successfully handled dependency */
	if (!atomic_read(&p_island->ref_count)) {
		/* power on the island */
		PSB_DEBUG_PM("Power up island %x\n", p_island->island);
		ret = p_island->p_funcs->power_up(g_ospm_data->dev, p_island);
		if (ret)
			p_island->island_state = OSPM_POWER_ON;
		else {
			PSB_DEBUG_PM("Power up island %x failed!\n", p_island->island);
			if (p_island->p_dependency)
				power_down_island(p_island->p_dependency);
			return ret;
		}
	}

	/* increment the ref count */
	atomic_inc(&p_island->ref_count);

	return ret;
}

/**
 * power_down_island
 *
 * Description: Power down the island and all of it's dependent islands
 */
static bool power_down_island(struct ospm_power_island *p_island)
{
	bool ret = true;

	if (atomic_dec_return(&p_island->ref_count) < 0) {
		DRM_ERROR("Island %x, UnExpect RefCount %d\n",
				p_island->island,
				atomic_read(&p_island->ref_count));
		dump_stack();
		goto power_down_err;
	}

	/* check to see if island is turned off */
	if (!atomic_read(&p_island->ref_count)) {
		/* power on the island */
		PSB_DEBUG_PM("Power down island %x\n", p_island->island);
		ret = p_island->p_funcs->power_down(
				g_ospm_data->dev,
				p_island);

		/* set the island state */
		if (ret)
			p_island->island_state = OSPM_POWER_OFF;
		else
			goto power_down_err;
	}

	/* handle the dependency later */
	if (p_island->p_dependency) {
		/* Power down dependent island */
		ret = power_down_island(p_island->p_dependency);
		if (!ret)
			goto power_down_err;
	}

	return ret;

power_down_err:
	atomic_inc(&p_island->ref_count);
	ret = false;
	return ret;
}

static bool any_island_on(void)
{
	struct ospm_power_island *p_island = NULL;
	u32 i = 0;
	bool ret = false;

	for (i = 0; i < ARRAY_SIZE(island_list); i++) {
		p_island = &island_list[i];

		if (atomic_read(&p_island->ref_count) > 0) {
			ret = true;
			break;
		}
	}

	return ret;
}

/**
 * power_island_get_safe
 *
 * Description: Notify PowerMgmt module that you will be accessing the
 * specified island's hw so don't power it off.  If the island is not
 * powered up, it will power it on. Will detect unbalanced calls
 *
 */
bool power_island_get_safe(u32 hw_island, u8 key)
{
	bool ret = true; // make the caller happy if already locked
	mutex_lock(&islands_safe);
	if (key_table[key & KEY_MASK] == 0) {
		ret = power_island_get(hw_island);
		key_table[key & KEY_MASK] = hw_island;
	} else {
		DRM_ERROR("tried to get island before put %x, do nothing", key);
	}
	mutex_unlock(&islands_safe);
	return ret;
}

/**
 * power_island_get
 *
 * Description: Notify PowerMgmt module that you will be accessing the
 * specified island's hw so don't power it off.  If the island is not
 * powered up, it will power it on.
 *
 */
bool power_island_get(u32 hw_island)
{
	u32 i = 0;
	bool ret = true, first_island = false;
	int pm_ret;
	struct ospm_power_island *p_island;
	struct drm_psb_private *dev_priv = g_ospm_data->dev->dev_private;

	mutex_lock(&g_ospm_data->ospm_lock);

	if (!any_island_on()) {
		PSB_DEBUG_PM("Resuming PCI\n");
		/* Here, we use runtime pm framework to suit
		 * S3 PCI suspend/resume
		 */
		wake_lock(&dev_priv->ospm_wake_lock);
		pm_ret = pm_runtime_get_sync(&g_ospm_data->dev->pdev->dev);
		if (pm_ret < 0) {
			ret = false;
			PSB_DEBUG_PM("pm_runtime_get_sync failed %p.\n",
				&g_ospm_data->dev->pdev->dev);
			goto out_err;
		}
		first_island = true;

	}

	for (i = 0; i < ARRAY_SIZE(island_list); i++) {
		if (hw_island & island_list[i].island) {
			p_island = &island_list[i];
			ret = power_up_island(p_island);
			if (!ret) {
				PSB_DEBUG_PM("power up failed %x\n",
					island_list[i].island);
				goto out_err;
			}
		}
	}

out_err:
	if (ret && first_island)
		pm_qos_update_request(&dev_priv->s0ix_qos, PM_QOS_DEFAULT_VALUE);
	mutex_unlock(&g_ospm_data->ospm_lock);

	return ret;
}

/**
 * power_island_put_safe
 *
 * Description: Notify PowerMgmt module that you are done accessing the
 * specified island's hw so feel free to power it off.  Note that this
 * function doesn't actually power off the islands.
 */
bool power_island_put_safe(u32 hw_island, u8 key)
{
	bool ret = true; // make the caller happy if already unlocked
	mutex_lock(&islands_safe);
	if (key_table[key & KEY_MASK] & hw_island) {
		if (unlikely(key_table[key & KEY_MASK] != hw_island))
			DRM_ERROR("unmatched put for key %x: get %x, put %x\n",
					key, key_table[key & KEY_MASK], hw_island);
		ret = power_island_put(hw_island);
		key_table[key & KEY_MASK] = 0;
        } else {
		DRM_ERROR("tried to put island without get %x, do nothing", key);
        }
	mutex_unlock(&islands_safe);
	return ret;
}


/**
 * power_island_put
 *
 * Description: Notify PowerMgmt module that you are done accessing the
 * specified island's hw so feel free to power it off.  Note that this
 * function doesn't actually power off the islands.
 */
bool power_island_put(u32 hw_island)
{
	bool ret = true;
	u32 i = 0;
	struct drm_psb_private *dev_priv = g_ospm_data->dev->dev_private;
	struct ospm_power_island *p_island;

	mutex_lock(&g_ospm_data->ospm_lock);

	for (i = 0; i < ARRAY_SIZE(island_list); i++) {
		if (hw_island & island_list[i].island) {
			/* Power down the island if needed */
			p_island = &island_list[i];
			ret = power_down_island(p_island);
			if (!ret) {
				PSB_DEBUG_PM("power down failed %x\n",
					island_list[i].island);
			}
		}
	}

	/* Check to see if we need to suspend PCI */
	if (!any_island_on()) {
		PSB_DEBUG_PM("Suspending PCI\n");
		/* Here, we use runtime pm framework to suit
		 * S3 PCI suspend/resume
		 */
		pm_qos_update_request(&dev_priv->s0ix_qos,
				CSTATE_EXIT_LATENCY_S0i1 - 1);
		pm_runtime_put_sync_suspend(&g_ospm_data->dev->pdev->dev);
		wake_unlock(&dev_priv->ospm_wake_lock);
	}
	mutex_unlock(&g_ospm_data->ospm_lock);

	return ret;
}

/**
 * log_power_island_active_requests
 *
 * Description: logs active safe island active requests
 */
void log_power_island_active_requests(void)
{
	int i;
	mutex_lock(&islands_safe);
        for (i = 0; i < KEY_TABLE_SIZE; i++)
		if (key_table[i & KEY_MASK])
			DRM_INFO("Request active for key %x on islands %x",
				i, key_table[i & KEY_MASK]);
	mutex_unlock(&islands_safe);
}


/**
 * is_island_on
 *
 * Description: checks to see if the island is up
 * returns true if hw_island is ON
 * returns false if hw_island is OFF
 */
bool is_island_on(u32 hw_island)
{
	/* get the power island */
	struct ospm_power_island *p_island = get_island_ptr(hw_island);
	bool island_on = false;

	if (!p_island) {
		DRM_ERROR("p_island is NULL\n");
		return false;
	}

	/* TODO: add lock here. */
	island_on = (p_island->island_state == OSPM_POWER_ON) ? true : false;

	return island_on;
}

u32 pipe_to_island(u32 pipe)
{
	u32 power_island = 0;

	switch (pipe) {
	case 0:
		power_island = OSPM_DISPLAY_A;
		break;
	case 1:
		power_island = OSPM_DISPLAY_B;
		break;
	case 2:
		power_island = OSPM_DISPLAY_C;
		break;
	default:
		DRM_ERROR("%s: invalid pipe %u\n", __func__, pipe);
		return 0;
	}

	return power_island;
}

/**
 * ospm_power_init
 *
 * Description: Initialize this ospm power management module
 */
void ospm_power_init(struct drm_device *dev)
{
	u32 i = 0;
	struct drm_psb_private *dev_priv = dev->dev_private;
	/* allocate ospm data */
	g_ospm_data = kmalloc(sizeof(struct _ospm_data_), GFP_KERNEL);
	if (!g_ospm_data)
		goto out_err;

	mutex_init(&g_ospm_data->ospm_lock);
	mutex_init(&islands_safe);
	g_ospm_data->dev = dev;
	gpDrmDevice = dev;

	wake_lock_init(&dev_priv->ospm_wake_lock, WAKE_LOCK_SUSPEND,
			"ospm_wake_lock");
	pm_qos_add_request(&dev_priv->s0ix_qos,
			PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
	/* initilize individual islands */
	for (i = 0; i < ARRAY_SIZE(island_list); i++) {
		island_list[i].p_funcs = kmalloc(sizeof(struct power_ops),
						GFP_KERNEL);
		if ((island_list[i].p_funcs) && (island_list[i].init_func)) {
			island_list[i].init_func(dev, &island_list[i]);
			atomic_set(&island_list[i].ref_count, 0);

			switch (island_list[i].island){
			case OSPM_DISPLAY_A:
			case OSPM_DISPLAY_C:
			case OSPM_DISPLAY_MIO:
				atomic_set(&island_list[i].ref_count, 1);
				island_list[i].island_state = OSPM_POWER_ON;
				if (island_list[i].p_dependency) {
					atomic_inc(&island_list[i].p_dependency->ref_count);
					island_list[i].island_state = OSPM_POWER_ON;
				}

				break;
			default:
				break;
			}
		}
	}

	dc_maxfifo_init(dev);
	rtpm_init(dev);
out_err:
	return;
}

/**
 * ospm_power_uninit
 *
 * Description: Uninitialize this ospm power management module
 */
void ospm_power_uninit(void)
{
	int i;
	struct drm_psb_private *dev_priv = gpDrmDevice->dev_private;

	PSB_DEBUG_PM("%s\n", __func__);

	rtpm_uninit(gpDrmDevice);

	/* Do we need to turn off all islands? */
	power_island_put(OSPM_ALL_ISLANDS);

	if (pm_qos_request_active(&dev_priv->s0ix_qos))
		pm_qos_remove_request(&dev_priv->s0ix_qos);

	for (i = 0; i < ARRAY_SIZE(island_list); i++)
		kfree(island_list[i].p_funcs);

	kfree(g_ospm_data);
}

/**
 * ospm_power_suspend
 *
 * Description: suspend all islands
 */
bool ospm_power_suspend(void)
{
	PSB_DEBUG_PM("%s\n", __func__);

	return true;
}

/**
 * ospm_power_resume
 *
 * Description: resume previously suspended islands.
 */
void ospm_power_resume(void)
{
	PSB_DEBUG_PM("%s\n", __func__);
}

/* FIXME: hkpatel */
/*** LEGACY SUPPORT ****/
/*** REMOVE ONCE CONVERTED ALL FUNCTIONS TO NEW ARCH */

/* Legacy Function for support */
bool ospm_power_using_hw_begin(int hw_island, u32 usage)
{
	bool ret = true;

	/*
	 * FIXME: make ospm_power_using_hw_begin used for Display islands only
	 * take effect for DSPB/HDMIO islands, becaused it's called by the OTM
	 * HDMI codes and not to impact CTP/MDFLD. But eventually need to
	 * replace hw_begin() with power_island_get() in OTM HDMI.
	 */
	if (hw_island == OSPM_DISPLAY_ISLAND)
		hw_island = OSPM_DISPLAY_B | OSPM_DISPLAY_HDMI;

	ret = power_island_get_safe(hw_island, PMKEY_USINGHW);

	return ret;
}
EXPORT_SYMBOL(ospm_power_using_hw_begin);

bool ospm_power_is_hw_on(u32 hw_island)
{
	return is_island_on(hw_island);
}
EXPORT_SYMBOL(ospm_power_is_hw_on);

void ospm_power_using_hw_end(int hw_island)
{
	/*
	 * FIXME: make ospm_power_using_hw_end used for Display islands only
	 * take effect for DSPB/HDMIO islands, becaused it's called by the OTM
	 * HDMI codes and not to impact CTP/MDFLD. But eventually need to
	 * replace hw_end() with power_island_put() in OTM HDMI.
	 */
	if (hw_island == OSPM_DISPLAY_ISLAND)
		hw_island = OSPM_DISPLAY_B | OSPM_DISPLAY_HDMI;

	power_island_put_safe(hw_island, PMKEY_USINGHW);
}
EXPORT_SYMBOL(ospm_power_using_hw_end);

#ifdef ENABLE_TNG_VID_VSP
void ospm_apm_power_down_msvdx(struct drm_device *dev, int force_off)
{
	unsigned long irq_flags;
	int ret, frame_finished = 0;
	int shp_ctx_count = 0;
	struct ospm_power_island *p_island;
	struct drm_psb_private *dev_priv = psb_priv(dev);
	struct psb_video_ctx *pos, *n;


	p_island = get_island_ptr(OSPM_VIDEO_DEC_ISLAND);

	if (!p_island) {
		DRM_ERROR("p_island is NULL\n");
		return;
	}

	if (force_off) {
		if (!ospm_power_is_hw_on(OSPM_VIDEO_DEC_ISLAND)) {
			PSB_DEBUG_PM("msvdx in power off.\n");
			return;
		}

		mutex_lock(&g_ospm_data->ospm_lock);
		ret = p_island->p_funcs->power_down(
			g_ospm_data->dev,
			p_island);

		/* set the island state */
		if (ret)
			p_island->island_state = OSPM_POWER_OFF;

		mutex_unlock(&g_ospm_data->ospm_lock);

		/* MSVDX_NEW_PMSTATE(dev, msvdx_priv, PSB_PMSTATE_POWERDOWN); */

		mutex_lock(&g_ospm_data->ospm_lock);
		ret = p_island->p_funcs->power_up(
			g_ospm_data->dev,
			p_island);

		/* set the island state */
		if (ret)
			p_island->island_state = OSPM_POWER_ON;

		mutex_unlock(&g_ospm_data->ospm_lock);

		power_island_put(OSPM_VIDEO_DEC_ISLAND);
		return;
	}

	if (!ospm_power_is_hw_on(OSPM_VIDEO_DEC_ISLAND))
		PSB_DEBUG_PM("msvdx in power off.\n");


#ifdef CONFIG_SLICE_HEADER_PARSING
	spin_lock_irqsave(&dev_priv->video_ctx_lock, irq_flags);
	list_for_each_entry_safe(pos, n, &dev_priv->video_ctx, head){
		if (pos->slice_extract_flag){
			shp_ctx_count++;
		}
		if (pos->frame_end) {
			frame_finished = 1;
			pos->frame_end = 0;
			break;
		}
	}
	spin_unlock_irqrestore(&dev_priv->video_ctx_lock, irq_flags);
#endif

	psb_msvdx_dequeue_send(dev);

#ifdef CONFIG_SLICE_HEADER_PARSING
	if (shp_ctx_count == 0 || frame_finished)
		power_island_put(OSPM_VIDEO_DEC_ISLAND);
#else
	power_island_put(OSPM_VIDEO_DEC_ISLAND);
#endif

	return;
}

void ospm_apm_power_down_topaz(struct drm_device *dev)
{
	int ret;
	struct ospm_power_island *p_island;

	PSB_DEBUG_PM("Power down VEC...\n");
	p_island = get_island_ptr(OSPM_VIDEO_ENC_ISLAND);

	if (!p_island) {
		DRM_ERROR("p_island is NULL\n");
		return;
	}

	mutex_lock(&g_ospm_data->ospm_lock);

	if (!ospm_power_is_hw_on(OSPM_VIDEO_ENC_ISLAND))
		goto out;

	if (atomic_read(&p_island->ref_count)) {
		PSB_DEBUG_PM("vec ref_count has been set(%d), bypass\n",
			     atomic_read(&p_island->ref_count));
		goto out;
	}

	ret = p_island->p_funcs->power_down(
			g_ospm_data->dev,
			p_island);

	/* set the island state */
	if (ret)
		p_island->island_state = OSPM_POWER_OFF;

	PSB_DEBUG_PM("Power down VEC done\n");
out:
	mutex_unlock(&g_ospm_data->ospm_lock);
	return;
}

void ospm_apm_power_down_vsp(struct drm_device *dev)
{
	int ret;
	struct ospm_power_island *p_island;
	struct drm_psb_private *dev_priv = dev->dev_private;
	struct vsp_private *vsp_priv = dev_priv->vsp_private;
	int island_ref;

	PSB_DEBUG_PM("Power down VPP...\n");
	p_island = get_island_ptr(OSPM_VIDEO_VPP_ISLAND);

	if (!p_island) {
		DRM_ERROR("p_island is NULL\n");
		return;
	}

	mutex_lock(&g_ospm_data->ospm_lock);

	if (!ospm_power_is_hw_on(OSPM_VIDEO_VPP_ISLAND))
		goto out;

	island_ref = atomic_read(&p_island->ref_count);
	if (island_ref)
		PSB_DEBUG_PM("VPP ref_count has been set(%d), bypass\n",
			     island_ref);

	if (vsp_priv->vsp_cmd_num > 0) {
		VSP_DEBUG("command in VSP, by pass\n");
		goto out;
	}

	ret = p_island->p_funcs->power_down(
		g_ospm_data->dev,
		p_island);

	/* set the island state */
	if (ret) {
		p_island->island_state = OSPM_POWER_OFF;
		atomic_set(&p_island->ref_count, 0);
	}

	/* handle the dependency */
	if (p_island->p_dependency) {
		/* Power down dependent island */
		do {
			power_down_island(p_island->p_dependency);
			island_ref--;
		} while (island_ref);
	}

	if (!any_island_on()) {
		PSB_DEBUG_PM("Suspending PCI\n");
		pm_qos_update_request(&dev_priv->s0ix_qos,
			CSTATE_EXIT_LATENCY_S0i1 - 1);
		pm_runtime_put(&g_ospm_data->dev->pdev->dev);
		wake_unlock(&dev_priv->ospm_wake_lock);
	}

	PSB_DEBUG_PM("Power down VPP done\n");
out:
	mutex_unlock(&g_ospm_data->ospm_lock);
	return;
}
#endif

int ospm_runtime_pm_allow(struct drm_device *dev)
{
	return 0;
}

void ospm_runtime_pm_forbid(struct drm_device *dev)
{
//	struct drm_psb_private *dev_priv = dev->dev_private;

//	pm_runtime_forbid(&dev->pdev->dev);
}