drm_bufs.c 43.9 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"

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

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

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

Dave Airlie's avatar
Dave Airlie committed
52
static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
53
					     drm_local_map_t *map)
Dave Airlie's avatar
Dave Airlie committed
54
{
Dave Airlie's avatar
Dave Airlie committed
55
	struct drm_map_list *entry;
56
	list_for_each_entry(entry, &dev->maplist, head) {
Dave Airlie's avatar
Dave Airlie committed
57
		if (entry->map && map->type == entry->map->type &&
58
59
		    ((entry->map->offset == map->offset) ||
		     (map->type == _DRM_SHM && map->flags==_DRM_CONTAINS_LOCK))) {
60
			return entry;
Dave Airlie's avatar
Dave Airlie committed
61
62
63
64
		}
	}

	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
65
66
}

67
static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
Adrian Bunk's avatar
Adrian Bunk committed
68
			  unsigned long user_token, int hashed_handle)
69
{
70
	int use_hashed_handle;
Dave Airlie's avatar
Dave Airlie committed
71
#if (BITS_PER_LONG == 64)
72
73
74
75
76
77
	use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
#elif (BITS_PER_LONG == 32)
	use_hashed_handle = hashed_handle;
#else
#error Unsupported long size. Neither 64 nor 32 bits.
#endif
78

79
80
	if (!use_hashed_handle) {
		int ret;
81
		hash->key = user_token >> PAGE_SHIFT;
82
83
84
		ret = drm_ht_insert_item(&dev->map_hash, hash);
		if (ret != -EINVAL)
			return ret;
85
	}
86
87
	return drm_ht_just_insert_please(&dev->map_hash, hash,
					 user_token, 32 - PAGE_SHIFT - 3,
88
					 0, DRM_MAP_HASH_OFFSET >> PAGE_SHIFT);
89
}
90

Linus Torvalds's avatar
Linus Torvalds committed
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
 * 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.
 */
104
static int drm_addmap_core(struct drm_device * dev, unsigned int offset,
105
			   unsigned int size, enum drm_map_type type,
Dave Airlie's avatar
Dave Airlie committed
106
107
			   enum drm_map_flags flags,
			   struct drm_map_list ** maplist)
Linus Torvalds's avatar
Linus Torvalds committed
108
{
109
	struct drm_map *map;
Dave Airlie's avatar
Dave Airlie committed
110
	struct drm_map_list *list;
111
	drm_dma_handle_t *dmah;
112
113
	unsigned long user_token;
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
114

Dave Airlie's avatar
Dave Airlie committed
115
116
	map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
	if (!map)
Linus Torvalds's avatar
Linus Torvalds committed
117
118
		return -ENOMEM;

119
120
121
122
	map->offset = offset;
	map->size = size;
	map->flags = flags;
	map->type = type;
Linus Torvalds's avatar
Linus Torvalds committed
123
124
125
126
127

	/* 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
128
129
	if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
130
131
		return -EINVAL;
	}
Dave Airlie's avatar
Dave Airlie committed
132
133
134
135
	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
136
137
		return -EINVAL;
	}
Dave Airlie's avatar
Dave Airlie committed
138
	map->mtrr = -1;
Linus Torvalds's avatar
Linus Torvalds committed
139
140
	map->handle = NULL;

Dave Airlie's avatar
Dave Airlie committed
141
	switch (map->type) {
Linus Torvalds's avatar
Linus Torvalds committed
142
143
	case _DRM_REGISTERS:
	case _DRM_FRAME_BUFFER:
Dave Airlie's avatar
Dave Airlie committed
144
#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
145
		if (map->offset + (map->size-1) < map->offset ||
Dave Airlie's avatar
Dave Airlie committed
146
147
		    map->offset < virt_to_phys(high_memory)) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
148
149
150
151
152
153
			return -EINVAL;
		}
#endif
#ifdef __alpha__
		map->offset += dev->hose->mem_space->start;
#endif
Dave Airlie's avatar
Dave Airlie committed
154
155
156
157
		/* 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.
		 */
158
159
160
		list = drm_find_matching_map(dev, map);
		if (list != NULL) {
			if (list->map->size != map->size) {
Dave Airlie's avatar
Dave Airlie committed
161
				DRM_DEBUG("Matching maps of type %d with "
Dave Airlie's avatar
Dave Airlie committed
162
163
164
					  "mismatched sizes, (%ld vs %ld)\n",
					  map->type, map->size,
					  list->map->size);
165
				list->map->size = map->size;
Dave Airlie's avatar
Dave Airlie committed
166
167
168
			}

			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
169
			*maplist = list;
Dave Airlie's avatar
Dave Airlie committed
170
171
172
			return 0;
		}

Linus Torvalds's avatar
Linus Torvalds committed
173
		if (drm_core_has_MTRR(dev)) {
Dave Airlie's avatar
Dave Airlie committed
174
175
176
177
			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
178
179
180
			}
		}
		if (map->type == _DRM_REGISTERS)
181
			map->handle = ioremap(map->offset, map->size);
Linus Torvalds's avatar
Linus Torvalds committed
182
183
		break;
	case _DRM_SHM:
184
185
186
187
188
189
190
191
192
193
194
195
196
		list = drm_find_matching_map(dev, map);
		if (list != NULL) {
			if(list->map->size != map->size) {
				DRM_DEBUG("Matching maps of type %d with "
					  "mismatched sizes, (%ld vs %ld)\n",
					  map->type, map->size, list->map->size);
				list->map->size = map->size;
			}

			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			*maplist = list;
			return 0;
		}
197
		map->handle = vmalloc_user(map->size);
Dave Airlie's avatar
Dave Airlie committed
198
199
200
201
		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
202
203
204
			return -ENOMEM;
		}
		map->offset = (unsigned long)map->handle;
Dave Airlie's avatar
Dave Airlie committed
205
		if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds's avatar
Linus Torvalds committed
206
207
			/* Prevent a 2nd X Server from creating a 2nd lock */
			if (dev->lock.hw_lock != NULL) {
Dave Airlie's avatar
Dave Airlie committed
208
209
				vfree(map->handle);
				drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
210
211
				return -EBUSY;
			}
Dave Airlie's avatar
Dave Airlie committed
212
			dev->sigdata.lock = dev->lock.hw_lock = map->handle;	/* Pointer to lock */
Linus Torvalds's avatar
Linus Torvalds committed
213
214
		}
		break;
215
	case _DRM_AGP: {
Dave Airlie's avatar
Dave Airlie committed
216
		struct drm_agp_mem *entry;
217
218
219
220
221
222
		int valid = 0;

		if (!drm_core_has_AGP(dev)) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EINVAL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
223
#ifdef __alpha__
224
		map->offset += dev->hose->mem_space->start;
Linus Torvalds's avatar
Linus Torvalds committed
225
#endif
226
227
228
229
230
231
232
233
234
235
236
237
238
		/* Note: dev->agp->base may actually be 0 when the DRM
		 * is not in control of AGP space. But if user space is
		 * it should already have added the AGP base itself.
		 */
		map->offset += dev->agp->base;
		map->mtrr = dev->agp->agp_mtrr;	/* for getmap */

		/* This assumes the DRM is in total control of AGP space.
		 * It's not always the case as AGP can be in the control
		 * of user space (i.e. i810 driver). So this loop will get
		 * skipped and we double check that dev->agp->memory is
		 * actually set as well as being invalid before EPERM'ing
		 */
239
		list_for_each_entry(entry, &dev->agp->memory, head) {
240
241
242
243
244
			if ((map->offset >= entry->bound) &&
			    (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
				valid = 1;
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
245
		}
246
		if (!list_empty(&dev->agp->memory) && !valid) {
247
248
249
250
251
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EPERM;
		}
		DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);

Linus Torvalds's avatar
Linus Torvalds committed
252
		break;
253
	}
Linus Torvalds's avatar
Linus Torvalds committed
254
255
256
257
258
	case _DRM_SCATTER_GATHER:
		if (!dev->sg) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EINVAL;
		}
259
		map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds's avatar
Linus Torvalds committed
260
		break;
Dave Airlie's avatar
Dave Airlie committed
261
	case _DRM_CONSISTENT:
262
		/* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
263
		 * As we're limiting the address to 2^32-1 (or less),
264
265
		 * casting it down to 32 bits is no problem, but we
		 * need to point to a 64bit variable first. */
266
267
		dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
		if (!dmah) {
268
269
270
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -ENOMEM;
		}
271
272
273
		map->handle = dmah->vaddr;
		map->offset = (unsigned long)dmah->busaddr;
		kfree(dmah);
274
		break;
Linus Torvalds's avatar
Linus Torvalds committed
275
	default:
Dave Airlie's avatar
Dave Airlie committed
276
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
277
278
279
280
		return -EINVAL;
	}

	list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlie's avatar
Dave Airlie committed
281
	if (!list) {
282
		if (map->type == _DRM_REGISTERS)
283
			iounmap(map->handle);
Linus Torvalds's avatar
Linus Torvalds committed
284
285
286
287
288
289
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		return -EINVAL;
	}
	memset(list, 0, sizeof(*list));
	list->map = map;

Dave Airlie's avatar
Dave Airlie committed
290
	mutex_lock(&dev->struct_mutex);
291
	list_add(&list->head, &dev->maplist);
292

293
	/* Assign a 32-bit handle */
Dave Airlie's avatar
Dave Airlie committed
294
	/* We do it here so that dev->struct_mutex protects the increment */
295
296
	user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
		map->offset;
297
	ret = drm_map_handle(dev, &list->hash, user_token, 0);
298
	if (ret) {
299
		if (map->type == _DRM_REGISTERS)
300
			iounmap(map->handle);
301
302
303
304
305
306
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		drm_free(list, sizeof(*list), DRM_MEM_MAPS);
		mutex_unlock(&dev->struct_mutex);
		return ret;
	}

307
	list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie's avatar
Dave Airlie committed
308
	mutex_unlock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
309

310
	*maplist = list;
311
	return 0;
312
	}
313

314
int drm_addmap(struct drm_device * dev, unsigned int offset,
315
316
	       unsigned int size, enum drm_map_type type,
	       enum drm_map_flags flags, drm_local_map_t ** map_ptr)
317
{
Dave Airlie's avatar
Dave Airlie committed
318
	struct drm_map_list *list;
319
320
321
322
323
324
325
	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
326

327
328
329
330
331
EXPORT_SYMBOL(drm_addmap);

int drm_addmap_ioctl(struct inode *inode, struct file *filp,
		     unsigned int cmd, unsigned long arg)
{
332
333
	struct drm_file *priv = filp->private_data;
	struct drm_device *dev = priv->head->dev;
334
	struct drm_map map;
Dave Airlie's avatar
Dave Airlie committed
335
	struct drm_map_list *maplist;
336
	struct drm_map __user *argp = (void __user *)arg;
337
338
339
340
341
	int err;

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

Dave Airlie's avatar
Dave Airlie committed
342
	if (copy_from_user(&map, argp, sizeof(map))) {
Linus Torvalds's avatar
Linus Torvalds committed
343
		return -EFAULT;
344
345
	}

346
347
348
	if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
		return -EPERM;

349
350
	err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
			      &maplist);
351

Dave Airlie's avatar
Dave Airlie committed
352
	if (err)
353
		return err;
354

355
	if (copy_to_user(argp, maplist->map, sizeof(struct drm_map)))
356
		return -EFAULT;
357
358
359

	/* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
	if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
360
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
361
	return 0;
Dave Airlie's avatar
Dave Airlie committed
362
}
Linus Torvalds's avatar
Linus Torvalds committed
363
364
365
366
367
368
369
370

/**
 * 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.
371
 * \param arg pointer to a struct drm_map structure.
Linus Torvalds's avatar
Linus Torvalds committed
372
373
374
375
376
377
 * \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.
 *
378
 * \sa drm_addmap
Linus Torvalds's avatar
Linus Torvalds committed
379
 */
380
int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
Linus Torvalds's avatar
Linus Torvalds committed
381
{
Dave Airlie's avatar
Dave Airlie committed
382
	struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie's avatar
Dave Airlie committed
383
	drm_dma_handle_t dmah;
384
	int found = 0;
Linus Torvalds's avatar
Linus Torvalds committed
385

Dave Airlie's avatar
Dave Airlie committed
386
	/* Find the list entry for the map and remove it */
387
	list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie's avatar
Dave Airlie committed
388
		if (r_list->map == map) {
389
			list_del(&r_list->head);
390
391
			drm_ht_remove_key(&dev->map_hash,
					  r_list->user_token >> PAGE_SHIFT);
392
393
			drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
			found = 1;
Dave Airlie's avatar
Dave Airlie committed
394
395
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
396
397
	}

398
	if (!found)
Linus Torvalds's avatar
Linus Torvalds committed
399
400
		return -EINVAL;

Dave Airlie's avatar
Dave Airlie committed
401
402
	switch (map->type) {
	case _DRM_REGISTERS:
403
		iounmap(map->handle);
Dave Airlie's avatar
Dave Airlie committed
404
405
406
407
		/* FALLTHROUGH */
	case _DRM_FRAME_BUFFER:
		if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
			int retcode;
Dave Airlie's avatar
Dave Airlie committed
408
409
			retcode = mtrr_del(map->mtrr, map->offset, map->size);
			DRM_DEBUG("mtrr_del=%d\n", retcode);
Linus Torvalds's avatar
Linus Torvalds committed
410
		}
Dave Airlie's avatar
Dave Airlie committed
411
412
413
414
415
416
417
418
419
420
421
422
423
		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
424
	}
Dave Airlie's avatar
Dave Airlie committed
425
426
	drm_free(map, sizeof(*map), DRM_MEM_MAPS);

Linus Torvalds's avatar
Linus Torvalds committed
427
428
	return 0;
}
Dave Airlie's avatar
Dave Airlie committed
429

430
int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
Dave Airlie's avatar
Dave Airlie committed
431
432
433
{
	int ret;

Dave Airlie's avatar
Dave Airlie committed
434
	mutex_lock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
435
	ret = drm_rmmap_locked(dev, map);
Dave Airlie's avatar
Dave Airlie committed
436
	mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
437
438
439

	return ret;
}
440

Dave Airlie's avatar
Dave Airlie committed
441
442
443
444
445
446
447
448
449
/* 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.
 */
450
451
452
int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
		    unsigned int cmd, unsigned long arg)
{
453
454
	struct drm_file *priv = filp->private_data;
	struct drm_device *dev = priv->head->dev;
455
	struct drm_map request;
Dave Airlie's avatar
Dave Airlie committed
456
	drm_local_map_t *map = NULL;
Dave Airlie's avatar
Dave Airlie committed
457
	struct drm_map_list *r_list;
Dave Airlie's avatar
Dave Airlie committed
458
	int ret;
459

460
	if (copy_from_user(&request, (struct drm_map __user *) arg, sizeof(request))) {
461
462
463
		return -EFAULT;
	}

Dave Airlie's avatar
Dave Airlie committed
464
	mutex_lock(&dev->struct_mutex);
465
	list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie's avatar
Dave Airlie committed
466
		if (r_list->map &&
Dave Airlie's avatar
Dave Airlie committed
467
		    r_list->user_token == (unsigned long)request.handle &&
Dave Airlie's avatar
Dave Airlie committed
468
469
470
471
472
473
474
475
476
		    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.
	 */
477
	if (list_empty(&dev->maplist) || !map) {
Dave Airlie's avatar
Dave Airlie committed
478
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
479
480
481
482
483
		return -EINVAL;
	}

	/* Register and framebuffer maps are permanent */
	if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie's avatar
Dave Airlie committed
484
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
485
486
487
488
489
		return 0;
	}

	ret = drm_rmmap_locked(dev, map);

Dave Airlie's avatar
Dave Airlie committed
490
	mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
491
492

	return ret;
493
}
Linus Torvalds's avatar
Linus Torvalds committed
494
495
496
497

/**
 * Cleanup after an error on one of the addbufs() functions.
 *
Dave Airlie's avatar
Dave Airlie committed
498
 * \param dev DRM device.
Linus Torvalds's avatar
Linus Torvalds committed
499
500
501
502
 * \param entry buffer entry where the error occurred.
 *
 * Frees any pages and buffers associated with the given entry.
 */
503
504
static void drm_cleanup_buf_error(struct drm_device * dev,
				  struct drm_buf_entry * entry)
Linus Torvalds's avatar
Linus Torvalds committed
505
506
507
508
509
510
{
	int i;

	if (entry->seg_count) {
		for (i = 0; i < entry->seg_count; i++) {
			if (entry->seglist[i]) {
Dave Airlie's avatar
Dave Airlie committed
511
				drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds's avatar
Linus Torvalds committed
512
513
514
			}
		}
		drm_free(entry->seglist,
Dave Airlie's avatar
Dave Airlie committed
515
516
			 entry->seg_count *
			 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds's avatar
Linus Torvalds committed
517
518
519
520

		entry->seg_count = 0;
	}

Dave Airlie's avatar
Dave Airlie committed
521
522
	if (entry->buf_count) {
		for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
523
524
			if (entry->buflist[i].dev_private) {
				drm_free(entry->buflist[i].dev_private,
Dave Airlie's avatar
Dave Airlie committed
525
526
					 entry->buflist[i].dev_priv_size,
					 DRM_MEM_BUFS);
Linus Torvalds's avatar
Linus Torvalds committed
527
528
529
			}
		}
		drm_free(entry->buflist,
Dave Airlie's avatar
Dave Airlie committed
530
531
			 entry->buf_count *
			 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds's avatar
Linus Torvalds committed
532
533
534
535
536
537
538

		entry->buf_count = 0;
	}
}

#if __OS_HAS_AGP
/**
539
 * Add AGP buffers for DMA transfers.
Linus Torvalds's avatar
Linus Torvalds committed
540
 *
541
 * \param dev struct drm_device to which the buffers are to be added.
542
 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds's avatar
Linus Torvalds committed
543
 * \return zero on success or a negative number on failure.
Dave Airlie's avatar
Dave Airlie committed
544
 *
Linus Torvalds's avatar
Linus Torvalds committed
545
546
547
548
 * 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.
 */
549
int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds's avatar
Linus Torvalds committed
550
{
551
552
	struct drm_device_dma *dma = dev->dma;
	struct drm_buf_entry *entry;
Dave Airlie's avatar
Dave Airlie committed
553
	struct drm_agp_mem *agp_entry;
Dave Airlie's avatar
Dave Airlie committed
554
	struct drm_buf *buf;
Linus Torvalds's avatar
Linus Torvalds committed
555
556
557
558
559
560
561
562
563
	unsigned long offset;
	unsigned long agp_offset;
	int count;
	int order;
	int size;
	int alignment;
	int page_order;
	int total;
	int byte_count;
564
	int i, valid;
Dave Airlie's avatar
Dave Airlie committed
565
	struct drm_buf **temp_buflist;
Linus Torvalds's avatar
Linus Torvalds committed
566

Dave Airlie's avatar
Dave Airlie committed
567
568
	if (!dma)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
569

570
571
	count = request->count;
	order = drm_order(request->size);
Linus Torvalds's avatar
Linus Torvalds committed
572
573
	size = 1 << order;

Dave Airlie's avatar
Dave Airlie committed
574
575
	alignment = (request->flags & _DRM_PAGE_ALIGN)
	    ? PAGE_ALIGN(size) : size;
Linus Torvalds's avatar
Linus Torvalds committed
576
577
578
579
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

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

Dave Airlie's avatar
Dave Airlie committed
582
583
584
	DRM_DEBUG("count:      %d\n", count);
	DRM_DEBUG("order:      %d\n", order);
	DRM_DEBUG("size:       %d\n", size);
585
	DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlie's avatar
Dave Airlie committed
586
587
588
	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
589

Dave Airlie's avatar
Dave Airlie committed
590
591
592
593
	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
594

595
596
	/* Make sure buffers are located in AGP memory that we own */
	valid = 0;
597
	list_for_each_entry(agp_entry, &dev->agp->memory, head) {
598
599
600
601
602
603
		if ((agp_offset >= agp_entry->bound) &&
		    (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
			valid = 1;
			break;
		}
	}
604
	if (!list_empty(&dev->agp->memory) && !valid) {
605
606
607
		DRM_DEBUG("zone invalid\n");
		return -EINVAL;
	}
Dave Airlie's avatar
Dave Airlie committed
608
609
610
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
611
612
		return -EBUSY;
	}
Dave Airlie's avatar
Dave Airlie committed
613
614
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
615

Dave Airlie's avatar
Dave Airlie committed
616
	mutex_lock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
617
	entry = &dma->bufs[order];
Dave Airlie's avatar
Dave Airlie committed
618
	if (entry->buf_count) {
Dave Airlie's avatar
Dave Airlie committed
619
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
620
621
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
Linus Torvalds's avatar
Linus Torvalds committed
622
623
624
	}

	if (count < 0 || count > 4096) {
Dave Airlie's avatar
Dave Airlie committed
625
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
626
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
627
628
629
		return -EINVAL;
	}

Dave Airlie's avatar
Dave Airlie committed
630
631
632
	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
Dave Airlie's avatar
Dave Airlie committed
633
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
634
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
635
636
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
637
	memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds's avatar
Linus Torvalds committed
638
639
640
641
642
643

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

	offset = 0;

Dave Airlie's avatar
Dave Airlie committed
644
645
646
647
648
649
	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
650

Dave Airlie's avatar
Dave Airlie committed
651
		buf->offset = (dma->byte_count + offset);
Linus Torvalds's avatar
Linus Torvalds committed
652
653
		buf->bus_address = agp_offset + offset;
		buf->address = (void *)(agp_offset + offset);
Dave Airlie's avatar
Dave Airlie committed
654
		buf->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
655
656
		buf->waiting = 0;
		buf->pending = 0;
Dave Airlie's avatar
Dave Airlie committed
657
658
		init_waitqueue_head(&buf->dma_wait);
		buf->filp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
659
660

		buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlie's avatar
Dave Airlie committed
661
662
		buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
		if (!buf->dev_private) {
Linus Torvalds's avatar
Linus Torvalds committed
663
664
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
Dave Airlie's avatar
Dave Airlie committed
665
			drm_cleanup_buf_error(dev, entry);
Dave Airlie's avatar
Dave Airlie committed
666
			mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
667
			atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
668
669
			return -ENOMEM;
		}
Dave Airlie's avatar
Dave Airlie committed
670
		memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds's avatar
Linus Torvalds committed
671

Dave Airlie's avatar
Dave Airlie committed
672
		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds's avatar
Linus Torvalds committed
673
674
675
676
677
678

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

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

Dave Airlie's avatar
Dave Airlie committed
681
682
683
684
685
	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
686
		/* Free the entry because it isn't valid */
Dave Airlie's avatar
Dave Airlie committed
687
		drm_cleanup_buf_error(dev, entry);
Dave Airlie's avatar
Dave Airlie committed
688
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
689
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
690
691
692
693
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

Dave Airlie's avatar
Dave Airlie committed
694
	for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
695
696
697
698
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

	dma->buf_count += entry->buf_count;
699
700
	dma->seg_count += entry->seg_count;
	dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
701
702
	dma->byte_count += byte_count;

Dave Airlie's avatar
Dave Airlie committed
703
704
	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
705

Dave Airlie's avatar
Dave Airlie committed
706
	mutex_unlock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
707

708
709
	request->count = entry->buf_count;
	request->size = size;
Linus Torvalds's avatar
Linus Torvalds committed
710
711
712

	dma->flags = _DRM_DMA_USE_AGP;

Dave Airlie's avatar
Dave Airlie committed
713
	atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
714
715
	return 0;
}
716
EXPORT_SYMBOL(drm_addbufs_agp);
Dave Airlie's avatar
Dave Airlie committed
717
#endif				/* __OS_HAS_AGP */
Linus Torvalds's avatar
Linus Torvalds committed
718

719
int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds's avatar
Linus Torvalds committed
720
{
721
	struct drm_device_dma *dma = dev->dma;
Linus Torvalds's avatar
Linus Torvalds committed
722
723
724
725
726
	int count;
	int order;
	int size;
	int total;
	int page_order;
727
	struct drm_buf_entry *entry;
Dave Airlie's avatar
Dave Airlie committed
728
	drm_dma_handle_t *dmah;
Dave Airlie's avatar
Dave Airlie committed
729
	struct drm_buf *buf;
Linus Torvalds's avatar
Linus Torvalds committed
730
731
732
733
734
735
	int alignment;
	unsigned long offset;
	int i;
	int byte_count;
	int page_count;
	unsigned long *temp_pagelist;
Dave Airlie's avatar
Dave Airlie committed
736
	struct drm_buf **temp_buflist;
Linus Torvalds's avatar
Linus Torvalds committed
737

Dave Airlie's avatar
Dave Airlie committed
738
739
	if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
		return -EINVAL;
740

Dave Airlie's avatar
Dave Airlie committed
741
742
	if (!dma)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
743

744
745
746
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

747
748
	count = request->count;
	order = drm_order(request->size);
Linus Torvalds's avatar
Linus Torvalds committed
749
750
	size = 1 << order;

Dave Airlie's avatar
Dave Airlie committed
751
752
	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
753

Dave Airlie's avatar
Dave Airlie committed
754
755
756
757
	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
758

759
	alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlie's avatar
Dave Airlie committed
760
	    ? PAGE_ALIGN(size) : size;
Linus Torvalds's avatar
Linus Torvalds committed
761
762
763
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

Dave Airlie's avatar
Dave Airlie committed
764
765
766
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
767
768
		return -EBUSY;
	}
Dave Airlie's avatar
Dave Airlie committed
769
770
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
771

Dave Airlie's avatar
Dave Airlie committed
772
	mutex_lock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
773
	entry = &dma->bufs[order];
Dave Airlie's avatar
Dave Airlie committed
774
	if (entry->buf_count) {
Dave Airlie's avatar
Dave Airlie committed
775
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
776
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
777
778
779
780
		return -ENOMEM;	/* May only call once for each order */
	}

	if (count < 0 || count > 4096) {
Dave Airlie's avatar
Dave Airlie committed
781
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
782
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
783
784
785
		return -EINVAL;
	}

Dave Airlie's avatar
Dave Airlie committed
786
787
788
	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
Dave Airlie's avatar
Dave Airlie committed
789
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
790
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
791
792
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
793
794
795
796
797
798
799
	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);
Dave Airlie's avatar
Dave Airlie committed
800
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
801
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
802
803
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
804
	memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds's avatar
Linus Torvalds committed
805
806
807
808

	/* Keep the original pagelist until we know all the allocations
	 * have succeeded
	 */
Dave Airlie's avatar
Dave Airlie committed
809
810
	temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
				  * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds's avatar
Linus Torvalds committed
811
	if (!temp_pagelist) {
Dave Airlie's avatar
Dave Airlie committed
812
813
814
815
		drm_free(entry->buflist,
			 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
		drm_free(entry->seglist,
			 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie's avatar
Dave Airlie committed
816
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
817
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
818
819
820
		return -ENOMEM;
	}
	memcpy(temp_pagelist,
Dave Airlie's avatar
Dave Airlie committed
821
822
823
	       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
824

Dave Airlie's avatar
Dave Airlie committed
825
	entry->buf_size = size;
Linus Torvalds's avatar
Linus Torvalds committed
826
827
828
829
	entry->page_order = page_order;
	byte_count = 0;
	page_count = 0;

Dave Airlie's avatar
Dave Airlie committed
830
	while (entry->buf_count < count) {
Dave Airlie's avatar
Dave Airlie committed
831
832
833
834
		
		dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
		
		if (!dmah) {
Linus Torvalds's avatar
Linus Torvalds committed
835
836
837
838
			/* 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
839
840
841
			drm_free(temp_pagelist,
				 (dma->page_count + (count << page_order))
				 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie's avatar
Dave Airlie committed
842
			mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
843
			atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
844
845
			return -ENOMEM;
		}
Dave Airlie's avatar
Dave Airlie committed
846
		entry->seglist[entry->seg_count++] = dmah;
Dave Airlie's avatar
Dave Airlie committed
847
848
849
		for (i = 0; i < (1 << page_order); i++) {
			DRM_DEBUG("page %d @ 0x%08lx\n",
				  dma->page_count + page_count,
Dave Airlie's avatar
Dave Airlie committed
850
				  (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds's avatar
Linus Torvalds committed
851
			temp_pagelist[dma->page_count + page_count++]
Dave Airlie's avatar
Dave Airlie committed
852
				= (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds's avatar
Linus Torvalds committed
853
		}
Dave Airlie's avatar
Dave Airlie committed
854
855
856
857
858
859
860
861
862
		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);
Dave Airlie's avatar
Dave Airlie committed
863
864
			buf->address = (void *)(dmah->vaddr + offset);
			buf->bus_address = dmah->busaddr + offset;
Dave Airlie's avatar
Dave Airlie committed
865
			buf->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
866
867
			buf->waiting = 0;
			buf->pending = 0;
Dave Airlie's avatar
Dave Airlie committed
868
869
			init_waitqueue_head(&buf->dma_wait);
			buf->filp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
870
871

			buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlie's avatar
Dave Airlie committed
872
873
874
			buf->dev_private = drm_alloc(buf->dev_priv_size,
						     DRM_MEM_BUFS);
			if (!buf->dev_private) {
Linus Torvalds's avatar
Linus Torvalds committed
875
876
877
				/* Set count correctly so we free the proper amount. */
				entry->buf_count = count;
				entry->seg_count = count;
Dave Airlie's avatar
Dave Airlie committed
878
879
880
881
882
883
				drm_cleanup_buf_error(dev, entry);
				drm_free(temp_pagelist,
					 (dma->page_count +
					  (count << page_order))
					 * sizeof(*dma->pagelist),
					 DRM_MEM_PAGES);
Dave Airlie's avatar
Dave Airlie committed
884
				mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
885
				atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
886
887
				return -ENOMEM;
			}
Dave Airlie's avatar
Dave Airlie committed
888
			memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds's avatar
Linus Torvalds committed
889

Dave Airlie's avatar
Dave Airlie committed
890
891
			DRM_DEBUG("buffer %d @ %p\n",
				  entry->buf_count, buf->address);
Linus Torvalds's avatar
Linus Torvalds committed
892
893
894
895
		}
		byte_count += PAGE_SIZE << page_order;
	}

Dave Airlie's avatar
Dave Airlie committed
896
897
898
899
	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
900
901
	if (!temp_buflist) {
		/* Free the entry because it isn't valid */
Dave Airlie's avatar
Dave Airlie committed
902
903
904
905
		drm_cleanup_buf_error(dev, entry);
		drm_free(temp_pagelist,
			 (dma->page_count + (count << page_order))
			 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie's avatar
Dave Airlie committed
906
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
907
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
908
909
910
911
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

Dave Airlie's avatar
Dave Airlie committed
912
	for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
913
914
915
916
917
918
919
920
		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
921
922
			 dma->page_count * sizeof(*dma->pagelist),
			 DRM_MEM_PAGES);
Linus Torvalds's avatar
Linus Torvalds committed
923
924
925
926
927
928
929
930
	}
	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
931
	mutex_unlock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
932

933
934
	request->count = entry->buf_count;
	request->size = size;
Linus Torvalds's avatar
Linus Torvalds committed
935

936
937
938
	if (request->flags & _DRM_PCI_BUFFER_RO)
		dma->flags = _DRM_DMA_USE_PCI_RO;

Dave Airlie's avatar
Dave Airlie committed
939
	atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
940
941
942
	return 0;

}
943
EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds's avatar
Linus Torvalds committed
944

945
static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds's avatar
Linus Torvalds committed
946
{
947
948
	struct drm_device_dma *dma = dev->dma;
	struct drm_buf_entry *entry;
Dave Airlie's avatar
Dave Airlie committed
949
	struct drm_buf *buf;
Linus Torvalds's avatar
Linus Torvalds committed
950
951
952
953
954
955
956
957
958
959
	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;
Dave Airlie's avatar
Dave Airlie committed
960
	struct drm_buf **temp_buflist;
Linus Torvalds's avatar
Linus Torvalds committed
961

Dave Airlie's avatar
Dave Airlie committed
962
963
964
965
966
	if (!drm_core_check_feature(dev, DRIVER_SG))
		return -EINVAL;

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

968
969
970
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

971
972
	count = request->count;
	order = drm_order(request->size);