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_TOTALAGP] = { "totalagp" },
67 [DRM_MEM_BOUNDAGP] = { "boundagp" },
68 [DRM_MEM_CTXBITMAP] = { "ctxbitmap"},
69 [DRM_MEM_STUB] = { "stub" },
70 { NULL, 0, } /* Last entry must be null */
73 void DRM(mem_init)(void)
78 for (mem = DRM(mem_stats); mem->name; ++mem) {
79 mem->succeed_count = 0;
82 mem->bytes_allocated = 0;
87 #if LINUX_VERSION_CODE < 0x020317
88 /* Changed to page count in 2.3.23 */
89 DRM(ram_available) = si.totalram >> PAGE_SHIFT;
91 DRM(ram_available) = si.totalram;
96 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
98 static int DRM(_mem_info)(char *buf, char **start, off_t offset,
99 int request, int *eof, void *data)
104 if (offset > DRM_PROC_LIMIT) {
110 *start = &buf[offset];
112 DRM_PROC_PRINT(" total counts "
113 " | outstanding \n");
114 DRM_PROC_PRINT("type alloc freed fail bytes freed"
115 " | allocs bytes\n\n");
116 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
118 DRM(ram_available) << (PAGE_SHIFT - 10));
119 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
120 "locked", 0, 0, 0, DRM(ram_used) >> 10);
121 DRM_PROC_PRINT("\n");
122 for (pt = DRM(mem_stats); pt->name; pt++) {
123 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
130 pt->succeed_count - pt->free_count,
131 (long)pt->bytes_allocated
132 - (long)pt->bytes_freed);
135 if (len > request + offset) return request;
140 int DRM(mem_info)(char *buf, char **start, off_t offset,
141 int len, int *eof, void *data)
145 spin_lock(&DRM(mem_lock));
146 ret = DRM(_mem_info)(buf, start, offset, len, eof, data);
147 spin_unlock(&DRM(mem_lock));
151 void *DRM(alloc)(size_t size, int area)
156 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
160 if (!(pt = kmalloc(size, GFP_KERNEL))) {
161 spin_lock(&DRM(mem_lock));
162 ++DRM(mem_stats)[area].fail_count;
163 spin_unlock(&DRM(mem_lock));
166 spin_lock(&DRM(mem_lock));
167 ++DRM(mem_stats)[area].succeed_count;
168 DRM(mem_stats)[area].bytes_allocated += size;
169 spin_unlock(&DRM(mem_lock));
173 void *DRM(realloc)(void *oldpt, size_t oldsize, size_t size, int area)
177 if (!(pt = DRM(alloc)(size, area))) return NULL;
178 if (oldpt && oldsize) {
179 memcpy(pt, oldpt, oldsize);
180 DRM(free)(oldpt, oldsize, area);
185 char *DRM(strdup)(const char *s, int area)
188 int length = s ? strlen(s) : 0;
190 if (!(pt = DRM(alloc)(length+1, area))) return NULL;
195 void DRM(strfree)(const char *s, int area)
201 size = 1 + (s ? strlen(s) : 0);
202 DRM(free)((void *)s, size, area);
205 void DRM(free)(void *pt, size_t size, int area)
210 if (!pt) DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
212 spin_lock(&DRM(mem_lock));
213 DRM(mem_stats)[area].bytes_freed += size;
214 free_count = ++DRM(mem_stats)[area].free_count;
215 alloc_count = DRM(mem_stats)[area].succeed_count;
216 spin_unlock(&DRM(mem_lock));
217 if (free_count > alloc_count) {
218 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
219 free_count, alloc_count);
223 unsigned long DRM(alloc_pages)(int order, int area)
225 unsigned long address;
226 unsigned long bytes = PAGE_SIZE << order;
230 spin_lock(&DRM(mem_lock));
231 if ((DRM(ram_used) >> PAGE_SHIFT)
232 > (DRM_RAM_PERCENT * DRM(ram_available)) / 100) {
233 spin_unlock(&DRM(mem_lock));
236 spin_unlock(&DRM(mem_lock));
238 address = __get_free_pages(GFP_KERNEL, order);
240 spin_lock(&DRM(mem_lock));
241 ++DRM(mem_stats)[area].fail_count;
242 spin_unlock(&DRM(mem_lock));
245 spin_lock(&DRM(mem_lock));
246 ++DRM(mem_stats)[area].succeed_count;
247 DRM(mem_stats)[area].bytes_allocated += bytes;
248 DRM(ram_used) += bytes;
249 spin_unlock(&DRM(mem_lock));
252 /* Zero outside the lock */
253 memset((void *)address, 0, bytes);
256 for (addr = address, sz = bytes;
258 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
259 #if LINUX_VERSION_CODE >= 0x020400
260 /* Argument type changed in 2.4.0-test6/pre8 */
261 mem_map_reserve(virt_to_page(addr));
263 mem_map_reserve(MAP_NR(addr));
270 void DRM(free_pages)(unsigned long address, int order, int area)
272 unsigned long bytes = PAGE_SIZE << order;
279 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
282 for (addr = address, sz = bytes;
284 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
285 #if LINUX_VERSION_CODE >= 0x020400
286 /* Argument type changed in 2.4.0-test6/pre8 */
287 mem_map_unreserve(virt_to_page(addr));
289 mem_map_unreserve(MAP_NR(addr));
292 free_pages(address, order);
295 spin_lock(&DRM(mem_lock));
296 free_count = ++DRM(mem_stats)[area].free_count;
297 alloc_count = DRM(mem_stats)[area].succeed_count;
298 DRM(mem_stats)[area].bytes_freed += bytes;
299 DRM(ram_used) -= bytes;
300 spin_unlock(&DRM(mem_lock));
301 if (free_count > alloc_count) {
303 "Excess frees: %d frees, %d allocs\n",
304 free_count, alloc_count);
308 void *DRM(ioremap)(unsigned long offset, unsigned long size)
313 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
314 "Mapping 0 bytes at 0x%08lx\n", offset);
318 if (!(pt = ioremap(offset, size))) {
319 spin_lock(&DRM(mem_lock));
320 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
321 spin_unlock(&DRM(mem_lock));
324 spin_lock(&DRM(mem_lock));
325 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
326 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += size;
327 spin_unlock(&DRM(mem_lock));
331 void DRM(ioremapfree)(void *pt, unsigned long size)
337 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
338 "Attempt to free NULL pointer\n");
342 spin_lock(&DRM(mem_lock));
343 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_freed += size;
344 free_count = ++DRM(mem_stats)[DRM_MEM_MAPPINGS].free_count;
345 alloc_count = DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
346 spin_unlock(&DRM(mem_lock));
347 if (free_count > alloc_count) {
348 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
349 "Excess frees: %d frees, %d allocs\n",
350 free_count, alloc_count);
354 #if __REALLY_HAVE_AGP
356 agp_memory *DRM(alloc_agp)(int pages, u32 type)
361 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
365 if ((handle = DRM(agp_allocate_memory)(pages, type))) {
366 spin_lock(&DRM(mem_lock));
367 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
368 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_allocated
369 += pages << PAGE_SHIFT;
370 spin_unlock(&DRM(mem_lock));
373 spin_lock(&DRM(mem_lock));
374 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].fail_count;
375 spin_unlock(&DRM(mem_lock));
379 int DRM(free_agp)(agp_memory *handle, int pages)
383 int retval = -EINVAL;
386 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
387 "Attempt to free NULL AGP handle\n");
391 if (DRM(agp_free_memory)(handle)) {
392 spin_lock(&DRM(mem_lock));
393 free_count = ++DRM(mem_stats)[DRM_MEM_TOTALAGP].free_count;
394 alloc_count = DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
395 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_freed
396 += pages << PAGE_SHIFT;
397 spin_unlock(&DRM(mem_lock));
398 if (free_count > alloc_count) {
399 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
400 "Excess frees: %d frees, %d allocs\n",
401 free_count, alloc_count);
408 int DRM(bind_agp)(agp_memory *handle, unsigned int start)
410 int retcode = -EINVAL;
413 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
414 "Attempt to bind NULL AGP handle\n");
418 if (!(retcode = DRM(agp_bind_memory)(handle, start))) {
419 spin_lock(&DRM(mem_lock));
420 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
421 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_allocated
422 += handle->page_count << PAGE_SHIFT;
423 spin_unlock(&DRM(mem_lock));
426 spin_lock(&DRM(mem_lock));
427 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].fail_count;
428 spin_unlock(&DRM(mem_lock));
432 int DRM(unbind_agp)(agp_memory *handle)
436 int retcode = -EINVAL;
439 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
440 "Attempt to unbind NULL AGP handle\n");
444 if ((retcode = DRM(agp_unbind_memory)(handle))) return retcode;
445 spin_lock(&DRM(mem_lock));
446 free_count = ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].free_count;
447 alloc_count = DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
448 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_freed
449 += handle->page_count << PAGE_SHIFT;
450 spin_unlock(&DRM(mem_lock));
451 if (free_count > alloc_count) {
452 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
453 "Excess frees: %d frees, %d allocs\n",
454 free_count, alloc_count);