drm_bufs.c 44.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/**
Dave Airlie's avatar
Dave Airlie committed
2
 * \file drm_bufs.c
Linus Torvalds's avatar
Linus Torvalds committed
3
 * Generic buffer template
Dave Airlie's avatar
Dave Airlie committed
4
 *
Linus Torvalds's avatar
Linus Torvalds committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 * \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
38
#include <linux/log2.h>
#include <asm/shmparam.h>
Linus Torvalds's avatar
Linus Torvalds committed
39
40
#include "drmP.h"

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

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

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

Dave Airlie's avatar
Dave Airlie committed
54
static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
55
						  struct drm_local_map *map)
Dave Airlie's avatar
Dave Airlie committed
56
{
Dave Airlie's avatar
Dave Airlie committed
57
	struct drm_map_list *entry;
58
	list_for_each_entry(entry, &dev->maplist, head) {
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
		/*
		 * Because the kernel-userspace ABI is fixed at a 32-bit offset
		 * while PCI resources may live above that, we ignore the map
		 * offset for maps of type _DRM_FRAMEBUFFER or _DRM_REGISTERS.
		 * It is assumed that each driver will have only one resource of
		 * each type.
		 */
		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;
		case _DRM_REGISTERS:
		case _DRM_FRAME_BUFFER:
76
			return entry;
77
78
		default: /* Make gcc happy */
			;
Dave Airlie's avatar
Dave Airlie committed
79
		}
80
81
		if (entry->map->offset == map->offset)
			return entry;
Dave Airlie's avatar
Dave Airlie committed
82
83
84
	}

	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
85
86
}

87
static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
88
			  unsigned long user_token, int hashed_handle, int shm)
89
{
90
91
92
	int use_hashed_handle, shift;
	unsigned long add;

Dave Airlie's avatar
Dave Airlie committed
93
#if (BITS_PER_LONG == 64)
94
95
96
97
98
99
	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
100

101
102
	if (!use_hashed_handle) {
		int ret;
103
		hash->key = user_token >> PAGE_SHIFT;
104
105
106
		ret = drm_ht_insert_item(&dev->map_hash, hash);
		if (ret != -EINVAL)
			return ret;
107
	}
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

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

130
131
	return drm_ht_just_insert_please(&dev->map_hash, hash,
					 user_token, 32 - PAGE_SHIFT - 3,
132
					 shift, add);
133
}
134

Linus Torvalds's avatar
Linus Torvalds committed
135
/**
136
137
 * Core function to create a range of memory available for mapping by a
 * non-root process.
Linus Torvalds's avatar
Linus Torvalds committed
138
139
140
141
142
 *
 * 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.
 */
143
static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
144
			   unsigned int size, enum drm_map_type type,
Dave Airlie's avatar
Dave Airlie committed
145
146
			   enum drm_map_flags flags,
			   struct drm_map_list ** maplist)
Linus Torvalds's avatar
Linus Torvalds committed
147
{
148
	struct drm_local_map *map;
Dave Airlie's avatar
Dave Airlie committed
149
	struct drm_map_list *list;
150
	drm_dma_handle_t *dmah;
151
152
	unsigned long user_token;
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
153

Dave Airlie's avatar
Dave Airlie committed
154
155
	map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
	if (!map)
Linus Torvalds's avatar
Linus Torvalds committed
156
157
		return -ENOMEM;

158
159
160
161
	map->offset = offset;
	map->size = size;
	map->flags = flags;
	map->type = type;
Linus Torvalds's avatar
Linus Torvalds committed
162
163
164
165
166

	/* Only allow shared memory to be removable since we only keep enough
	 * book keeping information about shared memory to allow for removal
	 * when processes fork.
	 */
Dave Airlie's avatar
Dave Airlie committed
167
168
	if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
169
170
		return -EINVAL;
	}
171
172
	DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
		  (unsigned long long)map->offset, map->size, map->type);
173
174
175
176
177
178
179
180

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

181
	if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Dave Airlie's avatar
Dave Airlie committed
182
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
183
184
		return -EINVAL;
	}
Dave Airlie's avatar
Dave Airlie committed
185
	map->mtrr = -1;
Linus Torvalds's avatar
Linus Torvalds committed
186
187
	map->handle = NULL;

Dave Airlie's avatar
Dave Airlie committed
188
	switch (map->type) {
Linus Torvalds's avatar
Linus Torvalds committed
189
190
	case _DRM_REGISTERS:
	case _DRM_FRAME_BUFFER:
Dave Airlie's avatar
Dave Airlie committed
191
#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
192
		if (map->offset + (map->size-1) < map->offset ||
Dave Airlie's avatar
Dave Airlie committed
193
194
		    map->offset < virt_to_phys(high_memory)) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
195
196
197
198
199
200
			return -EINVAL;
		}
#endif
#ifdef __alpha__
		map->offset += dev->hose->mem_space->start;
#endif
Dave Airlie's avatar
Dave Airlie committed
201
202
203
204
		/* 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.
		 */
205
206
207
		list = drm_find_matching_map(dev, map);
		if (list != NULL) {
			if (list->map->size != map->size) {
Dave Airlie's avatar
Dave Airlie committed
208
				DRM_DEBUG("Matching maps of type %d with "
Dave Airlie's avatar
Dave Airlie committed
209
210
211
					  "mismatched sizes, (%ld vs %ld)\n",
					  map->type, map->size,
					  list->map->size);
212
				list->map->size = map->size;
Dave Airlie's avatar
Dave Airlie committed
213
214
215
			}

			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
216
			*maplist = list;
Dave Airlie's avatar
Dave Airlie committed
217
218
219
			return 0;
		}

Linus Torvalds's avatar
Linus Torvalds committed
220
		if (drm_core_has_MTRR(dev)) {
Dave Airlie's avatar
Dave Airlie committed
221
222
223
224
			if (map->type == _DRM_FRAME_BUFFER ||
			    (map->flags & _DRM_WRITE_COMBINING)) {
				map->mtrr = mtrr_add(map->offset, map->size,
						     MTRR_TYPE_WRCOMB, 1);
Linus Torvalds's avatar
Linus Torvalds committed
225
226
			}
		}
227
		if (map->type == _DRM_REGISTERS) {
228
			map->handle = ioremap(map->offset, map->size);
229
230
231
232
233
			if (!map->handle) {
				drm_free(map, sizeof(*map), DRM_MEM_MAPS);
				return -ENOMEM;
			}
		}
234

Linus Torvalds's avatar
Linus Torvalds committed
235
236
		break;
	case _DRM_SHM:
237
238
239
240
241
242
243
244
245
246
247
248
249
		list = drm_find_matching_map(dev, map);
		if (list != NULL) {
			if(list->map->size != map->size) {
				DRM_DEBUG("Matching maps of type %d with "
					  "mismatched sizes, (%ld vs %ld)\n",
					  map->type, map->size, list->map->size);
				list->map->size = map->size;
			}

			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			*maplist = list;
			return 0;
		}
250
		map->handle = vmalloc_user(map->size);
Dave Airlie's avatar
Dave Airlie committed
251
252
253
254
		DRM_DEBUG("%lu %d %p\n",
			  map->size, drm_order(map->size), map->handle);
		if (!map->handle) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
255
256
257
			return -ENOMEM;
		}
		map->offset = (unsigned long)map->handle;
Dave Airlie's avatar
Dave Airlie committed
258
		if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds's avatar
Linus Torvalds committed
259
			/* Prevent a 2nd X Server from creating a 2nd lock */
260
			if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlie's avatar
Dave Airlie committed
261
262
				vfree(map->handle);
				drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
263
264
				return -EBUSY;
			}
265
			dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle;	/* Pointer to lock */
Linus Torvalds's avatar
Linus Torvalds committed
266
267
		}
		break;
268
	case _DRM_AGP: {
Dave Airlie's avatar
Dave Airlie committed
269
		struct drm_agp_mem *entry;
270
271
272
273
274
275
		int valid = 0;

		if (!drm_core_has_AGP(dev)) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EINVAL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
276
#ifdef __alpha__
277
		map->offset += dev->hose->mem_space->start;
Linus Torvalds's avatar
Linus Torvalds committed
278
#endif
279
280
281
282
283
		/* 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.
284
		 */
285
286
287
288
289
		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;
		}
290
291
292
293
294
295
296
297
		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
		 */
298
		list_for_each_entry(entry, &dev->agp->memory, head) {
299
300
301
302
303
			if ((map->offset >= entry->bound) &&
			    (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
				valid = 1;
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
304
		}
305
		if (!list_empty(&dev->agp->memory) && !valid) {
306
307
308
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EPERM;
		}
309
310
		DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
			  (unsigned long long)map->offset, map->size);
311

Jesse Barnes's avatar
Jesse Barnes committed
312
313
314
		break;
	case _DRM_GEM:
		DRM_ERROR("tried to rmmap GEM object\n");
Linus Torvalds's avatar
Linus Torvalds committed
315
		break;
316
	}
Linus Torvalds's avatar
Linus Torvalds committed
317
318
319
320
321
	case _DRM_SCATTER_GATHER:
		if (!dev->sg) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EINVAL;
		}
322
		map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds's avatar
Linus Torvalds committed
323
		break;
Dave Airlie's avatar
Dave Airlie committed
324
	case _DRM_CONSISTENT:
325
		/* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
326
		 * As we're limiting the address to 2^32-1 (or less),
327
328
		 * casting it down to 32 bits is no problem, but we
		 * need to point to a 64bit variable first. */
329
330
		dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
		if (!dmah) {
331
332
333
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -ENOMEM;
		}
334
335
336
		map->handle = dmah->vaddr;
		map->offset = (unsigned long)dmah->busaddr;
		kfree(dmah);
337
		break;
Linus Torvalds's avatar
Linus Torvalds committed
338
	default:
Dave Airlie's avatar
Dave Airlie committed
339
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
340
341
342
343
		return -EINVAL;
	}

	list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlie's avatar
Dave Airlie committed
344
	if (!list) {
345
		if (map->type == _DRM_REGISTERS)
346
			iounmap(map->handle);
Linus Torvalds's avatar
Linus Torvalds committed
347
348
349
350
351
352
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		return -EINVAL;
	}
	memset(list, 0, sizeof(*list));
	list->map = map;

Dave Airlie's avatar
Dave Airlie committed
353
	mutex_lock(&dev->struct_mutex);
354
	list_add(&list->head, &dev->maplist);
355

356
	/* Assign a 32-bit handle */
Dave Airlie's avatar
Dave Airlie committed
357
	/* We do it here so that dev->struct_mutex protects the increment */
358
359
	user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
		map->offset;
360
361
	ret = drm_map_handle(dev, &list->hash, user_token, 0,
			     (map->type == _DRM_SHM));
362
	if (ret) {
363
		if (map->type == _DRM_REGISTERS)
364
			iounmap(map->handle);
365
366
367
368
369
370
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		drm_free(list, sizeof(*list), DRM_MEM_MAPS);
		mutex_unlock(&dev->struct_mutex);
		return ret;
	}

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

374
375
	if (!(map->flags & _DRM_DRIVER))
		list->master = dev->primary->master;
376
	*maplist = list;
377
	return 0;
378
	}
379

380
int drm_addmap(struct drm_device * dev, resource_size_t offset,
381
	       unsigned int size, enum drm_map_type type,
382
	       enum drm_map_flags flags, struct drm_local_map ** map_ptr)
383
{
Dave Airlie's avatar
Dave Airlie committed
384
	struct drm_map_list *list;
385
386
387
388
389
390
391
	int rc;

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

393
394
EXPORT_SYMBOL(drm_addmap);

395
396
397
398
399
400
401
402
403
404
405
/**
 * 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.
 *
 */
406
407
int drm_addmap_ioctl(struct drm_device *dev, void *data,
		     struct drm_file *file_priv)
408
{
409
	struct drm_map *map = data;
Dave Airlie's avatar
Dave Airlie committed
410
	struct drm_map_list *maplist;
411
412
	int err;

413
	if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
414
415
		return -EPERM;

416
417
	err = drm_addmap_core(dev, map->offset, map->size, map->type,
			      map->flags, &maplist);
418

Dave Airlie's avatar
Dave Airlie committed
419
	if (err)
420
		return err;
421

422
	/* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
423
	map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds's avatar
Linus Torvalds committed
424
	return 0;
Dave Airlie's avatar
Dave Airlie committed
425
}
Linus Torvalds's avatar
Linus Torvalds committed
426
427
428
429
430
431
432
433
434

/**
 * 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.
 *
435
 * \sa drm_addmap
Linus Torvalds's avatar
Linus Torvalds committed
436
 */
437
int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds's avatar
Linus Torvalds committed
438
{
Dave Airlie's avatar
Dave Airlie committed
439
	struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie's avatar
Dave Airlie committed
440
	drm_dma_handle_t dmah;
441
	int found = 0;
442
	struct drm_master *master;
Linus Torvalds's avatar
Linus Torvalds committed
443

Dave Airlie's avatar
Dave Airlie committed
444
	/* Find the list entry for the map and remove it */
445
	list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie's avatar
Dave Airlie committed
446
		if (r_list->map == map) {
447
			master = r_list->master;
448
			list_del(&r_list->head);
449
450
			drm_ht_remove_key(&dev->map_hash,
					  r_list->user_token >> PAGE_SHIFT);
451
452
			drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
			found = 1;
Dave Airlie's avatar
Dave Airlie committed
453
454
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
455
456
	}

457
	if (!found)
Linus Torvalds's avatar
Linus Torvalds committed
458
459
		return -EINVAL;

Dave Airlie's avatar
Dave Airlie committed
460
461
	switch (map->type) {
	case _DRM_REGISTERS:
462
		iounmap(map->handle);
Dave Airlie's avatar
Dave Airlie committed
463
464
465
466
		/* FALLTHROUGH */
	case _DRM_FRAME_BUFFER:
		if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
			int retcode;
Dave Airlie's avatar
Dave Airlie committed
467
468
			retcode = mtrr_del(map->mtrr, map->offset, map->size);
			DRM_DEBUG("mtrr_del=%d\n", retcode);
Linus Torvalds's avatar
Linus Torvalds committed
469
		}
Dave Airlie's avatar
Dave Airlie committed
470
471
472
		break;
	case _DRM_SHM:
		vfree(map->handle);
473
474
475
476
477
		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;
478
			wake_up_interruptible_all(&master->lock.lock_queue);
479
		}
Dave Airlie's avatar
Dave Airlie committed
480
481
482
483
484
485
486
487
488
489
		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;
Jesse Barnes's avatar
Jesse Barnes committed
490
491
492
	case _DRM_GEM:
		DRM_ERROR("tried to rmmap GEM object\n");
		break;
Linus Torvalds's avatar
Linus Torvalds committed
493
	}
Dave Airlie's avatar
Dave Airlie committed
494
495
	drm_free(map, sizeof(*map), DRM_MEM_MAPS);

Linus Torvalds's avatar
Linus Torvalds committed
496
497
	return 0;
}
498
EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie's avatar
Dave Airlie committed
499

500
int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie's avatar
Dave Airlie committed
501
502
503
{
	int ret;

Dave Airlie's avatar
Dave Airlie committed
504
	mutex_lock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
505
	ret = drm_rmmap_locked(dev, map);
Dave Airlie's avatar
Dave Airlie committed
506
	mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
507
508
509

	return ret;
}
510
EXPORT_SYMBOL(drm_rmmap);
511

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

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

	/* Register and framebuffer maps are permanent */
	if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie's avatar
Dave Airlie committed
555
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
556
557
558
559
560
		return 0;
	}

	ret = drm_rmmap_locked(dev, map);

Dave Airlie's avatar
Dave Airlie committed
561
	mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
562
563

	return ret;
564
}
Linus Torvalds's avatar
Linus Torvalds committed
565
566
567
568

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

	if (entry->seg_count) {
		for (i = 0; i < entry->seg_count; i++) {
			if (entry->seglist[i]) {
Dave Airlie's avatar
Dave Airlie committed
582
				drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds's avatar
Linus Torvalds committed
583
584
585
			}
		}
		drm_free(entry->seglist,
Dave Airlie's avatar
Dave Airlie committed
586
587
			 entry->seg_count *
			 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds's avatar
Linus Torvalds committed
588
589
590
591

		entry->seg_count = 0;
	}

Dave Airlie's avatar
Dave Airlie committed
592
593
	if (entry->buf_count) {
		for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
594
595
			if (entry->buflist[i].dev_private) {
				drm_free(entry->buflist[i].dev_private,
Dave Airlie's avatar
Dave Airlie committed
596
597
					 entry->buflist[i].dev_priv_size,
					 DRM_MEM_BUFS);
Linus Torvalds's avatar
Linus Torvalds committed
598
599
600
			}
		}
		drm_free(entry->buflist,
Dave Airlie's avatar
Dave Airlie committed
601
602
			 entry->buf_count *
			 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds's avatar
Linus Torvalds committed
603
604
605
606
607
608
609

		entry->buf_count = 0;
	}
}

#if __OS_HAS_AGP
/**
610
 * Add AGP buffers for DMA transfers.
Linus Torvalds's avatar
Linus Torvalds committed
611
 *
612
 * \param dev struct drm_device to which the buffers are to be added.
613
 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds's avatar
Linus Torvalds committed
614
 * \return zero on success or a negative number on failure.
Dave Airlie's avatar
Dave Airlie committed
615
 *
Linus Torvalds's avatar
Linus Torvalds committed
616
617
618
619
 * 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.
 */
620
int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds's avatar
Linus Torvalds committed
621
{
622
623
	struct drm_device_dma *dma = dev->dma;
	struct drm_buf_entry *entry;
Dave Airlie's avatar
Dave Airlie committed
624
	struct drm_agp_mem *agp_entry;
Dave Airlie's avatar
Dave Airlie committed
625
	struct drm_buf *buf;
Linus Torvalds's avatar
Linus Torvalds committed
626
627
628
629
630
631
632
633
634
	unsigned long offset;
	unsigned long agp_offset;
	int count;
	int order;
	int size;
	int alignment;
	int page_order;
	int total;
	int byte_count;
635
	int i, valid;
Dave Airlie's avatar
Dave Airlie committed
636
	struct drm_buf **temp_buflist;
Linus Torvalds's avatar
Linus Torvalds committed
637

Dave Airlie's avatar
Dave Airlie committed
638
639
	if (!dma)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
640

641
642
	count = request->count;
	order = drm_order(request->size);
Linus Torvalds's avatar
Linus Torvalds committed
643
644
	size = 1 << order;

Dave Airlie's avatar
Dave Airlie committed
645
646
	alignment = (request->flags & _DRM_PAGE_ALIGN)
	    ? PAGE_ALIGN(size) : size;
Linus Torvalds's avatar
Linus Torvalds committed
647
648
649
650
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

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

Dave Airlie's avatar
Dave Airlie committed
653
654
655
	DRM_DEBUG("count:      %d\n", count);
	DRM_DEBUG("order:      %d\n", order);
	DRM_DEBUG("size:       %d\n", size);
656
	DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlie's avatar
Dave Airlie committed
657
658
659
	DRM_DEBUG("alignment:  %d\n", alignment);
	DRM_DEBUG("page_order: %d\n", page_order);
	DRM_DEBUG("total:      %d\n", total);
Linus Torvalds's avatar
Linus Torvalds committed
660

Dave Airlie's avatar
Dave Airlie committed
661
662
663
664
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
	if (dev->queue_count)
		return -EBUSY;	/* Not while in use */
Linus Torvalds's avatar
Linus Torvalds committed
665

666
667
	/* Make sure buffers are located in AGP memory that we own */
	valid = 0;
668
	list_for_each_entry(agp_entry, &dev->agp->memory, head) {
669
670
671
672
673
674
		if ((agp_offset >= agp_entry->bound) &&
		    (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
			valid = 1;
			break;
		}
	}
675
	if (!list_empty(&dev->agp->memory) && !valid) {
676
677
678
		DRM_DEBUG("zone invalid\n");
		return -EINVAL;
	}
Dave Airlie's avatar
Dave Airlie committed
679
680
681
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
682
683
		return -EBUSY;
	}
Dave Airlie's avatar
Dave Airlie committed
684
685
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
686

Dave Airlie's avatar
Dave Airlie committed
687
	mutex_lock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
688
	entry = &dma->bufs[order];
Dave Airlie's avatar
Dave Airlie committed
689
	if (entry->buf_count) {
Dave Airlie's avatar
Dave Airlie committed
690
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
691
692
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
Linus Torvalds's avatar
Linus Torvalds committed
693
694
695
	}

	if (count < 0 || count > 4096) {
Dave Airlie's avatar
Dave Airlie committed
696
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
697
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
698
699
700
		return -EINVAL;
	}

Dave Airlie's avatar
Dave Airlie committed
701
702
703
	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
Dave Airlie's avatar
Dave Airlie committed
704
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
705
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
706
707
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
708
	memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds's avatar
Linus Torvalds committed
709
710
711
712
713
714

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

	offset = 0;

Dave Airlie's avatar
Dave Airlie committed
715
716
717
718
719
720
	while (entry->buf_count < count) {
		buf = &entry->buflist[entry->buf_count];
		buf->idx = dma->buf_count + entry->buf_count;
		buf->total = alignment;
		buf->order = order;
		buf->used = 0;
Linus Torvalds's avatar
Linus Torvalds committed
721

Dave Airlie's avatar
Dave Airlie committed
722
		buf->offset = (dma->byte_count + offset);
Linus Torvalds's avatar
Linus Torvalds committed
723
724
		buf->bus_address = agp_offset + offset;
		buf->address = (void *)(agp_offset + offset);
Dave Airlie's avatar
Dave Airlie committed
725
		buf->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
726
727
		buf->waiting = 0;
		buf->pending = 0;
Dave Airlie's avatar
Dave Airlie committed
728
		init_waitqueue_head(&buf->dma_wait);
729
		buf->file_priv = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
730
731

		buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlie's avatar
Dave Airlie committed
732
733
		buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
		if (!buf->dev_private) {
Linus Torvalds's avatar
Linus Torvalds committed
734
735
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
Dave Airlie's avatar
Dave Airlie committed
736
			drm_cleanup_buf_error(dev, entry);
Dave Airlie's avatar
Dave Airlie committed
737
			mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
738
			atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
739
740
			return -ENOMEM;
		}
Dave Airlie's avatar
Dave Airlie committed
741
		memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds's avatar
Linus Torvalds committed
742

Dave Airlie's avatar
Dave Airlie committed
743
		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds's avatar
Linus Torvalds committed
744
745
746
747
748
749

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

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

Dave Airlie's avatar
Dave Airlie committed
752
753
754
755
756
	temp_buflist = drm_realloc(dma->buflist,
				   dma->buf_count * sizeof(*dma->buflist),
				   (dma->buf_count + entry->buf_count)
				   * sizeof(*dma->buflist), DRM_MEM_BUFS);
	if (!temp_buflist) {
Linus Torvalds's avatar
Linus Torvalds committed
757
		/* Free the entry because it isn't valid */
Dave Airlie's avatar
Dave Airlie committed
758
		drm_cleanup_buf_error(dev, entry);
Dave Airlie's avatar
Dave Airlie committed
759
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
760
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
761
762
763
764
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

Dave Airlie's avatar
Dave Airlie committed
765
	for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
766
767
768
769
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

	dma->buf_count += entry->buf_count;
770
771
	dma->seg_count += entry->seg_count;
	dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
772
773
	dma->byte_count += byte_count;

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

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

779
780
	request->count = entry->buf_count;
	request->size = size;
Linus Torvalds's avatar
Linus Torvalds committed
781
782
783

	dma->flags = _DRM_DMA_USE_AGP;

Dave Airlie's avatar
Dave Airlie committed
784
	atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
785
786
	return 0;
}
787
EXPORT_SYMBOL(drm_addbufs_agp);
Dave Airlie's avatar
Dave Airlie committed
788
#endif				/* __OS_HAS_AGP */
Linus Torvalds's avatar
Linus Torvalds committed
789

790
int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds's avatar
Linus Torvalds committed
791
{
792
	struct drm_device_dma *dma = dev->dma;
Linus Torvalds's avatar
Linus Torvalds committed
793
794
795
796
797
	int count;
	int order;
	int size;
	int total;
	int page_order;
798
	struct drm_buf_entry *entry;
Dave Airlie's avatar
Dave Airlie committed
799
	drm_dma_handle_t *dmah;
Dave Airlie's avatar
Dave Airlie committed
800
	struct drm_buf *buf;
Linus Torvalds's avatar
Linus Torvalds committed
801
802
803
804
805
806
	int alignment;
	unsigned long offset;
	int i;
	int byte_count;
	int page_count;
	unsigned long *temp_pagelist;
Dave Airlie's avatar
Dave Airlie committed
807
	struct drm_buf **temp_buflist;
Linus Torvalds's avatar
Linus Torvalds committed
808

Dave Airlie's avatar
Dave Airlie committed
809
810
	if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
		return -EINVAL;
811

Dave Airlie's avatar
Dave Airlie committed
812
813
	if (!dma)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
814

815
816
817
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

818
819
	count = request->count;
	order = drm_order(request->size);
Linus Torvalds's avatar
Linus Torvalds committed
820
821
	size = 1 << order;

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

Dave Airlie's avatar
Dave Airlie committed
825
826
827
828
	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
		return -EINVAL;
	if (dev->queue_count)
		return -EBUSY;	/* Not while in use */
Linus Torvalds's avatar
Linus Torvalds committed
829

830
	alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlie's avatar
Dave Airlie committed
831
	    ? PAGE_ALIGN(size) : size;
Linus Torvalds's avatar
Linus Torvalds committed
832
833
834
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

Dave Airlie's avatar
Dave Airlie committed
835
836
837
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
838
839
		return -EBUSY;
	}
Dave Airlie's avatar
Dave Airlie committed
840
841
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
842

Dave Airlie's avatar
Dave Airlie committed
843
	mutex_lock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
844
	entry = &dma->bufs[order];
Dave Airlie's avatar
Dave Airlie committed
845
	if (entry->buf_count) {
Dave Airlie's avatar
Dave Airlie committed
846
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
847
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
848
849
850
851
		return -ENOMEM;	/* May only call once for each order */
	}

	if (count < 0 || count > 4096) {
Dave Airlie's avatar
Dave Airlie committed
852
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
853
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
854
855
856
		return -EINVAL;
	}

Dave Airlie's avatar
Dave Airlie committed
857
858
859
	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
Dave Airlie's avatar
Dave Airlie committed
860
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
861
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
862
863
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
864
865
866
867
868
869
870
	memset(entry->buflist, 0, count * sizeof(*entry->buflist));

	entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
				   DRM_MEM_SEGS);
	if (!entry->seglist) {
		drm_free(entry->buflist,
			 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie's avatar
Dave Airlie committed
871
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
872
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
873
874
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
875
	memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds's avatar
Linus Torvalds committed
876
877
878
879

	/* Keep the original pagelist until we know all the allocations
	 * have succeeded
	 */
Dave Airlie's avatar
Dave Airlie committed
880
881
	temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
				  * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds's avatar
Linus Torvalds committed
882
	if (!temp_pagelist) {
Dave Airlie's avatar
Dave Airlie committed
883
884
885
886
		drm_free(entry->buflist,
			 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
		drm_free(entry->seglist,
			 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie's avatar
Dave Airlie committed
887
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
888
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
889
890
891
		return -ENOMEM;
	}
	memcpy(temp_pagelist,
Dave Airlie's avatar
Dave Airlie committed
892
893
894
	       dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
	DRM_DEBUG("pagelist: %d entries\n",
		  dma->page_count + (count << page_order));
Linus Torvalds's avatar
Linus Torvalds committed
895

Dave Airlie's avatar
Dave Airlie committed
896
	entry->buf_size = size;
Linus Torvalds's avatar
Linus Torvalds committed
897
898
899
900
	entry->page_order = page_order;
	byte_count = 0;
	page_count = 0;

Dave Airlie's avatar
Dave Airlie committed
901
	while (entry->buf_count < count) {
902

Dave Airlie's avatar
Dave Airlie committed
903
		dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
904

Dave Airlie's avatar
Dave Airlie committed
905
		if (!dmah) {
Linus Torvalds's avatar
Linus Torvalds committed
906
907
908
909
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
			entry->seg_count = count;
			drm_cleanup_buf_error(dev, entry);
Dave Airlie's avatar
Dave Airlie committed
910
911
912
			drm_free(temp_pagelist,
				 (dma->page_count + (count << page_order))
				 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie's avatar
Dave Airlie committed
913
			mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
914
			atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
915
916
			return -ENOMEM;
		}
Dave Airlie's avatar
Dave Airlie committed
917
		entry->seglist[entry->seg_count++] = dmah;
Dave Airlie's avatar
Dave Airlie committed
918
919
920
		for (i = 0; i < (1 << page_order); i++) {
			DRM_DEBUG("page %d @ 0x%08lx\n",
				  dma->page_count + page_count,
Dave Airlie's avatar
Dave Airlie committed
921
				  (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds's avatar
Linus Torvalds committed
922
			temp_pagelist[dma->page_count + page_count++]
Dave Airlie's avatar
Dave Airlie committed
923
				= (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds's avatar
Linus Torvalds committed
924
		}
Dave Airlie's avatar
Dave Airlie committed
925
926
927
928
929
930
931
932
933
		for (offset = 0;
		     offset + size <= total && entry->buf_count < count;
		     offset += alignment, ++entry->buf_count) {
			buf = &entry->buflist[entry->buf_count];
			buf->idx = dma->buf_count + entry->buf_count;
			buf->total = alignment;
			buf->order = order;
			buf->used = 0;
			buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlie's avatar
Dave Airlie committed
934
935
			buf->address = (void *)(dmah->vaddr + offset);
			buf->bus_address = dmah->busaddr + offset;
Dave Airlie's avatar
Dave Airlie committed
936
			buf->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
937
938
			buf->waiting = 0;
			buf->pending = 0;
Dave Airlie's avatar
Dave Airlie committed
939
			init_waitqueue_head(&buf->dma_wait);
940
			buf->file_priv = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
941
942

			buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlie's avatar
Dave Airlie committed
943
944
945
			buf->dev_private = drm_alloc(buf->dev_priv_size,
						     DRM_MEM_BUFS);
			if (!buf->dev_private) {
Linus Torvalds's avatar
Linus Torvalds committed
946
947
948
				/* Set count correctly so we free the proper amount. */
				entry->buf_count = count;
				entry->seg_count = count;
Dave Airlie's avatar
Dave Airlie committed
949
950
951
952
953
954
				drm_cleanup_buf_error(dev, entry);
				drm_free(temp_pagelist,
					 (dma->page_count +
					  (count << page_order))
					 * sizeof(*dma->pagelist),
					 DRM_MEM_PAGES);
Dave Airlie's avatar
Dave Airlie committed
955
				mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
956
				atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
957
958
				return -ENOMEM;
			}
Dave Airlie's avatar
Dave Airlie committed
959
			memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds's avatar
Linus Torvalds committed
960

Dave Airlie's avatar
Dave Airlie committed
961
962
			DRM_DEBUG("buffer %d @ %p\n",
				  entry->buf_count, buf->address);
Linus Torvalds's avatar
Linus Torvalds committed
963
964
965
966
		}
		byte_count += PAGE_SIZE << page_order;
	}

Dave Airlie's avatar
Dave Airlie committed
967
968
969
970
	temp_buflist = drm_realloc(dma->buflist,
				   dma->buf_count * sizeof(*dma->buflist),
				   (dma->buf_count + entry->buf_count)
				   * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds's avatar
Linus Torvalds committed
971
972
	if (!temp_buflist) {
		/* Free the entry because it isn't valid */
Dave Airlie's avatar
Dave Airlie committed
973
974
975
976
		drm_cleanup_buf_error(dev, entry);
		drm_free(temp_pagelist,
			 (dma->page_count + (count << page_order))
			 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie's avatar
Dave Airlie committed
977
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
978
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
979
980
981
982
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

Dave Airlie's avatar
Dave Airlie committed
983
	for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
984
985
986
987
988
989
990
991
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

	/* No allocations failed, so now we can replace the orginal pagelist
	 * with the new one.
	 */
	if (dma->page_count) {
		drm_free(dma->pagelist,
Dave Airlie's avatar
Dave Airlie committed
992
993
			 dma->page_count * sizeof(*dma->pagelist),
			 DRM_MEM_PAGES);
Linus Torvalds's avatar
Linus Torvalds committed
994
995
996
997
998
999
1000
1001
	}
	dma->pagelist = temp_pagelist;

	dma->buf_count += entry->buf_count;
	dma->seg_count += entry->seg_count;
	dma->page_count += entry->seg_count << page_order;
	dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);

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