e84605fc54af09e8073cd3a99c399fc658b1cfd4
[sfrench/cifs-2.6.git] / drivers / char / drm / drm_memory_debug.h
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 <linux/config.h>
35 #include "drmP.h"
36
37 typedef struct drm_mem_stats {
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;
44 } drm_mem_stats_t;
45
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 {
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 */
74 };
75
76 void drm_mem_init (void) {
77         drm_mem_stats_t *mem;
78         struct sysinfo si;
79
80         for (mem = drm_mem_stats; mem->name; ++mem) {
81                 mem->succeed_count = 0;
82                 mem->free_count = 0;
83                 mem->fail_count = 0;
84                 mem->bytes_allocated = 0;
85                 mem->bytes_freed = 0;
86         }
87
88         si_meminfo(&si);
89         drm_ram_available = si.totalram;
90         drm_ram_used = 0;
91 }
92
93 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
94
95 static int drm__mem_info (char *buf, char **start, off_t offset,
96                            int request, int *eof, void *data) {
97         drm_mem_stats_t *pt;
98         int len = 0;
99
100         if (offset > DRM_PROC_LIMIT) {
101                 *eof = 1;
102                 return 0;
103         }
104
105         *eof = 0;
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,
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",
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
131         if (len > request + offset)
132                 return request;
133         *eof = 1;
134         return len - offset;
135 }
136
137 int drm_mem_info (char *buf, char **start, off_t offset,
138                    int len, int *eof, void *data) {
139         int ret;
140
141         spin_lock(&drm_mem_lock);
142         ret = drm__mem_info (buf, start, offset, len, eof, data);
143         spin_unlock(&drm_mem_lock);
144         return ret;
145 }
146
147 void *drm_alloc (size_t size, int area) {
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))) {
156                 spin_lock(&drm_mem_lock);
157                 ++drm_mem_stats[area].fail_count;
158                 spin_unlock(&drm_mem_lock);
159                 return NULL;
160         }
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);
165         return pt;
166 }
167
168 void *drm_calloc (size_t nmemb, size_t size, int area) {
169         void *addr;
170
171         addr = drm_alloc (nmemb * size, area);
172         if (addr != NULL)
173                 memset((void *)addr, 0, size * nmemb);
174
175         return addr;
176 }
177
178 void *drm_realloc (void *oldpt, size_t oldsize, size_t size, int area) {
179         void *pt;
180
181         if (!(pt = drm_alloc (size, area)))
182                 return NULL;
183         if (oldpt && oldsize) {
184                 memcpy(pt, oldpt, oldsize);
185                 drm_free (oldpt, oldsize, area);
186         }
187         return pt;
188 }
189
190 void drm_free (void *pt, size_t size, int area) {
191         int alloc_count;
192         int free_count;
193
194         if (!pt)
195                 DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
196         else
197                 kfree(pt);
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);
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
209 unsigned long drm_alloc_pages (int order, int area) {
210         unsigned long address;
211         unsigned long bytes = PAGE_SIZE << order;
212         unsigned long addr;
213         unsigned int sz;
214
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);
219                 return 0;
220         }
221         spin_unlock(&drm_mem_lock);
222
223         address = __get_free_pages(GFP_KERNEL|__GFP_COMP, order);
224         if (!address) {
225                 spin_lock(&drm_mem_lock);
226                 ++drm_mem_stats[area].fail_count;
227                 spin_unlock(&drm_mem_lock);
228                 return 0;
229         }
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);
235
236         /* Zero outside the lock */
237         memset((void *)address, 0, bytes);
238
239         /* Reserve */
240         for (addr = address, sz = bytes;
241              sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
242                 SetPageReserved(virt_to_page(addr));
243         }
244
245         return address;
246 }
247
248 void drm_free_pages (unsigned long address, int order, int area) {
249         unsigned long bytes = PAGE_SIZE << order;
250         int alloc_count;
251         int free_count;
252         unsigned long addr;
253         unsigned int sz;
254
255         if (!address) {
256                 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
257         } else {
258                 /* Unreserve */
259                 for (addr = address, sz = bytes;
260                      sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
261                         ClearPageReserved(virt_to_page(addr));
262                 }
263                 free_pages(address, order);
264         }
265
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);
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
279 void *drm_ioremap (unsigned long offset, unsigned long size,
280                     drm_device_t * dev) {
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))) {
290                 spin_lock(&drm_mem_lock);
291                 ++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
292                 spin_unlock(&drm_mem_lock);
293                 return NULL;
294         }
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);
299         return pt;
300 }
301
302 void *drm_ioremap_nocache (unsigned long offset, unsigned long size,
303                             drm_device_t * dev) {
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))) {
313                 spin_lock(&drm_mem_lock);
314                 ++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
315                 spin_unlock(&drm_mem_lock);
316                 return NULL;
317         }
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);
322         return pt;
323 }
324
325 void drm_ioremapfree (void *pt, unsigned long size, drm_device_t * dev) {
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
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);
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
349 DRM_AGP_MEM *drm_alloc_agp (drm_device_t *dev, int pages, u32 type) {
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
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
361                     += pages << PAGE_SHIFT;
362                 spin_unlock(&drm_mem_lock);
363                 return handle;
364         }
365         spin_lock(&drm_mem_lock);
366         ++drm_mem_stats[DRM_MEM_TOTALAGP].fail_count;
367         spin_unlock(&drm_mem_lock);
368         return NULL;
369 }
370
371 int drm_free_agp (DRM_AGP_MEM * handle, int pages) {
372         int alloc_count;
373         int free_count;
374         int retval = -EINVAL;
375
376         if (!handle) {
377                 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
378                               "Attempt to free NULL AGP handle\n");
379                 return retval;
380         }
381
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
387                     += pages << PAGE_SHIFT;
388                 spin_unlock(&drm_mem_lock);
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
399 int drm_bind_agp (DRM_AGP_MEM * handle, unsigned int start) {
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
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);
414                 return retcode;
415         }
416         spin_lock(&drm_mem_lock);
417         ++drm_mem_stats[DRM_MEM_BOUNDAGP].fail_count;
418         spin_unlock(&drm_mem_lock);
419         return retcode;
420 }
421
422 int drm_unbind_agp (DRM_AGP_MEM * handle) {
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
433         if ((retcode = drm_agp_unbind_memory (handle)))
434                 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);
445         }
446         return retcode;
447 }
448 #endif