drm_bufs.c 40.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/**
Dave Airlie's avatar
Dave Airlie committed
2
 * \file drm_bufs.c
Linus Torvalds's avatar
Linus Torvalds committed
3
 * Generic buffer template
Dave Airlie's avatar
Dave Airlie committed
4
 *
Linus Torvalds's avatar
Linus Torvalds committed
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
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 */

/*
 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
 *
 * Copyright 1999, 2000 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 <linux/vmalloc.h>
#include "drmP.h"

Dave Airlie's avatar
Dave Airlie committed
39
unsigned long drm_get_resource_start(drm_device_t * dev, unsigned int resource)
Linus Torvalds's avatar
Linus Torvalds committed
40
{
Dave Airlie's avatar
Dave Airlie committed
41
42
	return pci_resource_start(dev->pdev, resource);
}
Dave Airlie's avatar
Dave Airlie committed
43

Dave Airlie's avatar
Dave Airlie committed
44
EXPORT_SYMBOL(drm_get_resource_start);
Linus Torvalds's avatar
Linus Torvalds committed
45

Dave Airlie's avatar
Dave Airlie committed
46
unsigned long drm_get_resource_len(drm_device_t * dev, unsigned int resource)
Dave Airlie's avatar
Dave Airlie committed
47
48
49
{
	return pci_resource_len(dev->pdev, resource);
}
Dave Airlie's avatar
Dave Airlie committed
50

Dave Airlie's avatar
Dave Airlie committed
51
EXPORT_SYMBOL(drm_get_resource_len);
Linus Torvalds's avatar
Linus Torvalds committed
52

Dave Airlie's avatar
Dave Airlie committed
53
54
static drm_map_list_t *drm_find_matching_map(drm_device_t * dev,
					     drm_local_map_t * map)
Dave Airlie's avatar
Dave Airlie committed
55
56
{
	struct list_head *list;
Linus Torvalds's avatar
Linus Torvalds committed
57

Dave Airlie's avatar
Dave Airlie committed
58
59
60
61
	list_for_each(list, &dev->maplist->head) {
		drm_map_list_t *entry = list_entry(list, drm_map_list_t, head);
		if (entry->map && map->type == entry->map->type &&
		    entry->map->offset == map->offset) {
62
			return entry;
Dave Airlie's avatar
Dave Airlie committed
63
64
65
66
		}
	}

	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
67
68
}

69
/*
70
 * Used to allocate 32-bit handles for mappings.
71
 */
72
73
74
75
#define START_RANGE 0x10000000
#define END_RANGE 0x40000000

#ifdef _LP64
Dave Airlie's avatar
Dave Airlie committed
76
77
static __inline__ unsigned int HandleID(unsigned long lhandle,
					drm_device_t * dev)
78
79
80
81
82
83
84
85
86
{
	static unsigned int map32_handle = START_RANGE;
	unsigned int hash;

	if (lhandle & 0xffffffff00000000) {
		hash = map32_handle;
		map32_handle += PAGE_SIZE;
		if (map32_handle > END_RANGE)
			map32_handle = START_RANGE;
Dave Airlie's avatar
Dave Airlie committed
87
	} else
88
89
90
91
		hash = lhandle;

	while (1) {
		drm_map_list_t *_entry;
Dave Airlie's avatar
Dave Airlie committed
92
		list_for_each_entry(_entry, &dev->maplist->head, head) {
93
94
95
96
97
98
99
100
101
102
103
104
			if (_entry->user_token == hash)
				break;
		}
		if (&_entry->head == &dev->maplist->head)
			return hash;

		hash += PAGE_SIZE;
		map32_handle += PAGE_SIZE;
	}
}
#else
# define HandleID(x,dev) (unsigned int)(x)
105
106
#endif

Linus Torvalds's avatar
Linus Torvalds committed
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
 *
 * \param inode device inode.
 * \param filp file pointer.
 * \param cmd command.
 * \param arg pointer to a drm_map structure.
 * \return zero on success or a negative value on error.
 *
 * Adjusts the memory offset to its absolute value according to the mapping
 * type.  Adds the map to the map list drm_device::maplist. Adds MTRR's where
 * applicable and if supported by the kernel.
 */
120
121
int drm_addmap_core(drm_device_t * dev, unsigned int offset,
		    unsigned int size, drm_map_type_t type,
Dave Airlie's avatar
Dave Airlie committed
122
		    drm_map_flags_t flags, drm_map_list_t ** maplist)
Linus Torvalds's avatar
Linus Torvalds committed
123
124
125
{
	drm_map_t *map;
	drm_map_list_t *list;
126
	drm_dma_handle_t *dmah;
Linus Torvalds's avatar
Linus Torvalds committed
127

Dave Airlie's avatar
Dave Airlie committed
128
129
	map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
	if (!map)
Linus Torvalds's avatar
Linus Torvalds committed
130
131
		return -ENOMEM;

132
133
134
135
	map->offset = offset;
	map->size = size;
	map->flags = flags;
	map->type = type;
Linus Torvalds's avatar
Linus Torvalds committed
136
137
138
139
140

	/* Only allow shared memory to be removable since we only keep enough
	 * book keeping information about shared memory to allow for removal
	 * when processes fork.
	 */
Dave Airlie's avatar
Dave Airlie committed
141
142
	if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
143
144
		return -EINVAL;
	}
Dave Airlie's avatar
Dave Airlie committed
145
146
147
148
	DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
		  map->offset, map->size, map->type);
	if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
149
150
		return -EINVAL;
	}
Dave Airlie's avatar
Dave Airlie committed
151
	map->mtrr = -1;
Linus Torvalds's avatar
Linus Torvalds committed
152
153
	map->handle = NULL;

Dave Airlie's avatar
Dave Airlie committed
154
	switch (map->type) {
Linus Torvalds's avatar
Linus Torvalds committed
155
156
	case _DRM_REGISTERS:
	case _DRM_FRAME_BUFFER:
Dave Airlie's avatar
Dave Airlie committed
157
#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie's avatar
Dave Airlie committed
158
159
160
		if (map->offset + map->size < map->offset ||
		    map->offset < virt_to_phys(high_memory)) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
161
162
163
164
165
166
			return -EINVAL;
		}
#endif
#ifdef __alpha__
		map->offset += dev->hose->mem_space->start;
#endif
Dave Airlie's avatar
Dave Airlie committed
167
168
169
170
		/* Some drivers preinitialize some maps, without the X Server
		 * needing to be aware of it.  Therefore, we just return success
		 * when the server tries to create a duplicate map.
		 */
171
172
173
		list = drm_find_matching_map(dev, map);
		if (list != NULL) {
			if (list->map->size != map->size) {
Dave Airlie's avatar
Dave Airlie committed
174
				DRM_DEBUG("Matching maps of type %d with "
Dave Airlie's avatar
Dave Airlie committed
175
176
177
					  "mismatched sizes, (%ld vs %ld)\n",
					  map->type, map->size,
					  list->map->size);
178
				list->map->size = map->size;
Dave Airlie's avatar
Dave Airlie committed
179
180
181
			}

			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
182
			*maplist = list;
Dave Airlie's avatar
Dave Airlie committed
183
184
185
			return 0;
		}

Linus Torvalds's avatar
Linus Torvalds committed
186
		if (drm_core_has_MTRR(dev)) {
Dave Airlie's avatar
Dave Airlie committed
187
188
189
190
			if (map->type == _DRM_FRAME_BUFFER ||
			    (map->flags & _DRM_WRITE_COMBINING)) {
				map->mtrr = mtrr_add(map->offset, map->size,
						     MTRR_TYPE_WRCOMB, 1);
Linus Torvalds's avatar
Linus Torvalds committed
191
192
193
			}
		}
		if (map->type == _DRM_REGISTERS)
Dave Airlie's avatar
Dave Airlie committed
194
			map->handle = drm_ioremap(map->offset, map->size, dev);
Linus Torvalds's avatar
Linus Torvalds committed
195
196
197
198
		break;

	case _DRM_SHM:
		map->handle = vmalloc_32(map->size);
Dave Airlie's avatar
Dave Airlie committed
199
200
201
202
		DRM_DEBUG("%lu %d %p\n",
			  map->size, drm_order(map->size), map->handle);
		if (!map->handle) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
203
204
205
			return -ENOMEM;
		}
		map->offset = (unsigned long)map->handle;
Dave Airlie's avatar
Dave Airlie committed
206
		if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds's avatar
Linus Torvalds committed
207
208
			/* Prevent a 2nd X Server from creating a 2nd lock */
			if (dev->lock.hw_lock != NULL) {
Dave Airlie's avatar
Dave Airlie committed
209
210
				vfree(map->handle);
				drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
211
212
				return -EBUSY;
			}
Dave Airlie's avatar
Dave Airlie committed
213
			dev->sigdata.lock = dev->lock.hw_lock = map->handle;	/* Pointer to lock */
Linus Torvalds's avatar
Linus Torvalds committed
214
215
216
217
218
219
220
221
		}
		break;
	case _DRM_AGP:
		if (drm_core_has_AGP(dev)) {
#ifdef __alpha__
			map->offset += dev->hose->mem_space->start;
#endif
			map->offset += dev->agp->base;
Dave Airlie's avatar
Dave Airlie committed
222
			map->mtrr = dev->agp->agp_mtrr;	/* for getmap */
Linus Torvalds's avatar
Linus Torvalds committed
223
224
225
226
227
228
229
		}
		break;
	case _DRM_SCATTER_GATHER:
		if (!dev->sg) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EINVAL;
		}
230
		map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds's avatar
Linus Torvalds committed
231
		break;
Dave Airlie's avatar
Dave Airlie committed
232
	case _DRM_CONSISTENT:
233
		/* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
234
		 * As we're limiting the address to 2^32-1 (or less),
235
236
		 * casting it down to 32 bits is no problem, but we
		 * need to point to a 64bit variable first. */
237
238
		dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
		if (!dmah) {
239
240
241
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -ENOMEM;
		}
242
243
244
		map->handle = dmah->vaddr;
		map->offset = (unsigned long)dmah->busaddr;
		kfree(dmah);
245
		break;
Linus Torvalds's avatar
Linus Torvalds committed
246
	default:
Dave Airlie's avatar
Dave Airlie committed
247
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
248
249
250
251
		return -EINVAL;
	}

	list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlie's avatar
Dave Airlie committed
252
	if (!list) {
Linus Torvalds's avatar
Linus Torvalds committed
253
254
255
256
257
258
259
260
		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);
261
	/* Assign a 32-bit handle */
262
	/* We do it here so that dev->struct_sem protects the increment */
Dave Airlie's avatar
Dave Airlie committed
263
	list->user_token = HandleID(map->type == _DRM_SHM
264
265
				    ? (unsigned long)map->handle
				    : map->offset, dev);
Dave Airlie's avatar
Dave Airlie committed
266
	up(&dev->struct_sem);
Linus Torvalds's avatar
Linus Torvalds committed
267

268
	*maplist = list;
269
270
	return 0;
}
271

Dave Airlie's avatar
Dave Airlie committed
272
int drm_addmap(drm_device_t * dev, unsigned int offset,
273
	       unsigned int size, drm_map_type_t type,
Dave Airlie's avatar
Dave Airlie committed
274
	       drm_map_flags_t flags, drm_local_map_t ** map_ptr)
275
276
277
278
279
280
281
282
283
{
	drm_map_list_t *list;
	int rc;

	rc = drm_addmap_core(dev, offset, size, type, flags, &list);
	if (!rc)
		*map_ptr = list->map;
	return rc;
}
Dave Airlie's avatar
Dave Airlie committed
284

285
286
287
288
289
290
291
292
EXPORT_SYMBOL(drm_addmap);

int drm_addmap_ioctl(struct inode *inode, struct file *filp,
		     unsigned int cmd, unsigned long arg)
{
	drm_file_t *priv = filp->private_data;
	drm_device_t *dev = priv->head->dev;
	drm_map_t map;
293
	drm_map_list_t *maplist;
294
295
296
297
298
299
	drm_map_t __user *argp = (void __user *)arg;
	int err;

	if (!(filp->f_mode & 3))
		return -EACCES;	/* Require read/write */

Dave Airlie's avatar
Dave Airlie committed
300
	if (copy_from_user(&map, argp, sizeof(map))) {
Linus Torvalds's avatar
Linus Torvalds committed
301
		return -EFAULT;
302
303
	}

304
305
	err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
			      &maplist);
306

Dave Airlie's avatar
Dave Airlie committed
307
	if (err)
308
		return err;
309

310
	if (copy_to_user(argp, maplist->map, sizeof(drm_map_t)))
311
		return -EFAULT;
312
	if (put_user((void *)maplist->user_token, &argp->handle))
313
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
314
	return 0;
Dave Airlie's avatar
Dave Airlie committed
315
}
Linus Torvalds's avatar
Linus Torvalds committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

/**
 * 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.
 *
331
 * \sa drm_addmap
Linus Torvalds's avatar
Linus Torvalds committed
332
 */
Dave Airlie's avatar
Dave Airlie committed
333
int drm_rmmap_locked(drm_device_t * dev, drm_local_map_t * map)
Linus Torvalds's avatar
Linus Torvalds committed
334
335
336
{
	struct list_head *list;
	drm_map_list_t *r_list = NULL;
Dave Airlie's avatar
Dave Airlie committed
337
	drm_dma_handle_t dmah;
Linus Torvalds's avatar
Linus Torvalds committed
338

Dave Airlie's avatar
Dave Airlie committed
339
	/* Find the list entry for the map and remove it */
Linus Torvalds's avatar
Linus Torvalds committed
340
341
342
	list_for_each(list, &dev->maplist->head) {
		r_list = list_entry(list, drm_map_list_t, head);

Dave Airlie's avatar
Dave Airlie committed
343
344
345
346
347
		if (r_list->map == map) {
			list_del(list);
			drm_free(list, sizeof(*list), DRM_MEM_MAPS);
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
348
349
	}

Dave Airlie's avatar
Dave Airlie committed
350
351
	/* List has wrapped around to the head pointer, or it's empty and we
	 * didn't find anything.
Linus Torvalds's avatar
Linus Torvalds committed
352
	 */
Dave Airlie's avatar
Dave Airlie committed
353
	if (list == (&dev->maplist->head)) {
Linus Torvalds's avatar
Linus Torvalds committed
354
355
356
		return -EINVAL;
	}

Dave Airlie's avatar
Dave Airlie committed
357
358
359
360
361
362
363
	switch (map->type) {
	case _DRM_REGISTERS:
		drm_ioremapfree(map->handle, map->size, dev);
		/* FALLTHROUGH */
	case _DRM_FRAME_BUFFER:
		if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
			int retcode;
Dave Airlie's avatar
Dave Airlie committed
364
365
			retcode = mtrr_del(map->mtrr, map->offset, map->size);
			DRM_DEBUG("mtrr_del=%d\n", retcode);
Linus Torvalds's avatar
Linus Torvalds committed
366
		}
Dave Airlie's avatar
Dave Airlie committed
367
368
369
370
371
372
373
374
375
376
377
378
379
		break;
	case _DRM_SHM:
		vfree(map->handle);
		break;
	case _DRM_AGP:
	case _DRM_SCATTER_GATHER:
		break;
	case _DRM_CONSISTENT:
		dmah.vaddr = map->handle;
		dmah.busaddr = map->offset;
		dmah.size = map->size;
		__drm_pci_free(dev, &dmah);
		break;
Linus Torvalds's avatar
Linus Torvalds committed
380
	}
Dave Airlie's avatar
Dave Airlie committed
381
382
	drm_free(map, sizeof(*map), DRM_MEM_MAPS);

Linus Torvalds's avatar
Linus Torvalds committed
383
384
	return 0;
}
Dave Airlie's avatar
Dave Airlie committed
385

Dave Airlie's avatar
Dave Airlie committed
386
387
EXPORT_SYMBOL(drm_rmmap_locked);

Dave Airlie's avatar
Dave Airlie committed
388
int drm_rmmap(drm_device_t * dev, drm_local_map_t * map)
Dave Airlie's avatar
Dave Airlie committed
389
390
391
392
393
394
395
396
397
{
	int ret;

	down(&dev->struct_sem);
	ret = drm_rmmap_locked(dev, map);
	up(&dev->struct_sem);

	return ret;
}
Dave Airlie's avatar
Dave Airlie committed
398

399
400
EXPORT_SYMBOL(drm_rmmap);

Dave Airlie's avatar
Dave Airlie committed
401
402
403
404
405
406
407
408
409
/* The rmmap ioctl appears to be unnecessary.  All mappings are torn down on
 * the last close of the device, and this is necessary for cleanup when things
 * exit uncleanly.  Therefore, having userland manually remove mappings seems
 * like a pointless exercise since they're going away anyway.
 *
 * One use case might be after addmap is allowed for normal users for SHM and
 * gets used by drivers that the server doesn't need to care about.  This seems
 * unlikely.
 */
410
411
412
413
414
415
int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
		    unsigned int cmd, unsigned long arg)
{
	drm_file_t *priv = filp->private_data;
	drm_device_t *dev = priv->head->dev;
	drm_map_t request;
Dave Airlie's avatar
Dave Airlie committed
416
417
418
	drm_local_map_t *map = NULL;
	struct list_head *list;
	int ret;
419

Dave Airlie's avatar
Dave Airlie committed
420
	if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {
421
422
423
		return -EFAULT;
	}

Dave Airlie's avatar
Dave Airlie committed
424
425
426
427
428
	down(&dev->struct_sem);
	list_for_each(list, &dev->maplist->head) {
		drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);

		if (r_list->map &&
Dave Airlie's avatar
Dave Airlie committed
429
		    r_list->user_token == (unsigned long)request.handle &&
Dave Airlie's avatar
Dave Airlie committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
		    r_list->map->flags & _DRM_REMOVABLE) {
			map = r_list->map;
			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;
	}

	if (!map)
		return -EINVAL;

	/* Register and framebuffer maps are permanent */
	if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
		up(&dev->struct_sem);
		return 0;
	}

	ret = drm_rmmap_locked(dev, map);

	up(&dev->struct_sem);

	return ret;
458
}
Linus Torvalds's avatar
Linus Torvalds committed
459
460
461
462

/**
 * Cleanup after an error on one of the addbufs() functions.
 *
Dave Airlie's avatar
Dave Airlie committed
463
 * \param dev DRM device.
Linus Torvalds's avatar
Linus Torvalds committed
464
465
466
467
 * \param entry buffer entry where the error occurred.
 *
 * Frees any pages and buffers associated with the given entry.
 */
Dave Airlie's avatar
Dave Airlie committed
468
static void drm_cleanup_buf_error(drm_device_t * dev, drm_buf_entry_t * entry)
Linus Torvalds's avatar
Linus Torvalds committed
469
470
471
472
473
474
475
{
	int i;

	if (entry->seg_count) {
		for (i = 0; i < entry->seg_count; i++) {
			if (entry->seglist[i]) {
				drm_free_pages(entry->seglist[i],
Dave Airlie's avatar
Dave Airlie committed
476
					       entry->page_order, DRM_MEM_DMA);
Linus Torvalds's avatar
Linus Torvalds committed
477
478
479
			}
		}
		drm_free(entry->seglist,
Dave Airlie's avatar
Dave Airlie committed
480
481
			 entry->seg_count *
			 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds's avatar
Linus Torvalds committed
482
483
484
485

		entry->seg_count = 0;
	}

Dave Airlie's avatar
Dave Airlie committed
486
487
	if (entry->buf_count) {
		for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
488
489
			if (entry->buflist[i].dev_private) {
				drm_free(entry->buflist[i].dev_private,
Dave Airlie's avatar
Dave Airlie committed
490
491
					 entry->buflist[i].dev_priv_size,
					 DRM_MEM_BUFS);
Linus Torvalds's avatar
Linus Torvalds committed
492
493
494
			}
		}
		drm_free(entry->buflist,
Dave Airlie's avatar
Dave Airlie committed
495
496
			 entry->buf_count *
			 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds's avatar
Linus Torvalds committed
497
498
499
500
501
502
503

		entry->buf_count = 0;
	}
}

#if __OS_HAS_AGP
/**
504
 * Add AGP buffers for DMA transfers.
Linus Torvalds's avatar
Linus Torvalds committed
505
 *
506
507
 * \param dev drm_device_t to which the buffers are to be added.
 * \param request pointer to a drm_buf_desc_t describing the request.
Linus Torvalds's avatar
Linus Torvalds committed
508
 * \return zero on success or a negative number on failure.
Dave Airlie's avatar
Dave Airlie committed
509
 *
Linus Torvalds's avatar
Linus Torvalds committed
510
511
512
513
 * 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.
 */
Dave Airlie's avatar
Dave Airlie committed
514
int drm_addbufs_agp(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds's avatar
Linus Torvalds committed
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
{
	drm_device_dma_t *dma = dev->dma;
	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;

Dave Airlie's avatar
Dave Airlie committed
531
532
	if (!dma)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
533

534
535
	count = request->count;
	order = drm_order(request->size);
Linus Torvalds's avatar
Linus Torvalds committed
536
537
	size = 1 << order;

Dave Airlie's avatar
Dave Airlie committed
538
539
	alignment = (request->flags & _DRM_PAGE_ALIGN)
	    ? PAGE_ALIGN(size) : size;
Linus Torvalds's avatar
Linus Torvalds committed
540
541
542
543
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

	byte_count = 0;
544
	agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds's avatar
Linus Torvalds committed
545

Dave Airlie's avatar
Dave Airlie committed
546
547
548
549
550
551
552
	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);
Linus Torvalds's avatar
Linus Torvalds committed
553

Dave Airlie's avatar
Dave Airlie committed
554
555
556
557
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
	if (dev->queue_count)
		return -EBUSY;	/* Not while in use */
Linus Torvalds's avatar
Linus Torvalds committed
558

Dave Airlie's avatar
Dave Airlie committed
559
560
561
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
562
563
		return -EBUSY;
	}
Dave Airlie's avatar
Dave Airlie committed
564
565
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
566

Dave Airlie's avatar
Dave Airlie committed
567
	down(&dev->struct_sem);
Linus Torvalds's avatar
Linus Torvalds committed
568
	entry = &dma->bufs[order];
Dave Airlie's avatar
Dave Airlie committed
569
570
571
572
	if (entry->buf_count) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
Linus Torvalds's avatar
Linus Torvalds committed
573
574
575
	}

	if (count < 0 || count > 4096) {
Dave Airlie's avatar
Dave Airlie committed
576
577
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
578
579
580
		return -EINVAL;
	}

Dave Airlie's avatar
Dave Airlie committed
581
582
583
584
585
	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
586
587
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
588
	memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds's avatar
Linus Torvalds committed
589
590
591
592
593
594

	entry->buf_size = size;
	entry->page_order = page_order;

	offset = 0;

Dave Airlie's avatar
Dave Airlie committed
595
596
597
598
599
600
	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;
Linus Torvalds's avatar
Linus Torvalds committed
601

Dave Airlie's avatar
Dave Airlie committed
602
		buf->offset = (dma->byte_count + offset);
Linus Torvalds's avatar
Linus Torvalds committed
603
604
		buf->bus_address = agp_offset + offset;
		buf->address = (void *)(agp_offset + offset);
Dave Airlie's avatar
Dave Airlie committed
605
		buf->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
606
607
		buf->waiting = 0;
		buf->pending = 0;
Dave Airlie's avatar
Dave Airlie committed
608
609
		init_waitqueue_head(&buf->dma_wait);
		buf->filp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
610
611

		buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlie's avatar
Dave Airlie committed
612
613
		buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
		if (!buf->dev_private) {
Linus Torvalds's avatar
Linus Torvalds committed
614
615
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
Dave Airlie's avatar
Dave Airlie committed
616
617
618
			drm_cleanup_buf_error(dev, entry);
			up(&dev->struct_sem);
			atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
619
620
			return -ENOMEM;
		}
Dave Airlie's avatar
Dave Airlie committed
621
		memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds's avatar
Linus Torvalds committed
622

Dave Airlie's avatar
Dave Airlie committed
623
		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds's avatar
Linus Torvalds committed
624
625
626
627
628
629

		offset += alignment;
		entry->buf_count++;
		byte_count += PAGE_SIZE << page_order;
	}

Dave Airlie's avatar
Dave Airlie committed
630
	DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
631

Dave Airlie's avatar
Dave Airlie committed
632
633
634
635
636
	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) {
Linus Torvalds's avatar
Linus Torvalds committed
637
		/* Free the entry because it isn't valid */
Dave Airlie's avatar
Dave Airlie committed
638
639
640
		drm_cleanup_buf_error(dev, entry);
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
641
642
643
644
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

Dave Airlie's avatar
Dave Airlie committed
645
	for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
646
647
648
649
650
651
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

	dma->buf_count += entry->buf_count;
	dma->byte_count += byte_count;

Dave Airlie's avatar
Dave Airlie committed
652
653
	DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
	DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds's avatar
Linus Torvalds committed
654

Dave Airlie's avatar
Dave Airlie committed
655
	up(&dev->struct_sem);
Linus Torvalds's avatar
Linus Torvalds committed
656

657
658
	request->count = entry->buf_count;
	request->size = size;
Linus Torvalds's avatar
Linus Torvalds committed
659
660
661

	dma->flags = _DRM_DMA_USE_AGP;

Dave Airlie's avatar
Dave Airlie committed
662
	atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
663
664
	return 0;
}
Dave Airlie's avatar
Dave Airlie committed
665

666
EXPORT_SYMBOL(drm_addbufs_agp);
Dave Airlie's avatar
Dave Airlie committed
667
#endif				/* __OS_HAS_AGP */
Linus Torvalds's avatar
Linus Torvalds committed
668

Dave Airlie's avatar
Dave Airlie committed
669
int drm_addbufs_pci(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds's avatar
Linus Torvalds committed
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
{
	drm_device_dma_t *dma = dev->dma;
	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;

Dave Airlie's avatar
Dave Airlie committed
688
689
690
691
	if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
		return -EINVAL;
	if (!dma)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
692

693
694
	count = request->count;
	order = drm_order(request->size);
Linus Torvalds's avatar
Linus Torvalds committed
695
696
	size = 1 << order;

Dave Airlie's avatar
Dave Airlie committed
697
698
	DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
		  request->count, request->size, size, order, dev->queue_count);
Linus Torvalds's avatar
Linus Torvalds committed
699

Dave Airlie's avatar
Dave Airlie committed
700
701
702
703
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
	if (dev->queue_count)
		return -EBUSY;	/* Not while in use */
Linus Torvalds's avatar
Linus Torvalds committed
704

705
	alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlie's avatar
Dave Airlie committed
706
	    ? PAGE_ALIGN(size) : size;
Linus Torvalds's avatar
Linus Torvalds committed
707
708
709
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

Dave Airlie's avatar
Dave Airlie committed
710
711
712
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
713
714
		return -EBUSY;
	}
Dave Airlie's avatar
Dave Airlie committed
715
716
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
717

Dave Airlie's avatar
Dave Airlie committed
718
	down(&dev->struct_sem);
Linus Torvalds's avatar
Linus Torvalds committed
719
	entry = &dma->bufs[order];
Dave Airlie's avatar
Dave Airlie committed
720
721
722
	if (entry->buf_count) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
723
724
725
726
		return -ENOMEM;	/* May only call once for each order */
	}

	if (count < 0 || count > 4096) {
Dave Airlie's avatar
Dave Airlie committed
727
728
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
729
730
731
		return -EINVAL;
	}

Dave Airlie's avatar
Dave Airlie committed
732
733
734
735
736
	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
737
738
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
739
740
741
742
743
744
745
746
747
	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);
Linus Torvalds's avatar
Linus Torvalds committed
748
749
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
750
	memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds's avatar
Linus Torvalds committed
751
752
753
754

	/* Keep the original pagelist until we know all the allocations
	 * have succeeded
	 */
Dave Airlie's avatar
Dave Airlie committed
755
756
	temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
				  * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds's avatar
Linus Torvalds committed
757
	if (!temp_pagelist) {
Dave Airlie's avatar
Dave Airlie committed
758
759
760
761
762
763
		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);
Linus Torvalds's avatar
Linus Torvalds committed
764
765
766
		return -ENOMEM;
	}
	memcpy(temp_pagelist,
Dave Airlie's avatar
Dave Airlie committed
767
768
769
	       dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
	DRM_DEBUG("pagelist: %d entries\n",
		  dma->page_count + (count << page_order));
Linus Torvalds's avatar
Linus Torvalds committed
770

Dave Airlie's avatar
Dave Airlie committed
771
	entry->buf_size = size;
Linus Torvalds's avatar
Linus Torvalds committed
772
773
774
775
	entry->page_order = page_order;
	byte_count = 0;
	page_count = 0;

Dave Airlie's avatar
Dave Airlie committed
776
777
778
	while (entry->buf_count < count) {
		page = drm_alloc_pages(page_order, DRM_MEM_DMA);
		if (!page) {
Linus Torvalds's avatar
Linus Torvalds committed
779
780
781
782
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
			entry->seg_count = count;
			drm_cleanup_buf_error(dev, entry);
Dave Airlie's avatar
Dave Airlie committed
783
784
785
786
787
			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);
Linus Torvalds's avatar
Linus Torvalds committed
788
789
790
			return -ENOMEM;
		}
		entry->seglist[entry->seg_count++] = page;
Dave Airlie's avatar
Dave Airlie committed
791
792
793
794
		for (i = 0; i < (1 << page_order); i++) {
			DRM_DEBUG("page %d @ 0x%08lx\n",
				  dma->page_count + page_count,
				  page + PAGE_SIZE * i);
Linus Torvalds's avatar
Linus Torvalds committed
795
			temp_pagelist[dma->page_count + page_count++]
Dave Airlie's avatar
Dave Airlie committed
796
			    = page + PAGE_SIZE * i;
Linus Torvalds's avatar
Linus Torvalds committed
797
		}
Dave Airlie's avatar
Dave Airlie committed
798
799
800
801
802
803
804
805
806
		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);
Linus Torvalds's avatar
Linus Torvalds committed
807
			buf->address = (void *)(page + offset);
Dave Airlie's avatar
Dave Airlie committed
808
			buf->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
809
810
			buf->waiting = 0;
			buf->pending = 0;
Dave Airlie's avatar
Dave Airlie committed
811
812
			init_waitqueue_head(&buf->dma_wait);
			buf->filp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
813
814

			buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlie's avatar
Dave Airlie committed
815
816
817
			buf->dev_private = drm_alloc(buf->dev_priv_size,
						     DRM_MEM_BUFS);
			if (!buf->dev_private) {
Linus Torvalds's avatar
Linus Torvalds committed
818
819
820
				/* Set count correctly so we free the proper amount. */
				entry->buf_count = count;
				entry->seg_count = count;
Dave Airlie's avatar
Dave Airlie committed
821
822
823
824
825
826
827
828
				drm_cleanup_buf_error(dev, 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);
Linus Torvalds's avatar
Linus Torvalds committed
829
830
				return -ENOMEM;
			}
Dave Airlie's avatar
Dave Airlie committed
831
			memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds's avatar
Linus Torvalds committed
832

Dave Airlie's avatar
Dave Airlie committed
833
834
			DRM_DEBUG("buffer %d @ %p\n",
				  entry->buf_count, buf->address);
Linus Torvalds's avatar
Linus Torvalds committed
835
836
837
838
		}
		byte_count += PAGE_SIZE << page_order;
	}

Dave Airlie's avatar
Dave Airlie committed
839
840
841
842
	temp_buflist = drm_realloc(dma->buflist,
				   dma->buf_count * sizeof(*dma->buflist),
				   (dma->buf_count + entry->buf_count)
				   * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds's avatar
Linus Torvalds committed
843
844
	if (!temp_buflist) {
		/* Free the entry because it isn't valid */
Dave Airlie's avatar
Dave Airlie committed
845
846
847
848
849
850
		drm_cleanup_buf_error(dev, 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);
Linus Torvalds's avatar
Linus Torvalds committed
851
852
853
854
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

Dave Airlie's avatar
Dave Airlie committed
855
	for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
856
857
858
859
860
861
862
863
		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,
Dave Airlie's avatar
Dave Airlie committed
864
865
			 dma->page_count * sizeof(*dma->pagelist),
			 DRM_MEM_PAGES);
Linus Torvalds's avatar
Linus Torvalds committed
866
867
868
869
870
871
872
873
	}
	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);

Dave Airlie's avatar
Dave Airlie committed
874
	up(&dev->struct_sem);
Linus Torvalds's avatar
Linus Torvalds committed
875

876
877
	request->count = entry->buf_count;
	request->size = size;
Linus Torvalds's avatar
Linus Torvalds committed
878

Dave Airlie's avatar
Dave Airlie committed
879
	atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
880
881
882
	return 0;

}
Dave Airlie's avatar
Dave Airlie committed
883

884
EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds's avatar
Linus Torvalds committed
885

Dave Airlie's avatar
Dave Airlie committed
886
static int drm_addbufs_sg(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds's avatar
Linus Torvalds committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
{
	drm_device_dma_t *dma = dev->dma;
	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;

Dave Airlie's avatar
Dave Airlie committed
903
904
905
906
907
	if (!drm_core_check_feature(dev, DRIVER_SG))
		return -EINVAL;

	if (!dma)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
908

909
910
	count = request->count;
	order = drm_order(request->size);
Linus Torvalds's avatar
Linus Torvalds committed
911
912
	size = 1 << order;

Dave Airlie's avatar
Dave Airlie committed
913
914
	alignment = (request->flags & _DRM_PAGE_ALIGN)
	    ? PAGE_ALIGN(size) : size;
Linus Torvalds's avatar
Linus Torvalds committed
915
916
917
918
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

	byte_count = 0;
919
	agp_offset = request->agp_start;
Linus Torvalds's avatar
Linus Torvalds committed
920

Dave Airlie's avatar
Dave Airlie committed
921
922
923
924
925
926
927
	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);
Linus Torvalds's avatar
Linus Torvalds committed
928

Dave Airlie's avatar
Dave Airlie committed
929
930
931
932
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
	if (dev->queue_count)
		return -EBUSY;	/* Not while in use */
Linus Torvalds's avatar
Linus Torvalds committed
933

Dave Airlie's avatar
Dave Airlie committed
934
935
936
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
937
938
		return -EBUSY;
	}
Dave Airlie's avatar
Dave Airlie committed
939
940
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
941

Dave Airlie's avatar
Dave Airlie committed
942
	down(&dev->struct_sem);
Linus Torvalds's avatar
Linus Torvalds committed
943
	entry = &dma->bufs[order];
Dave Airlie's avatar
Dave Airlie committed
944
945
946
947
	if (entry->buf_count) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
Linus Torvalds's avatar
Linus Torvalds committed
948
949
950
	}

	if (count < 0 || count > 4096) {
Dave Airlie's avatar
Dave Airlie committed
951
952
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
953
954
955
		return -EINVAL;
	}

Dave Airlie's avatar
Dave Airlie committed
956
957
958
959
960
	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
961
962
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
963
	memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds's avatar
Linus Torvalds committed
964
965
966
967
968
969

	entry->buf_size = size;
	entry->page_order = page_order;

	offset = 0;

Dave Airlie's avatar
Dave Airlie committed
970
971
972
973
974
975
	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;
Linus Torvalds's avatar
Linus Torvalds committed
976

Dave Airlie's avatar
Dave Airlie committed
977
		buf->offset = (dma->byte_count + offset);
Linus Torvalds's avatar
Linus Torvalds committed
978
		buf->bus_address = agp_offset + offset;
Dave Airlie's avatar
Dave Airlie committed
979
		buf->address = (void *)(agp_offset + offset
980
					+ (unsigned long)dev->sg->virtual);
Dave Airlie's avatar
Dave Airlie committed
981
		buf->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
982
983
		buf->waiting = 0;
		buf->pending = 0;
Dave Airlie's avatar
Dave Airlie committed
984
985
		init_waitqueue_head(&buf->dma_wait);
		buf->filp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
986
987

		buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlie's avatar
Dave Airlie committed
988
989
		buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
		if (!buf->dev_private) {
Linus Torvalds's avatar
Linus Torvalds committed
990
991
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
Dave Airlie's avatar
Dave Airlie committed
992
993
994
			drm_cleanup_buf_error(dev, entry);
			up(&dev->struct_sem);
			atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
995
996
997
			return -ENOMEM;
		}

Dave Airlie's avatar
Dave Airlie committed
998
		memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds's avatar
Linus Torvalds committed
999

Dave Airlie's avatar
Dave Airlie committed
1000
		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds's avatar
Linus Torvalds committed
1001
1002
1003
1004
1005
1006

		offset += alignment;
		entry->buf_count++;
		byte_count += PAGE_SIZE << page_order;
	}

Dave Airlie's avatar
Dave Airlie committed
1007
	DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds's avatar
Linus Torvalds committed
1008

Dave Airlie's avatar
Dave Airlie committed
1009
1010
1011
1012
1013
	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) {
Linus Torvalds's avatar
Linus Torvalds committed
1014
		/* Free the entry because it isn't valid */
Dave Airlie's avatar
Dave Airlie committed
1015
1016
1017
		drm_cleanup_buf_error(dev, entry);
		up(&dev->struct_sem);
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
1018
1019
1020
1021
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

Dave Airlie's avatar
Dave Airlie committed
1022
	for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1023
1024
1025
1026
1027
1028
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

	dma->buf_count += entry->buf_count;
	dma->byte_count += byte_count;

Dave Airlie's avatar
Dave Airlie committed
1029
1030
	DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
	DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds's avatar
Linus Torvalds committed
1031

Dave Airlie's avatar
Dave Airlie committed
1032
	up(&dev->struct_sem);
Linus Torvalds's avatar
Linus Torvalds committed
1033

1034
1035
	request->count = entry->buf_count;
	request->size = size;
Linus Torvalds's avatar
Linus Torvalds committed
1036
1037
1038

	dma->flags = _DRM_DMA_USE_SG;

Dave Airlie's avatar
Dave Airlie committed
1039
	atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
1040
1041
1042
	return 0;
}

Dave Airlie's avatar
Dave Airlie committed
1043
static int drm_addbufs_fb(drm_device_t * dev, drm_buf_desc_t * request)
Dave Airlie's avatar
Dave Airlie committed
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
{
	drm_device_dma_t *dma = dev->dma;
	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 (!drm_core_check_feature(dev, DRIVER_FB_DMA))
		return -EINVAL;
Dave Airlie's avatar
Dave Airlie committed
1062

Dave Airlie's avatar
Dave Airlie committed
1063
1064
1065
	if (!dma)
		return -EINVAL;

1066
1067
	count = request->count;
	order = drm_order(request->size);
Dave Airlie's avatar
Dave Airlie committed
1068
1069
	size = 1 << order;

1070
	alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlie's avatar
Dave Airlie committed
1071
1072
1073
1074
1075
	    ? PAGE_ALIGN(size) : size;
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

	byte_count = 0;
1076
	agp_offset = request->agp_start;
Dave Airlie's avatar
Dave Airlie committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188

	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 = NULL;

		buf->dev_priv_size = dev->driver->dev_priv_size;
		buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
		if (!buf->dev_private) {
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
			drm_cleanup_buf_error(dev, 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(dev, 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);

	up(&dev->struct_sem);

1189
1190
	request->count = entry->buf_count;
	request->size = size;
Dave Airlie's avatar
Dave Airlie committed
1191
1192
1193
1194
1195
1196
1197

	dma->flags = _DRM_DMA_USE_FB;

	atomic_dec(&dev->buf_alloc);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
/**
 * Add 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.
 *
 * According with the memory type specified in drm_buf_desc::flags and the
 * build options, it dispatches the call either to addbufs_agp(),
 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
 * PCI memory respectively.
 */
Dave Airlie's avatar
Dave Airlie committed
1212
1213
int drm_addbufs(struct inode *inode, struct file *filp,
		unsigned int cmd, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
1214
1215
1216
1217
{
	drm_buf_desc_t request;
	drm_file_t *priv = filp->private_data;
	drm_device_t *dev = priv->head->dev;
1218
	int ret;