blob: 4fcbc445a8e5454cb8ea5f0cb0cbc5729645f7d6 [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
186#ifdef __alpha__
187 map->offset += dev->hose->mem_space->start;
188#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000189 /* Some drivers preinitialize some maps, without the X Server
190 * needing to be aware of it. Therefore, we just return success
191 * when the server tries to create a duplicate map.
192 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000193 list = drm_find_matching_map(dev, map);
194 if (list != NULL) {
195 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000196 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197 "mismatched sizes, (%ld vs %ld)\n",
198 map->type, map->size,
199 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000200 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000201 }
202
Eric Anholt9a298b22009-03-24 12:23:04 -0700203 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000204 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000205 return 0;
206 }
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 if (map->type == _DRM_FRAME_BUFFER ||
210 (map->flags & _DRM_WRITE_COMBINING)) {
211 map->mtrr = mtrr_add(map->offset, map->size,
212 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 }
Scott Thompson0769d392007-08-25 18:17:49 +1000215 if (map->type == _DRM_REGISTERS) {
Christoph Hellwig004a7722007-01-08 21:56:59 +1100216 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000217 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700218 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000219 return -ENOMEM;
220 }
221 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100225 list = drm_find_matching_map(dev, map);
226 if (list != NULL) {
227 if(list->map->size != map->size) {
228 DRM_DEBUG("Matching maps of type %d with "
229 "mismatched sizes, (%ld vs %ld)\n",
230 map->type, map->size, list->map->size);
231 list->map->size = map->size;
232 }
233
Eric Anholt9a298b22009-03-24 12:23:04 -0700234 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100235 *maplist = list;
236 return 0;
237 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100238 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 DRM_DEBUG("%lu %d %p\n",
240 map->size, drm_order(map->size), map->handle);
241 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700242 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -ENOMEM;
244 }
245 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000248 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000249 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700250 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -EBUSY;
252 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000253 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100256 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000257 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100258 int valid = 0;
259
260 if (!drm_core_has_AGP(dev)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700261 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100264#ifdef __alpha__
265 map->offset += dev->hose->mem_space->start;
266#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000267 /* In some cases (i810 driver), user space may have already
268 * added the AGP base itself, because dev->agp->base previously
269 * only got set during AGP enable. So, only add the base
270 * address if the map's offset isn't already within the
271 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100272 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000273 if (map->offset < dev->agp->base ||
274 map->offset > dev->agp->base +
275 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
276 map->offset += dev->agp->base;
277 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100278 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
279
280 /* This assumes the DRM is in total control of AGP space.
281 * It's not always the case as AGP can be in the control
282 * of user space (i.e. i810 driver). So this loop will get
283 * skipped and we double check that dev->agp->memory is
284 * actually set as well as being invalid before EPERM'ing
285 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000286 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100287 if ((map->offset >= entry->bound) &&
288 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
289 valid = 1;
290 break;
291 }
292 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000293 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700294 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100295 return -EPERM;
296 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100297 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
298 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100301 }
Pekka Paalanen812c3692009-09-17 22:59:54 +0300302 case _DRM_GEM:
303 DRM_ERROR("tried to addmap GEM object\n");
304 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 case _DRM_SCATTER_GATHER:
306 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700307 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -EINVAL;
309 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000310 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000312 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000313 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000314 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000315 * casting it down to 32 bits is no problem, but we
316 * need to point to a 64bit variable first. */
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800317 dmah = drm_pci_alloc(dev, map->size, map->size);
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000318 if (!dmah) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700319 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000320 return -ENOMEM;
321 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000322 map->handle = dmah->vaddr;
323 map->offset = (unsigned long)dmah->busaddr;
324 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700327 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return -EINVAL;
329 }
330
Eric Anholt9a298b22009-03-24 12:23:04 -0700331 list = kmalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000332 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700333 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100334 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700335 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return -EINVAL;
337 }
338 memset(list, 0, sizeof(*list));
339 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
Eric Anholt9a298b22009-03-24 12:23:04 -0700681 entry->buflist = kmalloc(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 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 entry->buf_size = size;
690 entry->page_order = page_order;
691
692 offset = 0;
693
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 while (entry->buf_count < count) {
695 buf = &entry->buflist[entry->buf_count];
696 buf->idx = dma->buf_count + entry->buf_count;
697 buf->total = alignment;
698 buf->order = order;
699 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 buf->bus_address = agp_offset + offset;
703 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 buf->waiting = 0;
706 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000708 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -0700711 buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* Set count correctly so we free the proper amount. */
714 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000715 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100716 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000717 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return -ENOMEM;
719 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000722 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 offset += alignment;
725 entry->buf_count++;
726 byte_count += PAGE_SIZE << page_order;
727 }
728
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Eric Anholt9a298b22009-03-24 12:23:04 -0700731 temp_buflist = krealloc(dma->buflist,
732 (dma->buf_count + entry->buf_count) *
733 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000736 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100737 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -ENOMEM;
740 }
741 dma->buflist = temp_buflist;
742
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
745 }
746
747 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100748 dma->seg_count += entry->seg_count;
749 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 dma->byte_count += byte_count;
751
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000752 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
753 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Dave Airlie30e2fb12006-02-02 19:37:46 +1100755 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Dave Airlied59431b2005-07-10 15:00:06 +1000757 request->count = entry->buf_count;
758 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 dma->flags = _DRM_DMA_USE_AGP;
761
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000762 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return 0;
764}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765EXPORT_SYMBOL(drm_addbufs_agp);
766#endif /* __OS_HAS_AGP */
767
Dave Airlie84b1fd12007-07-11 15:53:27 +1000768int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Dave Airliecdd55a22007-07-11 16:32:08 +1000770 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int count;
772 int order;
773 int size;
774 int total;
775 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000776 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100777 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000778 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int alignment;
780 unsigned long offset;
781 int i;
782 int byte_count;
783 int page_count;
784 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000785 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
788 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100789
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000790 if (!dma)
791 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Dave Airlied985c102006-01-02 21:32:48 +1100793 if (!capable(CAP_SYS_ADMIN))
794 return -EPERM;
795
Dave Airlied59431b2005-07-10 15:00:06 +1000796 count = request->count;
797 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 size = 1 << order;
799
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
801 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
804 return -EINVAL;
805 if (dev->queue_count)
806 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Dave Airlied59431b2005-07-10 15:00:06 +1000808 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
811 total = PAGE_SIZE << page_order;
812
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 spin_lock(&dev->count_lock);
814 if (dev->buf_use) {
815 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -EBUSY;
817 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 atomic_inc(&dev->buf_alloc);
819 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Dave Airlie30e2fb12006-02-02 19:37:46 +1100821 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000823 if (entry->buf_count) {
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 -ENOMEM; /* May only call once for each order */
827 }
828
829 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100830 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return -EINVAL;
833 }
834
Eric Anholt9a298b22009-03-24 12:23:04 -0700835 entry->buflist = kmalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100837 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -ENOMEM;
840 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000841 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Eric Anholt9a298b22009-03-24 12:23:04 -0700843 entry->seglist = kmalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700845 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100846 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000847 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -ENOMEM;
849 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* Keep the original pagelist until we know all the allocations
853 * have succeeded
854 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700855 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
856 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700858 kfree(entry->buflist);
859 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100860 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000861 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return -ENOMEM;
863 }
864 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
866 DRM_DEBUG("pagelist: %d entries\n",
867 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000869 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 entry->page_order = page_order;
871 byte_count = 0;
872 page_count = 0;
873
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000875
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800876 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000877
Dave Airlieddf19b92006-03-19 18:56:12 +1100878 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* Set count correctly so we free the proper amount. */
880 entry->buf_count = count;
881 entry->seg_count = count;
882 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700883 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100884 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000885 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return -ENOMEM;
887 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100888 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 for (i = 0; i < (1 << page_order); i++) {
890 DRM_DEBUG("page %d @ 0x%08lx\n",
891 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100892 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100894 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 for (offset = 0;
897 offset + size <= total && entry->buf_count < count;
898 offset += alignment, ++entry->buf_count) {
899 buf = &entry->buflist[entry->buf_count];
900 buf->idx = dma->buf_count + entry->buf_count;
901 buf->total = alignment;
902 buf->order = order;
903 buf->used = 0;
904 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100905 buf->address = (void *)(dmah->vaddr + offset);
906 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 buf->waiting = 0;
909 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000911 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -0700914 buf->dev_private = kmalloc(buf->dev_priv_size,
915 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* Set count correctly so we free the proper amount. */
918 entry->buf_count = count;
919 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700921 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100922 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000923 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return -ENOMEM;
925 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000926 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 DRM_DEBUG("buffer %d @ %p\n",
929 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931 byte_count += PAGE_SIZE << page_order;
932 }
933
Eric Anholt9a298b22009-03-24 12:23:04 -0700934 temp_buflist = krealloc(dma->buflist,
935 (dma->buf_count + entry->buf_count) *
936 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!temp_buflist) {
938 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700940 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100941 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000942 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -ENOMEM;
944 }
945 dma->buflist = temp_buflist;
946
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000947 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
949 }
950
951 /* No allocations failed, so now we can replace the orginal pagelist
952 * with the new one.
953 */
954 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700955 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 dma->pagelist = temp_pagelist;
958
959 dma->buf_count += entry->buf_count;
960 dma->seg_count += entry->seg_count;
961 dma->page_count += entry->seg_count << page_order;
962 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
963
Dave Airlie30e2fb12006-02-02 19:37:46 +1100964 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dave Airlied59431b2005-07-10 15:00:06 +1000966 request->count = entry->buf_count;
967 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
George Sapountzis3417f332006-10-24 12:03:04 -0700969 if (request->flags & _DRM_PCI_BUFFER_RO)
970 dma->flags = _DRM_DMA_USE_PCI_RO;
971
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return 0;
974
975}
Dave Airlied84f76d2005-07-10 17:04:22 +1000976EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Dave Airlie84b1fd12007-07-11 15:53:27 +1000978static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Dave Airliecdd55a22007-07-11 16:32:08 +1000980 struct drm_device_dma *dma = dev->dma;
981 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000982 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 unsigned long offset;
984 unsigned long agp_offset;
985 int count;
986 int order;
987 int size;
988 int alignment;
989 int page_order;
990 int total;
991 int byte_count;
992 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000993 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 if (!drm_core_check_feature(dev, DRIVER_SG))
996 return -EINVAL;
997
998 if (!dma)
999 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Dave Airlied985c102006-01-02 21:32:48 +11001001 if (!capable(CAP_SYS_ADMIN))
1002 return -EPERM;
1003
Dave Airlied59431b2005-07-10 15:00:06 +10001004 count = request->count;
1005 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 size = 1 << order;
1007
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 alignment = (request->flags & _DRM_PAGE_ALIGN)
1009 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1011 total = PAGE_SIZE << page_order;
1012
1013 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001014 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001016 DRM_DEBUG("count: %d\n", count);
1017 DRM_DEBUG("order: %d\n", order);
1018 DRM_DEBUG("size: %d\n", size);
1019 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1020 DRM_DEBUG("alignment: %d\n", alignment);
1021 DRM_DEBUG("page_order: %d\n", page_order);
1022 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1025 return -EINVAL;
1026 if (dev->queue_count)
1027 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 spin_lock(&dev->count_lock);
1030 if (dev->buf_use) {
1031 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return -EBUSY;
1033 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 atomic_inc(&dev->buf_alloc);
1035 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Dave Airlie30e2fb12006-02-02 19:37:46 +11001037 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001039 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001040 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 atomic_dec(&dev->buf_alloc);
1042 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
1045 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001046 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return -EINVAL;
1049 }
1050
Eric Anholt9a298b22009-03-24 12:23:04 -07001051 entry->buflist = kmalloc(count * sizeof(*entry->buflist),
1052 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001054 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return -ENOMEM;
1057 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 entry->buf_size = size;
1061 entry->page_order = page_order;
1062
1063 offset = 0;
1064
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001065 while (entry->buf_count < count) {
1066 buf = &entry->buflist[entry->buf_count];
1067 buf->idx = dma->buf_count + entry->buf_count;
1068 buf->total = alignment;
1069 buf->order = order;
1070 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001074 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001075 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 buf->waiting = 0;
1078 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001079 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001080 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -07001083 buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001084 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* Set count correctly so we free the proper amount. */
1086 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001088 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return -ENOMEM;
1091 }
1092
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001095 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 offset += alignment;
1098 entry->buf_count++;
1099 byte_count += PAGE_SIZE << page_order;
1100 }
1101
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Eric Anholt9a298b22009-03-24 12:23:04 -07001104 temp_buflist = krealloc(dma->buflist,
1105 (dma->buf_count + entry->buf_count) *
1106 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001107 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001110 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001111 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -ENOMEM;
1113 }
1114 dma->buflist = temp_buflist;
1115
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1118 }
1119
1120 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001121 dma->seg_count += entry->seg_count;
1122 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 dma->byte_count += byte_count;
1124
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001125 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1126 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Dave Airlie30e2fb12006-02-02 19:37:46 +11001128 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Dave Airlied59431b2005-07-10 15:00:06 +10001130 request->count = entry->buf_count;
1131 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 dma->flags = _DRM_DMA_USE_SG;
1134
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001135 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return 0;
1137}
1138
Dave Airlie84b1fd12007-07-11 15:53:27 +10001139static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001140{
Dave Airliecdd55a22007-07-11 16:32:08 +10001141 struct drm_device_dma *dma = dev->dma;
1142 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001143 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001144 unsigned long offset;
1145 unsigned long agp_offset;
1146 int count;
1147 int order;
1148 int size;
1149 int alignment;
1150 int page_order;
1151 int total;
1152 int byte_count;
1153 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001154 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001155
1156 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1157 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001158
Dave Airlieb84397d62005-07-10 14:46:12 +10001159 if (!dma)
1160 return -EINVAL;
1161
Dave Airlied985c102006-01-02 21:32:48 +11001162 if (!capable(CAP_SYS_ADMIN))
1163 return -EPERM;
1164
Dave Airlied59431b2005-07-10 15:00:06 +10001165 count = request->count;
1166 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001167 size = 1 << order;
1168
Dave Airlied59431b2005-07-10 15:00:06 +10001169 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001170 ? PAGE_ALIGN(size) : size;
1171 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1172 total = PAGE_SIZE << page_order;
1173
1174 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001175 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001176
1177 DRM_DEBUG("count: %d\n", count);
1178 DRM_DEBUG("order: %d\n", order);
1179 DRM_DEBUG("size: %d\n", size);
1180 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1181 DRM_DEBUG("alignment: %d\n", alignment);
1182 DRM_DEBUG("page_order: %d\n", page_order);
1183 DRM_DEBUG("total: %d\n", total);
1184
1185 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1186 return -EINVAL;
1187 if (dev->queue_count)
1188 return -EBUSY; /* Not while in use */
1189
1190 spin_lock(&dev->count_lock);
1191 if (dev->buf_use) {
1192 spin_unlock(&dev->count_lock);
1193 return -EBUSY;
1194 }
1195 atomic_inc(&dev->buf_alloc);
1196 spin_unlock(&dev->count_lock);
1197
Dave Airlie30e2fb12006-02-02 19:37:46 +11001198 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001199 entry = &dma->bufs[order];
1200 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001201 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001202 atomic_dec(&dev->buf_alloc);
1203 return -ENOMEM; /* May only call once for each order */
1204 }
1205
1206 if (count < 0 || count > 4096) {
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 -EINVAL;
1210 }
1211
Eric Anholt9a298b22009-03-24 12:23:04 -07001212 entry->buflist = kmalloc(count * sizeof(*entry->buflist),
1213 GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001214 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001215 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001216 atomic_dec(&dev->buf_alloc);
1217 return -ENOMEM;
1218 }
1219 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1220
1221 entry->buf_size = size;
1222 entry->page_order = page_order;
1223
1224 offset = 0;
1225
1226 while (entry->buf_count < count) {
1227 buf = &entry->buflist[entry->buf_count];
1228 buf->idx = dma->buf_count + entry->buf_count;
1229 buf->total = alignment;
1230 buf->order = order;
1231 buf->used = 0;
1232
1233 buf->offset = (dma->byte_count + offset);
1234 buf->bus_address = agp_offset + offset;
1235 buf->address = (void *)(agp_offset + offset);
1236 buf->next = NULL;
1237 buf->waiting = 0;
1238 buf->pending = 0;
1239 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001240 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001241
1242 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -07001243 buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001244 if (!buf->dev_private) {
1245 /* Set count correctly so we free the proper amount. */
1246 entry->buf_count = count;
1247 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001248 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001249 atomic_dec(&dev->buf_alloc);
1250 return -ENOMEM;
1251 }
1252 memset(buf->dev_private, 0, buf->dev_priv_size);
1253
1254 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1255
1256 offset += alignment;
1257 entry->buf_count++;
1258 byte_count += PAGE_SIZE << page_order;
1259 }
1260
1261 DRM_DEBUG("byte_count: %d\n", byte_count);
1262
Eric Anholt9a298b22009-03-24 12:23:04 -07001263 temp_buflist = krealloc(dma->buflist,
1264 (dma->buf_count + entry->buf_count) *
1265 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001266 if (!temp_buflist) {
1267 /* Free the entry because it isn't valid */
1268 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001269 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001270 atomic_dec(&dev->buf_alloc);
1271 return -ENOMEM;
1272 }
1273 dma->buflist = temp_buflist;
1274
1275 for (i = 0; i < entry->buf_count; i++) {
1276 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1277 }
1278
1279 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001280 dma->seg_count += entry->seg_count;
1281 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001282 dma->byte_count += byte_count;
1283
1284 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1285 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1286
Dave Airlie30e2fb12006-02-02 19:37:46 +11001287 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001288
Dave Airlied59431b2005-07-10 15:00:06 +10001289 request->count = entry->buf_count;
1290 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001291
1292 dma->flags = _DRM_DMA_USE_FB;
1293
1294 atomic_dec(&dev->buf_alloc);
1295 return 0;
1296}
Dave Airlied985c102006-01-02 21:32:48 +11001297
Dave Airlieb84397d62005-07-10 14:46:12 +10001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299/**
1300 * Add buffers for DMA transfers (ioctl).
1301 *
1302 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001303 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001305 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 * \return zero on success or a negative number on failure.
1307 *
1308 * According with the memory type specified in drm_buf_desc::flags and the
1309 * build options, it dispatches the call either to addbufs_agp(),
1310 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1311 * PCI memory respectively.
1312 */
Eric Anholtc153f452007-09-03 12:06:45 +10001313int drm_addbufs(struct drm_device *dev, void *data,
1314 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Eric Anholtc153f452007-09-03 12:06:45 +10001316 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001317 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1320 return -EINVAL;
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001323 if (request->flags & _DRM_AGP_BUFFER)
1324 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 else
1326#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001327 if (request->flags & _DRM_SG_BUFFER)
1328 ret = drm_addbufs_sg(dev, request);
1329 else if (request->flags & _DRM_FB_BUFFER)
1330 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 else
Eric Anholtc153f452007-09-03 12:06:45 +10001332 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001333
Dave Airlied59431b2005-07-10 15:00:06 +10001334 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337/**
1338 * Get information about the buffer mappings.
1339 *
1340 * This was originally mean for debugging purposes, or by a sophisticated
1341 * client library to determine how best to use the available buffers (e.g.,
1342 * large buffers can be used for image transfer).
1343 *
1344 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001345 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * \param cmd command.
1347 * \param arg pointer to a drm_buf_info structure.
1348 * \return zero on success or a negative number on failure.
1349 *
1350 * Increments drm_device::buf_use while holding the drm_device::count_lock
1351 * lock, preventing of allocating more buffers after this call. Information
1352 * about each requested buffer is then copied into user space.
1353 */
Eric Anholtc153f452007-09-03 12:06:45 +10001354int drm_infobufs(struct drm_device *dev, void *data,
1355 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Dave Airliecdd55a22007-07-11 16:32:08 +10001357 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001358 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 int i;
1360 int count;
1361
1362 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1363 return -EINVAL;
1364
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001365 if (!dma)
1366 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001368 spin_lock(&dev->count_lock);
1369 if (atomic_read(&dev->buf_alloc)) {
1370 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -EBUSY;
1372 }
1373 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001374 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001376 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1377 if (dma->bufs[i].buf_count)
1378 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001381 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Eric Anholtc153f452007-09-03 12:06:45 +10001383 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001384 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1385 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001386 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001387 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001388 struct drm_buf_entry *from = &dma->bufs[i];
1389 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001390 if (copy_to_user(&to->count,
1391 &from->buf_count,
1392 sizeof(from->buf_count)) ||
1393 copy_to_user(&to->size,
1394 &from->buf_size,
1395 sizeof(from->buf_size)) ||
1396 copy_to_user(&to->low_mark,
1397 &list->low_mark,
1398 sizeof(list->low_mark)) ||
1399 copy_to_user(&to->high_mark,
1400 &list->high_mark,
1401 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return -EFAULT;
1403
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001404 DRM_DEBUG("%d %d %d %d %d\n",
1405 i,
1406 dma->bufs[i].buf_count,
1407 dma->bufs[i].buf_size,
1408 dma->bufs[i].freelist.low_mark,
1409 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 ++count;
1411 }
1412 }
1413 }
Eric Anholtc153f452007-09-03 12:06:45 +10001414 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 return 0;
1417}
1418
1419/**
1420 * Specifies a low and high water mark for buffer allocation
1421 *
1422 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001423 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * \param cmd command.
1425 * \param arg a pointer to a drm_buf_desc structure.
1426 * \return zero on success or a negative number on failure.
1427 *
1428 * Verifies that the size order is bounded between the admissible orders and
1429 * updates the respective drm_device_dma::bufs entry low and high water mark.
1430 *
1431 * \note This ioctl is deprecated and mostly never used.
1432 */
Eric Anholtc153f452007-09-03 12:06:45 +10001433int drm_markbufs(struct drm_device *dev, void *data,
1434 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Dave Airliecdd55a22007-07-11 16:32:08 +10001436 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001437 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001439 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1442 return -EINVAL;
1443
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001444 if (!dma)
1445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001447 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001448 request->size, request->low_mark, request->high_mark);
1449 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001450 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1451 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 entry = &dma->bufs[order];
1453
Eric Anholtc153f452007-09-03 12:06:45 +10001454 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001456 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return -EINVAL;
1458
Eric Anholtc153f452007-09-03 12:06:45 +10001459 entry->freelist.low_mark = request->low_mark;
1460 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 return 0;
1463}
1464
1465/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001466 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 *
1468 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001469 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 * \param cmd command.
1471 * \param arg pointer to a drm_buf_free structure.
1472 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001473 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 * Calls free_buffer() for each used buffer.
1475 * This function is primarily used for debugging.
1476 */
Eric Anholtc153f452007-09-03 12:06:45 +10001477int drm_freebufs(struct drm_device *dev, void *data,
1478 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Dave Airliecdd55a22007-07-11 16:32:08 +10001480 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001481 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 int i;
1483 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001484 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1487 return -EINVAL;
1488
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001489 if (!dma)
1490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Eric Anholtc153f452007-09-03 12:06:45 +10001492 DRM_DEBUG("%d\n", request->count);
1493 for (i = 0; i < request->count; i++) {
1494 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001496 if (idx < 0 || idx >= dma->buf_count) {
1497 DRM_ERROR("Index %d (of %d max)\n",
1498 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return -EINVAL;
1500 }
1501 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001502 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001503 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001504 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return -EINVAL;
1506 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001507 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509
1510 return 0;
1511}
1512
1513/**
1514 * Maps all of the DMA buffers into client-virtual space (ioctl).
1515 *
1516 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001517 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 * \param cmd command.
1519 * \param arg pointer to a drm_buf_map structure.
1520 * \return zero on success or a negative number on failure.
1521 *
George Sapountzis3417f332006-10-24 12:03:04 -07001522 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1523 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1524 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1525 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
Eric Anholtc153f452007-09-03 12:06:45 +10001527int drm_mapbufs(struct drm_device *dev, void *data,
1528 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Dave Airliecdd55a22007-07-11 16:32:08 +10001530 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 int retcode = 0;
1532 const int zero = 0;
1533 unsigned long virtual;
1534 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001535 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 int i;
1537
1538 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1539 return -EINVAL;
1540
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001541 if (!dma)
1542 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001544 spin_lock(&dev->count_lock);
1545 if (atomic_read(&dev->buf_alloc)) {
1546 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return -EBUSY;
1548 }
1549 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001550 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Eric Anholtc153f452007-09-03 12:06:45 +10001552 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001553 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001554 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001555 && (dma->flags & _DRM_DMA_USE_SG))
1556 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1557 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001558 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001559 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001561 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 retcode = -EINVAL;
1563 goto done;
1564 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001565 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001566 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001567 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001568 MAP_SHARED,
1569 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001570 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001572 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001573 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001574 PROT_READ | PROT_WRITE,
1575 MAP_SHARED, 0);
1576 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001578 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* Real error */
1580 retcode = (signed long)virtual;
1581 goto done;
1582 }
Eric Anholtc153f452007-09-03 12:06:45 +10001583 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001585 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001586 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001587 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001588 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 retcode = -EFAULT;
1590 goto done;
1591 }
Eric Anholtc153f452007-09-03 12:06:45 +10001592 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001593 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001594 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 retcode = -EFAULT;
1596 goto done;
1597 }
Eric Anholtc153f452007-09-03 12:06:45 +10001598 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001599 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 retcode = -EFAULT;
1601 goto done;
1602 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001603 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001604 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001605 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 retcode = -EFAULT;
1607 goto done;
1608 }
1609 }
1610 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001611 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001612 request->count = dma->buf_count;
1613 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 return retcode;
1616}
1617
Dave Airlie836cf042005-07-10 19:27:04 +10001618/**
1619 * Compute size order. Returns the exponent of the smaller power of two which
1620 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001621 *
Dave Airlie836cf042005-07-10 19:27:04 +10001622 * \param size size.
1623 * \return order.
1624 *
1625 * \todo Can be made faster.
1626 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001627int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001628{
1629 int order;
1630 unsigned long tmp;
1631
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001632 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001633
1634 if (size & (size - 1))
1635 ++order;
1636
1637 return order;
1638}
1639EXPORT_SYMBOL(drm_order);