aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/mali400/r3p2/mali/common/mali_pp_scheduler.c
blob: 3f10bdce722954f88cc94983fdc5c3ca76fa2a60 (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
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
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
/*
 * Copyright (C) 2012 ARM Limited. All rights reserved.
 * 
 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
 * 
 * A copy of the licence is included with the program, and can also be obtained from Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "mali_pp_scheduler.h"
#include "mali_kernel_common.h"
#include "mali_kernel_core.h"
#include "mali_osk.h"
#include "mali_osk_list.h"
#include "mali_scheduler.h"
#include "mali_pp.h"
#include "mali_pp_job.h"
#include "mali_group.h"
#include "mali_pm.h"
#include "mali_kernel_utilization.h"
#include "mali_session.h"
#include "mali_pm_domain.h"
#include "linux/mali/mali_utgard.h"

#if defined(CONFIG_DMA_SHARED_BUFFER)
#include "mali_dma_buf.h"
#endif
#if defined(CONFIG_GPU_TRACEPOINTS) && defined(CONFIG_TRACEPOINTS)
#include <linux/sched.h>
#include <trace/events/gpu.h>
#endif


/* With certain configurations, job deletion involves functions which cannot be called from atomic context.
 * This #if checks for those cases and enables job deletion to be deferred and run in a different context. */
#if defined(CONFIG_SYNC) || !defined(CONFIG_MALI_DMA_BUF_MAP_ON_ATTACH)
#define MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE 1
#endif

/* Maximum of 8 PP cores (a group can only have maximum of 1 PP core) */
#define MALI_MAX_NUMBER_OF_PP_GROUPS 9

static mali_bool mali_pp_scheduler_is_suspended(void);
static void mali_pp_scheduler_do_schedule(void *arg);
#if defined(MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE)
static void mali_pp_scheduler_do_job_delete(void *arg);
#endif
static void mali_pp_scheduler_job_queued(void);
static void mali_pp_scheduler_job_completed(void);

static u32 pp_version = 0;

/* Physical job queue */
static _MALI_OSK_LIST_HEAD_STATIC_INIT(job_queue);              /* List of physical jobs with some unscheduled work */
static u32 job_queue_depth = 0;

/* Physical groups */
static _MALI_OSK_LIST_HEAD_STATIC_INIT(group_list_working);     /* List of physical groups with working jobs on the pp core */
static _MALI_OSK_LIST_HEAD_STATIC_INIT(group_list_idle);        /* List of physical groups with idle jobs on the pp core */
static _MALI_OSK_LIST_HEAD_STATIC_INIT(group_list_disabled);    /* List of disabled physical groups */

/* Virtual job queue (Mali-450 only) */
static _MALI_OSK_LIST_HEAD_STATIC_INIT(virtual_job_queue);      /* List of unstarted jobs for the virtual group */
static u32 virtual_job_queue_depth = 0;

/* Virtual group (Mali-450 only) */
static struct mali_group *virtual_group = NULL;                 /* Virtual group (if any) */
static enum
{
	VIRTUAL_GROUP_IDLE,
	VIRTUAL_GROUP_WORKING,
	VIRTUAL_GROUP_DISABLED,
}
virtual_group_state = VIRTUAL_GROUP_IDLE;            /* Flag which indicates whether the virtual group is working or idle */

/* Number of physical cores */
static u32 num_cores = 0;
static u32 enabled_cores = 0;

/* Variables to allow safe pausing of the scheduler */
static _mali_osk_wait_queue_t *pp_scheduler_working_wait_queue = NULL;
static u32 pause_count = 0;

static _mali_osk_lock_t *pp_scheduler_lock = NULL;
/* Contains tid of thread that locked the scheduler or 0, if not locked */
MALI_DEBUG_CODE(static u32 pp_scheduler_lock_owner = 0);

static _mali_osk_wq_work_t *pp_scheduler_wq_schedule = NULL;

#if defined(MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE)
static _mali_osk_wq_work_t *pp_scheduler_wq_job_delete = NULL;
static _mali_osk_lock_t *pp_scheduler_job_delete_lock = NULL;
static _MALI_OSK_LIST_HEAD_STATIC_INIT(pp_scheduler_job_deletion_queue);
#endif

MALI_STATIC_INLINE mali_bool mali_pp_scheduler_has_virtual_group(void)
{
	return NULL != virtual_group;
}

_mali_osk_errcode_t mali_pp_scheduler_initialize(void)
{
	_mali_osk_lock_flags_t lock_flags;

#if defined(MALI_UPPER_HALF_SCHEDULING)
	lock_flags = _MALI_OSK_LOCKFLAG_ORDERED | _MALI_OSK_LOCKFLAG_SPINLOCK_IRQ | _MALI_OSK_LOCKFLAG_NONINTERRUPTABLE;
#else
	lock_flags = _MALI_OSK_LOCKFLAG_ORDERED | _MALI_OSK_LOCKFLAG_SPINLOCK | _MALI_OSK_LOCKFLAG_NONINTERRUPTABLE;
#endif

	pp_scheduler_lock = _mali_osk_lock_init(lock_flags, 0, _MALI_OSK_LOCK_ORDER_SCHEDULER);
	if (NULL == pp_scheduler_lock)
	{
		return _MALI_OSK_ERR_NOMEM;
	}

	pp_scheduler_working_wait_queue = _mali_osk_wait_queue_init();
	if (NULL == pp_scheduler_working_wait_queue)
	{
		_mali_osk_lock_term(pp_scheduler_lock);
		return _MALI_OSK_ERR_NOMEM;
	}

	pp_scheduler_wq_schedule = _mali_osk_wq_create_work(mali_pp_scheduler_do_schedule, NULL);
	if (NULL == pp_scheduler_wq_schedule)
	{
		_mali_osk_wait_queue_term(pp_scheduler_working_wait_queue);
		_mali_osk_lock_term(pp_scheduler_lock);
		return _MALI_OSK_ERR_NOMEM;
	}

#if defined(MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE)
	pp_scheduler_wq_job_delete = _mali_osk_wq_create_work(mali_pp_scheduler_do_job_delete, NULL);
	if (NULL == pp_scheduler_wq_job_delete)
	{
		_mali_osk_wq_delete_work(pp_scheduler_wq_schedule);
		_mali_osk_wait_queue_term(pp_scheduler_working_wait_queue);
		_mali_osk_lock_term(pp_scheduler_lock);
		return _MALI_OSK_ERR_NOMEM;
	}

	pp_scheduler_job_delete_lock = _mali_osk_lock_init(_MALI_OSK_LOCKFLAG_ORDERED | _MALI_OSK_LOCKFLAG_SPINLOCK_IRQ |_MALI_OSK_LOCKFLAG_NONINTERRUPTABLE, 0, _MALI_OSK_LOCK_ORDER_SCHEDULER_DEFERRED);
	if (NULL == pp_scheduler_job_delete_lock)
	{
		_mali_osk_wq_delete_work(pp_scheduler_wq_job_delete);
		_mali_osk_wq_delete_work(pp_scheduler_wq_schedule);
		_mali_osk_wait_queue_term(pp_scheduler_working_wait_queue);
		_mali_osk_lock_term(pp_scheduler_lock);
		return _MALI_OSK_ERR_NOMEM;
	}
#endif

	return _MALI_OSK_ERR_OK;
}

void mali_pp_scheduler_terminate(void)
{
#if defined(MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE)
	_mali_osk_lock_term(pp_scheduler_job_delete_lock);
	_mali_osk_wq_delete_work(pp_scheduler_wq_job_delete);
#endif

	_mali_osk_wq_delete_work(pp_scheduler_wq_schedule);
	_mali_osk_wait_queue_term(pp_scheduler_working_wait_queue);
	_mali_osk_lock_term(pp_scheduler_lock);
}

void mali_pp_scheduler_populate(void)
{
	struct mali_group *group;
	struct mali_pp_core *pp_core;
	u32 num_groups;
	u32 i;

	num_groups = mali_group_get_glob_num_groups();

	/* Do we have a virtual group? */
	for (i = 0; i < num_groups; i++)
	{
		group = mali_group_get_glob_group(i);

		if (mali_group_is_virtual(group))
		{
			MALI_DEBUG_PRINT(3, ("Found virtual group %p\n", group));

			virtual_group = group;
			break;
		}
	}

	/* Find all the available PP cores */
	for (i = 0; i < num_groups; i++)
	{
		group = mali_group_get_glob_group(i);
		pp_core = mali_group_get_pp_core(group);

		if (NULL != pp_core && !mali_group_is_virtual(group))
		{
			if (0 == pp_version)
			{
				/* Retrieve PP version from the first available PP core */
				pp_version = mali_pp_core_get_version(pp_core);
			}

			if (mali_pp_scheduler_has_virtual_group())
			{
				/* Add all physical PP cores to the virtual group */
				mali_group_lock(virtual_group);
				group->state = MALI_GROUP_STATE_JOINING_VIRTUAL;
				mali_group_add_group(virtual_group, group, MALI_TRUE);
				mali_group_unlock(virtual_group);
			}
			else
			{
				_mali_osk_list_add(&group->pp_scheduler_list, &group_list_idle);
			}

			num_cores++;
		}
	}
	enabled_cores = num_cores;
}

void mali_pp_scheduler_depopulate(void)
{
	struct mali_group *group, *temp;

	MALI_DEBUG_ASSERT(_mali_osk_list_empty(&group_list_working));
	MALI_DEBUG_ASSERT(VIRTUAL_GROUP_WORKING != virtual_group_state);

	/* Delete all groups owned by scheduler */
	if (mali_pp_scheduler_has_virtual_group())
	{
		mali_group_delete(virtual_group);
	}

	_MALI_OSK_LIST_FOREACHENTRY(group, temp, &group_list_idle, struct mali_group, pp_scheduler_list)
	{
		mali_group_delete(group);
	}
	_MALI_OSK_LIST_FOREACHENTRY(group, temp, &group_list_disabled, struct mali_group, pp_scheduler_list)
	{
		mali_group_delete(group);
	}
}

MALI_STATIC_INLINE void mali_pp_scheduler_lock(void)
{
	if(_MALI_OSK_ERR_OK != _mali_osk_lock_wait(pp_scheduler_lock, _MALI_OSK_LOCKMODE_RW))
	{
		/* Non-interruptable lock failed: this should never happen. */
		MALI_DEBUG_ASSERT(0);
	}
	MALI_DEBUG_PRINT(5, ("Mali PP scheduler: PP scheduler lock taken\n"));
	MALI_DEBUG_ASSERT(0 == pp_scheduler_lock_owner);
	MALI_DEBUG_CODE(pp_scheduler_lock_owner = _mali_osk_get_tid());
}

MALI_STATIC_INLINE void mali_pp_scheduler_unlock(void)
{
	MALI_DEBUG_PRINT(5, ("Mali PP scheduler: Releasing PP scheduler lock\n"));
	MALI_DEBUG_ASSERT(_mali_osk_get_tid() == pp_scheduler_lock_owner);
	MALI_DEBUG_CODE(pp_scheduler_lock_owner = 0);
	_mali_osk_lock_signal(pp_scheduler_lock, _MALI_OSK_LOCKMODE_RW);
}

MALI_STATIC_INLINE void mali_pp_scheduler_disable_empty_virtual(void)
{
	MALI_ASSERT_GROUP_LOCKED(virtual_group);

	if (mali_group_virtual_disable_if_empty(virtual_group))
	{
		MALI_DEBUG_PRINT(4, ("Disabling empty virtual group\n"));

		MALI_DEBUG_ASSERT(VIRTUAL_GROUP_IDLE == virtual_group_state);

		virtual_group_state = VIRTUAL_GROUP_DISABLED;
	}
}

MALI_STATIC_INLINE void mali_pp_scheduler_enable_empty_virtual(void)
{
	MALI_ASSERT_GROUP_LOCKED(virtual_group);

	if (mali_group_virtual_enable_if_empty(virtual_group))
	{
		MALI_DEBUG_PRINT(4, ("Re-enabling empty virtual group\n"));

		MALI_DEBUG_ASSERT(VIRTUAL_GROUP_DISABLED == virtual_group_state);

		virtual_group_state = VIRTUAL_GROUP_IDLE;
	}
}

#ifdef DEBUG
MALI_STATIC_INLINE void mali_pp_scheduler_assert_locked(void)
{
	MALI_DEBUG_ASSERT(_mali_osk_get_tid() == pp_scheduler_lock_owner);
}
#define MALI_ASSERT_PP_SCHEDULER_LOCKED() mali_pp_scheduler_assert_locked()
#else
#define MALI_ASSERT_PP_SCHEDULER_LOCKED()
#endif

/**
 * Returns a physical job if a physical job is ready to run (no barrier present)
 */
MALI_STATIC_INLINE struct mali_pp_job *mali_pp_scheduler_get_physical_job(void)
{
	MALI_ASSERT_PP_SCHEDULER_LOCKED();

	if (!_mali_osk_list_empty(&job_queue))
	{
		struct mali_pp_job *job;

		MALI_DEBUG_ASSERT(job_queue_depth > 0);
		job = _MALI_OSK_LIST_ENTRY(job_queue.next, struct mali_pp_job, list);

		if (!mali_pp_job_has_active_barrier(job))
		{
			return job;
		}
	}

	return NULL;
}

MALI_STATIC_INLINE void mali_pp_scheduler_dequeue_physical_job(struct mali_pp_job *job)
{
	MALI_ASSERT_PP_SCHEDULER_LOCKED();
	MALI_DEBUG_ASSERT(job_queue_depth > 0);

	/* Remove job from queue */
	if (!mali_pp_job_has_unstarted_sub_jobs(job))
	{
		/* All sub jobs have been started: remove job from queue */
		_mali_osk_list_delinit(&job->list);
	}

	--job_queue_depth;
}

/**
 * Returns a virtual job if a virtual job is ready to run (no barrier present)
 */
MALI_STATIC_INLINE struct mali_pp_job *mali_pp_scheduler_get_virtual_job(void)
{
	MALI_ASSERT_PP_SCHEDULER_LOCKED();
	MALI_DEBUG_ASSERT_POINTER(virtual_group);

	if (!_mali_osk_list_empty(&virtual_job_queue))
	{
		struct mali_pp_job *job;

		MALI_DEBUG_ASSERT(virtual_job_queue_depth > 0);
		job = _MALI_OSK_LIST_ENTRY(virtual_job_queue.next, struct mali_pp_job, list);

		if (!mali_pp_job_has_active_barrier(job))
		{
			return job;
		}
	}

	return NULL;
}

MALI_STATIC_INLINE void mali_pp_scheduler_dequeue_virtual_job(struct mali_pp_job *job)
{
	MALI_ASSERT_PP_SCHEDULER_LOCKED();
	MALI_DEBUG_ASSERT(virtual_job_queue_depth > 0);

	/* Remove job from queue */
	_mali_osk_list_delinit(&job->list);
	--virtual_job_queue_depth;
}

/**
 * Checks if the criteria is met for removing a physical core from virtual group
 */
MALI_STATIC_INLINE mali_bool mali_pp_scheduler_can_move_virtual_to_physical(void)
{
	MALI_ASSERT_PP_SCHEDULER_LOCKED();
	MALI_DEBUG_ASSERT(mali_pp_scheduler_has_virtual_group());
	MALI_ASSERT_GROUP_LOCKED(virtual_group);
	/*
	 * The criteria for taking out a physical group from a virtual group are the following:
	 * - There virtual group is idle
	 * - There are currently no physical groups (idle and working)
	 * - There are physical jobs to be scheduled (without a barrier)
	 */
	return (VIRTUAL_GROUP_IDLE == virtual_group_state) &&
	       _mali_osk_list_empty(&group_list_idle) &&
	       _mali_osk_list_empty(&group_list_working) &&
	       (NULL != mali_pp_scheduler_get_physical_job());
}

MALI_STATIC_INLINE struct mali_group *mali_pp_scheduler_acquire_physical_group(void)
{
	MALI_ASSERT_PP_SCHEDULER_LOCKED();

	if (!_mali_osk_list_empty(&group_list_idle))
	{
		MALI_DEBUG_PRINT(4, ("Mali PP scheduler: Acquiring physical group from idle list\n"));
		return _MALI_OSK_LIST_ENTRY(group_list_idle.next, struct mali_group, pp_scheduler_list);
	}
	else if (mali_pp_scheduler_has_virtual_group())
	{
		MALI_ASSERT_GROUP_LOCKED(virtual_group);
		if (mali_pp_scheduler_can_move_virtual_to_physical())
		{
			struct mali_group *group;
			MALI_DEBUG_PRINT(4, ("Mali PP scheduler: Acquiring physical group from virtual group\n"));
			group = mali_group_acquire_group(virtual_group);

			if (mali_pp_scheduler_has_virtual_group())
			{
				mali_pp_scheduler_disable_empty_virtual();
			}

			return group;
		}
	}

	return NULL;
}

static void mali_pp_scheduler_schedule(void)
{
	struct mali_group* physical_groups_to_start[MALI_MAX_NUMBER_OF_PP_GROUPS-1];
	struct mali_pp_job* physical_jobs_to_start[MALI_MAX_NUMBER_OF_PP_GROUPS-1];
	u32 physical_subjobs_to_start[MALI_MAX_NUMBER_OF_PP_GROUPS-1];
	int num_physical_jobs_to_start = 0;
	int i;

	if (mali_pp_scheduler_has_virtual_group())
	{
		/* Need to lock the virtual group because we might need to grab a physical group from it */
		mali_group_lock(virtual_group);
	}

	mali_pp_scheduler_lock();
	if (pause_count > 0)
	{
		/* Scheduler is suspended, don't schedule any jobs */
		mali_pp_scheduler_unlock();
		if (mali_pp_scheduler_has_virtual_group())
		{
			mali_group_unlock(virtual_group);
		}
		return;
	}

	/* Find physical job(s) to schedule first */
	while (1)
	{
		struct mali_group *group;
		struct mali_pp_job *job;
		u32 subjob;

		job = mali_pp_scheduler_get_physical_job();
		if (NULL == job)
		{
			break; /* No job, early out */
		}

		MALI_DEBUG_ASSERT(!mali_pp_job_is_virtual(job));
		MALI_DEBUG_ASSERT(mali_pp_job_has_unstarted_sub_jobs(job));
		MALI_DEBUG_ASSERT(1 <= mali_pp_job_get_sub_job_count(job));

		/* Acquire a physical group, either from the idle list or from the virtual group.
		 * In case the group was acquired from the virtual group, it's state will be
		 * LEAVING_VIRTUAL and must be set to IDLE before it can be used. */
		group = mali_pp_scheduler_acquire_physical_group();
		if (NULL == group)
		{
			/* Could not get a group to run the job on, early out */
			MALI_DEBUG_PRINT(4, ("Mali PP scheduler: No more physical groups available.\n"));
			break;
		}

		MALI_DEBUG_PRINT(4, ("Mali PP scheduler: Acquired physical group %p\n", group));

		/* Mark subjob as started */
		subjob = mali_pp_job_get_first_unstarted_sub_job(job);
		mali_pp_job_mark_sub_job_started(job, subjob);

		/* Remove job from queue (if we now got the last subjob) */
		mali_pp_scheduler_dequeue_physical_job(job);

		/* Move group to working list */
		_mali_osk_list_move(&(group->pp_scheduler_list), &group_list_working);

		/* Keep track of this group, so that we actually can start the job once we are done with the scheduler lock we are now holding */
		physical_groups_to_start[num_physical_jobs_to_start] = group;
		physical_jobs_to_start[num_physical_jobs_to_start] = job;
		physical_subjobs_to_start[num_physical_jobs_to_start] = subjob;
		++num_physical_jobs_to_start;

		MALI_DEBUG_ASSERT(num_physical_jobs_to_start < MALI_MAX_NUMBER_OF_PP_GROUPS);
	}

	/* See if we have a virtual job to schedule */
	if (mali_pp_scheduler_has_virtual_group())
	{
		if (VIRTUAL_GROUP_IDLE == virtual_group_state)
		{
			struct mali_pp_job *job = mali_pp_scheduler_get_virtual_job();
			if (NULL != job)
			{
				MALI_DEBUG_ASSERT(mali_pp_job_is_virtual(job));
				MALI_DEBUG_ASSERT(mali_pp_job_has_unstarted_sub_jobs(job));
				MALI_DEBUG_ASSERT(1 == mali_pp_job_get_sub_job_count(job));

				/* Mark the one and only subjob as started */
				mali_pp_job_mark_sub_job_started(job, 0);

				/* Remove job from queue */
				mali_pp_scheduler_dequeue_virtual_job(job);

				/* Virtual group is now working */
				virtual_group_state = VIRTUAL_GROUP_WORKING;

				/*
				 * We no longer need the scheduler lock,
				 * but we still need the virtual lock in order to start the virtual job
				 */
				mali_pp_scheduler_unlock();

				/* Start job */
				mali_group_start_pp_job(virtual_group, job, 0);
				MALI_DEBUG_PRINT(4, ("Mali PP scheduler: Virtual job %u (0x%08X) part %u/%u started (from schedule)\n",
				                     mali_pp_job_get_id(job), job, 1,
				                     mali_pp_job_get_sub_job_count(job)));

				/* And now we are all done with the virtual_group lock as well */
				mali_group_unlock(virtual_group);
			}
			else
			{
				/* No virtual job, release the two locks we are holding */
				mali_pp_scheduler_unlock();
				mali_group_unlock(virtual_group);
			}
		}
		else
		{
			/* Virtual core busy, release the two locks we are holding */
			mali_pp_scheduler_unlock();
			mali_group_unlock(virtual_group);
		}

	}
	else
	{
		/* There is no virtual group, release the only lock we are holding */
		mali_pp_scheduler_unlock();
	}

	/*
	 * Now we have released the scheduler lock, and we are ready to kick of the actual starting of the
	 * physical jobs.
	 * The reason we want to wait until we have released the scheduler lock is that job start actually
	 * may take quite a bit of time (quite many registers needs to be written). This will allow new jobs
	 * from user space to come in, and post processing of other PP jobs to happen at the same time as we
	 * start jobs.
	 */
	for (i = 0; i < num_physical_jobs_to_start; i++)
	{
		struct mali_group *group = physical_groups_to_start[i];
		struct mali_pp_job *job  = physical_jobs_to_start[i];
		u32 sub_job              = physical_subjobs_to_start[i];

		MALI_DEBUG_ASSERT_POINTER(group);
		MALI_DEBUG_ASSERT_POINTER(job);

		mali_group_lock(group);

		/* In case this group was acquired from a virtual core, update it's state to IDLE */
		group->state = MALI_GROUP_STATE_IDLE;

		mali_group_start_pp_job(group, job, sub_job);
		MALI_DEBUG_PRINT(4, ("Mali PP scheduler: Physical job %u (0x%08X) part %u/%u started (from schedule)\n",
		                     mali_pp_job_get_id(job), job, sub_job + 1,
		                     mali_pp_job_get_sub_job_count(job)));

		mali_group_unlock(group);

		/* remove the return value from mali_group_start_xx_job, since we can't fail on Mali-300++ */
	}
}

static void mali_pp_scheduler_return_job_to_user(struct mali_pp_job *job, mali_bool deferred)
{
	if (MALI_FALSE == mali_pp_job_use_no_notification(job))
	{
		u32 i;
		u32 num_counters_to_copy;
		mali_bool success = mali_pp_job_was_success(job);

		_mali_uk_pp_job_finished_s *jobres = job->finished_notification->result_buffer;
		_mali_osk_memset(jobres, 0, sizeof(_mali_uk_pp_job_finished_s)); /* @@@@ can be removed once we initialize all members in this struct */
		jobres->user_job_ptr = mali_pp_job_get_user_id(job);
		if (MALI_TRUE == success)
		{
			jobres->status = _MALI_UK_JOB_STATUS_END_SUCCESS;
		}
		else
		{
			jobres->status = _MALI_UK_JOB_STATUS_END_UNKNOWN_ERR;
		}

		if (mali_pp_job_is_virtual(job))
		{
			num_counters_to_copy = num_cores; /* Number of physical cores available */
		}
		else
		{
			num_counters_to_copy = mali_pp_job_get_sub_job_count(job);
		}

		for (i = 0; i < num_counters_to_copy; i++)
		{
			jobres->perf_counter0[i] = mali_pp_job_get_perf_counter_value0(job, i);
			jobres->perf_counter1[i] = mali_pp_job_get_perf_counter_value1(job, i);
		}

		mali_session_send_notification(mali_pp_job_get_session(job), job->finished_notification);
		job->finished_notification = NULL;
	}

#if defined(MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE)
	if (MALI_TRUE == deferred)
	{
		/* The deletion of the job object (releasing sync refs etc) must be done in a different context */
		_mali_osk_lock_wait(pp_scheduler_job_delete_lock, _MALI_OSK_LOCKMODE_RW);

		MALI_DEBUG_ASSERT(_mali_osk_list_empty(&job->list)); /* This job object should not be on any list */
		_mali_osk_list_addtail(&job->list, &pp_scheduler_job_deletion_queue);

		_mali_osk_lock_signal(pp_scheduler_job_delete_lock, _MALI_OSK_LOCKMODE_RW);

		_mali_osk_wq_schedule_work(pp_scheduler_wq_job_delete);
	}
	else
	{
		mali_pp_job_delete(job);
	}
#else
	MALI_DEBUG_ASSERT(MALI_FALSE == deferred); /* no use cases need this in this configuration */
	mali_pp_job_delete(job);
#endif
}

static void mali_pp_scheduler_do_schedule(void *arg)
{
	MALI_IGNORE(arg);

	mali_pp_scheduler_schedule();
}

#if defined(MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE)
static void mali_pp_scheduler_do_job_delete(void *arg)
{
	_MALI_OSK_LIST_HEAD_STATIC_INIT(list);
	struct mali_pp_job *job;
	struct mali_pp_job *tmp;

	MALI_IGNORE(arg);

	_mali_osk_lock_wait(pp_scheduler_job_delete_lock, _MALI_OSK_LOCKMODE_RW);

	/*
	 * Quickly "unhook" the jobs pending to be deleted, so we can release the lock before
	 * we start deleting the job objects (without any locks held
	 */
	_mali_osk_list_move_list(&pp_scheduler_job_deletion_queue, &list);

	_mali_osk_lock_signal(pp_scheduler_job_delete_lock, _MALI_OSK_LOCKMODE_RW);

	_MALI_OSK_LIST_FOREACHENTRY(job, tmp, &list, struct mali_pp_job, list)
	{
		mali_pp_job_delete(job); /* delete the job object itself */
	}
}
#endif

void mali_pp_scheduler_job_done(struct mali_group *group, struct mali_pp_job *job, u32 sub_job, mali_bool success)
{
	mali_bool job_is_done;
	mali_bool barrier_enforced = MALI_FALSE;

	MALI_DEBUG_PRINT(3, ("Mali PP scheduler: %s job %u (0x%08X) part %u/%u completed (%s)\n",
	                     mali_pp_job_is_virtual(job) ? "Virtual" : "Physical",
	                     mali_pp_job_get_id(job),
	                     job, sub_job + 1,
	                     mali_pp_job_get_sub_job_count(job),
	                     success ? "success" : "failure"));
	MALI_ASSERT_GROUP_LOCKED(group);
	mali_pp_scheduler_lock();

	mali_pp_job_mark_sub_job_completed(job, success);

	MALI_DEBUG_ASSERT(mali_pp_job_is_virtual(job) == mali_group_is_virtual(group));

	job_is_done = mali_pp_job_is_complete(job);

	if (job_is_done)
	{
		struct mali_session_data *session = mali_pp_job_get_session(job);
		struct mali_pp_job *job_head;

		/* Remove job from session list */
		_mali_osk_list_del(&job->session_list);

		MALI_DEBUG_PRINT(4, ("Mali PP scheduler: All parts completed for %s job %u (0x%08X)\n",
		                     mali_pp_job_is_virtual(job) ? "virtual" : "physical",
		                     mali_pp_job_get_id(job), job));
#if defined(CONFIG_SYNC)
		if (job->sync_point)
		{
			int error;
			if (success) error = 0;
			else error = -EFAULT;
			MALI_DEBUG_PRINT(4, ("Sync: Signal %spoint for job %d\n",
			                     success ? "" : "failed ",
					     mali_pp_job_get_id(job)));
			mali_sync_signal_pt(job->sync_point, error);
		}
#endif

		/* Send notification back to user space */
#if defined(MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE)
		mali_pp_scheduler_return_job_to_user(job, MALI_TRUE);
#else
		mali_pp_scheduler_return_job_to_user(job, MALI_FALSE);
#endif

		mali_pp_scheduler_job_completed();

		/* Resolve any barriers */
		if (!_mali_osk_list_empty(&session->job_list))
		{
			job_head = _MALI_OSK_LIST_ENTRY(session->job_list.next, struct mali_pp_job, session_list);
			if (mali_pp_job_has_active_barrier(job_head))
			{
				barrier_enforced = MALI_TRUE;
				mali_pp_job_barrier_enforced(job_head);
			}
		}
	}

	/* If paused, then this was the last job, so wake up sleeping workers */
	if (pause_count > 0)
	{
		/* Wake up sleeping workers. Their wake-up condition is that
		 * num_slots == num_slots_idle, so unless we are done working, no
		 * threads will actually be woken up.
		 */
		if (mali_group_is_virtual(group))
		{
			virtual_group_state = VIRTUAL_GROUP_IDLE;
		}
		else
		{
			_mali_osk_list_move(&(group->pp_scheduler_list), &group_list_idle);
		}
#if defined(CONFIG_GPU_TRACEPOINTS) && defined(CONFIG_TRACEPOINTS)
			trace_gpu_sched_switch(mali_pp_get_hw_core_desc(group->pp_core), sched_clock(), 0, 0, 0);
#endif
		_mali_osk_wait_queue_wake_up(pp_scheduler_working_wait_queue);
		mali_pp_scheduler_unlock();
		return;
	}

	if (barrier_enforced)
	{
		/* A barrier was resolved, so schedule previously blocked jobs */
		_mali_osk_wq_schedule_work(pp_scheduler_wq_schedule);
	}

	/* Recycle variables */
	job = NULL;
	sub_job = 0;

	if (mali_group_is_virtual(group))
	{
		/* Virtual group */

		/* Now that the virtual group is idle, check if we should reconfigure */
		struct mali_pp_job *physical_job = NULL;
		struct mali_group *physical_group = NULL;

		/* Obey the policy */
		virtual_group_state = VIRTUAL_GROUP_IDLE;

		if (mali_pp_scheduler_can_move_virtual_to_physical())
		{
			/* There is a runnable physical job and we can acquire a physical group */
			physical_job = mali_pp_scheduler_get_physical_job();
			MALI_DEBUG_ASSERT(mali_pp_job_has_unstarted_sub_jobs(physical_job));

			/* Mark subjob as started */
			sub_job = mali_pp_job_get_first_unstarted_sub_job(physical_job);
			mali_pp_job_mark_sub_job_started(physical_job, sub_job);

			/* Remove job from queue (if we now got the last subjob) */
			mali_pp_scheduler_dequeue_physical_job(physical_job);

			/* Acquire a physical group from the virtual group
			 * It's state will be LEAVING_VIRTUAL and must be set to IDLE before it can be used */
			physical_group = mali_group_acquire_group(virtual_group);

			/* Move physical group to the working list, as we will soon start a job on it */
			_mali_osk_list_move(&(physical_group->pp_scheduler_list), &group_list_working);

			mali_pp_scheduler_disable_empty_virtual();
		}

		/* Start the next virtual job */
		job = mali_pp_scheduler_get_virtual_job();
		if (NULL != job && VIRTUAL_GROUP_IDLE == virtual_group_state)
		{
			/* There is a runnable virtual job */
			MALI_DEBUG_ASSERT(mali_pp_job_is_virtual(job));
			MALI_DEBUG_ASSERT(mali_pp_job_has_unstarted_sub_jobs(job));
			MALI_DEBUG_ASSERT(1 == mali_pp_job_get_sub_job_count(job));

			mali_pp_job_mark_sub_job_started(job, 0);

			/* Remove job from queue */
			mali_pp_scheduler_dequeue_virtual_job(job);

			/* Virtual group is now working */
			virtual_group_state = VIRTUAL_GROUP_WORKING;

			mali_pp_scheduler_unlock();

			/* Start job */
			mali_group_start_pp_job(group, job, 0);
			MALI_DEBUG_PRINT(4, ("Mali PP scheduler: Virtual job %u (0x%08X) part %u/%u started (from job_done)\n",
			                     mali_pp_job_get_id(job), job, 1,
			                     mali_pp_job_get_sub_job_count(job)));
		}
		else
		{
#if defined(CONFIG_GPU_TRACEPOINTS) && defined(CONFIG_TRACEPOINTS)
			trace_gpu_sched_switch("Mali_Virtual_PP", sched_clock(), 0, 0, 0);
#endif
			mali_pp_scheduler_unlock();
		}

		/* Start a physical job (if we acquired a physical group earlier) */
		if (NULL != physical_job && NULL != physical_group)
		{
			mali_group_lock(physical_group);

			/* Set the group state from LEAVING_VIRTUAL to IDLE to complete the transition */
			physical_group->state = MALI_GROUP_STATE_IDLE;

			/* Start job on core */
			mali_group_start_pp_job(physical_group, physical_job, sub_job);
			MALI_DEBUG_PRINT(4, ("Mali PP scheduler: Physical job %u (0x%08X) part %u/%u started (from job_done)\n",
			                     mali_pp_job_get_id(physical_job), physical_job, sub_job + 1,
			                     mali_pp_job_get_sub_job_count(physical_job)));

			mali_group_unlock(physical_group);
		}
	}
	else
	{
		/* Physical group */
		job = mali_pp_scheduler_get_physical_job();
		if (NULL != job)
		{
			/* There is a runnable physical job */
			MALI_DEBUG_ASSERT(mali_pp_job_has_unstarted_sub_jobs(job));

			/* Mark subjob as started */
			sub_job = mali_pp_job_get_first_unstarted_sub_job(job);
			mali_pp_job_mark_sub_job_started(job, sub_job);

			/* Remove job from queue (if we now got the last subjob) */
			mali_pp_scheduler_dequeue_physical_job(job);

			mali_pp_scheduler_unlock();

			/* Group is already on the working list, so start the job */
			mali_group_start_pp_job(group, job, sub_job);
			MALI_DEBUG_PRINT(4, ("Mali PP scheduler: Physical job %u (0x%08X) part %u/%u started (from job_done)\n",
			                     mali_pp_job_get_id(job), job, sub_job + 1,
			                     mali_pp_job_get_sub_job_count(job)));
		}
		else if (mali_pp_scheduler_has_virtual_group())
		{
			/* Rejoin virtual group */
			/* In the future, a policy check might be useful here */

			/* We're no longer needed on the scheduler list */
			_mali_osk_list_delinit(&(group->pp_scheduler_list));

			/* Make sure no interrupts are handled for this group during
			 * the transition from physical to virtual */
			group->state = MALI_GROUP_STATE_JOINING_VIRTUAL;

			mali_pp_scheduler_unlock();
			mali_group_unlock(group);

			mali_group_lock(virtual_group);

			if (mali_pp_scheduler_has_virtual_group())
			{
				mali_pp_scheduler_enable_empty_virtual();
			}

			/* We need to recheck the group state since it is possible that someone has
			 * modified the group before we locked the virtual group. */
			if (MALI_GROUP_STATE_JOINING_VIRTUAL == group->state)
			{
				mali_group_add_group(virtual_group, group, MALI_TRUE);
			}

			mali_group_unlock(virtual_group);

			if (mali_pp_scheduler_has_virtual_group() && VIRTUAL_GROUP_IDLE == virtual_group_state)
			{
				_mali_osk_wq_schedule_work(pp_scheduler_wq_schedule);
			}

			/* We need to return from this function with the group lock held */
			mali_group_lock(group);
		}
		else
		{
			_mali_osk_list_move(&(group->pp_scheduler_list), &group_list_idle);
#if defined(CONFIG_GPU_TRACEPOINTS) && defined(CONFIG_TRACEPOINTS)
			trace_gpu_sched_switch(mali_pp_get_hw_core_desc(group->pp_core), sched_clock(), 0, 0, 0);
#endif
			mali_pp_scheduler_unlock();
		}
	}
}

void mali_pp_scheduler_suspend(void)
{
	mali_pp_scheduler_lock();
	pause_count++; /* Increment the pause_count so that no more jobs will be scheduled */
	mali_pp_scheduler_unlock();

	/* Go to sleep. When woken up again (in mali_pp_scheduler_job_done), the
	 * mali_pp_scheduler_suspended() function will be called. This will return true
	 * iff state is idle and pause_count > 0, so if the core is active this
	 * will not do anything.
	 */
	_mali_osk_wait_queue_wait_event(pp_scheduler_working_wait_queue, mali_pp_scheduler_is_suspended);
}

void mali_pp_scheduler_resume(void)
{
	mali_pp_scheduler_lock();
	pause_count--; /* Decrement pause_count to allow scheduling again (if it reaches 0) */
	mali_pp_scheduler_unlock();
	if (0 == pause_count)
	{
		mali_pp_scheduler_schedule();
	}
}

MALI_STATIC_INLINE void mali_pp_scheduler_queue_job(struct mali_pp_job *job, struct mali_session_data *session)
{
	MALI_DEBUG_ASSERT_POINTER(job);

#if defined(CONFIG_GPU_TRACEPOINTS) && defined(CONFIG_TRACEPOINTS)
	trace_gpu_job_enqueue(mali_pp_job_get_tid(job), mali_pp_job_get_id(job), "PP");
#endif

#if defined(CONFIG_DMA_SHARED_BUFFER) && !defined(CONFIG_MALI_DMA_BUF_MAP_ON_ATTACH)
	/* Map buffers attached to job */
	if (0 != job->num_memory_cookies)
	{
		mali_dma_buf_map_job(job);
	}
#endif /* CONFIG_DMA_SHARED_BUFFER */

	mali_pp_scheduler_job_queued();

	mali_pp_scheduler_lock();

	if (mali_pp_job_is_virtual(job))
	{
		/* Virtual job */
		virtual_job_queue_depth += 1;
		_mali_osk_list_addtail(&job->list, &virtual_job_queue);
	}
	else
	{
		job_queue_depth += mali_pp_job_get_sub_job_count(job);
		_mali_osk_list_addtail(&job->list, &job_queue);
	}

	if (mali_pp_job_has_active_barrier(job) && _mali_osk_list_empty(&session->job_list))
	{
		/* No running jobs on this session, so barrier condition already met */
		mali_pp_job_barrier_enforced(job);
	}

	/* Add job to session list */
	_mali_osk_list_addtail(&job->session_list, &session->job_list);

	MALI_DEBUG_PRINT(3, ("Mali PP scheduler: %s job %u (0x%08X) with %u parts queued\n",
	                     mali_pp_job_is_virtual(job) ? "Virtual" : "Physical",
	                     mali_pp_job_get_id(job), job, mali_pp_job_get_sub_job_count(job)));

	mali_pp_scheduler_unlock();
}

#if defined(CONFIG_SYNC)
static void sync_callback(struct sync_fence *fence, struct sync_fence_waiter *waiter)
{
	struct mali_pp_job *job = _MALI_OSK_CONTAINER_OF(waiter, struct mali_pp_job, sync_waiter);

	/* Schedule sync_callback_work */
	_mali_osk_wq_schedule_work(job->sync_work);
}

static void sync_callback_work(void *arg)
{
	struct mali_pp_job *job = (struct mali_pp_job *)arg;
	struct mali_session_data *session;
	int err;

	MALI_DEBUG_ASSERT_POINTER(job);

	session = job->session;

	/* Remove job from session pending job list */
	_mali_osk_lock_wait(session->pending_jobs_lock, _MALI_OSK_LOCKMODE_RW);
	_mali_osk_list_delinit(&job->list);
	_mali_osk_lock_signal(session->pending_jobs_lock, _MALI_OSK_LOCKMODE_RW);

	err = sync_fence_wait(job->pre_fence, 0);
	if (likely(0 == err))
	{
		MALI_DEBUG_PRINT(3, ("Mali sync: Job %d ready to run\n", mali_pp_job_get_id(job)));

		mali_pp_scheduler_queue_job(job, session);

		mali_pp_scheduler_schedule();
	}
	else
	{
		/* Fence signaled error */
		MALI_DEBUG_PRINT(3, ("Mali sync: Job %d abort due to sync error\n", mali_pp_job_get_id(job)));

		if (job->sync_point) mali_sync_signal_pt(job->sync_point, err);

		mali_pp_job_mark_sub_job_completed(job, MALI_FALSE); /* Flagging the job as failed. */
		mali_pp_scheduler_return_job_to_user(job, MALI_FALSE); /* This will also delete the job object */
	}
}
#endif

_mali_osk_errcode_t _mali_ukk_pp_start_job(void *ctx, _mali_uk_pp_start_job_s *uargs, int *fence)
{
	struct mali_session_data *session;
	struct mali_pp_job *job;

	MALI_DEBUG_ASSERT_POINTER(uargs);
	MALI_DEBUG_ASSERT_POINTER(ctx);

	session = (struct mali_session_data*)ctx;

	job = mali_pp_job_create(session, uargs, mali_scheduler_get_new_id());
	if (NULL == job)
	{
		MALI_PRINT_ERROR(("Failed to create job!\n"));
		return _MALI_OSK_ERR_NOMEM;
	}

	if (_MALI_OSK_ERR_OK != mali_pp_job_check(job))
	{
		/* Not a valid job, return to user immediately */
		mali_pp_job_mark_sub_job_completed(job, MALI_FALSE); /* Flagging the job as failed. */
		mali_pp_scheduler_return_job_to_user(job, MALI_FALSE); /* This will also delete the job object */
		return _MALI_OSK_ERR_OK; /* User is notified via a notification, so this call is ok */
	}

#if PROFILING_SKIP_PP_JOBS || PROFILING_SKIP_PP_AND_GP_JOBS
#warning PP jobs will not be executed
	mali_pp_scheduler_return_job_to_user(job, MALI_FALSE);
	return _MALI_OSK_ERR_OK;
#endif

#if defined(CONFIG_SYNC)
	if (_MALI_PP_JOB_FLAG_FENCE & job->uargs.flags)
	{
		int post_fence = -1;

		job->sync_point = mali_stream_create_point(job->uargs.stream);

		if (unlikely(NULL == job->sync_point))
		{
			/* Fence creation failed. */
			MALI_DEBUG_PRINT(2, ("Failed to create sync point for job %d\n", mali_pp_job_get_id(job)));
			mali_pp_job_mark_sub_job_completed(job, MALI_FALSE); /* Flagging the job as failed. */
			mali_pp_scheduler_return_job_to_user(job, MALI_FALSE); /* This will also delete the job object */
			return _MALI_OSK_ERR_OK; /* User is notified via a notification, so this call is ok */
		}

		post_fence = mali_stream_create_fence(job->sync_point);

		if (unlikely(0 > post_fence))
		{
			/* Fence creation failed. */
			/* mali_stream_create_fence already freed the sync_point */
			MALI_DEBUG_PRINT(2, ("Failed to create fence for job %d\n", mali_pp_job_get_id(job)));
			mali_pp_job_mark_sub_job_completed(job, MALI_FALSE); /* Flagging the job as failed. */
			mali_pp_scheduler_return_job_to_user(job, MALI_FALSE); /* This will also delete the job object */
			return _MALI_OSK_ERR_OK; /* User is notified via a notification, so this call is ok */
		}

		/* Grab a reference to the fence. It must be around when the
		 * job is completed, so the point can be signalled. */
		sync_fence_fdget(post_fence);

		*fence = post_fence;

		MALI_DEBUG_PRINT(3, ("Sync: Created fence %d for job %d\n", post_fence, mali_pp_job_get_id(job)));
	}
	else if (_MALI_PP_JOB_FLAG_EMPTY_FENCE & job->uargs.flags)
	{
		int empty_fence_fd = job->uargs.stream;
		struct sync_fence *empty_fence;
		struct sync_pt *pt;
		int ret;

		/* Grab and keep a reference to the fence. It must be around
		 * when the job is completed, so the point can be signalled. */
		empty_fence = sync_fence_fdget(empty_fence_fd);

		if (unlikely(NULL == empty_fence))
		{
			MALI_DEBUG_PRINT_ERROR(("Failed to accept empty fence: %d\n", empty_fence_fd));
			mali_pp_job_mark_sub_job_completed(job, MALI_FALSE); /* Flagging the job as failed. */
			mali_pp_scheduler_return_job_to_user(job, MALI_FALSE); /* This will also delete the job object */
			return _MALI_OSK_ERR_OK;
		}

		if (unlikely(list_empty(&empty_fence->pt_list_head)))
		{
			MALI_DEBUG_PRINT_ERROR(("Failed to accept empty fence: %d\n", empty_fence_fd));
			sync_fence_put(empty_fence);
			mali_pp_job_mark_sub_job_completed(job, MALI_FALSE); /* Flagging the job as failed. */
			mali_pp_scheduler_return_job_to_user(job, MALI_FALSE); /* This will also delete the job object */
			return _MALI_OSK_ERR_OK;
		}

		pt = list_first_entry(&empty_fence->pt_list_head, struct sync_pt, pt_list);

		ret = mali_sync_timed_commit(pt);

		if (unlikely(0 != ret))
		{
			MALI_DEBUG_PRINT_ERROR(("Empty fence not valid: %d\n", empty_fence_fd));
			sync_fence_put(empty_fence);
			mali_pp_job_mark_sub_job_completed(job, MALI_FALSE); /* Flagging the job as failed. */
			mali_pp_scheduler_return_job_to_user(job, MALI_FALSE); /* This will also delete the job object */
			return _MALI_OSK_ERR_OK;
		}

		job->sync_point = pt;

		*fence = empty_fence_fd;

		MALI_DEBUG_PRINT(3, ("Sync: Job %d now backs fence %d\n", mali_pp_job_get_id(job), empty_fence_fd));
	}

	if (0 < job->uargs.fence)
	{
		int pre_fence_fd = job->uargs.fence;
		int err;

		MALI_DEBUG_PRINT(3, ("Sync: Job %d waiting for fence %d\n", mali_pp_job_get_id(job), pre_fence_fd));

		job->pre_fence = sync_fence_fdget(pre_fence_fd); /* Reference will be released when job is deleted. */
		if (NULL == job->pre_fence)
		{
			MALI_DEBUG_PRINT(2, ("Failed to import fence %d\n", pre_fence_fd));
			if (job->sync_point) mali_sync_signal_pt(job->sync_point, -EINVAL);
			mali_pp_job_mark_sub_job_completed(job, MALI_FALSE); /* Flagging the job as failed. */
			mali_pp_scheduler_return_job_to_user(job, MALI_FALSE); /* This will also delete the job object */
			return _MALI_OSK_ERR_OK; /* User is notified via a notification, so this call is ok */
		}

		job->sync_work = _mali_osk_wq_create_work(sync_callback_work, (void*)job);
		if (NULL == job->sync_work)
		{
			if (job->sync_point) mali_sync_signal_pt(job->sync_point, -ENOMEM);
			mali_pp_job_mark_sub_job_completed(job, MALI_FALSE); /* Flagging the job as failed. */
			mali_pp_scheduler_return_job_to_user(job, MALI_FALSE); /* This will also delete the job object */
			return _MALI_OSK_ERR_OK; /* User is notified via a notification, so this call is ok */
		}

		/* Add pending job to session pending job list */
		_mali_osk_lock_wait(session->pending_jobs_lock, _MALI_OSK_LOCKMODE_RW);
		_mali_osk_list_addtail(&job->list, &session->pending_jobs);
		_mali_osk_lock_signal(session->pending_jobs_lock, _MALI_OSK_LOCKMODE_RW);

		sync_fence_waiter_init(&job->sync_waiter, sync_callback);
		err = sync_fence_wait_async(job->pre_fence, &job->sync_waiter);

		if (0 != err)
		{
			/* No async wait started, remove job from session pending job list */
			_mali_osk_lock_wait(session->pending_jobs_lock, _MALI_OSK_LOCKMODE_RW);
			_mali_osk_list_delinit(&job->list);
			_mali_osk_lock_signal(session->pending_jobs_lock, _MALI_OSK_LOCKMODE_RW);
		}

		if (1 == err)
		{
			/* Fence has already signalled */
			mali_pp_scheduler_queue_job(job, session);
			if (!_mali_osk_list_empty(&group_list_idle) || VIRTUAL_GROUP_IDLE == virtual_group_state)
			{
				mali_pp_scheduler_schedule();
			}
			return _MALI_OSK_ERR_OK;
		}
		else if (0 > err)
		{
			/* Sync fail */
			if (job->sync_point) mali_sync_signal_pt(job->sync_point, err);
			mali_pp_job_mark_sub_job_completed(job, MALI_FALSE); /* Flagging the job as failed. */
			mali_pp_scheduler_return_job_to_user(job, MALI_FALSE); /* This will also delete the job object */
			return _MALI_OSK_ERR_OK; /* User is notified via a notification, so this call is ok */
		}

	}
	else
#endif /* CONFIG_SYNC */
	{
		mali_pp_scheduler_queue_job(job, session);

		if (!_mali_osk_list_empty(&group_list_idle) || VIRTUAL_GROUP_IDLE == virtual_group_state)
		{
			mali_pp_scheduler_schedule();
		}
	}

	return _MALI_OSK_ERR_OK;
}

_mali_osk_errcode_t _mali_ukk_get_pp_number_of_cores(_mali_uk_get_pp_number_of_cores_s *args)
{
	MALI_DEBUG_ASSERT_POINTER(args);
	MALI_DEBUG_ASSERT_POINTER(args->ctx);
	args->number_of_total_cores = num_cores;
	args->number_of_enabled_cores = enabled_cores;
	return _MALI_OSK_ERR_OK;
}

u32 mali_pp_scheduler_get_num_cores_total(void)
{
	return num_cores;
}

u32 mali_pp_scheduler_get_num_cores_enabled(void)
{
	return enabled_cores;
}

_mali_osk_errcode_t _mali_ukk_get_pp_core_version(_mali_uk_get_pp_core_version_s *args)
{
	MALI_DEBUG_ASSERT_POINTER(args);
	MALI_DEBUG_ASSERT_POINTER(args->ctx);
	args->version = pp_version;
	return _MALI_OSK_ERR_OK;
}

void _mali_ukk_pp_job_disable_wb(_mali_uk_pp_disable_wb_s *args)
{
	struct mali_session_data *session;
	struct mali_pp_job *job;
	struct mali_pp_job *tmp;

	MALI_DEBUG_ASSERT_POINTER(args);
	MALI_DEBUG_ASSERT_POINTER(args->ctx);

	session = (struct mali_session_data*)args->ctx;

	/* Check queue for jobs that match */
	mali_pp_scheduler_lock();
	_MALI_OSK_LIST_FOREACHENTRY(job, tmp, &job_queue, struct mali_pp_job, list)
	{
		if (mali_pp_job_get_session(job) == session &&
		    mali_pp_job_get_frame_builder_id(job) == (u32)args->fb_id &&
		    mali_pp_job_get_flush_id(job) == (u32)args->flush_id)
		{
			if (args->wbx & _MALI_UK_PP_JOB_WB0)
			{
				mali_pp_job_disable_wb0(job);
			}
			if (args->wbx & _MALI_UK_PP_JOB_WB1)
			{
				mali_pp_job_disable_wb1(job);
			}
			if (args->wbx & _MALI_UK_PP_JOB_WB2)
			{
				mali_pp_job_disable_wb2(job);
			}
			break;
		}
	}
	mali_pp_scheduler_unlock();
}

void mali_pp_scheduler_abort_session(struct mali_session_data *session)
{
	struct mali_pp_job *job, *tmp_job;
	struct mali_group *group, *tmp_group;
	struct mali_group *groups[MALI_MAX_NUMBER_OF_GROUPS];
	s32 i = 0;
#if defined(MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE)
	_MALI_OSK_LIST_HEAD_STATIC_INIT(deferred_deletion_list);
#endif

	mali_pp_scheduler_lock();
	MALI_DEBUG_PRINT(3, ("Mali PP scheduler: Aborting all jobs from session 0x%08x\n", session));

	_MALI_OSK_LIST_FOREACHENTRY(job, tmp_job, &session->job_list, struct mali_pp_job, session_list)
	{
		/* Remove job from queue (if it's not queued, list_del has no effect) */
		_mali_osk_list_delinit(&job->list);

		if (mali_pp_job_is_virtual(job))
		{
			MALI_DEBUG_ASSERT(1 == mali_pp_job_get_sub_job_count(job));
			if (0 == mali_pp_job_get_first_unstarted_sub_job(job))
			{
				--virtual_job_queue_depth;
			}
		}
		else
		{
			job_queue_depth -= mali_pp_job_get_sub_job_count(job) - mali_pp_job_get_first_unstarted_sub_job(job);
		}

		/* Mark all unstarted jobs as failed */
		mali_pp_job_mark_unstarted_failed(job);

		if (mali_pp_job_is_complete(job))
		{
			_mali_osk_list_del(&job->session_list);

			/* It is safe to delete the job, since it won't land in job_done() */
			MALI_DEBUG_PRINT(4, ("Mali PP scheduler: Aborted PP job 0x%08x\n", job));

#if defined(MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE)
			MALI_DEBUG_ASSERT(_mali_osk_list_empty(&job->list));
			_mali_osk_list_addtail(&job->list, &deferred_deletion_list);
#else
			mali_pp_job_delete(job);
#endif

			mali_pp_scheduler_job_completed();
		}
		else
		{
			MALI_DEBUG_PRINT(4, ("Mali PP scheduler: Keeping partially started PP job 0x%08x in session\n", job));
		}
	}

	_MALI_OSK_LIST_FOREACHENTRY(group, tmp_group, &group_list_working, struct mali_group, pp_scheduler_list)
	{
		groups[i++] = group;
	}

	_MALI_OSK_LIST_FOREACHENTRY(group, tmp_group, &group_list_idle, struct mali_group, pp_scheduler_list)
	{
		groups[i++] = group;
	}

	mali_pp_scheduler_unlock();

#if defined(MALI_PP_SCHEDULER_USE_DEFERRED_JOB_DELETE)
	_MALI_OSK_LIST_FOREACHENTRY(job, tmp_job, &deferred_deletion_list, struct mali_pp_job, list)
	{
		mali_pp_job_delete(job);
	}
#endif

	/* Abort running jobs from this session */
	while (i > 0)
	{
		mali_group_abort_session(groups[--i], session);
	}

	if (mali_pp_scheduler_has_virtual_group())
	{
		mali_group_abort_session(virtual_group, session);
	}
}

static mali_bool mali_pp_scheduler_is_suspended(void)
{
	mali_bool ret;

	mali_pp_scheduler_lock();

	ret = pause_count > 0
	      && _mali_osk_list_empty(&group_list_working)
	      && VIRTUAL_GROUP_WORKING != virtual_group_state;

	mali_pp_scheduler_unlock();

	return ret;
}

int mali_pp_scheduler_get_queue_depth(void)
{
	return job_queue_depth;
}

#if MALI_STATE_TRACKING
u32 mali_pp_scheduler_dump_state(char *buf, u32 size)
{
	int n = 0;
	struct mali_group *group;
	struct mali_group *temp;

	n += _mali_osk_snprintf(buf + n, size - n, "PP:\n");
	n += _mali_osk_snprintf(buf + n, size - n, "\tQueue is %s\n", _mali_osk_list_empty(&job_queue) ? "empty" : "not empty");
	n += _mali_osk_snprintf(buf + n, size - n, "\n");

	_MALI_OSK_LIST_FOREACHENTRY(group, temp, &group_list_working, struct mali_group, pp_scheduler_list)
	{
		n += mali_group_dump_state(group, buf + n, size - n);
	}

	_MALI_OSK_LIST_FOREACHENTRY(group, temp, &group_list_idle, struct mali_group, pp_scheduler_list)
	{
		n += mali_group_dump_state(group, buf + n, size - n);
	}

	_MALI_OSK_LIST_FOREACHENTRY(group, temp, &group_list_disabled, struct mali_group, pp_scheduler_list)
	{
		n += mali_group_dump_state(group, buf + n, size - n);
	}

	if (mali_pp_scheduler_has_virtual_group())
	{
		n += mali_group_dump_state(virtual_group, buf + n, size -n);
	}

	n += _mali_osk_snprintf(buf + n, size - n, "\n");
	return n;
}
#endif

/* This function is intended for power on reset of all cores.
 * No locking is done for the list iteration, which can only be safe if the
 * scheduler is paused and all cores idle. That is always the case on init and
 * power on. */
void mali_pp_scheduler_reset_all_groups(void)
{
	struct mali_group *group, *temp;
	struct mali_group *groups[MALI_MAX_NUMBER_OF_GROUPS];
	s32 i = 0;

	if (mali_pp_scheduler_has_virtual_group())
	{
		mali_group_lock(virtual_group);
		mali_group_reset(virtual_group);
		mali_group_unlock(virtual_group);
	}

	MALI_DEBUG_ASSERT(_mali_osk_list_empty(&group_list_working));
	MALI_DEBUG_ASSERT(VIRTUAL_GROUP_WORKING != virtual_group_state);
	mali_pp_scheduler_lock();
	_MALI_OSK_LIST_FOREACHENTRY(group, temp, &group_list_idle, struct mali_group, pp_scheduler_list)
	{
		groups[i++] = group;
	}
	mali_pp_scheduler_unlock();

	while (i > 0)
	{
		group = groups[--i];

		mali_group_lock(group);
		mali_group_reset(group);
		mali_group_unlock(group);
	}
}

void mali_pp_scheduler_zap_all_active(struct mali_session_data *session)
{
	struct mali_group *group, *temp;
	struct mali_group *groups[MALI_MAX_NUMBER_OF_GROUPS];
	s32 i = 0;

	if (mali_pp_scheduler_has_virtual_group())
	{
		mali_group_zap_session(virtual_group, session);
	}

	mali_pp_scheduler_lock();
	_MALI_OSK_LIST_FOREACHENTRY(group, temp, &group_list_working, struct mali_group, pp_scheduler_list)
	{
		groups[i++] = group;
	}
	mali_pp_scheduler_unlock();

	while (i > 0)
	{
		mali_group_zap_session(groups[--i], session);
	}
}

/* A pm reference must be taken with _mali_osk_pm_dev_ref_add_no_power_on
 * before calling this function to avoid Mali powering down as HW is accessed.
 */
static void mali_pp_scheduler_enable_group_internal(struct mali_group *group)
{
	MALI_DEBUG_ASSERT_POINTER(group);

	mali_group_lock(group);

	if (MALI_GROUP_STATE_DISABLED != group->state)
	{
		mali_group_unlock(group);
		MALI_DEBUG_PRINT(4, ("Mali PP scheduler: PP group %p already enabled\n", group));
		return;
	}

	MALI_DEBUG_PRINT(3, ("Mali PP scheduler: Enabling PP group %p\n", group));

	mali_pp_scheduler_lock();

	MALI_DEBUG_ASSERT(MALI_GROUP_STATE_DISABLED == group->state);
	++enabled_cores;

	if (mali_pp_scheduler_has_virtual_group())
	{
		mali_bool update_hw;

		/* Add group to virtual group */
		_mali_osk_list_delinit(&(group->pp_scheduler_list));
		group->state = MALI_GROUP_STATE_JOINING_VIRTUAL;

		mali_pp_scheduler_unlock();
		mali_group_unlock(group);

		mali_group_lock(virtual_group);

		update_hw = mali_pm_is_power_on();

		mali_pm_domain_ref_get(group->pm_domain);
		MALI_DEBUG_ASSERT(NULL == group->pm_domain ||
		                  MALI_PM_DOMAIN_ON == mali_pm_domain_state_get(group->pm_domain));

		if (update_hw)
		{
			mali_group_lock(group);
			mali_group_power_on_group(group);
			mali_group_reset(group);
			mali_group_unlock(group);
		}

		mali_pp_scheduler_enable_empty_virtual();
		mali_group_add_group(virtual_group, group, update_hw);
		MALI_DEBUG_PRINT(4, ("Done enabling group %p. Added to virtual group.\n", group));

		mali_group_unlock(virtual_group);
	}
	else
	{
		mali_pm_domain_ref_get(group->pm_domain);
		MALI_DEBUG_ASSERT(NULL == group->pm_domain ||
		                  MALI_PM_DOMAIN_ON == mali_pm_domain_state_get(group->pm_domain));

		/* Put group on idle list */
		if (mali_pm_is_power_on())
		{
			mali_group_power_on_group(group);
			mali_group_reset(group);
		}

		_mali_osk_list_move(&(group->pp_scheduler_list), &group_list_idle);
		group->state = MALI_GROUP_STATE_IDLE;

		MALI_DEBUG_PRINT(4, ("Done enabling group %p. Now on idle list.\n", group));
		mali_pp_scheduler_unlock();
		mali_group_unlock(group);
	}
}

void mali_pp_scheduler_enable_group(struct mali_group *group)
{
	MALI_DEBUG_ASSERT_POINTER(group);

	_mali_osk_pm_dev_ref_add_no_power_on();

	mali_pp_scheduler_enable_group_internal(group);

	_mali_osk_pm_dev_ref_dec_no_power_on();

	/* Pick up any jobs that might have been queued if all PP groups were disabled. */
	mali_pp_scheduler_schedule();
}

static void mali_pp_scheduler_disable_group_internal(struct mali_group *group)
{
	if (mali_pp_scheduler_has_virtual_group())
	{
		mali_group_lock(virtual_group);

		MALI_DEBUG_ASSERT(VIRTUAL_GROUP_WORKING != virtual_group_state);
		if (MALI_GROUP_STATE_JOINING_VIRTUAL == group->state)
		{
			/* The group was in the process of being added to the virtual group.  We
			 * only need to change the state to reverse this. */
			group->state = MALI_GROUP_STATE_LEAVING_VIRTUAL;
		}
		else if (MALI_GROUP_STATE_IN_VIRTUAL == group->state)
		{
			/* Remove group from virtual group.  The state of the group will be
			 * LEAVING_VIRTUAL and the group will not be on any scheduler list. */
			mali_group_remove_group(virtual_group, group);

			mali_pp_scheduler_disable_empty_virtual();
		}

		mali_group_unlock(virtual_group);
	}

	mali_group_lock(group);
	mali_pp_scheduler_lock();

	MALI_DEBUG_ASSERT(   MALI_GROUP_STATE_IDLE            == group->state
	                  || MALI_GROUP_STATE_LEAVING_VIRTUAL == group->state
	                  || MALI_GROUP_STATE_DISABLED        == group->state);

	if (MALI_GROUP_STATE_DISABLED == group->state)
	{
		MALI_DEBUG_PRINT(4, ("Mali PP scheduler: PP group %p already disabled\n", group));
	}
	else
	{
		MALI_DEBUG_PRINT(3, ("Mali PP scheduler: Disabling PP group %p\n", group));

		--enabled_cores;
		_mali_osk_list_move(&(group->pp_scheduler_list), &group_list_disabled);
		group->state = MALI_GROUP_STATE_DISABLED;

		mali_group_power_off_group(group);
		mali_pm_domain_ref_put(group->pm_domain);
	}

	mali_pp_scheduler_unlock();
	mali_group_unlock(group);
}

void mali_pp_scheduler_disable_group(struct mali_group *group)
{
	MALI_DEBUG_ASSERT_POINTER(group);

	mali_pp_scheduler_suspend();
	_mali_osk_pm_dev_ref_add_no_power_on();

	mali_pp_scheduler_disable_group_internal(group);

	_mali_osk_pm_dev_ref_dec_no_power_on();
	mali_pp_scheduler_resume();
}


static void mali_pp_scheduler_notify_core_change(u32 num_cores)
{
	if (!mali_is_mali450())
	{
		/* Notify all user space sessions about the change, so number of master tile lists can be adapter */
		struct mali_session_data *session, *tmp;

		mali_session_lock();
		MALI_SESSION_FOREACH(session, tmp, link)
		{
			_mali_osk_notification_t *notobj = _mali_osk_notification_create(_MALI_NOTIFICATION_PP_NUM_CORE_CHANGE, sizeof(_mali_uk_pp_num_cores_changed_s));
			if (NULL != notobj)
			{
				_mali_uk_pp_num_cores_changed_s *data = notobj->result_buffer;
				data->number_of_enabled_cores = num_cores;
				mali_session_send_notification(session, notobj);
			}
			else
			{
				MALI_PRINT_ERROR(("Failed to notify user space session about num PP core change\n"));
			}
		}
		mali_session_unlock();
	}
}

static void mali_pp_scheduler_set_perf_level_mali400(u32 target_core_nr)
{
	struct mali_group *group;
	MALI_DEBUG_ASSERT(mali_is_mali400());

	if (target_core_nr > enabled_cores)
	{
		MALI_DEBUG_PRINT(2, ("Requesting %d cores: enabling %d cores\n", target_core_nr, target_core_nr - enabled_cores));

		_mali_osk_pm_dev_ref_add_no_power_on();
		_mali_osk_pm_dev_barrier();

		while (target_core_nr > enabled_cores)
		{
			mali_pp_scheduler_lock();

			MALI_DEBUG_ASSERT(!_mali_osk_list_empty(&group_list_disabled));

			group = _MALI_OSK_LIST_ENTRY(group_list_disabled.next, struct mali_group, pp_scheduler_list);

			MALI_DEBUG_ASSERT_POINTER(group);
			MALI_DEBUG_ASSERT(MALI_GROUP_STATE_DISABLED == group->state);

			mali_pp_scheduler_unlock();

			mali_pp_scheduler_enable_group_internal(group);
		}

		_mali_osk_pm_dev_ref_dec_no_power_on();

		mali_pp_scheduler_schedule();
	}
	else if (target_core_nr < enabled_cores)
	{
		MALI_DEBUG_PRINT(2, ("Requesting %d cores: disabling %d cores\n", target_core_nr, enabled_cores - target_core_nr));

		mali_pp_scheduler_suspend();

		MALI_DEBUG_ASSERT(_mali_osk_list_empty(&group_list_working));

		while (target_core_nr < enabled_cores)
		{
			mali_pp_scheduler_lock();

			MALI_DEBUG_ASSERT(!_mali_osk_list_empty(&group_list_idle));
			MALI_DEBUG_ASSERT(_mali_osk_list_empty(&group_list_working));

			group = _MALI_OSK_LIST_ENTRY(group_list_idle.next, struct mali_group, pp_scheduler_list);

			MALI_DEBUG_ASSERT_POINTER(group);
			MALI_DEBUG_ASSERT(   MALI_GROUP_STATE_IDLE            == group->state
					  || MALI_GROUP_STATE_LEAVING_VIRTUAL == group->state
					  || MALI_GROUP_STATE_DISABLED        == group->state);

			mali_pp_scheduler_unlock();

			mali_pp_scheduler_disable_group_internal(group);
		}

		mali_pp_scheduler_resume();
	}

	mali_pp_scheduler_notify_core_change(target_core_nr);
}

static void mali_pp_scheduler_set_perf_level_mali450(u32 target_core_nr)
{
	struct mali_group *group;
	MALI_DEBUG_ASSERT(mali_is_mali450());

	if (target_core_nr > enabled_cores)
	{
		/* Enable some cores */
		struct mali_pm_domain *domain;

		MALI_DEBUG_PRINT(2, ("Requesting %d cores: enabling %d cores\n", target_core_nr, target_core_nr - enabled_cores));

		_mali_osk_pm_dev_ref_add_no_power_on();
		_mali_osk_pm_dev_barrier();

		domain = mali_pm_domain_get(MALI_PMU_M450_DOM2);

		MALI_PM_DOMAIN_FOR_EACH_GROUP(group, domain)
		{
			mali_pp_scheduler_enable_group_internal(group);
			if (target_core_nr == enabled_cores) break;
		}

		if (target_core_nr > enabled_cores)
		{
			domain = mali_pm_domain_get(MALI_PMU_M450_DOM3);
			MALI_PM_DOMAIN_FOR_EACH_GROUP(group, domain)
			{
				mali_pp_scheduler_enable_group_internal(group);
				if (target_core_nr == enabled_cores) break;
			}
		}

		MALI_DEBUG_ASSERT(target_core_nr == enabled_cores);

		_mali_osk_pm_dev_ref_dec_no_power_on();

		mali_pp_scheduler_schedule();
	}
	else if (target_core_nr < enabled_cores)
	{
		/* Disable some cores */
		struct mali_pm_domain *domain;

		MALI_DEBUG_PRINT(2, ("Requesting %d cores: disabling %d cores\n", target_core_nr, enabled_cores - target_core_nr));

		mali_pp_scheduler_suspend();

		domain = mali_pm_domain_get(MALI_PMU_M450_DOM3);
		if (NULL != domain)
		{
			MALI_PM_DOMAIN_FOR_EACH_GROUP(group, domain)
			{
				mali_pp_scheduler_disable_group_internal(group);
				if (target_core_nr == enabled_cores) break;
			}
		}

		if (target_core_nr < enabled_cores)
		{
			domain = mali_pm_domain_get(MALI_PMU_M450_DOM2);
			MALI_DEBUG_ASSERT_POINTER(domain);
			MALI_PM_DOMAIN_FOR_EACH_GROUP(group, domain)
			{
				mali_pp_scheduler_disable_group_internal(group);
				if (target_core_nr == enabled_cores) break;
			}
		}

		MALI_DEBUG_ASSERT(target_core_nr == enabled_cores);

		mali_pp_scheduler_resume();
	}
}

int mali_pp_scheduler_set_perf_level(unsigned int cores)
{
	if (cores == enabled_cores) return 0;
	if (cores > num_cores) return -EINVAL;
	if (0 == cores) return -EINVAL;

	if (!mali_pp_scheduler_has_virtual_group())
	{
		/* Mali-400 */
		mali_pp_scheduler_set_perf_level_mali400(cores);
	}
	else
	{
		/* Mali-450 */
		mali_pp_scheduler_set_perf_level_mali450(cores);
	}

	return 0;
}

static void mali_pp_scheduler_job_queued(void)
{
	/* We hold a PM reference for every job we hold queued (and running) */
	_mali_osk_pm_dev_ref_add();

	if (mali_utilization_enabled())
	{
		/*
		 * We cheat a little bit by counting the PP as busy from the time a PP job is queued.
		 * This will be fine because we only loose the tiny idle gap between jobs, but
		 * we will instead get less utilization work to do (less locks taken)
		 */
		mali_utilization_pp_start();
	}
}

static void mali_pp_scheduler_job_completed(void)
{
	/* Release the PM reference we got in the mali_pp_scheduler_job_queued() function */
	_mali_osk_pm_dev_ref_dec();

	if (mali_utilization_enabled())
	{
		mali_utilization_pp_end();
	}
}