xref: /dflybsd-src/sys/dev/drm/drm_bufs.c (revision d89a0e31a842d13f34830f543170e9d31a70afd9)
1 /*
2  * Legacy: Generic DRM Buffer Management
3  *
4  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
8  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
9  * Author: Gareth Hughes <gareth@valinux.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice (including the next
19  * paragraph) shall be included in all copies or substantial portions of the
20  * Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  * OTHER DEALINGS IN THE SOFTWARE.
29  */
30 
31 #include <linux/vmalloc.h>
32 #include <linux/log2.h>
33 #include <linux/export.h>
34 #include <asm/shmparam.h>
35 #include <drm/drmP.h>
36 #include "drm_legacy.h"
37 
38 static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
39 						  struct drm_local_map *map)
40 {
41 	struct drm_map_list *entry;
42 	list_for_each_entry(entry, &dev->maplist, head) {
43 		/*
44 		 * Because the kernel-userspace ABI is fixed at a 32-bit offset
45 		 * while PCI resources may live above that, we only compare the
46 		 * lower 32 bits of the map offset for maps of type
47 		 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
48 		 * It is assumed that if a driver have more than one resource
49 		 * of each type, the lower 32 bits are different.
50 		 */
51 		if (!entry->map ||
52 		    map->type != entry->map->type ||
53 		    entry->master != dev->primary->master)
54 			continue;
55 		switch (map->type) {
56 		case _DRM_SHM:
57 			if (map->flags != _DRM_CONTAINS_LOCK)
58 				break;
59 			return entry;
60 		case _DRM_REGISTERS:
61 		case _DRM_FRAME_BUFFER:
62 			if ((entry->map->offset & 0xffffffff) ==
63 			    (map->offset & 0xffffffff))
64 				return entry;
65 		default: /* Make gcc happy */
66 			;
67 		}
68 		if (entry->map->offset == map->offset)
69 			return entry;
70 	}
71 
72 	return NULL;
73 }
74 
75 static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
76 			  unsigned long user_token, int hashed_handle, int shm)
77 {
78 	int use_hashed_handle, shift;
79 	unsigned long add;
80 
81 #if (BITS_PER_LONG == 64)
82 	use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
83 #elif (BITS_PER_LONG == 32)
84 	use_hashed_handle = hashed_handle;
85 #else
86 #error Unsupported long size. Neither 64 nor 32 bits.
87 #endif
88 
89 	if (!use_hashed_handle) {
90 		int ret;
91 		hash->key = user_token >> PAGE_SHIFT;
92 		ret = drm_ht_insert_item(&dev->map_hash, hash);
93 		if (ret != -EINVAL)
94 			return ret;
95 	}
96 
97 	shift = 0;
98 	add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
99 	if (shm && (SHMLBA > PAGE_SIZE)) {
100 		int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
101 
102 		/* For shared memory, we have to preserve the SHMLBA
103 		 * bits of the eventual vma->vm_pgoff value during
104 		 * mmap().  Otherwise we run into cache aliasing problems
105 		 * on some platforms.  On these platforms, the pgoff of
106 		 * a mmap() request is used to pick a suitable virtual
107 		 * address for the mmap() region such that it will not
108 		 * cause cache aliasing problems.
109 		 *
110 		 * Therefore, make sure the SHMLBA relevant bits of the
111 		 * hash value we use are equal to those in the original
112 		 * kernel virtual address.
113 		 */
114 		shift = bits;
115 		add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
116 	}
117 
118 	return drm_ht_just_insert_please(&dev->map_hash, hash,
119 					 user_token, 32 - PAGE_SHIFT - 3,
120 					 shift, add);
121 }
122 
123 /**
124  * Core function to create a range of memory available for mapping by a
125  * non-root process.
126  *
127  * Adjusts the memory offset to its absolute value according to the mapping
128  * type.  Adds the map to the map list drm_device::maplist. Adds MTRR's where
129  * applicable and if supported by the kernel.
130  */
131 static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
132 			   unsigned int size, enum drm_map_type type,
133 			   enum drm_map_flags flags,
134 			   struct drm_map_list ** maplist)
135 {
136 	struct drm_local_map *map;
137 	struct drm_map_list *list;
138 	drm_dma_handle_t *dmah;
139 	unsigned long user_token;
140 	int ret;
141 
142 	map = kmalloc(sizeof(*map), M_DRM, M_WAITOK);
143 	if (!map)
144 		return -ENOMEM;
145 
146 	map->offset = offset;
147 	map->size = size;
148 	map->flags = flags;
149 	map->type = type;
150 
151 	/* Only allow shared memory to be removable since we only keep enough
152 	 * book keeping information about shared memory to allow for removal
153 	 * when processes fork.
154 	 */
155 	if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
156 		kfree(map);
157 		return -EINVAL;
158 	}
159 	DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
160 		  (unsigned long long)map->offset, map->size, map->type);
161 
162 	/* page-align _DRM_SHM maps. They are allocated here so there is no security
163 	 * hole created by that and it works around various broken drivers that use
164 	 * a non-aligned quantity to map the SAREA. --BenH
165 	 */
166 	if (map->type == _DRM_SHM)
167 		map->size = PAGE_ALIGN(map->size);
168 
169 	if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
170 		kfree(map);
171 		return -EINVAL;
172 	}
173 	map->mtrr = -1;
174 	map->handle = NULL;
175 
176 	switch (map->type) {
177 	case _DRM_REGISTERS:
178 	case _DRM_FRAME_BUFFER:
179 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
180 		if (map->offset + (map->size-1) < map->offset ||
181 		    map->offset < virt_to_phys(high_memory)) {
182 			kfree(map);
183 			return -EINVAL;
184 		}
185 #endif
186 		/* Some drivers preinitialize some maps, without the X Server
187 		 * needing to be aware of it.  Therefore, we just return success
188 		 * when the server tries to create a duplicate map.
189 		 */
190 		list = drm_find_matching_map(dev, map);
191 		if (list != NULL) {
192 			if (list->map->size != map->size) {
193 				DRM_DEBUG("Matching maps of type %d with "
194 					  "mismatched sizes, (%ld vs %ld)\n",
195 					  map->type, map->size,
196 					  list->map->size);
197 				list->map->size = map->size;
198 			}
199 
200 			kfree(map);
201 			*maplist = list;
202 			return 0;
203 		}
204 
205 		if (map->type == _DRM_FRAME_BUFFER ||
206 		    (map->flags & _DRM_WRITE_COMBINING)) {
207 			map->mtrr =
208 				arch_phys_wc_add(map->offset, map->size);
209 		}
210 		if (map->type == _DRM_REGISTERS) {
211 			if (map->flags & _DRM_WRITE_COMBINING)
212 				map->handle = ioremap_wc(map->offset,
213 							 map->size);
214 			else
215 				map->handle = ioremap(map->offset, map->size);
216 			if (!map->handle) {
217 				kfree(map);
218 				return -ENOMEM;
219 			}
220 		}
221 
222 		break;
223 	case _DRM_SHM:
224 		list = drm_find_matching_map(dev, map);
225 		if (list != NULL) {
226 			if(list->map->size != map->size) {
227 				DRM_DEBUG("Matching maps of type %d with "
228 					  "mismatched sizes, (%ld vs %ld)\n",
229 					  map->type, map->size, list->map->size);
230 				list->map->size = map->size;
231 			}
232 
233 			kfree(map);
234 			*maplist = list;
235 			return 0;
236 		}
237 		map->handle = vmalloc_user(map->size);
238 		DRM_DEBUG("%lu %d %p\n",
239 			  map->size, order_base_2(map->size), map->handle);
240 		if (!map->handle) {
241 			kfree(map);
242 			return -ENOMEM;
243 		}
244 		map->offset = (unsigned long)map->handle;
245 		if (map->flags & _DRM_CONTAINS_LOCK) {
246 			/* Prevent a 2nd X Server from creating a 2nd lock */
247 			if (dev->primary->master->lock.hw_lock != NULL) {
248 				vfree(map->handle);
249 				kfree(map);
250 				return -EBUSY;
251 			}
252 			dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle;	/* Pointer to lock */
253 		}
254 		break;
255 	case _DRM_AGP: {
256 #if 0
257 		struct drm_agp_mem *entry;
258 		int valid = 0;
259 #endif
260 
261 		if (!dev->agp) {
262 			kfree(map);
263 			return -EINVAL;
264 		}
265 #ifdef __alpha__
266 		map->offset += dev->hose->mem_space->start;
267 #endif
268 		/* In some cases (i810 driver), user space may have already
269 		 * added the AGP base itself, because dev->agp->base previously
270 		 * only got set during AGP enable.  So, only add the base
271 		 * address if the map's offset isn't already within the
272 		 * aperture.
273 		 */
274 		if (map->offset < dev->agp->base ||
275 		    map->offset > dev->agp->base +
276 		    dev->agp->agp_info.ai_aperture_size - 1) {
277 			map->offset += dev->agp->base;
278 		}
279 		map->mtrr = dev->agp->agp_mtrr;	/* for getmap */
280 
281 		/* This assumes the DRM is in total control of AGP space.
282 		 * It's not always the case as AGP can be in the control
283 		 * of user space (i.e. i810 driver). So this loop will get
284 		 * skipped and we double check that dev->agp->memory is
285 		 * actually set as well as being invalid before EPERM'ing
286 		 */
287 #if 0
288 		list_for_each_entry(entry, &dev->agp->memory, head) {
289 			if ((map->offset >= entry->bound) &&
290 			    (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
291 				valid = 1;
292 				break;
293 			}
294 		}
295 		if (!list_empty(&dev->agp->memory) && !valid) {
296 			kfree(map);
297 			return -EPERM;
298 		}
299 #endif
300 		DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
301 			  (unsigned long long)map->offset, map->size);
302 
303 		break;
304 	}
305 	case _DRM_SCATTER_GATHER:
306 		if (!dev->sg) {
307 			kfree(map);
308 			return -EINVAL;
309 		}
310 		map->handle = (void *)(uintptr_t)(dev->sg->vaddr + offset);
311 		map->offset = dev->sg->vaddr + offset;
312 		break;
313 	case _DRM_CONSISTENT:
314 		/* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
315 		 * As we're limiting the address to 2^32-1 (or less),
316 		 * casting it down to 32 bits is no problem, but we
317 		 * need to point to a 64bit variable first. */
318 		dmah = drm_pci_alloc(dev, map->size, map->size);
319 		if (!dmah) {
320 			kfree(map);
321 			return -ENOMEM;
322 		}
323 		map->handle = dmah->vaddr;
324 		map->offset = (unsigned long)dmah->busaddr;
325 		kfree(dmah);
326 		break;
327 	default:
328 		kfree(map);
329 		return -EINVAL;
330 	}
331 
332 	list = kzalloc(sizeof(*list), GFP_KERNEL);
333 	if (!list) {
334 		if (map->type == _DRM_REGISTERS)
335 			iounmap(map->handle);
336 		kfree(map);
337 		return -EINVAL;
338 	}
339 	list->map = map;
340 
341 	mutex_lock(&dev->struct_mutex);
342 	list_add(&list->head, &dev->maplist);
343 
344 	/* Assign a 32-bit handle */
345 	/* We do it here so that dev->struct_mutex protects the increment */
346 	user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
347 		map->offset;
348 	ret = drm_map_handle(dev, &list->hash, user_token, 0,
349 			     (map->type == _DRM_SHM));
350 	if (ret) {
351 		if (map->type == _DRM_REGISTERS)
352 			iounmap(map->handle);
353 		kfree(map);
354 		kfree(list);
355 		mutex_unlock(&dev->struct_mutex);
356 		return ret;
357 	}
358 
359 	list->user_token = list->hash.key << PAGE_SHIFT;
360 	mutex_unlock(&dev->struct_mutex);
361 
362 	if (!(map->flags & _DRM_DRIVER))
363 		list->master = dev->primary->master;
364 	*maplist = list;
365 	return 0;
366 }
367 
368 int drm_legacy_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 {
372 	struct drm_map_list *list;
373 	int rc;
374 
375 	rc = drm_addmap_core(dev, offset, size, type, flags, &list);
376 	if (!rc)
377 		*map_ptr = list->map;
378 	return rc;
379 }
380 EXPORT_SYMBOL(drm_legacy_addmap);
381 
382 /**
383  * Ioctl to specify a range of memory that is available for mapping by a
384  * non-root process.
385  *
386  * \param inode device inode.
387  * \param file_priv DRM file private.
388  * \param cmd command.
389  * \param arg pointer to a drm_map structure.
390  * \return zero on success or a negative value on error.
391  *
392  */
393 int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
394 			    struct drm_file *file_priv)
395 {
396 	struct drm_map *map = data;
397 	struct drm_map_list *maplist;
398 	int err;
399 
400 	if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
401 		return -EPERM;
402 
403 	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
404 	    drm_core_check_feature(dev, DRIVER_MODESET))
405 		return -EINVAL;
406 
407 	err = drm_addmap_core(dev, map->offset, map->size, map->type,
408 			      map->flags, &maplist);
409 
410 	if (err)
411 		return err;
412 
413 	/* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
414 	map->handle = (void *)(unsigned long)maplist->user_token;
415 
416 	/*
417 	 * It appears that there are no users of this value whatsoever --
418 	 * drmAddMap just discards it.  Let's not encourage its use.
419 	 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
420 	 *  it's not a real mtrr index anymore.)
421 	 */
422 	map->mtrr = -1;
423 
424 	return 0;
425 }
426 
427 /*
428  * Get a mapping information.
429  *
430  * \param inode device inode.
431  * \param file_priv DRM file private.
432  * \param cmd command.
433  * \param arg user argument, pointing to a drm_map structure.
434  *
435  * \return zero on success or a negative number on failure.
436  *
437  * Searches for the mapping with the specified offset and copies its information
438  * into userspace
439  */
440 int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
441 			    struct drm_file *file_priv)
442 {
443 	struct drm_map *map = data;
444 	struct drm_map_list *r_list = NULL;
445 	struct list_head *list;
446 	int idx;
447 	int i;
448 
449 	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
450 	    drm_core_check_feature(dev, DRIVER_MODESET))
451 		return -EINVAL;
452 
453 	idx = map->offset;
454 	if (idx < 0)
455 		return -EINVAL;
456 
457 	i = 0;
458 	mutex_lock(&dev->struct_mutex);
459 	list_for_each(list, &dev->maplist) {
460 		if (i == idx) {
461 			r_list = list_entry(list, struct drm_map_list, head);
462 			break;
463 		}
464 		i++;
465 	}
466 	if (!r_list || !r_list->map) {
467 		mutex_unlock(&dev->struct_mutex);
468 		return -EINVAL;
469 	}
470 
471 	map->offset = r_list->map->offset;
472 	map->size = r_list->map->size;
473 	map->type = r_list->map->type;
474 	map->flags = r_list->map->flags;
475 	map->handle = (void *)(unsigned long) r_list->user_token;
476 	map->mtrr = r_list->map->mtrr;
477 
478 	mutex_unlock(&dev->struct_mutex);
479 
480 	return 0;
481 }
482 
483 /**
484  * Remove a map private from list and deallocate resources if the mapping
485  * isn't in use.
486  *
487  * Searches the map on drm_device::maplist, removes it from the list, see if
488  * its being used, and free any associate resource (such as MTRR's) if it's not
489  * being on use.
490  *
491  * \sa drm_legacy_addmap
492  */
493 int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
494 {
495 	struct drm_map_list *r_list = NULL, *list_t;
496 	drm_dma_handle_t dmah;
497 	int found = 0;
498 	struct drm_master *master;
499 
500 	/* Find the list entry for the map and remove it */
501 	list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
502 		if (r_list->map == map) {
503 			master = r_list->master;
504 			list_del(&r_list->head);
505 			drm_ht_remove_key(&dev->map_hash,
506 					  r_list->user_token >> PAGE_SHIFT);
507 			kfree(r_list);
508 			found = 1;
509 			break;
510 		}
511 	}
512 
513 	if (!found)
514 		return -EINVAL;
515 
516 	switch (map->type) {
517 	case _DRM_REGISTERS:
518 		iounmap(map->handle);
519 		/* FALLTHROUGH */
520 	case _DRM_FRAME_BUFFER:
521 		arch_phys_wc_del(map->mtrr);
522 		break;
523 	case _DRM_SHM:
524 		vfree(map->handle);
525 		if (master) {
526 			if (dev->sigdata.lock == master->lock.hw_lock)
527 				dev->sigdata.lock = NULL;
528 			master->lock.hw_lock = NULL;   /* SHM removed */
529 			master->lock.file_priv = NULL;
530 			wake_up_interruptible_all(&master->lock.lock_queue);
531 		}
532 		break;
533 	case _DRM_AGP:
534 	case _DRM_SCATTER_GATHER:
535 		break;
536 	case _DRM_CONSISTENT:
537 		dmah.vaddr = map->handle;
538 		dmah.busaddr = map->offset;
539 		dmah.size = map->size;
540 		__drm_legacy_pci_free(dev, &dmah);
541 		break;
542 	}
543 	kfree(map);
544 
545 	return 0;
546 }
547 EXPORT_SYMBOL(drm_legacy_rmmap_locked);
548 
549 void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
550 {
551 	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
552 	    drm_core_check_feature(dev, DRIVER_MODESET))
553 		return;
554 
555 	mutex_lock(&dev->struct_mutex);
556 	drm_legacy_rmmap_locked(dev, map);
557 	mutex_unlock(&dev->struct_mutex);
558 }
559 EXPORT_SYMBOL(drm_legacy_rmmap);
560 
561 #if 0
562 void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
563 {
564 	struct drm_map_list *r_list, *list_temp;
565 
566 	if (drm_core_check_feature(dev, DRIVER_MODESET))
567 		return;
568 
569 	mutex_lock(&dev->struct_mutex);
570 	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
571 		if (r_list->master == master) {
572 			drm_legacy_rmmap_locked(dev, r_list->map);
573 			r_list = NULL;
574 		}
575 	}
576 	mutex_unlock(&dev->struct_mutex);
577 }
578 #endif
579 
580 /* The rmmap ioctl appears to be unnecessary.  All mappings are torn down on
581  * the last close of the device, and this is necessary for cleanup when things
582  * exit uncleanly.  Therefore, having userland manually remove mappings seems
583  * like a pointless exercise since they're going away anyway.
584  *
585  * One use case might be after addmap is allowed for normal users for SHM and
586  * gets used by drivers that the server doesn't need to care about.  This seems
587  * unlikely.
588  *
589  * \param inode device inode.
590  * \param file_priv DRM file private.
591  * \param cmd command.
592  * \param arg pointer to a struct drm_map structure.
593  * \return zero on success or a negative value on error.
594  */
595 int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
596 			   struct drm_file *file_priv)
597 {
598 	struct drm_map *request = data;
599 	struct drm_local_map *map = NULL;
600 	struct drm_map_list *r_list;
601 	int ret;
602 
603 	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
604 	    drm_core_check_feature(dev, DRIVER_MODESET))
605 		return -EINVAL;
606 
607 	mutex_lock(&dev->struct_mutex);
608 	list_for_each_entry(r_list, &dev->maplist, head) {
609 		if (r_list->map &&
610 		    r_list->user_token == (unsigned long)request->handle &&
611 		    r_list->map->flags & _DRM_REMOVABLE) {
612 			map = r_list->map;
613 			break;
614 		}
615 	}
616 
617 	/* List has wrapped around to the head pointer, or its empty we didn't
618 	 * find anything.
619 	 */
620 	if (list_empty(&dev->maplist) || !map) {
621 		mutex_unlock(&dev->struct_mutex);
622 		return -EINVAL;
623 	}
624 
625 	/* Register and framebuffer maps are permanent */
626 	if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
627 		mutex_unlock(&dev->struct_mutex);
628 		return 0;
629 	}
630 
631 	ret = drm_legacy_rmmap_locked(dev, map);
632 
633 	mutex_unlock(&dev->struct_mutex);
634 
635 	return ret;
636 }
637 
638 /**
639  * Cleanup after an error on one of the addbufs() functions.
640  *
641  * \param dev DRM device.
642  * \param entry buffer entry where the error occurred.
643  *
644  * Frees any pages and buffers associated with the given entry.
645  */
646 static void drm_cleanup_buf_error(struct drm_device * dev,
647 				  struct drm_buf_entry * entry)
648 {
649 	int i;
650 
651 	if (entry->seg_count) {
652 		for (i = 0; i < entry->seg_count; i++) {
653 			if (entry->seglist[i]) {
654 				drm_pci_free(dev, entry->seglist[i]);
655 			}
656 		}
657 		kfree(entry->seglist);
658 
659 		entry->seg_count = 0;
660 	}
661 
662 	if (entry->buf_count) {
663 		for (i = 0; i < entry->buf_count; i++) {
664 			kfree(entry->buflist[i].dev_private);
665 		}
666 		kfree(entry->buflist);
667 
668 		entry->buf_count = 0;
669 	}
670 }
671 
672 #if IS_ENABLED(CONFIG_AGP)
673 /**
674  * Add AGP buffers for DMA transfers.
675  *
676  * \param dev struct drm_device to which the buffers are to be added.
677  * \param request pointer to a struct drm_buf_desc describing the request.
678  * \return zero on success or a negative number on failure.
679  *
680  * After some sanity checks creates a drm_buf structure for each buffer and
681  * reallocates the buffer list of the same size order to accommodate the new
682  * buffers.
683  */
684 int drm_legacy_addbufs_agp(struct drm_device *dev,
685 			   struct drm_buf_desc *request)
686 {
687 	struct drm_device_dma *dma = dev->dma;
688 	struct drm_buf_entry *entry;
689 	/* struct drm_agp_mem *agp_entry; */
690 	struct drm_buf *buf;
691 	unsigned long offset;
692 	unsigned long agp_offset;
693 	int count;
694 	int order;
695 	int size;
696 	int alignment;
697 	int page_order;
698 	int total;
699 	int byte_count;
700 #if 0
701 	int i, valid;
702 #else
703 	int i;
704 #endif
705 
706 	struct drm_buf **temp_buflist;
707 
708 	if (!dma)
709 		return -EINVAL;
710 
711 	count = request->count;
712 	order = order_base_2(request->size);
713 	size = 1 << order;
714 
715 	alignment = (request->flags & _DRM_PAGE_ALIGN)
716 	    ? PAGE_ALIGN(size) : size;
717 	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
718 	total = PAGE_SIZE << page_order;
719 
720 	byte_count = 0;
721 	agp_offset = dev->agp->base + request->agp_start;
722 
723 	DRM_DEBUG("count:      %d\n", count);
724 	DRM_DEBUG("order:      %d\n", order);
725 	DRM_DEBUG("size:       %d\n", size);
726 	DRM_DEBUG("agp_offset: %lx\n", agp_offset);
727 	DRM_DEBUG("alignment:  %d\n", alignment);
728 	DRM_DEBUG("page_order: %d\n", page_order);
729 	DRM_DEBUG("total:      %d\n", total);
730 
731 	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
732 		return -EINVAL;
733 
734 	/* Make sure buffers are located in AGP memory that we own */
735 #if 0
736 	valid = 0;
737 	list_for_each_entry(agp_entry, &dev->agp->memory, head) {
738 		if ((agp_offset >= agp_entry->bound) &&
739 		    (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
740 			valid = 1;
741 			break;
742 		}
743 	}
744 	if (!list_empty(&dev->agp->memory) && !valid) {
745 		DRM_DEBUG("zone invalid\n");
746 		return -EINVAL;
747 	}
748 #endif
749 	spin_lock(&dev->buf_lock);
750 	if (dev->buf_use) {
751 		spin_unlock(&dev->buf_lock);
752 		return -EBUSY;
753 	}
754 	atomic_inc(&dev->buf_alloc);
755 	spin_unlock(&dev->buf_lock);
756 
757 	mutex_lock(&dev->struct_mutex);
758 	entry = &dma->bufs[order];
759 	if (entry->buf_count) {
760 		mutex_unlock(&dev->struct_mutex);
761 		atomic_dec(&dev->buf_alloc);
762 		return -ENOMEM;	/* May only call once for each order */
763 	}
764 
765 	if (count < 0 || count > 4096) {
766 		mutex_unlock(&dev->struct_mutex);
767 		atomic_dec(&dev->buf_alloc);
768 		return -EINVAL;
769 	}
770 
771 	entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
772 	if (!entry->buflist) {
773 		mutex_unlock(&dev->struct_mutex);
774 		atomic_dec(&dev->buf_alloc);
775 		return -ENOMEM;
776 	}
777 
778 	entry->buf_size = size;
779 	entry->page_order = page_order;
780 
781 	offset = 0;
782 
783 	while (entry->buf_count < count) {
784 		buf = &entry->buflist[entry->buf_count];
785 		buf->idx = dma->buf_count + entry->buf_count;
786 		buf->total = alignment;
787 		buf->order = order;
788 		buf->used = 0;
789 
790 		buf->offset = (dma->byte_count + offset);
791 		buf->bus_address = agp_offset + offset;
792 		buf->address = (void *)(agp_offset + offset);
793 		buf->next = NULL;
794 		buf->waiting = 0;
795 		buf->pending = 0;
796 		buf->file_priv = NULL;
797 
798 		buf->dev_priv_size = dev->driver->dev_priv_size;
799 		buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
800 		if (!buf->dev_private) {
801 			/* Set count correctly so we free the proper amount. */
802 			entry->buf_count = count;
803 			drm_cleanup_buf_error(dev, entry);
804 			mutex_unlock(&dev->struct_mutex);
805 			atomic_dec(&dev->buf_alloc);
806 			return -ENOMEM;
807 		}
808 
809 		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
810 
811 		offset += alignment;
812 		entry->buf_count++;
813 		byte_count += PAGE_SIZE << page_order;
814 	}
815 
816 	DRM_DEBUG("byte_count: %d\n", byte_count);
817 
818 	temp_buflist = krealloc(dma->buflist,
819 				(dma->buf_count + entry->buf_count) *
820 				sizeof(*dma->buflist), M_DRM, M_WAITOK);
821 	if (!temp_buflist) {
822 		/* Free the entry because it isn't valid */
823 		drm_cleanup_buf_error(dev, entry);
824 		mutex_unlock(&dev->struct_mutex);
825 		atomic_dec(&dev->buf_alloc);
826 		return -ENOMEM;
827 	}
828 	dma->buflist = temp_buflist;
829 
830 	for (i = 0; i < entry->buf_count; i++) {
831 		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
832 	}
833 
834 	dma->buf_count += entry->buf_count;
835 	dma->seg_count += entry->seg_count;
836 	dma->page_count += byte_count >> PAGE_SHIFT;
837 	dma->byte_count += byte_count;
838 
839 	DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
840 	DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
841 
842 	mutex_unlock(&dev->struct_mutex);
843 
844 	request->count = entry->buf_count;
845 	request->size = size;
846 
847 	dma->flags = _DRM_DMA_USE_AGP;
848 
849 	atomic_dec(&dev->buf_alloc);
850 	return 0;
851 }
852 EXPORT_SYMBOL(drm_legacy_addbufs_agp);
853 #endif /* CONFIG_AGP */
854 
855 int drm_legacy_addbufs_pci(struct drm_device *dev,
856 			   struct drm_buf_desc *request)
857 {
858 	struct drm_device_dma *dma = dev->dma;
859 	int count;
860 	int order;
861 	int size;
862 	int total;
863 	int page_order;
864 	struct drm_buf_entry *entry;
865 	drm_dma_handle_t *dmah;
866 	struct drm_buf *buf;
867 	int alignment;
868 	unsigned long offset;
869 	int i;
870 	int byte_count;
871 	int page_count;
872 	unsigned long *temp_pagelist;
873 	struct drm_buf **temp_buflist;
874 
875 	if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
876 		return -EINVAL;
877 
878 	if (!dma)
879 		return -EINVAL;
880 
881 	if (!capable(CAP_SYS_ADMIN))
882 		return -EPERM;
883 
884 	count = request->count;
885 	order = order_base_2(request->size);
886 	size = 1 << order;
887 
888 	DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
889 		  request->count, request->size, size, order);
890 
891 	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
892 		return -EINVAL;
893 
894 	alignment = (request->flags & _DRM_PAGE_ALIGN)
895 	    ? PAGE_ALIGN(size) : size;
896 	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
897 	total = PAGE_SIZE << page_order;
898 
899 	spin_lock(&dev->buf_lock);
900 	if (dev->buf_use) {
901 		spin_unlock(&dev->buf_lock);
902 		return -EBUSY;
903 	}
904 	atomic_inc(&dev->buf_alloc);
905 	spin_unlock(&dev->buf_lock);
906 
907 	mutex_lock(&dev->struct_mutex);
908 	entry = &dma->bufs[order];
909 	if (entry->buf_count) {
910 		mutex_unlock(&dev->struct_mutex);
911 		atomic_dec(&dev->buf_alloc);
912 		return -ENOMEM;	/* May only call once for each order */
913 	}
914 
915 	if (count < 0 || count > 4096) {
916 		mutex_unlock(&dev->struct_mutex);
917 		atomic_dec(&dev->buf_alloc);
918 		return -EINVAL;
919 	}
920 
921 	entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
922 	if (!entry->buflist) {
923 		mutex_unlock(&dev->struct_mutex);
924 		atomic_dec(&dev->buf_alloc);
925 		return -ENOMEM;
926 	}
927 
928 	entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
929 	if (!entry->seglist) {
930 		kfree(entry->buflist);
931 		mutex_unlock(&dev->struct_mutex);
932 		atomic_dec(&dev->buf_alloc);
933 		return -ENOMEM;
934 	}
935 
936 	/* Keep the original pagelist until we know all the allocations
937 	 * have succeeded
938 	 */
939 	temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
940 			       sizeof(*dma->pagelist), M_DRM, M_WAITOK );
941 	if (!temp_pagelist) {
942 		kfree(entry->buflist);
943 		kfree(entry->seglist);
944 		mutex_unlock(&dev->struct_mutex);
945 		atomic_dec(&dev->buf_alloc);
946 		return -ENOMEM;
947 	}
948 	memcpy(temp_pagelist,
949 	       dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
950 	DRM_DEBUG("pagelist: %d entries\n",
951 		  dma->page_count + (count << page_order));
952 
953 	entry->buf_size = size;
954 	entry->page_order = page_order;
955 	byte_count = 0;
956 	page_count = 0;
957 
958 	while (entry->buf_count < count) {
959 
960 		dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
961 
962 		if (!dmah) {
963 			/* Set count correctly so we free the proper amount. */
964 			entry->buf_count = count;
965 			entry->seg_count = count;
966 			drm_cleanup_buf_error(dev, entry);
967 			kfree(temp_pagelist);
968 			mutex_unlock(&dev->struct_mutex);
969 			atomic_dec(&dev->buf_alloc);
970 			return -ENOMEM;
971 		}
972 		entry->seglist[entry->seg_count++] = dmah;
973 		for (i = 0; i < (1 << page_order); i++) {
974 			DRM_DEBUG("page %d @ 0x%08lx\n",
975 				  dma->page_count + page_count,
976 				  (unsigned long)dmah->vaddr + PAGE_SIZE * i);
977 			temp_pagelist[dma->page_count + page_count++]
978 				= (unsigned long)dmah->vaddr + PAGE_SIZE * i;
979 		}
980 		for (offset = 0;
981 		     offset + size <= total && entry->buf_count < count;
982 		     offset += alignment, ++entry->buf_count) {
983 			buf = &entry->buflist[entry->buf_count];
984 			buf->idx = dma->buf_count + entry->buf_count;
985 			buf->total = alignment;
986 			buf->order = order;
987 			buf->used = 0;
988 			buf->offset = (dma->byte_count + byte_count + offset);
989 			buf->address = ((char *)dmah->vaddr + offset);
990 			buf->bus_address = dmah->busaddr + offset;
991 			buf->next = NULL;
992 			buf->waiting = 0;
993 			buf->pending = 0;
994 			buf->file_priv = NULL;
995 
996 			buf->dev_priv_size = dev->driver->dev_priv_size;
997 			buf->dev_private = kzalloc(buf->dev_priv_size,
998 						GFP_KERNEL);
999 			if (!buf->dev_private) {
1000 				/* Set count correctly so we free the proper amount. */
1001 				entry->buf_count = count;
1002 				entry->seg_count = count;
1003 				drm_cleanup_buf_error(dev, entry);
1004 				kfree(temp_pagelist);
1005 				mutex_unlock(&dev->struct_mutex);
1006 				atomic_dec(&dev->buf_alloc);
1007 				return -ENOMEM;
1008 			}
1009 
1010 			DRM_DEBUG("buffer %d @ %p\n",
1011 				  entry->buf_count, buf->address);
1012 		}
1013 		byte_count += PAGE_SIZE << page_order;
1014 	}
1015 
1016 	temp_buflist = krealloc(dma->buflist,
1017 				(dma->buf_count + entry->buf_count) *
1018 				sizeof(*dma->buflist), M_DRM, M_WAITOK);
1019 	if (!temp_buflist) {
1020 		/* Free the entry because it isn't valid */
1021 		drm_cleanup_buf_error(dev, entry);
1022 		kfree(temp_pagelist);
1023 		mutex_unlock(&dev->struct_mutex);
1024 		atomic_dec(&dev->buf_alloc);
1025 		return -ENOMEM;
1026 	}
1027 	dma->buflist = temp_buflist;
1028 
1029 	for (i = 0; i < entry->buf_count; i++) {
1030 		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1031 	}
1032 
1033 	/* No allocations failed, so now we can replace the original pagelist
1034 	 * with the new one.
1035 	 */
1036 	if (dma->page_count) {
1037 		kfree(dma->pagelist);
1038 	}
1039 	dma->pagelist = temp_pagelist;
1040 
1041 	dma->buf_count += entry->buf_count;
1042 	dma->seg_count += entry->seg_count;
1043 	dma->page_count += entry->seg_count << page_order;
1044 	dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1045 
1046 	mutex_unlock(&dev->struct_mutex);
1047 
1048 	request->count = entry->buf_count;
1049 	request->size = size;
1050 
1051 	if (request->flags & _DRM_PCI_BUFFER_RO)
1052 		dma->flags = _DRM_DMA_USE_PCI_RO;
1053 
1054 	atomic_dec(&dev->buf_alloc);
1055 	return 0;
1056 
1057 }
1058 EXPORT_SYMBOL(drm_legacy_addbufs_pci);
1059 
1060 static int drm_legacy_addbufs_sg(struct drm_device *dev,
1061 				 struct drm_buf_desc *request)
1062 {
1063 	struct drm_device_dma *dma = dev->dma;
1064 	struct drm_buf_entry *entry;
1065 	struct drm_buf *buf;
1066 	unsigned long offset;
1067 	unsigned long agp_offset;
1068 	int count;
1069 	int order;
1070 	int size;
1071 	int alignment;
1072 	int page_order;
1073 	int total;
1074 	int byte_count;
1075 	int i;
1076 	struct drm_buf **temp_buflist;
1077 
1078 	if (!drm_core_check_feature(dev, DRIVER_SG))
1079 		return -EINVAL;
1080 
1081 	if (!dma)
1082 		return -EINVAL;
1083 
1084 	if (!capable(CAP_SYS_ADMIN))
1085 		return -EPERM;
1086 
1087 	count = request->count;
1088 	order = order_base_2(request->size);
1089 	size = 1 << order;
1090 
1091 	alignment = (request->flags & _DRM_PAGE_ALIGN)
1092 	    ? PAGE_ALIGN(size) : size;
1093 	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1094 	total = PAGE_SIZE << page_order;
1095 
1096 	byte_count = 0;
1097 	agp_offset = request->agp_start;
1098 
1099 	DRM_DEBUG("count:      %d\n", count);
1100 	DRM_DEBUG("order:      %d\n", order);
1101 	DRM_DEBUG("size:       %d\n", size);
1102 	DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1103 	DRM_DEBUG("alignment:  %d\n", alignment);
1104 	DRM_DEBUG("page_order: %d\n", page_order);
1105 	DRM_DEBUG("total:      %d\n", total);
1106 
1107 	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1108 		return -EINVAL;
1109 
1110 	spin_lock(&dev->buf_lock);
1111 	if (dev->buf_use) {
1112 		spin_unlock(&dev->buf_lock);
1113 		return -EBUSY;
1114 	}
1115 	atomic_inc(&dev->buf_alloc);
1116 	spin_unlock(&dev->buf_lock);
1117 
1118 	mutex_lock(&dev->struct_mutex);
1119 	entry = &dma->bufs[order];
1120 	if (entry->buf_count) {
1121 		mutex_unlock(&dev->struct_mutex);
1122 		atomic_dec(&dev->buf_alloc);
1123 		return -ENOMEM;	/* May only call once for each order */
1124 	}
1125 
1126 	if (count < 0 || count > 4096) {
1127 		mutex_unlock(&dev->struct_mutex);
1128 		atomic_dec(&dev->buf_alloc);
1129 		return -EINVAL;
1130 	}
1131 
1132 	entry->buflist = kzalloc(count * sizeof(*entry->buflist),
1133 				GFP_KERNEL);
1134 	if (!entry->buflist) {
1135 		mutex_unlock(&dev->struct_mutex);
1136 		atomic_dec(&dev->buf_alloc);
1137 		return -ENOMEM;
1138 	}
1139 
1140 	entry->buf_size = size;
1141 	entry->page_order = page_order;
1142 
1143 	offset = 0;
1144 
1145 	while (entry->buf_count < count) {
1146 		buf = &entry->buflist[entry->buf_count];
1147 		buf->idx = dma->buf_count + entry->buf_count;
1148 		buf->total = alignment;
1149 		buf->order = order;
1150 		buf->used = 0;
1151 
1152 		buf->offset = (dma->byte_count + offset);
1153 		buf->bus_address = agp_offset + offset;
1154 		buf->address = (void *)(agp_offset + offset + dev->sg->vaddr);
1155 		buf->next = NULL;
1156 		buf->waiting = 0;
1157 		buf->pending = 0;
1158 		buf->file_priv = NULL;
1159 
1160 		buf->dev_priv_size = dev->driver->dev_priv_size;
1161 		buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
1162 		if (!buf->dev_private) {
1163 			/* Set count correctly so we free the proper amount. */
1164 			entry->buf_count = count;
1165 			drm_cleanup_buf_error(dev, entry);
1166 			mutex_unlock(&dev->struct_mutex);
1167 			atomic_dec(&dev->buf_alloc);
1168 			return -ENOMEM;
1169 		}
1170 
1171 		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1172 
1173 		offset += alignment;
1174 		entry->buf_count++;
1175 		byte_count += PAGE_SIZE << page_order;
1176 	}
1177 
1178 	DRM_DEBUG("byte_count: %d\n", byte_count);
1179 
1180 	temp_buflist = krealloc(dma->buflist,
1181 				(dma->buf_count + entry->buf_count) *
1182 				sizeof(*dma->buflist), M_DRM, M_WAITOK);
1183 	if (!temp_buflist) {
1184 		/* Free the entry because it isn't valid */
1185 		drm_cleanup_buf_error(dev, entry);
1186 		mutex_unlock(&dev->struct_mutex);
1187 		atomic_dec(&dev->buf_alloc);
1188 		return -ENOMEM;
1189 	}
1190 	dma->buflist = temp_buflist;
1191 
1192 	for (i = 0; i < entry->buf_count; i++) {
1193 		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1194 	}
1195 
1196 	dma->buf_count += entry->buf_count;
1197 	dma->seg_count += entry->seg_count;
1198 	dma->page_count += byte_count >> PAGE_SHIFT;
1199 	dma->byte_count += byte_count;
1200 
1201 	DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1202 	DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1203 
1204 	mutex_unlock(&dev->struct_mutex);
1205 
1206 	request->count = entry->buf_count;
1207 	request->size = size;
1208 
1209 	dma->flags = _DRM_DMA_USE_SG;
1210 
1211 	atomic_dec(&dev->buf_alloc);
1212 	return 0;
1213 }
1214 
1215 /**
1216  * Add buffers for DMA transfers (ioctl).
1217  *
1218  * \param inode device inode.
1219  * \param file_priv DRM file private.
1220  * \param cmd command.
1221  * \param arg pointer to a struct drm_buf_desc request.
1222  * \return zero on success or a negative number on failure.
1223  *
1224  * According with the memory type specified in drm_buf_desc::flags and the
1225  * build options, it dispatches the call either to addbufs_agp(),
1226  * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1227  * PCI memory respectively.
1228  */
1229 int drm_legacy_addbufs(struct drm_device *dev, void *data,
1230 		       struct drm_file *file_priv)
1231 {
1232 	struct drm_buf_desc *request = data;
1233 	int ret;
1234 
1235 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1236 		return -EINVAL;
1237 
1238 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1239 		return -EINVAL;
1240 
1241 #if IS_ENABLED(CONFIG_AGP)
1242 	if (request->flags & _DRM_AGP_BUFFER)
1243 		ret = drm_legacy_addbufs_agp(dev, request);
1244 	else
1245 #endif
1246 	if (request->flags & _DRM_SG_BUFFER)
1247 		ret = drm_legacy_addbufs_sg(dev, request);
1248 	else if (request->flags & _DRM_FB_BUFFER)
1249 		ret = -EINVAL;
1250 	else
1251 		ret = drm_legacy_addbufs_pci(dev, request);
1252 
1253 	return ret;
1254 }
1255 
1256 /**
1257  * Get information about the buffer mappings.
1258  *
1259  * This was originally mean for debugging purposes, or by a sophisticated
1260  * client library to determine how best to use the available buffers (e.g.,
1261  * large buffers can be used for image transfer).
1262  *
1263  * \param inode device inode.
1264  * \param file_priv DRM file private.
1265  * \param cmd command.
1266  * \param arg pointer to a drm_buf_info structure.
1267  * \return zero on success or a negative number on failure.
1268  *
1269  * Increments drm_device::buf_use while holding the drm_device::buf_lock
1270  * lock, preventing of allocating more buffers after this call. Information
1271  * about each requested buffer is then copied into user space.
1272  */
1273 int drm_legacy_infobufs(struct drm_device *dev, void *data,
1274 			struct drm_file *file_priv)
1275 {
1276 	struct drm_device_dma *dma = dev->dma;
1277 	struct drm_buf_info *request = data;
1278 	int i;
1279 	int count;
1280 
1281 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1282 		return -EINVAL;
1283 
1284 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1285 		return -EINVAL;
1286 
1287 	if (!dma)
1288 		return -EINVAL;
1289 
1290 	spin_lock(&dev->buf_lock);
1291 	if (atomic_read(&dev->buf_alloc)) {
1292 		spin_unlock(&dev->buf_lock);
1293 		return -EBUSY;
1294 	}
1295 	++dev->buf_use;		/* Can't allocate more after this call */
1296 	spin_unlock(&dev->buf_lock);
1297 
1298 	for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1299 		if (dma->bufs[i].buf_count)
1300 			++count;
1301 	}
1302 
1303 	DRM_DEBUG("count = %d\n", count);
1304 
1305 	if (request->count >= count) {
1306 		for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1307 			if (dma->bufs[i].buf_count) {
1308 				struct drm_buf_desc __user *to =
1309 				    &request->list[count];
1310 				struct drm_buf_entry *from = &dma->bufs[i];
1311 				if (copy_to_user(&to->count,
1312 						 &from->buf_count,
1313 						 sizeof(from->buf_count)) ||
1314 				    copy_to_user(&to->size,
1315 						 &from->buf_size,
1316 						 sizeof(from->buf_size)) ||
1317 				    copy_to_user(&to->low_mark,
1318 						 &from->low_mark,
1319 						 sizeof(from->low_mark)) ||
1320 				    copy_to_user(&to->high_mark,
1321 						 &from->high_mark,
1322 						 sizeof(from->high_mark)))
1323 					return -EFAULT;
1324 
1325 				DRM_DEBUG("%d %d %d %d %d\n",
1326 					  i,
1327 					  dma->bufs[i].buf_count,
1328 					  dma->bufs[i].buf_size,
1329 					  dma->bufs[i].low_mark,
1330 					  dma->bufs[i].high_mark);
1331 				++count;
1332 			}
1333 		}
1334 	}
1335 	request->count = count;
1336 
1337 	return 0;
1338 }
1339 
1340 /**
1341  * Specifies a low and high water mark for buffer allocation
1342  *
1343  * \param inode device inode.
1344  * \param file_priv DRM file private.
1345  * \param cmd command.
1346  * \param arg a pointer to a drm_buf_desc structure.
1347  * \return zero on success or a negative number on failure.
1348  *
1349  * Verifies that the size order is bounded between the admissible orders and
1350  * updates the respective drm_device_dma::bufs entry low and high water mark.
1351  *
1352  * \note This ioctl is deprecated and mostly never used.
1353  */
1354 int drm_legacy_markbufs(struct drm_device *dev, void *data,
1355 			struct drm_file *file_priv)
1356 {
1357 	struct drm_device_dma *dma = dev->dma;
1358 	struct drm_buf_desc *request = data;
1359 	int order;
1360 	struct drm_buf_entry *entry;
1361 
1362 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1363 		return -EINVAL;
1364 
1365 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1366 		return -EINVAL;
1367 
1368 	if (!dma)
1369 		return -EINVAL;
1370 
1371 	DRM_DEBUG("%d, %d, %d\n",
1372 		  request->size, request->low_mark, request->high_mark);
1373 	order = order_base_2(request->size);
1374 	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1375 		return -EINVAL;
1376 	entry = &dma->bufs[order];
1377 
1378 	if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1379 		return -EINVAL;
1380 	if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1381 		return -EINVAL;
1382 
1383 	entry->low_mark = request->low_mark;
1384 	entry->high_mark = request->high_mark;
1385 
1386 	return 0;
1387 }
1388 
1389 /**
1390  * Unreserve the buffers in list, previously reserved using drmDMA.
1391  *
1392  * \param inode device inode.
1393  * \param file_priv DRM file private.
1394  * \param cmd command.
1395  * \param arg pointer to a drm_buf_free structure.
1396  * \return zero on success or a negative number on failure.
1397  *
1398  * Calls free_buffer() for each used buffer.
1399  * This function is primarily used for debugging.
1400  */
1401 int drm_legacy_freebufs(struct drm_device *dev, void *data,
1402 			struct drm_file *file_priv)
1403 {
1404 	struct drm_device_dma *dma = dev->dma;
1405 	struct drm_buf_free *request = data;
1406 	int i;
1407 	int idx;
1408 	struct drm_buf *buf;
1409 
1410 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1411 		return -EINVAL;
1412 
1413 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1414 		return -EINVAL;
1415 
1416 	if (!dma)
1417 		return -EINVAL;
1418 
1419 	DRM_DEBUG("%d\n", request->count);
1420 	for (i = 0; i < request->count; i++) {
1421 		if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1422 			return -EFAULT;
1423 		if (idx < 0 || idx >= dma->buf_count) {
1424 			DRM_ERROR("Index %d (of %d max)\n",
1425 				  idx, dma->buf_count - 1);
1426 			return -EINVAL;
1427 		}
1428 		buf = dma->buflist[idx];
1429 		if (buf->file_priv != file_priv) {
1430 			DRM_ERROR("Process %d freeing buffer not owned\n",
1431 				  DRM_CURRENTPID);
1432 			return -EINVAL;
1433 		}
1434 		drm_legacy_free_buffer(dev, buf);
1435 	}
1436 
1437 	return 0;
1438 }
1439 
1440 /**
1441  * Maps all of the DMA buffers into client-virtual space (ioctl).
1442  *
1443  * \param inode device inode.
1444  * \param file_priv DRM file private.
1445  * \param cmd command.
1446  * \param arg pointer to a drm_buf_map structure.
1447  * \return zero on success or a negative number on failure.
1448  *
1449  * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1450  * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1451  * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1452  * drm_mmap_dma().
1453  */
1454 int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1455 		       struct drm_file *file_priv)
1456 {
1457 	struct drm_device_dma *dma = dev->dma;
1458 	int retcode = 0;
1459 	const int zero = 0;
1460 	unsigned long virtual;
1461 	vm_offset_t address;
1462 	struct vmspace *vms  = DRM_CURPROC->td_proc->p_vmspace;
1463 	struct drm_buf_map *request = data;
1464 	int i;
1465 
1466 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1467 		return -EINVAL;
1468 
1469 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1470 		return -EINVAL;
1471 
1472 	if (!dma)
1473 		return -EINVAL;
1474 
1475 	spin_lock(&dev->buf_lock);
1476 	if (atomic_read(&dev->buf_alloc)) {
1477 		spin_unlock(&dev->buf_lock);
1478 		return -EBUSY;
1479 	}
1480 	dev->buf_use++;		/* Can't allocate more after this call */
1481 	spin_unlock(&dev->buf_lock);
1482 
1483 	if (request->count >= dma->buf_count) {
1484 		if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
1485 		    || (drm_core_check_feature(dev, DRIVER_SG)
1486 			&& (dma->flags & _DRM_DMA_USE_SG))) {
1487 			struct drm_local_map *map = dev->agp_buffer_map;
1488 			unsigned long token = dev->agp_buffer_token;
1489 
1490 			if (!map) {
1491 				retcode = -EINVAL;
1492 				goto done;
1493 			}
1494 			virtual = vm_mmap(&vms->vm_map, 0, map->size,
1495 					  PROT_READ | PROT_WRITE,
1496 					  VM_PROT_ALL,
1497 					  MAP_SHARED,
1498 					  SLIST_FIRST(&dev->devnode->si_hlist),
1499 					  token);
1500 		} else {
1501 			virtual = vm_mmap(&vms->vm_map, 0, dma->byte_count,
1502 					  PROT_READ | PROT_WRITE,
1503 					  VM_PROT_ALL,
1504 					  MAP_SHARED,
1505 					  SLIST_FIRST(&dev->devnode->si_hlist),
1506 					  0);
1507 		}
1508 		if (virtual > -1024UL) {
1509 			/* Real error */
1510 			retcode = (signed long)virtual;
1511 			goto done;
1512 		}
1513 		request->virtual = (void __user *)virtual;
1514 
1515 		for (i = 0; i < dma->buf_count; i++) {
1516 			if (copy_to_user(&request->list[i].idx,
1517 					 &dma->buflist[i]->idx,
1518 					 sizeof(request->list[0].idx))) {
1519 				retcode = -EFAULT;
1520 				goto done;
1521 			}
1522 			if (copy_to_user(&request->list[i].total,
1523 					 &dma->buflist[i]->total,
1524 					 sizeof(request->list[0].total))) {
1525 				retcode = -EFAULT;
1526 				goto done;
1527 			}
1528 			if (copy_to_user(&request->list[i].used,
1529 					 &zero, sizeof(zero))) {
1530 				retcode = -EFAULT;
1531 				goto done;
1532 			}
1533 			address = virtual + dma->buflist[i]->offset;	/* *** */
1534 			if (copy_to_user(&request->list[i].address,
1535 					 &address, sizeof(address))) {
1536 				retcode = -EFAULT;
1537 				goto done;
1538 			}
1539 		}
1540 	}
1541       done:
1542 	request->count = dma->buf_count;
1543 	DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1544 
1545 	return retcode;
1546 }
1547 
1548 int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
1549 		  struct drm_file *file_priv)
1550 {
1551 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1552 		return -EINVAL;
1553 
1554 	if (dev->driver->dma_ioctl)
1555 		return dev->driver->dma_ioctl(dev, data, file_priv);
1556 	else
1557 		return -EINVAL;
1558 }
1559 
1560 struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
1561 {
1562 	struct drm_map_list *entry;
1563 
1564 	list_for_each_entry(entry, &dev->maplist, head) {
1565 		if (entry->map && entry->map->type == _DRM_SHM &&
1566 		    (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1567 			return entry->map;
1568 		}
1569 	}
1570 	return NULL;
1571 }
1572 EXPORT_SYMBOL(drm_legacy_getsarea);
1573