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>
39 unsigned long drm_get_resource_start(struct drm_device
*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(struct drm_device
*dev
, unsigned int resource
)
47 return pci_resource_len(dev
->pdev
, resource
);
50 EXPORT_SYMBOL(drm_get_resource_len
);
52 static struct drm_map_list
*drm_find_matching_map(struct drm_device
*dev
,
55 struct drm_map_list
*entry
;
56 list_for_each_entry(entry
, &dev
->maplist
, head
) {
57 if (entry
->map
&& map
->type
== entry
->map
->type
&&
58 ((entry
->map
->offset
== map
->offset
) ||
59 (map
->type
== _DRM_SHM
&& map
->flags
==_DRM_CONTAINS_LOCK
))) {
67 static int drm_map_handle(struct drm_device
*dev
, struct drm_hash_item
*hash
,
68 unsigned long user_token
, int hashed_handle
)
70 int use_hashed_handle
;
71 #if (BITS_PER_LONG == 64)
72 use_hashed_handle
= ((user_token
& 0xFFFFFFFF00000000UL
) || hashed_handle
);
73 #elif (BITS_PER_LONG == 32)
74 use_hashed_handle
= hashed_handle
;
76 #error Unsupported long size. Neither 64 nor 32 bits.
79 if (!use_hashed_handle
) {
81 hash
->key
= user_token
>> PAGE_SHIFT
;
82 ret
= drm_ht_insert_item(&dev
->map_hash
, hash
);
86 return drm_ht_just_insert_please(&dev
->map_hash
, hash
,
87 user_token
, 32 - PAGE_SHIFT
- 3,
88 0, DRM_MAP_HASH_OFFSET
>> PAGE_SHIFT
);
92 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
94 * \param inode device inode.
95 * \param file_priv DRM file private.
97 * \param arg pointer to a drm_map structure.
98 * \return zero on success or a negative value on error.
100 * Adjusts the memory offset to its absolute value according to the mapping
101 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
102 * applicable and if supported by the kernel.
104 static int drm_addmap_core(struct drm_device
* dev
, unsigned int offset
,
105 unsigned int size
, enum drm_map_type type
,
106 enum drm_map_flags flags
,
107 struct drm_map_list
** maplist
)
110 struct drm_map_list
*list
;
111 drm_dma_handle_t
*dmah
;
112 unsigned long user_token
;
115 map
= drm_alloc(sizeof(*map
), DRM_MEM_MAPS
);
119 map
->offset
= offset
;
124 /* Only allow shared memory to be removable since we only keep enough
125 * book keeping information about shared memory to allow for removal
126 * when processes fork.
128 if ((map
->flags
& _DRM_REMOVABLE
) && map
->type
!= _DRM_SHM
) {
129 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
132 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
133 map
->offset
, map
->size
, map
->type
);
134 if ((map
->offset
& (~PAGE_MASK
)) || (map
->size
& (~PAGE_MASK
))) {
135 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
143 case _DRM_FRAME_BUFFER
:
144 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
145 if (map
->offset
+ (map
->size
-1) < map
->offset
||
146 map
->offset
< virt_to_phys(high_memory
)) {
147 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
152 map
->offset
+= dev
->hose
->mem_space
->start
;
154 /* Some drivers preinitialize some maps, without the X Server
155 * needing to be aware of it. Therefore, we just return success
156 * when the server tries to create a duplicate map.
158 list
= drm_find_matching_map(dev
, map
);
160 if (list
->map
->size
!= map
->size
) {
161 DRM_DEBUG("Matching maps of type %d with "
162 "mismatched sizes, (%ld vs %ld)\n",
163 map
->type
, map
->size
,
165 list
->map
->size
= map
->size
;
168 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
173 if (drm_core_has_MTRR(dev
)) {
174 if (map
->type
== _DRM_FRAME_BUFFER
||
175 (map
->flags
& _DRM_WRITE_COMBINING
)) {
176 map
->mtrr
= mtrr_add(map
->offset
, map
->size
,
177 MTRR_TYPE_WRCOMB
, 1);
180 if (map
->type
== _DRM_REGISTERS
) {
181 map
->handle
= ioremap(map
->offset
, map
->size
);
183 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
190 list
= drm_find_matching_map(dev
, map
);
192 if(list
->map
->size
!= map
->size
) {
193 DRM_DEBUG("Matching maps of type %d with "
194 "mismatched sizes, (%ld vs %ld)\n",
195 map
->type
, map
->size
, list
->map
->size
);
196 list
->map
->size
= map
->size
;
199 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
203 map
->handle
= vmalloc_user(map
->size
);
204 DRM_DEBUG("%lu %d %p\n",
205 map
->size
, drm_order(map
->size
), map
->handle
);
207 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
210 map
->offset
= (unsigned long)map
->handle
;
211 if (map
->flags
& _DRM_CONTAINS_LOCK
) {
212 /* Prevent a 2nd X Server from creating a 2nd lock */
213 if (dev
->lock
.hw_lock
!= NULL
) {
215 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
218 dev
->sigdata
.lock
= dev
->lock
.hw_lock
= map
->handle
; /* Pointer to lock */
222 struct drm_agp_mem
*entry
;
225 if (!drm_core_has_AGP(dev
)) {
226 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
230 map
->offset
+= dev
->hose
->mem_space
->start
;
232 /* In some cases (i810 driver), user space may have already
233 * added the AGP base itself, because dev->agp->base previously
234 * only got set during AGP enable. So, only add the base
235 * address if the map's offset isn't already within the
238 if (map
->offset
< dev
->agp
->base
||
239 map
->offset
> dev
->agp
->base
+
240 dev
->agp
->agp_info
.aper_size
* 1024 * 1024 - 1) {
241 map
->offset
+= dev
->agp
->base
;
243 map
->mtrr
= dev
->agp
->agp_mtrr
; /* for getmap */
245 /* This assumes the DRM is in total control of AGP space.
246 * It's not always the case as AGP can be in the control
247 * of user space (i.e. i810 driver). So this loop will get
248 * skipped and we double check that dev->agp->memory is
249 * actually set as well as being invalid before EPERM'ing
251 list_for_each_entry(entry
, &dev
->agp
->memory
, head
) {
252 if ((map
->offset
>= entry
->bound
) &&
253 (map
->offset
+ map
->size
<= entry
->bound
+ entry
->pages
* PAGE_SIZE
)) {
258 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
259 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
262 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map
->offset
, map
->size
);
266 case _DRM_SCATTER_GATHER
:
268 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
271 map
->offset
+= (unsigned long)dev
->sg
->virtual;
273 case _DRM_CONSISTENT
:
274 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
275 * As we're limiting the address to 2^32-1 (or less),
276 * casting it down to 32 bits is no problem, but we
277 * need to point to a 64bit variable first. */
278 dmah
= drm_pci_alloc(dev
, map
->size
, map
->size
, 0xffffffffUL
);
280 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
283 map
->handle
= dmah
->vaddr
;
284 map
->offset
= (unsigned long)dmah
->busaddr
;
288 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
292 list
= drm_alloc(sizeof(*list
), DRM_MEM_MAPS
);
294 if (map
->type
== _DRM_REGISTERS
)
295 iounmap(map
->handle
);
296 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
299 memset(list
, 0, sizeof(*list
));
302 mutex_lock(&dev
->struct_mutex
);
303 list_add(&list
->head
, &dev
->maplist
);
305 /* Assign a 32-bit handle */
306 /* We do it here so that dev->struct_mutex protects the increment */
307 user_token
= (map
->type
== _DRM_SHM
) ? (unsigned long)map
->handle
:
309 ret
= drm_map_handle(dev
, &list
->hash
, user_token
, 0);
311 if (map
->type
== _DRM_REGISTERS
)
312 iounmap(map
->handle
);
313 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
314 drm_free(list
, sizeof(*list
), DRM_MEM_MAPS
);
315 mutex_unlock(&dev
->struct_mutex
);
319 list
->user_token
= list
->hash
.key
<< PAGE_SHIFT
;
320 mutex_unlock(&dev
->struct_mutex
);
326 int drm_addmap(struct drm_device
* dev
, unsigned int offset
,
327 unsigned int size
, enum drm_map_type type
,
328 enum drm_map_flags flags
, drm_local_map_t
** map_ptr
)
330 struct drm_map_list
*list
;
333 rc
= drm_addmap_core(dev
, offset
, size
, type
, flags
, &list
);
335 *map_ptr
= list
->map
;
339 EXPORT_SYMBOL(drm_addmap
);
341 int drm_addmap_ioctl(struct drm_device
*dev
, void *data
,
342 struct drm_file
*file_priv
)
344 struct drm_map
*map
= data
;
345 struct drm_map_list
*maplist
;
348 if (!(capable(CAP_SYS_ADMIN
) || map
->type
== _DRM_AGP
))
351 err
= drm_addmap_core(dev
, map
->offset
, map
->size
, map
->type
,
352 map
->flags
, &maplist
);
357 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
358 map
->handle
= (void *)(unsigned long)maplist
->user_token
;
363 * Remove a map private from list and deallocate resources if the mapping
366 * \param inode device inode.
367 * \param file_priv DRM file private.
368 * \param cmd command.
369 * \param arg pointer to a struct drm_map structure.
370 * \return zero on success or a negative value on error.
372 * Searches the map on drm_device::maplist, removes it from the list, see if
373 * its being used, and free any associate resource (such as MTRR's) if it's not
378 int drm_rmmap_locked(struct drm_device
*dev
, drm_local_map_t
*map
)
380 struct drm_map_list
*r_list
= NULL
, *list_t
;
381 drm_dma_handle_t dmah
;
384 /* Find the list entry for the map and remove it */
385 list_for_each_entry_safe(r_list
, list_t
, &dev
->maplist
, head
) {
386 if (r_list
->map
== map
) {
387 list_del(&r_list
->head
);
388 drm_ht_remove_key(&dev
->map_hash
,
389 r_list
->user_token
>> PAGE_SHIFT
);
390 drm_free(r_list
, sizeof(*r_list
), DRM_MEM_MAPS
);
401 iounmap(map
->handle
);
403 case _DRM_FRAME_BUFFER
:
404 if (drm_core_has_MTRR(dev
) && map
->mtrr
>= 0) {
406 retcode
= mtrr_del(map
->mtrr
, map
->offset
, map
->size
);
407 DRM_DEBUG("mtrr_del=%d\n", retcode
);
414 case _DRM_SCATTER_GATHER
:
416 case _DRM_CONSISTENT
:
417 dmah
.vaddr
= map
->handle
;
418 dmah
.busaddr
= map
->offset
;
419 dmah
.size
= map
->size
;
420 __drm_pci_free(dev
, &dmah
);
423 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
428 int drm_rmmap(struct drm_device
*dev
, drm_local_map_t
*map
)
432 mutex_lock(&dev
->struct_mutex
);
433 ret
= drm_rmmap_locked(dev
, map
);
434 mutex_unlock(&dev
->struct_mutex
);
438 EXPORT_SYMBOL(drm_rmmap
);
440 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
441 * the last close of the device, and this is necessary for cleanup when things
442 * exit uncleanly. Therefore, having userland manually remove mappings seems
443 * like a pointless exercise since they're going away anyway.
445 * One use case might be after addmap is allowed for normal users for SHM and
446 * gets used by drivers that the server doesn't need to care about. This seems
449 int drm_rmmap_ioctl(struct drm_device
*dev
, void *data
,
450 struct drm_file
*file_priv
)
452 struct drm_map
*request
= data
;
453 drm_local_map_t
*map
= NULL
;
454 struct drm_map_list
*r_list
;
457 mutex_lock(&dev
->struct_mutex
);
458 list_for_each_entry(r_list
, &dev
->maplist
, head
) {
460 r_list
->user_token
== (unsigned long)request
->handle
&&
461 r_list
->map
->flags
& _DRM_REMOVABLE
) {
467 /* List has wrapped around to the head pointer, or its empty we didn't
470 if (list_empty(&dev
->maplist
) || !map
) {
471 mutex_unlock(&dev
->struct_mutex
);
475 /* Register and framebuffer maps are permanent */
476 if ((map
->type
== _DRM_REGISTERS
) || (map
->type
== _DRM_FRAME_BUFFER
)) {
477 mutex_unlock(&dev
->struct_mutex
);
481 ret
= drm_rmmap_locked(dev
, map
);
483 mutex_unlock(&dev
->struct_mutex
);
489 * Cleanup after an error on one of the addbufs() functions.
491 * \param dev DRM device.
492 * \param entry buffer entry where the error occurred.
494 * Frees any pages and buffers associated with the given entry.
496 static void drm_cleanup_buf_error(struct drm_device
* dev
,
497 struct drm_buf_entry
* entry
)
501 if (entry
->seg_count
) {
502 for (i
= 0; i
< entry
->seg_count
; i
++) {
503 if (entry
->seglist
[i
]) {
504 drm_pci_free(dev
, entry
->seglist
[i
]);
507 drm_free(entry
->seglist
,
509 sizeof(*entry
->seglist
), DRM_MEM_SEGS
);
511 entry
->seg_count
= 0;
514 if (entry
->buf_count
) {
515 for (i
= 0; i
< entry
->buf_count
; i
++) {
516 if (entry
->buflist
[i
].dev_private
) {
517 drm_free(entry
->buflist
[i
].dev_private
,
518 entry
->buflist
[i
].dev_priv_size
,
522 drm_free(entry
->buflist
,
524 sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
526 entry
->buf_count
= 0;
532 * Add AGP buffers for DMA transfers.
534 * \param dev struct drm_device to which the buffers are to be added.
535 * \param request pointer to a struct drm_buf_desc describing the request.
536 * \return zero on success or a negative number on failure.
538 * After some sanity checks creates a drm_buf structure for each buffer and
539 * reallocates the buffer list of the same size order to accommodate the new
542 int drm_addbufs_agp(struct drm_device
* dev
, struct drm_buf_desc
* request
)
544 struct drm_device_dma
*dma
= dev
->dma
;
545 struct drm_buf_entry
*entry
;
546 struct drm_agp_mem
*agp_entry
;
548 unsigned long offset
;
549 unsigned long agp_offset
;
558 struct drm_buf
**temp_buflist
;
563 count
= request
->count
;
564 order
= drm_order(request
->size
);
567 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
568 ? PAGE_ALIGN(size
) : size
;
569 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
570 total
= PAGE_SIZE
<< page_order
;
573 agp_offset
= dev
->agp
->base
+ request
->agp_start
;
575 DRM_DEBUG("count: %d\n", count
);
576 DRM_DEBUG("order: %d\n", order
);
577 DRM_DEBUG("size: %d\n", size
);
578 DRM_DEBUG("agp_offset: %lx\n", agp_offset
);
579 DRM_DEBUG("alignment: %d\n", alignment
);
580 DRM_DEBUG("page_order: %d\n", page_order
);
581 DRM_DEBUG("total: %d\n", total
);
583 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
585 if (dev
->queue_count
)
586 return -EBUSY
; /* Not while in use */
588 /* Make sure buffers are located in AGP memory that we own */
590 list_for_each_entry(agp_entry
, &dev
->agp
->memory
, head
) {
591 if ((agp_offset
>= agp_entry
->bound
) &&
592 (agp_offset
+ total
* count
<= agp_entry
->bound
+ agp_entry
->pages
* PAGE_SIZE
)) {
597 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
598 DRM_DEBUG("zone invalid\n");
601 spin_lock(&dev
->count_lock
);
603 spin_unlock(&dev
->count_lock
);
606 atomic_inc(&dev
->buf_alloc
);
607 spin_unlock(&dev
->count_lock
);
609 mutex_lock(&dev
->struct_mutex
);
610 entry
= &dma
->bufs
[order
];
611 if (entry
->buf_count
) {
612 mutex_unlock(&dev
->struct_mutex
);
613 atomic_dec(&dev
->buf_alloc
);
614 return -ENOMEM
; /* May only call once for each order */
617 if (count
< 0 || count
> 4096) {
618 mutex_unlock(&dev
->struct_mutex
);
619 atomic_dec(&dev
->buf_alloc
);
623 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
625 if (!entry
->buflist
) {
626 mutex_unlock(&dev
->struct_mutex
);
627 atomic_dec(&dev
->buf_alloc
);
630 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
632 entry
->buf_size
= size
;
633 entry
->page_order
= page_order
;
637 while (entry
->buf_count
< count
) {
638 buf
= &entry
->buflist
[entry
->buf_count
];
639 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
640 buf
->total
= alignment
;
644 buf
->offset
= (dma
->byte_count
+ offset
);
645 buf
->bus_address
= agp_offset
+ offset
;
646 buf
->address
= (void *)(agp_offset
+ offset
);
650 init_waitqueue_head(&buf
->dma_wait
);
651 buf
->file_priv
= NULL
;
653 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
654 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
655 if (!buf
->dev_private
) {
656 /* Set count correctly so we free the proper amount. */
657 entry
->buf_count
= count
;
658 drm_cleanup_buf_error(dev
, entry
);
659 mutex_unlock(&dev
->struct_mutex
);
660 atomic_dec(&dev
->buf_alloc
);
663 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
665 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
669 byte_count
+= PAGE_SIZE
<< page_order
;
672 DRM_DEBUG("byte_count: %d\n", byte_count
);
674 temp_buflist
= drm_realloc(dma
->buflist
,
675 dma
->buf_count
* sizeof(*dma
->buflist
),
676 (dma
->buf_count
+ entry
->buf_count
)
677 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
679 /* Free the entry because it isn't valid */
680 drm_cleanup_buf_error(dev
, entry
);
681 mutex_unlock(&dev
->struct_mutex
);
682 atomic_dec(&dev
->buf_alloc
);
685 dma
->buflist
= temp_buflist
;
687 for (i
= 0; i
< entry
->buf_count
; i
++) {
688 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
691 dma
->buf_count
+= entry
->buf_count
;
692 dma
->seg_count
+= entry
->seg_count
;
693 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
694 dma
->byte_count
+= byte_count
;
696 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
697 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
699 mutex_unlock(&dev
->struct_mutex
);
701 request
->count
= entry
->buf_count
;
702 request
->size
= size
;
704 dma
->flags
= _DRM_DMA_USE_AGP
;
706 atomic_dec(&dev
->buf_alloc
);
709 EXPORT_SYMBOL(drm_addbufs_agp
);
710 #endif /* __OS_HAS_AGP */
712 int drm_addbufs_pci(struct drm_device
* dev
, struct drm_buf_desc
* request
)
714 struct drm_device_dma
*dma
= dev
->dma
;
720 struct drm_buf_entry
*entry
;
721 drm_dma_handle_t
*dmah
;
724 unsigned long offset
;
728 unsigned long *temp_pagelist
;
729 struct drm_buf
**temp_buflist
;
731 if (!drm_core_check_feature(dev
, DRIVER_PCI_DMA
))
737 if (!capable(CAP_SYS_ADMIN
))
740 count
= request
->count
;
741 order
= drm_order(request
->size
);
744 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
745 request
->count
, request
->size
, size
, order
, dev
->queue_count
);
747 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
749 if (dev
->queue_count
)
750 return -EBUSY
; /* Not while in use */
752 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
753 ? PAGE_ALIGN(size
) : size
;
754 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
755 total
= PAGE_SIZE
<< page_order
;
757 spin_lock(&dev
->count_lock
);
759 spin_unlock(&dev
->count_lock
);
762 atomic_inc(&dev
->buf_alloc
);
763 spin_unlock(&dev
->count_lock
);
765 mutex_lock(&dev
->struct_mutex
);
766 entry
= &dma
->bufs
[order
];
767 if (entry
->buf_count
) {
768 mutex_unlock(&dev
->struct_mutex
);
769 atomic_dec(&dev
->buf_alloc
);
770 return -ENOMEM
; /* May only call once for each order */
773 if (count
< 0 || count
> 4096) {
774 mutex_unlock(&dev
->struct_mutex
);
775 atomic_dec(&dev
->buf_alloc
);
779 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
781 if (!entry
->buflist
) {
782 mutex_unlock(&dev
->struct_mutex
);
783 atomic_dec(&dev
->buf_alloc
);
786 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
788 entry
->seglist
= drm_alloc(count
* sizeof(*entry
->seglist
),
790 if (!entry
->seglist
) {
791 drm_free(entry
->buflist
,
792 count
* sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
793 mutex_unlock(&dev
->struct_mutex
);
794 atomic_dec(&dev
->buf_alloc
);
797 memset(entry
->seglist
, 0, count
* sizeof(*entry
->seglist
));
799 /* Keep the original pagelist until we know all the allocations
802 temp_pagelist
= drm_alloc((dma
->page_count
+ (count
<< page_order
))
803 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
804 if (!temp_pagelist
) {
805 drm_free(entry
->buflist
,
806 count
* sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
807 drm_free(entry
->seglist
,
808 count
* sizeof(*entry
->seglist
), DRM_MEM_SEGS
);
809 mutex_unlock(&dev
->struct_mutex
);
810 atomic_dec(&dev
->buf_alloc
);
813 memcpy(temp_pagelist
,
814 dma
->pagelist
, dma
->page_count
* sizeof(*dma
->pagelist
));
815 DRM_DEBUG("pagelist: %d entries\n",
816 dma
->page_count
+ (count
<< page_order
));
818 entry
->buf_size
= size
;
819 entry
->page_order
= page_order
;
823 while (entry
->buf_count
< count
) {
825 dmah
= drm_pci_alloc(dev
, PAGE_SIZE
<< page_order
, 0x1000, 0xfffffffful
);
828 /* Set count correctly so we free the proper amount. */
829 entry
->buf_count
= count
;
830 entry
->seg_count
= count
;
831 drm_cleanup_buf_error(dev
, entry
);
832 drm_free(temp_pagelist
,
833 (dma
->page_count
+ (count
<< page_order
))
834 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
835 mutex_unlock(&dev
->struct_mutex
);
836 atomic_dec(&dev
->buf_alloc
);
839 entry
->seglist
[entry
->seg_count
++] = dmah
;
840 for (i
= 0; i
< (1 << page_order
); i
++) {
841 DRM_DEBUG("page %d @ 0x%08lx\n",
842 dma
->page_count
+ page_count
,
843 (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
);
844 temp_pagelist
[dma
->page_count
+ page_count
++]
845 = (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
;
848 offset
+ size
<= total
&& entry
->buf_count
< count
;
849 offset
+= alignment
, ++entry
->buf_count
) {
850 buf
= &entry
->buflist
[entry
->buf_count
];
851 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
852 buf
->total
= alignment
;
855 buf
->offset
= (dma
->byte_count
+ byte_count
+ offset
);
856 buf
->address
= (void *)(dmah
->vaddr
+ offset
);
857 buf
->bus_address
= dmah
->busaddr
+ offset
;
861 init_waitqueue_head(&buf
->dma_wait
);
862 buf
->file_priv
= NULL
;
864 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
865 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
,
867 if (!buf
->dev_private
) {
868 /* Set count correctly so we free the proper amount. */
869 entry
->buf_count
= count
;
870 entry
->seg_count
= count
;
871 drm_cleanup_buf_error(dev
, entry
);
872 drm_free(temp_pagelist
,
874 (count
<< page_order
))
875 * sizeof(*dma
->pagelist
),
877 mutex_unlock(&dev
->struct_mutex
);
878 atomic_dec(&dev
->buf_alloc
);
881 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
883 DRM_DEBUG("buffer %d @ %p\n",
884 entry
->buf_count
, buf
->address
);
886 byte_count
+= PAGE_SIZE
<< page_order
;
889 temp_buflist
= drm_realloc(dma
->buflist
,
890 dma
->buf_count
* sizeof(*dma
->buflist
),
891 (dma
->buf_count
+ entry
->buf_count
)
892 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
894 /* Free the entry because it isn't valid */
895 drm_cleanup_buf_error(dev
, entry
);
896 drm_free(temp_pagelist
,
897 (dma
->page_count
+ (count
<< page_order
))
898 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
899 mutex_unlock(&dev
->struct_mutex
);
900 atomic_dec(&dev
->buf_alloc
);
903 dma
->buflist
= temp_buflist
;
905 for (i
= 0; i
< entry
->buf_count
; i
++) {
906 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
909 /* No allocations failed, so now we can replace the orginal pagelist
912 if (dma
->page_count
) {
913 drm_free(dma
->pagelist
,
914 dma
->page_count
* sizeof(*dma
->pagelist
),
917 dma
->pagelist
= temp_pagelist
;
919 dma
->buf_count
+= entry
->buf_count
;
920 dma
->seg_count
+= entry
->seg_count
;
921 dma
->page_count
+= entry
->seg_count
<< page_order
;
922 dma
->byte_count
+= PAGE_SIZE
* (entry
->seg_count
<< page_order
);
924 mutex_unlock(&dev
->struct_mutex
);
926 request
->count
= entry
->buf_count
;
927 request
->size
= size
;
929 if (request
->flags
& _DRM_PCI_BUFFER_RO
)
930 dma
->flags
= _DRM_DMA_USE_PCI_RO
;
932 atomic_dec(&dev
->buf_alloc
);
936 EXPORT_SYMBOL(drm_addbufs_pci
);
938 static int drm_addbufs_sg(struct drm_device
* dev
, struct drm_buf_desc
* request
)
940 struct drm_device_dma
*dma
= dev
->dma
;
941 struct drm_buf_entry
*entry
;
943 unsigned long offset
;
944 unsigned long agp_offset
;
953 struct drm_buf
**temp_buflist
;
955 if (!drm_core_check_feature(dev
, DRIVER_SG
))
961 if (!capable(CAP_SYS_ADMIN
))
964 count
= request
->count
;
965 order
= drm_order(request
->size
);
968 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
969 ? PAGE_ALIGN(size
) : size
;
970 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
971 total
= PAGE_SIZE
<< page_order
;
974 agp_offset
= request
->agp_start
;
976 DRM_DEBUG("count: %d\n", count
);
977 DRM_DEBUG("order: %d\n", order
);
978 DRM_DEBUG("size: %d\n", size
);
979 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
980 DRM_DEBUG("alignment: %d\n", alignment
);
981 DRM_DEBUG("page_order: %d\n", page_order
);
982 DRM_DEBUG("total: %d\n", total
);
984 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
986 if (dev
->queue_count
)
987 return -EBUSY
; /* Not while in use */
989 spin_lock(&dev
->count_lock
);
991 spin_unlock(&dev
->count_lock
);
994 atomic_inc(&dev
->buf_alloc
);
995 spin_unlock(&dev
->count_lock
);
997 mutex_lock(&dev
->struct_mutex
);
998 entry
= &dma
->bufs
[order
];
999 if (entry
->buf_count
) {
1000 mutex_unlock(&dev
->struct_mutex
);
1001 atomic_dec(&dev
->buf_alloc
);
1002 return -ENOMEM
; /* May only call once for each order */
1005 if (count
< 0 || count
> 4096) {
1006 mutex_unlock(&dev
->struct_mutex
);
1007 atomic_dec(&dev
->buf_alloc
);
1011 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
1013 if (!entry
->buflist
) {
1014 mutex_unlock(&dev
->struct_mutex
);
1015 atomic_dec(&dev
->buf_alloc
);
1018 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
1020 entry
->buf_size
= size
;
1021 entry
->page_order
= page_order
;
1025 while (entry
->buf_count
< count
) {
1026 buf
= &entry
->buflist
[entry
->buf_count
];
1027 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1028 buf
->total
= alignment
;
1032 buf
->offset
= (dma
->byte_count
+ offset
);
1033 buf
->bus_address
= agp_offset
+ offset
;
1034 buf
->address
= (void *)(agp_offset
+ offset
1035 + (unsigned long)dev
->sg
->virtual);
1039 init_waitqueue_head(&buf
->dma_wait
);
1040 buf
->file_priv
= NULL
;
1042 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1043 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
1044 if (!buf
->dev_private
) {
1045 /* Set count correctly so we free the proper amount. */
1046 entry
->buf_count
= count
;
1047 drm_cleanup_buf_error(dev
, entry
);
1048 mutex_unlock(&dev
->struct_mutex
);
1049 atomic_dec(&dev
->buf_alloc
);
1053 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
1055 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1057 offset
+= alignment
;
1059 byte_count
+= PAGE_SIZE
<< page_order
;
1062 DRM_DEBUG("byte_count: %d\n", byte_count
);
1064 temp_buflist
= drm_realloc(dma
->buflist
,
1065 dma
->buf_count
* sizeof(*dma
->buflist
),
1066 (dma
->buf_count
+ entry
->buf_count
)
1067 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
1068 if (!temp_buflist
) {
1069 /* Free the entry because it isn't valid */
1070 drm_cleanup_buf_error(dev
, entry
);
1071 mutex_unlock(&dev
->struct_mutex
);
1072 atomic_dec(&dev
->buf_alloc
);
1075 dma
->buflist
= temp_buflist
;
1077 for (i
= 0; i
< entry
->buf_count
; i
++) {
1078 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1081 dma
->buf_count
+= entry
->buf_count
;
1082 dma
->seg_count
+= entry
->seg_count
;
1083 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1084 dma
->byte_count
+= byte_count
;
1086 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1087 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1089 mutex_unlock(&dev
->struct_mutex
);
1091 request
->count
= entry
->buf_count
;
1092 request
->size
= size
;
1094 dma
->flags
= _DRM_DMA_USE_SG
;
1096 atomic_dec(&dev
->buf_alloc
);
1100 static int drm_addbufs_fb(struct drm_device
* dev
, struct drm_buf_desc
* request
)
1102 struct drm_device_dma
*dma
= dev
->dma
;
1103 struct drm_buf_entry
*entry
;
1104 struct drm_buf
*buf
;
1105 unsigned long offset
;
1106 unsigned long agp_offset
;
1115 struct drm_buf
**temp_buflist
;
1117 if (!drm_core_check_feature(dev
, DRIVER_FB_DMA
))
1123 if (!capable(CAP_SYS_ADMIN
))
1126 count
= request
->count
;
1127 order
= drm_order(request
->size
);
1130 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
1131 ? PAGE_ALIGN(size
) : size
;
1132 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
1133 total
= PAGE_SIZE
<< page_order
;
1136 agp_offset
= request
->agp_start
;
1138 DRM_DEBUG("count: %d\n", count
);
1139 DRM_DEBUG("order: %d\n", order
);
1140 DRM_DEBUG("size: %d\n", size
);
1141 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
1142 DRM_DEBUG("alignment: %d\n", alignment
);
1143 DRM_DEBUG("page_order: %d\n", page_order
);
1144 DRM_DEBUG("total: %d\n", total
);
1146 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1148 if (dev
->queue_count
)
1149 return -EBUSY
; /* Not while in use */
1151 spin_lock(&dev
->count_lock
);
1153 spin_unlock(&dev
->count_lock
);
1156 atomic_inc(&dev
->buf_alloc
);
1157 spin_unlock(&dev
->count_lock
);
1159 mutex_lock(&dev
->struct_mutex
);
1160 entry
= &dma
->bufs
[order
];
1161 if (entry
->buf_count
) {
1162 mutex_unlock(&dev
->struct_mutex
);
1163 atomic_dec(&dev
->buf_alloc
);
1164 return -ENOMEM
; /* May only call once for each order */
1167 if (count
< 0 || count
> 4096) {
1168 mutex_unlock(&dev
->struct_mutex
);
1169 atomic_dec(&dev
->buf_alloc
);
1173 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
1175 if (!entry
->buflist
) {
1176 mutex_unlock(&dev
->struct_mutex
);
1177 atomic_dec(&dev
->buf_alloc
);
1180 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
1182 entry
->buf_size
= size
;
1183 entry
->page_order
= page_order
;
1187 while (entry
->buf_count
< count
) {
1188 buf
= &entry
->buflist
[entry
->buf_count
];
1189 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1190 buf
->total
= alignment
;
1194 buf
->offset
= (dma
->byte_count
+ offset
);
1195 buf
->bus_address
= agp_offset
+ offset
;
1196 buf
->address
= (void *)(agp_offset
+ offset
);
1200 init_waitqueue_head(&buf
->dma_wait
);
1201 buf
->file_priv
= NULL
;
1203 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1204 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
1205 if (!buf
->dev_private
) {
1206 /* Set count correctly so we free the proper amount. */
1207 entry
->buf_count
= count
;
1208 drm_cleanup_buf_error(dev
, entry
);
1209 mutex_unlock(&dev
->struct_mutex
);
1210 atomic_dec(&dev
->buf_alloc
);
1213 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
1215 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1217 offset
+= alignment
;
1219 byte_count
+= PAGE_SIZE
<< page_order
;
1222 DRM_DEBUG("byte_count: %d\n", byte_count
);
1224 temp_buflist
= drm_realloc(dma
->buflist
,
1225 dma
->buf_count
* sizeof(*dma
->buflist
),
1226 (dma
->buf_count
+ entry
->buf_count
)
1227 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
1228 if (!temp_buflist
) {
1229 /* Free the entry because it isn't valid */
1230 drm_cleanup_buf_error(dev
, entry
);
1231 mutex_unlock(&dev
->struct_mutex
);
1232 atomic_dec(&dev
->buf_alloc
);
1235 dma
->buflist
= temp_buflist
;
1237 for (i
= 0; i
< entry
->buf_count
; i
++) {
1238 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1241 dma
->buf_count
+= entry
->buf_count
;
1242 dma
->seg_count
+= entry
->seg_count
;
1243 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1244 dma
->byte_count
+= byte_count
;
1246 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1247 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1249 mutex_unlock(&dev
->struct_mutex
);
1251 request
->count
= entry
->buf_count
;
1252 request
->size
= size
;
1254 dma
->flags
= _DRM_DMA_USE_FB
;
1256 atomic_dec(&dev
->buf_alloc
);
1262 * Add buffers for DMA transfers (ioctl).
1264 * \param inode device inode.
1265 * \param file_priv DRM file private.
1266 * \param cmd command.
1267 * \param arg pointer to a struct drm_buf_desc request.
1268 * \return zero on success or a negative number on failure.
1270 * According with the memory type specified in drm_buf_desc::flags and the
1271 * build options, it dispatches the call either to addbufs_agp(),
1272 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1273 * PCI memory respectively.
1275 int drm_addbufs(struct drm_device
*dev
, void *data
,
1276 struct drm_file
*file_priv
)
1278 struct drm_buf_desc
*request
= data
;
1281 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1285 if (request
->flags
& _DRM_AGP_BUFFER
)
1286 ret
= drm_addbufs_agp(dev
, request
);
1289 if (request
->flags
& _DRM_SG_BUFFER
)
1290 ret
= drm_addbufs_sg(dev
, request
);
1291 else if (request
->flags
& _DRM_FB_BUFFER
)
1292 ret
= drm_addbufs_fb(dev
, request
);
1294 ret
= drm_addbufs_pci(dev
, request
);
1300 * Get information about the buffer mappings.
1302 * This was originally mean for debugging purposes, or by a sophisticated
1303 * client library to determine how best to use the available buffers (e.g.,
1304 * large buffers can be used for image transfer).
1306 * \param inode device inode.
1307 * \param file_priv DRM file private.
1308 * \param cmd command.
1309 * \param arg pointer to a drm_buf_info structure.
1310 * \return zero on success or a negative number on failure.
1312 * Increments drm_device::buf_use while holding the drm_device::count_lock
1313 * lock, preventing of allocating more buffers after this call. Information
1314 * about each requested buffer is then copied into user space.
1316 int drm_infobufs(struct drm_device
*dev
, void *data
,
1317 struct drm_file
*file_priv
)
1319 struct drm_device_dma
*dma
= dev
->dma
;
1320 struct drm_buf_info
*request
= data
;
1324 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1330 spin_lock(&dev
->count_lock
);
1331 if (atomic_read(&dev
->buf_alloc
)) {
1332 spin_unlock(&dev
->count_lock
);
1335 ++dev
->buf_use
; /* Can't allocate more after this call */
1336 spin_unlock(&dev
->count_lock
);
1338 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1339 if (dma
->bufs
[i
].buf_count
)
1343 DRM_DEBUG("count = %d\n", count
);
1345 if (request
->count
>= count
) {
1346 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1347 if (dma
->bufs
[i
].buf_count
) {
1348 struct drm_buf_desc __user
*to
=
1349 &request
->list
[count
];
1350 struct drm_buf_entry
*from
= &dma
->bufs
[i
];
1351 struct drm_freelist
*list
= &dma
->bufs
[i
].freelist
;
1352 if (copy_to_user(&to
->count
,
1354 sizeof(from
->buf_count
)) ||
1355 copy_to_user(&to
->size
,
1357 sizeof(from
->buf_size
)) ||
1358 copy_to_user(&to
->low_mark
,
1360 sizeof(list
->low_mark
)) ||
1361 copy_to_user(&to
->high_mark
,
1363 sizeof(list
->high_mark
)))
1366 DRM_DEBUG("%d %d %d %d %d\n",
1368 dma
->bufs
[i
].buf_count
,
1369 dma
->bufs
[i
].buf_size
,
1370 dma
->bufs
[i
].freelist
.low_mark
,
1371 dma
->bufs
[i
].freelist
.high_mark
);
1376 request
->count
= count
;
1382 * Specifies a low and high water mark for buffer allocation
1384 * \param inode device inode.
1385 * \param file_priv DRM file private.
1386 * \param cmd command.
1387 * \param arg a pointer to a drm_buf_desc structure.
1388 * \return zero on success or a negative number on failure.
1390 * Verifies that the size order is bounded between the admissible orders and
1391 * updates the respective drm_device_dma::bufs entry low and high water mark.
1393 * \note This ioctl is deprecated and mostly never used.
1395 int drm_markbufs(struct drm_device
*dev
, void *data
,
1396 struct drm_file
*file_priv
)
1398 struct drm_device_dma
*dma
= dev
->dma
;
1399 struct drm_buf_desc
*request
= data
;
1401 struct drm_buf_entry
*entry
;
1403 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1409 DRM_DEBUG("%d, %d, %d\n",
1410 request
->size
, request
->low_mark
, request
->high_mark
);
1411 order
= drm_order(request
->size
);
1412 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1414 entry
= &dma
->bufs
[order
];
1416 if (request
->low_mark
< 0 || request
->low_mark
> entry
->buf_count
)
1418 if (request
->high_mark
< 0 || request
->high_mark
> entry
->buf_count
)
1421 entry
->freelist
.low_mark
= request
->low_mark
;
1422 entry
->freelist
.high_mark
= request
->high_mark
;
1428 * Unreserve the buffers in list, previously reserved using drmDMA.
1430 * \param inode device inode.
1431 * \param file_priv DRM file private.
1432 * \param cmd command.
1433 * \param arg pointer to a drm_buf_free structure.
1434 * \return zero on success or a negative number on failure.
1436 * Calls free_buffer() for each used buffer.
1437 * This function is primarily used for debugging.
1439 int drm_freebufs(struct drm_device
*dev
, void *data
,
1440 struct drm_file
*file_priv
)
1442 struct drm_device_dma
*dma
= dev
->dma
;
1443 struct drm_buf_free
*request
= data
;
1446 struct drm_buf
*buf
;
1448 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1454 DRM_DEBUG("%d\n", request
->count
);
1455 for (i
= 0; i
< request
->count
; i
++) {
1456 if (copy_from_user(&idx
, &request
->list
[i
], sizeof(idx
)))
1458 if (idx
< 0 || idx
>= dma
->buf_count
) {
1459 DRM_ERROR("Index %d (of %d max)\n",
1460 idx
, dma
->buf_count
- 1);
1463 buf
= dma
->buflist
[idx
];
1464 if (buf
->file_priv
!= file_priv
) {
1465 DRM_ERROR("Process %d freeing buffer not owned\n",
1466 task_pid_nr(current
));
1469 drm_free_buffer(dev
, buf
);
1476 * Maps all of the DMA buffers into client-virtual space (ioctl).
1478 * \param inode device inode.
1479 * \param file_priv DRM file private.
1480 * \param cmd command.
1481 * \param arg pointer to a drm_buf_map structure.
1482 * \return zero on success or a negative number on failure.
1484 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1485 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1486 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1489 int drm_mapbufs(struct drm_device
*dev
, void *data
,
1490 struct drm_file
*file_priv
)
1492 struct drm_device_dma
*dma
= dev
->dma
;
1495 unsigned long virtual;
1496 unsigned long address
;
1497 struct drm_buf_map
*request
= data
;
1500 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1506 spin_lock(&dev
->count_lock
);
1507 if (atomic_read(&dev
->buf_alloc
)) {
1508 spin_unlock(&dev
->count_lock
);
1511 dev
->buf_use
++; /* Can't allocate more after this call */
1512 spin_unlock(&dev
->count_lock
);
1514 if (request
->count
>= dma
->buf_count
) {
1515 if ((drm_core_has_AGP(dev
) && (dma
->flags
& _DRM_DMA_USE_AGP
))
1516 || (drm_core_check_feature(dev
, DRIVER_SG
)
1517 && (dma
->flags
& _DRM_DMA_USE_SG
))
1518 || (drm_core_check_feature(dev
, DRIVER_FB_DMA
)
1519 && (dma
->flags
& _DRM_DMA_USE_FB
))) {
1520 struct drm_map
*map
= dev
->agp_buffer_map
;
1521 unsigned long token
= dev
->agp_buffer_token
;
1527 down_write(¤t
->mm
->mmap_sem
);
1528 virtual = do_mmap(file_priv
->filp
, 0, map
->size
,
1529 PROT_READ
| PROT_WRITE
,
1532 up_write(¤t
->mm
->mmap_sem
);
1534 down_write(¤t
->mm
->mmap_sem
);
1535 virtual = do_mmap(file_priv
->filp
, 0, dma
->byte_count
,
1536 PROT_READ
| PROT_WRITE
,
1538 up_write(¤t
->mm
->mmap_sem
);
1540 if (virtual > -1024UL) {
1542 retcode
= (signed long)virtual;
1545 request
->virtual = (void __user
*)virtual;
1547 for (i
= 0; i
< dma
->buf_count
; i
++) {
1548 if (copy_to_user(&request
->list
[i
].idx
,
1549 &dma
->buflist
[i
]->idx
,
1550 sizeof(request
->list
[0].idx
))) {
1554 if (copy_to_user(&request
->list
[i
].total
,
1555 &dma
->buflist
[i
]->total
,
1556 sizeof(request
->list
[0].total
))) {
1560 if (copy_to_user(&request
->list
[i
].used
,
1561 &zero
, sizeof(zero
))) {
1565 address
= virtual + dma
->buflist
[i
]->offset
; /* *** */
1566 if (copy_to_user(&request
->list
[i
].address
,
1567 &address
, sizeof(address
))) {
1574 request
->count
= dma
->buf_count
;
1575 DRM_DEBUG("%d buffers, retcode = %d\n", request
->count
, retcode
);
1581 * Compute size order. Returns the exponent of the smaller power of two which
1582 * is greater or equal to given number.
1587 * \todo Can be made faster.
1589 int drm_order(unsigned long size
)
1594 for (order
= 0, tmp
= size
>> 1; tmp
; tmp
>>= 1, order
++) ;
1596 if (size
& (size
- 1))
1601 EXPORT_SYMBOL(drm_order
);