2 * \file drm_memory_debug.c
3 * Memory management wrappers for DRM.
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
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.
38 typedef struct drm_mem_stats {
43 unsigned long bytes_allocated;
44 unsigned long bytes_freed;
47 static spinlock_t drm_mem_lock = SPIN_LOCK_UNLOCKED;
48 static unsigned long drm_ram_available = 0; /* In pages */
49 static unsigned long drm_ram_used = 0;
50 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)
81 for (mem = drm_mem_stats; mem->name; ++mem) {
82 mem->succeed_count = 0;
85 mem->bytes_allocated = 0;
90 drm_ram_available = si.totalram;
94 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
96 static int drm__mem_info(char *buf, char **start, off_t offset,
97 int request, int *eof, void *data)
102 if (offset > DRM_PROC_LIMIT) {
108 *start = &buf[offset];
110 DRM_PROC_PRINT(" total counts "
111 " | outstanding \n");
112 DRM_PROC_PRINT("type alloc freed fail bytes freed"
113 " | allocs bytes\n\n");
114 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
116 drm_ram_available << (PAGE_SHIFT - 10));
117 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
118 "locked", 0, 0, 0, drm_ram_used >> 10);
119 DRM_PROC_PRINT("\n");
120 for (pt = drm_mem_stats; pt->name; pt++) {
121 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
128 pt->succeed_count - pt->free_count,
129 (long)pt->bytes_allocated
130 - (long)pt->bytes_freed);
133 if (len > request + offset)
139 int drm_mem_info(char *buf, char **start, off_t offset,
140 int len, int *eof, void *data)
144 spin_lock(&drm_mem_lock);
145 ret = drm__mem_info(buf, start, offset, len, eof, data);
146 spin_unlock(&drm_mem_lock);
150 void *drm_alloc(size_t size, int area)
155 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
159 if (!(pt = kmalloc(size, GFP_KERNEL))) {
160 spin_lock(&drm_mem_lock);
161 ++drm_mem_stats[area].fail_count;
162 spin_unlock(&drm_mem_lock);
165 spin_lock(&drm_mem_lock);
166 ++drm_mem_stats[area].succeed_count;
167 drm_mem_stats[area].bytes_allocated += size;
168 spin_unlock(&drm_mem_lock);
171 EXPORT_SYMBOL(drm_alloc);
173 void *drm_calloc(size_t nmemb, size_t size, int area)
177 addr = drm_alloc(nmemb * size, area);
179 memset((void *)addr, 0, size * nmemb);
183 EXPORT_SYMBOL(drm_calloc);
185 void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
189 if (!(pt = drm_alloc(size, area)))
191 if (oldpt && oldsize) {
192 memcpy(pt, oldpt, oldsize);
193 drm_free(oldpt, oldsize, area);
197 EXPORT_SYMBOL(drm_realloc);
199 void drm_free(void *pt, size_t size, int area)
205 DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
208 spin_lock(&drm_mem_lock);
209 drm_mem_stats[area].bytes_freed += size;
210 free_count = ++drm_mem_stats[area].free_count;
211 alloc_count = drm_mem_stats[area].succeed_count;
212 spin_unlock(&drm_mem_lock);
213 if (free_count > alloc_count) {
214 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
215 free_count, alloc_count);
218 EXPORT_SYMBOL(drm_free);
220 unsigned long drm_alloc_pages(int order, int area)
222 unsigned long address;
223 unsigned long bytes = PAGE_SIZE << order;
227 spin_lock(&drm_mem_lock);
228 if ((drm_ram_used >> PAGE_SHIFT)
229 > (DRM_RAM_PERCENT * drm_ram_available) / 100) {
230 spin_unlock(&drm_mem_lock);
233 spin_unlock(&drm_mem_lock);
235 address = __get_free_pages(GFP_KERNEL, order);
237 spin_lock(&drm_mem_lock);
238 ++drm_mem_stats[area].fail_count;
239 spin_unlock(&drm_mem_lock);
242 spin_lock(&drm_mem_lock);
243 ++drm_mem_stats[area].succeed_count;
244 drm_mem_stats[area].bytes_allocated += bytes;
245 drm_ram_used += bytes;
246 spin_unlock(&drm_mem_lock);
248 /* Zero outside the lock */
249 memset((void *)address, 0, bytes);
252 for (addr = address, sz = bytes;
253 sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
254 SetPageReserved(virt_to_page(addr));
260 void drm_free_pages(unsigned long address, int order, int area)
262 unsigned long bytes = PAGE_SIZE << order;
269 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
272 for (addr = address, sz = bytes;
273 sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
274 ClearPageReserved(virt_to_page(addr));
276 free_pages(address, order);
279 spin_lock(&drm_mem_lock);
280 free_count = ++drm_mem_stats[area].free_count;
281 alloc_count = drm_mem_stats[area].succeed_count;
282 drm_mem_stats[area].bytes_freed += bytes;
283 drm_ram_used -= bytes;
284 spin_unlock(&drm_mem_lock);
285 if (free_count > alloc_count) {
287 "Excess frees: %d frees, %d allocs\n",
288 free_count, alloc_count);
294 DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type)
299 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
303 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
304 if ((handle = drm_agp_allocate_memory(pages, type))) {
306 if ((handle = drm_agp_allocate_memory(dev->agp->bridge, pages, type))) {
308 spin_lock(&drm_mem_lock);
309 ++drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
310 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_allocated
311 += pages << PAGE_SHIFT;
312 spin_unlock(&drm_mem_lock);
315 spin_lock(&drm_mem_lock);
316 ++drm_mem_stats[DRM_MEM_TOTALAGP].fail_count;
317 spin_unlock(&drm_mem_lock);
321 int drm_free_agp(DRM_AGP_MEM * handle, int pages)
325 int retval = -EINVAL;
328 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
329 "Attempt to free NULL AGP handle\n");
333 if (drm_agp_free_memory(handle)) {
334 spin_lock(&drm_mem_lock);
335 free_count = ++drm_mem_stats[DRM_MEM_TOTALAGP].free_count;
336 alloc_count = drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
337 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_freed
338 += pages << PAGE_SHIFT;
339 spin_unlock(&drm_mem_lock);
340 if (free_count > alloc_count) {
341 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
342 "Excess frees: %d frees, %d allocs\n",
343 free_count, alloc_count);
350 int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
352 int retcode = -EINVAL;
355 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
356 "Attempt to bind NULL AGP handle\n");
360 if (!(retcode = drm_agp_bind_memory(handle, start))) {
361 spin_lock(&drm_mem_lock);
362 ++drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
363 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_allocated
364 += handle->page_count << PAGE_SHIFT;
365 spin_unlock(&drm_mem_lock);
368 spin_lock(&drm_mem_lock);
369 ++drm_mem_stats[DRM_MEM_BOUNDAGP].fail_count;
370 spin_unlock(&drm_mem_lock);
374 int drm_unbind_agp(DRM_AGP_MEM * handle)
378 int retcode = -EINVAL;
381 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
382 "Attempt to unbind NULL AGP handle\n");
386 if ((retcode = drm_agp_unbind_memory(handle)))
388 spin_lock(&drm_mem_lock);
389 free_count = ++drm_mem_stats[DRM_MEM_BOUNDAGP].free_count;
390 alloc_count = drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
391 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_freed
392 += handle->page_count << PAGE_SHIFT;
393 spin_unlock(&drm_mem_lock);
394 if (free_count > alloc_count) {
395 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
396 "Excess frees: %d frees, %d allocs\n",
397 free_count, alloc_count);