blob: 706b75251ea102dc8d1e8cf5c1e87a3d1b7dd6d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlief0c408b2006-01-02 19:52:09 +11002 * \file drm_memory_debug.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Memory management wrappers for DRM.
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
13 *
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:
20 *
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
23 * Software.
24 *
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.
32 */
33
34#include <linux/config.h>
35#include "drmP.h"
36
37typedef struct drm_mem_stats {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100038 const char *name;
39 int succeed_count;
40 int free_count;
41 int fail_count;
42 unsigned long bytes_allocated;
43 unsigned long bytes_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044} drm_mem_stats_t;
45
Dave Airlief0c408b2006-01-02 19:52:09 +110046static spinlock_t drm_mem_lock = SPIN_LOCK_UNLOCKED;
47static unsigned long drm_ram_available = 0; /* In pages */
48static unsigned long drm_ram_used = 0;
49static drm_mem_stats_t drm_mem_stats[] =
Dave Airlieb5e89ed2005-09-25 14:28:13 +100050{
Dave Airlief0c408b2006-01-02 19:52:09 +110051 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
Dave Airlief0c408b2006-01-02 19:52:09 +110076void drm_mem_init (void) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 drm_mem_stats_t *mem;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100078 struct sysinfo si;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Dave Airlief0c408b2006-01-02 19:52:09 +110080 for (mem = drm_mem_stats; mem->name; ++mem) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100081 mem->succeed_count = 0;
82 mem->free_count = 0;
83 mem->fail_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 mem->bytes_allocated = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100085 mem->bytes_freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87
88 si_meminfo(&si);
Dave Airlief0c408b2006-01-02 19:52:09 +110089 drm_ram_available = si.totalram;
90 drm_ram_used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/* drm_mem_info is called whenever a process reads /dev/drm/mem. */
94
Dave Airlief0c408b2006-01-02 19:52:09 +110095static int drm__mem_info (char *buf, char **start, off_t offset,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100096 int request, int *eof, void *data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 drm_mem_stats_t *pt;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 if (offset > DRM_PROC_LIMIT) {
101 *eof = 1;
102 return 0;
103 }
104
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000105 *eof = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *start = &buf[offset];
107
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",
113 "system", 0, 0, 0,
Dave Airlief0c408b2006-01-02 19:52:09 +1100114 drm_ram_available << (PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
Dave Airlief0c408b2006-01-02 19:52:09 +1100116 "locked", 0, 0, 0, drm_ram_used >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 DRM_PROC_PRINT("\n");
Dave Airlief0c408b2006-01-02 19:52:09 +1100118 for (pt = drm_mem_stats; pt->name; pt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
120 pt->name,
121 pt->succeed_count,
122 pt->free_count,
123 pt->fail_count,
124 pt->bytes_allocated,
125 pt->bytes_freed,
126 pt->succeed_count - pt->free_count,
127 (long)pt->bytes_allocated
128 - (long)pt->bytes_freed);
129 }
130
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 if (len > request + offset)
132 return request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *eof = 1;
134 return len - offset;
135}
136
Dave Airlief0c408b2006-01-02 19:52:09 +1100137int drm_mem_info (char *buf, char **start, off_t offset,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138 int len, int *eof, void *data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int ret;
140
Dave Airlief0c408b2006-01-02 19:52:09 +1100141 spin_lock(&drm_mem_lock);
142 ret = drm__mem_info (buf, start, offset, len, eof, data);
143 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return ret;
145}
146
Dave Airlief0c408b2006-01-02 19:52:09 +1100147void *drm_alloc (size_t size, int area) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 void *pt;
149
150 if (!size) {
151 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
152 return NULL;
153 }
154
155 if (!(pt = kmalloc(size, GFP_KERNEL))) {
Dave Airlief0c408b2006-01-02 19:52:09 +1100156 spin_lock(&drm_mem_lock);
157 ++drm_mem_stats[area].fail_count;
158 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return NULL;
160 }
Dave Airlief0c408b2006-01-02 19:52:09 +1100161 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return pt;
166}
167
Dave Airlief0c408b2006-01-02 19:52:09 +1100168void *drm_calloc (size_t nmemb, size_t size, int area) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 void *addr;
170
Dave Airlief0c408b2006-01-02 19:52:09 +1100171 addr = drm_alloc (nmemb * size, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (addr != NULL)
173 memset((void *)addr, 0, size * nmemb);
174
175 return addr;
176}
177
Dave Airlief0c408b2006-01-02 19:52:09 +1100178void *drm_realloc (void *oldpt, size_t oldsize, size_t size, int area) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 void *pt;
180
Dave Airlief0c408b2006-01-02 19:52:09 +1100181 if (!(pt = drm_alloc (size, area)))
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000182 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (oldpt && oldsize) {
184 memcpy(pt, oldpt, oldsize);
Dave Airlief0c408b2006-01-02 19:52:09 +1100185 drm_free (oldpt, oldsize, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187 return pt;
188}
189
Dave Airlief0c408b2006-01-02 19:52:09 +1100190void drm_free (void *pt, size_t size, int area) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int alloc_count;
192 int free_count;
193
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 if (!pt)
195 DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
196 else
197 kfree(pt);
Dave Airlief0c408b2006-01-02 19:52:09 +1100198 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (free_count > alloc_count) {
204 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
205 free_count, alloc_count);
206 }
207}
208
Dave Airlief0c408b2006-01-02 19:52:09 +1100209unsigned long drm_alloc_pages (int order, int area) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned long address;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 unsigned long bytes = PAGE_SIZE << order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 unsigned long addr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Dave Airlief0c408b2006-01-02 19:52:09 +1100215 spin_lock(&drm_mem_lock);
216 if ((drm_ram_used >> PAGE_SHIFT)
217 > (DRM_RAM_PERCENT * drm_ram_available) / 100) {
218 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
220 }
Dave Airlief0c408b2006-01-02 19:52:09 +1100221 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 address = __get_free_pages(GFP_KERNEL, order);
224 if (!address) {
Dave Airlief0c408b2006-01-02 19:52:09 +1100225 spin_lock(&drm_mem_lock);
226 ++drm_mem_stats[area].fail_count;
227 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return 0;
229 }
Dave Airlief0c408b2006-01-02 19:52:09 +1100230 spin_lock(&drm_mem_lock);
231 ++drm_mem_stats[area].succeed_count;
232 drm_mem_stats[area].bytes_allocated += bytes;
233 drm_ram_used += bytes;
234 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000236 /* Zero outside the lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 memset((void *)address, 0, bytes);
238
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 /* Reserve */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 for (addr = address, sz = bytes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 SetPageReserved(virt_to_page(addr));
243 }
244
245 return address;
246}
247
Dave Airlief0c408b2006-01-02 19:52:09 +1100248void drm_free_pages (unsigned long address, int order, int area) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 unsigned long bytes = PAGE_SIZE << order;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000250 int alloc_count;
251 int free_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unsigned long addr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 unsigned int sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (!address) {
256 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
257 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 /* Unreserve */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 for (addr = address, sz = bytes;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000260 sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 ClearPageReserved(virt_to_page(addr));
262 }
263 free_pages(address, order);
264 }
265
Dave Airlief0c408b2006-01-02 19:52:09 +1100266 spin_lock(&drm_mem_lock);
267 free_count = ++drm_mem_stats[area].free_count;
268 alloc_count = drm_mem_stats[area].succeed_count;
269 drm_mem_stats[area].bytes_freed += bytes;
270 drm_ram_used -= bytes;
271 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (free_count > alloc_count) {
273 DRM_MEM_ERROR(area,
274 "Excess frees: %d frees, %d allocs\n",
275 free_count, alloc_count);
276 }
277}
278
Dave Airlief0c408b2006-01-02 19:52:09 +1100279void *drm_ioremap (unsigned long offset, unsigned long size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000280 drm_device_t * dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 void *pt;
282
283 if (!size) {
284 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
285 "Mapping 0 bytes at 0x%08lx\n", offset);
286 return NULL;
287 }
288
289 if (!(pt = drm_ioremap(offset, size, dev))) {
Dave Airlief0c408b2006-01-02 19:52:09 +1100290 spin_lock(&drm_mem_lock);
291 ++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
292 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return NULL;
294 }
Dave Airlief0c408b2006-01-02 19:52:09 +1100295 spin_lock(&drm_mem_lock);
296 ++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
297 drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
298 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return pt;
300}
301
Dave Airlief0c408b2006-01-02 19:52:09 +1100302void *drm_ioremap_nocache (unsigned long offset, unsigned long size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000303 drm_device_t * dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 void *pt;
305
306 if (!size) {
307 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
308 "Mapping 0 bytes at 0x%08lx\n", offset);
309 return NULL;
310 }
311
312 if (!(pt = drm_ioremap_nocache(offset, size, dev))) {
Dave Airlief0c408b2006-01-02 19:52:09 +1100313 spin_lock(&drm_mem_lock);
314 ++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
315 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return NULL;
317 }
Dave Airlief0c408b2006-01-02 19:52:09 +1100318 spin_lock(&drm_mem_lock);
319 ++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
320 drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
321 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return pt;
323}
324
Dave Airlief0c408b2006-01-02 19:52:09 +1100325void drm_ioremapfree (void *pt, unsigned long size, drm_device_t * dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int alloc_count;
327 int free_count;
328
329 if (!pt)
330 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
331 "Attempt to free NULL pointer\n");
332 else
333 drm_ioremapfree(pt, size, dev);
334
Dave Airlief0c408b2006-01-02 19:52:09 +1100335 spin_lock(&drm_mem_lock);
336 drm_mem_stats[DRM_MEM_MAPPINGS].bytes_freed += size;
337 free_count = ++drm_mem_stats[DRM_MEM_MAPPINGS].free_count;
338 alloc_count = drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
339 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (free_count > alloc_count) {
341 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
342 "Excess frees: %d frees, %d allocs\n",
343 free_count, alloc_count);
344 }
345}
346
347#if __OS_HAS_AGP
348
Dave Airlief0c408b2006-01-02 19:52:09 +1100349DRM_AGP_MEM *drm_alloc_agp (drm_device_t *dev, int pages, u32 type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 DRM_AGP_MEM *handle;
351
352 if (!pages) {
353 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
354 return NULL;
355 }
356
Dave Airlief0c408b2006-01-02 19:52:09 +1100357 if ((handle = drm_agp_allocate_memory (pages, type))) {
358 spin_lock(&drm_mem_lock);
359 ++drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
360 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_allocated
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000361 += pages << PAGE_SHIFT;
Dave Airlief0c408b2006-01-02 19:52:09 +1100362 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return handle;
364 }
Dave Airlief0c408b2006-01-02 19:52:09 +1100365 spin_lock(&drm_mem_lock);
366 ++drm_mem_stats[DRM_MEM_TOTALAGP].fail_count;
367 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return NULL;
369}
370
Dave Airlief0c408b2006-01-02 19:52:09 +1100371int drm_free_agp (DRM_AGP_MEM * handle, int pages) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372 int alloc_count;
373 int free_count;
374 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (!handle) {
377 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
378 "Attempt to free NULL AGP handle\n");
379 return retval;
380 }
381
Dave Airlief0c408b2006-01-02 19:52:09 +1100382 if (drm_agp_free_memory (handle)) {
383 spin_lock(&drm_mem_lock);
384 free_count = ++drm_mem_stats[DRM_MEM_TOTALAGP].free_count;
385 alloc_count = drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
386 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_freed
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 += pages << PAGE_SHIFT;
Dave Airlief0c408b2006-01-02 19:52:09 +1100388 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (free_count > alloc_count) {
390 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
391 "Excess frees: %d frees, %d allocs\n",
392 free_count, alloc_count);
393 }
394 return 0;
395 }
396 return retval;
397}
398
Dave Airlief0c408b2006-01-02 19:52:09 +1100399int drm_bind_agp (DRM_AGP_MEM * handle, unsigned int start) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 int retcode = -EINVAL;
401
402 if (!handle) {
403 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
404 "Attempt to bind NULL AGP handle\n");
405 return retcode;
406 }
407
Dave Airlief0c408b2006-01-02 19:52:09 +1100408 if (!(retcode = drm_agp_bind_memory (handle, start))) {
409 spin_lock(&drm_mem_lock);
410 ++drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
411 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_allocated
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000412 += handle->page_count << PAGE_SHIFT;
Dave Airlief0c408b2006-01-02 19:52:09 +1100413 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return retcode;
415 }
Dave Airlief0c408b2006-01-02 19:52:09 +1100416 spin_lock(&drm_mem_lock);
417 ++drm_mem_stats[DRM_MEM_BOUNDAGP].fail_count;
418 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return retcode;
420}
421
Dave Airlief0c408b2006-01-02 19:52:09 +1100422int drm_unbind_agp (DRM_AGP_MEM * handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int alloc_count;
424 int free_count;
425 int retcode = -EINVAL;
426
427 if (!handle) {
428 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
429 "Attempt to unbind NULL AGP handle\n");
430 return retcode;
431 }
432
Dave Airlief0c408b2006-01-02 19:52:09 +1100433 if ((retcode = drm_agp_unbind_memory (handle)))
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000434 return retcode;
Dave Airlief0c408b2006-01-02 19:52:09 +1100435 spin_lock(&drm_mem_lock);
436 free_count = ++drm_mem_stats[DRM_MEM_BOUNDAGP].free_count;
437 alloc_count = drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
438 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_freed
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000439 += handle->page_count << PAGE_SHIFT;
Dave Airlief0c408b2006-01-02 19:52:09 +1100440 spin_unlock(&drm_mem_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (free_count > alloc_count) {
442 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
443 "Excess frees: %d frees, %d allocs\n",
444 free_count, alloc_count);
445 }
446 return retcode;
447}
448#endif