drm_bufs.c 42.0 KB
Newer Older
L
Linus Torvalds 已提交
1
/**
D
Dave Airlie 已提交
2
 * \file drm_bufs.c
L
Linus Torvalds 已提交
3
 * Generic buffer template
D
Dave Airlie 已提交
4
 *
L
Linus Torvalds 已提交
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
 * \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>
37
#include <linux/slab.h>
38
#include <linux/log2.h>
39
#include <linux/export.h>
40
#include <asm/shmparam.h>
41
#include <drm/drmP.h>
L
Linus Torvalds 已提交
42

D
Dave Airlie 已提交
43
static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
44
						  struct drm_local_map *map)
D
Dave Airlie 已提交
45
{
D
Dave Airlie 已提交
46
	struct drm_map_list *entry;
47
	list_for_each_entry(entry, &dev->maplist, head) {
48 49
		/*
		 * Because the kernel-userspace ABI is fixed at a 32-bit offset
50 51 52 53 54
		 * while PCI resources may live above that, we only compare the
		 * lower 32 bits of the map offset for maps of type
		 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
		 * It is assumed that if a driver have more than one resource
		 * of each type, the lower 32 bits are different.
55 56 57 58 59 60 61 62 63
		 */
		if (!entry->map ||
		    map->type != entry->map->type ||
		    entry->master != dev->primary->master)
			continue;
		switch (map->type) {
		case _DRM_SHM:
			if (map->flags != _DRM_CONTAINS_LOCK)
				break;
64
			return entry;
65 66
		case _DRM_REGISTERS:
		case _DRM_FRAME_BUFFER:
67 68 69
			if ((entry->map->offset & 0xffffffff) ==
			    (map->offset & 0xffffffff))
				return entry;
70 71
		default: /* Make gcc happy */
			;
D
Dave Airlie 已提交
72
		}
73 74
		if (entry->map->offset == map->offset)
			return entry;
D
Dave Airlie 已提交
75 76 77
	}

	return NULL;
L
Linus Torvalds 已提交
78 79
}

80
static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
81
			  unsigned long user_token, int hashed_handle, int shm)
82
{
83 84 85
	int use_hashed_handle, shift;
	unsigned long add;

D
Dave Airlie 已提交
86
#if (BITS_PER_LONG == 64)
87 88 89 90 91 92
	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
93

94 95
	if (!use_hashed_handle) {
		int ret;
96
		hash->key = user_token >> PAGE_SHIFT;
97 98 99
		ret = drm_ht_insert_item(&dev->map_hash, hash);
		if (ret != -EINVAL)
			return ret;
100
	}
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

	shift = 0;
	add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
	if (shm && (SHMLBA > PAGE_SIZE)) {
		int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;

		/* For shared memory, we have to preserve the SHMLBA
		 * bits of the eventual vma->vm_pgoff value during
		 * mmap().  Otherwise we run into cache aliasing problems
		 * on some platforms.  On these platforms, the pgoff of
		 * a mmap() request is used to pick a suitable virtual
		 * address for the mmap() region such that it will not
		 * cause cache aliasing problems.
		 *
		 * Therefore, make sure the SHMLBA relevant bits of the
		 * hash value we use are equal to those in the original
		 * kernel virtual address.
		 */
		shift = bits;
		add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
	}

123 124
	return drm_ht_just_insert_please(&dev->map_hash, hash,
					 user_token, 32 - PAGE_SHIFT - 3,
125
					 shift, add);
126
}
127

L
Linus Torvalds 已提交
128
/**
129 130
 * Core function to create a range of memory available for mapping by a
 * non-root process.
L
Linus Torvalds 已提交
131 132 133 134 135
 *
 * 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.
 */
136
static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
137
			   unsigned int size, enum drm_map_type type,
D
Dave Airlie 已提交
138 139
			   enum drm_map_flags flags,
			   struct drm_map_list ** maplist)
L
Linus Torvalds 已提交
140
{
141
	struct drm_local_map *map;
D
Dave Airlie 已提交
142
	struct drm_map_list *list;
143
	drm_dma_handle_t *dmah;
144 145
	unsigned long user_token;
	int ret;
L
Linus Torvalds 已提交
146

147
	map = kmalloc(sizeof(*map), GFP_KERNEL);
D
Dave Airlie 已提交
148
	if (!map)
L
Linus Torvalds 已提交
149 150
		return -ENOMEM;

151 152 153 154
	map->offset = offset;
	map->size = size;
	map->flags = flags;
	map->type = type;
L
Linus Torvalds 已提交
155 156 157 158 159

	/* 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.
	 */
D
Dave Airlie 已提交
160
	if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
161
		kfree(map);
L
Linus Torvalds 已提交
162 163
		return -EINVAL;
	}
164 165
	DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
		  (unsigned long long)map->offset, map->size, map->type);
166 167 168 169 170 171 172 173

	/* page-align _DRM_SHM maps. They are allocated here so there is no security
	 * hole created by that and it works around various broken drivers that use
	 * a non-aligned quantity to map the SAREA. --BenH
	 */
	if (map->type == _DRM_SHM)
		map->size = PAGE_ALIGN(map->size);

174
	if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
175
		kfree(map);
L
Linus Torvalds 已提交
176 177
		return -EINVAL;
	}
D
Dave Airlie 已提交
178
	map->mtrr = -1;
L
Linus Torvalds 已提交
179 180
	map->handle = NULL;

D
Dave Airlie 已提交
181
	switch (map->type) {
L
Linus Torvalds 已提交
182 183
	case _DRM_REGISTERS:
	case _DRM_FRAME_BUFFER:
J
Jordan Crouse 已提交
184
#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
185
		if (map->offset + (map->size-1) < map->offset ||
D
Dave Airlie 已提交
186
		    map->offset < virt_to_phys(high_memory)) {
187
			kfree(map);
L
Linus Torvalds 已提交
188 189 190
			return -EINVAL;
		}
#endif
D
Dave Airlie 已提交
191 192 193 194
		/* 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.
		 */
195 196 197
		list = drm_find_matching_map(dev, map);
		if (list != NULL) {
			if (list->map->size != map->size) {
D
Dave Airlie 已提交
198
				DRM_DEBUG("Matching maps of type %d with "
D
Dave Airlie 已提交
199 200 201
					  "mismatched sizes, (%ld vs %ld)\n",
					  map->type, map->size,
					  list->map->size);
202
				list->map->size = map->size;
D
Dave Airlie 已提交
203 204
			}

205
			kfree(map);
206
			*maplist = list;
D
Dave Airlie 已提交
207 208 209
			return 0;
		}

L
Linus Torvalds 已提交
210
		if (drm_core_has_MTRR(dev)) {
D
Dave Airlie 已提交
211 212
			if (map->type == _DRM_FRAME_BUFFER ||
			    (map->flags & _DRM_WRITE_COMBINING)) {
213 214
				map->mtrr =
					arch_phys_wc_add(map->offset, map->size);
L
Linus Torvalds 已提交
215 216
			}
		}
217
		if (map->type == _DRM_REGISTERS) {
218 219 220 221 222
			if (map->flags & _DRM_WRITE_COMBINING)
				map->handle = ioremap_wc(map->offset,
							 map->size);
			else
				map->handle = ioremap(map->offset, map->size);
223
			if (!map->handle) {
224
				kfree(map);
225 226 227
				return -ENOMEM;
			}
		}
D
Dave Airlie 已提交
228

L
Linus Torvalds 已提交
229 230
		break;
	case _DRM_SHM:
231 232 233 234 235 236 237 238 239
		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;
			}

240
			kfree(map);
241 242 243
			*maplist = list;
			return 0;
		}
244
		map->handle = vmalloc_user(map->size);
D
Dave Airlie 已提交
245 246 247
		DRM_DEBUG("%lu %d %p\n",
			  map->size, drm_order(map->size), map->handle);
		if (!map->handle) {
248
			kfree(map);
L
Linus Torvalds 已提交
249 250 251
			return -ENOMEM;
		}
		map->offset = (unsigned long)map->handle;
D
Dave Airlie 已提交
252
		if (map->flags & _DRM_CONTAINS_LOCK) {
L
Linus Torvalds 已提交
253
			/* Prevent a 2nd X Server from creating a 2nd lock */
254
			if (dev->primary->master->lock.hw_lock != NULL) {
D
Dave Airlie 已提交
255
				vfree(map->handle);
256
				kfree(map);
L
Linus Torvalds 已提交
257 258
				return -EBUSY;
			}
259
			dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle;	/* Pointer to lock */
L
Linus Torvalds 已提交
260 261
		}
		break;
262
	case _DRM_AGP: {
D
Dave Airlie 已提交
263
		struct drm_agp_mem *entry;
264 265 266
		int valid = 0;

		if (!drm_core_has_AGP(dev)) {
267
			kfree(map);
268 269
			return -EINVAL;
		}
L
Linus Torvalds 已提交
270
#ifdef __alpha__
271
		map->offset += dev->hose->mem_space->start;
L
Linus Torvalds 已提交
272
#endif
273 274 275 276 277
		/* In some cases (i810 driver), user space may have already
		 * added the AGP base itself, because dev->agp->base previously
		 * only got set during AGP enable.  So, only add the base
		 * address if the map's offset isn't already within the
		 * aperture.
278
		 */
279 280 281 282 283
		if (map->offset < dev->agp->base ||
		    map->offset > dev->agp->base +
		    dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
			map->offset += dev->agp->base;
		}
284 285 286 287 288 289 290 291
		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
		 */
292
		list_for_each_entry(entry, &dev->agp->memory, head) {
293 294 295 296 297
			if ((map->offset >= entry->bound) &&
			    (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
				valid = 1;
				break;
			}
L
Linus Torvalds 已提交
298
		}
299
		if (!list_empty(&dev->agp->memory) && !valid) {
300
			kfree(map);
301 302
			return -EPERM;
		}
303 304
		DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
			  (unsigned long long)map->offset, map->size);
305

J
Jesse Barnes 已提交
306
		break;
307
	}
J
Jesse Barnes 已提交
308
	case _DRM_GEM:
309
		DRM_ERROR("tried to addmap GEM object\n");
L
Linus Torvalds 已提交
310 311 312
		break;
	case _DRM_SCATTER_GATHER:
		if (!dev->sg) {
313
			kfree(map);
L
Linus Torvalds 已提交
314 315
			return -EINVAL;
		}
316
		map->offset += (unsigned long)dev->sg->virtual;
L
Linus Torvalds 已提交
317
		break;
D
Dave Airlie 已提交
318
	case _DRM_CONSISTENT:
D
Dave Airlie 已提交
319
		/* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
320
		 * As we're limiting the address to 2^32-1 (or less),
D
Dave Airlie 已提交
321 322
		 * casting it down to 32 bits is no problem, but we
		 * need to point to a 64bit variable first. */
323
		dmah = drm_pci_alloc(dev, map->size, map->size);
324
		if (!dmah) {
325
			kfree(map);
D
Dave Airlie 已提交
326 327
			return -ENOMEM;
		}
328 329 330
		map->handle = dmah->vaddr;
		map->offset = (unsigned long)dmah->busaddr;
		kfree(dmah);
D
Dave Airlie 已提交
331
		break;
L
Linus Torvalds 已提交
332
	default:
333
		kfree(map);
L
Linus Torvalds 已提交
334 335 336
		return -EINVAL;
	}

337
	list = kzalloc(sizeof(*list), GFP_KERNEL);
D
Dave Airlie 已提交
338
	if (!list) {
339
		if (map->type == _DRM_REGISTERS)
340
			iounmap(map->handle);
341
		kfree(map);
L
Linus Torvalds 已提交
342 343 344 345
		return -EINVAL;
	}
	list->map = map;

D
Dave Airlie 已提交
346
	mutex_lock(&dev->struct_mutex);
347
	list_add(&list->head, &dev->maplist);
348

349
	/* Assign a 32-bit handle */
D
Dave Airlie 已提交
350
	/* We do it here so that dev->struct_mutex protects the increment */
351 352
	user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
		map->offset;
353 354
	ret = drm_map_handle(dev, &list->hash, user_token, 0,
			     (map->type == _DRM_SHM));
355
	if (ret) {
356
		if (map->type == _DRM_REGISTERS)
357
			iounmap(map->handle);
358 359
		kfree(map);
		kfree(list);
360 361 362 363
		mutex_unlock(&dev->struct_mutex);
		return ret;
	}

364
	list->user_token = list->hash.key << PAGE_SHIFT;
D
Dave Airlie 已提交
365
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
366

367 368
	if (!(map->flags & _DRM_DRIVER))
		list->master = dev->primary->master;
369
	*maplist = list;
370
	return 0;
371
	}
372

373
int drm_addmap(struct drm_device * dev, resource_size_t offset,
374
	       unsigned int size, enum drm_map_type type,
375
	       enum drm_map_flags flags, struct drm_local_map ** map_ptr)
376
{
D
Dave Airlie 已提交
377
	struct drm_map_list *list;
378 379 380 381 382 383 384
	int rc;

	rc = drm_addmap_core(dev, offset, size, type, flags, &list);
	if (!rc)
		*map_ptr = list->map;
	return rc;
}
D
Dave Airlie 已提交
385

386 387
EXPORT_SYMBOL(drm_addmap);

388 389 390 391 392 393 394 395 396 397 398
/**
 * Ioctl to specify a range of memory that is available for mapping by a
 * non-root process.
 *
 * \param inode device inode.
 * \param file_priv DRM file private.
 * \param cmd command.
 * \param arg pointer to a drm_map structure.
 * \return zero on success or a negative value on error.
 *
 */
399 400
int drm_addmap_ioctl(struct drm_device *dev, void *data,
		     struct drm_file *file_priv)
401
{
402
	struct drm_map *map = data;
D
Dave Airlie 已提交
403
	struct drm_map_list *maplist;
404 405
	int err;

406
	if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
407 408
		return -EPERM;

409 410
	err = drm_addmap_core(dev, map->offset, map->size, map->type,
			      map->flags, &maplist);
411

D
Dave Airlie 已提交
412
	if (err)
413
		return err;
414

415
	/* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
416
	map->handle = (void *)(unsigned long)maplist->user_token;
L
Linus Torvalds 已提交
417
	return 0;
D
Dave Airlie 已提交
418
}
L
Linus Torvalds 已提交
419 420 421 422 423 424 425 426 427

/**
 * Remove a map private from list and deallocate resources if the mapping
 * isn't in use.
 *
 * 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.
 *
428
 * \sa drm_addmap
L
Linus Torvalds 已提交
429
 */
430
int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
L
Linus Torvalds 已提交
431
{
D
Dave Airlie 已提交
432
	struct drm_map_list *r_list = NULL, *list_t;
D
Dave Airlie 已提交
433
	drm_dma_handle_t dmah;
434
	int found = 0;
435
	struct drm_master *master;
L
Linus Torvalds 已提交
436

D
Dave Airlie 已提交
437
	/* Find the list entry for the map and remove it */
438
	list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
D
Dave Airlie 已提交
439
		if (r_list->map == map) {
440
			master = r_list->master;
441
			list_del(&r_list->head);
442 443
			drm_ht_remove_key(&dev->map_hash,
					  r_list->user_token >> PAGE_SHIFT);
444
			kfree(r_list);
445
			found = 1;
D
Dave Airlie 已提交
446 447
			break;
		}
L
Linus Torvalds 已提交
448 449
	}

450
	if (!found)
L
Linus Torvalds 已提交
451 452
		return -EINVAL;

D
Dave Airlie 已提交
453 454
	switch (map->type) {
	case _DRM_REGISTERS:
455
		iounmap(map->handle);
D
Dave Airlie 已提交
456 457
		/* FALLTHROUGH */
	case _DRM_FRAME_BUFFER:
458 459
		if (drm_core_has_MTRR(dev))
			arch_phys_wc_del(map->mtrr);
D
Dave Airlie 已提交
460 461 462
		break;
	case _DRM_SHM:
		vfree(map->handle);
463 464 465 466 467
		if (master) {
			if (dev->sigdata.lock == master->lock.hw_lock)
				dev->sigdata.lock = NULL;
			master->lock.hw_lock = NULL;   /* SHM removed */
			master->lock.file_priv = NULL;
468
			wake_up_interruptible_all(&master->lock.lock_queue);
469
		}
D
Dave Airlie 已提交
470 471 472 473 474 475 476 477 478 479
		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;
J
Jesse Barnes 已提交
480 481 482
	case _DRM_GEM:
		DRM_ERROR("tried to rmmap GEM object\n");
		break;
L
Linus Torvalds 已提交
483
	}
484
	kfree(map);
D
Dave Airlie 已提交
485

L
Linus Torvalds 已提交
486 487
	return 0;
}
488
EXPORT_SYMBOL(drm_rmmap_locked);
D
Dave Airlie 已提交
489

490
int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
D
Dave Airlie 已提交
491 492 493
{
	int ret;

D
Dave Airlie 已提交
494
	mutex_lock(&dev->struct_mutex);
D
Dave Airlie 已提交
495
	ret = drm_rmmap_locked(dev, map);
D
Dave Airlie 已提交
496
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
497 498 499

	return ret;
}
J
Jesse Barnes 已提交
500
EXPORT_SYMBOL(drm_rmmap);
501

D
Dave Airlie 已提交
502 503 504 505 506 507 508 509
/* 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.
510 511 512 513 514 515
 *
 * \param inode device inode.
 * \param file_priv DRM file private.
 * \param cmd command.
 * \param arg pointer to a struct drm_map structure.
 * \return zero on success or a negative value on error.
D
Dave Airlie 已提交
516
 */
517 518
int drm_rmmap_ioctl(struct drm_device *dev, void *data,
		    struct drm_file *file_priv)
519
{
520
	struct drm_map *request = data;
521
	struct drm_local_map *map = NULL;
D
Dave Airlie 已提交
522
	struct drm_map_list *r_list;
D
Dave Airlie 已提交
523
	int ret;
524

D
Dave Airlie 已提交
525
	mutex_lock(&dev->struct_mutex);
526
	list_for_each_entry(r_list, &dev->maplist, head) {
D
Dave Airlie 已提交
527
		if (r_list->map &&
528
		    r_list->user_token == (unsigned long)request->handle &&
D
Dave Airlie 已提交
529 530 531 532 533 534 535 536 537
		    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.
	 */
538
	if (list_empty(&dev->maplist) || !map) {
D
Dave Airlie 已提交
539
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
540 541 542 543 544
		return -EINVAL;
	}

	/* Register and framebuffer maps are permanent */
	if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
D
Dave Airlie 已提交
545
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
546 547 548 549 550
		return 0;
	}

	ret = drm_rmmap_locked(dev, map);

D
Dave Airlie 已提交
551
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
552 553

	return ret;
554
}
L
Linus Torvalds 已提交
555 556 557 558

/**
 * Cleanup after an error on one of the addbufs() functions.
 *
D
Dave Airlie 已提交
559
 * \param dev DRM device.
L
Linus Torvalds 已提交
560 561 562 563
 * \param entry buffer entry where the error occurred.
 *
 * Frees any pages and buffers associated with the given entry.
 */
564 565
static void drm_cleanup_buf_error(struct drm_device * dev,
				  struct drm_buf_entry * entry)
L
Linus Torvalds 已提交
566 567 568 569 570 571
{
	int i;

	if (entry->seg_count) {
		for (i = 0; i < entry->seg_count; i++) {
			if (entry->seglist[i]) {
D
Dave Airlie 已提交
572
				drm_pci_free(dev, entry->seglist[i]);
L
Linus Torvalds 已提交
573 574
			}
		}
575
		kfree(entry->seglist);
L
Linus Torvalds 已提交
576 577 578 579

		entry->seg_count = 0;
	}

D
Dave Airlie 已提交
580 581
	if (entry->buf_count) {
		for (i = 0; i < entry->buf_count; i++) {
582
			kfree(entry->buflist[i].dev_private);
L
Linus Torvalds 已提交
583
		}
584
		kfree(entry->buflist);
L
Linus Torvalds 已提交
585 586 587 588 589 590 591

		entry->buf_count = 0;
	}
}

#if __OS_HAS_AGP
/**
592
 * Add AGP buffers for DMA transfers.
L
Linus Torvalds 已提交
593
 *
594
 * \param dev struct drm_device to which the buffers are to be added.
595
 * \param request pointer to a struct drm_buf_desc describing the request.
L
Linus Torvalds 已提交
596
 * \return zero on success or a negative number on failure.
D
Dave Airlie 已提交
597
 *
L
Linus Torvalds 已提交
598 599 600 601
 * 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.
 */
602
int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
L
Linus Torvalds 已提交
603
{
604 605
	struct drm_device_dma *dma = dev->dma;
	struct drm_buf_entry *entry;
D
Dave Airlie 已提交
606
	struct drm_agp_mem *agp_entry;
D
Dave Airlie 已提交
607
	struct drm_buf *buf;
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615 616
	unsigned long offset;
	unsigned long agp_offset;
	int count;
	int order;
	int size;
	int alignment;
	int page_order;
	int total;
	int byte_count;
617
	int i, valid;
D
Dave Airlie 已提交
618
	struct drm_buf **temp_buflist;
L
Linus Torvalds 已提交
619

D
Dave Airlie 已提交
620 621
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
622

623 624
	count = request->count;
	order = drm_order(request->size);
L
Linus Torvalds 已提交
625 626
	size = 1 << order;

D
Dave Airlie 已提交
627 628
	alignment = (request->flags & _DRM_PAGE_ALIGN)
	    ? PAGE_ALIGN(size) : size;
L
Linus Torvalds 已提交
629 630 631 632
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

	byte_count = 0;
633
	agp_offset = dev->agp->base + request->agp_start;
L
Linus Torvalds 已提交
634

D
Dave Airlie 已提交
635 636 637
	DRM_DEBUG("count:      %d\n", count);
	DRM_DEBUG("order:      %d\n", order);
	DRM_DEBUG("size:       %d\n", size);
638
	DRM_DEBUG("agp_offset: %lx\n", agp_offset);
D
Dave Airlie 已提交
639 640 641
	DRM_DEBUG("alignment:  %d\n", alignment);
	DRM_DEBUG("page_order: %d\n", page_order);
	DRM_DEBUG("total:      %d\n", total);
L
Linus Torvalds 已提交
642

D
Dave Airlie 已提交
643 644
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
L
Linus Torvalds 已提交
645

646 647
	/* Make sure buffers are located in AGP memory that we own */
	valid = 0;
648
	list_for_each_entry(agp_entry, &dev->agp->memory, head) {
649 650 651 652 653 654
		if ((agp_offset >= agp_entry->bound) &&
		    (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
			valid = 1;
			break;
		}
	}
655
	if (!list_empty(&dev->agp->memory) && !valid) {
656 657 658
		DRM_DEBUG("zone invalid\n");
		return -EINVAL;
	}
D
Dave Airlie 已提交
659 660 661
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
662 663
		return -EBUSY;
	}
D
Dave Airlie 已提交
664 665
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
666

D
Dave Airlie 已提交
667
	mutex_lock(&dev->struct_mutex);
L
Linus Torvalds 已提交
668
	entry = &dma->bufs[order];
D
Dave Airlie 已提交
669
	if (entry->buf_count) {
D
Dave Airlie 已提交
670
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
671 672
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
L
Linus Torvalds 已提交
673 674 675
	}

	if (count < 0 || count > 4096) {
D
Dave Airlie 已提交
676
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
677
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
678 679 680
		return -EINVAL;
	}

681
	entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
D
Dave Airlie 已提交
682
	if (!entry->buflist) {
D
Dave Airlie 已提交
683
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
684
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
685 686 687 688 689 690 691 692
		return -ENOMEM;
	}

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

	offset = 0;

D
Dave Airlie 已提交
693 694 695 696 697 698
	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;
L
Linus Torvalds 已提交
699

D
Dave Airlie 已提交
700
		buf->offset = (dma->byte_count + offset);
L
Linus Torvalds 已提交
701 702
		buf->bus_address = agp_offset + offset;
		buf->address = (void *)(agp_offset + offset);
D
Dave Airlie 已提交
703
		buf->next = NULL;
L
Linus Torvalds 已提交
704 705
		buf->waiting = 0;
		buf->pending = 0;
706
		buf->file_priv = NULL;
L
Linus Torvalds 已提交
707 708

		buf->dev_priv_size = dev->driver->dev_priv_size;
709
		buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
D
Dave Airlie 已提交
710
		if (!buf->dev_private) {
L
Linus Torvalds 已提交
711 712
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
D
Dave Airlie 已提交
713
			drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
714
			mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
715
			atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
716 717 718
			return -ENOMEM;
		}

D
Dave Airlie 已提交
719
		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
L
Linus Torvalds 已提交
720 721 722 723 724 725

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

D
Dave Airlie 已提交
726
	DRM_DEBUG("byte_count: %d\n", byte_count);
L
Linus Torvalds 已提交
727

728 729 730
	temp_buflist = krealloc(dma->buflist,
				(dma->buf_count + entry->buf_count) *
				sizeof(*dma->buflist), GFP_KERNEL);
D
Dave Airlie 已提交
731
	if (!temp_buflist) {
L
Linus Torvalds 已提交
732
		/* Free the entry because it isn't valid */
D
Dave Airlie 已提交
733
		drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
734
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
735
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
736 737 738 739
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

D
Dave Airlie 已提交
740
	for (i = 0; i < entry->buf_count; i++) {
L
Linus Torvalds 已提交
741 742 743 744
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

	dma->buf_count += entry->buf_count;
745 746
	dma->seg_count += entry->seg_count;
	dma->page_count += byte_count >> PAGE_SHIFT;
L
Linus Torvalds 已提交
747 748
	dma->byte_count += byte_count;

D
Dave Airlie 已提交
749 750
	DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
	DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
L
Linus Torvalds 已提交
751

D
Dave Airlie 已提交
752
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
753

754 755
	request->count = entry->buf_count;
	request->size = size;
L
Linus Torvalds 已提交
756 757 758

	dma->flags = _DRM_DMA_USE_AGP;

D
Dave Airlie 已提交
759
	atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
760 761
	return 0;
}
762
EXPORT_SYMBOL(drm_addbufs_agp);
D
Dave Airlie 已提交
763
#endif				/* __OS_HAS_AGP */
L
Linus Torvalds 已提交
764

765
int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
L
Linus Torvalds 已提交
766
{
767
	struct drm_device_dma *dma = dev->dma;
L
Linus Torvalds 已提交
768 769 770 771 772
	int count;
	int order;
	int size;
	int total;
	int page_order;
773
	struct drm_buf_entry *entry;
D
Dave Airlie 已提交
774
	drm_dma_handle_t *dmah;
D
Dave Airlie 已提交
775
	struct drm_buf *buf;
L
Linus Torvalds 已提交
776 777 778 779 780 781
	int alignment;
	unsigned long offset;
	int i;
	int byte_count;
	int page_count;
	unsigned long *temp_pagelist;
D
Dave Airlie 已提交
782
	struct drm_buf **temp_buflist;
L
Linus Torvalds 已提交
783

D
Dave Airlie 已提交
784 785
	if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
		return -EINVAL;
786

D
Dave Airlie 已提交
787 788
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
789

790 791 792
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

793 794
	count = request->count;
	order = drm_order(request->size);
L
Linus Torvalds 已提交
795 796
	size = 1 << order;

D
Daniel Vetter 已提交
797 798
	DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
		  request->count, request->size, size, order);
L
Linus Torvalds 已提交
799

D
Dave Airlie 已提交
800 801
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
L
Linus Torvalds 已提交
802

803
	alignment = (request->flags & _DRM_PAGE_ALIGN)
D
Dave Airlie 已提交
804
	    ? PAGE_ALIGN(size) : size;
L
Linus Torvalds 已提交
805 806 807
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

D
Dave Airlie 已提交
808 809 810
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
811 812
		return -EBUSY;
	}
D
Dave Airlie 已提交
813 814
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
815

D
Dave Airlie 已提交
816
	mutex_lock(&dev->struct_mutex);
L
Linus Torvalds 已提交
817
	entry = &dma->bufs[order];
D
Dave Airlie 已提交
818
	if (entry->buf_count) {
D
Dave Airlie 已提交
819
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
820
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
821 822 823 824
		return -ENOMEM;	/* May only call once for each order */
	}

	if (count < 0 || count > 4096) {
D
Dave Airlie 已提交
825
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
826
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
827 828 829
		return -EINVAL;
	}

830
	entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
D
Dave Airlie 已提交
831
	if (!entry->buflist) {
D
Dave Airlie 已提交
832
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
833
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
834 835
		return -ENOMEM;
	}
D
Dave Airlie 已提交
836

837
	entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
D
Dave Airlie 已提交
838
	if (!entry->seglist) {
839
		kfree(entry->buflist);
D
Dave Airlie 已提交
840
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
841
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
842 843 844 845 846 847
		return -ENOMEM;
	}

	/* Keep the original pagelist until we know all the allocations
	 * have succeeded
	 */
848 849
	temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
			       sizeof(*dma->pagelist), GFP_KERNEL);
L
Linus Torvalds 已提交
850
	if (!temp_pagelist) {
851 852
		kfree(entry->buflist);
		kfree(entry->seglist);
D
Dave Airlie 已提交
853
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
854
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
855 856 857
		return -ENOMEM;
	}
	memcpy(temp_pagelist,
D
Dave Airlie 已提交
858 859 860
	       dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
	DRM_DEBUG("pagelist: %d entries\n",
		  dma->page_count + (count << page_order));
L
Linus Torvalds 已提交
861

D
Dave Airlie 已提交
862
	entry->buf_size = size;
L
Linus Torvalds 已提交
863 864 865 866
	entry->page_order = page_order;
	byte_count = 0;
	page_count = 0;

D
Dave Airlie 已提交
867
	while (entry->buf_count < count) {
D
Dave Airlie 已提交
868

869
		dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
D
Dave Airlie 已提交
870

D
Dave Airlie 已提交
871
		if (!dmah) {
L
Linus Torvalds 已提交
872 873 874 875
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
			entry->seg_count = count;
			drm_cleanup_buf_error(dev, entry);
876
			kfree(temp_pagelist);
D
Dave Airlie 已提交
877
			mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
878
			atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
879 880
			return -ENOMEM;
		}
D
Dave Airlie 已提交
881
		entry->seglist[entry->seg_count++] = dmah;
D
Dave Airlie 已提交
882 883 884
		for (i = 0; i < (1 << page_order); i++) {
			DRM_DEBUG("page %d @ 0x%08lx\n",
				  dma->page_count + page_count,
D
Dave Airlie 已提交
885
				  (unsigned long)dmah->vaddr + PAGE_SIZE * i);
L
Linus Torvalds 已提交
886
			temp_pagelist[dma->page_count + page_count++]
D
Dave Airlie 已提交
887
				= (unsigned long)dmah->vaddr + PAGE_SIZE * i;
L
Linus Torvalds 已提交
888
		}
D
Dave Airlie 已提交
889 890 891 892 893 894 895 896 897
		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);
D
Dave Airlie 已提交
898 899
			buf->address = (void *)(dmah->vaddr + offset);
			buf->bus_address = dmah->busaddr + offset;
D
Dave Airlie 已提交
900
			buf->next = NULL;
L
Linus Torvalds 已提交
901 902
			buf->waiting = 0;
			buf->pending = 0;
903
			buf->file_priv = NULL;
L
Linus Torvalds 已提交
904 905

			buf->dev_priv_size = dev->driver->dev_priv_size;
906 907
			buf->dev_private = kzalloc(buf->dev_priv_size,
						GFP_KERNEL);
D
Dave Airlie 已提交
908
			if (!buf->dev_private) {
L
Linus Torvalds 已提交
909 910 911
				/* Set count correctly so we free the proper amount. */
				entry->buf_count = count;
				entry->seg_count = count;
D
Dave Airlie 已提交
912
				drm_cleanup_buf_error(dev, entry);
913
				kfree(temp_pagelist);
D
Dave Airlie 已提交
914
				mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
915
				atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
916 917 918
				return -ENOMEM;
			}

D
Dave Airlie 已提交
919 920
			DRM_DEBUG("buffer %d @ %p\n",
				  entry->buf_count, buf->address);
L
Linus Torvalds 已提交
921 922 923 924
		}
		byte_count += PAGE_SIZE << page_order;
	}

925 926 927
	temp_buflist = krealloc(dma->buflist,
				(dma->buf_count + entry->buf_count) *
				sizeof(*dma->buflist), GFP_KERNEL);
L
Linus Torvalds 已提交
928 929
	if (!temp_buflist) {
		/* Free the entry because it isn't valid */
D
Dave Airlie 已提交
930
		drm_cleanup_buf_error(dev, entry);
931
		kfree(temp_pagelist);
D
Dave Airlie 已提交
932
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
933
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
934 935 936 937
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

D
Dave Airlie 已提交
938
	for (i = 0; i < entry->buf_count; i++) {
L
Linus Torvalds 已提交
939 940 941
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

T
Thomas Weber 已提交
942
	/* No allocations failed, so now we can replace the original pagelist
L
Linus Torvalds 已提交
943 944 945
	 * with the new one.
	 */
	if (dma->page_count) {
946
		kfree(dma->pagelist);
L
Linus Torvalds 已提交
947 948 949 950 951 952 953 954
	}
	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);

D
Dave Airlie 已提交
955
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
956

957 958
	request->count = entry->buf_count;
	request->size = size;
L
Linus Torvalds 已提交
959

960 961 962
	if (request->flags & _DRM_PCI_BUFFER_RO)
		dma->flags = _DRM_DMA_USE_PCI_RO;

D
Dave Airlie 已提交
963
	atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
964 965 966
	return 0;

}
967
EXPORT_SYMBOL(drm_addbufs_pci);
L
Linus Torvalds 已提交
968

969
static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
L
Linus Torvalds 已提交
970
{
971 972
	struct drm_device_dma *dma = dev->dma;
	struct drm_buf_entry *entry;
D
Dave Airlie 已提交
973
	struct drm_buf *buf;
L
Linus Torvalds 已提交
974 975 976 977 978 979 980 981 982 983
	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;
D
Dave Airlie 已提交
984
	struct drm_buf **temp_buflist;
L
Linus Torvalds 已提交
985

D
Dave Airlie 已提交
986 987 988 989 990
	if (!drm_core_check_feature(dev, DRIVER_SG))
		return -EINVAL;

	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
991

992 993 994
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

995 996
	count = request->count;
	order = drm_order(request->size);
L
Linus Torvalds 已提交
997 998
	size = 1 << order;

D
Dave Airlie 已提交
999 1000
	alignment = (request->flags & _DRM_PAGE_ALIGN)
	    ? PAGE_ALIGN(size) : size;
L
Linus Torvalds 已提交
1001 1002 1003 1004
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

	byte_count = 0;
1005
	agp_offset = request->agp_start;
L
Linus Torvalds 已提交
1006

D
Dave Airlie 已提交
1007 1008 1009 1010 1011 1012 1013
	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);
L
Linus Torvalds 已提交
1014

D
Dave Airlie 已提交
1015 1016
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
L
Linus Torvalds 已提交
1017

D
Dave Airlie 已提交
1018 1019 1020
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1021 1022
		return -EBUSY;
	}
D
Dave Airlie 已提交
1023 1024
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1025

D
Dave Airlie 已提交
1026
	mutex_lock(&dev->struct_mutex);
L
Linus Torvalds 已提交
1027
	entry = &dma->bufs[order];
D
Dave Airlie 已提交
1028
	if (entry->buf_count) {
D
Dave Airlie 已提交
1029
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1030 1031
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
L
Linus Torvalds 已提交
1032 1033 1034
	}

	if (count < 0 || count > 4096) {
D
Dave Airlie 已提交
1035
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1036
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
1037 1038 1039
		return -EINVAL;
	}

1040
	entry->buflist = kzalloc(count * sizeof(*entry->buflist),
1041
				GFP_KERNEL);
D
Dave Airlie 已提交
1042
	if (!entry->buflist) {
D
Dave Airlie 已提交
1043
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1044
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
1045 1046 1047 1048 1049 1050 1051 1052
		return -ENOMEM;
	}

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

	offset = 0;

D
Dave Airlie 已提交
1053 1054 1055 1056 1057 1058
	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;
L
Linus Torvalds 已提交
1059

D
Dave Airlie 已提交
1060
		buf->offset = (dma->byte_count + offset);
L
Linus Torvalds 已提交
1061
		buf->bus_address = agp_offset + offset;
D
Dave Airlie 已提交
1062
		buf->address = (void *)(agp_offset + offset
1063
					+ (unsigned long)dev->sg->virtual);
D
Dave Airlie 已提交
1064
		buf->next = NULL;
L
Linus Torvalds 已提交
1065 1066
		buf->waiting = 0;
		buf->pending = 0;
1067
		buf->file_priv = NULL;
L
Linus Torvalds 已提交
1068 1069

		buf->dev_priv_size = dev->driver->dev_priv_size;
1070
		buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
D
Dave Airlie 已提交
1071
		if (!buf->dev_private) {
L
Linus Torvalds 已提交
1072 1073
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
D
Dave Airlie 已提交
1074
			drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
1075
			mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1076
			atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
1077 1078 1079
			return -ENOMEM;
		}

D
Dave Airlie 已提交
1080
		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
L
Linus Torvalds 已提交
1081 1082 1083 1084 1085 1086

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

D
Dave Airlie 已提交
1087
	DRM_DEBUG("byte_count: %d\n", byte_count);
L
Linus Torvalds 已提交
1088

1089 1090 1091
	temp_buflist = krealloc(dma->buflist,
				(dma->buf_count + entry->buf_count) *
				sizeof(*dma->buflist), GFP_KERNEL);
D
Dave Airlie 已提交
1092
	if (!temp_buflist) {
L
Linus Torvalds 已提交
1093
		/* Free the entry because it isn't valid */
D
Dave Airlie 已提交
1094
		drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
1095
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1096
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
1097 1098 1099 1100
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

D
Dave Airlie 已提交
1101
	for (i = 0; i < entry->buf_count; i++) {
L
Linus Torvalds 已提交
1102 1103 1104 1105
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

	dma->buf_count += entry->buf_count;
1106 1107
	dma->seg_count += entry->seg_count;
	dma->page_count += byte_count >> PAGE_SHIFT;
L
Linus Torvalds 已提交
1108 1109
	dma->byte_count += byte_count;

D
Dave Airlie 已提交
1110 1111
	DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
	DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
L
Linus Torvalds 已提交
1112

D
Dave Airlie 已提交
1113
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
1114

1115 1116
	request->count = entry->buf_count;
	request->size = size;
L
Linus Torvalds 已提交
1117 1118 1119

	dma->flags = _DRM_DMA_USE_SG;

D
Dave Airlie 已提交
1120
	atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
1121 1122 1123
	return 0;
}

1124
static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
D
Dave Airlie 已提交
1125
{
1126 1127
	struct drm_device_dma *dma = dev->dma;
	struct drm_buf_entry *entry;
D
Dave Airlie 已提交
1128
	struct drm_buf *buf;
D
Dave Airlie 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	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;
D
Dave Airlie 已提交
1139
	struct drm_buf **temp_buflist;
D
Dave Airlie 已提交
1140 1141 1142

	if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
		return -EINVAL;
D
Dave Airlie 已提交
1143

D
Dave Airlie 已提交
1144 1145 1146
	if (!dma)
		return -EINVAL;

1147 1148 1149
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

1150 1151
	count = request->count;
	order = drm_order(request->size);
D
Dave Airlie 已提交
1152 1153
	size = 1 << order;

1154
	alignment = (request->flags & _DRM_PAGE_ALIGN)
D
Dave Airlie 已提交
1155 1156 1157 1158 1159
	    ? PAGE_ALIGN(size) : size;
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

	byte_count = 0;
1160
	agp_offset = request->agp_start;
D
Dave Airlie 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

	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;

	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);

D
Dave Airlie 已提交
1181
	mutex_lock(&dev->struct_mutex);
D
Dave Airlie 已提交
1182 1183
	entry = &dma->bufs[order];
	if (entry->buf_count) {
D
Dave Airlie 已提交
1184
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1185 1186 1187 1188 1189
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
	}

	if (count < 0 || count > 4096) {
D
Dave Airlie 已提交
1190
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1191 1192 1193 1194
		atomic_dec(&dev->buf_alloc);
		return -EINVAL;
	}

1195
	entry->buflist = kzalloc(count * sizeof(*entry->buflist),
1196
				GFP_KERNEL);
D
Dave Airlie 已提交
1197
	if (!entry->buflist) {
D
Dave Airlie 已提交
1198
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;
	}

	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;
1221
		buf->file_priv = NULL;
D
Dave Airlie 已提交
1222 1223

		buf->dev_priv_size = dev->driver->dev_priv_size;
1224
		buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
D
Dave Airlie 已提交
1225 1226 1227 1228
		if (!buf->dev_private) {
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
			drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
1229
			mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
			atomic_dec(&dev->buf_alloc);
			return -ENOMEM;
		}

		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);

1243 1244 1245
	temp_buflist = krealloc(dma->buflist,
				(dma->buf_count + entry->buf_count) *
				sizeof(*dma->buflist), GFP_KERNEL);
D
Dave Airlie 已提交
1246 1247 1248
	if (!temp_buflist) {
		/* Free the entry because it isn't valid */
		drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
1249
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
		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;
1260 1261
	dma->seg_count += entry->seg_count;
	dma->page_count += byte_count >> PAGE_SHIFT;
D
Dave Airlie 已提交
1262 1263 1264 1265 1266
	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);

D
Dave Airlie 已提交
1267
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1268

1269 1270
	request->count = entry->buf_count;
	request->size = size;
D
Dave Airlie 已提交
1271 1272 1273 1274 1275 1276

	dma->flags = _DRM_DMA_USE_FB;

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

D
Dave Airlie 已提交
1278

L
Linus Torvalds 已提交
1279 1280 1281 1282
/**
 * Add buffers for DMA transfers (ioctl).
 *
 * \param inode device inode.
1283
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1284
 * \param cmd command.
1285
 * \param arg pointer to a struct drm_buf_desc request.
L
Linus Torvalds 已提交
1286 1287 1288 1289 1290 1291 1292
 * \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.
 */
1293 1294
int drm_addbufs(struct drm_device *dev, void *data,
		struct drm_file *file_priv)
L
Linus Torvalds 已提交
1295
{
1296
	struct drm_buf_desc *request = data;
1297
	int ret;
D
Dave Airlie 已提交
1298

L
Linus Torvalds 已提交
1299 1300 1301 1302
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

#if __OS_HAS_AGP
1303 1304
	if (request->flags & _DRM_AGP_BUFFER)
		ret = drm_addbufs_agp(dev, request);
L
Linus Torvalds 已提交
1305 1306
	else
#endif
1307 1308 1309 1310
	if (request->flags & _DRM_SG_BUFFER)
		ret = drm_addbufs_sg(dev, request);
	else if (request->flags & _DRM_FB_BUFFER)
		ret = drm_addbufs_fb(dev, request);
L
Linus Torvalds 已提交
1311
	else
1312
		ret = drm_addbufs_pci(dev, request);
1313 1314

	return ret;
L
Linus Torvalds 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
}

/**
 * Get information about the buffer mappings.
 *
 * This was originally mean for debugging purposes, or by a sophisticated
 * client library to determine how best to use the available buffers (e.g.,
 * large buffers can be used for image transfer).
 *
 * \param inode device inode.
1325
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1326 1327 1328 1329 1330 1331 1332 1333
 * \param cmd command.
 * \param arg pointer to a drm_buf_info structure.
 * \return zero on success or a negative number on failure.
 *
 * Increments drm_device::buf_use while holding the drm_device::count_lock
 * lock, preventing of allocating more buffers after this call. Information
 * about each requested buffer is then copied into user space.
 */
1334 1335
int drm_infobufs(struct drm_device *dev, void *data,
		 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1336
{
1337
	struct drm_device_dma *dma = dev->dma;
1338
	struct drm_buf_info *request = data;
L
Linus Torvalds 已提交
1339 1340 1341 1342 1343 1344
	int i;
	int count;

	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1345 1346
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1347

D
Dave Airlie 已提交
1348 1349 1350
	spin_lock(&dev->count_lock);
	if (atomic_read(&dev->buf_alloc)) {
		spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1351 1352 1353
		return -EBUSY;
	}
	++dev->buf_use;		/* Can't allocate more after this call */
D
Dave Airlie 已提交
1354
	spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1355

D
Dave Airlie 已提交
1356 1357 1358
	for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
		if (dma->bufs[i].buf_count)
			++count;
L
Linus Torvalds 已提交
1359 1360
	}

D
Dave Airlie 已提交
1361
	DRM_DEBUG("count = %d\n", count);
L
Linus Torvalds 已提交
1362

1363
	if (request->count >= count) {
D
Dave Airlie 已提交
1364 1365
		for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
			if (dma->bufs[i].buf_count) {
1366
				struct drm_buf_desc __user *to =
1367
				    &request->list[count];
1368 1369
				struct drm_buf_entry *from = &dma->bufs[i];
				struct drm_freelist *list = &dma->bufs[i].freelist;
D
Dave Airlie 已提交
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
				if (copy_to_user(&to->count,
						 &from->buf_count,
						 sizeof(from->buf_count)) ||
				    copy_to_user(&to->size,
						 &from->buf_size,
						 sizeof(from->buf_size)) ||
				    copy_to_user(&to->low_mark,
						 &list->low_mark,
						 sizeof(list->low_mark)) ||
				    copy_to_user(&to->high_mark,
						 &list->high_mark,
						 sizeof(list->high_mark)))
L
Linus Torvalds 已提交
1382 1383
					return -EFAULT;

D
Dave Airlie 已提交
1384 1385 1386 1387 1388 1389
				DRM_DEBUG("%d %d %d %d %d\n",
					  i,
					  dma->bufs[i].buf_count,
					  dma->bufs[i].buf_size,
					  dma->bufs[i].freelist.low_mark,
					  dma->bufs[i].freelist.high_mark);
L
Linus Torvalds 已提交
1390 1391 1392 1393
				++count;
			}
		}
	}
1394
	request->count = count;
L
Linus Torvalds 已提交
1395 1396 1397 1398 1399 1400 1401 1402

	return 0;
}

/**
 * Specifies a low and high water mark for buffer allocation
 *
 * \param inode device inode.
1403
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1404 1405 1406 1407 1408 1409 1410 1411 1412
 * \param cmd command.
 * \param arg a pointer to a drm_buf_desc structure.
 * \return zero on success or a negative number on failure.
 *
 * Verifies that the size order is bounded between the admissible orders and
 * updates the respective drm_device_dma::bufs entry low and high water mark.
 *
 * \note This ioctl is deprecated and mostly never used.
 */
1413 1414
int drm_markbufs(struct drm_device *dev, void *data,
		 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1415
{
1416
	struct drm_device_dma *dma = dev->dma;
1417
	struct drm_buf_desc *request = data;
L
Linus Torvalds 已提交
1418
	int order;
1419
	struct drm_buf_entry *entry;
L
Linus Torvalds 已提交
1420 1421 1422 1423

	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1424 1425
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1426

D
Dave Airlie 已提交
1427
	DRM_DEBUG("%d, %d, %d\n",
1428 1429
		  request->size, request->low_mark, request->high_mark);
	order = drm_order(request->size);
D
Dave Airlie 已提交
1430 1431
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
L
Linus Torvalds 已提交
1432 1433
	entry = &dma->bufs[order];

1434
	if (request->low_mark < 0 || request->low_mark > entry->buf_count)
L
Linus Torvalds 已提交
1435
		return -EINVAL;
1436
	if (request->high_mark < 0 || request->high_mark > entry->buf_count)
L
Linus Torvalds 已提交
1437 1438
		return -EINVAL;

1439 1440
	entry->freelist.low_mark = request->low_mark;
	entry->freelist.high_mark = request->high_mark;
L
Linus Torvalds 已提交
1441 1442 1443 1444 1445

	return 0;
}

/**
D
Dave Airlie 已提交
1446
 * Unreserve the buffers in list, previously reserved using drmDMA.
L
Linus Torvalds 已提交
1447 1448
 *
 * \param inode device inode.
1449
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1450 1451 1452
 * \param cmd command.
 * \param arg pointer to a drm_buf_free structure.
 * \return zero on success or a negative number on failure.
D
Dave Airlie 已提交
1453
 *
L
Linus Torvalds 已提交
1454 1455 1456
 * Calls free_buffer() for each used buffer.
 * This function is primarily used for debugging.
 */
1457 1458
int drm_freebufs(struct drm_device *dev, void *data,
		 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1459
{
1460
	struct drm_device_dma *dma = dev->dma;
1461
	struct drm_buf_free *request = data;
L
Linus Torvalds 已提交
1462 1463
	int i;
	int idx;
D
Dave Airlie 已提交
1464
	struct drm_buf *buf;
L
Linus Torvalds 已提交
1465 1466 1467 1468

	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1469 1470
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1471

1472 1473 1474
	DRM_DEBUG("%d\n", request->count);
	for (i = 0; i < request->count; i++) {
		if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
L
Linus Torvalds 已提交
1475
			return -EFAULT;
D
Dave Airlie 已提交
1476 1477 1478
		if (idx < 0 || idx >= dma->buf_count) {
			DRM_ERROR("Index %d (of %d max)\n",
				  idx, dma->buf_count - 1);
L
Linus Torvalds 已提交
1479 1480 1481
			return -EINVAL;
		}
		buf = dma->buflist[idx];
1482
		if (buf->file_priv != file_priv) {
D
Dave Airlie 已提交
1483
			DRM_ERROR("Process %d freeing buffer not owned\n",
1484
				  task_pid_nr(current));
L
Linus Torvalds 已提交
1485 1486
			return -EINVAL;
		}
D
Dave Airlie 已提交
1487
		drm_free_buffer(dev, buf);
L
Linus Torvalds 已提交
1488 1489 1490 1491 1492 1493 1494 1495 1496
	}

	return 0;
}

/**
 * Maps all of the DMA buffers into client-virtual space (ioctl).
 *
 * \param inode device inode.
1497
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1498 1499 1500 1501
 * \param cmd command.
 * \param arg pointer to a drm_buf_map structure.
 * \return zero on success or a negative number on failure.
 *
1502 1503
 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1504 1505
 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
 * drm_mmap_dma().
L
Linus Torvalds 已提交
1506
 */
1507 1508
int drm_mapbufs(struct drm_device *dev, void *data,
	        struct drm_file *file_priv)
L
Linus Torvalds 已提交
1509
{
1510
	struct drm_device_dma *dma = dev->dma;
L
Linus Torvalds 已提交
1511 1512 1513 1514
	int retcode = 0;
	const int zero = 0;
	unsigned long virtual;
	unsigned long address;
1515
	struct drm_buf_map *request = data;
L
Linus Torvalds 已提交
1516 1517 1518 1519 1520
	int i;

	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1521 1522
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1523

D
Dave Airlie 已提交
1524 1525 1526
	spin_lock(&dev->count_lock);
	if (atomic_read(&dev->buf_alloc)) {
		spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1527 1528 1529
		return -EBUSY;
	}
	dev->buf_use++;		/* Can't allocate more after this call */
D
Dave Airlie 已提交
1530
	spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1531

1532
	if (request->count >= dma->buf_count) {
D
Dave Airlie 已提交
1533
		if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
D
Dave Airlie 已提交
1534
		    || (drm_core_check_feature(dev, DRIVER_SG)
D
Dave Airlie 已提交
1535 1536 1537
			&& (dma->flags & _DRM_DMA_USE_SG))
		    || (drm_core_check_feature(dev, DRIVER_FB_DMA)
			&& (dma->flags & _DRM_DMA_USE_FB))) {
1538
			struct drm_local_map *map = dev->agp_buffer_map;
1539
			unsigned long token = dev->agp_buffer_token;
L
Linus Torvalds 已提交
1540

D
Dave Airlie 已提交
1541
			if (!map) {
L
Linus Torvalds 已提交
1542 1543 1544
				retcode = -EINVAL;
				goto done;
			}
1545
			virtual = vm_mmap(file_priv->filp, 0, map->size,
D
Dave Airlie 已提交
1546
					  PROT_READ | PROT_WRITE,
1547 1548
					  MAP_SHARED,
					  token);
L
Linus Torvalds 已提交
1549
		} else {
1550
			virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
D
Dave Airlie 已提交
1551 1552
					  PROT_READ | PROT_WRITE,
					  MAP_SHARED, 0);
L
Linus Torvalds 已提交
1553
		}
D
Dave Airlie 已提交
1554
		if (virtual > -1024UL) {
L
Linus Torvalds 已提交
1555 1556 1557 1558
			/* Real error */
			retcode = (signed long)virtual;
			goto done;
		}
1559
		request->virtual = (void __user *)virtual;
L
Linus Torvalds 已提交
1560

D
Dave Airlie 已提交
1561
		for (i = 0; i < dma->buf_count; i++) {
1562
			if (copy_to_user(&request->list[i].idx,
D
Dave Airlie 已提交
1563
					 &dma->buflist[i]->idx,
1564
					 sizeof(request->list[0].idx))) {
L
Linus Torvalds 已提交
1565 1566 1567
				retcode = -EFAULT;
				goto done;
			}
1568
			if (copy_to_user(&request->list[i].total,
D
Dave Airlie 已提交
1569
					 &dma->buflist[i]->total,
1570
					 sizeof(request->list[0].total))) {
L
Linus Torvalds 已提交
1571 1572 1573
				retcode = -EFAULT;
				goto done;
			}
1574
			if (copy_to_user(&request->list[i].used,
D
Dave Airlie 已提交
1575
					 &zero, sizeof(zero))) {
L
Linus Torvalds 已提交
1576 1577 1578
				retcode = -EFAULT;
				goto done;
			}
D
Dave Airlie 已提交
1579
			address = virtual + dma->buflist[i]->offset;	/* *** */
1580
			if (copy_to_user(&request->list[i].address,
D
Dave Airlie 已提交
1581
					 &address, sizeof(address))) {
L
Linus Torvalds 已提交
1582 1583 1584 1585 1586
				retcode = -EFAULT;
				goto done;
			}
		}
	}
D
Dave Airlie 已提交
1587
      done:
1588 1589
	request->count = dma->buf_count;
	DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
L
Linus Torvalds 已提交
1590 1591 1592 1593

	return retcode;
}

D
Dave Airlie 已提交
1594 1595 1596
/**
 * Compute size order.  Returns the exponent of the smaller power of two which
 * is greater or equal to given number.
D
Dave Airlie 已提交
1597
 *
D
Dave Airlie 已提交
1598 1599 1600 1601 1602
 * \param size size.
 * \return order.
 *
 * \todo Can be made faster.
 */
D
Dave Airlie 已提交
1603
int drm_order(unsigned long size)
D
Dave Airlie 已提交
1604 1605 1606 1607
{
	int order;
	unsigned long tmp;

D
Dave Airlie 已提交
1608
	for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
D
Dave Airlie 已提交
1609 1610 1611 1612 1613 1614 1615

	if (size & (size - 1))
		++order;

	return order;
}
EXPORT_SYMBOL(drm_order);