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