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);
380 struct drm_local_map *drm_legacy_findmap(struct drm_device *dev,
383 struct drm_map_list *_entry;
384 list_for_each_entry(_entry, &dev->maplist, head)
385 if (_entry->user_token == token)
389 EXPORT_SYMBOL(drm_legacy_findmap);
392 * Ioctl to specify a range of memory that is available for mapping by a
395 * \param inode device inode.
396 * \param file_priv DRM file private.
397 * \param cmd command.
398 * \param arg pointer to a drm_map structure.
399 * \return zero on success or a negative value on error.
402 int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
403 struct drm_file *file_priv)
405 struct drm_map *map = data;
406 struct drm_map_list *maplist;
409 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
412 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
413 !drm_core_check_feature(dev, DRIVER_LEGACY))
416 err = drm_addmap_core(dev, map->offset, map->size, map->type,
417 map->flags, &maplist);
422 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
423 map->handle = (void *)(unsigned long)maplist->user_token;
426 * It appears that there are no users of this value whatsoever --
427 * drmAddMap just discards it. Let's not encourage its use.
428 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
429 * it's not a real mtrr index anymore.)
437 * Get a mapping information.
439 * \param inode device inode.
440 * \param file_priv DRM file private.
441 * \param cmd command.
442 * \param arg user argument, pointing to a drm_map structure.
444 * \return zero on success or a negative number on failure.
446 * Searches for the mapping with the specified offset and copies its information
449 int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
450 struct drm_file *file_priv)
452 struct drm_map *map = data;
453 struct drm_map_list *r_list = NULL;
454 struct list_head *list;
458 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
459 !drm_core_check_feature(dev, DRIVER_LEGACY))
467 mutex_lock(&dev->struct_mutex);
468 list_for_each(list, &dev->maplist) {
470 r_list = list_entry(list, struct drm_map_list, head);
475 if (!r_list || !r_list->map) {
476 mutex_unlock(&dev->struct_mutex);
480 map->offset = r_list->map->offset;
481 map->size = r_list->map->size;
482 map->type = r_list->map->type;
483 map->flags = r_list->map->flags;
484 map->handle = (void *)(unsigned long) r_list->user_token;
485 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
487 mutex_unlock(&dev->struct_mutex);
493 * Remove a map private from list and deallocate resources if the mapping
496 * Searches the map on drm_device::maplist, removes it from the list, see if
497 * it's being used, and free any associated resource (such as MTRR's) if it's not
500 * \sa drm_legacy_addmap
502 int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
504 struct drm_map_list *r_list = NULL, *list_t;
505 drm_dma_handle_t dmah;
507 struct drm_master *master;
509 /* Find the list entry for the map and remove it */
510 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
511 if (r_list->map == map) {
512 master = r_list->master;
513 list_del(&r_list->head);
514 drm_ht_remove_key(&dev->map_hash,
515 r_list->user_token >> PAGE_SHIFT);
527 iounmap(map->handle);
529 case _DRM_FRAME_BUFFER:
530 arch_phys_wc_del(map->mtrr);
535 if (dev->sigdata.lock == master->lock.hw_lock)
536 dev->sigdata.lock = NULL;
537 master->lock.hw_lock = NULL; /* SHM removed */
538 master->lock.file_priv = NULL;
539 wake_up_interruptible_all(&master->lock.lock_queue);
543 case _DRM_SCATTER_GATHER:
545 case _DRM_CONSISTENT:
546 dmah.vaddr = map->handle;
547 dmah.busaddr = map->offset;
548 dmah.size = map->size;
549 __drm_legacy_pci_free(dev, &dmah);
556 EXPORT_SYMBOL(drm_legacy_rmmap_locked);
558 void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
560 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
561 !drm_core_check_feature(dev, DRIVER_LEGACY))
564 mutex_lock(&dev->struct_mutex);
565 drm_legacy_rmmap_locked(dev, map);
566 mutex_unlock(&dev->struct_mutex);
568 EXPORT_SYMBOL(drm_legacy_rmmap);
570 void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
572 struct drm_map_list *r_list, *list_temp;
574 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
577 mutex_lock(&dev->struct_mutex);
578 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
579 if (r_list->master == master) {
580 drm_legacy_rmmap_locked(dev, r_list->map);
584 mutex_unlock(&dev->struct_mutex);
587 void drm_legacy_rmmaps(struct drm_device *dev)
589 struct drm_map_list *r_list, *list_temp;
591 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
592 drm_legacy_rmmap(dev, r_list->map);
595 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
596 * the last close of the device, and this is necessary for cleanup when things
597 * exit uncleanly. Therefore, having userland manually remove mappings seems
598 * like a pointless exercise since they're going away anyway.
600 * One use case might be after addmap is allowed for normal users for SHM and
601 * gets used by drivers that the server doesn't need to care about. This seems
604 * \param inode device inode.
605 * \param file_priv DRM file private.
606 * \param cmd command.
607 * \param arg pointer to a struct drm_map structure.
608 * \return zero on success or a negative value on error.
610 int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
611 struct drm_file *file_priv)
613 struct drm_map *request = data;
614 struct drm_local_map *map = NULL;
615 struct drm_map_list *r_list;
618 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
619 !drm_core_check_feature(dev, DRIVER_LEGACY))
622 mutex_lock(&dev->struct_mutex);
623 list_for_each_entry(r_list, &dev->maplist, head) {
625 r_list->user_token == (unsigned long)request->handle &&
626 r_list->map->flags & _DRM_REMOVABLE) {
632 /* List has wrapped around to the head pointer, or it's empty we didn't
635 if (list_empty(&dev->maplist) || !map) {
636 mutex_unlock(&dev->struct_mutex);
640 /* Register and framebuffer maps are permanent */
641 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
642 mutex_unlock(&dev->struct_mutex);
646 ret = drm_legacy_rmmap_locked(dev, map);
648 mutex_unlock(&dev->struct_mutex);
654 * Cleanup after an error on one of the addbufs() functions.
656 * \param dev DRM device.
657 * \param entry buffer entry where the error occurred.
659 * Frees any pages and buffers associated with the given entry.
661 static void drm_cleanup_buf_error(struct drm_device *dev,
662 struct drm_buf_entry *entry)
666 if (entry->seg_count) {
667 for (i = 0; i < entry->seg_count; i++) {
668 if (entry->seglist[i]) {
669 drm_pci_free(dev, entry->seglist[i]);
672 kfree(entry->seglist);
674 entry->seg_count = 0;
677 if (entry->buf_count) {
678 for (i = 0; i < entry->buf_count; i++) {
679 kfree(entry->buflist[i].dev_private);
681 kfree(entry->buflist);
683 entry->buf_count = 0;
687 #if IS_ENABLED(CONFIG_AGP)
689 * Add AGP buffers for DMA transfers.
691 * \param dev struct drm_device to which the buffers are to be added.
692 * \param request pointer to a struct drm_buf_desc describing the request.
693 * \return zero on success or a negative number on failure.
695 * After some sanity checks creates a drm_buf structure for each buffer and
696 * reallocates the buffer list of the same size order to accommodate the new
699 int drm_legacy_addbufs_agp(struct drm_device *dev,
700 struct drm_buf_desc *request)
702 struct drm_device_dma *dma = dev->dma;
703 struct drm_buf_entry *entry;
704 struct drm_agp_mem *agp_entry;
706 unsigned long offset;
707 unsigned long agp_offset;
716 struct drm_buf **temp_buflist;
721 count = request->count;
722 order = order_base_2(request->size);
725 alignment = (request->flags & _DRM_PAGE_ALIGN)
726 ? PAGE_ALIGN(size) : size;
727 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
728 total = PAGE_SIZE << page_order;
731 agp_offset = dev->agp->base + request->agp_start;
733 DRM_DEBUG("count: %d\n", count);
734 DRM_DEBUG("order: %d\n", order);
735 DRM_DEBUG("size: %d\n", size);
736 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
737 DRM_DEBUG("alignment: %d\n", alignment);
738 DRM_DEBUG("page_order: %d\n", page_order);
739 DRM_DEBUG("total: %d\n", total);
741 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
744 /* Make sure buffers are located in AGP memory that we own */
746 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
747 if ((agp_offset >= agp_entry->bound) &&
748 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
753 if (!list_empty(&dev->agp->memory) && !valid) {
754 DRM_DEBUG("zone invalid\n");
757 spin_lock(&dev->buf_lock);
759 spin_unlock(&dev->buf_lock);
762 atomic_inc(&dev->buf_alloc);
763 spin_unlock(&dev->buf_lock);
765 mutex_lock(&dev->struct_mutex);
766 entry = &dma->bufs[order];
767 if (entry->buf_count) {
768 mutex_unlock(&dev->struct_mutex);
769 atomic_dec(&dev->buf_alloc);
770 return -ENOMEM; /* May only call once for each order */
773 if (count < 0 || count > 4096) {
774 mutex_unlock(&dev->struct_mutex);
775 atomic_dec(&dev->buf_alloc);
779 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
780 if (!entry->buflist) {
781 mutex_unlock(&dev->struct_mutex);
782 atomic_dec(&dev->buf_alloc);
786 entry->buf_size = size;
787 entry->page_order = page_order;
791 while (entry->buf_count < count) {
792 buf = &entry->buflist[entry->buf_count];
793 buf->idx = dma->buf_count + entry->buf_count;
794 buf->total = alignment;
798 buf->offset = (dma->byte_count + offset);
799 buf->bus_address = agp_offset + offset;
800 buf->address = (void *)(agp_offset + offset);
804 buf->file_priv = NULL;
806 buf->dev_priv_size = dev->driver->dev_priv_size;
807 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
808 if (!buf->dev_private) {
809 /* Set count correctly so we free the proper amount. */
810 entry->buf_count = count;
811 drm_cleanup_buf_error(dev, entry);
812 mutex_unlock(&dev->struct_mutex);
813 atomic_dec(&dev->buf_alloc);
817 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
821 byte_count += PAGE_SIZE << page_order;
824 DRM_DEBUG("byte_count: %d\n", byte_count);
826 temp_buflist = krealloc(dma->buflist,
827 (dma->buf_count + entry->buf_count) *
828 sizeof(*dma->buflist), GFP_KERNEL);
830 /* Free the entry because it isn't valid */
831 drm_cleanup_buf_error(dev, entry);
832 mutex_unlock(&dev->struct_mutex);
833 atomic_dec(&dev->buf_alloc);
836 dma->buflist = temp_buflist;
838 for (i = 0; i < entry->buf_count; i++) {
839 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
842 dma->buf_count += entry->buf_count;
843 dma->seg_count += entry->seg_count;
844 dma->page_count += byte_count >> PAGE_SHIFT;
845 dma->byte_count += byte_count;
847 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
848 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
850 mutex_unlock(&dev->struct_mutex);
852 request->count = entry->buf_count;
853 request->size = size;
855 dma->flags = _DRM_DMA_USE_AGP;
857 atomic_dec(&dev->buf_alloc);
860 EXPORT_SYMBOL(drm_legacy_addbufs_agp);
861 #endif /* CONFIG_AGP */
863 int drm_legacy_addbufs_pci(struct drm_device *dev,
864 struct drm_buf_desc *request)
866 struct drm_device_dma *dma = dev->dma;
872 struct drm_buf_entry *entry;
873 drm_dma_handle_t *dmah;
876 unsigned long offset;
880 unsigned long *temp_pagelist;
881 struct drm_buf **temp_buflist;
883 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
889 if (!capable(CAP_SYS_ADMIN))
892 count = request->count;
893 order = order_base_2(request->size);
896 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
897 request->count, request->size, size, order);
899 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
902 alignment = (request->flags & _DRM_PAGE_ALIGN)
903 ? PAGE_ALIGN(size) : size;
904 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
905 total = PAGE_SIZE << page_order;
907 spin_lock(&dev->buf_lock);
909 spin_unlock(&dev->buf_lock);
912 atomic_inc(&dev->buf_alloc);
913 spin_unlock(&dev->buf_lock);
915 mutex_lock(&dev->struct_mutex);
916 entry = &dma->bufs[order];
917 if (entry->buf_count) {
918 mutex_unlock(&dev->struct_mutex);
919 atomic_dec(&dev->buf_alloc);
920 return -ENOMEM; /* May only call once for each order */
923 if (count < 0 || count > 4096) {
924 mutex_unlock(&dev->struct_mutex);
925 atomic_dec(&dev->buf_alloc);
929 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
930 if (!entry->buflist) {
931 mutex_unlock(&dev->struct_mutex);
932 atomic_dec(&dev->buf_alloc);
936 entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
937 if (!entry->seglist) {
938 kfree(entry->buflist);
939 mutex_unlock(&dev->struct_mutex);
940 atomic_dec(&dev->buf_alloc);
944 /* Keep the original pagelist until we know all the allocations
947 temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
948 sizeof(*dma->pagelist),
950 if (!temp_pagelist) {
951 kfree(entry->buflist);
952 kfree(entry->seglist);
953 mutex_unlock(&dev->struct_mutex);
954 atomic_dec(&dev->buf_alloc);
957 memcpy(temp_pagelist,
958 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
959 DRM_DEBUG("pagelist: %d entries\n",
960 dma->page_count + (count << page_order));
962 entry->buf_size = size;
963 entry->page_order = page_order;
967 while (entry->buf_count < count) {
969 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
972 /* Set count correctly so we free the proper amount. */
973 entry->buf_count = count;
974 entry->seg_count = count;
975 drm_cleanup_buf_error(dev, entry);
976 kfree(temp_pagelist);
977 mutex_unlock(&dev->struct_mutex);
978 atomic_dec(&dev->buf_alloc);
981 entry->seglist[entry->seg_count++] = dmah;
982 for (i = 0; i < (1 << page_order); i++) {
983 DRM_DEBUG("page %d @ 0x%08lx\n",
984 dma->page_count + page_count,
985 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
986 temp_pagelist[dma->page_count + page_count++]
987 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
990 offset + size <= total && entry->buf_count < count;
991 offset += alignment, ++entry->buf_count) {
992 buf = &entry->buflist[entry->buf_count];
993 buf->idx = dma->buf_count + entry->buf_count;
994 buf->total = alignment;
997 buf->offset = (dma->byte_count + byte_count + offset);
998 buf->address = (void *)(dmah->vaddr + offset);
999 buf->bus_address = dmah->busaddr + offset;
1003 buf->file_priv = NULL;
1005 buf->dev_priv_size = dev->driver->dev_priv_size;
1006 buf->dev_private = kzalloc(buf->dev_priv_size,
1008 if (!buf->dev_private) {
1009 /* Set count correctly so we free the proper amount. */
1010 entry->buf_count = count;
1011 entry->seg_count = count;
1012 drm_cleanup_buf_error(dev, entry);
1013 kfree(temp_pagelist);
1014 mutex_unlock(&dev->struct_mutex);
1015 atomic_dec(&dev->buf_alloc);
1019 DRM_DEBUG("buffer %d @ %p\n",
1020 entry->buf_count, buf->address);
1022 byte_count += PAGE_SIZE << page_order;
1025 temp_buflist = krealloc(dma->buflist,
1026 (dma->buf_count + entry->buf_count) *
1027 sizeof(*dma->buflist), GFP_KERNEL);
1028 if (!temp_buflist) {
1029 /* Free the entry because it isn't valid */
1030 drm_cleanup_buf_error(dev, entry);
1031 kfree(temp_pagelist);
1032 mutex_unlock(&dev->struct_mutex);
1033 atomic_dec(&dev->buf_alloc);
1036 dma->buflist = temp_buflist;
1038 for (i = 0; i < entry->buf_count; i++) {
1039 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1042 /* No allocations failed, so now we can replace the original pagelist
1045 if (dma->page_count) {
1046 kfree(dma->pagelist);
1048 dma->pagelist = temp_pagelist;
1050 dma->buf_count += entry->buf_count;
1051 dma->seg_count += entry->seg_count;
1052 dma->page_count += entry->seg_count << page_order;
1053 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1055 mutex_unlock(&dev->struct_mutex);
1057 request->count = entry->buf_count;
1058 request->size = size;
1060 if (request->flags & _DRM_PCI_BUFFER_RO)
1061 dma->flags = _DRM_DMA_USE_PCI_RO;
1063 atomic_dec(&dev->buf_alloc);
1067 EXPORT_SYMBOL(drm_legacy_addbufs_pci);
1069 static int drm_legacy_addbufs_sg(struct drm_device *dev,
1070 struct drm_buf_desc *request)
1072 struct drm_device_dma *dma = dev->dma;
1073 struct drm_buf_entry *entry;
1074 struct drm_buf *buf;
1075 unsigned long offset;
1076 unsigned long agp_offset;
1085 struct drm_buf **temp_buflist;
1087 if (!drm_core_check_feature(dev, DRIVER_SG))
1093 if (!capable(CAP_SYS_ADMIN))
1096 count = request->count;
1097 order = order_base_2(request->size);
1100 alignment = (request->flags & _DRM_PAGE_ALIGN)
1101 ? PAGE_ALIGN(size) : size;
1102 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1103 total = PAGE_SIZE << page_order;
1106 agp_offset = request->agp_start;
1108 DRM_DEBUG("count: %d\n", count);
1109 DRM_DEBUG("order: %d\n", order);
1110 DRM_DEBUG("size: %d\n", size);
1111 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1112 DRM_DEBUG("alignment: %d\n", alignment);
1113 DRM_DEBUG("page_order: %d\n", page_order);
1114 DRM_DEBUG("total: %d\n", total);
1116 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1119 spin_lock(&dev->buf_lock);
1121 spin_unlock(&dev->buf_lock);
1124 atomic_inc(&dev->buf_alloc);
1125 spin_unlock(&dev->buf_lock);
1127 mutex_lock(&dev->struct_mutex);
1128 entry = &dma->bufs[order];
1129 if (entry->buf_count) {
1130 mutex_unlock(&dev->struct_mutex);
1131 atomic_dec(&dev->buf_alloc);
1132 return -ENOMEM; /* May only call once for each order */
1135 if (count < 0 || count > 4096) {
1136 mutex_unlock(&dev->struct_mutex);
1137 atomic_dec(&dev->buf_alloc);
1141 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
1142 if (!entry->buflist) {
1143 mutex_unlock(&dev->struct_mutex);
1144 atomic_dec(&dev->buf_alloc);
1148 entry->buf_size = size;
1149 entry->page_order = page_order;
1153 while (entry->buf_count < count) {
1154 buf = &entry->buflist[entry->buf_count];
1155 buf->idx = dma->buf_count + entry->buf_count;
1156 buf->total = alignment;
1160 buf->offset = (dma->byte_count + offset);
1161 buf->bus_address = agp_offset + offset;
1162 buf->address = (void *)(agp_offset + offset
1163 + (unsigned long)dev->sg->virtual);
1167 buf->file_priv = NULL;
1169 buf->dev_priv_size = dev->driver->dev_priv_size;
1170 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
1171 if (!buf->dev_private) {
1172 /* Set count correctly so we free the proper amount. */
1173 entry->buf_count = count;
1174 drm_cleanup_buf_error(dev, entry);
1175 mutex_unlock(&dev->struct_mutex);
1176 atomic_dec(&dev->buf_alloc);
1180 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1182 offset += alignment;
1184 byte_count += PAGE_SIZE << page_order;
1187 DRM_DEBUG("byte_count: %d\n", byte_count);
1189 temp_buflist = krealloc(dma->buflist,
1190 (dma->buf_count + entry->buf_count) *
1191 sizeof(*dma->buflist), GFP_KERNEL);
1192 if (!temp_buflist) {
1193 /* Free the entry because it isn't valid */
1194 drm_cleanup_buf_error(dev, entry);
1195 mutex_unlock(&dev->struct_mutex);
1196 atomic_dec(&dev->buf_alloc);
1199 dma->buflist = temp_buflist;
1201 for (i = 0; i < entry->buf_count; i++) {
1202 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1205 dma->buf_count += entry->buf_count;
1206 dma->seg_count += entry->seg_count;
1207 dma->page_count += byte_count >> PAGE_SHIFT;
1208 dma->byte_count += byte_count;
1210 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1211 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1213 mutex_unlock(&dev->struct_mutex);
1215 request->count = entry->buf_count;
1216 request->size = size;
1218 dma->flags = _DRM_DMA_USE_SG;
1220 atomic_dec(&dev->buf_alloc);
1225 * Add buffers for DMA transfers (ioctl).
1227 * \param inode device inode.
1228 * \param file_priv DRM file private.
1229 * \param cmd command.
1230 * \param arg pointer to a struct drm_buf_desc request.
1231 * \return zero on success or a negative number on failure.
1233 * According with the memory type specified in drm_buf_desc::flags and the
1234 * build options, it dispatches the call either to addbufs_agp(),
1235 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1236 * PCI memory respectively.
1238 int drm_legacy_addbufs(struct drm_device *dev, void *data,
1239 struct drm_file *file_priv)
1241 struct drm_buf_desc *request = data;
1244 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1247 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1250 #if IS_ENABLED(CONFIG_AGP)
1251 if (request->flags & _DRM_AGP_BUFFER)
1252 ret = drm_legacy_addbufs_agp(dev, request);
1255 if (request->flags & _DRM_SG_BUFFER)
1256 ret = drm_legacy_addbufs_sg(dev, request);
1257 else if (request->flags & _DRM_FB_BUFFER)
1260 ret = drm_legacy_addbufs_pci(dev, request);
1266 * Get information about the buffer mappings.
1268 * This was originally mean for debugging purposes, or by a sophisticated
1269 * client library to determine how best to use the available buffers (e.g.,
1270 * large buffers can be used for image transfer).
1272 * \param inode device inode.
1273 * \param file_priv DRM file private.
1274 * \param cmd command.
1275 * \param arg pointer to a drm_buf_info structure.
1276 * \return zero on success or a negative number on failure.
1278 * Increments drm_device::buf_use while holding the drm_device::buf_lock
1279 * lock, preventing of allocating more buffers after this call. Information
1280 * about each requested buffer is then copied into user space.
1282 int __drm_legacy_infobufs(struct drm_device *dev,
1284 int (*f)(void *, int, struct drm_buf_entry *))
1286 struct drm_device_dma *dma = dev->dma;
1290 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1293 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1299 spin_lock(&dev->buf_lock);
1300 if (atomic_read(&dev->buf_alloc)) {
1301 spin_unlock(&dev->buf_lock);
1304 ++dev->buf_use; /* Can't allocate more after this call */
1305 spin_unlock(&dev->buf_lock);
1307 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1308 if (dma->bufs[i].buf_count)
1312 DRM_DEBUG("count = %d\n", count);
1315 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1316 struct drm_buf_entry *from = &dma->bufs[i];
1317 if (from->buf_count) {
1318 if (f(data, count, from) < 0)
1320 DRM_DEBUG("%d %d %d %d %d\n",
1322 dma->bufs[i].buf_count,
1323 dma->bufs[i].buf_size,
1324 dma->bufs[i].low_mark,
1325 dma->bufs[i].high_mark);
1335 static int copy_one_buf(void *data, int count, struct drm_buf_entry *from)
1337 struct drm_buf_info *request = data;
1338 struct drm_buf_desc __user *to = &request->list[count];
1339 struct drm_buf_desc v = {.count = from->buf_count,
1340 .size = from->buf_size,
1341 .low_mark = from->low_mark,
1342 .high_mark = from->high_mark};
1343 return copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags));
1346 int drm_legacy_infobufs(struct drm_device *dev, void *data,
1347 struct drm_file *file_priv)
1349 struct drm_buf_info *request = data;
1350 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
1354 * Specifies a low and high water mark for buffer allocation
1356 * \param inode device inode.
1357 * \param file_priv DRM file private.
1358 * \param cmd command.
1359 * \param arg a pointer to a drm_buf_desc structure.
1360 * \return zero on success or a negative number on failure.
1362 * Verifies that the size order is bounded between the admissible orders and
1363 * updates the respective drm_device_dma::bufs entry low and high water mark.
1365 * \note This ioctl is deprecated and mostly never used.
1367 int drm_legacy_markbufs(struct drm_device *dev, void *data,
1368 struct drm_file *file_priv)
1370 struct drm_device_dma *dma = dev->dma;
1371 struct drm_buf_desc *request = data;
1373 struct drm_buf_entry *entry;
1375 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1378 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1384 DRM_DEBUG("%d, %d, %d\n",
1385 request->size, request->low_mark, request->high_mark);
1386 order = order_base_2(request->size);
1387 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1389 entry = &dma->bufs[order];
1391 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1393 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1396 entry->low_mark = request->low_mark;
1397 entry->high_mark = request->high_mark;
1403 * Unreserve the buffers in list, previously reserved using drmDMA.
1405 * \param inode device inode.
1406 * \param file_priv DRM file private.
1407 * \param cmd command.
1408 * \param arg pointer to a drm_buf_free structure.
1409 * \return zero on success or a negative number on failure.
1411 * Calls free_buffer() for each used buffer.
1412 * This function is primarily used for debugging.
1414 int drm_legacy_freebufs(struct drm_device *dev, void *data,
1415 struct drm_file *file_priv)
1417 struct drm_device_dma *dma = dev->dma;
1418 struct drm_buf_free *request = data;
1421 struct drm_buf *buf;
1423 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1426 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1432 DRM_DEBUG("%d\n", request->count);
1433 for (i = 0; i < request->count; i++) {
1434 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1436 if (idx < 0 || idx >= dma->buf_count) {
1437 DRM_ERROR("Index %d (of %d max)\n",
1438 idx, dma->buf_count - 1);
1441 idx = array_index_nospec(idx, dma->buf_count);
1442 buf = dma->buflist[idx];
1443 if (buf->file_priv != file_priv) {
1444 DRM_ERROR("Process %d freeing buffer not owned\n",
1445 task_pid_nr(current));
1448 drm_legacy_free_buffer(dev, buf);
1455 * Maps all of the DMA buffers into client-virtual space (ioctl).
1457 * \param inode device inode.
1458 * \param file_priv DRM file private.
1459 * \param cmd command.
1460 * \param arg pointer to a drm_buf_map structure.
1461 * \return zero on success or a negative number on failure.
1463 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1464 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1465 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1468 int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p,
1470 int (*f)(void *, int, unsigned long,
1472 struct drm_file *file_priv)
1474 struct drm_device_dma *dma = dev->dma;
1476 unsigned long virtual;
1479 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1482 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1488 spin_lock(&dev->buf_lock);
1489 if (atomic_read(&dev->buf_alloc)) {
1490 spin_unlock(&dev->buf_lock);
1493 dev->buf_use++; /* Can't allocate more after this call */
1494 spin_unlock(&dev->buf_lock);
1496 if (*p >= dma->buf_count) {
1497 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
1498 || (drm_core_check_feature(dev, DRIVER_SG)
1499 && (dma->flags & _DRM_DMA_USE_SG))) {
1500 struct drm_local_map *map = dev->agp_buffer_map;
1501 unsigned long token = dev->agp_buffer_token;
1507 virtual = vm_mmap(file_priv->filp, 0, map->size,
1508 PROT_READ | PROT_WRITE,
1512 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
1513 PROT_READ | PROT_WRITE,
1516 if (virtual > -1024UL) {
1518 retcode = (signed long)virtual;
1521 *v = (void __user *)virtual;
1523 for (i = 0; i < dma->buf_count; i++) {
1524 if (f(data, i, virtual, dma->buflist[i]) < 0) {
1531 *p = dma->buf_count;
1532 DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode);
1537 static int map_one_buf(void *data, int idx, unsigned long virtual,
1538 struct drm_buf *buf)
1540 struct drm_buf_map *request = data;
1541 unsigned long address = virtual + buf->offset; /* *** */
1543 if (copy_to_user(&request->list[idx].idx, &buf->idx,
1544 sizeof(request->list[0].idx)))
1546 if (copy_to_user(&request->list[idx].total, &buf->total,
1547 sizeof(request->list[0].total)))
1549 if (clear_user(&request->list[idx].used, sizeof(int)))
1551 if (copy_to_user(&request->list[idx].address, &address,
1557 int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1558 struct drm_file *file_priv)
1560 struct drm_buf_map *request = data;
1561 return __drm_legacy_mapbufs(dev, data, &request->count,
1562 &request->virtual, map_one_buf,
1566 int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
1567 struct drm_file *file_priv)
1569 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1572 if (dev->driver->dma_ioctl)
1573 return dev->driver->dma_ioctl(dev, data, file_priv);
1578 struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
1580 struct drm_map_list *entry;
1582 list_for_each_entry(entry, &dev->maplist, head) {
1583 if (entry->map && entry->map->type == _DRM_SHM &&
1584 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1590 EXPORT_SYMBOL(drm_legacy_getsarea);