blob: f7ba82ebf65ae03ead6f466d9f2579cc753a0c95 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_bufs.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Generic buffer template
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
11 *
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
David Millerf1a2a9b2009-02-18 15:41:02 -080038#include <linux/log2.h>
39#include <asm/shmparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "drmP.h"
41
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +110042resource_size_t drm_get_resource_start(struct drm_device *dev, unsigned int resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Dave Airlie836cf042005-07-10 19:27:04 +100044 return pci_resource_start(dev->pdev, resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
Dave Airlie836cf042005-07-10 19:27:04 +100046EXPORT_SYMBOL(drm_get_resource_start);
47
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +110048resource_size_t drm_get_resource_len(struct drm_device *dev, unsigned int resource)
Dave Airlie836cf042005-07-10 19:27:04 +100049{
50 return pci_resource_len(dev->pdev, resource);
51}
Dave Airlieb5e89ed2005-09-25 14:28:13 +100052
Dave Airlie836cf042005-07-10 19:27:04 +100053EXPORT_SYMBOL(drm_get_resource_len);
54
Dave Airlie55910512007-07-11 16:53:40 +100055static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +110056 struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +100057{
Dave Airlie55910512007-07-11 16:53:40 +100058 struct drm_map_list *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +100059 list_for_each_entry(entry, &dev->maplist, head) {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110060 /*
61 * Because the kernel-userspace ABI is fixed at a 32-bit offset
62 * while PCI resources may live above that, we ignore the map
63 * offset for maps of type _DRM_FRAMEBUFFER or _DRM_REGISTERS.
64 * It is assumed that each driver will have only one resource of
65 * each type.
66 */
67 if (!entry->map ||
68 map->type != entry->map->type ||
69 entry->master != dev->primary->master)
70 continue;
71 switch (map->type) {
72 case _DRM_SHM:
73 if (map->flags != _DRM_CONTAINS_LOCK)
74 break;
75 case _DRM_REGISTERS:
76 case _DRM_FRAME_BUFFER:
Dave Airlie89625eb2005-09-05 21:23:23 +100077 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110078 default: /* Make gcc happy */
79 ;
Dave Airlie836cf042005-07-10 19:27:04 +100080 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110081 if (entry->map->offset == map->offset)
82 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100083 }
84
85 return NULL;
86}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Dave Airliee0be4282007-07-12 10:26:44 +100088static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080089 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100090{
David Millerf1a2a9b2009-02-18 15:41:02 -080091 int use_hashed_handle, shift;
92 unsigned long add;
93
Dave Airliec2604ce2006-08-12 16:03:26 +100094#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100095 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
96#elif (BITS_PER_LONG == 32)
97 use_hashed_handle = hashed_handle;
98#else
99#error Unsupported long size. Neither 64 nor 32 bits.
100#endif
Dave Airlied1f2b552005-08-05 22:11:22 +1000101
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000102 if (!use_hashed_handle) {
103 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +1100104 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000105 ret = drm_ht_insert_item(&dev->map_hash, hash);
106 if (ret != -EINVAL)
107 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +1000108 }
David Millerf1a2a9b2009-02-18 15:41:02 -0800109
110 shift = 0;
111 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
112 if (shm && (SHMLBA > PAGE_SIZE)) {
113 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
114
115 /* For shared memory, we have to preserve the SHMLBA
116 * bits of the eventual vma->vm_pgoff value during
117 * mmap(). Otherwise we run into cache aliasing problems
118 * on some platforms. On these platforms, the pgoff of
119 * a mmap() request is used to pick a suitable virtual
120 * address for the mmap() region such that it will not
121 * cause cache aliasing problems.
122 *
123 * Therefore, make sure the SHMLBA relevant bits of the
124 * hash value we use are equal to those in the original
125 * kernel virtual address.
126 */
127 shift = bits;
128 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
129 }
130
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000131 return drm_ht_just_insert_please(&dev->map_hash, hash,
132 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800133 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000134}
Dave Airlie9a186642005-06-23 21:29:18 +1000135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/**
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100137 * Core function to create a range of memory available for mapping by a
138 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
140 * Adjusts the memory offset to its absolute value according to the mapping
141 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
142 * applicable and if supported by the kernel.
143 */
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100144static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000145 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000146 enum drm_map_flags flags,
147 struct drm_map_list ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100149 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000150 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000151 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000152 unsigned long user_token;
153 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Eric Anholt9a298b22009-03-24 12:23:04 -0700155 map = kmalloc(sizeof(*map), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000156 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -ENOMEM;
158
Dave Airlie7ab98402005-07-10 16:56:52 +1000159 map->offset = offset;
160 map->size = size;
161 map->flags = flags;
162 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /* Only allow shared memory to be removable since we only keep enough
165 * book keeping information about shared memory to allow for removal
166 * when processes fork.
167 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000168 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700169 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -EINVAL;
171 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100172 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
173 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000174
175 /* page-align _DRM_SHM maps. They are allocated here so there is no security
176 * hole created by that and it works around various broken drivers that use
177 * a non-aligned quantity to map the SAREA. --BenH
178 */
179 if (map->type == _DRM_SHM)
180 map->size = PAGE_ALIGN(map->size);
181
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100182 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700183 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -EINVAL;
185 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 map->handle = NULL;
188
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000189 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 case _DRM_REGISTERS:
191 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000192#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100193 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 map->offset < virt_to_phys(high_memory)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700195 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -EINVAL;
197 }
198#endif
199#ifdef __alpha__
200 map->offset += dev->hose->mem_space->start;
201#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000202 /* Some drivers preinitialize some maps, without the X Server
203 * needing to be aware of it. Therefore, we just return success
204 * when the server tries to create a duplicate map.
205 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000206 list = drm_find_matching_map(dev, map);
207 if (list != NULL) {
208 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000209 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 "mismatched sizes, (%ld vs %ld)\n",
211 map->type, map->size,
212 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000213 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000214 }
215
Eric Anholt9a298b22009-03-24 12:23:04 -0700216 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000217 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000218 return 0;
219 }
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222 if (map->type == _DRM_FRAME_BUFFER ||
223 (map->flags & _DRM_WRITE_COMBINING)) {
224 map->mtrr = mtrr_add(map->offset, map->size,
225 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 }
Scott Thompson0769d392007-08-25 18:17:49 +1000228 if (map->type == _DRM_REGISTERS) {
Christoph Hellwig004a7722007-01-08 21:56:59 +1100229 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000230 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700231 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000232 return -ENOMEM;
233 }
234 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100238 list = drm_find_matching_map(dev, map);
239 if (list != NULL) {
240 if(list->map->size != map->size) {
241 DRM_DEBUG("Matching maps of type %d with "
242 "mismatched sizes, (%ld vs %ld)\n",
243 map->type, map->size, list->map->size);
244 list->map->size = map->size;
245 }
246
Eric Anholt9a298b22009-03-24 12:23:04 -0700247 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100248 *maplist = list;
249 return 0;
250 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100251 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000252 DRM_DEBUG("%lu %d %p\n",
253 map->size, drm_order(map->size), map->handle);
254 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700255 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return -ENOMEM;
257 }
258 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000259 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000261 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000262 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700263 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return -EBUSY;
265 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000266 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100269 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000270 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100271 int valid = 0;
272
273 if (!drm_core_has_AGP(dev)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700274 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100275 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100277#ifdef __alpha__
278 map->offset += dev->hose->mem_space->start;
279#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000280 /* In some cases (i810 driver), user space may have already
281 * added the AGP base itself, because dev->agp->base previously
282 * only got set during AGP enable. So, only add the base
283 * address if the map's offset isn't already within the
284 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100285 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000286 if (map->offset < dev->agp->base ||
287 map->offset > dev->agp->base +
288 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
289 map->offset += dev->agp->base;
290 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100291 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
292
293 /* This assumes the DRM is in total control of AGP space.
294 * It's not always the case as AGP can be in the control
295 * of user space (i.e. i810 driver). So this loop will get
296 * skipped and we double check that dev->agp->memory is
297 * actually set as well as being invalid before EPERM'ing
298 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000299 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100300 if ((map->offset >= entry->bound) &&
301 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
302 valid = 1;
303 break;
304 }
305 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000306 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700307 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100308 return -EPERM;
309 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100310 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
311 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100314 }
Pekka Paalanen812c3692009-09-17 22:59:54 +0300315 case _DRM_GEM:
316 DRM_ERROR("tried to addmap GEM object\n");
317 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 case _DRM_SCATTER_GATHER:
319 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700320 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -EINVAL;
322 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000323 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000325 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000326 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000327 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000328 * casting it down to 32 bits is no problem, but we
329 * need to point to a 64bit variable first. */
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800330 dmah = drm_pci_alloc(dev, map->size, map->size);
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000331 if (!dmah) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700332 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000333 return -ENOMEM;
334 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000335 map->handle = dmah->vaddr;
336 map->offset = (unsigned long)dmah->busaddr;
337 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700340 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -EINVAL;
342 }
343
Eric Anholt9a298b22009-03-24 12:23:04 -0700344 list = kmalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000345 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700346 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100347 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700348 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return -EINVAL;
350 }
351 memset(list, 0, sizeof(*list));
352 list->map = map;
353
Dave Airlie30e2fb12006-02-02 19:37:46 +1100354 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000355 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000356
Dave Airlied1f2b552005-08-05 22:11:22 +1000357 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100358 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000359 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
360 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800361 ret = drm_map_handle(dev, &list->hash, user_token, 0,
362 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000363 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700364 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100365 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700366 kfree(map);
367 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000368 mutex_unlock(&dev->struct_mutex);
369 return ret;
370 }
371
Thomas Hellstrom15450852007-02-08 16:14:05 +1100372 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100373 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Ben Skeggs2ff2e8a2009-05-26 10:35:52 +1000375 if (!(map->flags & _DRM_DRIVER))
376 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000377 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000378 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100379 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000380
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100381int drm_addmap(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000382 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100383 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000384{
Dave Airlie55910512007-07-11 16:53:40 +1000385 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000386 int rc;
387
388 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
389 if (!rc)
390 *map_ptr = list->map;
391 return rc;
392}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000393
Dave Airlie7ab98402005-07-10 16:56:52 +1000394EXPORT_SYMBOL(drm_addmap);
395
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100396/**
397 * Ioctl to specify a range of memory that is available for mapping by a
398 * non-root process.
399 *
400 * \param inode device inode.
401 * \param file_priv DRM file private.
402 * \param cmd command.
403 * \param arg pointer to a drm_map structure.
404 * \return zero on success or a negative value on error.
405 *
406 */
Eric Anholtc153f452007-09-03 12:06:45 +1000407int drm_addmap_ioctl(struct drm_device *dev, void *data,
408 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000409{
Eric Anholtc153f452007-09-03 12:06:45 +1000410 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000411 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000412 int err;
413
Dave Airlie7c1c2872008-11-28 14:22:24 +1000414 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100415 return -EPERM;
416
Eric Anholtc153f452007-09-03 12:06:45 +1000417 err = drm_addmap_core(dev, map->offset, map->size, map->type,
418 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000419
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000420 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000421 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000422
Dave Airlie67e1a012005-10-24 18:41:39 +1000423 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
Eric Anholtc153f452007-09-03 12:06:45 +1000424 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000426}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/**
429 * Remove a map private from list and deallocate resources if the mapping
430 * isn't in use.
431 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * Searches the map on drm_device::maplist, removes it from the list, see if
433 * its being used, and free any associate resource (such as MTRR's) if it's not
434 * being on use.
435 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000436 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100438int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Dave Airlie55910512007-07-11 16:53:40 +1000440 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000441 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000442 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000443 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dave Airlie836cf042005-07-10 19:27:04 +1000445 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000446 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000447 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000448 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000449 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100450 drm_ht_remove_key(&dev->map_hash,
451 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700452 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000453 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Dave Airlie836cf042005-07-10 19:27:04 +1000457
Dave Airliebd1b3312007-05-26 05:01:51 +1000458 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000459 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000460
461 switch (map->type) {
462 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100463 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000464 /* FALLTHROUGH */
465 case _DRM_FRAME_BUFFER:
466 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
467 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468 retcode = mtrr_del(map->mtrr, map->offset, map->size);
469 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000470 }
471 break;
472 case _DRM_SHM:
473 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000474 if (master) {
475 if (dev->sigdata.lock == master->lock.hw_lock)
476 dev->sigdata.lock = NULL;
477 master->lock.hw_lock = NULL; /* SHM removed */
478 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100479 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000480 }
Dave Airlie836cf042005-07-10 19:27:04 +1000481 break;
482 case _DRM_AGP:
483 case _DRM_SCATTER_GATHER:
484 break;
485 case _DRM_CONSISTENT:
486 dmah.vaddr = map->handle;
487 dmah.busaddr = map->offset;
488 dmah.size = map->size;
489 __drm_pci_free(dev, &dmah);
490 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800491 case _DRM_GEM:
492 DRM_ERROR("tried to rmmap GEM object\n");
493 break;
Dave Airlie836cf042005-07-10 19:27:04 +1000494 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700495 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return 0;
498}
Dave Airlie4e74f362008-12-19 10:23:14 +1100499EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000500
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100501int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000502{
503 int ret;
504
Dave Airlie30e2fb12006-02-02 19:37:46 +1100505 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000506 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100507 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000508
509 return ret;
510}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000511EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000512
Dave Airlie836cf042005-07-10 19:27:04 +1000513/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
514 * the last close of the device, and this is necessary for cleanup when things
515 * exit uncleanly. Therefore, having userland manually remove mappings seems
516 * like a pointless exercise since they're going away anyway.
517 *
518 * One use case might be after addmap is allowed for normal users for SHM and
519 * gets used by drivers that the server doesn't need to care about. This seems
520 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100521 *
522 * \param inode device inode.
523 * \param file_priv DRM file private.
524 * \param cmd command.
525 * \param arg pointer to a struct drm_map structure.
526 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000527 */
Eric Anholtc153f452007-09-03 12:06:45 +1000528int drm_rmmap_ioctl(struct drm_device *dev, void *data,
529 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000530{
Eric Anholtc153f452007-09-03 12:06:45 +1000531 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100532 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000533 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000534 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000535
Dave Airlie30e2fb12006-02-02 19:37:46 +1100536 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000537 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000538 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000539 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000540 r_list->map->flags & _DRM_REMOVABLE) {
541 map = r_list->map;
542 break;
543 }
544 }
545
546 /* List has wrapped around to the head pointer, or its empty we didn't
547 * find anything.
548 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000549 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100550 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000551 return -EINVAL;
552 }
553
Dave Airlie836cf042005-07-10 19:27:04 +1000554 /* Register and framebuffer maps are permanent */
555 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100556 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000557 return 0;
558 }
559
560 ret = drm_rmmap_locked(dev, map);
561
Dave Airlie30e2fb12006-02-02 19:37:46 +1100562 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000563
564 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000565}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567/**
568 * Cleanup after an error on one of the addbufs() functions.
569 *
Dave Airlie836cf042005-07-10 19:27:04 +1000570 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * \param entry buffer entry where the error occurred.
572 *
573 * Frees any pages and buffers associated with the given entry.
574 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000575static void drm_cleanup_buf_error(struct drm_device * dev,
576 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 int i;
579
580 if (entry->seg_count) {
581 for (i = 0; i < entry->seg_count; i++) {
582 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100583 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700586 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 entry->seg_count = 0;
589 }
590
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 if (entry->buf_count) {
592 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700593 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700595 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 entry->buf_count = 0;
598 }
599}
600
601#if __OS_HAS_AGP
602/**
Dave Airlied59431b2005-07-10 15:00:06 +1000603 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000605 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000606 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * After some sanity checks creates a drm_buf structure for each buffer and
610 * reallocates the buffer list of the same size order to accommodate the new
611 * buffers.
612 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000613int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Dave Airliecdd55a22007-07-11 16:32:08 +1000615 struct drm_device_dma *dma = dev->dma;
616 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000617 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000618 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 unsigned long offset;
620 unsigned long agp_offset;
621 int count;
622 int order;
623 int size;
624 int alignment;
625 int page_order;
626 int total;
627 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100628 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000629 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631 if (!dma)
632 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Dave Airlied59431b2005-07-10 15:00:06 +1000634 count = request->count;
635 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 size = 1 << order;
637
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 alignment = (request->flags & _DRM_PAGE_ALIGN)
639 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
641 total = PAGE_SIZE << page_order;
642
643 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000644 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000646 DRM_DEBUG("count: %d\n", count);
647 DRM_DEBUG("order: %d\n", order);
648 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100649 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 DRM_DEBUG("alignment: %d\n", alignment);
651 DRM_DEBUG("page_order: %d\n", page_order);
652 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000654 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
655 return -EINVAL;
656 if (dev->queue_count)
657 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Dave Airlie54ba2f72007-02-10 12:07:47 +1100659 /* Make sure buffers are located in AGP memory that we own */
660 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000661 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100662 if ((agp_offset >= agp_entry->bound) &&
663 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
664 valid = 1;
665 break;
666 }
667 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000668 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100669 DRM_DEBUG("zone invalid\n");
670 return -EINVAL;
671 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000672 spin_lock(&dev->count_lock);
673 if (dev->buf_use) {
674 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -EBUSY;
676 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 atomic_inc(&dev->buf_alloc);
678 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dave Airlie30e2fb12006-02-02 19:37:46 +1100680 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100683 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000684 atomic_dec(&dev->buf_alloc);
685 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100689 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return -EINVAL;
692 }
693
Eric Anholt9a298b22009-03-24 12:23:04 -0700694 entry->buflist = kmalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000695 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100696 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000697 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -ENOMEM;
699 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 entry->buf_size = size;
703 entry->page_order = page_order;
704
705 offset = 0;
706
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 while (entry->buf_count < count) {
708 buf = &entry->buflist[entry->buf_count];
709 buf->idx = dma->buf_count + entry->buf_count;
710 buf->total = alignment;
711 buf->order = order;
712 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 buf->bus_address = agp_offset + offset;
716 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000717 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 buf->waiting = 0;
719 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000721 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -0700724 buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Set count correctly so we free the proper amount. */
727 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100729 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return -ENOMEM;
732 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000733 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000735 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 offset += alignment;
738 entry->buf_count++;
739 byte_count += PAGE_SIZE << page_order;
740 }
741
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Eric Anholt9a298b22009-03-24 12:23:04 -0700744 temp_buflist = krealloc(dma->buflist,
745 (dma->buf_count + entry->buf_count) *
746 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000749 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100750 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000751 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return -ENOMEM;
753 }
754 dma->buflist = temp_buflist;
755
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
758 }
759
760 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100761 dma->seg_count += entry->seg_count;
762 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 dma->byte_count += byte_count;
764
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
766 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Dave Airlie30e2fb12006-02-02 19:37:46 +1100768 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Dave Airlied59431b2005-07-10 15:00:06 +1000770 request->count = entry->buf_count;
771 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 dma->flags = _DRM_DMA_USE_AGP;
774
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 0;
777}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000778EXPORT_SYMBOL(drm_addbufs_agp);
779#endif /* __OS_HAS_AGP */
780
Dave Airlie84b1fd12007-07-11 15:53:27 +1000781int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Dave Airliecdd55a22007-07-11 16:32:08 +1000783 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int count;
785 int order;
786 int size;
787 int total;
788 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000789 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100790 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000791 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int alignment;
793 unsigned long offset;
794 int i;
795 int byte_count;
796 int page_count;
797 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000798 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
801 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100802
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 if (!dma)
804 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Dave Airlied985c102006-01-02 21:32:48 +1100806 if (!capable(CAP_SYS_ADMIN))
807 return -EPERM;
808
Dave Airlied59431b2005-07-10 15:00:06 +1000809 count = request->count;
810 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 size = 1 << order;
812
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
814 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000816 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
817 return -EINVAL;
818 if (dev->queue_count)
819 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Dave Airlied59431b2005-07-10 15:00:06 +1000821 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
824 total = PAGE_SIZE << page_order;
825
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 spin_lock(&dev->count_lock);
827 if (dev->buf_use) {
828 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return -EBUSY;
830 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 atomic_inc(&dev->buf_alloc);
832 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Dave Airlie30e2fb12006-02-02 19:37:46 +1100834 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100837 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -ENOMEM; /* May only call once for each order */
840 }
841
842 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100843 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return -EINVAL;
846 }
847
Eric Anholt9a298b22009-03-24 12:23:04 -0700848 entry->buflist = kmalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100850 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000851 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return -ENOMEM;
853 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000854 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Eric Anholt9a298b22009-03-24 12:23:04 -0700856 entry->seglist = kmalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700858 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100859 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -ENOMEM;
862 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* Keep the original pagelist until we know all the allocations
866 * have succeeded
867 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700868 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
869 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700871 kfree(entry->buflist);
872 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100873 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return -ENOMEM;
876 }
877 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000878 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
879 DRM_DEBUG("pagelist: %d entries\n",
880 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 entry->page_order = page_order;
884 byte_count = 0;
885 page_count = 0;
886
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000888
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800889 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000890
Dave Airlieddf19b92006-03-19 18:56:12 +1100891 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* Set count correctly so we free the proper amount. */
893 entry->buf_count = count;
894 entry->seg_count = count;
895 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700896 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100897 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return -ENOMEM;
900 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100901 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000902 for (i = 0; i < (1 << page_order); i++) {
903 DRM_DEBUG("page %d @ 0x%08lx\n",
904 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100905 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100907 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 for (offset = 0;
910 offset + size <= total && entry->buf_count < count;
911 offset += alignment, ++entry->buf_count) {
912 buf = &entry->buflist[entry->buf_count];
913 buf->idx = dma->buf_count + entry->buf_count;
914 buf->total = alignment;
915 buf->order = order;
916 buf->used = 0;
917 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100918 buf->address = (void *)(dmah->vaddr + offset);
919 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 buf->waiting = 0;
922 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000923 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000924 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -0700927 buf->dev_private = kmalloc(buf->dev_priv_size,
928 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* Set count correctly so we free the proper amount. */
931 entry->buf_count = count;
932 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000933 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700934 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100935 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return -ENOMEM;
938 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000941 DRM_DEBUG("buffer %d @ %p\n",
942 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 byte_count += PAGE_SIZE << page_order;
945 }
946
Eric Anholt9a298b22009-03-24 12:23:04 -0700947 temp_buflist = krealloc(dma->buflist,
948 (dma->buf_count + entry->buf_count) *
949 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!temp_buflist) {
951 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700953 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100954 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -ENOMEM;
957 }
958 dma->buflist = temp_buflist;
959
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000960 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
962 }
963
964 /* No allocations failed, so now we can replace the orginal pagelist
965 * with the new one.
966 */
967 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700968 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 dma->pagelist = temp_pagelist;
971
972 dma->buf_count += entry->buf_count;
973 dma->seg_count += entry->seg_count;
974 dma->page_count += entry->seg_count << page_order;
975 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
976
Dave Airlie30e2fb12006-02-02 19:37:46 +1100977 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Dave Airlied59431b2005-07-10 15:00:06 +1000979 request->count = entry->buf_count;
980 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
George Sapountzis3417f332006-10-24 12:03:04 -0700982 if (request->flags & _DRM_PCI_BUFFER_RO)
983 dma->flags = _DRM_DMA_USE_PCI_RO;
984
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000985 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return 0;
987
988}
Dave Airlied84f76d2005-07-10 17:04:22 +1000989EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Dave Airlie84b1fd12007-07-11 15:53:27 +1000991static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Dave Airliecdd55a22007-07-11 16:32:08 +1000993 struct drm_device_dma *dma = dev->dma;
994 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000995 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 unsigned long offset;
997 unsigned long agp_offset;
998 int count;
999 int order;
1000 int size;
1001 int alignment;
1002 int page_order;
1003 int total;
1004 int byte_count;
1005 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001006 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 if (!drm_core_check_feature(dev, DRIVER_SG))
1009 return -EINVAL;
1010
1011 if (!dma)
1012 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Dave Airlied985c102006-01-02 21:32:48 +11001014 if (!capable(CAP_SYS_ADMIN))
1015 return -EPERM;
1016
Dave Airlied59431b2005-07-10 15:00:06 +10001017 count = request->count;
1018 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 size = 1 << order;
1020
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 alignment = (request->flags & _DRM_PAGE_ALIGN)
1022 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1024 total = PAGE_SIZE << page_order;
1025
1026 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001027 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 DRM_DEBUG("count: %d\n", count);
1030 DRM_DEBUG("order: %d\n", order);
1031 DRM_DEBUG("size: %d\n", size);
1032 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1033 DRM_DEBUG("alignment: %d\n", alignment);
1034 DRM_DEBUG("page_order: %d\n", page_order);
1035 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1038 return -EINVAL;
1039 if (dev->queue_count)
1040 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 spin_lock(&dev->count_lock);
1043 if (dev->buf_use) {
1044 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return -EBUSY;
1046 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 atomic_inc(&dev->buf_alloc);
1048 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Dave Airlie30e2fb12006-02-02 19:37:46 +11001050 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001053 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001054 atomic_dec(&dev->buf_alloc);
1055 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
1058 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001059 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001060 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return -EINVAL;
1062 }
1063
Eric Anholt9a298b22009-03-24 12:23:04 -07001064 entry->buflist = kmalloc(count * sizeof(*entry->buflist),
1065 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001067 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -ENOMEM;
1070 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001071 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 entry->buf_size = size;
1074 entry->page_order = page_order;
1075
1076 offset = 0;
1077
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 while (entry->buf_count < count) {
1079 buf = &entry->buflist[entry->buf_count];
1080 buf->idx = dma->buf_count + entry->buf_count;
1081 buf->total = alignment;
1082 buf->order = order;
1083 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001088 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 buf->waiting = 0;
1091 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001093 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -07001096 buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001097 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* Set count correctly so we free the proper amount. */
1099 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001100 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001101 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -ENOMEM;
1104 }
1105
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001106 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 offset += alignment;
1111 entry->buf_count++;
1112 byte_count += PAGE_SIZE << page_order;
1113 }
1114
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001115 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Eric Anholt9a298b22009-03-24 12:23:04 -07001117 temp_buflist = krealloc(dma->buflist,
1118 (dma->buf_count + entry->buf_count) *
1119 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001120 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001122 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001123 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return -ENOMEM;
1126 }
1127 dma->buflist = temp_buflist;
1128
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1131 }
1132
1133 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001134 dma->seg_count += entry->seg_count;
1135 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 dma->byte_count += byte_count;
1137
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001138 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1139 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Dave Airlie30e2fb12006-02-02 19:37:46 +11001141 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Dave Airlied59431b2005-07-10 15:00:06 +10001143 request->count = entry->buf_count;
1144 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 dma->flags = _DRM_DMA_USE_SG;
1147
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001148 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return 0;
1150}
1151
Dave Airlie84b1fd12007-07-11 15:53:27 +10001152static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001153{
Dave Airliecdd55a22007-07-11 16:32:08 +10001154 struct drm_device_dma *dma = dev->dma;
1155 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001156 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001157 unsigned long offset;
1158 unsigned long agp_offset;
1159 int count;
1160 int order;
1161 int size;
1162 int alignment;
1163 int page_order;
1164 int total;
1165 int byte_count;
1166 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001167 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001168
1169 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1170 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171
Dave Airlieb84397d62005-07-10 14:46:12 +10001172 if (!dma)
1173 return -EINVAL;
1174
Dave Airlied985c102006-01-02 21:32:48 +11001175 if (!capable(CAP_SYS_ADMIN))
1176 return -EPERM;
1177
Dave Airlied59431b2005-07-10 15:00:06 +10001178 count = request->count;
1179 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001180 size = 1 << order;
1181
Dave Airlied59431b2005-07-10 15:00:06 +10001182 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001183 ? PAGE_ALIGN(size) : size;
1184 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1185 total = PAGE_SIZE << page_order;
1186
1187 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001188 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001189
1190 DRM_DEBUG("count: %d\n", count);
1191 DRM_DEBUG("order: %d\n", order);
1192 DRM_DEBUG("size: %d\n", size);
1193 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1194 DRM_DEBUG("alignment: %d\n", alignment);
1195 DRM_DEBUG("page_order: %d\n", page_order);
1196 DRM_DEBUG("total: %d\n", total);
1197
1198 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1199 return -EINVAL;
1200 if (dev->queue_count)
1201 return -EBUSY; /* Not while in use */
1202
1203 spin_lock(&dev->count_lock);
1204 if (dev->buf_use) {
1205 spin_unlock(&dev->count_lock);
1206 return -EBUSY;
1207 }
1208 atomic_inc(&dev->buf_alloc);
1209 spin_unlock(&dev->count_lock);
1210
Dave Airlie30e2fb12006-02-02 19:37:46 +11001211 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001212 entry = &dma->bufs[order];
1213 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001214 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001215 atomic_dec(&dev->buf_alloc);
1216 return -ENOMEM; /* May only call once for each order */
1217 }
1218
1219 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001220 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001221 atomic_dec(&dev->buf_alloc);
1222 return -EINVAL;
1223 }
1224
Eric Anholt9a298b22009-03-24 12:23:04 -07001225 entry->buflist = kmalloc(count * sizeof(*entry->buflist),
1226 GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001227 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001228 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001229 atomic_dec(&dev->buf_alloc);
1230 return -ENOMEM;
1231 }
1232 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1233
1234 entry->buf_size = size;
1235 entry->page_order = page_order;
1236
1237 offset = 0;
1238
1239 while (entry->buf_count < count) {
1240 buf = &entry->buflist[entry->buf_count];
1241 buf->idx = dma->buf_count + entry->buf_count;
1242 buf->total = alignment;
1243 buf->order = order;
1244 buf->used = 0;
1245
1246 buf->offset = (dma->byte_count + offset);
1247 buf->bus_address = agp_offset + offset;
1248 buf->address = (void *)(agp_offset + offset);
1249 buf->next = NULL;
1250 buf->waiting = 0;
1251 buf->pending = 0;
1252 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001253 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001254
1255 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -07001256 buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001257 if (!buf->dev_private) {
1258 /* Set count correctly so we free the proper amount. */
1259 entry->buf_count = count;
1260 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001261 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001262 atomic_dec(&dev->buf_alloc);
1263 return -ENOMEM;
1264 }
1265 memset(buf->dev_private, 0, buf->dev_priv_size);
1266
1267 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1268
1269 offset += alignment;
1270 entry->buf_count++;
1271 byte_count += PAGE_SIZE << page_order;
1272 }
1273
1274 DRM_DEBUG("byte_count: %d\n", byte_count);
1275
Eric Anholt9a298b22009-03-24 12:23:04 -07001276 temp_buflist = krealloc(dma->buflist,
1277 (dma->buf_count + entry->buf_count) *
1278 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001279 if (!temp_buflist) {
1280 /* Free the entry because it isn't valid */
1281 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001282 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001283 atomic_dec(&dev->buf_alloc);
1284 return -ENOMEM;
1285 }
1286 dma->buflist = temp_buflist;
1287
1288 for (i = 0; i < entry->buf_count; i++) {
1289 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1290 }
1291
1292 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001293 dma->seg_count += entry->seg_count;
1294 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001295 dma->byte_count += byte_count;
1296
1297 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1298 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1299
Dave Airlie30e2fb12006-02-02 19:37:46 +11001300 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001301
Dave Airlied59431b2005-07-10 15:00:06 +10001302 request->count = entry->buf_count;
1303 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001304
1305 dma->flags = _DRM_DMA_USE_FB;
1306
1307 atomic_dec(&dev->buf_alloc);
1308 return 0;
1309}
Dave Airlied985c102006-01-02 21:32:48 +11001310
Dave Airlieb84397d62005-07-10 14:46:12 +10001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/**
1313 * Add buffers for DMA transfers (ioctl).
1314 *
1315 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001316 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001318 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 * \return zero on success or a negative number on failure.
1320 *
1321 * According with the memory type specified in drm_buf_desc::flags and the
1322 * build options, it dispatches the call either to addbufs_agp(),
1323 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1324 * PCI memory respectively.
1325 */
Eric Anholtc153f452007-09-03 12:06:45 +10001326int drm_addbufs(struct drm_device *dev, void *data,
1327 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Eric Anholtc153f452007-09-03 12:06:45 +10001329 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001330 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1333 return -EINVAL;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001336 if (request->flags & _DRM_AGP_BUFFER)
1337 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 else
1339#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001340 if (request->flags & _DRM_SG_BUFFER)
1341 ret = drm_addbufs_sg(dev, request);
1342 else if (request->flags & _DRM_FB_BUFFER)
1343 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 else
Eric Anholtc153f452007-09-03 12:06:45 +10001345 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001346
Dave Airlied59431b2005-07-10 15:00:06 +10001347 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350/**
1351 * Get information about the buffer mappings.
1352 *
1353 * This was originally mean for debugging purposes, or by a sophisticated
1354 * client library to determine how best to use the available buffers (e.g.,
1355 * large buffers can be used for image transfer).
1356 *
1357 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001358 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * \param cmd command.
1360 * \param arg pointer to a drm_buf_info structure.
1361 * \return zero on success or a negative number on failure.
1362 *
1363 * Increments drm_device::buf_use while holding the drm_device::count_lock
1364 * lock, preventing of allocating more buffers after this call. Information
1365 * about each requested buffer is then copied into user space.
1366 */
Eric Anholtc153f452007-09-03 12:06:45 +10001367int drm_infobufs(struct drm_device *dev, void *data,
1368 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Dave Airliecdd55a22007-07-11 16:32:08 +10001370 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001371 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 int i;
1373 int count;
1374
1375 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1376 return -EINVAL;
1377
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001378 if (!dma)
1379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001381 spin_lock(&dev->count_lock);
1382 if (atomic_read(&dev->buf_alloc)) {
1383 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return -EBUSY;
1385 }
1386 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001387 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001389 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1390 if (dma->bufs[i].buf_count)
1391 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001394 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Eric Anholtc153f452007-09-03 12:06:45 +10001396 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001397 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1398 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001399 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001400 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001401 struct drm_buf_entry *from = &dma->bufs[i];
1402 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001403 if (copy_to_user(&to->count,
1404 &from->buf_count,
1405 sizeof(from->buf_count)) ||
1406 copy_to_user(&to->size,
1407 &from->buf_size,
1408 sizeof(from->buf_size)) ||
1409 copy_to_user(&to->low_mark,
1410 &list->low_mark,
1411 sizeof(list->low_mark)) ||
1412 copy_to_user(&to->high_mark,
1413 &list->high_mark,
1414 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return -EFAULT;
1416
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001417 DRM_DEBUG("%d %d %d %d %d\n",
1418 i,
1419 dma->bufs[i].buf_count,
1420 dma->bufs[i].buf_size,
1421 dma->bufs[i].freelist.low_mark,
1422 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 ++count;
1424 }
1425 }
1426 }
Eric Anholtc153f452007-09-03 12:06:45 +10001427 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 return 0;
1430}
1431
1432/**
1433 * Specifies a low and high water mark for buffer allocation
1434 *
1435 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001436 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 * \param cmd command.
1438 * \param arg a pointer to a drm_buf_desc structure.
1439 * \return zero on success or a negative number on failure.
1440 *
1441 * Verifies that the size order is bounded between the admissible orders and
1442 * updates the respective drm_device_dma::bufs entry low and high water mark.
1443 *
1444 * \note This ioctl is deprecated and mostly never used.
1445 */
Eric Anholtc153f452007-09-03 12:06:45 +10001446int drm_markbufs(struct drm_device *dev, void *data,
1447 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Dave Airliecdd55a22007-07-11 16:32:08 +10001449 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001450 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001452 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1455 return -EINVAL;
1456
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001457 if (!dma)
1458 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001460 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001461 request->size, request->low_mark, request->high_mark);
1462 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001463 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1464 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 entry = &dma->bufs[order];
1466
Eric Anholtc153f452007-09-03 12:06:45 +10001467 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001469 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return -EINVAL;
1471
Eric Anholtc153f452007-09-03 12:06:45 +10001472 entry->freelist.low_mark = request->low_mark;
1473 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 return 0;
1476}
1477
1478/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001479 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 *
1481 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001482 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 * \param cmd command.
1484 * \param arg pointer to a drm_buf_free structure.
1485 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001486 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * Calls free_buffer() for each used buffer.
1488 * This function is primarily used for debugging.
1489 */
Eric Anholtc153f452007-09-03 12:06:45 +10001490int drm_freebufs(struct drm_device *dev, void *data,
1491 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Dave Airliecdd55a22007-07-11 16:32:08 +10001493 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001494 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int i;
1496 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001497 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1500 return -EINVAL;
1501
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001502 if (!dma)
1503 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Eric Anholtc153f452007-09-03 12:06:45 +10001505 DRM_DEBUG("%d\n", request->count);
1506 for (i = 0; i < request->count; i++) {
1507 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001509 if (idx < 0 || idx >= dma->buf_count) {
1510 DRM_ERROR("Index %d (of %d max)\n",
1511 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return -EINVAL;
1513 }
1514 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001515 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001516 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001517 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return -EINVAL;
1519 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001520 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
1523 return 0;
1524}
1525
1526/**
1527 * Maps all of the DMA buffers into client-virtual space (ioctl).
1528 *
1529 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001530 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 * \param cmd command.
1532 * \param arg pointer to a drm_buf_map structure.
1533 * \return zero on success or a negative number on failure.
1534 *
George Sapountzis3417f332006-10-24 12:03:04 -07001535 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1536 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1537 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1538 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 */
Eric Anholtc153f452007-09-03 12:06:45 +10001540int drm_mapbufs(struct drm_device *dev, void *data,
1541 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Dave Airliecdd55a22007-07-11 16:32:08 +10001543 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 int retcode = 0;
1545 const int zero = 0;
1546 unsigned long virtual;
1547 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001548 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 int i;
1550
1551 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1552 return -EINVAL;
1553
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001554 if (!dma)
1555 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001557 spin_lock(&dev->count_lock);
1558 if (atomic_read(&dev->buf_alloc)) {
1559 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return -EBUSY;
1561 }
1562 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001563 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Eric Anholtc153f452007-09-03 12:06:45 +10001565 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001566 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001567 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001568 && (dma->flags & _DRM_DMA_USE_SG))
1569 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1570 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001571 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001572 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001574 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 retcode = -EINVAL;
1576 goto done;
1577 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001578 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001579 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001580 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001581 MAP_SHARED,
1582 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001583 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001585 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001586 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001587 PROT_READ | PROT_WRITE,
1588 MAP_SHARED, 0);
1589 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001591 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* Real error */
1593 retcode = (signed long)virtual;
1594 goto done;
1595 }
Eric Anholtc153f452007-09-03 12:06:45 +10001596 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001598 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001599 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001600 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001601 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 retcode = -EFAULT;
1603 goto done;
1604 }
Eric Anholtc153f452007-09-03 12:06:45 +10001605 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001606 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001607 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 retcode = -EFAULT;
1609 goto done;
1610 }
Eric Anholtc153f452007-09-03 12:06:45 +10001611 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001612 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 retcode = -EFAULT;
1614 goto done;
1615 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001616 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001617 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001618 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 retcode = -EFAULT;
1620 goto done;
1621 }
1622 }
1623 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001624 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001625 request->count = dma->buf_count;
1626 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 return retcode;
1629}
1630
Dave Airlie836cf042005-07-10 19:27:04 +10001631/**
1632 * Compute size order. Returns the exponent of the smaller power of two which
1633 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001634 *
Dave Airlie836cf042005-07-10 19:27:04 +10001635 * \param size size.
1636 * \return order.
1637 *
1638 * \todo Can be made faster.
1639 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001640int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001641{
1642 int order;
1643 unsigned long tmp;
1644
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001645 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001646
1647 if (size & (size - 1))
1648 ++order;
1649
1650 return order;
1651}
1652EXPORT_SYMBOL(drm_order);