blob: 6d80d17f1e96f9b6d137cfd7ec553378620ca6e2 [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
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000154 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
155 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) {
168 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
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);
173 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return -EINVAL;
176 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000177 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 map->handle = NULL;
179
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000180 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 case _DRM_REGISTERS:
182 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000183#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100184 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 map->offset < virt_to_phys(high_memory)) {
186 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
188 }
189#endif
190#ifdef __alpha__
191 map->offset += dev->hose->mem_space->start;
192#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000193 /* Some drivers preinitialize some maps, without the X Server
194 * needing to be aware of it. Therefore, we just return success
195 * when the server tries to create a duplicate map.
196 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000197 list = drm_find_matching_map(dev, map);
198 if (list != NULL) {
199 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000200 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000201 "mismatched sizes, (%ld vs %ld)\n",
202 map->type, map->size,
203 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000204 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000205 }
206
207 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Dave Airlie89625eb2005-09-05 21:23:23 +1000208 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000209 return 0;
210 }
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 if (map->type == _DRM_FRAME_BUFFER ||
214 (map->flags & _DRM_WRITE_COMBINING)) {
215 map->mtrr = mtrr_add(map->offset, map->size,
216 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 }
Scott Thompson0769d392007-08-25 18:17:49 +1000219 if (map->type == _DRM_REGISTERS) {
Christoph Hellwig004a7722007-01-08 21:56:59 +1100220 map->handle = ioremap(map->offset, map->size);
Scott Thompson0769d392007-08-25 18:17:49 +1000221 if (!map->handle) {
222 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
223 return -ENOMEM;
224 }
225 }
Dave Airliebc5f4522007-11-05 12:50:58 +1000226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 case _DRM_SHM:
Dave Airlie54ba2f72007-02-10 12:07:47 +1100229 list = drm_find_matching_map(dev, map);
230 if (list != NULL) {
231 if(list->map->size != map->size) {
232 DRM_DEBUG("Matching maps of type %d with "
233 "mismatched sizes, (%ld vs %ld)\n",
234 map->type, map->size, list->map->size);
235 list->map->size = map->size;
236 }
237
238 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
239 *maplist = list;
240 return 0;
241 }
Thomas Hellstromf239b7b2007-01-08 21:22:50 +1100242 map->handle = vmalloc_user(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 DRM_DEBUG("%lu %d %p\n",
244 map->size, drm_order(map->size), map->handle);
245 if (!map->handle) {
246 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -ENOMEM;
248 }
249 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000250 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Prevent a 2nd X Server from creating a 2nd lock */
Dave Airlie7c1c2872008-11-28 14:22:24 +1000252 if (dev->primary->master->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 vfree(map->handle);
254 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EBUSY;
256 }
Dave Airlie7c1c2872008-11-28 14:22:24 +1000257 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100260 case _DRM_AGP: {
Dave Airlie55910512007-07-11 16:53:40 +1000261 struct drm_agp_mem *entry;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100262 int valid = 0;
263
264 if (!drm_core_has_AGP(dev)) {
265 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
266 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100268#ifdef __alpha__
269 map->offset += dev->hose->mem_space->start;
270#endif
Eric Anholt47a184a2007-11-22 16:55:15 +1000271 /* In some cases (i810 driver), user space may have already
272 * added the AGP base itself, because dev->agp->base previously
273 * only got set during AGP enable. So, only add the base
274 * address if the map's offset isn't already within the
275 * aperture.
Dave Airlie54ba2f72007-02-10 12:07:47 +1100276 */
Eric Anholt47a184a2007-11-22 16:55:15 +1000277 if (map->offset < dev->agp->base ||
278 map->offset > dev->agp->base +
279 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
280 map->offset += dev->agp->base;
281 }
Dave Airlie54ba2f72007-02-10 12:07:47 +1100282 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
283
284 /* This assumes the DRM is in total control of AGP space.
285 * It's not always the case as AGP can be in the control
286 * of user space (i.e. i810 driver). So this loop will get
287 * skipped and we double check that dev->agp->memory is
288 * actually set as well as being invalid before EPERM'ing
289 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000290 list_for_each_entry(entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100291 if ((map->offset >= entry->bound) &&
292 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
293 valid = 1;
294 break;
295 }
296 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000297 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100298 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
299 return -EPERM;
300 }
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100301 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
302 (unsigned long long)map->offset, map->size);
Dave Airlie54ba2f72007-02-10 12:07:47 +1100303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800305 case _DRM_GEM:
306 DRM_ERROR("tried to rmmap GEM object\n");
307 break;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 case _DRM_SCATTER_GATHER:
310 if (!dev->sg) {
311 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
312 return -EINVAL;
313 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000314 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000317 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000318 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000319 * casting it down to 32 bits is no problem, but we
320 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000321 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
322 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000323 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
324 return -ENOMEM;
325 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000326 map->handle = dmah->vaddr;
327 map->offset = (unsigned long)dmah->busaddr;
328 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -EINVAL;
333 }
334
335 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336 if (!list) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700337 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100338 iounmap(map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
340 return -EINVAL;
341 }
342 memset(list, 0, sizeof(*list));
343 list->map = map;
344
Dave Airlie30e2fb12006-02-02 19:37:46 +1100345 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000346 list_add(&list->head, &dev->maplist);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000347
Dave Airlied1f2b552005-08-05 22:11:22 +1000348 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100349 /* We do it here so that dev->struct_mutex protects the increment */
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000350 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
351 map->offset;
David Millerf1a2a9b2009-02-18 15:41:02 -0800352 ret = drm_map_handle(dev, &list->hash, user_token, 0,
353 (map->type == _DRM_SHM));
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000354 if (ret) {
Amol Lad85abb3f2006-10-25 09:55:34 -0700355 if (map->type == _DRM_REGISTERS)
Christoph Hellwig004a7722007-01-08 21:56:59 +1100356 iounmap(map->handle);
Thomas Hellstrom8d153f72006-08-07 22:36:47 +1000357 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
358 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
359 mutex_unlock(&dev->struct_mutex);
360 return ret;
361 }
362
Thomas Hellstrom15450852007-02-08 16:14:05 +1100363 list->user_token = list->hash.key << PAGE_SHIFT;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100364 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dave Airlie7c1c2872008-11-28 14:22:24 +1000366 list->master = dev->primary->master;
Dave Airlie89625eb2005-09-05 21:23:23 +1000367 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000368 return 0;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100369 }
Dave Airlie89625eb2005-09-05 21:23:23 +1000370
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100371int drm_addmap(struct drm_device * dev, resource_size_t offset,
Dave Airliec60ce622007-07-11 15:27:12 +1000372 unsigned int size, enum drm_map_type type,
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100373 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000374{
Dave Airlie55910512007-07-11 16:53:40 +1000375 struct drm_map_list *list;
Dave Airlie89625eb2005-09-05 21:23:23 +1000376 int rc;
377
378 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
379 if (!rc)
380 *map_ptr = list->map;
381 return rc;
382}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383
Dave Airlie7ab98402005-07-10 16:56:52 +1000384EXPORT_SYMBOL(drm_addmap);
385
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100386/**
387 * Ioctl to specify a range of memory that is available for mapping by a
388 * non-root process.
389 *
390 * \param inode device inode.
391 * \param file_priv DRM file private.
392 * \param cmd command.
393 * \param arg pointer to a drm_map structure.
394 * \return zero on success or a negative value on error.
395 *
396 */
Eric Anholtc153f452007-09-03 12:06:45 +1000397int drm_addmap_ioctl(struct drm_device *dev, void *data,
398 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000399{
Eric Anholtc153f452007-09-03 12:06:45 +1000400 struct drm_map *map = data;
Dave Airlie55910512007-07-11 16:53:40 +1000401 struct drm_map_list *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000402 int err;
403
Dave Airlie7c1c2872008-11-28 14:22:24 +1000404 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
Dave Airlied985c102006-01-02 21:32:48 +1100405 return -EPERM;
406
Eric Anholtc153f452007-09-03 12:06:45 +1000407 err = drm_addmap_core(dev, map->offset, map->size, map->type,
408 map->flags, &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000409
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000411 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000412
Dave Airlie67e1a012005-10-24 18:41:39 +1000413 /* 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 +1000414 map->handle = (void *)(unsigned long)maplist->user_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000416}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/**
419 * Remove a map private from list and deallocate resources if the mapping
420 * isn't in use.
421 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * Searches the map on drm_device::maplist, removes it from the list, see if
423 * its being used, and free any associate resource (such as MTRR's) if it's not
424 * being on use.
425 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000426 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100428int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Dave Airlie55910512007-07-11 16:53:40 +1000430 struct drm_map_list *r_list = NULL, *list_t;
Dave Airlie836cf042005-07-10 19:27:04 +1000431 drm_dma_handle_t dmah;
Dave Airliebd1b3312007-05-26 05:01:51 +1000432 int found = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000433 struct drm_master *master;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Dave Airlie836cf042005-07-10 19:27:04 +1000435 /* Find the list entry for the map and remove it */
Dave Airliebd1b3312007-05-26 05:01:51 +1000436 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000437 if (r_list->map == map) {
Dave Airlie7c1c2872008-11-28 14:22:24 +1000438 master = r_list->master;
Dave Airliebd1b3312007-05-26 05:01:51 +1000439 list_del(&r_list->head);
Thomas Hellstrom15450852007-02-08 16:14:05 +1100440 drm_ht_remove_key(&dev->map_hash,
441 r_list->user_token >> PAGE_SHIFT);
Dave Airliebd1b3312007-05-26 05:01:51 +1000442 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
443 found = 1;
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000444 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Dave Airlie836cf042005-07-10 19:27:04 +1000447
Dave Airliebd1b3312007-05-26 05:01:51 +1000448 if (!found)
Dave Airlie836cf042005-07-10 19:27:04 +1000449 return -EINVAL;
Dave Airlie836cf042005-07-10 19:27:04 +1000450
451 switch (map->type) {
452 case _DRM_REGISTERS:
Christoph Hellwig004a7722007-01-08 21:56:59 +1100453 iounmap(map->handle);
Dave Airlie836cf042005-07-10 19:27:04 +1000454 /* FALLTHROUGH */
455 case _DRM_FRAME_BUFFER:
456 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
457 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458 retcode = mtrr_del(map->mtrr, map->offset, map->size);
459 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000460 }
461 break;
462 case _DRM_SHM:
463 vfree(map->handle);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000464 if (master) {
465 if (dev->sigdata.lock == master->lock.hw_lock)
466 dev->sigdata.lock = NULL;
467 master->lock.hw_lock = NULL; /* SHM removed */
468 master->lock.file_priv = NULL;
Thomas Hellstrom171901d2009-03-02 11:10:55 +0100469 wake_up_interruptible_all(&master->lock.lock_queue);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000470 }
Dave Airlie836cf042005-07-10 19:27:04 +1000471 break;
472 case _DRM_AGP:
473 case _DRM_SCATTER_GATHER:
474 break;
475 case _DRM_CONSISTENT:
476 dmah.vaddr = map->handle;
477 dmah.busaddr = map->offset;
478 dmah.size = map->size;
479 __drm_pci_free(dev, &dmah);
480 break;
Jesse Barnesa2c0a972008-11-05 10:31:53 -0800481 case _DRM_GEM:
482 DRM_ERROR("tried to rmmap GEM object\n");
483 break;
Dave Airlie836cf042005-07-10 19:27:04 +1000484 }
485 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 0;
488}
Dave Airlie4e74f362008-12-19 10:23:14 +1100489EXPORT_SYMBOL(drm_rmmap_locked);
Dave Airlie836cf042005-07-10 19:27:04 +1000490
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100491int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000492{
493 int ret;
494
Dave Airlie30e2fb12006-02-02 19:37:46 +1100495 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000496 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100497 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000498
499 return ret;
500}
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000501EXPORT_SYMBOL(drm_rmmap);
Dave Airlie7ab98402005-07-10 16:56:52 +1000502
Dave Airlie836cf042005-07-10 19:27:04 +1000503/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
504 * the last close of the device, and this is necessary for cleanup when things
505 * exit uncleanly. Therefore, having userland manually remove mappings seems
506 * like a pointless exercise since they're going away anyway.
507 *
508 * One use case might be after addmap is allowed for normal users for SHM and
509 * gets used by drivers that the server doesn't need to care about. This seems
510 * unlikely.
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100511 *
512 * \param inode device inode.
513 * \param file_priv DRM file private.
514 * \param cmd command.
515 * \param arg pointer to a struct drm_map structure.
516 * \return zero on success or a negative value on error.
Dave Airlie836cf042005-07-10 19:27:04 +1000517 */
Eric Anholtc153f452007-09-03 12:06:45 +1000518int drm_rmmap_ioctl(struct drm_device *dev, void *data,
519 struct drm_file *file_priv)
Dave Airlie7ab98402005-07-10 16:56:52 +1000520{
Eric Anholtc153f452007-09-03 12:06:45 +1000521 struct drm_map *request = data;
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +1100522 struct drm_local_map *map = NULL;
Dave Airlie55910512007-07-11 16:53:40 +1000523 struct drm_map_list *r_list;
Dave Airlie836cf042005-07-10 19:27:04 +1000524 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000525
Dave Airlie30e2fb12006-02-02 19:37:46 +1100526 mutex_lock(&dev->struct_mutex);
Dave Airliebd1b3312007-05-26 05:01:51 +1000527 list_for_each_entry(r_list, &dev->maplist, head) {
Dave Airlie836cf042005-07-10 19:27:04 +1000528 if (r_list->map &&
Eric Anholtc153f452007-09-03 12:06:45 +1000529 r_list->user_token == (unsigned long)request->handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000530 r_list->map->flags & _DRM_REMOVABLE) {
531 map = r_list->map;
532 break;
533 }
534 }
535
536 /* List has wrapped around to the head pointer, or its empty we didn't
537 * find anything.
538 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000539 if (list_empty(&dev->maplist) || !map) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100540 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000541 return -EINVAL;
542 }
543
Dave Airlie836cf042005-07-10 19:27:04 +1000544 /* Register and framebuffer maps are permanent */
545 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100546 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000547 return 0;
548 }
549
550 ret = drm_rmmap_locked(dev, map);
551
Dave Airlie30e2fb12006-02-02 19:37:46 +1100552 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000553
554 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000555}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557/**
558 * Cleanup after an error on one of the addbufs() functions.
559 *
Dave Airlie836cf042005-07-10 19:27:04 +1000560 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * \param entry buffer entry where the error occurred.
562 *
563 * Frees any pages and buffers associated with the given entry.
564 */
Dave Airliecdd55a22007-07-11 16:32:08 +1000565static void drm_cleanup_buf_error(struct drm_device * dev,
566 struct drm_buf_entry * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 int i;
569
570 if (entry->seg_count) {
571 for (i = 0; i < entry->seg_count; i++) {
572 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100573 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 }
576 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577 entry->seg_count *
578 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 entry->seg_count = 0;
581 }
582
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 if (entry->buf_count) {
584 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (entry->buflist[i].dev_private) {
586 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000587 entry->buflist[i].dev_priv_size,
588 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 }
591 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000592 entry->buf_count *
593 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 entry->buf_count = 0;
596 }
597}
598
599#if __OS_HAS_AGP
600/**
Dave Airlied59431b2005-07-10 15:00:06 +1000601 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 *
Dave Airlie84b1fd12007-07-11 15:53:27 +1000603 * \param dev struct drm_device to which the buffers are to be added.
Dave Airliec60ce622007-07-11 15:27:12 +1000604 * \param request pointer to a struct drm_buf_desc describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * After some sanity checks creates a drm_buf structure for each buffer and
608 * reallocates the buffer list of the same size order to accommodate the new
609 * buffers.
610 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000611int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Dave Airliecdd55a22007-07-11 16:32:08 +1000613 struct drm_device_dma *dma = dev->dma;
614 struct drm_buf_entry *entry;
Dave Airlie55910512007-07-11 16:53:40 +1000615 struct drm_agp_mem *agp_entry;
Dave Airlie056219e2007-07-11 16:17:42 +1000616 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 unsigned long offset;
618 unsigned long agp_offset;
619 int count;
620 int order;
621 int size;
622 int alignment;
623 int page_order;
624 int total;
625 int byte_count;
Dave Airlie54ba2f72007-02-10 12:07:47 +1100626 int i, valid;
Dave Airlie056219e2007-07-11 16:17:42 +1000627 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 if (!dma)
630 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Dave Airlied59431b2005-07-10 15:00:06 +1000632 count = request->count;
633 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 size = 1 << order;
635
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000636 alignment = (request->flags & _DRM_PAGE_ALIGN)
637 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
639 total = PAGE_SIZE << page_order;
640
641 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000642 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000644 DRM_DEBUG("count: %d\n", count);
645 DRM_DEBUG("order: %d\n", order);
646 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100647 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000648 DRM_DEBUG("alignment: %d\n", alignment);
649 DRM_DEBUG("page_order: %d\n", page_order);
650 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000652 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
653 return -EINVAL;
654 if (dev->queue_count)
655 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Dave Airlie54ba2f72007-02-10 12:07:47 +1100657 /* Make sure buffers are located in AGP memory that we own */
658 valid = 0;
Dave Airliebd1b3312007-05-26 05:01:51 +1000659 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100660 if ((agp_offset >= agp_entry->bound) &&
661 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
662 valid = 1;
663 break;
664 }
665 }
Dave Airliebd1b3312007-05-26 05:01:51 +1000666 if (!list_empty(&dev->agp->memory) && !valid) {
Dave Airlie54ba2f72007-02-10 12:07:47 +1100667 DRM_DEBUG("zone invalid\n");
668 return -EINVAL;
669 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000670 spin_lock(&dev->count_lock);
671 if (dev->buf_use) {
672 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -EBUSY;
674 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 atomic_inc(&dev->buf_alloc);
676 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Dave Airlie30e2fb12006-02-02 19:37:46 +1100678 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100681 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000682 atomic_dec(&dev->buf_alloc);
683 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
686 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100687 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return -EINVAL;
690 }
691
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
693 DRM_MEM_BUFS);
694 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;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
724 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
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743 temp_buflist = drm_realloc(dma->buflist,
744 dma->buf_count * sizeof(*dma->buflist),
745 (dma->buf_count + entry->buf_count)
746 * sizeof(*dma->buflist), DRM_MEM_BUFS);
747 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000749 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100750 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000751 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return -ENOMEM;
753 }
754 dma->buflist = temp_buflist;
755
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
758 }
759
760 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100761 dma->seg_count += entry->seg_count;
762 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 dma->byte_count += byte_count;
764
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000765 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
766 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Dave Airlie30e2fb12006-02-02 19:37:46 +1100768 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Dave Airlied59431b2005-07-10 15:00:06 +1000770 request->count = entry->buf_count;
771 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 dma->flags = _DRM_DMA_USE_AGP;
774
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 0;
777}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000778EXPORT_SYMBOL(drm_addbufs_agp);
779#endif /* __OS_HAS_AGP */
780
Dave Airlie84b1fd12007-07-11 15:53:27 +1000781int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Dave Airliecdd55a22007-07-11 16:32:08 +1000783 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int count;
785 int order;
786 int size;
787 int total;
788 int page_order;
Dave Airliecdd55a22007-07-11 16:32:08 +1000789 struct drm_buf_entry *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100790 drm_dma_handle_t *dmah;
Dave Airlie056219e2007-07-11 16:17:42 +1000791 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int alignment;
793 unsigned long offset;
794 int i;
795 int byte_count;
796 int page_count;
797 unsigned long *temp_pagelist;
Dave Airlie056219e2007-07-11 16:17:42 +1000798 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
801 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100802
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 if (!dma)
804 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Dave Airlied985c102006-01-02 21:32:48 +1100806 if (!capable(CAP_SYS_ADMIN))
807 return -EPERM;
808
Dave Airlied59431b2005-07-10 15:00:06 +1000809 count = request->count;
810 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 size = 1 << order;
812
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000813 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
814 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000816 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
817 return -EINVAL;
818 if (dev->queue_count)
819 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Dave Airlied59431b2005-07-10 15:00:06 +1000821 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
824 total = PAGE_SIZE << page_order;
825
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 spin_lock(&dev->count_lock);
827 if (dev->buf_use) {
828 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return -EBUSY;
830 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 atomic_inc(&dev->buf_alloc);
832 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Dave Airlie30e2fb12006-02-02 19:37:46 +1100834 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100837 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -ENOMEM; /* May only call once for each order */
840 }
841
842 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100843 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return -EINVAL;
846 }
847
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
849 DRM_MEM_BUFS);
850 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100851 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return -ENOMEM;
854 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
858 DRM_MEM_SEGS);
859 if (!entry->seglist) {
860 drm_free(entry->buflist,
861 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100862 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000863 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return -ENOMEM;
865 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 /* Keep the original pagelist until we know all the allocations
869 * have succeeded
870 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
872 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 drm_free(entry->buflist,
875 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
876 drm_free(entry->seglist,
877 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100878 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return -ENOMEM;
881 }
882 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000883 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
884 DRM_DEBUG("pagelist: %d entries\n",
885 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 entry->page_order = page_order;
889 byte_count = 0;
890 page_count = 0;
891
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 while (entry->buf_count < count) {
Dave Airliebc5f4522007-11-05 12:50:58 +1000893
Dave Airlieddf19b92006-03-19 18:56:12 +1100894 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
Dave Airliebc5f4522007-11-05 12:50:58 +1000895
Dave Airlieddf19b92006-03-19 18:56:12 +1100896 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /* Set count correctly so we free the proper amount. */
898 entry->buf_count = count;
899 entry->seg_count = count;
900 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 drm_free(temp_pagelist,
902 (dma->page_count + (count << page_order))
903 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100904 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return -ENOMEM;
907 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100908 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 for (i = 0; i < (1 << page_order); i++) {
910 DRM_DEBUG("page %d @ 0x%08lx\n",
911 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100912 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100914 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 for (offset = 0;
917 offset + size <= total && entry->buf_count < count;
918 offset += alignment, ++entry->buf_count) {
919 buf = &entry->buflist[entry->buf_count];
920 buf->idx = dma->buf_count + entry->buf_count;
921 buf->total = alignment;
922 buf->order = order;
923 buf->used = 0;
924 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100925 buf->address = (void *)(dmah->vaddr + offset);
926 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000927 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 buf->waiting = 0;
929 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +1000931 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000934 buf->dev_private = drm_alloc(buf->dev_priv_size,
935 DRM_MEM_BUFS);
936 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* Set count correctly so we free the proper amount. */
938 entry->buf_count = count;
939 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000940 drm_cleanup_buf_error(dev, entry);
941 drm_free(temp_pagelist,
942 (dma->page_count +
943 (count << page_order))
944 * sizeof(*dma->pagelist),
945 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100946 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000947 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return -ENOMEM;
949 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000950 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 DRM_DEBUG("buffer %d @ %p\n",
953 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955 byte_count += PAGE_SIZE << page_order;
956 }
957
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000958 temp_buflist = drm_realloc(dma->buflist,
959 dma->buf_count * sizeof(*dma->buflist),
960 (dma->buf_count + entry->buf_count)
961 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!temp_buflist) {
963 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000964 drm_cleanup_buf_error(dev, entry);
965 drm_free(temp_pagelist,
966 (dma->page_count + (count << page_order))
967 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100968 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000969 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return -ENOMEM;
971 }
972 dma->buflist = temp_buflist;
973
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
976 }
977
978 /* No allocations failed, so now we can replace the orginal pagelist
979 * with the new one.
980 */
981 if (dma->page_count) {
982 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000983 dma->page_count * sizeof(*dma->pagelist),
984 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 dma->pagelist = temp_pagelist;
987
988 dma->buf_count += entry->buf_count;
989 dma->seg_count += entry->seg_count;
990 dma->page_count += entry->seg_count << page_order;
991 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
992
Dave Airlie30e2fb12006-02-02 19:37:46 +1100993 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Dave Airlied59431b2005-07-10 15:00:06 +1000995 request->count = entry->buf_count;
996 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
George Sapountzis3417f332006-10-24 12:03:04 -0700998 if (request->flags & _DRM_PCI_BUFFER_RO)
999 dma->flags = _DRM_DMA_USE_PCI_RO;
1000
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
1003
1004}
Dave Airlied84f76d2005-07-10 17:04:22 +10001005EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Dave Airlie84b1fd12007-07-11 15:53:27 +10001007static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Dave Airliecdd55a22007-07-11 16:32:08 +10001009 struct drm_device_dma *dma = dev->dma;
1010 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001011 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 unsigned long offset;
1013 unsigned long agp_offset;
1014 int count;
1015 int order;
1016 int size;
1017 int alignment;
1018 int page_order;
1019 int total;
1020 int byte_count;
1021 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001022 struct drm_buf **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 if (!drm_core_check_feature(dev, DRIVER_SG))
1025 return -EINVAL;
1026
1027 if (!dma)
1028 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Dave Airlied985c102006-01-02 21:32:48 +11001030 if (!capable(CAP_SYS_ADMIN))
1031 return -EPERM;
1032
Dave Airlied59431b2005-07-10 15:00:06 +10001033 count = request->count;
1034 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 size = 1 << order;
1036
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001037 alignment = (request->flags & _DRM_PAGE_ALIGN)
1038 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1040 total = PAGE_SIZE << page_order;
1041
1042 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001043 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001045 DRM_DEBUG("count: %d\n", count);
1046 DRM_DEBUG("order: %d\n", order);
1047 DRM_DEBUG("size: %d\n", size);
1048 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1049 DRM_DEBUG("alignment: %d\n", alignment);
1050 DRM_DEBUG("page_order: %d\n", page_order);
1051 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1054 return -EINVAL;
1055 if (dev->queue_count)
1056 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 spin_lock(&dev->count_lock);
1059 if (dev->buf_use) {
1060 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return -EBUSY;
1062 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001063 atomic_inc(&dev->buf_alloc);
1064 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Dave Airlie30e2fb12006-02-02 19:37:46 +11001066 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001068 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001069 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001070 atomic_dec(&dev->buf_alloc);
1071 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
1074 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001075 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EINVAL;
1078 }
1079
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1081 DRM_MEM_BUFS);
1082 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001083 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001084 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -ENOMEM;
1086 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 entry->buf_size = size;
1090 entry->page_order = page_order;
1091
1092 offset = 0;
1093
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 while (entry->buf_count < count) {
1095 buf = &entry->buflist[entry->buf_count];
1096 buf->idx = dma->buf_count + entry->buf_count;
1097 buf->total = alignment;
1098 buf->order = order;
1099 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001101 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001103 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +10001104 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 buf->waiting = 0;
1107 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001109 buf->file_priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1113 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /* Set count correctly so we free the proper amount. */
1115 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001117 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001118 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -ENOMEM;
1120 }
1121
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001122 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001124 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 offset += alignment;
1127 entry->buf_count++;
1128 byte_count += PAGE_SIZE << page_order;
1129 }
1130
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001131 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001133 temp_buflist = drm_realloc(dma->buflist,
1134 dma->buf_count * sizeof(*dma->buflist),
1135 (dma->buf_count + entry->buf_count)
1136 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1137 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001139 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001140 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return -ENOMEM;
1143 }
1144 dma->buflist = temp_buflist;
1145
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001146 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1148 }
1149
1150 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001151 dma->seg_count += entry->seg_count;
1152 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 dma->byte_count += byte_count;
1154
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001155 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1156 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Dave Airlie30e2fb12006-02-02 19:37:46 +11001158 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Dave Airlied59431b2005-07-10 15:00:06 +10001160 request->count = entry->buf_count;
1161 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 dma->flags = _DRM_DMA_USE_SG;
1164
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001165 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return 0;
1167}
1168
Dave Airlie84b1fd12007-07-11 15:53:27 +10001169static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001170{
Dave Airliecdd55a22007-07-11 16:32:08 +10001171 struct drm_device_dma *dma = dev->dma;
1172 struct drm_buf_entry *entry;
Dave Airlie056219e2007-07-11 16:17:42 +10001173 struct drm_buf *buf;
Dave Airlieb84397d62005-07-10 14:46:12 +10001174 unsigned long offset;
1175 unsigned long agp_offset;
1176 int count;
1177 int order;
1178 int size;
1179 int alignment;
1180 int page_order;
1181 int total;
1182 int byte_count;
1183 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001184 struct drm_buf **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001185
1186 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1187 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001188
Dave Airlieb84397d62005-07-10 14:46:12 +10001189 if (!dma)
1190 return -EINVAL;
1191
Dave Airlied985c102006-01-02 21:32:48 +11001192 if (!capable(CAP_SYS_ADMIN))
1193 return -EPERM;
1194
Dave Airlied59431b2005-07-10 15:00:06 +10001195 count = request->count;
1196 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001197 size = 1 << order;
1198
Dave Airlied59431b2005-07-10 15:00:06 +10001199 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001200 ? PAGE_ALIGN(size) : size;
1201 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1202 total = PAGE_SIZE << page_order;
1203
1204 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001205 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001206
1207 DRM_DEBUG("count: %d\n", count);
1208 DRM_DEBUG("order: %d\n", order);
1209 DRM_DEBUG("size: %d\n", size);
1210 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1211 DRM_DEBUG("alignment: %d\n", alignment);
1212 DRM_DEBUG("page_order: %d\n", page_order);
1213 DRM_DEBUG("total: %d\n", total);
1214
1215 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1216 return -EINVAL;
1217 if (dev->queue_count)
1218 return -EBUSY; /* Not while in use */
1219
1220 spin_lock(&dev->count_lock);
1221 if (dev->buf_use) {
1222 spin_unlock(&dev->count_lock);
1223 return -EBUSY;
1224 }
1225 atomic_inc(&dev->buf_alloc);
1226 spin_unlock(&dev->count_lock);
1227
Dave Airlie30e2fb12006-02-02 19:37:46 +11001228 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001229 entry = &dma->bufs[order];
1230 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001231 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001232 atomic_dec(&dev->buf_alloc);
1233 return -ENOMEM; /* May only call once for each order */
1234 }
1235
1236 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001237 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001238 atomic_dec(&dev->buf_alloc);
1239 return -EINVAL;
1240 }
1241
1242 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1243 DRM_MEM_BUFS);
1244 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001245 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001246 atomic_dec(&dev->buf_alloc);
1247 return -ENOMEM;
1248 }
1249 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1250
1251 entry->buf_size = size;
1252 entry->page_order = page_order;
1253
1254 offset = 0;
1255
1256 while (entry->buf_count < count) {
1257 buf = &entry->buflist[entry->buf_count];
1258 buf->idx = dma->buf_count + entry->buf_count;
1259 buf->total = alignment;
1260 buf->order = order;
1261 buf->used = 0;
1262
1263 buf->offset = (dma->byte_count + offset);
1264 buf->bus_address = agp_offset + offset;
1265 buf->address = (void *)(agp_offset + offset);
1266 buf->next = NULL;
1267 buf->waiting = 0;
1268 buf->pending = 0;
1269 init_waitqueue_head(&buf->dma_wait);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001270 buf->file_priv = NULL;
Dave Airlieb84397d62005-07-10 14:46:12 +10001271
1272 buf->dev_priv_size = dev->driver->dev_priv_size;
1273 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1274 if (!buf->dev_private) {
1275 /* Set count correctly so we free the proper amount. */
1276 entry->buf_count = count;
1277 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001278 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001279 atomic_dec(&dev->buf_alloc);
1280 return -ENOMEM;
1281 }
1282 memset(buf->dev_private, 0, buf->dev_priv_size);
1283
1284 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1285
1286 offset += alignment;
1287 entry->buf_count++;
1288 byte_count += PAGE_SIZE << page_order;
1289 }
1290
1291 DRM_DEBUG("byte_count: %d\n", byte_count);
1292
1293 temp_buflist = drm_realloc(dma->buflist,
1294 dma->buf_count * sizeof(*dma->buflist),
1295 (dma->buf_count + entry->buf_count)
1296 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1297 if (!temp_buflist) {
1298 /* Free the entry because it isn't valid */
1299 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001300 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001301 atomic_dec(&dev->buf_alloc);
1302 return -ENOMEM;
1303 }
1304 dma->buflist = temp_buflist;
1305
1306 for (i = 0; i < entry->buf_count; i++) {
1307 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1308 }
1309
1310 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001311 dma->seg_count += entry->seg_count;
1312 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001313 dma->byte_count += byte_count;
1314
1315 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1316 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1317
Dave Airlie30e2fb12006-02-02 19:37:46 +11001318 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001319
Dave Airlied59431b2005-07-10 15:00:06 +10001320 request->count = entry->buf_count;
1321 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001322
1323 dma->flags = _DRM_DMA_USE_FB;
1324
1325 atomic_dec(&dev->buf_alloc);
1326 return 0;
1327}
Dave Airlied985c102006-01-02 21:32:48 +11001328
Dave Airlieb84397d62005-07-10 14:46:12 +10001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330/**
1331 * Add buffers for DMA transfers (ioctl).
1332 *
1333 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001334 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * \param cmd command.
Dave Airliec60ce622007-07-11 15:27:12 +10001336 * \param arg pointer to a struct drm_buf_desc request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * \return zero on success or a negative number on failure.
1338 *
1339 * According with the memory type specified in drm_buf_desc::flags and the
1340 * build options, it dispatches the call either to addbufs_agp(),
1341 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1342 * PCI memory respectively.
1343 */
Eric Anholtc153f452007-09-03 12:06:45 +10001344int drm_addbufs(struct drm_device *dev, void *data,
1345 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Eric Anholtc153f452007-09-03 12:06:45 +10001347 struct drm_buf_desc *request = data;
Dave Airlied59431b2005-07-10 15:00:06 +10001348 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1351 return -EINVAL;
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353#if __OS_HAS_AGP
Eric Anholtc153f452007-09-03 12:06:45 +10001354 if (request->flags & _DRM_AGP_BUFFER)
1355 ret = drm_addbufs_agp(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 else
1357#endif
Eric Anholtc153f452007-09-03 12:06:45 +10001358 if (request->flags & _DRM_SG_BUFFER)
1359 ret = drm_addbufs_sg(dev, request);
1360 else if (request->flags & _DRM_FB_BUFFER)
1361 ret = drm_addbufs_fb(dev, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 else
Eric Anholtc153f452007-09-03 12:06:45 +10001363 ret = drm_addbufs_pci(dev, request);
Dave Airlied59431b2005-07-10 15:00:06 +10001364
Dave Airlied59431b2005-07-10 15:00:06 +10001365 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368/**
1369 * Get information about the buffer mappings.
1370 *
1371 * This was originally mean for debugging purposes, or by a sophisticated
1372 * client library to determine how best to use the available buffers (e.g.,
1373 * large buffers can be used for image transfer).
1374 *
1375 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001376 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 * \param cmd command.
1378 * \param arg pointer to a drm_buf_info structure.
1379 * \return zero on success or a negative number on failure.
1380 *
1381 * Increments drm_device::buf_use while holding the drm_device::count_lock
1382 * lock, preventing of allocating more buffers after this call. Information
1383 * about each requested buffer is then copied into user space.
1384 */
Eric Anholtc153f452007-09-03 12:06:45 +10001385int drm_infobufs(struct drm_device *dev, void *data,
1386 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Dave Airliecdd55a22007-07-11 16:32:08 +10001388 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001389 struct drm_buf_info *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int i;
1391 int count;
1392
1393 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1394 return -EINVAL;
1395
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001396 if (!dma)
1397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001399 spin_lock(&dev->count_lock);
1400 if (atomic_read(&dev->buf_alloc)) {
1401 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return -EBUSY;
1403 }
1404 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001405 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001407 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1408 if (dma->bufs[i].buf_count)
1409 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001412 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Eric Anholtc153f452007-09-03 12:06:45 +10001414 if (request->count >= count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001415 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1416 if (dma->bufs[i].buf_count) {
Dave Airliec60ce622007-07-11 15:27:12 +10001417 struct drm_buf_desc __user *to =
Eric Anholtc153f452007-09-03 12:06:45 +10001418 &request->list[count];
Dave Airliecdd55a22007-07-11 16:32:08 +10001419 struct drm_buf_entry *from = &dma->bufs[i];
1420 struct drm_freelist *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001421 if (copy_to_user(&to->count,
1422 &from->buf_count,
1423 sizeof(from->buf_count)) ||
1424 copy_to_user(&to->size,
1425 &from->buf_size,
1426 sizeof(from->buf_size)) ||
1427 copy_to_user(&to->low_mark,
1428 &list->low_mark,
1429 sizeof(list->low_mark)) ||
1430 copy_to_user(&to->high_mark,
1431 &list->high_mark,
1432 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return -EFAULT;
1434
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001435 DRM_DEBUG("%d %d %d %d %d\n",
1436 i,
1437 dma->bufs[i].buf_count,
1438 dma->bufs[i].buf_size,
1439 dma->bufs[i].freelist.low_mark,
1440 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 ++count;
1442 }
1443 }
1444 }
Eric Anholtc153f452007-09-03 12:06:45 +10001445 request->count = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 return 0;
1448}
1449
1450/**
1451 * Specifies a low and high water mark for buffer allocation
1452 *
1453 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001454 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 * \param cmd command.
1456 * \param arg a pointer to a drm_buf_desc structure.
1457 * \return zero on success or a negative number on failure.
1458 *
1459 * Verifies that the size order is bounded between the admissible orders and
1460 * updates the respective drm_device_dma::bufs entry low and high water mark.
1461 *
1462 * \note This ioctl is deprecated and mostly never used.
1463 */
Eric Anholtc153f452007-09-03 12:06:45 +10001464int drm_markbufs(struct drm_device *dev, void *data,
1465 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Dave Airliecdd55a22007-07-11 16:32:08 +10001467 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001468 struct drm_buf_desc *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 int order;
Dave Airliecdd55a22007-07-11 16:32:08 +10001470 struct drm_buf_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1473 return -EINVAL;
1474
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001475 if (!dma)
1476 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001478 DRM_DEBUG("%d, %d, %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001479 request->size, request->low_mark, request->high_mark);
1480 order = drm_order(request->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001481 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1482 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 entry = &dma->bufs[order];
1484
Eric Anholtc153f452007-09-03 12:06:45 +10001485 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return -EINVAL;
Eric Anholtc153f452007-09-03 12:06:45 +10001487 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return -EINVAL;
1489
Eric Anholtc153f452007-09-03 12:06:45 +10001490 entry->freelist.low_mark = request->low_mark;
1491 entry->freelist.high_mark = request->high_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 return 0;
1494}
1495
1496/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001497 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *
1499 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001500 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 * \param cmd command.
1502 * \param arg pointer to a drm_buf_free structure.
1503 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001504 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 * Calls free_buffer() for each used buffer.
1506 * This function is primarily used for debugging.
1507 */
Eric Anholtc153f452007-09-03 12:06:45 +10001508int drm_freebufs(struct drm_device *dev, void *data,
1509 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Dave Airliecdd55a22007-07-11 16:32:08 +10001511 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001512 struct drm_buf_free *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 int i;
1514 int idx;
Dave Airlie056219e2007-07-11 16:17:42 +10001515 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1518 return -EINVAL;
1519
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001520 if (!dma)
1521 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Eric Anholtc153f452007-09-03 12:06:45 +10001523 DRM_DEBUG("%d\n", request->count);
1524 for (i = 0; i < request->count; i++) {
1525 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001527 if (idx < 0 || idx >= dma->buf_count) {
1528 DRM_ERROR("Index %d (of %d max)\n",
1529 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return -EINVAL;
1531 }
1532 buf = dma->buflist[idx];
Eric Anholt6c340ea2007-08-25 20:23:09 +10001533 if (buf->file_priv != file_priv) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001534 DRM_ERROR("Process %d freeing buffer not owned\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001535 task_pid_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return -EINVAL;
1537 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001538 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540
1541 return 0;
1542}
1543
1544/**
1545 * Maps all of the DMA buffers into client-virtual space (ioctl).
1546 *
1547 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001548 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 * \param cmd command.
1550 * \param arg pointer to a drm_buf_map structure.
1551 * \return zero on success or a negative number on failure.
1552 *
George Sapountzis3417f332006-10-24 12:03:04 -07001553 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1554 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1555 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1556 * drm_mmap_dma().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
Eric Anholtc153f452007-09-03 12:06:45 +10001558int drm_mapbufs(struct drm_device *dev, void *data,
1559 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Dave Airliecdd55a22007-07-11 16:32:08 +10001561 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int retcode = 0;
1563 const int zero = 0;
1564 unsigned long virtual;
1565 unsigned long address;
Eric Anholtc153f452007-09-03 12:06:45 +10001566 struct drm_buf_map *request = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 int i;
1568
1569 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1570 return -EINVAL;
1571
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001572 if (!dma)
1573 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001575 spin_lock(&dev->count_lock);
1576 if (atomic_read(&dev->buf_alloc)) {
1577 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return -EBUSY;
1579 }
1580 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001581 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Eric Anholtc153f452007-09-03 12:06:45 +10001583 if (request->count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001584 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001585 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001586 && (dma->flags & _DRM_DMA_USE_SG))
1587 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1588 && (dma->flags & _DRM_DMA_USE_FB))) {
Benjamin Herrenschmidtf77d3902009-02-02 16:55:46 +11001589 struct drm_local_map *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001590 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001592 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 retcode = -EINVAL;
1594 goto done;
1595 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001596 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001597 virtual = do_mmap(file_priv->filp, 0, map->size,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001598 PROT_READ | PROT_WRITE,
Eric Anholtc153f452007-09-03 12:06:45 +10001599 MAP_SHARED,
1600 token);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001601 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001603 down_write(&current->mm->mmap_sem);
Eric Anholt6c340ea2007-08-25 20:23:09 +10001604 virtual = do_mmap(file_priv->filp, 0, dma->byte_count,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001605 PROT_READ | PROT_WRITE,
1606 MAP_SHARED, 0);
1607 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001609 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /* Real error */
1611 retcode = (signed long)virtual;
1612 goto done;
1613 }
Eric Anholtc153f452007-09-03 12:06:45 +10001614 request->virtual = (void __user *)virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001616 for (i = 0; i < dma->buf_count; i++) {
Eric Anholtc153f452007-09-03 12:06:45 +10001617 if (copy_to_user(&request->list[i].idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001618 &dma->buflist[i]->idx,
Eric Anholtc153f452007-09-03 12:06:45 +10001619 sizeof(request->list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 retcode = -EFAULT;
1621 goto done;
1622 }
Eric Anholtc153f452007-09-03 12:06:45 +10001623 if (copy_to_user(&request->list[i].total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001624 &dma->buflist[i]->total,
Eric Anholtc153f452007-09-03 12:06:45 +10001625 sizeof(request->list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 retcode = -EFAULT;
1627 goto done;
1628 }
Eric Anholtc153f452007-09-03 12:06:45 +10001629 if (copy_to_user(&request->list[i].used,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001630 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 retcode = -EFAULT;
1632 goto done;
1633 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001634 address = virtual + dma->buflist[i]->offset; /* *** */
Eric Anholtc153f452007-09-03 12:06:45 +10001635 if (copy_to_user(&request->list[i].address,
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001636 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 retcode = -EFAULT;
1638 goto done;
1639 }
1640 }
1641 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001642 done:
Eric Anholtc153f452007-09-03 12:06:45 +10001643 request->count = dma->buf_count;
1644 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 return retcode;
1647}
1648
Dave Airlie836cf042005-07-10 19:27:04 +10001649/**
1650 * Compute size order. Returns the exponent of the smaller power of two which
1651 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001652 *
Dave Airlie836cf042005-07-10 19:27:04 +10001653 * \param size size.
1654 * \return order.
1655 *
1656 * \todo Can be made faster.
1657 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001658int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001659{
1660 int order;
1661 unsigned long tmp;
1662
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001663 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001664
1665 if (size & (size - 1))
1666 ++order;
1667
1668 return order;
1669}
1670EXPORT_SYMBOL(drm_order);