3 * Generic buffer template
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
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
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 <linux/slab.h>
38 #include <linux/log2.h>
39 #include <asm/shmparam.h>
42 resource_size_t
drm_get_resource_start(struct drm_device
*dev
, unsigned int resource
)
44 return pci_resource_start(dev
->pdev
, resource
);
46 EXPORT_SYMBOL(drm_get_resource_start
);
48 resource_size_t
drm_get_resource_len(struct drm_device
*dev
, unsigned int resource
)
50 return pci_resource_len(dev
->pdev
, resource
);
53 EXPORT_SYMBOL(drm_get_resource_len
);
55 static struct drm_map_list
*drm_find_matching_map(struct drm_device
*dev
,
56 struct drm_local_map
*map
)
58 struct drm_map_list
*entry
;
59 list_for_each_entry(entry
, &dev
->maplist
, head
) {
61 * Because the kernel-userspace ABI is fixed at a 32-bit offset
62 * while PCI resources may live above that, we ignore the map
63 * offset for maps of type _DRM_FRAMEBUFFER or _DRM_REGISTERS.
64 * It is assumed that each driver will have only one resource of
68 map
->type
!= entry
->map
->type
||
69 entry
->master
!= dev
->primary
->master
)
73 if (map
->flags
!= _DRM_CONTAINS_LOCK
)
76 case _DRM_FRAME_BUFFER
:
78 default: /* Make gcc happy */
81 if (entry
->map
->offset
== map
->offset
)
88 static int drm_map_handle(struct drm_device
*dev
, struct drm_hash_item
*hash
,
89 unsigned long user_token
, int hashed_handle
, int shm
)
91 int use_hashed_handle
, shift
;
94 #if (BITS_PER_LONG == 64)
95 use_hashed_handle
= ((user_token
& 0xFFFFFFFF00000000UL
) || hashed_handle
);
96 #elif (BITS_PER_LONG == 32)
97 use_hashed_handle
= hashed_handle
;
99 #error Unsupported long size. Neither 64 nor 32 bits.
102 if (!use_hashed_handle
) {
104 hash
->key
= user_token
>> PAGE_SHIFT
;
105 ret
= drm_ht_insert_item(&dev
->map_hash
, hash
);
111 add
= DRM_MAP_HASH_OFFSET
>> PAGE_SHIFT
;
112 if (shm
&& (SHMLBA
> PAGE_SIZE
)) {
113 int bits
= ilog2(SHMLBA
>> PAGE_SHIFT
) + 1;
115 /* For shared memory, we have to preserve the SHMLBA
116 * bits of the eventual vma->vm_pgoff value during
117 * mmap(). Otherwise we run into cache aliasing problems
118 * on some platforms. On these platforms, the pgoff of
119 * a mmap() request is used to pick a suitable virtual
120 * address for the mmap() region such that it will not
121 * cause cache aliasing problems.
123 * Therefore, make sure the SHMLBA relevant bits of the
124 * hash value we use are equal to those in the original
125 * kernel virtual address.
128 add
|= ((user_token
>> PAGE_SHIFT
) & ((1UL << bits
) - 1UL));
131 return drm_ht_just_insert_please(&dev
->map_hash
, hash
,
132 user_token
, 32 - PAGE_SHIFT
- 3,
137 * Core function to create a range of memory available for mapping by a
140 * Adjusts the memory offset to its absolute value according to the mapping
141 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
142 * applicable and if supported by the kernel.
144 static int drm_addmap_core(struct drm_device
* dev
, resource_size_t offset
,
145 unsigned int size
, enum drm_map_type type
,
146 enum drm_map_flags flags
,
147 struct drm_map_list
** maplist
)
149 struct drm_local_map
*map
;
150 struct drm_map_list
*list
;
151 drm_dma_handle_t
*dmah
;
152 unsigned long user_token
;
155 map
= kmalloc(sizeof(*map
), GFP_KERNEL
);
159 map
->offset
= offset
;
164 /* Only allow shared memory to be removable since we only keep enough
165 * book keeping information about shared memory to allow for removal
166 * when processes fork.
168 if ((map
->flags
& _DRM_REMOVABLE
) && map
->type
!= _DRM_SHM
) {
172 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
173 (unsigned long long)map
->offset
, map
->size
, map
->type
);
175 /* page-align _DRM_SHM maps. They are allocated here so there is no security
176 * hole created by that and it works around various broken drivers that use
177 * a non-aligned quantity to map the SAREA. --BenH
179 if (map
->type
== _DRM_SHM
)
180 map
->size
= PAGE_ALIGN(map
->size
);
182 if ((map
->offset
& (~(resource_size_t
)PAGE_MASK
)) || (map
->size
& (~PAGE_MASK
))) {
191 case _DRM_FRAME_BUFFER
:
192 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
193 if (map
->offset
+ (map
->size
-1) < map
->offset
||
194 map
->offset
< virt_to_phys(high_memory
)) {
200 map
->offset
+= dev
->hose
->mem_space
->start
;
202 /* Some drivers preinitialize some maps, without the X Server
203 * needing to be aware of it. Therefore, we just return success
204 * when the server tries to create a duplicate map.
206 list
= drm_find_matching_map(dev
, map
);
208 if (list
->map
->size
!= map
->size
) {
209 DRM_DEBUG("Matching maps of type %d with "
210 "mismatched sizes, (%ld vs %ld)\n",
211 map
->type
, map
->size
,
213 list
->map
->size
= map
->size
;
221 if (drm_core_has_MTRR(dev
)) {
222 if (map
->type
== _DRM_FRAME_BUFFER
||
223 (map
->flags
& _DRM_WRITE_COMBINING
)) {
224 map
->mtrr
= mtrr_add(map
->offset
, map
->size
,
225 MTRR_TYPE_WRCOMB
, 1);
228 if (map
->type
== _DRM_REGISTERS
) {
229 map
->handle
= ioremap(map
->offset
, map
->size
);
238 list
= drm_find_matching_map(dev
, map
);
240 if(list
->map
->size
!= map
->size
) {
241 DRM_DEBUG("Matching maps of type %d with "
242 "mismatched sizes, (%ld vs %ld)\n",
243 map
->type
, map
->size
, list
->map
->size
);
244 list
->map
->size
= map
->size
;
251 map
->handle
= vmalloc_user(map
->size
);
252 DRM_DEBUG("%lu %d %p\n",
253 map
->size
, drm_order(map
->size
), map
->handle
);
258 map
->offset
= (unsigned long)map
->handle
;
259 if (map
->flags
& _DRM_CONTAINS_LOCK
) {
260 /* Prevent a 2nd X Server from creating a 2nd lock */
261 if (dev
->primary
->master
->lock
.hw_lock
!= NULL
) {
266 dev
->sigdata
.lock
= dev
->primary
->master
->lock
.hw_lock
= map
->handle
; /* Pointer to lock */
270 struct drm_agp_mem
*entry
;
273 if (!drm_core_has_AGP(dev
)) {
278 map
->offset
+= dev
->hose
->mem_space
->start
;
280 /* In some cases (i810 driver), user space may have already
281 * added the AGP base itself, because dev->agp->base previously
282 * only got set during AGP enable. So, only add the base
283 * address if the map's offset isn't already within the
286 if (map
->offset
< dev
->agp
->base
||
287 map
->offset
> dev
->agp
->base
+
288 dev
->agp
->agp_info
.aper_size
* 1024 * 1024 - 1) {
289 map
->offset
+= dev
->agp
->base
;
291 map
->mtrr
= dev
->agp
->agp_mtrr
; /* for getmap */
293 /* This assumes the DRM is in total control of AGP space.
294 * It's not always the case as AGP can be in the control
295 * of user space (i.e. i810 driver). So this loop will get
296 * skipped and we double check that dev->agp->memory is
297 * actually set as well as being invalid before EPERM'ing
299 list_for_each_entry(entry
, &dev
->agp
->memory
, head
) {
300 if ((map
->offset
>= entry
->bound
) &&
301 (map
->offset
+ map
->size
<= entry
->bound
+ entry
->pages
* PAGE_SIZE
)) {
306 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
310 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
311 (unsigned long long)map
->offset
, map
->size
);
316 DRM_ERROR("tried to addmap GEM object\n");
318 case _DRM_SCATTER_GATHER
:
323 map
->offset
+= (unsigned long)dev
->sg
->virtual;
325 case _DRM_CONSISTENT
:
326 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
327 * As we're limiting the address to 2^32-1 (or less),
328 * casting it down to 32 bits is no problem, but we
329 * need to point to a 64bit variable first. */
330 dmah
= drm_pci_alloc(dev
, map
->size
, map
->size
);
335 map
->handle
= dmah
->vaddr
;
336 map
->offset
= (unsigned long)dmah
->busaddr
;
344 list
= kmalloc(sizeof(*list
), GFP_KERNEL
);
346 if (map
->type
== _DRM_REGISTERS
)
347 iounmap(map
->handle
);
351 memset(list
, 0, sizeof(*list
));
354 mutex_lock(&dev
->struct_mutex
);
355 list_add(&list
->head
, &dev
->maplist
);
357 /* Assign a 32-bit handle */
358 /* We do it here so that dev->struct_mutex protects the increment */
359 user_token
= (map
->type
== _DRM_SHM
) ? (unsigned long)map
->handle
:
361 ret
= drm_map_handle(dev
, &list
->hash
, user_token
, 0,
362 (map
->type
== _DRM_SHM
));
364 if (map
->type
== _DRM_REGISTERS
)
365 iounmap(map
->handle
);
368 mutex_unlock(&dev
->struct_mutex
);
372 list
->user_token
= list
->hash
.key
<< PAGE_SHIFT
;
373 mutex_unlock(&dev
->struct_mutex
);
375 if (!(map
->flags
& _DRM_DRIVER
))
376 list
->master
= dev
->primary
->master
;
381 int drm_addmap(struct drm_device
* dev
, resource_size_t offset
,
382 unsigned int size
, enum drm_map_type type
,
383 enum drm_map_flags flags
, struct drm_local_map
** map_ptr
)
385 struct drm_map_list
*list
;
388 rc
= drm_addmap_core(dev
, offset
, size
, type
, flags
, &list
);
390 *map_ptr
= list
->map
;
394 EXPORT_SYMBOL(drm_addmap
);
397 * Ioctl to specify a range of memory that is available for mapping by a
400 * \param inode device inode.
401 * \param file_priv DRM file private.
402 * \param cmd command.
403 * \param arg pointer to a drm_map structure.
404 * \return zero on success or a negative value on error.
407 int drm_addmap_ioctl(struct drm_device
*dev
, void *data
,
408 struct drm_file
*file_priv
)
410 struct drm_map
*map
= data
;
411 struct drm_map_list
*maplist
;
414 if (!(capable(CAP_SYS_ADMIN
) || map
->type
== _DRM_AGP
|| map
->type
== _DRM_SHM
))
417 err
= drm_addmap_core(dev
, map
->offset
, map
->size
, map
->type
,
418 map
->flags
, &maplist
);
423 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
424 map
->handle
= (void *)(unsigned long)maplist
->user_token
;
429 * Remove a map private from list and deallocate resources if the mapping
432 * Searches the map on drm_device::maplist, removes it from the list, see if
433 * its being used, and free any associate resource (such as MTRR's) if it's not
438 int drm_rmmap_locked(struct drm_device
*dev
, struct drm_local_map
*map
)
440 struct drm_map_list
*r_list
= NULL
, *list_t
;
441 drm_dma_handle_t dmah
;
443 struct drm_master
*master
;
445 /* Find the list entry for the map and remove it */
446 list_for_each_entry_safe(r_list
, list_t
, &dev
->maplist
, head
) {
447 if (r_list
->map
== map
) {
448 master
= r_list
->master
;
449 list_del(&r_list
->head
);
450 drm_ht_remove_key(&dev
->map_hash
,
451 r_list
->user_token
>> PAGE_SHIFT
);
463 iounmap(map
->handle
);
465 case _DRM_FRAME_BUFFER
:
466 if (drm_core_has_MTRR(dev
) && map
->mtrr
>= 0) {
468 retcode
= mtrr_del(map
->mtrr
, map
->offset
, map
->size
);
469 DRM_DEBUG("mtrr_del=%d\n", retcode
);
475 if (dev
->sigdata
.lock
== master
->lock
.hw_lock
)
476 dev
->sigdata
.lock
= NULL
;
477 master
->lock
.hw_lock
= NULL
; /* SHM removed */
478 master
->lock
.file_priv
= NULL
;
479 wake_up_interruptible_all(&master
->lock
.lock_queue
);
483 case _DRM_SCATTER_GATHER
:
485 case _DRM_CONSISTENT
:
486 dmah
.vaddr
= map
->handle
;
487 dmah
.busaddr
= map
->offset
;
488 dmah
.size
= map
->size
;
489 __drm_pci_free(dev
, &dmah
);
492 DRM_ERROR("tried to rmmap GEM object\n");
499 EXPORT_SYMBOL(drm_rmmap_locked
);
501 int drm_rmmap(struct drm_device
*dev
, struct drm_local_map
*map
)
505 mutex_lock(&dev
->struct_mutex
);
506 ret
= drm_rmmap_locked(dev
, map
);
507 mutex_unlock(&dev
->struct_mutex
);
511 EXPORT_SYMBOL(drm_rmmap
);
513 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
514 * the last close of the device, and this is necessary for cleanup when things
515 * exit uncleanly. Therefore, having userland manually remove mappings seems
516 * like a pointless exercise since they're going away anyway.
518 * One use case might be after addmap is allowed for normal users for SHM and
519 * gets used by drivers that the server doesn't need to care about. This seems
522 * \param inode device inode.
523 * \param file_priv DRM file private.
524 * \param cmd command.
525 * \param arg pointer to a struct drm_map structure.
526 * \return zero on success or a negative value on error.
528 int drm_rmmap_ioctl(struct drm_device
*dev
, void *data
,
529 struct drm_file
*file_priv
)
531 struct drm_map
*request
= data
;
532 struct drm_local_map
*map
= NULL
;
533 struct drm_map_list
*r_list
;
536 mutex_lock(&dev
->struct_mutex
);
537 list_for_each_entry(r_list
, &dev
->maplist
, head
) {
539 r_list
->user_token
== (unsigned long)request
->handle
&&
540 r_list
->map
->flags
& _DRM_REMOVABLE
) {
546 /* List has wrapped around to the head pointer, or its empty we didn't
549 if (list_empty(&dev
->maplist
) || !map
) {
550 mutex_unlock(&dev
->struct_mutex
);
554 /* Register and framebuffer maps are permanent */
555 if ((map
->type
== _DRM_REGISTERS
) || (map
->type
== _DRM_FRAME_BUFFER
)) {
556 mutex_unlock(&dev
->struct_mutex
);
560 ret
= drm_rmmap_locked(dev
, map
);
562 mutex_unlock(&dev
->struct_mutex
);
568 * Cleanup after an error on one of the addbufs() functions.
570 * \param dev DRM device.
571 * \param entry buffer entry where the error occurred.
573 * Frees any pages and buffers associated with the given entry.
575 static void drm_cleanup_buf_error(struct drm_device
* dev
,
576 struct drm_buf_entry
* entry
)
580 if (entry
->seg_count
) {
581 for (i
= 0; i
< entry
->seg_count
; i
++) {
582 if (entry
->seglist
[i
]) {
583 drm_pci_free(dev
, entry
->seglist
[i
]);
586 kfree(entry
->seglist
);
588 entry
->seg_count
= 0;
591 if (entry
->buf_count
) {
592 for (i
= 0; i
< entry
->buf_count
; i
++) {
593 kfree(entry
->buflist
[i
].dev_private
);
595 kfree(entry
->buflist
);
597 entry
->buf_count
= 0;
603 * Add AGP buffers for DMA transfers.
605 * \param dev struct drm_device to which the buffers are to be added.
606 * \param request pointer to a struct drm_buf_desc describing the request.
607 * \return zero on success or a negative number on failure.
609 * After some sanity checks creates a drm_buf structure for each buffer and
610 * reallocates the buffer list of the same size order to accommodate the new
613 int drm_addbufs_agp(struct drm_device
* dev
, struct drm_buf_desc
* request
)
615 struct drm_device_dma
*dma
= dev
->dma
;
616 struct drm_buf_entry
*entry
;
617 struct drm_agp_mem
*agp_entry
;
619 unsigned long offset
;
620 unsigned long agp_offset
;
629 struct drm_buf
**temp_buflist
;
634 count
= request
->count
;
635 order
= drm_order(request
->size
);
638 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
639 ? PAGE_ALIGN(size
) : size
;
640 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
641 total
= PAGE_SIZE
<< page_order
;
644 agp_offset
= dev
->agp
->base
+ request
->agp_start
;
646 DRM_DEBUG("count: %d\n", count
);
647 DRM_DEBUG("order: %d\n", order
);
648 DRM_DEBUG("size: %d\n", size
);
649 DRM_DEBUG("agp_offset: %lx\n", agp_offset
);
650 DRM_DEBUG("alignment: %d\n", alignment
);
651 DRM_DEBUG("page_order: %d\n", page_order
);
652 DRM_DEBUG("total: %d\n", total
);
654 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
656 if (dev
->queue_count
)
657 return -EBUSY
; /* Not while in use */
659 /* Make sure buffers are located in AGP memory that we own */
661 list_for_each_entry(agp_entry
, &dev
->agp
->memory
, head
) {
662 if ((agp_offset
>= agp_entry
->bound
) &&
663 (agp_offset
+ total
* count
<= agp_entry
->bound
+ agp_entry
->pages
* PAGE_SIZE
)) {
668 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
669 DRM_DEBUG("zone invalid\n");
672 spin_lock(&dev
->count_lock
);
674 spin_unlock(&dev
->count_lock
);
677 atomic_inc(&dev
->buf_alloc
);
678 spin_unlock(&dev
->count_lock
);
680 mutex_lock(&dev
->struct_mutex
);
681 entry
= &dma
->bufs
[order
];
682 if (entry
->buf_count
) {
683 mutex_unlock(&dev
->struct_mutex
);
684 atomic_dec(&dev
->buf_alloc
);
685 return -ENOMEM
; /* May only call once for each order */
688 if (count
< 0 || count
> 4096) {
689 mutex_unlock(&dev
->struct_mutex
);
690 atomic_dec(&dev
->buf_alloc
);
694 entry
->buflist
= kmalloc(count
* sizeof(*entry
->buflist
), GFP_KERNEL
);
695 if (!entry
->buflist
) {
696 mutex_unlock(&dev
->struct_mutex
);
697 atomic_dec(&dev
->buf_alloc
);
700 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
702 entry
->buf_size
= size
;
703 entry
->page_order
= page_order
;
707 while (entry
->buf_count
< count
) {
708 buf
= &entry
->buflist
[entry
->buf_count
];
709 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
710 buf
->total
= alignment
;
714 buf
->offset
= (dma
->byte_count
+ offset
);
715 buf
->bus_address
= agp_offset
+ offset
;
716 buf
->address
= (void *)(agp_offset
+ offset
);
720 init_waitqueue_head(&buf
->dma_wait
);
721 buf
->file_priv
= NULL
;
723 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
724 buf
->dev_private
= kmalloc(buf
->dev_priv_size
, GFP_KERNEL
);
725 if (!buf
->dev_private
) {
726 /* Set count correctly so we free the proper amount. */
727 entry
->buf_count
= count
;
728 drm_cleanup_buf_error(dev
, entry
);
729 mutex_unlock(&dev
->struct_mutex
);
730 atomic_dec(&dev
->buf_alloc
);
733 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
735 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
739 byte_count
+= PAGE_SIZE
<< page_order
;
742 DRM_DEBUG("byte_count: %d\n", byte_count
);
744 temp_buflist
= krealloc(dma
->buflist
,
745 (dma
->buf_count
+ entry
->buf_count
) *
746 sizeof(*dma
->buflist
), GFP_KERNEL
);
748 /* Free the entry because it isn't valid */
749 drm_cleanup_buf_error(dev
, entry
);
750 mutex_unlock(&dev
->struct_mutex
);
751 atomic_dec(&dev
->buf_alloc
);
754 dma
->buflist
= temp_buflist
;
756 for (i
= 0; i
< entry
->buf_count
; i
++) {
757 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
760 dma
->buf_count
+= entry
->buf_count
;
761 dma
->seg_count
+= entry
->seg_count
;
762 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
763 dma
->byte_count
+= byte_count
;
765 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
766 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
768 mutex_unlock(&dev
->struct_mutex
);
770 request
->count
= entry
->buf_count
;
771 request
->size
= size
;
773 dma
->flags
= _DRM_DMA_USE_AGP
;
775 atomic_dec(&dev
->buf_alloc
);
778 EXPORT_SYMBOL(drm_addbufs_agp
);
779 #endif /* __OS_HAS_AGP */
781 int drm_addbufs_pci(struct drm_device
* dev
, struct drm_buf_desc
* request
)
783 struct drm_device_dma
*dma
= dev
->dma
;
789 struct drm_buf_entry
*entry
;
790 drm_dma_handle_t
*dmah
;
793 unsigned long offset
;
797 unsigned long *temp_pagelist
;
798 struct drm_buf
**temp_buflist
;
800 if (!drm_core_check_feature(dev
, DRIVER_PCI_DMA
))
806 if (!capable(CAP_SYS_ADMIN
))
809 count
= request
->count
;
810 order
= drm_order(request
->size
);
813 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
814 request
->count
, request
->size
, size
, order
, dev
->queue_count
);
816 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
818 if (dev
->queue_count
)
819 return -EBUSY
; /* Not while in use */
821 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
822 ? PAGE_ALIGN(size
) : size
;
823 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
824 total
= PAGE_SIZE
<< page_order
;
826 spin_lock(&dev
->count_lock
);
828 spin_unlock(&dev
->count_lock
);
831 atomic_inc(&dev
->buf_alloc
);
832 spin_unlock(&dev
->count_lock
);
834 mutex_lock(&dev
->struct_mutex
);
835 entry
= &dma
->bufs
[order
];
836 if (entry
->buf_count
) {
837 mutex_unlock(&dev
->struct_mutex
);
838 atomic_dec(&dev
->buf_alloc
);
839 return -ENOMEM
; /* May only call once for each order */
842 if (count
< 0 || count
> 4096) {
843 mutex_unlock(&dev
->struct_mutex
);
844 atomic_dec(&dev
->buf_alloc
);
848 entry
->buflist
= kmalloc(count
* sizeof(*entry
->buflist
), GFP_KERNEL
);
849 if (!entry
->buflist
) {
850 mutex_unlock(&dev
->struct_mutex
);
851 atomic_dec(&dev
->buf_alloc
);
854 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
856 entry
->seglist
= kmalloc(count
* sizeof(*entry
->seglist
), GFP_KERNEL
);
857 if (!entry
->seglist
) {
858 kfree(entry
->buflist
);
859 mutex_unlock(&dev
->struct_mutex
);
860 atomic_dec(&dev
->buf_alloc
);
863 memset(entry
->seglist
, 0, count
* sizeof(*entry
->seglist
));
865 /* Keep the original pagelist until we know all the allocations
868 temp_pagelist
= kmalloc((dma
->page_count
+ (count
<< page_order
)) *
869 sizeof(*dma
->pagelist
), GFP_KERNEL
);
870 if (!temp_pagelist
) {
871 kfree(entry
->buflist
);
872 kfree(entry
->seglist
);
873 mutex_unlock(&dev
->struct_mutex
);
874 atomic_dec(&dev
->buf_alloc
);
877 memcpy(temp_pagelist
,
878 dma
->pagelist
, dma
->page_count
* sizeof(*dma
->pagelist
));
879 DRM_DEBUG("pagelist: %d entries\n",
880 dma
->page_count
+ (count
<< page_order
));
882 entry
->buf_size
= size
;
883 entry
->page_order
= page_order
;
887 while (entry
->buf_count
< count
) {
889 dmah
= drm_pci_alloc(dev
, PAGE_SIZE
<< page_order
, 0x1000);
892 /* Set count correctly so we free the proper amount. */
893 entry
->buf_count
= count
;
894 entry
->seg_count
= count
;
895 drm_cleanup_buf_error(dev
, entry
);
896 kfree(temp_pagelist
);
897 mutex_unlock(&dev
->struct_mutex
);
898 atomic_dec(&dev
->buf_alloc
);
901 entry
->seglist
[entry
->seg_count
++] = dmah
;
902 for (i
= 0; i
< (1 << page_order
); i
++) {
903 DRM_DEBUG("page %d @ 0x%08lx\n",
904 dma
->page_count
+ page_count
,
905 (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
);
906 temp_pagelist
[dma
->page_count
+ page_count
++]
907 = (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
;
910 offset
+ size
<= total
&& entry
->buf_count
< count
;
911 offset
+= alignment
, ++entry
->buf_count
) {
912 buf
= &entry
->buflist
[entry
->buf_count
];
913 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
914 buf
->total
= alignment
;
917 buf
->offset
= (dma
->byte_count
+ byte_count
+ offset
);
918 buf
->address
= (void *)(dmah
->vaddr
+ offset
);
919 buf
->bus_address
= dmah
->busaddr
+ offset
;
923 init_waitqueue_head(&buf
->dma_wait
);
924 buf
->file_priv
= NULL
;
926 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
927 buf
->dev_private
= kmalloc(buf
->dev_priv_size
,
929 if (!buf
->dev_private
) {
930 /* Set count correctly so we free the proper amount. */
931 entry
->buf_count
= count
;
932 entry
->seg_count
= count
;
933 drm_cleanup_buf_error(dev
, entry
);
934 kfree(temp_pagelist
);
935 mutex_unlock(&dev
->struct_mutex
);
936 atomic_dec(&dev
->buf_alloc
);
939 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
941 DRM_DEBUG("buffer %d @ %p\n",
942 entry
->buf_count
, buf
->address
);
944 byte_count
+= PAGE_SIZE
<< page_order
;
947 temp_buflist
= krealloc(dma
->buflist
,
948 (dma
->buf_count
+ entry
->buf_count
) *
949 sizeof(*dma
->buflist
), GFP_KERNEL
);
951 /* Free the entry because it isn't valid */
952 drm_cleanup_buf_error(dev
, entry
);
953 kfree(temp_pagelist
);
954 mutex_unlock(&dev
->struct_mutex
);
955 atomic_dec(&dev
->buf_alloc
);
958 dma
->buflist
= temp_buflist
;
960 for (i
= 0; i
< entry
->buf_count
; i
++) {
961 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
964 /* No allocations failed, so now we can replace the orginal pagelist
967 if (dma
->page_count
) {
968 kfree(dma
->pagelist
);
970 dma
->pagelist
= temp_pagelist
;
972 dma
->buf_count
+= entry
->buf_count
;
973 dma
->seg_count
+= entry
->seg_count
;
974 dma
->page_count
+= entry
->seg_count
<< page_order
;
975 dma
->byte_count
+= PAGE_SIZE
* (entry
->seg_count
<< page_order
);
977 mutex_unlock(&dev
->struct_mutex
);
979 request
->count
= entry
->buf_count
;
980 request
->size
= size
;
982 if (request
->flags
& _DRM_PCI_BUFFER_RO
)
983 dma
->flags
= _DRM_DMA_USE_PCI_RO
;
985 atomic_dec(&dev
->buf_alloc
);
989 EXPORT_SYMBOL(drm_addbufs_pci
);
991 static int drm_addbufs_sg(struct drm_device
* dev
, struct drm_buf_desc
* request
)
993 struct drm_device_dma
*dma
= dev
->dma
;
994 struct drm_buf_entry
*entry
;
996 unsigned long offset
;
997 unsigned long agp_offset
;
1006 struct drm_buf
**temp_buflist
;
1008 if (!drm_core_check_feature(dev
, DRIVER_SG
))
1014 if (!capable(CAP_SYS_ADMIN
))
1017 count
= request
->count
;
1018 order
= drm_order(request
->size
);
1021 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
1022 ? PAGE_ALIGN(size
) : size
;
1023 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
1024 total
= PAGE_SIZE
<< page_order
;
1027 agp_offset
= request
->agp_start
;
1029 DRM_DEBUG("count: %d\n", count
);
1030 DRM_DEBUG("order: %d\n", order
);
1031 DRM_DEBUG("size: %d\n", size
);
1032 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
1033 DRM_DEBUG("alignment: %d\n", alignment
);
1034 DRM_DEBUG("page_order: %d\n", page_order
);
1035 DRM_DEBUG("total: %d\n", total
);
1037 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1039 if (dev
->queue_count
)
1040 return -EBUSY
; /* Not while in use */
1042 spin_lock(&dev
->count_lock
);
1044 spin_unlock(&dev
->count_lock
);
1047 atomic_inc(&dev
->buf_alloc
);
1048 spin_unlock(&dev
->count_lock
);
1050 mutex_lock(&dev
->struct_mutex
);
1051 entry
= &dma
->bufs
[order
];
1052 if (entry
->buf_count
) {
1053 mutex_unlock(&dev
->struct_mutex
);
1054 atomic_dec(&dev
->buf_alloc
);
1055 return -ENOMEM
; /* May only call once for each order */
1058 if (count
< 0 || count
> 4096) {
1059 mutex_unlock(&dev
->struct_mutex
);
1060 atomic_dec(&dev
->buf_alloc
);
1064 entry
->buflist
= kmalloc(count
* sizeof(*entry
->buflist
),
1066 if (!entry
->buflist
) {
1067 mutex_unlock(&dev
->struct_mutex
);
1068 atomic_dec(&dev
->buf_alloc
);
1071 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
1073 entry
->buf_size
= size
;
1074 entry
->page_order
= page_order
;
1078 while (entry
->buf_count
< count
) {
1079 buf
= &entry
->buflist
[entry
->buf_count
];
1080 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1081 buf
->total
= alignment
;
1085 buf
->offset
= (dma
->byte_count
+ offset
);
1086 buf
->bus_address
= agp_offset
+ offset
;
1087 buf
->address
= (void *)(agp_offset
+ offset
1088 + (unsigned long)dev
->sg
->virtual);
1092 init_waitqueue_head(&buf
->dma_wait
);
1093 buf
->file_priv
= NULL
;
1095 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1096 buf
->dev_private
= kmalloc(buf
->dev_priv_size
, GFP_KERNEL
);
1097 if (!buf
->dev_private
) {
1098 /* Set count correctly so we free the proper amount. */
1099 entry
->buf_count
= count
;
1100 drm_cleanup_buf_error(dev
, entry
);
1101 mutex_unlock(&dev
->struct_mutex
);
1102 atomic_dec(&dev
->buf_alloc
);
1106 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
1108 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1110 offset
+= alignment
;
1112 byte_count
+= PAGE_SIZE
<< page_order
;
1115 DRM_DEBUG("byte_count: %d\n", byte_count
);
1117 temp_buflist
= krealloc(dma
->buflist
,
1118 (dma
->buf_count
+ entry
->buf_count
) *
1119 sizeof(*dma
->buflist
), GFP_KERNEL
);
1120 if (!temp_buflist
) {
1121 /* Free the entry because it isn't valid */
1122 drm_cleanup_buf_error(dev
, entry
);
1123 mutex_unlock(&dev
->struct_mutex
);
1124 atomic_dec(&dev
->buf_alloc
);
1127 dma
->buflist
= temp_buflist
;
1129 for (i
= 0; i
< entry
->buf_count
; i
++) {
1130 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1133 dma
->buf_count
+= entry
->buf_count
;
1134 dma
->seg_count
+= entry
->seg_count
;
1135 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1136 dma
->byte_count
+= byte_count
;
1138 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1139 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1141 mutex_unlock(&dev
->struct_mutex
);
1143 request
->count
= entry
->buf_count
;
1144 request
->size
= size
;
1146 dma
->flags
= _DRM_DMA_USE_SG
;
1148 atomic_dec(&dev
->buf_alloc
);
1152 static int drm_addbufs_fb(struct drm_device
* dev
, struct drm_buf_desc
* request
)
1154 struct drm_device_dma
*dma
= dev
->dma
;
1155 struct drm_buf_entry
*entry
;
1156 struct drm_buf
*buf
;
1157 unsigned long offset
;
1158 unsigned long agp_offset
;
1167 struct drm_buf
**temp_buflist
;
1169 if (!drm_core_check_feature(dev
, DRIVER_FB_DMA
))
1175 if (!capable(CAP_SYS_ADMIN
))
1178 count
= request
->count
;
1179 order
= drm_order(request
->size
);
1182 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
1183 ? PAGE_ALIGN(size
) : size
;
1184 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
1185 total
= PAGE_SIZE
<< page_order
;
1188 agp_offset
= request
->agp_start
;
1190 DRM_DEBUG("count: %d\n", count
);
1191 DRM_DEBUG("order: %d\n", order
);
1192 DRM_DEBUG("size: %d\n", size
);
1193 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
1194 DRM_DEBUG("alignment: %d\n", alignment
);
1195 DRM_DEBUG("page_order: %d\n", page_order
);
1196 DRM_DEBUG("total: %d\n", total
);
1198 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1200 if (dev
->queue_count
)
1201 return -EBUSY
; /* Not while in use */
1203 spin_lock(&dev
->count_lock
);
1205 spin_unlock(&dev
->count_lock
);
1208 atomic_inc(&dev
->buf_alloc
);
1209 spin_unlock(&dev
->count_lock
);
1211 mutex_lock(&dev
->struct_mutex
);
1212 entry
= &dma
->bufs
[order
];
1213 if (entry
->buf_count
) {
1214 mutex_unlock(&dev
->struct_mutex
);
1215 atomic_dec(&dev
->buf_alloc
);
1216 return -ENOMEM
; /* May only call once for each order */
1219 if (count
< 0 || count
> 4096) {
1220 mutex_unlock(&dev
->struct_mutex
);
1221 atomic_dec(&dev
->buf_alloc
);
1225 entry
->buflist
= kmalloc(count
* sizeof(*entry
->buflist
),
1227 if (!entry
->buflist
) {
1228 mutex_unlock(&dev
->struct_mutex
);
1229 atomic_dec(&dev
->buf_alloc
);
1232 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
1234 entry
->buf_size
= size
;
1235 entry
->page_order
= page_order
;
1239 while (entry
->buf_count
< count
) {
1240 buf
= &entry
->buflist
[entry
->buf_count
];
1241 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1242 buf
->total
= alignment
;
1246 buf
->offset
= (dma
->byte_count
+ offset
);
1247 buf
->bus_address
= agp_offset
+ offset
;
1248 buf
->address
= (void *)(agp_offset
+ offset
);
1252 init_waitqueue_head(&buf
->dma_wait
);
1253 buf
->file_priv
= NULL
;
1255 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1256 buf
->dev_private
= kmalloc(buf
->dev_priv_size
, GFP_KERNEL
);
1257 if (!buf
->dev_private
) {
1258 /* Set count correctly so we free the proper amount. */
1259 entry
->buf_count
= count
;
1260 drm_cleanup_buf_error(dev
, entry
);
1261 mutex_unlock(&dev
->struct_mutex
);
1262 atomic_dec(&dev
->buf_alloc
);
1265 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
1267 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1269 offset
+= alignment
;
1271 byte_count
+= PAGE_SIZE
<< page_order
;
1274 DRM_DEBUG("byte_count: %d\n", byte_count
);
1276 temp_buflist
= krealloc(dma
->buflist
,
1277 (dma
->buf_count
+ entry
->buf_count
) *
1278 sizeof(*dma
->buflist
), GFP_KERNEL
);
1279 if (!temp_buflist
) {
1280 /* Free the entry because it isn't valid */
1281 drm_cleanup_buf_error(dev
, entry
);
1282 mutex_unlock(&dev
->struct_mutex
);
1283 atomic_dec(&dev
->buf_alloc
);
1286 dma
->buflist
= temp_buflist
;
1288 for (i
= 0; i
< entry
->buf_count
; i
++) {
1289 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1292 dma
->buf_count
+= entry
->buf_count
;
1293 dma
->seg_count
+= entry
->seg_count
;
1294 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1295 dma
->byte_count
+= byte_count
;
1297 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1298 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1300 mutex_unlock(&dev
->struct_mutex
);
1302 request
->count
= entry
->buf_count
;
1303 request
->size
= size
;
1305 dma
->flags
= _DRM_DMA_USE_FB
;
1307 atomic_dec(&dev
->buf_alloc
);
1313 * Add buffers for DMA transfers (ioctl).
1315 * \param inode device inode.
1316 * \param file_priv DRM file private.
1317 * \param cmd command.
1318 * \param arg pointer to a struct drm_buf_desc request.
1319 * \return zero on success or a negative number on failure.
1321 * According with the memory type specified in drm_buf_desc::flags and the
1322 * build options, it dispatches the call either to addbufs_agp(),
1323 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1324 * PCI memory respectively.
1326 int drm_addbufs(struct drm_device
*dev
, void *data
,
1327 struct drm_file
*file_priv
)
1329 struct drm_buf_desc
*request
= data
;
1332 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1336 if (request
->flags
& _DRM_AGP_BUFFER
)
1337 ret
= drm_addbufs_agp(dev
, request
);
1340 if (request
->flags
& _DRM_SG_BUFFER
)
1341 ret
= drm_addbufs_sg(dev
, request
);
1342 else if (request
->flags
& _DRM_FB_BUFFER
)
1343 ret
= drm_addbufs_fb(dev
, request
);
1345 ret
= drm_addbufs_pci(dev
, request
);
1351 * Get information about the buffer mappings.
1353 * This was originally mean for debugging purposes, or by a sophisticated
1354 * client library to determine how best to use the available buffers (e.g.,
1355 * large buffers can be used for image transfer).
1357 * \param inode device inode.
1358 * \param file_priv DRM file private.
1359 * \param cmd command.
1360 * \param arg pointer to a drm_buf_info structure.
1361 * \return zero on success or a negative number on failure.
1363 * Increments drm_device::buf_use while holding the drm_device::count_lock
1364 * lock, preventing of allocating more buffers after this call. Information
1365 * about each requested buffer is then copied into user space.
1367 int drm_infobufs(struct drm_device
*dev
, void *data
,
1368 struct drm_file
*file_priv
)
1370 struct drm_device_dma
*dma
= dev
->dma
;
1371 struct drm_buf_info
*request
= data
;
1375 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1381 spin_lock(&dev
->count_lock
);
1382 if (atomic_read(&dev
->buf_alloc
)) {
1383 spin_unlock(&dev
->count_lock
);
1386 ++dev
->buf_use
; /* Can't allocate more after this call */
1387 spin_unlock(&dev
->count_lock
);
1389 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1390 if (dma
->bufs
[i
].buf_count
)
1394 DRM_DEBUG("count = %d\n", count
);
1396 if (request
->count
>= count
) {
1397 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1398 if (dma
->bufs
[i
].buf_count
) {
1399 struct drm_buf_desc __user
*to
=
1400 &request
->list
[count
];
1401 struct drm_buf_entry
*from
= &dma
->bufs
[i
];
1402 struct drm_freelist
*list
= &dma
->bufs
[i
].freelist
;
1403 if (copy_to_user(&to
->count
,
1405 sizeof(from
->buf_count
)) ||
1406 copy_to_user(&to
->size
,
1408 sizeof(from
->buf_size
)) ||
1409 copy_to_user(&to
->low_mark
,
1411 sizeof(list
->low_mark
)) ||
1412 copy_to_user(&to
->high_mark
,
1414 sizeof(list
->high_mark
)))
1417 DRM_DEBUG("%d %d %d %d %d\n",
1419 dma
->bufs
[i
].buf_count
,
1420 dma
->bufs
[i
].buf_size
,
1421 dma
->bufs
[i
].freelist
.low_mark
,
1422 dma
->bufs
[i
].freelist
.high_mark
);
1427 request
->count
= count
;
1433 * Specifies a low and high water mark for buffer allocation
1435 * \param inode device inode.
1436 * \param file_priv DRM file private.
1437 * \param cmd command.
1438 * \param arg a pointer to a drm_buf_desc structure.
1439 * \return zero on success or a negative number on failure.
1441 * Verifies that the size order is bounded between the admissible orders and
1442 * updates the respective drm_device_dma::bufs entry low and high water mark.
1444 * \note This ioctl is deprecated and mostly never used.
1446 int drm_markbufs(struct drm_device
*dev
, void *data
,
1447 struct drm_file
*file_priv
)
1449 struct drm_device_dma
*dma
= dev
->dma
;
1450 struct drm_buf_desc
*request
= data
;
1452 struct drm_buf_entry
*entry
;
1454 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1460 DRM_DEBUG("%d, %d, %d\n",
1461 request
->size
, request
->low_mark
, request
->high_mark
);
1462 order
= drm_order(request
->size
);
1463 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1465 entry
= &dma
->bufs
[order
];
1467 if (request
->low_mark
< 0 || request
->low_mark
> entry
->buf_count
)
1469 if (request
->high_mark
< 0 || request
->high_mark
> entry
->buf_count
)
1472 entry
->freelist
.low_mark
= request
->low_mark
;
1473 entry
->freelist
.high_mark
= request
->high_mark
;
1479 * Unreserve the buffers in list, previously reserved using drmDMA.
1481 * \param inode device inode.
1482 * \param file_priv DRM file private.
1483 * \param cmd command.
1484 * \param arg pointer to a drm_buf_free structure.
1485 * \return zero on success or a negative number on failure.
1487 * Calls free_buffer() for each used buffer.
1488 * This function is primarily used for debugging.
1490 int drm_freebufs(struct drm_device
*dev
, void *data
,
1491 struct drm_file
*file_priv
)
1493 struct drm_device_dma
*dma
= dev
->dma
;
1494 struct drm_buf_free
*request
= data
;
1497 struct drm_buf
*buf
;
1499 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1505 DRM_DEBUG("%d\n", request
->count
);
1506 for (i
= 0; i
< request
->count
; i
++) {
1507 if (copy_from_user(&idx
, &request
->list
[i
], sizeof(idx
)))
1509 if (idx
< 0 || idx
>= dma
->buf_count
) {
1510 DRM_ERROR("Index %d (of %d max)\n",
1511 idx
, dma
->buf_count
- 1);
1514 buf
= dma
->buflist
[idx
];
1515 if (buf
->file_priv
!= file_priv
) {
1516 DRM_ERROR("Process %d freeing buffer not owned\n",
1517 task_pid_nr(current
));
1520 drm_free_buffer(dev
, buf
);
1527 * Maps all of the DMA buffers into client-virtual space (ioctl).
1529 * \param inode device inode.
1530 * \param file_priv DRM file private.
1531 * \param cmd command.
1532 * \param arg pointer to a drm_buf_map structure.
1533 * \return zero on success or a negative number on failure.
1535 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1536 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1537 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1540 int drm_mapbufs(struct drm_device
*dev
, void *data
,
1541 struct drm_file
*file_priv
)
1543 struct drm_device_dma
*dma
= dev
->dma
;
1546 unsigned long virtual;
1547 unsigned long address
;
1548 struct drm_buf_map
*request
= data
;
1551 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1557 spin_lock(&dev
->count_lock
);
1558 if (atomic_read(&dev
->buf_alloc
)) {
1559 spin_unlock(&dev
->count_lock
);
1562 dev
->buf_use
++; /* Can't allocate more after this call */
1563 spin_unlock(&dev
->count_lock
);
1565 if (request
->count
>= dma
->buf_count
) {
1566 if ((drm_core_has_AGP(dev
) && (dma
->flags
& _DRM_DMA_USE_AGP
))
1567 || (drm_core_check_feature(dev
, DRIVER_SG
)
1568 && (dma
->flags
& _DRM_DMA_USE_SG
))
1569 || (drm_core_check_feature(dev
, DRIVER_FB_DMA
)
1570 && (dma
->flags
& _DRM_DMA_USE_FB
))) {
1571 struct drm_local_map
*map
= dev
->agp_buffer_map
;
1572 unsigned long token
= dev
->agp_buffer_token
;
1578 down_write(¤t
->mm
->mmap_sem
);
1579 virtual = do_mmap(file_priv
->filp
, 0, map
->size
,
1580 PROT_READ
| PROT_WRITE
,
1583 up_write(¤t
->mm
->mmap_sem
);
1585 down_write(¤t
->mm
->mmap_sem
);
1586 virtual = do_mmap(file_priv
->filp
, 0, dma
->byte_count
,
1587 PROT_READ
| PROT_WRITE
,
1589 up_write(¤t
->mm
->mmap_sem
);
1591 if (virtual > -1024UL) {
1593 retcode
= (signed long)virtual;
1596 request
->virtual = (void __user
*)virtual;
1598 for (i
= 0; i
< dma
->buf_count
; i
++) {
1599 if (copy_to_user(&request
->list
[i
].idx
,
1600 &dma
->buflist
[i
]->idx
,
1601 sizeof(request
->list
[0].idx
))) {
1605 if (copy_to_user(&request
->list
[i
].total
,
1606 &dma
->buflist
[i
]->total
,
1607 sizeof(request
->list
[0].total
))) {
1611 if (copy_to_user(&request
->list
[i
].used
,
1612 &zero
, sizeof(zero
))) {
1616 address
= virtual + dma
->buflist
[i
]->offset
; /* *** */
1617 if (copy_to_user(&request
->list
[i
].address
,
1618 &address
, sizeof(address
))) {
1625 request
->count
= dma
->buf_count
;
1626 DRM_DEBUG("%d buffers, retcode = %d\n", request
->count
, retcode
);
1632 * Compute size order. Returns the exponent of the smaller power of two which
1633 * is greater or equal to given number.
1638 * \todo Can be made faster.
1640 int drm_order(unsigned long size
)
1645 for (order
= 0, tmp
= size
>> 1; tmp
; tmp
>>= 1, order
++) ;
1647 if (size
& (size
- 1))
1652 EXPORT_SYMBOL(drm_order
);