2 * \file drm_memory_debug.h
3 * Memory management wrappers for DRM.
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 * OTHER DEALINGS IN THE SOFTWARE.
34 #include <linux/config.h>
37 typedef struct drm_mem_stats {
42 unsigned long bytes_allocated;
43 unsigned long bytes_freed;
46 static DEFINE_SPINLOCK(drm_mem_lock);
47 static unsigned long drm_ram_available = 0; /* In pages */
48 static unsigned long drm_ram_used = 0;
49 static drm_mem_stats_t drm_mem_stats[] =
51 [DRM_MEM_DMA] = {"dmabufs"},
52 [DRM_MEM_SAREA] = {"sareas"},
53 [DRM_MEM_DRIVER] = {"driver"},
54 [DRM_MEM_MAGIC] = {"magic"},
55 [DRM_MEM_IOCTLS] = {"ioctltab"},
56 [DRM_MEM_MAPS] = {"maplist"},
57 [DRM_MEM_VMAS] = {"vmalist"},
58 [DRM_MEM_BUFS] = {"buflist"},
59 [DRM_MEM_SEGS] = {"seglist"},
60 [DRM_MEM_PAGES] = {"pagelist"},
61 [DRM_MEM_FILES] = {"files"},
62 [DRM_MEM_QUEUES] = {"queues"},
63 [DRM_MEM_CMDS] = {"commands"},
64 [DRM_MEM_MAPPINGS] = {"mappings"},
65 [DRM_MEM_BUFLISTS] = {"buflists"},
66 [DRM_MEM_AGPLISTS] = {"agplist"},
67 [DRM_MEM_SGLISTS] = {"sglist"},
68 [DRM_MEM_TOTALAGP] = {"totalagp"},
69 [DRM_MEM_BOUNDAGP] = {"boundagp"},
70 [DRM_MEM_CTXBITMAP] = {"ctxbitmap"},
71 [DRM_MEM_CTXLIST] = {"ctxlist"},
72 [DRM_MEM_STUB] = {"stub"},
73 {NULL, 0,} /* Last entry must be null */
76 void drm_mem_init (void) {
80 for (mem = drm_mem_stats; mem->name; ++mem) {
81 mem->succeed_count = 0;
84 mem->bytes_allocated = 0;
89 drm_ram_available = si.totalram;
93 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
95 static int drm__mem_info (char *buf, char **start, off_t offset,
96 int request, int *eof, void *data) {
100 if (offset > DRM_PROC_LIMIT) {
106 *start = &buf[offset];
108 DRM_PROC_PRINT(" total counts "
109 " | outstanding \n");
110 DRM_PROC_PRINT("type alloc freed fail bytes freed"
111 " | allocs bytes\n\n");
112 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
114 drm_ram_available << (PAGE_SHIFT - 10));
115 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
116 "locked", 0, 0, 0, drm_ram_used >> 10);
117 DRM_PROC_PRINT("\n");
118 for (pt = drm_mem_stats; pt->name; pt++) {
119 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
126 pt->succeed_count - pt->free_count,
127 (long)pt->bytes_allocated
128 - (long)pt->bytes_freed);
131 if (len > request + offset)
137 int drm_mem_info (char *buf, char **start, off_t offset,
138 int len, int *eof, void *data) {
141 spin_lock(&drm_mem_lock);
142 ret = drm__mem_info (buf, start, offset, len, eof, data);
143 spin_unlock(&drm_mem_lock);
147 void *drm_alloc (size_t size, int area) {
151 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
155 if (!(pt = kmalloc(size, GFP_KERNEL))) {
156 spin_lock(&drm_mem_lock);
157 ++drm_mem_stats[area].fail_count;
158 spin_unlock(&drm_mem_lock);
161 spin_lock(&drm_mem_lock);
162 ++drm_mem_stats[area].succeed_count;
163 drm_mem_stats[area].bytes_allocated += size;
164 spin_unlock(&drm_mem_lock);
168 void *drm_calloc (size_t nmemb, size_t size, int area) {
171 addr = drm_alloc (nmemb * size, area);
173 memset((void *)addr, 0, size * nmemb);
178 void *drm_realloc (void *oldpt, size_t oldsize, size_t size, int area) {
181 if (!(pt = drm_alloc (size, area)))
183 if (oldpt && oldsize) {
184 memcpy(pt, oldpt, oldsize);
185 drm_free (oldpt, oldsize, area);
190 void drm_free (void *pt, size_t size, int area) {
195 DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
198 spin_lock(&drm_mem_lock);
199 drm_mem_stats[area].bytes_freed += size;
200 free_count = ++drm_mem_stats[area].free_count;
201 alloc_count = drm_mem_stats[area].succeed_count;
202 spin_unlock(&drm_mem_lock);
203 if (free_count > alloc_count) {
204 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
205 free_count, alloc_count);
209 void *drm_ioremap (unsigned long offset, unsigned long size,
210 drm_device_t * dev) {
214 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
215 "Mapping 0 bytes at 0x%08lx\n", offset);
219 if (!(pt = drm_ioremap(offset, size, dev))) {
220 spin_lock(&drm_mem_lock);
221 ++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
222 spin_unlock(&drm_mem_lock);
225 spin_lock(&drm_mem_lock);
226 ++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
227 drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
228 spin_unlock(&drm_mem_lock);
233 void *drm_ioremap_nocache (unsigned long offset, unsigned long size,
234 drm_device_t * dev) {
238 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
239 "Mapping 0 bytes at 0x%08lx\n", offset);
243 if (!(pt = drm_ioremap_nocache(offset, size, dev))) {
244 spin_lock(&drm_mem_lock);
245 ++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
246 spin_unlock(&drm_mem_lock);
249 spin_lock(&drm_mem_lock);
250 ++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
251 drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
252 spin_unlock(&drm_mem_lock);
257 void drm_ioremapfree (void *pt, unsigned long size, drm_device_t * dev) {
262 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
263 "Attempt to free NULL pointer\n");
265 drm_ioremapfree(pt, size, dev);
267 spin_lock(&drm_mem_lock);
268 drm_mem_stats[DRM_MEM_MAPPINGS].bytes_freed += size;
269 free_count = ++drm_mem_stats[DRM_MEM_MAPPINGS].free_count;
270 alloc_count = drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
271 spin_unlock(&drm_mem_lock);
272 if (free_count > alloc_count) {
273 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
274 "Excess frees: %d frees, %d allocs\n",
275 free_count, alloc_count);
281 DRM_AGP_MEM *drm_alloc_agp (drm_device_t *dev, int pages, u32 type) {
285 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
289 if ((handle = drm_agp_allocate_memory (pages, type))) {
290 spin_lock(&drm_mem_lock);
291 ++drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
292 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_allocated
293 += pages << PAGE_SHIFT;
294 spin_unlock(&drm_mem_lock);
297 spin_lock(&drm_mem_lock);
298 ++drm_mem_stats[DRM_MEM_TOTALAGP].fail_count;
299 spin_unlock(&drm_mem_lock);
303 int drm_free_agp (DRM_AGP_MEM * handle, int pages) {
306 int retval = -EINVAL;
309 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
310 "Attempt to free NULL AGP handle\n");
314 if (drm_agp_free_memory (handle)) {
315 spin_lock(&drm_mem_lock);
316 free_count = ++drm_mem_stats[DRM_MEM_TOTALAGP].free_count;
317 alloc_count = drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
318 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_freed
319 += pages << PAGE_SHIFT;
320 spin_unlock(&drm_mem_lock);
321 if (free_count > alloc_count) {
322 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
323 "Excess frees: %d frees, %d allocs\n",
324 free_count, alloc_count);
331 int drm_bind_agp (DRM_AGP_MEM * handle, unsigned int start) {
332 int retcode = -EINVAL;
335 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
336 "Attempt to bind NULL AGP handle\n");
340 if (!(retcode = drm_agp_bind_memory (handle, start))) {
341 spin_lock(&drm_mem_lock);
342 ++drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
343 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_allocated
344 += handle->page_count << PAGE_SHIFT;
345 spin_unlock(&drm_mem_lock);
348 spin_lock(&drm_mem_lock);
349 ++drm_mem_stats[DRM_MEM_BOUNDAGP].fail_count;
350 spin_unlock(&drm_mem_lock);
354 int drm_unbind_agp (DRM_AGP_MEM * handle) {
357 int retcode = -EINVAL;
360 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
361 "Attempt to unbind NULL AGP handle\n");
365 if ((retcode = drm_agp_unbind_memory (handle)))
367 spin_lock(&drm_mem_lock);
368 free_count = ++drm_mem_stats[DRM_MEM_BOUNDAGP].free_count;
369 alloc_count = drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
370 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_freed
371 += handle->page_count << PAGE_SHIFT;
372 spin_unlock(&drm_mem_lock);
373 if (free_count > alloc_count) {
374 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
375 "Excess frees: %d frees, %d allocs\n",
376 free_count, alloc_count);