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