blob: e1bd713d91a27d20b25d9e856124af94ca645151 [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
Dave Airlie55910512007-07-11 16:53:40 +100042static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +110043 struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +100044{
Dave Airlie55910512007-07-11 16:53:40 +100045 struct drm_map_list *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +100046 list_for_each_entry(entry, &dev->maplist, head) {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110047 /*
48 * Because the kernel-userspace ABI is fixed at a 32-bit offset
49 * while PCI resources may live above that, we ignore the map
50 * offset for maps of type _DRM_FRAMEBUFFER or _DRM_REGISTERS.
51 * It is assumed that each driver will have only one resource of
52 * each type.
53 */
54 if (!entry->map ||
55 map->type != entry->map->type ||
56 entry->master != dev->primary->master)
57 continue;
58 switch (map->type) {
59 case _DRM_SHM:
60 if (map->flags != _DRM_CONTAINS_LOCK)
61 break;
62 case _DRM_REGISTERS:
63 case _DRM_FRAME_BUFFER:
Dave Airlie89625eb2005-09-05 21:23:23 +100064 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110065 default: /* Make gcc happy */
66 ;
Dave Airlie836cf042005-07-10 19:27:04 +100067 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110068 if (entry->map->offset == map->offset)
69 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100070 }
71
72 return NULL;
73}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Dave Airliee0be4282007-07-12 10:26:44 +100075static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080076 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100077{
David Millerf1a2a9b2009-02-18 15:41:02 -080078 int use_hashed_handle, shift;
79 unsigned long add;
80
Dave Airliec2604ce2006-08-12 16:03:26 +100081#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100082 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
83#elif (BITS_PER_LONG == 32)
84 use_hashed_handle = hashed_handle;
85#else
86#error Unsupported long size. Neither 64 nor 32 bits.
87#endif
Dave Airlied1f2b552005-08-05 22:11:22 +100088
Thomas Hellstrome08870c2006-09-22 04:18:37 +100089 if (!use_hashed_handle) {
90 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +110091 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +100092 ret = drm_ht_insert_item(&dev->map_hash, hash);
93 if (ret != -EINVAL)
94 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +100095 }
David Millerf1a2a9b2009-02-18 15:41:02 -080096
97 shift = 0;
98 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
99 if (shm && (SHMLBA > PAGE_SIZE)) {
100 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
101
102 /* For shared memory, we have to preserve the SHMLBA
103 * bits of the eventual vma->vm_pgoff value during
104 * mmap(). Otherwise we run into cache aliasing problems
105 * on some platforms. On these platforms, the pgoff of
106 * a mmap() request is used to pick a suitable virtual
107 * address for the mmap() region such that it will not
108 * cause cache aliasing problems.
109 *
110 * Therefore, make sure the SHMLBA relevant bits of the
111 * hash value we use are equal to those in the original
112 * kernel virtual address.
113 */
114 shift = bits;
115 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
116 }
117
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000118 return drm_ht_just_insert_please(&dev->map_hash, hash,
119 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800120 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000121}
Dave Airlie9a186642005-06-23 21:29:18 +1000122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/**
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100124 * Core function to create a range of memory available for mapping by a
125 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 *
127 * Adjusts the memory offset to its absolute value according to the mapping
128 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
129 * applicable and if supported by the kernel.
130 */
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100131static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000132 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000133 enum drm_map_flags flags,
134 struct drm_map_list ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100136 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000137 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000138 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000139 unsigned long user_token;
140 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Eric Anholt9a298b22009-03-24 12:23:04 -0700142 map = kmalloc(sizeof(*map), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000143 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -ENOMEM;
145
Dave Airlie7ab98402005-07-10 16:56:52 +1000146 map->offset = offset;
147 map->size = size;
148 map->flags = flags;
149 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* Only allow shared memory to be removable since we only keep enough
152 * book keeping information about shared memory to allow for removal
153 * when processes fork.
154 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000155 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700156 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -EINVAL;
158 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100159 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
160 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000161
162 /* page-align _DRM_SHM maps. They are allocated here so there is no security
163 * hole created by that and it works around various broken drivers that use
164 * a non-aligned quantity to map the SAREA. --BenH
165 */
166 if (map->type == _DRM_SHM)
167 map->size = PAGE_ALIGN(map->size);
168
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100169 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700170 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -EINVAL;
172 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000173 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 map->handle = NULL;
175
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 case _DRM_REGISTERS:
178 case _DRM_FRAME_BUFFER:
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600179#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100180 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000181 map->offset < virt_to_phys(high_memory)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700182 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -EINVAL;
184 }
185#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000186 /* Some drivers preinitialize some maps, without the X Server
187 * needing to be aware of it. Therefore, we just return success
188 * when the server tries to create a duplicate map.
189 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000190 list = drm_find_matching_map(dev, map);
191 if (list != NULL) {
192 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000193 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 "mismatched sizes, (%ld vs %ld)\n",
195 map->type, map->size,
196 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000197 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000198 }
199
Eric Anholt9a298b22009-03-24 12:23:04 -0700200 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000201 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000202 return 0;
203 }
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 if (map->type == _DRM_FRAME_BUFFER ||
207 (map->flags & _DRM_WRITE_COMBINING)) {
208 map->mtrr = mtrr_add(map->offset, map->size,
209 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211 }
Scott Thompson0769d392007-08-25 18:17:49 +1000212 if (map->type == _DRM_REGISTERS) {
Christoph Hellwig004a7722007-01-08 21:56:59 +1100213 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000214 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700215 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000216 return -ENOMEM;
217 }
218 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100222 list = drm_find_matching_map(dev, map);
223 if (list != NULL) {
224 if(list->map->size != map->size) {
225 DRM_DEBUG("Matching maps of type %d with "
226 "mismatched sizes, (%ld vs %ld)\n",
227 map->type, map->size, list->map->size);
228 list->map->size = map->size;
229 }
230
Eric Anholt9a298b22009-03-24 12:23:04 -0700231 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100232 *maplist = list;
233 return 0;
234 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100235 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000236 DRM_DEBUG("%lu %d %p\n",
237 map->size, drm_order(map->size), map->handle);
238 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700239 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -ENOMEM;
241 }
242 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000245 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700247 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -EBUSY;
249 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000250 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100253 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000254 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100255 int valid = 0;
256
257 if (!drm_core_has_AGP(dev)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700258 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100259 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100261#ifdef __alpha__
262 map->offset += dev->hose->mem_space->start;
263#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000264 /* In some cases (i810 driver), user space may have already
265 * added the AGP base itself, because dev->agp->base previously
266 * only got set during AGP enable. So, only add the base
267 * address if the map's offset isn't already within the
268 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100269 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000270 if (map->offset < dev->agp->base ||
271 map->offset > dev->agp->base +
272 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
273 map->offset += dev->agp->base;
274 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100275 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
276
277 /* This assumes the DRM is in total control of AGP space.
278 * It's not always the case as AGP can be in the control
279 * of user space (i.e. i810 driver). So this loop will get
280 * skipped and we double check that dev->agp->memory is
281 * actually set as well as being invalid before EPERM'ing
282 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000283 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100284 if ((map->offset >= entry->bound) &&
285 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
286 valid = 1;
287 break;
288 }
289 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000290 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700291 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100292 return -EPERM;
293 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100294 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
295 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100298 }
Pekka Paalanen812c3692009-09-17 22:59:54 +0300299 case _DRM_GEM:
300 DRM_ERROR("tried to addmap GEM object\n");
301 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 case _DRM_SCATTER_GATHER:
303 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700304 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return -EINVAL;
306 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000307 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000309 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000310 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000311 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000312 * casting it down to 32 bits is no problem, but we
313 * need to point to a 64bit variable first. */
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800314 dmah = drm_pci_alloc(dev, map->size, map->size);
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000315 if (!dmah) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700316 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000317 return -ENOMEM;
318 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000319 map->handle = dmah->vaddr;
320 map->offset = (unsigned long)dmah->busaddr;
321 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700324 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -EINVAL;
326 }
327
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400328 list = kzalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700330 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100331 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700332 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return -EINVAL;
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 list->map = map;
336
Dave Airlie30e2fb12006-02-02 19:37:46 +1100337 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000338 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000339
Dave Airlied1f2b552005-08-05 22:11:22 +1000340 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100341 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000342 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
343 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800344 ret = drm_map_handle(dev, &list->hash, user_token, 0,
345 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000346 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700347 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100348 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700349 kfree(map);
350 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000351 mutex_unlock(&dev->struct_mutex);
352 return ret;
353 }
354
Thomas Hellstrom15450852007-02-08 16:14:05 +1100355 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100356 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Ben Skeggs2ff2e8a2009-05-26 10:35:52 +1000358 if (!(map->flags & _DRM_DRIVER))
359 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000360 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000361 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100362 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000363
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100364int drm_addmap(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000365 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100366 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000367{
Dave Airlie55910512007-07-11 16:53:40 +1000368 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000369 int rc;
370
371 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
372 if (!rc)
373 *map_ptr = list->map;
374 return rc;
375}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376
Dave Airlie7ab98402005-07-10 16:56:52 +1000377EXPORT_SYMBOL(drm_addmap);
378
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100379/**
380 * Ioctl to specify a range of memory that is available for mapping by a
381 * non-root process.
382 *
383 * \param inode device inode.
384 * \param file_priv DRM file private.
385 * \param cmd command.
386 * \param arg pointer to a drm_map structure.
387 * \return zero on success or a negative value on error.
388 *
389 */
Eric Anholtc153f452007-09-03 12:06:45 +1000390int drm_addmap_ioctl(struct drm_device *dev, void *data,
391 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000392{
Eric Anholtc153f452007-09-03 12:06:45 +1000393 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000394 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000395 int err;
396
Dave Airlie7c1c2872008-11-28 14:22:24 +1000397 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100398 return -EPERM;
399
Eric Anholtc153f452007-09-03 12:06:45 +1000400 err = drm_addmap_core(dev, map->offset, map->size, map->type,
401 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000402
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000403 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000404 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000405
Dave Airlie67e1a012005-10-24 18:41:39 +1000406 /* 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 +1000407 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000409}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/**
412 * Remove a map private from list and deallocate resources if the mapping
413 * isn't in use.
414 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * Searches the map on drm_device::maplist, removes it from the list, see if
416 * its being used, and free any associate resource (such as MTRR's) if it's not
417 * being on use.
418 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000419 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100421int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Dave Airlie55910512007-07-11 16:53:40 +1000423 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000424 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000425 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000426 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Dave Airlie836cf042005-07-10 19:27:04 +1000428 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000429 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000430 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000431 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000432 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100433 drm_ht_remove_key(&dev->map_hash,
434 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700435 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000436 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000437 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Dave Airlie836cf042005-07-10 19:27:04 +1000440
Dave Airliebd1b3312007-05-26 05:01:51 +1000441 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000442 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000443
444 switch (map->type) {
445 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100446 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000447 /* FALLTHROUGH */
448 case _DRM_FRAME_BUFFER:
449 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
450 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451 retcode = mtrr_del(map->mtrr, map->offset, map->size);
452 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000453 }
454 break;
455 case _DRM_SHM:
456 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000457 if (master) {
458 if (dev->sigdata.lock == master->lock.hw_lock)
459 dev->sigdata.lock = NULL;
460 master->lock.hw_lock = NULL; /* SHM removed */
461 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100462 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000463 }
Dave Airlie836cf042005-07-10 19:27:04 +1000464 break;
465 case _DRM_AGP:
466 case _DRM_SCATTER_GATHER:
467 break;
468 case _DRM_CONSISTENT:
469 dmah.vaddr = map->handle;
470 dmah.busaddr = map->offset;
471 dmah.size = map->size;
472 __drm_pci_free(dev, &dmah);
473 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800474 case _DRM_GEM:
475 DRM_ERROR("tried to rmmap GEM object\n");
476 break;
Dave Airlie836cf042005-07-10 19:27:04 +1000477 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700478 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 0;
481}
Dave Airlie4e74f362008-12-19 10:23:14 +1100482EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000483
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100484int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000485{
486 int ret;
487
Dave Airlie30e2fb12006-02-02 19:37:46 +1100488 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000489 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100490 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000491
492 return ret;
493}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000494EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000495
Dave Airlie836cf042005-07-10 19:27:04 +1000496/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
497 * the last close of the device, and this is necessary for cleanup when things
498 * exit uncleanly. Therefore, having userland manually remove mappings seems
499 * like a pointless exercise since they're going away anyway.
500 *
501 * One use case might be after addmap is allowed for normal users for SHM and
502 * gets used by drivers that the server doesn't need to care about. This seems
503 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100504 *
505 * \param inode device inode.
506 * \param file_priv DRM file private.
507 * \param cmd command.
508 * \param arg pointer to a struct drm_map structure.
509 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000510 */
Eric Anholtc153f452007-09-03 12:06:45 +1000511int drm_rmmap_ioctl(struct drm_device *dev, void *data,
512 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000513{
Eric Anholtc153f452007-09-03 12:06:45 +1000514 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100515 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000516 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000517 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000518
Dave Airlie30e2fb12006-02-02 19:37:46 +1100519 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000520 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000521 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000522 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000523 r_list->map->flags & _DRM_REMOVABLE) {
524 map = r_list->map;
525 break;
526 }
527 }
528
529 /* List has wrapped around to the head pointer, or its empty we didn't
530 * find anything.
531 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000532 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100533 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000534 return -EINVAL;
535 }
536
Dave Airlie836cf042005-07-10 19:27:04 +1000537 /* Register and framebuffer maps are permanent */
538 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100539 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000540 return 0;
541 }
542
543 ret = drm_rmmap_locked(dev, map);
544
Dave Airlie30e2fb12006-02-02 19:37:46 +1100545 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000546
547 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000548}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550/**
551 * Cleanup after an error on one of the addbufs() functions.
552 *
Dave Airlie836cf042005-07-10 19:27:04 +1000553 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * \param entry buffer entry where the error occurred.
555 *
556 * Frees any pages and buffers associated with the given entry.
557 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000558static void drm_cleanup_buf_error(struct drm_device * dev,
559 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 int i;
562
563 if (entry->seg_count) {
564 for (i = 0; i < entry->seg_count; i++) {
565 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100566 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700569 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 entry->seg_count = 0;
572 }
573
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000574 if (entry->buf_count) {
575 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700576 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700578 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 entry->buf_count = 0;
581 }
582}
583
584#if __OS_HAS_AGP
585/**
Dave Airlied59431b2005-07-10 15:00:06 +1000586 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000588 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000589 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * After some sanity checks creates a drm_buf structure for each buffer and
593 * reallocates the buffer list of the same size order to accommodate the new
594 * buffers.
595 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000596int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Dave Airliecdd55a22007-07-11 16:32:08 +1000598 struct drm_device_dma *dma = dev->dma;
599 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000600 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000601 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 unsigned long offset;
603 unsigned long agp_offset;
604 int count;
605 int order;
606 int size;
607 int alignment;
608 int page_order;
609 int total;
610 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100611 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000612 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000614 if (!dma)
615 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Dave Airlied59431b2005-07-10 15:00:06 +1000617 count = request->count;
618 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 size = 1 << order;
620
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 alignment = (request->flags & _DRM_PAGE_ALIGN)
622 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
624 total = PAGE_SIZE << page_order;
625
626 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000627 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 DRM_DEBUG("count: %d\n", count);
630 DRM_DEBUG("order: %d\n", order);
631 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100632 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000633 DRM_DEBUG("alignment: %d\n", alignment);
634 DRM_DEBUG("page_order: %d\n", page_order);
635 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
638 return -EINVAL;
639 if (dev->queue_count)
640 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Dave Airlie54ba2f72007-02-10 12:07:47 +1100642 /* Make sure buffers are located in AGP memory that we own */
643 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000644 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100645 if ((agp_offset >= agp_entry->bound) &&
646 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
647 valid = 1;
648 break;
649 }
650 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000651 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100652 DRM_DEBUG("zone invalid\n");
653 return -EINVAL;
654 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 spin_lock(&dev->count_lock);
656 if (dev->buf_use) {
657 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -EBUSY;
659 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000660 atomic_inc(&dev->buf_alloc);
661 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Dave Airlie30e2fb12006-02-02 19:37:46 +1100663 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100666 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000667 atomic_dec(&dev->buf_alloc);
668 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
671 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100672 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000673 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EINVAL;
675 }
676
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400677 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000678 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100679 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return -ENOMEM;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 entry->buf_size = size;
685 entry->page_order = page_order;
686
687 offset = 0;
688
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 while (entry->buf_count < count) {
690 buf = &entry->buflist[entry->buf_count];
691 buf->idx = dma->buf_count + entry->buf_count;
692 buf->total = alignment;
693 buf->order = order;
694 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 buf->bus_address = agp_offset + offset;
698 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 buf->waiting = 0;
701 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000702 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000703 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400706 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /* Set count correctly so we free the proper amount. */
709 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000710 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100711 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return -ENOMEM;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 offset += alignment;
719 entry->buf_count++;
720 byte_count += PAGE_SIZE << page_order;
721 }
722
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Eric Anholt9a298b22009-03-24 12:23:04 -0700725 temp_buflist = krealloc(dma->buflist,
726 (dma->buf_count + entry->buf_count) *
727 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000730 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100731 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return -ENOMEM;
734 }
735 dma->buflist = temp_buflist;
736
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
739 }
740
741 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100742 dma->seg_count += entry->seg_count;
743 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 dma->byte_count += byte_count;
745
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
747 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Dave Airlie30e2fb12006-02-02 19:37:46 +1100749 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Dave Airlied59431b2005-07-10 15:00:06 +1000751 request->count = entry->buf_count;
752 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 dma->flags = _DRM_DMA_USE_AGP;
755
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759EXPORT_SYMBOL(drm_addbufs_agp);
760#endif /* __OS_HAS_AGP */
761
Dave Airlie84b1fd12007-07-11 15:53:27 +1000762int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Dave Airliecdd55a22007-07-11 16:32:08 +1000764 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int count;
766 int order;
767 int size;
768 int total;
769 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000770 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100771 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000772 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int alignment;
774 unsigned long offset;
775 int i;
776 int byte_count;
777 int page_count;
778 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000779 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
782 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100783
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784 if (!dma)
785 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Dave Airlied985c102006-01-02 21:32:48 +1100787 if (!capable(CAP_SYS_ADMIN))
788 return -EPERM;
789
Dave Airlied59431b2005-07-10 15:00:06 +1000790 count = request->count;
791 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 size = 1 << order;
793
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
795 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
798 return -EINVAL;
799 if (dev->queue_count)
800 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Dave Airlied59431b2005-07-10 15:00:06 +1000802 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
805 total = PAGE_SIZE << page_order;
806
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 spin_lock(&dev->count_lock);
808 if (dev->buf_use) {
809 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -EBUSY;
811 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 atomic_inc(&dev->buf_alloc);
813 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Dave Airlie30e2fb12006-02-02 19:37:46 +1100815 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100818 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -ENOMEM; /* May only call once for each order */
821 }
822
823 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100824 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return -EINVAL;
827 }
828
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400829 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100831 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return -ENOMEM;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400836 entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700838 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100839 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return -ENOMEM;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* Keep the original pagelist until we know all the allocations
845 * have succeeded
846 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700847 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
848 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700850 kfree(entry->buflist);
851 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100852 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return -ENOMEM;
855 }
856 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
858 DRM_DEBUG("pagelist: %d entries\n",
859 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 entry->page_order = page_order;
863 byte_count = 0;
864 page_count = 0;
865
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000867
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800868 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000869
Dave Airlieddf19b92006-03-19 18:56:12 +1100870 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /* Set count correctly so we free the proper amount. */
872 entry->buf_count = count;
873 entry->seg_count = count;
874 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700875 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100876 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return -ENOMEM;
879 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100880 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000881 for (i = 0; i < (1 << page_order); i++) {
882 DRM_DEBUG("page %d @ 0x%08lx\n",
883 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100884 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100886 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 for (offset = 0;
889 offset + size <= total && entry->buf_count < count;
890 offset += alignment, ++entry->buf_count) {
891 buf = &entry->buflist[entry->buf_count];
892 buf->idx = dma->buf_count + entry->buf_count;
893 buf->total = alignment;
894 buf->order = order;
895 buf->used = 0;
896 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100897 buf->address = (void *)(dmah->vaddr + offset);
898 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 buf->waiting = 0;
901 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000902 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000903 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400906 buf->dev_private = kzalloc(buf->dev_priv_size,
907 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* Set count correctly so we free the proper amount. */
910 entry->buf_count = count;
911 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700913 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100914 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -ENOMEM;
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 DRM_DEBUG("buffer %d @ %p\n",
920 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922 byte_count += PAGE_SIZE << page_order;
923 }
924
Eric Anholt9a298b22009-03-24 12:23:04 -0700925 temp_buflist = krealloc(dma->buflist,
926 (dma->buf_count + entry->buf_count) *
927 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (!temp_buflist) {
929 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700931 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100932 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000933 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return -ENOMEM;
935 }
936 dma->buflist = temp_buflist;
937
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
940 }
941
Thomas Weber88393162010-03-16 11:47:56 +0100942 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * with the new one.
944 */
945 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700946 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 dma->pagelist = temp_pagelist;
949
950 dma->buf_count += entry->buf_count;
951 dma->seg_count += entry->seg_count;
952 dma->page_count += entry->seg_count << page_order;
953 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
954
Dave Airlie30e2fb12006-02-02 19:37:46 +1100955 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Dave Airlied59431b2005-07-10 15:00:06 +1000957 request->count = entry->buf_count;
958 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
George Sapountzis3417f332006-10-24 12:03:04 -0700960 if (request->flags & _DRM_PCI_BUFFER_RO)
961 dma->flags = _DRM_DMA_USE_PCI_RO;
962
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return 0;
965
966}
Dave Airlied84f76d2005-07-10 17:04:22 +1000967EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Dave Airlie84b1fd12007-07-11 15:53:27 +1000969static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Dave Airliecdd55a22007-07-11 16:32:08 +1000971 struct drm_device_dma *dma = dev->dma;
972 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000973 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned long offset;
975 unsigned long agp_offset;
976 int count;
977 int order;
978 int size;
979 int alignment;
980 int page_order;
981 int total;
982 int byte_count;
983 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000984 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 if (!drm_core_check_feature(dev, DRIVER_SG))
987 return -EINVAL;
988
989 if (!dma)
990 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Dave Airlied985c102006-01-02 21:32:48 +1100992 if (!capable(CAP_SYS_ADMIN))
993 return -EPERM;
994
Dave Airlied59431b2005-07-10 15:00:06 +1000995 count = request->count;
996 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 size = 1 << order;
998
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 alignment = (request->flags & _DRM_PAGE_ALIGN)
1000 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1002 total = PAGE_SIZE << page_order;
1003
1004 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001005 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 DRM_DEBUG("count: %d\n", count);
1008 DRM_DEBUG("order: %d\n", order);
1009 DRM_DEBUG("size: %d\n", size);
1010 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1011 DRM_DEBUG("alignment: %d\n", alignment);
1012 DRM_DEBUG("page_order: %d\n", page_order);
1013 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001015 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1016 return -EINVAL;
1017 if (dev->queue_count)
1018 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001020 spin_lock(&dev->count_lock);
1021 if (dev->buf_use) {
1022 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return -EBUSY;
1024 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 atomic_inc(&dev->buf_alloc);
1026 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Dave Airlie30e2fb12006-02-02 19:37:46 +11001028 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001031 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001032 atomic_dec(&dev->buf_alloc);
1033 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
1036 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001037 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -EINVAL;
1040 }
1041
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001042 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001043 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001044 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001045 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001046 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return -ENOMEM;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 entry->buf_size = size;
1051 entry->page_order = page_order;
1052
1053 offset = 0;
1054
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 while (entry->buf_count < count) {
1056 buf = &entry->buflist[entry->buf_count];
1057 buf->idx = dma->buf_count + entry->buf_count;
1058 buf->total = alignment;
1059 buf->order = order;
1060 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001064 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001065 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 buf->waiting = 0;
1068 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001070 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001073 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001074 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /* Set count correctly so we free the proper amount. */
1076 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001078 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001079 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return -ENOMEM;
1081 }
1082
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 offset += alignment;
1086 entry->buf_count++;
1087 byte_count += PAGE_SIZE << page_order;
1088 }
1089
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Eric Anholt9a298b22009-03-24 12:23:04 -07001092 temp_buflist = krealloc(dma->buflist,
1093 (dma->buf_count + entry->buf_count) *
1094 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001097 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001098 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return -ENOMEM;
1101 }
1102 dma->buflist = temp_buflist;
1103
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001104 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1106 }
1107
1108 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001109 dma->seg_count += entry->seg_count;
1110 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 dma->byte_count += byte_count;
1112
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001113 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1114 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Dave Airlie30e2fb12006-02-02 19:37:46 +11001116 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Dave Airlied59431b2005-07-10 15:00:06 +10001118 request->count = entry->buf_count;
1119 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 dma->flags = _DRM_DMA_USE_SG;
1122
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return 0;
1125}
1126
Dave Airlie84b1fd12007-07-11 15:53:27 +10001127static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001128{
Dave Airliecdd55a22007-07-11 16:32:08 +10001129 struct drm_device_dma *dma = dev->dma;
1130 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001131 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001132 unsigned long offset;
1133 unsigned long agp_offset;
1134 int count;
1135 int order;
1136 int size;
1137 int alignment;
1138 int page_order;
1139 int total;
1140 int byte_count;
1141 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001142 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001143
1144 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1145 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001146
Dave Airlieb84397d62005-07-10 14:46:12 +10001147 if (!dma)
1148 return -EINVAL;
1149
Dave Airlied985c102006-01-02 21:32:48 +11001150 if (!capable(CAP_SYS_ADMIN))
1151 return -EPERM;
1152
Dave Airlied59431b2005-07-10 15:00:06 +10001153 count = request->count;
1154 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001155 size = 1 << order;
1156
Dave Airlied59431b2005-07-10 15:00:06 +10001157 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001158 ? PAGE_ALIGN(size) : size;
1159 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1160 total = PAGE_SIZE << page_order;
1161
1162 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001163 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001164
1165 DRM_DEBUG("count: %d\n", count);
1166 DRM_DEBUG("order: %d\n", order);
1167 DRM_DEBUG("size: %d\n", size);
1168 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1169 DRM_DEBUG("alignment: %d\n", alignment);
1170 DRM_DEBUG("page_order: %d\n", page_order);
1171 DRM_DEBUG("total: %d\n", total);
1172
1173 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1174 return -EINVAL;
1175 if (dev->queue_count)
1176 return -EBUSY; /* Not while in use */
1177
1178 spin_lock(&dev->count_lock);
1179 if (dev->buf_use) {
1180 spin_unlock(&dev->count_lock);
1181 return -EBUSY;
1182 }
1183 atomic_inc(&dev->buf_alloc);
1184 spin_unlock(&dev->count_lock);
1185
Dave Airlie30e2fb12006-02-02 19:37:46 +11001186 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001187 entry = &dma->bufs[order];
1188 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001189 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001190 atomic_dec(&dev->buf_alloc);
1191 return -ENOMEM; /* May only call once for each order */
1192 }
1193
1194 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001195 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001196 atomic_dec(&dev->buf_alloc);
1197 return -EINVAL;
1198 }
1199
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001200 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001201 GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001202 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001203 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001204 atomic_dec(&dev->buf_alloc);
1205 return -ENOMEM;
1206 }
Dave Airlieb84397d62005-07-10 14:46:12 +10001207
1208 entry->buf_size = size;
1209 entry->page_order = page_order;
1210
1211 offset = 0;
1212
1213 while (entry->buf_count < count) {
1214 buf = &entry->buflist[entry->buf_count];
1215 buf->idx = dma->buf_count + entry->buf_count;
1216 buf->total = alignment;
1217 buf->order = order;
1218 buf->used = 0;
1219
1220 buf->offset = (dma->byte_count + offset);
1221 buf->bus_address = agp_offset + offset;
1222 buf->address = (void *)(agp_offset + offset);
1223 buf->next = NULL;
1224 buf->waiting = 0;
1225 buf->pending = 0;
1226 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001227 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001228
1229 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001230 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001231 if (!buf->dev_private) {
1232 /* Set count correctly so we free the proper amount. */
1233 entry->buf_count = count;
1234 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001235 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001236 atomic_dec(&dev->buf_alloc);
1237 return -ENOMEM;
1238 }
Dave Airlieb84397d62005-07-10 14:46:12 +10001239
1240 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1241
1242 offset += alignment;
1243 entry->buf_count++;
1244 byte_count += PAGE_SIZE << page_order;
1245 }
1246
1247 DRM_DEBUG("byte_count: %d\n", byte_count);
1248
Eric Anholt9a298b22009-03-24 12:23:04 -07001249 temp_buflist = krealloc(dma->buflist,
1250 (dma->buf_count + entry->buf_count) *
1251 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001252 if (!temp_buflist) {
1253 /* Free the entry because it isn't valid */
1254 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001255 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001256 atomic_dec(&dev->buf_alloc);
1257 return -ENOMEM;
1258 }
1259 dma->buflist = temp_buflist;
1260
1261 for (i = 0; i < entry->buf_count; i++) {
1262 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1263 }
1264
1265 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001266 dma->seg_count += entry->seg_count;
1267 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001268 dma->byte_count += byte_count;
1269
1270 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1271 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1272
Dave Airlie30e2fb12006-02-02 19:37:46 +11001273 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001274
Dave Airlied59431b2005-07-10 15:00:06 +10001275 request->count = entry->buf_count;
1276 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001277
1278 dma->flags = _DRM_DMA_USE_FB;
1279
1280 atomic_dec(&dev->buf_alloc);
1281 return 0;
1282}
Dave Airlied985c102006-01-02 21:32:48 +11001283
Dave Airlieb84397d62005-07-10 14:46:12 +10001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285/**
1286 * Add buffers for DMA transfers (ioctl).
1287 *
1288 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001289 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001291 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 * \return zero on success or a negative number on failure.
1293 *
1294 * According with the memory type specified in drm_buf_desc::flags and the
1295 * build options, it dispatches the call either to addbufs_agp(),
1296 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1297 * PCI memory respectively.
1298 */
Eric Anholtc153f452007-09-03 12:06:45 +10001299int drm_addbufs(struct drm_device *dev, void *data,
1300 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Eric Anholtc153f452007-09-03 12:06:45 +10001302 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001303 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1306 return -EINVAL;
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001309 if (request->flags & _DRM_AGP_BUFFER)
1310 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 else
1312#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001313 if (request->flags & _DRM_SG_BUFFER)
1314 ret = drm_addbufs_sg(dev, request);
1315 else if (request->flags & _DRM_FB_BUFFER)
1316 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 else
Eric Anholtc153f452007-09-03 12:06:45 +10001318 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001319
Dave Airlied59431b2005-07-10 15:00:06 +10001320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323/**
1324 * Get information about the buffer mappings.
1325 *
1326 * This was originally mean for debugging purposes, or by a sophisticated
1327 * client library to determine how best to use the available buffers (e.g.,
1328 * large buffers can be used for image transfer).
1329 *
1330 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001331 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 * \param cmd command.
1333 * \param arg pointer to a drm_buf_info structure.
1334 * \return zero on success or a negative number on failure.
1335 *
1336 * Increments drm_device::buf_use while holding the drm_device::count_lock
1337 * lock, preventing of allocating more buffers after this call. Information
1338 * about each requested buffer is then copied into user space.
1339 */
Eric Anholtc153f452007-09-03 12:06:45 +10001340int drm_infobufs(struct drm_device *dev, void *data,
1341 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Dave Airliecdd55a22007-07-11 16:32:08 +10001343 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001344 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int i;
1346 int count;
1347
1348 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1349 return -EINVAL;
1350
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001351 if (!dma)
1352 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001354 spin_lock(&dev->count_lock);
1355 if (atomic_read(&dev->buf_alloc)) {
1356 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return -EBUSY;
1358 }
1359 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001360 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001362 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1363 if (dma->bufs[i].buf_count)
1364 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001367 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Eric Anholtc153f452007-09-03 12:06:45 +10001369 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001370 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1371 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001372 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001373 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001374 struct drm_buf_entry *from = &dma->bufs[i];
1375 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001376 if (copy_to_user(&to->count,
1377 &from->buf_count,
1378 sizeof(from->buf_count)) ||
1379 copy_to_user(&to->size,
1380 &from->buf_size,
1381 sizeof(from->buf_size)) ||
1382 copy_to_user(&to->low_mark,
1383 &list->low_mark,
1384 sizeof(list->low_mark)) ||
1385 copy_to_user(&to->high_mark,
1386 &list->high_mark,
1387 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return -EFAULT;
1389
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001390 DRM_DEBUG("%d %d %d %d %d\n",
1391 i,
1392 dma->bufs[i].buf_count,
1393 dma->bufs[i].buf_size,
1394 dma->bufs[i].freelist.low_mark,
1395 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 ++count;
1397 }
1398 }
1399 }
Eric Anholtc153f452007-09-03 12:06:45 +10001400 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 return 0;
1403}
1404
1405/**
1406 * Specifies a low and high water mark for buffer allocation
1407 *
1408 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001409 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 * \param cmd command.
1411 * \param arg a pointer to a drm_buf_desc structure.
1412 * \return zero on success or a negative number on failure.
1413 *
1414 * Verifies that the size order is bounded between the admissible orders and
1415 * updates the respective drm_device_dma::bufs entry low and high water mark.
1416 *
1417 * \note This ioctl is deprecated and mostly never used.
1418 */
Eric Anholtc153f452007-09-03 12:06:45 +10001419int drm_markbufs(struct drm_device *dev, void *data,
1420 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Dave Airliecdd55a22007-07-11 16:32:08 +10001422 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001423 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001425 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1428 return -EINVAL;
1429
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001430 if (!dma)
1431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001433 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001434 request->size, request->low_mark, request->high_mark);
1435 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001436 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1437 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 entry = &dma->bufs[order];
1439
Eric Anholtc153f452007-09-03 12:06:45 +10001440 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001442 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return -EINVAL;
1444
Eric Anholtc153f452007-09-03 12:06:45 +10001445 entry->freelist.low_mark = request->low_mark;
1446 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 return 0;
1449}
1450
1451/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001452 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 *
1454 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001455 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 * \param cmd command.
1457 * \param arg pointer to a drm_buf_free structure.
1458 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001459 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 * Calls free_buffer() for each used buffer.
1461 * This function is primarily used for debugging.
1462 */
Eric Anholtc153f452007-09-03 12:06:45 +10001463int drm_freebufs(struct drm_device *dev, void *data,
1464 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Dave Airliecdd55a22007-07-11 16:32:08 +10001466 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001467 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int i;
1469 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001470 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1473 return -EINVAL;
1474
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001475 if (!dma)
1476 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Eric Anholtc153f452007-09-03 12:06:45 +10001478 DRM_DEBUG("%d\n", request->count);
1479 for (i = 0; i < request->count; i++) {
1480 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001482 if (idx < 0 || idx >= dma->buf_count) {
1483 DRM_ERROR("Index %d (of %d max)\n",
1484 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return -EINVAL;
1486 }
1487 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001488 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001489 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001490 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -EINVAL;
1492 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001493 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495
1496 return 0;
1497}
1498
1499/**
1500 * Maps all of the DMA buffers into client-virtual space (ioctl).
1501 *
1502 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001503 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 * \param cmd command.
1505 * \param arg pointer to a drm_buf_map structure.
1506 * \return zero on success or a negative number on failure.
1507 *
George Sapountzis3417f332006-10-24 12:03:04 -07001508 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1509 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1510 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1511 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 */
Eric Anholtc153f452007-09-03 12:06:45 +10001513int drm_mapbufs(struct drm_device *dev, void *data,
1514 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Dave Airliecdd55a22007-07-11 16:32:08 +10001516 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 int retcode = 0;
1518 const int zero = 0;
1519 unsigned long virtual;
1520 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001521 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 int i;
1523
1524 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1525 return -EINVAL;
1526
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001527 if (!dma)
1528 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001530 spin_lock(&dev->count_lock);
1531 if (atomic_read(&dev->buf_alloc)) {
1532 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return -EBUSY;
1534 }
1535 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001536 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Eric Anholtc153f452007-09-03 12:06:45 +10001538 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001539 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001541 && (dma->flags & _DRM_DMA_USE_SG))
1542 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1543 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001544 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001545 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001547 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 retcode = -EINVAL;
1549 goto done;
1550 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001551 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001552 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001553 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001554 MAP_SHARED,
1555 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001556 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001558 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001559 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 PROT_READ | PROT_WRITE,
1561 MAP_SHARED, 0);
1562 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001564 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* Real error */
1566 retcode = (signed long)virtual;
1567 goto done;
1568 }
Eric Anholtc153f452007-09-03 12:06:45 +10001569 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001571 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001572 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001573 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001574 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 retcode = -EFAULT;
1576 goto done;
1577 }
Eric Anholtc153f452007-09-03 12:06:45 +10001578 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001579 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001580 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 retcode = -EFAULT;
1582 goto done;
1583 }
Eric Anholtc153f452007-09-03 12:06:45 +10001584 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001585 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 retcode = -EFAULT;
1587 goto done;
1588 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001589 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001590 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001591 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 retcode = -EFAULT;
1593 goto done;
1594 }
1595 }
1596 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001597 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001598 request->count = dma->buf_count;
1599 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 return retcode;
1602}
1603
Dave Airlie836cf042005-07-10 19:27:04 +10001604/**
1605 * Compute size order. Returns the exponent of the smaller power of two which
1606 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001607 *
Dave Airlie836cf042005-07-10 19:27:04 +10001608 * \param size size.
1609 * \return order.
1610 *
1611 * \todo Can be made faster.
1612 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001613int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001614{
1615 int order;
1616 unsigned long tmp;
1617
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001618 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001619
1620 if (size & (size - 1))
1621 ++order;
1622
1623 return order;
1624}
1625EXPORT_SYMBOL(drm_order);