drm: small cleanups
[linux-2.6/linux-2.6-openrd.git] / drivers / char / drm / drm_bufs.c
blobf28e70ae66065d4a498760af053d1ea5fa0b6a99
1 /**
2 * \file drm_bufs.h
3 * Generic buffer template
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
9 /*
10 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
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:
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.
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.
36 #include <linux/vmalloc.h>
37 #include "drmP.h"
39 unsigned long drm_get_resource_start(drm_device_t *dev, unsigned int resource)
41 return pci_resource_start(dev->pdev, resource);
43 EXPORT_SYMBOL(drm_get_resource_start);
45 unsigned long drm_get_resource_len(drm_device_t *dev, unsigned int resource)
47 return pci_resource_len(dev->pdev, resource);
49 EXPORT_SYMBOL(drm_get_resource_len);
51 static drm_map_list_t *drm_find_matching_map(drm_device_t *dev,
52 drm_local_map_t *map)
54 struct list_head *list;
56 list_for_each(list, &dev->maplist->head) {
57 drm_map_list_t *entry = list_entry(list, drm_map_list_t, head);
58 if (entry->map && map->type == entry->map->type &&
59 entry->map->offset == map->offset) {
60 return entry;
64 return NULL;
68 * Used to allocate 32-bit handles for mappings.
70 #define START_RANGE 0x10000000
71 #define END_RANGE 0x40000000
73 #ifdef _LP64
74 static __inline__ unsigned int HandleID(unsigned long lhandle, drm_device_t *dev)
76 static unsigned int map32_handle = START_RANGE;
77 unsigned int hash;
79 if (lhandle & 0xffffffff00000000) {
80 hash = map32_handle;
81 map32_handle += PAGE_SIZE;
82 if (map32_handle > END_RANGE)
83 map32_handle = START_RANGE;
84 } else
85 hash = lhandle;
87 while (1) {
88 drm_map_list_t *_entry;
89 list_for_each_entry(_entry, &dev->maplist->head,head) {
90 if (_entry->user_token == hash)
91 break;
93 if (&_entry->head == &dev->maplist->head)
94 return hash;
96 hash += PAGE_SIZE;
97 map32_handle += PAGE_SIZE;
100 #else
101 # define HandleID(x,dev) (unsigned int)(x)
102 #endif
105 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
107 * \param inode device inode.
108 * \param filp file pointer.
109 * \param cmd command.
110 * \param arg pointer to a drm_map structure.
111 * \return zero on success or a negative value on error.
113 * Adjusts the memory offset to its absolute value according to the mapping
114 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
115 * applicable and if supported by the kernel.
117 int drm_addmap_core(drm_device_t * dev, unsigned int offset,
118 unsigned int size, drm_map_type_t type,
119 drm_map_flags_t flags, drm_map_list_t **maplist)
121 drm_map_t *map;
122 drm_map_list_t *list;
123 drm_dma_handle_t *dmah;
125 map = drm_alloc( sizeof(*map), DRM_MEM_MAPS );
126 if ( !map )
127 return -ENOMEM;
129 map->offset = offset;
130 map->size = size;
131 map->flags = flags;
132 map->type = type;
134 /* Only allow shared memory to be removable since we only keep enough
135 * book keeping information about shared memory to allow for removal
136 * when processes fork.
138 if ( (map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM ) {
139 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
140 return -EINVAL;
142 DRM_DEBUG( "offset = 0x%08lx, size = 0x%08lx, type = %d\n",
143 map->offset, map->size, map->type );
144 if ( (map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK)) ) {
145 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
146 return -EINVAL;
148 map->mtrr = -1;
149 map->handle = NULL;
151 switch ( map->type ) {
152 case _DRM_REGISTERS:
153 case _DRM_FRAME_BUFFER:
154 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
155 if ( map->offset + map->size < map->offset ||
156 map->offset < virt_to_phys(high_memory) ) {
157 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
158 return -EINVAL;
160 #endif
161 #ifdef __alpha__
162 map->offset += dev->hose->mem_space->start;
163 #endif
164 /* Some drivers preinitialize some maps, without the X Server
165 * needing to be aware of it. Therefore, we just return success
166 * when the server tries to create a duplicate map.
168 list = drm_find_matching_map(dev, map);
169 if (list != NULL) {
170 if (list->map->size != map->size) {
171 DRM_DEBUG("Matching maps of type %d with "
172 "mismatched sizes, (%ld vs %ld)\n",
173 map->type, map->size, list->map->size);
174 list->map->size = map->size;
177 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
178 *maplist = list;
179 return 0;
182 if (drm_core_has_MTRR(dev)) {
183 if ( map->type == _DRM_FRAME_BUFFER ||
184 (map->flags & _DRM_WRITE_COMBINING) ) {
185 map->mtrr = mtrr_add( map->offset, map->size,
186 MTRR_TYPE_WRCOMB, 1 );
189 if (map->type == _DRM_REGISTERS)
190 map->handle = drm_ioremap( map->offset, map->size,
191 dev );
192 break;
194 case _DRM_SHM:
195 map->handle = vmalloc_32(map->size);
196 DRM_DEBUG( "%lu %d %p\n",
197 map->size, drm_order( map->size ), map->handle );
198 if ( !map->handle ) {
199 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
200 return -ENOMEM;
202 map->offset = (unsigned long)map->handle;
203 if ( map->flags & _DRM_CONTAINS_LOCK ) {
204 /* Prevent a 2nd X Server from creating a 2nd lock */
205 if (dev->lock.hw_lock != NULL) {
206 vfree( map->handle );
207 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
208 return -EBUSY;
210 dev->sigdata.lock =
211 dev->lock.hw_lock = map->handle; /* Pointer to lock */
213 break;
214 case _DRM_AGP:
215 if (drm_core_has_AGP(dev)) {
216 #ifdef __alpha__
217 map->offset += dev->hose->mem_space->start;
218 #endif
219 map->offset += dev->agp->base;
220 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
222 break;
223 case _DRM_SCATTER_GATHER:
224 if (!dev->sg) {
225 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
226 return -EINVAL;
228 map->offset += (unsigned long)dev->sg->virtual;
229 break;
230 case _DRM_CONSISTENT:
231 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
232 * As we're limiting the address to 2^32-1 (or less),
233 * casting it down to 32 bits is no problem, but we
234 * need to point to a 64bit variable first. */
235 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
236 if (!dmah) {
237 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
238 return -ENOMEM;
240 map->handle = dmah->vaddr;
241 map->offset = (unsigned long)dmah->busaddr;
242 kfree(dmah);
243 break;
244 default:
245 drm_free( map, sizeof(*map), DRM_MEM_MAPS );
246 return -EINVAL;
249 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
250 if(!list) {
251 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
252 return -EINVAL;
254 memset(list, 0, sizeof(*list));
255 list->map = map;
257 down(&dev->struct_sem);
258 list_add(&list->head, &dev->maplist->head);
259 /* Assign a 32-bit handle */
260 /* We do it here so that dev->struct_sem protects the increment */
261 list->user_token = HandleID(map->type==_DRM_SHM
262 ? (unsigned long)map->handle
263 : map->offset, dev);
264 up(&dev->struct_sem);
266 *maplist = list;
267 return 0;
270 int drm_addmap(drm_device_t *dev, unsigned int offset,
271 unsigned int size, drm_map_type_t type,
272 drm_map_flags_t flags, drm_local_map_t **map_ptr)
274 drm_map_list_t *list;
275 int rc;
277 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
278 if (!rc)
279 *map_ptr = list->map;
280 return rc;
282 EXPORT_SYMBOL(drm_addmap);
284 int drm_addmap_ioctl(struct inode *inode, struct file *filp,
285 unsigned int cmd, unsigned long arg)
287 drm_file_t *priv = filp->private_data;
288 drm_device_t *dev = priv->head->dev;
289 drm_map_t map;
290 drm_map_list_t *maplist;
291 drm_map_t __user *argp = (void __user *)arg;
292 int err;
294 if (!(filp->f_mode & 3))
295 return -EACCES; /* Require read/write */
297 if (copy_from_user(& map, argp, sizeof(map))) {
298 return -EFAULT;
301 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
302 &maplist);
304 if (err)
305 return err;
307 if (copy_to_user(argp, maplist->map, sizeof(drm_map_t)))
308 return -EFAULT;
309 if (put_user(maplist->user_token, &argp->handle))
310 return -EFAULT;
311 return 0;
316 * Remove a map private from list and deallocate resources if the mapping
317 * isn't in use.
319 * \param inode device inode.
320 * \param filp file pointer.
321 * \param cmd command.
322 * \param arg pointer to a drm_map_t structure.
323 * \return zero on success or a negative value on error.
325 * Searches the map on drm_device::maplist, removes it from the list, see if
326 * its being used, and free any associate resource (such as MTRR's) if it's not
327 * being on use.
329 * \sa drm_addmap
331 int drm_rmmap_locked(drm_device_t *dev, drm_local_map_t *map)
333 struct list_head *list;
334 drm_map_list_t *r_list = NULL;
335 drm_dma_handle_t dmah;
337 /* Find the list entry for the map and remove it */
338 list_for_each(list, &dev->maplist->head) {
339 r_list = list_entry(list, drm_map_list_t, head);
341 if (r_list->map == map) {
342 list_del(list);
343 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
344 break;
348 /* List has wrapped around to the head pointer, or it's empty and we
349 * didn't find anything.
351 if (list == (&dev->maplist->head)) {
352 return -EINVAL;
355 switch (map->type) {
356 case _DRM_REGISTERS:
357 drm_ioremapfree(map->handle, map->size, dev);
358 /* FALLTHROUGH */
359 case _DRM_FRAME_BUFFER:
360 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
361 int retcode;
362 retcode = mtrr_del(map->mtrr, map->offset,
363 map->size);
364 DRM_DEBUG ("mtrr_del=%d\n", retcode);
366 break;
367 case _DRM_SHM:
368 vfree(map->handle);
369 break;
370 case _DRM_AGP:
371 case _DRM_SCATTER_GATHER:
372 break;
373 case _DRM_CONSISTENT:
374 dmah.vaddr = map->handle;
375 dmah.busaddr = map->offset;
376 dmah.size = map->size;
377 __drm_pci_free(dev, &dmah);
378 break;
380 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
382 return 0;
384 EXPORT_SYMBOL(drm_rmmap_locked);
386 int drm_rmmap(drm_device_t *dev, drm_local_map_t *map)
388 int ret;
390 down(&dev->struct_sem);
391 ret = drm_rmmap_locked(dev, map);
392 up(&dev->struct_sem);
394 return ret;
396 EXPORT_SYMBOL(drm_rmmap);
398 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
399 * the last close of the device, and this is necessary for cleanup when things
400 * exit uncleanly. Therefore, having userland manually remove mappings seems
401 * like a pointless exercise since they're going away anyway.
403 * One use case might be after addmap is allowed for normal users for SHM and
404 * gets used by drivers that the server doesn't need to care about. This seems
405 * unlikely.
407 int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
408 unsigned int cmd, unsigned long arg)
410 drm_file_t *priv = filp->private_data;
411 drm_device_t *dev = priv->head->dev;
412 drm_map_t request;
413 drm_local_map_t *map = NULL;
414 struct list_head *list;
415 int ret;
417 if (copy_from_user(&request, (drm_map_t __user *)arg, sizeof(request))) {
418 return -EFAULT;
421 down(&dev->struct_sem);
422 list_for_each(list, &dev->maplist->head) {
423 drm_map_list_t *r_list = list_entry(list, drm_map_list_t, head);
425 if (r_list->map &&
426 r_list->user_token == (unsigned long) request.handle &&
427 r_list->map->flags & _DRM_REMOVABLE) {
428 map = r_list->map;
429 break;
433 /* List has wrapped around to the head pointer, or its empty we didn't
434 * find anything.
436 if (list == (&dev->maplist->head)) {
437 up(&dev->struct_sem);
438 return -EINVAL;
441 if (!map)
442 return -EINVAL;
444 /* Register and framebuffer maps are permanent */
445 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
446 up(&dev->struct_sem);
447 return 0;
450 ret = drm_rmmap_locked(dev, map);
452 up(&dev->struct_sem);
454 return ret;
458 * Cleanup after an error on one of the addbufs() functions.
460 * \param dev DRM device.
461 * \param entry buffer entry where the error occurred.
463 * Frees any pages and buffers associated with the given entry.
465 static void drm_cleanup_buf_error(drm_device_t *dev, drm_buf_entry_t *entry)
467 int i;
469 if (entry->seg_count) {
470 for (i = 0; i < entry->seg_count; i++) {
471 if (entry->seglist[i]) {
472 drm_free_pages(entry->seglist[i],
473 entry->page_order,
474 DRM_MEM_DMA);
477 drm_free(entry->seglist,
478 entry->seg_count *
479 sizeof(*entry->seglist),
480 DRM_MEM_SEGS);
482 entry->seg_count = 0;
485 if (entry->buf_count) {
486 for (i = 0; i < entry->buf_count; i++) {
487 if (entry->buflist[i].dev_private) {
488 drm_free(entry->buflist[i].dev_private,
489 entry->buflist[i].dev_priv_size,
490 DRM_MEM_BUFS);
493 drm_free(entry->buflist,
494 entry->buf_count *
495 sizeof(*entry->buflist),
496 DRM_MEM_BUFS);
498 entry->buf_count = 0;
502 #if __OS_HAS_AGP
504 * Add AGP buffers for DMA transfers.
506 * \param dev drm_device_t to which the buffers are to be added.
507 * \param request pointer to a drm_buf_desc_t describing the request.
508 * \return zero on success or a negative number on failure.
510 * After some sanity checks creates a drm_buf structure for each buffer and
511 * reallocates the buffer list of the same size order to accommodate the new
512 * buffers.
514 int drm_addbufs_agp(drm_device_t *dev, drm_buf_desc_t *request)
516 drm_device_dma_t *dma = dev->dma;
517 drm_buf_entry_t *entry;
518 drm_buf_t *buf;
519 unsigned long offset;
520 unsigned long agp_offset;
521 int count;
522 int order;
523 int size;
524 int alignment;
525 int page_order;
526 int total;
527 int byte_count;
528 int i;
529 drm_buf_t **temp_buflist;
531 if ( !dma ) return -EINVAL;
533 count = request->count;
534 order = drm_order(request->size);
535 size = 1 << order;
537 alignment = (request->flags & _DRM_PAGE_ALIGN)
538 ? PAGE_ALIGN(size) : size;
539 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
540 total = PAGE_SIZE << page_order;
542 byte_count = 0;
543 agp_offset = dev->agp->base + request->agp_start;
545 DRM_DEBUG( "count: %d\n", count );
546 DRM_DEBUG( "order: %d\n", order );
547 DRM_DEBUG( "size: %d\n", size );
548 DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
549 DRM_DEBUG( "alignment: %d\n", alignment );
550 DRM_DEBUG( "page_order: %d\n", page_order );
551 DRM_DEBUG( "total: %d\n", total );
553 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
554 if ( dev->queue_count ) return -EBUSY; /* Not while in use */
556 spin_lock( &dev->count_lock );
557 if ( dev->buf_use ) {
558 spin_unlock( &dev->count_lock );
559 return -EBUSY;
561 atomic_inc( &dev->buf_alloc );
562 spin_unlock( &dev->count_lock );
564 down( &dev->struct_sem );
565 entry = &dma->bufs[order];
566 if ( entry->buf_count ) {
567 up( &dev->struct_sem );
568 atomic_dec( &dev->buf_alloc );
569 return -ENOMEM; /* May only call once for each order */
572 if (count < 0 || count > 4096) {
573 up( &dev->struct_sem );
574 atomic_dec( &dev->buf_alloc );
575 return -EINVAL;
578 entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
579 DRM_MEM_BUFS );
580 if ( !entry->buflist ) {
581 up( &dev->struct_sem );
582 atomic_dec( &dev->buf_alloc );
583 return -ENOMEM;
585 memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
587 entry->buf_size = size;
588 entry->page_order = page_order;
590 offset = 0;
592 while ( entry->buf_count < count ) {
593 buf = &entry->buflist[entry->buf_count];
594 buf->idx = dma->buf_count + entry->buf_count;
595 buf->total = alignment;
596 buf->order = order;
597 buf->used = 0;
599 buf->offset = (dma->byte_count + offset);
600 buf->bus_address = agp_offset + offset;
601 buf->address = (void *)(agp_offset + offset);
602 buf->next = NULL;
603 buf->waiting = 0;
604 buf->pending = 0;
605 init_waitqueue_head( &buf->dma_wait );
606 buf->filp = NULL;
608 buf->dev_priv_size = dev->driver->dev_priv_size;
609 buf->dev_private = drm_alloc( buf->dev_priv_size,
610 DRM_MEM_BUFS );
611 if(!buf->dev_private) {
612 /* Set count correctly so we free the proper amount. */
613 entry->buf_count = count;
614 drm_cleanup_buf_error(dev,entry);
615 up( &dev->struct_sem );
616 atomic_dec( &dev->buf_alloc );
617 return -ENOMEM;
619 memset( buf->dev_private, 0, buf->dev_priv_size );
621 DRM_DEBUG( "buffer %d @ %p\n",
622 entry->buf_count, buf->address );
624 offset += alignment;
625 entry->buf_count++;
626 byte_count += PAGE_SIZE << page_order;
629 DRM_DEBUG( "byte_count: %d\n", byte_count );
631 temp_buflist = drm_realloc( dma->buflist,
632 dma->buf_count * sizeof(*dma->buflist),
633 (dma->buf_count + entry->buf_count)
634 * sizeof(*dma->buflist),
635 DRM_MEM_BUFS );
636 if(!temp_buflist) {
637 /* Free the entry because it isn't valid */
638 drm_cleanup_buf_error(dev,entry);
639 up( &dev->struct_sem );
640 atomic_dec( &dev->buf_alloc );
641 return -ENOMEM;
643 dma->buflist = temp_buflist;
645 for ( i = 0 ; i < entry->buf_count ; i++ ) {
646 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
649 dma->buf_count += entry->buf_count;
650 dma->byte_count += byte_count;
652 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
653 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
655 up( &dev->struct_sem );
657 request->count = entry->buf_count;
658 request->size = size;
660 dma->flags = _DRM_DMA_USE_AGP;
662 atomic_dec( &dev->buf_alloc );
663 return 0;
665 EXPORT_SYMBOL(drm_addbufs_agp);
666 #endif /* __OS_HAS_AGP */
668 int drm_addbufs_pci(drm_device_t *dev, drm_buf_desc_t *request)
670 drm_device_dma_t *dma = dev->dma;
671 int count;
672 int order;
673 int size;
674 int total;
675 int page_order;
676 drm_buf_entry_t *entry;
677 unsigned long page;
678 drm_buf_t *buf;
679 int alignment;
680 unsigned long offset;
681 int i;
682 int byte_count;
683 int page_count;
684 unsigned long *temp_pagelist;
685 drm_buf_t **temp_buflist;
687 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA)) return -EINVAL;
688 if ( !dma ) return -EINVAL;
690 count = request->count;
691 order = drm_order(request->size);
692 size = 1 << order;
694 DRM_DEBUG( "count=%d, size=%d (%d), order=%d, queue_count=%d\n",
695 request->count, request->size, size,
696 order, dev->queue_count );
698 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
699 if ( dev->queue_count ) return -EBUSY; /* Not while in use */
701 alignment = (request->flags & _DRM_PAGE_ALIGN)
702 ? PAGE_ALIGN(size) : size;
703 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
704 total = PAGE_SIZE << page_order;
706 spin_lock( &dev->count_lock );
707 if ( dev->buf_use ) {
708 spin_unlock( &dev->count_lock );
709 return -EBUSY;
711 atomic_inc( &dev->buf_alloc );
712 spin_unlock( &dev->count_lock );
714 down( &dev->struct_sem );
715 entry = &dma->bufs[order];
716 if ( entry->buf_count ) {
717 up( &dev->struct_sem );
718 atomic_dec( &dev->buf_alloc );
719 return -ENOMEM; /* May only call once for each order */
722 if (count < 0 || count > 4096) {
723 up( &dev->struct_sem );
724 atomic_dec( &dev->buf_alloc );
725 return -EINVAL;
728 entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
729 DRM_MEM_BUFS );
730 if ( !entry->buflist ) {
731 up( &dev->struct_sem );
732 atomic_dec( &dev->buf_alloc );
733 return -ENOMEM;
735 memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
737 entry->seglist = drm_alloc( count * sizeof(*entry->seglist),
738 DRM_MEM_SEGS );
739 if ( !entry->seglist ) {
740 drm_free( entry->buflist,
741 count * sizeof(*entry->buflist),
742 DRM_MEM_BUFS );
743 up( &dev->struct_sem );
744 atomic_dec( &dev->buf_alloc );
745 return -ENOMEM;
747 memset( entry->seglist, 0, count * sizeof(*entry->seglist) );
749 /* Keep the original pagelist until we know all the allocations
750 * have succeeded
752 temp_pagelist = drm_alloc( (dma->page_count + (count << page_order))
753 * sizeof(*dma->pagelist),
754 DRM_MEM_PAGES );
755 if (!temp_pagelist) {
756 drm_free( entry->buflist,
757 count * sizeof(*entry->buflist),
758 DRM_MEM_BUFS );
759 drm_free( entry->seglist,
760 count * sizeof(*entry->seglist),
761 DRM_MEM_SEGS );
762 up( &dev->struct_sem );
763 atomic_dec( &dev->buf_alloc );
764 return -ENOMEM;
766 memcpy(temp_pagelist,
767 dma->pagelist,
768 dma->page_count * sizeof(*dma->pagelist));
769 DRM_DEBUG( "pagelist: %d entries\n",
770 dma->page_count + (count << page_order) );
772 entry->buf_size = size;
773 entry->page_order = page_order;
774 byte_count = 0;
775 page_count = 0;
777 while ( entry->buf_count < count ) {
778 page = drm_alloc_pages( page_order, DRM_MEM_DMA );
779 if ( !page ) {
780 /* Set count correctly so we free the proper amount. */
781 entry->buf_count = count;
782 entry->seg_count = count;
783 drm_cleanup_buf_error(dev, entry);
784 drm_free( temp_pagelist,
785 (dma->page_count + (count << page_order))
786 * sizeof(*dma->pagelist),
787 DRM_MEM_PAGES );
788 up( &dev->struct_sem );
789 atomic_dec( &dev->buf_alloc );
790 return -ENOMEM;
792 entry->seglist[entry->seg_count++] = page;
793 for ( i = 0 ; i < (1 << page_order) ; i++ ) {
794 DRM_DEBUG( "page %d @ 0x%08lx\n",
795 dma->page_count + page_count,
796 page + PAGE_SIZE * i );
797 temp_pagelist[dma->page_count + page_count++]
798 = page + PAGE_SIZE * i;
800 for ( offset = 0 ;
801 offset + size <= total && entry->buf_count < count ;
802 offset += alignment, ++entry->buf_count ) {
803 buf = &entry->buflist[entry->buf_count];
804 buf->idx = dma->buf_count + entry->buf_count;
805 buf->total = alignment;
806 buf->order = order;
807 buf->used = 0;
808 buf->offset = (dma->byte_count + byte_count + offset);
809 buf->address = (void *)(page + offset);
810 buf->next = NULL;
811 buf->waiting = 0;
812 buf->pending = 0;
813 init_waitqueue_head( &buf->dma_wait );
814 buf->filp = NULL;
816 buf->dev_priv_size = dev->driver->dev_priv_size;
817 buf->dev_private = drm_alloc( buf->dev_priv_size,
818 DRM_MEM_BUFS );
819 if(!buf->dev_private) {
820 /* Set count correctly so we free the proper amount. */
821 entry->buf_count = count;
822 entry->seg_count = count;
823 drm_cleanup_buf_error(dev,entry);
824 drm_free( temp_pagelist,
825 (dma->page_count + (count << page_order))
826 * sizeof(*dma->pagelist),
827 DRM_MEM_PAGES );
828 up( &dev->struct_sem );
829 atomic_dec( &dev->buf_alloc );
830 return -ENOMEM;
832 memset( buf->dev_private, 0, buf->dev_priv_size );
834 DRM_DEBUG( "buffer %d @ %p\n",
835 entry->buf_count, buf->address );
837 byte_count += PAGE_SIZE << page_order;
840 temp_buflist = drm_realloc( dma->buflist,
841 dma->buf_count * sizeof(*dma->buflist),
842 (dma->buf_count + entry->buf_count)
843 * sizeof(*dma->buflist),
844 DRM_MEM_BUFS );
845 if (!temp_buflist) {
846 /* Free the entry because it isn't valid */
847 drm_cleanup_buf_error(dev,entry);
848 drm_free( temp_pagelist,
849 (dma->page_count + (count << page_order))
850 * sizeof(*dma->pagelist),
851 DRM_MEM_PAGES );
852 up( &dev->struct_sem );
853 atomic_dec( &dev->buf_alloc );
854 return -ENOMEM;
856 dma->buflist = temp_buflist;
858 for ( i = 0 ; i < entry->buf_count ; i++ ) {
859 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
862 /* No allocations failed, so now we can replace the orginal pagelist
863 * with the new one.
865 if (dma->page_count) {
866 drm_free(dma->pagelist,
867 dma->page_count * sizeof(*dma->pagelist),
868 DRM_MEM_PAGES);
870 dma->pagelist = temp_pagelist;
872 dma->buf_count += entry->buf_count;
873 dma->seg_count += entry->seg_count;
874 dma->page_count += entry->seg_count << page_order;
875 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
877 up( &dev->struct_sem );
879 request->count = entry->buf_count;
880 request->size = size;
882 atomic_dec( &dev->buf_alloc );
883 return 0;
886 EXPORT_SYMBOL(drm_addbufs_pci);
888 static int drm_addbufs_sg(drm_device_t *dev, drm_buf_desc_t *request)
890 drm_device_dma_t *dma = dev->dma;
891 drm_buf_entry_t *entry;
892 drm_buf_t *buf;
893 unsigned long offset;
894 unsigned long agp_offset;
895 int count;
896 int order;
897 int size;
898 int alignment;
899 int page_order;
900 int total;
901 int byte_count;
902 int i;
903 drm_buf_t **temp_buflist;
905 if (!drm_core_check_feature(dev, DRIVER_SG)) return -EINVAL;
907 if ( !dma ) return -EINVAL;
909 count = request->count;
910 order = drm_order(request->size);
911 size = 1 << order;
913 alignment = (request->flags & _DRM_PAGE_ALIGN)
914 ? PAGE_ALIGN(size) : size;
915 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
916 total = PAGE_SIZE << page_order;
918 byte_count = 0;
919 agp_offset = request->agp_start;
921 DRM_DEBUG( "count: %d\n", count );
922 DRM_DEBUG( "order: %d\n", order );
923 DRM_DEBUG( "size: %d\n", size );
924 DRM_DEBUG( "agp_offset: %lu\n", agp_offset );
925 DRM_DEBUG( "alignment: %d\n", alignment );
926 DRM_DEBUG( "page_order: %d\n", page_order );
927 DRM_DEBUG( "total: %d\n", total );
929 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
930 if ( dev->queue_count ) return -EBUSY; /* Not while in use */
932 spin_lock( &dev->count_lock );
933 if ( dev->buf_use ) {
934 spin_unlock( &dev->count_lock );
935 return -EBUSY;
937 atomic_inc( &dev->buf_alloc );
938 spin_unlock( &dev->count_lock );
940 down( &dev->struct_sem );
941 entry = &dma->bufs[order];
942 if ( entry->buf_count ) {
943 up( &dev->struct_sem );
944 atomic_dec( &dev->buf_alloc );
945 return -ENOMEM; /* May only call once for each order */
948 if (count < 0 || count > 4096) {
949 up( &dev->struct_sem );
950 atomic_dec( &dev->buf_alloc );
951 return -EINVAL;
954 entry->buflist = drm_alloc( count * sizeof(*entry->buflist),
955 DRM_MEM_BUFS );
956 if ( !entry->buflist ) {
957 up( &dev->struct_sem );
958 atomic_dec( &dev->buf_alloc );
959 return -ENOMEM;
961 memset( entry->buflist, 0, count * sizeof(*entry->buflist) );
963 entry->buf_size = size;
964 entry->page_order = page_order;
966 offset = 0;
968 while ( entry->buf_count < count ) {
969 buf = &entry->buflist[entry->buf_count];
970 buf->idx = dma->buf_count + entry->buf_count;
971 buf->total = alignment;
972 buf->order = order;
973 buf->used = 0;
975 buf->offset = (dma->byte_count + offset);
976 buf->bus_address = agp_offset + offset;
977 buf->address = (void *)(agp_offset + offset
978 + (unsigned long)dev->sg->virtual);
979 buf->next = NULL;
980 buf->waiting = 0;
981 buf->pending = 0;
982 init_waitqueue_head( &buf->dma_wait );
983 buf->filp = NULL;
985 buf->dev_priv_size = dev->driver->dev_priv_size;
986 buf->dev_private = drm_alloc( buf->dev_priv_size,
987 DRM_MEM_BUFS );
988 if(!buf->dev_private) {
989 /* Set count correctly so we free the proper amount. */
990 entry->buf_count = count;
991 drm_cleanup_buf_error(dev,entry);
992 up( &dev->struct_sem );
993 atomic_dec( &dev->buf_alloc );
994 return -ENOMEM;
997 memset( buf->dev_private, 0, buf->dev_priv_size );
999 DRM_DEBUG( "buffer %d @ %p\n",
1000 entry->buf_count, buf->address );
1002 offset += alignment;
1003 entry->buf_count++;
1004 byte_count += PAGE_SIZE << page_order;
1007 DRM_DEBUG( "byte_count: %d\n", byte_count );
1009 temp_buflist = drm_realloc( dma->buflist,
1010 dma->buf_count * sizeof(*dma->buflist),
1011 (dma->buf_count + entry->buf_count)
1012 * sizeof(*dma->buflist),
1013 DRM_MEM_BUFS );
1014 if(!temp_buflist) {
1015 /* Free the entry because it isn't valid */
1016 drm_cleanup_buf_error(dev,entry);
1017 up( &dev->struct_sem );
1018 atomic_dec( &dev->buf_alloc );
1019 return -ENOMEM;
1021 dma->buflist = temp_buflist;
1023 for ( i = 0 ; i < entry->buf_count ; i++ ) {
1024 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1027 dma->buf_count += entry->buf_count;
1028 dma->byte_count += byte_count;
1030 DRM_DEBUG( "dma->buf_count : %d\n", dma->buf_count );
1031 DRM_DEBUG( "entry->buf_count : %d\n", entry->buf_count );
1033 up( &dev->struct_sem );
1035 request->count = entry->buf_count;
1036 request->size = size;
1038 dma->flags = _DRM_DMA_USE_SG;
1040 atomic_dec( &dev->buf_alloc );
1041 return 0;
1044 static int drm_addbufs_fb(drm_device_t *dev, drm_buf_desc_t *request)
1046 drm_device_dma_t *dma = dev->dma;
1047 drm_buf_entry_t *entry;
1048 drm_buf_t *buf;
1049 unsigned long offset;
1050 unsigned long agp_offset;
1051 int count;
1052 int order;
1053 int size;
1054 int alignment;
1055 int page_order;
1056 int total;
1057 int byte_count;
1058 int i;
1059 drm_buf_t **temp_buflist;
1061 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1062 return -EINVAL;
1064 if (!dma)
1065 return -EINVAL;
1067 count = request->count;
1068 order = drm_order(request->size);
1069 size = 1 << order;
1071 alignment = (request->flags & _DRM_PAGE_ALIGN)
1072 ? PAGE_ALIGN(size) : size;
1073 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1074 total = PAGE_SIZE << page_order;
1076 byte_count = 0;
1077 agp_offset = request->agp_start;
1079 DRM_DEBUG("count: %d\n", count);
1080 DRM_DEBUG("order: %d\n", order);
1081 DRM_DEBUG("size: %d\n", size);
1082 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1083 DRM_DEBUG("alignment: %d\n", alignment);
1084 DRM_DEBUG("page_order: %d\n", page_order);
1085 DRM_DEBUG("total: %d\n", total);
1087 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1088 return -EINVAL;
1089 if (dev->queue_count)
1090 return -EBUSY; /* Not while in use */
1092 spin_lock(&dev->count_lock);
1093 if (dev->buf_use) {
1094 spin_unlock(&dev->count_lock);
1095 return -EBUSY;
1097 atomic_inc(&dev->buf_alloc);
1098 spin_unlock(&dev->count_lock);
1100 down(&dev->struct_sem);
1101 entry = &dma->bufs[order];
1102 if (entry->buf_count) {
1103 up(&dev->struct_sem);
1104 atomic_dec(&dev->buf_alloc);
1105 return -ENOMEM; /* May only call once for each order */
1108 if (count < 0 || count > 4096) {
1109 up(&dev->struct_sem);
1110 atomic_dec(&dev->buf_alloc);
1111 return -EINVAL;
1114 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1115 DRM_MEM_BUFS);
1116 if (!entry->buflist) {
1117 up(&dev->struct_sem);
1118 atomic_dec(&dev->buf_alloc);
1119 return -ENOMEM;
1121 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1123 entry->buf_size = size;
1124 entry->page_order = page_order;
1126 offset = 0;
1128 while (entry->buf_count < count) {
1129 buf = &entry->buflist[entry->buf_count];
1130 buf->idx = dma->buf_count + entry->buf_count;
1131 buf->total = alignment;
1132 buf->order = order;
1133 buf->used = 0;
1135 buf->offset = (dma->byte_count + offset);
1136 buf->bus_address = agp_offset + offset;
1137 buf->address = (void *)(agp_offset + offset);
1138 buf->next = NULL;
1139 buf->waiting = 0;
1140 buf->pending = 0;
1141 init_waitqueue_head(&buf->dma_wait);
1142 buf->filp = NULL;
1144 buf->dev_priv_size = dev->driver->dev_priv_size;
1145 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1146 if (!buf->dev_private) {
1147 /* Set count correctly so we free the proper amount. */
1148 entry->buf_count = count;
1149 drm_cleanup_buf_error(dev, entry);
1150 up(&dev->struct_sem);
1151 atomic_dec(&dev->buf_alloc);
1152 return -ENOMEM;
1154 memset(buf->dev_private, 0, buf->dev_priv_size);
1156 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1158 offset += alignment;
1159 entry->buf_count++;
1160 byte_count += PAGE_SIZE << page_order;
1163 DRM_DEBUG("byte_count: %d\n", byte_count);
1165 temp_buflist = drm_realloc(dma->buflist,
1166 dma->buf_count * sizeof(*dma->buflist),
1167 (dma->buf_count + entry->buf_count)
1168 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1169 if (!temp_buflist) {
1170 /* Free the entry because it isn't valid */
1171 drm_cleanup_buf_error(dev, entry);
1172 up(&dev->struct_sem);
1173 atomic_dec(&dev->buf_alloc);
1174 return -ENOMEM;
1176 dma->buflist = temp_buflist;
1178 for (i = 0; i < entry->buf_count; i++) {
1179 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1182 dma->buf_count += entry->buf_count;
1183 dma->byte_count += byte_count;
1185 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1186 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1188 up(&dev->struct_sem);
1190 request->count = entry->buf_count;
1191 request->size = size;
1193 dma->flags = _DRM_DMA_USE_FB;
1195 atomic_dec(&dev->buf_alloc);
1196 return 0;
1200 * Add buffers for DMA transfers (ioctl).
1202 * \param inode device inode.
1203 * \param filp file pointer.
1204 * \param cmd command.
1205 * \param arg pointer to a drm_buf_desc_t request.
1206 * \return zero on success or a negative number on failure.
1208 * According with the memory type specified in drm_buf_desc::flags and the
1209 * build options, it dispatches the call either to addbufs_agp(),
1210 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1211 * PCI memory respectively.
1213 int drm_addbufs( struct inode *inode, struct file *filp,
1214 unsigned int cmd, unsigned long arg )
1216 drm_buf_desc_t request;
1217 drm_file_t *priv = filp->private_data;
1218 drm_device_t *dev = priv->head->dev;
1219 int ret;
1221 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1222 return -EINVAL;
1224 if ( copy_from_user( &request, (drm_buf_desc_t __user *)arg,
1225 sizeof(request) ) )
1226 return -EFAULT;
1228 #if __OS_HAS_AGP
1229 if ( request.flags & _DRM_AGP_BUFFER )
1230 ret=drm_addbufs_agp(dev, &request);
1231 else
1232 #endif
1233 if ( request.flags & _DRM_SG_BUFFER )
1234 ret=drm_addbufs_sg(dev, &request);
1235 else if ( request.flags & _DRM_FB_BUFFER)
1236 ret=drm_addbufs_fb(dev, &request);
1237 else
1238 ret=drm_addbufs_pci(dev, &request);
1240 if (ret==0) {
1241 if (copy_to_user((void __user *)arg, &request,
1242 sizeof(request))) {
1243 ret = -EFAULT;
1246 return ret;
1251 * Get information about the buffer mappings.
1253 * This was originally mean for debugging purposes, or by a sophisticated
1254 * client library to determine how best to use the available buffers (e.g.,
1255 * large buffers can be used for image transfer).
1257 * \param inode device inode.
1258 * \param filp file pointer.
1259 * \param cmd command.
1260 * \param arg pointer to a drm_buf_info structure.
1261 * \return zero on success or a negative number on failure.
1263 * Increments drm_device::buf_use while holding the drm_device::count_lock
1264 * lock, preventing of allocating more buffers after this call. Information
1265 * about each requested buffer is then copied into user space.
1267 int drm_infobufs( struct inode *inode, struct file *filp,
1268 unsigned int cmd, unsigned long arg )
1270 drm_file_t *priv = filp->private_data;
1271 drm_device_t *dev = priv->head->dev;
1272 drm_device_dma_t *dma = dev->dma;
1273 drm_buf_info_t request;
1274 drm_buf_info_t __user *argp = (void __user *)arg;
1275 int i;
1276 int count;
1278 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1279 return -EINVAL;
1281 if ( !dma ) return -EINVAL;
1283 spin_lock( &dev->count_lock );
1284 if ( atomic_read( &dev->buf_alloc ) ) {
1285 spin_unlock( &dev->count_lock );
1286 return -EBUSY;
1288 ++dev->buf_use; /* Can't allocate more after this call */
1289 spin_unlock( &dev->count_lock );
1291 if ( copy_from_user( &request, argp, sizeof(request) ) )
1292 return -EFAULT;
1294 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
1295 if ( dma->bufs[i].buf_count ) ++count;
1298 DRM_DEBUG( "count = %d\n", count );
1300 if ( request.count >= count ) {
1301 for ( i = 0, count = 0 ; i < DRM_MAX_ORDER + 1 ; i++ ) {
1302 if ( dma->bufs[i].buf_count ) {
1303 drm_buf_desc_t __user *to = &request.list[count];
1304 drm_buf_entry_t *from = &dma->bufs[i];
1305 drm_freelist_t *list = &dma->bufs[i].freelist;
1306 if ( copy_to_user( &to->count,
1307 &from->buf_count,
1308 sizeof(from->buf_count) ) ||
1309 copy_to_user( &to->size,
1310 &from->buf_size,
1311 sizeof(from->buf_size) ) ||
1312 copy_to_user( &to->low_mark,
1313 &list->low_mark,
1314 sizeof(list->low_mark) ) ||
1315 copy_to_user( &to->high_mark,
1316 &list->high_mark,
1317 sizeof(list->high_mark) ) )
1318 return -EFAULT;
1320 DRM_DEBUG( "%d %d %d %d %d\n",
1322 dma->bufs[i].buf_count,
1323 dma->bufs[i].buf_size,
1324 dma->bufs[i].freelist.low_mark,
1325 dma->bufs[i].freelist.high_mark );
1326 ++count;
1330 request.count = count;
1332 if ( copy_to_user( argp, &request, sizeof(request) ) )
1333 return -EFAULT;
1335 return 0;
1339 * Specifies a low and high water mark for buffer allocation
1341 * \param inode device inode.
1342 * \param filp file pointer.
1343 * \param cmd command.
1344 * \param arg a pointer to a drm_buf_desc structure.
1345 * \return zero on success or a negative number on failure.
1347 * Verifies that the size order is bounded between the admissible orders and
1348 * updates the respective drm_device_dma::bufs entry low and high water mark.
1350 * \note This ioctl is deprecated and mostly never used.
1352 int drm_markbufs( struct inode *inode, struct file *filp,
1353 unsigned int cmd, unsigned long arg )
1355 drm_file_t *priv = filp->private_data;
1356 drm_device_t *dev = priv->head->dev;
1357 drm_device_dma_t *dma = dev->dma;
1358 drm_buf_desc_t request;
1359 int order;
1360 drm_buf_entry_t *entry;
1362 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1363 return -EINVAL;
1365 if ( !dma ) return -EINVAL;
1367 if ( copy_from_user( &request,
1368 (drm_buf_desc_t __user *)arg,
1369 sizeof(request) ) )
1370 return -EFAULT;
1372 DRM_DEBUG( "%d, %d, %d\n",
1373 request.size, request.low_mark, request.high_mark );
1374 order = drm_order( request.size );
1375 if ( order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ) return -EINVAL;
1376 entry = &dma->bufs[order];
1378 if ( request.low_mark < 0 || request.low_mark > entry->buf_count )
1379 return -EINVAL;
1380 if ( request.high_mark < 0 || request.high_mark > entry->buf_count )
1381 return -EINVAL;
1383 entry->freelist.low_mark = request.low_mark;
1384 entry->freelist.high_mark = request.high_mark;
1386 return 0;
1390 * Unreserve the buffers in list, previously reserved using drmDMA.
1392 * \param inode device inode.
1393 * \param filp file pointer.
1394 * \param cmd command.
1395 * \param arg pointer to a drm_buf_free structure.
1396 * \return zero on success or a negative number on failure.
1398 * Calls free_buffer() for each used buffer.
1399 * This function is primarily used for debugging.
1401 int drm_freebufs( struct inode *inode, struct file *filp,
1402 unsigned int cmd, unsigned long arg )
1404 drm_file_t *priv = filp->private_data;
1405 drm_device_t *dev = priv->head->dev;
1406 drm_device_dma_t *dma = dev->dma;
1407 drm_buf_free_t request;
1408 int i;
1409 int idx;
1410 drm_buf_t *buf;
1412 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1413 return -EINVAL;
1415 if ( !dma ) return -EINVAL;
1417 if ( copy_from_user( &request,
1418 (drm_buf_free_t __user *)arg,
1419 sizeof(request) ) )
1420 return -EFAULT;
1422 DRM_DEBUG( "%d\n", request.count );
1423 for ( i = 0 ; i < request.count ; i++ ) {
1424 if ( copy_from_user( &idx,
1425 &request.list[i],
1426 sizeof(idx) ) )
1427 return -EFAULT;
1428 if ( idx < 0 || idx >= dma->buf_count ) {
1429 DRM_ERROR( "Index %d (of %d max)\n",
1430 idx, dma->buf_count - 1 );
1431 return -EINVAL;
1433 buf = dma->buflist[idx];
1434 if ( buf->filp != filp ) {
1435 DRM_ERROR( "Process %d freeing buffer not owned\n",
1436 current->pid );
1437 return -EINVAL;
1439 drm_free_buffer( dev, buf );
1442 return 0;
1446 * Maps all of the DMA buffers into client-virtual space (ioctl).
1448 * \param inode device inode.
1449 * \param filp file pointer.
1450 * \param cmd command.
1451 * \param arg pointer to a drm_buf_map structure.
1452 * \return zero on success or a negative number on failure.
1454 * Maps the AGP or SG buffer region with do_mmap(), and copies information
1455 * about each buffer into user space. The PCI buffers are already mapped on the
1456 * addbufs_pci() call.
1458 int drm_mapbufs( struct inode *inode, struct file *filp,
1459 unsigned int cmd, unsigned long arg )
1461 drm_file_t *priv = filp->private_data;
1462 drm_device_t *dev = priv->head->dev;
1463 drm_device_dma_t *dma = dev->dma;
1464 drm_buf_map_t __user *argp = (void __user *)arg;
1465 int retcode = 0;
1466 const int zero = 0;
1467 unsigned long virtual;
1468 unsigned long address;
1469 drm_buf_map_t request;
1470 int i;
1472 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1473 return -EINVAL;
1475 if ( !dma ) return -EINVAL;
1477 spin_lock( &dev->count_lock );
1478 if ( atomic_read( &dev->buf_alloc ) ) {
1479 spin_unlock( &dev->count_lock );
1480 return -EBUSY;
1482 dev->buf_use++; /* Can't allocate more after this call */
1483 spin_unlock( &dev->count_lock );
1485 if ( copy_from_user( &request, argp, sizeof(request) ) )
1486 return -EFAULT;
1488 if ( request.count >= dma->buf_count ) {
1489 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1490 || (drm_core_check_feature(dev, DRIVER_SG)
1491 && (dma->flags & _DRM_DMA_USE_SG))
1492 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1493 && (dma->flags & _DRM_DMA_USE_FB))) {
1494 drm_map_t *map = dev->agp_buffer_map;
1495 unsigned long token = dev->agp_buffer_token;
1497 if ( !map ) {
1498 retcode = -EINVAL;
1499 goto done;
1502 down_write( &current->mm->mmap_sem );
1503 virtual = do_mmap( filp, 0, map->size,
1504 PROT_READ | PROT_WRITE,
1505 MAP_SHARED,
1506 token );
1507 up_write( &current->mm->mmap_sem );
1508 } else {
1509 down_write( &current->mm->mmap_sem );
1510 virtual = do_mmap( filp, 0, dma->byte_count,
1511 PROT_READ | PROT_WRITE,
1512 MAP_SHARED, 0 );
1513 up_write( &current->mm->mmap_sem );
1515 if ( virtual > -1024UL ) {
1516 /* Real error */
1517 retcode = (signed long)virtual;
1518 goto done;
1520 request.virtual = (void __user *)virtual;
1522 for ( i = 0 ; i < dma->buf_count ; i++ ) {
1523 if ( copy_to_user( &request.list[i].idx,
1524 &dma->buflist[i]->idx,
1525 sizeof(request.list[0].idx) ) ) {
1526 retcode = -EFAULT;
1527 goto done;
1529 if ( copy_to_user( &request.list[i].total,
1530 &dma->buflist[i]->total,
1531 sizeof(request.list[0].total) ) ) {
1532 retcode = -EFAULT;
1533 goto done;
1535 if ( copy_to_user( &request.list[i].used,
1536 &zero,
1537 sizeof(zero) ) ) {
1538 retcode = -EFAULT;
1539 goto done;
1541 address = virtual + dma->buflist[i]->offset; /* *** */
1542 if ( copy_to_user( &request.list[i].address,
1543 &address,
1544 sizeof(address) ) ) {
1545 retcode = -EFAULT;
1546 goto done;
1550 done:
1551 request.count = dma->buf_count;
1552 DRM_DEBUG( "%d buffers, retcode = %d\n", request.count, retcode );
1554 if ( copy_to_user( argp, &request, sizeof(request) ) )
1555 return -EFAULT;
1557 return retcode;
1561 * Compute size order. Returns the exponent of the smaller power of two which
1562 * is greater or equal to given number.
1564 * \param size size.
1565 * \return order.
1567 * \todo Can be made faster.
1569 int drm_order( unsigned long size )
1571 int order;
1572 unsigned long tmp;
1574 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++)
1577 if (size & (size - 1))
1578 ++order;
1580 return order;
1582 EXPORT_SYMBOL(drm_order);