OSDN Git Service

Remove #if 0'd code and some unused string functions
[android-x86/external-libdrm.git] / linux-core / drm_memory.h
1 /* drm_memory.h -- Memory management wrappers for DRM -*- linux-c -*-
2  * Created: Thu Feb  4 14:00:34 1999 by faith@valinux.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
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:
14  *
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
17  * Software.
18  *
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.
26  *
27  * Authors:
28  *    Rickard E. (Rik) Faith <faith@valinux.com>
29  *    Gareth Hughes <gareth@valinux.com>
30  */
31
32 #define __NO_VERSION__
33 #include <linux/config.h>
34 #include "drmP.h"
35 #include <linux/wrapper.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         [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_STUB]      = { "stub"     },
71         { NULL, 0, }            /* Last entry must be null */
72 };
73
74 void DRM(mem_init)(void)
75 {
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 {
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) 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 {
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 {
149         void *pt;
150
151         if (!size) {
152                 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
153                 return NULL;
154         }
155
156         if (!(pt = kmalloc(size, GFP_KERNEL))) {
157                 spin_lock(&DRM(mem_lock));
158                 ++DRM(mem_stats)[area].fail_count;
159                 spin_unlock(&DRM(mem_lock));
160                 return NULL;
161         }
162         spin_lock(&DRM(mem_lock));
163         ++DRM(mem_stats)[area].succeed_count;
164         DRM(mem_stats)[area].bytes_allocated += size;
165         spin_unlock(&DRM(mem_lock));
166         return pt;
167 }
168
169 void *DRM(realloc)(void *oldpt, size_t oldsize, size_t size, int area)
170 {
171         void *pt;
172
173         if (!(pt = DRM(alloc)(size, area))) return NULL;
174         if (oldpt && oldsize) {
175                 memcpy(pt, oldpt, oldsize);
176                 DRM(free)(oldpt, oldsize, area);
177         }
178         return pt;
179 }
180
181 void DRM(free)(void *pt, size_t size, int area)
182 {
183         int alloc_count;
184         int free_count;
185
186         if (!pt) DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
187         else     kfree(pt);
188         spin_lock(&DRM(mem_lock));
189         DRM(mem_stats)[area].bytes_freed += size;
190         free_count  = ++DRM(mem_stats)[area].free_count;
191         alloc_count =   DRM(mem_stats)[area].succeed_count;
192         spin_unlock(&DRM(mem_lock));
193         if (free_count > alloc_count) {
194                 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
195                               free_count, alloc_count);
196         }
197 }
198
199 unsigned long DRM(alloc_pages)(int order, int area)
200 {
201         unsigned long address;
202         unsigned long bytes       = PAGE_SIZE << order;
203         unsigned long addr;
204         unsigned int  sz;
205
206         spin_lock(&DRM(mem_lock));
207         if ((DRM(ram_used) >> PAGE_SHIFT)
208             > (DRM_RAM_PERCENT * DRM(ram_available)) / 100) {
209                 spin_unlock(&DRM(mem_lock));
210                 return 0;
211         }
212         spin_unlock(&DRM(mem_lock));
213
214         address = __get_free_pages(GFP_KERNEL, order);
215         if (!address) {
216                 spin_lock(&DRM(mem_lock));
217                 ++DRM(mem_stats)[area].fail_count;
218                 spin_unlock(&DRM(mem_lock));
219                 return 0;
220         }
221         spin_lock(&DRM(mem_lock));
222         ++DRM(mem_stats)[area].succeed_count;
223         DRM(mem_stats)[area].bytes_allocated += bytes;
224         DRM(ram_used)                        += bytes;
225         spin_unlock(&DRM(mem_lock));
226
227
228                                 /* Zero outside the lock */
229         memset((void *)address, 0, bytes);
230
231                                 /* Reserve */
232         for (addr = address, sz = bytes;
233              sz > 0;
234              addr += PAGE_SIZE, sz -= PAGE_SIZE) {
235                 mem_map_reserve(virt_to_page(addr));
236         }
237
238         return address;
239 }
240
241 void DRM(free_pages)(unsigned long address, int order, int area)
242 {
243         unsigned long bytes = PAGE_SIZE << order;
244         int               alloc_count;
245         int               free_count;
246         unsigned long addr;
247         unsigned int  sz;
248
249         if (!address) {
250                 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
251         } else {
252                                 /* Unreserve */
253                 for (addr = address, sz = bytes;
254                      sz > 0;
255                      addr += PAGE_SIZE, sz -= PAGE_SIZE) {
256                         mem_map_unreserve(virt_to_page(addr));
257                 }
258                 free_pages(address, order);
259         }
260
261         spin_lock(&DRM(mem_lock));
262         free_count  = ++DRM(mem_stats)[area].free_count;
263         alloc_count =   DRM(mem_stats)[area].succeed_count;
264         DRM(mem_stats)[area].bytes_freed += bytes;
265         DRM(ram_used)                    -= bytes;
266         spin_unlock(&DRM(mem_lock));
267         if (free_count > alloc_count) {
268                 DRM_MEM_ERROR(area,
269                               "Excess frees: %d frees, %d allocs\n",
270                               free_count, alloc_count);
271         }
272 }
273
274 void *DRM(ioremap)(unsigned long offset, unsigned long size)
275 {
276         void *pt;
277
278         if (!size) {
279                 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
280                               "Mapping 0 bytes at 0x%08lx\n", offset);
281                 return NULL;
282         }
283
284         if (!(pt = ioremap(offset, size))) {
285                 spin_lock(&DRM(mem_lock));
286                 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
287                 spin_unlock(&DRM(mem_lock));
288                 return NULL;
289         }
290         spin_lock(&DRM(mem_lock));
291         ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
292         DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += size;
293         spin_unlock(&DRM(mem_lock));
294         return pt;
295 }
296
297 void *DRM(ioremap_nocache)(unsigned long offset, unsigned long size)
298 {
299         void *pt;
300
301         if (!size) {
302                 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
303                               "Mapping 0 bytes at 0x%08lx\n", offset);
304                 return NULL;
305         }
306
307         if (!(pt = ioremap_nocache(offset, size))) {
308                 spin_lock(&DRM(mem_lock));
309                 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
310                 spin_unlock(&DRM(mem_lock));
311                 return NULL;
312         }
313         spin_lock(&DRM(mem_lock));
314         ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
315         DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += size;
316         spin_unlock(&DRM(mem_lock));
317         return pt;
318 }
319
320 void DRM(ioremapfree)(void *pt, unsigned long size)
321 {
322         int alloc_count;
323         int free_count;
324
325         if (!pt)
326                 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
327                               "Attempt to free NULL pointer\n");
328         else
329                 iounmap(pt);
330
331         spin_lock(&DRM(mem_lock));
332         DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_freed += size;
333         free_count  = ++DRM(mem_stats)[DRM_MEM_MAPPINGS].free_count;
334         alloc_count =   DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
335         spin_unlock(&DRM(mem_lock));
336         if (free_count > alloc_count) {
337                 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
338                               "Excess frees: %d frees, %d allocs\n",
339                               free_count, alloc_count);
340         }
341 }
342
343 #if __REALLY_HAVE_AGP
344
345 agp_memory *DRM(alloc_agp)(int pages, u32 type)
346 {
347         agp_memory *handle;
348
349         if (!pages) {
350                 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
351                 return NULL;
352         }
353
354         if ((handle = DRM(agp_allocate_memory)(pages, type))) {
355                 spin_lock(&DRM(mem_lock));
356                 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
357                 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_allocated
358                         += pages << PAGE_SHIFT;
359                 spin_unlock(&DRM(mem_lock));
360                 return handle;
361         }
362         spin_lock(&DRM(mem_lock));
363         ++DRM(mem_stats)[DRM_MEM_TOTALAGP].fail_count;
364         spin_unlock(&DRM(mem_lock));
365         return NULL;
366 }
367
368 int DRM(free_agp)(agp_memory *handle, int pages)
369 {
370         int           alloc_count;
371         int           free_count;
372         int           retval = -EINVAL;
373
374         if (!handle) {
375                 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
376                               "Attempt to free NULL AGP handle\n");
377                 return retval;;
378         }
379
380         if (DRM(agp_free_memory)(handle)) {
381                 spin_lock(&DRM(mem_lock));
382                 free_count  = ++DRM(mem_stats)[DRM_MEM_TOTALAGP].free_count;
383                 alloc_count =   DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
384                 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_freed
385                         += pages << PAGE_SHIFT;
386                 spin_unlock(&DRM(mem_lock));
387                 if (free_count > alloc_count) {
388                         DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
389                                       "Excess frees: %d frees, %d allocs\n",
390                                       free_count, alloc_count);
391                 }
392                 return 0;
393         }
394         return retval;
395 }
396
397 int DRM(bind_agp)(agp_memory *handle, unsigned int start)
398 {
399         int retcode = -EINVAL;
400
401         if (!handle) {
402                 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
403                               "Attempt to bind NULL AGP handle\n");
404                 return retcode;
405         }
406
407         if (!(retcode = DRM(agp_bind_memory)(handle, start))) {
408                 spin_lock(&DRM(mem_lock));
409                 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
410                 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_allocated
411                         += handle->page_count << PAGE_SHIFT;
412                 spin_unlock(&DRM(mem_lock));
413                 return retcode;
414         }
415         spin_lock(&DRM(mem_lock));
416         ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].fail_count;
417         spin_unlock(&DRM(mem_lock));
418         return retcode;
419 }
420
421 int DRM(unbind_agp)(agp_memory *handle)
422 {
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))) return retcode;
434         spin_lock(&DRM(mem_lock));
435         free_count  = ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].free_count;
436         alloc_count = DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
437         DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_freed
438                 += handle->page_count << PAGE_SHIFT;
439         spin_unlock(&DRM(mem_lock));
440         if (free_count > alloc_count) {
441                 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
442                               "Excess frees: %d frees, %d allocs\n",
443                               free_count, alloc_count);
444         }
445         return retcode;
446 }
447 #endif