1 /*************************************************************************/ /*!
3 @Title Host memory management implementation for Linux
4 @Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @License Dual MIT/GPLv2
7 The contents of this file are subject to the MIT license as set out below.
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
19 Alternatively, the contents of this file may be used under the terms of
20 the GNU General Public License Version 2 ("GPL") in which case the provisions
21 of GPL are applicable instead of those above.
23 If you wish to allow use of your version of this file only under the terms of
24 GPL, and not to allow others to use your version of this file under the terms
25 of the MIT license, indicate your decision by deleting the provisions above
26 and replace them with the notice and other provisions required by GPL as set
27 out in the file called "GPL-COPYING" included in this distribution. If you do
28 not delete the provisions above, a recipient may use your version of this file
29 under the terms of either the MIT license or GPL.
31 This License is also included in this distribution in the file called
34 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
35 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
36 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
37 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
38 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
39 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
40 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 */ /**************************************************************************/
43 #include <linux/slab.h>
44 #include <linux/vmalloc.h>
46 #include <linux/string.h>
50 #if defined(PVRSRV_ENABLE_PROCESS_STATS)
51 #include "process_stats.h"
54 IMG_INTERNAL IMG_PVOID OSAllocMem(IMG_UINT32 ui32Size)
56 IMG_PVOID pvRet = IMG_NULL;
58 if (ui32Size > PVR_LINUX_KMALLOC_ALLOCATION_THRESHOLD)
60 pvRet = vmalloc(ui32Size);
62 if (pvRet == IMG_NULL)
64 pvRet = kmalloc(ui32Size, GFP_KERNEL);
67 #if defined(PVRSRV_ENABLE_PROCESS_STATS)
69 if (pvRet != IMG_NULL)
72 if (!is_vmalloc_addr(pvRet))
74 #if !defined(PVRSRV_ENABLE_MEMORY_STATS)
75 PVRSRVStatsIncrMemAllocStat(PVRSRV_MEM_ALLOC_TYPE_KMALLOC, ksize(pvRet));
78 IMG_CPU_PHYADDR sCpuPAddr;
81 PVRSRVStatsAddMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_KMALLOC,
91 #if !defined(PVRSRV_ENABLE_MEMORY_STATS)
92 PVRSRVStatsIncrMemAllocStatAndTrack(PVRSRV_MEM_ALLOC_TYPE_VMALLOC,
93 ((ui32Size + PAGE_SIZE -1) & ~(PAGE_SIZE-1)),
94 (IMG_UINT64)(IMG_UINTPTR_T) pvRet);
97 IMG_CPU_PHYADDR sCpuPAddr;
100 PVRSRVStatsAddMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_VMALLOC,
103 ((ui32Size + PAGE_SIZE -1) & ~(PAGE_SIZE-1)),
115 IMG_INTERNAL IMG_PVOID OSAllocMemstatMem(IMG_UINT32 ui32Size)
117 IMG_PVOID pvRet = IMG_NULL;
119 if (ui32Size > PVR_LINUX_KMALLOC_ALLOCATION_THRESHOLD)
121 pvRet = vmalloc(ui32Size);
123 if (pvRet == IMG_NULL)
125 pvRet = kmalloc(ui32Size, GFP_KERNEL);
131 IMG_INTERNAL IMG_PVOID OSAllocZMem(IMG_UINT32 ui32Size)
133 IMG_PVOID pvRet = IMG_NULL;
135 if (ui32Size > PVR_LINUX_KMALLOC_ALLOCATION_THRESHOLD)
137 pvRet = vzalloc(ui32Size);
139 if (pvRet == IMG_NULL)
141 pvRet = kzalloc(ui32Size, GFP_KERNEL);
144 #if defined(PVRSRV_ENABLE_PROCESS_STATS)
146 if (pvRet != IMG_NULL)
149 if (!is_vmalloc_addr(pvRet))
151 #if !defined(PVRSRV_ENABLE_MEMORY_STATS)
152 PVRSRVStatsIncrMemAllocStat(PVRSRV_MEM_ALLOC_TYPE_KMALLOC, ksize(pvRet));
155 IMG_CPU_PHYADDR sCpuPAddr;
156 sCpuPAddr.uiAddr = 0;
158 PVRSRVStatsAddMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_KMALLOC,
168 #if !defined(PVRSRV_ENABLE_MEMORY_STATS)
169 PVRSRVStatsIncrMemAllocStatAndTrack(PVRSRV_MEM_ALLOC_TYPE_VMALLOC,
170 ((ui32Size + PAGE_SIZE -1) & ~(PAGE_SIZE-1)),
171 (IMG_UINT64)(IMG_UINTPTR_T) pvRet);
174 IMG_CPU_PHYADDR sCpuPAddr;
175 sCpuPAddr.uiAddr = 0;
177 PVRSRVStatsAddMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_VMALLOC,
180 ((ui32Size + PAGE_SIZE -1) & ~(PAGE_SIZE-1)),
191 IMG_INTERNAL IMG_PVOID OSAllocMemstatZMem(IMG_UINT32 ui32Size)
193 IMG_PVOID pvRet = IMG_NULL;
195 if (ui32Size > PVR_LINUX_KMALLOC_ALLOCATION_THRESHOLD)
197 pvRet = vzalloc(ui32Size);
199 if (pvRet == IMG_NULL)
201 pvRet = kzalloc(ui32Size, GFP_KERNEL);
207 IMG_INTERNAL void OSFreeMem(IMG_PVOID pvMem)
210 if ( !is_vmalloc_addr(pvMem) )
212 #if defined(PVRSRV_ENABLE_PROCESS_STATS)
213 if (pvMem != IMG_NULL)
215 #if !defined(PVRSRV_ENABLE_MEMORY_STATS)
216 PVRSRVStatsDecrMemAllocStat(PVRSRV_MEM_ALLOC_TYPE_KMALLOC, ksize(pvMem));
218 PVRSRVStatsRemoveMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_KMALLOC,
219 (IMG_UINT64)(IMG_UINTPTR_T) pvMem);
227 #if defined(PVRSRV_ENABLE_PROCESS_STATS)
228 if (pvMem != IMG_NULL)
230 #if !defined(PVRSRV_ENABLE_MEMORY_STATS)
231 PVRSRVStatsDecrMemAllocStatAndUntrack(PVRSRV_MEM_ALLOC_TYPE_VMALLOC,
232 (IMG_UINT64)(IMG_UINTPTR_T) pvMem);
234 PVRSRVStatsRemoveMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_VMALLOC,
235 (IMG_UINT64)(IMG_UINTPTR_T) pvMem);
243 IMG_INTERNAL void OSFreeMemstatMem(IMG_PVOID pvMem)
245 if ( !is_vmalloc_addr(pvMem) )