2 * Legacy: Generic DRM Buffer Management
4 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
9 * Author: Gareth Hughes <gareth@valinux.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice (including the next
19 * paragraph) shall be included in all copies or substantial portions of the
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
31 #include <linux/vmalloc.h>
32 #include <linux/slab.h>
33 #include <linux/log2.h>
34 #include <linux/export.h>
35 #include <asm/shmparam.h>
37 #include "drm_legacy.h"
39 #include <linux/nospec.h>
41 static struct drm_map_list
*drm_find_matching_map(struct drm_device
*dev
,
42 struct drm_local_map
*map
)
44 struct drm_map_list
*entry
;
45 list_for_each_entry(entry
, &dev
->maplist
, head
) {
47 * Because the kernel-userspace ABI is fixed at a 32-bit offset
48 * while PCI resources may live above that, we only compare the
49 * lower 32 bits of the map offset for maps of type
50 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
51 * It is assumed that if a driver have more than one resource
52 * of each type, the lower 32 bits are different.
55 map
->type
!= entry
->map
->type
||
56 entry
->master
!= dev
->master
)
60 if (map
->flags
!= _DRM_CONTAINS_LOCK
)
64 case _DRM_FRAME_BUFFER
:
65 if ((entry
->map
->offset
& 0xffffffff) ==
66 (map
->offset
& 0xffffffff))
68 default: /* Make gcc happy */
71 if (entry
->map
->offset
== map
->offset
)
78 static int drm_map_handle(struct drm_device
*dev
, struct drm_hash_item
*hash
,
79 unsigned long user_token
, int hashed_handle
, int shm
)
81 int use_hashed_handle
, shift
;
84 #if (BITS_PER_LONG == 64)
85 use_hashed_handle
= ((user_token
& 0xFFFFFFFF00000000UL
) || hashed_handle
);
86 #elif (BITS_PER_LONG == 32)
87 use_hashed_handle
= hashed_handle
;
89 #error Unsupported long size. Neither 64 nor 32 bits.
92 if (!use_hashed_handle
) {
94 hash
->key
= user_token
>> PAGE_SHIFT
;
95 ret
= drm_ht_insert_item(&dev
->map_hash
, hash
);
101 add
= DRM_MAP_HASH_OFFSET
>> PAGE_SHIFT
;
102 if (shm
&& (SHMLBA
> PAGE_SIZE
)) {
103 int bits
= ilog2(SHMLBA
>> PAGE_SHIFT
) + 1;
105 /* For shared memory, we have to preserve the SHMLBA
106 * bits of the eventual vma->vm_pgoff value during
107 * mmap(). Otherwise we run into cache aliasing problems
108 * on some platforms. On these platforms, the pgoff of
109 * a mmap() request is used to pick a suitable virtual
110 * address for the mmap() region such that it will not
111 * cause cache aliasing problems.
113 * Therefore, make sure the SHMLBA relevant bits of the
114 * hash value we use are equal to those in the original
115 * kernel virtual address.
118 add
|= ((user_token
>> PAGE_SHIFT
) & ((1UL << bits
) - 1UL));
121 return drm_ht_just_insert_please(&dev
->map_hash
, hash
,
122 user_token
, 32 - PAGE_SHIFT
- 3,
127 * Core function to create a range of memory available for mapping by a
130 * Adjusts the memory offset to its absolute value according to the mapping
131 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
132 * applicable and if supported by the kernel.
134 static int drm_addmap_core(struct drm_device
* dev
, resource_size_t offset
,
135 unsigned int size
, enum drm_map_type type
,
136 enum drm_map_flags flags
,
137 struct drm_map_list
** maplist
)
139 struct drm_local_map
*map
;
140 struct drm_map_list
*list
;
141 drm_dma_handle_t
*dmah
;
142 unsigned long user_token
;
145 map
= kmalloc(sizeof(*map
), GFP_KERNEL
);
149 map
->offset
= offset
;
154 /* Only allow shared memory to be removable since we only keep enough
155 * book keeping information about shared memory to allow for removal
156 * when processes fork.
158 if ((map
->flags
& _DRM_REMOVABLE
) && map
->type
!= _DRM_SHM
) {
162 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
163 (unsigned long long)map
->offset
, map
->size
, map
->type
);
165 /* page-align _DRM_SHM maps. They are allocated here so there is no security
166 * hole created by that and it works around various broken drivers that use
167 * a non-aligned quantity to map the SAREA. --BenH
169 if (map
->type
== _DRM_SHM
)
170 map
->size
= PAGE_ALIGN(map
->size
);
172 if ((map
->offset
& (~(resource_size_t
)PAGE_MASK
)) || (map
->size
& (~PAGE_MASK
))) {
181 case _DRM_FRAME_BUFFER
:
182 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
183 if (map
->offset
+ (map
->size
-1) < map
->offset
||
184 map
->offset
< virt_to_phys(high_memory
)) {
189 /* Some drivers preinitialize some maps, without the X Server
190 * needing to be aware of it. Therefore, we just return success
191 * when the server tries to create a duplicate map.
193 list
= drm_find_matching_map(dev
, map
);
195 if (list
->map
->size
!= map
->size
) {
196 DRM_DEBUG("Matching maps of type %d with "
197 "mismatched sizes, (%ld vs %ld)\n",
198 map
->type
, map
->size
,
200 list
->map
->size
= map
->size
;
208 if (map
->type
== _DRM_FRAME_BUFFER
||
209 (map
->flags
& _DRM_WRITE_COMBINING
)) {
211 arch_phys_wc_add(map
->offset
, map
->size
);
213 if (map
->type
== _DRM_REGISTERS
) {
214 if (map
->flags
& _DRM_WRITE_COMBINING
)
215 map
->handle
= ioremap_wc(map
->offset
,
218 map
->handle
= ioremap(map
->offset
, map
->size
);
227 list
= drm_find_matching_map(dev
, map
);
229 if(list
->map
->size
!= map
->size
) {
230 DRM_DEBUG("Matching maps of type %d with "
231 "mismatched sizes, (%ld vs %ld)\n",
232 map
->type
, map
->size
, list
->map
->size
);
233 list
->map
->size
= map
->size
;
240 map
->handle
= vmalloc_user(map
->size
);
241 DRM_DEBUG("%lu %d %p\n",
242 map
->size
, order_base_2(map
->size
), map
->handle
);
247 map
->offset
= (unsigned long)map
->handle
;
248 if (map
->flags
& _DRM_CONTAINS_LOCK
) {
249 /* Prevent a 2nd X Server from creating a 2nd lock */
250 if (dev
->master
->lock
.hw_lock
!= NULL
) {
255 dev
->sigdata
.lock
= dev
->master
->lock
.hw_lock
= map
->handle
; /* Pointer to lock */
259 struct drm_agp_mem
*entry
;
267 map
->offset
+= dev
->hose
->mem_space
->start
;
269 /* In some cases (i810 driver), user space may have already
270 * added the AGP base itself, because dev->agp->base previously
271 * only got set during AGP enable. So, only add the base
272 * address if the map's offset isn't already within the
275 if (map
->offset
< dev
->agp
->base
||
276 map
->offset
> dev
->agp
->base
+
277 dev
->agp
->agp_info
.aper_size
* 1024 * 1024 - 1) {
278 map
->offset
+= dev
->agp
->base
;
280 map
->mtrr
= dev
->agp
->agp_mtrr
; /* for getmap */
282 /* This assumes the DRM is in total control of AGP space.
283 * It's not always the case as AGP can be in the control
284 * of user space (i.e. i810 driver). So this loop will get
285 * skipped and we double check that dev->agp->memory is
286 * actually set as well as being invalid before EPERM'ing
288 list_for_each_entry(entry
, &dev
->agp
->memory
, head
) {
289 if ((map
->offset
>= entry
->bound
) &&
290 (map
->offset
+ map
->size
<= entry
->bound
+ entry
->pages
* PAGE_SIZE
)) {
295 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
299 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
300 (unsigned long long)map
->offset
, map
->size
);
304 case _DRM_SCATTER_GATHER
:
309 map
->offset
+= (unsigned long)dev
->sg
->virtual;
311 case _DRM_CONSISTENT
:
312 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
313 * As we're limiting the address to 2^32-1 (or less),
314 * casting it down to 32 bits is no problem, but we
315 * need to point to a 64bit variable first. */
316 dmah
= drm_pci_alloc(dev
, map
->size
, map
->size
);
321 map
->handle
= dmah
->vaddr
;
322 map
->offset
= (unsigned long)dmah
->busaddr
;
330 list
= kzalloc(sizeof(*list
), GFP_KERNEL
);
332 if (map
->type
== _DRM_REGISTERS
)
333 iounmap(map
->handle
);
339 mutex_lock(&dev
->struct_mutex
);
340 list_add(&list
->head
, &dev
->maplist
);
342 /* Assign a 32-bit handle */
343 /* We do it here so that dev->struct_mutex protects the increment */
344 user_token
= (map
->type
== _DRM_SHM
) ? (unsigned long)map
->handle
:
346 ret
= drm_map_handle(dev
, &list
->hash
, user_token
, 0,
347 (map
->type
== _DRM_SHM
));
349 if (map
->type
== _DRM_REGISTERS
)
350 iounmap(map
->handle
);
353 mutex_unlock(&dev
->struct_mutex
);
357 list
->user_token
= list
->hash
.key
<< PAGE_SHIFT
;
358 mutex_unlock(&dev
->struct_mutex
);
360 if (!(map
->flags
& _DRM_DRIVER
))
361 list
->master
= dev
->master
;
366 int drm_legacy_addmap(struct drm_device
* dev
, resource_size_t offset
,
367 unsigned int size
, enum drm_map_type type
,
368 enum drm_map_flags flags
, struct drm_local_map
**map_ptr
)
370 struct drm_map_list
*list
;
373 rc
= drm_addmap_core(dev
, offset
, size
, type
, flags
, &list
);
375 *map_ptr
= list
->map
;
378 EXPORT_SYMBOL(drm_legacy_addmap
);
381 * Ioctl to specify a range of memory that is available for mapping by a
384 * \param inode device inode.
385 * \param file_priv DRM file private.
386 * \param cmd command.
387 * \param arg pointer to a drm_map structure.
388 * \return zero on success or a negative value on error.
391 int drm_legacy_addmap_ioctl(struct drm_device
*dev
, void *data
,
392 struct drm_file
*file_priv
)
394 struct drm_map
*map
= data
;
395 struct drm_map_list
*maplist
;
398 if (!(capable(CAP_SYS_ADMIN
) || map
->type
== _DRM_AGP
|| map
->type
== _DRM_SHM
))
401 if (!drm_core_check_feature(dev
, DRIVER_KMS_LEGACY_CONTEXT
) &&
402 !drm_core_check_feature(dev
, DRIVER_LEGACY
))
405 err
= drm_addmap_core(dev
, map
->offset
, map
->size
, map
->type
,
406 map
->flags
, &maplist
);
411 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
412 map
->handle
= (void *)(unsigned long)maplist
->user_token
;
415 * It appears that there are no users of this value whatsoever --
416 * drmAddMap just discards it. Let's not encourage its use.
417 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
418 * it's not a real mtrr index anymore.)
426 * Get a mapping information.
428 * \param inode device inode.
429 * \param file_priv DRM file private.
430 * \param cmd command.
431 * \param arg user argument, pointing to a drm_map structure.
433 * \return zero on success or a negative number on failure.
435 * Searches for the mapping with the specified offset and copies its information
438 int drm_legacy_getmap_ioctl(struct drm_device
*dev
, void *data
,
439 struct drm_file
*file_priv
)
441 struct drm_map
*map
= data
;
442 struct drm_map_list
*r_list
= NULL
;
443 struct list_head
*list
;
447 if (!drm_core_check_feature(dev
, DRIVER_KMS_LEGACY_CONTEXT
) &&
448 !drm_core_check_feature(dev
, DRIVER_LEGACY
))
456 mutex_lock(&dev
->struct_mutex
);
457 list_for_each(list
, &dev
->maplist
) {
459 r_list
= list_entry(list
, struct drm_map_list
, head
);
464 if (!r_list
|| !r_list
->map
) {
465 mutex_unlock(&dev
->struct_mutex
);
469 map
->offset
= r_list
->map
->offset
;
470 map
->size
= r_list
->map
->size
;
471 map
->type
= r_list
->map
->type
;
472 map
->flags
= r_list
->map
->flags
;
473 map
->handle
= (void *)(unsigned long) r_list
->user_token
;
474 map
->mtrr
= arch_phys_wc_index(r_list
->map
->mtrr
);
476 mutex_unlock(&dev
->struct_mutex
);
482 * Remove a map private from list and deallocate resources if the mapping
485 * Searches the map on drm_device::maplist, removes it from the list, see if
486 * its being used, and free any associate resource (such as MTRR's) if it's not
489 * \sa drm_legacy_addmap
491 int drm_legacy_rmmap_locked(struct drm_device
*dev
, struct drm_local_map
*map
)
493 struct drm_map_list
*r_list
= NULL
, *list_t
;
494 drm_dma_handle_t dmah
;
496 struct drm_master
*master
;
498 /* Find the list entry for the map and remove it */
499 list_for_each_entry_safe(r_list
, list_t
, &dev
->maplist
, head
) {
500 if (r_list
->map
== map
) {
501 master
= r_list
->master
;
502 list_del(&r_list
->head
);
503 drm_ht_remove_key(&dev
->map_hash
,
504 r_list
->user_token
>> PAGE_SHIFT
);
516 iounmap(map
->handle
);
518 case _DRM_FRAME_BUFFER
:
519 arch_phys_wc_del(map
->mtrr
);
524 if (dev
->sigdata
.lock
== master
->lock
.hw_lock
)
525 dev
->sigdata
.lock
= NULL
;
526 master
->lock
.hw_lock
= NULL
; /* SHM removed */
527 master
->lock
.file_priv
= NULL
;
528 wake_up_interruptible_all(&master
->lock
.lock_queue
);
532 case _DRM_SCATTER_GATHER
:
534 case _DRM_CONSISTENT
:
535 dmah
.vaddr
= map
->handle
;
536 dmah
.busaddr
= map
->offset
;
537 dmah
.size
= map
->size
;
538 __drm_legacy_pci_free(dev
, &dmah
);
545 EXPORT_SYMBOL(drm_legacy_rmmap_locked
);
547 void drm_legacy_rmmap(struct drm_device
*dev
, struct drm_local_map
*map
)
549 if (!drm_core_check_feature(dev
, DRIVER_KMS_LEGACY_CONTEXT
) &&
550 !drm_core_check_feature(dev
, DRIVER_LEGACY
))
553 mutex_lock(&dev
->struct_mutex
);
554 drm_legacy_rmmap_locked(dev
, map
);
555 mutex_unlock(&dev
->struct_mutex
);
557 EXPORT_SYMBOL(drm_legacy_rmmap
);
559 void drm_legacy_master_rmmaps(struct drm_device
*dev
, struct drm_master
*master
)
561 struct drm_map_list
*r_list
, *list_temp
;
563 if (!drm_core_check_feature(dev
, DRIVER_LEGACY
))
566 mutex_lock(&dev
->struct_mutex
);
567 list_for_each_entry_safe(r_list
, list_temp
, &dev
->maplist
, head
) {
568 if (r_list
->master
== master
) {
569 drm_legacy_rmmap_locked(dev
, r_list
->map
);
573 mutex_unlock(&dev
->struct_mutex
);
576 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
577 * the last close of the device, and this is necessary for cleanup when things
578 * exit uncleanly. Therefore, having userland manually remove mappings seems
579 * like a pointless exercise since they're going away anyway.
581 * One use case might be after addmap is allowed for normal users for SHM and
582 * gets used by drivers that the server doesn't need to care about. This seems
585 * \param inode device inode.
586 * \param file_priv DRM file private.
587 * \param cmd command.
588 * \param arg pointer to a struct drm_map structure.
589 * \return zero on success or a negative value on error.
591 int drm_legacy_rmmap_ioctl(struct drm_device
*dev
, void *data
,
592 struct drm_file
*file_priv
)
594 struct drm_map
*request
= data
;
595 struct drm_local_map
*map
= NULL
;
596 struct drm_map_list
*r_list
;
599 if (!drm_core_check_feature(dev
, DRIVER_KMS_LEGACY_CONTEXT
) &&
600 !drm_core_check_feature(dev
, DRIVER_LEGACY
))
603 mutex_lock(&dev
->struct_mutex
);
604 list_for_each_entry(r_list
, &dev
->maplist
, head
) {
606 r_list
->user_token
== (unsigned long)request
->handle
&&
607 r_list
->map
->flags
& _DRM_REMOVABLE
) {
613 /* List has wrapped around to the head pointer, or its empty we didn't
616 if (list_empty(&dev
->maplist
) || !map
) {
617 mutex_unlock(&dev
->struct_mutex
);
621 /* Register and framebuffer maps are permanent */
622 if ((map
->type
== _DRM_REGISTERS
) || (map
->type
== _DRM_FRAME_BUFFER
)) {
623 mutex_unlock(&dev
->struct_mutex
);
627 ret
= drm_legacy_rmmap_locked(dev
, map
);
629 mutex_unlock(&dev
->struct_mutex
);
635 * Cleanup after an error on one of the addbufs() functions.
637 * \param dev DRM device.
638 * \param entry buffer entry where the error occurred.
640 * Frees any pages and buffers associated with the given entry.
642 static void drm_cleanup_buf_error(struct drm_device
* dev
,
643 struct drm_buf_entry
* entry
)
647 if (entry
->seg_count
) {
648 for (i
= 0; i
< entry
->seg_count
; i
++) {
649 if (entry
->seglist
[i
]) {
650 drm_pci_free(dev
, entry
->seglist
[i
]);
653 kfree(entry
->seglist
);
655 entry
->seg_count
= 0;
658 if (entry
->buf_count
) {
659 for (i
= 0; i
< entry
->buf_count
; i
++) {
660 kfree(entry
->buflist
[i
].dev_private
);
662 kfree(entry
->buflist
);
664 entry
->buf_count
= 0;
668 #if IS_ENABLED(CONFIG_AGP)
670 * Add AGP buffers for DMA transfers.
672 * \param dev struct drm_device to which the buffers are to be added.
673 * \param request pointer to a struct drm_buf_desc describing the request.
674 * \return zero on success or a negative number on failure.
676 * After some sanity checks creates a drm_buf structure for each buffer and
677 * reallocates the buffer list of the same size order to accommodate the new
680 int drm_legacy_addbufs_agp(struct drm_device
*dev
,
681 struct drm_buf_desc
*request
)
683 struct drm_device_dma
*dma
= dev
->dma
;
684 struct drm_buf_entry
*entry
;
685 struct drm_agp_mem
*agp_entry
;
687 unsigned long offset
;
688 unsigned long agp_offset
;
697 struct drm_buf
**temp_buflist
;
702 count
= request
->count
;
703 order
= order_base_2(request
->size
);
706 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
707 ? PAGE_ALIGN(size
) : size
;
708 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
709 total
= PAGE_SIZE
<< page_order
;
712 agp_offset
= dev
->agp
->base
+ request
->agp_start
;
714 DRM_DEBUG("count: %d\n", count
);
715 DRM_DEBUG("order: %d\n", order
);
716 DRM_DEBUG("size: %d\n", size
);
717 DRM_DEBUG("agp_offset: %lx\n", agp_offset
);
718 DRM_DEBUG("alignment: %d\n", alignment
);
719 DRM_DEBUG("page_order: %d\n", page_order
);
720 DRM_DEBUG("total: %d\n", total
);
722 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
725 /* Make sure buffers are located in AGP memory that we own */
727 list_for_each_entry(agp_entry
, &dev
->agp
->memory
, head
) {
728 if ((agp_offset
>= agp_entry
->bound
) &&
729 (agp_offset
+ total
* count
<= agp_entry
->bound
+ agp_entry
->pages
* PAGE_SIZE
)) {
734 if (!list_empty(&dev
->agp
->memory
) && !valid
) {
735 DRM_DEBUG("zone invalid\n");
738 spin_lock(&dev
->buf_lock
);
740 spin_unlock(&dev
->buf_lock
);
743 atomic_inc(&dev
->buf_alloc
);
744 spin_unlock(&dev
->buf_lock
);
746 mutex_lock(&dev
->struct_mutex
);
747 entry
= &dma
->bufs
[order
];
748 if (entry
->buf_count
) {
749 mutex_unlock(&dev
->struct_mutex
);
750 atomic_dec(&dev
->buf_alloc
);
751 return -ENOMEM
; /* May only call once for each order */
754 if (count
< 0 || count
> 4096) {
755 mutex_unlock(&dev
->struct_mutex
);
756 atomic_dec(&dev
->buf_alloc
);
760 entry
->buflist
= kcalloc(count
, sizeof(*entry
->buflist
), GFP_KERNEL
);
761 if (!entry
->buflist
) {
762 mutex_unlock(&dev
->struct_mutex
);
763 atomic_dec(&dev
->buf_alloc
);
767 entry
->buf_size
= size
;
768 entry
->page_order
= page_order
;
772 while (entry
->buf_count
< count
) {
773 buf
= &entry
->buflist
[entry
->buf_count
];
774 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
775 buf
->total
= alignment
;
779 buf
->offset
= (dma
->byte_count
+ offset
);
780 buf
->bus_address
= agp_offset
+ offset
;
781 buf
->address
= (void *)(agp_offset
+ offset
);
785 buf
->file_priv
= NULL
;
787 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
788 buf
->dev_private
= kzalloc(buf
->dev_priv_size
, GFP_KERNEL
);
789 if (!buf
->dev_private
) {
790 /* Set count correctly so we free the proper amount. */
791 entry
->buf_count
= count
;
792 drm_cleanup_buf_error(dev
, entry
);
793 mutex_unlock(&dev
->struct_mutex
);
794 atomic_dec(&dev
->buf_alloc
);
798 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
802 byte_count
+= PAGE_SIZE
<< page_order
;
805 DRM_DEBUG("byte_count: %d\n", byte_count
);
807 temp_buflist
= krealloc(dma
->buflist
,
808 (dma
->buf_count
+ entry
->buf_count
) *
809 sizeof(*dma
->buflist
), GFP_KERNEL
);
811 /* Free the entry because it isn't valid */
812 drm_cleanup_buf_error(dev
, entry
);
813 mutex_unlock(&dev
->struct_mutex
);
814 atomic_dec(&dev
->buf_alloc
);
817 dma
->buflist
= temp_buflist
;
819 for (i
= 0; i
< entry
->buf_count
; i
++) {
820 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
823 dma
->buf_count
+= entry
->buf_count
;
824 dma
->seg_count
+= entry
->seg_count
;
825 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
826 dma
->byte_count
+= byte_count
;
828 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
829 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
831 mutex_unlock(&dev
->struct_mutex
);
833 request
->count
= entry
->buf_count
;
834 request
->size
= size
;
836 dma
->flags
= _DRM_DMA_USE_AGP
;
838 atomic_dec(&dev
->buf_alloc
);
841 EXPORT_SYMBOL(drm_legacy_addbufs_agp
);
842 #endif /* CONFIG_AGP */
844 int drm_legacy_addbufs_pci(struct drm_device
*dev
,
845 struct drm_buf_desc
*request
)
847 struct drm_device_dma
*dma
= dev
->dma
;
853 struct drm_buf_entry
*entry
;
854 drm_dma_handle_t
*dmah
;
857 unsigned long offset
;
861 unsigned long *temp_pagelist
;
862 struct drm_buf
**temp_buflist
;
864 if (!drm_core_check_feature(dev
, DRIVER_PCI_DMA
))
870 if (!capable(CAP_SYS_ADMIN
))
873 count
= request
->count
;
874 order
= order_base_2(request
->size
);
877 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
878 request
->count
, request
->size
, size
, order
);
880 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
883 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
884 ? PAGE_ALIGN(size
) : size
;
885 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
886 total
= PAGE_SIZE
<< page_order
;
888 spin_lock(&dev
->buf_lock
);
890 spin_unlock(&dev
->buf_lock
);
893 atomic_inc(&dev
->buf_alloc
);
894 spin_unlock(&dev
->buf_lock
);
896 mutex_lock(&dev
->struct_mutex
);
897 entry
= &dma
->bufs
[order
];
898 if (entry
->buf_count
) {
899 mutex_unlock(&dev
->struct_mutex
);
900 atomic_dec(&dev
->buf_alloc
);
901 return -ENOMEM
; /* May only call once for each order */
904 if (count
< 0 || count
> 4096) {
905 mutex_unlock(&dev
->struct_mutex
);
906 atomic_dec(&dev
->buf_alloc
);
910 entry
->buflist
= kcalloc(count
, sizeof(*entry
->buflist
), GFP_KERNEL
);
911 if (!entry
->buflist
) {
912 mutex_unlock(&dev
->struct_mutex
);
913 atomic_dec(&dev
->buf_alloc
);
917 entry
->seglist
= kcalloc(count
, sizeof(*entry
->seglist
), GFP_KERNEL
);
918 if (!entry
->seglist
) {
919 kfree(entry
->buflist
);
920 mutex_unlock(&dev
->struct_mutex
);
921 atomic_dec(&dev
->buf_alloc
);
925 /* Keep the original pagelist until we know all the allocations
928 temp_pagelist
= kmalloc_array(dma
->page_count
+ (count
<< page_order
),
929 sizeof(*dma
->pagelist
),
931 if (!temp_pagelist
) {
932 kfree(entry
->buflist
);
933 kfree(entry
->seglist
);
934 mutex_unlock(&dev
->struct_mutex
);
935 atomic_dec(&dev
->buf_alloc
);
938 memcpy(temp_pagelist
,
939 dma
->pagelist
, dma
->page_count
* sizeof(*dma
->pagelist
));
940 DRM_DEBUG("pagelist: %d entries\n",
941 dma
->page_count
+ (count
<< page_order
));
943 entry
->buf_size
= size
;
944 entry
->page_order
= page_order
;
948 while (entry
->buf_count
< count
) {
950 dmah
= drm_pci_alloc(dev
, PAGE_SIZE
<< page_order
, 0x1000);
953 /* Set count correctly so we free the proper amount. */
954 entry
->buf_count
= count
;
955 entry
->seg_count
= count
;
956 drm_cleanup_buf_error(dev
, entry
);
957 kfree(temp_pagelist
);
958 mutex_unlock(&dev
->struct_mutex
);
959 atomic_dec(&dev
->buf_alloc
);
962 entry
->seglist
[entry
->seg_count
++] = dmah
;
963 for (i
= 0; i
< (1 << page_order
); i
++) {
964 DRM_DEBUG("page %d @ 0x%08lx\n",
965 dma
->page_count
+ page_count
,
966 (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
);
967 temp_pagelist
[dma
->page_count
+ page_count
++]
968 = (unsigned long)dmah
->vaddr
+ PAGE_SIZE
* i
;
971 offset
+ size
<= total
&& entry
->buf_count
< count
;
972 offset
+= alignment
, ++entry
->buf_count
) {
973 buf
= &entry
->buflist
[entry
->buf_count
];
974 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
975 buf
->total
= alignment
;
978 buf
->offset
= (dma
->byte_count
+ byte_count
+ offset
);
979 buf
->address
= (void *)(dmah
->vaddr
+ offset
);
980 buf
->bus_address
= dmah
->busaddr
+ offset
;
984 buf
->file_priv
= NULL
;
986 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
987 buf
->dev_private
= kzalloc(buf
->dev_priv_size
,
989 if (!buf
->dev_private
) {
990 /* Set count correctly so we free the proper amount. */
991 entry
->buf_count
= count
;
992 entry
->seg_count
= count
;
993 drm_cleanup_buf_error(dev
, entry
);
994 kfree(temp_pagelist
);
995 mutex_unlock(&dev
->struct_mutex
);
996 atomic_dec(&dev
->buf_alloc
);
1000 DRM_DEBUG("buffer %d @ %p\n",
1001 entry
->buf_count
, buf
->address
);
1003 byte_count
+= PAGE_SIZE
<< page_order
;
1006 temp_buflist
= krealloc(dma
->buflist
,
1007 (dma
->buf_count
+ entry
->buf_count
) *
1008 sizeof(*dma
->buflist
), GFP_KERNEL
);
1009 if (!temp_buflist
) {
1010 /* Free the entry because it isn't valid */
1011 drm_cleanup_buf_error(dev
, entry
);
1012 kfree(temp_pagelist
);
1013 mutex_unlock(&dev
->struct_mutex
);
1014 atomic_dec(&dev
->buf_alloc
);
1017 dma
->buflist
= temp_buflist
;
1019 for (i
= 0; i
< entry
->buf_count
; i
++) {
1020 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1023 /* No allocations failed, so now we can replace the original pagelist
1026 if (dma
->page_count
) {
1027 kfree(dma
->pagelist
);
1029 dma
->pagelist
= temp_pagelist
;
1031 dma
->buf_count
+= entry
->buf_count
;
1032 dma
->seg_count
+= entry
->seg_count
;
1033 dma
->page_count
+= entry
->seg_count
<< page_order
;
1034 dma
->byte_count
+= PAGE_SIZE
* (entry
->seg_count
<< page_order
);
1036 mutex_unlock(&dev
->struct_mutex
);
1038 request
->count
= entry
->buf_count
;
1039 request
->size
= size
;
1041 if (request
->flags
& _DRM_PCI_BUFFER_RO
)
1042 dma
->flags
= _DRM_DMA_USE_PCI_RO
;
1044 atomic_dec(&dev
->buf_alloc
);
1048 EXPORT_SYMBOL(drm_legacy_addbufs_pci
);
1050 static int drm_legacy_addbufs_sg(struct drm_device
*dev
,
1051 struct drm_buf_desc
*request
)
1053 struct drm_device_dma
*dma
= dev
->dma
;
1054 struct drm_buf_entry
*entry
;
1055 struct drm_buf
*buf
;
1056 unsigned long offset
;
1057 unsigned long agp_offset
;
1066 struct drm_buf
**temp_buflist
;
1068 if (!drm_core_check_feature(dev
, DRIVER_SG
))
1074 if (!capable(CAP_SYS_ADMIN
))
1077 count
= request
->count
;
1078 order
= order_base_2(request
->size
);
1081 alignment
= (request
->flags
& _DRM_PAGE_ALIGN
)
1082 ? PAGE_ALIGN(size
) : size
;
1083 page_order
= order
- PAGE_SHIFT
> 0 ? order
- PAGE_SHIFT
: 0;
1084 total
= PAGE_SIZE
<< page_order
;
1087 agp_offset
= request
->agp_start
;
1089 DRM_DEBUG("count: %d\n", count
);
1090 DRM_DEBUG("order: %d\n", order
);
1091 DRM_DEBUG("size: %d\n", size
);
1092 DRM_DEBUG("agp_offset: %lu\n", agp_offset
);
1093 DRM_DEBUG("alignment: %d\n", alignment
);
1094 DRM_DEBUG("page_order: %d\n", page_order
);
1095 DRM_DEBUG("total: %d\n", total
);
1097 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1100 spin_lock(&dev
->buf_lock
);
1102 spin_unlock(&dev
->buf_lock
);
1105 atomic_inc(&dev
->buf_alloc
);
1106 spin_unlock(&dev
->buf_lock
);
1108 mutex_lock(&dev
->struct_mutex
);
1109 entry
= &dma
->bufs
[order
];
1110 if (entry
->buf_count
) {
1111 mutex_unlock(&dev
->struct_mutex
);
1112 atomic_dec(&dev
->buf_alloc
);
1113 return -ENOMEM
; /* May only call once for each order */
1116 if (count
< 0 || count
> 4096) {
1117 mutex_unlock(&dev
->struct_mutex
);
1118 atomic_dec(&dev
->buf_alloc
);
1122 entry
->buflist
= kcalloc(count
, sizeof(*entry
->buflist
), GFP_KERNEL
);
1123 if (!entry
->buflist
) {
1124 mutex_unlock(&dev
->struct_mutex
);
1125 atomic_dec(&dev
->buf_alloc
);
1129 entry
->buf_size
= size
;
1130 entry
->page_order
= page_order
;
1134 while (entry
->buf_count
< count
) {
1135 buf
= &entry
->buflist
[entry
->buf_count
];
1136 buf
->idx
= dma
->buf_count
+ entry
->buf_count
;
1137 buf
->total
= alignment
;
1141 buf
->offset
= (dma
->byte_count
+ offset
);
1142 buf
->bus_address
= agp_offset
+ offset
;
1143 buf
->address
= (void *)(agp_offset
+ offset
1144 + (unsigned long)dev
->sg
->virtual);
1148 buf
->file_priv
= NULL
;
1150 buf
->dev_priv_size
= dev
->driver
->dev_priv_size
;
1151 buf
->dev_private
= kzalloc(buf
->dev_priv_size
, GFP_KERNEL
);
1152 if (!buf
->dev_private
) {
1153 /* Set count correctly so we free the proper amount. */
1154 entry
->buf_count
= count
;
1155 drm_cleanup_buf_error(dev
, entry
);
1156 mutex_unlock(&dev
->struct_mutex
);
1157 atomic_dec(&dev
->buf_alloc
);
1161 DRM_DEBUG("buffer %d @ %p\n", entry
->buf_count
, buf
->address
);
1163 offset
+= alignment
;
1165 byte_count
+= PAGE_SIZE
<< page_order
;
1168 DRM_DEBUG("byte_count: %d\n", byte_count
);
1170 temp_buflist
= krealloc(dma
->buflist
,
1171 (dma
->buf_count
+ entry
->buf_count
) *
1172 sizeof(*dma
->buflist
), GFP_KERNEL
);
1173 if (!temp_buflist
) {
1174 /* Free the entry because it isn't valid */
1175 drm_cleanup_buf_error(dev
, entry
);
1176 mutex_unlock(&dev
->struct_mutex
);
1177 atomic_dec(&dev
->buf_alloc
);
1180 dma
->buflist
= temp_buflist
;
1182 for (i
= 0; i
< entry
->buf_count
; i
++) {
1183 dma
->buflist
[i
+ dma
->buf_count
] = &entry
->buflist
[i
];
1186 dma
->buf_count
+= entry
->buf_count
;
1187 dma
->seg_count
+= entry
->seg_count
;
1188 dma
->page_count
+= byte_count
>> PAGE_SHIFT
;
1189 dma
->byte_count
+= byte_count
;
1191 DRM_DEBUG("dma->buf_count : %d\n", dma
->buf_count
);
1192 DRM_DEBUG("entry->buf_count : %d\n", entry
->buf_count
);
1194 mutex_unlock(&dev
->struct_mutex
);
1196 request
->count
= entry
->buf_count
;
1197 request
->size
= size
;
1199 dma
->flags
= _DRM_DMA_USE_SG
;
1201 atomic_dec(&dev
->buf_alloc
);
1206 * Add buffers for DMA transfers (ioctl).
1208 * \param inode device inode.
1209 * \param file_priv DRM file private.
1210 * \param cmd command.
1211 * \param arg pointer to a struct drm_buf_desc request.
1212 * \return zero on success or a negative number on failure.
1214 * According with the memory type specified in drm_buf_desc::flags and the
1215 * build options, it dispatches the call either to addbufs_agp(),
1216 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1217 * PCI memory respectively.
1219 int drm_legacy_addbufs(struct drm_device
*dev
, void *data
,
1220 struct drm_file
*file_priv
)
1222 struct drm_buf_desc
*request
= data
;
1225 if (!drm_core_check_feature(dev
, DRIVER_LEGACY
))
1228 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1231 #if IS_ENABLED(CONFIG_AGP)
1232 if (request
->flags
& _DRM_AGP_BUFFER
)
1233 ret
= drm_legacy_addbufs_agp(dev
, request
);
1236 if (request
->flags
& _DRM_SG_BUFFER
)
1237 ret
= drm_legacy_addbufs_sg(dev
, request
);
1238 else if (request
->flags
& _DRM_FB_BUFFER
)
1241 ret
= drm_legacy_addbufs_pci(dev
, request
);
1247 * Get information about the buffer mappings.
1249 * This was originally mean for debugging purposes, or by a sophisticated
1250 * client library to determine how best to use the available buffers (e.g.,
1251 * large buffers can be used for image transfer).
1253 * \param inode device inode.
1254 * \param file_priv DRM file private.
1255 * \param cmd command.
1256 * \param arg pointer to a drm_buf_info structure.
1257 * \return zero on success or a negative number on failure.
1259 * Increments drm_device::buf_use while holding the drm_device::buf_lock
1260 * lock, preventing of allocating more buffers after this call. Information
1261 * about each requested buffer is then copied into user space.
1263 int drm_legacy_infobufs(struct drm_device
*dev
, void *data
,
1264 struct drm_file
*file_priv
)
1266 struct drm_device_dma
*dma
= dev
->dma
;
1267 struct drm_buf_info
*request
= data
;
1271 if (!drm_core_check_feature(dev
, DRIVER_LEGACY
))
1274 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1280 spin_lock(&dev
->buf_lock
);
1281 if (atomic_read(&dev
->buf_alloc
)) {
1282 spin_unlock(&dev
->buf_lock
);
1285 ++dev
->buf_use
; /* Can't allocate more after this call */
1286 spin_unlock(&dev
->buf_lock
);
1288 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1289 if (dma
->bufs
[i
].buf_count
)
1293 DRM_DEBUG("count = %d\n", count
);
1295 if (request
->count
>= count
) {
1296 for (i
= 0, count
= 0; i
< DRM_MAX_ORDER
+ 1; i
++) {
1297 if (dma
->bufs
[i
].buf_count
) {
1298 struct drm_buf_desc __user
*to
=
1299 &request
->list
[count
];
1300 struct drm_buf_entry
*from
= &dma
->bufs
[i
];
1301 if (copy_to_user(&to
->count
,
1303 sizeof(from
->buf_count
)) ||
1304 copy_to_user(&to
->size
,
1306 sizeof(from
->buf_size
)) ||
1307 copy_to_user(&to
->low_mark
,
1309 sizeof(from
->low_mark
)) ||
1310 copy_to_user(&to
->high_mark
,
1312 sizeof(from
->high_mark
)))
1315 DRM_DEBUG("%d %d %d %d %d\n",
1317 dma
->bufs
[i
].buf_count
,
1318 dma
->bufs
[i
].buf_size
,
1319 dma
->bufs
[i
].low_mark
,
1320 dma
->bufs
[i
].high_mark
);
1325 request
->count
= count
;
1331 * Specifies a low and high water mark for buffer allocation
1333 * \param inode device inode.
1334 * \param file_priv DRM file private.
1335 * \param cmd command.
1336 * \param arg a pointer to a drm_buf_desc structure.
1337 * \return zero on success or a negative number on failure.
1339 * Verifies that the size order is bounded between the admissible orders and
1340 * updates the respective drm_device_dma::bufs entry low and high water mark.
1342 * \note This ioctl is deprecated and mostly never used.
1344 int drm_legacy_markbufs(struct drm_device
*dev
, void *data
,
1345 struct drm_file
*file_priv
)
1347 struct drm_device_dma
*dma
= dev
->dma
;
1348 struct drm_buf_desc
*request
= data
;
1350 struct drm_buf_entry
*entry
;
1352 if (!drm_core_check_feature(dev
, DRIVER_LEGACY
))
1355 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1361 DRM_DEBUG("%d, %d, %d\n",
1362 request
->size
, request
->low_mark
, request
->high_mark
);
1363 order
= order_base_2(request
->size
);
1364 if (order
< DRM_MIN_ORDER
|| order
> DRM_MAX_ORDER
)
1366 entry
= &dma
->bufs
[order
];
1368 if (request
->low_mark
< 0 || request
->low_mark
> entry
->buf_count
)
1370 if (request
->high_mark
< 0 || request
->high_mark
> entry
->buf_count
)
1373 entry
->low_mark
= request
->low_mark
;
1374 entry
->high_mark
= request
->high_mark
;
1380 * Unreserve the buffers in list, previously reserved using drmDMA.
1382 * \param inode device inode.
1383 * \param file_priv DRM file private.
1384 * \param cmd command.
1385 * \param arg pointer to a drm_buf_free structure.
1386 * \return zero on success or a negative number on failure.
1388 * Calls free_buffer() for each used buffer.
1389 * This function is primarily used for debugging.
1391 int drm_legacy_freebufs(struct drm_device
*dev
, void *data
,
1392 struct drm_file
*file_priv
)
1394 struct drm_device_dma
*dma
= dev
->dma
;
1395 struct drm_buf_free
*request
= data
;
1398 struct drm_buf
*buf
;
1400 if (!drm_core_check_feature(dev
, DRIVER_LEGACY
))
1403 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1409 DRM_DEBUG("%d\n", request
->count
);
1410 for (i
= 0; i
< request
->count
; i
++) {
1411 if (copy_from_user(&idx
, &request
->list
[i
], sizeof(idx
)))
1413 if (idx
< 0 || idx
>= dma
->buf_count
) {
1414 DRM_ERROR("Index %d (of %d max)\n",
1415 idx
, dma
->buf_count
- 1);
1418 idx
= array_index_nospec(idx
, dma
->buf_count
);
1419 buf
= dma
->buflist
[idx
];
1420 if (buf
->file_priv
!= file_priv
) {
1421 DRM_ERROR("Process %d freeing buffer not owned\n",
1422 task_pid_nr(current
));
1425 drm_legacy_free_buffer(dev
, buf
);
1432 * Maps all of the DMA buffers into client-virtual space (ioctl).
1434 * \param inode device inode.
1435 * \param file_priv DRM file private.
1436 * \param cmd command.
1437 * \param arg pointer to a drm_buf_map structure.
1438 * \return zero on success or a negative number on failure.
1440 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1441 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1442 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1445 int drm_legacy_mapbufs(struct drm_device
*dev
, void *data
,
1446 struct drm_file
*file_priv
)
1448 struct drm_device_dma
*dma
= dev
->dma
;
1451 unsigned long virtual;
1452 unsigned long address
;
1453 struct drm_buf_map
*request
= data
;
1456 if (!drm_core_check_feature(dev
, DRIVER_LEGACY
))
1459 if (!drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
1465 spin_lock(&dev
->buf_lock
);
1466 if (atomic_read(&dev
->buf_alloc
)) {
1467 spin_unlock(&dev
->buf_lock
);
1470 dev
->buf_use
++; /* Can't allocate more after this call */
1471 spin_unlock(&dev
->buf_lock
);
1473 if (request
->count
>= dma
->buf_count
) {
1474 if ((dev
->agp
&& (dma
->flags
& _DRM_DMA_USE_AGP
))
1475 || (drm_core_check_feature(dev
, DRIVER_SG
)
1476 && (dma
->flags
& _DRM_DMA_USE_SG
))) {
1477 struct drm_local_map
*map
= dev
->agp_buffer_map
;
1478 unsigned long token
= dev
->agp_buffer_token
;
1484 virtual = vm_mmap(file_priv
->filp
, 0, map
->size
,
1485 PROT_READ
| PROT_WRITE
,
1489 virtual = vm_mmap(file_priv
->filp
, 0, dma
->byte_count
,
1490 PROT_READ
| PROT_WRITE
,
1493 if (virtual > -1024UL) {
1495 retcode
= (signed long)virtual;
1498 request
->virtual = (void __user
*)virtual;
1500 for (i
= 0; i
< dma
->buf_count
; i
++) {
1501 if (copy_to_user(&request
->list
[i
].idx
,
1502 &dma
->buflist
[i
]->idx
,
1503 sizeof(request
->list
[0].idx
))) {
1507 if (copy_to_user(&request
->list
[i
].total
,
1508 &dma
->buflist
[i
]->total
,
1509 sizeof(request
->list
[0].total
))) {
1513 if (copy_to_user(&request
->list
[i
].used
,
1514 &zero
, sizeof(zero
))) {
1518 address
= virtual + dma
->buflist
[i
]->offset
; /* *** */
1519 if (copy_to_user(&request
->list
[i
].address
,
1520 &address
, sizeof(address
))) {
1527 request
->count
= dma
->buf_count
;
1528 DRM_DEBUG("%d buffers, retcode = %d\n", request
->count
, retcode
);
1533 int drm_legacy_dma_ioctl(struct drm_device
*dev
, void *data
,
1534 struct drm_file
*file_priv
)
1536 if (!drm_core_check_feature(dev
, DRIVER_LEGACY
))
1539 if (dev
->driver
->dma_ioctl
)
1540 return dev
->driver
->dma_ioctl(dev
, data
, file_priv
);
1545 struct drm_local_map
*drm_legacy_getsarea(struct drm_device
*dev
)
1547 struct drm_map_list
*entry
;
1549 list_for_each_entry(entry
, &dev
->maplist
, head
) {
1550 if (entry
->map
&& entry
->map
->type
== _DRM_SHM
&&
1551 (entry
->map
->flags
& _DRM_CONTAINS_LOCK
)) {
1557 EXPORT_SYMBOL(drm_legacy_getsarea
);