]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/char/drm/drm_bufs.c
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 drm_map_list_t
*drm_find_matching_map(struct drm_device
*dev
,
55 drm_map_list_t
*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
, drm_hash_item_t
*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 filp file pointer.
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
, drm_map_list_t
** maplist
)
109 drm_map_list_t
*list
;
110 drm_dma_handle_t
*dmah
;
111 unsigned long user_token
;
114 map
= drm_alloc(sizeof(*map
), DRM_MEM_MAPS
);
118 map
->offset
= offset
;
123 /* Only allow shared memory to be removable since we only keep enough
124 * book keeping information about shared memory to allow for removal
125 * when processes fork.
127 if ((map
->flags
& _DRM_REMOVABLE
) && map
->type
!= _DRM_SHM
) {
128 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
131 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
132 map
->offset
, map
->size
, map
->type
);
133 if ((map
->offset
& (~PAGE_MASK
)) || (map
->size
& (~PAGE_MASK
))) {
134 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
142 case _DRM_FRAME_BUFFER
:
143 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
144 if (map
->offset
+ (map
->size
-1) < map
->offset
||
145 map
->offset
< virt_to_phys(high_memory
)) {
146 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
151 map
->offset
+= dev
->hose
->mem_space
->start
;
153 /* Some drivers preinitialize some maps, without the X Server
154 * needing to be aware of it. Therefore, we just return success
155 * when the server tries to create a duplicate map.
157 list
= drm_find_matching_map(dev
, map
);
159 if (list
->map
->size
!= map
->size
) {
160 DRM_DEBUG("Matching maps of type %d with "
161 "mismatched sizes, (%ld vs %ld)\n",
162 map
->type
, map
->size
,
164 list
->map
->size
= map
->size
;
167 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
172 if (drm_core_has_MTRR(dev
)) {
173 if (map
->type
== _DRM_FRAME_BUFFER
||
174 (map
->flags
& _DRM_WRITE_COMBINING
)) {
175 map
->mtrr
= mtrr_add(map
->offset
, map
->size
,
176 MTRR_TYPE_WRCOMB
, 1);
179 if (map
->type
== _DRM_REGISTERS
)
180 map
->handle
= ioremap(map
->offset
, map
->size
);
183 list
= drm_find_matching_map(dev
, map
);
185 if(list
->map
->size
!= map
->size
) {
186 DRM_DEBUG("Matching maps of type %d with "
187 "mismatched sizes, (%ld vs %ld)\n",
188 map
->type
, map
->size
, list
->map
->size
);
189 list
->map
->size
= map
->size
;
192 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
196 map
->handle
= vmalloc_user(map
->size
);
197 DRM_DEBUG("%lu %d %p\n",
198 map
->size
, drm_order(map
->size
), map
->handle
);
200 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
203 map
->offset
= (unsigned long)map
->handle
;
204 if (map
->flags
& _DRM_CONTAINS_LOCK
) {
205 /* Prevent a 2nd X Server from creating a 2nd lock */
206 if (dev
->lock
.hw_lock
!= NULL
) {
208 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
211 dev
->sigdata
.lock
= dev
->lock
.hw_lock
= map
->handle
; /* Pointer to lock */
215 drm_agp_mem_t
*entry
;
218 if (!drm_core_has_AGP(dev
)) {
219 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
223 map
->offset
+= dev
->hose
->mem_space
->start
;
225 /* Note: dev->agp->base may actually be 0 when the DRM
226 * is not in control of AGP space. But if user space is
227 * it should already have added the AGP base itself.
229 map
->offset
+= dev
->agp
->base
;
230 map
->mtrr
= dev
->agp
->agp_mtrr
; /* for getmap */
232 /* This assumes the DRM is in total control of AGP space.
233 * It's not always the case as AGP can be in the control
234 * of user space (i.e. i810 driver). So this loop will get
235 * skipped and we double check that dev->agp->memory is
236 * actually set as well as being invalid before EPERM'ing
238 list_for_each_entry(entry
, &dev
->agp
->memory
, head
) {
239 if ((map
->offset
>= entry
->bound
) &&
240 (map
->offset
+ map
->size
<= entry
->bound
+ entry
->pages
* PAGE_SIZE
)) {
245 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
246 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
249 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map
->offset
, map
->size
);
253 case _DRM_SCATTER_GATHER
:
255 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
258 map
->offset
+= (unsigned long)dev
->sg
->virtual;
260 case _DRM_CONSISTENT
:
261 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
262 * As we're limiting the address to 2^32-1 (or less),
263 * casting it down to 32 bits is no problem, but we
264 * need to point to a 64bit variable first. */
265 dmah
= drm_pci_alloc(dev
, map
->size
, map
->size
, 0xffffffffUL
);
267 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
270 map
->handle
= dmah
->vaddr
;
271 map
->offset
= (unsigned long)dmah
->busaddr
;
275 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
279 list
= drm_alloc(sizeof(*list
), DRM_MEM_MAPS
);
281 if (map
->type
== _DRM_REGISTERS
)
282 iounmap(map
->handle
);
283 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
286 memset(list
, 0, sizeof(*list
));
289 mutex_lock(&dev
->struct_mutex
);
290 list_add(&list
->head
, &dev
->maplist
);
292 /* Assign a 32-bit handle */
293 /* We do it here so that dev->struct_mutex protects the increment */
294 user_token
= (map
->type
== _DRM_SHM
) ? (unsigned long)map
->handle
:
296 ret
= drm_map_handle(dev
, &list
->hash
, user_token
, 0);
298 if (map
->type
== _DRM_REGISTERS
)
299 iounmap(map
->handle
);
300 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
301 drm_free(list
, sizeof(*list
), DRM_MEM_MAPS
);
302 mutex_unlock(&dev
->struct_mutex
);
306 list
->user_token
= list
->hash
.key
<< PAGE_SHIFT
;
307 mutex_unlock(&dev
->struct_mutex
);
313 int drm_addmap(struct drm_device
* dev
, unsigned int offset
,
314 unsigned int size
, enum drm_map_type type
,
315 enum drm_map_flags flags
, drm_local_map_t
** map_ptr
)
317 drm_map_list_t
*list
;
320 rc
= drm_addmap_core(dev
, offset
, size
, type
, flags
, &list
);
322 *map_ptr
= list
->map
;
326 EXPORT_SYMBOL(drm_addmap
);
328 int drm_addmap_ioctl(struct inode
*inode
, struct file
*filp
,
329 unsigned int cmd
, unsigned long arg
)
331 struct drm_file
*priv
= filp
->private_data
;
332 struct drm_device
*dev
= priv
->head
->dev
;
334 drm_map_list_t
*maplist
;
335 struct drm_map __user
*argp
= (void __user
*)arg
;
338 if (!(filp
->f_mode
& 3))
339 return -EACCES
; /* Require read/write */
341 if (copy_from_user(&map
, argp
, sizeof(map
))) {
345 if (!(capable(CAP_SYS_ADMIN
) || map
.type
== _DRM_AGP
))
348 err
= drm_addmap_core(dev
, map
.offset
, map
.size
, map
.type
, map
.flags
,
354 if (copy_to_user(argp
, maplist
->map
, sizeof(struct drm_map
)))
357 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
358 if (put_user((void *)(unsigned long)maplist
->user_token
, &argp
->handle
))
364 * Remove a map private from list and deallocate resources if the mapping
367 * \param inode device inode.
368 * \param filp file pointer.
369 * \param cmd command.
370 * \param arg pointer to a struct drm_map structure.
371 * \return zero on success or a negative value on error.
373 * Searches the map on drm_device::maplist, removes it from the list, see if
374 * its being used, and free any associate resource (such as MTRR's) if it's not
379 int drm_rmmap_locked(struct drm_device
*dev
, drm_local_map_t
*map
)
381 drm_map_list_t
*r_list
= NULL
, *list_t
;
382 drm_dma_handle_t dmah
;
385 /* Find the list entry for the map and remove it */
386 list_for_each_entry_safe(r_list
, list_t
, &dev
->maplist
, head
) {
387 if (r_list
->map
== map
) {
388 list_del(&r_list
->head
);
389 drm_ht_remove_key(&dev
->map_hash
,
390 r_list
->user_token
>> PAGE_SHIFT
);
391 drm_free(r_list
, sizeof(*r_list
), DRM_MEM_MAPS
);
402 iounmap(map
->handle
);
404 case _DRM_FRAME_BUFFER
:
405 if (drm_core_has_MTRR(dev
) && map
->mtrr
>= 0) {
407 retcode
= mtrr_del(map
->mtrr
, map
->offset
, map
->size
);
408 DRM_DEBUG("mtrr_del=%d\n", retcode
);
415 case _DRM_SCATTER_GATHER
:
417 case _DRM_CONSISTENT
:
418 dmah
.vaddr
= map
->handle
;
419 dmah
.busaddr
= map
->offset
;
420 dmah
.size
= map
->size
;
421 __drm_pci_free(dev
, &dmah
);
424 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
429 int drm_rmmap(struct drm_device
*dev
, drm_local_map_t
*map
)
433 mutex_lock(&dev
->struct_mutex
);
434 ret
= drm_rmmap_locked(dev
, map
);
435 mutex_unlock(&dev
->struct_mutex
);
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 inode
*inode
, struct file
*filp
,
450 unsigned int cmd
, unsigned long arg
)
452 struct drm_file
*priv
= filp
->private_data
;
453 struct drm_device
*dev
= priv
->head
->dev
;
454 struct drm_map request
;
455 drm_local_map_t
*map
= NULL
;
456 drm_map_list_t
*r_list
;
459 if (copy_from_user(&request
, (struct drm_map __user
*) arg
, sizeof(request
))) {
463 mutex_lock(&dev
->struct_mutex
);
464 list_for_each_entry(r_list
, &dev
->maplist
, head
) {
466 r_list
->user_token
== (unsigned long)request
.handle
&&
467 r_list
->map
->flags
& _DRM_REMOVABLE
) {
473 /* List has wrapped around to the head pointer, or its empty we didn't
476 if (list_empty(&dev
->maplist
) || !map
) {
477 mutex_unlock(&dev
->struct_mutex
);
482 mutex_unlock(&dev
->struct_mutex
);
486 /* Register and framebuffer maps are permanent */
487 if ((map
->type
== _DRM_REGISTERS
) || (map
->type
== _DRM_FRAME_BUFFER
)) {
488 mutex_unlock(&dev
->struct_mutex
);
492 ret
= drm_rmmap_locked(dev
, map
);
494 mutex_unlock(&dev
->struct_mutex
);
500 * Cleanup after an error on one of the addbufs() functions.
502 * \param dev DRM device.
503 * \param entry buffer entry where the error occurred.
505 * Frees any pages and buffers associated with the given entry.
507 static void drm_cleanup_buf_error(struct drm_device
* dev
,
508 struct drm_buf_entry
* entry
)
512 if (entry
->seg_count
) {
513 for (i
= 0; i
< entry
->seg_count
; i
++) {
514 if (entry
->seglist
[i
]) {
515 drm_pci_free(dev
, entry
->seglist
[i
]);
518 drm_free(entry
->seglist
,
520 sizeof(*entry
->seglist
), DRM_MEM_SEGS
);
522 entry
->seg_count
= 0;
525 if (entry
->buf_count
) {
526 for (i
= 0; i
< entry
->buf_count
; i
++) {
527 if (entry
->buflist
[i
].dev_private
) {
528 drm_free(entry
->buflist
[i
].dev_private
,
529 entry
->buflist
[i
].dev_priv_size
,
533 drm_free(entry
->buflist
,
535 sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
537 entry
->buf_count
= 0;
543 * Add AGP buffers for DMA transfers.
545 * \param dev struct drm_device to which the buffers are to be added.
546 * \param request pointer to a struct drm_buf_desc describing the request.
547 * \return zero on success or a negative number on failure.
549 * After some sanity checks creates a drm_buf structure for each buffer and
550 * reallocates the buffer list of the same size order to accommodate the new
553 int drm_addbufs_agp(struct drm_device
* dev
, struct drm_buf_desc
* request
)
555 struct drm_device_dma
*dma
= dev
->dma
;
556 struct drm_buf_entry
*entry
;
557 drm_agp_mem_t
*agp_entry
;
559 unsigned long offset
;
560 unsigned long agp_offset
;
569 struct drm_buf
**temp_buflist
;
574 count
= request
->count
;
575 order
= drm_order(request
->size
);
578 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
579 ? PAGE_ALIGN(size
) : size
;
580 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
581 total
= PAGE_SIZE
<< page_order
;
584 agp_offset
= dev
->agp
->base
+ request
->agp_start
;
586 DRM_DEBUG("count: %d\n", count
);
587 DRM_DEBUG("order: %d\n", order
);
588 DRM_DEBUG("size: %d\n", size
);
589 DRM_DEBUG("agp_offset: %lx\n", agp_offset
);
590 DRM_DEBUG("alignment: %d\n", alignment
);
591 DRM_DEBUG("page_order: %d\n", page_order
);
592 DRM_DEBUG("total: %d\n", total
);
594 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
596 if (dev
->queue_count
)
597 return -EBUSY
; /* Not while in use */
599 /* Make sure buffers are located in AGP memory that we own */
601 list_for_each_entry(agp_entry
, &dev
->agp
->memory
, head
) {
602 if ((agp_offset
>= agp_entry
->bound
) &&
603 (agp_offset
+ total
* count
<= agp_entry
->bound
+ agp_entry
->pages
* PAGE_SIZE
)) {
608 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
609 DRM_DEBUG("zone invalid\n");
612 spin_lock(&dev
->count_lock
);
614 spin_unlock(&dev
->count_lock
);
617 atomic_inc(&dev
->buf_alloc
);
618 spin_unlock(&dev
->count_lock
);
620 mutex_lock(&dev
->struct_mutex
);
621 entry
= &dma
->bufs
[order
];
622 if (entry
->buf_count
) {
623 mutex_unlock(&dev
->struct_mutex
);
624 atomic_dec(&dev
->buf_alloc
);
625 return -ENOMEM
; /* May only call once for each order */
628 if (count
< 0 || count
> 4096) {
629 mutex_unlock(&dev
->struct_mutex
);
630 atomic_dec(&dev
->buf_alloc
);
634 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
636 if (!entry
->buflist
) {
637 mutex_unlock(&dev
->struct_mutex
);
638 atomic_dec(&dev
->buf_alloc
);
641 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
643 entry
->buf_size
= size
;
644 entry
->page_order
= page_order
;
648 while (entry
->buf_count
< count
) {
649 buf
= &entry
->buflist
[entry
->buf_count
];
650 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
651 buf
->total
= alignment
;
655 buf
->offset
= (dma
->byte_count
+ offset
);
656 buf
->bus_address
= agp_offset
+ offset
;
657 buf
->address
= (void *)(agp_offset
+ offset
);
661 init_waitqueue_head(&buf
->dma_wait
);
664 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
665 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
666 if (!buf
->dev_private
) {
667 /* Set count correctly so we free the proper amount. */
668 entry
->buf_count
= count
;
669 drm_cleanup_buf_error(dev
, entry
);
670 mutex_unlock(&dev
->struct_mutex
);
671 atomic_dec(&dev
->buf_alloc
);
674 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
676 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
680 byte_count
+= PAGE_SIZE
<< page_order
;
683 DRM_DEBUG("byte_count: %d\n", byte_count
);
685 temp_buflist
= drm_realloc(dma
->buflist
,
686 dma
->buf_count
* sizeof(*dma
->buflist
),
687 (dma
->buf_count
+ entry
->buf_count
)
688 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
690 /* Free the entry because it isn't valid */
691 drm_cleanup_buf_error(dev
, entry
);
692 mutex_unlock(&dev
->struct_mutex
);
693 atomic_dec(&dev
->buf_alloc
);
696 dma
->buflist
= temp_buflist
;
698 for (i
= 0; i
< entry
->buf_count
; i
++) {
699 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
702 dma
->buf_count
+= entry
->buf_count
;
703 dma
->seg_count
+= entry
->seg_count
;
704 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
705 dma
->byte_count
+= byte_count
;
707 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
708 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
710 mutex_unlock(&dev
->struct_mutex
);
712 request
->count
= entry
->buf_count
;
713 request
->size
= size
;
715 dma
->flags
= _DRM_DMA_USE_AGP
;
717 atomic_dec(&dev
->buf_alloc
);
720 EXPORT_SYMBOL(drm_addbufs_agp
);
721 #endif /* __OS_HAS_AGP */
723 int drm_addbufs_pci(struct drm_device
* dev
, struct drm_buf_desc
* request
)
725 struct drm_device_dma
*dma
= dev
->dma
;
731 struct drm_buf_entry
*entry
;
732 drm_dma_handle_t
*dmah
;
735 unsigned long offset
;
739 unsigned long *temp_pagelist
;
740 struct drm_buf
**temp_buflist
;
742 if (!drm_core_check_feature(dev
, DRIVER_PCI_DMA
))
748 if (!capable(CAP_SYS_ADMIN
))
751 count
= request
->count
;
752 order
= drm_order(request
->size
);
755 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
756 request
->count
, request
->size
, size
, order
, dev
->queue_count
);
758 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
760 if (dev
->queue_count
)
761 return -EBUSY
; /* Not while in use */
763 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
764 ? PAGE_ALIGN(size
) : size
;
765 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
766 total
= PAGE_SIZE
<< page_order
;
768 spin_lock(&dev
->count_lock
);
770 spin_unlock(&dev
->count_lock
);
773 atomic_inc(&dev
->buf_alloc
);
774 spin_unlock(&dev
->count_lock
);
776 mutex_lock(&dev
->struct_mutex
);
777 entry
= &dma
->bufs
[order
];
778 if (entry
->buf_count
) {
779 mutex_unlock(&dev
->struct_mutex
);
780 atomic_dec(&dev
->buf_alloc
);
781 return -ENOMEM
; /* May only call once for each order */
784 if (count
< 0 || count
> 4096) {
785 mutex_unlock(&dev
->struct_mutex
);
786 atomic_dec(&dev
->buf_alloc
);
790 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
792 if (!entry
->buflist
) {
793 mutex_unlock(&dev
->struct_mutex
);
794 atomic_dec(&dev
->buf_alloc
);
797 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
799 entry
->seglist
= drm_alloc(count
* sizeof(*entry
->seglist
),
801 if (!entry
->seglist
) {
802 drm_free(entry
->buflist
,
803 count
* sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
804 mutex_unlock(&dev
->struct_mutex
);
805 atomic_dec(&dev
->buf_alloc
);
808 memset(entry
->seglist
, 0, count
* sizeof(*entry
->seglist
));
810 /* Keep the original pagelist until we know all the allocations
813 temp_pagelist
= drm_alloc((dma
->page_count
+ (count
<< page_order
))
814 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
815 if (!temp_pagelist
) {
816 drm_free(entry
->buflist
,
817 count
* sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
818 drm_free(entry
->seglist
,
819 count
* sizeof(*entry
->seglist
), DRM_MEM_SEGS
);
820 mutex_unlock(&dev
->struct_mutex
);
821 atomic_dec(&dev
->buf_alloc
);
824 memcpy(temp_pagelist
,
825 dma
->pagelist
, dma
->page_count
* sizeof(*dma
->pagelist
));
826 DRM_DEBUG("pagelist: %d entries\n",
827 dma
->page_count
+ (count
<< page_order
));
829 entry
->buf_size
= size
;
830 entry
->page_order
= page_order
;
834 while (entry
->buf_count
< count
) {
836 dmah
= drm_pci_alloc(dev
, PAGE_SIZE
<< page_order
, 0x1000, 0xfffffffful
);
839 /* Set count correctly so we free the proper amount. */
840 entry
->buf_count
= count
;
841 entry
->seg_count
= count
;
842 drm_cleanup_buf_error(dev
, entry
);
843 drm_free(temp_pagelist
,
844 (dma
->page_count
+ (count
<< page_order
))
845 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
846 mutex_unlock(&dev
->struct_mutex
);
847 atomic_dec(&dev
->buf_alloc
);
850 entry
->seglist
[entry
->seg_count
++] = dmah
;
851 for (i
= 0; i
< (1 << page_order
); i
++) {
852 DRM_DEBUG("page %d @ 0x%08lx\n",
853 dma
->page_count
+ page_count
,
854 (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
);
855 temp_pagelist
[dma
->page_count
+ page_count
++]
856 = (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
;
859 offset
+ size
<= total
&& entry
->buf_count
< count
;
860 offset
+= alignment
, ++entry
->buf_count
) {
861 buf
= &entry
->buflist
[entry
->buf_count
];
862 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
863 buf
->total
= alignment
;
866 buf
->offset
= (dma
->byte_count
+ byte_count
+ offset
);
867 buf
->address
= (void *)(dmah
->vaddr
+ offset
);
868 buf
->bus_address
= dmah
->busaddr
+ offset
;
872 init_waitqueue_head(&buf
->dma_wait
);
875 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
876 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
,
878 if (!buf
->dev_private
) {
879 /* Set count correctly so we free the proper amount. */
880 entry
->buf_count
= count
;
881 entry
->seg_count
= count
;
882 drm_cleanup_buf_error(dev
, entry
);
883 drm_free(temp_pagelist
,
885 (count
<< page_order
))
886 * sizeof(*dma
->pagelist
),
888 mutex_unlock(&dev
->struct_mutex
);
889 atomic_dec(&dev
->buf_alloc
);
892 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
894 DRM_DEBUG("buffer %d @ %p\n",
895 entry
->buf_count
, buf
->address
);
897 byte_count
+= PAGE_SIZE
<< page_order
;
900 temp_buflist
= drm_realloc(dma
->buflist
,
901 dma
->buf_count
* sizeof(*dma
->buflist
),
902 (dma
->buf_count
+ entry
->buf_count
)
903 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
905 /* Free the entry because it isn't valid */
906 drm_cleanup_buf_error(dev
, entry
);
907 drm_free(temp_pagelist
,
908 (dma
->page_count
+ (count
<< page_order
))
909 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
910 mutex_unlock(&dev
->struct_mutex
);
911 atomic_dec(&dev
->buf_alloc
);
914 dma
->buflist
= temp_buflist
;
916 for (i
= 0; i
< entry
->buf_count
; i
++) {
917 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
920 /* No allocations failed, so now we can replace the orginal pagelist
923 if (dma
->page_count
) {
924 drm_free(dma
->pagelist
,
925 dma
->page_count
* sizeof(*dma
->pagelist
),
928 dma
->pagelist
= temp_pagelist
;
930 dma
->buf_count
+= entry
->buf_count
;
931 dma
->seg_count
+= entry
->seg_count
;
932 dma
->page_count
+= entry
->seg_count
<< page_order
;
933 dma
->byte_count
+= PAGE_SIZE
* (entry
->seg_count
<< page_order
);
935 mutex_unlock(&dev
->struct_mutex
);
937 request
->count
= entry
->buf_count
;
938 request
->size
= size
;
940 if (request
->flags
& _DRM_PCI_BUFFER_RO
)
941 dma
->flags
= _DRM_DMA_USE_PCI_RO
;
943 atomic_dec(&dev
->buf_alloc
);
947 EXPORT_SYMBOL(drm_addbufs_pci
);
949 static int drm_addbufs_sg(struct drm_device
* dev
, struct drm_buf_desc
* request
)
951 struct drm_device_dma
*dma
= dev
->dma
;
952 struct drm_buf_entry
*entry
;
954 unsigned long offset
;
955 unsigned long agp_offset
;
964 struct drm_buf
**temp_buflist
;
966 if (!drm_core_check_feature(dev
, DRIVER_SG
))
972 if (!capable(CAP_SYS_ADMIN
))
975 count
= request
->count
;
976 order
= drm_order(request
->size
);
979 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
980 ? PAGE_ALIGN(size
) : size
;
981 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
982 total
= PAGE_SIZE
<< page_order
;
985 agp_offset
= request
->agp_start
;
987 DRM_DEBUG("count: %d\n", count
);
988 DRM_DEBUG("order: %d\n", order
);
989 DRM_DEBUG("size: %d\n", size
);
990 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
991 DRM_DEBUG("alignment: %d\n", alignment
);
992 DRM_DEBUG("page_order: %d\n", page_order
);
993 DRM_DEBUG("total: %d\n", total
);
995 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
997 if (dev
->queue_count
)
998 return -EBUSY
; /* Not while in use */
1000 spin_lock(&dev
->count_lock
);
1002 spin_unlock(&dev
->count_lock
);
1005 atomic_inc(&dev
->buf_alloc
);
1006 spin_unlock(&dev
->count_lock
);
1008 mutex_lock(&dev
->struct_mutex
);
1009 entry
= &dma
->bufs
[order
];
1010 if (entry
->buf_count
) {
1011 mutex_unlock(&dev
->struct_mutex
);
1012 atomic_dec(&dev
->buf_alloc
);
1013 return -ENOMEM
; /* May only call once for each order */
1016 if (count
< 0 || count
> 4096) {
1017 mutex_unlock(&dev
->struct_mutex
);
1018 atomic_dec(&dev
->buf_alloc
);
1022 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
1024 if (!entry
->buflist
) {
1025 mutex_unlock(&dev
->struct_mutex
);
1026 atomic_dec(&dev
->buf_alloc
);
1029 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
1031 entry
->buf_size
= size
;
1032 entry
->page_order
= page_order
;
1036 while (entry
->buf_count
< count
) {
1037 buf
= &entry
->buflist
[entry
->buf_count
];
1038 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1039 buf
->total
= alignment
;
1043 buf
->offset
= (dma
->byte_count
+ offset
);
1044 buf
->bus_address
= agp_offset
+ offset
;
1045 buf
->address
= (void *)(agp_offset
+ offset
1046 + (unsigned long)dev
->sg
->virtual);
1050 init_waitqueue_head(&buf
->dma_wait
);
1053 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1054 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
1055 if (!buf
->dev_private
) {
1056 /* Set count correctly so we free the proper amount. */
1057 entry
->buf_count
= count
;
1058 drm_cleanup_buf_error(dev
, entry
);
1059 mutex_unlock(&dev
->struct_mutex
);
1060 atomic_dec(&dev
->buf_alloc
);
1064 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
1066 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1068 offset
+= alignment
;
1070 byte_count
+= PAGE_SIZE
<< page_order
;
1073 DRM_DEBUG("byte_count: %d\n", byte_count
);
1075 temp_buflist
= drm_realloc(dma
->buflist
,
1076 dma
->buf_count
* sizeof(*dma
->buflist
),
1077 (dma
->buf_count
+ entry
->buf_count
)
1078 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
1079 if (!temp_buflist
) {
1080 /* Free the entry because it isn't valid */
1081 drm_cleanup_buf_error(dev
, entry
);
1082 mutex_unlock(&dev
->struct_mutex
);
1083 atomic_dec(&dev
->buf_alloc
);
1086 dma
->buflist
= temp_buflist
;
1088 for (i
= 0; i
< entry
->buf_count
; i
++) {
1089 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1092 dma
->buf_count
+= entry
->buf_count
;
1093 dma
->seg_count
+= entry
->seg_count
;
1094 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1095 dma
->byte_count
+= byte_count
;
1097 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1098 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1100 mutex_unlock(&dev
->struct_mutex
);
1102 request
->count
= entry
->buf_count
;
1103 request
->size
= size
;
1105 dma
->flags
= _DRM_DMA_USE_SG
;
1107 atomic_dec(&dev
->buf_alloc
);
1111 static int drm_addbufs_fb(struct drm_device
* dev
, struct drm_buf_desc
* request
)
1113 struct drm_device_dma
*dma
= dev
->dma
;
1114 struct drm_buf_entry
*entry
;
1115 struct drm_buf
*buf
;
1116 unsigned long offset
;
1117 unsigned long agp_offset
;
1126 struct drm_buf
**temp_buflist
;
1128 if (!drm_core_check_feature(dev
, DRIVER_FB_DMA
))
1134 if (!capable(CAP_SYS_ADMIN
))
1137 count
= request
->count
;
1138 order
= drm_order(request
->size
);
1141 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
1142 ? PAGE_ALIGN(size
) : size
;
1143 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
1144 total
= PAGE_SIZE
<< page_order
;
1147 agp_offset
= request
->agp_start
;
1149 DRM_DEBUG("count: %d\n", count
);
1150 DRM_DEBUG("order: %d\n", order
);
1151 DRM_DEBUG("size: %d\n", size
);
1152 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
1153 DRM_DEBUG("alignment: %d\n", alignment
);
1154 DRM_DEBUG("page_order: %d\n", page_order
);
1155 DRM_DEBUG("total: %d\n", total
);
1157 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1159 if (dev
->queue_count
)
1160 return -EBUSY
; /* Not while in use */
1162 spin_lock(&dev
->count_lock
);
1164 spin_unlock(&dev
->count_lock
);
1167 atomic_inc(&dev
->buf_alloc
);
1168 spin_unlock(&dev
->count_lock
);
1170 mutex_lock(&dev
->struct_mutex
);
1171 entry
= &dma
->bufs
[order
];
1172 if (entry
->buf_count
) {
1173 mutex_unlock(&dev
->struct_mutex
);
1174 atomic_dec(&dev
->buf_alloc
);
1175 return -ENOMEM
; /* May only call once for each order */
1178 if (count
< 0 || count
> 4096) {
1179 mutex_unlock(&dev
->struct_mutex
);
1180 atomic_dec(&dev
->buf_alloc
);
1184 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
1186 if (!entry
->buflist
) {
1187 mutex_unlock(&dev
->struct_mutex
);
1188 atomic_dec(&dev
->buf_alloc
);
1191 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
1193 entry
->buf_size
= size
;
1194 entry
->page_order
= page_order
;
1198 while (entry
->buf_count
< count
) {
1199 buf
= &entry
->buflist
[entry
->buf_count
];
1200 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1201 buf
->total
= alignment
;
1205 buf
->offset
= (dma
->byte_count
+ offset
);
1206 buf
->bus_address
= agp_offset
+ offset
;
1207 buf
->address
= (void *)(agp_offset
+ offset
);
1211 init_waitqueue_head(&buf
->dma_wait
);
1214 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1215 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
1216 if (!buf
->dev_private
) {
1217 /* Set count correctly so we free the proper amount. */
1218 entry
->buf_count
= count
;
1219 drm_cleanup_buf_error(dev
, entry
);
1220 mutex_unlock(&dev
->struct_mutex
);
1221 atomic_dec(&dev
->buf_alloc
);
1224 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
1226 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1228 offset
+= alignment
;
1230 byte_count
+= PAGE_SIZE
<< page_order
;
1233 DRM_DEBUG("byte_count: %d\n", byte_count
);
1235 temp_buflist
= drm_realloc(dma
->buflist
,
1236 dma
->buf_count
* sizeof(*dma
->buflist
),
1237 (dma
->buf_count
+ entry
->buf_count
)
1238 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
1239 if (!temp_buflist
) {
1240 /* Free the entry because it isn't valid */
1241 drm_cleanup_buf_error(dev
, entry
);
1242 mutex_unlock(&dev
->struct_mutex
);
1243 atomic_dec(&dev
->buf_alloc
);
1246 dma
->buflist
= temp_buflist
;
1248 for (i
= 0; i
< entry
->buf_count
; i
++) {
1249 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1252 dma
->buf_count
+= entry
->buf_count
;
1253 dma
->seg_count
+= entry
->seg_count
;
1254 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1255 dma
->byte_count
+= byte_count
;
1257 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1258 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1260 mutex_unlock(&dev
->struct_mutex
);
1262 request
->count
= entry
->buf_count
;
1263 request
->size
= size
;
1265 dma
->flags
= _DRM_DMA_USE_FB
;
1267 atomic_dec(&dev
->buf_alloc
);
1273 * Add buffers for DMA transfers (ioctl).
1275 * \param inode device inode.
1276 * \param filp file pointer.
1277 * \param cmd command.
1278 * \param arg pointer to a struct drm_buf_desc request.
1279 * \return zero on success or a negative number on failure.
1281 * According with the memory type specified in drm_buf_desc::flags and the
1282 * build options, it dispatches the call either to addbufs_agp(),
1283 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1284 * PCI memory respectively.
1286 int drm_addbufs(struct inode
*inode
, struct file
*filp
,
1287 unsigned int cmd
, unsigned long arg
)
1289 struct drm_buf_desc request
;
1290 struct drm_file
*priv
= filp
->private_data
;
1291 struct drm_device
*dev
= priv
->head
->dev
;
1294 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1297 if (copy_from_user(&request
, (struct drm_buf_desc __user
*) arg
,
1302 if (request
.flags
& _DRM_AGP_BUFFER
)
1303 ret
= drm_addbufs_agp(dev
, &request
);
1306 if (request
.flags
& _DRM_SG_BUFFER
)
1307 ret
= drm_addbufs_sg(dev
, &request
);
1308 else if (request
.flags
& _DRM_FB_BUFFER
)
1309 ret
= drm_addbufs_fb(dev
, &request
);
1311 ret
= drm_addbufs_pci(dev
, &request
);
1314 if (copy_to_user((void __user
*)arg
, &request
, sizeof(request
))) {
1322 * Get information about the buffer mappings.
1324 * This was originally mean for debugging purposes, or by a sophisticated
1325 * client library to determine how best to use the available buffers (e.g.,
1326 * large buffers can be used for image transfer).
1328 * \param inode device inode.
1329 * \param filp file pointer.
1330 * \param cmd command.
1331 * \param arg pointer to a drm_buf_info structure.
1332 * \return zero on success or a negative number on failure.
1334 * Increments drm_device::buf_use while holding the drm_device::count_lock
1335 * lock, preventing of allocating more buffers after this call. Information
1336 * about each requested buffer is then copied into user space.
1338 int drm_infobufs(struct inode
*inode
, struct file
*filp
,
1339 unsigned int cmd
, unsigned long arg
)
1341 struct drm_file
*priv
= filp
->private_data
;
1342 struct drm_device
*dev
= priv
->head
->dev
;
1343 struct drm_device_dma
*dma
= dev
->dma
;
1344 struct drm_buf_info request
;
1345 struct drm_buf_info __user
*argp
= (void __user
*)arg
;
1349 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1355 spin_lock(&dev
->count_lock
);
1356 if (atomic_read(&dev
->buf_alloc
)) {
1357 spin_unlock(&dev
->count_lock
);
1360 ++dev
->buf_use
; /* Can't allocate more after this call */
1361 spin_unlock(&dev
->count_lock
);
1363 if (copy_from_user(&request
, argp
, sizeof(request
)))
1366 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1367 if (dma
->bufs
[i
].buf_count
)
1371 DRM_DEBUG("count = %d\n", count
);
1373 if (request
.count
>= count
) {
1374 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1375 if (dma
->bufs
[i
].buf_count
) {
1376 struct drm_buf_desc __user
*to
=
1377 &request
.list
[count
];
1378 struct drm_buf_entry
*from
= &dma
->bufs
[i
];
1379 struct drm_freelist
*list
= &dma
->bufs
[i
].freelist
;
1380 if (copy_to_user(&to
->count
,
1382 sizeof(from
->buf_count
)) ||
1383 copy_to_user(&to
->size
,
1385 sizeof(from
->buf_size
)) ||
1386 copy_to_user(&to
->low_mark
,
1388 sizeof(list
->low_mark
)) ||
1389 copy_to_user(&to
->high_mark
,
1391 sizeof(list
->high_mark
)))
1394 DRM_DEBUG("%d %d %d %d %d\n",
1396 dma
->bufs
[i
].buf_count
,
1397 dma
->bufs
[i
].buf_size
,
1398 dma
->bufs
[i
].freelist
.low_mark
,
1399 dma
->bufs
[i
].freelist
.high_mark
);
1404 request
.count
= count
;
1406 if (copy_to_user(argp
, &request
, sizeof(request
)))
1413 * Specifies a low and high water mark for buffer allocation
1415 * \param inode device inode.
1416 * \param filp file pointer.
1417 * \param cmd command.
1418 * \param arg a pointer to a drm_buf_desc structure.
1419 * \return zero on success or a negative number on failure.
1421 * Verifies that the size order is bounded between the admissible orders and
1422 * updates the respective drm_device_dma::bufs entry low and high water mark.
1424 * \note This ioctl is deprecated and mostly never used.
1426 int drm_markbufs(struct inode
*inode
, struct file
*filp
,
1427 unsigned int cmd
, unsigned long arg
)
1429 struct drm_file
*priv
= filp
->private_data
;
1430 struct drm_device
*dev
= priv
->head
->dev
;
1431 struct drm_device_dma
*dma
= dev
->dma
;
1432 struct drm_buf_desc request
;
1434 struct drm_buf_entry
*entry
;
1436 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1442 if (copy_from_user(&request
,
1443 (struct drm_buf_desc __user
*) arg
, sizeof(request
)))
1446 DRM_DEBUG("%d, %d, %d\n",
1447 request
.size
, request
.low_mark
, request
.high_mark
);
1448 order
= drm_order(request
.size
);
1449 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1451 entry
= &dma
->bufs
[order
];
1453 if (request
.low_mark
< 0 || request
.low_mark
> entry
->buf_count
)
1455 if (request
.high_mark
< 0 || request
.high_mark
> entry
->buf_count
)
1458 entry
->freelist
.low_mark
= request
.low_mark
;
1459 entry
->freelist
.high_mark
= request
.high_mark
;
1465 * Unreserve the buffers in list, previously reserved using drmDMA.
1467 * \param inode device inode.
1468 * \param filp file pointer.
1469 * \param cmd command.
1470 * \param arg pointer to a drm_buf_free structure.
1471 * \return zero on success or a negative number on failure.
1473 * Calls free_buffer() for each used buffer.
1474 * This function is primarily used for debugging.
1476 int drm_freebufs(struct inode
*inode
, struct file
*filp
,
1477 unsigned int cmd
, unsigned long arg
)
1479 struct drm_file
*priv
= filp
->private_data
;
1480 struct drm_device
*dev
= priv
->head
->dev
;
1481 struct drm_device_dma
*dma
= dev
->dma
;
1482 struct drm_buf_free request
;
1485 struct drm_buf
*buf
;
1487 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1493 if (copy_from_user(&request
,
1494 (struct drm_buf_free __user
*) arg
, sizeof(request
)))
1497 DRM_DEBUG("%d\n", request
.count
);
1498 for (i
= 0; i
< request
.count
; i
++) {
1499 if (copy_from_user(&idx
, &request
.list
[i
], sizeof(idx
)))
1501 if (idx
< 0 || idx
>= dma
->buf_count
) {
1502 DRM_ERROR("Index %d (of %d max)\n",
1503 idx
, dma
->buf_count
- 1);
1506 buf
= dma
->buflist
[idx
];
1507 if (buf
->filp
!= filp
) {
1508 DRM_ERROR("Process %d freeing buffer not owned\n",
1512 drm_free_buffer(dev
, buf
);
1519 * Maps all of the DMA buffers into client-virtual space (ioctl).
1521 * \param inode device inode.
1522 * \param filp file pointer.
1523 * \param cmd command.
1524 * \param arg pointer to a drm_buf_map structure.
1525 * \return zero on success or a negative number on failure.
1527 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1528 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1529 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1532 int drm_mapbufs(struct inode
*inode
, struct file
*filp
,
1533 unsigned int cmd
, unsigned long arg
)
1535 struct drm_file
*priv
= filp
->private_data
;
1536 struct drm_device
*dev
= priv
->head
->dev
;
1537 struct drm_device_dma
*dma
= dev
->dma
;
1538 struct drm_buf_map __user
*argp
= (void __user
*)arg
;
1541 unsigned long virtual;
1542 unsigned long address
;
1543 struct drm_buf_map request
;
1546 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1552 spin_lock(&dev
->count_lock
);
1553 if (atomic_read(&dev
->buf_alloc
)) {
1554 spin_unlock(&dev
->count_lock
);
1557 dev
->buf_use
++; /* Can't allocate more after this call */
1558 spin_unlock(&dev
->count_lock
);
1560 if (copy_from_user(&request
, argp
, sizeof(request
)))
1563 if (request
.count
>= dma
->buf_count
) {
1564 if ((drm_core_has_AGP(dev
) && (dma
->flags
& _DRM_DMA_USE_AGP
))
1565 || (drm_core_check_feature(dev
, DRIVER_SG
)
1566 && (dma
->flags
& _DRM_DMA_USE_SG
))
1567 || (drm_core_check_feature(dev
, DRIVER_FB_DMA
)
1568 && (dma
->flags
& _DRM_DMA_USE_FB
))) {
1569 struct drm_map
*map
= dev
->agp_buffer_map
;
1570 unsigned long token
= dev
->agp_buffer_token
;
1577 down_write(¤t
->mm
->mmap_sem
);
1578 virtual = do_mmap(filp
, 0, map
->size
,
1579 PROT_READ
| PROT_WRITE
,
1581 up_write(¤t
->mm
->mmap_sem
);
1583 down_write(¤t
->mm
->mmap_sem
);
1584 virtual = do_mmap(filp
, 0, dma
->byte_count
,
1585 PROT_READ
| PROT_WRITE
,
1587 up_write(¤t
->mm
->mmap_sem
);
1589 if (virtual > -1024UL) {
1591 retcode
= (signed long)virtual;
1594 request
.virtual = (void __user
*)virtual;
1596 for (i
= 0; i
< dma
->buf_count
; i
++) {
1597 if (copy_to_user(&request
.list
[i
].idx
,
1598 &dma
->buflist
[i
]->idx
,
1599 sizeof(request
.list
[0].idx
))) {
1603 if (copy_to_user(&request
.list
[i
].total
,
1604 &dma
->buflist
[i
]->total
,
1605 sizeof(request
.list
[0].total
))) {
1609 if (copy_to_user(&request
.list
[i
].used
,
1610 &zero
, sizeof(zero
))) {
1614 address
= virtual + dma
->buflist
[i
]->offset
; /* *** */
1615 if (copy_to_user(&request
.list
[i
].address
,
1616 &address
, sizeof(address
))) {
1623 request
.count
= dma
->buf_count
;
1624 DRM_DEBUG("%d buffers, retcode = %d\n", request
.count
, retcode
);
1626 if (copy_to_user(argp
, &request
, sizeof(request
)))
1633 * Compute size order. Returns the exponent of the smaller power of two which
1634 * is greater or equal to given number.
1639 * \todo Can be made faster.
1641 int drm_order(unsigned long size
)
1646 for (order
= 0, tmp
= size
>> 1; tmp
; tmp
>>= 1, order
++) ;
1648 if (size
& (size
- 1))
1653 EXPORT_SYMBOL(drm_order
);