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