]>
git.proxmox.com Git - mirror_ubuntu-focal-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 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 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
,
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 /* Note: dev->agp->base may actually be 0 when the DRM
233 * is not in control of AGP space. But if user space is
234 * it should already have added the AGP base itself.
236 map
->offset
+= dev
->agp
->base
;
237 map
->mtrr
= dev
->agp
->agp_mtrr
; /* for getmap */
239 /* This assumes the DRM is in total control of AGP space.
240 * It's not always the case as AGP can be in the control
241 * of user space (i.e. i810 driver). So this loop will get
242 * skipped and we double check that dev->agp->memory is
243 * actually set as well as being invalid before EPERM'ing
245 list_for_each_entry(entry
, &dev
->agp
->memory
, head
) {
246 if ((map
->offset
>= entry
->bound
) &&
247 (map
->offset
+ map
->size
<= entry
->bound
+ entry
->pages
* PAGE_SIZE
)) {
252 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
253 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
256 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map
->offset
, map
->size
);
260 case _DRM_SCATTER_GATHER
:
262 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
265 map
->offset
+= (unsigned long)dev
->sg
->virtual;
267 case _DRM_CONSISTENT
:
268 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
269 * As we're limiting the address to 2^32-1 (or less),
270 * casting it down to 32 bits is no problem, but we
271 * need to point to a 64bit variable first. */
272 dmah
= drm_pci_alloc(dev
, map
->size
, map
->size
, 0xffffffffUL
);
274 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
277 map
->handle
= dmah
->vaddr
;
278 map
->offset
= (unsigned long)dmah
->busaddr
;
282 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
286 list
= drm_alloc(sizeof(*list
), DRM_MEM_MAPS
);
288 if (map
->type
== _DRM_REGISTERS
)
289 iounmap(map
->handle
);
290 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
293 memset(list
, 0, sizeof(*list
));
296 mutex_lock(&dev
->struct_mutex
);
297 list_add(&list
->head
, &dev
->maplist
);
299 /* Assign a 32-bit handle */
300 /* We do it here so that dev->struct_mutex protects the increment */
301 user_token
= (map
->type
== _DRM_SHM
) ? (unsigned long)map
->handle
:
303 ret
= drm_map_handle(dev
, &list
->hash
, user_token
, 0);
305 if (map
->type
== _DRM_REGISTERS
)
306 iounmap(map
->handle
);
307 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
308 drm_free(list
, sizeof(*list
), DRM_MEM_MAPS
);
309 mutex_unlock(&dev
->struct_mutex
);
313 list
->user_token
= list
->hash
.key
<< PAGE_SHIFT
;
314 mutex_unlock(&dev
->struct_mutex
);
320 int drm_addmap(struct drm_device
* dev
, unsigned int offset
,
321 unsigned int size
, enum drm_map_type type
,
322 enum drm_map_flags flags
, drm_local_map_t
** map_ptr
)
324 struct drm_map_list
*list
;
327 rc
= drm_addmap_core(dev
, offset
, size
, type
, flags
, &list
);
329 *map_ptr
= list
->map
;
333 EXPORT_SYMBOL(drm_addmap
);
335 int drm_addmap_ioctl(struct inode
*inode
, struct file
*filp
,
336 unsigned int cmd
, unsigned long arg
)
338 struct drm_file
*priv
= filp
->private_data
;
339 struct drm_device
*dev
= priv
->head
->dev
;
341 struct drm_map_list
*maplist
;
342 struct drm_map __user
*argp
= (void __user
*)arg
;
345 if (!(filp
->f_mode
& 3))
346 return -EACCES
; /* Require read/write */
348 if (copy_from_user(&map
, argp
, sizeof(map
))) {
352 if (!(capable(CAP_SYS_ADMIN
) || map
.type
== _DRM_AGP
))
355 err
= drm_addmap_core(dev
, map
.offset
, map
.size
, map
.type
, map
.flags
,
361 if (copy_to_user(argp
, maplist
->map
, sizeof(struct drm_map
)))
364 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
365 if (put_user((void *)(unsigned long)maplist
->user_token
, &argp
->handle
))
371 * Remove a map private from list and deallocate resources if the mapping
374 * \param inode device inode.
375 * \param filp file pointer.
376 * \param cmd command.
377 * \param arg pointer to a struct drm_map structure.
378 * \return zero on success or a negative value on error.
380 * Searches the map on drm_device::maplist, removes it from the list, see if
381 * its being used, and free any associate resource (such as MTRR's) if it's not
386 int drm_rmmap_locked(struct drm_device
*dev
, drm_local_map_t
*map
)
388 struct drm_map_list
*r_list
= NULL
, *list_t
;
389 drm_dma_handle_t dmah
;
392 /* Find the list entry for the map and remove it */
393 list_for_each_entry_safe(r_list
, list_t
, &dev
->maplist
, head
) {
394 if (r_list
->map
== map
) {
395 list_del(&r_list
->head
);
396 drm_ht_remove_key(&dev
->map_hash
,
397 r_list
->user_token
>> PAGE_SHIFT
);
398 drm_free(r_list
, sizeof(*r_list
), DRM_MEM_MAPS
);
409 iounmap(map
->handle
);
411 case _DRM_FRAME_BUFFER
:
412 if (drm_core_has_MTRR(dev
) && map
->mtrr
>= 0) {
414 retcode
= mtrr_del(map
->mtrr
, map
->offset
, map
->size
);
415 DRM_DEBUG("mtrr_del=%d\n", retcode
);
422 case _DRM_SCATTER_GATHER
:
424 case _DRM_CONSISTENT
:
425 dmah
.vaddr
= map
->handle
;
426 dmah
.busaddr
= map
->offset
;
427 dmah
.size
= map
->size
;
428 __drm_pci_free(dev
, &dmah
);
431 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
436 int drm_rmmap(struct drm_device
*dev
, drm_local_map_t
*map
)
440 mutex_lock(&dev
->struct_mutex
);
441 ret
= drm_rmmap_locked(dev
, map
);
442 mutex_unlock(&dev
->struct_mutex
);
447 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
448 * the last close of the device, and this is necessary for cleanup when things
449 * exit uncleanly. Therefore, having userland manually remove mappings seems
450 * like a pointless exercise since they're going away anyway.
452 * One use case might be after addmap is allowed for normal users for SHM and
453 * gets used by drivers that the server doesn't need to care about. This seems
456 int drm_rmmap_ioctl(struct inode
*inode
, struct file
*filp
,
457 unsigned int cmd
, unsigned long arg
)
459 struct drm_file
*priv
= filp
->private_data
;
460 struct drm_device
*dev
= priv
->head
->dev
;
461 struct drm_map request
;
462 drm_local_map_t
*map
= NULL
;
463 struct drm_map_list
*r_list
;
466 if (copy_from_user(&request
, (struct drm_map __user
*) arg
, sizeof(request
))) {
470 mutex_lock(&dev
->struct_mutex
);
471 list_for_each_entry(r_list
, &dev
->maplist
, head
) {
473 r_list
->user_token
== (unsigned long)request
.handle
&&
474 r_list
->map
->flags
& _DRM_REMOVABLE
) {
480 /* List has wrapped around to the head pointer, or its empty we didn't
483 if (list_empty(&dev
->maplist
) || !map
) {
484 mutex_unlock(&dev
->struct_mutex
);
488 /* Register and framebuffer maps are permanent */
489 if ((map
->type
== _DRM_REGISTERS
) || (map
->type
== _DRM_FRAME_BUFFER
)) {
490 mutex_unlock(&dev
->struct_mutex
);
494 ret
= drm_rmmap_locked(dev
, map
);
496 mutex_unlock(&dev
->struct_mutex
);
502 * Cleanup after an error on one of the addbufs() functions.
504 * \param dev DRM device.
505 * \param entry buffer entry where the error occurred.
507 * Frees any pages and buffers associated with the given entry.
509 static void drm_cleanup_buf_error(struct drm_device
* dev
,
510 struct drm_buf_entry
* entry
)
514 if (entry
->seg_count
) {
515 for (i
= 0; i
< entry
->seg_count
; i
++) {
516 if (entry
->seglist
[i
]) {
517 drm_pci_free(dev
, entry
->seglist
[i
]);
520 drm_free(entry
->seglist
,
522 sizeof(*entry
->seglist
), DRM_MEM_SEGS
);
524 entry
->seg_count
= 0;
527 if (entry
->buf_count
) {
528 for (i
= 0; i
< entry
->buf_count
; i
++) {
529 if (entry
->buflist
[i
].dev_private
) {
530 drm_free(entry
->buflist
[i
].dev_private
,
531 entry
->buflist
[i
].dev_priv_size
,
535 drm_free(entry
->buflist
,
537 sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
539 entry
->buf_count
= 0;
545 * Add AGP buffers for DMA transfers.
547 * \param dev struct drm_device to which the buffers are to be added.
548 * \param request pointer to a struct drm_buf_desc describing the request.
549 * \return zero on success or a negative number on failure.
551 * After some sanity checks creates a drm_buf structure for each buffer and
552 * reallocates the buffer list of the same size order to accommodate the new
555 int drm_addbufs_agp(struct drm_device
* dev
, struct drm_buf_desc
* request
)
557 struct drm_device_dma
*dma
= dev
->dma
;
558 struct drm_buf_entry
*entry
;
559 struct drm_agp_mem
*agp_entry
;
561 unsigned long offset
;
562 unsigned long agp_offset
;
571 struct drm_buf
**temp_buflist
;
576 count
= request
->count
;
577 order
= drm_order(request
->size
);
580 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
581 ? PAGE_ALIGN(size
) : size
;
582 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
583 total
= PAGE_SIZE
<< page_order
;
586 agp_offset
= dev
->agp
->base
+ request
->agp_start
;
588 DRM_DEBUG("count: %d\n", count
);
589 DRM_DEBUG("order: %d\n", order
);
590 DRM_DEBUG("size: %d\n", size
);
591 DRM_DEBUG("agp_offset: %lx\n", agp_offset
);
592 DRM_DEBUG("alignment: %d\n", alignment
);
593 DRM_DEBUG("page_order: %d\n", page_order
);
594 DRM_DEBUG("total: %d\n", total
);
596 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
598 if (dev
->queue_count
)
599 return -EBUSY
; /* Not while in use */
601 /* Make sure buffers are located in AGP memory that we own */
603 list_for_each_entry(agp_entry
, &dev
->agp
->memory
, head
) {
604 if ((agp_offset
>= agp_entry
->bound
) &&
605 (agp_offset
+ total
* count
<= agp_entry
->bound
+ agp_entry
->pages
* PAGE_SIZE
)) {
610 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
611 DRM_DEBUG("zone invalid\n");
614 spin_lock(&dev
->count_lock
);
616 spin_unlock(&dev
->count_lock
);
619 atomic_inc(&dev
->buf_alloc
);
620 spin_unlock(&dev
->count_lock
);
622 mutex_lock(&dev
->struct_mutex
);
623 entry
= &dma
->bufs
[order
];
624 if (entry
->buf_count
) {
625 mutex_unlock(&dev
->struct_mutex
);
626 atomic_dec(&dev
->buf_alloc
);
627 return -ENOMEM
; /* May only call once for each order */
630 if (count
< 0 || count
> 4096) {
631 mutex_unlock(&dev
->struct_mutex
);
632 atomic_dec(&dev
->buf_alloc
);
636 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
638 if (!entry
->buflist
) {
639 mutex_unlock(&dev
->struct_mutex
);
640 atomic_dec(&dev
->buf_alloc
);
643 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
645 entry
->buf_size
= size
;
646 entry
->page_order
= page_order
;
650 while (entry
->buf_count
< count
) {
651 buf
= &entry
->buflist
[entry
->buf_count
];
652 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
653 buf
->total
= alignment
;
657 buf
->offset
= (dma
->byte_count
+ offset
);
658 buf
->bus_address
= agp_offset
+ offset
;
659 buf
->address
= (void *)(agp_offset
+ offset
);
663 init_waitqueue_head(&buf
->dma_wait
);
666 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
667 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
668 if (!buf
->dev_private
) {
669 /* Set count correctly so we free the proper amount. */
670 entry
->buf_count
= count
;
671 drm_cleanup_buf_error(dev
, entry
);
672 mutex_unlock(&dev
->struct_mutex
);
673 atomic_dec(&dev
->buf_alloc
);
676 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
678 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
682 byte_count
+= PAGE_SIZE
<< page_order
;
685 DRM_DEBUG("byte_count: %d\n", byte_count
);
687 temp_buflist
= drm_realloc(dma
->buflist
,
688 dma
->buf_count
* sizeof(*dma
->buflist
),
689 (dma
->buf_count
+ entry
->buf_count
)
690 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
692 /* Free the entry because it isn't valid */
693 drm_cleanup_buf_error(dev
, entry
);
694 mutex_unlock(&dev
->struct_mutex
);
695 atomic_dec(&dev
->buf_alloc
);
698 dma
->buflist
= temp_buflist
;
700 for (i
= 0; i
< entry
->buf_count
; i
++) {
701 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
704 dma
->buf_count
+= entry
->buf_count
;
705 dma
->seg_count
+= entry
->seg_count
;
706 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
707 dma
->byte_count
+= byte_count
;
709 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
710 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
712 mutex_unlock(&dev
->struct_mutex
);
714 request
->count
= entry
->buf_count
;
715 request
->size
= size
;
717 dma
->flags
= _DRM_DMA_USE_AGP
;
719 atomic_dec(&dev
->buf_alloc
);
722 EXPORT_SYMBOL(drm_addbufs_agp
);
723 #endif /* __OS_HAS_AGP */
725 int drm_addbufs_pci(struct drm_device
* dev
, struct drm_buf_desc
* request
)
727 struct drm_device_dma
*dma
= dev
->dma
;
733 struct drm_buf_entry
*entry
;
734 drm_dma_handle_t
*dmah
;
737 unsigned long offset
;
741 unsigned long *temp_pagelist
;
742 struct drm_buf
**temp_buflist
;
744 if (!drm_core_check_feature(dev
, DRIVER_PCI_DMA
))
750 if (!capable(CAP_SYS_ADMIN
))
753 count
= request
->count
;
754 order
= drm_order(request
->size
);
757 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
758 request
->count
, request
->size
, size
, order
, dev
->queue_count
);
760 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
762 if (dev
->queue_count
)
763 return -EBUSY
; /* Not while in use */
765 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
766 ? PAGE_ALIGN(size
) : size
;
767 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
768 total
= PAGE_SIZE
<< page_order
;
770 spin_lock(&dev
->count_lock
);
772 spin_unlock(&dev
->count_lock
);
775 atomic_inc(&dev
->buf_alloc
);
776 spin_unlock(&dev
->count_lock
);
778 mutex_lock(&dev
->struct_mutex
);
779 entry
= &dma
->bufs
[order
];
780 if (entry
->buf_count
) {
781 mutex_unlock(&dev
->struct_mutex
);
782 atomic_dec(&dev
->buf_alloc
);
783 return -ENOMEM
; /* May only call once for each order */
786 if (count
< 0 || count
> 4096) {
787 mutex_unlock(&dev
->struct_mutex
);
788 atomic_dec(&dev
->buf_alloc
);
792 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
794 if (!entry
->buflist
) {
795 mutex_unlock(&dev
->struct_mutex
);
796 atomic_dec(&dev
->buf_alloc
);
799 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
801 entry
->seglist
= drm_alloc(count
* sizeof(*entry
->seglist
),
803 if (!entry
->seglist
) {
804 drm_free(entry
->buflist
,
805 count
* sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
806 mutex_unlock(&dev
->struct_mutex
);
807 atomic_dec(&dev
->buf_alloc
);
810 memset(entry
->seglist
, 0, count
* sizeof(*entry
->seglist
));
812 /* Keep the original pagelist until we know all the allocations
815 temp_pagelist
= drm_alloc((dma
->page_count
+ (count
<< page_order
))
816 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
817 if (!temp_pagelist
) {
818 drm_free(entry
->buflist
,
819 count
* sizeof(*entry
->buflist
), DRM_MEM_BUFS
);
820 drm_free(entry
->seglist
,
821 count
* sizeof(*entry
->seglist
), DRM_MEM_SEGS
);
822 mutex_unlock(&dev
->struct_mutex
);
823 atomic_dec(&dev
->buf_alloc
);
826 memcpy(temp_pagelist
,
827 dma
->pagelist
, dma
->page_count
* sizeof(*dma
->pagelist
));
828 DRM_DEBUG("pagelist: %d entries\n",
829 dma
->page_count
+ (count
<< page_order
));
831 entry
->buf_size
= size
;
832 entry
->page_order
= page_order
;
836 while (entry
->buf_count
< count
) {
838 dmah
= drm_pci_alloc(dev
, PAGE_SIZE
<< page_order
, 0x1000, 0xfffffffful
);
841 /* Set count correctly so we free the proper amount. */
842 entry
->buf_count
= count
;
843 entry
->seg_count
= count
;
844 drm_cleanup_buf_error(dev
, entry
);
845 drm_free(temp_pagelist
,
846 (dma
->page_count
+ (count
<< page_order
))
847 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
848 mutex_unlock(&dev
->struct_mutex
);
849 atomic_dec(&dev
->buf_alloc
);
852 entry
->seglist
[entry
->seg_count
++] = dmah
;
853 for (i
= 0; i
< (1 << page_order
); i
++) {
854 DRM_DEBUG("page %d @ 0x%08lx\n",
855 dma
->page_count
+ page_count
,
856 (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
);
857 temp_pagelist
[dma
->page_count
+ page_count
++]
858 = (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
;
861 offset
+ size
<= total
&& entry
->buf_count
< count
;
862 offset
+= alignment
, ++entry
->buf_count
) {
863 buf
= &entry
->buflist
[entry
->buf_count
];
864 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
865 buf
->total
= alignment
;
868 buf
->offset
= (dma
->byte_count
+ byte_count
+ offset
);
869 buf
->address
= (void *)(dmah
->vaddr
+ offset
);
870 buf
->bus_address
= dmah
->busaddr
+ offset
;
874 init_waitqueue_head(&buf
->dma_wait
);
877 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
878 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
,
880 if (!buf
->dev_private
) {
881 /* Set count correctly so we free the proper amount. */
882 entry
->buf_count
= count
;
883 entry
->seg_count
= count
;
884 drm_cleanup_buf_error(dev
, entry
);
885 drm_free(temp_pagelist
,
887 (count
<< page_order
))
888 * sizeof(*dma
->pagelist
),
890 mutex_unlock(&dev
->struct_mutex
);
891 atomic_dec(&dev
->buf_alloc
);
894 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
896 DRM_DEBUG("buffer %d @ %p\n",
897 entry
->buf_count
, buf
->address
);
899 byte_count
+= PAGE_SIZE
<< page_order
;
902 temp_buflist
= drm_realloc(dma
->buflist
,
903 dma
->buf_count
* sizeof(*dma
->buflist
),
904 (dma
->buf_count
+ entry
->buf_count
)
905 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
907 /* Free the entry because it isn't valid */
908 drm_cleanup_buf_error(dev
, entry
);
909 drm_free(temp_pagelist
,
910 (dma
->page_count
+ (count
<< page_order
))
911 * sizeof(*dma
->pagelist
), DRM_MEM_PAGES
);
912 mutex_unlock(&dev
->struct_mutex
);
913 atomic_dec(&dev
->buf_alloc
);
916 dma
->buflist
= temp_buflist
;
918 for (i
= 0; i
< entry
->buf_count
; i
++) {
919 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
922 /* No allocations failed, so now we can replace the orginal pagelist
925 if (dma
->page_count
) {
926 drm_free(dma
->pagelist
,
927 dma
->page_count
* sizeof(*dma
->pagelist
),
930 dma
->pagelist
= temp_pagelist
;
932 dma
->buf_count
+= entry
->buf_count
;
933 dma
->seg_count
+= entry
->seg_count
;
934 dma
->page_count
+= entry
->seg_count
<< page_order
;
935 dma
->byte_count
+= PAGE_SIZE
* (entry
->seg_count
<< page_order
);
937 mutex_unlock(&dev
->struct_mutex
);
939 request
->count
= entry
->buf_count
;
940 request
->size
= size
;
942 if (request
->flags
& _DRM_PCI_BUFFER_RO
)
943 dma
->flags
= _DRM_DMA_USE_PCI_RO
;
945 atomic_dec(&dev
->buf_alloc
);
949 EXPORT_SYMBOL(drm_addbufs_pci
);
951 static int drm_addbufs_sg(struct drm_device
* dev
, struct drm_buf_desc
* request
)
953 struct drm_device_dma
*dma
= dev
->dma
;
954 struct drm_buf_entry
*entry
;
956 unsigned long offset
;
957 unsigned long agp_offset
;
966 struct drm_buf
**temp_buflist
;
968 if (!drm_core_check_feature(dev
, DRIVER_SG
))
974 if (!capable(CAP_SYS_ADMIN
))
977 count
= request
->count
;
978 order
= drm_order(request
->size
);
981 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
982 ? PAGE_ALIGN(size
) : size
;
983 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
984 total
= PAGE_SIZE
<< page_order
;
987 agp_offset
= request
->agp_start
;
989 DRM_DEBUG("count: %d\n", count
);
990 DRM_DEBUG("order: %d\n", order
);
991 DRM_DEBUG("size: %d\n", size
);
992 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
993 DRM_DEBUG("alignment: %d\n", alignment
);
994 DRM_DEBUG("page_order: %d\n", page_order
);
995 DRM_DEBUG("total: %d\n", total
);
997 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
999 if (dev
->queue_count
)
1000 return -EBUSY
; /* Not while in use */
1002 spin_lock(&dev
->count_lock
);
1004 spin_unlock(&dev
->count_lock
);
1007 atomic_inc(&dev
->buf_alloc
);
1008 spin_unlock(&dev
->count_lock
);
1010 mutex_lock(&dev
->struct_mutex
);
1011 entry
= &dma
->bufs
[order
];
1012 if (entry
->buf_count
) {
1013 mutex_unlock(&dev
->struct_mutex
);
1014 atomic_dec(&dev
->buf_alloc
);
1015 return -ENOMEM
; /* May only call once for each order */
1018 if (count
< 0 || count
> 4096) {
1019 mutex_unlock(&dev
->struct_mutex
);
1020 atomic_dec(&dev
->buf_alloc
);
1024 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
1026 if (!entry
->buflist
) {
1027 mutex_unlock(&dev
->struct_mutex
);
1028 atomic_dec(&dev
->buf_alloc
);
1031 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
1033 entry
->buf_size
= size
;
1034 entry
->page_order
= page_order
;
1038 while (entry
->buf_count
< count
) {
1039 buf
= &entry
->buflist
[entry
->buf_count
];
1040 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1041 buf
->total
= alignment
;
1045 buf
->offset
= (dma
->byte_count
+ offset
);
1046 buf
->bus_address
= agp_offset
+ offset
;
1047 buf
->address
= (void *)(agp_offset
+ offset
1048 + (unsigned long)dev
->sg
->virtual);
1052 init_waitqueue_head(&buf
->dma_wait
);
1055 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1056 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
1057 if (!buf
->dev_private
) {
1058 /* Set count correctly so we free the proper amount. */
1059 entry
->buf_count
= count
;
1060 drm_cleanup_buf_error(dev
, entry
);
1061 mutex_unlock(&dev
->struct_mutex
);
1062 atomic_dec(&dev
->buf_alloc
);
1066 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
1068 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1070 offset
+= alignment
;
1072 byte_count
+= PAGE_SIZE
<< page_order
;
1075 DRM_DEBUG("byte_count: %d\n", byte_count
);
1077 temp_buflist
= drm_realloc(dma
->buflist
,
1078 dma
->buf_count
* sizeof(*dma
->buflist
),
1079 (dma
->buf_count
+ entry
->buf_count
)
1080 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
1081 if (!temp_buflist
) {
1082 /* Free the entry because it isn't valid */
1083 drm_cleanup_buf_error(dev
, entry
);
1084 mutex_unlock(&dev
->struct_mutex
);
1085 atomic_dec(&dev
->buf_alloc
);
1088 dma
->buflist
= temp_buflist
;
1090 for (i
= 0; i
< entry
->buf_count
; i
++) {
1091 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1094 dma
->buf_count
+= entry
->buf_count
;
1095 dma
->seg_count
+= entry
->seg_count
;
1096 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1097 dma
->byte_count
+= byte_count
;
1099 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1100 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1102 mutex_unlock(&dev
->struct_mutex
);
1104 request
->count
= entry
->buf_count
;
1105 request
->size
= size
;
1107 dma
->flags
= _DRM_DMA_USE_SG
;
1109 atomic_dec(&dev
->buf_alloc
);
1113 static int drm_addbufs_fb(struct drm_device
* dev
, struct drm_buf_desc
* request
)
1115 struct drm_device_dma
*dma
= dev
->dma
;
1116 struct drm_buf_entry
*entry
;
1117 struct drm_buf
*buf
;
1118 unsigned long offset
;
1119 unsigned long agp_offset
;
1128 struct drm_buf
**temp_buflist
;
1130 if (!drm_core_check_feature(dev
, DRIVER_FB_DMA
))
1136 if (!capable(CAP_SYS_ADMIN
))
1139 count
= request
->count
;
1140 order
= drm_order(request
->size
);
1143 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
1144 ? PAGE_ALIGN(size
) : size
;
1145 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
1146 total
= PAGE_SIZE
<< page_order
;
1149 agp_offset
= request
->agp_start
;
1151 DRM_DEBUG("count: %d\n", count
);
1152 DRM_DEBUG("order: %d\n", order
);
1153 DRM_DEBUG("size: %d\n", size
);
1154 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
1155 DRM_DEBUG("alignment: %d\n", alignment
);
1156 DRM_DEBUG("page_order: %d\n", page_order
);
1157 DRM_DEBUG("total: %d\n", total
);
1159 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1161 if (dev
->queue_count
)
1162 return -EBUSY
; /* Not while in use */
1164 spin_lock(&dev
->count_lock
);
1166 spin_unlock(&dev
->count_lock
);
1169 atomic_inc(&dev
->buf_alloc
);
1170 spin_unlock(&dev
->count_lock
);
1172 mutex_lock(&dev
->struct_mutex
);
1173 entry
= &dma
->bufs
[order
];
1174 if (entry
->buf_count
) {
1175 mutex_unlock(&dev
->struct_mutex
);
1176 atomic_dec(&dev
->buf_alloc
);
1177 return -ENOMEM
; /* May only call once for each order */
1180 if (count
< 0 || count
> 4096) {
1181 mutex_unlock(&dev
->struct_mutex
);
1182 atomic_dec(&dev
->buf_alloc
);
1186 entry
->buflist
= drm_alloc(count
* sizeof(*entry
->buflist
),
1188 if (!entry
->buflist
) {
1189 mutex_unlock(&dev
->struct_mutex
);
1190 atomic_dec(&dev
->buf_alloc
);
1193 memset(entry
->buflist
, 0, count
* sizeof(*entry
->buflist
));
1195 entry
->buf_size
= size
;
1196 entry
->page_order
= page_order
;
1200 while (entry
->buf_count
< count
) {
1201 buf
= &entry
->buflist
[entry
->buf_count
];
1202 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1203 buf
->total
= alignment
;
1207 buf
->offset
= (dma
->byte_count
+ offset
);
1208 buf
->bus_address
= agp_offset
+ offset
;
1209 buf
->address
= (void *)(agp_offset
+ offset
);
1213 init_waitqueue_head(&buf
->dma_wait
);
1216 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1217 buf
->dev_private
= drm_alloc(buf
->dev_priv_size
, DRM_MEM_BUFS
);
1218 if (!buf
->dev_private
) {
1219 /* Set count correctly so we free the proper amount. */
1220 entry
->buf_count
= count
;
1221 drm_cleanup_buf_error(dev
, entry
);
1222 mutex_unlock(&dev
->struct_mutex
);
1223 atomic_dec(&dev
->buf_alloc
);
1226 memset(buf
->dev_private
, 0, buf
->dev_priv_size
);
1228 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1230 offset
+= alignment
;
1232 byte_count
+= PAGE_SIZE
<< page_order
;
1235 DRM_DEBUG("byte_count: %d\n", byte_count
);
1237 temp_buflist
= drm_realloc(dma
->buflist
,
1238 dma
->buf_count
* sizeof(*dma
->buflist
),
1239 (dma
->buf_count
+ entry
->buf_count
)
1240 * sizeof(*dma
->buflist
), DRM_MEM_BUFS
);
1241 if (!temp_buflist
) {
1242 /* Free the entry because it isn't valid */
1243 drm_cleanup_buf_error(dev
, entry
);
1244 mutex_unlock(&dev
->struct_mutex
);
1245 atomic_dec(&dev
->buf_alloc
);
1248 dma
->buflist
= temp_buflist
;
1250 for (i
= 0; i
< entry
->buf_count
; i
++) {
1251 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1254 dma
->buf_count
+= entry
->buf_count
;
1255 dma
->seg_count
+= entry
->seg_count
;
1256 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1257 dma
->byte_count
+= byte_count
;
1259 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1260 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1262 mutex_unlock(&dev
->struct_mutex
);
1264 request
->count
= entry
->buf_count
;
1265 request
->size
= size
;
1267 dma
->flags
= _DRM_DMA_USE_FB
;
1269 atomic_dec(&dev
->buf_alloc
);
1275 * Add buffers for DMA transfers (ioctl).
1277 * \param inode device inode.
1278 * \param filp file pointer.
1279 * \param cmd command.
1280 * \param arg pointer to a struct drm_buf_desc request.
1281 * \return zero on success or a negative number on failure.
1283 * According with the memory type specified in drm_buf_desc::flags and the
1284 * build options, it dispatches the call either to addbufs_agp(),
1285 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1286 * PCI memory respectively.
1288 int drm_addbufs(struct inode
*inode
, struct file
*filp
,
1289 unsigned int cmd
, unsigned long arg
)
1291 struct drm_buf_desc request
;
1292 struct drm_file
*priv
= filp
->private_data
;
1293 struct drm_device
*dev
= priv
->head
->dev
;
1296 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1299 if (copy_from_user(&request
, (struct drm_buf_desc __user
*) arg
,
1304 if (request
.flags
& _DRM_AGP_BUFFER
)
1305 ret
= drm_addbufs_agp(dev
, &request
);
1308 if (request
.flags
& _DRM_SG_BUFFER
)
1309 ret
= drm_addbufs_sg(dev
, &request
);
1310 else if (request
.flags
& _DRM_FB_BUFFER
)
1311 ret
= drm_addbufs_fb(dev
, &request
);
1313 ret
= drm_addbufs_pci(dev
, &request
);
1316 if (copy_to_user((void __user
*)arg
, &request
, sizeof(request
))) {
1324 * Get information about the buffer mappings.
1326 * This was originally mean for debugging purposes, or by a sophisticated
1327 * client library to determine how best to use the available buffers (e.g.,
1328 * large buffers can be used for image transfer).
1330 * \param inode device inode.
1331 * \param filp file pointer.
1332 * \param cmd command.
1333 * \param arg pointer to a drm_buf_info structure.
1334 * \return zero on success or a negative number on failure.
1336 * Increments drm_device::buf_use while holding the drm_device::count_lock
1337 * lock, preventing of allocating more buffers after this call. Information
1338 * about each requested buffer is then copied into user space.
1340 int drm_infobufs(struct inode
*inode
, struct file
*filp
,
1341 unsigned int cmd
, unsigned long arg
)
1343 struct drm_file
*priv
= filp
->private_data
;
1344 struct drm_device
*dev
= priv
->head
->dev
;
1345 struct drm_device_dma
*dma
= dev
->dma
;
1346 struct drm_buf_info request
;
1347 struct drm_buf_info __user
*argp
= (void __user
*)arg
;
1351 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1357 spin_lock(&dev
->count_lock
);
1358 if (atomic_read(&dev
->buf_alloc
)) {
1359 spin_unlock(&dev
->count_lock
);
1362 ++dev
->buf_use
; /* Can't allocate more after this call */
1363 spin_unlock(&dev
->count_lock
);
1365 if (copy_from_user(&request
, argp
, sizeof(request
)))
1368 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1369 if (dma
->bufs
[i
].buf_count
)
1373 DRM_DEBUG("count = %d\n", count
);
1375 if (request
.count
>= count
) {
1376 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1377 if (dma
->bufs
[i
].buf_count
) {
1378 struct drm_buf_desc __user
*to
=
1379 &request
.list
[count
];
1380 struct drm_buf_entry
*from
= &dma
->bufs
[i
];
1381 struct drm_freelist
*list
= &dma
->bufs
[i
].freelist
;
1382 if (copy_to_user(&to
->count
,
1384 sizeof(from
->buf_count
)) ||
1385 copy_to_user(&to
->size
,
1387 sizeof(from
->buf_size
)) ||
1388 copy_to_user(&to
->low_mark
,
1390 sizeof(list
->low_mark
)) ||
1391 copy_to_user(&to
->high_mark
,
1393 sizeof(list
->high_mark
)))
1396 DRM_DEBUG("%d %d %d %d %d\n",
1398 dma
->bufs
[i
].buf_count
,
1399 dma
->bufs
[i
].buf_size
,
1400 dma
->bufs
[i
].freelist
.low_mark
,
1401 dma
->bufs
[i
].freelist
.high_mark
);
1406 request
.count
= count
;
1408 if (copy_to_user(argp
, &request
, sizeof(request
)))
1415 * Specifies a low and high water mark for buffer allocation
1417 * \param inode device inode.
1418 * \param filp file pointer.
1419 * \param cmd command.
1420 * \param arg a pointer to a drm_buf_desc structure.
1421 * \return zero on success or a negative number on failure.
1423 * Verifies that the size order is bounded between the admissible orders and
1424 * updates the respective drm_device_dma::bufs entry low and high water mark.
1426 * \note This ioctl is deprecated and mostly never used.
1428 int drm_markbufs(struct inode
*inode
, struct file
*filp
,
1429 unsigned int cmd
, unsigned long arg
)
1431 struct drm_file
*priv
= filp
->private_data
;
1432 struct drm_device
*dev
= priv
->head
->dev
;
1433 struct drm_device_dma
*dma
= dev
->dma
;
1434 struct drm_buf_desc request
;
1436 struct drm_buf_entry
*entry
;
1438 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1444 if (copy_from_user(&request
,
1445 (struct drm_buf_desc __user
*) arg
, sizeof(request
)))
1448 DRM_DEBUG("%d, %d, %d\n",
1449 request
.size
, request
.low_mark
, request
.high_mark
);
1450 order
= drm_order(request
.size
);
1451 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1453 entry
= &dma
->bufs
[order
];
1455 if (request
.low_mark
< 0 || request
.low_mark
> entry
->buf_count
)
1457 if (request
.high_mark
< 0 || request
.high_mark
> entry
->buf_count
)
1460 entry
->freelist
.low_mark
= request
.low_mark
;
1461 entry
->freelist
.high_mark
= request
.high_mark
;
1467 * Unreserve the buffers in list, previously reserved using drmDMA.
1469 * \param inode device inode.
1470 * \param filp file pointer.
1471 * \param cmd command.
1472 * \param arg pointer to a drm_buf_free structure.
1473 * \return zero on success or a negative number on failure.
1475 * Calls free_buffer() for each used buffer.
1476 * This function is primarily used for debugging.
1478 int drm_freebufs(struct inode
*inode
, struct file
*filp
,
1479 unsigned int cmd
, unsigned long arg
)
1481 struct drm_file
*priv
= filp
->private_data
;
1482 struct drm_device
*dev
= priv
->head
->dev
;
1483 struct drm_device_dma
*dma
= dev
->dma
;
1484 struct drm_buf_free request
;
1487 struct drm_buf
*buf
;
1489 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1495 if (copy_from_user(&request
,
1496 (struct drm_buf_free __user
*) arg
, sizeof(request
)))
1499 DRM_DEBUG("%d\n", request
.count
);
1500 for (i
= 0; i
< request
.count
; i
++) {
1501 if (copy_from_user(&idx
, &request
.list
[i
], sizeof(idx
)))
1503 if (idx
< 0 || idx
>= dma
->buf_count
) {
1504 DRM_ERROR("Index %d (of %d max)\n",
1505 idx
, dma
->buf_count
- 1);
1508 buf
= dma
->buflist
[idx
];
1509 if (buf
->filp
!= filp
) {
1510 DRM_ERROR("Process %d freeing buffer not owned\n",
1514 drm_free_buffer(dev
, buf
);
1521 * Maps all of the DMA buffers into client-virtual space (ioctl).
1523 * \param inode device inode.
1524 * \param filp file pointer.
1525 * \param cmd command.
1526 * \param arg pointer to a drm_buf_map structure.
1527 * \return zero on success or a negative number on failure.
1529 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1530 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1531 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1534 int drm_mapbufs(struct inode
*inode
, struct file
*filp
,
1535 unsigned int cmd
, unsigned long arg
)
1537 struct drm_file
*priv
= filp
->private_data
;
1538 struct drm_device
*dev
= priv
->head
->dev
;
1539 struct drm_device_dma
*dma
= dev
->dma
;
1540 struct drm_buf_map __user
*argp
= (void __user
*)arg
;
1543 unsigned long virtual;
1544 unsigned long address
;
1545 struct drm_buf_map request
;
1548 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1554 spin_lock(&dev
->count_lock
);
1555 if (atomic_read(&dev
->buf_alloc
)) {
1556 spin_unlock(&dev
->count_lock
);
1559 dev
->buf_use
++; /* Can't allocate more after this call */
1560 spin_unlock(&dev
->count_lock
);
1562 if (copy_from_user(&request
, argp
, sizeof(request
)))
1565 if (request
.count
>= dma
->buf_count
) {
1566 if ((drm_core_has_AGP(dev
) && (dma
->flags
& _DRM_DMA_USE_AGP
))
1567 || (drm_core_check_feature(dev
, DRIVER_SG
)
1568 && (dma
->flags
& _DRM_DMA_USE_SG
))
1569 || (drm_core_check_feature(dev
, DRIVER_FB_DMA
)
1570 && (dma
->flags
& _DRM_DMA_USE_FB
))) {
1571 struct drm_map
*map
= dev
->agp_buffer_map
;
1572 unsigned long token
= dev
->agp_buffer_token
;
1579 down_write(¤t
->mm
->mmap_sem
);
1580 virtual = do_mmap(filp
, 0, map
->size
,
1581 PROT_READ
| PROT_WRITE
,
1583 up_write(¤t
->mm
->mmap_sem
);
1585 down_write(¤t
->mm
->mmap_sem
);
1586 virtual = do_mmap(filp
, 0, dma
->byte_count
,
1587 PROT_READ
| PROT_WRITE
,
1589 up_write(¤t
->mm
->mmap_sem
);
1591 if (virtual > -1024UL) {
1593 retcode
= (signed long)virtual;
1596 request
.virtual = (void __user
*)virtual;
1598 for (i
= 0; i
< dma
->buf_count
; i
++) {
1599 if (copy_to_user(&request
.list
[i
].idx
,
1600 &dma
->buflist
[i
]->idx
,
1601 sizeof(request
.list
[0].idx
))) {
1605 if (copy_to_user(&request
.list
[i
].total
,
1606 &dma
->buflist
[i
]->total
,
1607 sizeof(request
.list
[0].total
))) {
1611 if (copy_to_user(&request
.list
[i
].used
,
1612 &zero
, sizeof(zero
))) {
1616 address
= virtual + dma
->buflist
[i
]->offset
; /* *** */
1617 if (copy_to_user(&request
.list
[i
].address
,
1618 &address
, sizeof(address
))) {
1625 request
.count
= dma
->buf_count
;
1626 DRM_DEBUG("%d buffers, retcode = %d\n", request
.count
, retcode
);
1628 if (copy_to_user(argp
, &request
, sizeof(request
)))
1635 * Compute size order. Returns the exponent of the smaller power of two which
1636 * is greater or equal to given number.
1641 * \todo Can be made faster.
1643 int drm_order(unsigned long size
)
1648 for (order
= 0, tmp
= size
>> 1; tmp
; tmp
>>= 1, order
++) ;
1650 if (size
& (size
- 1))
1655 EXPORT_SYMBOL(drm_order
);