1 /* memory.c -- Memory management wrappers for DRM -*- linux-c -*-
2 * Created: Thu Feb 4 14:00:34 1999 by faith@precisioninsight.com
3 * Revised: Fri Aug 20 13:04:33 1999 by faith@precisioninsight.com
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
27 * $PI: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/generic/memory.c,v 1.4 1999/08/20 20:00:53 faith Exp $
32 #define __NO_VERSION__
35 typedef struct drm_mem_stats
{
40 unsigned long bytes_allocated
;
41 unsigned long bytes_freed
;
44 static spinlock_t drm_mem_lock
= SPIN_LOCK_UNLOCKED
;
45 static unsigned long drm_ram_available
= 0;
46 static unsigned long drm_ram_used
= 0;
47 static drm_mem_stats_t drm_mem_stats
[] = {
48 [DRM_MEM_DMA
] = { "dmabufs" },
49 [DRM_MEM_SAREA
] = { "sareas" },
50 [DRM_MEM_DRIVER
] = { "driver" },
51 [DRM_MEM_MAGIC
] = { "magic" },
52 [DRM_MEM_IOCTLS
] = { "ioctltab" },
53 [DRM_MEM_MAPS
] = { "maplist" },
54 [DRM_MEM_VMAS
] = { "vmalist" },
55 [DRM_MEM_BUFS
] = { "buflist" },
56 [DRM_MEM_SEGS
] = { "seglist" },
57 [DRM_MEM_PAGES
] = { "pagelist" },
58 [DRM_MEM_FILES
] = { "files" },
59 [DRM_MEM_QUEUES
] = { "queues" },
60 [DRM_MEM_CMDS
] = { "commands" },
61 [DRM_MEM_MAPPINGS
] = { "mappings" },
62 [DRM_MEM_BUFLISTS
] = { "buflists" },
63 { NULL
, 0, } /* Last entry must be null */
66 void drm_mem_init(void)
71 for (mem
= drm_mem_stats
; mem
->name
; ++mem
) {
72 mem
->succeed_count
= 0;
75 mem
->bytes_allocated
= 0;
80 drm_ram_available
= si
.totalram
;
84 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
86 static int _drm_mem_info(char *buf
, char **start
, off_t offset
, int len
,
91 if (offset
> 0) return 0; /* no partial requests */
94 DRM_PROC_PRINT(" total counts "
96 DRM_PROC_PRINT("type alloc freed fail bytes freed"
97 " | allocs bytes\n\n");
98 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu |\n",
99 "system", 0, 0, 0, drm_ram_available
);
100 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu |\n",
101 "locked", 0, 0, 0, drm_ram_used
);
102 DRM_PROC_PRINT("\n");
103 for (pt
= drm_mem_stats
; pt
->name
; pt
++) {
104 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
111 pt
->succeed_count
- pt
->free_count
,
112 (long)pt
->bytes_allocated
113 - (long)pt
->bytes_freed
);
119 int drm_mem_info(char *buf
, char **start
, off_t offset
, int len
,
120 int *eof
, void *data
)
124 spin_lock(&drm_mem_lock
);
125 ret
= _drm_mem_info(buf
, start
, offset
, len
, eof
, data
);
126 spin_unlock(&drm_mem_lock
);
130 void *drm_alloc(size_t size
, int area
)
135 DRM_MEM_ERROR(area
, "Allocating 0 bytes\n");
139 if (!(pt
= kmalloc(size
, GFP_KERNEL
))) {
140 spin_lock(&drm_mem_lock
);
141 ++drm_mem_stats
[area
].fail_count
;
142 spin_unlock(&drm_mem_lock
);
145 spin_lock(&drm_mem_lock
);
146 ++drm_mem_stats
[area
].succeed_count
;
147 drm_mem_stats
[area
].bytes_allocated
+= size
;
148 spin_unlock(&drm_mem_lock
);
152 void *drm_realloc(void *oldpt
, size_t oldsize
, size_t size
, int area
)
156 if (!(pt
= drm_alloc(size
, area
))) return NULL
;
157 if (oldpt
&& oldsize
) {
158 memcpy(pt
, oldpt
, oldsize
);
159 drm_free(oldpt
, oldsize
, area
);
164 char *drm_strdup(const char *s
, int area
)
167 int length
= s
? strlen(s
) : 0;
169 if (!(pt
= drm_alloc(length
+1, area
))) return NULL
;
174 void drm_strfree(const char *s
, int area
)
180 size
= 1 + (s
? strlen(s
) : 0);
181 drm_free((void *)s
, size
, area
);
184 void drm_free(void *pt
, size_t size
, int area
)
189 if (!pt
) DRM_MEM_ERROR(area
, "Attempt to free NULL pointer\n");
190 else kfree_s(pt
, size
);
191 spin_lock(&drm_mem_lock
);
192 drm_mem_stats
[area
].bytes_freed
+= size
;
193 free_count
= ++drm_mem_stats
[area
].free_count
;
194 alloc_count
= drm_mem_stats
[area
].succeed_count
;
195 spin_unlock(&drm_mem_lock
);
196 if (free_count
> alloc_count
) {
197 DRM_MEM_ERROR(area
, "Excess frees: %d frees, %d allocs\n",
198 free_count
, alloc_count
);
202 unsigned long drm_alloc_pages(int order
, int area
)
204 unsigned long address
;
205 unsigned long bytes
= PAGE_SIZE
<< order
;
209 spin_lock(&drm_mem_lock
);
210 if (drm_ram_used
> +(DRM_RAM_PERCENT
* drm_ram_available
) / 100) {
211 spin_unlock(&drm_mem_lock
);
214 spin_unlock(&drm_mem_lock
);
216 address
= __get_free_pages(GFP_KERNEL
, order
);
218 spin_lock(&drm_mem_lock
);
219 ++drm_mem_stats
[area
].fail_count
;
220 spin_unlock(&drm_mem_lock
);
223 spin_lock(&drm_mem_lock
);
224 ++drm_mem_stats
[area
].succeed_count
;
225 drm_mem_stats
[area
].bytes_allocated
+= bytes
;
226 drm_ram_used
+= bytes
;
227 spin_unlock(&drm_mem_lock
);
230 /* Zero outside the lock */
231 memset((void *)address
, 0, bytes
);
234 for (addr
= address
, sz
= bytes
;
236 addr
+= PAGE_SIZE
, sz
-= PAGE_SIZE
) {
237 mem_map_reserve(MAP_NR(addr
));
243 void drm_free_pages(unsigned long address
, int order
, int area
)
245 unsigned long bytes
= PAGE_SIZE
<< order
;
252 DRM_MEM_ERROR(area
, "Attempt to free address 0\n");
255 for (addr
= address
, sz
= bytes
;
257 addr
+= PAGE_SIZE
, sz
-= PAGE_SIZE
) {
258 mem_map_unreserve(MAP_NR(addr
));
260 free_pages(address
, order
);
263 spin_lock(&drm_mem_lock
);
264 free_count
= ++drm_mem_stats
[area
].free_count
;
265 alloc_count
= drm_mem_stats
[area
].succeed_count
;
266 drm_mem_stats
[area
].bytes_freed
+= bytes
;
267 drm_ram_used
-= bytes
;
268 spin_unlock(&drm_mem_lock
);
269 if (free_count
> alloc_count
) {
271 "Excess frees: %d frees, %d allocs\n",
272 free_count
, alloc_count
);
276 void *drm_ioremap(unsigned long offset
, unsigned long size
)
281 DRM_MEM_ERROR(DRM_MEM_MAPPINGS
,
282 "Mapping 0 bytes at 0x%08lx\n", offset
);
286 if (!(pt
= ioremap(offset
, size
))) {
287 spin_lock(&drm_mem_lock
);
288 ++drm_mem_stats
[DRM_MEM_MAPPINGS
].fail_count
;
289 spin_unlock(&drm_mem_lock
);
292 spin_lock(&drm_mem_lock
);
293 ++drm_mem_stats
[DRM_MEM_MAPPINGS
].succeed_count
;
294 drm_mem_stats
[DRM_MEM_MAPPINGS
].bytes_allocated
+= size
;
295 spin_unlock(&drm_mem_lock
);
299 void drm_ioremapfree(void *pt
, unsigned long size
)
305 DRM_MEM_ERROR(DRM_MEM_MAPPINGS
,
306 "Attempt to free NULL pointer\n");
310 spin_lock(&drm_mem_lock
);
311 drm_mem_stats
[DRM_MEM_MAPPINGS
].bytes_freed
+= size
;
312 free_count
= ++drm_mem_stats
[DRM_MEM_MAPPINGS
].free_count
;
313 alloc_count
= drm_mem_stats
[DRM_MEM_MAPPINGS
].succeed_count
;
314 spin_unlock(&drm_mem_lock
);
315 if (free_count
> alloc_count
) {
316 DRM_MEM_ERROR(DRM_MEM_MAPPINGS
,
317 "Excess frees: %d frees, %d allocs\n",
318 free_count
, alloc_count
);