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>
34 #define __NO_VERSION__
36 #include <linux/config.h>
37 #endif /* __linux__ */
38 #include "dev/drm/drmP.h"
40 #include <linux/wrapper.h>
41 #endif /* __linux__ */
46 #include <sys/agpio.h>
49 #define malloctype DRM(M_DRM)
50 /* The macros confliced in the MALLOC_DEFINE */
52 MALLOC_DEFINE(malloctype, "drm", "DRM Data Structures");
55 #endif /* __FreeBSD__ */
57 typedef struct drm_mem_stats {
62 unsigned long bytes_allocated;
63 unsigned long bytes_freed;
67 static spinlock_t DRM(mem_lock) = SPIN_LOCK_UNLOCKED;
68 #endif /* __linux__ */
70 static DRM_OS_SPINTYPE DRM(mem_lock);
71 #endif /* __FreeBSD__ */
72 static unsigned long DRM(ram_available) = 0; /* In pages */
73 static unsigned long DRM(ram_used) = 0;
74 static drm_mem_stats_t DRM(mem_stats)[] = {
75 [DRM_MEM_DMA] = { "dmabufs" },
76 [DRM_MEM_SAREA] = { "sareas" },
77 [DRM_MEM_DRIVER] = { "driver" },
78 [DRM_MEM_MAGIC] = { "magic" },
79 [DRM_MEM_IOCTLS] = { "ioctltab" },
80 [DRM_MEM_MAPS] = { "maplist" },
81 [DRM_MEM_VMAS] = { "vmalist" },
82 [DRM_MEM_BUFS] = { "buflist" },
83 [DRM_MEM_SEGS] = { "seglist" },
84 [DRM_MEM_PAGES] = { "pagelist" },
85 [DRM_MEM_FILES] = { "files" },
86 [DRM_MEM_QUEUES] = { "queues" },
87 [DRM_MEM_CMDS] = { "commands" },
88 [DRM_MEM_MAPPINGS] = { "mappings" },
89 [DRM_MEM_BUFLISTS] = { "buflists" },
90 [DRM_MEM_AGPLISTS] = { "agplist" },
91 [DRM_MEM_SGLISTS] = { "sglist" },
92 [DRM_MEM_TOTALAGP] = { "totalagp" },
93 [DRM_MEM_BOUNDAGP] = { "boundagp" },
94 [DRM_MEM_CTXBITMAP] = { "ctxbitmap"},
95 [DRM_MEM_STUB] = { "stub" },
96 { NULL, 0, } /* Last entry must be null */
99 void DRM(mem_init)(void)
101 drm_mem_stats_t *mem;
104 #endif /* __linux__ */
107 DRM_OS_SPININIT(DRM(mem_lock), "drm memory");
108 #endif /* __FreeBSD__ */
110 for (mem = DRM(mem_stats); mem->name; ++mem) {
111 mem->succeed_count = 0;
114 mem->bytes_allocated = 0;
115 mem->bytes_freed = 0;
120 DRM(ram_available) = si.totalram;
121 #endif /* __linux__ */
123 DRM(ram_available) = 0; /* si.totalram */
124 #endif /* __FreeBSD__ */
128 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
130 static int DRM(_mem_info)(char *buf, char **start, off_t offset,
131 int request, int *eof, void *data)
136 if (offset > DRM_PROC_LIMIT) {
142 *start = &buf[offset];
144 DRM_PROC_PRINT(" total counts "
145 " | outstanding \n");
146 DRM_PROC_PRINT("type alloc freed fail bytes freed"
147 " | allocs bytes\n\n");
148 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
150 DRM(ram_available) << (PAGE_SHIFT - 10));
151 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
152 "locked", 0, 0, 0, DRM(ram_used) >> 10);
153 DRM_PROC_PRINT("\n");
154 for (pt = DRM(mem_stats); pt->name; pt++) {
155 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
162 pt->succeed_count - pt->free_count,
163 (long)pt->bytes_allocated
164 - (long)pt->bytes_freed);
167 if (len > request + offset) return request;
171 #endif /* __linux__ */
174 static int DRM(_mem_info) DRM_SYSCTL_HANDLER_ARGS
180 DRM_SYSCTL_PRINT(" total counts "
181 " | outstanding \n");
182 DRM_SYSCTL_PRINT("type alloc freed fail bytes freed"
183 " | allocs bytes\n\n");
184 DRM_SYSCTL_PRINT("%-9.9s %5d %5d %4d %10lu |\n",
185 "system", 0, 0, 0, DRM(ram_available));
186 DRM_SYSCTL_PRINT("%-9.9s %5d %5d %4d %10lu |\n",
187 "locked", 0, 0, 0, DRM(ram_used));
188 DRM_SYSCTL_PRINT("\n");
189 for (pt = DRM(mem_stats); pt->name; pt++) {
190 DRM_SYSCTL_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
197 pt->succeed_count - pt->free_count,
198 (long)pt->bytes_allocated
199 - (long)pt->bytes_freed);
201 SYSCTL_OUT(req, "", 1);
205 #endif /* __FreeBSD__ */
208 int DRM(mem_info)(char *buf, char **start, off_t offset,
209 int len, int *eof, void *data)
210 #endif /* __linux__ */
212 int DRM(mem_info) DRM_SYSCTL_HANDLER_ARGS
213 #endif /* __FreeBSD__ */
217 DRM_OS_SPINLOCK(&DRM(mem_lock));
219 ret = DRM(_mem_info)(buf, start, offset, len, eof, data);
220 #endif /* __linux__ */
222 ret = DRM(_mem_info)(oidp, arg1, arg2, req);
223 #endif /* __FreeBSD__ */
224 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
228 void *DRM(alloc)(size_t size, int area)
233 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
238 if (!(pt = kmalloc(size, GFP_KERNEL))) {
239 #endif /* __linux__ */
241 if (!(pt = malloc(size, DRM(M_DRM), M_NOWAIT))) {
242 #endif /* __FreeBSD__ */
243 DRM_OS_SPINLOCK(&DRM(mem_lock));
244 ++DRM(mem_stats)[area].fail_count;
245 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
248 DRM_OS_SPINLOCK(&DRM(mem_lock));
249 ++DRM(mem_stats)[area].succeed_count;
250 DRM(mem_stats)[area].bytes_allocated += size;
251 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
255 void *DRM(realloc)(void *oldpt, size_t oldsize, size_t size, int area)
259 if (!(pt = DRM(alloc)(size, area))) return NULL;
260 if (oldpt && oldsize) {
261 memcpy(pt, oldpt, oldsize);
262 DRM(free)(oldpt, oldsize, area);
267 char *DRM(strdup)(const char *s, int area)
270 int length = s ? strlen(s) : 0;
272 if (!(pt = DRM(alloc)(length+1, area))) return NULL;
277 void DRM(strfree)(char *s, int area)
283 size = 1 + strlen(s);
284 DRM(free)((void *)s, size, area);
287 void DRM(free)(void *pt, size_t size, int area)
292 if (!pt) DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
295 #endif /* __linux__ */
297 else free(pt, DRM(M_DRM));
298 #endif /* __FreeBSD__ */
299 DRM_OS_SPINLOCK(&DRM(mem_lock));
300 DRM(mem_stats)[area].bytes_freed += size;
301 free_count = ++DRM(mem_stats)[area].free_count;
302 alloc_count = DRM(mem_stats)[area].succeed_count;
303 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
304 if (free_count > alloc_count) {
305 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
306 free_count, alloc_count);
310 unsigned long DRM(alloc_pages)(int order, int area)
313 unsigned long address;
316 #endif /* __linux__ */
319 #endif /* __FreeBSD__ */
320 unsigned long bytes = PAGE_SIZE << order;
323 DRM_OS_SPINLOCK(&DRM(mem_lock));
324 if ((DRM(ram_used) >> PAGE_SHIFT)
325 > (DRM_RAM_PERCENT * DRM(ram_available)) / 100) {
326 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
329 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
330 #endif /* __linux__ */
333 address = __get_free_pages(GFP_KERNEL, order);
334 #endif /* __linux__ */
336 address = (vm_offset_t) contigmalloc(bytes, DRM(M_DRM), M_WAITOK, 0, ~0, 1, 0);
337 #endif /* __FreeBSD__ */
339 DRM_OS_SPINLOCK(&DRM(mem_lock));
340 ++DRM(mem_stats)[area].fail_count;
341 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
344 DRM_OS_SPINLOCK(&DRM(mem_lock));
345 ++DRM(mem_stats)[area].succeed_count;
346 DRM(mem_stats)[area].bytes_allocated += bytes;
347 DRM(ram_used) += bytes;
348 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
351 /* Zero outside the lock */
352 memset((void *)address, 0, bytes);
356 for (addr = address, sz = bytes;
358 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
359 mem_map_reserve(virt_to_page(addr));
361 #endif /* __linux__ */
366 void DRM(free_pages)(unsigned long address, int order, int area)
368 unsigned long bytes = PAGE_SIZE << order;
373 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
379 for (addr = address, sz = bytes;
381 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
382 mem_map_unreserve(virt_to_page(addr));
384 free_pages(address, order);
385 #endif /* __linux__ */
387 contigfree((void *) address, bytes, DRM(M_DRM));
388 #endif /* __FreeBSD__ */
391 DRM_OS_SPINLOCK(&DRM(mem_lock));
392 free_count = ++DRM(mem_stats)[area].free_count;
393 alloc_count = DRM(mem_stats)[area].succeed_count;
394 DRM(mem_stats)[area].bytes_freed += bytes;
395 DRM(ram_used) -= bytes;
396 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
397 if (free_count > alloc_count) {
399 "Excess frees: %d frees, %d allocs\n",
400 free_count, alloc_count);
404 void *DRM(ioremap)(unsigned long offset, unsigned long size)
409 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
410 "Mapping 0 bytes at 0x%08lx\n", offset);
415 if (!(pt = ioremap(offset, size))) {
416 #endif /* __linux__ */
418 if (!(pt = pmap_mapdev(offset, size))) {
419 #endif /* __FreeBSD__ */
420 DRM_OS_SPINLOCK(&DRM(mem_lock));
421 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
422 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
425 DRM_OS_SPINLOCK(&DRM(mem_lock));
426 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
427 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += size;
428 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
432 void DRM(ioremapfree)(void *pt, unsigned long size)
438 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
439 "Attempt to free NULL pointer\n");
443 #endif /* __linux__ */
445 pmap_unmapdev((vm_offset_t) pt, size);
446 #endif /* __FreeBSD__ */
448 DRM_OS_SPINLOCK(&DRM(mem_lock));
449 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_freed += size;
450 free_count = ++DRM(mem_stats)[DRM_MEM_MAPPINGS].free_count;
451 alloc_count = DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
452 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
453 if (free_count > alloc_count) {
454 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
455 "Excess frees: %d frees, %d allocs\n",
456 free_count, alloc_count);
460 #if __REALLY_HAVE_AGP
461 agp_memory *DRM(alloc_agp)(int pages, u32 type)
466 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
470 if ((handle = DRM(agp_allocate_memory)(pages, type))) {
471 DRM_OS_SPINLOCK(&DRM(mem_lock));
472 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
473 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_allocated
474 += pages << PAGE_SHIFT;
475 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
478 DRM_OS_SPINLOCK(&DRM(mem_lock));
479 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].fail_count;
480 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
484 int DRM(free_agp)(agp_memory *handle, int pages)
490 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
491 "Attempt to free NULL AGP handle\n");
492 return DRM_OS_ERR(EINVAL);
495 if (DRM(agp_free_memory)(handle)) {
496 DRM_OS_SPINLOCK(&DRM(mem_lock));
497 free_count = ++DRM(mem_stats)[DRM_MEM_TOTALAGP].free_count;
498 alloc_count = DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
499 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_freed
500 += pages << PAGE_SHIFT;
501 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
502 if (free_count > alloc_count) {
503 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
504 "Excess frees: %d frees, %d allocs\n",
505 free_count, alloc_count);
509 return DRM_OS_ERR(EINVAL);
512 int DRM(bind_agp)(agp_memory *handle, unsigned int start)
516 device_t dev = agp_find_device();
517 struct agp_memory_info info;
520 return DRM_OS_ERR(EINVAL);
521 #endif /* __FreeBSD__ */
524 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
525 "Attempt to bind NULL AGP handle\n");
526 return DRM_OS_ERR(EINVAL);
529 if (!(retcode = DRM(agp_bind_memory)(handle, start))) {
530 DRM_OS_SPINLOCK(&DRM(mem_lock));
531 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
533 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_allocated
534 += handle->page_count << PAGE_SHIFT;
535 #endif /* __linux__ */
537 agp_memory_info(dev, handle, &info);
538 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_allocated
540 #endif /* __FreeBSD__ */
541 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
544 DRM_OS_SPINLOCK(&DRM(mem_lock));
545 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].fail_count;
546 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
550 int DRM(unbind_agp)(agp_memory *handle)
554 int retcode = DRM_OS_ERR(EINVAL);
556 device_t dev = agp_find_device();
557 struct agp_memory_info info;
560 return DRM_OS_ERR(EINVAL);
561 #endif /* __FreeBSD__ */
564 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
565 "Attempt to unbind NULL AGP handle\n");
570 agp_memory_info(dev, handle, &info);
571 #endif /* __FreeBSD__ */
573 if ((retcode = DRM(agp_unbind_memory)(handle)))
576 DRM_OS_SPINLOCK(&DRM(mem_lock));
577 free_count = ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].free_count;
578 alloc_count = DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
580 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_freed
581 += handle->page_count << PAGE_SHIFT;
582 #endif /* __linux__ */
584 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_freed
586 #endif /* __FreeBSD__ */
587 DRM_OS_SPINUNLOCK(&DRM(mem_lock));
588 if (free_count > alloc_count) {
589 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
590 "Excess frees: %d frees, %d allocs\n",
591 free_count, alloc_count);