blob: 006b06d29727069b30ff304f5ad58084804a689d [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>
37#include "drmP.h"
38
Dave Airlied985c102006-01-02 21:32:48 +110039unsigned long drm_get_resource_start(drm_device_t *dev, unsigned int resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Dave Airlie836cf042005-07-10 19:27:04 +100041 return pci_resource_start(dev->pdev, resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
Dave Airlie836cf042005-07-10 19:27:04 +100043EXPORT_SYMBOL(drm_get_resource_start);
44
Dave Airlied985c102006-01-02 21:32:48 +110045unsigned long drm_get_resource_len(drm_device_t *dev, unsigned int resource)
Dave Airlie836cf042005-07-10 19:27:04 +100046{
47 return pci_resource_len(dev->pdev, resource);
48}
Dave Airlieb5e89ed2005-09-25 14:28:13 +100049
Dave Airlie836cf042005-07-10 19:27:04 +100050EXPORT_SYMBOL(drm_get_resource_len);
51
Dave Airlied985c102006-01-02 21:32:48 +110052static drm_map_list_t *drm_find_matching_map(drm_device_t *dev,
53 drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +100054{
55 struct list_head *list;
56
57 list_for_each(list, &dev->maplist->head) {
58 drm_map_list_t *entry = list_entry(list, drm_map_list_t, head);
59 if (entry->map && map->type == entry->map->type &&
60 entry->map->offset == map->offset) {
Dave Airlie89625eb2005-09-05 21:23:23 +100061 return entry;
Dave Airlie836cf042005-07-10 19:27:04 +100062 }
63 }
64
65 return NULL;
66}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Dave Airlie9a186642005-06-23 21:29:18 +100068/*
Dave Airlied1f2b552005-08-05 22:11:22 +100069 * Used to allocate 32-bit handles for mappings.
Dave Airlie9a186642005-06-23 21:29:18 +100070 */
Dave Airlied1f2b552005-08-05 22:11:22 +100071#define START_RANGE 0x10000000
72#define END_RANGE 0x40000000
73
74#ifdef _LP64
Dave Airlieb5e89ed2005-09-25 14:28:13 +100075static __inline__ unsigned int HandleID(unsigned long lhandle,
Dave Airlied985c102006-01-02 21:32:48 +110076 drm_device_t *dev)
Dave Airlied1f2b552005-08-05 22:11:22 +100077{
78 static unsigned int map32_handle = START_RANGE;
79 unsigned int hash;
80
81 if (lhandle & 0xffffffff00000000) {
82 hash = map32_handle;
83 map32_handle += PAGE_SIZE;
84 if (map32_handle > END_RANGE)
85 map32_handle = START_RANGE;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086 } else
Dave Airlied1f2b552005-08-05 22:11:22 +100087 hash = lhandle;
88
89 while (1) {
90 drm_map_list_t *_entry;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100091 list_for_each_entry(_entry, &dev->maplist->head, head) {
Dave Airlied1f2b552005-08-05 22:11:22 +100092 if (_entry->user_token == hash)
93 break;
94 }
95 if (&_entry->head == &dev->maplist->head)
96 return hash;
97
98 hash += PAGE_SIZE;
99 map32_handle += PAGE_SIZE;
100 }
101}
102#else
103# define HandleID(x,dev) (unsigned int)(x)
Dave Airlie9a186642005-06-23 21:29:18 +1000104#endif
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/**
107 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
108 *
109 * \param inode device inode.
110 * \param filp file pointer.
111 * \param cmd command.
112 * \param arg pointer to a drm_map structure.
113 * \return zero on success or a negative value on error.
114 *
115 * Adjusts the memory offset to its absolute value according to the mapping
116 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
117 * applicable and if supported by the kernel.
118 */
Dave Airlieb3a83632005-09-30 18:37:36 +1000119static int drm_addmap_core(drm_device_t * dev, unsigned int offset,
120 unsigned int size, drm_map_type_t type,
121 drm_map_flags_t flags, drm_map_list_t ** maplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 drm_map_t *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 drm_map_list_t *list;
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000125 drm_dma_handle_t *dmah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000127 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
128 if (!map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return -ENOMEM;
130
Dave Airlie7ab98402005-07-10 16:56:52 +1000131 map->offset = offset;
132 map->size = size;
133 map->flags = flags;
134 map->type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 /* Only allow shared memory to be removable since we only keep enough
137 * book keeping information about shared memory to allow for removal
138 * when processes fork.
139 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
141 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -EINVAL;
143 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000144 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
145 map->offset, map->size, map->type);
146 if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
147 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -EINVAL;
149 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150 map->mtrr = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 map->handle = NULL;
152
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000153 switch (map->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 case _DRM_REGISTERS:
155 case _DRM_FRAME_BUFFER:
Dave Airlie88f399c2005-08-20 17:43:33 +1000156#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
Dave Airlie8d2ea622006-01-11 20:48:09 +1100157 if (map->offset + (map->size-1) < map->offset ||
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000158 map->offset < virt_to_phys(high_memory)) {
159 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -EINVAL;
161 }
162#endif
163#ifdef __alpha__
164 map->offset += dev->hose->mem_space->start;
165#endif
Dave Airlie836cf042005-07-10 19:27:04 +1000166 /* Some drivers preinitialize some maps, without the X Server
167 * needing to be aware of it. Therefore, we just return success
168 * when the server tries to create a duplicate map.
169 */
Dave Airlie89625eb2005-09-05 21:23:23 +1000170 list = drm_find_matching_map(dev, map);
171 if (list != NULL) {
172 if (list->map->size != map->size) {
Dave Airlie836cf042005-07-10 19:27:04 +1000173 DRM_DEBUG("Matching maps of type %d with "
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 "mismatched sizes, (%ld vs %ld)\n",
175 map->type, map->size,
176 list->map->size);
Dave Airlie89625eb2005-09-05 21:23:23 +1000177 list->map->size = map->size;
Dave Airlie836cf042005-07-10 19:27:04 +1000178 }
179
180 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Dave Airlie89625eb2005-09-05 21:23:23 +1000181 *maplist = list;
Dave Airlie836cf042005-07-10 19:27:04 +1000182 return 0;
183 }
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (drm_core_has_MTRR(dev)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 if (map->type == _DRM_FRAME_BUFFER ||
187 (map->flags & _DRM_WRITE_COMBINING)) {
188 map->mtrr = mtrr_add(map->offset, map->size,
189 MTRR_TYPE_WRCOMB, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191 }
192 if (map->type == _DRM_REGISTERS)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 map->handle = drm_ioremap(map->offset, map->size, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 break;
195
196 case _DRM_SHM:
197 map->handle = vmalloc_32(map->size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198 DRM_DEBUG("%lu %d %p\n",
199 map->size, drm_order(map->size), map->handle);
200 if (!map->handle) {
201 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -ENOMEM;
203 }
204 map->offset = (unsigned long)map->handle;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000205 if (map->flags & _DRM_CONTAINS_LOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* Prevent a 2nd X Server from creating a 2nd lock */
207 if (dev->lock.hw_lock != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 vfree(map->handle);
209 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -EBUSY;
211 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000212 dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 break;
215 case _DRM_AGP:
216 if (drm_core_has_AGP(dev)) {
217#ifdef __alpha__
218 map->offset += dev->hose->mem_space->start;
219#endif
220 map->offset += dev->agp->base;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000221 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 break;
224 case _DRM_SCATTER_GATHER:
225 if (!dev->sg) {
226 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
227 return -EINVAL;
228 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000229 map->offset += (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 case _DRM_CONSISTENT:
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000232 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000233 * As we're limiting the address to 2^32-1 (or less),
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000234 * casting it down to 32 bits is no problem, but we
235 * need to point to a 64bit variable first. */
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000236 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
237 if (!dmah) {
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000238 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
239 return -ENOMEM;
240 }
Dave Airlie9c8da5e2005-07-10 15:38:56 +1000241 map->handle = dmah->vaddr;
242 map->offset = (unsigned long)dmah->busaddr;
243 kfree(dmah);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return -EINVAL;
248 }
249
250 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000251 if (!list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
253 return -EINVAL;
254 }
255 memset(list, 0, sizeof(*list));
256 list->map = map;
257
Dave Airlie30e2fb12006-02-02 19:37:46 +1100258 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 list_add(&list->head, &dev->maplist->head);
Dave Airlied1f2b552005-08-05 22:11:22 +1000260 /* Assign a 32-bit handle */
Dave Airlie30e2fb12006-02-02 19:37:46 +1100261 /* We do it here so that dev->struct_mutex protects the increment */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000262 list->user_token = HandleID(map->type == _DRM_SHM
Dave Airlied1f2b552005-08-05 22:11:22 +1000263 ? (unsigned long)map->handle
264 : map->offset, dev);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100265 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dave Airlie89625eb2005-09-05 21:23:23 +1000267 *maplist = list;
Dave Airlie7ab98402005-07-10 16:56:52 +1000268 return 0;
269}
Dave Airlie89625eb2005-09-05 21:23:23 +1000270
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000271int drm_addmap(drm_device_t * dev, unsigned int offset,
Dave Airlie89625eb2005-09-05 21:23:23 +1000272 unsigned int size, drm_map_type_t type,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000273 drm_map_flags_t flags, drm_local_map_t ** map_ptr)
Dave Airlie89625eb2005-09-05 21:23:23 +1000274{
275 drm_map_list_t *list;
276 int rc;
277
278 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
279 if (!rc)
280 *map_ptr = list->map;
281 return rc;
282}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283
Dave Airlie7ab98402005-07-10 16:56:52 +1000284EXPORT_SYMBOL(drm_addmap);
285
286int drm_addmap_ioctl(struct inode *inode, struct file *filp,
287 unsigned int cmd, unsigned long arg)
288{
289 drm_file_t *priv = filp->private_data;
290 drm_device_t *dev = priv->head->dev;
291 drm_map_t map;
Dave Airlie89625eb2005-09-05 21:23:23 +1000292 drm_map_list_t *maplist;
Dave Airlie7ab98402005-07-10 16:56:52 +1000293 drm_map_t __user *argp = (void __user *)arg;
294 int err;
295
296 if (!(filp->f_mode & 3))
297 return -EACCES; /* Require read/write */
298
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299 if (copy_from_user(&map, argp, sizeof(map))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -EFAULT;
Dave Airlie7ab98402005-07-10 16:56:52 +1000301 }
302
Dave Airlied985c102006-01-02 21:32:48 +1100303 if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
304 return -EPERM;
305
Dave Airlie89625eb2005-09-05 21:23:23 +1000306 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
307 &maplist);
Dave Airlie7ab98402005-07-10 16:56:52 +1000308
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000309 if (err)
Dave Airlie7ab98402005-07-10 16:56:52 +1000310 return err;
Dave Airlie7ab98402005-07-10 16:56:52 +1000311
Dave Airlie89625eb2005-09-05 21:23:23 +1000312 if (copy_to_user(argp, maplist->map, sizeof(drm_map_t)))
Dave Airlied1f2b552005-08-05 22:11:22 +1000313 return -EFAULT;
Dave Airlie67e1a012005-10-24 18:41:39 +1000314
315 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
316 if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
Dave Airlied1f2b552005-08-05 22:11:22 +1000317 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
Dave Airlie88f399c2005-08-20 17:43:33 +1000319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/**
322 * Remove a map private from list and deallocate resources if the mapping
323 * isn't in use.
324 *
325 * \param inode device inode.
326 * \param filp file pointer.
327 * \param cmd command.
328 * \param arg pointer to a drm_map_t structure.
329 * \return zero on success or a negative value on error.
330 *
331 * Searches the map on drm_device::maplist, removes it from the list, see if
332 * its being used, and free any associate resource (such as MTRR's) if it's not
333 * being on use.
334 *
Dave Airlie7ab98402005-07-10 16:56:52 +1000335 * \sa drm_addmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
Dave Airlied985c102006-01-02 21:32:48 +1100337int drm_rmmap_locked(drm_device_t *dev, drm_local_map_t *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct list_head *list;
340 drm_map_list_t *r_list = NULL;
Dave Airlie836cf042005-07-10 19:27:04 +1000341 drm_dma_handle_t dmah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Dave Airlie836cf042005-07-10 19:27:04 +1000343 /* Find the list entry for the map and remove it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 list_for_each(list, &dev->maplist->head) {
345 r_list = list_entry(list, drm_map_list_t, head);
346
Dave Airlie836cf042005-07-10 19:27:04 +1000347 if (r_list->map == map) {
348 list_del(list);
349 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
Dave Airlie2d0f9ea2005-07-10 14:34:13 +1000350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Dave Airlie836cf042005-07-10 19:27:04 +1000353
354 /* List has wrapped around to the head pointer, or it's empty and we
355 * didn't find anything.
356 */
357 if (list == (&dev->maplist->head)) {
358 return -EINVAL;
359 }
360
361 switch (map->type) {
362 case _DRM_REGISTERS:
363 drm_ioremapfree(map->handle, map->size, dev);
364 /* FALLTHROUGH */
365 case _DRM_FRAME_BUFFER:
366 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
367 int retcode;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000368 retcode = mtrr_del(map->mtrr, map->offset, map->size);
369 DRM_DEBUG("mtrr_del=%d\n", retcode);
Dave Airlie836cf042005-07-10 19:27:04 +1000370 }
371 break;
372 case _DRM_SHM:
373 vfree(map->handle);
374 break;
375 case _DRM_AGP:
376 case _DRM_SCATTER_GATHER:
377 break;
378 case _DRM_CONSISTENT:
379 dmah.vaddr = map->handle;
380 dmah.busaddr = map->offset;
381 dmah.size = map->size;
382 __drm_pci_free(dev, &dmah);
383 break;
384 }
385 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388}
Dave Airlie836cf042005-07-10 19:27:04 +1000389
Dave Airlied985c102006-01-02 21:32:48 +1100390int drm_rmmap(drm_device_t *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000391{
392 int ret;
393
Dave Airlie30e2fb12006-02-02 19:37:46 +1100394 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000395 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100396 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000397
398 return ret;
399}
Dave Airlie7ab98402005-07-10 16:56:52 +1000400
Dave Airlie836cf042005-07-10 19:27:04 +1000401/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
402 * the last close of the device, and this is necessary for cleanup when things
403 * exit uncleanly. Therefore, having userland manually remove mappings seems
404 * like a pointless exercise since they're going away anyway.
405 *
406 * One use case might be after addmap is allowed for normal users for SHM and
407 * gets used by drivers that the server doesn't need to care about. This seems
408 * unlikely.
409 */
Dave Airlie7ab98402005-07-10 16:56:52 +1000410int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
411 unsigned int cmd, unsigned long arg)
412{
413 drm_file_t *priv = filp->private_data;
414 drm_device_t *dev = priv->head->dev;
415 drm_map_t request;
Dave Airlie836cf042005-07-10 19:27:04 +1000416 drm_local_map_t *map = NULL;
417 struct list_head *list;
418 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000419
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000420 if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {
Dave Airlie7ab98402005-07-10 16:56:52 +1000421 return -EFAULT;
422 }
423
Dave Airlie30e2fb12006-02-02 19:37:46 +1100424 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000425 list_for_each(list, &dev->maplist->head) {
426 drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
427
428 if (r_list->map &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000429 r_list->user_token == (unsigned long)request.handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000430 r_list->map->flags & _DRM_REMOVABLE) {
431 map = r_list->map;
432 break;
433 }
434 }
435
436 /* List has wrapped around to the head pointer, or its empty we didn't
437 * find anything.
438 */
439 if (list == (&dev->maplist->head)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100440 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000441 return -EINVAL;
442 }
443
444 if (!map)
445 return -EINVAL;
446
447 /* Register and framebuffer maps are permanent */
448 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100449 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000450 return 0;
451 }
452
453 ret = drm_rmmap_locked(dev, map);
454
Dave Airlie30e2fb12006-02-02 19:37:46 +1100455 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000456
457 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000458}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460/**
461 * Cleanup after an error on one of the addbufs() functions.
462 *
Dave Airlie836cf042005-07-10 19:27:04 +1000463 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * \param entry buffer entry where the error occurred.
465 *
466 * Frees any pages and buffers associated with the given entry.
467 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468static void drm_cleanup_buf_error(drm_device_t * dev, drm_buf_entry_t * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 int i;
471
472 if (entry->seg_count) {
473 for (i = 0; i < entry->seg_count; i++) {
474 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100475 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 }
478 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000479 entry->seg_count *
480 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 entry->seg_count = 0;
483 }
484
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485 if (entry->buf_count) {
486 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (entry->buflist[i].dev_private) {
488 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000489 entry->buflist[i].dev_priv_size,
490 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 }
493 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 entry->buf_count *
495 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 entry->buf_count = 0;
498 }
499}
500
501#if __OS_HAS_AGP
502/**
Dave Airlied59431b2005-07-10 15:00:06 +1000503 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *
Dave Airlied59431b2005-07-10 15:00:06 +1000505 * \param dev drm_device_t to which the buffers are to be added.
506 * \param request pointer to a drm_buf_desc_t describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000508 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * After some sanity checks creates a drm_buf structure for each buffer and
510 * reallocates the buffer list of the same size order to accommodate the new
511 * buffers.
512 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000513int drm_addbufs_agp(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 drm_buf_entry_t *entry;
517 drm_buf_t *buf;
518 unsigned long offset;
519 unsigned long agp_offset;
520 int count;
521 int order;
522 int size;
523 int alignment;
524 int page_order;
525 int total;
526 int byte_count;
527 int i;
528 drm_buf_t **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000530 if (!dma)
531 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Dave Airlied59431b2005-07-10 15:00:06 +1000533 count = request->count;
534 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 size = 1 << order;
536
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000537 alignment = (request->flags & _DRM_PAGE_ALIGN)
538 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
540 total = PAGE_SIZE << page_order;
541
542 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000543 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 DRM_DEBUG("count: %d\n", count);
546 DRM_DEBUG("order: %d\n", order);
547 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100548 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 DRM_DEBUG("alignment: %d\n", alignment);
550 DRM_DEBUG("page_order: %d\n", page_order);
551 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
554 return -EINVAL;
555 if (dev->queue_count)
556 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000558 spin_lock(&dev->count_lock);
559 if (dev->buf_use) {
560 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -EBUSY;
562 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 atomic_inc(&dev->buf_alloc);
564 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Dave Airlie30e2fb12006-02-02 19:37:46 +1100566 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100569 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 atomic_dec(&dev->buf_alloc);
571 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
574 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100575 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return -EINVAL;
578 }
579
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
581 DRM_MEM_BUFS);
582 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100583 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000584 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -ENOMEM;
586 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000587 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 entry->buf_size = size;
590 entry->page_order = page_order;
591
592 offset = 0;
593
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594 while (entry->buf_count < count) {
595 buf = &entry->buflist[entry->buf_count];
596 buf->idx = dma->buf_count + entry->buf_count;
597 buf->total = alignment;
598 buf->order = order;
599 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 buf->bus_address = agp_offset + offset;
603 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000604 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 buf->waiting = 0;
606 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000607 init_waitqueue_head(&buf->dma_wait);
608 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
612 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Set count correctly so we free the proper amount. */
614 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100616 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -ENOMEM;
619 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 offset += alignment;
625 entry->buf_count++;
626 byte_count += PAGE_SIZE << page_order;
627 }
628
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000629 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631 temp_buflist = drm_realloc(dma->buflist,
632 dma->buf_count * sizeof(*dma->buflist),
633 (dma->buf_count + entry->buf_count)
634 * sizeof(*dma->buflist), DRM_MEM_BUFS);
635 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000637 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100638 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000639 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return -ENOMEM;
641 }
642 dma->buflist = temp_buflist;
643
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000644 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
646 }
647
648 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100649 dma->seg_count += entry->seg_count;
650 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 dma->byte_count += byte_count;
652
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
654 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Dave Airlie30e2fb12006-02-02 19:37:46 +1100656 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Dave Airlied59431b2005-07-10 15:00:06 +1000658 request->count = entry->buf_count;
659 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 dma->flags = _DRM_DMA_USE_AGP;
662
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000663 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
665}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666EXPORT_SYMBOL(drm_addbufs_agp);
667#endif /* __OS_HAS_AGP */
668
669int drm_addbufs_pci(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int count;
673 int order;
674 int size;
675 int total;
676 int page_order;
677 drm_buf_entry_t *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100678 drm_dma_handle_t *dmah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 drm_buf_t *buf;
680 int alignment;
681 unsigned long offset;
682 int i;
683 int byte_count;
684 int page_count;
685 unsigned long *temp_pagelist;
686 drm_buf_t **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
689 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100690
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 if (!dma)
692 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Dave Airlied985c102006-01-02 21:32:48 +1100694 if (!capable(CAP_SYS_ADMIN))
695 return -EPERM;
696
Dave Airlied59431b2005-07-10 15:00:06 +1000697 count = request->count;
698 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 size = 1 << order;
700
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
702 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
705 return -EINVAL;
706 if (dev->queue_count)
707 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Dave Airlied59431b2005-07-10 15:00:06 +1000709 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000710 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
712 total = PAGE_SIZE << page_order;
713
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 spin_lock(&dev->count_lock);
715 if (dev->buf_use) {
716 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -EBUSY;
718 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 atomic_inc(&dev->buf_alloc);
720 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Dave Airlie30e2fb12006-02-02 19:37:46 +1100722 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000724 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100725 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000726 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -ENOMEM; /* May only call once for each order */
728 }
729
730 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100731 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000732 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return -EINVAL;
734 }
735
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000736 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
737 DRM_MEM_BUFS);
738 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100739 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -ENOMEM;
742 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000745 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
746 DRM_MEM_SEGS);
747 if (!entry->seglist) {
748 drm_free(entry->buflist,
749 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
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 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000754 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* Keep the original pagelist until we know all the allocations
757 * have succeeded
758 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
760 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000762 drm_free(entry->buflist,
763 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
764 drm_free(entry->seglist,
765 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100766 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000767 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -ENOMEM;
769 }
770 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000771 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
772 DRM_DEBUG("pagelist: %d entries\n",
773 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 entry->page_order = page_order;
777 byte_count = 0;
778 page_count = 0;
779
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000780 while (entry->buf_count < count) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100781
782 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
783
784 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* Set count correctly so we free the proper amount. */
786 entry->buf_count = count;
787 entry->seg_count = count;
788 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 drm_free(temp_pagelist,
790 (dma->page_count + (count << page_order))
791 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100792 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -ENOMEM;
795 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100796 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000797 for (i = 0; i < (1 << page_order); i++) {
798 DRM_DEBUG("page %d @ 0x%08lx\n",
799 dma->page_count + page_count,
Dave Airlieddf19b92006-03-19 18:56:12 +1100800 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100802 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804 for (offset = 0;
805 offset + size <= total && entry->buf_count < count;
806 offset += alignment, ++entry->buf_count) {
807 buf = &entry->buflist[entry->buf_count];
808 buf->idx = dma->buf_count + entry->buf_count;
809 buf->total = alignment;
810 buf->order = order;
811 buf->used = 0;
812 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100813 buf->address = (void *)(dmah->vaddr + offset);
814 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000815 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 buf->waiting = 0;
817 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 init_waitqueue_head(&buf->dma_wait);
819 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 buf->dev_private = drm_alloc(buf->dev_priv_size,
823 DRM_MEM_BUFS);
824 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /* Set count correctly so we free the proper amount. */
826 entry->buf_count = count;
827 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000828 drm_cleanup_buf_error(dev, entry);
829 drm_free(temp_pagelist,
830 (dma->page_count +
831 (count << page_order))
832 * sizeof(*dma->pagelist),
833 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100834 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000835 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return -ENOMEM;
837 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 DRM_DEBUG("buffer %d @ %p\n",
841 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 byte_count += PAGE_SIZE << page_order;
844 }
845
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 temp_buflist = drm_realloc(dma->buflist,
847 dma->buf_count * sizeof(*dma->buflist),
848 (dma->buf_count + entry->buf_count)
849 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (!temp_buflist) {
851 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 drm_cleanup_buf_error(dev, entry);
853 drm_free(temp_pagelist,
854 (dma->page_count + (count << page_order))
855 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100856 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000857 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -ENOMEM;
859 }
860 dma->buflist = temp_buflist;
861
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
864 }
865
866 /* No allocations failed, so now we can replace the orginal pagelist
867 * with the new one.
868 */
869 if (dma->page_count) {
870 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 dma->page_count * sizeof(*dma->pagelist),
872 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874 dma->pagelist = temp_pagelist;
875
876 dma->buf_count += entry->buf_count;
877 dma->seg_count += entry->seg_count;
878 dma->page_count += entry->seg_count << page_order;
879 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
880
Dave Airlie30e2fb12006-02-02 19:37:46 +1100881 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Dave Airlied59431b2005-07-10 15:00:06 +1000883 request->count = entry->buf_count;
884 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000886 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 0;
888
889}
Dave Airlied84f76d2005-07-10 17:04:22 +1000890EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892static int drm_addbufs_sg(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 drm_buf_entry_t *entry;
896 drm_buf_t *buf;
897 unsigned long offset;
898 unsigned long agp_offset;
899 int count;
900 int order;
901 int size;
902 int alignment;
903 int page_order;
904 int total;
905 int byte_count;
906 int i;
907 drm_buf_t **temp_buflist;
908
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 if (!drm_core_check_feature(dev, DRIVER_SG))
910 return -EINVAL;
911
912 if (!dma)
913 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Dave Airlied985c102006-01-02 21:32:48 +1100915 if (!capable(CAP_SYS_ADMIN))
916 return -EPERM;
917
Dave Airlied59431b2005-07-10 15:00:06 +1000918 count = request->count;
919 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 size = 1 << order;
921
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000922 alignment = (request->flags & _DRM_PAGE_ALIGN)
923 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
925 total = PAGE_SIZE << page_order;
926
927 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000928 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 DRM_DEBUG("count: %d\n", count);
931 DRM_DEBUG("order: %d\n", order);
932 DRM_DEBUG("size: %d\n", size);
933 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
934 DRM_DEBUG("alignment: %d\n", alignment);
935 DRM_DEBUG("page_order: %d\n", page_order);
936 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
939 return -EINVAL;
940 if (dev->queue_count)
941 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000943 spin_lock(&dev->count_lock);
944 if (dev->buf_use) {
945 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return -EBUSY;
947 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000948 atomic_inc(&dev->buf_alloc);
949 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Dave Airlie30e2fb12006-02-02 19:37:46 +1100951 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000953 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100954 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 atomic_dec(&dev->buf_alloc);
956 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
959 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100960 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000961 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -EINVAL;
963 }
964
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000965 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
966 DRM_MEM_BUFS);
967 if (!entry->buflist) {
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 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 entry->buf_size = size;
975 entry->page_order = page_order;
976
977 offset = 0;
978
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000979 while (entry->buf_count < count) {
980 buf = &entry->buflist[entry->buf_count];
981 buf->idx = dma->buf_count + entry->buf_count;
982 buf->total = alignment;
983 buf->order = order;
984 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000988 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +1000989 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000990 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 buf->waiting = 0;
992 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000993 init_waitqueue_head(&buf->dma_wait);
994 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000997 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
998 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* Set count correctly so we free the proper amount. */
1000 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001002 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return -ENOMEM;
1005 }
1006
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001007 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 offset += alignment;
1012 entry->buf_count++;
1013 byte_count += PAGE_SIZE << page_order;
1014 }
1015
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001016 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 temp_buflist = drm_realloc(dma->buflist,
1019 dma->buf_count * sizeof(*dma->buflist),
1020 (dma->buf_count + entry->buf_count)
1021 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1022 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001024 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001025 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001026 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -ENOMEM;
1028 }
1029 dma->buflist = temp_buflist;
1030
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1033 }
1034
1035 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001036 dma->seg_count += entry->seg_count;
1037 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 dma->byte_count += byte_count;
1039
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1041 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Dave Airlie30e2fb12006-02-02 19:37:46 +11001043 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Dave Airlied59431b2005-07-10 15:00:06 +10001045 request->count = entry->buf_count;
1046 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 dma->flags = _DRM_DMA_USE_SG;
1049
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001050 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return 0;
1052}
1053
Dave Airlie5d23faf2006-04-23 18:26:40 +10001054static int drm_addbufs_fb(drm_device_t * dev, drm_buf_desc_t * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001055{
Dave Airlieb84397d62005-07-10 14:46:12 +10001056 drm_device_dma_t *dma = dev->dma;
Dave Airlieb84397d62005-07-10 14:46:12 +10001057 drm_buf_entry_t *entry;
1058 drm_buf_t *buf;
1059 unsigned long offset;
1060 unsigned long agp_offset;
1061 int count;
1062 int order;
1063 int size;
1064 int alignment;
1065 int page_order;
1066 int total;
1067 int byte_count;
1068 int i;
1069 drm_buf_t **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001070
1071 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1072 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001073
Dave Airlieb84397d62005-07-10 14:46:12 +10001074 if (!dma)
1075 return -EINVAL;
1076
Dave Airlied985c102006-01-02 21:32:48 +11001077 if (!capable(CAP_SYS_ADMIN))
1078 return -EPERM;
1079
Dave Airlied59431b2005-07-10 15:00:06 +10001080 count = request->count;
1081 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001082 size = 1 << order;
1083
Dave Airlied59431b2005-07-10 15:00:06 +10001084 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001085 ? PAGE_ALIGN(size) : size;
1086 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1087 total = PAGE_SIZE << page_order;
1088
1089 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001090 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001091
1092 DRM_DEBUG("count: %d\n", count);
1093 DRM_DEBUG("order: %d\n", order);
1094 DRM_DEBUG("size: %d\n", size);
1095 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1096 DRM_DEBUG("alignment: %d\n", alignment);
1097 DRM_DEBUG("page_order: %d\n", page_order);
1098 DRM_DEBUG("total: %d\n", total);
1099
1100 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1101 return -EINVAL;
1102 if (dev->queue_count)
1103 return -EBUSY; /* Not while in use */
1104
1105 spin_lock(&dev->count_lock);
1106 if (dev->buf_use) {
1107 spin_unlock(&dev->count_lock);
1108 return -EBUSY;
1109 }
1110 atomic_inc(&dev->buf_alloc);
1111 spin_unlock(&dev->count_lock);
1112
Dave Airlie30e2fb12006-02-02 19:37:46 +11001113 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001114 entry = &dma->bufs[order];
1115 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001116 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001117 atomic_dec(&dev->buf_alloc);
1118 return -ENOMEM; /* May only call once for each order */
1119 }
1120
1121 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001122 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001123 atomic_dec(&dev->buf_alloc);
1124 return -EINVAL;
1125 }
1126
1127 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1128 DRM_MEM_BUFS);
1129 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001130 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001131 atomic_dec(&dev->buf_alloc);
1132 return -ENOMEM;
1133 }
1134 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1135
1136 entry->buf_size = size;
1137 entry->page_order = page_order;
1138
1139 offset = 0;
1140
1141 while (entry->buf_count < count) {
1142 buf = &entry->buflist[entry->buf_count];
1143 buf->idx = dma->buf_count + entry->buf_count;
1144 buf->total = alignment;
1145 buf->order = order;
1146 buf->used = 0;
1147
1148 buf->offset = (dma->byte_count + offset);
1149 buf->bus_address = agp_offset + offset;
1150 buf->address = (void *)(agp_offset + offset);
1151 buf->next = NULL;
1152 buf->waiting = 0;
1153 buf->pending = 0;
1154 init_waitqueue_head(&buf->dma_wait);
1155 buf->filp = NULL;
1156
1157 buf->dev_priv_size = dev->driver->dev_priv_size;
1158 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1159 if (!buf->dev_private) {
1160 /* Set count correctly so we free the proper amount. */
1161 entry->buf_count = count;
1162 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001163 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001164 atomic_dec(&dev->buf_alloc);
1165 return -ENOMEM;
1166 }
1167 memset(buf->dev_private, 0, buf->dev_priv_size);
1168
1169 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1170
1171 offset += alignment;
1172 entry->buf_count++;
1173 byte_count += PAGE_SIZE << page_order;
1174 }
1175
1176 DRM_DEBUG("byte_count: %d\n", byte_count);
1177
1178 temp_buflist = drm_realloc(dma->buflist,
1179 dma->buf_count * sizeof(*dma->buflist),
1180 (dma->buf_count + entry->buf_count)
1181 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1182 if (!temp_buflist) {
1183 /* Free the entry because it isn't valid */
1184 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001185 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001186 atomic_dec(&dev->buf_alloc);
1187 return -ENOMEM;
1188 }
1189 dma->buflist = temp_buflist;
1190
1191 for (i = 0; i < entry->buf_count; i++) {
1192 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1193 }
1194
1195 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001196 dma->seg_count += entry->seg_count;
1197 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001198 dma->byte_count += byte_count;
1199
1200 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1201 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1202
Dave Airlie30e2fb12006-02-02 19:37:46 +11001203 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001204
Dave Airlied59431b2005-07-10 15:00:06 +10001205 request->count = entry->buf_count;
1206 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001207
1208 dma->flags = _DRM_DMA_USE_FB;
1209
1210 atomic_dec(&dev->buf_alloc);
1211 return 0;
1212}
Dave Airlied985c102006-01-02 21:32:48 +11001213
Dave Airlieb84397d62005-07-10 14:46:12 +10001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/**
1216 * Add buffers for DMA transfers (ioctl).
1217 *
1218 * \param inode device inode.
1219 * \param filp file pointer.
1220 * \param cmd command.
1221 * \param arg pointer to a drm_buf_desc_t request.
1222 * \return zero on success or a negative number on failure.
1223 *
1224 * According with the memory type specified in drm_buf_desc::flags and the
1225 * build options, it dispatches the call either to addbufs_agp(),
1226 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1227 * PCI memory respectively.
1228 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001229int drm_addbufs(struct inode *inode, struct file *filp,
1230 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 drm_buf_desc_t request;
1233 drm_file_t *priv = filp->private_data;
1234 drm_device_t *dev = priv->head->dev;
Dave Airlied59431b2005-07-10 15:00:06 +10001235 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1238 return -EINVAL;
1239
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001240 if (copy_from_user(&request, (drm_buf_desc_t __user *) arg,
1241 sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -EFAULT;
1243
1244#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001245 if (request.flags & _DRM_AGP_BUFFER)
1246 ret = drm_addbufs_agp(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 else
1248#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001249 if (request.flags & _DRM_SG_BUFFER)
1250 ret = drm_addbufs_sg(dev, &request);
1251 else if (request.flags & _DRM_FB_BUFFER)
1252 ret = drm_addbufs_fb(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001254 ret = drm_addbufs_pci(dev, &request);
Dave Airlied59431b2005-07-10 15:00:06 +10001255
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001256 if (ret == 0) {
1257 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
Dave Airlied59431b2005-07-10 15:00:06 +10001258 ret = -EFAULT;
1259 }
1260 }
1261 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/**
1265 * Get information about the buffer mappings.
1266 *
1267 * This was originally mean for debugging purposes, or by a sophisticated
1268 * client library to determine how best to use the available buffers (e.g.,
1269 * large buffers can be used for image transfer).
1270 *
1271 * \param inode device inode.
1272 * \param filp file pointer.
1273 * \param cmd command.
1274 * \param arg pointer to a drm_buf_info structure.
1275 * \return zero on success or a negative number on failure.
1276 *
1277 * Increments drm_device::buf_use while holding the drm_device::count_lock
1278 * lock, preventing of allocating more buffers after this call. Information
1279 * about each requested buffer is then copied into user space.
1280 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001281int drm_infobufs(struct inode *inode, struct file *filp,
1282 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 drm_file_t *priv = filp->private_data;
1285 drm_device_t *dev = priv->head->dev;
1286 drm_device_dma_t *dma = dev->dma;
1287 drm_buf_info_t request;
1288 drm_buf_info_t __user *argp = (void __user *)arg;
1289 int i;
1290 int count;
1291
1292 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1293 return -EINVAL;
1294
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001295 if (!dma)
1296 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001298 spin_lock(&dev->count_lock);
1299 if (atomic_read(&dev->buf_alloc)) {
1300 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return -EBUSY;
1302 }
1303 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001304 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001306 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EFAULT;
1308
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001309 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1310 if (dma->bufs[i].buf_count)
1311 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001314 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001316 if (request.count >= count) {
1317 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1318 if (dma->bufs[i].buf_count) {
1319 drm_buf_desc_t __user *to =
1320 &request.list[count];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 drm_buf_entry_t *from = &dma->bufs[i];
1322 drm_freelist_t *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001323 if (copy_to_user(&to->count,
1324 &from->buf_count,
1325 sizeof(from->buf_count)) ||
1326 copy_to_user(&to->size,
1327 &from->buf_size,
1328 sizeof(from->buf_size)) ||
1329 copy_to_user(&to->low_mark,
1330 &list->low_mark,
1331 sizeof(list->low_mark)) ||
1332 copy_to_user(&to->high_mark,
1333 &list->high_mark,
1334 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return -EFAULT;
1336
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001337 DRM_DEBUG("%d %d %d %d %d\n",
1338 i,
1339 dma->bufs[i].buf_count,
1340 dma->bufs[i].buf_size,
1341 dma->bufs[i].freelist.low_mark,
1342 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 ++count;
1344 }
1345 }
1346 }
1347 request.count = count;
1348
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001349 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return -EFAULT;
1351
1352 return 0;
1353}
1354
1355/**
1356 * Specifies a low and high water mark for buffer allocation
1357 *
1358 * \param inode device inode.
1359 * \param filp file pointer.
1360 * \param cmd command.
1361 * \param arg a pointer to a drm_buf_desc structure.
1362 * \return zero on success or a negative number on failure.
1363 *
1364 * Verifies that the size order is bounded between the admissible orders and
1365 * updates the respective drm_device_dma::bufs entry low and high water mark.
1366 *
1367 * \note This ioctl is deprecated and mostly never used.
1368 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001369int drm_markbufs(struct inode *inode, struct file *filp,
1370 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 drm_file_t *priv = filp->private_data;
1373 drm_device_t *dev = priv->head->dev;
1374 drm_device_dma_t *dma = dev->dma;
1375 drm_buf_desc_t request;
1376 int order;
1377 drm_buf_entry_t *entry;
1378
1379 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1380 return -EINVAL;
1381
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001382 if (!dma)
1383 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001385 if (copy_from_user(&request,
1386 (drm_buf_desc_t __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EFAULT;
1388
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001389 DRM_DEBUG("%d, %d, %d\n",
1390 request.size, request.low_mark, request.high_mark);
1391 order = drm_order(request.size);
1392 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1393 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 entry = &dma->bufs[order];
1395
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001396 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001398 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return -EINVAL;
1400
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001401 entry->freelist.low_mark = request.low_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 entry->freelist.high_mark = request.high_mark;
1403
1404 return 0;
1405}
1406
1407/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 *
1410 * \param inode device inode.
1411 * \param filp file pointer.
1412 * \param cmd command.
1413 * \param arg pointer to a drm_buf_free structure.
1414 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001415 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * Calls free_buffer() for each used buffer.
1417 * This function is primarily used for debugging.
1418 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001419int drm_freebufs(struct inode *inode, struct file *filp,
1420 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 drm_file_t *priv = filp->private_data;
1423 drm_device_t *dev = priv->head->dev;
1424 drm_device_dma_t *dma = dev->dma;
1425 drm_buf_free_t request;
1426 int i;
1427 int idx;
1428 drm_buf_t *buf;
1429
1430 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1431 return -EINVAL;
1432
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001433 if (!dma)
1434 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001436 if (copy_from_user(&request,
1437 (drm_buf_free_t __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return -EFAULT;
1439
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001440 DRM_DEBUG("%d\n", request.count);
1441 for (i = 0; i < request.count; i++) {
1442 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001444 if (idx < 0 || idx >= dma->buf_count) {
1445 DRM_ERROR("Index %d (of %d max)\n",
1446 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return -EINVAL;
1448 }
1449 buf = dma->buflist[idx];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001450 if (buf->filp != filp) {
1451 DRM_ERROR("Process %d freeing buffer not owned\n",
1452 current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return -EINVAL;
1454 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001455 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457
1458 return 0;
1459}
1460
1461/**
1462 * Maps all of the DMA buffers into client-virtual space (ioctl).
1463 *
1464 * \param inode device inode.
1465 * \param filp file pointer.
1466 * \param cmd command.
1467 * \param arg pointer to a drm_buf_map structure.
1468 * \return zero on success or a negative number on failure.
1469 *
1470 * Maps the AGP or SG buffer region with do_mmap(), and copies information
1471 * about each buffer into user space. The PCI buffers are already mapped on the
1472 * addbufs_pci() call.
1473 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001474int drm_mapbufs(struct inode *inode, struct file *filp,
1475 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 drm_file_t *priv = filp->private_data;
1478 drm_device_t *dev = priv->head->dev;
1479 drm_device_dma_t *dma = dev->dma;
1480 drm_buf_map_t __user *argp = (void __user *)arg;
1481 int retcode = 0;
1482 const int zero = 0;
1483 unsigned long virtual;
1484 unsigned long address;
1485 drm_buf_map_t request;
1486 int i;
1487
1488 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1489 return -EINVAL;
1490
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001491 if (!dma)
1492 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 spin_lock(&dev->count_lock);
1495 if (atomic_read(&dev->buf_alloc)) {
1496 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return -EBUSY;
1498 }
1499 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001500 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001502 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return -EFAULT;
1504
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001505 if (request.count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001506 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001507 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001508 && (dma->flags & _DRM_DMA_USE_SG))
1509 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1510 && (dma->flags & _DRM_DMA_USE_FB))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 drm_map_t *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001512 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001514 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 retcode = -EINVAL;
1516 goto done;
1517 }
1518
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001519 down_write(&current->mm->mmap_sem);
1520 virtual = do_mmap(filp, 0, map->size,
1521 PROT_READ | PROT_WRITE,
1522 MAP_SHARED, token);
1523 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001525 down_write(&current->mm->mmap_sem);
1526 virtual = do_mmap(filp, 0, dma->byte_count,
1527 PROT_READ | PROT_WRITE,
1528 MAP_SHARED, 0);
1529 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001531 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /* Real error */
1533 retcode = (signed long)virtual;
1534 goto done;
1535 }
1536 request.virtual = (void __user *)virtual;
1537
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001538 for (i = 0; i < dma->buf_count; i++) {
1539 if (copy_to_user(&request.list[i].idx,
1540 &dma->buflist[i]->idx,
1541 sizeof(request.list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 retcode = -EFAULT;
1543 goto done;
1544 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001545 if (copy_to_user(&request.list[i].total,
1546 &dma->buflist[i]->total,
1547 sizeof(request.list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 retcode = -EFAULT;
1549 goto done;
1550 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001551 if (copy_to_user(&request.list[i].used,
1552 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 retcode = -EFAULT;
1554 goto done;
1555 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001556 address = virtual + dma->buflist[i]->offset; /* *** */
1557 if (copy_to_user(&request.list[i].address,
1558 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 retcode = -EFAULT;
1560 goto done;
1561 }
1562 }
1563 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001564 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 request.count = dma->buf_count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001566 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001568 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return -EFAULT;
1570
1571 return retcode;
1572}
1573
Dave Airlie836cf042005-07-10 19:27:04 +10001574/**
1575 * Compute size order. Returns the exponent of the smaller power of two which
1576 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001577 *
Dave Airlie836cf042005-07-10 19:27:04 +10001578 * \param size size.
1579 * \return order.
1580 *
1581 * \todo Can be made faster.
1582 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001583int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001584{
1585 int order;
1586 unsigned long tmp;
1587
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001588 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001589
1590 if (size & (size - 1))
1591 ++order;
1592
1593 return order;
1594}
1595EXPORT_SYMBOL(drm_order);
Dave Airlied985c102006-01-02 21:32:48 +11001596
1597