Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 2 | * \file drm_memory_debug.h |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Memory management wrappers for DRM. |
| 4 | * |
| 5 | * \author Rickard E. (Rik) Faith <faith@valinux.com> |
| 6 | * \author Gareth Hughes <gareth@valinux.com> |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. |
| 11 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
| 12 | * All Rights Reserved. |
| 13 | * |
| 14 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 15 | * copy of this software and associated documentation files (the "Software"), |
| 16 | * to deal in the Software without restriction, including without limitation |
| 17 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 18 | * and/or sell copies of the Software, and to permit persons to whom the |
| 19 | * Software is furnished to do so, subject to the following conditions: |
| 20 | * |
| 21 | * The above copyright notice and this permission notice (including the next |
| 22 | * paragraph) shall be included in all copies or substantial portions of the |
| 23 | * Software. |
| 24 | * |
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 28 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 29 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 30 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 31 | * OTHER DEALINGS IN THE SOFTWARE. |
| 32 | */ |
| 33 | |
| 34 | #include <linux/config.h> |
| 35 | #include "drmP.h" |
| 36 | |
| 37 | typedef struct drm_mem_stats { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 38 | const char *name; |
| 39 | int succeed_count; |
| 40 | int free_count; |
| 41 | int fail_count; |
| 42 | unsigned long bytes_allocated; |
| 43 | unsigned long bytes_freed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } drm_mem_stats_t; |
| 45 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 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[] = |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 50 | { |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 51 | [DRM_MEM_DMA] = {"dmabufs"}, |
| 52 | [DRM_MEM_SAREA] = {"sareas"}, |
| 53 | [DRM_MEM_DRIVER] = {"driver"}, |
| 54 | [DRM_MEM_MAGIC] = {"magic"}, |
| 55 | [DRM_MEM_IOCTLS] = {"ioctltab"}, |
| 56 | [DRM_MEM_MAPS] = {"maplist"}, |
| 57 | [DRM_MEM_VMAS] = {"vmalist"}, |
| 58 | [DRM_MEM_BUFS] = {"buflist"}, |
| 59 | [DRM_MEM_SEGS] = {"seglist"}, |
| 60 | [DRM_MEM_PAGES] = {"pagelist"}, |
| 61 | [DRM_MEM_FILES] = {"files"}, |
| 62 | [DRM_MEM_QUEUES] = {"queues"}, |
| 63 | [DRM_MEM_CMDS] = {"commands"}, |
| 64 | [DRM_MEM_MAPPINGS] = {"mappings"}, |
| 65 | [DRM_MEM_BUFLISTS] = {"buflists"}, |
| 66 | [DRM_MEM_AGPLISTS] = {"agplist"}, |
| 67 | [DRM_MEM_SGLISTS] = {"sglist"}, |
| 68 | [DRM_MEM_TOTALAGP] = {"totalagp"}, |
| 69 | [DRM_MEM_BOUNDAGP] = {"boundagp"}, |
| 70 | [DRM_MEM_CTXBITMAP] = {"ctxbitmap"}, |
| 71 | [DRM_MEM_CTXLIST] = {"ctxlist"}, |
| 72 | [DRM_MEM_STUB] = {"stub"}, |
| 73 | {NULL, 0,} /* Last entry must be null */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 76 | void drm_mem_init (void) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | drm_mem_stats_t *mem; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 78 | struct sysinfo si; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 80 | for (mem = drm_mem_stats; mem->name; ++mem) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 81 | mem->succeed_count = 0; |
| 82 | mem->free_count = 0; |
| 83 | mem->fail_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | mem->bytes_allocated = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 85 | mem->bytes_freed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | si_meminfo(&si); |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 89 | drm_ram_available = si.totalram; |
| 90 | drm_ram_used = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* drm_mem_info is called whenever a process reads /dev/drm/mem. */ |
| 94 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 95 | static int drm__mem_info (char *buf, char **start, off_t offset, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 96 | int request, int *eof, void *data) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | drm_mem_stats_t *pt; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 98 | int len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | if (offset > DRM_PROC_LIMIT) { |
| 101 | *eof = 1; |
| 102 | return 0; |
| 103 | } |
| 104 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 105 | *eof = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | *start = &buf[offset]; |
| 107 | |
| 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", |
| 113 | "system", 0, 0, 0, |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 114 | drm_ram_available << (PAGE_SHIFT - 10)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n", |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 116 | "locked", 0, 0, 0, drm_ram_used >> 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | DRM_PROC_PRINT("\n"); |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 118 | for (pt = drm_mem_stats; pt->name; pt++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n", |
| 120 | pt->name, |
| 121 | pt->succeed_count, |
| 122 | pt->free_count, |
| 123 | pt->fail_count, |
| 124 | pt->bytes_allocated, |
| 125 | pt->bytes_freed, |
| 126 | pt->succeed_count - pt->free_count, |
| 127 | (long)pt->bytes_allocated |
| 128 | - (long)pt->bytes_freed); |
| 129 | } |
| 130 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 131 | if (len > request + offset) |
| 132 | return request; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | *eof = 1; |
| 134 | return len - offset; |
| 135 | } |
| 136 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 137 | int drm_mem_info (char *buf, char **start, off_t offset, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 138 | int len, int *eof, void *data) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | int ret; |
| 140 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 141 | spin_lock(&drm_mem_lock); |
| 142 | ret = drm__mem_info (buf, start, offset, len, eof, data); |
| 143 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return ret; |
| 145 | } |
| 146 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 147 | void *drm_alloc (size_t size, int area) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | void *pt; |
| 149 | |
| 150 | if (!size) { |
| 151 | DRM_MEM_ERROR(area, "Allocating 0 bytes\n"); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | if (!(pt = kmalloc(size, GFP_KERNEL))) { |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 156 | spin_lock(&drm_mem_lock); |
| 157 | ++drm_mem_stats[area].fail_count; |
| 158 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | return NULL; |
| 160 | } |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 161 | spin_lock(&drm_mem_lock); |
| 162 | ++drm_mem_stats[area].succeed_count; |
| 163 | drm_mem_stats[area].bytes_allocated += size; |
| 164 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return pt; |
| 166 | } |
| 167 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 168 | void *drm_calloc (size_t nmemb, size_t size, int area) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | void *addr; |
| 170 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 171 | addr = drm_alloc (nmemb * size, area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | if (addr != NULL) |
| 173 | memset((void *)addr, 0, size * nmemb); |
| 174 | |
| 175 | return addr; |
| 176 | } |
| 177 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 178 | void *drm_realloc (void *oldpt, size_t oldsize, size_t size, int area) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | void *pt; |
| 180 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 181 | if (!(pt = drm_alloc (size, area))) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 182 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if (oldpt && oldsize) { |
| 184 | memcpy(pt, oldpt, oldsize); |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 185 | drm_free (oldpt, oldsize, area); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | return pt; |
| 188 | } |
| 189 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 190 | void drm_free (void *pt, size_t size, int area) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | int alloc_count; |
| 192 | int free_count; |
| 193 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 194 | if (!pt) |
| 195 | DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n"); |
| 196 | else |
| 197 | kfree(pt); |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 198 | spin_lock(&drm_mem_lock); |
| 199 | drm_mem_stats[area].bytes_freed += size; |
| 200 | free_count = ++drm_mem_stats[area].free_count; |
| 201 | alloc_count = drm_mem_stats[area].succeed_count; |
| 202 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | if (free_count > alloc_count) { |
| 204 | DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n", |
| 205 | free_count, alloc_count); |
| 206 | } |
| 207 | } |
| 208 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 209 | unsigned long drm_alloc_pages (int order, int area) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | unsigned long address; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 211 | unsigned long bytes = PAGE_SIZE << order; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | unsigned long addr; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 213 | unsigned int sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 215 | spin_lock(&drm_mem_lock); |
| 216 | if ((drm_ram_used >> PAGE_SHIFT) |
| 217 | > (DRM_RAM_PERCENT * drm_ram_available) / 100) { |
| 218 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 221 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | address = __get_free_pages(GFP_KERNEL, order); |
| 224 | if (!address) { |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 225 | spin_lock(&drm_mem_lock); |
| 226 | ++drm_mem_stats[area].fail_count; |
| 227 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | return 0; |
| 229 | } |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 230 | spin_lock(&drm_mem_lock); |
| 231 | ++drm_mem_stats[area].succeed_count; |
| 232 | drm_mem_stats[area].bytes_allocated += bytes; |
| 233 | drm_ram_used += bytes; |
| 234 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 236 | /* Zero outside the lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | memset((void *)address, 0, bytes); |
| 238 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 239 | /* Reserve */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | for (addr = address, sz = bytes; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 241 | sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | SetPageReserved(virt_to_page(addr)); |
| 243 | } |
| 244 | |
| 245 | return address; |
| 246 | } |
| 247 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 248 | void drm_free_pages (unsigned long address, int order, int area) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | unsigned long bytes = PAGE_SIZE << order; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 250 | int alloc_count; |
| 251 | int free_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | unsigned long addr; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 253 | unsigned int sz; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | if (!address) { |
| 256 | DRM_MEM_ERROR(area, "Attempt to free address 0\n"); |
| 257 | } else { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 258 | /* Unreserve */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | for (addr = address, sz = bytes; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 260 | sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | ClearPageReserved(virt_to_page(addr)); |
| 262 | } |
| 263 | free_pages(address, order); |
| 264 | } |
| 265 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 266 | spin_lock(&drm_mem_lock); |
| 267 | free_count = ++drm_mem_stats[area].free_count; |
| 268 | alloc_count = drm_mem_stats[area].succeed_count; |
| 269 | drm_mem_stats[area].bytes_freed += bytes; |
| 270 | drm_ram_used -= bytes; |
| 271 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (free_count > alloc_count) { |
| 273 | DRM_MEM_ERROR(area, |
| 274 | "Excess frees: %d frees, %d allocs\n", |
| 275 | free_count, alloc_count); |
| 276 | } |
| 277 | } |
| 278 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 279 | void *drm_ioremap (unsigned long offset, unsigned long size, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 280 | drm_device_t * dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | void *pt; |
| 282 | |
| 283 | if (!size) { |
| 284 | DRM_MEM_ERROR(DRM_MEM_MAPPINGS, |
| 285 | "Mapping 0 bytes at 0x%08lx\n", offset); |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | if (!(pt = drm_ioremap(offset, size, dev))) { |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 290 | spin_lock(&drm_mem_lock); |
| 291 | ++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count; |
| 292 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return NULL; |
| 294 | } |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 295 | spin_lock(&drm_mem_lock); |
| 296 | ++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count; |
| 297 | drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size; |
| 298 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return pt; |
| 300 | } |
| 301 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 302 | void *drm_ioremap_nocache (unsigned long offset, unsigned long size, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 303 | drm_device_t * dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | void *pt; |
| 305 | |
| 306 | if (!size) { |
| 307 | DRM_MEM_ERROR(DRM_MEM_MAPPINGS, |
| 308 | "Mapping 0 bytes at 0x%08lx\n", offset); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | if (!(pt = drm_ioremap_nocache(offset, size, dev))) { |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 313 | spin_lock(&drm_mem_lock); |
| 314 | ++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count; |
| 315 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | return NULL; |
| 317 | } |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 318 | spin_lock(&drm_mem_lock); |
| 319 | ++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count; |
| 320 | drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size; |
| 321 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | return pt; |
| 323 | } |
| 324 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 325 | void drm_ioremapfree (void *pt, unsigned long size, drm_device_t * dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | int alloc_count; |
| 327 | int free_count; |
| 328 | |
| 329 | if (!pt) |
| 330 | DRM_MEM_ERROR(DRM_MEM_MAPPINGS, |
| 331 | "Attempt to free NULL pointer\n"); |
| 332 | else |
| 333 | drm_ioremapfree(pt, size, dev); |
| 334 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 335 | spin_lock(&drm_mem_lock); |
| 336 | drm_mem_stats[DRM_MEM_MAPPINGS].bytes_freed += size; |
| 337 | free_count = ++drm_mem_stats[DRM_MEM_MAPPINGS].free_count; |
| 338 | alloc_count = drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count; |
| 339 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | if (free_count > alloc_count) { |
| 341 | DRM_MEM_ERROR(DRM_MEM_MAPPINGS, |
| 342 | "Excess frees: %d frees, %d allocs\n", |
| 343 | free_count, alloc_count); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | #if __OS_HAS_AGP |
| 348 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 349 | DRM_AGP_MEM *drm_alloc_agp (drm_device_t *dev, int pages, u32 type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | DRM_AGP_MEM *handle; |
| 351 | |
| 352 | if (!pages) { |
| 353 | DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n"); |
| 354 | return NULL; |
| 355 | } |
| 356 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 357 | if ((handle = drm_agp_allocate_memory (pages, type))) { |
| 358 | spin_lock(&drm_mem_lock); |
| 359 | ++drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count; |
| 360 | drm_mem_stats[DRM_MEM_TOTALAGP].bytes_allocated |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 361 | += pages << PAGE_SHIFT; |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 362 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | return handle; |
| 364 | } |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 365 | spin_lock(&drm_mem_lock); |
| 366 | ++drm_mem_stats[DRM_MEM_TOTALAGP].fail_count; |
| 367 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return NULL; |
| 369 | } |
| 370 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 371 | int drm_free_agp (DRM_AGP_MEM * handle, int pages) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 372 | int alloc_count; |
| 373 | int free_count; |
| 374 | int retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
| 376 | if (!handle) { |
| 377 | DRM_MEM_ERROR(DRM_MEM_TOTALAGP, |
| 378 | "Attempt to free NULL AGP handle\n"); |
| 379 | return retval; |
| 380 | } |
| 381 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 382 | if (drm_agp_free_memory (handle)) { |
| 383 | spin_lock(&drm_mem_lock); |
| 384 | free_count = ++drm_mem_stats[DRM_MEM_TOTALAGP].free_count; |
| 385 | alloc_count = drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count; |
| 386 | drm_mem_stats[DRM_MEM_TOTALAGP].bytes_freed |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 387 | += pages << PAGE_SHIFT; |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 388 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | if (free_count > alloc_count) { |
| 390 | DRM_MEM_ERROR(DRM_MEM_TOTALAGP, |
| 391 | "Excess frees: %d frees, %d allocs\n", |
| 392 | free_count, alloc_count); |
| 393 | } |
| 394 | return 0; |
| 395 | } |
| 396 | return retval; |
| 397 | } |
| 398 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 399 | int drm_bind_agp (DRM_AGP_MEM * handle, unsigned int start) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | int retcode = -EINVAL; |
| 401 | |
| 402 | if (!handle) { |
| 403 | DRM_MEM_ERROR(DRM_MEM_BOUNDAGP, |
| 404 | "Attempt to bind NULL AGP handle\n"); |
| 405 | return retcode; |
| 406 | } |
| 407 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 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 |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 412 | += handle->page_count << PAGE_SHIFT; |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 413 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | return retcode; |
| 415 | } |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 416 | spin_lock(&drm_mem_lock); |
| 417 | ++drm_mem_stats[DRM_MEM_BOUNDAGP].fail_count; |
| 418 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | return retcode; |
| 420 | } |
| 421 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 422 | int drm_unbind_agp (DRM_AGP_MEM * handle) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | int alloc_count; |
| 424 | int free_count; |
| 425 | int retcode = -EINVAL; |
| 426 | |
| 427 | if (!handle) { |
| 428 | DRM_MEM_ERROR(DRM_MEM_BOUNDAGP, |
| 429 | "Attempt to unbind NULL AGP handle\n"); |
| 430 | return retcode; |
| 431 | } |
| 432 | |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 433 | if ((retcode = drm_agp_unbind_memory (handle))) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 434 | return retcode; |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 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 |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 439 | += handle->page_count << PAGE_SHIFT; |
Dave Airlie | f0c408b | 2006-01-02 19:52:09 +1100 | [diff] [blame^] | 440 | spin_unlock(&drm_mem_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 445 | } |
| 446 | return retcode; |
| 447 | } |
| 448 | #endif |