summaryrefslogtreecommitdiff
path: root/linux-core/drm_memory_debug.h
blob: 1e0a63b7f69ce555f50527f5dc572c891bd4f713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
 * \file drm_memory_debug.h
 * Memory management wrappers for DRM.
 *
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 */

/*
 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "drmP.h"

typedef struct drm_mem_stats {
	const char *name;
	int succeed_count;
	int free_count;
	int fail_count;
	unsigned long bytes_allocated;
	unsigned long bytes_freed;
} drm_mem_stats_t;

static spinlock_t drm_mem_lock = SPIN_LOCK_UNLOCKED;
static unsigned long drm_ram_available = 0;	/* In pages */
static unsigned long drm_ram_used = 0;
static drm_mem_stats_t drm_mem_stats[] =
{
	[DRM_MEM_DMA] = {"dmabufs"},
	[DRM_MEM_SAREA] = {"sareas"},
	[DRM_MEM_DRIVER] = {"driver"},
	[DRM_MEM_MAGIC] = {"magic"},
	[DRM_MEM_IOCTLS] = {"ioctltab"},
	[DRM_MEM_MAPS] = {"maplist"},
	[DRM_MEM_VMAS] = {"vmalist"},
	[DRM_MEM_BUFS] = {"buflist"},
	[DRM_MEM_SEGS] = {"seglist"},
	[DRM_MEM_PAGES] = {"pagelist"},
	[DRM_MEM_FILES] = {"files"},
	[DRM_MEM_QUEUES] = {"queues"},
	[DRM_MEM_CMDS] = {"commands"},
	[DRM_MEM_MAPPINGS] = {"mappings"},
	[DRM_MEM_BUFLISTS] = {"buflists"},
	[DRM_MEM_AGPLISTS] = {"agplist"},
	[DRM_MEM_SGLISTS] = {"sglist"},
	[DRM_MEM_TOTALAGP] = {"totalagp"},
	[DRM_MEM_BOUNDAGP] = {"boundagp"},
	[DRM_MEM_CTXBITMAP] = {"ctxbitmap"},
	[DRM_MEM_CTXLIST] = {"ctxlist"},
	[DRM_MEM_STUB] = {"stub"},
	{NULL, 0,}		/* Last entry must be null */
};

void drm_mem_init (void) {
	drm_mem_stats_t *mem;
	struct sysinfo si;

	for (mem = drm_mem_stats; mem->name; ++mem) {
		mem->succeed_count = 0;
		mem->free_count = 0;
		mem->fail_count = 0;
		mem->bytes_allocated = 0;
		mem->bytes_freed = 0;
	}

	si_meminfo(&si);
	drm_ram_available = si.totalram;
	drm_ram_used = 0;
}

/* drm_mem_info is called whenever a process reads /dev/drm/mem. */

static int drm__mem_info (char *buf, char **start, off_t offset,
			   int request, int *eof, void *data) {
	drm_mem_stats_t *pt;
	int len = 0;

	if (offset > DRM_PROC_LIMIT) {
		*eof = 1;
		return 0;
	}

	*eof = 0;
	*start = &buf[offset];

	DRM_PROC_PRINT("		  total counts			"
		       " |    outstanding  \n");
	DRM_PROC_PRINT("type	   alloc freed fail	bytes	   freed"
		       " | allocs      bytes\n\n");
	DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB         |\n",
		       "system", 0, 0, 0,
		       drm_ram_available << (PAGE_SHIFT - 10));
	DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB         |\n",
		       "locked", 0, 0, 0, drm_ram_used >> 10);
	DRM_PROC_PRINT("\n");
	for (pt = drm_mem_stats; pt->name; pt++) {
		DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
			       pt->name,
			       pt->succeed_count,
			       pt->free_count,
			       pt->fail_count,
			       pt->bytes_allocated,
			       pt->bytes_freed,
			       pt->succeed_count - pt->free_count,
			       (long)pt->bytes_allocated
			       - (long)pt->bytes_freed);
	}

	if (len > request + offset)
		return request;
	*eof = 1;
	return len - offset;
}

int drm_mem_info (char *buf, char **start, off_t offset,
		   int len, int *eof, void *data) {
	int ret;

	spin_lock(&drm_mem_lock);
	ret = drm__mem_info (buf, start, offset, len, eof, data);
	spin_unlock(&drm_mem_lock);
	return ret;
}

void *drm_alloc (size_t size, int area) {
	void *pt;

	if (!size) {
		DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
		return NULL;
	}

	if (!(pt = kmalloc(size, GFP_KERNEL))) {
		spin_lock(&drm_mem_lock);
		++drm_mem_stats[area].fail_count;
		spin_unlock(&drm_mem_lock);
		return NULL;
	}
	spin_lock(&drm_mem_lock);
	++drm_mem_stats[area].succeed_count;
	drm_mem_stats[area].bytes_allocated += size;
	spin_unlock(&drm_mem_lock);
	return pt;
}

void *drm_calloc (size_t nmemb, size_t size, int area) {
	void *addr;

	addr = drm_alloc (nmemb * size, area);
	if (addr != NULL)
		memset((void *)addr, 0, size * nmemb);

	return addr;
}

void *drm_realloc (void *oldpt, size_t oldsize, size_t size, int area) {
	void *pt;

	if (!(pt = drm_alloc (size, area)))
		return NULL;
	if (oldpt && oldsize) {
		memcpy(pt, oldpt, oldsize);
		drm_free (oldpt, oldsize, area);
	}
	return pt;
}

void drm_free (void *pt, size_t size, int area) {
	int alloc_count;
	int free_count;

	if (!pt)
		DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
	else
		kfree(pt);
	spin_lock(&drm_mem_lock);
	drm_mem_stats[area].bytes_freed += size;
	free_count = ++drm_mem_stats[area].free_count;
	alloc_count = drm_mem_stats[area].succeed_count;
	spin_unlock(&drm_mem_lock);
	if (free_count > alloc_count) {
		DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
			      free_count, alloc_count);
	}
}

unsigned long drm_alloc_pages (int order, int area) {
	unsigned long address;
	unsigned long bytes = PAGE_SIZE << order;
	unsigned long addr;
	unsigned int sz;

	spin_lock(&drm_mem_lock);
	if ((drm_ram_used >> PAGE_SHIFT)
	    > (DRM_RAM_PERCENT * drm_ram_available) / 100) {
		spin_unlock(&drm_mem_lock);
		return 0;
	}
	spin_unlock(&drm_mem_lock);

	address = __get_free_pages(GFP_KERNEL, order);
	if (!address) {
		spin_lock(&drm_mem_lock);
		++drm_mem_stats[area].fail_count;
		spin_unlock(&drm_mem_lock);
		return 0;
	}
	spin_lock(&drm_mem_lock);
	++drm_mem_stats[area].succeed_count;
	drm_mem_stats[area].bytes_allocated += bytes;
	drm_ram_used += bytes;
	spin_unlock(&drm_mem_lock);

	/* Zero outside the lock */
	memset((void *)address, 0, bytes);

	/* Reserve */
	for (addr = address, sz = bytes;
	     sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
		SetPageReserved(virt_to_page(addr));
	}

	return address;
}

void drm_free_pages (unsigned long address, int order, int area) {
	unsigned long bytes = PAGE_SIZE << order;
	int alloc_count;
	int free_count;
	unsigned long addr;
	unsigned int sz;

	if (!address) {
		DRM_MEM_ERROR(area, "Attempt to free address 0\n");
	} else {
		/* Unreserve */
		for (addr = address, sz = bytes;
		     sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
			ClearPageReserved(virt_to_page(addr));
		}
		free_pages(address, order);
	}

	spin_lock(&drm_mem_lock);
	free_count = ++drm_mem_stats[area].free_count;
	alloc_count = drm_mem_stats[area].succeed_count;
	drm_mem_stats[area].bytes_freed += bytes;
	drm_ram_used -= bytes;
	spin_unlock(&drm_mem_lock);
	if (free_count > alloc_count) {
		DRM_MEM_ERROR(area,
			      "Excess frees: %d frees, %d allocs\n",
			      free_count, alloc_count);
	}
}

void *drm_ioremap (unsigned long offset, unsigned long size,
		    drm_device_t * dev) {
	void *pt;

	if (!size) {
		DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
			      "Mapping 0 bytes at 0x%08lx\n", offset);
		return NULL;
	}

	if (!(pt = drm_ioremap(offset, size, dev))) {
		spin_lock(&drm_mem_lock);
		++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
		spin_unlock(&drm_mem_lock);
		return NULL;
	}
	spin_lock(&drm_mem_lock);
	++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
	drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
	spin_unlock(&drm_mem_lock);
	return pt;
}

void *drm_ioremap_nocache (unsigned long offset, unsigned long size,
			    drm_device_t * dev) {
	void *pt;

	if (!size) {
		DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
			      "Mapping 0 bytes at 0x%08lx\n", offset);
		return NULL;
	}

	if (!(pt = drm_ioremap_nocache(offset, size, dev))) {
		spin_lock(&drm_mem_lock);
		++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
		spin_unlock(&drm_mem_lock);
		return NULL;
	}
	spin_lock(&drm_mem_lock);
	++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
	drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
	spin_unlock(&drm_mem_lock);
	return pt;
}

void drm_ioremapfree (void *pt, unsigned long size, drm_device_t * dev) {
	int alloc_count;
	int free_count;

	if (!pt)
		DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
			      "Attempt to free NULL pointer\n");
	else
		drm_ioremapfree(pt, size, dev);

	spin_lock(&drm_mem_lock);
	drm_mem_stats[DRM_MEM_MAPPINGS].bytes_freed += size;
	free_count = ++drm_mem_stats[DRM_MEM_MAPPINGS].free_count;
	alloc_count = drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
	spin_unlock(&drm_mem_lock);
	if (free_count > alloc_count) {
		DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
			      "Excess frees: %d frees, %d allocs\n",
			      free_count, alloc_count);
	}
}

#if __OS_HAS_AGP

DRM_AGP_MEM *drm_alloc_agp (drm_device_t *dev, int pages, u32 type) {
	DRM_AGP_MEM *handle;

	if (!pages) {
		DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
		return NULL;
	}

	if ((handle = drm_agp_allocate_memory (pages, type))) {
		spin_lock(&drm_mem_lock);
		++drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
		drm_mem_stats[DRM_MEM_TOTALAGP].bytes_allocated
		    += pages << PAGE_SHIFT;
		spin_unlock(&drm_mem_lock);
		return handle;
	}
	spin_lock(&drm_mem_lock);
	++drm_mem_stats[DRM_MEM_TOTALAGP].fail_count;
	spin_unlock(&drm_mem_lock);
	return NULL;
}

int drm_free_agp (DRM_AGP_MEM * handle, int pages) {
	int alloc_count;
	int free_count;
	int retval = -EINVAL;

	if (!handle) {
		DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
			      "Attempt to free NULL AGP handle\n");
		return retval;
	}

	if (drm_agp_free_memory (handle)) {
		spin_lock(&drm_mem_lock);
		free_count = ++drm_mem_stats[DRM_MEM_TOTALAGP].free_count;
		alloc_count = drm_mem_stats[DRM_MEM_TOTALAGP].succeed_count;
		drm_mem_stats[DRM_MEM_TOTALAGP].bytes_freed
		    += pages << PAGE_SHIFT;
		spin_unlock(&drm_mem_lock);
		if (free_count > alloc_count) {
			DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
				      "Excess frees: %d frees, %d allocs\n",
				      free_count, alloc_count);
		}
		return 0;
	}
	return retval;
}

int drm_bind_agp (DRM_AGP_MEM * handle, unsigned int start) {
	int retcode = -EINVAL;

	if (!handle) {
		DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
			      "Attempt to bind NULL AGP handle\n");
		return retcode;
	}

	if (!(retcode = drm_agp_bind_memory (handle, start))) {
		spin_lock(&drm_mem_lock);
		++drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
		drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_allocated
		    += handle->page_count << PAGE_SHIFT;
		spin_unlock(&drm_mem_lock);
		return retcode;
	}
	spin_lock(&drm_mem_lock);
	++drm_mem_stats[DRM_MEM_BOUNDAGP].fail_count;
	spin_unlock(&drm_mem_lock);
	return retcode;
}

int drm_unbind_agp (DRM_AGP_MEM * handle) {
	int alloc_count;
	int free_count;
	int retcode = -EINVAL;

	if (!handle) {
		DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
			      "Attempt to unbind NULL AGP handle\n");
		return retcode;
	}

	if ((retcode = drm_agp_unbind_memory (handle)))
		return retcode;
	spin_lock(&drm_mem_lock);
	free_count = ++drm_mem_stats[DRM_MEM_BOUNDAGP].free_count;
	alloc_count = drm_mem_stats[DRM_MEM_BOUNDAGP].succeed_count;
	drm_mem_stats[DRM_MEM_BOUNDAGP].bytes_freed
	    += handle->page_count << PAGE_SHIFT;
	spin_unlock(&drm_mem_lock);
	if (free_count > alloc_count) {
		DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
			      "Excess frees: %d frees, %d allocs\n",
			      free_count, alloc_count);
	}
	return retcode;
}
#endif
class="hl opt">) { DRM(free)(map, sizeof(*map), DRM_MEM_MAPS); return -EINVAL; } memset(list, 0, sizeof(*list)); list->map = map; down(&dev->struct_sem); list_add(&list->head, &dev->maplist->head); up(&dev->struct_sem); if ( copy_to_user( (drm_map_t *)arg, map, sizeof(*map) ) ) return -EFAULT; if ( map->type != _DRM_SHM ) { if ( copy_to_user( &((drm_map_t *)arg)->handle, &map->offset, sizeof(map->offset) ) ) return -EFAULT; } return 0; } /** * Remove a map private from list and deallocate resources if the mapping * isn't in use. * * \param inode device inode. * \param filp file pointer. * \param cmd command. * \param arg pointer to a drm_map_t structure. * \return zero on success or a negative value on error. * * Searches the map on drm_device::maplist, removes it from the list, see if * its being used, and free any associate resource (such as MTRR's) if it's not * being on use. * * \sa addmap(). */ int DRM(rmmap)(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { drm_file_t *priv = filp->private_data; drm_device_t *dev = priv->dev; struct list_head *list; drm_map_list_t *r_list = NULL; drm_vma_entry_t *pt, *prev; drm_map_t *map; drm_map_t request; int found_maps = 0; if (copy_from_user(&request, (drm_map_t *)arg, sizeof(request))) { return -EFAULT; } down(&dev->struct_sem); list = &dev->maplist->head; list_for_each(list, &dev->maplist->head) { r_list = list_entry(list, drm_map_list_t, head); if(r_list->map && r_list->map->handle == request.handle && r_list->map->flags & _DRM_REMOVABLE) break; } /* List has wrapped around to the head pointer, or its empty we didn't * find anything. */ if(list == (&dev->maplist->head)) { up(&dev->struct_sem); return -EINVAL; } map = r_list->map; list_del(list); DRM(free)(list, sizeof(*list), DRM_MEM_MAPS); for (pt = dev->vmalist, prev = NULL; pt; prev = pt, pt = pt->next) { if (pt->vma->vm_private_data == map) found_maps++; } if(!found_maps) { switch (map->type) { case _DRM_REGISTERS: case _DRM_FRAME_BUFFER: #if __REALLY_HAVE_MTRR if (map->mtrr >= 0) { int retcode; retcode = mtrr_del(map->mtrr, map->offset, map->size); DRM_DEBUG("mtrr_del = %d\n", retcode); } #endif DRM(ioremapfree)(map->handle, map->size, dev); break; case _DRM_SHM: vfree(map->handle); break; case _DRM_AGP: case _DRM_SCATTER_GATHER: break; } DRM(free)(map, sizeof(*map), DRM_MEM_MAPS); } up(&dev->struct_sem); return 0; } #if __HAVE_DMA /** * Cleanup after an error on one of the addbufs() functions. * * \param entry buffer entry where the error occurred. * * Frees any pages and buffers associated with the given entry. */ static void DRM(cleanup_buf_error)(drm_buf_entry_t *entry) { int i; if (entry->seg_count) { for (i = 0; i < entry->seg_count; i++) { if (entry->seglist[i]) { DRM(free_pages)(entry->seglist[i], entry->page_order, DRM_MEM_DMA); } } DRM(free)(entry->seglist, entry->seg_count * sizeof(*entry->seglist), DRM_MEM_SEGS); entry->seg_count = 0; } if (entry->buf_count) { for (i = 0; i < entry->buf_count; i++) { if (entry->buflist[i].dev_private) { DRM(free)(entry->buflist[i].dev_private, entry->buflist[i].dev_priv_size, DRM_MEM_BUFS); } } DRM(free)(entry->buflist, entry->buf_count * sizeof(*entry->buflist), DRM_MEM_BUFS); #if __HAVE_DMA_FREELIST DRM(freelist_destroy)(&entry->freelist); #endif entry->buf_count = 0; } } #if __REALLY_HAVE_AGP /** * Add AGP buffers for DMA transfers (ioctl). * * \param inode device inode. * \param filp file pointer. * \param cmd command. * \param arg pointer to a drm_buf_desc_t request. * \return zero on success or a negative number on failure. * * After some sanity checks creates a drm_buf structure for each buffer and * reallocates the buffer list of the same size order to accommodate the new * buffers. */ int DRM(addbufs_agp)( struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg ) { drm_file_t *priv = filp->private_data; drm_device_t *dev = priv->dev; drm_device_dma_t *dma = dev->dma; drm_buf_desc_t request; drm_buf_entry_t *entry; drm_buf_t *buf; unsigned long offset; unsigned long agp_offset; int count; int order; int size; int alignment; int page_order; int total; int byte_count; int i; drm_buf_t **temp_buflist; if ( !dma ) return -EINVAL; if ( copy_from_user( &request, (drm_buf_desc_t *)arg, sizeof(request) ) ) return -EFAULT; count = request.count; order = DRM(order)( request.size ); size = 1 << order; alignment = (request.flags & _DRM_PAGE_ALIGN) ? PAGE_ALIGN(size) : size; page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; total = PAGE_SIZE << page_order; byte_count = 0; agp_offset = dev->agp->base + request.agp_start; DRM_DEBUG( "count: %d\n", count ); DRM_DEBUG( "order: %d\n", order ); DRM_DEBUG( "size: %d\n", size ); DRM_DEBUG( "agp_offset: %lu\n", agp_offset ); DRM_DEBUG( "alignment: %d\n", alignment ); DRM_DEBUG( "page_order: %d\n", page_order ); DRM_DEBUG( "total: %d\n", total ); if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL; if ( dev->queue_count ) return -EBUSY; /* Not while in use */ spin_lock( &dev->count_lock ); if ( dev->buf_use ) { spin_unlock( &dev->count_lock ); return -EBUSY; } atomic_inc( &dev->buf_alloc ); spin_unlock( &dev->count_lock ); down( &dev->struct_sem ); entry = &dma->bufs[order]; if ( entry->buf_count ) { up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; /* May only call once for each order */ } if (count < 0 || count > 4096) { up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -EINVAL; } entry->buflist = DRM(alloc)( count * sizeof(*entry->buflist), DRM_MEM_BUFS ); if ( !entry->buflist ) { up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; } memset( entry->buflist, 0, count * sizeof(*entry->buflist) ); entry->buf_size = size; entry->page_order = page_order; offset = 0; while ( entry->buf_count < count ) { buf = &entry->buflist[entry->buf_count]; buf->idx = dma->buf_count + entry->buf_count; buf->total = alignment; buf->order = order; buf->used = 0; buf->offset = (dma->byte_count + offset); buf->bus_address = agp_offset + offset; buf->address = (void *)(agp_offset + offset); buf->next = NULL; buf->waiting = 0; buf->pending = 0; init_waitqueue_head( &buf->dma_wait ); buf->filp = 0; buf->dev_priv_size = sizeof(DRIVER_BUF_PRIV_T); buf->dev_private = DRM(alloc)( sizeof(DRIVER_BUF_PRIV_T), DRM_MEM_BUFS ); if(!buf->dev_private) { /* Set count correctly so we free the proper amount. */ entry->buf_count = count; DRM(cleanup_buf_error)(entry); up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; } memset( buf->dev_private, 0, buf->dev_priv_size ); DRM_DEBUG( "buffer %d @ %p\n", entry->buf_count, buf->address ); offset += alignment; entry->buf_count++; byte_count += PAGE_SIZE << page_order; } DRM_DEBUG( "byte_count: %d\n", byte_count ); temp_buflist = DRM(realloc)( dma->buflist, dma->buf_count * sizeof(*dma->buflist), (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), DRM_MEM_BUFS ); if(!temp_buflist) { /* Free the entry because it isn't valid */ DRM(cleanup_buf_error)(entry); up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; } dma->buflist = temp_buflist; for ( i = 0 ; i < entry->buf_count ; i++ ) { dma->buflist[i + dma->buf_count] = &entry->buflist[i]; } dma->buf_count += entry->buf_count; dma->byte_count += byte_count; DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count ); DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count ); #if __HAVE_DMA_FREELIST DRM(freelist_create)( &entry->freelist, entry->buf_count ); for ( i = 0 ; i < entry->buf_count ; i++ ) { DRM(freelist_put)( dev, &entry->freelist, &entry->buflist[i] ); } #endif up( &dev->struct_sem ); request.count = entry->buf_count; request.size = size; if ( copy_to_user( (drm_buf_desc_t *)arg, &request, sizeof(request) ) ) return -EFAULT; dma->flags = _DRM_DMA_USE_AGP; atomic_dec( &dev->buf_alloc ); return 0; } #endif /* __REALLY_HAVE_AGP */ #if __HAVE_PCI_DMA int DRM(addbufs_pci)( struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg ) { drm_file_t *priv = filp->private_data; drm_device_t *dev = priv->dev; drm_device_dma_t *dma = dev->dma; drm_buf_desc_t request; int count; int order; int size; int total; int page_order; drm_buf_entry_t *entry; unsigned long page; drm_buf_t *buf; int alignment; unsigned long offset; int i; int byte_count; int page_count; unsigned long *temp_pagelist; drm_buf_t **temp_buflist; if ( !dma ) return -EINVAL; if ( copy_from_user( &request, (drm_buf_desc_t *)arg, sizeof(request) ) ) return -EFAULT; count = request.count; order = DRM(order)( request.size ); size = 1 << order; DRM_DEBUG( "count=%d, size=%d (%d), order=%d, queue_count=%d\n", request.count, request.size, size, order, dev->queue_count ); if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL; if ( dev->queue_count ) return -EBUSY; /* Not while in use */ alignment = (request.flags & _DRM_PAGE_ALIGN) ? PAGE_ALIGN(size) : size; page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0; total = PAGE_SIZE << page_order; spin_lock( &dev->count_lock ); if ( dev->buf_use ) { spin_unlock( &dev->count_lock ); return -EBUSY; } atomic_inc( &dev->buf_alloc ); spin_unlock( &dev->count_lock ); down( &dev->struct_sem ); entry = &dma->bufs[order]; if ( entry->buf_count ) { up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; /* May only call once for each order */ } if (count < 0 || count > 4096) { up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -EINVAL; } entry->buflist = DRM(alloc)( count * sizeof(*entry->buflist), DRM_MEM_BUFS ); if ( !entry->buflist ) { up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; } memset( entry->buflist, 0, count * sizeof(*entry->buflist) ); entry->seglist = DRM(alloc)( count * sizeof(*entry->seglist), DRM_MEM_SEGS ); if ( !entry->seglist ) { DRM(free)( entry->buflist, count * sizeof(*entry->buflist), DRM_MEM_BUFS ); up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; } memset( entry->seglist, 0, count * sizeof(*entry->seglist) ); /* Keep the original pagelist until we know all the allocations * have succeeded */ temp_pagelist = DRM(alloc)( (dma->page_count + (count << page_order)) * sizeof(*dma->pagelist), DRM_MEM_PAGES ); if (!temp_pagelist) { DRM(free)( entry->buflist, count * sizeof(*entry->buflist), DRM_MEM_BUFS ); DRM(free)( entry->seglist, count * sizeof(*entry->seglist), DRM_MEM_SEGS ); up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; } memcpy(temp_pagelist, dma->pagelist, dma->page_count * sizeof(*dma->pagelist)); DRM_DEBUG( "pagelist: %d entries\n", dma->page_count + (count << page_order) ); entry->buf_size = size; entry->page_order = page_order; byte_count = 0; page_count = 0; while ( entry->buf_count < count ) { page = DRM(alloc_pages)( page_order, DRM_MEM_DMA ); if ( !page ) { /* Set count correctly so we free the proper amount. */ entry->buf_count = count; entry->seg_count = count; DRM(cleanup_buf_error)(entry); DRM(free)( temp_pagelist, (dma->page_count + (count << page_order)) * sizeof(*dma->pagelist), DRM_MEM_PAGES ); up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; } entry->seglist[entry->seg_count++] = page; for ( i = 0 ; i < (1 << page_order) ; i++ ) { DRM_DEBUG( "page %d @ 0x%08lx\n", dma->page_count + page_count, page + PAGE_SIZE * i ); temp_pagelist[dma->page_count + page_count++] = page + PAGE_SIZE * i; } for ( offset = 0 ; offset + size <= total && entry->buf_count < count ; offset += alignment, ++entry->buf_count ) { buf = &entry->buflist[entry->buf_count]; buf->idx = dma->buf_count + entry->buf_count; buf->total = alignment; buf->order = order; buf->used = 0; buf->offset = (dma->byte_count + byte_count + offset); buf->address = (void *)(page + offset); buf->next = NULL; buf->waiting = 0; buf->pending = 0; init_waitqueue_head( &buf->dma_wait ); buf->filp = 0; buf->dev_priv_size = sizeof(DRIVER_BUF_PRIV_T); buf->dev_private = DRM(alloc)( sizeof(DRIVER_BUF_PRIV_T), DRM_MEM_BUFS ); if(!buf->dev_private) { /* Set count correctly so we free the proper amount. */ entry->buf_count = count; entry->seg_count = count; DRM(cleanup_buf_error)(entry); DRM(free)( temp_pagelist, (dma->page_count + (count << page_order)) * sizeof(*dma->pagelist), DRM_MEM_PAGES ); up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; } memset( buf->dev_private, 0, buf->dev_priv_size ); DRM_DEBUG( "buffer %d @ %p\n", entry->buf_count, buf->address ); } byte_count += PAGE_SIZE << page_order; } temp_buflist = DRM(realloc)( dma->buflist, dma->buf_count * sizeof(*dma->buflist), (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist), DRM_MEM_BUFS ); if (!temp_buflist) { /* Free the entry because it isn't valid */ DRM(cleanup_buf_error)(entry); DRM(free)( temp_pagelist, (dma->page_count + (count << page_order)) * sizeof(*dma->pagelist), DRM_MEM_PAGES ); up( &dev->struct_sem ); atomic_dec( &dev->buf_alloc ); return -ENOMEM; } dma->buflist = temp_buflist; for ( i = 0 ; i < entry->buf_count ; i++ ) { dma->buflist[i + dma->buf_count] = &entry->buflist[i]; } /* No allocations failed, so now we can replace the orginal pagelist * with the new one. */ if (dma->page_count) { DRM(free)(dma->pagelist, dma->page_count * sizeof(*dma->pagelist), DRM_MEM_PAGES); } dma->pagelist = temp_pagelist; dma->buf_count += entry->buf_count; dma->seg_count += entry->seg_count; dma->page_count += entry->seg_count << page_order; dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order); #if __HAVE_DMA_FREELIST DRM(freelist_create)( &entry->freelist, entry->buf_count ); for ( i = 0 ; i < entry->buf_count ; i++ ) { DRM(freelist_put)( dev, &entry->freelist, &entry->buflist[i] ); } #endif up( &dev->struct_sem ); request.count = entry->buf_count; request.size = size; if ( copy_to_user( (drm_buf_desc_t *)arg, &request, sizeof(request) ) ) return -EFAULT; atomic_dec( &dev->buf_alloc ); return 0; } #endif /* __HAVE_PCI_DMA */ #if __HAVE_SG int DRM(addbufs_sg)( struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg ) { drm_file_t *priv = filp->private_data; drm_device_t *dev = priv->dev; drm_device_dma_t *dma = dev->dma; drm_buf_desc_t request; drm_buf_entry_t *entry; drm_buf_t *buf; unsigned long offset; unsigned long agp_offset; int count; int order; int size; int alignment; int page_order; int total; int byte_count; int i; drm_buf_t **temp_buflist; if ( !dma ) return -EINVAL; if ( copy_from_user( &request, (drm_buf_desc_t *)arg, sizeof(request) ) ) return -EFAULT; count = request.count; order = DRM(order)( request.size ); size = 1 << order; alignment = (request.flags & _DRM_PAGE_ALIGN) ? PAGE_ALIGN(size) : size;