Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 379 lines (332 sloc) 10.169 kb
0c4490c2 »
2009-07-01 initial 4.40 import
1 /**
2 * \file drm_memory_debug.h
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 "drmP.h"
35
36 typedef struct drm_mem_stats {
37 const char *name;
38 int succeed_count;
39 int free_count;
40 int fail_count;
41 unsigned long bytes_allocated;
42 unsigned long bytes_freed;
43 } drm_mem_stats_t;
44
45 static spinlock_t drm_mem_lock = SPIN_LOCK_UNLOCKED;
46 static unsigned long drm_ram_available = 0; /* In pages */
47 static unsigned long drm_ram_used = 0;
48 static drm_mem_stats_t drm_mem_stats[] =
49 {
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_CTXLIST] = {"ctxlist"},
71 [DRM_MEM_STUB] = {"stub"},
72 {NULL, 0,} /* Last entry must be null */
73 };
74
75 void drm_mem_init (void) {
76 drm_mem_stats_t *mem;
77 struct sysinfo si;
78
79 for (mem = drm_mem_stats; mem->name; ++mem) {
80 mem->succeed_count = 0;
81 mem->free_count = 0;
82 mem->fail_count = 0;
83 mem->bytes_allocated = 0;
84 mem->bytes_freed = 0;
85 }
86
87 si_meminfo(&si);
88 drm_ram_available = si.totalram;
89 drm_ram_used = 0;
90 }
91
92 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
93
94 static int drm__mem_info (char *buf, char **start, off_t offset,
95 int request, int *eof, void *data) {
96 drm_mem_stats_t *pt;
97 int len = 0;
98
99 if (offset > DRM_PROC_LIMIT) {
100 *eof = 1;
101 return 0;
102 }
103
104 *eof = 0;
105 *start = &buf[offset];
106
107 DRM_PROC_PRINT(" total counts "
108 " | outstanding \n");
109 DRM_PROC_PRINT("type alloc freed fail bytes freed"
110 " | allocs bytes\n\n");
111 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
112 "system", 0, 0, 0,
113 drm_ram_available << (PAGE_SHIFT - 10));
114 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB |\n",
115 "locked", 0, 0, 0, drm_ram_used >> 10);
116 DRM_PROC_PRINT("\n");
117 for (pt = drm_mem_stats; pt->name; pt++) {
118 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
119 pt->name,
120 pt->succeed_count,
121 pt->free_count,
122 pt->fail_count,
123 pt->bytes_allocated,
124 pt->bytes_freed,
125 pt->succeed_count - pt->free_count,
126 (long)pt->bytes_allocated
127 - (long)pt->bytes_freed);
128 }
129
130 if (len > request + offset)
131 return request;
132 *eof = 1;
133 return len - offset;
134 }
135
136 int drm_mem_info (char *buf, char **start, off_t offset,
137 int len, int *eof, void *data) {
138 int ret;
139
140 spin_lock(&drm_mem_lock);
141 ret = drm__mem_info (buf, start, offset, len, eof, data);
142 spin_unlock(&drm_mem_lock);
143 return ret;
144 }
145
146 void *drm_alloc (size_t size, int area) {
147 void *pt;
148
149 if (!size) {
150 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
151 return NULL;
152 }
153
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);
158 return NULL;
159 }
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);
164 return pt;
165 }
166
167 void *drm_calloc (size_t nmemb, size_t size, int area) {
168 void *addr;
169
170 addr = drm_alloc (nmemb * size, area);
171 if (addr != NULL)
172 memset((void *)addr, 0, size * nmemb);
173
174 return addr;
175 }
176
177 void *drm_realloc (void *oldpt, size_t oldsize, size_t size, int area) {
178 void *pt;
179
180 if (!(pt = drm_alloc (size, area)))
181 return NULL;
182 if (oldpt && oldsize) {
183 memcpy(pt, oldpt, oldsize);
184 drm_free (oldpt, oldsize, area);
185 }
186 return pt;
187 }
188
189 void drm_free (void *pt, size_t size, int area) {
190 int alloc_count;
191 int free_count;
192
193 if (!pt)
194 DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
195 else
196 kfree(pt);
197 spin_lock(&drm_mem_lock);
198 drm_mem_stats[area].bytes_freed += size;
199 free_count = ++drm_mem_stats[area].free_count;
200 alloc_count = drm_mem_stats[area].succeed_count;
201 spin_unlock(&drm_mem_lock);
202 if (free_count > alloc_count) {
203 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
204 free_count, alloc_count);
205 }
206 }
207
208 unsigned long drm_alloc_pages (int order, int area) {
209 unsigned long address;
210 unsigned long bytes = PAGE_SIZE << order;
211 unsigned long addr;
212 unsigned int sz;
213
214 spin_lock(&drm_mem_lock);
215 if ((drm_ram_used >> PAGE_SHIFT)
216 > (DRM_RAM_PERCENT * drm_ram_available) / 100) {
217 spin_unlock(&drm_mem_lock);
218 return 0;
219 }
220 spin_unlock(&drm_mem_lock);
221
222 address = __get_free_pages(GFP_KERNEL, order);
223 if (!address) {
224 spin_lock(&drm_mem_lock);
225 ++drm_mem_stats[area].fail_count;
226 spin_unlock(&drm_mem_lock);
227 return 0;
228 }
229 spin_lock(&drm_mem_lock);
230 ++drm_mem_stats[area].succeed_count;
231 drm_mem_stats[area].bytes_allocated += bytes;
232 drm_ram_used += bytes;
233 spin_unlock(&drm_mem_lock);
234
235 /* Zero outside the lock */
236 memset((void *)address, 0, bytes);
237
238 /* Reserve */
239 for (addr = address, sz = bytes;
240 sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
241 SetPageReserved(virt_to_page(addr));
242 }
243
244 return address;
245 }
246
247 void drm_free_pages (unsigned long address, int order, int area) {
248 unsigned long bytes = PAGE_SIZE << order;
249 int alloc_count;
250 int free_count;
251 unsigned long addr;
252 unsigned int sz;
253
254 if (!address) {
255 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
256 } else {
257 /* Unreserve */
258 for (addr = address, sz = bytes;
259 sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
260 ClearPageReserved(virt_to_page(addr));
261 }
262 free_pages(address, order);
263 }
264
265 spin_lock(&drm_mem_lock);
266 free_count = ++drm_mem_stats[area].free_count;
267 alloc_count = drm_mem_stats[area].succeed_count;
268 drm_mem_stats[area].bytes_freed += bytes;
269 drm_ram_used -= bytes;
270 spin_unlock(&drm_mem_lock);
271 if (free_count > alloc_count) {
272 DRM_MEM_ERROR(area,
273 "Excess frees: %d frees, %d allocs\n",
274 free_count, alloc_count);
275 }
276 }
277
278 #if __OS_HAS_AGP
279
280 DRM_AGP_MEM *drm_alloc_agp (struct drm_device *dev, int pages, u32 type) {
281 DRM_AGP_MEM *handle;
282
283 if (!pages) {
284 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
285 return NULL;
286 }
287
288 if ((handle = drm_agp_allocate_memory (pages, type))) {
289 spin_lock(&drm_mem_lock);
290 ++drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
291 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_allocated
292 += pages << PAGE_SHIFT;
293 spin_unlock(&drm_mem_lock);
294 return handle;
295 }
296 spin_lock(&drm_mem_lock);
297 ++drm_mem_stats[DRM_MEM_TOTALAGP].fail_count;
298 spin_unlock(&drm_mem_lock);
299 return NULL;
300 }
301
302 int drm_free_agp (DRM_AGP_MEM * handle, int pages) {
303 int alloc_count;
304 int free_count;
305 int retval = -EINVAL;
306
307 if (!handle) {
308 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
309 "Attempt to free NULL AGP handle\n");
310 return retval;
311 }
312
313 if (drm_agp_free_memory (handle)) {
314 spin_lock(&drm_mem_lock);
315 free_count = ++drm_mem_stats[DRM_MEM_TOTALAGP].free_count;
316 alloc_count = drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
317 drm_mem_stats[DRM_MEM_TOTALAGP].bytes_freed
318 += pages << PAGE_SHIFT;
319 spin_unlock(&drm_mem_lock);
320 if (free_count > alloc_count) {
321 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
322 "Excess frees: %d frees, %d allocs\n",
323 free_count, alloc_count);
324 }
325 return 0;
326 }
327 return retval;
328 }
329
330 int drm_bind_agp (DRM_AGP_MEM * handle, unsigned int start) {
331 int retcode = -EINVAL;
332
333 if (!handle) {
334 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
335 "Attempt to bind NULL AGP handle\n");
336 return retcode;
337 }
338
339 if (!(retcode = drm_agp_bind_memory (handle, start))) {
340 spin_lock(&drm_mem_lock);
341 ++drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
342 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_allocated
343 += handle->page_count << PAGE_SHIFT;
344 spin_unlock(&drm_mem_lock);
345 return retcode;
346 }
347 spin_lock(&drm_mem_lock);
348 ++drm_mem_stats[DRM_MEM_BOUNDAGP].fail_count;
349 spin_unlock(&drm_mem_lock);
350 return retcode;
351 }
352
353 int drm_unbind_agp (DRM_AGP_MEM * handle) {
354 int alloc_count;
355 int free_count;
356 int retcode = -EINVAL;
357
358 if (!handle) {
359 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
360 "Attempt to unbind NULL AGP handle\n");
361 return retcode;
362 }
363
364 if ((retcode = drm_agp_unbind_memory (handle)))
365 return retcode;
366 spin_lock(&drm_mem_lock);
367 free_count = ++drm_mem_stats[DRM_MEM_BOUNDAGP].free_count;
368 alloc_count = drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
369 drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_freed
370 += handle->page_count << PAGE_SHIFT;
371 spin_unlock(&drm_mem_lock);
372 if (free_count > alloc_count) {
373 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
374 "Excess frees: %d frees, %d allocs\n",
375 free_count, alloc_count);
376 }
377 return retcode;
378 }
379 #endif
Something went wrong with that request. Please try again.