1 /* memory.c -- Memory management wrappers for DRM -*- linux-c -*-
2  * Created: Thu Feb  4 14:00:34 1999 by faith@precisioninsight.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  * 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.
26  *
27  * Authors:
28  *    Rickard E. (Rik) Faith <faith@valinux.com>
29  *
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_TOTALAGP]  = { "totalagp" },
67 	[DRM_MEM_BOUNDAGP]  = { "boundagp" },
68 	[DRM_MEM_CTXBITMAP] = { "ctxbitmap"},
69 	{ NULL, 0, }		/* Last entry must be null */
70 };
71 
drm_mem_init(void)72 void drm_mem_init(void)
73 {
74 	drm_mem_stats_t *mem;
75 	struct sysinfo	si;
76 
77 	for (mem = drm_mem_stats; mem->name; ++mem) {
78 		mem->succeed_count   = 0;
79 		mem->free_count	     = 0;
80 		mem->fail_count	     = 0;
81 		mem->bytes_allocated = 0;
82 		mem->bytes_freed     = 0;
83 	}
84 
85 	si_meminfo(&si);
86 #if LINUX_VERSION_CODE < 0x020317
87 				/* Changed to page count in 2.3.23 */
88 	drm_ram_available = si.totalram >> PAGE_SHIFT;
89 #else
90 	drm_ram_available = si.totalram;
91 #endif
92 	drm_ram_used	  = 0;
93 }
94 
95 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
96 
_drm_mem_info(char * buf,char ** start,off_t offset,int len,int * eof,void * data)97 static int _drm_mem_info(char *buf, char **start, off_t offset, int len,
98 			 int *eof, void *data)
99 {
100 	drm_mem_stats_t *pt;
101 
102 	if (offset > 0) return 0; /* no partial requests */
103 	len  = 0;
104 	*eof = 1;
105 	DRM_PROC_PRINT("		  total counts			"
106 		       " |    outstanding  \n");
107 	DRM_PROC_PRINT("type	   alloc freed fail	bytes	   freed"
108 		       " | allocs      bytes\n\n");
109 	DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB         |\n",
110 		       "system", 0, 0, 0,
111 		       drm_ram_available << (PAGE_SHIFT - 10));
112 	DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB         |\n",
113 		       "locked", 0, 0, 0, drm_ram_used >> 10);
114 	DRM_PROC_PRINT("\n");
115 	for (pt = drm_mem_stats; pt->name; pt++) {
116 		DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
117 			       pt->name,
118 			       pt->succeed_count,
119 			       pt->free_count,
120 			       pt->fail_count,
121 			       pt->bytes_allocated,
122 			       pt->bytes_freed,
123 			       pt->succeed_count - pt->free_count,
124 			       (long)pt->bytes_allocated
125 			       - (long)pt->bytes_freed);
126 	}
127 
128 	return len;
129 }
130 
drm_mem_info(char * buf,char ** start,off_t offset,int len,int * eof,void * data)131 int drm_mem_info(char *buf, char **start, off_t offset, int len,
132 		 int *eof, void *data)
133 {
134 	int ret;
135 
136 	spin_lock(&drm_mem_lock);
137 	ret = _drm_mem_info(buf, start, offset, len, eof, data);
138 	spin_unlock(&drm_mem_lock);
139 	return ret;
140 }
141 
drm_alloc(size_t size,int area)142 void *drm_alloc(size_t size, int area)
143 {
144 	void *pt;
145 
146 	if (!size) {
147 		DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
148 		return NULL;
149 	}
150 
151 	if (!(pt = kmalloc(size, GFP_KERNEL))) {
152 		spin_lock(&drm_mem_lock);
153 		++drm_mem_stats[area].fail_count;
154 		spin_unlock(&drm_mem_lock);
155 		return NULL;
156 	}
157 	spin_lock(&drm_mem_lock);
158 	++drm_mem_stats[area].succeed_count;
159 	drm_mem_stats[area].bytes_allocated += size;
160 	spin_unlock(&drm_mem_lock);
161 	return pt;
162 }
163 
drm_realloc(void * oldpt,size_t oldsize,size_t size,int area)164 void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
165 {
166 	void *pt;
167 
168 	if (!(pt = drm_alloc(size, area))) return NULL;
169 	if (oldpt && oldsize) {
170 		memcpy(pt, oldpt, oldsize);
171 		drm_free(oldpt, oldsize, area);
172 	}
173 	return pt;
174 }
175 
drm_strdup(const char * s,int area)176 char *drm_strdup(const char *s, int area)
177 {
178 	char *pt;
179 	int	 length = s ? strlen(s) : 0;
180 
181 	if (!(pt = drm_alloc(length+1, area))) return NULL;
182 	strcpy(pt, s);
183 	return pt;
184 }
185 
drm_strfree(const char * s,int area)186 void drm_strfree(const char *s, int area)
187 {
188 	unsigned int size;
189 
190 	if (!s) return;
191 
192 	size = 1 + (s ? strlen(s) : 0);
193 	drm_free((void *)s, size, area);
194 }
195 
drm_free(void * pt,size_t size,int area)196 void drm_free(void *pt, size_t size, int area)
197 {
198 	int alloc_count;
199 	int free_count;
200 
201 	if (!pt) DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
202 	else	 kfree(pt);
203 	spin_lock(&drm_mem_lock);
204 	drm_mem_stats[area].bytes_freed += size;
205 	free_count  = ++drm_mem_stats[area].free_count;
206 	alloc_count =	drm_mem_stats[area].succeed_count;
207 	spin_unlock(&drm_mem_lock);
208 	if (free_count > alloc_count) {
209 		DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
210 			      free_count, alloc_count);
211 	}
212 }
213 
drm_alloc_pages(int order,int area)214 unsigned long drm_alloc_pages(int order, int area)
215 {
216 	unsigned long address;
217 	unsigned long bytes	  = PAGE_SIZE << order;
218 	unsigned long addr;
219 	unsigned int  sz;
220 
221 	spin_lock(&drm_mem_lock);
222 	if ((drm_ram_used >> PAGE_SHIFT)
223 	    > (DRM_RAM_PERCENT * drm_ram_available) / 100) {
224 		spin_unlock(&drm_mem_lock);
225 		return 0;
226 	}
227 	spin_unlock(&drm_mem_lock);
228 
229 	address = __get_free_pages(GFP_KERNEL, order);
230 	if (!address) {
231 		spin_lock(&drm_mem_lock);
232 		++drm_mem_stats[area].fail_count;
233 		spin_unlock(&drm_mem_lock);
234 		return 0;
235 	}
236 	spin_lock(&drm_mem_lock);
237 	++drm_mem_stats[area].succeed_count;
238 	drm_mem_stats[area].bytes_allocated += bytes;
239 	drm_ram_used		            += bytes;
240 	spin_unlock(&drm_mem_lock);
241 
242 
243 				/* Zero outside the lock */
244 	memset((void *)address, 0, bytes);
245 
246 				/* Reserve */
247 	for (addr = address, sz = bytes;
248 	     sz > 0;
249 	     addr += PAGE_SIZE, sz -= PAGE_SIZE) {
250 #if LINUX_VERSION_CODE >= 0x020400
251 				/* Argument type changed in 2.4.0-test6/pre8 */
252 		mem_map_reserve(virt_to_page(addr));
253 #else
254 		mem_map_reserve(MAP_NR(addr));
255 #endif
256 	}
257 
258 	return address;
259 }
260 
drm_free_pages(unsigned long address,int order,int area)261 void drm_free_pages(unsigned long address, int order, int area)
262 {
263 	unsigned long bytes = PAGE_SIZE << order;
264 	int		  alloc_count;
265 	int		  free_count;
266 	unsigned long addr;
267 	unsigned int  sz;
268 
269 	if (!address) {
270 		DRM_MEM_ERROR(area, "Attempt to free address 0\n");
271 	} else {
272 				/* Unreserve */
273 		for (addr = address, sz = bytes;
274 		     sz > 0;
275 		     addr += PAGE_SIZE, sz -= PAGE_SIZE) {
276 #if LINUX_VERSION_CODE >= 0x020400
277 				/* Argument type changed in 2.4.0-test6/pre8 */
278 			mem_map_unreserve(virt_to_page(addr));
279 #else
280 			mem_map_unreserve(MAP_NR(addr));
281 #endif
282 		}
283 		free_pages(address, order);
284 	}
285 
286 	spin_lock(&drm_mem_lock);
287 	free_count  = ++drm_mem_stats[area].free_count;
288 	alloc_count =	drm_mem_stats[area].succeed_count;
289 	drm_mem_stats[area].bytes_freed += bytes;
290 	drm_ram_used			-= bytes;
291 	spin_unlock(&drm_mem_lock);
292 	if (free_count > alloc_count) {
293 		DRM_MEM_ERROR(area,
294 			      "Excess frees: %d frees, %d allocs\n",
295 			      free_count, alloc_count);
296 	}
297 }
298 
drm_ioremap(unsigned long offset,unsigned long size,drm_device_t * dev)299 void *drm_ioremap(unsigned long offset, unsigned long size, drm_device_t *dev)
300 {
301 	void *pt;
302 
303 	if (!size) {
304 		DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
305 			      "Mapping 0 bytes at 0x%08lx\n", offset);
306 		return NULL;
307 	}
308 
309 	if (!(pt = ioremap(offset, size))) {
310 		spin_lock(&drm_mem_lock);
311 		++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
312 		spin_unlock(&drm_mem_lock);
313 		return NULL;
314 	}
315 	spin_lock(&drm_mem_lock);
316 	++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
317 	drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
318 	spin_unlock(&drm_mem_lock);
319 	return pt;
320 }
321 
drm_ioremapfree(void * pt,unsigned long size,drm_device_t * dev)322 void drm_ioremapfree(void *pt, unsigned long size, drm_device_t *dev)
323 {
324 	int alloc_count;
325 	int free_count;
326 
327 	if (!pt)
328 		DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
329 			      "Attempt to free NULL pointer\n");
330 	else
331 		iounmap(pt);
332 
333 	spin_lock(&drm_mem_lock);
334 	drm_mem_stats[DRM_MEM_MAPPINGS].bytes_freed += size;
335 	free_count  = ++drm_mem_stats[DRM_MEM_MAPPINGS].free_count;
336 	alloc_count =	drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
337 	spin_unlock(&drm_mem_lock);
338 	if (free_count > alloc_count) {
339 		DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
340 			      "Excess frees: %d frees, %d allocs\n",
341 			      free_count, alloc_count);
342 	}
343 }
344 
345 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
drm_alloc_agp(int pages,u32 type)346 agp_memory *drm_alloc_agp(int pages, u32 type)
347 {
348 	agp_memory *handle;
349 
350 	if (!pages) {
351 		DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
352 		return NULL;
353 	}
354 
355 	if ((handle = drm_agp_allocate_memory(pages, type))) {
356 		spin_lock(&drm_mem_lock);
357 		++drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
358 		drm_mem_stats[DRM_MEM_TOTALAGP].bytes_allocated
359 			+= pages << PAGE_SHIFT;
360 		spin_unlock(&drm_mem_lock);
361 		return handle;
362 	}
363 	spin_lock(&drm_mem_lock);
364 	++drm_mem_stats[DRM_MEM_TOTALAGP].fail_count;
365 	spin_unlock(&drm_mem_lock);
366 	return NULL;
367 }
368 
drm_free_agp(agp_memory * handle,int pages)369 int drm_free_agp(agp_memory *handle, int pages)
370 {
371 	int           alloc_count;
372 	int           free_count;
373 	int           retval = -EINVAL;
374 
375 	if (!handle) {
376 		DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
377 			      "Attempt to free NULL AGP handle\n");
378 		return retval;;
379 	}
380 
381 	if (drm_agp_free_memory(handle)) {
382 		spin_lock(&drm_mem_lock);
383 		free_count  = ++drm_mem_stats[DRM_MEM_TOTALAGP].free_count;
384 		alloc_count =   drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
385 		drm_mem_stats[DRM_MEM_TOTALAGP].bytes_freed
386 			+= pages << PAGE_SHIFT;
387 		spin_unlock(&drm_mem_lock);
388 		if (free_count > alloc_count) {
389 			DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
390 				      "Excess frees: %d frees, %d allocs\n",
391 				      free_count, alloc_count);
392 		}
393 		return 0;
394 	}
395 	return retval;
396 }
397 
drm_bind_agp(agp_memory * handle,unsigned int start)398 int drm_bind_agp(agp_memory *handle, unsigned int start)
399 {
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 
drm_unbind_agp(agp_memory * handle)422 int drm_unbind_agp(agp_memory *handle)
423 {
424 	int alloc_count;
425 	int free_count;
426 	int retcode = -EINVAL;
427 
428 	if (!handle) {
429 		DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
430 			      "Attempt to unbind NULL AGP handle\n");
431 		return retcode;
432 	}
433 
434 	if ((retcode = drm_agp_unbind_memory(handle))) 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
449