e3e359d6bfcabc6968e0d93af2bc8304944b7052
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / rogue_m / services / server / env / linux / allocmem.c
1 /*************************************************************************/ /*!
2 @File
3 @Title          Host memory management implementation for Linux
4 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @License        Dual MIT/GPLv2
6
7 The contents of this file are subject to the MIT license as set out below.
8
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:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
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.
22
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.
30
31 This License is also included in this distribution in the file called
32 "MIT-COPYING".
33
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 */ /**************************************************************************/
42
43 #include <linux/slab.h>
44 #include <linux/vmalloc.h>
45 #include <linux/mm.h>
46 #include <linux/string.h>
47
48 #include "img_defs.h"
49 #include "allocmem.h"
50 #if defined(PVRSRV_ENABLE_PROCESS_STATS)
51 #include "process_stats.h"
52 #endif
53
54 IMG_INTERNAL IMG_PVOID OSAllocMem(IMG_UINT32 ui32Size)
55 {
56         IMG_PVOID pvRet = IMG_NULL;
57
58         if (ui32Size > PVR_LINUX_KMALLOC_ALLOCATION_THRESHOLD)
59         {
60                 pvRet = vmalloc(ui32Size);
61         }
62         if (pvRet == IMG_NULL)
63         {
64                 pvRet = kmalloc(ui32Size, GFP_KERNEL);
65         }
66
67 #if defined(PVRSRV_ENABLE_PROCESS_STATS)
68
69         if (pvRet != IMG_NULL)
70         {
71
72                 if (!is_vmalloc_addr(pvRet))
73                 {
74 #if !defined(PVRSRV_ENABLE_MEMORY_STATS)
75                         PVRSRVStatsIncrMemAllocStat(PVRSRV_MEM_ALLOC_TYPE_KMALLOC, ksize(pvRet));
76 #else
77                         {
78                                 IMG_CPU_PHYADDR sCpuPAddr;
79                                 sCpuPAddr.uiAddr = 0;
80
81                                 PVRSRVStatsAddMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_KMALLOC,
82                                                              pvRet,
83                                                              sCpuPAddr,
84                                                              ksize(pvRet),
85                                                              IMG_NULL);
86                         }
87 #endif
88                 }
89                 else
90                 {
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);
95 #else
96                         {
97                                 IMG_CPU_PHYADDR sCpuPAddr;
98                                 sCpuPAddr.uiAddr = 0;
99
100                                 PVRSRVStatsAddMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_VMALLOC,
101                                                                                          pvRet,
102                                                                                          sCpuPAddr,
103                                                                                          ((ui32Size + PAGE_SIZE -1) & ~(PAGE_SIZE-1)),
104                                                                                          IMG_NULL);
105                         }
106 #endif
107                 }
108
109         }
110 #endif
111         return pvRet;
112 }
113
114
115 IMG_INTERNAL IMG_PVOID OSAllocMemstatMem(IMG_UINT32 ui32Size)
116 {
117         IMG_PVOID pvRet = IMG_NULL;
118
119         if (ui32Size > PVR_LINUX_KMALLOC_ALLOCATION_THRESHOLD)
120         {
121                 pvRet = vmalloc(ui32Size);
122         }
123         if (pvRet == IMG_NULL)
124         {
125                 pvRet = kmalloc(ui32Size, GFP_KERNEL);
126         }
127
128         return pvRet;
129 }
130
131 IMG_INTERNAL IMG_PVOID OSAllocZMem(IMG_UINT32 ui32Size)
132 {
133         IMG_PVOID pvRet = IMG_NULL;
134
135         if (ui32Size > PVR_LINUX_KMALLOC_ALLOCATION_THRESHOLD)
136         {
137                 pvRet = vzalloc(ui32Size);
138         }
139         if (pvRet == IMG_NULL)
140         {
141                 pvRet = kzalloc(ui32Size, GFP_KERNEL);
142         }
143
144 #if defined(PVRSRV_ENABLE_PROCESS_STATS)
145
146         if (pvRet != IMG_NULL)
147         {
148
149                 if (!is_vmalloc_addr(pvRet))
150                 {
151 #if !defined(PVRSRV_ENABLE_MEMORY_STATS)
152                         PVRSRVStatsIncrMemAllocStat(PVRSRV_MEM_ALLOC_TYPE_KMALLOC, ksize(pvRet));
153 #else
154                         {
155                                 IMG_CPU_PHYADDR sCpuPAddr;
156                                 sCpuPAddr.uiAddr = 0;
157
158                                 PVRSRVStatsAddMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_KMALLOC,
159                                                              pvRet,
160                                                              sCpuPAddr,
161                                                              ksize(pvRet),
162                                                              IMG_NULL);
163                         }
164 #endif
165                 }
166                 else
167                 {
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);
172 #else
173                         {
174                                 IMG_CPU_PHYADDR sCpuPAddr;
175                                 sCpuPAddr.uiAddr = 0;
176
177                                 PVRSRVStatsAddMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_VMALLOC,
178                                                                                          pvRet,
179                                                                                          sCpuPAddr,
180                                                                                          ((ui32Size + PAGE_SIZE -1) & ~(PAGE_SIZE-1)),
181                                                                                          IMG_NULL);
182                         }
183 #endif
184                 }
185
186         }
187 #endif
188         return pvRet;
189 }
190
191 IMG_INTERNAL IMG_PVOID OSAllocMemstatZMem(IMG_UINT32 ui32Size)
192 {
193         IMG_PVOID pvRet = IMG_NULL;
194
195         if (ui32Size > PVR_LINUX_KMALLOC_ALLOCATION_THRESHOLD)
196         {
197                 pvRet = vzalloc(ui32Size);
198         }
199         if (pvRet == IMG_NULL)
200         {
201                 pvRet = kzalloc(ui32Size, GFP_KERNEL);
202         }
203
204         return pvRet;
205 }
206
207 IMG_INTERNAL void OSFreeMem(IMG_PVOID pvMem)
208 {
209
210         if ( !is_vmalloc_addr(pvMem) )
211         {
212 #if defined(PVRSRV_ENABLE_PROCESS_STATS)
213                 if (pvMem != IMG_NULL)
214                 {
215 #if !defined(PVRSRV_ENABLE_MEMORY_STATS)
216                         PVRSRVStatsDecrMemAllocStat(PVRSRV_MEM_ALLOC_TYPE_KMALLOC, ksize(pvMem));
217 #else
218                         PVRSRVStatsRemoveMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_KMALLOC,
219                                                        (IMG_UINT64)(IMG_UINTPTR_T) pvMem);
220 #endif
221                 }
222 #endif
223                 kfree(pvMem);
224         }
225         else
226         {
227 #if defined(PVRSRV_ENABLE_PROCESS_STATS)
228                 if (pvMem != IMG_NULL)
229                 {
230 #if !defined(PVRSRV_ENABLE_MEMORY_STATS)
231                         PVRSRVStatsDecrMemAllocStatAndUntrack(PVRSRV_MEM_ALLOC_TYPE_VMALLOC,
232                                                              (IMG_UINT64)(IMG_UINTPTR_T) pvMem);
233 #else
234                         PVRSRVStatsRemoveMemAllocRecord(PVRSRV_MEM_ALLOC_TYPE_VMALLOC,
235                                                        (IMG_UINT64)(IMG_UINTPTR_T) pvMem);
236 #endif
237                 }
238 #endif
239                 vfree(pvMem);
240         }
241 }
242
243 IMG_INTERNAL void OSFreeMemstatMem(IMG_PVOID pvMem)
244 {
245         if ( !is_vmalloc_addr(pvMem) )
246         {
247                 kfree(pvMem);
248         }
249         else
250         {
251                 vfree(pvMem);
252         }
253 }