blob: 61e1ef90d4e5190cc9ca515f5f043b55a0465c5f [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
Tormod Volden66aa6962011-05-30 19:45:43 +000049 * while PCI resources may live above that, we only compare the
50 * lower 32 bits of the map offset for maps of type
51 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
52 * It is assumed that if a driver have more than one resource
53 * of each type, the lower 32 bits are different.
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110054 */
55 if (!entry->map ||
56 map->type != entry->map->type ||
57 entry->master != dev->primary->master)
58 continue;
59 switch (map->type) {
60 case _DRM_SHM:
61 if (map->flags != _DRM_CONTAINS_LOCK)
62 break;
Tormod Volden66aa6962011-05-30 19:45:43 +000063 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110064 case _DRM_REGISTERS:
65 case _DRM_FRAME_BUFFER:
Tormod Volden66aa6962011-05-30 19:45:43 +000066 if ((entry->map->offset & 0xffffffff) ==
67 (map->offset & 0xffffffff))
68 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110069 default: /* Make gcc happy */
70 ;
Dave Airlie836cf042005-07-10 19:27:04 +100071 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110072 if (entry->map->offset == map->offset)
73 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100074 }
75
76 return NULL;
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Dave Airliee0be4282007-07-12 10:26:44 +100079static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080080 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100081{
David Millerf1a2a9b2009-02-18 15:41:02 -080082 int use_hashed_handle, shift;
83 unsigned long add;
84
Dave Airliec2604ce2006-08-12 16:03:26 +100085#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100086 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
87#elif (BITS_PER_LONG == 32)
88 use_hashed_handle = hashed_handle;
89#else
90#error Unsupported long size. Neither 64 nor 32 bits.
91#endif
Dave Airlied1f2b552005-08-05 22:11:22 +100092
Thomas Hellstrome08870c2006-09-22 04:18:37 +100093 if (!use_hashed_handle) {
94 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +110095 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +100096 ret = drm_ht_insert_item(&dev->map_hash, hash);
97 if (ret != -EINVAL)
98 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +100099 }
David Millerf1a2a9b2009-02-18 15:41:02 -0800100
101 shift = 0;
102 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
103 if (shm && (SHMLBA > PAGE_SIZE)) {
104 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
105
106 /* For shared memory, we have to preserve the SHMLBA
107 * bits of the eventual vma->vm_pgoff value during
108 * mmap(). Otherwise we run into cache aliasing problems
109 * on some platforms. On these platforms, the pgoff of
110 * a mmap() request is used to pick a suitable virtual
111 * address for the mmap() region such that it will not
112 * cause cache aliasing problems.
113 *
114 * Therefore, make sure the SHMLBA relevant bits of the
115 * hash value we use are equal to those in the original
116 * kernel virtual address.
117 */
118 shift = bits;
119 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
120 }
121
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000122 return drm_ht_just_insert_please(&dev->map_hash, hash,
123 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800124 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000125}
Dave Airlie9a186642005-06-23 21:29:18 +1000126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/**
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100128 * Core function to create a range of memory available for mapping by a
129 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *
131 * Adjusts the memory offset to its absolute value according to the mapping
132 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
133 * applicable and if supported by the kernel.
134 */
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100135static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000136 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000137 enum drm_map_flags flags,
138 struct drm_map_list ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100140 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000141 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000142 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000143 unsigned long user_token;
144 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Eric Anholt9a298b22009-03-24 12:23:04 -0700146 map = kmalloc(sizeof(*map), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000147 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -ENOMEM;
149
Dave Airlie7ab98402005-07-10 16:56:52 +1000150 map->offset = offset;
151 map->size = size;
152 map->flags = flags;
153 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* Only allow shared memory to be removable since we only keep enough
156 * book keeping information about shared memory to allow for removal
157 * when processes fork.
158 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700160 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -EINVAL;
162 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100163 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
164 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000165
166 /* page-align _DRM_SHM maps. They are allocated here so there is no security
167 * hole created by that and it works around various broken drivers that use
168 * a non-aligned quantity to map the SAREA. --BenH
169 */
170 if (map->type == _DRM_SHM)
171 map->size = PAGE_ALIGN(map->size);
172
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100173 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700174 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return -EINVAL;
176 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000177 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 map->handle = NULL;
179
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000180 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 case _DRM_REGISTERS:
182 case _DRM_FRAME_BUFFER:
Jordan Crouse4b7fb9b2010-05-27 13:40:26 -0600183#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100184 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 map->offset < virt_to_phys(high_memory)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700186 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
188 }
189#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000190 /* Some drivers preinitialize some maps, without the X Server
191 * needing to be aware of it. Therefore, we just return success
192 * when the server tries to create a duplicate map.
193 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000194 list = drm_find_matching_map(dev, map);
195 if (list != NULL) {
196 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000197 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198 "mismatched sizes, (%ld vs %ld)\n",
199 map->type, map->size,
200 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000201 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000202 }
203
Eric Anholt9a298b22009-03-24 12:23:04 -0700204 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000205 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000206 return 0;
207 }
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 if (map->type == _DRM_FRAME_BUFFER ||
211 (map->flags & _DRM_WRITE_COMBINING)) {
212 map->mtrr = mtrr_add(map->offset, map->size,
213 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 }
Scott Thompson0769d392007-08-25 18:17:49 +1000216 if (map->type == _DRM_REGISTERS) {
Christoph Hellwig004a7722007-01-08 21:56:59 +1100217 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000218 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700219 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000220 return -ENOMEM;
221 }
222 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100226 list = drm_find_matching_map(dev, map);
227 if (list != NULL) {
228 if(list->map->size != map->size) {
229 DRM_DEBUG("Matching maps of type %d with "
230 "mismatched sizes, (%ld vs %ld)\n",
231 map->type, map->size, list->map->size);
232 list->map->size = map->size;
233 }
234
Eric Anholt9a298b22009-03-24 12:23:04 -0700235 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100236 *maplist = list;
237 return 0;
238 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100239 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000240 DRM_DEBUG("%lu %d %p\n",
241 map->size, drm_order(map->size), map->handle);
242 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700243 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -ENOMEM;
245 }
246 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000249 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000250 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700251 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return -EBUSY;
253 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000254 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100257 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000258 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100259 int valid = 0;
260
261 if (!drm_core_has_AGP(dev)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700262 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100263 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100265#ifdef __alpha__
266 map->offset += dev->hose->mem_space->start;
267#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000268 /* In some cases (i810 driver), user space may have already
269 * added the AGP base itself, because dev->agp->base previously
270 * only got set during AGP enable. So, only add the base
271 * address if the map's offset isn't already within the
272 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100273 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000274 if (map->offset < dev->agp->base ||
275 map->offset > dev->agp->base +
276 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
277 map->offset += dev->agp->base;
278 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100279 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
280
281 /* This assumes the DRM is in total control of AGP space.
282 * It's not always the case as AGP can be in the control
283 * of user space (i.e. i810 driver). So this loop will get
284 * skipped and we double check that dev->agp->memory is
285 * actually set as well as being invalid before EPERM'ing
286 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000287 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100288 if ((map->offset >= entry->bound) &&
289 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
290 valid = 1;
291 break;
292 }
293 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000294 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700295 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100296 return -EPERM;
297 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100298 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
299 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100302 }
Pekka Paalanen812c3692009-09-17 22:59:54 +0300303 case _DRM_GEM:
304 DRM_ERROR("tried to addmap GEM object\n");
305 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 case _DRM_SCATTER_GATHER:
307 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700308 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return -EINVAL;
310 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000311 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000314 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000315 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000316 * casting it down to 32 bits is no problem, but we
317 * need to point to a 64bit variable first. */
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800318 dmah = drm_pci_alloc(dev, map->size, map->size);
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000319 if (!dmah) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700320 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000321 return -ENOMEM;
322 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000323 map->handle = dmah->vaddr;
324 map->offset = (unsigned long)dmah->busaddr;
325 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700328 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -EINVAL;
330 }
331
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400332 list = kzalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700334 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100335 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700336 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return -EINVAL;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 list->map = map;
340
Dave Airlie30e2fb12006-02-02 19:37:46 +1100341 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000342 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000343
Dave Airlied1f2b552005-08-05 22:11:22 +1000344 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100345 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000346 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
347 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800348 ret = drm_map_handle(dev, &list->hash, user_token, 0,
349 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000350 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700351 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100352 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700353 kfree(map);
354 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000355 mutex_unlock(&dev->struct_mutex);
356 return ret;
357 }
358
Thomas Hellstrom15450852007-02-08 16:14:05 +1100359 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100360 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Ben Skeggs2ff2e8a2009-05-26 10:35:52 +1000362 if (!(map->flags & _DRM_DRIVER))
363 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000364 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000365 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100366 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000367
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100368int drm_addmap(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000369 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100370 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000371{
Dave Airlie55910512007-07-11 16:53:40 +1000372 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000373 int rc;
374
375 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
376 if (!rc)
377 *map_ptr = list->map;
378 return rc;
379}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000380
Dave Airlie7ab98402005-07-10 16:56:52 +1000381EXPORT_SYMBOL(drm_addmap);
382
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100383/**
384 * Ioctl to specify a range of memory that is available for mapping by a
385 * non-root process.
386 *
387 * \param inode device inode.
388 * \param file_priv DRM file private.
389 * \param cmd command.
390 * \param arg pointer to a drm_map structure.
391 * \return zero on success or a negative value on error.
392 *
393 */
Eric Anholtc153f452007-09-03 12:06:45 +1000394int drm_addmap_ioctl(struct drm_device *dev, void *data,
395 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000396{
Eric Anholtc153f452007-09-03 12:06:45 +1000397 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000398 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000399 int err;
400
Dave Airlie7c1c2872008-11-28 14:22:24 +1000401 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100402 return -EPERM;
403
Eric Anholtc153f452007-09-03 12:06:45 +1000404 err = drm_addmap_core(dev, map->offset, map->size, map->type,
405 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000406
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000407 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000408 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000409
Dave Airlie67e1a012005-10-24 18:41:39 +1000410 /* 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 +1000411 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000413}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/**
416 * Remove a map private from list and deallocate resources if the mapping
417 * isn't in use.
418 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * Searches the map on drm_device::maplist, removes it from the list, see if
420 * its being used, and free any associate resource (such as MTRR's) if it's not
421 * being on use.
422 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000423 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100425int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Dave Airlie55910512007-07-11 16:53:40 +1000427 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000428 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000429 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000430 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Dave Airlie836cf042005-07-10 19:27:04 +1000432 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000433 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000434 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000435 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000436 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100437 drm_ht_remove_key(&dev->map_hash,
438 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700439 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000440 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
Dave Airlie836cf042005-07-10 19:27:04 +1000444
Dave Airliebd1b3312007-05-26 05:01:51 +1000445 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000446 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000447
448 switch (map->type) {
449 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100450 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000451 /* FALLTHROUGH */
452 case _DRM_FRAME_BUFFER:
453 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
454 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000455 retcode = mtrr_del(map->mtrr, map->offset, map->size);
456 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000457 }
458 break;
459 case _DRM_SHM:
460 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000461 if (master) {
462 if (dev->sigdata.lock == master->lock.hw_lock)
463 dev->sigdata.lock = NULL;
464 master->lock.hw_lock = NULL; /* SHM removed */
465 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100466 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000467 }
Dave Airlie836cf042005-07-10 19:27:04 +1000468 break;
469 case _DRM_AGP:
470 case _DRM_SCATTER_GATHER:
471 break;
472 case _DRM_CONSISTENT:
473 dmah.vaddr = map->handle;
474 dmah.busaddr = map->offset;
475 dmah.size = map->size;
476 __drm_pci_free(dev, &dmah);
477 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800478 case _DRM_GEM:
479 DRM_ERROR("tried to rmmap GEM object\n");
480 break;
Dave Airlie836cf042005-07-10 19:27:04 +1000481 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700482 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485}
Dave Airlie4e74f362008-12-19 10:23:14 +1100486EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000487
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100488int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000489{
490 int ret;
491
Dave Airlie30e2fb12006-02-02 19:37:46 +1100492 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000493 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100494 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000495
496 return ret;
497}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000498EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000499
Dave Airlie836cf042005-07-10 19:27:04 +1000500/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
501 * the last close of the device, and this is necessary for cleanup when things
502 * exit uncleanly. Therefore, having userland manually remove mappings seems
503 * like a pointless exercise since they're going away anyway.
504 *
505 * One use case might be after addmap is allowed for normal users for SHM and
506 * gets used by drivers that the server doesn't need to care about. This seems
507 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100508 *
509 * \param inode device inode.
510 * \param file_priv DRM file private.
511 * \param cmd command.
512 * \param arg pointer to a struct drm_map structure.
513 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000514 */
Eric Anholtc153f452007-09-03 12:06:45 +1000515int drm_rmmap_ioctl(struct drm_device *dev, void *data,
516 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000517{
Eric Anholtc153f452007-09-03 12:06:45 +1000518 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100519 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000520 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000521 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000522
Dave Airlie30e2fb12006-02-02 19:37:46 +1100523 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000524 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000525 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000526 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000527 r_list->map->flags & _DRM_REMOVABLE) {
528 map = r_list->map;
529 break;
530 }
531 }
532
533 /* List has wrapped around to the head pointer, or its empty we didn't
534 * find anything.
535 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000536 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100537 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000538 return -EINVAL;
539 }
540
Dave Airlie836cf042005-07-10 19:27:04 +1000541 /* Register and framebuffer maps are permanent */
542 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100543 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000544 return 0;
545 }
546
547 ret = drm_rmmap_locked(dev, map);
548
Dave Airlie30e2fb12006-02-02 19:37:46 +1100549 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000550
551 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000552}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554/**
555 * Cleanup after an error on one of the addbufs() functions.
556 *
Dave Airlie836cf042005-07-10 19:27:04 +1000557 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * \param entry buffer entry where the error occurred.
559 *
560 * Frees any pages and buffers associated with the given entry.
561 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000562static void drm_cleanup_buf_error(struct drm_device * dev,
563 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 int i;
566
567 if (entry->seg_count) {
568 for (i = 0; i < entry->seg_count; i++) {
569 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100570 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700573 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 entry->seg_count = 0;
576 }
577
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 if (entry->buf_count) {
579 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700580 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700582 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 entry->buf_count = 0;
585 }
586}
587
588#if __OS_HAS_AGP
589/**
Dave Airlied59431b2005-07-10 15:00:06 +1000590 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000592 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000593 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * After some sanity checks creates a drm_buf structure for each buffer and
597 * reallocates the buffer list of the same size order to accommodate the new
598 * buffers.
599 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000600int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Dave Airliecdd55a22007-07-11 16:32:08 +1000602 struct drm_device_dma *dma = dev->dma;
603 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000604 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000605 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 unsigned long offset;
607 unsigned long agp_offset;
608 int count;
609 int order;
610 int size;
611 int alignment;
612 int page_order;
613 int total;
614 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100615 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000616 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000618 if (!dma)
619 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dave Airlied59431b2005-07-10 15:00:06 +1000621 count = request->count;
622 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 size = 1 << order;
624
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000625 alignment = (request->flags & _DRM_PAGE_ALIGN)
626 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
628 total = PAGE_SIZE << page_order;
629
630 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000631 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000633 DRM_DEBUG("count: %d\n", count);
634 DRM_DEBUG("order: %d\n", order);
635 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100636 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 DRM_DEBUG("alignment: %d\n", alignment);
638 DRM_DEBUG("page_order: %d\n", page_order);
639 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000641 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
642 return -EINVAL;
643 if (dev->queue_count)
644 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Dave Airlie54ba2f72007-02-10 12:07:47 +1100646 /* Make sure buffers are located in AGP memory that we own */
647 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000648 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100649 if ((agp_offset >= agp_entry->bound) &&
650 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
651 valid = 1;
652 break;
653 }
654 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000655 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100656 DRM_DEBUG("zone invalid\n");
657 return -EINVAL;
658 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000659 spin_lock(&dev->count_lock);
660 if (dev->buf_use) {
661 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -EBUSY;
663 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000664 atomic_inc(&dev->buf_alloc);
665 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Dave Airlie30e2fb12006-02-02 19:37:46 +1100667 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100670 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000671 atomic_dec(&dev->buf_alloc);
672 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
675 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100676 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000677 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -EINVAL;
679 }
680
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400681 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 if (!entry->buflist) {
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return -ENOMEM;
686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 entry->buf_size = size;
689 entry->page_order = page_order;
690
691 offset = 0;
692
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000693 while (entry->buf_count < count) {
694 buf = &entry->buflist[entry->buf_count];
695 buf->idx = dma->buf_count + entry->buf_count;
696 buf->total = alignment;
697 buf->order = order;
698 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 buf->bus_address = agp_offset + offset;
702 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 buf->waiting = 0;
705 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000707 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400710 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Set count correctly so we free the proper amount. */
713 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100715 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -ENOMEM;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 offset += alignment;
723 entry->buf_count++;
724 byte_count += PAGE_SIZE << page_order;
725 }
726
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000727 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Eric Anholt9a298b22009-03-24 12:23:04 -0700729 temp_buflist = krealloc(dma->buflist,
730 (dma->buf_count + entry->buf_count) *
731 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100735 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000736 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -ENOMEM;
738 }
739 dma->buflist = temp_buflist;
740
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000741 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
743 }
744
745 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100746 dma->seg_count += entry->seg_count;
747 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 dma->byte_count += byte_count;
749
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
751 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Dave Airlie30e2fb12006-02-02 19:37:46 +1100753 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Dave Airlied59431b2005-07-10 15:00:06 +1000755 request->count = entry->buf_count;
756 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 dma->flags = _DRM_DMA_USE_AGP;
759
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000760 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 0;
762}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000763EXPORT_SYMBOL(drm_addbufs_agp);
764#endif /* __OS_HAS_AGP */
765
Dave Airlie84b1fd12007-07-11 15:53:27 +1000766int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Dave Airliecdd55a22007-07-11 16:32:08 +1000768 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int count;
770 int order;
771 int size;
772 int total;
773 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000774 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100775 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000776 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int alignment;
778 unsigned long offset;
779 int i;
780 int byte_count;
781 int page_count;
782 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000783 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000785 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
786 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100787
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000788 if (!dma)
789 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Dave Airlied985c102006-01-02 21:32:48 +1100791 if (!capable(CAP_SYS_ADMIN))
792 return -EPERM;
793
Dave Airlied59431b2005-07-10 15:00:06 +1000794 count = request->count;
795 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 size = 1 << order;
797
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000798 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
799 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
802 return -EINVAL;
803 if (dev->queue_count)
804 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Dave Airlied59431b2005-07-10 15:00:06 +1000806 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
809 total = PAGE_SIZE << page_order;
810
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 spin_lock(&dev->count_lock);
812 if (dev->buf_use) {
813 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -EBUSY;
815 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000816 atomic_inc(&dev->buf_alloc);
817 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Dave Airlie30e2fb12006-02-02 19:37:46 +1100819 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100822 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -ENOMEM; /* May only call once for each order */
825 }
826
827 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100828 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -EINVAL;
831 }
832
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400833 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000834 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100835 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -ENOMEM;
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400840 entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000841 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700842 kfree(entry->buflist);
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 -ENOMEM;
846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 /* Keep the original pagelist until we know all the allocations
849 * have succeeded
850 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700851 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
852 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700854 kfree(entry->buflist);
855 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100856 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -ENOMEM;
859 }
860 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
862 DRM_DEBUG("pagelist: %d entries\n",
863 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 entry->page_order = page_order;
867 byte_count = 0;
868 page_count = 0;
869
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000871
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800872 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000873
Dave Airlieddf19b92006-03-19 18:56:12 +1100874 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 /* Set count correctly so we free the proper amount. */
876 entry->buf_count = count;
877 entry->seg_count = count;
878 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700879 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100880 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000881 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return -ENOMEM;
883 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100884 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000885 for (i = 0; i < (1 << page_order); i++) {
886 DRM_DEBUG("page %d @ 0x%08lx\n",
887 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100888 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100890 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 for (offset = 0;
893 offset + size <= total && entry->buf_count < count;
894 offset += alignment, ++entry->buf_count) {
895 buf = &entry->buflist[entry->buf_count];
896 buf->idx = dma->buf_count + entry->buf_count;
897 buf->total = alignment;
898 buf->order = order;
899 buf->used = 0;
900 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100901 buf->address = (void *)(dmah->vaddr + offset);
902 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000903 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 buf->waiting = 0;
905 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000906 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000907 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -0400910 buf->dev_private = kzalloc(buf->dev_priv_size,
911 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* Set count correctly so we free the proper amount. */
914 entry->buf_count = count;
915 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700917 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100918 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -ENOMEM;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000923 DRM_DEBUG("buffer %d @ %p\n",
924 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 byte_count += PAGE_SIZE << page_order;
927 }
928
Eric Anholt9a298b22009-03-24 12:23:04 -0700929 temp_buflist = krealloc(dma->buflist,
930 (dma->buf_count + entry->buf_count) *
931 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!temp_buflist) {
933 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000934 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700935 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100936 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000937 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return -ENOMEM;
939 }
940 dma->buflist = temp_buflist;
941
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
944 }
945
Thomas Weber88393162010-03-16 11:47:56 +0100946 /* No allocations failed, so now we can replace the original pagelist
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 * with the new one.
948 */
949 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700950 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952 dma->pagelist = temp_pagelist;
953
954 dma->buf_count += entry->buf_count;
955 dma->seg_count += entry->seg_count;
956 dma->page_count += entry->seg_count << page_order;
957 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
958
Dave Airlie30e2fb12006-02-02 19:37:46 +1100959 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Dave Airlied59431b2005-07-10 15:00:06 +1000961 request->count = entry->buf_count;
962 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
George Sapountzis3417f332006-10-24 12:03:04 -0700964 if (request->flags & _DRM_PCI_BUFFER_RO)
965 dma->flags = _DRM_DMA_USE_PCI_RO;
966
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return 0;
969
970}
Dave Airlied84f76d2005-07-10 17:04:22 +1000971EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Dave Airlie84b1fd12007-07-11 15:53:27 +1000973static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Dave Airliecdd55a22007-07-11 16:32:08 +1000975 struct drm_device_dma *dma = dev->dma;
976 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000977 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 unsigned long offset;
979 unsigned long agp_offset;
980 int count;
981 int order;
982 int size;
983 int alignment;
984 int page_order;
985 int total;
986 int byte_count;
987 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000988 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000990 if (!drm_core_check_feature(dev, DRIVER_SG))
991 return -EINVAL;
992
993 if (!dma)
994 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Dave Airlied985c102006-01-02 21:32:48 +1100996 if (!capable(CAP_SYS_ADMIN))
997 return -EPERM;
998
Dave Airlied59431b2005-07-10 15:00:06 +1000999 count = request->count;
1000 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 size = 1 << order;
1002
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 alignment = (request->flags & _DRM_PAGE_ALIGN)
1004 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1006 total = PAGE_SIZE << page_order;
1007
1008 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001009 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011 DRM_DEBUG("count: %d\n", count);
1012 DRM_DEBUG("order: %d\n", order);
1013 DRM_DEBUG("size: %d\n", size);
1014 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1015 DRM_DEBUG("alignment: %d\n", alignment);
1016 DRM_DEBUG("page_order: %d\n", page_order);
1017 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001019 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1020 return -EINVAL;
1021 if (dev->queue_count)
1022 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 spin_lock(&dev->count_lock);
1025 if (dev->buf_use) {
1026 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -EBUSY;
1028 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 atomic_inc(&dev->buf_alloc);
1030 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Dave Airlie30e2fb12006-02-02 19:37:46 +11001032 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001035 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 atomic_dec(&dev->buf_alloc);
1037 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
1040 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001041 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return -EINVAL;
1044 }
1045
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001046 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001047 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001048 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001049 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001050 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return -ENOMEM;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 entry->buf_size = size;
1055 entry->page_order = page_order;
1056
1057 offset = 0;
1058
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 while (entry->buf_count < count) {
1060 buf = &entry->buflist[entry->buf_count];
1061 buf->idx = dma->buf_count + entry->buf_count;
1062 buf->total = alignment;
1063 buf->order = order;
1064 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001069 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 buf->waiting = 0;
1072 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001074 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001077 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* Set count correctly so we free the proper amount. */
1080 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001081 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001082 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return -ENOMEM;
1085 }
1086
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 offset += alignment;
1090 entry->buf_count++;
1091 byte_count += PAGE_SIZE << page_order;
1092 }
1093
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Eric Anholt9a298b22009-03-24 12:23:04 -07001096 temp_buflist = krealloc(dma->buflist,
1097 (dma->buf_count + entry->buf_count) *
1098 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001102 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -ENOMEM;
1105 }
1106 dma->buflist = temp_buflist;
1107
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1110 }
1111
1112 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001113 dma->seg_count += entry->seg_count;
1114 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 dma->byte_count += byte_count;
1116
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001117 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1118 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Dave Airlie30e2fb12006-02-02 19:37:46 +11001120 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Dave Airlied59431b2005-07-10 15:00:06 +10001122 request->count = entry->buf_count;
1123 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 dma->flags = _DRM_DMA_USE_SG;
1126
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001127 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return 0;
1129}
1130
Dave Airlie84b1fd12007-07-11 15:53:27 +10001131static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001132{
Dave Airliecdd55a22007-07-11 16:32:08 +10001133 struct drm_device_dma *dma = dev->dma;
1134 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001135 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001136 unsigned long offset;
1137 unsigned long agp_offset;
1138 int count;
1139 int order;
1140 int size;
1141 int alignment;
1142 int page_order;
1143 int total;
1144 int byte_count;
1145 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001146 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001147
1148 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1149 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001150
Dave Airlieb84397d62005-07-10 14:46:12 +10001151 if (!dma)
1152 return -EINVAL;
1153
Dave Airlied985c102006-01-02 21:32:48 +11001154 if (!capable(CAP_SYS_ADMIN))
1155 return -EPERM;
1156
Dave Airlied59431b2005-07-10 15:00:06 +10001157 count = request->count;
1158 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001159 size = 1 << order;
1160
Dave Airlied59431b2005-07-10 15:00:06 +10001161 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001162 ? PAGE_ALIGN(size) : size;
1163 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1164 total = PAGE_SIZE << page_order;
1165
1166 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001167 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001168
1169 DRM_DEBUG("count: %d\n", count);
1170 DRM_DEBUG("order: %d\n", order);
1171 DRM_DEBUG("size: %d\n", size);
1172 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1173 DRM_DEBUG("alignment: %d\n", alignment);
1174 DRM_DEBUG("page_order: %d\n", page_order);
1175 DRM_DEBUG("total: %d\n", total);
1176
1177 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1178 return -EINVAL;
1179 if (dev->queue_count)
1180 return -EBUSY; /* Not while in use */
1181
1182 spin_lock(&dev->count_lock);
1183 if (dev->buf_use) {
1184 spin_unlock(&dev->count_lock);
1185 return -EBUSY;
1186 }
1187 atomic_inc(&dev->buf_alloc);
1188 spin_unlock(&dev->count_lock);
1189
Dave Airlie30e2fb12006-02-02 19:37:46 +11001190 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001191 entry = &dma->bufs[order];
1192 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001193 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001194 atomic_dec(&dev->buf_alloc);
1195 return -ENOMEM; /* May only call once for each order */
1196 }
1197
1198 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001199 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001200 atomic_dec(&dev->buf_alloc);
1201 return -EINVAL;
1202 }
1203
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001204 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
Eric Anholt9a298b22009-03-24 12:23:04 -07001205 GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001206 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001207 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001208 atomic_dec(&dev->buf_alloc);
1209 return -ENOMEM;
1210 }
Dave Airlieb84397d62005-07-10 14:46:12 +10001211
1212 entry->buf_size = size;
1213 entry->page_order = page_order;
1214
1215 offset = 0;
1216
1217 while (entry->buf_count < count) {
1218 buf = &entry->buflist[entry->buf_count];
1219 buf->idx = dma->buf_count + entry->buf_count;
1220 buf->total = alignment;
1221 buf->order = order;
1222 buf->used = 0;
1223
1224 buf->offset = (dma->byte_count + offset);
1225 buf->bus_address = agp_offset + offset;
1226 buf->address = (void *)(agp_offset + offset);
1227 buf->next = NULL;
1228 buf->waiting = 0;
1229 buf->pending = 0;
1230 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001231 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001232
1233 buf->dev_priv_size = dev->driver->dev_priv_size;
Davidlohr Bueso94e33702010-08-11 09:18:52 -04001234 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001235 if (!buf->dev_private) {
1236 /* Set count correctly so we free the proper amount. */
1237 entry->buf_count = count;
1238 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001239 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001240 atomic_dec(&dev->buf_alloc);
1241 return -ENOMEM;
1242 }
Dave Airlieb84397d62005-07-10 14:46:12 +10001243
1244 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1245
1246 offset += alignment;
1247 entry->buf_count++;
1248 byte_count += PAGE_SIZE << page_order;
1249 }
1250
1251 DRM_DEBUG("byte_count: %d\n", byte_count);
1252
Eric Anholt9a298b22009-03-24 12:23:04 -07001253 temp_buflist = krealloc(dma->buflist,
1254 (dma->buf_count + entry->buf_count) *
1255 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001256 if (!temp_buflist) {
1257 /* Free the entry because it isn't valid */
1258 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001259 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001260 atomic_dec(&dev->buf_alloc);
1261 return -ENOMEM;
1262 }
1263 dma->buflist = temp_buflist;
1264
1265 for (i = 0; i < entry->buf_count; i++) {
1266 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1267 }
1268
1269 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001270 dma->seg_count += entry->seg_count;
1271 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001272 dma->byte_count += byte_count;
1273
1274 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1275 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1276
Dave Airlie30e2fb12006-02-02 19:37:46 +11001277 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001278
Dave Airlied59431b2005-07-10 15:00:06 +10001279 request->count = entry->buf_count;
1280 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001281
1282 dma->flags = _DRM_DMA_USE_FB;
1283
1284 atomic_dec(&dev->buf_alloc);
1285 return 0;
1286}
Dave Airlied985c102006-01-02 21:32:48 +11001287
Dave Airlieb84397d62005-07-10 14:46:12 +10001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289/**
1290 * Add buffers for DMA transfers (ioctl).
1291 *
1292 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001293 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001295 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * \return zero on success or a negative number on failure.
1297 *
1298 * According with the memory type specified in drm_buf_desc::flags and the
1299 * build options, it dispatches the call either to addbufs_agp(),
1300 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1301 * PCI memory respectively.
1302 */
Eric Anholtc153f452007-09-03 12:06:45 +10001303int drm_addbufs(struct drm_device *dev, void *data,
1304 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Eric Anholtc153f452007-09-03 12:06:45 +10001306 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001307 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1310 return -EINVAL;
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001313 if (request->flags & _DRM_AGP_BUFFER)
1314 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 else
1316#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001317 if (request->flags & _DRM_SG_BUFFER)
1318 ret = drm_addbufs_sg(dev, request);
1319 else if (request->flags & _DRM_FB_BUFFER)
1320 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 else
Eric Anholtc153f452007-09-03 12:06:45 +10001322 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001323
Dave Airlied59431b2005-07-10 15:00:06 +10001324 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/**
1328 * Get information about the buffer mappings.
1329 *
1330 * This was originally mean for debugging purposes, or by a sophisticated
1331 * client library to determine how best to use the available buffers (e.g.,
1332 * large buffers can be used for image transfer).
1333 *
1334 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001335 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * \param cmd command.
1337 * \param arg pointer to a drm_buf_info structure.
1338 * \return zero on success or a negative number on failure.
1339 *
1340 * Increments drm_device::buf_use while holding the drm_device::count_lock
1341 * lock, preventing of allocating more buffers after this call. Information
1342 * about each requested buffer is then copied into user space.
1343 */
Eric Anholtc153f452007-09-03 12:06:45 +10001344int drm_infobufs(struct drm_device *dev, void *data,
1345 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Dave Airliecdd55a22007-07-11 16:32:08 +10001347 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001348 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int i;
1350 int count;
1351
1352 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1353 return -EINVAL;
1354
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001355 if (!dma)
1356 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001358 spin_lock(&dev->count_lock);
1359 if (atomic_read(&dev->buf_alloc)) {
1360 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return -EBUSY;
1362 }
1363 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001364 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001366 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1367 if (dma->bufs[i].buf_count)
1368 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001371 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Eric Anholtc153f452007-09-03 12:06:45 +10001373 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001374 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1375 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001376 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001377 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001378 struct drm_buf_entry *from = &dma->bufs[i];
1379 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001380 if (copy_to_user(&to->count,
1381 &from->buf_count,
1382 sizeof(from->buf_count)) ||
1383 copy_to_user(&to->size,
1384 &from->buf_size,
1385 sizeof(from->buf_size)) ||
1386 copy_to_user(&to->low_mark,
1387 &list->low_mark,
1388 sizeof(list->low_mark)) ||
1389 copy_to_user(&to->high_mark,
1390 &list->high_mark,
1391 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return -EFAULT;
1393
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001394 DRM_DEBUG("%d %d %d %d %d\n",
1395 i,
1396 dma->bufs[i].buf_count,
1397 dma->bufs[i].buf_size,
1398 dma->bufs[i].freelist.low_mark,
1399 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 ++count;
1401 }
1402 }
1403 }
Eric Anholtc153f452007-09-03 12:06:45 +10001404 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 return 0;
1407}
1408
1409/**
1410 * Specifies a low and high water mark for buffer allocation
1411 *
1412 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001413 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * \param cmd command.
1415 * \param arg a pointer to a drm_buf_desc structure.
1416 * \return zero on success or a negative number on failure.
1417 *
1418 * Verifies that the size order is bounded between the admissible orders and
1419 * updates the respective drm_device_dma::bufs entry low and high water mark.
1420 *
1421 * \note This ioctl is deprecated and mostly never used.
1422 */
Eric Anholtc153f452007-09-03 12:06:45 +10001423int drm_markbufs(struct drm_device *dev, void *data,
1424 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Dave Airliecdd55a22007-07-11 16:32:08 +10001426 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001427 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001429 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1432 return -EINVAL;
1433
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001434 if (!dma)
1435 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001437 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001438 request->size, request->low_mark, request->high_mark);
1439 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001440 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 entry = &dma->bufs[order];
1443
Eric Anholtc153f452007-09-03 12:06:45 +10001444 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001446 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return -EINVAL;
1448
Eric Anholtc153f452007-09-03 12:06:45 +10001449 entry->freelist.low_mark = request->low_mark;
1450 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 return 0;
1453}
1454
1455/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001456 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
1458 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001459 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 * \param cmd command.
1461 * \param arg pointer to a drm_buf_free structure.
1462 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001463 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * Calls free_buffer() for each used buffer.
1465 * This function is primarily used for debugging.
1466 */
Eric Anholtc153f452007-09-03 12:06:45 +10001467int drm_freebufs(struct drm_device *dev, void *data,
1468 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Dave Airliecdd55a22007-07-11 16:32:08 +10001470 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001471 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int i;
1473 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001474 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1477 return -EINVAL;
1478
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001479 if (!dma)
1480 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Eric Anholtc153f452007-09-03 12:06:45 +10001482 DRM_DEBUG("%d\n", request->count);
1483 for (i = 0; i < request->count; i++) {
1484 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001486 if (idx < 0 || idx >= dma->buf_count) {
1487 DRM_ERROR("Index %d (of %d max)\n",
1488 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return -EINVAL;
1490 }
1491 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001492 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001493 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001494 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -EINVAL;
1496 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001497 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
1500 return 0;
1501}
1502
1503/**
1504 * Maps all of the DMA buffers into client-virtual space (ioctl).
1505 *
1506 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001507 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 * \param cmd command.
1509 * \param arg pointer to a drm_buf_map structure.
1510 * \return zero on success or a negative number on failure.
1511 *
George Sapountzis3417f332006-10-24 12:03:04 -07001512 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1513 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1514 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1515 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 */
Eric Anholtc153f452007-09-03 12:06:45 +10001517int drm_mapbufs(struct drm_device *dev, void *data,
1518 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Dave Airliecdd55a22007-07-11 16:32:08 +10001520 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 int retcode = 0;
1522 const int zero = 0;
1523 unsigned long virtual;
1524 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001525 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 int i;
1527
1528 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1529 return -EINVAL;
1530
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001531 if (!dma)
1532 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001534 spin_lock(&dev->count_lock);
1535 if (atomic_read(&dev->buf_alloc)) {
1536 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EBUSY;
1538 }
1539 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Eric Anholtc153f452007-09-03 12:06:45 +10001542 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001543 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001544 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001545 && (dma->flags & _DRM_DMA_USE_SG))
1546 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1547 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001548 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001549 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001551 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 retcode = -EINVAL;
1553 goto done;
1554 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001555 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001556 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001557 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001558 MAP_SHARED,
1559 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001562 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001563 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001564 PROT_READ | PROT_WRITE,
1565 MAP_SHARED, 0);
1566 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001568 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 /* Real error */
1570 retcode = (signed long)virtual;
1571 goto done;
1572 }
Eric Anholtc153f452007-09-03 12:06:45 +10001573 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001575 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001576 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001577 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001578 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 retcode = -EFAULT;
1580 goto done;
1581 }
Eric Anholtc153f452007-09-03 12:06:45 +10001582 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001583 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001584 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 retcode = -EFAULT;
1586 goto done;
1587 }
Eric Anholtc153f452007-09-03 12:06:45 +10001588 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001589 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 retcode = -EFAULT;
1591 goto done;
1592 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001593 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001594 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001595 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 retcode = -EFAULT;
1597 goto done;
1598 }
1599 }
1600 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001601 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001602 request->count = dma->buf_count;
1603 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 return retcode;
1606}
1607
Dave Airlie836cf042005-07-10 19:27:04 +10001608/**
1609 * Compute size order. Returns the exponent of the smaller power of two which
1610 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001611 *
Dave Airlie836cf042005-07-10 19:27:04 +10001612 * \param size size.
1613 * \return order.
1614 *
1615 * \todo Can be made faster.
1616 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001617int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001618{
1619 int order;
1620 unsigned long tmp;
1621
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001622 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001623
1624 if (size & (size - 1))
1625 ++order;
1626
1627 return order;
1628}
1629EXPORT_SYMBOL(drm_order);