drm_bufs.c 43.2 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/**
Dave Airlie's avatar
Dave Airlie committed
2
 * \file drm_bufs.c
Linus Torvalds's avatar
Linus Torvalds committed
3
 * Generic buffer template
Dave Airlie's avatar
Dave Airlie committed
4
 *
Linus Torvalds's avatar
Linus Torvalds committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 */

/*
 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
 *
 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <linux/vmalloc.h>
#include "drmP.h"

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

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

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

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

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

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

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

Linus Torvalds's avatar
Linus Torvalds committed
91
92
93
94
/**
 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
 *
 * \param inode device inode.
95
 * \param file_priv DRM file private.
Linus Torvalds's avatar
Linus Torvalds committed
96
97
98
99
100
101
102
103
 * \param cmd command.
 * \param arg pointer to a drm_map structure.
 * \return zero on success or a negative value on error.
 *
 * Adjusts the memory offset to its absolute value according to the mapping
 * type.  Adds the map to the map list drm_device::maplist. Adds MTRR's where
 * applicable and if supported by the kernel.
 */
104
static int drm_addmap_core(struct drm_device * dev, unsigned int offset,
105
			   unsigned int size, enum drm_map_type type,
Dave Airlie's avatar
Dave Airlie committed
106
107
			   enum drm_map_flags flags,
			   struct drm_map_list ** maplist)
Linus Torvalds's avatar
Linus Torvalds committed
108
{
109
	struct drm_map *map;
Dave Airlie's avatar
Dave Airlie committed
110
	struct drm_map_list *list;
111
	drm_dma_handle_t *dmah;
112
113
	unsigned long user_token;
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
114

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

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

	/* Only allow shared memory to be removable since we only keep enough
	 * book keeping information about shared memory to allow for removal
	 * when processes fork.
	 */
Dave Airlie's avatar
Dave Airlie committed
128
129
	if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
130
131
		return -EINVAL;
	}
Dave Airlie's avatar
Dave Airlie committed
132
133
134
135
	DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
		  map->offset, map->size, map->type);
	if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
136
137
		return -EINVAL;
	}
Dave Airlie's avatar
Dave Airlie committed
138
	map->mtrr = -1;
Linus Torvalds's avatar
Linus Torvalds committed
139
140
	map->handle = NULL;

Dave Airlie's avatar
Dave Airlie committed
141
	switch (map->type) {
Linus Torvalds's avatar
Linus Torvalds committed
142
143
	case _DRM_REGISTERS:
	case _DRM_FRAME_BUFFER:
Dave Airlie's avatar
Dave Airlie committed
144
#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
145
		if (map->offset + (map->size-1) < map->offset ||
Dave Airlie's avatar
Dave Airlie committed
146
147
		    map->offset < virt_to_phys(high_memory)) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
148
149
150
151
152
153
			return -EINVAL;
		}
#endif
#ifdef __alpha__
		map->offset += dev->hose->mem_space->start;
#endif
Dave Airlie's avatar
Dave Airlie committed
154
155
156
157
		/* Some drivers preinitialize some maps, without the X Server
		 * needing to be aware of it.  Therefore, we just return success
		 * when the server tries to create a duplicate map.
		 */
158
159
160
		list = drm_find_matching_map(dev, map);
		if (list != NULL) {
			if (list->map->size != map->size) {
Dave Airlie's avatar
Dave Airlie committed
161
				DRM_DEBUG("Matching maps of type %d with "
Dave Airlie's avatar
Dave Airlie committed
162
163
164
					  "mismatched sizes, (%ld vs %ld)\n",
					  map->type, map->size,
					  list->map->size);
165
				list->map->size = map->size;
Dave Airlie's avatar
Dave Airlie committed
166
167
168
			}

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

Linus Torvalds's avatar
Linus Torvalds committed
173
		if (drm_core_has_MTRR(dev)) {
Dave Airlie's avatar
Dave Airlie committed
174
175
176
177
			if (map->type == _DRM_FRAME_BUFFER ||
			    (map->flags & _DRM_WRITE_COMBINING)) {
				map->mtrr = mtrr_add(map->offset, map->size,
						     MTRR_TYPE_WRCOMB, 1);
Linus Torvalds's avatar
Linus Torvalds committed
178
179
			}
		}
180
		if (map->type == _DRM_REGISTERS) {
181
			map->handle = ioremap(map->offset, map->size);
182
183
184
185
186
			if (!map->handle) {
				drm_free(map, sizeof(*map), DRM_MEM_MAPS);
				return -ENOMEM;
			}
		}
187

Linus Torvalds's avatar
Linus Torvalds committed
188
189
		break;
	case _DRM_SHM:
190
191
192
193
194
195
196
197
198
199
200
201
202
		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;
		}
203
		map->handle = vmalloc_user(map->size);
Dave Airlie's avatar
Dave Airlie committed
204
205
206
207
		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
208
209
210
			return -ENOMEM;
		}
		map->offset = (unsigned long)map->handle;
Dave Airlie's avatar
Dave Airlie committed
211
		if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds's avatar
Linus Torvalds committed
212
			/* Prevent a 2nd X Server from creating a 2nd lock */
213
			if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlie's avatar
Dave Airlie committed
214
215
				vfree(map->handle);
				drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
216
217
				return -EBUSY;
			}
218
			dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle;	/* Pointer to lock */
Linus Torvalds's avatar
Linus Torvalds committed
219
220
		}
		break;
221
	case _DRM_AGP: {
Dave Airlie's avatar
Dave Airlie committed
222
		struct drm_agp_mem *entry;
223
224
225
226
227
228
		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
229
#ifdef __alpha__
230
		map->offset += dev->hose->mem_space->start;
Linus Torvalds's avatar
Linus Torvalds committed
231
#endif
232
233
234
235
236
		/* 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.
237
		 */
238
239
240
241
242
		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;
		}
243
244
245
246
247
248
249
250
		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
		 */
251
		list_for_each_entry(entry, &dev->agp->memory, head) {
252
253
254
255
256
			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
257
		}
258
		if (!list_empty(&dev->agp->memory) && !valid) {
259
260
261
262
263
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EPERM;
		}
		DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);

Jesse Barnes's avatar
Jesse Barnes committed
264
265
266
		break;
	case _DRM_GEM:
		DRM_ERROR("tried to rmmap GEM object\n");
Linus Torvalds's avatar
Linus Torvalds committed
267
		break;
268
	}
Linus Torvalds's avatar
Linus Torvalds committed
269
270
271
272
273
	case _DRM_SCATTER_GATHER:
		if (!dev->sg) {
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -EINVAL;
		}
274
		map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds's avatar
Linus Torvalds committed
275
		break;
Dave Airlie's avatar
Dave Airlie committed
276
	case _DRM_CONSISTENT:
277
		/* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
278
		 * As we're limiting the address to 2^32-1 (or less),
279
280
		 * casting it down to 32 bits is no problem, but we
		 * need to point to a 64bit variable first. */
281
282
		dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
		if (!dmah) {
283
284
285
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
			return -ENOMEM;
		}
286
287
288
		map->handle = dmah->vaddr;
		map->offset = (unsigned long)dmah->busaddr;
		kfree(dmah);
289
		break;
Linus Torvalds's avatar
Linus Torvalds committed
290
	default:
Dave Airlie's avatar
Dave Airlie committed
291
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds's avatar
Linus Torvalds committed
292
293
294
295
		return -EINVAL;
	}

	list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlie's avatar
Dave Airlie committed
296
	if (!list) {
297
		if (map->type == _DRM_REGISTERS)
298
			iounmap(map->handle);
Linus Torvalds's avatar
Linus Torvalds committed
299
300
301
302
303
304
		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
305
	mutex_lock(&dev->struct_mutex);
306
	list_add(&list->head, &dev->maplist);
307

308
	/* Assign a 32-bit handle */
Dave Airlie's avatar
Dave Airlie committed
309
	/* We do it here so that dev->struct_mutex protects the increment */
310
311
	user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
		map->offset;
312
	ret = drm_map_handle(dev, &list->hash, user_token, 0);
313
	if (ret) {
314
		if (map->type == _DRM_REGISTERS)
315
			iounmap(map->handle);
316
317
318
319
320
321
		drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		drm_free(list, sizeof(*list), DRM_MEM_MAPS);
		mutex_unlock(&dev->struct_mutex);
		return ret;
	}

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

325
	list->master = dev->primary->master;
326
	*maplist = list;
327
	return 0;
328
	}
329

330
int drm_addmap(struct drm_device * dev, unsigned int offset,
331
332
	       unsigned int size, enum drm_map_type type,
	       enum drm_map_flags flags, drm_local_map_t ** map_ptr)
333
{
Dave Airlie's avatar
Dave Airlie committed
334
	struct drm_map_list *list;
335
336
337
338
339
340
341
	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
342

343
344
EXPORT_SYMBOL(drm_addmap);

345
346
int drm_addmap_ioctl(struct drm_device *dev, void *data,
		     struct drm_file *file_priv)
347
{
348
	struct drm_map *map = data;
Dave Airlie's avatar
Dave Airlie committed
349
	struct drm_map_list *maplist;
350
351
	int err;

352
	if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
353
354
		return -EPERM;

355
356
	err = drm_addmap_core(dev, map->offset, map->size, map->type,
			      map->flags, &maplist);
357

Dave Airlie's avatar
Dave Airlie committed
358
	if (err)
359
		return err;
360

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

/**
 * Remove a map private from list and deallocate resources if the mapping
 * isn't in use.
 *
 * \param inode device inode.
371
 * \param file_priv DRM file private.
Linus Torvalds's avatar
Linus Torvalds committed
372
 * \param cmd command.
373
 * \param arg pointer to a struct drm_map structure.
Linus Torvalds's avatar
Linus Torvalds committed
374
375
376
377
378
379
 * \return zero on success or a negative value on error.
 *
 * Searches the map on drm_device::maplist, removes it from the list, see if
 * its being used, and free any associate resource (such as MTRR's) if it's not
 * being on use.
 *
380
 * \sa drm_addmap
Linus Torvalds's avatar
Linus Torvalds committed
381
 */
382
int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
Linus Torvalds's avatar
Linus Torvalds committed
383
{
Dave Airlie's avatar
Dave Airlie committed
384
	struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie's avatar
Dave Airlie committed
385
	drm_dma_handle_t dmah;
386
	int found = 0;
387
	struct drm_master *master;
Linus Torvalds's avatar
Linus Torvalds committed
388

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

402
	if (!found)
Linus Torvalds's avatar
Linus Torvalds committed
403
404
		return -EINVAL;

Dave Airlie's avatar
Dave Airlie committed
405
406
	switch (map->type) {
	case _DRM_REGISTERS:
407
		iounmap(map->handle);
Dave Airlie's avatar
Dave Airlie committed
408
409
410
411
		/* FALLTHROUGH */
	case _DRM_FRAME_BUFFER:
		if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
			int retcode;
Dave Airlie's avatar
Dave Airlie committed
412
413
			retcode = mtrr_del(map->mtrr, map->offset, map->size);
			DRM_DEBUG("mtrr_del=%d\n", retcode);
Linus Torvalds's avatar
Linus Torvalds committed
414
		}
Dave Airlie's avatar
Dave Airlie committed
415
416
417
		break;
	case _DRM_SHM:
		vfree(map->handle);
418
419
420
421
422
423
424
		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;
			wake_up_interruptible(&master->lock.lock_queue);
		}
Dave Airlie's avatar
Dave Airlie committed
425
426
427
428
429
430
431
432
433
434
		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
435
436
437
	case _DRM_GEM:
		DRM_ERROR("tried to rmmap GEM object\n");
		break;
Linus Torvalds's avatar
Linus Torvalds committed
438
	}
Dave Airlie's avatar
Dave Airlie committed
439
440
	drm_free(map, sizeof(*map), DRM_MEM_MAPS);

Linus Torvalds's avatar
Linus Torvalds committed
441
442
	return 0;
}
443
EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie's avatar
Dave Airlie committed
444

445
int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
Dave Airlie's avatar
Dave Airlie committed
446
447
448
{
	int ret;

Dave Airlie's avatar
Dave Airlie committed
449
	mutex_lock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
450
	ret = drm_rmmap_locked(dev, map);
Dave Airlie's avatar
Dave Airlie committed
451
	mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
452
453
454

	return ret;
}
455
EXPORT_SYMBOL(drm_rmmap);
456

Dave Airlie's avatar
Dave Airlie committed
457
458
459
460
461
462
463
464
465
/* 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.
 */
466
467
int drm_rmmap_ioctl(struct drm_device *dev, void *data,
		    struct drm_file *file_priv)
468
{
469
	struct drm_map *request = data;
Dave Airlie's avatar
Dave Airlie committed
470
	drm_local_map_t *map = NULL;
Dave Airlie's avatar
Dave Airlie committed
471
	struct drm_map_list *r_list;
Dave Airlie's avatar
Dave Airlie committed
472
	int ret;
473

Dave Airlie's avatar
Dave Airlie committed
474
	mutex_lock(&dev->struct_mutex);
475
	list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie's avatar
Dave Airlie committed
476
		if (r_list->map &&
477
		    r_list->user_token == (unsigned long)request->handle &&
Dave Airlie's avatar
Dave Airlie committed
478
479
480
481
482
483
484
485
486
		    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.
	 */
487
	if (list_empty(&dev->maplist) || !map) {
Dave Airlie's avatar
Dave Airlie committed
488
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
489
490
491
492
493
		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
494
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
495
496
497
498
499
		return 0;
	}

	ret = drm_rmmap_locked(dev, map);

Dave Airlie's avatar
Dave Airlie committed
500
	mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
501
502

	return ret;
503
}
Linus Torvalds's avatar
Linus Torvalds committed
504
505
506
507

/**
 * Cleanup after an error on one of the addbufs() functions.
 *
Dave Airlie's avatar
Dave Airlie committed
508
 * \param dev DRM device.
Linus Torvalds's avatar
Linus Torvalds committed
509
510
511
512
 * \param entry buffer entry where the error occurred.
 *
 * Frees any pages and buffers associated with the given entry.
 */
513
514
static void drm_cleanup_buf_error(struct drm_device * dev,
				  struct drm_buf_entry * entry)
Linus Torvalds's avatar
Linus Torvalds committed
515
516
517
518
519
520
{
	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
521
				drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds's avatar
Linus Torvalds committed
522
523
524
			}
		}
		drm_free(entry->seglist,
Dave Airlie's avatar
Dave Airlie committed
525
526
			 entry->seg_count *
			 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds's avatar
Linus Torvalds committed
527
528
529
530

		entry->seg_count = 0;
	}

Dave Airlie's avatar
Dave Airlie committed
531
532
	if (entry->buf_count) {
		for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
533
534
			if (entry->buflist[i].dev_private) {
				drm_free(entry->buflist[i].dev_private,
Dave Airlie's avatar
Dave Airlie committed
535
536
					 entry->buflist[i].dev_priv_size,
					 DRM_MEM_BUFS);
Linus Torvalds's avatar
Linus Torvalds committed
537
538
539
			}
		}
		drm_free(entry->buflist,
Dave Airlie's avatar
Dave Airlie committed
540
541
			 entry->buf_count *
			 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds's avatar
Linus Torvalds committed
542
543
544
545
546
547
548

		entry->buf_count = 0;
	}
}

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

Dave Airlie's avatar
Dave Airlie committed
577
578
	if (!dma)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
579

580
581
	count = request->count;
	order = drm_order(request->size);
Linus Torvalds's avatar
Linus Torvalds committed
582
583
	size = 1 << order;

Dave Airlie's avatar
Dave Airlie committed
584
585
	alignment = (request->flags & _DRM_PAGE_ALIGN)
	    ? PAGE_ALIGN(size) : size;
Linus Torvalds's avatar
Linus Torvalds committed
586
587
588
589
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

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

Dave Airlie's avatar
Dave Airlie committed
592
593
594
	DRM_DEBUG("count:      %d\n", count);
	DRM_DEBUG("order:      %d\n", order);
	DRM_DEBUG("size:       %d\n", size);
595
	DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlie's avatar
Dave Airlie committed
596
597
598
	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
599

Dave Airlie's avatar
Dave Airlie committed
600
601
602
603
	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
604

605
606
	/* Make sure buffers are located in AGP memory that we own */
	valid = 0;
607
	list_for_each_entry(agp_entry, &dev->agp->memory, head) {
608
609
610
611
612
613
		if ((agp_offset >= agp_entry->bound) &&
		    (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
			valid = 1;
			break;
		}
	}
614
	if (!list_empty(&dev->agp->memory) && !valid) {
615
616
617
		DRM_DEBUG("zone invalid\n");
		return -EINVAL;
	}
Dave Airlie's avatar
Dave Airlie committed
618
619
620
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
621
622
		return -EBUSY;
	}
Dave Airlie's avatar
Dave Airlie committed
623
624
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
625

Dave Airlie's avatar
Dave Airlie committed
626
	mutex_lock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
627
	entry = &dma->bufs[order];
Dave Airlie's avatar
Dave Airlie committed
628
	if (entry->buf_count) {
Dave Airlie's avatar
Dave Airlie committed
629
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
630
631
		atomic_dec(&dev->buf_alloc);
		return -ENOMEM;	/* May only call once for each order */
Linus Torvalds's avatar
Linus Torvalds committed
632
633
634
	}

	if (count < 0 || count > 4096) {
Dave Airlie's avatar
Dave Airlie committed
635
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
636
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
637
638
639
		return -EINVAL;
	}

Dave Airlie's avatar
Dave Airlie committed
640
641
642
	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
Dave Airlie's avatar
Dave Airlie committed
643
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
644
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
645
646
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
647
	memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds's avatar
Linus Torvalds committed
648
649
650
651
652
653

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

	offset = 0;

Dave Airlie's avatar
Dave Airlie committed
654
655
656
657
658
659
	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
660

Dave Airlie's avatar
Dave Airlie committed
661
		buf->offset = (dma->byte_count + offset);
Linus Torvalds's avatar
Linus Torvalds committed
662
663
		buf->bus_address = agp_offset + offset;
		buf->address = (void *)(agp_offset + offset);
Dave Airlie's avatar
Dave Airlie committed
664
		buf->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
665
666
		buf->waiting = 0;
		buf->pending = 0;
Dave Airlie's avatar
Dave Airlie committed
667
		init_waitqueue_head(&buf->dma_wait);
668
		buf->file_priv = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
669
670

		buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlie's avatar
Dave Airlie committed
671
672
		buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
		if (!buf->dev_private) {
Linus Torvalds's avatar
Linus Torvalds committed
673
674
			/* Set count correctly so we free the proper amount. */
			entry->buf_count = count;
Dave Airlie's avatar
Dave Airlie committed
675
			drm_cleanup_buf_error(dev, entry);
Dave Airlie's avatar
Dave Airlie committed
676
			mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
677
			atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
678
679
			return -ENOMEM;
		}
Dave Airlie's avatar
Dave Airlie committed
680
		memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds's avatar
Linus Torvalds committed
681

Dave Airlie's avatar
Dave Airlie committed
682
		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds's avatar
Linus Torvalds committed
683
684
685
686
687
688

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

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

Dave Airlie's avatar
Dave Airlie committed
691
692
693
694
695
	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
696
		/* Free the entry because it isn't valid */
Dave Airlie's avatar
Dave Airlie committed
697
		drm_cleanup_buf_error(dev, entry);
Dave Airlie's avatar
Dave Airlie committed
698
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
699
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
700
701
702
703
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

Dave Airlie's avatar
Dave Airlie committed
704
	for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
705
706
707
708
		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
	}

	dma->buf_count += entry->buf_count;
709
710
	dma->seg_count += entry->seg_count;
	dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
711
712
	dma->byte_count += byte_count;

Dave Airlie's avatar
Dave Airlie committed
713
714
	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
715

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

718
719
	request->count = entry->buf_count;
	request->size = size;
Linus Torvalds's avatar
Linus Torvalds committed
720
721
722

	dma->flags = _DRM_DMA_USE_AGP;

Dave Airlie's avatar
Dave Airlie committed
723
	atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
724
725
	return 0;
}
726
EXPORT_SYMBOL(drm_addbufs_agp);
Dave Airlie's avatar
Dave Airlie committed
727
#endif				/* __OS_HAS_AGP */
Linus Torvalds's avatar
Linus Torvalds committed
728

729
int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds's avatar
Linus Torvalds committed
730
{
731
	struct drm_device_dma *dma = dev->dma;
Linus Torvalds's avatar
Linus Torvalds committed
732
733
734
735
736
	int count;
	int order;
	int size;
	int total;
	int page_order;
737
	struct drm_buf_entry *entry;
Dave Airlie's avatar
Dave Airlie committed
738
	drm_dma_handle_t *dmah;
Dave Airlie's avatar
Dave Airlie committed
739
	struct drm_buf *buf;
Linus Torvalds's avatar
Linus Torvalds committed
740
741
742
743
744
745
	int alignment;
	unsigned long offset;
	int i;
	int byte_count;
	int page_count;
	unsigned long *temp_pagelist;
Dave Airlie's avatar
Dave Airlie committed
746
	struct drm_buf **temp_buflist;
Linus Torvalds's avatar
Linus Torvalds committed
747

Dave Airlie's avatar
Dave Airlie committed
748
749
	if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
		return -EINVAL;
750

Dave Airlie's avatar
Dave Airlie committed
751
752
	if (!dma)
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
753

754
755
756
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

757
758
	count = request->count;
	order = drm_order(request->size);
Linus Torvalds's avatar
Linus Torvalds committed
759
760
	size = 1 << order;

Dave Airlie's avatar
Dave Airlie committed
761
762
	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
763

Dave Airlie's avatar
Dave Airlie committed
764
765
766
767
	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
768

769
	alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlie's avatar
Dave Airlie committed
770
	    ? PAGE_ALIGN(size) : size;
Linus Torvalds's avatar
Linus Torvalds committed
771
772
773
	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
	total = PAGE_SIZE << page_order;

Dave Airlie's avatar
Dave Airlie committed
774
775
776
	spin_lock(&dev->count_lock);
	if (dev->buf_use) {
		spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
777
778
		return -EBUSY;
	}
Dave Airlie's avatar
Dave Airlie committed
779
780
	atomic_inc(&dev->buf_alloc);
	spin_unlock(&dev->count_lock);
Linus Torvalds's avatar
Linus Torvalds committed
781

Dave Airlie's avatar
Dave Airlie committed
782
	mutex_lock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
783
	entry = &dma->bufs[order];
Dave Airlie's avatar
Dave Airlie committed
784
	if (entry->buf_count) {
Dave Airlie's avatar
Dave Airlie committed
785
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
786
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
787
788
789
790
		return -ENOMEM;	/* May only call once for each order */
	}

	if (count < 0 || count > 4096) {
Dave Airlie's avatar
Dave Airlie committed
791
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
792
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
793
794
795
		return -EINVAL;
	}

Dave Airlie's avatar
Dave Airlie committed
796
797
798
	entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
				   DRM_MEM_BUFS);
	if (!entry->buflist) {
Dave Airlie's avatar
Dave Airlie committed
799
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
800
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
801
802
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
803
804
805
806
807
808
809
	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
810
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
811
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
812
813
		return -ENOMEM;
	}
Dave Airlie's avatar
Dave Airlie committed
814
	memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds's avatar
Linus Torvalds committed
815
816
817
818

	/* Keep the original pagelist until we know all the allocations
	 * have succeeded
	 */
Dave Airlie's avatar
Dave Airlie committed
819
820
	temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
				  * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds's avatar
Linus Torvalds committed
821
	if (!temp_pagelist) {
Dave Airlie's avatar
Dave Airlie committed
822
823
824
825
		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
826
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
827
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
828
829
830
		return -ENOMEM;
	}
	memcpy(temp_pagelist,
Dave Airlie's avatar
Dave Airlie committed
831
832
833
	       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
834

Dave Airlie's avatar
Dave Airlie committed
835
	entry->buf_size = size;
Linus Torvalds's avatar
Linus Torvalds committed
836
837
838
839
	entry->page_order = page_order;
	byte_count = 0;
	page_count = 0;

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

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

Dave Airlie's avatar
Dave Airlie committed
844
		if (!dmah) {
Linus Torvalds's avatar
Linus Torvalds committed
845
846
847
848
			/* 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
849
850
851
			drm_free(temp_pagelist,
				 (dma->page_count + (count << page_order))
				 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
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
			return -ENOMEM;
		}
Dave Airlie's avatar
Dave Airlie committed
856
		entry->seglist[entry->seg_count++] = dmah;
Dave Airlie's avatar
Dave Airlie committed
857
858
859
		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
860
				  (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds's avatar
Linus Torvalds committed
861
			temp_pagelist[dma->page_count + page_count++]
Dave Airlie's avatar
Dave Airlie committed
862
				= (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds's avatar
Linus Torvalds committed
863
		}
Dave Airlie's avatar
Dave Airlie committed
864
865
866
867
868
869
870
871
872
		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
873
874
			buf->address = (void *)(dmah->vaddr + offset);
			buf->bus_address = dmah->busaddr + offset;
Dave Airlie's avatar
Dave Airlie committed
875
			buf->next = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
876
877
			buf->waiting = 0;
			buf->pending = 0;
Dave Airlie's avatar
Dave Airlie committed
878
			init_waitqueue_head(&buf->dma_wait);
879
			buf->file_priv = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
880
881

			buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlie's avatar
Dave Airlie committed
882
883
884
			buf->dev_private = drm_alloc(buf->dev_priv_size,
						     DRM_MEM_BUFS);
			if (!buf->dev_private) {
Linus Torvalds's avatar
Linus Torvalds committed
885
886
887
				/* Set count correctly so we free the proper amount. */
				entry->buf_count = count;
				entry->seg_count = count;
Dave Airlie's avatar
Dave Airlie committed
888
889
890
891
892
893
				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
894
				mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
895
				atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
896
897
				return -ENOMEM;
			}
Dave Airlie's avatar
Dave Airlie committed
898
			memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds's avatar
Linus Torvalds committed
899

Dave Airlie's avatar
Dave Airlie committed
900
901
			DRM_DEBUG("buffer %d @ %p\n",
				  entry->buf_count, buf->address);
Linus Torvalds's avatar
Linus Torvalds committed
902
903
904
905
		}
		byte_count += PAGE_SIZE << page_order;
	}

Dave Airlie's avatar
Dave Airlie committed
906
907
908
909
	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
910
911
	if (!temp_buflist) {
		/* Free the entry because it isn't valid */
Dave Airlie's avatar
Dave Airlie committed
912
913
914
915
		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
916
		mutex_unlock(&dev->struct_mutex);
Dave Airlie's avatar
Dave Airlie committed
917
		atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
918
919
920
921
		return -ENOMEM;
	}
	dma->buflist = temp_buflist;

Dave Airlie's avatar
Dave Airlie committed
922
	for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
923
924
925
926
927
928
929
930
		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
931
932
			 dma->page_count * sizeof(*dma->pagelist),
			 DRM_MEM_PAGES);
Linus Torvalds's avatar
Linus Torvalds committed
933
934
935
936
937
938
939
940
	}
	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
941
	mutex_unlock(&dev->struct_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
942

943
944
	request->count = entry->buf_count;
	request->size = size;
Linus Torvalds's avatar
Linus Torvalds committed
945

946
947
948
	if (request->flags & _DRM_PCI_BUFFER_RO)
		dma->flags = _DRM_DMA_USE_PCI_RO;

Dave Airlie's avatar
Dave Airlie committed
949
	atomic_dec(&dev->buf_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
950
951
952
	return 0;

}
953
EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds's avatar
Linus Torvalds committed
954

955
static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds's avatar
Linus Torvalds committed
956
{
957
958
	struct drm_device_dma *dma = dev->dma;
	struct drm_buf_entry *entry;
Dave Airlie's avatar
Dave Airlie committed
959
	struct drm_buf *buf;
Linus Torvalds's avatar
Linus Torvalds committed
960
961
962
963
964
965
966
967
968
969
	unsigned long offset;
	unsigned long agp_offset;
	int count;
	int order;
	int size;
	int alignment;
	int page_order;
	int total;
	int byte_count;
	int i;
Dave Airlie's avatar
Dave Airlie committed
970
	struct drm_buf **temp_buflist;
Linus Torvalds's avatar
Linus Torvalds committed
971

Dave Airlie's avatar
Dave Airlie committed
972
973
974
975
976
	if (!drm_core_check_feature(dev, DRIVER_SG))
		return -EINVAL;

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

978
979
980
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

Dave Airlie's avatar