drm_bufs.c 38.6 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;
		}

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

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

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

D
Daniel Vetter 已提交
264
		if (!dev->agp) {
265
			kfree(map);
266 267
			return -EINVAL;
		}
L
Linus Torvalds 已提交
268
#ifdef __alpha__
269
		map->offset += dev->hose->mem_space->start;
L
Linus Torvalds 已提交
270
#endif
271 272 273 274 275
		/* 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.
276
		 */
277 278 279 280 281
		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;
		}
282 283 284 285 286 287 288 289
		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
		 */
290
		list_for_each_entry(entry, &dev->agp->memory, head) {
291 292 293 294 295
			if ((map->offset >= entry->bound) &&
			    (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
				valid = 1;
				break;
			}
L
Linus Torvalds 已提交
296
		}
297
		if (!list_empty(&dev->agp->memory) && !valid) {
298
			kfree(map);
299 300
			return -EPERM;
		}
301 302
		DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
			  (unsigned long long)map->offset, map->size);
303

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

332
	list = kzalloc(sizeof(*list), GFP_KERNEL);
D
Dave Airlie 已提交
333
	if (!list) {
334
		if (map->type == _DRM_REGISTERS)
335
			iounmap(map->handle);
336
		kfree(map);
L
Linus Torvalds 已提交
337 338 339 340
		return -EINVAL;
	}
	list->map = map;

D
Dave Airlie 已提交
341
	mutex_lock(&dev->struct_mutex);
342
	list_add(&list->head, &dev->maplist);
343

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

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

362 363
	if (!(map->flags & _DRM_DRIVER))
		list->master = dev->primary->master;
364
	*maplist = list;
365
	return 0;
366
}
367

368
int drm_addmap(struct drm_device * dev, resource_size_t offset,
369
	       unsigned int size, enum drm_map_type type,
370
	       enum drm_map_flags flags, struct drm_local_map ** map_ptr)
371
{
D
Dave Airlie 已提交
372
	struct drm_map_list *list;
373 374 375 376 377 378 379
	int rc;

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

381 382
EXPORT_SYMBOL(drm_addmap);

383 384 385 386 387 388 389 390 391 392 393
/**
 * 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.
 *
 */
394 395
int drm_addmap_ioctl(struct drm_device *dev, void *data,
		     struct drm_file *file_priv)
396
{
397
	struct drm_map *map = data;
D
Dave Airlie 已提交
398
	struct drm_map_list *maplist;
399 400
	int err;

401
	if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
402 403
		return -EPERM;

404 405
	err = drm_addmap_core(dev, map->offset, map->size, map->type,
			      map->flags, &maplist);
406

D
Dave Airlie 已提交
407
	if (err)
408
		return err;
409

410
	/* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
411
	map->handle = (void *)(unsigned long)maplist->user_token;
412 413 414 415 416 417 418 419 420

	/*
	 * 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 已提交
421
	return 0;
D
Dave Airlie 已提交
422
}
L
Linus Torvalds 已提交
423 424 425 426 427 428 429 430 431

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

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

454
	if (!found)
L
Linus Torvalds 已提交
455 456
		return -EINVAL;

D
Dave Airlie 已提交
457 458
	switch (map->type) {
	case _DRM_REGISTERS:
459
		iounmap(map->handle);
D
Dave Airlie 已提交
460 461
		/* FALLTHROUGH */
	case _DRM_FRAME_BUFFER:
462
		arch_phys_wc_del(map->mtrr);
D
Dave Airlie 已提交
463 464 465
		break;
	case _DRM_SHM:
		vfree(map->handle);
466 467 468 469 470
		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;
471
			wake_up_interruptible_all(&master->lock.lock_queue);
472
		}
D
Dave Airlie 已提交
473 474 475 476 477 478 479 480 481 482
		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;
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
	count = request->count;
624
	order = order_base_2(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;
	}
659
	spin_lock(&dev->buf_lock);
D
Dave Airlie 已提交
660
	if (dev->buf_use) {
661
		spin_unlock(&dev->buf_lock);
L
Linus Torvalds 已提交
662 663
		return -EBUSY;
	}
D
Dave Airlie 已提交
664
	atomic_inc(&dev->buf_alloc);
665
	spin_unlock(&dev->buf_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
	count = request->count;
794
	order = order_base_2(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;

808
	spin_lock(&dev->buf_lock);
D
Dave Airlie 已提交
809
	if (dev->buf_use) {
810
		spin_unlock(&dev->buf_lock);
L
Linus Torvalds 已提交
811 812
		return -EBUSY;
	}
D
Dave Airlie 已提交
813
	atomic_inc(&dev->buf_alloc);
814
	spin_unlock(&dev->buf_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
	count = request->count;
996
	order = order_base_2(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

1018
	spin_lock(&dev->buf_lock);
D
Dave Airlie 已提交
1019
	if (dev->buf_use) {
1020
		spin_unlock(&dev->buf_lock);
L
Linus Torvalds 已提交
1021 1022
		return -EBUSY;
	}
D
Dave Airlie 已提交
1023
	atomic_inc(&dev->buf_alloc);
1024
	spin_unlock(&dev->buf_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 1124 1125 1126 1127
	return 0;
}

/**
 * Add buffers for DMA transfers (ioctl).
 *
 * \param inode device inode.
1128
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1129
 * \param cmd command.
1130
 * \param arg pointer to a struct drm_buf_desc request.
L
Linus Torvalds 已提交
1131 1132 1133 1134 1135 1136 1137
 * \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.
 */
1138 1139
int drm_addbufs(struct drm_device *dev, void *data,
		struct drm_file *file_priv)
L
Linus Torvalds 已提交
1140
{
1141
	struct drm_buf_desc *request = data;
1142
	int ret;
D
Dave Airlie 已提交
1143

1144 1145 1146
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

L
Linus Torvalds 已提交
1147 1148 1149 1150
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

#if __OS_HAS_AGP
1151 1152
	if (request->flags & _DRM_AGP_BUFFER)
		ret = drm_addbufs_agp(dev, request);
L
Linus Torvalds 已提交
1153 1154
	else
#endif
1155 1156 1157
	if (request->flags & _DRM_SG_BUFFER)
		ret = drm_addbufs_sg(dev, request);
	else if (request->flags & _DRM_FB_BUFFER)
1158
		ret = -EINVAL;
L
Linus Torvalds 已提交
1159
	else
1160
		ret = drm_addbufs_pci(dev, request);
1161 1162

	return ret;
L
Linus Torvalds 已提交
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
}

/**
 * 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.
1173
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1174 1175 1176 1177
 * \param cmd command.
 * \param arg pointer to a drm_buf_info structure.
 * \return zero on success or a negative number on failure.
 *
1178
 * Increments drm_device::buf_use while holding the drm_device::buf_lock
L
Linus Torvalds 已提交
1179 1180 1181
 * lock, preventing of allocating more buffers after this call. Information
 * about each requested buffer is then copied into user space.
 */
1182 1183
int drm_infobufs(struct drm_device *dev, void *data,
		 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1184
{
1185
	struct drm_device_dma *dma = dev->dma;
1186
	struct drm_buf_info *request = data;
L
Linus Torvalds 已提交
1187 1188 1189
	int i;
	int count;

1190 1191 1192
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

L
Linus Torvalds 已提交
1193 1194 1195
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1196 1197
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1198

1199
	spin_lock(&dev->buf_lock);
D
Dave Airlie 已提交
1200
	if (atomic_read(&dev->buf_alloc)) {
1201
		spin_unlock(&dev->buf_lock);
L
Linus Torvalds 已提交
1202 1203 1204
		return -EBUSY;
	}
	++dev->buf_use;		/* Can't allocate more after this call */
1205
	spin_unlock(&dev->buf_lock);
L
Linus Torvalds 已提交
1206

D
Dave Airlie 已提交
1207 1208 1209
	for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
		if (dma->bufs[i].buf_count)
			++count;
L
Linus Torvalds 已提交
1210 1211
	}

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

1214
	if (request->count >= count) {
D
Dave Airlie 已提交
1215 1216
		for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
			if (dma->bufs[i].buf_count) {
1217
				struct drm_buf_desc __user *to =
1218
				    &request->list[count];
1219
				struct drm_buf_entry *from = &dma->bufs[i];
D
Dave Airlie 已提交
1220 1221 1222 1223 1224 1225 1226
				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,
1227 1228
						 &from->low_mark,
						 sizeof(from->low_mark)) ||
D
Dave Airlie 已提交
1229
				    copy_to_user(&to->high_mark,
1230 1231
						 &from->high_mark,
						 sizeof(from->high_mark)))
L
Linus Torvalds 已提交
1232 1233
					return -EFAULT;

D
Dave Airlie 已提交
1234 1235 1236 1237
				DRM_DEBUG("%d %d %d %d %d\n",
					  i,
					  dma->bufs[i].buf_count,
					  dma->bufs[i].buf_size,
1238 1239
					  dma->bufs[i].low_mark,
					  dma->bufs[i].high_mark);
L
Linus Torvalds 已提交
1240 1241 1242 1243
				++count;
			}
		}
	}
1244
	request->count = count;
L
Linus Torvalds 已提交
1245 1246 1247 1248 1249 1250 1251 1252

	return 0;
}

/**
 * Specifies a low and high water mark for buffer allocation
 *
 * \param inode device inode.
1253
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262
 * \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.
 */
1263 1264
int drm_markbufs(struct drm_device *dev, void *data,
		 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1265
{
1266
	struct drm_device_dma *dma = dev->dma;
1267
	struct drm_buf_desc *request = data;
L
Linus Torvalds 已提交
1268
	int order;
1269
	struct drm_buf_entry *entry;
L
Linus Torvalds 已提交
1270

1271 1272 1273
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

L
Linus Torvalds 已提交
1274 1275 1276
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1277 1278
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1279

D
Dave Airlie 已提交
1280
	DRM_DEBUG("%d, %d, %d\n",
1281
		  request->size, request->low_mark, request->high_mark);
1282
	order = order_base_2(request->size);
D
Dave Airlie 已提交
1283 1284
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
L
Linus Torvalds 已提交
1285 1286
	entry = &dma->bufs[order];

1287
	if (request->low_mark < 0 || request->low_mark > entry->buf_count)
L
Linus Torvalds 已提交
1288
		return -EINVAL;
1289
	if (request->high_mark < 0 || request->high_mark > entry->buf_count)
L
Linus Torvalds 已提交
1290 1291
		return -EINVAL;

1292 1293
	entry->low_mark = request->low_mark;
	entry->high_mark = request->high_mark;
L
Linus Torvalds 已提交
1294 1295 1296 1297 1298

	return 0;
}

/**
D
Dave Airlie 已提交
1299
 * Unreserve the buffers in list, previously reserved using drmDMA.
L
Linus Torvalds 已提交
1300 1301
 *
 * \param inode device inode.
1302
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1303 1304 1305
 * \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 已提交
1306
 *
L
Linus Torvalds 已提交
1307 1308 1309
 * Calls free_buffer() for each used buffer.
 * This function is primarily used for debugging.
 */
1310 1311
int drm_freebufs(struct drm_device *dev, void *data,
		 struct drm_file *file_priv)
L
Linus Torvalds 已提交
1312
{
1313
	struct drm_device_dma *dma = dev->dma;
1314
	struct drm_buf_free *request = data;
L
Linus Torvalds 已提交
1315 1316
	int i;
	int idx;
D
Dave Airlie 已提交
1317
	struct drm_buf *buf;
L
Linus Torvalds 已提交
1318

1319 1320 1321
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

L
Linus Torvalds 已提交
1322 1323 1324
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1325 1326
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1327

1328 1329 1330
	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 已提交
1331
			return -EFAULT;
D
Dave Airlie 已提交
1332 1333 1334
		if (idx < 0 || idx >= dma->buf_count) {
			DRM_ERROR("Index %d (of %d max)\n",
				  idx, dma->buf_count - 1);
L
Linus Torvalds 已提交
1335 1336 1337
			return -EINVAL;
		}
		buf = dma->buflist[idx];
1338
		if (buf->file_priv != file_priv) {
D
Dave Airlie 已提交
1339
			DRM_ERROR("Process %d freeing buffer not owned\n",
1340
				  task_pid_nr(current));
L
Linus Torvalds 已提交
1341 1342
			return -EINVAL;
		}
D
Dave Airlie 已提交
1343
		drm_free_buffer(dev, buf);
L
Linus Torvalds 已提交
1344 1345 1346 1347 1348 1349 1350 1351 1352
	}

	return 0;
}

/**
 * Maps all of the DMA buffers into client-virtual space (ioctl).
 *
 * \param inode device inode.
1353
 * \param file_priv DRM file private.
L
Linus Torvalds 已提交
1354 1355 1356 1357
 * \param cmd command.
 * \param arg pointer to a drm_buf_map structure.
 * \return zero on success or a negative number on failure.
 *
1358 1359
 * 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
1360 1361
 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
 * drm_mmap_dma().
L
Linus Torvalds 已提交
1362
 */
1363 1364
int drm_mapbufs(struct drm_device *dev, void *data,
	        struct drm_file *file_priv)
L
Linus Torvalds 已提交
1365
{
1366
	struct drm_device_dma *dma = dev->dma;
L
Linus Torvalds 已提交
1367 1368 1369 1370
	int retcode = 0;
	const int zero = 0;
	unsigned long virtual;
	unsigned long address;
1371
	struct drm_buf_map *request = data;
L
Linus Torvalds 已提交
1372 1373
	int i;

1374 1375 1376
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

L
Linus Torvalds 已提交
1377 1378 1379
	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
		return -EINVAL;

D
Dave Airlie 已提交
1380 1381
	if (!dma)
		return -EINVAL;
L
Linus Torvalds 已提交
1382

1383
	spin_lock(&dev->buf_lock);
D
Dave Airlie 已提交
1384
	if (atomic_read(&dev->buf_alloc)) {
1385
		spin_unlock(&dev->buf_lock);
L
Linus Torvalds 已提交
1386 1387 1388
		return -EBUSY;
	}
	dev->buf_use++;		/* Can't allocate more after this call */
1389
	spin_unlock(&dev->buf_lock);
L
Linus Torvalds 已提交
1390

1391
	if (request->count >= dma->buf_count) {
D
Daniel Vetter 已提交
1392
		if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
D
Dave Airlie 已提交
1393
		    || (drm_core_check_feature(dev, DRIVER_SG)
1394
			&& (dma->flags & _DRM_DMA_USE_SG))) {
1395
			struct drm_local_map *map = dev->agp_buffer_map;
1396
			unsigned long token = dev->agp_buffer_token;
L
Linus Torvalds 已提交
1397

D
Dave Airlie 已提交
1398
			if (!map) {
L
Linus Torvalds 已提交
1399 1400 1401
				retcode = -EINVAL;
				goto done;
			}
1402
			virtual = vm_mmap(file_priv->filp, 0, map->size,
D
Dave Airlie 已提交
1403
					  PROT_READ | PROT_WRITE,
1404 1405
					  MAP_SHARED,
					  token);
L
Linus Torvalds 已提交
1406
		} else {
1407
			virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
D
Dave Airlie 已提交
1408 1409
					  PROT_READ | PROT_WRITE,
					  MAP_SHARED, 0);
L
Linus Torvalds 已提交
1410
		}
D
Dave Airlie 已提交
1411
		if (virtual > -1024UL) {
L
Linus Torvalds 已提交
1412 1413 1414 1415
			/* Real error */
			retcode = (signed long)virtual;
			goto done;
		}
1416
		request->virtual = (void __user *)virtual;
L
Linus Torvalds 已提交
1417

D
Dave Airlie 已提交
1418
		for (i = 0; i < dma->buf_count; i++) {
1419
			if (copy_to_user(&request->list[i].idx,
D
Dave Airlie 已提交
1420
					 &dma->buflist[i]->idx,
1421
					 sizeof(request->list[0].idx))) {
L
Linus Torvalds 已提交
1422 1423 1424
				retcode = -EFAULT;
				goto done;
			}
1425
			if (copy_to_user(&request->list[i].total,
D
Dave Airlie 已提交
1426
					 &dma->buflist[i]->total,
1427
					 sizeof(request->list[0].total))) {
L
Linus Torvalds 已提交
1428 1429 1430
				retcode = -EFAULT;
				goto done;
			}
1431
			if (copy_to_user(&request->list[i].used,
D
Dave Airlie 已提交
1432
					 &zero, sizeof(zero))) {
L
Linus Torvalds 已提交
1433 1434 1435
				retcode = -EFAULT;
				goto done;
			}
D
Dave Airlie 已提交
1436
			address = virtual + dma->buflist[i]->offset;	/* *** */
1437
			if (copy_to_user(&request->list[i].address,
D
Dave Airlie 已提交
1438
					 &address, sizeof(address))) {
L
Linus Torvalds 已提交
1439 1440 1441 1442 1443
				retcode = -EFAULT;
				goto done;
			}
		}
	}
D
Dave Airlie 已提交
1444
      done:
1445 1446
	request->count = dma->buf_count;
	DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
L
Linus Torvalds 已提交
1447 1448 1449 1450

	return retcode;
}

1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
int drm_dma_ioctl(struct drm_device *dev, void *data,
		  struct drm_file *file_priv)
{
	if (drm_core_check_feature(dev, DRIVER_MODESET))
		return -EINVAL;

	if (dev->driver->dma_ioctl)
		return dev->driver->dma_ioctl(dev, data, file_priv);
	else
		return -EINVAL;
}

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
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);