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 #include <linux/config.h>
35 typedef struct drm_mem_stats {
40 unsigned long bytes_allocated;
41 unsigned long bytes_freed;
44 static spinlock_t DRM(mem_lock) = SPIN_LOCK_UNLOCKED;
45 static unsigned long DRM(ram_available) = 0; /* In pages */
46 static unsigned long DRM(ram_used) = 0;
47 static drm_mem_stats_t DRM(mem_stats)[] = {
48 [DRM_MEM_DMA] = { "dmabufs" },
49 [DRM_MEM_SAREA] = { "sareas" },
50 [DRM_MEM_DRIVER] = { "driver" },
51 [DRM_MEM_MAGIC] = { "magic" },
52 [DRM_MEM_IOCTLS] = { "ioctltab" },
53 [DRM_MEM_MAPS] = { "maplist" },
54 [DRM_MEM_VMAS] = { "vmalist" },
55 [DRM_MEM_BUFS] = { "buflist" },
56 [DRM_MEM_SEGS] = { "seglist" },
57 [DRM_MEM_PAGES] = { "pagelist" },
58 [DRM_MEM_FILES] = { "files" },
59 [DRM_MEM_QUEUES] = { "queues" },
60 [DRM_MEM_CMDS] = { "commands" },
61 [DRM_MEM_MAPPINGS] = { "mappings" },
62 [DRM_MEM_BUFLISTS] = { "buflists" },
63 [DRM_MEM_AGPLISTS] = { "agplist" },
64 [DRM_MEM_SGLISTS] = { "sglist" },
65 [DRM_MEM_TOTALAGP] = { "totalagp" },
66 [DRM_MEM_BOUNDAGP] = { "boundagp" },
67 [DRM_MEM_CTXBITMAP] = { "ctxbitmap"},
68 [DRM_MEM_STUB] = { "stub" },
69 { NULL, 0, } /* Last entry must be null */
72 void DRM(mem_init)(void)
77 for (mem = DRM(mem_stats); mem->name; ++mem) {
78 mem->succeed_count = 0;
81 mem->bytes_allocated = 0;
86 DRM(ram_available) = si.totalram;
90 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
92 static int DRM(_mem_info)(char *buf, char **start, off_t offset,
93 int request, int *eof, void *data)
98 if (offset > DRM_PROC_LIMIT) {
104 *start = &buf[offset];
106 DRM_PROC_PRINT(" total counts "
107 " | outstanding \n");
108 DRM_PROC_PRINT("type alloc freed fail bytes freed"
109 " | allocs bytes\n\n");
110 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
112 DRM(ram_available) << (PAGE_SHIFT - 10));
113 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
114 "locked", 0, 0, 0, DRM(ram_used) >> 10);
115 DRM_PROC_PRINT("\n");
116 for (pt = DRM(mem_stats); pt->name; pt++) {
117 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
124 pt->succeed_count - pt->free_count,
125 (long)pt->bytes_allocated
126 - (long)pt->bytes_freed);
129 if (len > request + offset) return request;
134 int DRM(mem_info)(char *buf, char **start, off_t offset,
135 int len, int *eof, void *data)
139 spin_lock(&DRM(mem_lock));
140 ret = DRM(_mem_info)(buf, start, offset, len, eof, data);
141 spin_unlock(&DRM(mem_lock));
145 void *DRM(alloc)(size_t size, int area)
150 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
154 if (!(pt = kmalloc(size, GFP_KERNEL))) {
155 spin_lock(&DRM(mem_lock));
156 ++DRM(mem_stats)[area].fail_count;
157 spin_unlock(&DRM(mem_lock));
160 spin_lock(&DRM(mem_lock));
161 ++DRM(mem_stats)[area].succeed_count;
162 DRM(mem_stats)[area].bytes_allocated += size;
163 spin_unlock(&DRM(mem_lock));
167 void *DRM(realloc)(void *oldpt, size_t oldsize, size_t size, int area)
171 if (!(pt = DRM(alloc)(size, area))) return NULL;
172 if (oldpt && oldsize) {
173 memcpy(pt, oldpt, oldsize);
174 DRM(free)(oldpt, oldsize, area);
179 void DRM(free)(void *pt, size_t size, int area)
184 if (!pt) DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
186 spin_lock(&DRM(mem_lock));
187 DRM(mem_stats)[area].bytes_freed += size;
188 free_count = ++DRM(mem_stats)[area].free_count;
189 alloc_count = DRM(mem_stats)[area].succeed_count;
190 spin_unlock(&DRM(mem_lock));
191 if (free_count > alloc_count) {
192 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
193 free_count, alloc_count);
197 unsigned long DRM(alloc_pages)(int order, int area)
199 unsigned long address;
200 unsigned long bytes = PAGE_SIZE << order;
204 spin_lock(&DRM(mem_lock));
205 if ((DRM(ram_used) >> PAGE_SHIFT)
206 > (DRM_RAM_PERCENT * DRM(ram_available)) / 100) {
207 spin_unlock(&DRM(mem_lock));
210 spin_unlock(&DRM(mem_lock));
212 address = __get_free_pages(GFP_KERNEL, order);
214 spin_lock(&DRM(mem_lock));
215 ++DRM(mem_stats)[area].fail_count;
216 spin_unlock(&DRM(mem_lock));
219 spin_lock(&DRM(mem_lock));
220 ++DRM(mem_stats)[area].succeed_count;
221 DRM(mem_stats)[area].bytes_allocated += bytes;
222 DRM(ram_used) += bytes;
223 spin_unlock(&DRM(mem_lock));
226 /* Zero outside the lock */
227 memset((void *)address, 0, bytes);
230 for (addr = address, sz = bytes;
232 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
233 SetPageReserved(virt_to_page(addr));
239 void DRM(free_pages)(unsigned long address, int order, int area)
241 unsigned long bytes = PAGE_SIZE << order;
248 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
251 for (addr = address, sz = bytes;
253 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
254 ClearPageReserved(virt_to_page(addr));
256 free_pages(address, order);
259 spin_lock(&DRM(mem_lock));
260 free_count = ++DRM(mem_stats)[area].free_count;
261 alloc_count = DRM(mem_stats)[area].succeed_count;
262 DRM(mem_stats)[area].bytes_freed += bytes;
263 DRM(ram_used) -= bytes;
264 spin_unlock(&DRM(mem_lock));
265 if (free_count > alloc_count) {
267 "Excess frees: %d frees, %d allocs\n",
268 free_count, alloc_count);
272 void *DRM(ioremap)(unsigned long offset, unsigned long size)
277 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
278 "Mapping 0 bytes at 0x%08lx\n", offset);
282 if (!(pt = ioremap(offset, size))) {
283 spin_lock(&DRM(mem_lock));
284 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
285 spin_unlock(&DRM(mem_lock));
288 spin_lock(&DRM(mem_lock));
289 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
290 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += size;
291 spin_unlock(&DRM(mem_lock));
295 void *DRM(ioremap_nocache)(unsigned long offset, unsigned long size)
300 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
301 "Mapping 0 bytes at 0x%08lx\n", offset);
305 if (!(pt = ioremap_nocache(offset, size))) {
306 spin_lock(&DRM(mem_lock));
307 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
308 spin_unlock(&DRM(mem_lock));
311 spin_lock(&DRM(mem_lock));
312 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
313 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += size;
314 spin_unlock(&DRM(mem_lock));
318 void DRM(ioremapfree)(void *pt, unsigned long size)
324 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
325 "Attempt to free NULL pointer\n");
329 spin_lock(&DRM(mem_lock));
330 DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_freed += size;
331 free_count = ++DRM(mem_stats)[DRM_MEM_MAPPINGS].free_count;
332 alloc_count = DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
333 spin_unlock(&DRM(mem_lock));
334 if (free_count > alloc_count) {
335 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
336 "Excess frees: %d frees, %d allocs\n",
337 free_count, alloc_count);
341 #if __REALLY_HAVE_AGP
343 agp_memory *DRM(alloc_agp)(int pages, u32 type)
348 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
352 if ((handle = DRM(agp_allocate_memory)(pages, type))) {
353 spin_lock(&DRM(mem_lock));
354 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
355 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_allocated
356 += pages << PAGE_SHIFT;
357 spin_unlock(&DRM(mem_lock));
360 spin_lock(&DRM(mem_lock));
361 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].fail_count;
362 spin_unlock(&DRM(mem_lock));
366 int DRM(free_agp)(agp_memory *handle, int pages)
370 int retval = -EINVAL;
373 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
374 "Attempt to free NULL AGP handle\n");
378 if (DRM(agp_free_memory)(handle)) {
379 spin_lock(&DRM(mem_lock));
380 free_count = ++DRM(mem_stats)[DRM_MEM_TOTALAGP].free_count;
381 alloc_count = DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
382 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_freed
383 += pages << PAGE_SHIFT;
384 spin_unlock(&DRM(mem_lock));
385 if (free_count > alloc_count) {
386 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
387 "Excess frees: %d frees, %d allocs\n",
388 free_count, alloc_count);
395 int DRM(bind_agp)(agp_memory *handle, unsigned int start)
397 int retcode = -EINVAL;
400 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
401 "Attempt to bind NULL AGP handle\n");
405 if (!(retcode = DRM(agp_bind_memory)(handle, start))) {
406 spin_lock(&DRM(mem_lock));
407 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
408 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_allocated
409 += handle->page_count << PAGE_SHIFT;
410 spin_unlock(&DRM(mem_lock));
413 spin_lock(&DRM(mem_lock));
414 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].fail_count;
415 spin_unlock(&DRM(mem_lock));
419 int DRM(unbind_agp)(agp_memory *handle)
423 int retcode = -EINVAL;
426 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
427 "Attempt to unbind NULL AGP handle\n");
431 if ((retcode = DRM(agp_unbind_memory)(handle))) return retcode;
432 spin_lock(&DRM(mem_lock));
433 free_count = ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].free_count;
434 alloc_count = DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
435 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_freed
436 += handle->page_count << PAGE_SHIFT;
437 spin_unlock(&DRM(mem_lock));
438 if (free_count > alloc_count) {
439 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
440 "Excess frees: %d frees, %d allocs\n",
441 free_count, alloc_count);