blob: 8417cc4c43f1104a5ae6bfe35e28936db9f5235a [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>
David Millerf1a2a9b2009-02-18 15:41:02 -080037#include <linux/log2.h>
38#include <asm/shmparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "drmP.h"
40
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +110041resource_size_t drm_get_resource_start(struct drm_device *dev, unsigned int resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Dave Airlie836cf042005-07-10 19:27:04 +100043 return pci_resource_start(dev->pdev, resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
Dave Airlie836cf042005-07-10 19:27:04 +100045EXPORT_SYMBOL(drm_get_resource_start);
46
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +110047resource_size_t drm_get_resource_len(struct drm_device *dev, unsigned int resource)
Dave Airlie836cf042005-07-10 19:27:04 +100048{
49 return pci_resource_len(dev->pdev, resource);
50}
Dave Airlieb5e89ed2005-09-25 14:28:13 +100051
Dave Airlie836cf042005-07-10 19:27:04 +100052EXPORT_SYMBOL(drm_get_resource_len);
53
Dave Airlie55910512007-07-11 16:53:40 +100054static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +110055 struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +100056{
Dave Airlie55910512007-07-11 16:53:40 +100057 struct drm_map_list *entry;
Dave Airliebd1b3312007-05-26 05:01:51 +100058 list_for_each_entry(entry, &dev->maplist, head) {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110059 /*
60 * Because the kernel-userspace ABI is fixed at a 32-bit offset
61 * while PCI resources may live above that, we ignore the map
62 * offset for maps of type _DRM_FRAMEBUFFER or _DRM_REGISTERS.
63 * It is assumed that each driver will have only one resource of
64 * each type.
65 */
66 if (!entry->map ||
67 map->type != entry->map->type ||
68 entry->master != dev->primary->master)
69 continue;
70 switch (map->type) {
71 case _DRM_SHM:
72 if (map->flags != _DRM_CONTAINS_LOCK)
73 break;
74 case _DRM_REGISTERS:
75 case _DRM_FRAME_BUFFER:
Dave Airlie89625eb2005-09-05 21:23:23 +100076 return entry;
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110077 default: /* Make gcc happy */
78 ;
Dave Airlie836cf042005-07-10 19:27:04 +100079 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +110080 if (entry->map->offset == map->offset)
81 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100082 }
83
84 return NULL;
85}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Dave Airliee0be4282007-07-12 10:26:44 +100087static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
David Millerf1a2a9b2009-02-18 15:41:02 -080088 unsigned long user_token, int hashed_handle, int shm)
Dave Airlied1f2b552005-08-05 22:11:22 +100089{
David Millerf1a2a9b2009-02-18 15:41:02 -080090 int use_hashed_handle, shift;
91 unsigned long add;
92
Dave Airliec2604ce2006-08-12 16:03:26 +100093#if (BITS_PER_LONG == 64)
Thomas Hellstrom8d153f72006-08-07 22:36:47 +100094 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
95#elif (BITS_PER_LONG == 32)
96 use_hashed_handle = hashed_handle;
97#else
98#error Unsupported long size. Neither 64 nor 32 bits.
99#endif
Dave Airlied1f2b552005-08-05 22:11:22 +1000100
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000101 if (!use_hashed_handle) {
102 int ret;
Thomas Hellstrom15450852007-02-08 16:14:05 +1100103 hash->key = user_token >> PAGE_SHIFT;
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000104 ret = drm_ht_insert_item(&dev->map_hash, hash);
105 if (ret != -EINVAL)
106 return ret;
Dave Airlied1f2b552005-08-05 22:11:22 +1000107 }
David Millerf1a2a9b2009-02-18 15:41:02 -0800108
109 shift = 0;
110 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
111 if (shm && (SHMLBA > PAGE_SIZE)) {
112 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
113
114 /* For shared memory, we have to preserve the SHMLBA
115 * bits of the eventual vma->vm_pgoff value during
116 * mmap(). Otherwise we run into cache aliasing problems
117 * on some platforms. On these platforms, the pgoff of
118 * a mmap() request is used to pick a suitable virtual
119 * address for the mmap() region such that it will not
120 * cause cache aliasing problems.
121 *
122 * Therefore, make sure the SHMLBA relevant bits of the
123 * hash value we use are equal to those in the original
124 * kernel virtual address.
125 */
126 shift = bits;
127 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
128 }
129
Thomas Hellstrome08870c2006-09-22 04:18:37 +1000130 return drm_ht_just_insert_please(&dev->map_hash, hash,
131 user_token, 32 - PAGE_SHIFT - 3,
David Millerf1a2a9b2009-02-18 15:41:02 -0800132 shift, add);
Dave Airlied1f2b552005-08-05 22:11:22 +1000133}
Dave Airlie9a186642005-06-23 21:29:18 +1000134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100136 * Core function to create a range of memory available for mapping by a
137 * non-root process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *
139 * Adjusts the memory offset to its absolute value according to the mapping
140 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
141 * applicable and if supported by the kernel.
142 */
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100143static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000144 unsigned int size, enum drm_map_type type,
Dave Airlie55910512007-07-11 16:53:40 +1000145 enum drm_map_flags flags,
146 struct drm_map_list ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100148 struct drm_local_map *map;
Dave Airlie55910512007-07-11 16:53:40 +1000149 struct drm_map_list *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000150 drm_dma_handle_t *dmah;
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000151 unsigned long user_token;
152 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Eric Anholt9a298b22009-03-24 12:23:04 -0700154 map = kmalloc(sizeof(*map), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000155 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -ENOMEM;
157
Dave Airlie7ab98402005-07-10 16:56:52 +1000158 map->offset = offset;
159 map->size = size;
160 map->flags = flags;
161 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /* Only allow shared memory to be removable since we only keep enough
164 * book keeping information about shared memory to allow for removal
165 * when processes fork.
166 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000167 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700168 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -EINVAL;
170 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100171 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
172 (unsigned long long)map->offset, map->size, map->type);
Benjamin Herrenschmidtb6741372009-05-18 11:56:16 +1000173
174 /* page-align _DRM_SHM maps. They are allocated here so there is no security
175 * hole created by that and it works around various broken drivers that use
176 * a non-aligned quantity to map the SAREA. --BenH
177 */
178 if (map->type == _DRM_SHM)
179 map->size = PAGE_ALIGN(map->size);
180
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100181 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700182 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -EINVAL;
184 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 map->handle = NULL;
187
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000188 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 case _DRM_REGISTERS:
190 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000191#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100192 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 map->offset < virt_to_phys(high_memory)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700194 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EINVAL;
196 }
197#endif
198#ifdef __alpha__
199 map->offset += dev->hose->mem_space->start;
200#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000201 /* Some drivers preinitialize some maps, without the X Server
202 * needing to be aware of it. Therefore, we just return success
203 * when the server tries to create a duplicate map.
204 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000205 list = drm_find_matching_map(dev, map);
206 if (list != NULL) {
207 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000208 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 "mismatched sizes, (%ld vs %ld)\n",
210 map->type, map->size,
211 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000212 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000213 }
214
Eric Anholt9a298b22009-03-24 12:23:04 -0700215 kfree(map);
Dave Airlie89625eb2005-09-05 21:23:23 +1000216 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000217 return 0;
218 }
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000221 if (map->type == _DRM_FRAME_BUFFER ||
222 (map->flags & _DRM_WRITE_COMBINING)) {
223 map->mtrr = mtrr_add(map->offset, map->size,
224 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226 }
Scott Thompson0769d392007-08-25 18:17:49 +1000227 if (map->type == _DRM_REGISTERS) {
Christoph Hellwig004a7722007-01-08 21:56:59 +1100228 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000229 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700230 kfree(map);
Scott Thompson0769d392007-08-25 18:17:49 +1000231 return -ENOMEM;
232 }
233 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100237 list = drm_find_matching_map(dev, map);
238 if (list != NULL) {
239 if(list->map->size != map->size) {
240 DRM_DEBUG("Matching maps of type %d with "
241 "mismatched sizes, (%ld vs %ld)\n",
242 map->type, map->size, list->map->size);
243 list->map->size = map->size;
244 }
245
Eric Anholt9a298b22009-03-24 12:23:04 -0700246 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100247 *maplist = list;
248 return 0;
249 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100250 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000251 DRM_DEBUG("%lu %d %p\n",
252 map->size, drm_order(map->size), map->handle);
253 if (!map->handle) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700254 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -ENOMEM;
256 }
257 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000260 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 vfree(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700262 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -EBUSY;
264 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000265 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100268 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000269 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100270 int valid = 0;
271
272 if (!drm_core_has_AGP(dev)) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700273 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100274 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100276#ifdef __alpha__
277 map->offset += dev->hose->mem_space->start;
278#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000279 /* In some cases (i810 driver), user space may have already
280 * added the AGP base itself, because dev->agp->base previously
281 * only got set during AGP enable. So, only add the base
282 * address if the map's offset isn't already within the
283 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100284 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000285 if (map->offset < dev->agp->base ||
286 map->offset > dev->agp->base +
287 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
288 map->offset += dev->agp->base;
289 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100290 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
291
292 /* This assumes the DRM is in total control of AGP space.
293 * It's not always the case as AGP can be in the control
294 * of user space (i.e. i810 driver). So this loop will get
295 * skipped and we double check that dev->agp->memory is
296 * actually set as well as being invalid before EPERM'ing
297 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000298 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100299 if ((map->offset >= entry->bound) &&
300 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
301 valid = 1;
302 break;
303 }
304 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000305 if (!list_empty(&dev->agp->memory) && !valid) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700306 kfree(map);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100307 return -EPERM;
308 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100309 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
310 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100313 }
Pekka Paalanen812c3692009-09-17 22:59:54 +0300314 case _DRM_GEM:
315 DRM_ERROR("tried to addmap GEM object\n");
316 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 case _DRM_SCATTER_GATHER:
318 if (!dev->sg) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700319 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return -EINVAL;
321 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000322 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000325 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000326 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000327 * casting it down to 32 bits is no problem, but we
328 * need to point to a 64bit variable first. */
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800329 dmah = drm_pci_alloc(dev, map->size, map->size);
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000330 if (!dmah) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700331 kfree(map);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000332 return -ENOMEM;
333 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000334 map->handle = dmah->vaddr;
335 map->offset = (unsigned long)dmah->busaddr;
336 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000337 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 default:
Eric Anholt9a298b22009-03-24 12:23:04 -0700339 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return -EINVAL;
341 }
342
Eric Anholt9a298b22009-03-24 12:23:04 -0700343 list = kmalloc(sizeof(*list), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000344 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700345 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100346 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700347 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return -EINVAL;
349 }
350 memset(list, 0, sizeof(*list));
351 list->map = map;
352
Dave Airlie30e2fb12006-02-02 19:37:46 +1100353 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000354 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000355
Dave Airlied1f2b552005-08-05 22:11:22 +1000356 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100357 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000358 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
359 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800360 ret = drm_map_handle(dev, &list->hash, user_token, 0,
361 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000362 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700363 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100364 iounmap(map->handle);
Eric Anholt9a298b22009-03-24 12:23:04 -0700365 kfree(map);
366 kfree(list);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000367 mutex_unlock(&dev->struct_mutex);
368 return ret;
369 }
370
Thomas Hellstrom15450852007-02-08 16:14:05 +1100371 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100372 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Ben Skeggs2ff2e8a2009-05-26 10:35:52 +1000374 if (!(map->flags & _DRM_DRIVER))
375 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000376 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000377 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100378 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000379
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100380int drm_addmap(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000381 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100382 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000383{
Dave Airlie55910512007-07-11 16:53:40 +1000384 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000385 int rc;
386
387 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
388 if (!rc)
389 *map_ptr = list->map;
390 return rc;
391}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000392
Dave Airlie7ab98402005-07-10 16:56:52 +1000393EXPORT_SYMBOL(drm_addmap);
394
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100395/**
396 * Ioctl to specify a range of memory that is available for mapping by a
397 * non-root process.
398 *
399 * \param inode device inode.
400 * \param file_priv DRM file private.
401 * \param cmd command.
402 * \param arg pointer to a drm_map structure.
403 * \return zero on success or a negative value on error.
404 *
405 */
Eric Anholtc153f452007-09-03 12:06:45 +1000406int drm_addmap_ioctl(struct drm_device *dev, void *data,
407 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000408{
Eric Anholtc153f452007-09-03 12:06:45 +1000409 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000410 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000411 int err;
412
Dave Airlie7c1c2872008-11-28 14:22:24 +1000413 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100414 return -EPERM;
415
Eric Anholtc153f452007-09-03 12:06:45 +1000416 err = drm_addmap_core(dev, map->offset, map->size, map->type,
417 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000418
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000419 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000420 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000421
Dave Airlie67e1a012005-10-24 18:41:39 +1000422 /* 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 +1000423 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000425}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/**
428 * Remove a map private from list and deallocate resources if the mapping
429 * isn't in use.
430 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * Searches the map on drm_device::maplist, removes it from the list, see if
432 * its being used, and free any associate resource (such as MTRR's) if it's not
433 * being on use.
434 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000435 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100437int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Dave Airlie55910512007-07-11 16:53:40 +1000439 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000440 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000441 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000442 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Dave Airlie836cf042005-07-10 19:27:04 +1000444 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000445 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000446 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000447 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000448 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100449 drm_ht_remove_key(&dev->map_hash,
450 r_list->user_token >> PAGE_SHIFT);
Eric Anholt9a298b22009-03-24 12:23:04 -0700451 kfree(r_list);
Dave Airliebd1b3312007-05-26 05:01:51 +1000452 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000453 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Dave Airlie836cf042005-07-10 19:27:04 +1000456
Dave Airliebd1b3312007-05-26 05:01:51 +1000457 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000458 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000459
460 switch (map->type) {
461 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100462 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000463 /* FALLTHROUGH */
464 case _DRM_FRAME_BUFFER:
465 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
466 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467 retcode = mtrr_del(map->mtrr, map->offset, map->size);
468 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000469 }
470 break;
471 case _DRM_SHM:
472 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000473 if (master) {
474 if (dev->sigdata.lock == master->lock.hw_lock)
475 dev->sigdata.lock = NULL;
476 master->lock.hw_lock = NULL; /* SHM removed */
477 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100478 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000479 }
Dave Airlie836cf042005-07-10 19:27:04 +1000480 break;
481 case _DRM_AGP:
482 case _DRM_SCATTER_GATHER:
483 break;
484 case _DRM_CONSISTENT:
485 dmah.vaddr = map->handle;
486 dmah.busaddr = map->offset;
487 dmah.size = map->size;
488 __drm_pci_free(dev, &dmah);
489 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800490 case _DRM_GEM:
491 DRM_ERROR("tried to rmmap GEM object\n");
492 break;
Dave Airlie836cf042005-07-10 19:27:04 +1000493 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700494 kfree(map);
Dave Airlie836cf042005-07-10 19:27:04 +1000495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0;
497}
Dave Airlie4e74f362008-12-19 10:23:14 +1100498EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000499
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100500int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000501{
502 int ret;
503
Dave Airlie30e2fb12006-02-02 19:37:46 +1100504 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000505 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100506 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000507
508 return ret;
509}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000510EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000511
Dave Airlie836cf042005-07-10 19:27:04 +1000512/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
513 * the last close of the device, and this is necessary for cleanup when things
514 * exit uncleanly. Therefore, having userland manually remove mappings seems
515 * like a pointless exercise since they're going away anyway.
516 *
517 * One use case might be after addmap is allowed for normal users for SHM and
518 * gets used by drivers that the server doesn't need to care about. This seems
519 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100520 *
521 * \param inode device inode.
522 * \param file_priv DRM file private.
523 * \param cmd command.
524 * \param arg pointer to a struct drm_map structure.
525 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000526 */
Eric Anholtc153f452007-09-03 12:06:45 +1000527int drm_rmmap_ioctl(struct drm_device *dev, void *data,
528 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000529{
Eric Anholtc153f452007-09-03 12:06:45 +1000530 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100531 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000532 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000533 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000534
Dave Airlie30e2fb12006-02-02 19:37:46 +1100535 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000536 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000537 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000538 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000539 r_list->map->flags & _DRM_REMOVABLE) {
540 map = r_list->map;
541 break;
542 }
543 }
544
545 /* List has wrapped around to the head pointer, or its empty we didn't
546 * find anything.
547 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000548 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100549 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000550 return -EINVAL;
551 }
552
Dave Airlie836cf042005-07-10 19:27:04 +1000553 /* Register and framebuffer maps are permanent */
554 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100555 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000556 return 0;
557 }
558
559 ret = drm_rmmap_locked(dev, map);
560
Dave Airlie30e2fb12006-02-02 19:37:46 +1100561 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000562
563 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000564}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566/**
567 * Cleanup after an error on one of the addbufs() functions.
568 *
Dave Airlie836cf042005-07-10 19:27:04 +1000569 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 * \param entry buffer entry where the error occurred.
571 *
572 * Frees any pages and buffers associated with the given entry.
573 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000574static void drm_cleanup_buf_error(struct drm_device * dev,
575 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 int i;
578
579 if (entry->seg_count) {
580 for (i = 0; i < entry->seg_count; i++) {
581 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100582 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700585 kfree(entry->seglist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 entry->seg_count = 0;
588 }
589
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000590 if (entry->buf_count) {
591 for (i = 0; i < entry->buf_count; i++) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700592 kfree(entry->buflist[i].dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Eric Anholt9a298b22009-03-24 12:23:04 -0700594 kfree(entry->buflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 entry->buf_count = 0;
597 }
598}
599
600#if __OS_HAS_AGP
601/**
Dave Airlied59431b2005-07-10 15:00:06 +1000602 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000604 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000605 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000607 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * After some sanity checks creates a drm_buf structure for each buffer and
609 * reallocates the buffer list of the same size order to accommodate the new
610 * buffers.
611 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000612int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Dave Airliecdd55a22007-07-11 16:32:08 +1000614 struct drm_device_dma *dma = dev->dma;
615 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000616 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000617 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 unsigned long offset;
619 unsigned long agp_offset;
620 int count;
621 int order;
622 int size;
623 int alignment;
624 int page_order;
625 int total;
626 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100627 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000628 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000630 if (!dma)
631 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dave Airlied59431b2005-07-10 15:00:06 +1000633 count = request->count;
634 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 size = 1 << order;
636
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 alignment = (request->flags & _DRM_PAGE_ALIGN)
638 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
640 total = PAGE_SIZE << page_order;
641
642 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000643 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000645 DRM_DEBUG("count: %d\n", count);
646 DRM_DEBUG("order: %d\n", order);
647 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100648 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000649 DRM_DEBUG("alignment: %d\n", alignment);
650 DRM_DEBUG("page_order: %d\n", page_order);
651 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
654 return -EINVAL;
655 if (dev->queue_count)
656 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Dave Airlie54ba2f72007-02-10 12:07:47 +1100658 /* Make sure buffers are located in AGP memory that we own */
659 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000660 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100661 if ((agp_offset >= agp_entry->bound) &&
662 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
663 valid = 1;
664 break;
665 }
666 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000667 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100668 DRM_DEBUG("zone invalid\n");
669 return -EINVAL;
670 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000671 spin_lock(&dev->count_lock);
672 if (dev->buf_use) {
673 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -EBUSY;
675 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000676 atomic_inc(&dev->buf_alloc);
677 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Dave Airlie30e2fb12006-02-02 19:37:46 +1100679 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000681 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100682 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 atomic_dec(&dev->buf_alloc);
684 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100688 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000689 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -EINVAL;
691 }
692
Eric Anholt9a298b22009-03-24 12:23:04 -0700693 entry->buflist = kmalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100695 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000696 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return -ENOMEM;
698 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 entry->buf_size = size;
702 entry->page_order = page_order;
703
704 offset = 0;
705
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 while (entry->buf_count < count) {
707 buf = &entry->buflist[entry->buf_count];
708 buf->idx = dma->buf_count + entry->buf_count;
709 buf->total = alignment;
710 buf->order = order;
711 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000713 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 buf->bus_address = agp_offset + offset;
715 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 buf->waiting = 0;
718 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000720 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -0700723 buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* Set count correctly so we free the proper amount. */
726 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000727 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100728 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -ENOMEM;
731 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 offset += alignment;
737 entry->buf_count++;
738 byte_count += PAGE_SIZE << page_order;
739 }
740
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000741 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Eric Anholt9a298b22009-03-24 12:23:04 -0700743 temp_buflist = krealloc(dma->buflist,
744 (dma->buf_count + entry->buf_count) *
745 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000748 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100749 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return -ENOMEM;
752 }
753 dma->buflist = temp_buflist;
754
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000755 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
757 }
758
759 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100760 dma->seg_count += entry->seg_count;
761 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 dma->byte_count += byte_count;
763
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000764 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
765 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Dave Airlie30e2fb12006-02-02 19:37:46 +1100767 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Dave Airlied59431b2005-07-10 15:00:06 +1000769 request->count = entry->buf_count;
770 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 dma->flags = _DRM_DMA_USE_AGP;
773
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return 0;
776}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000777EXPORT_SYMBOL(drm_addbufs_agp);
778#endif /* __OS_HAS_AGP */
779
Dave Airlie84b1fd12007-07-11 15:53:27 +1000780int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Dave Airliecdd55a22007-07-11 16:32:08 +1000782 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int count;
784 int order;
785 int size;
786 int total;
787 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000788 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100789 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000790 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int alignment;
792 unsigned long offset;
793 int i;
794 int byte_count;
795 int page_count;
796 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000797 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
800 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100801
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 if (!dma)
803 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Dave Airlied985c102006-01-02 21:32:48 +1100805 if (!capable(CAP_SYS_ADMIN))
806 return -EPERM;
807
Dave Airlied59431b2005-07-10 15:00:06 +1000808 count = request->count;
809 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 size = 1 << order;
811
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
813 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
816 return -EINVAL;
817 if (dev->queue_count)
818 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Dave Airlied59431b2005-07-10 15:00:06 +1000820 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
823 total = PAGE_SIZE << page_order;
824
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 spin_lock(&dev->count_lock);
826 if (dev->buf_use) {
827 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -EBUSY;
829 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 atomic_inc(&dev->buf_alloc);
831 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Dave Airlie30e2fb12006-02-02 19:37:46 +1100833 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000835 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100836 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return -ENOMEM; /* May only call once for each order */
839 }
840
841 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100842 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EINVAL;
845 }
846
Eric Anholt9a298b22009-03-24 12:23:04 -0700847 entry->buflist = kmalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100849 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000850 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -ENOMEM;
852 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000853 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Eric Anholt9a298b22009-03-24 12:23:04 -0700855 entry->seglist = kmalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 if (!entry->seglist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700857 kfree(entry->buflist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100858 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return -ENOMEM;
861 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* Keep the original pagelist until we know all the allocations
865 * have succeeded
866 */
Eric Anholt9a298b22009-03-24 12:23:04 -0700867 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
868 sizeof(*dma->pagelist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (!temp_pagelist) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700870 kfree(entry->buflist);
871 kfree(entry->seglist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100872 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000873 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return -ENOMEM;
875 }
876 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
878 DRM_DEBUG("pagelist: %d entries\n",
879 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000881 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 entry->page_order = page_order;
883 byte_count = 0;
884 page_count = 0;
885
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000887
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800888 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
Dave Airliebc5f4522007-11-05 12:50:58 +1000889
Dave Airlieddf19b92006-03-19 18:56:12 +1100890 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* Set count correctly so we free the proper amount. */
892 entry->buf_count = count;
893 entry->seg_count = count;
894 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700895 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100896 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return -ENOMEM;
899 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100900 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 for (i = 0; i < (1 << page_order); i++) {
902 DRM_DEBUG("page %d @ 0x%08lx\n",
903 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100904 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100906 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 for (offset = 0;
909 offset + size <= total && entry->buf_count < count;
910 offset += alignment, ++entry->buf_count) {
911 buf = &entry->buflist[entry->buf_count];
912 buf->idx = dma->buf_count + entry->buf_count;
913 buf->total = alignment;
914 buf->order = order;
915 buf->used = 0;
916 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100917 buf->address = (void *)(dmah->vaddr + offset);
918 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 buf->waiting = 0;
921 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000922 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000923 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -0700926 buf->dev_private = kmalloc(buf->dev_priv_size,
927 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* Set count correctly so we free the proper amount. */
930 entry->buf_count = count;
931 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000932 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700933 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100934 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000935 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return -ENOMEM;
937 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000940 DRM_DEBUG("buffer %d @ %p\n",
941 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 byte_count += PAGE_SIZE << page_order;
944 }
945
Eric Anholt9a298b22009-03-24 12:23:04 -0700946 temp_buflist = krealloc(dma->buflist,
947 (dma->buf_count + entry->buf_count) *
948 sizeof(*dma->buflist), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!temp_buflist) {
950 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 drm_cleanup_buf_error(dev, entry);
Eric Anholt9a298b22009-03-24 12:23:04 -0700952 kfree(temp_pagelist);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100953 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000954 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -ENOMEM;
956 }
957 dma->buflist = temp_buflist;
958
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000959 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
961 }
962
963 /* No allocations failed, so now we can replace the orginal pagelist
964 * with the new one.
965 */
966 if (dma->page_count) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700967 kfree(dma->pagelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 dma->pagelist = temp_pagelist;
970
971 dma->buf_count += entry->buf_count;
972 dma->seg_count += entry->seg_count;
973 dma->page_count += entry->seg_count << page_order;
974 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
975
Dave Airlie30e2fb12006-02-02 19:37:46 +1100976 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Dave Airlied59431b2005-07-10 15:00:06 +1000978 request->count = entry->buf_count;
979 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
George Sapountzis3417f332006-10-24 12:03:04 -0700981 if (request->flags & _DRM_PCI_BUFFER_RO)
982 dma->flags = _DRM_DMA_USE_PCI_RO;
983
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000984 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return 0;
986
987}
Dave Airlied84f76d2005-07-10 17:04:22 +1000988EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Dave Airlie84b1fd12007-07-11 15:53:27 +1000990static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Dave Airliecdd55a22007-07-11 16:32:08 +1000992 struct drm_device_dma *dma = dev->dma;
993 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000994 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 unsigned long offset;
996 unsigned long agp_offset;
997 int count;
998 int order;
999 int size;
1000 int alignment;
1001 int page_order;
1002 int total;
1003 int byte_count;
1004 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001005 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 if (!drm_core_check_feature(dev, DRIVER_SG))
1008 return -EINVAL;
1009
1010 if (!dma)
1011 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Dave Airlied985c102006-01-02 21:32:48 +11001013 if (!capable(CAP_SYS_ADMIN))
1014 return -EPERM;
1015
Dave Airlied59431b2005-07-10 15:00:06 +10001016 count = request->count;
1017 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 size = 1 << order;
1019
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001020 alignment = (request->flags & _DRM_PAGE_ALIGN)
1021 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1023 total = PAGE_SIZE << page_order;
1024
1025 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001026 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 DRM_DEBUG("count: %d\n", count);
1029 DRM_DEBUG("order: %d\n", order);
1030 DRM_DEBUG("size: %d\n", size);
1031 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1032 DRM_DEBUG("alignment: %d\n", alignment);
1033 DRM_DEBUG("page_order: %d\n", page_order);
1034 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1037 return -EINVAL;
1038 if (dev->queue_count)
1039 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001041 spin_lock(&dev->count_lock);
1042 if (dev->buf_use) {
1043 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return -EBUSY;
1045 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001046 atomic_inc(&dev->buf_alloc);
1047 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Dave Airlie30e2fb12006-02-02 19:37:46 +11001049 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001051 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001052 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 atomic_dec(&dev->buf_alloc);
1054 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056
1057 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001058 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return -EINVAL;
1061 }
1062
Eric Anholt9a298b22009-03-24 12:23:04 -07001063 entry->buflist = kmalloc(count * sizeof(*entry->buflist),
1064 GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001065 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001066 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001067 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return -ENOMEM;
1069 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 entry->buf_size = size;
1073 entry->page_order = page_order;
1074
1075 offset = 0;
1076
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001077 while (entry->buf_count < count) {
1078 buf = &entry->buflist[entry->buf_count];
1079 buf->idx = dma->buf_count + entry->buf_count;
1080 buf->total = alignment;
1081 buf->order = order;
1082 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001084 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001086 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001087 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001088 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 buf->waiting = 0;
1090 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001091 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001092 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -07001095 buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001096 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 /* Set count correctly so we free the proper amount. */
1098 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001100 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return -ENOMEM;
1103 }
1104
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001107 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 offset += alignment;
1110 entry->buf_count++;
1111 byte_count += PAGE_SIZE << page_order;
1112 }
1113
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001114 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Eric Anholt9a298b22009-03-24 12:23:04 -07001116 temp_buflist = krealloc(dma->buflist,
1117 (dma->buf_count + entry->buf_count) *
1118 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001119 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001121 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001122 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return -ENOMEM;
1125 }
1126 dma->buflist = temp_buflist;
1127
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001128 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1130 }
1131
1132 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001133 dma->seg_count += entry->seg_count;
1134 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 dma->byte_count += byte_count;
1136
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001137 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1138 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Dave Airlie30e2fb12006-02-02 19:37:46 +11001140 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Dave Airlied59431b2005-07-10 15:00:06 +10001142 request->count = entry->buf_count;
1143 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 dma->flags = _DRM_DMA_USE_SG;
1146
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001147 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return 0;
1149}
1150
Dave Airlie84b1fd12007-07-11 15:53:27 +10001151static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001152{
Dave Airliecdd55a22007-07-11 16:32:08 +10001153 struct drm_device_dma *dma = dev->dma;
1154 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001155 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001156 unsigned long offset;
1157 unsigned long agp_offset;
1158 int count;
1159 int order;
1160 int size;
1161 int alignment;
1162 int page_order;
1163 int total;
1164 int byte_count;
1165 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001166 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001167
1168 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1169 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001170
Dave Airlieb84397d62005-07-10 14:46:12 +10001171 if (!dma)
1172 return -EINVAL;
1173
Dave Airlied985c102006-01-02 21:32:48 +11001174 if (!capable(CAP_SYS_ADMIN))
1175 return -EPERM;
1176
Dave Airlied59431b2005-07-10 15:00:06 +10001177 count = request->count;
1178 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001179 size = 1 << order;
1180
Dave Airlied59431b2005-07-10 15:00:06 +10001181 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001182 ? PAGE_ALIGN(size) : size;
1183 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1184 total = PAGE_SIZE << page_order;
1185
1186 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001187 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001188
1189 DRM_DEBUG("count: %d\n", count);
1190 DRM_DEBUG("order: %d\n", order);
1191 DRM_DEBUG("size: %d\n", size);
1192 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1193 DRM_DEBUG("alignment: %d\n", alignment);
1194 DRM_DEBUG("page_order: %d\n", page_order);
1195 DRM_DEBUG("total: %d\n", total);
1196
1197 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1198 return -EINVAL;
1199 if (dev->queue_count)
1200 return -EBUSY; /* Not while in use */
1201
1202 spin_lock(&dev->count_lock);
1203 if (dev->buf_use) {
1204 spin_unlock(&dev->count_lock);
1205 return -EBUSY;
1206 }
1207 atomic_inc(&dev->buf_alloc);
1208 spin_unlock(&dev->count_lock);
1209
Dave Airlie30e2fb12006-02-02 19:37:46 +11001210 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001211 entry = &dma->bufs[order];
1212 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001213 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001214 atomic_dec(&dev->buf_alloc);
1215 return -ENOMEM; /* May only call once for each order */
1216 }
1217
1218 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001219 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001220 atomic_dec(&dev->buf_alloc);
1221 return -EINVAL;
1222 }
1223
Eric Anholt9a298b22009-03-24 12:23:04 -07001224 entry->buflist = kmalloc(count * sizeof(*entry->buflist),
1225 GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001226 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001227 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001228 atomic_dec(&dev->buf_alloc);
1229 return -ENOMEM;
1230 }
1231 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1232
1233 entry->buf_size = size;
1234 entry->page_order = page_order;
1235
1236 offset = 0;
1237
1238 while (entry->buf_count < count) {
1239 buf = &entry->buflist[entry->buf_count];
1240 buf->idx = dma->buf_count + entry->buf_count;
1241 buf->total = alignment;
1242 buf->order = order;
1243 buf->used = 0;
1244
1245 buf->offset = (dma->byte_count + offset);
1246 buf->bus_address = agp_offset + offset;
1247 buf->address = (void *)(agp_offset + offset);
1248 buf->next = NULL;
1249 buf->waiting = 0;
1250 buf->pending = 0;
1251 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001252 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001253
1254 buf->dev_priv_size = dev->driver->dev_priv_size;
Eric Anholt9a298b22009-03-24 12:23:04 -07001255 buf->dev_private = kmalloc(buf->dev_priv_size, GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001256 if (!buf->dev_private) {
1257 /* Set count correctly so we free the proper amount. */
1258 entry->buf_count = count;
1259 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001260 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001261 atomic_dec(&dev->buf_alloc);
1262 return -ENOMEM;
1263 }
1264 memset(buf->dev_private, 0, buf->dev_priv_size);
1265
1266 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1267
1268 offset += alignment;
1269 entry->buf_count++;
1270 byte_count += PAGE_SIZE << page_order;
1271 }
1272
1273 DRM_DEBUG("byte_count: %d\n", byte_count);
1274
Eric Anholt9a298b22009-03-24 12:23:04 -07001275 temp_buflist = krealloc(dma->buflist,
1276 (dma->buf_count + entry->buf_count) *
1277 sizeof(*dma->buflist), GFP_KERNEL);
Dave Airlieb84397d62005-07-10 14:46:12 +10001278 if (!temp_buflist) {
1279 /* Free the entry because it isn't valid */
1280 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001281 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001282 atomic_dec(&dev->buf_alloc);
1283 return -ENOMEM;
1284 }
1285 dma->buflist = temp_buflist;
1286
1287 for (i = 0; i < entry->buf_count; i++) {
1288 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1289 }
1290
1291 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001292 dma->seg_count += entry->seg_count;
1293 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001294 dma->byte_count += byte_count;
1295
1296 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1297 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1298
Dave Airlie30e2fb12006-02-02 19:37:46 +11001299 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001300
Dave Airlied59431b2005-07-10 15:00:06 +10001301 request->count = entry->buf_count;
1302 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001303
1304 dma->flags = _DRM_DMA_USE_FB;
1305
1306 atomic_dec(&dev->buf_alloc);
1307 return 0;
1308}
Dave Airlied985c102006-01-02 21:32:48 +11001309
Dave Airlieb84397d62005-07-10 14:46:12 +10001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/**
1312 * Add buffers for DMA transfers (ioctl).
1313 *
1314 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001315 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001317 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * \return zero on success or a negative number on failure.
1319 *
1320 * According with the memory type specified in drm_buf_desc::flags and the
1321 * build options, it dispatches the call either to addbufs_agp(),
1322 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1323 * PCI memory respectively.
1324 */
Eric Anholtc153f452007-09-03 12:06:45 +10001325int drm_addbufs(struct drm_device *dev, void *data,
1326 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Eric Anholtc153f452007-09-03 12:06:45 +10001328 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001329 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1332 return -EINVAL;
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001335 if (request->flags & _DRM_AGP_BUFFER)
1336 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 else
1338#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001339 if (request->flags & _DRM_SG_BUFFER)
1340 ret = drm_addbufs_sg(dev, request);
1341 else if (request->flags & _DRM_FB_BUFFER)
1342 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 else
Eric Anholtc153f452007-09-03 12:06:45 +10001344 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001345
Dave Airlied59431b2005-07-10 15:00:06 +10001346 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349/**
1350 * Get information about the buffer mappings.
1351 *
1352 * This was originally mean for debugging purposes, or by a sophisticated
1353 * client library to determine how best to use the available buffers (e.g.,
1354 * large buffers can be used for image transfer).
1355 *
1356 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001357 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * \param cmd command.
1359 * \param arg pointer to a drm_buf_info structure.
1360 * \return zero on success or a negative number on failure.
1361 *
1362 * Increments drm_device::buf_use while holding the drm_device::count_lock
1363 * lock, preventing of allocating more buffers after this call. Information
1364 * about each requested buffer is then copied into user space.
1365 */
Eric Anholtc153f452007-09-03 12:06:45 +10001366int drm_infobufs(struct drm_device *dev, void *data,
1367 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Dave Airliecdd55a22007-07-11 16:32:08 +10001369 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001370 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 int i;
1372 int count;
1373
1374 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1375 return -EINVAL;
1376
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001377 if (!dma)
1378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001380 spin_lock(&dev->count_lock);
1381 if (atomic_read(&dev->buf_alloc)) {
1382 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return -EBUSY;
1384 }
1385 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001386 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001388 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1389 if (dma->bufs[i].buf_count)
1390 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001393 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Eric Anholtc153f452007-09-03 12:06:45 +10001395 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001396 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1397 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001398 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001399 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001400 struct drm_buf_entry *from = &dma->bufs[i];
1401 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001402 if (copy_to_user(&to->count,
1403 &from->buf_count,
1404 sizeof(from->buf_count)) ||
1405 copy_to_user(&to->size,
1406 &from->buf_size,
1407 sizeof(from->buf_size)) ||
1408 copy_to_user(&to->low_mark,
1409 &list->low_mark,
1410 sizeof(list->low_mark)) ||
1411 copy_to_user(&to->high_mark,
1412 &list->high_mark,
1413 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EFAULT;
1415
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001416 DRM_DEBUG("%d %d %d %d %d\n",
1417 i,
1418 dma->bufs[i].buf_count,
1419 dma->bufs[i].buf_size,
1420 dma->bufs[i].freelist.low_mark,
1421 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 ++count;
1423 }
1424 }
1425 }
Eric Anholtc153f452007-09-03 12:06:45 +10001426 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 return 0;
1429}
1430
1431/**
1432 * Specifies a low and high water mark for buffer allocation
1433 *
1434 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001435 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 * \param cmd command.
1437 * \param arg a pointer to a drm_buf_desc structure.
1438 * \return zero on success or a negative number on failure.
1439 *
1440 * Verifies that the size order is bounded between the admissible orders and
1441 * updates the respective drm_device_dma::bufs entry low and high water mark.
1442 *
1443 * \note This ioctl is deprecated and mostly never used.
1444 */
Eric Anholtc153f452007-09-03 12:06:45 +10001445int drm_markbufs(struct drm_device *dev, void *data,
1446 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Dave Airliecdd55a22007-07-11 16:32:08 +10001448 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001449 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001451 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1454 return -EINVAL;
1455
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001456 if (!dma)
1457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001459 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001460 request->size, request->low_mark, request->high_mark);
1461 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001462 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1463 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 entry = &dma->bufs[order];
1465
Eric Anholtc153f452007-09-03 12:06:45 +10001466 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001468 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -EINVAL;
1470
Eric Anholtc153f452007-09-03 12:06:45 +10001471 entry->freelist.low_mark = request->low_mark;
1472 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 return 0;
1475}
1476
1477/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001478 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 *
1480 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001481 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 * \param cmd command.
1483 * \param arg pointer to a drm_buf_free structure.
1484 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001485 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 * Calls free_buffer() for each used buffer.
1487 * This function is primarily used for debugging.
1488 */
Eric Anholtc153f452007-09-03 12:06:45 +10001489int drm_freebufs(struct drm_device *dev, void *data,
1490 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Dave Airliecdd55a22007-07-11 16:32:08 +10001492 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001493 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 int i;
1495 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001496 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1499 return -EINVAL;
1500
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001501 if (!dma)
1502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Eric Anholtc153f452007-09-03 12:06:45 +10001504 DRM_DEBUG("%d\n", request->count);
1505 for (i = 0; i < request->count; i++) {
1506 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001508 if (idx < 0 || idx >= dma->buf_count) {
1509 DRM_ERROR("Index %d (of %d max)\n",
1510 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EINVAL;
1512 }
1513 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001514 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001515 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001516 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return -EINVAL;
1518 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001519 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521
1522 return 0;
1523}
1524
1525/**
1526 * Maps all of the DMA buffers into client-virtual space (ioctl).
1527 *
1528 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001529 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 * \param cmd command.
1531 * \param arg pointer to a drm_buf_map structure.
1532 * \return zero on success or a negative number on failure.
1533 *
George Sapountzis3417f332006-10-24 12:03:04 -07001534 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1535 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1536 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1537 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 */
Eric Anholtc153f452007-09-03 12:06:45 +10001539int drm_mapbufs(struct drm_device *dev, void *data,
1540 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
Dave Airliecdd55a22007-07-11 16:32:08 +10001542 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 int retcode = 0;
1544 const int zero = 0;
1545 unsigned long virtual;
1546 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001547 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 int i;
1549
1550 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1551 return -EINVAL;
1552
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001553 if (!dma)
1554 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001556 spin_lock(&dev->count_lock);
1557 if (atomic_read(&dev->buf_alloc)) {
1558 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return -EBUSY;
1560 }
1561 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001562 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Eric Anholtc153f452007-09-03 12:06:45 +10001564 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001565 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001566 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001567 && (dma->flags & _DRM_DMA_USE_SG))
1568 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1569 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001570 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001571 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001573 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 retcode = -EINVAL;
1575 goto done;
1576 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001577 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001578 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001579 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001580 MAP_SHARED,
1581 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001582 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001584 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001585 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001586 PROT_READ | PROT_WRITE,
1587 MAP_SHARED, 0);
1588 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001590 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /* Real error */
1592 retcode = (signed long)virtual;
1593 goto done;
1594 }
Eric Anholtc153f452007-09-03 12:06:45 +10001595 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001597 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001598 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001599 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001600 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 retcode = -EFAULT;
1602 goto done;
1603 }
Eric Anholtc153f452007-09-03 12:06:45 +10001604 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001605 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001606 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 retcode = -EFAULT;
1608 goto done;
1609 }
Eric Anholtc153f452007-09-03 12:06:45 +10001610 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001611 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 retcode = -EFAULT;
1613 goto done;
1614 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001615 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001616 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001617 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 retcode = -EFAULT;
1619 goto done;
1620 }
1621 }
1622 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001623 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001624 request->count = dma->buf_count;
1625 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 return retcode;
1628}
1629
Dave Airlie836cf042005-07-10 19:27:04 +10001630/**
1631 * Compute size order. Returns the exponent of the smaller power of two which
1632 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001633 *
Dave Airlie836cf042005-07-10 19:27:04 +10001634 * \param size size.
1635 * \return order.
1636 *
1637 * \todo Can be made faster.
1638 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001639int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001640{
1641 int order;
1642 unsigned long tmp;
1643
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001644 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001645
1646 if (size & (size - 1))
1647 ++order;
1648
1649 return order;
1650}
1651EXPORT_SYMBOL(drm_order);