xref: /dflybsd-src/sys/dev/drm/drm_bufs.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1df4baf3dSFrançois Tigeot /*
21b13d190SFrançois Tigeot  * Legacy: Generic DRM Buffer Management
3df4baf3dSFrançois Tigeot  *
47f3c3d6fSHasso Tepper  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
57f3c3d6fSHasso Tepper  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
67f3c3d6fSHasso Tepper  * All Rights Reserved.
77f3c3d6fSHasso Tepper  *
81b13d190SFrançois Tigeot  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
91b13d190SFrançois Tigeot  * Author: Gareth Hughes <gareth@valinux.com>
101b13d190SFrançois Tigeot  *
117f3c3d6fSHasso Tepper  * Permission is hereby granted, free of charge, to any person obtaining a
127f3c3d6fSHasso Tepper  * copy of this software and associated documentation files (the "Software"),
137f3c3d6fSHasso Tepper  * to deal in the Software without restriction, including without limitation
147f3c3d6fSHasso Tepper  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
157f3c3d6fSHasso Tepper  * and/or sell copies of the Software, and to permit persons to whom the
167f3c3d6fSHasso Tepper  * Software is furnished to do so, subject to the following conditions:
177f3c3d6fSHasso Tepper  *
187f3c3d6fSHasso Tepper  * The above copyright notice and this permission notice (including the next
197f3c3d6fSHasso Tepper  * paragraph) shall be included in all copies or substantial portions of the
207f3c3d6fSHasso Tepper  * Software.
217f3c3d6fSHasso Tepper  *
227f3c3d6fSHasso Tepper  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237f3c3d6fSHasso Tepper  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247f3c3d6fSHasso Tepper  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
257f3c3d6fSHasso Tepper  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
267f3c3d6fSHasso Tepper  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
277f3c3d6fSHasso Tepper  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
287f3c3d6fSHasso Tepper  * OTHER DEALINGS IN THE SOFTWARE.
297f3c3d6fSHasso Tepper  */
307f3c3d6fSHasso Tepper 
3188fc0f68SFrançois Tigeot #include <linux/vmalloc.h>
32*3f2dd94aSFrançois Tigeot #include <linux/slab.h>
3388fc0f68SFrançois Tigeot #include <linux/log2.h>
34df4baf3dSFrançois Tigeot #include <linux/export.h>
3588fc0f68SFrançois Tigeot #include <asm/shmparam.h>
3618e26a6dSFrançois Tigeot #include <drm/drmP.h>
371b13d190SFrançois Tigeot #include "drm_legacy.h"
387f3c3d6fSHasso Tepper 
3909f141c4SFrançois Tigeot #include <sys/conf.h>
4062bba8f6SFrançois Tigeot #include <sys/mman.h>
4162bba8f6SFrançois Tigeot #include <vm/vm_map.h>
4262bba8f6SFrançois Tigeot 
drm_find_matching_map(struct drm_device * dev,struct drm_local_map * map)4388fc0f68SFrançois Tigeot static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
4488fc0f68SFrançois Tigeot 						  struct drm_local_map *map)
4588fc0f68SFrançois Tigeot {
4688fc0f68SFrançois Tigeot 	struct drm_map_list *entry;
4788fc0f68SFrançois Tigeot 	list_for_each_entry(entry, &dev->maplist, head) {
4888fc0f68SFrançois Tigeot 		/*
4988fc0f68SFrançois Tigeot 		 * Because the kernel-userspace ABI is fixed at a 32-bit offset
5088fc0f68SFrançois Tigeot 		 * while PCI resources may live above that, we only compare the
5188fc0f68SFrançois Tigeot 		 * lower 32 bits of the map offset for maps of type
5288fc0f68SFrançois Tigeot 		 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
5388fc0f68SFrançois Tigeot 		 * It is assumed that if a driver have more than one resource
5488fc0f68SFrançois Tigeot 		 * of each type, the lower 32 bits are different.
5588fc0f68SFrançois Tigeot 		 */
5688fc0f68SFrançois Tigeot 		if (!entry->map ||
5788fc0f68SFrançois Tigeot 		    map->type != entry->map->type ||
581dedbd3bSFrançois Tigeot 		    entry->master != dev->master)
5988fc0f68SFrançois Tigeot 			continue;
6088fc0f68SFrançois Tigeot 		switch (map->type) {
6188fc0f68SFrançois Tigeot 		case _DRM_SHM:
6288fc0f68SFrançois Tigeot 			if (map->flags != _DRM_CONTAINS_LOCK)
6388fc0f68SFrançois Tigeot 				break;
6488fc0f68SFrançois Tigeot 			return entry;
6588fc0f68SFrançois Tigeot 		case _DRM_REGISTERS:
6688fc0f68SFrançois Tigeot 		case _DRM_FRAME_BUFFER:
6788fc0f68SFrançois Tigeot 			if ((entry->map->offset & 0xffffffff) ==
6888fc0f68SFrançois Tigeot 			    (map->offset & 0xffffffff))
6988fc0f68SFrançois Tigeot 				return entry;
7088fc0f68SFrançois Tigeot 		default: /* Make gcc happy */
7188fc0f68SFrançois Tigeot 			;
7288fc0f68SFrançois Tigeot 		}
7388fc0f68SFrançois Tigeot 		if (entry->map->offset == map->offset)
7488fc0f68SFrançois Tigeot 			return entry;
7588fc0f68SFrançois Tigeot 	}
7688fc0f68SFrançois Tigeot 
7788fc0f68SFrançois Tigeot 	return NULL;
7888fc0f68SFrançois Tigeot }
7988fc0f68SFrançois Tigeot 
drm_map_handle(struct drm_device * dev,struct drm_hash_item * hash,unsigned long user_token,int hashed_handle,int shm)8088fc0f68SFrançois Tigeot static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
8188fc0f68SFrançois Tigeot 			  unsigned long user_token, int hashed_handle, int shm)
8288fc0f68SFrançois Tigeot {
83ef130734SFrançois Tigeot 	int use_hashed_handle, shift;
84ef130734SFrançois Tigeot 	unsigned long add;
85ef130734SFrançois Tigeot 
86ef130734SFrançois Tigeot #if (BITS_PER_LONG == 64)
87ef130734SFrançois Tigeot 	use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
88ef130734SFrançois Tigeot #elif (BITS_PER_LONG == 32)
89ef130734SFrançois Tigeot 	use_hashed_handle = hashed_handle;
90ef130734SFrançois Tigeot #else
91ef130734SFrançois Tigeot #error Unsupported long size. Neither 64 nor 32 bits.
9288fc0f68SFrançois Tigeot #endif
9388fc0f68SFrançois Tigeot 
94ef130734SFrançois Tigeot 	if (!use_hashed_handle) {
95ef130734SFrançois Tigeot 		int ret;
96ef130734SFrançois Tigeot 		hash->key = user_token >> PAGE_SHIFT;
97ef130734SFrançois Tigeot 		ret = drm_ht_insert_item(&dev->map_hash, hash);
98ef130734SFrançois Tigeot 		if (ret != -EINVAL)
99ef130734SFrançois Tigeot 			return ret;
100ef130734SFrançois Tigeot 	}
101ef130734SFrançois Tigeot 
102ef130734SFrançois Tigeot 	shift = 0;
103ef130734SFrançois Tigeot 	add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
104ef130734SFrançois Tigeot 	if (shm && (SHMLBA > PAGE_SIZE)) {
105ef130734SFrançois Tigeot 		int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
106ef130734SFrançois Tigeot 
107ef130734SFrançois Tigeot 		/* For shared memory, we have to preserve the SHMLBA
108ef130734SFrançois Tigeot 		 * bits of the eventual vma->vm_pgoff value during
109ef130734SFrançois Tigeot 		 * mmap().  Otherwise we run into cache aliasing problems
110ef130734SFrançois Tigeot 		 * on some platforms.  On these platforms, the pgoff of
111ef130734SFrançois Tigeot 		 * a mmap() request is used to pick a suitable virtual
112ef130734SFrançois Tigeot 		 * address for the mmap() region such that it will not
113ef130734SFrançois Tigeot 		 * cause cache aliasing problems.
114ef130734SFrançois Tigeot 		 *
115ef130734SFrançois Tigeot 		 * Therefore, make sure the SHMLBA relevant bits of the
116ef130734SFrançois Tigeot 		 * hash value we use are equal to those in the original
117ef130734SFrançois Tigeot 		 * kernel virtual address.
118ef130734SFrançois Tigeot 		 */
119ef130734SFrançois Tigeot 		shift = bits;
120ef130734SFrançois Tigeot 		add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
121ef130734SFrançois Tigeot 	}
122ef130734SFrançois Tigeot 
123ef130734SFrançois Tigeot 	return drm_ht_just_insert_please(&dev->map_hash, hash,
124ef130734SFrançois Tigeot 					 user_token, 32 - PAGE_SHIFT - 3,
125ef130734SFrançois Tigeot 					 shift, add);
126ef130734SFrançois Tigeot }
127ef130734SFrançois Tigeot 
12888fc0f68SFrançois Tigeot /**
12988fc0f68SFrançois Tigeot  * Core function to create a range of memory available for mapping by a
13088fc0f68SFrançois Tigeot  * non-root process.
13188fc0f68SFrançois Tigeot  *
13288fc0f68SFrançois Tigeot  * Adjusts the memory offset to its absolute value according to the mapping
13388fc0f68SFrançois Tigeot  * type.  Adds the map to the map list drm_device::maplist. Adds MTRR's where
13488fc0f68SFrançois Tigeot  * applicable and if supported by the kernel.
13588fc0f68SFrançois Tigeot  */
drm_addmap_core(struct drm_device * dev,resource_size_t offset,unsigned int size,enum drm_map_type type,enum drm_map_flags flags,struct drm_map_list ** maplist)13688fc0f68SFrançois Tigeot static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
137df4baf3dSFrançois Tigeot 			   unsigned int size, enum drm_map_type type,
13888fc0f68SFrançois Tigeot 			   enum drm_map_flags flags,
13988fc0f68SFrançois Tigeot 			   struct drm_map_list ** maplist)
1407f3c3d6fSHasso Tepper {
141f599cd46SFrançois Tigeot 	struct drm_local_map *map;
142ef130734SFrançois Tigeot 	struct drm_map_list *list;
1435c002123SFrançois Tigeot 	drm_dma_handle_t *dmah;
144ef130734SFrançois Tigeot 	unsigned long user_token;
145ef130734SFrançois Tigeot 	int ret;
14679d1f0c0SFrançois Tigeot 
147*3f2dd94aSFrançois Tigeot 	map = kmalloc(sizeof(*map), M_DRM, GFP_KERNEL);
14888fc0f68SFrançois Tigeot 	if (!map)
149b922632fSImre Vadász 		return -ENOMEM;
15079d1f0c0SFrançois Tigeot 
15179d1f0c0SFrançois Tigeot 	map->offset = offset;
15279d1f0c0SFrançois Tigeot 	map->size = size;
15379d1f0c0SFrançois Tigeot 	map->flags = flags;
15488fc0f68SFrançois Tigeot 	map->type = type;
1557f3c3d6fSHasso Tepper 
1567f3c3d6fSHasso Tepper 	/* Only allow shared memory to be removable since we only keep enough
1577f3c3d6fSHasso Tepper 	 * book keeping information about shared memory to allow for removal
1587f3c3d6fSHasso Tepper 	 * when processes fork.
1597f3c3d6fSHasso Tepper 	 */
16088fc0f68SFrançois Tigeot 	if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
161175896dfSzrj 		kfree(map);
162b922632fSImre Vadász 		return -EINVAL;
1637f3c3d6fSHasso Tepper 	}
164df4baf3dSFrançois Tigeot 	DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
165df4baf3dSFrançois Tigeot 		  (unsigned long long)map->offset, map->size, map->type);
1667f3c3d6fSHasso Tepper 
167ef130734SFrançois Tigeot 	/* page-align _DRM_SHM maps. They are allocated here so there is no security
168ef130734SFrançois Tigeot 	 * hole created by that and it works around various broken drivers that use
169ef130734SFrançois Tigeot 	 * a non-aligned quantity to map the SAREA. --BenH
1707f3c3d6fSHasso Tepper 	 */
171ef130734SFrançois Tigeot 	if (map->type == _DRM_SHM)
172ef130734SFrançois Tigeot 		map->size = PAGE_ALIGN(map->size);
173ef130734SFrançois Tigeot 
174d653c727SFrançois Tigeot 	if ((map->offset & (~(resource_size_t) LINUX_PAGE_MASK)) || (map->size & (~LINUX_PAGE_MASK))) {
175ef130734SFrançois Tigeot 		kfree(map);
176ef130734SFrançois Tigeot 		return -EINVAL;
1777f3c3d6fSHasso Tepper 	}
17888fc0f68SFrançois Tigeot 	map->mtrr = -1;
17988fc0f68SFrançois Tigeot 	map->handle = NULL;
1807f3c3d6fSHasso Tepper 
1817f3c3d6fSHasso Tepper 	switch (map->type) {
1827f3c3d6fSHasso Tepper 	case _DRM_REGISTERS:
1837f3c3d6fSHasso Tepper 	case _DRM_FRAME_BUFFER:
184ef130734SFrançois Tigeot #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
185ef130734SFrançois Tigeot 		if (map->offset + (map->size-1) < map->offset ||
186ef130734SFrançois Tigeot 		    map->offset < virt_to_phys(high_memory)) {
187ef130734SFrançois Tigeot 			kfree(map);
188ef130734SFrançois Tigeot 			return -EINVAL;
189ef130734SFrançois Tigeot 		}
190ef130734SFrançois Tigeot #endif
191ef130734SFrançois Tigeot 		/* Some drivers preinitialize some maps, without the X Server
192ef130734SFrançois Tigeot 		 * needing to be aware of it.  Therefore, we just return success
193ef130734SFrançois Tigeot 		 * when the server tries to create a duplicate map.
194ef130734SFrançois Tigeot 		 */
195ef130734SFrançois Tigeot 		list = drm_find_matching_map(dev, map);
196ef130734SFrançois Tigeot 		if (list != NULL) {
197ef130734SFrançois Tigeot 			if (list->map->size != map->size) {
198ef130734SFrançois Tigeot 				DRM_DEBUG("Matching maps of type %d with "
199ef130734SFrançois Tigeot 					  "mismatched sizes, (%ld vs %ld)\n",
200ef130734SFrançois Tigeot 					  map->type, map->size,
201ef130734SFrançois Tigeot 					  list->map->size);
202ef130734SFrançois Tigeot 				list->map->size = map->size;
203ef130734SFrançois Tigeot 			}
204ef130734SFrançois Tigeot 
205ef130734SFrançois Tigeot 			kfree(map);
206ef130734SFrançois Tigeot 			*maplist = list;
207ef130734SFrançois Tigeot 			return 0;
208ef130734SFrançois Tigeot 		}
2096431cd91SFrançois Tigeot 
2106431cd91SFrançois Tigeot 		if (map->type == _DRM_FRAME_BUFFER ||
2116431cd91SFrançois Tigeot 		    (map->flags & _DRM_WRITE_COMBINING)) {
2126431cd91SFrançois Tigeot 			map->mtrr =
2136431cd91SFrançois Tigeot 				arch_phys_wc_add(map->offset, map->size);
2146431cd91SFrançois Tigeot 		}
2156431cd91SFrançois Tigeot 		if (map->type == _DRM_REGISTERS) {
2166431cd91SFrançois Tigeot 			if (map->flags & _DRM_WRITE_COMBINING)
2176431cd91SFrançois Tigeot 				map->handle = ioremap_wc(map->offset,
2186431cd91SFrançois Tigeot 							 map->size);
2196431cd91SFrançois Tigeot 			else
2206431cd91SFrançois Tigeot 				map->handle = ioremap(map->offset, map->size);
2216431cd91SFrançois Tigeot 			if (!map->handle) {
2226431cd91SFrançois Tigeot 				kfree(map);
2236431cd91SFrançois Tigeot 				return -ENOMEM;
2246431cd91SFrançois Tigeot 			}
2256431cd91SFrançois Tigeot 		}
2266431cd91SFrançois Tigeot 
2277f3c3d6fSHasso Tepper 		break;
2287f3c3d6fSHasso Tepper 	case _DRM_SHM:
229ef130734SFrançois Tigeot 		list = drm_find_matching_map(dev, map);
230ef130734SFrançois Tigeot 		if (list != NULL) {
231ef130734SFrançois Tigeot 			if(list->map->size != map->size) {
232ef130734SFrançois Tigeot 				DRM_DEBUG("Matching maps of type %d with "
233ef130734SFrançois Tigeot 					  "mismatched sizes, (%ld vs %ld)\n",
234ef130734SFrançois Tigeot 					  map->type, map->size, list->map->size);
235ef130734SFrançois Tigeot 				list->map->size = map->size;
236ef130734SFrançois Tigeot 			}
237ef130734SFrançois Tigeot 
238ef130734SFrançois Tigeot 			kfree(map);
239ef130734SFrançois Tigeot 			*maplist = list;
240ef130734SFrançois Tigeot 			return 0;
241ef130734SFrançois Tigeot 		}
24288fc0f68SFrançois Tigeot 		map->handle = vmalloc_user(map->size);
2437f3c3d6fSHasso Tepper 		DRM_DEBUG("%lu %d %p\n",
2444cd92098Szrj 			  map->size, order_base_2(map->size), map->handle);
2455c002123SFrançois Tigeot 		if (!map->handle) {
246175896dfSzrj 			kfree(map);
247b922632fSImre Vadász 			return -ENOMEM;
2487f3c3d6fSHasso Tepper 		}
2495c002123SFrançois Tigeot 		map->offset = (unsigned long)map->handle;
2507f3c3d6fSHasso Tepper 		if (map->flags & _DRM_CONTAINS_LOCK) {
2517f3c3d6fSHasso Tepper 			/* Prevent a 2nd X Server from creating a 2nd lock */
2521dedbd3bSFrançois Tigeot 			if (dev->master->lock.hw_lock != NULL) {
25388fc0f68SFrançois Tigeot 				vfree(map->handle);
254175896dfSzrj 				kfree(map);
255b922632fSImre Vadász 				return -EBUSY;
2567f3c3d6fSHasso Tepper 			}
2571dedbd3bSFrançois Tigeot 			dev->sigdata.lock = dev->master->lock.hw_lock = map->handle;	/* Pointer to lock */
2587f3c3d6fSHasso Tepper 		}
2597f3c3d6fSHasso Tepper 		break;
26088fc0f68SFrançois Tigeot 	case _DRM_AGP: {
261ef130734SFrançois Tigeot #if 0
262ef130734SFrançois Tigeot 		struct drm_agp_mem *entry;
263ef130734SFrançois Tigeot 		int valid = 0;
26453e4e524Szrj 
26553e4e524Szrj 		if (!dev->agp) {
26653e4e524Szrj 			kfree(map);
26753e4e524Szrj 			return -EINVAL;
26853e4e524Szrj 		}
269ef130734SFrançois Tigeot #ifdef __alpha__
270ef130734SFrançois Tigeot 		map->offset += dev->hose->mem_space->start;
271ef130734SFrançois Tigeot #endif
2727f3c3d6fSHasso Tepper 		/* In some cases (i810 driver), user space may have already
2737f3c3d6fSHasso Tepper 		 * added the AGP base itself, because dev->agp->base previously
2747f3c3d6fSHasso Tepper 		 * only got set during AGP enable.  So, only add the base
2757f3c3d6fSHasso Tepper 		 * address if the map's offset isn't already within the
2767f3c3d6fSHasso Tepper 		 * aperture.
2777f3c3d6fSHasso Tepper 		 */
2787f3c3d6fSHasso Tepper 		if (map->offset < dev->agp->base ||
2797f3c3d6fSHasso Tepper 		    map->offset > dev->agp->base +
280*3f2dd94aSFrançois Tigeot 		    dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
2817f3c3d6fSHasso Tepper 			map->offset += dev->agp->base;
2827f3c3d6fSHasso Tepper 		}
2839d567857SJean-Sébastien Pédron 		map->mtrr = dev->agp->agp_mtrr;	/* for getmap */
284ef130734SFrançois Tigeot 
285ef130734SFrançois Tigeot 		/* This assumes the DRM is in total control of AGP space.
286ef130734SFrançois Tigeot 		 * It's not always the case as AGP can be in the control
287ef130734SFrançois Tigeot 		 * of user space (i.e. i810 driver). So this loop will get
288ef130734SFrançois Tigeot 		 * skipped and we double check that dev->agp->memory is
289ef130734SFrançois Tigeot 		 * actually set as well as being invalid before EPERM'ing
290ef130734SFrançois Tigeot 		 */
291ef130734SFrançois Tigeot 		list_for_each_entry(entry, &dev->agp->memory, head) {
2927f3c3d6fSHasso Tepper 			if ((map->offset >= entry->bound) &&
293ef130734SFrançois Tigeot 			    (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
2947f3c3d6fSHasso Tepper 				valid = 1;
2957f3c3d6fSHasso Tepper 				break;
2967f3c3d6fSHasso Tepper 			}
2977f3c3d6fSHasso Tepper 		}
298ef130734SFrançois Tigeot 		if (!list_empty(&dev->agp->memory) && !valid) {
299175896dfSzrj 			kfree(map);
300ef130734SFrançois Tigeot 			return -EPERM;
301ef130734SFrançois Tigeot 		}
302ef130734SFrançois Tigeot 		DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
303ef130734SFrançois Tigeot 			  (unsigned long long)map->offset, map->size);
304ef130734SFrançois Tigeot 
3057f3c3d6fSHasso Tepper 		break;
306*3f2dd94aSFrançois Tigeot #endif
307*3f2dd94aSFrançois Tigeot 		return -EINVAL;		/* AGP hardware is no longer supported */
30888fc0f68SFrançois Tigeot 	}
3097f3c3d6fSHasso Tepper 	case _DRM_SCATTER_GATHER:
3107f3c3d6fSHasso Tepper 		if (!dev->sg) {
311175896dfSzrj 			kfree(map);
312b922632fSImre Vadász 			return -EINVAL;
3137f3c3d6fSHasso Tepper 		}
3145c002123SFrançois Tigeot 		map->handle = (void *)(uintptr_t)(dev->sg->vaddr + offset);
31599f70504SFrançois Tigeot 		map->offset = dev->sg->vaddr + offset;
3167f3c3d6fSHasso Tepper 		break;
3177f3c3d6fSHasso Tepper 	case _DRM_CONSISTENT:
318b31e9d59SFrançois Tigeot 		/* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
319b31e9d59SFrançois Tigeot 		 * As we're limiting the address to 2^32-1 (or less),
320b31e9d59SFrançois Tigeot 		 * casting it down to 32 bits is no problem, but we
321b31e9d59SFrançois Tigeot 		 * need to point to a 64bit variable first. */
322b31e9d59SFrançois Tigeot 		dmah = drm_pci_alloc(dev, map->size, map->size);
3235c002123SFrançois Tigeot 		if (!dmah) {
324158486a6SFrançois Tigeot 			kfree(map);
325b31e9d59SFrançois Tigeot 			return -ENOMEM;
3267f3c3d6fSHasso Tepper 		}
3275c002123SFrançois Tigeot 		map->handle = dmah->vaddr;
32888fc0f68SFrançois Tigeot 		map->offset = (unsigned long)dmah->busaddr;
32988fc0f68SFrançois Tigeot 		kfree(dmah);
3307f3c3d6fSHasso Tepper 		break;
3317f3c3d6fSHasso Tepper 	default:
332175896dfSzrj 		kfree(map);
333b922632fSImre Vadász 		return -EINVAL;
3347f3c3d6fSHasso Tepper 	}
3357f3c3d6fSHasso Tepper 
33688fc0f68SFrançois Tigeot 	list = kzalloc(sizeof(*list), GFP_KERNEL);
33788fc0f68SFrançois Tigeot 	if (!list) {
33888fc0f68SFrançois Tigeot 		if (map->type == _DRM_REGISTERS)
33988fc0f68SFrançois Tigeot 			iounmap(map->handle);
34088fc0f68SFrançois Tigeot 		kfree(map);
34188fc0f68SFrançois Tigeot 		return -EINVAL;
34288fc0f68SFrançois Tigeot 	}
34388fc0f68SFrançois Tigeot 	list->map = map;
34488fc0f68SFrançois Tigeot 
34588fc0f68SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
34688fc0f68SFrançois Tigeot 	list_add(&list->head, &dev->maplist);
347ef130734SFrançois Tigeot 
348ef130734SFrançois Tigeot 	/* Assign a 32-bit handle */
349ef130734SFrançois Tigeot 	/* We do it here so that dev->struct_mutex protects the increment */
350ef130734SFrançois Tigeot 	user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
351ef130734SFrançois Tigeot 		map->offset;
352ef130734SFrançois Tigeot 	ret = drm_map_handle(dev, &list->hash, user_token, 0,
353ef130734SFrançois Tigeot 			     (map->type == _DRM_SHM));
354ef130734SFrançois Tigeot 	if (ret) {
355ef130734SFrançois Tigeot 		if (map->type == _DRM_REGISTERS)
356ef130734SFrançois Tigeot 			iounmap(map->handle);
357ef130734SFrançois Tigeot 		kfree(map);
358ef130734SFrançois Tigeot 		kfree(list);
359ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
360ef130734SFrançois Tigeot 		return ret;
361ef130734SFrançois Tigeot 	}
362ef130734SFrançois Tigeot 
363ef130734SFrançois Tigeot 	list->user_token = list->hash.key << PAGE_SHIFT;
36488fc0f68SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
3657f3c3d6fSHasso Tepper 
366ef130734SFrançois Tigeot 	if (!(map->flags & _DRM_DRIVER))
3671dedbd3bSFrançois Tigeot 		list->master = dev->master;
36888fc0f68SFrançois Tigeot 	*maplist = list;
3697f3c3d6fSHasso Tepper 	return 0;
3707f3c3d6fSHasso Tepper }
3717f3c3d6fSHasso Tepper 
drm_legacy_addmap(struct drm_device * dev,resource_size_t offset,unsigned int size,enum drm_map_type type,enum drm_map_flags flags,struct drm_local_map ** map_ptr)37288fc0f68SFrançois Tigeot int drm_legacy_addmap(struct drm_device * dev, resource_size_t offset,
37388fc0f68SFrançois Tigeot 		      unsigned int size, enum drm_map_type type,
37488fc0f68SFrançois Tigeot 		      enum drm_map_flags flags, struct drm_local_map **map_ptr)
37588fc0f68SFrançois Tigeot {
37688fc0f68SFrançois Tigeot 	struct drm_map_list *list;
37788fc0f68SFrançois Tigeot 	int rc;
37888fc0f68SFrançois Tigeot 
37988fc0f68SFrançois Tigeot 	rc = drm_addmap_core(dev, offset, size, type, flags, &list);
38088fc0f68SFrançois Tigeot 	if (!rc)
38188fc0f68SFrançois Tigeot 		*map_ptr = list->map;
38288fc0f68SFrançois Tigeot 	return rc;
38388fc0f68SFrançois Tigeot }
38488fc0f68SFrançois Tigeot EXPORT_SYMBOL(drm_legacy_addmap);
38588fc0f68SFrançois Tigeot 
386df4baf3dSFrançois Tigeot /**
387df4baf3dSFrançois Tigeot  * Ioctl to specify a range of memory that is available for mapping by a
388df4baf3dSFrançois Tigeot  * non-root process.
389df4baf3dSFrançois Tigeot  *
390df4baf3dSFrançois Tigeot  * \param inode device inode.
391df4baf3dSFrançois Tigeot  * \param file_priv DRM file private.
392df4baf3dSFrançois Tigeot  * \param cmd command.
393df4baf3dSFrançois Tigeot  * \param arg pointer to a drm_map structure.
394df4baf3dSFrançois Tigeot  * \return zero on success or a negative value on error.
395df4baf3dSFrançois Tigeot  *
396df4baf3dSFrançois Tigeot  */
drm_legacy_addmap_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)3971b13d190SFrançois Tigeot int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
398b3705d71SHasso Tepper 			    struct drm_file *file_priv)
3997f3c3d6fSHasso Tepper {
400ef130734SFrançois Tigeot 	struct drm_map *map = data;
401ef130734SFrançois Tigeot 	struct drm_map_list *maplist;
4027f3c3d6fSHasso Tepper 	int err;
4037f3c3d6fSHasso Tepper 
404ef130734SFrançois Tigeot 	if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
405ef130734SFrançois Tigeot 		return -EPERM;
4067f3c3d6fSHasso Tepper 
407ef130734SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
4081dedbd3bSFrançois Tigeot 	    !drm_core_check_feature(dev, DRIVER_LEGACY))
409ef130734SFrançois Tigeot 		return -EINVAL;
4107f3c3d6fSHasso Tepper 
411ef130734SFrançois Tigeot 	err = drm_addmap_core(dev, map->offset, map->size, map->type,
412ef130734SFrançois Tigeot 			      map->flags, &maplist);
413ef130734SFrançois Tigeot 
414ef130734SFrançois Tigeot 	if (err)
4157f3c3d6fSHasso Tepper 		return err;
4167f3c3d6fSHasso Tepper 
417ef130734SFrançois Tigeot 	/* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
418ef130734SFrançois Tigeot 	map->handle = (void *)(unsigned long)maplist->user_token;
419ef130734SFrançois Tigeot 
420ef130734SFrançois Tigeot 	/*
421ef130734SFrançois Tigeot 	 * It appears that there are no users of this value whatsoever --
422ef130734SFrançois Tigeot 	 * drmAddMap just discards it.  Let's not encourage its use.
423ef130734SFrançois Tigeot 	 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
424ef130734SFrançois Tigeot 	 *  it's not a real mtrr index anymore.)
425ef130734SFrançois Tigeot 	 */
426ef130734SFrançois Tigeot 	map->mtrr = -1;
4277f3c3d6fSHasso Tepper 
4287f3c3d6fSHasso Tepper 	return 0;
4297f3c3d6fSHasso Tepper }
4307f3c3d6fSHasso Tepper 
4318621f407SFrançois Tigeot /*
4328621f407SFrançois Tigeot  * Get a mapping information.
4338621f407SFrançois Tigeot  *
4348621f407SFrançois Tigeot  * \param inode device inode.
4358621f407SFrançois Tigeot  * \param file_priv DRM file private.
4368621f407SFrançois Tigeot  * \param cmd command.
4378621f407SFrançois Tigeot  * \param arg user argument, pointing to a drm_map structure.
4388621f407SFrançois Tigeot  *
4398621f407SFrançois Tigeot  * \return zero on success or a negative number on failure.
4408621f407SFrançois Tigeot  *
4418621f407SFrançois Tigeot  * Searches for the mapping with the specified offset and copies its information
4428621f407SFrançois Tigeot  * into userspace
4438621f407SFrançois Tigeot  */
drm_legacy_getmap_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)4448621f407SFrançois Tigeot int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
4458621f407SFrançois Tigeot 			    struct drm_file *file_priv)
4468621f407SFrançois Tigeot {
4478621f407SFrançois Tigeot 	struct drm_map *map = data;
4488621f407SFrançois Tigeot 	struct drm_map_list *r_list = NULL;
4498621f407SFrançois Tigeot 	struct list_head *list;
4508621f407SFrançois Tigeot 	int idx;
4518621f407SFrançois Tigeot 	int i;
4528621f407SFrançois Tigeot 
4538621f407SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
4541dedbd3bSFrançois Tigeot 	    !drm_core_check_feature(dev, DRIVER_LEGACY))
4558621f407SFrançois Tigeot 		return -EINVAL;
4568621f407SFrançois Tigeot 
4578621f407SFrançois Tigeot 	idx = map->offset;
4588621f407SFrançois Tigeot 	if (idx < 0)
4598621f407SFrançois Tigeot 		return -EINVAL;
4608621f407SFrançois Tigeot 
4618621f407SFrançois Tigeot 	i = 0;
4628621f407SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
4638621f407SFrançois Tigeot 	list_for_each(list, &dev->maplist) {
4648621f407SFrançois Tigeot 		if (i == idx) {
4658621f407SFrançois Tigeot 			r_list = list_entry(list, struct drm_map_list, head);
4668621f407SFrançois Tigeot 			break;
4678621f407SFrançois Tigeot 		}
4688621f407SFrançois Tigeot 		i++;
4698621f407SFrançois Tigeot 	}
4708621f407SFrançois Tigeot 	if (!r_list || !r_list->map) {
4718621f407SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
4728621f407SFrançois Tigeot 		return -EINVAL;
4738621f407SFrançois Tigeot 	}
4748621f407SFrançois Tigeot 
4758621f407SFrançois Tigeot 	map->offset = r_list->map->offset;
4768621f407SFrançois Tigeot 	map->size = r_list->map->size;
4778621f407SFrançois Tigeot 	map->type = r_list->map->type;
4788621f407SFrançois Tigeot 	map->flags = r_list->map->flags;
4798621f407SFrançois Tigeot 	map->handle = (void *)(unsigned long) r_list->user_token;
4808621f407SFrançois Tigeot 	map->mtrr = r_list->map->mtrr;
4818621f407SFrançois Tigeot 
4828621f407SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
4838621f407SFrançois Tigeot 
4848621f407SFrançois Tigeot 	return 0;
4858621f407SFrançois Tigeot }
4868621f407SFrançois Tigeot 
4871b13d190SFrançois Tigeot /**
4881b13d190SFrançois Tigeot  * Remove a map private from list and deallocate resources if the mapping
4891b13d190SFrançois Tigeot  * isn't in use.
4901b13d190SFrançois Tigeot  *
4911b13d190SFrançois Tigeot  * Searches the map on drm_device::maplist, removes it from the list, see if
4921b13d190SFrançois Tigeot  * its being used, and free any associate resource (such as MTRR's) if it's not
4931b13d190SFrançois Tigeot  * being on use.
4941b13d190SFrançois Tigeot  *
4951b13d190SFrançois Tigeot  * \sa drm_legacy_addmap
4961b13d190SFrançois Tigeot  */
drm_legacy_rmmap_locked(struct drm_device * dev,struct drm_local_map * map)4971b13d190SFrançois Tigeot int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
4987f3c3d6fSHasso Tepper {
499f599cd46SFrançois Tigeot 	struct drm_map_list *r_list = NULL, *list_t;
5005c002123SFrançois Tigeot 	drm_dma_handle_t dmah;
501f599cd46SFrançois Tigeot 	int found = 0;
502ef130734SFrançois Tigeot 	struct drm_master *master;
50379f713b0SFrançois Tigeot 
504f599cd46SFrançois Tigeot 	/* Find the list entry for the map and remove it */
505f599cd46SFrançois Tigeot 	list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
506f599cd46SFrançois Tigeot 		if (r_list->map == map) {
507ef130734SFrançois Tigeot 			master = r_list->master;
508f599cd46SFrançois Tigeot 			list_del(&r_list->head);
509ef130734SFrançois Tigeot 			drm_ht_remove_key(&dev->map_hash,
510ef130734SFrançois Tigeot 					  r_list->user_token >> PAGE_SHIFT);
5111b13d190SFrançois Tigeot 			kfree(r_list);
512f599cd46SFrançois Tigeot 			found = 1;
513f599cd46SFrançois Tigeot 			break;
514f599cd46SFrançois Tigeot 		}
515f599cd46SFrançois Tigeot 	}
516f599cd46SFrançois Tigeot 
517f599cd46SFrançois Tigeot 	if (!found)
5181b13d190SFrançois Tigeot 		return -EINVAL;
5197f3c3d6fSHasso Tepper 
5207f3c3d6fSHasso Tepper 	switch (map->type) {
5217f3c3d6fSHasso Tepper 	case _DRM_REGISTERS:
522ef130734SFrançois Tigeot 		iounmap(map->handle);
5237f3c3d6fSHasso Tepper 		/* FALLTHROUGH */
5247f3c3d6fSHasso Tepper 	case _DRM_FRAME_BUFFER:
5256431cd91SFrançois Tigeot 		arch_phys_wc_del(map->mtrr);
5267f3c3d6fSHasso Tepper 		break;
5277f3c3d6fSHasso Tepper 	case _DRM_SHM:
528ef130734SFrançois Tigeot 		vfree(map->handle);
529ef130734SFrançois Tigeot 		if (master) {
530ef130734SFrançois Tigeot 			if (dev->sigdata.lock == master->lock.hw_lock)
531ef130734SFrançois Tigeot 				dev->sigdata.lock = NULL;
532ef130734SFrançois Tigeot 			master->lock.hw_lock = NULL;   /* SHM removed */
533ef130734SFrançois Tigeot 			master->lock.file_priv = NULL;
534ef130734SFrançois Tigeot 			wake_up_interruptible_all(&master->lock.lock_queue);
535ef130734SFrançois Tigeot 		}
5367f3c3d6fSHasso Tepper 		break;
5377f3c3d6fSHasso Tepper 	case _DRM_AGP:
5387f3c3d6fSHasso Tepper 	case _DRM_SCATTER_GATHER:
5397f3c3d6fSHasso Tepper 		break;
5407f3c3d6fSHasso Tepper 	case _DRM_CONSISTENT:
5415c002123SFrançois Tigeot 		dmah.vaddr = map->handle;
5425c002123SFrançois Tigeot 		dmah.busaddr = map->offset;
5431b13d190SFrançois Tigeot 		dmah.size = map->size;
5441b13d190SFrançois Tigeot 		__drm_legacy_pci_free(dev, &dmah);
5457f3c3d6fSHasso Tepper 		break;
5461b13d190SFrançois Tigeot 	}
5471b13d190SFrançois Tigeot 	kfree(map);
5481b13d190SFrançois Tigeot 
5491b13d190SFrançois Tigeot 	return 0;
5507f3c3d6fSHasso Tepper }
551ef130734SFrançois Tigeot EXPORT_SYMBOL(drm_legacy_rmmap_locked);
55279f713b0SFrançois Tigeot 
drm_legacy_rmmap(struct drm_device * dev,struct drm_local_map * map)553ef130734SFrançois Tigeot void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
5541b13d190SFrançois Tigeot {
5558621f407SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
5561dedbd3bSFrançois Tigeot 	    !drm_core_check_feature(dev, DRIVER_LEGACY))
557ef130734SFrançois Tigeot 		return;
5588621f407SFrançois Tigeot 
5591b13d190SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
560ef130734SFrançois Tigeot 	drm_legacy_rmmap_locked(dev, map);
5611b13d190SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
5627f3c3d6fSHasso Tepper }
5631b13d190SFrançois Tigeot EXPORT_SYMBOL(drm_legacy_rmmap);
5647f3c3d6fSHasso Tepper 
drm_legacy_master_rmmaps(struct drm_device * dev,struct drm_master * master)565ef130734SFrançois Tigeot void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
566ef130734SFrançois Tigeot {
567ef130734SFrançois Tigeot 	struct drm_map_list *r_list, *list_temp;
568ef130734SFrançois Tigeot 
5691dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
570ef130734SFrançois Tigeot 		return;
571ef130734SFrançois Tigeot 
572ef130734SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
573ef130734SFrançois Tigeot 	list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
574ef130734SFrançois Tigeot 		if (r_list->master == master) {
575ef130734SFrançois Tigeot 			drm_legacy_rmmap_locked(dev, r_list->map);
576ef130734SFrançois Tigeot 			r_list = NULL;
577ef130734SFrançois Tigeot 		}
578ef130734SFrançois Tigeot 	}
579ef130734SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
580ef130734SFrançois Tigeot }
581ef130734SFrançois Tigeot 
582f599cd46SFrançois Tigeot /* The rmmap ioctl appears to be unnecessary.  All mappings are torn down on
583f599cd46SFrançois Tigeot  * the last close of the device, and this is necessary for cleanup when things
584f599cd46SFrançois Tigeot  * exit uncleanly.  Therefore, having userland manually remove mappings seems
585f599cd46SFrançois Tigeot  * like a pointless exercise since they're going away anyway.
586f599cd46SFrançois Tigeot  *
587f599cd46SFrançois Tigeot  * One use case might be after addmap is allowed for normal users for SHM and
588f599cd46SFrançois Tigeot  * gets used by drivers that the server doesn't need to care about.  This seems
589f599cd46SFrançois Tigeot  * unlikely.
590f599cd46SFrançois Tigeot  *
591f599cd46SFrançois Tigeot  * \param inode device inode.
592f599cd46SFrançois Tigeot  * \param file_priv DRM file private.
593f599cd46SFrançois Tigeot  * \param cmd command.
594f599cd46SFrançois Tigeot  * \param arg pointer to a struct drm_map structure.
595f599cd46SFrançois Tigeot  * \return zero on success or a negative value on error.
5967f3c3d6fSHasso Tepper  */
drm_legacy_rmmap_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)5971b13d190SFrançois Tigeot int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
598b3705d71SHasso Tepper 			   struct drm_file *file_priv)
5997f3c3d6fSHasso Tepper {
600b3705d71SHasso Tepper 	struct drm_map *request = data;
601f599cd46SFrançois Tigeot 	struct drm_local_map *map = NULL;
602f599cd46SFrançois Tigeot 	struct drm_map_list *r_list;
60388fc0f68SFrançois Tigeot 	int ret;
6047f3c3d6fSHasso Tepper 
60588fc0f68SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
6061dedbd3bSFrançois Tigeot 	    !drm_core_check_feature(dev, DRIVER_LEGACY))
60788fc0f68SFrançois Tigeot 		return -EINVAL;
60888fc0f68SFrançois Tigeot 
60988fc0f68SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
610f599cd46SFrançois Tigeot 	list_for_each_entry(r_list, &dev->maplist, head) {
611f599cd46SFrançois Tigeot 		if (r_list->map &&
612f599cd46SFrançois Tigeot 		    r_list->user_token == (unsigned long)request->handle &&
613f599cd46SFrançois Tigeot 		    r_list->map->flags & _DRM_REMOVABLE) {
614f599cd46SFrançois Tigeot 			map = r_list->map;
6157f3c3d6fSHasso Tepper 			break;
6167f3c3d6fSHasso Tepper 		}
617f599cd46SFrançois Tigeot 	}
6187f3c3d6fSHasso Tepper 
619f599cd46SFrançois Tigeot 	/* List has wrapped around to the head pointer, or its empty we didn't
620f599cd46SFrançois Tigeot 	 * find anything.
621f599cd46SFrançois Tigeot 	 */
622f599cd46SFrançois Tigeot 	if (list_empty(&dev->maplist) || !map) {
62388fc0f68SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
624f599cd46SFrançois Tigeot 		return -EINVAL;
625f599cd46SFrançois Tigeot 	}
626f599cd46SFrançois Tigeot 
627f599cd46SFrançois Tigeot 	/* Register and framebuffer maps are permanent */
628f599cd46SFrançois Tigeot 	if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
62988fc0f68SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
630f599cd46SFrançois Tigeot 		return 0;
6317f3c3d6fSHasso Tepper 	}
6327f3c3d6fSHasso Tepper 
63388fc0f68SFrançois Tigeot 	ret = drm_legacy_rmmap_locked(dev, map);
6347f3c3d6fSHasso Tepper 
63588fc0f68SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
6367f3c3d6fSHasso Tepper 
63788fc0f68SFrançois Tigeot 	return ret;
6387f3c3d6fSHasso Tepper }
6397f3c3d6fSHasso Tepper 
640df4baf3dSFrançois Tigeot /**
641df4baf3dSFrançois Tigeot  * Cleanup after an error on one of the addbufs() functions.
642df4baf3dSFrançois Tigeot  *
643df4baf3dSFrançois Tigeot  * \param dev DRM device.
644df4baf3dSFrançois Tigeot  * \param entry buffer entry where the error occurred.
645df4baf3dSFrançois Tigeot  *
646df4baf3dSFrançois Tigeot  * Frees any pages and buffers associated with the given entry.
647df4baf3dSFrançois Tigeot  */
drm_cleanup_buf_error(struct drm_device * dev,struct drm_buf_entry * entry)648b3705d71SHasso Tepper static void drm_cleanup_buf_error(struct drm_device * dev,
649df4baf3dSFrançois Tigeot 				  struct drm_buf_entry * entry)
6507f3c3d6fSHasso Tepper {
6517f3c3d6fSHasso Tepper 	int i;
6527f3c3d6fSHasso Tepper 
6537f3c3d6fSHasso Tepper 	if (entry->seg_count) {
6547f3c3d6fSHasso Tepper 		for (i = 0; i < entry->seg_count; i++) {
655ef130734SFrançois Tigeot 			if (entry->seglist[i]) {
6567f3c3d6fSHasso Tepper 				drm_pci_free(dev, entry->seglist[i]);
6577f3c3d6fSHasso Tepper 			}
658ef130734SFrançois Tigeot 		}
659175896dfSzrj 		kfree(entry->seglist);
6607f3c3d6fSHasso Tepper 
6617f3c3d6fSHasso Tepper 		entry->seg_count = 0;
6627f3c3d6fSHasso Tepper 	}
6637f3c3d6fSHasso Tepper 
6647f3c3d6fSHasso Tepper 	if (entry->buf_count) {
6657f3c3d6fSHasso Tepper 		for (i = 0; i < entry->buf_count; i++) {
666175896dfSzrj 			kfree(entry->buflist[i].dev_private);
6677f3c3d6fSHasso Tepper 		}
668175896dfSzrj 		kfree(entry->buflist);
6697f3c3d6fSHasso Tepper 
6707f3c3d6fSHasso Tepper 		entry->buf_count = 0;
6717f3c3d6fSHasso Tepper 	}
6727f3c3d6fSHasso Tepper }
6737f3c3d6fSHasso Tepper 
674ef130734SFrançois Tigeot #if IS_ENABLED(CONFIG_AGP)
675ef130734SFrançois Tigeot /**
676ef130734SFrançois Tigeot  * Add AGP buffers for DMA transfers.
677ef130734SFrançois Tigeot  *
678ef130734SFrançois Tigeot  * \param dev struct drm_device to which the buffers are to be added.
679ef130734SFrançois Tigeot  * \param request pointer to a struct drm_buf_desc describing the request.
680ef130734SFrançois Tigeot  * \return zero on success or a negative number on failure.
681ef130734SFrançois Tigeot  *
682ef130734SFrançois Tigeot  * After some sanity checks creates a drm_buf structure for each buffer and
683ef130734SFrançois Tigeot  * reallocates the buffer list of the same size order to accommodate the new
684ef130734SFrançois Tigeot  * buffers.
685ef130734SFrançois Tigeot  */
drm_legacy_addbufs_agp(struct drm_device * dev,struct drm_buf_desc * request)686ef130734SFrançois Tigeot int drm_legacy_addbufs_agp(struct drm_device *dev,
687ef130734SFrançois Tigeot 			   struct drm_buf_desc *request)
6887f3c3d6fSHasso Tepper {
6894250aa95Szrj 	struct drm_device_dma *dma = dev->dma;
6904250aa95Szrj 	struct drm_buf_entry *entry;
691*3f2dd94aSFrançois Tigeot 	struct drm_agp_mem *agp_entry;
6924250aa95Szrj 	struct drm_buf *buf;
6937f3c3d6fSHasso Tepper 	unsigned long offset;
6947f3c3d6fSHasso Tepper 	unsigned long agp_offset;
6957f3c3d6fSHasso Tepper 	int count;
6967f3c3d6fSHasso Tepper 	int order;
6977f3c3d6fSHasso Tepper 	int size;
6987f3c3d6fSHasso Tepper 	int alignment;
6997f3c3d6fSHasso Tepper 	int page_order;
7007f3c3d6fSHasso Tepper 	int total;
7017f3c3d6fSHasso Tepper 	int byte_count;
702ef130734SFrançois Tigeot 	int i, valid;
7034250aa95Szrj 	struct drm_buf **temp_buflist;
7047f3c3d6fSHasso Tepper 
705ef130734SFrançois Tigeot 	if (!dma)
706ef130734SFrançois Tigeot 		return -EINVAL;
707ef130734SFrançois Tigeot 
7087f3c3d6fSHasso Tepper 	count = request->count;
7094cd92098Szrj 	order = order_base_2(request->size);
7107f3c3d6fSHasso Tepper 	size = 1 << order;
7117f3c3d6fSHasso Tepper 
7127f3c3d6fSHasso Tepper 	alignment = (request->flags & _DRM_PAGE_ALIGN)
713ef130734SFrançois Tigeot 	    ? PAGE_ALIGN(size) : size;
7147f3c3d6fSHasso Tepper 	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
7157f3c3d6fSHasso Tepper 	total = PAGE_SIZE << page_order;
7167f3c3d6fSHasso Tepper 
7177f3c3d6fSHasso Tepper 	byte_count = 0;
7187f3c3d6fSHasso Tepper 	agp_offset = dev->agp->base + request->agp_start;
7197f3c3d6fSHasso Tepper 
7207f3c3d6fSHasso Tepper 	DRM_DEBUG("count:      %d\n", count);
7217f3c3d6fSHasso Tepper 	DRM_DEBUG("order:      %d\n", order);
7227f3c3d6fSHasso Tepper 	DRM_DEBUG("size:       %d\n", size);
723ef130734SFrançois Tigeot 	DRM_DEBUG("agp_offset: %lx\n", agp_offset);
7247f3c3d6fSHasso Tepper 	DRM_DEBUG("alignment:  %d\n", alignment);
7257f3c3d6fSHasso Tepper 	DRM_DEBUG("page_order: %d\n", page_order);
7267f3c3d6fSHasso Tepper 	DRM_DEBUG("total:      %d\n", total);
7277f3c3d6fSHasso Tepper 
728ef130734SFrançois Tigeot 	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
729ef130734SFrançois Tigeot 		return -EINVAL;
730ef130734SFrançois Tigeot 
7317f3c3d6fSHasso Tepper 	/* Make sure buffers are located in AGP memory that we own */
732ef130734SFrançois Tigeot 	valid = 0;
733ef130734SFrançois Tigeot 	list_for_each_entry(agp_entry, &dev->agp->memory, head) {
7347f3c3d6fSHasso Tepper 		if ((agp_offset >= agp_entry->bound) &&
735ef130734SFrançois Tigeot 		    (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
7367f3c3d6fSHasso Tepper 			valid = 1;
7377f3c3d6fSHasso Tepper 			break;
7387f3c3d6fSHasso Tepper 		}
7397f3c3d6fSHasso Tepper 	}
740ef130734SFrançois Tigeot 	if (!list_empty(&dev->agp->memory) && !valid) {
7417f3c3d6fSHasso Tepper 		DRM_DEBUG("zone invalid\n");
742b922632fSImre Vadász 		return -EINVAL;
743ef130734SFrançois Tigeot 	}
744ec5b6af4SFrançois Tigeot 	lockmgr(&dev->buf_lock, LK_EXCLUSIVE);
745ef130734SFrançois Tigeot 	if (dev->buf_use) {
746ec5b6af4SFrançois Tigeot 		lockmgr(&dev->buf_lock, LK_RELEASE);
747ef130734SFrançois Tigeot 		return -EBUSY;
748ef130734SFrançois Tigeot 	}
749ef130734SFrançois Tigeot 	atomic_inc(&dev->buf_alloc);
750ec5b6af4SFrançois Tigeot 	lockmgr(&dev->buf_lock, LK_RELEASE);
7517f3c3d6fSHasso Tepper 
752ef130734SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
7537f3c3d6fSHasso Tepper 	entry = &dma->bufs[order];
754ef130734SFrançois Tigeot 	if (entry->buf_count) {
755ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
756ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
757ef130734SFrançois Tigeot 		return -ENOMEM;	/* May only call once for each order */
758ef130734SFrançois Tigeot 	}
7597f3c3d6fSHasso Tepper 
760ef130734SFrançois Tigeot 	if (count < 0 || count > 4096) {
761ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
762ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
763ef130734SFrançois Tigeot 		return -EINVAL;
764ef130734SFrançois Tigeot 	}
765ef130734SFrançois Tigeot 
7661dedbd3bSFrançois Tigeot 	entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
7677f3c3d6fSHasso Tepper 	if (!entry->buflist) {
768ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
769ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
770b922632fSImre Vadász 		return -ENOMEM;
7717f3c3d6fSHasso Tepper 	}
7727f3c3d6fSHasso Tepper 
7737f3c3d6fSHasso Tepper 	entry->buf_size = size;
7747f3c3d6fSHasso Tepper 	entry->page_order = page_order;
7757f3c3d6fSHasso Tepper 
7767f3c3d6fSHasso Tepper 	offset = 0;
7777f3c3d6fSHasso Tepper 
7787f3c3d6fSHasso Tepper 	while (entry->buf_count < count) {
7797f3c3d6fSHasso Tepper 		buf = &entry->buflist[entry->buf_count];
7807f3c3d6fSHasso Tepper 		buf->idx = dma->buf_count + entry->buf_count;
7817f3c3d6fSHasso Tepper 		buf->total = alignment;
7827f3c3d6fSHasso Tepper 		buf->order = order;
7837f3c3d6fSHasso Tepper 		buf->used = 0;
7847f3c3d6fSHasso Tepper 
7857f3c3d6fSHasso Tepper 		buf->offset = (dma->byte_count + offset);
7867f3c3d6fSHasso Tepper 		buf->bus_address = agp_offset + offset;
7877f3c3d6fSHasso Tepper 		buf->address = (void *)(agp_offset + offset);
7887f3c3d6fSHasso Tepper 		buf->next = NULL;
789ef130734SFrançois Tigeot 		buf->waiting = 0;
7907f3c3d6fSHasso Tepper 		buf->pending = 0;
7917f3c3d6fSHasso Tepper 		buf->file_priv = NULL;
7927f3c3d6fSHasso Tepper 
793ba55f2f5SFrançois Tigeot 		buf->dev_priv_size = dev->driver->dev_priv_size;
794ef130734SFrançois Tigeot 		buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
795ef130734SFrançois Tigeot 		if (!buf->dev_private) {
7967f3c3d6fSHasso Tepper 			/* Set count correctly so we free the proper amount. */
7977f3c3d6fSHasso Tepper 			entry->buf_count = count;
7987f3c3d6fSHasso Tepper 			drm_cleanup_buf_error(dev, entry);
799ef130734SFrançois Tigeot 			mutex_unlock(&dev->struct_mutex);
800ef130734SFrançois Tigeot 			atomic_dec(&dev->buf_alloc);
801b922632fSImre Vadász 			return -ENOMEM;
8027f3c3d6fSHasso Tepper 		}
8037f3c3d6fSHasso Tepper 
804ef130734SFrançois Tigeot 		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
805ef130734SFrançois Tigeot 
8067f3c3d6fSHasso Tepper 		offset += alignment;
8077f3c3d6fSHasso Tepper 		entry->buf_count++;
8087f3c3d6fSHasso Tepper 		byte_count += PAGE_SIZE << page_order;
8097f3c3d6fSHasso Tepper 	}
8107f3c3d6fSHasso Tepper 
8117f3c3d6fSHasso Tepper 	DRM_DEBUG("byte_count: %d\n", byte_count);
8127f3c3d6fSHasso Tepper 
8135718399fSFrançois Tigeot 	temp_buflist = krealloc(dma->buflist,
814*3f2dd94aSFrançois Tigeot 				(dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
815*3f2dd94aSFrançois Tigeot 				M_DRM, GFP_KERNEL);
816ef130734SFrançois Tigeot 	if (!temp_buflist) {
8177f3c3d6fSHasso Tepper 		/* Free the entry because it isn't valid */
8187f3c3d6fSHasso Tepper 		drm_cleanup_buf_error(dev, entry);
819ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
820ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
821b922632fSImre Vadász 		return -ENOMEM;
8227f3c3d6fSHasso Tepper 	}
8237f3c3d6fSHasso Tepper 	dma->buflist = temp_buflist;
8247f3c3d6fSHasso Tepper 
8257f3c3d6fSHasso Tepper 	for (i = 0; i < entry->buf_count; i++) {
8267f3c3d6fSHasso Tepper 		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
8277f3c3d6fSHasso Tepper 	}
8287f3c3d6fSHasso Tepper 
8297f3c3d6fSHasso Tepper 	dma->buf_count += entry->buf_count;
830ef130734SFrançois Tigeot 	dma->seg_count += entry->seg_count;
831ef130734SFrançois Tigeot 	dma->page_count += byte_count >> PAGE_SHIFT;
8327f3c3d6fSHasso Tepper 	dma->byte_count += byte_count;
8337f3c3d6fSHasso Tepper 
8347f3c3d6fSHasso Tepper 	DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
8357f3c3d6fSHasso Tepper 	DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
8367f3c3d6fSHasso Tepper 
837ef130734SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
838ef130734SFrançois Tigeot 
8397f3c3d6fSHasso Tepper 	request->count = entry->buf_count;
8407f3c3d6fSHasso Tepper 	request->size = size;
8417f3c3d6fSHasso Tepper 
8427f3c3d6fSHasso Tepper 	dma->flags = _DRM_DMA_USE_AGP;
8437f3c3d6fSHasso Tepper 
844ef130734SFrançois Tigeot 	atomic_dec(&dev->buf_alloc);
8457f3c3d6fSHasso Tepper 	return 0;
8467f3c3d6fSHasso Tepper }
847ef130734SFrançois Tigeot EXPORT_SYMBOL(drm_legacy_addbufs_agp);
848ef130734SFrançois Tigeot #endif /* CONFIG_AGP */
8497f3c3d6fSHasso Tepper 
drm_legacy_addbufs_pci(struct drm_device * dev,struct drm_buf_desc * request)850ef130734SFrançois Tigeot int drm_legacy_addbufs_pci(struct drm_device *dev,
851ef130734SFrançois Tigeot 			   struct drm_buf_desc *request)
8527f3c3d6fSHasso Tepper {
8534250aa95Szrj 	struct drm_device_dma *dma = dev->dma;
8547f3c3d6fSHasso Tepper 	int count;
8557f3c3d6fSHasso Tepper 	int order;
8567f3c3d6fSHasso Tepper 	int size;
8577f3c3d6fSHasso Tepper 	int total;
8587f3c3d6fSHasso Tepper 	int page_order;
8594250aa95Szrj 	struct drm_buf_entry *entry;
860b31e9d59SFrançois Tigeot 	drm_dma_handle_t *dmah;
8614250aa95Szrj 	struct drm_buf *buf;
8627f3c3d6fSHasso Tepper 	int alignment;
8637f3c3d6fSHasso Tepper 	unsigned long offset;
8647f3c3d6fSHasso Tepper 	int i;
8657f3c3d6fSHasso Tepper 	int byte_count;
8667f3c3d6fSHasso Tepper 	int page_count;
8677f3c3d6fSHasso Tepper 	unsigned long *temp_pagelist;
8684250aa95Szrj 	struct drm_buf **temp_buflist;
8697f3c3d6fSHasso Tepper 
870ef130734SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
871ef130734SFrançois Tigeot 		return -EINVAL;
872ef130734SFrançois Tigeot 
873ef130734SFrançois Tigeot 	if (!dma)
874ef130734SFrançois Tigeot 		return -EINVAL;
875ef130734SFrançois Tigeot 
876ef130734SFrançois Tigeot 	if (!capable(CAP_SYS_ADMIN))
877ef130734SFrançois Tigeot 		return -EPERM;
878ef130734SFrançois Tigeot 
8797f3c3d6fSHasso Tepper 	count = request->count;
8804cd92098Szrj 	order = order_base_2(request->size);
8817f3c3d6fSHasso Tepper 	size = 1 << order;
8827f3c3d6fSHasso Tepper 
8837f3c3d6fSHasso Tepper 	DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
8847f3c3d6fSHasso Tepper 		  request->count, request->size, size, order);
8857f3c3d6fSHasso Tepper 
886ef130734SFrançois Tigeot 	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
887ef130734SFrançois Tigeot 		return -EINVAL;
888ef130734SFrançois Tigeot 
8897f3c3d6fSHasso Tepper 	alignment = (request->flags & _DRM_PAGE_ALIGN)
890ef130734SFrançois Tigeot 	    ? PAGE_ALIGN(size) : size;
8917f3c3d6fSHasso Tepper 	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
8927f3c3d6fSHasso Tepper 	total = PAGE_SIZE << page_order;
8937f3c3d6fSHasso Tepper 
894ec5b6af4SFrançois Tigeot 	lockmgr(&dev->buf_lock, LK_EXCLUSIVE);
895ef130734SFrançois Tigeot 	if (dev->buf_use) {
896ec5b6af4SFrançois Tigeot 		lockmgr(&dev->buf_lock, LK_RELEASE);
897ef130734SFrançois Tigeot 		return -EBUSY;
898ef130734SFrançois Tigeot 	}
899ef130734SFrançois Tigeot 	atomic_inc(&dev->buf_alloc);
900ec5b6af4SFrançois Tigeot 	lockmgr(&dev->buf_lock, LK_RELEASE);
9017f3c3d6fSHasso Tepper 
902ef130734SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
903ef130734SFrançois Tigeot 	entry = &dma->bufs[order];
904ef130734SFrançois Tigeot 	if (entry->buf_count) {
905ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
906ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
907ef130734SFrançois Tigeot 		return -ENOMEM;	/* May only call once for each order */
908ef130734SFrançois Tigeot 	}
909ef130734SFrançois Tigeot 
910ef130734SFrançois Tigeot 	if (count < 0 || count > 4096) {
911ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
912ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
913ef130734SFrançois Tigeot 		return -EINVAL;
914ef130734SFrançois Tigeot 	}
915ef130734SFrançois Tigeot 
9161dedbd3bSFrançois Tigeot 	entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
917ef130734SFrançois Tigeot 	if (!entry->buflist) {
918ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
919ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
920ef130734SFrançois Tigeot 		return -ENOMEM;
921ef130734SFrançois Tigeot 	}
922ef130734SFrançois Tigeot 
9231dedbd3bSFrançois Tigeot 	entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
924ef130734SFrançois Tigeot 	if (!entry->seglist) {
925ef130734SFrançois Tigeot 		kfree(entry->buflist);
926ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
927ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
928ef130734SFrançois Tigeot 		return -ENOMEM;
929ef130734SFrançois Tigeot 	}
9307f3c3d6fSHasso Tepper 
9317f3c3d6fSHasso Tepper 	/* Keep the original pagelist until we know all the allocations
9327f3c3d6fSHasso Tepper 	 * have succeeded
9337f3c3d6fSHasso Tepper 	 */
9341dedbd3bSFrançois Tigeot 	temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
9351dedbd3bSFrançois Tigeot 				      sizeof(*dma->pagelist),
9361dedbd3bSFrançois Tigeot 				      GFP_KERNEL);
937ef130734SFrançois Tigeot 	if (!temp_pagelist) {
938175896dfSzrj 		kfree(entry->buflist);
939ef130734SFrançois Tigeot 		kfree(entry->seglist);
940ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
941ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
942b922632fSImre Vadász 		return -ENOMEM;
9437f3c3d6fSHasso Tepper 	}
944ef130734SFrançois Tigeot 	memcpy(temp_pagelist,
945ef130734SFrançois Tigeot 	       dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
9467f3c3d6fSHasso Tepper 	DRM_DEBUG("pagelist: %d entries\n",
9477f3c3d6fSHasso Tepper 		  dma->page_count + (count << page_order));
9487f3c3d6fSHasso Tepper 
9497f3c3d6fSHasso Tepper 	entry->buf_size = size;
9507f3c3d6fSHasso Tepper 	entry->page_order = page_order;
9517f3c3d6fSHasso Tepper 	byte_count = 0;
9527f3c3d6fSHasso Tepper 	page_count = 0;
9537f3c3d6fSHasso Tepper 
9547f3c3d6fSHasso Tepper 	while (entry->buf_count < count) {
955ef130734SFrançois Tigeot 
956b31e9d59SFrançois Tigeot 		dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
957b31e9d59SFrançois Tigeot 
958b31e9d59SFrançois Tigeot 		if (!dmah) {
9597f3c3d6fSHasso Tepper 			/* Set count correctly so we free the proper amount. */
9607f3c3d6fSHasso Tepper 			entry->buf_count = count;
9617f3c3d6fSHasso Tepper 			entry->seg_count = count;
9627f3c3d6fSHasso Tepper 			drm_cleanup_buf_error(dev, entry);
963175896dfSzrj 			kfree(temp_pagelist);
964ef130734SFrançois Tigeot 			mutex_unlock(&dev->struct_mutex);
965ef130734SFrançois Tigeot 			atomic_dec(&dev->buf_alloc);
966b31e9d59SFrançois Tigeot 			return -ENOMEM;
9677f3c3d6fSHasso Tepper 		}
9687f3c3d6fSHasso Tepper 		entry->seglist[entry->seg_count++] = dmah;
9697f3c3d6fSHasso Tepper 		for (i = 0; i < (1 << page_order); i++) {
970b31e9d59SFrançois Tigeot 			DRM_DEBUG("page %d @ 0x%08lx\n",
9717f3c3d6fSHasso Tepper 				  dma->page_count + page_count,
972b31e9d59SFrançois Tigeot 				  (unsigned long)dmah->vaddr + PAGE_SIZE * i);
973b31e9d59SFrançois Tigeot 			temp_pagelist[dma->page_count + page_count++]
974b31e9d59SFrançois Tigeot 				= (unsigned long)dmah->vaddr + PAGE_SIZE * i;
9757f3c3d6fSHasso Tepper 		}
9767f3c3d6fSHasso Tepper 		for (offset = 0;
9777f3c3d6fSHasso Tepper 		     offset + size <= total && entry->buf_count < count;
9787f3c3d6fSHasso Tepper 		     offset += alignment, ++entry->buf_count) {
9797f3c3d6fSHasso Tepper 			buf = &entry->buflist[entry->buf_count];
9807f3c3d6fSHasso Tepper 			buf->idx = dma->buf_count + entry->buf_count;
9817f3c3d6fSHasso Tepper 			buf->total = alignment;
9827f3c3d6fSHasso Tepper 			buf->order = order;
9837f3c3d6fSHasso Tepper 			buf->used = 0;
9847f3c3d6fSHasso Tepper 			buf->offset = (dma->byte_count + byte_count + offset);
985*3f2dd94aSFrançois Tigeot 			buf->address = (void *)(dmah->vaddr + offset);
9867f3c3d6fSHasso Tepper 			buf->bus_address = dmah->busaddr + offset;
9877f3c3d6fSHasso Tepper 			buf->next = NULL;
988ef130734SFrançois Tigeot 			buf->waiting = 0;
9897f3c3d6fSHasso Tepper 			buf->pending = 0;
9907f3c3d6fSHasso Tepper 			buf->file_priv = NULL;
9917f3c3d6fSHasso Tepper 
992ba55f2f5SFrançois Tigeot 			buf->dev_priv_size = dev->driver->dev_priv_size;
993ef130734SFrançois Tigeot 			buf->dev_private = kzalloc(buf->dev_priv_size,
994ef130734SFrançois Tigeot 						GFP_KERNEL);
995ef130734SFrançois Tigeot 			if (!buf->dev_private) {
9967f3c3d6fSHasso Tepper 				/* Set count correctly so we free the proper amount. */
9977f3c3d6fSHasso Tepper 				entry->buf_count = count;
9987f3c3d6fSHasso Tepper 				entry->seg_count = count;
9997f3c3d6fSHasso Tepper 				drm_cleanup_buf_error(dev, entry);
1000175896dfSzrj 				kfree(temp_pagelist);
1001ef130734SFrançois Tigeot 				mutex_unlock(&dev->struct_mutex);
1002ef130734SFrançois Tigeot 				atomic_dec(&dev->buf_alloc);
1003b922632fSImre Vadász 				return -ENOMEM;
10047f3c3d6fSHasso Tepper 			}
10057f3c3d6fSHasso Tepper 
10067f3c3d6fSHasso Tepper 			DRM_DEBUG("buffer %d @ %p\n",
10077f3c3d6fSHasso Tepper 				  entry->buf_count, buf->address);
10087f3c3d6fSHasso Tepper 		}
10097f3c3d6fSHasso Tepper 		byte_count += PAGE_SIZE << page_order;
10107f3c3d6fSHasso Tepper 	}
10117f3c3d6fSHasso Tepper 
10125718399fSFrançois Tigeot 	temp_buflist = krealloc(dma->buflist,
1013*3f2dd94aSFrançois Tigeot 				(dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
1014*3f2dd94aSFrançois Tigeot 				M_DRM, GFP_KERNEL);
1015ef130734SFrançois Tigeot 	if (!temp_buflist) {
10167f3c3d6fSHasso Tepper 		/* Free the entry because it isn't valid */
10177f3c3d6fSHasso Tepper 		drm_cleanup_buf_error(dev, entry);
1018175896dfSzrj 		kfree(temp_pagelist);
1019ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
1020ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
1021b922632fSImre Vadász 		return -ENOMEM;
10227f3c3d6fSHasso Tepper 	}
10237f3c3d6fSHasso Tepper 	dma->buflist = temp_buflist;
10247f3c3d6fSHasso Tepper 
10257f3c3d6fSHasso Tepper 	for (i = 0; i < entry->buf_count; i++) {
10267f3c3d6fSHasso Tepper 		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
10277f3c3d6fSHasso Tepper 	}
10287f3c3d6fSHasso Tepper 
1029175896dfSzrj 	/* No allocations failed, so now we can replace the original pagelist
10307f3c3d6fSHasso Tepper 	 * with the new one.
10317f3c3d6fSHasso Tepper 	 */
1032ef130734SFrançois Tigeot 	if (dma->page_count) {
1033175896dfSzrj 		kfree(dma->pagelist);
1034ef130734SFrançois Tigeot 	}
10357f3c3d6fSHasso Tepper 	dma->pagelist = temp_pagelist;
10367f3c3d6fSHasso Tepper 
10377f3c3d6fSHasso Tepper 	dma->buf_count += entry->buf_count;
10387f3c3d6fSHasso Tepper 	dma->seg_count += entry->seg_count;
10397f3c3d6fSHasso Tepper 	dma->page_count += entry->seg_count << page_order;
10407f3c3d6fSHasso Tepper 	dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
10417f3c3d6fSHasso Tepper 
1042ef130734SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
1043ef130734SFrançois Tigeot 
10447f3c3d6fSHasso Tepper 	request->count = entry->buf_count;
10457f3c3d6fSHasso Tepper 	request->size = size;
10467f3c3d6fSHasso Tepper 
1047ef130734SFrançois Tigeot 	if (request->flags & _DRM_PCI_BUFFER_RO)
1048ef130734SFrançois Tigeot 		dma->flags = _DRM_DMA_USE_PCI_RO;
1049ef130734SFrançois Tigeot 
1050ef130734SFrançois Tigeot 	atomic_dec(&dev->buf_alloc);
10517f3c3d6fSHasso Tepper 	return 0;
10527f3c3d6fSHasso Tepper 
10537f3c3d6fSHasso Tepper }
1054ef130734SFrançois Tigeot EXPORT_SYMBOL(drm_legacy_addbufs_pci);
10557f3c3d6fSHasso Tepper 
drm_legacy_addbufs_sg(struct drm_device * dev,struct drm_buf_desc * request)1056ef130734SFrançois Tigeot static int drm_legacy_addbufs_sg(struct drm_device *dev,
1057ef130734SFrançois Tigeot 				 struct drm_buf_desc *request)
10587f3c3d6fSHasso Tepper {
10594250aa95Szrj 	struct drm_device_dma *dma = dev->dma;
10604250aa95Szrj 	struct drm_buf_entry *entry;
10614250aa95Szrj 	struct drm_buf *buf;
10627f3c3d6fSHasso Tepper 	unsigned long offset;
10637f3c3d6fSHasso Tepper 	unsigned long agp_offset;
10647f3c3d6fSHasso Tepper 	int count;
10657f3c3d6fSHasso Tepper 	int order;
10667f3c3d6fSHasso Tepper 	int size;
10677f3c3d6fSHasso Tepper 	int alignment;
10687f3c3d6fSHasso Tepper 	int page_order;
10697f3c3d6fSHasso Tepper 	int total;
10707f3c3d6fSHasso Tepper 	int byte_count;
10717f3c3d6fSHasso Tepper 	int i;
10724250aa95Szrj 	struct drm_buf **temp_buflist;
10737f3c3d6fSHasso Tepper 
1074ef130734SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_SG))
1075ef130734SFrançois Tigeot 		return -EINVAL;
1076ef130734SFrançois Tigeot 
1077ef130734SFrançois Tigeot 	if (!dma)
1078ef130734SFrançois Tigeot 		return -EINVAL;
1079ef130734SFrançois Tigeot 
1080ef130734SFrançois Tigeot 	if (!capable(CAP_SYS_ADMIN))
1081ef130734SFrançois Tigeot 		return -EPERM;
1082ef130734SFrançois Tigeot 
10837f3c3d6fSHasso Tepper 	count = request->count;
10844cd92098Szrj 	order = order_base_2(request->size);
10857f3c3d6fSHasso Tepper 	size = 1 << order;
10867f3c3d6fSHasso Tepper 
10877f3c3d6fSHasso Tepper 	alignment = (request->flags & _DRM_PAGE_ALIGN)
1088ef130734SFrançois Tigeot 	    ? PAGE_ALIGN(size) : size;
10897f3c3d6fSHasso Tepper 	page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
10907f3c3d6fSHasso Tepper 	total = PAGE_SIZE << page_order;
10917f3c3d6fSHasso Tepper 
10927f3c3d6fSHasso Tepper 	byte_count = 0;
10937f3c3d6fSHasso Tepper 	agp_offset = request->agp_start;
10947f3c3d6fSHasso Tepper 
10957f3c3d6fSHasso Tepper 	DRM_DEBUG("count:      %d\n", count);
10967f3c3d6fSHasso Tepper 	DRM_DEBUG("order:      %d\n", order);
10977f3c3d6fSHasso Tepper 	DRM_DEBUG("size:       %d\n", size);
1098ef130734SFrançois Tigeot 	DRM_DEBUG("agp_offset: %lu\n", agp_offset);
10997f3c3d6fSHasso Tepper 	DRM_DEBUG("alignment:  %d\n", alignment);
11007f3c3d6fSHasso Tepper 	DRM_DEBUG("page_order: %d\n", page_order);
11017f3c3d6fSHasso Tepper 	DRM_DEBUG("total:      %d\n", total);
11027f3c3d6fSHasso Tepper 
1103ef130734SFrançois Tigeot 	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1104ef130734SFrançois Tigeot 		return -EINVAL;
11057f3c3d6fSHasso Tepper 
1106ec5b6af4SFrançois Tigeot 	lockmgr(&dev->buf_lock, LK_EXCLUSIVE);
1107ef130734SFrançois Tigeot 	if (dev->buf_use) {
1108ec5b6af4SFrançois Tigeot 		lockmgr(&dev->buf_lock, LK_RELEASE);
1109ef130734SFrançois Tigeot 		return -EBUSY;
1110ef130734SFrançois Tigeot 	}
1111ef130734SFrançois Tigeot 	atomic_inc(&dev->buf_alloc);
1112ec5b6af4SFrançois Tigeot 	lockmgr(&dev->buf_lock, LK_RELEASE);
1113ef130734SFrançois Tigeot 
1114ef130734SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
1115ef130734SFrançois Tigeot 	entry = &dma->bufs[order];
1116ef130734SFrançois Tigeot 	if (entry->buf_count) {
1117ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
1118ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
1119ef130734SFrançois Tigeot 		return -ENOMEM;	/* May only call once for each order */
1120ef130734SFrançois Tigeot 	}
1121ef130734SFrançois Tigeot 
1122ef130734SFrançois Tigeot 	if (count < 0 || count > 4096) {
1123ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
1124ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
1125ef130734SFrançois Tigeot 		return -EINVAL;
1126ef130734SFrançois Tigeot 	}
1127ef130734SFrançois Tigeot 
11281dedbd3bSFrançois Tigeot 	entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
1129ef130734SFrançois Tigeot 	if (!entry->buflist) {
1130ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
1131ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
1132b922632fSImre Vadász 		return -ENOMEM;
1133ef130734SFrançois Tigeot 	}
11347f3c3d6fSHasso Tepper 
11357f3c3d6fSHasso Tepper 	entry->buf_size = size;
11367f3c3d6fSHasso Tepper 	entry->page_order = page_order;
11377f3c3d6fSHasso Tepper 
11387f3c3d6fSHasso Tepper 	offset = 0;
11397f3c3d6fSHasso Tepper 
11407f3c3d6fSHasso Tepper 	while (entry->buf_count < count) {
11417f3c3d6fSHasso Tepper 		buf = &entry->buflist[entry->buf_count];
11427f3c3d6fSHasso Tepper 		buf->idx = dma->buf_count + entry->buf_count;
11437f3c3d6fSHasso Tepper 		buf->total = alignment;
11447f3c3d6fSHasso Tepper 		buf->order = order;
11457f3c3d6fSHasso Tepper 		buf->used = 0;
11467f3c3d6fSHasso Tepper 
11477f3c3d6fSHasso Tepper 		buf->offset = (dma->byte_count + offset);
11487f3c3d6fSHasso Tepper 		buf->bus_address = agp_offset + offset;
114999f70504SFrançois Tigeot 		buf->address = (void *)(agp_offset + offset + dev->sg->vaddr);
11507f3c3d6fSHasso Tepper 		buf->next = NULL;
1151ef130734SFrançois Tigeot 		buf->waiting = 0;
11527f3c3d6fSHasso Tepper 		buf->pending = 0;
11537f3c3d6fSHasso Tepper 		buf->file_priv = NULL;
11547f3c3d6fSHasso Tepper 
1155ba55f2f5SFrançois Tigeot 		buf->dev_priv_size = dev->driver->dev_priv_size;
1156ef130734SFrançois Tigeot 		buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
1157ef130734SFrançois Tigeot 		if (!buf->dev_private) {
11587f3c3d6fSHasso Tepper 			/* Set count correctly so we free the proper amount. */
11597f3c3d6fSHasso Tepper 			entry->buf_count = count;
11607f3c3d6fSHasso Tepper 			drm_cleanup_buf_error(dev, entry);
1161ef130734SFrançois Tigeot 			mutex_unlock(&dev->struct_mutex);
1162ef130734SFrançois Tigeot 			atomic_dec(&dev->buf_alloc);
1163b922632fSImre Vadász 			return -ENOMEM;
11647f3c3d6fSHasso Tepper 		}
11657f3c3d6fSHasso Tepper 
1166ef130734SFrançois Tigeot 		DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
11677f3c3d6fSHasso Tepper 
11687f3c3d6fSHasso Tepper 		offset += alignment;
11697f3c3d6fSHasso Tepper 		entry->buf_count++;
11707f3c3d6fSHasso Tepper 		byte_count += PAGE_SIZE << page_order;
11717f3c3d6fSHasso Tepper 	}
11727f3c3d6fSHasso Tepper 
11737f3c3d6fSHasso Tepper 	DRM_DEBUG("byte_count: %d\n", byte_count);
11747f3c3d6fSHasso Tepper 
11755718399fSFrançois Tigeot 	temp_buflist = krealloc(dma->buflist,
1176*3f2dd94aSFrançois Tigeot 				(dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
1177*3f2dd94aSFrançois Tigeot 				M_DRM, GFP_KERNEL);
1178ef130734SFrançois Tigeot 	if (!temp_buflist) {
11797f3c3d6fSHasso Tepper 		/* Free the entry because it isn't valid */
11807f3c3d6fSHasso Tepper 		drm_cleanup_buf_error(dev, entry);
1181ef130734SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
1182ef130734SFrançois Tigeot 		atomic_dec(&dev->buf_alloc);
1183b922632fSImre Vadász 		return -ENOMEM;
11847f3c3d6fSHasso Tepper 	}
11857f3c3d6fSHasso Tepper 	dma->buflist = temp_buflist;
11867f3c3d6fSHasso Tepper 
11877f3c3d6fSHasso Tepper 	for (i = 0; i < entry->buf_count; i++) {
11887f3c3d6fSHasso Tepper 		dma->buflist[i + dma->buf_count] = &entry->buflist[i];
11897f3c3d6fSHasso Tepper 	}
11907f3c3d6fSHasso Tepper 
11917f3c3d6fSHasso Tepper 	dma->buf_count += entry->buf_count;
1192ef130734SFrançois Tigeot 	dma->seg_count += entry->seg_count;
1193ef130734SFrançois Tigeot 	dma->page_count += byte_count >> PAGE_SHIFT;
11947f3c3d6fSHasso Tepper 	dma->byte_count += byte_count;
11957f3c3d6fSHasso Tepper 
11967f3c3d6fSHasso Tepper 	DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
11977f3c3d6fSHasso Tepper 	DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
11987f3c3d6fSHasso Tepper 
1199ef130734SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
1200ef130734SFrançois Tigeot 
12017f3c3d6fSHasso Tepper 	request->count = entry->buf_count;
12027f3c3d6fSHasso Tepper 	request->size = size;
12037f3c3d6fSHasso Tepper 
12047f3c3d6fSHasso Tepper 	dma->flags = _DRM_DMA_USE_SG;
12057f3c3d6fSHasso Tepper 
1206ef130734SFrançois Tigeot 	atomic_dec(&dev->buf_alloc);
12077f3c3d6fSHasso Tepper 	return 0;
12087f3c3d6fSHasso Tepper }
12097f3c3d6fSHasso Tepper 
1210df4baf3dSFrançois Tigeot /**
1211df4baf3dSFrançois Tigeot  * Add buffers for DMA transfers (ioctl).
1212df4baf3dSFrançois Tigeot  *
1213df4baf3dSFrançois Tigeot  * \param inode device inode.
1214df4baf3dSFrançois Tigeot  * \param file_priv DRM file private.
1215df4baf3dSFrançois Tigeot  * \param cmd command.
1216df4baf3dSFrançois Tigeot  * \param arg pointer to a struct drm_buf_desc request.
1217df4baf3dSFrançois Tigeot  * \return zero on success or a negative number on failure.
1218df4baf3dSFrançois Tigeot  *
1219df4baf3dSFrançois Tigeot  * According with the memory type specified in drm_buf_desc::flags and the
1220df4baf3dSFrançois Tigeot  * build options, it dispatches the call either to addbufs_agp(),
1221df4baf3dSFrançois Tigeot  * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1222df4baf3dSFrançois Tigeot  * PCI memory respectively.
1223df4baf3dSFrançois Tigeot  */
drm_legacy_addbufs(struct drm_device * dev,void * data,struct drm_file * file_priv)12241b13d190SFrançois Tigeot int drm_legacy_addbufs(struct drm_device *dev, void *data,
1225df4baf3dSFrançois Tigeot 		       struct drm_file *file_priv)
12267f3c3d6fSHasso Tepper {
1227b3705d71SHasso Tepper 	struct drm_buf_desc *request = data;
1228ef130734SFrançois Tigeot 	int ret;
12297f3c3d6fSHasso Tepper 
12301dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1231ef130734SFrançois Tigeot 		return -EINVAL;
1232ef130734SFrançois Tigeot 
1233ef130734SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1234ef130734SFrançois Tigeot 		return -EINVAL;
1235ef130734SFrançois Tigeot 
1236ef130734SFrançois Tigeot #if IS_ENABLED(CONFIG_AGP)
12377f3c3d6fSHasso Tepper 	if (request->flags & _DRM_AGP_BUFFER)
1238ef130734SFrançois Tigeot 		ret = drm_legacy_addbufs_agp(dev, request);
12397f3c3d6fSHasso Tepper 	else
1240ef130734SFrançois Tigeot #endif
1241ef130734SFrançois Tigeot 	if (request->flags & _DRM_SG_BUFFER)
1242ef130734SFrançois Tigeot 		ret = drm_legacy_addbufs_sg(dev, request);
1243ef130734SFrançois Tigeot 	else if (request->flags & _DRM_FB_BUFFER)
1244ef130734SFrançois Tigeot 		ret = -EINVAL;
1245ef130734SFrançois Tigeot 	else
1246ef130734SFrançois Tigeot 		ret = drm_legacy_addbufs_pci(dev, request);
12477f3c3d6fSHasso Tepper 
1248ef130734SFrançois Tigeot 	return ret;
12497f3c3d6fSHasso Tepper }
12507f3c3d6fSHasso Tepper 
1251df4baf3dSFrançois Tigeot /**
1252df4baf3dSFrançois Tigeot  * Get information about the buffer mappings.
1253df4baf3dSFrançois Tigeot  *
1254df4baf3dSFrançois Tigeot  * This was originally mean for debugging purposes, or by a sophisticated
1255df4baf3dSFrançois Tigeot  * client library to determine how best to use the available buffers (e.g.,
1256df4baf3dSFrançois Tigeot  * large buffers can be used for image transfer).
1257df4baf3dSFrançois Tigeot  *
1258df4baf3dSFrançois Tigeot  * \param inode device inode.
1259df4baf3dSFrançois Tigeot  * \param file_priv DRM file private.
1260df4baf3dSFrançois Tigeot  * \param cmd command.
1261df4baf3dSFrançois Tigeot  * \param arg pointer to a drm_buf_info structure.
1262df4baf3dSFrançois Tigeot  * \return zero on success or a negative number on failure.
1263df4baf3dSFrançois Tigeot  *
126424edb884SFrançois Tigeot  * Increments drm_device::buf_use while holding the drm_device::buf_lock
1265df4baf3dSFrançois Tigeot  * lock, preventing of allocating more buffers after this call. Information
1266df4baf3dSFrançois Tigeot  * about each requested buffer is then copied into user space.
1267df4baf3dSFrançois Tigeot  */
__drm_legacy_infobufs(struct drm_device * dev,void * data,int * p,int (* f)(void *,int,struct drm_buf_entry *))1268*3f2dd94aSFrançois Tigeot int __drm_legacy_infobufs(struct drm_device *dev,
1269*3f2dd94aSFrançois Tigeot 			void *data, int *p,
1270*3f2dd94aSFrançois Tigeot 			int (*f)(void *, int, struct drm_buf_entry *))
12717f3c3d6fSHasso Tepper {
127224edb884SFrançois Tigeot 	struct drm_device_dma *dma = dev->dma;
12737f3c3d6fSHasso Tepper 	int i;
12747f3c3d6fSHasso Tepper 	int count;
12757f3c3d6fSHasso Tepper 
12761dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
127724edb884SFrançois Tigeot 		return -EINVAL;
127824edb884SFrançois Tigeot 
127924edb884SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
128024edb884SFrançois Tigeot 		return -EINVAL;
128124edb884SFrançois Tigeot 
128224edb884SFrançois Tigeot 	if (!dma)
128324edb884SFrançois Tigeot 		return -EINVAL;
128424edb884SFrançois Tigeot 
1285ec5b6af4SFrançois Tigeot 	lockmgr(&dev->buf_lock, LK_EXCLUSIVE);
128624edb884SFrançois Tigeot 	if (atomic_read(&dev->buf_alloc)) {
1287ec5b6af4SFrançois Tigeot 		lockmgr(&dev->buf_lock, LK_RELEASE);
128824edb884SFrançois Tigeot 		return -EBUSY;
128924edb884SFrançois Tigeot 	}
12907f3c3d6fSHasso Tepper 	++dev->buf_use;		/* Can't allocate more after this call */
1291ec5b6af4SFrançois Tigeot 	lockmgr(&dev->buf_lock, LK_RELEASE);
12927f3c3d6fSHasso Tepper 
12937f3c3d6fSHasso Tepper 	for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1294b3705d71SHasso Tepper 		if (dma->bufs[i].buf_count)
1295b3705d71SHasso Tepper 			++count;
12967f3c3d6fSHasso Tepper 	}
12977f3c3d6fSHasso Tepper 
12987f3c3d6fSHasso Tepper 	DRM_DEBUG("count = %d\n", count);
12997f3c3d6fSHasso Tepper 
1300*3f2dd94aSFrançois Tigeot 	if (*p >= count) {
13017f3c3d6fSHasso Tepper 		for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
130224edb884SFrançois Tigeot 			struct drm_buf_entry *from = &dma->bufs[i];
1303*3f2dd94aSFrançois Tigeot 			if (from->buf_count) {
1304*3f2dd94aSFrançois Tigeot 				if (f(data, count, from) < 0)
130524edb884SFrançois Tigeot 					return -EFAULT;
13067f3c3d6fSHasso Tepper 				DRM_DEBUG("%d %d %d %d %d\n",
130724edb884SFrançois Tigeot 					  i,
130824edb884SFrançois Tigeot 					  dma->bufs[i].buf_count,
13097f3c3d6fSHasso Tepper 					  dma->bufs[i].buf_size,
131024edb884SFrançois Tigeot 					  dma->bufs[i].low_mark,
131124edb884SFrançois Tigeot 					  dma->bufs[i].high_mark);
13127f3c3d6fSHasso Tepper 				++count;
13137f3c3d6fSHasso Tepper 			}
13147f3c3d6fSHasso Tepper 		}
13157f3c3d6fSHasso Tepper 	}
1316*3f2dd94aSFrançois Tigeot 	*p = count;
13177f3c3d6fSHasso Tepper 
131824edb884SFrançois Tigeot 	return 0;
13197f3c3d6fSHasso Tepper }
13207f3c3d6fSHasso Tepper 
copy_one_buf(void * data,int count,struct drm_buf_entry * from)1321*3f2dd94aSFrançois Tigeot static int copy_one_buf(void *data, int count, struct drm_buf_entry *from)
1322*3f2dd94aSFrançois Tigeot {
1323*3f2dd94aSFrançois Tigeot 	struct drm_buf_info *request = data;
1324*3f2dd94aSFrançois Tigeot 	struct drm_buf_desc __user *to = &request->list[count];
1325*3f2dd94aSFrançois Tigeot 	struct drm_buf_desc v = {.count = from->buf_count,
1326*3f2dd94aSFrançois Tigeot 				 .size = from->buf_size,
1327*3f2dd94aSFrançois Tigeot 				 .low_mark = from->low_mark,
1328*3f2dd94aSFrançois Tigeot 				 .high_mark = from->high_mark};
1329*3f2dd94aSFrançois Tigeot 	return copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags));
1330*3f2dd94aSFrançois Tigeot }
1331*3f2dd94aSFrançois Tigeot 
drm_legacy_infobufs(struct drm_device * dev,void * data,struct drm_file * file_priv)1332*3f2dd94aSFrançois Tigeot int drm_legacy_infobufs(struct drm_device *dev, void *data,
1333*3f2dd94aSFrançois Tigeot 			struct drm_file *file_priv)
1334*3f2dd94aSFrançois Tigeot {
1335*3f2dd94aSFrançois Tigeot 	struct drm_buf_info *request = data;
1336*3f2dd94aSFrançois Tigeot 	return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
1337*3f2dd94aSFrançois Tigeot }
1338*3f2dd94aSFrançois Tigeot 
1339df4baf3dSFrançois Tigeot /**
1340df4baf3dSFrançois Tigeot  * Specifies a low and high water mark for buffer allocation
1341df4baf3dSFrançois Tigeot  *
1342df4baf3dSFrançois Tigeot  * \param inode device inode.
1343df4baf3dSFrançois Tigeot  * \param file_priv DRM file private.
1344df4baf3dSFrançois Tigeot  * \param cmd command.
1345df4baf3dSFrançois Tigeot  * \param arg a pointer to a drm_buf_desc structure.
1346df4baf3dSFrançois Tigeot  * \return zero on success or a negative number on failure.
1347df4baf3dSFrançois Tigeot  *
1348df4baf3dSFrançois Tigeot  * Verifies that the size order is bounded between the admissible orders and
1349df4baf3dSFrançois Tigeot  * updates the respective drm_device_dma::bufs entry low and high water mark.
1350df4baf3dSFrançois Tigeot  *
1351df4baf3dSFrançois Tigeot  * \note This ioctl is deprecated and mostly never used.
1352df4baf3dSFrançois Tigeot  */
drm_legacy_markbufs(struct drm_device * dev,void * data,struct drm_file * file_priv)13531b13d190SFrançois Tigeot int drm_legacy_markbufs(struct drm_device *dev, void *data,
1354df4baf3dSFrançois Tigeot 			struct drm_file *file_priv)
13557f3c3d6fSHasso Tepper {
135624edb884SFrançois Tigeot 	struct drm_device_dma *dma = dev->dma;
1357b3705d71SHasso Tepper 	struct drm_buf_desc *request = data;
13587f3c3d6fSHasso Tepper 	int order;
135924edb884SFrançois Tigeot 	struct drm_buf_entry *entry;
136024edb884SFrançois Tigeot 
13611dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
136224edb884SFrançois Tigeot 		return -EINVAL;
136324edb884SFrançois Tigeot 
136424edb884SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
136524edb884SFrançois Tigeot 		return -EINVAL;
136624edb884SFrançois Tigeot 
136724edb884SFrançois Tigeot 	if (!dma)
136824edb884SFrançois Tigeot 		return -EINVAL;
13697f3c3d6fSHasso Tepper 
13707f3c3d6fSHasso Tepper 	DRM_DEBUG("%d, %d, %d\n",
13717f3c3d6fSHasso Tepper 		  request->size, request->low_mark, request->high_mark);
13724cd92098Szrj 	order = order_base_2(request->size);
137324edb884SFrançois Tigeot 	if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
137424edb884SFrançois Tigeot 		return -EINVAL;
137524edb884SFrançois Tigeot 	entry = &dma->bufs[order];
13767f3c3d6fSHasso Tepper 
137724edb884SFrançois Tigeot 	if (request->low_mark < 0 || request->low_mark > entry->buf_count)
137824edb884SFrançois Tigeot 		return -EINVAL;
137924edb884SFrançois Tigeot 	if (request->high_mark < 0 || request->high_mark > entry->buf_count)
138024edb884SFrançois Tigeot 		return -EINVAL;
13817f3c3d6fSHasso Tepper 
138224edb884SFrançois Tigeot 	entry->low_mark = request->low_mark;
138324edb884SFrançois Tigeot 	entry->high_mark = request->high_mark;
13847f3c3d6fSHasso Tepper 
13857f3c3d6fSHasso Tepper 	return 0;
13867f3c3d6fSHasso Tepper }
13877f3c3d6fSHasso Tepper 
1388df4baf3dSFrançois Tigeot /**
1389df4baf3dSFrançois Tigeot  * Unreserve the buffers in list, previously reserved using drmDMA.
1390df4baf3dSFrançois Tigeot  *
1391df4baf3dSFrançois Tigeot  * \param inode device inode.
1392df4baf3dSFrançois Tigeot  * \param file_priv DRM file private.
1393df4baf3dSFrançois Tigeot  * \param cmd command.
1394df4baf3dSFrançois Tigeot  * \param arg pointer to a drm_buf_free structure.
1395df4baf3dSFrançois Tigeot  * \return zero on success or a negative number on failure.
1396df4baf3dSFrançois Tigeot  *
1397df4baf3dSFrançois Tigeot  * Calls free_buffer() for each used buffer.
1398df4baf3dSFrançois Tigeot  * This function is primarily used for debugging.
1399df4baf3dSFrançois Tigeot  */
drm_legacy_freebufs(struct drm_device * dev,void * data,struct drm_file * file_priv)14001b13d190SFrançois Tigeot int drm_legacy_freebufs(struct drm_device *dev, void *data,
1401df4baf3dSFrançois Tigeot 			struct drm_file *file_priv)
14027f3c3d6fSHasso Tepper {
14034250aa95Szrj 	struct drm_device_dma *dma = dev->dma;
1404b3705d71SHasso Tepper 	struct drm_buf_free *request = data;
14057f3c3d6fSHasso Tepper 	int i;
14067f3c3d6fSHasso Tepper 	int idx;
14074250aa95Szrj 	struct drm_buf *buf;
1408ef130734SFrançois Tigeot 
14091dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1410ef130734SFrançois Tigeot 		return -EINVAL;
1411ef130734SFrançois Tigeot 
1412ef130734SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1413ef130734SFrançois Tigeot 		return -EINVAL;
1414ef130734SFrançois Tigeot 
1415ef130734SFrançois Tigeot 	if (!dma)
1416ef130734SFrançois Tigeot 		return -EINVAL;
14177f3c3d6fSHasso Tepper 
14187f3c3d6fSHasso Tepper 	DRM_DEBUG("%d\n", request->count);
14197f3c3d6fSHasso Tepper 	for (i = 0; i < request->count; i++) {
1420ef130734SFrançois Tigeot 		if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1421ef130734SFrançois Tigeot 			return -EFAULT;
14227f3c3d6fSHasso Tepper 		if (idx < 0 || idx >= dma->buf_count) {
14237f3c3d6fSHasso Tepper 			DRM_ERROR("Index %d (of %d max)\n",
14247f3c3d6fSHasso Tepper 				  idx, dma->buf_count - 1);
1425ef130734SFrançois Tigeot 			return -EINVAL;
14267f3c3d6fSHasso Tepper 		}
14277f3c3d6fSHasso Tepper 		buf = dma->buflist[idx];
14287f3c3d6fSHasso Tepper 		if (buf->file_priv != file_priv) {
14297f3c3d6fSHasso Tepper 			DRM_ERROR("Process %d freeing buffer not owned\n",
14307f3c3d6fSHasso Tepper 				  DRM_CURRENTPID);
1431ef130734SFrançois Tigeot 			return -EINVAL;
14327f3c3d6fSHasso Tepper 		}
14331b13d190SFrançois Tigeot 		drm_legacy_free_buffer(dev, buf);
14347f3c3d6fSHasso Tepper 	}
14357f3c3d6fSHasso Tepper 
1436ef130734SFrançois Tigeot 	return 0;
14377f3c3d6fSHasso Tepper }
14387f3c3d6fSHasso Tepper 
1439df4baf3dSFrançois Tigeot /**
1440df4baf3dSFrançois Tigeot  * Maps all of the DMA buffers into client-virtual space (ioctl).
1441df4baf3dSFrançois Tigeot  *
1442df4baf3dSFrançois Tigeot  * \param inode device inode.
1443df4baf3dSFrançois Tigeot  * \param file_priv DRM file private.
1444df4baf3dSFrançois Tigeot  * \param cmd command.
1445df4baf3dSFrançois Tigeot  * \param arg pointer to a drm_buf_map structure.
1446df4baf3dSFrançois Tigeot  * \return zero on success or a negative number on failure.
1447df4baf3dSFrançois Tigeot  *
1448df4baf3dSFrançois Tigeot  * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1449df4baf3dSFrançois Tigeot  * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1450df4baf3dSFrançois Tigeot  * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1451df4baf3dSFrançois Tigeot  * drm_mmap_dma().
1452df4baf3dSFrançois Tigeot  */
__drm_legacy_mapbufs(struct drm_device * dev,void * data,int * p,void __user ** v,int (* f)(void *,int,unsigned long,struct drm_buf *),struct drm_file * file_priv)1453*3f2dd94aSFrançois Tigeot int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p,
1454*3f2dd94aSFrançois Tigeot 			 void __user **v,
1455*3f2dd94aSFrançois Tigeot 			 int (*f)(void *, int, unsigned long,
1456*3f2dd94aSFrançois Tigeot 				  struct drm_buf *),
1457df4baf3dSFrançois Tigeot 		         struct drm_file *file_priv)
14587f3c3d6fSHasso Tepper {
1459*3f2dd94aSFrançois Tigeot #ifndef __DragonFly__
14604250aa95Szrj 	struct drm_device_dma *dma = dev->dma;
14617f3c3d6fSHasso Tepper 	int retcode = 0;
1462ef130734SFrançois Tigeot 	unsigned long virtual;
14637f3c3d6fSHasso Tepper 	int i;
14647f3c3d6fSHasso Tepper 
14651dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
146688fc0f68SFrançois Tigeot 		return -EINVAL;
146788fc0f68SFrançois Tigeot 
146888fc0f68SFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
146988fc0f68SFrançois Tigeot 		return -EINVAL;
147088fc0f68SFrançois Tigeot 
1471394ce297SFrançois Tigeot 	if (!dma)
1472394ce297SFrançois Tigeot 		return -EINVAL;
1473394ce297SFrançois Tigeot 
1474ec5b6af4SFrançois Tigeot 	lockmgr(&dev->buf_lock, LK_EXCLUSIVE);
1475394ce297SFrançois Tigeot 	if (atomic_read(&dev->buf_alloc)) {
1476ec5b6af4SFrançois Tigeot 		lockmgr(&dev->buf_lock, LK_RELEASE);
1477394ce297SFrançois Tigeot 		return -EBUSY;
1478394ce297SFrançois Tigeot 	}
14797f3c3d6fSHasso Tepper 	dev->buf_use++;		/* Can't allocate more after this call */
1480ec5b6af4SFrançois Tigeot 	lockmgr(&dev->buf_lock, LK_RELEASE);
14817f3c3d6fSHasso Tepper 
1482*3f2dd94aSFrançois Tigeot 	if (*p >= dma->buf_count) {
1483ef130734SFrançois Tigeot 		if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
1484ef130734SFrançois Tigeot 		    || (drm_core_check_feature(dev, DRIVER_SG)
1485ef130734SFrançois Tigeot 			&& (dma->flags & _DRM_DMA_USE_SG))) {
1486ef130734SFrançois Tigeot 			struct drm_local_map *map = dev->agp_buffer_map;
1487ef130734SFrançois Tigeot 			unsigned long token = dev->agp_buffer_token;
14887f3c3d6fSHasso Tepper 
1489ef130734SFrançois Tigeot 			if (!map) {
1490b922632fSImre Vadász 				retcode = -EINVAL;
14917f3c3d6fSHasso Tepper 				goto done;
14927f3c3d6fSHasso Tepper 			}
1493*3f2dd94aSFrançois Tigeot 			virtual = vm_mmap(file_priv->filp, 0, map->size,
1494ef130734SFrançois Tigeot 					  PROT_READ | PROT_WRITE,
1495ef130734SFrançois Tigeot 					  MAP_SHARED,
149638284916SSascha Wildner 					  token, NULL);
14977f3c3d6fSHasso Tepper 		} else {
1498*3f2dd94aSFrançois Tigeot 			virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
1499ef130734SFrançois Tigeot 					  PROT_READ | PROT_WRITE,
1500ef130734SFrançois Tigeot 					  MAP_SHARED,
150138284916SSascha Wildner 					  0, NULL);
15027f3c3d6fSHasso Tepper 		}
1503ef130734SFrançois Tigeot 		if (virtual > -1024UL) {
1504ef130734SFrançois Tigeot 			/* Real error */
1505ef130734SFrançois Tigeot 			retcode = (signed long)virtual;
15067f3c3d6fSHasso Tepper 			goto done;
1507ef130734SFrançois Tigeot 		}
1508*3f2dd94aSFrançois Tigeot 		*v = (void __user *)virtual;
15097f3c3d6fSHasso Tepper 
15107f3c3d6fSHasso Tepper 		for (i = 0; i < dma->buf_count; i++) {
1511*3f2dd94aSFrançois Tigeot 			if (f(data, i, virtual, dma->buflist[i]) < 0) {
1512b922632fSImre Vadász 				retcode = -EFAULT;
15137f3c3d6fSHasso Tepper 				goto done;
15147f3c3d6fSHasso Tepper 			}
15157f3c3d6fSHasso Tepper 		}
1516ef130734SFrançois Tigeot 	}
15177f3c3d6fSHasso Tepper       done:
1518*3f2dd94aSFrançois Tigeot 	*p = dma->buf_count;
1519*3f2dd94aSFrançois Tigeot 	DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode);
15207f3c3d6fSHasso Tepper 
15217f3c3d6fSHasso Tepper 	return retcode;
1522*3f2dd94aSFrançois Tigeot #else
1523*3f2dd94aSFrançois Tigeot 	return -EINVAL;
1524*3f2dd94aSFrançois Tigeot #endif
1525*3f2dd94aSFrançois Tigeot }
1526*3f2dd94aSFrançois Tigeot 
map_one_buf(void * data,int idx,unsigned long virtual,struct drm_buf * buf)1527*3f2dd94aSFrançois Tigeot static int map_one_buf(void *data, int idx, unsigned long virtual,
1528*3f2dd94aSFrançois Tigeot 			struct drm_buf *buf)
1529*3f2dd94aSFrançois Tigeot {
1530*3f2dd94aSFrançois Tigeot #ifndef __DragonFly__
1531*3f2dd94aSFrançois Tigeot 	struct drm_buf_map *request = data;
1532*3f2dd94aSFrançois Tigeot 	unsigned long address = virtual + buf->offset;	/* *** */
1533*3f2dd94aSFrançois Tigeot 
1534*3f2dd94aSFrançois Tigeot 	if (copy_to_user(&request->list[idx].idx, &buf->idx,
1535*3f2dd94aSFrançois Tigeot 			 sizeof(request->list[0].idx)))
1536*3f2dd94aSFrançois Tigeot 		return -EFAULT;
1537*3f2dd94aSFrançois Tigeot 	if (copy_to_user(&request->list[idx].total, &buf->total,
1538*3f2dd94aSFrançois Tigeot 			 sizeof(request->list[0].total)))
1539*3f2dd94aSFrançois Tigeot 		return -EFAULT;
1540*3f2dd94aSFrançois Tigeot 	if (clear_user(&request->list[idx].used, sizeof(int)))
1541*3f2dd94aSFrançois Tigeot 		return -EFAULT;
1542*3f2dd94aSFrançois Tigeot 	if (copy_to_user(&request->list[idx].address, &address,
1543*3f2dd94aSFrançois Tigeot 			 sizeof(address)))
1544*3f2dd94aSFrançois Tigeot 		return -EFAULT;
1545*3f2dd94aSFrançois Tigeot #endif
1546*3f2dd94aSFrançois Tigeot 	return 0;
1547*3f2dd94aSFrançois Tigeot }
1548*3f2dd94aSFrançois Tigeot 
drm_legacy_mapbufs(struct drm_device * dev,void * data,struct drm_file * file_priv)1549*3f2dd94aSFrançois Tigeot int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1550*3f2dd94aSFrançois Tigeot 		       struct drm_file *file_priv)
1551*3f2dd94aSFrançois Tigeot {
1552*3f2dd94aSFrançois Tigeot 	struct drm_buf_map *request = data;
1553*3f2dd94aSFrançois Tigeot 	return __drm_legacy_mapbufs(dev, data, &request->count,
1554*3f2dd94aSFrançois Tigeot 				    &request->virtual, map_one_buf,
1555*3f2dd94aSFrançois Tigeot 				    file_priv);
15567f3c3d6fSHasso Tepper }
15571b13d190SFrançois Tigeot 
drm_legacy_dma_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)15581b13d190SFrançois Tigeot int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
15591b13d190SFrançois Tigeot 		  struct drm_file *file_priv)
15601b13d190SFrançois Tigeot {
15611dedbd3bSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
15621b13d190SFrançois Tigeot 		return -EINVAL;
15631b13d190SFrançois Tigeot 
15641b13d190SFrançois Tigeot 	if (dev->driver->dma_ioctl)
15651b13d190SFrançois Tigeot 		return dev->driver->dma_ioctl(dev, data, file_priv);
15661b13d190SFrançois Tigeot 	else
15671b13d190SFrançois Tigeot 		return -EINVAL;
15681b13d190SFrançois Tigeot }
15691b13d190SFrançois Tigeot 
drm_legacy_getsarea(struct drm_device * dev)15701b13d190SFrançois Tigeot struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
15711b13d190SFrançois Tigeot {
15721b13d190SFrançois Tigeot 	struct drm_map_list *entry;
15731b13d190SFrançois Tigeot 
15741b13d190SFrançois Tigeot 	list_for_each_entry(entry, &dev->maplist, head) {
15751b13d190SFrançois Tigeot 		if (entry->map && entry->map->type == _DRM_SHM &&
15761b13d190SFrançois Tigeot 		    (entry->map->flags & _DRM_CONTAINS_LOCK)) {
15771b13d190SFrançois Tigeot 			return entry->map;
15781b13d190SFrançois Tigeot 		}
15791b13d190SFrançois Tigeot 	}
15801b13d190SFrançois Tigeot 	return NULL;
15811b13d190SFrançois Tigeot }
15821b13d190SFrançois Tigeot EXPORT_SYMBOL(drm_legacy_getsarea);
1583