aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/img-rogue/1.17/physmem.c
blob: 34c174d9daed7d67eefaac85a9f47097fb351b67 (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
/*************************************************************************/ /*!
@File           physmem.c
@Title          Physmem
@Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
@Description    Common entry point for creation of RAM backed PMR's
@License        Dual MIT/GPLv2

The contents of this file are subject to the MIT license as set out below.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 ("GPL") in which case the provisions
of GPL are applicable instead of those above.

If you wish to allow use of your version of this file only under the terms of
GPL, and not to allow others to use your version of this file under the terms
of the MIT license, indicate your decision by deleting the provisions above
and replace them with the notice and other provisions required by GPL as set
out in the file called "GPL-COPYING" included in this distribution. If you do
not delete the provisions above, a recipient may use your version of this file
under the terms of either the MIT license or GPL.

This License is also included in this distribution in the file called
"MIT-COPYING".

EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ /***************************************************************************/
#include "img_types.h"
#include "img_defs.h"
#include "pvrsrv_error.h"
#include "pvrsrv_memallocflags.h"
#include "device.h"
#include "physmem.h"
#include "pvrsrv.h"
#include "osfunc.h"
#include "pdump_physmem.h"
#include "pdump_km.h"
#include "rgx_heaps.h"
#include "pvr_ricommon.h"

#include "physmem_lma.h"
#include "physmem_osmem.h"

#if defined(DEBUG)
static IMG_UINT32 gPMRAllocFail;

#if defined(__linux__)
#include <linux/moduleparam.h>

module_param(gPMRAllocFail, uint, 0644);
MODULE_PARM_DESC(gPMRAllocFail, "When number of PMR allocs reaches "
				 "this value, it will fail (default value is 0 which "
				 "means that alloc function will behave normally).");
#endif /* defined(__linux__) */
#endif /* defined(DEBUG) */

#if defined(PVRSRV_ENABLE_PROCESS_STATS)
#include "process_stats.h"
#include "proc_stats.h"
#endif

PVRSRV_ERROR DevPhysMemAlloc(PVRSRV_DEVICE_NODE	*psDevNode,
                             IMG_UINT32 ui32MemSize,
                             IMG_UINT32 ui32Log2Align,
                             const IMG_UINT8 u8Value,
                             IMG_BOOL bInitPage,
#if defined(PDUMP)
                             const IMG_CHAR *pszDevSpace,
                             const IMG_CHAR *pszSymbolicAddress,
                             IMG_HANDLE *phHandlePtr,
#endif
                             IMG_HANDLE hMemHandle,
                             IMG_DEV_PHYADDR *psDevPhysAddr)
{
	void *pvCpuVAddr;
	PVRSRV_ERROR eError;
#if defined(PDUMP)
	IMG_CHAR szFilenameOut[PDUMP_PARAM_MAX_FILE_NAME];
	PDUMP_FILEOFFSET_T uiOffsetOut;
	IMG_UINT32 ui32PageSize;
	IMG_UINT32 ui32PDumpMemSize = ui32MemSize;
	PVRSRV_ERROR ePDumpError;
#endif
	PG_HANDLE *psMemHandle;
	IMG_UINT64 uiMask;
	IMG_DEV_PHYADDR sDevPhysAddr_int;
	IMG_PID uiPid = 0;

	psMemHandle = hMemHandle;

#if defined(PVRSRV_ENABLE_PROCESS_STATS)
	uiPid = psDevNode->eDevState == PVRSRV_DEVICE_STATE_INIT ?
	        PVR_SYS_ALLOC_PID : OSGetCurrentClientProcessIDKM();
#endif

	/* Allocate the pages */
	eError = PhysHeapPagesAlloc(psDevNode->psMMUPhysHeap,
	                            TRUNCATE_64BITS_TO_SIZE_T(ui32MemSize),
	                            psMemHandle,
	                            &sDevPhysAddr_int,
	                            uiPid);
	PVR_LOG_RETURN_IF_ERROR(eError, "pfnDevPxAlloc:1");

	/* Check to see if the page allocator returned pages with our desired
	 * alignment, which is not unlikely
	 */
	uiMask = (1 << ui32Log2Align) - 1;
	if (ui32Log2Align && (sDevPhysAddr_int.uiAddr & uiMask))
	{
		/* use over allocation instead */
		PhysHeapPagesFree(psDevNode->psMMUPhysHeap, psMemHandle);

		ui32MemSize += (IMG_UINT32) uiMask;
		eError = PhysHeapPagesAlloc(psDevNode->psMMUPhysHeap,
		                            TRUNCATE_64BITS_TO_SIZE_T(ui32MemSize),
		                            psMemHandle,
		                            &sDevPhysAddr_int,
		                            uiPid);
		PVR_LOG_RETURN_IF_ERROR(eError, "pfnDevPxAlloc:2");

		sDevPhysAddr_int.uiAddr += uiMask;
		sDevPhysAddr_int.uiAddr &= ~uiMask;
	}
	*psDevPhysAddr = sDevPhysAddr_int;

#if defined(PDUMP)
	ui32PageSize = ui32Log2Align? (1 << ui32Log2Align) : OSGetPageSize();
	eError = PDumpMalloc(psDevNode,
	                     pszDevSpace,
	                     pszSymbolicAddress,
	                     ui32PDumpMemSize,
	                     ui32PageSize,
	                     IMG_FALSE,
	                     0,
	                     phHandlePtr,
	                     PDUMP_NONE);
	if (PVRSRV_OK != eError)
	{
		PDUMPCOMMENT(psDevNode, "Allocating pages failed");
		*phHandlePtr = NULL;
	}
	ePDumpError = eError;
#endif

	if (bInitPage)
	{
		/*Map the page to the CPU VA space */
		eError = PhysHeapPagesMap(psDevNode->psMMUPhysHeap,
		                          psMemHandle,
		                          ui32MemSize,
		                          &sDevPhysAddr_int,
		                          &pvCpuVAddr);
		if (PVRSRV_OK != eError)
		{
			PVR_LOG_ERROR(eError, "DevPxMap");
			PhysHeapPagesFree(psDevNode->psMMUPhysHeap, psMemHandle);
			return eError;
		}

		/*Fill the memory with given content */
		OSDeviceMemSet(pvCpuVAddr, u8Value, ui32MemSize);

		/*Map the page to the CPU VA space */
		eError = PhysHeapPagesClean(psDevNode->psMMUPhysHeap,
		                            psMemHandle,
		                            0,
		                            ui32MemSize);
		if (PVRSRV_OK != eError)
		{
			PVR_LOG_ERROR(eError, "DevPxClean");
			PhysHeapPagesUnMap(psDevNode->psMMUPhysHeap, psMemHandle, pvCpuVAddr);
			PhysHeapPagesFree(psDevNode->psMMUPhysHeap, psMemHandle);
			return eError;
		}

#if defined(PDUMP)
		if (ePDumpError != PVRSRV_ERROR_PDUMP_CAPTURE_BOUND_TO_ANOTHER_DEVICE)
		{
			/* PDumping of the page contents can be done in two ways
			 * 1. Store the single byte init value to the .prm file
			 *    and load the same value to the entire dummy page buffer
			 *    This method requires lot of LDB's inserted into the out2.txt
			 *
			 * 2. Store the entire contents of the buffer to the .prm file
			 *    and load them back.
			 *    This only needs a single LDB instruction in the .prm file
			 *    and chosen this method
			 *    size of .prm file might go up but that's not huge at least
			 *    for this allocation
			 */
			/* Write the buffer contents to the prm file */
			eError = PDumpWriteParameterBlob(psDevNode,
											 pvCpuVAddr,
											 ui32PDumpMemSize,
											 PDUMP_FLAGS_CONTINUOUS,
											 szFilenameOut,
											 sizeof(szFilenameOut),
											 &uiOffsetOut);
			if (PVRSRV_OK == eError)
			{
				/* Load the buffer back to the allocated memory when playing the pdump */
				eError = PDumpPMRLDB(psDevNode,
									 pszDevSpace,
									 pszSymbolicAddress,
									 0,
									 ui32PDumpMemSize,
									 szFilenameOut,
									 uiOffsetOut,
									 PDUMP_FLAGS_CONTINUOUS);
				if (PVRSRV_OK != eError)
				{
					PDUMP_ERROR(psDevNode, eError, "Failed to write LDB statement to script file");
					PVR_LOG_ERROR(eError, "PDumpPMRLDB");
				}
			}
			else if (eError != PVRSRV_ERROR_PDUMP_NOT_ALLOWED)
			{
				PDUMP_ERROR(psDevNode, eError, "Failed to write device allocation to parameter file");
				PVR_LOG_ERROR(eError, "PDumpWriteParameterBlob");
			}
			else
			{
				/* Else write to parameter file prevented under the flags and
				 * current state of the driver so skip write to script and error IF.
				 * This is expected e.g., if not in the capture range.
				 */
				eError = PVRSRV_OK;
			}
		}
#endif

		/* Unmap the page */
		PhysHeapPagesUnMap(psDevNode->psMMUPhysHeap,
		                   psMemHandle,
		                   pvCpuVAddr);
	}

	return PVRSRV_OK;
}

void DevPhysMemFree(PVRSRV_DEVICE_NODE *psDevNode,
#if defined(PDUMP)
							IMG_HANDLE hPDUMPMemHandle,
#endif
							IMG_HANDLE	hMemHandle)
{
	PG_HANDLE *psMemHandle;

	psMemHandle = hMemHandle;
	PhysHeapPagesFree(psDevNode->psMMUPhysHeap, psMemHandle);
#if defined(PDUMP)
	if (NULL != hPDUMPMemHandle)
	{
		PDumpFree(psDevNode, hPDUMPMemHandle);
	}
#endif

}


/* Checks the input parameters and adjusts them if possible and necessary */
static inline PVRSRV_ERROR _ValidateParams(IMG_UINT32 ui32NumPhysChunks,
                                           IMG_UINT32 ui32NumVirtChunks,
                                           PVRSRV_MEMALLOCFLAGS_T uiFlags,
                                           IMG_UINT32 *puiLog2AllocPageSize,
                                           IMG_DEVMEM_SIZE_T *puiSize,
                                           PMR_SIZE_T *puiChunkSize)
{
	IMG_UINT32 uiLog2AllocPageSize = *puiLog2AllocPageSize;
	IMG_DEVMEM_SIZE_T uiSize = *puiSize;
	PMR_SIZE_T uiChunkSize = *puiChunkSize;
	/* Sparse if we have different number of virtual and physical chunks plus
	 * in general all allocations with more than one virtual chunk */
	IMG_BOOL bIsSparse = (ui32NumVirtChunks != ui32NumPhysChunks ||
			ui32NumVirtChunks > 1) ? IMG_TRUE : IMG_FALSE;

	if (ui32NumPhysChunks == 0 && ui32NumVirtChunks == 0)
	{
		PVR_DPF((PVR_DBG_ERROR,
				"%s: Number of physical chunks and number of virtual chunks "
				"cannot be both 0",
				__func__));

		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	/* Protect against ridiculous page sizes */
	if (uiLog2AllocPageSize > RGX_HEAP_2MB_PAGE_SHIFT)
	{
		PVR_DPF((PVR_DBG_ERROR, "Page size is too big: 2^%u.", uiLog2AllocPageSize));
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	/* Range check of the alloc size */
	if (uiSize >= 0x1000000000ULL)
	{
		PVR_DPF((PVR_DBG_ERROR,
				 "%s: Cancelling allocation request of over 64 GB. "
				 "This is likely a bug."
				, __func__));
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	/* Fail if requesting coherency on one side but uncached on the other */
	if (PVRSRV_CHECK_CPU_CACHE_COHERENT(uiFlags) &&
	    (PVRSRV_CHECK_GPU_UNCACHED(uiFlags) || PVRSRV_CHECK_GPU_WRITE_COMBINE(uiFlags)))
	{
		PVR_DPF((PVR_DBG_ERROR, "Request for CPU coherency but specifying GPU uncached "
				"Please use GPU cached flags for coherency."));
		return PVRSRV_ERROR_UNSUPPORTED_CACHE_MODE;
	}

	if (PVRSRV_CHECK_GPU_CACHE_COHERENT(uiFlags) &&
	    (PVRSRV_CHECK_CPU_UNCACHED(uiFlags) || PVRSRV_CHECK_CPU_WRITE_COMBINE(uiFlags)))
	{
		PVR_DPF((PVR_DBG_ERROR, "Request for GPU coherency but specifying CPU uncached "
				"Please use CPU cached flags for coherency."));
		return PVRSRV_ERROR_UNSUPPORTED_CACHE_MODE;
	}

	if (PVRSRV_CHECK_ZERO_ON_ALLOC(uiFlags) && PVRSRV_CHECK_POISON_ON_ALLOC(uiFlags))
	{
		PVR_DPF((PVR_DBG_ERROR,
				"%s: Zero on Alloc and Poison on Alloc are mutually exclusive.",
				__func__));
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	if (bIsSparse)
	{
		/* For sparse we need correct parameters like a suitable page size....  */
		if (OSGetPageShift() > uiLog2AllocPageSize)
		{
			PVR_DPF((PVR_DBG_ERROR,
					"%s: Invalid log2-contiguity for sparse allocation. "
					"Requested %u, required minimum %zd",
					__func__,
					uiLog2AllocPageSize,
					OSGetPageShift() ));

			return PVRSRV_ERROR_INVALID_PARAMS;
		}

		/* ... chunk size must be a equal to page size ... */
		if (uiChunkSize != (1 << uiLog2AllocPageSize))
		{
			PVR_DPF((PVR_DBG_ERROR,
					 "%s: Invalid chunk size for sparse allocation. Requested "
					 "%#" IMG_UINT64_FMTSPECx ", must be same as page size %#x.",
					__func__, uiChunkSize, 1 << uiLog2AllocPageSize));

			return PVRSRV_ERROR_PMR_NOT_PAGE_MULTIPLE;
		}

		if (ui32NumVirtChunks * uiChunkSize != uiSize)
		{
			PVR_DPF((PVR_DBG_ERROR,
					 "%s: Total alloc size (%#" IMG_UINT64_FMTSPECx ") "
					 "is not equal to virtual chunks * chunk size "
					 "(%#" IMG_UINT64_FMTSPECx ")",
					__func__, uiSize, ui32NumVirtChunks * uiChunkSize));

			return PVRSRV_ERROR_PMR_NOT_PAGE_MULTIPLE;
		}

		if (ui32NumPhysChunks > ui32NumVirtChunks)
		{
			PVR_DPF((PVR_DBG_ERROR,
					"%s: Number of physical chunks (%u) must not be greater "
					"than number of virtual chunks (%u)",
					__func__,
					ui32NumPhysChunks,
					ui32NumVirtChunks));

			return PVRSRV_ERROR_INVALID_PARAMS;
		}
	}
	else
	{
		/*
		 * Silently round up alignment/pagesize if request was less that PAGE_SHIFT
		 * because it would never be harmful for memory to be _more_ contiguous that
		 * was desired.
		 */
		uiLog2AllocPageSize = OSGetPageShift() > uiLog2AllocPageSize ?
				OSGetPageShift() : uiLog2AllocPageSize;

		/* Same for total size */
		uiSize = PVR_ALIGN(uiSize, (IMG_DEVMEM_SIZE_T)OSGetPageSize());
		*puiChunkSize = uiSize;
	}

	if ((uiSize & ((1ULL << uiLog2AllocPageSize) - 1)) != 0)
	{
		PVR_DPF((PVR_DBG_ERROR,
		        "%s: Total size (%#" IMG_UINT64_FMTSPECx ") "
		        "must be a multiple of the requested contiguity (%"
		        IMG_UINT64_FMTSPEC ")", __func__, uiSize,
		        (IMG_UINT64) (1ULL << uiLog2AllocPageSize)));
		return PVRSRV_ERROR_PMR_NOT_PAGE_MULTIPLE;
	}

	*puiLog2AllocPageSize = uiLog2AllocPageSize;
	*puiSize = uiSize;

	return PVRSRV_OK;
}

static PVRSRV_ERROR _DevPhysHeapFromFlags(PVRSRV_MEMALLOCFLAGS_T uiFlags,
										  PVRSRV_PHYS_HEAP *peDevPhysHeap)
{
	PVRSRV_PHYS_HEAP eHeap = PVRSRV_GET_PHYS_HEAP_HINT(uiFlags);

	switch (eHeap)
	{
		case PVRSRV_PHYS_HEAP_FW_PREMAP0:
		case PVRSRV_PHYS_HEAP_FW_PREMAP1:
		case PVRSRV_PHYS_HEAP_FW_PREMAP2:
		case PVRSRV_PHYS_HEAP_FW_PREMAP3:
		case PVRSRV_PHYS_HEAP_FW_PREMAP4:
		case PVRSRV_PHYS_HEAP_FW_PREMAP5:
		case PVRSRV_PHYS_HEAP_FW_PREMAP6:
		case PVRSRV_PHYS_HEAP_FW_PREMAP7:
		{
			/* keep heap (with check) */
			PVR_RETURN_IF_INVALID_PARAM(PVRSRV_VZ_MODE_IS(HOST));
			break;
		}
		case PVRSRV_PHYS_HEAP_LAST:
		{
			return PVRSRV_ERROR_INVALID_PARAMS;
		}
		default:
		{
			break;
		}
	}

	*peDevPhysHeap = eHeap;

	return PVRSRV_OK;
}

PVRSRV_ERROR
PhysmemNewRamBackedPMR_direct(CONNECTION_DATA *psConnection,
                       PVRSRV_DEVICE_NODE *psDevNode,
                       IMG_DEVMEM_SIZE_T uiSize,
                       PMR_SIZE_T uiChunkSize,
                       IMG_UINT32 ui32NumPhysChunks,
                       IMG_UINT32 ui32NumVirtChunks,
                       IMG_UINT32 *pui32MappingTable,
                       IMG_UINT32 uiLog2AllocPageSize,
                       PVRSRV_MEMALLOCFLAGS_T uiFlags,
                       IMG_UINT32 uiAnnotationLength,
                       const IMG_CHAR *pszAnnotation,
                       IMG_PID uiPid,
                       PMR **ppsPMRPtr,
                       IMG_UINT32 ui32PDumpFlags,
                       PVRSRV_MEMALLOCFLAGS_T *puiPMRFlags)
{
	PVRSRV_ERROR eError;
	PVRSRV_PHYS_HEAP ePhysHeapIdx;
	PVRSRV_MEMALLOCFLAGS_T uiPMRFlags = uiFlags;
	PFN_SYS_DEV_CHECK_MEM_ALLOC_SIZE pfnCheckMemAllocSize =
		psDevNode->psDevConfig->pfnCheckMemAllocSize;

	PVR_UNREFERENCED_PARAMETER(uiAnnotationLength);

	eError = _ValidateParams(ui32NumPhysChunks,
	                         ui32NumVirtChunks,
	                         uiFlags,
	                         &uiLog2AllocPageSize,
	                         &uiSize,
	                         &uiChunkSize);
	PVR_RETURN_IF_ERROR(eError);

	eError = _DevPhysHeapFromFlags(uiFlags, &ePhysHeapIdx);
	PVR_RETURN_IF_ERROR(eError);

	if (ePhysHeapIdx == PVRSRV_PHYS_HEAP_DEFAULT)
	{
		ePhysHeapIdx = psDevNode->psDevConfig->eDefaultHeap;
		PVRSRV_CHANGE_PHYS_HEAP_HINT(ePhysHeapIdx, uiPMRFlags);
	}

	if (ePhysHeapIdx == PVRSRV_PHYS_HEAP_GPU_LOCAL)
	{
		if ((uiFlags & PVRSRV_MEMALLOCFLAGS_CPU_MAPPABLE_MASK) == 0)
		{
			ePhysHeapIdx = PVRSRV_PHYS_HEAP_GPU_PRIVATE;
			PVRSRV_SET_PHYS_HEAP_HINT(GPU_PRIVATE, uiPMRFlags);
			PVR_DPF((PVR_DBG_VERBOSE, "%s: Consider explicit use of GPU_PRIVATE for PMR %s."
			        " Implicit conversion to GPU PRIVATE performed",
			        __func__, pszAnnotation));
		}
		else if (PVRSRV_CHECK_GPU_CACHE_COHERENT(uiFlags) &&
				 PVRSRVSystemSnoopingOfCPUCache(psDevNode->psDevConfig))
		{
			ePhysHeapIdx = PVRSRV_PHYS_HEAP_GPU_COHERENT;
			PVRSRV_SET_PHYS_HEAP_HINT(GPU_COHERENT, uiPMRFlags);
		}
	}
	else if (ePhysHeapIdx == PVRSRV_PHYS_HEAP_GPU_PRIVATE)
	{
		if (uiFlags & PVRSRV_MEMALLOCFLAGS_CPU_MAPPABLE_MASK)
		{
			PVR_DPF((PVR_DBG_ERROR, "%s: Invalid flags for PMR %s!"
			        " Client requested GPU_PRIVATE physical heap with CPU access flags.",
			        __func__, pszAnnotation));
			return PVRSRV_ERROR_INVALID_HEAP;
		}
	}

	if (NULL == psDevNode->apsPhysHeap[ePhysHeapIdx])
	{
		/* In case a heap hasn't been acquired for this type, return invalid heap error */
		PVR_DPF((PVR_DBG_ERROR, "%s: Requested allocation on device node (%p) from "
		        "an invalid heap (HeapIndex=%d)",
		        __func__, psDevNode, ePhysHeapIdx));
		return PVRSRV_ERROR_INVALID_HEAP;
	}

	/* Apply memory budgeting policy */
	if (pfnCheckMemAllocSize)
	{
		IMG_UINT64 uiMemSize = (IMG_UINT64)uiChunkSize * ui32NumPhysChunks;

		eError = pfnCheckMemAllocSize(psDevNode->psDevConfig->hSysData, uiMemSize);
		PVR_RETURN_IF_ERROR(eError);
	}

#if defined(DEBUG)
	if (gPMRAllocFail > 0)
	{
		static IMG_UINT32 ui32AllocCount = 1;

		if (ui32AllocCount < gPMRAllocFail)
		{
			ui32AllocCount++;
		}
		else
		{
			PVR_DPF((PVR_DBG_ERROR, "%s failed on %d allocation.",
			         __func__, ui32AllocCount));
			return PVRSRV_ERROR_OUT_OF_MEMORY;
		}
	}
#endif /* defined(DEBUG) */

	/* If the driver is in an 'init' state all of the allocated memory
	 * should be attributed to the driver (PID 1) rather than to the
	 * process those allocations are made under. Same applies to the memory
	 * allocated for the Firmware. */
	if (psDevNode->eDevState == PVRSRV_DEVICE_STATE_INIT ||
	    PVRSRV_CHECK_FW_MAIN(uiFlags))
	{
		uiPid = PVR_SYS_ALLOC_PID;
	}

	eError = PhysHeapCreatePMR(psDevNode->apsPhysHeap[ePhysHeapIdx],
							   psConnection,
							   uiSize,
							   uiChunkSize,
							   ui32NumPhysChunks,
							   ui32NumVirtChunks,
							   pui32MappingTable,
							   uiLog2AllocPageSize,
							   uiFlags,
							   pszAnnotation,
							   uiPid,
							   ppsPMRPtr,
							   ui32PDumpFlags);

	if (puiPMRFlags != NULL)
	{
		*puiPMRFlags = uiPMRFlags;
	}

#if defined(PVRSRV_ENABLE_PROCESS_STATS)
	if (eError != PVRSRV_OK)
	{
		PVRSRVStatsUpdateOOMStats(PVRSRV_PROCESS_STAT_TYPE_OOM_PHYSMEM_COUNT,
		                          OSGetCurrentClientProcessIDKM());
	}
#endif

	return eError;
}

PVRSRV_ERROR
PhysmemNewRamBackedPMR(CONNECTION_DATA *psConnection,
                       PVRSRV_DEVICE_NODE *psDevNode,
                       IMG_DEVMEM_SIZE_T uiSize,
                       PMR_SIZE_T uiChunkSize,
                       IMG_UINT32 ui32NumPhysChunks,
                       IMG_UINT32 ui32NumVirtChunks,
                       IMG_UINT32 *pui32MappingTable,
                       IMG_UINT32 uiLog2AllocPageSize,
                       PVRSRV_MEMALLOCFLAGS_T uiFlags,
                       IMG_UINT32 uiAnnotationLength,
                       const IMG_CHAR *pszAnnotation,
                       IMG_PID uiPid,
                       PMR **ppsPMRPtr,
                       IMG_UINT32 ui32PDumpFlags,
                       PVRSRV_MEMALLOCFLAGS_T *puiPMRFlags)
{
	PVRSRV_PHYS_HEAP ePhysHeap = PVRSRV_GET_PHYS_HEAP_HINT(uiFlags);

	PVR_LOG_RETURN_IF_INVALID_PARAM(uiAnnotationLength != 0, "uiAnnotationLength");
	PVR_LOG_RETURN_IF_INVALID_PARAM(pszAnnotation != NULL, "pszAnnotation");

	if (ePhysHeap == PVRSRV_PHYS_HEAP_DEFAULT)
	{
		ePhysHeap = psDevNode->psDevConfig->eDefaultHeap;
	}

	if (!PhysHeapUserModeAlloc(ePhysHeap))
	{
		PVR_DPF((PVR_DBG_ERROR, "%s: Invalid phys heap hint: %d.", __func__, ePhysHeap));
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	return PhysmemNewRamBackedPMR_direct(psConnection,
										 psDevNode,
										 uiSize,
										 uiChunkSize,
										 ui32NumPhysChunks,
										 ui32NumVirtChunks,
										 pui32MappingTable,
										 uiLog2AllocPageSize,
										 uiFlags,
										 uiAnnotationLength,
										 pszAnnotation,
										 uiPid,
										 ppsPMRPtr,
										 ui32PDumpFlags,
										 puiPMRFlags);
}

PVRSRV_ERROR
PhysmemNewRamBackedLockedPMR(CONNECTION_DATA *psConnection,
							PVRSRV_DEVICE_NODE *psDevNode,
							IMG_DEVMEM_SIZE_T uiSize,
							PMR_SIZE_T uiChunkSize,
							IMG_UINT32 ui32NumPhysChunks,
							IMG_UINT32 ui32NumVirtChunks,
							IMG_UINT32 *pui32MappingTable,
							IMG_UINT32 uiLog2PageSize,
							PVRSRV_MEMALLOCFLAGS_T uiFlags,
							IMG_UINT32 uiAnnotationLength,
							const IMG_CHAR *pszAnnotation,
							IMG_PID uiPid,
							PMR **ppsPMRPtr,
							IMG_UINT32 ui32PDumpFlags,
							PVRSRV_MEMALLOCFLAGS_T *puiPMRFlags)
{

	PVRSRV_ERROR eError;
	eError = PhysmemNewRamBackedPMR(psConnection,
									psDevNode,
									uiSize,
									uiChunkSize,
									ui32NumPhysChunks,
									ui32NumVirtChunks,
									pui32MappingTable,
									uiLog2PageSize,
									uiFlags,
									uiAnnotationLength,
									pszAnnotation,
									uiPid,
									ppsPMRPtr,
									ui32PDumpFlags,
									puiPMRFlags);

	if (eError == PVRSRV_OK)
	{
		eError = PMRLockSysPhysAddresses(*ppsPMRPtr);
	}

	return eError;
}

PVRSRV_ERROR
PVRSRVGetMaxPhysHeapCountKM(CONNECTION_DATA *psConnection,
              			  PVRSRV_DEVICE_NODE *psDevNode,
              			  IMG_UINT32 *pui32PhysHeapCount)
{
	PVR_UNREFERENCED_PARAMETER(psConnection);
	PVRSRVGetDevicePhysHeapCount(psDevNode, pui32PhysHeapCount);
	return PVRSRV_OK;
}

PVRSRV_ERROR
PVRSRVGetDefaultPhysicalHeapKM(CONNECTION_DATA *psConnection,
			  PVRSRV_DEVICE_NODE *psDevNode,
			  PVRSRV_PHYS_HEAP *peHeap)
{
	PVR_UNREFERENCED_PARAMETER(psConnection);
	*peHeap = psDevNode->psDevConfig->eDefaultHeap;
	return PVRSRV_OK;
}

PVRSRV_ERROR
PVRSRVGetHeapPhysMemUsageKM(CONNECTION_DATA *psConnection,
			  PVRSRV_DEVICE_NODE *psDevNode,
			  IMG_UINT32 ui32PhysHeapCount,
			  PHYS_HEAP_MEM_STATS *apPhysHeapMemStats)
{
	PHYS_HEAP *psPhysHeap;
	IMG_UINT uiHeapIndex, i = 0;

	PVR_UNREFERENCED_PARAMETER(psConnection);

	if (ui32PhysHeapCount != psDevNode->ui32UserAllocHeapCount)
	{
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	for (uiHeapIndex = PVRSRV_PHYS_HEAP_DEFAULT+1; (uiHeapIndex < PVRSRV_PHYS_HEAP_LAST); uiHeapIndex++)
	{
		psPhysHeap = psDevNode->apsPhysHeap[uiHeapIndex];

		if (psPhysHeap && PhysHeapUserModeAlloc(uiHeapIndex))
		{
			PVR_ASSERT(i < ui32PhysHeapCount);

			PhysheapGetPhysMemUsage(psPhysHeap, &apPhysHeapMemStats[i].ui64TotalSize,
					&apPhysHeapMemStats[i].ui64FreeSize);

			i++;
		}
	}
	return PVRSRV_OK;
}

PVRSRV_ERROR
PVRSRVGetHeapPhysMemUsagePkdKM(CONNECTION_DATA *psConnection,
			  PVRSRV_DEVICE_NODE *psDevNode,
			  IMG_UINT32 ui32PhysHeapCount,
			  PHYS_HEAP_MEM_STATS_PKD *apPhysHeapMemStats)
{
	PHYS_HEAP *psPhysHeap;
	IMG_UINT uiHeapIndex, i = 0;

	PVR_UNREFERENCED_PARAMETER(psConnection);

	if (ui32PhysHeapCount != psDevNode->ui32UserAllocHeapCount)
	{
		return PVRSRV_ERROR_INVALID_PARAMS;
	}

	for (uiHeapIndex = PVRSRV_PHYS_HEAP_DEFAULT+1; (uiHeapIndex < PVRSRV_PHYS_HEAP_LAST); uiHeapIndex++)
	{
		psPhysHeap = psDevNode->apsPhysHeap[uiHeapIndex];

		if (psPhysHeap && PhysHeapUserModeAlloc(uiHeapIndex))
		{
			PVR_ASSERT(i < ui32PhysHeapCount);

			PhysheapGetPhysMemUsage(psPhysHeap, &apPhysHeapMemStats[i].ui64TotalSize,
					&apPhysHeapMemStats[i].ui64FreeSize);

			i++;
		}
	}
	return PVRSRV_OK;
}

PVRSRV_ERROR
PVRSRVPhysHeapGetMemInfoKM(CONNECTION_DATA *psConnection,
			  PVRSRV_DEVICE_NODE *psDevNode,
			  IMG_UINT32 ui32PhysHeapCount,
			  PVRSRV_PHYS_HEAP *paePhysHeapID,
			  PHYS_HEAP_MEM_STATS *paPhysHeapMemStats)
{
	PVR_UNREFERENCED_PARAMETER(psConnection);
	return PhysHeapGetMemInfo(psDevNode,
				  ui32PhysHeapCount,
				  paePhysHeapID,
				  paPhysHeapMemStats);
}

PVRSRV_ERROR
PVRSRVPhysHeapGetMemInfoPkdKM(CONNECTION_DATA *psConnection,
			  PVRSRV_DEVICE_NODE *psDevNode,
			  IMG_UINT32 ui32PhysHeapCount,
			  PVRSRV_PHYS_HEAP *paePhysHeapID,
			  PHYS_HEAP_MEM_STATS_PKD *paPhysHeapMemStats)
{
	PVR_UNREFERENCED_PARAMETER(psConnection);
	return PhysHeapGetMemInfoPkd(psDevNode,
				  ui32PhysHeapCount,
				  paePhysHeapID,
				  paPhysHeapMemStats);
}

/* 'Wrapper' function to call PMRImportPMR(), which first checks the PMR is
 * for the current device. This avoids the need to do this in pmr.c, which
 * would then need PVRSRV_DEVICE_NODE (defining this type in pmr.h causes a
 * typedef redefinition issue).
 */
PVRSRV_ERROR
PhysmemImportPMR(CONNECTION_DATA *psConnection,
             PVRSRV_DEVICE_NODE *psDevNode,
             PMR_EXPORT *psPMRExport,
             PMR_PASSWORD_T uiPassword,
             PMR_SIZE_T uiSize,
             PMR_LOG2ALIGN_T uiLog2Contig,
             PMR **ppsPMR)
{
	PVR_UNREFERENCED_PARAMETER(psConnection);

	if (PMRGetExportDeviceNode(psPMRExport) != psDevNode)
	{
		PVR_DPF((PVR_DBG_ERROR, "%s: PMR invalid for this device", __func__));
		return PVRSRV_ERROR_PMR_NOT_PERMITTED;
	}

	return PMRImportPMR(psPMRExport,
	                    uiPassword,
	                    uiSize,
	                    uiLog2Contig,
	                    ppsPMR);
}