drm_bufs.c 42.5 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
		DRM_DEBUG("%lu %d %p\n",
246
			  map->size, order_base_2(map->size), map->handle);
D
Dave Airlie 已提交
247
		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;
417 418 419 420 421 422 423 424 425

	/*
	 * It appears that there are no users of this value whatsoever --
	 * drmAddMap just discards it.  Let's not encourage its use.
	 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
	 *  it's not a real mtrr index anymore.)
	 */
	map->mtrr = -1;

L
Linus Torvalds 已提交
426
	return 0;
D
Dave Airlie 已提交
427
}
L
Linus Torvalds 已提交
428 429 430 431 432 433 434 435 436

/**
 * 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.
 *
437
 * \sa drm_addmap
L
Linus Torvalds 已提交
438
 */
439
int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
L
Linus Torvalds 已提交
440
{
D
Dave Airlie 已提交
441
	struct drm_map_list *r_list = NULL, *list_t;
D
Dave Airlie 已提交
442
	drm_dma_handle_t dmah;
443
	int found = 0;
444
	struct drm_master *master;
L
Linus Torvalds 已提交
445

D
Dave Airlie 已提交
446
	/* Find the list entry for the map and remove it */
447
	list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
D
Dave Airlie 已提交
448
		if (r_list->map == map) {
449
			master = r_list->master;
450
			list_del(&r_list->head);
451 452
			drm_ht_remove_key(&dev->map_hash,
					  r_list->user_token >> PAGE_SHIFT);
453
			kfree(r_list);
454
			found = 1;
D
Dave Airlie 已提交
455 456
			break;
		}
L
Linus Torvalds 已提交
457 458
	}

459
	if (!found)
L
Linus Torvalds 已提交
460 461
		return -EINVAL;

D
Dave Airlie 已提交
462 463
	switch (map->type) {
	case _DRM_REGISTERS:
464
		iounmap(map->handle);
D
Dave Airlie 已提交
465 466
		/* FALLTHROUGH */
	case _DRM_FRAME_BUFFER:
467 468
		if (drm_core_has_MTRR(dev))
			arch_phys_wc_del(map->mtrr);
D
Dave Airlie 已提交
469 470 471
		break;
	case _DRM_SHM:
		vfree(map->handle);
472 473 474 475 476
		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;
477
			wake_up_interruptible_all(&master->lock.lock_queue);
478
		}
D
Dave Airlie 已提交
479 480 481 482 483 484 485 486 487 488
		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 已提交
489 490 491
	case _DRM_GEM:
		DRM_ERROR("tried to rmmap GEM object\n");
		break;
L
Linus Torvalds 已提交
492
	}
493
	kfree(map);
D
Dave Airlie 已提交
494

L
Linus Torvalds 已提交
495 496
	return 0;
}
497
EXPORT_SYMBOL(drm_rmmap_locked);
D
Dave Airlie 已提交
498

499
int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
D
Dave Airlie 已提交
500 501 502
{
	int ret;

D
Dave Airlie 已提交
503
	mutex_lock(&dev->struct_mutex);
D
Dave Airlie 已提交
504
	ret = drm_rmmap_locked(dev, map);
D
Dave Airlie 已提交
505
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
506 507 508

	return ret;
}
J
Jesse Barnes 已提交
509
EXPORT_SYMBOL(drm_rmmap);
510

D
Dave Airlie 已提交
511 512 513 514 515 516 517 518
/* 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.
519 520 521 522 523 524
 *
 * \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 已提交
525
 */
526 527
int drm_rmmap_ioctl(struct drm_device *dev, void *data,
		    struct drm_file *file_priv)
528
{
529
	struct drm_map *request = data;
530
	struct drm_local_map *map = NULL;
D
Dave Airlie 已提交
531
	struct drm_map_list *r_list;
D
Dave Airlie 已提交
532
	int ret;
533

D
Dave Airlie 已提交
534
	mutex_lock(&dev->struct_mutex);
535
	list_for_each_entry(r_list, &dev->maplist, head) {
D
Dave Airlie 已提交
536
		if (r_list->map &&
537
		    r_list->user_token == (unsigned long)request->handle &&
D
Dave Airlie 已提交
538 539 540 541 542 543 544 545 546
		    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.
	 */
547
	if (list_empty(&dev->maplist) || !map) {
D
Dave Airlie 已提交
548
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
549 550 551 552 553
		return -EINVAL;
	}

	/* Register and framebuffer maps are permanent */
	if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
D
Dave Airlie 已提交
554
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
555 556 557 558 559
		return 0;
	}

	ret = drm_rmmap_locked(dev, map);

D
Dave Airlie 已提交
560
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
561 562

	return ret;
563
}
L
Linus Torvalds 已提交
564 565 566 567

/**
 * Cleanup after an error on one of the addbufs() functions.
 *
D
Dave Airlie 已提交
568
 * \param dev DRM device.
L
Linus Torvalds 已提交
569 570 571 572
 * \param entry buffer entry where the error occurred.
 *
 * Frees any pages and buffers associated with the given entry.
 */
573 574
static void drm_cleanup_buf_error(struct drm_device * dev,
				  struct drm_buf_entry * entry)
L
Linus Torvalds 已提交
575 576 577 578 579 580
{
	int i;

	if (entry->seg_count) {
		for (i = 0; i < entry->seg_count; i++) {
			if (entry->seglist[i]) {
D
Dave Airlie 已提交
581
				drm_pci_free(dev, entry->seglist[i]);
L
Linus Torvalds 已提交
582 583
			}
		}
584
		kfree(entry->seglist);
L
Linus Torvalds 已提交
585 586 587 588

		entry->seg_count = 0;
	}

D
Dave Airlie 已提交
589 590
	if (entry->buf_count) {
		for (i = 0; i < entry->buf_count; i++) {
591
			kfree(entry->buflist[i].dev_private);
L
Linus Torvalds 已提交
592
		}
593
		kfree(entry->buflist);
L
Linus Torvalds 已提交
594 595 596 597 598 599 600

		entry->buf_count = 0;
	}
}

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

D
Dave Airlie 已提交
629 630
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
631

632
	count = request->count;
633
	order = order_base_2(request->size);
L
Linus Torvalds 已提交
634 635
	size = 1 << order;

D
Dave Airlie 已提交
636 637
	alignment = (request->flags & _DRM_PAGE_ALIGN)
	    ? PAGE_ALIGN(size) : size;
L
Linus Torvalds 已提交
638 639 640 641
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

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

D
Dave Airlie 已提交
644 645 646
	DRM_DEBUG("count:      %d\n", count);
	DRM_DEBUG("order:      %d\n", order);
	DRM_DEBUG("size:       %d\n", size);
647
	DRM_DEBUG("agp_offset: %lx\n", agp_offset);
D
Dave Airlie 已提交
648 649 650
	DRM_DEBUG("alignment:  %d\n", alignment);
	DRM_DEBUG("page_order: %d\n", page_order);
	DRM_DEBUG("total:      %d\n", total);
L
Linus Torvalds 已提交
651

D
Dave Airlie 已提交
652 653
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
L
Linus Torvalds 已提交
654

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

D
Dave Airlie 已提交
676
	mutex_lock(&dev->struct_mutex);
L
Linus Torvalds 已提交
677
	entry = &dma->bufs[order];
D
Dave Airlie 已提交
678
	if (entry->buf_count) {
D
Dave Airlie 已提交
679
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
680 681
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
L
Linus Torvalds 已提交
682 683 684
	}

	if (count < 0 || count > 4096) {
D
Dave Airlie 已提交
685
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
686
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
687 688 689
		return -EINVAL;
	}

690
	entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
D
Dave Airlie 已提交
691
	if (!entry->buflist) {
D
Dave Airlie 已提交
692
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
693
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
694 695 696 697 698 699 700 701
		return -ENOMEM;
	}

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

	offset = 0;

D
Dave Airlie 已提交
702 703 704 705 706 707
	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 已提交
708

D
Dave Airlie 已提交
709
		buf->offset = (dma->byte_count + offset);
L
Linus Torvalds 已提交
710 711
		buf->bus_address = agp_offset + offset;
		buf->address = (void *)(agp_offset + offset);
D
Dave Airlie 已提交
712
		buf->next = NULL;
L
Linus Torvalds 已提交
713 714
		buf->waiting = 0;
		buf->pending = 0;
715
		buf->file_priv = NULL;
L
Linus Torvalds 已提交
716 717

		buf->dev_priv_size = dev->driver->dev_priv_size;
718
		buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
D
Dave Airlie 已提交
719
		if (!buf->dev_private) {
L
Linus Torvalds 已提交
720 721
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
D
Dave Airlie 已提交
722
			drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
723
			mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
724
			atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
725 726 727
			return -ENOMEM;
		}

D
Dave Airlie 已提交
728
		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
L
Linus Torvalds 已提交
729 730 731 732 733 734

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

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

737 738 739
	temp_buflist = krealloc(dma->buflist,
				(dma->buf_count + entry->buf_count) *
				sizeof(*dma->buflist), GFP_KERNEL);
D
Dave Airlie 已提交
740
	if (!temp_buflist) {
L
Linus Torvalds 已提交
741
		/* Free the entry because it isn't valid */
D
Dave Airlie 已提交
742
		drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
743
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
744
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
745 746 747 748
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

D
Dave Airlie 已提交
749
	for (i = 0; i < entry->buf_count; i++) {
L
Linus Torvalds 已提交
750 751 752 753
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

	dma->buf_count += entry->buf_count;
754 755
	dma->seg_count += entry->seg_count;
	dma->page_count += byte_count >> PAGE_SHIFT;
L
Linus Torvalds 已提交
756 757
	dma->byte_count += byte_count;

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

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

763 764
	request->count = entry->buf_count;
	request->size = size;
L
Linus Torvalds 已提交
765 766 767

	dma->flags = _DRM_DMA_USE_AGP;

D
Dave Airlie 已提交
768
	atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
769 770
	return 0;
}
771
EXPORT_SYMBOL(drm_addbufs_agp);
D
Dave Airlie 已提交
772
#endif				/* __OS_HAS_AGP */
L
Linus Torvalds 已提交
773

774
int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
L
Linus Torvalds 已提交
775
{
776
	struct drm_device_dma *dma = dev->dma;
L
Linus Torvalds 已提交
777 778 779 780 781
	int count;
	int order;
	int size;
	int total;
	int page_order;
782
	struct drm_buf_entry *entry;
D
Dave Airlie 已提交
783
	drm_dma_handle_t *dmah;
D
Dave Airlie 已提交
784
	struct drm_buf *buf;
L
Linus Torvalds 已提交
785 786 787 788 789 790
	int alignment;
	unsigned long offset;
	int i;
	int byte_count;
	int page_count;
	unsigned long *temp_pagelist;
D
Dave Airlie 已提交
791
	struct drm_buf **temp_buflist;
L
Linus Torvalds 已提交
792

D
Dave Airlie 已提交
793 794
	if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
		return -EINVAL;
795

D
Dave Airlie 已提交
796 797
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
798

799 800 801
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

802
	count = request->count;
803
	order = order_base_2(request->size);
L
Linus Torvalds 已提交
804 805
	size = 1 << order;

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

D
Dave Airlie 已提交
809 810
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
L
Linus Torvalds 已提交
811

812
	alignment = (request->flags & _DRM_PAGE_ALIGN)
D
Dave Airlie 已提交
813
	    ? PAGE_ALIGN(size) : size;
L
Linus Torvalds 已提交
814 815 816
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

D
Dave Airlie 已提交
817 818 819
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
820 821
		return -EBUSY;
	}
D
Dave Airlie 已提交
822 823
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
824

D
Dave Airlie 已提交
825
	mutex_lock(&dev->struct_mutex);
L
Linus Torvalds 已提交
826
	entry = &dma->bufs[order];
D
Dave Airlie 已提交
827
	if (entry->buf_count) {
D
Dave Airlie 已提交
828
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
829
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
830 831 832 833
		return -ENOMEM;	/* May only call once for each order */
	}

	if (count < 0 || count > 4096) {
D
Dave Airlie 已提交
834
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
835
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
836 837 838
		return -EINVAL;
	}

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

846
	entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
D
Dave Airlie 已提交
847
	if (!entry->seglist) {
848
		kfree(entry->buflist);
D
Dave Airlie 已提交
849
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
850
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
851 852 853 854 855 856
		return -ENOMEM;
	}

	/* Keep the original pagelist until we know all the allocations
	 * have succeeded
	 */
857 858
	temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
			       sizeof(*dma->pagelist), GFP_KERNEL);
L
Linus Torvalds 已提交
859
	if (!temp_pagelist) {
860 861
		kfree(entry->buflist);
		kfree(entry->seglist);
D
Dave Airlie 已提交
862
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
863
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
864 865 866
		return -ENOMEM;
	}
	memcpy(temp_pagelist,
D
Dave Airlie 已提交
867 868 869
	       dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
	DRM_DEBUG("pagelist: %d entries\n",
		  dma->page_count + (count << page_order));
L
Linus Torvalds 已提交
870

D
Dave Airlie 已提交
871
	entry->buf_size = size;
L
Linus Torvalds 已提交
872 873 874 875
	entry->page_order = page_order;
	byte_count = 0;
	page_count = 0;

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

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

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

			buf->dev_priv_size = dev->driver->dev_priv_size;
915 916
			buf->dev_private = kzalloc(buf->dev_priv_size,
						GFP_KERNEL);
D
Dave Airlie 已提交
917
			if (!buf->dev_private) {
L
Linus Torvalds 已提交
918 919 920
				/* Set count correctly so we free the proper amount. */
				entry->buf_count = count;
				entry->seg_count = count;
D
Dave Airlie 已提交
921
				drm_cleanup_buf_error(dev, entry);
922
				kfree(temp_pagelist);
D
Dave Airlie 已提交
923
				mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
924
				atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
925 926 927
				return -ENOMEM;
			}

D
Dave Airlie 已提交
928 929
			DRM_DEBUG("buffer %d @ %p\n",
				  entry->buf_count, buf->address);
L
Linus Torvalds 已提交
930 931 932 933
		}
		byte_count += PAGE_SIZE << page_order;
	}

934 935 936
	temp_buflist = krealloc(dma->buflist,
				(dma->buf_count + entry->buf_count) *
				sizeof(*dma->buflist), GFP_KERNEL);
L
Linus Torvalds 已提交
937 938
	if (!temp_buflist) {
		/* Free the entry because it isn't valid */
D
Dave Airlie 已提交
939
		drm_cleanup_buf_error(dev, entry);
940
		kfree(temp_pagelist);
D
Dave Airlie 已提交
941
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
942
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
943 944 945 946
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

D
Dave Airlie 已提交
947
	for (i = 0; i < entry->buf_count; i++) {
L
Linus Torvalds 已提交
948 949 950
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

T
Thomas Weber 已提交
951
	/* No allocations failed, so now we can replace the original pagelist
L
Linus Torvalds 已提交
952 953 954
	 * with the new one.
	 */
	if (dma->page_count) {
955
		kfree(dma->pagelist);
L
Linus Torvalds 已提交
956 957 958 959 960 961 962 963
	}
	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 已提交
964
	mutex_unlock(&dev->struct_mutex);
L
Linus Torvalds 已提交
965

966 967
	request->count = entry->buf_count;
	request->size = size;
L
Linus Torvalds 已提交
968

969 970 971
	if (request->flags & _DRM_PCI_BUFFER_RO)
		dma->flags = _DRM_DMA_USE_PCI_RO;

D
Dave Airlie 已提交
972
	atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
973 974 975
	return 0;

}
976
EXPORT_SYMBOL(drm_addbufs_pci);
L
Linus Torvalds 已提交
977

978
static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
L
Linus Torvalds 已提交
979
{
980 981
	struct drm_device_dma *dma = dev->dma;
	struct drm_buf_entry *entry;
D
Dave Airlie 已提交
982
	struct drm_buf *buf;
L
Linus Torvalds 已提交
983 984 985 986 987 988 989 990 991 992
	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 已提交
993
	struct drm_buf **temp_buflist;
L
Linus Torvalds 已提交
994

D
Dave Airlie 已提交
995 996 997 998 999
	if (!drm_core_check_feature(dev, DRIVER_SG))
		return -EINVAL;

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

1001 1002 1003
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

1004
	count = request->count;
1005
	order = order_base_2(request->size);
L
Linus Torvalds 已提交
1006 1007
	size = 1 << order;

D
Dave Airlie 已提交
1008 1009
	alignment = (request->flags & _DRM_PAGE_ALIGN)
	    ? PAGE_ALIGN(size) : size;
L
Linus Torvalds 已提交
1010 1011 1012 1013
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

	byte_count = 0;
1014
	agp_offset = request->agp_start;
L
Linus Torvalds 已提交
1015

D
Dave Airlie 已提交
1016 1017 1018 1019 1020 1021 1022
	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 已提交
1023

D
Dave Airlie 已提交
1024 1025
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
L
Linus Torvalds 已提交
1026

D
Dave Airlie 已提交
1027 1028 1029
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1030 1031
		return -EBUSY;
	}
D
Dave Airlie 已提交
1032 1033
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1034

D
Dave Airlie 已提交
1035
	mutex_lock(&dev->struct_mutex);
L
Linus Torvalds 已提交
1036
	entry = &dma->bufs[order];
D
Dave Airlie 已提交
1037
	if (entry->buf_count) {
D
Dave Airlie 已提交
1038
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1039 1040
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
L
Linus Torvalds 已提交
1041 1042 1043
	}

	if (count < 0 || count > 4096) {
D
Dave Airlie 已提交
1044
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1045
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
1046 1047 1048
		return -EINVAL;
	}

1049
	entry->buflist = kzalloc(count * sizeof(*entry->buflist),
1050
				GFP_KERNEL);
D
Dave Airlie 已提交
1051
	if (!entry->buflist) {
D
Dave Airlie 已提交
1052
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1053
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
1054 1055 1056 1057 1058 1059 1060 1061
		return -ENOMEM;
	}

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

	offset = 0;

D
Dave Airlie 已提交
1062 1063 1064 1065 1066 1067
	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 已提交
1068

D
Dave Airlie 已提交
1069
		buf->offset = (dma->byte_count + offset);
L
Linus Torvalds 已提交
1070
		buf->bus_address = agp_offset + offset;
D
Dave Airlie 已提交
1071
		buf->address = (void *)(agp_offset + offset
1072
					+ (unsigned long)dev->sg->virtual);
D
Dave Airlie 已提交
1073
		buf->next = NULL;
L
Linus Torvalds 已提交
1074 1075
		buf->waiting = 0;
		buf->pending = 0;
1076
		buf->file_priv = NULL;
L
Linus Torvalds 已提交
1077 1078

		buf->dev_priv_size = dev->driver->dev_priv_size;
1079
		buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
D
Dave Airlie 已提交
1080
		if (!buf->dev_private) {
L
Linus Torvalds 已提交
1081 1082
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
D
Dave Airlie 已提交
1083
			drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
1084
			mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1085
			atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
1086 1087 1088
			return -ENOMEM;
		}

D
Dave Airlie 已提交
1089
		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
L
Linus Torvalds 已提交
1090 1091 1092 1093 1094 1095

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

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

1098 1099 1100
	temp_buflist = krealloc(dma->buflist,
				(dma->buf_count + entry->buf_count) *
				sizeof(*dma->buflist), GFP_KERNEL);
D
Dave Airlie 已提交
1101
	if (!temp_buflist) {
L
Linus Torvalds 已提交
1102
		/* Free the entry because it isn't valid */
D
Dave Airlie 已提交
1103
		drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
1104
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1105
		atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
1106 1107 1108 1109
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

D
Dave Airlie 已提交
1110
	for (i = 0; i < entry->buf_count; i++) {
L
Linus Torvalds 已提交
1111 1112 1113 1114
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

	dma->buf_count += entry->buf_count;
1115 1116
	dma->seg_count += entry->seg_count;
	dma->page_count += byte_count >> PAGE_SHIFT;
L
Linus Torvalds 已提交
1117 1118
	dma->byte_count += byte_count;

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

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

1124 1125
	request->count = entry->buf_count;
	request->size = size;
L
Linus Torvalds 已提交
1126 1127 1128

	dma->flags = _DRM_DMA_USE_SG;

D
Dave Airlie 已提交
1129
	atomic_dec(&dev->buf_alloc);
L
Linus Torvalds 已提交
1130 1131 1132
	return 0;
}

1133
static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
D
Dave Airlie 已提交
1134
{
1135 1136
	struct drm_device_dma *dma = dev->dma;
	struct drm_buf_entry *entry;
D
Dave Airlie 已提交
1137
	struct drm_buf *buf;
D
Dave Airlie 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
	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 已提交
1148
	struct drm_buf **temp_buflist;
D
Dave Airlie 已提交
1149 1150 1151

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

D
Dave Airlie 已提交
1153 1154 1155
	if (!dma)
		return -EINVAL;

1156 1157 1158
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

1159
	count = request->count;
1160
	order = order_base_2(request->size);
D
Dave Airlie 已提交
1161 1162
	size = 1 << order;

1163
	alignment = (request->flags & _DRM_PAGE_ALIGN)
D
Dave Airlie 已提交
1164 1165 1166 1167 1168
	    ? PAGE_ALIGN(size) : size;
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

	byte_count = 0;
1169
	agp_offset = request->agp_start;
D
Dave Airlie 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

	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 已提交
1190
	mutex_lock(&dev->struct_mutex);
D
Dave Airlie 已提交
1191 1192
	entry = &dma->bufs[order];
	if (entry->buf_count) {
D
Dave Airlie 已提交
1193
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1194 1195 1196 1197 1198
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
	}

	if (count < 0 || count > 4096) {
D
Dave Airlie 已提交
1199
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1200 1201 1202 1203
		atomic_dec(&dev->buf_alloc);
		return -EINVAL;
	}

1204
	entry->buflist = kzalloc(count * sizeof(*entry->buflist),
1205
				GFP_KERNEL);
D
Dave Airlie 已提交
1206
	if (!entry->buflist) {
D
Dave Airlie 已提交
1207
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
		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;
1230
		buf->file_priv = NULL;
D
Dave Airlie 已提交
1231 1232

		buf->dev_priv_size = dev->driver->dev_priv_size;
1233
		buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
D
Dave Airlie 已提交
1234 1235 1236 1237
		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 已提交
1238
			mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
			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);

1252 1253 1254
	temp_buflist = krealloc(dma->buflist,
				(dma->buf_count + entry->buf_count) *
				sizeof(*dma->buflist), GFP_KERNEL);
D
Dave Airlie 已提交
1255 1256 1257
	if (!temp_buflist) {
		/* Free the entry because it isn't valid */
		drm_cleanup_buf_error(dev, entry);
D
Dave Airlie 已提交
1258
		mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
		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;
1269 1270
	dma->seg_count += entry->seg_count;
	dma->page_count += byte_count >> PAGE_SHIFT;
D
Dave Airlie 已提交
1271 1272 1273 1274 1275
	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 已提交
1276
	mutex_unlock(&dev->struct_mutex);
D
Dave Airlie 已提交
1277

1278 1279
	request->count = entry->buf_count;
	request->size = size;
D
Dave Airlie 已提交
1280 1281 1282 1283 1284 1285

	dma->flags = _DRM_DMA_USE_FB;

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

D
Dave Airlie 已提交
1287

L
Linus Torvalds 已提交
1288 1289 1290 1291
/**
 * Add buffers for DMA transfers (ioctl).
 *
 * \param inode device inode.
1292
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1293
 * \param cmd command.
1294
 * \param arg pointer to a struct drm_buf_desc request.
L
Linus Torvalds 已提交
1295 1296 1297 1298 1299 1300 1301
 * \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.
 */
1302 1303
int drm_addbufs(struct drm_device *dev, void *data,
		struct drm_file *file_priv)
L
Linus Torvalds 已提交
1304
{
1305
	struct drm_buf_desc *request = data;
1306
	int ret;
D
Dave Airlie 已提交
1307

1308 1309 1310
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

L
Linus Torvalds 已提交
1311 1312 1313 1314
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

#if __OS_HAS_AGP
1315 1316
	if (request->flags & _DRM_AGP_BUFFER)
		ret = drm_addbufs_agp(dev, request);
L
Linus Torvalds 已提交
1317 1318
	else
#endif
1319 1320 1321 1322
	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 已提交
1323
	else
1324
		ret = drm_addbufs_pci(dev, request);
1325 1326

	return ret;
L
Linus Torvalds 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
}

/**
 * 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.
1337
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1338 1339 1340 1341 1342 1343 1344 1345
 * \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.
 */
1346 1347
int drm_infobufs(struct drm_device *dev, void *data,
		 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1348
{
1349
	struct drm_device_dma *dma = dev->dma;
1350
	struct drm_buf_info *request = data;
L
Linus Torvalds 已提交
1351 1352 1353
	int i;
	int count;

1354 1355 1356
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

L
Linus Torvalds 已提交
1357 1358 1359
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1360 1361
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1362

D
Dave Airlie 已提交
1363 1364 1365
	spin_lock(&dev->count_lock);
	if (atomic_read(&dev->buf_alloc)) {
		spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1366 1367 1368
		return -EBUSY;
	}
	++dev->buf_use;		/* Can't allocate more after this call */
D
Dave Airlie 已提交
1369
	spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1370

D
Dave Airlie 已提交
1371 1372 1373
	for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
		if (dma->bufs[i].buf_count)
			++count;
L
Linus Torvalds 已提交
1374 1375
	}

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

1378
	if (request->count >= count) {
D
Dave Airlie 已提交
1379 1380
		for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
			if (dma->bufs[i].buf_count) {
1381
				struct drm_buf_desc __user *to =
1382
				    &request->list[count];
1383 1384
				struct drm_buf_entry *from = &dma->bufs[i];
				struct drm_freelist *list = &dma->bufs[i].freelist;
D
Dave Airlie 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
				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 已提交
1397 1398
					return -EFAULT;

D
Dave Airlie 已提交
1399 1400 1401 1402 1403 1404
				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 已提交
1405 1406 1407 1408
				++count;
			}
		}
	}
1409
	request->count = count;
L
Linus Torvalds 已提交
1410 1411 1412 1413 1414 1415 1416 1417

	return 0;
}

/**
 * Specifies a low and high water mark for buffer allocation
 *
 * \param inode device inode.
1418
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1419 1420 1421 1422 1423 1424 1425 1426 1427
 * \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.
 */
1428 1429
int drm_markbufs(struct drm_device *dev, void *data,
		 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1430
{
1431
	struct drm_device_dma *dma = dev->dma;
1432
	struct drm_buf_desc *request = data;
L
Linus Torvalds 已提交
1433
	int order;
1434
	struct drm_buf_entry *entry;
L
Linus Torvalds 已提交
1435

1436 1437 1438
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

L
Linus Torvalds 已提交
1439 1440 1441
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1442 1443
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1444

D
Dave Airlie 已提交
1445
	DRM_DEBUG("%d, %d, %d\n",
1446
		  request->size, request->low_mark, request->high_mark);
1447
	order = order_base_2(request->size);
D
Dave Airlie 已提交
1448 1449
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
L
Linus Torvalds 已提交
1450 1451
	entry = &dma->bufs[order];

1452
	if (request->low_mark < 0 || request->low_mark > entry->buf_count)
L
Linus Torvalds 已提交
1453
		return -EINVAL;
1454
	if (request->high_mark < 0 || request->high_mark > entry->buf_count)
L
Linus Torvalds 已提交
1455 1456
		return -EINVAL;

1457 1458
	entry->freelist.low_mark = request->low_mark;
	entry->freelist.high_mark = request->high_mark;
L
Linus Torvalds 已提交
1459 1460 1461 1462 1463

	return 0;
}

/**
D
Dave Airlie 已提交
1464
 * Unreserve the buffers in list, previously reserved using drmDMA.
L
Linus Torvalds 已提交
1465 1466
 *
 * \param inode device inode.
1467
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1468 1469 1470
 * \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 已提交
1471
 *
L
Linus Torvalds 已提交
1472 1473 1474
 * Calls free_buffer() for each used buffer.
 * This function is primarily used for debugging.
 */
1475 1476
int drm_freebufs(struct drm_device *dev, void *data,
		 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1477
{
1478
	struct drm_device_dma *dma = dev->dma;
1479
	struct drm_buf_free *request = data;
L
Linus Torvalds 已提交
1480 1481
	int i;
	int idx;
D
Dave Airlie 已提交
1482
	struct drm_buf *buf;
L
Linus Torvalds 已提交
1483

1484 1485 1486
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

L
Linus Torvalds 已提交
1487 1488 1489
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1490 1491
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1492

1493 1494 1495
	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 已提交
1496
			return -EFAULT;
D
Dave Airlie 已提交
1497 1498 1499
		if (idx < 0 || idx >= dma->buf_count) {
			DRM_ERROR("Index %d (of %d max)\n",
				  idx, dma->buf_count - 1);
L
Linus Torvalds 已提交
1500 1501 1502
			return -EINVAL;
		}
		buf = dma->buflist[idx];
1503
		if (buf->file_priv != file_priv) {
D
Dave Airlie 已提交
1504
			DRM_ERROR("Process %d freeing buffer not owned\n",
1505
				  task_pid_nr(current));
L
Linus Torvalds 已提交
1506 1507
			return -EINVAL;
		}
D
Dave Airlie 已提交
1508
		drm_free_buffer(dev, buf);
L
Linus Torvalds 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517
	}

	return 0;
}

/**
 * Maps all of the DMA buffers into client-virtual space (ioctl).
 *
 * \param inode device inode.
1518
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1519 1520 1521 1522
 * \param cmd command.
 * \param arg pointer to a drm_buf_map structure.
 * \return zero on success or a negative number on failure.
 *
1523 1524
 * 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
1525 1526
 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
 * drm_mmap_dma().
L
Linus Torvalds 已提交
1527
 */
1528 1529
int drm_mapbufs(struct drm_device *dev, void *data,
	        struct drm_file *file_priv)
L
Linus Torvalds 已提交
1530
{
1531
	struct drm_device_dma *dma = dev->dma;
L
Linus Torvalds 已提交
1532 1533 1534 1535
	int retcode = 0;
	const int zero = 0;
	unsigned long virtual;
	unsigned long address;
1536
	struct drm_buf_map *request = data;
L
Linus Torvalds 已提交
1537 1538
	int i;

1539 1540 1541
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

L
Linus Torvalds 已提交
1542 1543 1544
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1545 1546
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1547

D
Dave Airlie 已提交
1548 1549 1550
	spin_lock(&dev->count_lock);
	if (atomic_read(&dev->buf_alloc)) {
		spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1551 1552 1553
		return -EBUSY;
	}
	dev->buf_use++;		/* Can't allocate more after this call */
D
Dave Airlie 已提交
1554
	spin_unlock(&dev->count_lock);
L
Linus Torvalds 已提交
1555

1556
	if (request->count >= dma->buf_count) {
D
Dave Airlie 已提交
1557
		if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
D
Dave Airlie 已提交
1558
		    || (drm_core_check_feature(dev, DRIVER_SG)
D
Dave Airlie 已提交
1559 1560 1561
			&& (dma->flags & _DRM_DMA_USE_SG))
		    || (drm_core_check_feature(dev, DRIVER_FB_DMA)
			&& (dma->flags & _DRM_DMA_USE_FB))) {
1562
			struct drm_local_map *map = dev->agp_buffer_map;
1563
			unsigned long token = dev->agp_buffer_token;
L
Linus Torvalds 已提交
1564

D
Dave Airlie 已提交
1565
			if (!map) {
L
Linus Torvalds 已提交
1566 1567 1568
				retcode = -EINVAL;
				goto done;
			}
1569
			virtual = vm_mmap(file_priv->filp, 0, map->size,
D
Dave Airlie 已提交
1570
					  PROT_READ | PROT_WRITE,
1571 1572
					  MAP_SHARED,
					  token);
L
Linus Torvalds 已提交
1573
		} else {
1574
			virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
D
Dave Airlie 已提交
1575 1576
					  PROT_READ | PROT_WRITE,
					  MAP_SHARED, 0);
L
Linus Torvalds 已提交
1577
		}
D
Dave Airlie 已提交
1578
		if (virtual > -1024UL) {
L
Linus Torvalds 已提交
1579 1580 1581 1582
			/* Real error */
			retcode = (signed long)virtual;
			goto done;
		}
1583
		request->virtual = (void __user *)virtual;
L
Linus Torvalds 已提交
1584

D
Dave Airlie 已提交
1585
		for (i = 0; i < dma->buf_count; i++) {
1586
			if (copy_to_user(&request->list[i].idx,
D
Dave Airlie 已提交
1587
					 &dma->buflist[i]->idx,
1588
					 sizeof(request->list[0].idx))) {
L
Linus Torvalds 已提交
1589 1590 1591
				retcode = -EFAULT;
				goto done;
			}
1592
			if (copy_to_user(&request->list[i].total,
D
Dave Airlie 已提交
1593
					 &dma->buflist[i]->total,
1594
					 sizeof(request->list[0].total))) {
L
Linus Torvalds 已提交
1595 1596 1597
				retcode = -EFAULT;
				goto done;
			}
1598
			if (copy_to_user(&request->list[i].used,
D
Dave Airlie 已提交
1599
					 &zero, sizeof(zero))) {
L
Linus Torvalds 已提交
1600 1601 1602
				retcode = -EFAULT;
				goto done;
			}
D
Dave Airlie 已提交
1603
			address = virtual + dma->buflist[i]->offset;	/* *** */
1604
			if (copy_to_user(&request->list[i].address,
D
Dave Airlie 已提交
1605
					 &address, sizeof(address))) {
L
Linus Torvalds 已提交
1606 1607 1608 1609 1610
				retcode = -EFAULT;
				goto done;
			}
		}
	}
D
Dave Airlie 已提交
1611
      done:
1612 1613
	request->count = dma->buf_count;
	DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
L
Linus Torvalds 已提交
1614 1615 1616 1617

	return retcode;
}

1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
struct drm_local_map *drm_getsarea(struct drm_device *dev)
{
	struct drm_map_list *entry;

	list_for_each_entry(entry, &dev->maplist, head) {
		if (entry->map && entry->map->type == _DRM_SHM &&
		    (entry->map->flags & _DRM_CONTAINS_LOCK)) {
			return entry->map;
		}
	}
	return NULL;
}
EXPORT_SYMBOL(drm_getsarea);