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 ((handle = drm_agp_allocate_memory(dev->agp->bridge, pages, type))) {
304 spin_lock(&drm_mem_lock);
305 ++drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
306 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_allocated
307 += pages << PAGE_SHIFT;
308 spin_unlock(&drm_mem_lock);
311 spin_lock(&drm_mem_lock);
312 ++drm_mem_stats[DRM_MEM_TOTALAGP].fail_count;
313 spin_unlock(&drm_mem_lock);
317 int drm_free_agp(DRM_AGP_MEM * handle, int pages)
321 int retval = -EINVAL;
324 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
325 "Attempt to free NULL AGP handle\n");
329 if (drm_agp_free_memory(handle)) {
330 spin_lock(&drm_mem_lock);
331 free_count = ++drm_mem_stats[DRM_MEM_TOTALAGP].free_count;
332 alloc_count = drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
333 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_freed
334 += pages << PAGE_SHIFT;
335 spin_unlock(&drm_mem_lock);
336 if (free_count > alloc_count) {
337 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
338 "Excess frees: %d frees, %d allocs\n",
339 free_count, alloc_count);
346 int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
348 int retcode = -EINVAL;
351 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
352 "Attempt to bind NULL AGP handle\n");
356 if (!(retcode = drm_agp_bind_memory(handle, start))) {
357 spin_lock(&drm_mem_lock);
358 ++drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
359 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_allocated
360 += handle->page_count << PAGE_SHIFT;
361 spin_unlock(&drm_mem_lock);
364 spin_lock(&drm_mem_lock);
365 ++drm_mem_stats[DRM_MEM_BOUNDAGP].fail_count;
366 spin_unlock(&drm_mem_lock);
370 int drm_unbind_agp(DRM_AGP_MEM * handle)
374 int retcode = -EINVAL;
377 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
378 "Attempt to unbind NULL AGP handle\n");
382 if ((retcode = drm_agp_unbind_memory(handle)))
384 spin_lock(&drm_mem_lock);
385 free_count = ++drm_mem_stats[DRM_MEM_BOUNDAGP].free_count;
386 alloc_count = drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
387 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_freed
388 += handle->page_count << PAGE_SHIFT;
389 spin_unlock(&drm_mem_lock);
390 if (free_count > alloc_count) {
391 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
392 "Excess frees: %d frees, %d allocs\n",
393 free_count, alloc_count);