blob: 8a9cf12e6183caba73a62d887e522d0a10e0afbd [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 +1000389EXPORT_SYMBOL(drm_rmmap_locked);
390
Dave Airlied985c102006-01-02 21:32:48 +1100391int drm_rmmap(drm_device_t *dev, drm_local_map_t *map)
Dave Airlie836cf042005-07-10 19:27:04 +1000392{
393 int ret;
394
Dave Airlie30e2fb12006-02-02 19:37:46 +1100395 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000396 ret = drm_rmmap_locked(dev, map);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100397 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000398
399 return ret;
400}
Dave Airlie7ab98402005-07-10 16:56:52 +1000401EXPORT_SYMBOL(drm_rmmap);
402
Dave Airlie836cf042005-07-10 19:27:04 +1000403/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
404 * the last close of the device, and this is necessary for cleanup when things
405 * exit uncleanly. Therefore, having userland manually remove mappings seems
406 * like a pointless exercise since they're going away anyway.
407 *
408 * One use case might be after addmap is allowed for normal users for SHM and
409 * gets used by drivers that the server doesn't need to care about. This seems
410 * unlikely.
411 */
Dave Airlie7ab98402005-07-10 16:56:52 +1000412int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
413 unsigned int cmd, unsigned long arg)
414{
415 drm_file_t *priv = filp->private_data;
416 drm_device_t *dev = priv->head->dev;
417 drm_map_t request;
Dave Airlie836cf042005-07-10 19:27:04 +1000418 drm_local_map_t *map = NULL;
419 struct list_head *list;
420 int ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000421
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {
Dave Airlie7ab98402005-07-10 16:56:52 +1000423 return -EFAULT;
424 }
425
Dave Airlie30e2fb12006-02-02 19:37:46 +1100426 mutex_lock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000427 list_for_each(list, &dev->maplist->head) {
428 drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
429
430 if (r_list->map &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 r_list->user_token == (unsigned long)request.handle &&
Dave Airlie836cf042005-07-10 19:27:04 +1000432 r_list->map->flags & _DRM_REMOVABLE) {
433 map = r_list->map;
434 break;
435 }
436 }
437
438 /* List has wrapped around to the head pointer, or its empty we didn't
439 * find anything.
440 */
441 if (list == (&dev->maplist->head)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100442 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000443 return -EINVAL;
444 }
445
446 if (!map)
447 return -EINVAL;
448
449 /* Register and framebuffer maps are permanent */
450 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100451 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000452 return 0;
453 }
454
455 ret = drm_rmmap_locked(dev, map);
456
Dave Airlie30e2fb12006-02-02 19:37:46 +1100457 mutex_unlock(&dev->struct_mutex);
Dave Airlie836cf042005-07-10 19:27:04 +1000458
459 return ret;
Dave Airlie7ab98402005-07-10 16:56:52 +1000460}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462/**
463 * Cleanup after an error on one of the addbufs() functions.
464 *
Dave Airlie836cf042005-07-10 19:27:04 +1000465 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * \param entry buffer entry where the error occurred.
467 *
468 * Frees any pages and buffers associated with the given entry.
469 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000470static void drm_cleanup_buf_error(drm_device_t * dev, drm_buf_entry_t * entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int i;
473
474 if (entry->seg_count) {
475 for (i = 0; i < entry->seg_count; i++) {
476 if (entry->seglist[i]) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100477 drm_pci_free(dev, entry->seglist[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 }
480 drm_free(entry->seglist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 entry->seg_count *
482 sizeof(*entry->seglist), DRM_MEM_SEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 entry->seg_count = 0;
485 }
486
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000487 if (entry->buf_count) {
488 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (entry->buflist[i].dev_private) {
490 drm_free(entry->buflist[i].dev_private,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000491 entry->buflist[i].dev_priv_size,
492 DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 }
495 drm_free(entry->buflist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000496 entry->buf_count *
497 sizeof(*entry->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 entry->buf_count = 0;
500 }
501}
502
503#if __OS_HAS_AGP
504/**
Dave Airlied59431b2005-07-10 15:00:06 +1000505 * Add AGP buffers for DMA transfers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *
Dave Airlied59431b2005-07-10 15:00:06 +1000507 * \param dev drm_device_t to which the buffers are to be added.
508 * \param request pointer to a drm_buf_desc_t describing the request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000510 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * After some sanity checks creates a drm_buf structure for each buffer and
512 * reallocates the buffer list of the same size order to accommodate the new
513 * buffers.
514 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000515int drm_addbufs_agp(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 drm_buf_entry_t *entry;
519 drm_buf_t *buf;
520 unsigned long offset;
521 unsigned long agp_offset;
522 int count;
523 int order;
524 int size;
525 int alignment;
526 int page_order;
527 int total;
528 int byte_count;
529 int i;
530 drm_buf_t **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 if (!dma)
533 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Dave Airlied59431b2005-07-10 15:00:06 +1000535 count = request->count;
536 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 size = 1 << order;
538
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000539 alignment = (request->flags & _DRM_PAGE_ALIGN)
540 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
542 total = PAGE_SIZE << page_order;
543
544 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000545 agp_offset = dev->agp->base + request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000547 DRM_DEBUG("count: %d\n", count);
548 DRM_DEBUG("order: %d\n", order);
549 DRM_DEBUG("size: %d\n", size);
Dave Airlied985c102006-01-02 21:32:48 +1100550 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 DRM_DEBUG("alignment: %d\n", alignment);
552 DRM_DEBUG("page_order: %d\n", page_order);
553 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000555 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
556 return -EINVAL;
557 if (dev->queue_count)
558 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000560 spin_lock(&dev->count_lock);
561 if (dev->buf_use) {
562 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -EBUSY;
564 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000565 atomic_inc(&dev->buf_alloc);
566 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Dave Airlie30e2fb12006-02-02 19:37:46 +1100568 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100571 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 atomic_dec(&dev->buf_alloc);
573 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
576 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100577 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return -EINVAL;
580 }
581
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
583 DRM_MEM_BUFS);
584 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100585 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return -ENOMEM;
588 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 entry->buf_size = size;
592 entry->page_order = page_order;
593
594 offset = 0;
595
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 while (entry->buf_count < count) {
597 buf = &entry->buflist[entry->buf_count];
598 buf->idx = dma->buf_count + entry->buf_count;
599 buf->total = alignment;
600 buf->order = order;
601 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000603 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 buf->bus_address = agp_offset + offset;
605 buf->address = (void *)(agp_offset + offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 buf->waiting = 0;
608 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000609 init_waitqueue_head(&buf->dma_wait);
610 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
614 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* Set count correctly so we free the proper amount. */
616 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100618 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -ENOMEM;
621 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000622 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000624 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 offset += alignment;
627 entry->buf_count++;
628 byte_count += PAGE_SIZE << page_order;
629 }
630
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000631 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000633 temp_buflist = drm_realloc(dma->buflist,
634 dma->buf_count * sizeof(*dma->buflist),
635 (dma->buf_count + entry->buf_count)
636 * sizeof(*dma->buflist), DRM_MEM_BUFS);
637 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000639 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100640 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000641 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return -ENOMEM;
643 }
644 dma->buflist = temp_buflist;
645
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000646 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
648 }
649
650 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +1100651 dma->seg_count += entry->seg_count;
652 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 dma->byte_count += byte_count;
654
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000655 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
656 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Dave Airlie30e2fb12006-02-02 19:37:46 +1100658 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Dave Airlied59431b2005-07-10 15:00:06 +1000660 request->count = entry->buf_count;
661 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 dma->flags = _DRM_DMA_USE_AGP;
664
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000668EXPORT_SYMBOL(drm_addbufs_agp);
669#endif /* __OS_HAS_AGP */
670
671int drm_addbufs_pci(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int count;
675 int order;
676 int size;
677 int total;
678 int page_order;
679 drm_buf_entry_t *entry;
Dave Airlieddf19b92006-03-19 18:56:12 +1100680 drm_dma_handle_t *dmah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 drm_buf_t *buf;
682 int alignment;
683 unsigned long offset;
684 int i;
685 int byte_count;
686 int page_count;
687 unsigned long *temp_pagelist;
688 drm_buf_t **temp_buflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
691 return -EINVAL;
Dave Airlied985c102006-01-02 21:32:48 +1100692
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000693 if (!dma)
694 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Dave Airlied985c102006-01-02 21:32:48 +1100696 if (!capable(CAP_SYS_ADMIN))
697 return -EPERM;
698
Dave Airlied59431b2005-07-10 15:00:06 +1000699 count = request->count;
700 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 size = 1 << order;
702
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
704 request->count, request->size, size, order, dev->queue_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000706 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
707 return -EINVAL;
708 if (dev->queue_count)
709 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dave Airlied59431b2005-07-10 15:00:06 +1000711 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
714 total = PAGE_SIZE << page_order;
715
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 spin_lock(&dev->count_lock);
717 if (dev->buf_use) {
718 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return -EBUSY;
720 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 atomic_inc(&dev->buf_alloc);
722 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Dave Airlie30e2fb12006-02-02 19:37:46 +1100724 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000726 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100727 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000728 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return -ENOMEM; /* May only call once for each order */
730 }
731
732 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100733 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EINVAL;
736 }
737
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
739 DRM_MEM_BUFS);
740 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100741 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return -ENOMEM;
744 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000745 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
748 DRM_MEM_SEGS);
749 if (!entry->seglist) {
750 drm_free(entry->buflist,
751 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100752 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000753 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return -ENOMEM;
755 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 /* Keep the original pagelist until we know all the allocations
759 * have succeeded
760 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
762 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (!temp_pagelist) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000764 drm_free(entry->buflist,
765 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
766 drm_free(entry->seglist,
767 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100768 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000769 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -ENOMEM;
771 }
772 memcpy(temp_pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000773 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
774 DRM_DEBUG("pagelist: %d entries\n",
775 dma->page_count + (count << page_order));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000777 entry->buf_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 entry->page_order = page_order;
779 byte_count = 0;
780 page_count = 0;
781
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000782 while (entry->buf_count < count) {
Dave Airlieddf19b92006-03-19 18:56:12 +1100783
784 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
785
786 if (!dmah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* Set count correctly so we free the proper amount. */
788 entry->buf_count = count;
789 entry->seg_count = count;
790 drm_cleanup_buf_error(dev, entry);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 drm_free(temp_pagelist,
792 (dma->page_count + (count << page_order))
793 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100794 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -ENOMEM;
797 }
Dave Airlieddf19b92006-03-19 18:56:12 +1100798 entry->seglist[entry->seg_count++] = dmah;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000799 for (i = 0; i < (1 << page_order); i++) {
800 DRM_DEBUG("page %d @ 0x%08lx\n",
801 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 temp_pagelist[dma->page_count + page_count++]
Dave Airlieddf19b92006-03-19 18:56:12 +1100804 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 for (offset = 0;
807 offset + size <= total && entry->buf_count < count;
808 offset += alignment, ++entry->buf_count) {
809 buf = &entry->buflist[entry->buf_count];
810 buf->idx = dma->buf_count + entry->buf_count;
811 buf->total = alignment;
812 buf->order = order;
813 buf->used = 0;
814 buf->offset = (dma->byte_count + byte_count + offset);
Dave Airlieddf19b92006-03-19 18:56:12 +1100815 buf->address = (void *)(dmah->vaddr + offset);
816 buf->bus_address = dmah->busaddr + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 buf->waiting = 0;
819 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000820 init_waitqueue_head(&buf->dma_wait);
821 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 buf->dev_private = drm_alloc(buf->dev_priv_size,
825 DRM_MEM_BUFS);
826 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 /* Set count correctly so we free the proper amount. */
828 entry->buf_count = count;
829 entry->seg_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 drm_cleanup_buf_error(dev, entry);
831 drm_free(temp_pagelist,
832 (dma->page_count +
833 (count << page_order))
834 * sizeof(*dma->pagelist),
835 DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100836 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return -ENOMEM;
839 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 DRM_DEBUG("buffer %d @ %p\n",
843 entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 byte_count += PAGE_SIZE << page_order;
846 }
847
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000848 temp_buflist = drm_realloc(dma->buflist,
849 dma->buf_count * sizeof(*dma->buflist),
850 (dma->buf_count + entry->buf_count)
851 * sizeof(*dma->buflist), DRM_MEM_BUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (!temp_buflist) {
853 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000854 drm_cleanup_buf_error(dev, entry);
855 drm_free(temp_pagelist,
856 (dma->page_count + (count << page_order))
857 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
Dave Airlie30e2fb12006-02-02 19:37:46 +1100858 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return -ENOMEM;
861 }
862 dma->buflist = temp_buflist;
863
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000864 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
866 }
867
868 /* No allocations failed, so now we can replace the orginal pagelist
869 * with the new one.
870 */
871 if (dma->page_count) {
872 drm_free(dma->pagelist,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000873 dma->page_count * sizeof(*dma->pagelist),
874 DRM_MEM_PAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 dma->pagelist = temp_pagelist;
877
878 dma->buf_count += entry->buf_count;
879 dma->seg_count += entry->seg_count;
880 dma->page_count += entry->seg_count << page_order;
881 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
882
Dave Airlie30e2fb12006-02-02 19:37:46 +1100883 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Dave Airlied59431b2005-07-10 15:00:06 +1000885 request->count = entry->buf_count;
886 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000888 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return 0;
890
891}
Dave Airlied84f76d2005-07-10 17:04:22 +1000892EXPORT_SYMBOL(drm_addbufs_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000894static int drm_addbufs_sg(drm_device_t * dev, drm_buf_desc_t * request)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 drm_device_dma_t *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 drm_buf_entry_t *entry;
898 drm_buf_t *buf;
899 unsigned long offset;
900 unsigned long agp_offset;
901 int count;
902 int order;
903 int size;
904 int alignment;
905 int page_order;
906 int total;
907 int byte_count;
908 int i;
909 drm_buf_t **temp_buflist;
910
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000911 if (!drm_core_check_feature(dev, DRIVER_SG))
912 return -EINVAL;
913
914 if (!dma)
915 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Dave Airlied985c102006-01-02 21:32:48 +1100917 if (!capable(CAP_SYS_ADMIN))
918 return -EPERM;
919
Dave Airlied59431b2005-07-10 15:00:06 +1000920 count = request->count;
921 order = drm_order(request->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 size = 1 << order;
923
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000924 alignment = (request->flags & _DRM_PAGE_ALIGN)
925 ? PAGE_ALIGN(size) : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
927 total = PAGE_SIZE << page_order;
928
929 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +1000930 agp_offset = request->agp_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000932 DRM_DEBUG("count: %d\n", count);
933 DRM_DEBUG("order: %d\n", order);
934 DRM_DEBUG("size: %d\n", size);
935 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
936 DRM_DEBUG("alignment: %d\n", alignment);
937 DRM_DEBUG("page_order: %d\n", page_order);
938 DRM_DEBUG("total: %d\n", total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000940 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
941 return -EINVAL;
942 if (dev->queue_count)
943 return -EBUSY; /* Not while in use */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000945 spin_lock(&dev->count_lock);
946 if (dev->buf_use) {
947 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return -EBUSY;
949 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000950 atomic_inc(&dev->buf_alloc);
951 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Dave Airlie30e2fb12006-02-02 19:37:46 +1100953 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 entry = &dma->bufs[order];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100956 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000957 atomic_dec(&dev->buf_alloc);
958 return -ENOMEM; /* May only call once for each order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
961 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100962 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000963 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -EINVAL;
965 }
966
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000967 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
968 DRM_MEM_BUFS);
969 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100970 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000971 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return -ENOMEM;
973 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 entry->buf_size = size;
977 entry->page_order = page_order;
978
979 offset = 0;
980
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000981 while (entry->buf_count < count) {
982 buf = &entry->buflist[entry->buf_count];
983 buf->idx = dma->buf_count + entry->buf_count;
984 buf->total = alignment;
985 buf->order = order;
986 buf->used = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000988 buf->offset = (dma->byte_count + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 buf->bus_address = agp_offset + offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000990 buf->address = (void *)(agp_offset + offset
Dave Airlied1f2b552005-08-05 22:11:22 +1000991 + (unsigned long)dev->sg->virtual);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 buf->next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 buf->waiting = 0;
994 buf->pending = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000995 init_waitqueue_head(&buf->dma_wait);
996 buf->filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 buf->dev_priv_size = dev->driver->dev_priv_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1000 if (!buf->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /* Set count correctly so we free the proper amount. */
1002 entry->buf_count = count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001003 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001004 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return -ENOMEM;
1007 }
1008
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009 memset(buf->dev_private, 0, buf->dev_priv_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001011 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 offset += alignment;
1014 entry->buf_count++;
1015 byte_count += PAGE_SIZE << page_order;
1016 }
1017
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001018 DRM_DEBUG("byte_count: %d\n", byte_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001020 temp_buflist = drm_realloc(dma->buflist,
1021 dma->buf_count * sizeof(*dma->buflist),
1022 (dma->buf_count + entry->buf_count)
1023 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1024 if (!temp_buflist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /* Free the entry because it isn't valid */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001026 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001027 mutex_unlock(&dev->struct_mutex);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001028 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return -ENOMEM;
1030 }
1031 dma->buflist = temp_buflist;
1032
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 for (i = 0; i < entry->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1035 }
1036
1037 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001038 dma->seg_count += entry->seg_count;
1039 dma->page_count += byte_count >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 dma->byte_count += byte_count;
1041
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1043 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Dave Airlie30e2fb12006-02-02 19:37:46 +11001045 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Dave Airlied59431b2005-07-10 15:00:06 +10001047 request->count = entry->buf_count;
1048 request->size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 dma->flags = _DRM_DMA_USE_SG;
1051
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 atomic_dec(&dev->buf_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
1054}
1055
Dave Airlied985c102006-01-02 21:32:48 +11001056int drm_addbufs_fb(drm_device_t * dev, drm_buf_desc_t * request)
Dave Airlieb84397d62005-07-10 14:46:12 +10001057{
Dave Airlieb84397d62005-07-10 14:46:12 +10001058 drm_device_dma_t *dma = dev->dma;
Dave Airlieb84397d62005-07-10 14:46:12 +10001059 drm_buf_entry_t *entry;
1060 drm_buf_t *buf;
1061 unsigned long offset;
1062 unsigned long agp_offset;
1063 int count;
1064 int order;
1065 int size;
1066 int alignment;
1067 int page_order;
1068 int total;
1069 int byte_count;
1070 int i;
1071 drm_buf_t **temp_buflist;
Dave Airlieb84397d62005-07-10 14:46:12 +10001072
1073 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1074 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001075
Dave Airlieb84397d62005-07-10 14:46:12 +10001076 if (!dma)
1077 return -EINVAL;
1078
Dave Airlied985c102006-01-02 21:32:48 +11001079 if (!capable(CAP_SYS_ADMIN))
1080 return -EPERM;
1081
Dave Airlied59431b2005-07-10 15:00:06 +10001082 count = request->count;
1083 order = drm_order(request->size);
Dave Airlieb84397d62005-07-10 14:46:12 +10001084 size = 1 << order;
1085
Dave Airlied59431b2005-07-10 15:00:06 +10001086 alignment = (request->flags & _DRM_PAGE_ALIGN)
Dave Airlieb84397d62005-07-10 14:46:12 +10001087 ? PAGE_ALIGN(size) : size;
1088 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1089 total = PAGE_SIZE << page_order;
1090
1091 byte_count = 0;
Dave Airlied59431b2005-07-10 15:00:06 +10001092 agp_offset = request->agp_start;
Dave Airlieb84397d62005-07-10 14:46:12 +10001093
1094 DRM_DEBUG("count: %d\n", count);
1095 DRM_DEBUG("order: %d\n", order);
1096 DRM_DEBUG("size: %d\n", size);
1097 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1098 DRM_DEBUG("alignment: %d\n", alignment);
1099 DRM_DEBUG("page_order: %d\n", page_order);
1100 DRM_DEBUG("total: %d\n", total);
1101
1102 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1103 return -EINVAL;
1104 if (dev->queue_count)
1105 return -EBUSY; /* Not while in use */
1106
1107 spin_lock(&dev->count_lock);
1108 if (dev->buf_use) {
1109 spin_unlock(&dev->count_lock);
1110 return -EBUSY;
1111 }
1112 atomic_inc(&dev->buf_alloc);
1113 spin_unlock(&dev->count_lock);
1114
Dave Airlie30e2fb12006-02-02 19:37:46 +11001115 mutex_lock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001116 entry = &dma->bufs[order];
1117 if (entry->buf_count) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001118 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001119 atomic_dec(&dev->buf_alloc);
1120 return -ENOMEM; /* May only call once for each order */
1121 }
1122
1123 if (count < 0 || count > 4096) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001124 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001125 atomic_dec(&dev->buf_alloc);
1126 return -EINVAL;
1127 }
1128
1129 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1130 DRM_MEM_BUFS);
1131 if (!entry->buflist) {
Dave Airlie30e2fb12006-02-02 19:37:46 +11001132 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001133 atomic_dec(&dev->buf_alloc);
1134 return -ENOMEM;
1135 }
1136 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1137
1138 entry->buf_size = size;
1139 entry->page_order = page_order;
1140
1141 offset = 0;
1142
1143 while (entry->buf_count < count) {
1144 buf = &entry->buflist[entry->buf_count];
1145 buf->idx = dma->buf_count + entry->buf_count;
1146 buf->total = alignment;
1147 buf->order = order;
1148 buf->used = 0;
1149
1150 buf->offset = (dma->byte_count + offset);
1151 buf->bus_address = agp_offset + offset;
1152 buf->address = (void *)(agp_offset + offset);
1153 buf->next = NULL;
1154 buf->waiting = 0;
1155 buf->pending = 0;
1156 init_waitqueue_head(&buf->dma_wait);
1157 buf->filp = NULL;
1158
1159 buf->dev_priv_size = dev->driver->dev_priv_size;
1160 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1161 if (!buf->dev_private) {
1162 /* Set count correctly so we free the proper amount. */
1163 entry->buf_count = count;
1164 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001165 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001166 atomic_dec(&dev->buf_alloc);
1167 return -ENOMEM;
1168 }
1169 memset(buf->dev_private, 0, buf->dev_priv_size);
1170
1171 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1172
1173 offset += alignment;
1174 entry->buf_count++;
1175 byte_count += PAGE_SIZE << page_order;
1176 }
1177
1178 DRM_DEBUG("byte_count: %d\n", byte_count);
1179
1180 temp_buflist = drm_realloc(dma->buflist,
1181 dma->buf_count * sizeof(*dma->buflist),
1182 (dma->buf_count + entry->buf_count)
1183 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1184 if (!temp_buflist) {
1185 /* Free the entry because it isn't valid */
1186 drm_cleanup_buf_error(dev, entry);
Dave Airlie30e2fb12006-02-02 19:37:46 +11001187 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001188 atomic_dec(&dev->buf_alloc);
1189 return -ENOMEM;
1190 }
1191 dma->buflist = temp_buflist;
1192
1193 for (i = 0; i < entry->buf_count; i++) {
1194 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1195 }
1196
1197 dma->buf_count += entry->buf_count;
Dave Airlied985c102006-01-02 21:32:48 +11001198 dma->seg_count += entry->seg_count;
1199 dma->page_count += byte_count >> PAGE_SHIFT;
Dave Airlieb84397d62005-07-10 14:46:12 +10001200 dma->byte_count += byte_count;
1201
1202 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1203 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1204
Dave Airlie30e2fb12006-02-02 19:37:46 +11001205 mutex_unlock(&dev->struct_mutex);
Dave Airlieb84397d62005-07-10 14:46:12 +10001206
Dave Airlied59431b2005-07-10 15:00:06 +10001207 request->count = entry->buf_count;
1208 request->size = size;
Dave Airlieb84397d62005-07-10 14:46:12 +10001209
1210 dma->flags = _DRM_DMA_USE_FB;
1211
1212 atomic_dec(&dev->buf_alloc);
1213 return 0;
1214}
Dave Airlied985c102006-01-02 21:32:48 +11001215EXPORT_SYMBOL(drm_addbufs_fb);
1216
Dave Airlieb84397d62005-07-10 14:46:12 +10001217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/**
1219 * Add buffers for DMA transfers (ioctl).
1220 *
1221 * \param inode device inode.
1222 * \param filp file pointer.
1223 * \param cmd command.
1224 * \param arg pointer to a drm_buf_desc_t request.
1225 * \return zero on success or a negative number on failure.
1226 *
1227 * According with the memory type specified in drm_buf_desc::flags and the
1228 * build options, it dispatches the call either to addbufs_agp(),
1229 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1230 * PCI memory respectively.
1231 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001232int drm_addbufs(struct inode *inode, struct file *filp,
1233 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 drm_buf_desc_t request;
1236 drm_file_t *priv = filp->private_data;
1237 drm_device_t *dev = priv->head->dev;
Dave Airlied59431b2005-07-10 15:00:06 +10001238 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1241 return -EINVAL;
1242
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001243 if (copy_from_user(&request, (drm_buf_desc_t __user *) arg,
1244 sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EFAULT;
1246
1247#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001248 if (request.flags & _DRM_AGP_BUFFER)
1249 ret = drm_addbufs_agp(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 else
1251#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001252 if (request.flags & _DRM_SG_BUFFER)
1253 ret = drm_addbufs_sg(dev, &request);
1254 else if (request.flags & _DRM_FB_BUFFER)
1255 ret = drm_addbufs_fb(dev, &request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 else
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001257 ret = drm_addbufs_pci(dev, &request);
Dave Airlied59431b2005-07-10 15:00:06 +10001258
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001259 if (ret == 0) {
1260 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
Dave Airlied59431b2005-07-10 15:00:06 +10001261 ret = -EFAULT;
1262 }
1263 }
1264 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267/**
1268 * Get information about the buffer mappings.
1269 *
1270 * This was originally mean for debugging purposes, or by a sophisticated
1271 * client library to determine how best to use the available buffers (e.g.,
1272 * large buffers can be used for image transfer).
1273 *
1274 * \param inode device inode.
1275 * \param filp file pointer.
1276 * \param cmd command.
1277 * \param arg pointer to a drm_buf_info structure.
1278 * \return zero on success or a negative number on failure.
1279 *
1280 * Increments drm_device::buf_use while holding the drm_device::count_lock
1281 * lock, preventing of allocating more buffers after this call. Information
1282 * about each requested buffer is then copied into user space.
1283 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001284int drm_infobufs(struct inode *inode, struct file *filp,
1285 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 drm_file_t *priv = filp->private_data;
1288 drm_device_t *dev = priv->head->dev;
1289 drm_device_dma_t *dma = dev->dma;
1290 drm_buf_info_t request;
1291 drm_buf_info_t __user *argp = (void __user *)arg;
1292 int i;
1293 int count;
1294
1295 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1296 return -EINVAL;
1297
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001298 if (!dma)
1299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001301 spin_lock(&dev->count_lock);
1302 if (atomic_read(&dev->buf_alloc)) {
1303 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EBUSY;
1305 }
1306 ++dev->buf_use; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001307 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001309 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return -EFAULT;
1311
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001312 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1313 if (dma->bufs[i].buf_count)
1314 ++count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001317 DRM_DEBUG("count = %d\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001319 if (request.count >= count) {
1320 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1321 if (dma->bufs[i].buf_count) {
1322 drm_buf_desc_t __user *to =
1323 &request.list[count];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 drm_buf_entry_t *from = &dma->bufs[i];
1325 drm_freelist_t *list = &dma->bufs[i].freelist;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001326 if (copy_to_user(&to->count,
1327 &from->buf_count,
1328 sizeof(from->buf_count)) ||
1329 copy_to_user(&to->size,
1330 &from->buf_size,
1331 sizeof(from->buf_size)) ||
1332 copy_to_user(&to->low_mark,
1333 &list->low_mark,
1334 sizeof(list->low_mark)) ||
1335 copy_to_user(&to->high_mark,
1336 &list->high_mark,
1337 sizeof(list->high_mark)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -EFAULT;
1339
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001340 DRM_DEBUG("%d %d %d %d %d\n",
1341 i,
1342 dma->bufs[i].buf_count,
1343 dma->bufs[i].buf_size,
1344 dma->bufs[i].freelist.low_mark,
1345 dma->bufs[i].freelist.high_mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 ++count;
1347 }
1348 }
1349 }
1350 request.count = count;
1351
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001352 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -EFAULT;
1354
1355 return 0;
1356}
1357
1358/**
1359 * Specifies a low and high water mark for buffer allocation
1360 *
1361 * \param inode device inode.
1362 * \param filp file pointer.
1363 * \param cmd command.
1364 * \param arg a pointer to a drm_buf_desc structure.
1365 * \return zero on success or a negative number on failure.
1366 *
1367 * Verifies that the size order is bounded between the admissible orders and
1368 * updates the respective drm_device_dma::bufs entry low and high water mark.
1369 *
1370 * \note This ioctl is deprecated and mostly never used.
1371 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001372int drm_markbufs(struct inode *inode, struct file *filp,
1373 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 drm_file_t *priv = filp->private_data;
1376 drm_device_t *dev = priv->head->dev;
1377 drm_device_dma_t *dma = dev->dma;
1378 drm_buf_desc_t request;
1379 int order;
1380 drm_buf_entry_t *entry;
1381
1382 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1383 return -EINVAL;
1384
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001385 if (!dma)
1386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001388 if (copy_from_user(&request,
1389 (drm_buf_desc_t __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EFAULT;
1391
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001392 DRM_DEBUG("%d, %d, %d\n",
1393 request.size, request.low_mark, request.high_mark);
1394 order = drm_order(request.size);
1395 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1396 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 entry = &dma->bufs[order];
1398
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001399 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001401 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return -EINVAL;
1403
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001404 entry->freelist.low_mark = request.low_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 entry->freelist.high_mark = request.high_mark;
1406
1407 return 0;
1408}
1409
1410/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001411 * Unreserve the buffers in list, previously reserved using drmDMA.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 *
1413 * \param inode device inode.
1414 * \param filp file pointer.
1415 * \param cmd command.
1416 * \param arg pointer to a drm_buf_free structure.
1417 * \return zero on success or a negative number on failure.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001418 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * Calls free_buffer() for each used buffer.
1420 * This function is primarily used for debugging.
1421 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001422int drm_freebufs(struct inode *inode, struct file *filp,
1423 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 drm_file_t *priv = filp->private_data;
1426 drm_device_t *dev = priv->head->dev;
1427 drm_device_dma_t *dma = dev->dma;
1428 drm_buf_free_t request;
1429 int i;
1430 int idx;
1431 drm_buf_t *buf;
1432
1433 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1434 return -EINVAL;
1435
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001436 if (!dma)
1437 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001439 if (copy_from_user(&request,
1440 (drm_buf_free_t __user *) arg, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return -EFAULT;
1442
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001443 DRM_DEBUG("%d\n", request.count);
1444 for (i = 0; i < request.count; i++) {
1445 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001447 if (idx < 0 || idx >= dma->buf_count) {
1448 DRM_ERROR("Index %d (of %d max)\n",
1449 idx, dma->buf_count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return -EINVAL;
1451 }
1452 buf = dma->buflist[idx];
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001453 if (buf->filp != filp) {
1454 DRM_ERROR("Process %d freeing buffer not owned\n",
1455 current->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return -EINVAL;
1457 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001458 drm_free_buffer(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460
1461 return 0;
1462}
1463
1464/**
1465 * Maps all of the DMA buffers into client-virtual space (ioctl).
1466 *
1467 * \param inode device inode.
1468 * \param filp file pointer.
1469 * \param cmd command.
1470 * \param arg pointer to a drm_buf_map structure.
1471 * \return zero on success or a negative number on failure.
1472 *
1473 * Maps the AGP or SG buffer region with do_mmap(), and copies information
1474 * about each buffer into user space. The PCI buffers are already mapped on the
1475 * addbufs_pci() call.
1476 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001477int drm_mapbufs(struct inode *inode, struct file *filp,
1478 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 drm_file_t *priv = filp->private_data;
1481 drm_device_t *dev = priv->head->dev;
1482 drm_device_dma_t *dma = dev->dma;
1483 drm_buf_map_t __user *argp = (void __user *)arg;
1484 int retcode = 0;
1485 const int zero = 0;
1486 unsigned long virtual;
1487 unsigned long address;
1488 drm_buf_map_t request;
1489 int i;
1490
1491 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1492 return -EINVAL;
1493
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 if (!dma)
1495 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001497 spin_lock(&dev->count_lock);
1498 if (atomic_read(&dev->buf_alloc)) {
1499 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return -EBUSY;
1501 }
1502 dev->buf_use++; /* Can't allocate more after this call */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001503 spin_unlock(&dev->count_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001505 if (copy_from_user(&request, argp, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EFAULT;
1507
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001508 if (request.count >= dma->buf_count) {
Dave Airlieb84397d62005-07-10 14:46:12 +10001509 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001510 || (drm_core_check_feature(dev, DRIVER_SG)
Dave Airlieb84397d62005-07-10 14:46:12 +10001511 && (dma->flags & _DRM_DMA_USE_SG))
1512 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1513 && (dma->flags & _DRM_DMA_USE_FB))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 drm_map_t *map = dev->agp_buffer_map;
Dave Airlied1f2b552005-08-05 22:11:22 +10001515 unsigned long token = dev->agp_buffer_token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001517 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 retcode = -EINVAL;
1519 goto done;
1520 }
1521
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001522 down_write(&current->mm->mmap_sem);
1523 virtual = do_mmap(filp, 0, map->size,
1524 PROT_READ | PROT_WRITE,
1525 MAP_SHARED, token);
1526 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001528 down_write(&current->mm->mmap_sem);
1529 virtual = do_mmap(filp, 0, dma->byte_count,
1530 PROT_READ | PROT_WRITE,
1531 MAP_SHARED, 0);
1532 up_write(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001534 if (virtual > -1024UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /* Real error */
1536 retcode = (signed long)virtual;
1537 goto done;
1538 }
1539 request.virtual = (void __user *)virtual;
1540
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001541 for (i = 0; i < dma->buf_count; i++) {
1542 if (copy_to_user(&request.list[i].idx,
1543 &dma->buflist[i]->idx,
1544 sizeof(request.list[0].idx))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 retcode = -EFAULT;
1546 goto done;
1547 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001548 if (copy_to_user(&request.list[i].total,
1549 &dma->buflist[i]->total,
1550 sizeof(request.list[0].total))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 retcode = -EFAULT;
1552 goto done;
1553 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001554 if (copy_to_user(&request.list[i].used,
1555 &zero, sizeof(zero))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 retcode = -EFAULT;
1557 goto done;
1558 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001559 address = virtual + dma->buflist[i]->offset; /* *** */
1560 if (copy_to_user(&request.list[i].address,
1561 &address, sizeof(address))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 retcode = -EFAULT;
1563 goto done;
1564 }
1565 }
1566 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001567 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 request.count = dma->buf_count;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001569 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001571 if (copy_to_user(argp, &request, sizeof(request)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return -EFAULT;
1573
1574 return retcode;
1575}
1576
Dave Airlie836cf042005-07-10 19:27:04 +10001577/**
1578 * Compute size order. Returns the exponent of the smaller power of two which
1579 * is greater or equal to given number.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001580 *
Dave Airlie836cf042005-07-10 19:27:04 +10001581 * \param size size.
1582 * \return order.
1583 *
1584 * \todo Can be made faster.
1585 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001586int drm_order(unsigned long size)
Dave Airlie836cf042005-07-10 19:27:04 +10001587{
1588 int order;
1589 unsigned long tmp;
1590
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001591 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
Dave Airlie836cf042005-07-10 19:27:04 +10001592
1593 if (size & (size - 1))
1594 ++order;
1595
1596 return order;
1597}
1598EXPORT_SYMBOL(drm_order);
Dave Airlied985c102006-01-02 21:32:48 +11001599
1600