gpu: pvr: pdumpfs: add Kconfig and debugfs pdump mode handling
[sgx.git] / pvr / mm.h
1 /**********************************************************************
2  *
3  * Copyright(c) 2008 Imagination Technologies Ltd. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful but, except
10  * as otherwise stated in writing, without any warranty; without even the
11  * implied warranty of merchantability or fitness for a particular purpose.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * Imagination Technologies Ltd. <gpl-support@imgtec.com>
23  * Home Park Estate, Kings Langley, Herts, WD4 8LZ, UK
24  *
25  ******************************************************************************/
26
27 #ifndef __IMG_LINUX_MM_H__
28 #define __IMG_LINUX_MM_H__
29
30 #include <linux/version.h>
31 #include <linux/slab.h>
32 #include <linux/mm.h>
33 #include <linux/list.h>
34
35 #include <linux/io.h>
36
37 #define PHYS_TO_PFN(phys)                       ((phys) >> PAGE_SHIFT)
38 #define PFN_TO_PHYS(pfn)                        ((pfn) << PAGE_SHIFT)
39
40 #define RANGE_TO_PAGES(range)                                           \
41         (((range) + (PAGE_SIZE - 1)) >> PAGE_SHIFT)
42
43 #define ADDR_TO_PAGE_OFFSET(addr) (((unsigned long)(addr)) & (PAGE_SIZE - 1))
44
45 #define REMAP_PFN_RANGE(vma, addr, pfn, size, prot)                     \
46         remap_pfn_range(vma, addr, pfn, size, prot)
47
48 #define IO_REMAP_PFN_RANGE(vma, addr, pfn, size, prot)                  \
49         io_remap_pfn_range(vma, addr, pfn, size, prot)
50
51 #define VM_INSERT_PAGE(vma, addr, page) vm_insert_page(vma, addr, page)
52
53 static inline u32 VMallocToPhys(void *pCpuVAddr)
54 {
55         return page_to_phys(vmalloc_to_page(pCpuVAddr)) +
56                 ADDR_TO_PAGE_OFFSET(pCpuVAddr);
57
58 }
59
60 enum LINUX_MEM_AREA_TYPE {
61         LINUX_MEM_AREA_IOREMAP,
62         LINUX_MEM_AREA_EXTERNAL_KV,
63         LINUX_MEM_AREA_IO,
64         LINUX_MEM_AREA_VMALLOC,
65         LINUX_MEM_AREA_ALLOC_PAGES,
66         LINUX_MEM_AREA_SUB_ALLOC,
67         LINUX_MEM_AREA_TYPE_COUNT
68 };
69
70 struct LinuxMemArea;
71
72 struct LinuxMemArea {
73         enum LINUX_MEM_AREA_TYPE eAreaType;
74         union _uData {
75                 struct _sIORemap {
76                         struct IMG_CPU_PHYADDR CPUPhysAddr;
77                         void __iomem *pvIORemapCookie;
78                 } sIORemap;
79                 struct _sExternalKV {
80                         IMG_BOOL bPhysContig;
81                         union {
82                                 struct IMG_SYS_PHYADDR SysPhysAddr;
83                                 struct IMG_SYS_PHYADDR *pSysPhysAddr;
84                         } uPhysAddr;
85                         void *pvExternalKV;
86                 } sExternalKV;
87                 struct _sIO {
88                         struct IMG_CPU_PHYADDR CPUPhysAddr;
89                 } sIO;
90                 struct _sVmalloc {
91                         void *pvVmallocAddress;
92                 } sVmalloc;
93                 struct _sPageList {
94                         struct page **pvPageList;
95                         void *hBlockPageList;
96                 } sPageList;
97                 struct _sSubAlloc {
98                         struct LinuxMemArea *psParentLinuxMemArea;
99                         u32 ui32ByteOffset;
100                 } sSubAlloc;
101         } uData;
102         u32 ui32ByteSize;
103         u32 ui32AreaFlags;
104         IMG_BOOL bMMapRegistered;
105         struct list_head sMMapItem;
106         struct list_head sMMapOffsetStructList;
107 };
108
109 struct kmem_cache;
110
111 enum PVRSRV_ERROR LinuxMMInit(void);
112
113 void LinuxMMCleanup(void);
114
115 #if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
116 #define KMallocWrapper(ui32ByteSize)    \
117                 _KMallocWrapper(ui32ByteSize, __FILE__, __LINE__)
118 #else
119 #define KMallocWrapper(ui32ByteSize)    \
120                 _KMallocWrapper(ui32ByteSize, NULL, 0)
121 #endif
122 void *_KMallocWrapper(u32 ui32ByteSize, char *szFileName, u32 ui32Line);
123
124 #if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
125 #define KFreeWrapper(pvCpuVAddr) _KFreeWrapper(pvCpuVAddr, __FILE__, __LINE__)
126 #else
127 #define KFreeWrapper(pvCpuVAddr) _KFreeWrapper(pvCpuVAddr, NULL, 0)
128 #endif
129 void _KFreeWrapper(void *pvCpuVAddr, char *pszFileName, u32 ui32Line);
130
131 #if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
132 #define VMallocWrapper(ui32Bytes, ui32AllocFlags)       \
133                 _VMallocWrapper(ui32Bytes, ui32AllocFlags, __FILE__, __LINE__)
134 #else
135 #define VMallocWrapper(ui32Bytes, ui32AllocFlags)       \
136                 _VMallocWrapper(ui32Bytes, ui32AllocFlags, NULL, 0)
137 #endif
138 void *_VMallocWrapper(u32 ui32Bytes, u32 ui32AllocFlags, char *pszFileName,
139                       u32 ui32Line);
140
141 #if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
142 #define VFreeWrapper(pvCpuVAddr) _VFreeWrapper(pvCpuVAddr, __FILE__, __LINE__)
143 #else
144 #define VFreeWrapper(pvCpuVAddr) _VFreeWrapper(pvCpuVAddr, NULL, 0)
145 #endif
146 void _VFreeWrapper(void *pvCpuVAddr, char *pszFileName, u32 ui32Line);
147
148 struct LinuxMemArea *NewVMallocLinuxMemArea(u32 ui32Bytes, u32 ui32AreaFlags);
149
150 void FreeVMallocLinuxMemArea(struct LinuxMemArea *psLinuxMemArea);
151
152 #if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
153 #define IORemapWrapper(BasePAddr, ui32Bytes, ui32MappingFlags) \
154     _IORemapWrapper(BasePAddr, ui32Bytes, ui32MappingFlags, __FILE__, __LINE__)
155 #else
156 #define IORemapWrapper(BasePAddr, ui32Bytes, ui32MappingFlags) \
157     _IORemapWrapper(BasePAddr, ui32Bytes, ui32MappingFlags, NULL, 0)
158 #endif
159 void __iomem *_IORemapWrapper(struct IMG_CPU_PHYADDR BasePAddr, u32 ui32Bytes,
160                 u32 ui32MappingFlags, char *pszFileName,
161                 u32 ui32Line);
162
163 struct LinuxMemArea *NewIORemapLinuxMemArea(struct IMG_CPU_PHYADDR BasePAddr,
164                 u32 ui32Bytes, u32 ui32AreaFlags);
165
166 void FreeIORemapLinuxMemArea(struct LinuxMemArea *psLinuxMemArea);
167
168 struct LinuxMemArea *NewExternalKVLinuxMemArea(
169                 struct IMG_SYS_PHYADDR *pBasePAddr, void *pvCPUVAddr,
170                 u32 ui32Bytes, IMG_BOOL bPhysContig, u32 ui32AreaFlags);
171
172 void FreeExternalKVLinuxMemArea(struct LinuxMemArea *psLinuxMemArea);
173
174 #if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
175 #define IOUnmapWrapper(pvIORemapCookie) \
176     _IOUnmapWrapper(pvIORemapCookie, __FILE__, __LINE__)
177 #else
178 #define IOUnmapWrapper(pvIORemapCookie) \
179     _IOUnmapWrapper(pvIORemapCookie, NULL, 0)
180 #endif
181 void _IOUnmapWrapper(void __iomem *pvIORemapCookie, char *pszFileName,
182                          u32 ui32Line);
183
184 struct page *LinuxMemAreaOffsetToPage(struct LinuxMemArea *psLinuxMemArea,
185                                       u32 ui32ByteOffset);
186
187 #if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
188 #define KMemCacheAllocWrapper(psCache, Flags)   \
189                 _KMemCacheAllocWrapper(psCache, Flags, __FILE__, __LINE__)
190 #else
191 #define KMemCacheAllocWrapper(psCache, Flags)   \
192                 _KMemCacheAllocWrapper(psCache, Flags, NULL, 0)
193 #endif
194
195 void *_KMemCacheAllocWrapper(struct kmem_cache *psCache, gfp_t Flags,
196                                  char *pszFileName, u32 ui32Line);
197
198 #if defined(DEBUG_LINUX_MEMORY_ALLOCATIONS)
199 #define KMemCacheFreeWrapper(psCache, pvObject) \
200                 _KMemCacheFreeWrapper(psCache, pvObject, __FILE__, __LINE__)
201 #else
202 #define KMemCacheFreeWrapper(psCache, pvObject) \
203                 _KMemCacheFreeWrapper(psCache, pvObject, NULL, 0)
204 #endif
205 void _KMemCacheFreeWrapper(struct kmem_cache *psCache, void *pvObject,
206                                 char *pszFileName, u32 ui32Line);
207
208 const char *KMemCacheNameWrapper(struct kmem_cache *psCache);
209
210 struct LinuxMemArea *NewIOLinuxMemArea(struct IMG_CPU_PHYADDR BasePAddr,
211                                 u32 ui32Bytes, u32 ui32AreaFlags);
212
213 void FreeIOLinuxMemArea(struct LinuxMemArea *psLinuxMemArea);
214
215 struct LinuxMemArea *NewAllocPagesLinuxMemArea(u32 ui32Bytes,
216                                 u32 ui32AreaFlags);
217
218 void FreeAllocPagesLinuxMemArea(struct LinuxMemArea *psLinuxMemArea);
219
220 struct LinuxMemArea *NewSubLinuxMemArea(
221                                 struct LinuxMemArea *psParentLinuxMemArea,
222                                 u32 ui32ByteOffset, u32 ui32Bytes);
223
224 void LinuxMemAreaDeepFree(struct LinuxMemArea *psLinuxMemArea);
225
226 void *LinuxMemAreaToCpuVAddr(struct LinuxMemArea *psLinuxMemArea);
227
228 struct IMG_CPU_PHYADDR LinuxMemAreaToCpuPAddr(
229                                 struct LinuxMemArea *psLinuxMemArea,
230                                 u32 ui32ByteOffset);
231
232 #define  LinuxMemAreaToCpuPFN(psLinuxMemArea, ui32ByteOffset)           \
233         PHYS_TO_PFN(LinuxMemAreaToCpuPAddr(psLinuxMemArea,              \
234                                            ui32ByteOffset).uiAddr)
235
236 void inv_cache_mem_area(const struct LinuxMemArea *mem_area);
237
238 IMG_BOOL LinuxMemAreaPhysIsContig(struct LinuxMemArea *psLinuxMemArea);
239
240 static inline struct LinuxMemArea *LinuxMemAreaRoot(struct LinuxMemArea
241                                                     *psLinuxMemArea)
242 {
243         if (psLinuxMemArea->eAreaType == LINUX_MEM_AREA_SUB_ALLOC)
244                 return psLinuxMemArea->uData.sSubAlloc.psParentLinuxMemArea;
245         else
246                 return psLinuxMemArea;
247 }
248
249 static inline enum LINUX_MEM_AREA_TYPE LinuxMemAreaRootType(struct LinuxMemArea
250                                                             *psLinuxMemArea)
251 {
252         return LinuxMemAreaRoot(psLinuxMemArea)->eAreaType;
253 }
254
255 const char *LinuxMemAreaTypeToString(enum LINUX_MEM_AREA_TYPE eMemAreaType);
256
257 #if defined(CONFIG_PVR_DEBUG_EXTRA) || defined(DEBUG_LINUX_MEM_AREAS)
258 const char *HAPFlagsToString(u32 ui32Flags);
259 #endif
260
261 #endif