Import 2.3.18pre1
[davej-history.git] / drivers / char / drm / memory.c
blobf7b5335a5fc7c9cb00f80c8f754f5511ef265a49
1 /* memory.c -- Memory management wrappers for DRM -*- linux-c -*-
2 * Created: Thu Feb 4 14:00:34 1999 by faith@precisioninsight.com
3 * Revised: Fri Aug 20 13:04:33 1999 by faith@precisioninsight.com
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6 * All Rights Reserved.
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
17 * Software.
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.
27 * $PI: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/generic/memory.c,v 1.4 1999/08/20 20:00:53 faith Exp $
28 * $XFree86$
32 #define __NO_VERSION__
33 #include "drmP.h"
35 typedef struct drm_mem_stats {
36 const char *name;
37 int succeed_count;
38 int free_count;
39 int fail_count;
40 unsigned long bytes_allocated;
41 unsigned long bytes_freed;
42 } drm_mem_stats_t;
44 static spinlock_t drm_mem_lock = SPIN_LOCK_UNLOCKED;
45 static unsigned long drm_ram_available = 0;
46 static unsigned long drm_ram_used = 0;
47 static drm_mem_stats_t drm_mem_stats[] = {
48 [DRM_MEM_DMA] = { "dmabufs" },
49 [DRM_MEM_SAREA] = { "sareas" },
50 [DRM_MEM_DRIVER] = { "driver" },
51 [DRM_MEM_MAGIC] = { "magic" },
52 [DRM_MEM_IOCTLS] = { "ioctltab" },
53 [DRM_MEM_MAPS] = { "maplist" },
54 [DRM_MEM_VMAS] = { "vmalist" },
55 [DRM_MEM_BUFS] = { "buflist" },
56 [DRM_MEM_SEGS] = { "seglist" },
57 [DRM_MEM_PAGES] = { "pagelist" },
58 [DRM_MEM_FILES] = { "files" },
59 [DRM_MEM_QUEUES] = { "queues" },
60 [DRM_MEM_CMDS] = { "commands" },
61 [DRM_MEM_MAPPINGS] = { "mappings" },
62 [DRM_MEM_BUFLISTS] = { "buflists" },
63 { NULL, 0, } /* Last entry must be null */
66 void drm_mem_init(void)
68 drm_mem_stats_t *mem;
69 struct sysinfo si;
71 for (mem = drm_mem_stats; mem->name; ++mem) {
72 mem->succeed_count = 0;
73 mem->free_count = 0;
74 mem->fail_count = 0;
75 mem->bytes_allocated = 0;
76 mem->bytes_freed = 0;
79 si_meminfo(&si);
80 drm_ram_available = si.totalram;
81 drm_ram_used = 0;
84 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
86 static int _drm_mem_info(char *buf, char **start, off_t offset, int len,
87 int *eof, void *data)
89 drm_mem_stats_t *pt;
91 if (offset > 0) return 0; /* no partial requests */
92 len = 0;
93 *eof = 1;
94 DRM_PROC_PRINT(" total counts "
95 " | outstanding \n");
96 DRM_PROC_PRINT("type alloc freed fail bytes freed"
97 " | allocs bytes\n\n");
98 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu |\n",
99 "system", 0, 0, 0, drm_ram_available);
100 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu |\n",
101 "locked", 0, 0, 0, drm_ram_used);
102 DRM_PROC_PRINT("\n");
103 for (pt = drm_mem_stats; pt->name; pt++) {
104 DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
105 pt->name,
106 pt->succeed_count,
107 pt->free_count,
108 pt->fail_count,
109 pt->bytes_allocated,
110 pt->bytes_freed,
111 pt->succeed_count - pt->free_count,
112 (long)pt->bytes_allocated
113 - (long)pt->bytes_freed);
116 return len;
119 int drm_mem_info(char *buf, char **start, off_t offset, int len,
120 int *eof, void *data)
122 int ret;
124 spin_lock(&drm_mem_lock);
125 ret = _drm_mem_info(buf, start, offset, len, eof, data);
126 spin_unlock(&drm_mem_lock);
127 return ret;
130 void *drm_alloc(size_t size, int area)
132 void *pt;
134 if (!size) {
135 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
136 return NULL;
139 if (!(pt = kmalloc(size, GFP_KERNEL))) {
140 spin_lock(&drm_mem_lock);
141 ++drm_mem_stats[area].fail_count;
142 spin_unlock(&drm_mem_lock);
143 return NULL;
145 spin_lock(&drm_mem_lock);
146 ++drm_mem_stats[area].succeed_count;
147 drm_mem_stats[area].bytes_allocated += size;
148 spin_unlock(&drm_mem_lock);
149 return pt;
152 void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
154 void *pt;
156 if (!(pt = drm_alloc(size, area))) return NULL;
157 if (oldpt && oldsize) {
158 memcpy(pt, oldpt, oldsize);
159 drm_free(oldpt, oldsize, area);
161 return pt;
164 char *drm_strdup(const char *s, int area)
166 char *pt;
167 int length = s ? strlen(s) : 0;
169 if (!(pt = drm_alloc(length+1, area))) return NULL;
170 strcpy(pt, s);
171 return pt;
174 void drm_strfree(const char *s, int area)
176 unsigned int size;
178 if (!s) return;
180 size = 1 + (s ? strlen(s) : 0);
181 drm_free((void *)s, size, area);
184 void drm_free(void *pt, size_t size, int area)
186 int alloc_count;
187 int free_count;
189 if (!pt) DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
190 else kfree_s(pt, size);
191 spin_lock(&drm_mem_lock);
192 drm_mem_stats[area].bytes_freed += size;
193 free_count = ++drm_mem_stats[area].free_count;
194 alloc_count = drm_mem_stats[area].succeed_count;
195 spin_unlock(&drm_mem_lock);
196 if (free_count > alloc_count) {
197 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
198 free_count, alloc_count);
202 unsigned long drm_alloc_pages(int order, int area)
204 unsigned long address;
205 unsigned long bytes = PAGE_SIZE << order;
206 unsigned long addr;
207 unsigned int sz;
209 spin_lock(&drm_mem_lock);
210 if (drm_ram_used > +(DRM_RAM_PERCENT * drm_ram_available) / 100) {
211 spin_unlock(&drm_mem_lock);
212 return 0;
214 spin_unlock(&drm_mem_lock);
216 address = __get_free_pages(GFP_KERNEL, order);
217 if (!address) {
218 spin_lock(&drm_mem_lock);
219 ++drm_mem_stats[area].fail_count;
220 spin_unlock(&drm_mem_lock);
221 return 0;
223 spin_lock(&drm_mem_lock);
224 ++drm_mem_stats[area].succeed_count;
225 drm_mem_stats[area].bytes_allocated += bytes;
226 drm_ram_used += bytes;
227 spin_unlock(&drm_mem_lock);
230 /* Zero outside the lock */
231 memset((void *)address, 0, bytes);
233 /* Reserve */
234 for (addr = address, sz = bytes;
235 sz > 0;
236 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
237 mem_map_reserve(MAP_NR(addr));
240 return address;
243 void drm_free_pages(unsigned long address, int order, int area)
245 unsigned long bytes = PAGE_SIZE << order;
246 int alloc_count;
247 int free_count;
248 unsigned long addr;
249 unsigned int sz;
251 if (!address) {
252 DRM_MEM_ERROR(area, "Attempt to free address 0\n");
253 } else {
254 /* Unreserve */
255 for (addr = address, sz = bytes;
256 sz > 0;
257 addr += PAGE_SIZE, sz -= PAGE_SIZE) {
258 mem_map_unreserve(MAP_NR(addr));
260 free_pages(address, order);
263 spin_lock(&drm_mem_lock);
264 free_count = ++drm_mem_stats[area].free_count;
265 alloc_count = drm_mem_stats[area].succeed_count;
266 drm_mem_stats[area].bytes_freed += bytes;
267 drm_ram_used -= bytes;
268 spin_unlock(&drm_mem_lock);
269 if (free_count > alloc_count) {
270 DRM_MEM_ERROR(area,
271 "Excess frees: %d frees, %d allocs\n",
272 free_count, alloc_count);
276 void *drm_ioremap(unsigned long offset, unsigned long size)
278 void *pt;
280 if (!size) {
281 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
282 "Mapping 0 bytes at 0x%08lx\n", offset);
283 return NULL;
286 if (!(pt = ioremap(offset, size))) {
287 spin_lock(&drm_mem_lock);
288 ++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
289 spin_unlock(&drm_mem_lock);
290 return NULL;
292 spin_lock(&drm_mem_lock);
293 ++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
294 drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
295 spin_unlock(&drm_mem_lock);
296 return pt;
299 void drm_ioremapfree(void *pt, unsigned long size)
301 int alloc_count;
302 int free_count;
304 if (!pt)
305 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
306 "Attempt to free NULL pointer\n");
307 else
308 iounmap(pt);
310 spin_lock(&drm_mem_lock);
311 drm_mem_stats[DRM_MEM_MAPPINGS].bytes_freed += size;
312 free_count = ++drm_mem_stats[DRM_MEM_MAPPINGS].free_count;
313 alloc_count = drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
314 spin_unlock(&drm_mem_lock);
315 if (free_count > alloc_count) {
316 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
317 "Excess frees: %d frees, %d allocs\n",
318 free_count, alloc_count);