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 resource_size_t
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 resource_size_t
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
,
53 struct drm_local_map
*map
)
55 struct drm_map_list
*entry
;
56 list_for_each_entry(entry
, &dev
->maplist
, head
) {
57 if (entry
->map
&& (entry
->master
== dev
->primary
->master
) && (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 * Core function to create a range of memory available for mapping by a
95 * Adjusts the memory offset to its absolute value according to the mapping
96 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
97 * applicable and if supported by the kernel.
99 static int drm_addmap_core(struct drm_device
* dev
, unsigned int offset
,
100 unsigned int size
, enum drm_map_type type
,
101 enum drm_map_flags flags
,
102 struct drm_map_list
** maplist
)
104 struct drm_local_map
*map
;
105 struct drm_map_list
*list
;
106 drm_dma_handle_t
*dmah
;
107 unsigned long user_token
;
110 map
= drm_alloc(sizeof(*map
), DRM_MEM_MAPS
);
114 map
->offset
= offset
;
119 /* Only allow shared memory to be removable since we only keep enough
120 * book keeping information about shared memory to allow for removal
121 * when processes fork.
123 if ((map
->flags
& _DRM_REMOVABLE
) && map
->type
!= _DRM_SHM
) {
124 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
127 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
128 map
->offset
, map
->size
, map
->type
);
129 if ((map
->offset
& (~PAGE_MASK
)) || (map
->size
& (~PAGE_MASK
))) {
130 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
138 case _DRM_FRAME_BUFFER
:
139 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
140 if (map
->offset
+ (map
->size
-1) < map
->offset
||
141 map
->offset
< virt_to_phys(high_memory
)) {
142 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
147 map
->offset
+= dev
->hose
->mem_space
->start
;
149 /* Some drivers preinitialize some maps, without the X Server
150 * needing to be aware of it. Therefore, we just return success
151 * when the server tries to create a duplicate map.
153 list
= drm_find_matching_map(dev
, map
);
155 if (list
->map
->size
!= map
->size
) {
156 DRM_DEBUG("Matching maps of type %d with "
157 "mismatched sizes, (%ld vs %ld)\n",
158 map
->type
, map
->size
,
160 list
->map
->size
= map
->size
;
163 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
168 if (drm_core_has_MTRR(dev
)) {
169 if (map
->type
== _DRM_FRAME_BUFFER
||
170 (map
->flags
& _DRM_WRITE_COMBINING
)) {
171 map
->mtrr
= mtrr_add(map
->offset
, map
->size
,
172 MTRR_TYPE_WRCOMB
, 1);
175 if (map
->type
== _DRM_REGISTERS
) {
176 map
->handle
= ioremap(map
->offset
, map
->size
);
178 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
185 list
= drm_find_matching_map(dev
, map
);
187 if(list
->map
->size
!= map
->size
) {
188 DRM_DEBUG("Matching maps of type %d with "
189 "mismatched sizes, (%ld vs %ld)\n",
190 map
->type
, map
->size
, list
->map
->size
);
191 list
->map
->size
= map
->size
;
194 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
198 map
->handle
= vmalloc_user(map
->size
);
199 DRM_DEBUG("%lu %d %p\n",
200 map
->size
, drm_order(map
->size
), map
->handle
);
202 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
205 map
->offset
= (unsigned long)map
->handle
;
206 if (map
->flags
& _DRM_CONTAINS_LOCK
) {
207 /* Prevent a 2nd X Server from creating a 2nd lock */
208 if (dev
->primary
->master
->lock
.hw_lock
!= NULL
) {
210 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
213 dev
->sigdata
.lock
= dev
->primary
->master
->lock
.hw_lock
= map
->handle
; /* Pointer to lock */
217 struct drm_agp_mem
*entry
;
220 if (!drm_core_has_AGP(dev
)) {
221 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
225 map
->offset
+= dev
->hose
->mem_space
->start
;
227 /* In some cases (i810 driver), user space may have already
228 * added the AGP base itself, because dev->agp->base previously
229 * only got set during AGP enable. So, only add the base
230 * address if the map's offset isn't already within the
233 if (map
->offset
< dev
->agp
->base
||
234 map
->offset
> dev
->agp
->base
+
235 dev
->agp
->agp_info
.aper_size
* 1024 * 1024 - 1) {
236 map
->offset
+= dev
->agp
->base
;
238 map
->mtrr
= dev
->agp
->agp_mtrr
; /* for getmap */
240 /* This assumes the DRM is in total control of AGP space.
241 * It's not always the case as AGP can be in the control
242 * of user space (i.e. i810 driver). So this loop will get
243 * skipped and we double check that dev->agp->memory is
244 * actually set as well as being invalid before EPERM'ing
246 list_for_each_entry(entry
, &dev
->agp
->memory
, head
) {
247 if ((map
->offset
>= entry
->bound
) &&
248 (map
->offset
+ map
->size
<= entry
->bound
+ entry
->pages
* PAGE_SIZE
)) {
253 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
254 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
257 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map
->offset
, map
->size
);
261 DRM_ERROR("tried to rmmap GEM object\n");
264 case _DRM_SCATTER_GATHER
:
266 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
269 map
->offset
+= (unsigned long)dev
->sg
->virtual;
271 case _DRM_CONSISTENT
:
272 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
273 * As we're limiting the address to 2^32-1 (or less),
274 * casting it down to 32 bits is no problem, but we
275 * need to point to a 64bit variable first. */
276 dmah
= drm_pci_alloc(dev
, map
->size
, map
->size
, 0xffffffffUL
);
278 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
281 map
->handle
= dmah
->vaddr
;
282 map
->offset
= (unsigned long)dmah
->busaddr
;
286 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
290 list
= drm_alloc(sizeof(*list
), DRM_MEM_MAPS
);
292 if (map
->type
== _DRM_REGISTERS
)
293 iounmap(map
->handle
);
294 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
297 memset(list
, 0, sizeof(*list
));
300 mutex_lock(&dev
->struct_mutex
);
301 list_add(&list
->head
, &dev
->maplist
);
303 /* Assign a 32-bit handle */
304 /* We do it here so that dev->struct_mutex protects the increment */
305 user_token
= (map
->type
== _DRM_SHM
) ? (unsigned long)map
->handle
:
307 ret
= drm_map_handle(dev
, &list
->hash
, user_token
, 0);
309 if (map
->type
== _DRM_REGISTERS
)
310 iounmap(map
->handle
);
311 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
312 drm_free(list
, sizeof(*list
), DRM_MEM_MAPS
);
313 mutex_unlock(&dev
->struct_mutex
);
317 list
->user_token
= list
->hash
.key
<< PAGE_SHIFT
;
318 mutex_unlock(&dev
->struct_mutex
);
320 list
->master
= dev
->primary
->master
;
325 int drm_addmap(struct drm_device
* dev
, unsigned int offset
,
326 unsigned int size
, enum drm_map_type type
,
327 enum drm_map_flags flags
, struct drm_local_map
** map_ptr
)
329 struct drm_map_list
*list
;
332 rc
= drm_addmap_core(dev
, offset
, size
, type
, flags
, &list
);
334 *map_ptr
= list
->map
;
338 EXPORT_SYMBOL(drm_addmap
);
341 * Ioctl to specify a range of memory that is available for mapping by a
344 * \param inode device inode.
345 * \param file_priv DRM file private.
346 * \param cmd command.
347 * \param arg pointer to a drm_map structure.
348 * \return zero on success or a negative value on error.
351 int drm_addmap_ioctl(struct drm_device
*dev
, void *data
,
352 struct drm_file
*file_priv
)
354 struct drm_map
*map
= data
;
355 struct drm_map_list
*maplist
;
358 if (!(capable(CAP_SYS_ADMIN
) || map
->type
== _DRM_AGP
|| map
->type
== _DRM_SHM
))
361 err
= drm_addmap_core(dev
, map
->offset
, map
->size
, map
->type
,
362 map
->flags
, &maplist
);
367 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
368 map
->handle
= (void *)(unsigned long)maplist
->user_token
;
373 * Remove a map private from list and deallocate resources if the mapping
376 * Searches the map on drm_device::maplist, removes it from the list, see if
377 * its being used, and free any associate resource (such as MTRR's) if it's not
382 int drm_rmmap_locked(struct drm_device
*dev
, struct drm_local_map
*map
)
384 struct drm_map_list
*r_list
= NULL
, *list_t
;
385 drm_dma_handle_t dmah
;
387 struct drm_master
*master
;
389 /* Find the list entry for the map and remove it */
390 list_for_each_entry_safe(r_list
, list_t
, &dev
->maplist
, head
) {
391 if (r_list
->map
== map
) {
392 master
= r_list
->master
;
393 list_del(&r_list
->head
);
394 drm_ht_remove_key(&dev
->map_hash
,
395 r_list
->user_token
>> PAGE_SHIFT
);
396 drm_free(r_list
, sizeof(*r_list
), DRM_MEM_MAPS
);
407 iounmap(map
->handle
);
409 case _DRM_FRAME_BUFFER
:
410 if (drm_core_has_MTRR(dev
) && map
->mtrr
>= 0) {
412 retcode
= mtrr_del(map
->mtrr
, map
->offset
, map
->size
);
413 DRM_DEBUG("mtrr_del=%d\n", retcode
);
419 if (dev
->sigdata
.lock
== master
->lock
.hw_lock
)
420 dev
->sigdata
.lock
= NULL
;
421 master
->lock
.hw_lock
= NULL
; /* SHM removed */
422 master
->lock
.file_priv
= NULL
;
423 wake_up_interruptible_all(&master
->lock
.lock_queue
);
427 case _DRM_SCATTER_GATHER
:
429 case _DRM_CONSISTENT
:
430 dmah
.vaddr
= map
->handle
;
431 dmah
.busaddr
= map
->offset
;
432 dmah
.size
= map
->size
;
433 __drm_pci_free(dev
, &dmah
);
436 DRM_ERROR("tried to rmmap GEM object\n");
439 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
443 EXPORT_SYMBOL(drm_rmmap_locked
);
445 int drm_rmmap(struct drm_device
*dev
, struct drm_local_map
*map
)
449 mutex_lock(&dev
->struct_mutex
);
450 ret
= drm_rmmap_locked(dev
, map
);
451 mutex_unlock(&dev
->struct_mutex
);
455 EXPORT_SYMBOL(drm_rmmap
);
457 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
458 * the last close of the device, and this is necessary for cleanup when things
459 * exit uncleanly. Therefore, having userland manually remove mappings seems
460 * like a pointless exercise since they're going away anyway.
462 * One use case might be after addmap is allowed for normal users for SHM and
463 * gets used by drivers that the server doesn't need to care about. This seems
466 * \param inode device inode.
467 * \param file_priv DRM file private.
468 * \param cmd command.
469 * \param arg pointer to a struct drm_map structure.
470 * \return zero on success or a negative value on error.
472 int drm_rmmap_ioctl(struct drm_device
*dev
, void *data
,
473 struct drm_file
*file_priv
)
475 struct drm_map
*request
= data
;
476 struct drm_local_map
*map
= NULL
;
477 struct drm_map_list
*r_list
;
480 mutex_lock(&dev
->struct_mutex
);
481 list_for_each_entry(r_list
, &dev
->maplist
, head
) {
483 r_list
->user_token
== (unsigned long)request
->handle
&&
484 r_list
->map
->flags
& _DRM_REMOVABLE
) {
490 /* List has wrapped around to the head pointer, or its empty we didn't
493 if (list_empty(&dev
->maplist
) || !map
) {
494 mutex_unlock(&dev
->struct_mutex
);
498 /* Register and framebuffer maps are permanent */
499 if ((map
->type
== _DRM_REGISTERS
) || (map
->type
== _DRM_FRAME_BUFFER
)) {
500 mutex_unlock(&dev
->struct_mutex
);
504 ret
= drm_rmmap_locked(dev
, map
);
506 mutex_unlock(&dev
->struct_mutex
);
512 * Cleanup after an error on one of the addbufs() functions.
514 * \param dev DRM device.
515 * \param entry buffer entry where the error occurred.
517 * Frees any pages and buffers associated with the given entry.
519 static void drm_cleanup_buf_error(struct drm_device
* dev
,
520 struct drm_buf_entry
* entry
)
524 if (entry
->seg_count
) {
525 for (i
= 0; i
< entry
->seg_count
; i
++) {
526 if (entry
->seglist
[i
]) {
527 drm_pci_free(dev
, entry
->seglist
[i
]);
530 drm_free(entry
->seglist
,
532 sizeof(*entry
->seglist
), DRM_MEM_SEGS
);
534 entry
->seg_count
= 0;
537 if (entry
->buf_count
) {
538 for (i
= 0; i
< entry
->buf_count
; i
++) {
539 if (entry
->buflist
[i
].dev_private
) {
540 drm_free(entry
->buflist
[i
].dev_private
,
541 entry
->buflist
[i
].dev_priv_size
,
545 drm_free(entry
->buflist
,
547 sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
549 entry
->buf_count
= 0;
555 * Add AGP buffers for DMA transfers.
557 * \param dev struct drm_device to which the buffers are to be added.
558 * \param request pointer to a struct drm_buf_desc describing the request.
559 * \return zero on success or a negative number on failure.
561 * After some sanity checks creates a drm_buf structure for each buffer and
562 * reallocates the buffer list of the same size order to accommodate the new
565 int drm_addbufs_agp(struct drm_device
* dev
, struct drm_buf_desc
* request
)
567 struct drm_device_dma
*dma
= dev
->dma
;
568 struct drm_buf_entry
*entry
;
569 struct drm_agp_mem
*agp_entry
;
571 unsigned long offset
;
572 unsigned long agp_offset
;
581 struct drm_buf
**temp_buflist
;
586 count
= request
->count
;
587 order
= drm_order(request
->size
);
590 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
591 ? PAGE_ALIGN(size
) : size
;
592 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
593 total
= PAGE_SIZE
<< page_order
;
596 agp_offset
= dev
->agp
->base
+ request
->agp_start
;
598 DRM_DEBUG("count: %d\n", count
);
599 DRM_DEBUG("order: %d\n", order
);
600 DRM_DEBUG("size: %d\n", size
);
601 DRM_DEBUG("agp_offset: %lx\n", agp_offset
);
602 DRM_DEBUG("alignment: %d\n", alignment
);
603 DRM_DEBUG("page_order: %d\n", page_order
);
604 DRM_DEBUG("total: %d\n", total
);
606 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
608 if (dev
->queue_count
)
609 return -EBUSY
; /* Not while in use */
611 /* Make sure buffers are located in AGP memory that we own */
613 list_for_each_entry(agp_entry
, &dev
->agp
->memory
, head
) {
614 if ((agp_offset
>= agp_entry
->bound
) &&
615 (agp_offset
+ total
* count
<= agp_entry
->bound
+ agp_entry
->pages
* PAGE_SIZE
)) {
620 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
621 DRM_DEBUG("zone invalid\n");
624 spin_lock(&dev
->count_lock
);
626 spin_unlock(&dev
->count_lock
);
629 atomic_inc(&dev
->buf_alloc
);
630 spin_unlock(&dev
->count_lock
);
632 mutex_lock(&dev
->struct_mutex
);
633 entry
= &dma
->bufs
[order
];
634 if (entry
->buf_count
) {
635 mutex_unlock(&dev
->struct_mutex
);
636 atomic_dec(&dev
->buf_alloc
);
637 return -ENOMEM
; /* May only call once for each order */
640 if (count
< 0 || count
> 4096) {
641 mutex_unlock(&dev
->struct_mutex
);
642 atomic_dec(&dev
->buf_alloc
);
646 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
648 if (!entry
->buflist
) {
649 mutex_unlock(&dev
->struct_mutex
);
650 atomic_dec(&dev
->buf_alloc
);
653 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
655 entry
->buf_size
= size
;
656 entry
->page_order
= page_order
;
660 while (entry
->buf_count
< count
) {
661 buf
= &entry
->buflist
[entry
->buf_count
];
662 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
663 buf
->total
= alignment
;
667 buf
->offset
= (dma
->byte_count
+ offset
);
668 buf
->bus_address
= agp_offset
+ offset
;
669 buf
->address
= (void *)(agp_offset
+ offset
);
673 init_waitqueue_head(&buf
->dma_wait
);
674 buf
->file_priv
= NULL
;
676 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
677 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
678 if (!buf
->dev_private
) {
679 /* Set count correctly so we free the proper amount. */
680 entry
->buf_count
= count
;
681 drm_cleanup_buf_error(dev
, entry
);
682 mutex_unlock(&dev
->struct_mutex
);
683 atomic_dec(&dev
->buf_alloc
);
686 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
688 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
692 byte_count
+= PAGE_SIZE
<< page_order
;
695 DRM_DEBUG("byte_count: %d\n", byte_count
);
697 temp_buflist
= drm_realloc(dma
->buflist
,
698 dma
->buf_count
* sizeof(*dma
->buflist
),
699 (dma
->buf_count
+ entry
->buf_count
)
700 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
702 /* Free the entry because it isn't valid */
703 drm_cleanup_buf_error(dev
, entry
);
704 mutex_unlock(&dev
->struct_mutex
);
705 atomic_dec(&dev
->buf_alloc
);
708 dma
->buflist
= temp_buflist
;
710 for (i
= 0; i
< entry
->buf_count
; i
++) {
711 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
714 dma
->buf_count
+= entry
->buf_count
;
715 dma
->seg_count
+= entry
->seg_count
;
716 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
717 dma
->byte_count
+= byte_count
;
719 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
720 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
722 mutex_unlock(&dev
->struct_mutex
);
724 request
->count
= entry
->buf_count
;
725 request
->size
= size
;
727 dma
->flags
= _DRM_DMA_USE_AGP
;
729 atomic_dec(&dev
->buf_alloc
);
732 EXPORT_SYMBOL(drm_addbufs_agp
);
733 #endif /* __OS_HAS_AGP */
735 int drm_addbufs_pci(struct drm_device
* dev
, struct drm_buf_desc
* request
)
737 struct drm_device_dma
*dma
= dev
->dma
;
743 struct drm_buf_entry
*entry
;
744 drm_dma_handle_t
*dmah
;
747 unsigned long offset
;
751 unsigned long *temp_pagelist
;
752 struct drm_buf
**temp_buflist
;
754 if (!drm_core_check_feature(dev
, DRIVER_PCI_DMA
))
760 if (!capable(CAP_SYS_ADMIN
))
763 count
= request
->count
;
764 order
= drm_order(request
->size
);
767 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
768 request
->count
, request
->size
, size
, order
, dev
->queue_count
);
770 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
772 if (dev
->queue_count
)
773 return -EBUSY
; /* Not while in use */
775 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
776 ? PAGE_ALIGN(size
) : size
;
777 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
778 total
= PAGE_SIZE
<< page_order
;
780 spin_lock(&dev
->count_lock
);
782 spin_unlock(&dev
->count_lock
);
785 atomic_inc(&dev
->buf_alloc
);
786 spin_unlock(&dev
->count_lock
);
788 mutex_lock(&dev
->struct_mutex
);
789 entry
= &dma
->bufs
[order
];
790 if (entry
->buf_count
) {
791 mutex_unlock(&dev
->struct_mutex
);
792 atomic_dec(&dev
->buf_alloc
);
793 return -ENOMEM
; /* May only call once for each order */
796 if (count
< 0 || count
> 4096) {
797 mutex_unlock(&dev
->struct_mutex
);
798 atomic_dec(&dev
->buf_alloc
);
802 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
804 if (!entry
->buflist
) {
805 mutex_unlock(&dev
->struct_mutex
);
806 atomic_dec(&dev
->buf_alloc
);
809 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
811 entry
->seglist
= drm_alloc(count
* sizeof(*entry
->seglist
),
813 if (!entry
->seglist
) {
814 drm_free(entry
->buflist
,
815 count
* sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
816 mutex_unlock(&dev
->struct_mutex
);
817 atomic_dec(&dev
->buf_alloc
);
820 memset(entry
->seglist
, 0, count
* sizeof(*entry
->seglist
));
822 /* Keep the original pagelist until we know all the allocations
825 temp_pagelist
= drm_alloc((dma
->page_count
+ (count
<< page_order
))
826 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
827 if (!temp_pagelist
) {
828 drm_free(entry
->buflist
,
829 count
* sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
830 drm_free(entry
->seglist
,
831 count
* sizeof(*entry
->seglist
), DRM_MEM_SEGS
);
832 mutex_unlock(&dev
->struct_mutex
);
833 atomic_dec(&dev
->buf_alloc
);
836 memcpy(temp_pagelist
,
837 dma
->pagelist
, dma
->page_count
* sizeof(*dma
->pagelist
));
838 DRM_DEBUG("pagelist: %d entries\n",
839 dma
->page_count
+ (count
<< page_order
));
841 entry
->buf_size
= size
;
842 entry
->page_order
= page_order
;
846 while (entry
->buf_count
< count
) {
848 dmah
= drm_pci_alloc(dev
, PAGE_SIZE
<< page_order
, 0x1000, 0xfffffffful
);
851 /* Set count correctly so we free the proper amount. */
852 entry
->buf_count
= count
;
853 entry
->seg_count
= count
;
854 drm_cleanup_buf_error(dev
, entry
);
855 drm_free(temp_pagelist
,
856 (dma
->page_count
+ (count
<< page_order
))
857 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
858 mutex_unlock(&dev
->struct_mutex
);
859 atomic_dec(&dev
->buf_alloc
);
862 entry
->seglist
[entry
->seg_count
++] = dmah
;
863 for (i
= 0; i
< (1 << page_order
); i
++) {
864 DRM_DEBUG("page %d @ 0x%08lx\n",
865 dma
->page_count
+ page_count
,
866 (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
);
867 temp_pagelist
[dma
->page_count
+ page_count
++]
868 = (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
;
871 offset
+ size
<= total
&& entry
->buf_count
< count
;
872 offset
+= alignment
, ++entry
->buf_count
) {
873 buf
= &entry
->buflist
[entry
->buf_count
];
874 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
875 buf
->total
= alignment
;
878 buf
->offset
= (dma
->byte_count
+ byte_count
+ offset
);
879 buf
->address
= (void *)(dmah
->vaddr
+ offset
);
880 buf
->bus_address
= dmah
->busaddr
+ offset
;
884 init_waitqueue_head(&buf
->dma_wait
);
885 buf
->file_priv
= NULL
;
887 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
888 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
,
890 if (!buf
->dev_private
) {
891 /* Set count correctly so we free the proper amount. */
892 entry
->buf_count
= count
;
893 entry
->seg_count
= count
;
894 drm_cleanup_buf_error(dev
, entry
);
895 drm_free(temp_pagelist
,
897 (count
<< page_order
))
898 * sizeof(*dma
->pagelist
),
900 mutex_unlock(&dev
->struct_mutex
);
901 atomic_dec(&dev
->buf_alloc
);
904 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
906 DRM_DEBUG("buffer %d @ %p\n",
907 entry
->buf_count
, buf
->address
);
909 byte_count
+= PAGE_SIZE
<< page_order
;
912 temp_buflist
= drm_realloc(dma
->buflist
,
913 dma
->buf_count
* sizeof(*dma
->buflist
),
914 (dma
->buf_count
+ entry
->buf_count
)
915 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
917 /* Free the entry because it isn't valid */
918 drm_cleanup_buf_error(dev
, entry
);
919 drm_free(temp_pagelist
,
920 (dma
->page_count
+ (count
<< page_order
))
921 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
922 mutex_unlock(&dev
->struct_mutex
);
923 atomic_dec(&dev
->buf_alloc
);
926 dma
->buflist
= temp_buflist
;
928 for (i
= 0; i
< entry
->buf_count
; i
++) {
929 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
932 /* No allocations failed, so now we can replace the orginal pagelist
935 if (dma
->page_count
) {
936 drm_free(dma
->pagelist
,
937 dma
->page_count
* sizeof(*dma
->pagelist
),
940 dma
->pagelist
= temp_pagelist
;
942 dma
->buf_count
+= entry
->buf_count
;
943 dma
->seg_count
+= entry
->seg_count
;
944 dma
->page_count
+= entry
->seg_count
<< page_order
;
945 dma
->byte_count
+= PAGE_SIZE
* (entry
->seg_count
<< page_order
);
947 mutex_unlock(&dev
->struct_mutex
);
949 request
->count
= entry
->buf_count
;
950 request
->size
= size
;
952 if (request
->flags
& _DRM_PCI_BUFFER_RO
)
953 dma
->flags
= _DRM_DMA_USE_PCI_RO
;
955 atomic_dec(&dev
->buf_alloc
);
959 EXPORT_SYMBOL(drm_addbufs_pci
);
961 static int drm_addbufs_sg(struct drm_device
* dev
, struct drm_buf_desc
* request
)
963 struct drm_device_dma
*dma
= dev
->dma
;
964 struct drm_buf_entry
*entry
;
966 unsigned long offset
;
967 unsigned long agp_offset
;
976 struct drm_buf
**temp_buflist
;
978 if (!drm_core_check_feature(dev
, DRIVER_SG
))
984 if (!capable(CAP_SYS_ADMIN
))
987 count
= request
->count
;
988 order
= drm_order(request
->size
);
991 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
992 ? PAGE_ALIGN(size
) : size
;
993 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
994 total
= PAGE_SIZE
<< page_order
;
997 agp_offset
= request
->agp_start
;
999 DRM_DEBUG("count: %d\n", count
);
1000 DRM_DEBUG("order: %d\n", order
);
1001 DRM_DEBUG("size: %d\n", size
);
1002 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
1003 DRM_DEBUG("alignment: %d\n", alignment
);
1004 DRM_DEBUG("page_order: %d\n", page_order
);
1005 DRM_DEBUG("total: %d\n", total
);
1007 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1009 if (dev
->queue_count
)
1010 return -EBUSY
; /* Not while in use */
1012 spin_lock(&dev
->count_lock
);
1014 spin_unlock(&dev
->count_lock
);
1017 atomic_inc(&dev
->buf_alloc
);
1018 spin_unlock(&dev
->count_lock
);
1020 mutex_lock(&dev
->struct_mutex
);
1021 entry
= &dma
->bufs
[order
];
1022 if (entry
->buf_count
) {
1023 mutex_unlock(&dev
->struct_mutex
);
1024 atomic_dec(&dev
->buf_alloc
);
1025 return -ENOMEM
; /* May only call once for each order */
1028 if (count
< 0 || count
> 4096) {
1029 mutex_unlock(&dev
->struct_mutex
);
1030 atomic_dec(&dev
->buf_alloc
);
1034 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
1036 if (!entry
->buflist
) {
1037 mutex_unlock(&dev
->struct_mutex
);
1038 atomic_dec(&dev
->buf_alloc
);
1041 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
1043 entry
->buf_size
= size
;
1044 entry
->page_order
= page_order
;
1048 while (entry
->buf_count
< count
) {
1049 buf
= &entry
->buflist
[entry
->buf_count
];
1050 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1051 buf
->total
= alignment
;
1055 buf
->offset
= (dma
->byte_count
+ offset
);
1056 buf
->bus_address
= agp_offset
+ offset
;
1057 buf
->address
= (void *)(agp_offset
+ offset
1058 + (unsigned long)dev
->sg
->virtual);
1062 init_waitqueue_head(&buf
->dma_wait
);
1063 buf
->file_priv
= NULL
;
1065 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1066 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
1067 if (!buf
->dev_private
) {
1068 /* Set count correctly so we free the proper amount. */
1069 entry
->buf_count
= count
;
1070 drm_cleanup_buf_error(dev
, entry
);
1071 mutex_unlock(&dev
->struct_mutex
);
1072 atomic_dec(&dev
->buf_alloc
);
1076 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
1078 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1080 offset
+= alignment
;
1082 byte_count
+= PAGE_SIZE
<< page_order
;
1085 DRM_DEBUG("byte_count: %d\n", byte_count
);
1087 temp_buflist
= drm_realloc(dma
->buflist
,
1088 dma
->buf_count
* sizeof(*dma
->buflist
),
1089 (dma
->buf_count
+ entry
->buf_count
)
1090 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
1091 if (!temp_buflist
) {
1092 /* Free the entry because it isn't valid */
1093 drm_cleanup_buf_error(dev
, entry
);
1094 mutex_unlock(&dev
->struct_mutex
);
1095 atomic_dec(&dev
->buf_alloc
);
1098 dma
->buflist
= temp_buflist
;
1100 for (i
= 0; i
< entry
->buf_count
; i
++) {
1101 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1104 dma
->buf_count
+= entry
->buf_count
;
1105 dma
->seg_count
+= entry
->seg_count
;
1106 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1107 dma
->byte_count
+= byte_count
;
1109 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1110 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1112 mutex_unlock(&dev
->struct_mutex
);
1114 request
->count
= entry
->buf_count
;
1115 request
->size
= size
;
1117 dma
->flags
= _DRM_DMA_USE_SG
;
1119 atomic_dec(&dev
->buf_alloc
);
1123 static int drm_addbufs_fb(struct drm_device
* dev
, struct drm_buf_desc
* request
)
1125 struct drm_device_dma
*dma
= dev
->dma
;
1126 struct drm_buf_entry
*entry
;
1127 struct drm_buf
*buf
;
1128 unsigned long offset
;
1129 unsigned long agp_offset
;
1138 struct drm_buf
**temp_buflist
;
1140 if (!drm_core_check_feature(dev
, DRIVER_FB_DMA
))
1146 if (!capable(CAP_SYS_ADMIN
))
1149 count
= request
->count
;
1150 order
= drm_order(request
->size
);
1153 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
1154 ? PAGE_ALIGN(size
) : size
;
1155 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
1156 total
= PAGE_SIZE
<< page_order
;
1159 agp_offset
= request
->agp_start
;
1161 DRM_DEBUG("count: %d\n", count
);
1162 DRM_DEBUG("order: %d\n", order
);
1163 DRM_DEBUG("size: %d\n", size
);
1164 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
1165 DRM_DEBUG("alignment: %d\n", alignment
);
1166 DRM_DEBUG("page_order: %d\n", page_order
);
1167 DRM_DEBUG("total: %d\n", total
);
1169 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1171 if (dev
->queue_count
)
1172 return -EBUSY
; /* Not while in use */
1174 spin_lock(&dev
->count_lock
);
1176 spin_unlock(&dev
->count_lock
);
1179 atomic_inc(&dev
->buf_alloc
);
1180 spin_unlock(&dev
->count_lock
);
1182 mutex_lock(&dev
->struct_mutex
);
1183 entry
= &dma
->bufs
[order
];
1184 if (entry
->buf_count
) {
1185 mutex_unlock(&dev
->struct_mutex
);
1186 atomic_dec(&dev
->buf_alloc
);
1187 return -ENOMEM
; /* May only call once for each order */
1190 if (count
< 0 || count
> 4096) {
1191 mutex_unlock(&dev
->struct_mutex
);
1192 atomic_dec(&dev
->buf_alloc
);
1196 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
1198 if (!entry
->buflist
) {
1199 mutex_unlock(&dev
->struct_mutex
);
1200 atomic_dec(&dev
->buf_alloc
);
1203 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
1205 entry
->buf_size
= size
;
1206 entry
->page_order
= page_order
;
1210 while (entry
->buf_count
< count
) {
1211 buf
= &entry
->buflist
[entry
->buf_count
];
1212 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1213 buf
->total
= alignment
;
1217 buf
->offset
= (dma
->byte_count
+ offset
);
1218 buf
->bus_address
= agp_offset
+ offset
;
1219 buf
->address
= (void *)(agp_offset
+ offset
);
1223 init_waitqueue_head(&buf
->dma_wait
);
1224 buf
->file_priv
= NULL
;
1226 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1227 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
1228 if (!buf
->dev_private
) {
1229 /* Set count correctly so we free the proper amount. */
1230 entry
->buf_count
= count
;
1231 drm_cleanup_buf_error(dev
, entry
);
1232 mutex_unlock(&dev
->struct_mutex
);
1233 atomic_dec(&dev
->buf_alloc
);
1236 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
1238 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1240 offset
+= alignment
;
1242 byte_count
+= PAGE_SIZE
<< page_order
;
1245 DRM_DEBUG("byte_count: %d\n", byte_count
);
1247 temp_buflist
= drm_realloc(dma
->buflist
,
1248 dma
->buf_count
* sizeof(*dma
->buflist
),
1249 (dma
->buf_count
+ entry
->buf_count
)
1250 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
1251 if (!temp_buflist
) {
1252 /* Free the entry because it isn't valid */
1253 drm_cleanup_buf_error(dev
, entry
);
1254 mutex_unlock(&dev
->struct_mutex
);
1255 atomic_dec(&dev
->buf_alloc
);
1258 dma
->buflist
= temp_buflist
;
1260 for (i
= 0; i
< entry
->buf_count
; i
++) {
1261 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1264 dma
->buf_count
+= entry
->buf_count
;
1265 dma
->seg_count
+= entry
->seg_count
;
1266 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1267 dma
->byte_count
+= byte_count
;
1269 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1270 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1272 mutex_unlock(&dev
->struct_mutex
);
1274 request
->count
= entry
->buf_count
;
1275 request
->size
= size
;
1277 dma
->flags
= _DRM_DMA_USE_FB
;
1279 atomic_dec(&dev
->buf_alloc
);
1285 * Add buffers for DMA transfers (ioctl).
1287 * \param inode device inode.
1288 * \param file_priv DRM file private.
1289 * \param cmd command.
1290 * \param arg pointer to a struct drm_buf_desc request.
1291 * \return zero on success or a negative number on failure.
1293 * According with the memory type specified in drm_buf_desc::flags and the
1294 * build options, it dispatches the call either to addbufs_agp(),
1295 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1296 * PCI memory respectively.
1298 int drm_addbufs(struct drm_device
*dev
, void *data
,
1299 struct drm_file
*file_priv
)
1301 struct drm_buf_desc
*request
= data
;
1304 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1308 if (request
->flags
& _DRM_AGP_BUFFER
)
1309 ret
= drm_addbufs_agp(dev
, request
);
1312 if (request
->flags
& _DRM_SG_BUFFER
)
1313 ret
= drm_addbufs_sg(dev
, request
);
1314 else if (request
->flags
& _DRM_FB_BUFFER
)
1315 ret
= drm_addbufs_fb(dev
, request
);
1317 ret
= drm_addbufs_pci(dev
, request
);
1323 * Get information about the buffer mappings.
1325 * This was originally mean for debugging purposes, or by a sophisticated
1326 * client library to determine how best to use the available buffers (e.g.,
1327 * large buffers can be used for image transfer).
1329 * \param inode device inode.
1330 * \param file_priv DRM file private.
1331 * \param cmd command.
1332 * \param arg pointer to a drm_buf_info structure.
1333 * \return zero on success or a negative number on failure.
1335 * Increments drm_device::buf_use while holding the drm_device::count_lock
1336 * lock, preventing of allocating more buffers after this call. Information
1337 * about each requested buffer is then copied into user space.
1339 int drm_infobufs(struct drm_device
*dev
, void *data
,
1340 struct drm_file
*file_priv
)
1342 struct drm_device_dma
*dma
= dev
->dma
;
1343 struct drm_buf_info
*request
= data
;
1347 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1353 spin_lock(&dev
->count_lock
);
1354 if (atomic_read(&dev
->buf_alloc
)) {
1355 spin_unlock(&dev
->count_lock
);
1358 ++dev
->buf_use
; /* Can't allocate more after this call */
1359 spin_unlock(&dev
->count_lock
);
1361 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1362 if (dma
->bufs
[i
].buf_count
)
1366 DRM_DEBUG("count = %d\n", count
);
1368 if (request
->count
>= count
) {
1369 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1370 if (dma
->bufs
[i
].buf_count
) {
1371 struct drm_buf_desc __user
*to
=
1372 &request
->list
[count
];
1373 struct drm_buf_entry
*from
= &dma
->bufs
[i
];
1374 struct drm_freelist
*list
= &dma
->bufs
[i
].freelist
;
1375 if (copy_to_user(&to
->count
,
1377 sizeof(from
->buf_count
)) ||
1378 copy_to_user(&to
->size
,
1380 sizeof(from
->buf_size
)) ||
1381 copy_to_user(&to
->low_mark
,
1383 sizeof(list
->low_mark
)) ||
1384 copy_to_user(&to
->high_mark
,
1386 sizeof(list
->high_mark
)))
1389 DRM_DEBUG("%d %d %d %d %d\n",
1391 dma
->bufs
[i
].buf_count
,
1392 dma
->bufs
[i
].buf_size
,
1393 dma
->bufs
[i
].freelist
.low_mark
,
1394 dma
->bufs
[i
].freelist
.high_mark
);
1399 request
->count
= count
;
1405 * Specifies a low and high water mark for buffer allocation
1407 * \param inode device inode.
1408 * \param file_priv DRM file private.
1409 * \param cmd command.
1410 * \param arg a pointer to a drm_buf_desc structure.
1411 * \return zero on success or a negative number on failure.
1413 * Verifies that the size order is bounded between the admissible orders and
1414 * updates the respective drm_device_dma::bufs entry low and high water mark.
1416 * \note This ioctl is deprecated and mostly never used.
1418 int drm_markbufs(struct drm_device
*dev
, void *data
,
1419 struct drm_file
*file_priv
)
1421 struct drm_device_dma
*dma
= dev
->dma
;
1422 struct drm_buf_desc
*request
= data
;
1424 struct drm_buf_entry
*entry
;
1426 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1432 DRM_DEBUG("%d, %d, %d\n",
1433 request
->size
, request
->low_mark
, request
->high_mark
);
1434 order
= drm_order(request
->size
);
1435 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1437 entry
= &dma
->bufs
[order
];
1439 if (request
->low_mark
< 0 || request
->low_mark
> entry
->buf_count
)
1441 if (request
->high_mark
< 0 || request
->high_mark
> entry
->buf_count
)
1444 entry
->freelist
.low_mark
= request
->low_mark
;
1445 entry
->freelist
.high_mark
= request
->high_mark
;
1451 * Unreserve the buffers in list, previously reserved using drmDMA.
1453 * \param inode device inode.
1454 * \param file_priv DRM file private.
1455 * \param cmd command.
1456 * \param arg pointer to a drm_buf_free structure.
1457 * \return zero on success or a negative number on failure.
1459 * Calls free_buffer() for each used buffer.
1460 * This function is primarily used for debugging.
1462 int drm_freebufs(struct drm_device
*dev
, void *data
,
1463 struct drm_file
*file_priv
)
1465 struct drm_device_dma
*dma
= dev
->dma
;
1466 struct drm_buf_free
*request
= data
;
1469 struct drm_buf
*buf
;
1471 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1477 DRM_DEBUG("%d\n", request
->count
);
1478 for (i
= 0; i
< request
->count
; i
++) {
1479 if (copy_from_user(&idx
, &request
->list
[i
], sizeof(idx
)))
1481 if (idx
< 0 || idx
>= dma
->buf_count
) {
1482 DRM_ERROR("Index %d (of %d max)\n",
1483 idx
, dma
->buf_count
- 1);
1486 buf
= dma
->buflist
[idx
];
1487 if (buf
->file_priv
!= file_priv
) {
1488 DRM_ERROR("Process %d freeing buffer not owned\n",
1489 task_pid_nr(current
));
1492 drm_free_buffer(dev
, buf
);
1499 * Maps all of the DMA buffers into client-virtual space (ioctl).
1501 * \param inode device inode.
1502 * \param file_priv DRM file private.
1503 * \param cmd command.
1504 * \param arg pointer to a drm_buf_map structure.
1505 * \return zero on success or a negative number on failure.
1507 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1508 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1509 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1512 int drm_mapbufs(struct drm_device
*dev
, void *data
,
1513 struct drm_file
*file_priv
)
1515 struct drm_device_dma
*dma
= dev
->dma
;
1518 unsigned long virtual;
1519 unsigned long address
;
1520 struct drm_buf_map
*request
= data
;
1523 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1529 spin_lock(&dev
->count_lock
);
1530 if (atomic_read(&dev
->buf_alloc
)) {
1531 spin_unlock(&dev
->count_lock
);
1534 dev
->buf_use
++; /* Can't allocate more after this call */
1535 spin_unlock(&dev
->count_lock
);
1537 if (request
->count
>= dma
->buf_count
) {
1538 if ((drm_core_has_AGP(dev
) && (dma
->flags
& _DRM_DMA_USE_AGP
))
1539 || (drm_core_check_feature(dev
, DRIVER_SG
)
1540 && (dma
->flags
& _DRM_DMA_USE_SG
))
1541 || (drm_core_check_feature(dev
, DRIVER_FB_DMA
)
1542 && (dma
->flags
& _DRM_DMA_USE_FB
))) {
1543 struct drm_local_map
*map
= dev
->agp_buffer_map
;
1544 unsigned long token
= dev
->agp_buffer_token
;
1550 down_write(¤t
->mm
->mmap_sem
);
1551 virtual = do_mmap(file_priv
->filp
, 0, map
->size
,
1552 PROT_READ
| PROT_WRITE
,
1555 up_write(¤t
->mm
->mmap_sem
);
1557 down_write(¤t
->mm
->mmap_sem
);
1558 virtual = do_mmap(file_priv
->filp
, 0, dma
->byte_count
,
1559 PROT_READ
| PROT_WRITE
,
1561 up_write(¤t
->mm
->mmap_sem
);
1563 if (virtual > -1024UL) {
1565 retcode
= (signed long)virtual;
1568 request
->virtual = (void __user
*)virtual;
1570 for (i
= 0; i
< dma
->buf_count
; i
++) {
1571 if (copy_to_user(&request
->list
[i
].idx
,
1572 &dma
->buflist
[i
]->idx
,
1573 sizeof(request
->list
[0].idx
))) {
1577 if (copy_to_user(&request
->list
[i
].total
,
1578 &dma
->buflist
[i
]->total
,
1579 sizeof(request
->list
[0].total
))) {
1583 if (copy_to_user(&request
->list
[i
].used
,
1584 &zero
, sizeof(zero
))) {
1588 address
= virtual + dma
->buflist
[i
]->offset
; /* *** */
1589 if (copy_to_user(&request
->list
[i
].address
,
1590 &address
, sizeof(address
))) {
1597 request
->count
= dma
->buf_count
;
1598 DRM_DEBUG("%d buffers, retcode = %d\n", request
->count
, retcode
);
1604 * Compute size order. Returns the exponent of the smaller power of two which
1605 * is greater or equal to given number.
1610 * \todo Can be made faster.
1612 int drm_order(unsigned long size
)
1617 for (order
= 0, tmp
= size
>> 1; tmp
; tmp
>>= 1, order
++) ;
1619 if (size
& (size
- 1))
1624 EXPORT_SYMBOL(drm_order
);
This page took 0.085909 seconds and 5 git commands to generate.