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 DRM(ram_available) = si.totalram;
92 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
94 static int DRM(_mem_info)(char *buf, char **start, off_t offset,
95 int request, int *eof, void *data)
100 if (offset > DRM_PROC_LIMIT) {
106 *start = &buf[offset];
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",
114 DRM(ram_available) << (PAGE_SHIFT - 10));
115 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
116 "locked", 0, 0, 0, DRM(ram_used) >> 10);
117 DRM_PROC_PRINT("\n");
118 for (pt = DRM(mem_stats); pt->name; pt++) {
119 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
126 pt->succeed_count - pt->free_count,
127 (long)pt->bytes_allocated
128 - (long)pt->bytes_freed);
131 if (len > request + offset) return request;
136 int DRM(mem_info)(char *buf, char **start, off_t offset,
137 int len, int *eof, void *data)
141 spin_lock(&DRM(mem_lock));
142 ret = DRM(_mem_info)(buf, start, offset, len, eof, data);
143 spin_unlock(&DRM(mem_lock));
147 void *DRM(alloc)(size_t size, int area)
152 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
156 if (!(pt = kmalloc(size, GFP_KERNEL))) {
157 spin_lock(&DRM(mem_lock));
158 ++DRM(mem_stats)[area].fail_count;
159 spin_unlock(&DRM(mem_lock));
162 spin_lock(&DRM(mem_lock));
163 ++DRM(mem_stats)[area].succeed_count;
164 DRM(mem_stats)[area].bytes_allocated += size;
165 spin_unlock(&DRM(mem_lock));
169 void *DRM(realloc)(void *oldpt, size_t oldsize, size_t size, int area)
173 if (!(pt = DRM(alloc)(size, area))) return NULL;
174 if (oldpt && oldsize) {
175 memcpy(pt, oldpt, oldsize);
176 DRM(free)(oldpt, oldsize, area);
181 void DRM(free)(void *pt, size_t size, int area)
186 if (!pt) DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
188 spin_lock(&DRM(mem_lock));
189 DRM(mem_stats)[area].bytes_freed += size;
190 free_count = ++DRM(mem_stats)[area].free_count;
191 alloc_count = DRM(mem_stats)[area].succeed_count;
192 spin_unlock(&DRM(mem_lock));
193 if (free_count > alloc_count) {
194 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
195 free_count, alloc_count);
199 unsigned long DRM(alloc_pages)(int order, int area)
201 unsigned long address;
202 unsigned long bytes = PAGE_SIZE << order;
206 spin_lock(&DRM(mem_lock));
207 if ((DRM(ram_used) >> PAGE_SHIFT)
208 > (DRM_RAM_PERCENT * DRM(ram_available)) / 100) {
209 spin_unlock(&DRM(mem_lock));
212 spin_unlock(&DRM(mem_lock));
214 address = __get_free_pages(GFP_KERNEL, order);
216 spin_lock(&DRM(mem_lock));
217 ++DRM(mem_stats)[area].fail_count;
218 spin_unlock(&DRM(mem_lock));
221 spin_lock(&DRM(mem_lock));
222 ++DRM(mem_stats)[area].succeed_count;
223 DRM(mem_stats)[area].bytes_allocated += bytes;
224 DRM(ram_used) += bytes;
225 spin_unlock(&DRM(mem_lock));
228 /* Zero outside the lock */
229 memset((void *)address, 0, bytes);
232 for (addr = address, sz = bytes;
234 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
235 mem_map_reserve(virt_to_page(addr));
241 void DRM(free_pages)(unsigned long address, int order, int area)
243 unsigned long bytes = PAGE_SIZE << order;
250 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
253 for (addr = address, sz = bytes;
255 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
256 mem_map_unreserve(virt_to_page(addr));
258 free_pages(address, order);
261 spin_lock(&DRM(mem_lock));
262 free_count = ++DRM(mem_stats)[area].free_count;
263 alloc_count = DRM(mem_stats)[area].succeed_count;
264 DRM(mem_stats)[area].bytes_freed += bytes;
265 DRM(ram_used) -= bytes;
266 spin_unlock(&DRM(mem_lock));
267 if (free_count > alloc_count) {
269 "Excess frees: %d frees, %d allocs\n",
270 free_count, alloc_count);
274 void *DRM(ioremap)(unsigned long offset, unsigned long size)
279 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
280 "Mapping 0 bytes at 0x%08lx\n", offset);
284 if (!(pt = ioremap(offset, size))) {
285 spin_lock(&DRM(mem_lock));
286 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
287 spin_unlock(&DRM(mem_lock));
290 spin_lock(&DRM(mem_lock));
291 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
292 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += size;
293 spin_unlock(&DRM(mem_lock));
297 void *DRM(ioremap_nocache)(unsigned long offset, unsigned long size)
302 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
303 "Mapping 0 bytes at 0x%08lx\n", offset);
307 if (!(pt = ioremap_nocache(offset, size))) {
308 spin_lock(&DRM(mem_lock));
309 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
310 spin_unlock(&DRM(mem_lock));
313 spin_lock(&DRM(mem_lock));
314 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
315 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += size;
316 spin_unlock(&DRM(mem_lock));
320 void DRM(ioremapfree)(void *pt, unsigned long size)
326 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
327 "Attempt to free NULL pointer\n");
331 spin_lock(&DRM(mem_lock));
332 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_freed += size;
333 free_count = ++DRM(mem_stats)[DRM_MEM_MAPPINGS].free_count;
334 alloc_count = DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
335 spin_unlock(&DRM(mem_lock));
336 if (free_count > alloc_count) {
337 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
338 "Excess frees: %d frees, %d allocs\n",
339 free_count, alloc_count);
343 #if __REALLY_HAVE_AGP
345 agp_memory *DRM(alloc_agp)(int pages, u32 type)
350 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
354 if ((handle = DRM(agp_allocate_memory)(pages, type))) {
355 spin_lock(&DRM(mem_lock));
356 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
357 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_allocated
358 += pages << PAGE_SHIFT;
359 spin_unlock(&DRM(mem_lock));
362 spin_lock(&DRM(mem_lock));
363 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].fail_count;
364 spin_unlock(&DRM(mem_lock));
368 int DRM(free_agp)(agp_memory *handle, int pages)
372 int retval = -EINVAL;
375 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
376 "Attempt to free NULL AGP handle\n");
380 if (DRM(agp_free_memory)(handle)) {
381 spin_lock(&DRM(mem_lock));
382 free_count = ++DRM(mem_stats)[DRM_MEM_TOTALAGP].free_count;
383 alloc_count = DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
384 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_freed
385 += pages << PAGE_SHIFT;
386 spin_unlock(&DRM(mem_lock));
387 if (free_count > alloc_count) {
388 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
389 "Excess frees: %d frees, %d allocs\n",
390 free_count, alloc_count);
397 int DRM(bind_agp)(agp_memory *handle, unsigned int start)
399 int retcode = -EINVAL;
402 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
403 "Attempt to bind NULL AGP handle\n");
407 if (!(retcode = DRM(agp_bind_memory)(handle, start))) {
408 spin_lock(&DRM(mem_lock));
409 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
410 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_allocated
411 += handle->page_count << PAGE_SHIFT;
412 spin_unlock(&DRM(mem_lock));
415 spin_lock(&DRM(mem_lock));
416 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].fail_count;
417 spin_unlock(&DRM(mem_lock));
421 int DRM(unbind_agp)(agp_memory *handle)
425 int retcode = -EINVAL;
428 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
429 "Attempt to unbind NULL AGP handle\n");
433 if ((retcode = DRM(agp_unbind_memory)(handle))) return retcode;
434 spin_lock(&DRM(mem_lock));
435 free_count = ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].free_count;
436 alloc_count = DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
437 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_freed
438 += handle->page_count << PAGE_SHIFT;
439 spin_unlock(&DRM(mem_lock));
440 if (free_count > alloc_count) {
441 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
442 "Excess frees: %d frees, %d allocs\n",
443 free_count, alloc_count);