1 /* memory.c -- Memory management wrappers for DRM -*- linux-c -*-
2 * Created: Thu Feb 4 14:00:34 1999 by faith@precisioninsight.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 * PRECISION INSIGHT 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 OTHER
25 * DEALINGS IN THE SOFTWARE.
28 * Rickard E. (Rik) Faith <faith@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 { 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 #if LINUX_VERSION_CODE < 0x020317
87 /* Changed to page count in 2.3.23 */
88 drm_ram_available = si.totalram >> PAGE_SHIFT;
90 drm_ram_available = si.totalram;
95 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
97 static int _drm_mem_info(char *buf, char **start, off_t offset, int len,
102 if (offset > 0) return 0; /* no partial requests */
105 DRM_PROC_PRINT(" total counts "
106 " | outstanding \n");
107 DRM_PROC_PRINT("type alloc freed fail bytes freed"
108 " | allocs bytes\n\n");
109 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
111 drm_ram_available << (PAGE_SHIFT - 10));
112 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
113 "locked", 0, 0, 0, drm_ram_used >> 10);
114 DRM_PROC_PRINT("\n");
115 for (pt = drm_mem_stats; pt->name; pt++) {
116 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
123 pt->succeed_count - pt->free_count,
124 (long)pt->bytes_allocated
125 - (long)pt->bytes_freed);
131 int drm_mem_info(char *buf, char **start, off_t offset, int len,
132 int *eof, void *data)
136 spin_lock(&drm_mem_lock);
137 ret = _drm_mem_info(buf, start, offset, len, eof, data);
138 spin_unlock(&drm_mem_lock);
142 void *drm_alloc(size_t size, int area)
147 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
151 if (!(pt = kmalloc(size, GFP_KERNEL))) {
152 spin_lock(&drm_mem_lock);
153 ++drm_mem_stats[area].fail_count;
154 spin_unlock(&drm_mem_lock);
157 spin_lock(&drm_mem_lock);
158 ++drm_mem_stats[area].succeed_count;
159 drm_mem_stats[area].bytes_allocated += size;
160 spin_unlock(&drm_mem_lock);
164 void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
168 if (!(pt = drm_alloc(size, area))) return NULL;
169 if (oldpt && oldsize) {
170 memcpy(pt, oldpt, oldsize);
171 drm_free(oldpt, oldsize, area);
176 char *drm_strdup(const char *s, int area)
179 int length = s ? strlen(s) : 0;
181 if (!(pt = drm_alloc(length+1, area))) return NULL;
186 void drm_strfree(const char *s, int area)
192 size = 1 + (s ? strlen(s) : 0);
193 drm_free((void *)s, size, area);
196 void drm_free(void *pt, size_t size, int area)
201 if (!pt) DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
203 spin_lock(&drm_mem_lock);
204 drm_mem_stats[area].bytes_freed += size;
205 free_count = ++drm_mem_stats[area].free_count;
206 alloc_count = drm_mem_stats[area].succeed_count;
207 spin_unlock(&drm_mem_lock);
208 if (free_count > alloc_count) {
209 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
210 free_count, alloc_count);
214 unsigned long drm_alloc_pages(int order, int area)
216 unsigned long address;
217 unsigned long bytes = PAGE_SIZE << order;
221 spin_lock(&drm_mem_lock);
222 if ((drm_ram_used >> PAGE_SHIFT)
223 > (DRM_RAM_PERCENT * drm_ram_available) / 100) {
224 spin_unlock(&drm_mem_lock);
227 spin_unlock(&drm_mem_lock);
229 address = __get_free_pages(GFP_KERNEL, order);
231 spin_lock(&drm_mem_lock);
232 ++drm_mem_stats[area].fail_count;
233 spin_unlock(&drm_mem_lock);
236 spin_lock(&drm_mem_lock);
237 ++drm_mem_stats[area].succeed_count;
238 drm_mem_stats[area].bytes_allocated += bytes;
239 drm_ram_used += bytes;
240 spin_unlock(&drm_mem_lock);
243 /* Zero outside the lock */
244 memset((void *)address, 0, bytes);
247 for (addr = address, sz = bytes;
249 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
250 #if LINUX_VERSION_CODE >= 0x020400
251 /* Argument type changed in 2.4.0-test6/pre8 */
252 mem_map_reserve(virt_to_page(addr));
254 mem_map_reserve(MAP_NR(addr));
261 void drm_free_pages(unsigned long address, int order, int area)
263 unsigned long bytes = PAGE_SIZE << order;
270 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
273 for (addr = address, sz = bytes;
275 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
276 #if LINUX_VERSION_CODE >= 0x020400
277 /* Argument type changed in 2.4.0-test6/pre8 */
278 mem_map_unreserve(virt_to_page(addr));
280 mem_map_unreserve(MAP_NR(addr));
283 free_pages(address, order);
286 spin_lock(&drm_mem_lock);
287 free_count = ++drm_mem_stats[area].free_count;
288 alloc_count = drm_mem_stats[area].succeed_count;
289 drm_mem_stats[area].bytes_freed += bytes;
290 drm_ram_used -= bytes;
291 spin_unlock(&drm_mem_lock);
292 if (free_count > alloc_count) {
294 "Excess frees: %d frees, %d allocs\n",
295 free_count, alloc_count);
299 void *drm_ioremap(unsigned long offset, unsigned long size)
304 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
305 "Mapping 0 bytes at 0x%08lx\n", offset);
309 if (!(pt = ioremap(offset, size))) {
310 spin_lock(&drm_mem_lock);
311 ++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
312 spin_unlock(&drm_mem_lock);
315 spin_lock(&drm_mem_lock);
316 ++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
317 drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
318 spin_unlock(&drm_mem_lock);
322 void drm_ioremapfree(void *pt, unsigned long size)
328 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
329 "Attempt to free NULL pointer\n");
333 spin_lock(&drm_mem_lock);
334 drm_mem_stats[DRM_MEM_MAPPINGS].bytes_freed += size;
335 free_count = ++drm_mem_stats[DRM_MEM_MAPPINGS].free_count;
336 alloc_count = drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
337 spin_unlock(&drm_mem_lock);
338 if (free_count > alloc_count) {
339 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
340 "Excess frees: %d frees, %d allocs\n",
341 free_count, alloc_count);
345 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
346 agp_memory *drm_alloc_agp(int pages, u32 type)
351 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
355 if ((handle = drm_agp_allocate_memory(pages, type))) {
356 spin_lock(&drm_mem_lock);
357 ++drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
358 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_allocated
359 += pages << PAGE_SHIFT;
360 spin_unlock(&drm_mem_lock);
363 spin_lock(&drm_mem_lock);
364 ++drm_mem_stats[DRM_MEM_TOTALAGP].fail_count;
365 spin_unlock(&drm_mem_lock);
369 int drm_free_agp(agp_memory *handle, int pages)
373 int retval = -EINVAL;
376 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
377 "Attempt to free NULL AGP handle\n");
381 if (drm_agp_free_memory(handle)) {
382 spin_lock(&drm_mem_lock);
383 free_count = ++drm_mem_stats[DRM_MEM_TOTALAGP].free_count;
384 alloc_count = drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
385 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_freed
386 += pages << PAGE_SHIFT;
387 spin_unlock(&drm_mem_lock);
388 if (free_count > alloc_count) {
389 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
390 "Excess frees: %d frees, %d allocs\n",
391 free_count, alloc_count);
398 int drm_bind_agp(agp_memory *handle, unsigned int start)
400 int retcode = -EINVAL;
403 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
404 "Attempt to bind NULL AGP handle\n");
408 if (!(retcode = drm_agp_bind_memory(handle, start))) {
409 spin_lock(&drm_mem_lock);
410 ++drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
411 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_allocated
412 += handle->page_count << PAGE_SHIFT;
413 spin_unlock(&drm_mem_lock);
416 spin_lock(&drm_mem_lock);
417 ++drm_mem_stats[DRM_MEM_BOUNDAGP].fail_count;
418 spin_unlock(&drm_mem_lock);
422 int drm_unbind_agp(agp_memory *handle)
426 int retcode = -EINVAL;
429 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
430 "Attempt to unbind NULL AGP handle\n");
434 if ((retcode = drm_agp_unbind_memory(handle))) return retcode;
435 spin_lock(&drm_mem_lock);
436 free_count = ++drm_mem_stats[DRM_MEM_BOUNDAGP].free_count;
437 alloc_count = drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
438 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_freed
439 += handle->page_count << PAGE_SHIFT;
440 spin_unlock(&drm_mem_lock);
441 if (free_count > alloc_count) {
442 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
443 "Excess frees: %d frees, %d allocs\n",
444 free_count, alloc_count);