1 /* drm_memory.h -- Memory management wrappers for DRM -*- linux-c -*-
2 * Created: Thu Feb 4 14:00:34 1999 by faith@valinux.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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 * VA LINUX SYSTEMS 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
25 * OTHER DEALINGS IN THE SOFTWARE.
28 * Rickard E. (Rik) Faith <faith@valinux.com>
29 * Gareth Hughes <gareth@valinux.com>
32 #define __NO_VERSION__
33 #include <linux/config.h>
35 #include <linux/wrapper.h>
37 typedef struct drm_mem_stats {
42 unsigned long bytes_allocated;
43 unsigned long bytes_freed;
46 static spinlock_t DRM(mem_lock) = SPIN_LOCK_UNLOCKED;
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)[] = {
50 [DRM_MEM_DMA] = { "dmabufs" },
51 [DRM_MEM_SAREA] = { "sareas" },
52 [DRM_MEM_DRIVER] = { "driver" },
53 [DRM_MEM_MAGIC] = { "magic" },
54 [DRM_MEM_IOCTLS] = { "ioctltab" },
55 [DRM_MEM_MAPS] = { "maplist" },
56 [DRM_MEM_VMAS] = { "vmalist" },
57 [DRM_MEM_BUFS] = { "buflist" },
58 [DRM_MEM_SEGS] = { "seglist" },
59 [DRM_MEM_PAGES] = { "pagelist" },
60 [DRM_MEM_FILES] = { "files" },
61 [DRM_MEM_QUEUES] = { "queues" },
62 [DRM_MEM_CMDS] = { "commands" },
63 [DRM_MEM_MAPPINGS] = { "mappings" },
64 [DRM_MEM_BUFLISTS] = { "buflists" },
65 [DRM_MEM_AGPLISTS] = { "agplist" },
66 [DRM_MEM_SGLISTS] = { "sglist" },
67 [DRM_MEM_TOTALAGP] = { "totalagp" },
68 [DRM_MEM_BOUNDAGP] = { "boundagp" },
69 [DRM_MEM_CTXBITMAP] = { "ctxbitmap"},
70 [DRM_MEM_STUB] = { "stub" },
71 { NULL, 0, } /* Last entry must be null */
74 void DRM(mem_init)(void)
79 for (mem = DRM(mem_stats); mem->name; ++mem) {
80 mem->succeed_count = 0;
83 mem->bytes_allocated = 0;
88 #if LINUX_VERSION_CODE < 0x020317
89 /* Changed to page count in 2.3.23 */
90 DRM(ram_available) = si.totalram >> PAGE_SHIFT;
92 DRM(ram_available) = si.totalram;
97 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
99 static int DRM(_mem_info)(char *buf, char **start, off_t offset,
100 int request, int *eof, void *data)
105 if (offset > DRM_PROC_LIMIT) {
111 *start = &buf[offset];
113 DRM_PROC_PRINT(" total counts "
114 " | outstanding \n");
115 DRM_PROC_PRINT("type alloc freed fail bytes freed"
116 " | allocs bytes\n\n");
117 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
119 DRM(ram_available) << (PAGE_SHIFT - 10));
120 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
121 "locked", 0, 0, 0, DRM(ram_used) >> 10);
122 DRM_PROC_PRINT("\n");
123 for (pt = DRM(mem_stats); pt->name; pt++) {
124 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
131 pt->succeed_count - pt->free_count,
132 (long)pt->bytes_allocated
133 - (long)pt->bytes_freed);
136 if (len > request + offset) return request;
141 int DRM(mem_info)(char *buf, char **start, off_t offset,
142 int len, int *eof, void *data)
146 spin_lock(&DRM(mem_lock));
147 ret = DRM(_mem_info)(buf, start, offset, len, eof, data);
148 spin_unlock(&DRM(mem_lock));
152 void *DRM(alloc)(size_t size, int area)
157 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
161 if (!(pt = kmalloc(size, GFP_KERNEL))) {
162 spin_lock(&DRM(mem_lock));
163 ++DRM(mem_stats)[area].fail_count;
164 spin_unlock(&DRM(mem_lock));
167 spin_lock(&DRM(mem_lock));
168 ++DRM(mem_stats)[area].succeed_count;
169 DRM(mem_stats)[area].bytes_allocated += size;
170 spin_unlock(&DRM(mem_lock));
174 void *DRM(realloc)(void *oldpt, size_t oldsize, size_t size, int area)
178 if (!(pt = DRM(alloc)(size, area))) return NULL;
179 if (oldpt && oldsize) {
180 memcpy(pt, oldpt, oldsize);
181 DRM(free)(oldpt, oldsize, area);
186 char *DRM(strdup)(const char *s, int area)
189 int length = s ? strlen(s) : 0;
191 if (!(pt = DRM(alloc)(length+1, area))) return NULL;
196 void DRM(strfree)(const char *s, int area)
202 size = 1 + (s ? strlen(s) : 0);
203 DRM(free)((void *)s, size, area);
206 void DRM(free)(void *pt, size_t size, int area)
211 if (!pt) DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
213 spin_lock(&DRM(mem_lock));
214 DRM(mem_stats)[area].bytes_freed += size;
215 free_count = ++DRM(mem_stats)[area].free_count;
216 alloc_count = DRM(mem_stats)[area].succeed_count;
217 spin_unlock(&DRM(mem_lock));
218 if (free_count > alloc_count) {
219 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
220 free_count, alloc_count);
224 unsigned long DRM(alloc_pages)(int order, int area)
226 unsigned long address;
227 unsigned long bytes = PAGE_SIZE << order;
231 spin_lock(&DRM(mem_lock));
232 if ((DRM(ram_used) >> PAGE_SHIFT)
233 > (DRM_RAM_PERCENT * DRM(ram_available)) / 100) {
234 spin_unlock(&DRM(mem_lock));
237 spin_unlock(&DRM(mem_lock));
239 address = __get_free_pages(GFP_KERNEL, order);
241 spin_lock(&DRM(mem_lock));
242 ++DRM(mem_stats)[area].fail_count;
243 spin_unlock(&DRM(mem_lock));
246 spin_lock(&DRM(mem_lock));
247 ++DRM(mem_stats)[area].succeed_count;
248 DRM(mem_stats)[area].bytes_allocated += bytes;
249 DRM(ram_used) += bytes;
250 spin_unlock(&DRM(mem_lock));
253 /* Zero outside the lock */
254 memset((void *)address, 0, bytes);
257 for (addr = address, sz = bytes;
259 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
260 #if LINUX_VERSION_CODE >= 0x020400
261 /* Argument type changed in 2.4.0-test6/pre8 */
262 mem_map_reserve(virt_to_page(addr));
264 mem_map_reserve(MAP_NR(addr));
271 void DRM(free_pages)(unsigned long address, int order, int area)
273 unsigned long bytes = PAGE_SIZE << order;
280 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
283 for (addr = address, sz = bytes;
285 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
286 #if LINUX_VERSION_CODE >= 0x020400
287 /* Argument type changed in 2.4.0-test6/pre8 */
288 mem_map_unreserve(virt_to_page(addr));
290 mem_map_unreserve(MAP_NR(addr));
293 free_pages(address, order);
296 spin_lock(&DRM(mem_lock));
297 free_count = ++DRM(mem_stats)[area].free_count;
298 alloc_count = DRM(mem_stats)[area].succeed_count;
299 DRM(mem_stats)[area].bytes_freed += bytes;
300 DRM(ram_used) -= bytes;
301 spin_unlock(&DRM(mem_lock));
302 if (free_count > alloc_count) {
304 "Excess frees: %d frees, %d allocs\n",
305 free_count, alloc_count);
309 void *DRM(ioremap)(unsigned long offset, unsigned long size)
314 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
315 "Mapping 0 bytes at 0x%08lx\n", offset);
319 if (!(pt = ioremap(offset, size))) {
320 spin_lock(&DRM(mem_lock));
321 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
322 spin_unlock(&DRM(mem_lock));
325 spin_lock(&DRM(mem_lock));
326 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
327 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += size;
328 spin_unlock(&DRM(mem_lock));
332 void DRM(ioremapfree)(void *pt, unsigned long size)
338 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
339 "Attempt to free NULL pointer\n");
343 spin_lock(&DRM(mem_lock));
344 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_freed += size;
345 free_count = ++DRM(mem_stats)[DRM_MEM_MAPPINGS].free_count;
346 alloc_count = DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
347 spin_unlock(&DRM(mem_lock));
348 if (free_count > alloc_count) {
349 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
350 "Excess frees: %d frees, %d allocs\n",
351 free_count, alloc_count);
355 #if __REALLY_HAVE_AGP
357 agp_memory *DRM(alloc_agp)(int pages, u32 type)
362 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
366 if ((handle = DRM(agp_allocate_memory)(pages, type))) {
367 spin_lock(&DRM(mem_lock));
368 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
369 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_allocated
370 += pages << PAGE_SHIFT;
371 spin_unlock(&DRM(mem_lock));
374 spin_lock(&DRM(mem_lock));
375 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].fail_count;
376 spin_unlock(&DRM(mem_lock));
380 int DRM(free_agp)(agp_memory *handle, int pages)
384 int retval = -EINVAL;
387 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
388 "Attempt to free NULL AGP handle\n");
392 if (DRM(agp_free_memory)(handle)) {
393 spin_lock(&DRM(mem_lock));
394 free_count = ++DRM(mem_stats)[DRM_MEM_TOTALAGP].free_count;
395 alloc_count = DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
396 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_freed
397 += pages << PAGE_SHIFT;
398 spin_unlock(&DRM(mem_lock));
399 if (free_count > alloc_count) {
400 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
401 "Excess frees: %d frees, %d allocs\n",
402 free_count, alloc_count);
409 int DRM(bind_agp)(agp_memory *handle, unsigned int start)
411 int retcode = -EINVAL;
414 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
415 "Attempt to bind NULL AGP handle\n");
419 if (!(retcode = DRM(agp_bind_memory)(handle, start))) {
420 spin_lock(&DRM(mem_lock));
421 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
422 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_allocated
423 += handle->page_count << PAGE_SHIFT;
424 spin_unlock(&DRM(mem_lock));
427 spin_lock(&DRM(mem_lock));
428 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].fail_count;
429 spin_unlock(&DRM(mem_lock));
433 int DRM(unbind_agp)(agp_memory *handle)
437 int retcode = -EINVAL;
440 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
441 "Attempt to unbind NULL AGP handle\n");
445 if ((retcode = DRM(agp_unbind_memory)(handle))) return retcode;
446 spin_lock(&DRM(mem_lock));
447 free_count = ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].free_count;
448 alloc_count = DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
449 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_freed
450 += handle->page_count << PAGE_SHIFT;
451 spin_unlock(&DRM(mem_lock));
452 if (free_count > alloc_count) {
453 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
454 "Excess frees: %d frees, %d allocs\n",
455 free_count, alloc_count);