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/export.h>
32 #include <linux/log2.h>
34 #include <linux/mman.h>
35 #include <linux/nospec.h>
36 #include <linux/pci.h>
37 #include <linux/slab.h>
38 #include <linux/uaccess.h>
39 #include <linux/vmalloc.h>
41 #include <asm/shmparam.h>
43 #include <drm/drm_device.h>
44 #include <drm/drm_drv.h>
45 #include <drm/drm_file.h>
46 #include <drm/drm_print.h>
48 #include "drm_legacy.h"
51 static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
52 struct drm_local_map *map)
54 struct drm_map_list *entry;
56 list_for_each_entry(entry, &dev->maplist, head) {
58 * Because the kernel-userspace ABI is fixed at a 32-bit offset
59 * while PCI resources may live above that, we only compare the
60 * lower 32 bits of the map offset for maps of type
61 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
62 * It is assumed that if a driver have more than one resource
63 * of each type, the lower 32 bits are different.
66 map->type != entry->map->type ||
67 entry->master != dev->master)
71 if (map->flags != _DRM_CONTAINS_LOCK)
75 case _DRM_FRAME_BUFFER:
76 if ((entry->map->offset & 0xffffffff) ==
77 (map->offset & 0xffffffff))
80 default: /* Make gcc happy */
83 if (entry->map->offset == map->offset)
90 static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
91 unsigned long user_token, int hashed_handle, int shm)
93 int use_hashed_handle, shift;
96 #if (BITS_PER_LONG == 64)
97 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
98 #elif (BITS_PER_LONG == 32)
99 use_hashed_handle = hashed_handle;
101 #error Unsupported long size. Neither 64 nor 32 bits.
104 if (!use_hashed_handle) {
107 hash->key = user_token >> PAGE_SHIFT;
108 ret = drm_ht_insert_item(&dev->map_hash, hash);
114 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
115 if (shm && (SHMLBA > PAGE_SIZE)) {
116 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
118 /* For shared memory, we have to preserve the SHMLBA
119 * bits of the eventual vma->vm_pgoff value during
120 * mmap(). Otherwise we run into cache aliasing problems
121 * on some platforms. On these platforms, the pgoff of
122 * a mmap() request is used to pick a suitable virtual
123 * address for the mmap() region such that it will not
124 * cause cache aliasing problems.
126 * Therefore, make sure the SHMLBA relevant bits of the
127 * hash value we use are equal to those in the original
128 * kernel virtual address.
131 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
134 return drm_ht_just_insert_please(&dev->map_hash, hash,
135 user_token, 32 - PAGE_SHIFT - 3,
140 * Core function to create a range of memory available for mapping by a
143 * Adjusts the memory offset to its absolute value according to the mapping
144 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
145 * applicable and if supported by the kernel.
147 static int drm_addmap_core(struct drm_device *dev, resource_size_t offset,
148 unsigned int size, enum drm_map_type type,
149 enum drm_map_flags flags,
150 struct drm_map_list **maplist)
152 struct drm_local_map *map;
153 struct drm_map_list *list;
154 unsigned long user_token;
157 map = kmalloc(sizeof(*map), GFP_KERNEL);
161 map->offset = offset;
166 /* Only allow shared memory to be removable since we only keep enough
167 * book keeping information about shared memory to allow for removal
168 * when processes fork.
170 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
174 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
175 (unsigned long long)map->offset, map->size, map->type);
177 /* page-align _DRM_SHM maps. They are allocated here so there is no security
178 * hole created by that and it works around various broken drivers that use
179 * a non-aligned quantity to map the SAREA. --BenH
181 if (map->type == _DRM_SHM)
182 map->size = PAGE_ALIGN(map->size);
184 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
193 case _DRM_FRAME_BUFFER:
194 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
195 if (map->offset + (map->size-1) < map->offset ||
196 map->offset < virt_to_phys(high_memory)) {
201 /* Some drivers preinitialize some maps, without the X Server
202 * needing to be aware of it. Therefore, we just return success
203 * when the server tries to create a duplicate map.
205 list = drm_find_matching_map(dev, map);
207 if (list->map->size != map->size) {
208 DRM_DEBUG("Matching maps of type %d with "
209 "mismatched sizes, (%ld vs %ld)\n",
210 map->type, map->size,
212 list->map->size = map->size;
220 if (map->type == _DRM_FRAME_BUFFER ||
221 (map->flags & _DRM_WRITE_COMBINING)) {
223 arch_phys_wc_add(map->offset, map->size);
225 if (map->type == _DRM_REGISTERS) {
226 if (map->flags & _DRM_WRITE_COMBINING)
227 map->handle = ioremap_wc(map->offset,
230 map->handle = ioremap(map->offset, map->size);
239 list = drm_find_matching_map(dev, map);
241 if (list->map->size != map->size) {
242 DRM_DEBUG("Matching maps of type %d with "
243 "mismatched sizes, (%ld vs %ld)\n",
244 map->type, map->size, list->map->size);
245 list->map->size = map->size;
252 map->handle = vmalloc_user(map->size);
253 DRM_DEBUG("%lu %d %p\n",
254 map->size, order_base_2(map->size), map->handle);
259 map->offset = (unsigned long)map->handle;
260 if (map->flags & _DRM_CONTAINS_LOCK) {
261 /* Prevent a 2nd X Server from creating a 2nd lock */
262 if (dev->master->lock.hw_lock != NULL) {
267 dev->sigdata.lock = dev->master->lock.hw_lock = map->handle; /* Pointer to lock */
271 struct drm_agp_mem *entry;
279 map->offset += dev->hose->mem_space->start;
281 /* In some cases (i810 driver), user space may have already
282 * added the AGP base itself, because dev->agp->base previously
283 * only got set during AGP enable. So, only add the base
284 * address if the map's offset isn't already within the
287 if (map->offset < dev->agp->base ||
288 map->offset > dev->agp->base +
289 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
290 map->offset += dev->agp->base;
292 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
294 /* This assumes the DRM is in total control of AGP space.
295 * It's not always the case as AGP can be in the control
296 * of user space (i.e. i810 driver). So this loop will get
297 * skipped and we double check that dev->agp->memory is
298 * actually set as well as being invalid before EPERM'ing
300 list_for_each_entry(entry, &dev->agp->memory, head) {
301 if ((map->offset >= entry->bound) &&
302 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
307 if (!list_empty(&dev->agp->memory) && !valid) {
311 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
312 (unsigned long long)map->offset, map->size);
316 case _DRM_SCATTER_GATHER:
321 map->offset += (unsigned long)dev->sg->virtual;
323 case _DRM_CONSISTENT:
324 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
325 * As we're limiting the address to 2^32-1 (or less),
326 * casting it down to 32 bits is no problem, but we
327 * need to point to a 64bit variable first.
329 map->handle = dma_alloc_coherent(dev->dev,
343 list = kzalloc(sizeof(*list), GFP_KERNEL);
345 if (map->type == _DRM_REGISTERS)
346 iounmap(map->handle);
352 mutex_lock(&dev->struct_mutex);
353 list_add(&list->head, &dev->maplist);
355 /* Assign a 32-bit handle */
356 /* We do it here so that dev->struct_mutex protects the increment */
357 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
359 ret = drm_map_handle(dev, &list->hash, user_token, 0,
360 (map->type == _DRM_SHM));
362 if (map->type == _DRM_REGISTERS)
363 iounmap(map->handle);
366 mutex_unlock(&dev->struct_mutex);
370 list->user_token = list->hash.key << PAGE_SHIFT;
371 mutex_unlock(&dev->struct_mutex);
373 if (!(map->flags & _DRM_DRIVER))
374 list->master = dev->master;
379 int drm_legacy_addmap(struct drm_device *dev, resource_size_t offset,
380 unsigned int size, enum drm_map_type type,
381 enum drm_map_flags flags, struct drm_local_map **map_ptr)
383 struct drm_map_list *list;
386 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
388 *map_ptr = list->map;
391 EXPORT_SYMBOL(drm_legacy_addmap);
393 struct drm_local_map *drm_legacy_findmap(struct drm_device *dev,
396 struct drm_map_list *_entry;
398 list_for_each_entry(_entry, &dev->maplist, head)
399 if (_entry->user_token == token)
403 EXPORT_SYMBOL(drm_legacy_findmap);
406 * Ioctl to specify a range of memory that is available for mapping by a
409 * \param inode device inode.
410 * \param file_priv DRM file private.
411 * \param cmd command.
412 * \param arg pointer to a drm_map structure.
413 * \return zero on success or a negative value on error.
416 int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
417 struct drm_file *file_priv)
419 struct drm_map *map = data;
420 struct drm_map_list *maplist;
423 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
426 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
429 err = drm_addmap_core(dev, map->offset, map->size, map->type,
430 map->flags, &maplist);
435 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
436 map->handle = (void *)(unsigned long)maplist->user_token;
439 * It appears that there are no users of this value whatsoever --
440 * drmAddMap just discards it. Let's not encourage its use.
441 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
442 * it's not a real mtrr index anymore.)
450 * Get a mapping information.
452 * \param inode device inode.
453 * \param file_priv DRM file private.
454 * \param cmd command.
455 * \param arg user argument, pointing to a drm_map structure.
457 * \return zero on success or a negative number on failure.
459 * Searches for the mapping with the specified offset and copies its information
462 int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
463 struct drm_file *file_priv)
465 struct drm_map *map = data;
466 struct drm_map_list *r_list = NULL;
467 struct list_head *list;
471 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
479 mutex_lock(&dev->struct_mutex);
480 list_for_each(list, &dev->maplist) {
482 r_list = list_entry(list, struct drm_map_list, head);
487 if (!r_list || !r_list->map) {
488 mutex_unlock(&dev->struct_mutex);
492 map->offset = r_list->map->offset;
493 map->size = r_list->map->size;
494 map->type = r_list->map->type;
495 map->flags = r_list->map->flags;
496 map->handle = (void *)(unsigned long) r_list->user_token;
497 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
499 mutex_unlock(&dev->struct_mutex);
505 * Remove a map private from list and deallocate resources if the mapping
508 * Searches the map on drm_device::maplist, removes it from the list, see if
509 * it's being used, and free any associated resource (such as MTRR's) if it's not
512 * \sa drm_legacy_addmap
514 int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
516 struct drm_map_list *r_list = NULL, *list_t;
518 struct drm_master *master;
520 /* Find the list entry for the map and remove it */
521 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
522 if (r_list->map == map) {
523 master = r_list->master;
524 list_del(&r_list->head);
525 drm_ht_remove_key(&dev->map_hash,
526 r_list->user_token >> PAGE_SHIFT);
538 iounmap(map->handle);
540 case _DRM_FRAME_BUFFER:
541 arch_phys_wc_del(map->mtrr);
546 if (dev->sigdata.lock == master->lock.hw_lock)
547 dev->sigdata.lock = NULL;
548 master->lock.hw_lock = NULL; /* SHM removed */
549 master->lock.file_priv = NULL;
550 wake_up_interruptible_all(&master->lock.lock_queue);
554 case _DRM_SCATTER_GATHER:
556 case _DRM_CONSISTENT:
557 dma_free_coherent(dev->dev,
567 EXPORT_SYMBOL(drm_legacy_rmmap_locked);
569 void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
571 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
574 mutex_lock(&dev->struct_mutex);
575 drm_legacy_rmmap_locked(dev, map);
576 mutex_unlock(&dev->struct_mutex);
578 EXPORT_SYMBOL(drm_legacy_rmmap);
580 void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
582 struct drm_map_list *r_list, *list_temp;
584 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
587 mutex_lock(&dev->struct_mutex);
588 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
589 if (r_list->master == master) {
590 drm_legacy_rmmap_locked(dev, r_list->map);
594 mutex_unlock(&dev->struct_mutex);
597 void drm_legacy_rmmaps(struct drm_device *dev)
599 struct drm_map_list *r_list, *list_temp;
601 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
602 drm_legacy_rmmap(dev, r_list->map);
605 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
606 * the last close of the device, and this is necessary for cleanup when things
607 * exit uncleanly. Therefore, having userland manually remove mappings seems
608 * like a pointless exercise since they're going away anyway.
610 * One use case might be after addmap is allowed for normal users for SHM and
611 * gets used by drivers that the server doesn't need to care about. This seems
614 * \param inode device inode.
615 * \param file_priv DRM file private.
616 * \param cmd command.
617 * \param arg pointer to a struct drm_map structure.
618 * \return zero on success or a negative value on error.
620 int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
621 struct drm_file *file_priv)
623 struct drm_map *request = data;
624 struct drm_local_map *map = NULL;
625 struct drm_map_list *r_list;
628 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
631 mutex_lock(&dev->struct_mutex);
632 list_for_each_entry(r_list, &dev->maplist, head) {
634 r_list->user_token == (unsigned long)request->handle &&
635 r_list->map->flags & _DRM_REMOVABLE) {
641 /* List has wrapped around to the head pointer, or it's empty we didn't
644 if (list_empty(&dev->maplist) || !map) {
645 mutex_unlock(&dev->struct_mutex);
649 /* Register and framebuffer maps are permanent */
650 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
651 mutex_unlock(&dev->struct_mutex);
655 ret = drm_legacy_rmmap_locked(dev, map);
657 mutex_unlock(&dev->struct_mutex);
663 * Cleanup after an error on one of the addbufs() functions.
665 * \param dev DRM device.
666 * \param entry buffer entry where the error occurred.
668 * Frees any pages and buffers associated with the given entry.
670 static void drm_cleanup_buf_error(struct drm_device *dev,
671 struct drm_buf_entry *entry)
673 drm_dma_handle_t *dmah;
676 if (entry->seg_count) {
677 for (i = 0; i < entry->seg_count; i++) {
678 if (entry->seglist[i]) {
679 dmah = entry->seglist[i];
680 dma_free_coherent(dev->dev,
687 kfree(entry->seglist);
689 entry->seg_count = 0;
692 if (entry->buf_count) {
693 for (i = 0; i < entry->buf_count; i++) {
694 kfree(entry->buflist[i].dev_private);
696 kfree(entry->buflist);
698 entry->buf_count = 0;
702 #if IS_ENABLED(CONFIG_AGP)
704 * Add AGP buffers for DMA transfers.
706 * \param dev struct drm_device to which the buffers are to be added.
707 * \param request pointer to a struct drm_buf_desc describing the request.
708 * \return zero on success or a negative number on failure.
710 * After some sanity checks creates a drm_buf structure for each buffer and
711 * reallocates the buffer list of the same size order to accommodate the new
714 int drm_legacy_addbufs_agp(struct drm_device *dev,
715 struct drm_buf_desc *request)
717 struct drm_device_dma *dma = dev->dma;
718 struct drm_buf_entry *entry;
719 struct drm_agp_mem *agp_entry;
721 unsigned long offset;
722 unsigned long agp_offset;
731 struct drm_buf **temp_buflist;
736 count = request->count;
737 order = order_base_2(request->size);
740 alignment = (request->flags & _DRM_PAGE_ALIGN)
741 ? PAGE_ALIGN(size) : size;
742 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
743 total = PAGE_SIZE << page_order;
746 agp_offset = dev->agp->base + request->agp_start;
748 DRM_DEBUG("count: %d\n", count);
749 DRM_DEBUG("order: %d\n", order);
750 DRM_DEBUG("size: %d\n", size);
751 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
752 DRM_DEBUG("alignment: %d\n", alignment);
753 DRM_DEBUG("page_order: %d\n", page_order);
754 DRM_DEBUG("total: %d\n", total);
756 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
759 /* Make sure buffers are located in AGP memory that we own */
761 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
762 if ((agp_offset >= agp_entry->bound) &&
763 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
768 if (!list_empty(&dev->agp->memory) && !valid) {
769 DRM_DEBUG("zone invalid\n");
772 spin_lock(&dev->buf_lock);
774 spin_unlock(&dev->buf_lock);
777 atomic_inc(&dev->buf_alloc);
778 spin_unlock(&dev->buf_lock);
780 mutex_lock(&dev->struct_mutex);
781 entry = &dma->bufs[order];
782 if (entry->buf_count) {
783 mutex_unlock(&dev->struct_mutex);
784 atomic_dec(&dev->buf_alloc);
785 return -ENOMEM; /* May only call once for each order */
788 if (count < 0 || count > 4096) {
789 mutex_unlock(&dev->struct_mutex);
790 atomic_dec(&dev->buf_alloc);
794 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
795 if (!entry->buflist) {
796 mutex_unlock(&dev->struct_mutex);
797 atomic_dec(&dev->buf_alloc);
801 entry->buf_size = size;
802 entry->page_order = page_order;
806 while (entry->buf_count < count) {
807 buf = &entry->buflist[entry->buf_count];
808 buf->idx = dma->buf_count + entry->buf_count;
809 buf->total = alignment;
813 buf->offset = (dma->byte_count + offset);
814 buf->bus_address = agp_offset + offset;
815 buf->address = (void *)(agp_offset + offset);
819 buf->file_priv = NULL;
821 buf->dev_priv_size = dev->driver->dev_priv_size;
822 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
823 if (!buf->dev_private) {
824 /* Set count correctly so we free the proper amount. */
825 entry->buf_count = count;
826 drm_cleanup_buf_error(dev, entry);
827 mutex_unlock(&dev->struct_mutex);
828 atomic_dec(&dev->buf_alloc);
832 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
836 byte_count += PAGE_SIZE << page_order;
839 DRM_DEBUG("byte_count: %d\n", byte_count);
841 temp_buflist = krealloc(dma->buflist,
842 (dma->buf_count + entry->buf_count) *
843 sizeof(*dma->buflist), GFP_KERNEL);
845 /* Free the entry because it isn't valid */
846 drm_cleanup_buf_error(dev, entry);
847 mutex_unlock(&dev->struct_mutex);
848 atomic_dec(&dev->buf_alloc);
851 dma->buflist = temp_buflist;
853 for (i = 0; i < entry->buf_count; i++) {
854 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
857 dma->buf_count += entry->buf_count;
858 dma->seg_count += entry->seg_count;
859 dma->page_count += byte_count >> PAGE_SHIFT;
860 dma->byte_count += byte_count;
862 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
863 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
865 mutex_unlock(&dev->struct_mutex);
867 request->count = entry->buf_count;
868 request->size = size;
870 dma->flags = _DRM_DMA_USE_AGP;
872 atomic_dec(&dev->buf_alloc);
875 EXPORT_SYMBOL(drm_legacy_addbufs_agp);
876 #endif /* CONFIG_AGP */
878 int drm_legacy_addbufs_pci(struct drm_device *dev,
879 struct drm_buf_desc *request)
881 struct drm_device_dma *dma = dev->dma;
887 struct drm_buf_entry *entry;
888 drm_dma_handle_t *dmah;
891 unsigned long offset;
895 unsigned long *temp_pagelist;
896 struct drm_buf **temp_buflist;
898 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
904 if (!capable(CAP_SYS_ADMIN))
907 count = request->count;
908 order = order_base_2(request->size);
911 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
912 request->count, request->size, size, order);
914 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
917 alignment = (request->flags & _DRM_PAGE_ALIGN)
918 ? PAGE_ALIGN(size) : size;
919 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
920 total = PAGE_SIZE << page_order;
922 spin_lock(&dev->buf_lock);
924 spin_unlock(&dev->buf_lock);
927 atomic_inc(&dev->buf_alloc);
928 spin_unlock(&dev->buf_lock);
930 mutex_lock(&dev->struct_mutex);
931 entry = &dma->bufs[order];
932 if (entry->buf_count) {
933 mutex_unlock(&dev->struct_mutex);
934 atomic_dec(&dev->buf_alloc);
935 return -ENOMEM; /* May only call once for each order */
938 if (count < 0 || count > 4096) {
939 mutex_unlock(&dev->struct_mutex);
940 atomic_dec(&dev->buf_alloc);
944 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
945 if (!entry->buflist) {
946 mutex_unlock(&dev->struct_mutex);
947 atomic_dec(&dev->buf_alloc);
951 entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
952 if (!entry->seglist) {
953 kfree(entry->buflist);
954 mutex_unlock(&dev->struct_mutex);
955 atomic_dec(&dev->buf_alloc);
959 /* Keep the original pagelist until we know all the allocations
962 temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
963 sizeof(*dma->pagelist),
965 if (!temp_pagelist) {
966 kfree(entry->buflist);
967 kfree(entry->seglist);
968 mutex_unlock(&dev->struct_mutex);
969 atomic_dec(&dev->buf_alloc);
972 memcpy(temp_pagelist,
973 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
974 DRM_DEBUG("pagelist: %d entries\n",
975 dma->page_count + (count << page_order));
977 entry->buf_size = size;
978 entry->page_order = page_order;
982 while (entry->buf_count < count) {
983 dmah = kmalloc(sizeof(drm_dma_handle_t), GFP_KERNEL);
985 /* Set count correctly so we free the proper amount. */
986 entry->buf_count = count;
987 entry->seg_count = count;
988 drm_cleanup_buf_error(dev, entry);
989 kfree(temp_pagelist);
990 mutex_unlock(&dev->struct_mutex);
991 atomic_dec(&dev->buf_alloc);
996 dmah->vaddr = dma_alloc_coherent(dev->dev,
1003 /* Set count correctly so we free the proper amount. */
1004 entry->buf_count = count;
1005 entry->seg_count = count;
1006 drm_cleanup_buf_error(dev, entry);
1007 kfree(temp_pagelist);
1008 mutex_unlock(&dev->struct_mutex);
1009 atomic_dec(&dev->buf_alloc);
1012 entry->seglist[entry->seg_count++] = dmah;
1013 for (i = 0; i < (1 << page_order); i++) {
1014 DRM_DEBUG("page %d @ 0x%08lx\n",
1015 dma->page_count + page_count,
1016 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
1017 temp_pagelist[dma->page_count + page_count++]
1018 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
1021 offset + size <= total && entry->buf_count < count;
1022 offset += alignment, ++entry->buf_count) {
1023 buf = &entry->buflist[entry->buf_count];
1024 buf->idx = dma->buf_count + entry->buf_count;
1025 buf->total = alignment;
1028 buf->offset = (dma->byte_count + byte_count + offset);
1029 buf->address = (void *)(dmah->vaddr + offset);
1030 buf->bus_address = dmah->busaddr + offset;
1034 buf->file_priv = NULL;
1036 buf->dev_priv_size = dev->driver->dev_priv_size;
1037 buf->dev_private = kzalloc(buf->dev_priv_size,
1039 if (!buf->dev_private) {
1040 /* Set count correctly so we free the proper amount. */
1041 entry->buf_count = count;
1042 entry->seg_count = count;
1043 drm_cleanup_buf_error(dev, entry);
1044 kfree(temp_pagelist);
1045 mutex_unlock(&dev->struct_mutex);
1046 atomic_dec(&dev->buf_alloc);
1050 DRM_DEBUG("buffer %d @ %p\n",
1051 entry->buf_count, buf->address);
1053 byte_count += PAGE_SIZE << page_order;
1056 temp_buflist = krealloc(dma->buflist,
1057 (dma->buf_count + entry->buf_count) *
1058 sizeof(*dma->buflist), GFP_KERNEL);
1059 if (!temp_buflist) {
1060 /* Free the entry because it isn't valid */
1061 drm_cleanup_buf_error(dev, entry);
1062 kfree(temp_pagelist);
1063 mutex_unlock(&dev->struct_mutex);
1064 atomic_dec(&dev->buf_alloc);
1067 dma->buflist = temp_buflist;
1069 for (i = 0; i < entry->buf_count; i++) {
1070 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1073 /* No allocations failed, so now we can replace the original pagelist
1076 if (dma->page_count) {
1077 kfree(dma->pagelist);
1079 dma->pagelist = temp_pagelist;
1081 dma->buf_count += entry->buf_count;
1082 dma->seg_count += entry->seg_count;
1083 dma->page_count += entry->seg_count << page_order;
1084 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1086 mutex_unlock(&dev->struct_mutex);
1088 request->count = entry->buf_count;
1089 request->size = size;
1091 if (request->flags & _DRM_PCI_BUFFER_RO)
1092 dma->flags = _DRM_DMA_USE_PCI_RO;
1094 atomic_dec(&dev->buf_alloc);
1098 EXPORT_SYMBOL(drm_legacy_addbufs_pci);
1100 static int drm_legacy_addbufs_sg(struct drm_device *dev,
1101 struct drm_buf_desc *request)
1103 struct drm_device_dma *dma = dev->dma;
1104 struct drm_buf_entry *entry;
1105 struct drm_buf *buf;
1106 unsigned long offset;
1107 unsigned long agp_offset;
1116 struct drm_buf **temp_buflist;
1118 if (!drm_core_check_feature(dev, DRIVER_SG))
1124 if (!capable(CAP_SYS_ADMIN))
1127 count = request->count;
1128 order = order_base_2(request->size);
1131 alignment = (request->flags & _DRM_PAGE_ALIGN)
1132 ? PAGE_ALIGN(size) : size;
1133 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1134 total = PAGE_SIZE << page_order;
1137 agp_offset = request->agp_start;
1139 DRM_DEBUG("count: %d\n", count);
1140 DRM_DEBUG("order: %d\n", order);
1141 DRM_DEBUG("size: %d\n", size);
1142 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1143 DRM_DEBUG("alignment: %d\n", alignment);
1144 DRM_DEBUG("page_order: %d\n", page_order);
1145 DRM_DEBUG("total: %d\n", total);
1147 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1150 spin_lock(&dev->buf_lock);
1152 spin_unlock(&dev->buf_lock);
1155 atomic_inc(&dev->buf_alloc);
1156 spin_unlock(&dev->buf_lock);
1158 mutex_lock(&dev->struct_mutex);
1159 entry = &dma->bufs[order];
1160 if (entry->buf_count) {
1161 mutex_unlock(&dev->struct_mutex);
1162 atomic_dec(&dev->buf_alloc);
1163 return -ENOMEM; /* May only call once for each order */
1166 if (count < 0 || count > 4096) {
1167 mutex_unlock(&dev->struct_mutex);
1168 atomic_dec(&dev->buf_alloc);
1172 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
1173 if (!entry->buflist) {
1174 mutex_unlock(&dev->struct_mutex);
1175 atomic_dec(&dev->buf_alloc);
1179 entry->buf_size = size;
1180 entry->page_order = page_order;
1184 while (entry->buf_count < count) {
1185 buf = &entry->buflist[entry->buf_count];
1186 buf->idx = dma->buf_count + entry->buf_count;
1187 buf->total = alignment;
1191 buf->offset = (dma->byte_count + offset);
1192 buf->bus_address = agp_offset + offset;
1193 buf->address = (void *)(agp_offset + offset
1194 + (unsigned long)dev->sg->virtual);
1198 buf->file_priv = NULL;
1200 buf->dev_priv_size = dev->driver->dev_priv_size;
1201 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
1202 if (!buf->dev_private) {
1203 /* Set count correctly so we free the proper amount. */
1204 entry->buf_count = count;
1205 drm_cleanup_buf_error(dev, entry);
1206 mutex_unlock(&dev->struct_mutex);
1207 atomic_dec(&dev->buf_alloc);
1211 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1213 offset += alignment;
1215 byte_count += PAGE_SIZE << page_order;
1218 DRM_DEBUG("byte_count: %d\n", byte_count);
1220 temp_buflist = krealloc(dma->buflist,
1221 (dma->buf_count + entry->buf_count) *
1222 sizeof(*dma->buflist), GFP_KERNEL);
1223 if (!temp_buflist) {
1224 /* Free the entry because it isn't valid */
1225 drm_cleanup_buf_error(dev, entry);
1226 mutex_unlock(&dev->struct_mutex);
1227 atomic_dec(&dev->buf_alloc);
1230 dma->buflist = temp_buflist;
1232 for (i = 0; i < entry->buf_count; i++) {
1233 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1236 dma->buf_count += entry->buf_count;
1237 dma->seg_count += entry->seg_count;
1238 dma->page_count += byte_count >> PAGE_SHIFT;
1239 dma->byte_count += byte_count;
1241 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1242 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1244 mutex_unlock(&dev->struct_mutex);
1246 request->count = entry->buf_count;
1247 request->size = size;
1249 dma->flags = _DRM_DMA_USE_SG;
1251 atomic_dec(&dev->buf_alloc);
1256 * Add buffers for DMA transfers (ioctl).
1258 * \param inode device inode.
1259 * \param file_priv DRM file private.
1260 * \param cmd command.
1261 * \param arg pointer to a struct drm_buf_desc request.
1262 * \return zero on success or a negative number on failure.
1264 * According with the memory type specified in drm_buf_desc::flags and the
1265 * build options, it dispatches the call either to addbufs_agp(),
1266 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1267 * PCI memory respectively.
1269 int drm_legacy_addbufs(struct drm_device *dev, void *data,
1270 struct drm_file *file_priv)
1272 struct drm_buf_desc *request = data;
1275 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1278 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1281 #if IS_ENABLED(CONFIG_AGP)
1282 if (request->flags & _DRM_AGP_BUFFER)
1283 ret = drm_legacy_addbufs_agp(dev, request);
1286 if (request->flags & _DRM_SG_BUFFER)
1287 ret = drm_legacy_addbufs_sg(dev, request);
1288 else if (request->flags & _DRM_FB_BUFFER)
1291 ret = drm_legacy_addbufs_pci(dev, request);
1297 * Get information about the buffer mappings.
1299 * This was originally mean for debugging purposes, or by a sophisticated
1300 * client library to determine how best to use the available buffers (e.g.,
1301 * large buffers can be used for image transfer).
1303 * \param inode device inode.
1304 * \param file_priv DRM file private.
1305 * \param cmd command.
1306 * \param arg pointer to a drm_buf_info structure.
1307 * \return zero on success or a negative number on failure.
1309 * Increments drm_device::buf_use while holding the drm_device::buf_lock
1310 * lock, preventing of allocating more buffers after this call. Information
1311 * about each requested buffer is then copied into user space.
1313 int __drm_legacy_infobufs(struct drm_device *dev,
1315 int (*f)(void *, int, struct drm_buf_entry *))
1317 struct drm_device_dma *dma = dev->dma;
1321 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1324 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1330 spin_lock(&dev->buf_lock);
1331 if (atomic_read(&dev->buf_alloc)) {
1332 spin_unlock(&dev->buf_lock);
1335 ++dev->buf_use; /* Can't allocate more after this call */
1336 spin_unlock(&dev->buf_lock);
1338 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1339 if (dma->bufs[i].buf_count)
1343 DRM_DEBUG("count = %d\n", count);
1346 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1347 struct drm_buf_entry *from = &dma->bufs[i];
1349 if (from->buf_count) {
1350 if (f(data, count, from) < 0)
1352 DRM_DEBUG("%d %d %d %d %d\n",
1354 dma->bufs[i].buf_count,
1355 dma->bufs[i].buf_size,
1356 dma->bufs[i].low_mark,
1357 dma->bufs[i].high_mark);
1367 static int copy_one_buf(void *data, int count, struct drm_buf_entry *from)
1369 struct drm_buf_info *request = data;
1370 struct drm_buf_desc __user *to = &request->list[count];
1371 struct drm_buf_desc v = {.count = from->buf_count,
1372 .size = from->buf_size,
1373 .low_mark = from->low_mark,
1374 .high_mark = from->high_mark};
1376 if (copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags)))
1381 int drm_legacy_infobufs(struct drm_device *dev, void *data,
1382 struct drm_file *file_priv)
1384 struct drm_buf_info *request = data;
1386 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
1390 * Specifies a low and high water mark for buffer allocation
1392 * \param inode device inode.
1393 * \param file_priv DRM file private.
1394 * \param cmd command.
1395 * \param arg a pointer to a drm_buf_desc structure.
1396 * \return zero on success or a negative number on failure.
1398 * Verifies that the size order is bounded between the admissible orders and
1399 * updates the respective drm_device_dma::bufs entry low and high water mark.
1401 * \note This ioctl is deprecated and mostly never used.
1403 int drm_legacy_markbufs(struct drm_device *dev, void *data,
1404 struct drm_file *file_priv)
1406 struct drm_device_dma *dma = dev->dma;
1407 struct drm_buf_desc *request = data;
1409 struct drm_buf_entry *entry;
1411 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1414 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1420 DRM_DEBUG("%d, %d, %d\n",
1421 request->size, request->low_mark, request->high_mark);
1422 order = order_base_2(request->size);
1423 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1425 entry = &dma->bufs[order];
1427 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1429 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1432 entry->low_mark = request->low_mark;
1433 entry->high_mark = request->high_mark;
1439 * Unreserve the buffers in list, previously reserved using drmDMA.
1441 * \param inode device inode.
1442 * \param file_priv DRM file private.
1443 * \param cmd command.
1444 * \param arg pointer to a drm_buf_free structure.
1445 * \return zero on success or a negative number on failure.
1447 * Calls free_buffer() for each used buffer.
1448 * This function is primarily used for debugging.
1450 int drm_legacy_freebufs(struct drm_device *dev, void *data,
1451 struct drm_file *file_priv)
1453 struct drm_device_dma *dma = dev->dma;
1454 struct drm_buf_free *request = data;
1457 struct drm_buf *buf;
1459 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1462 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1468 DRM_DEBUG("%d\n", request->count);
1469 for (i = 0; i < request->count; i++) {
1470 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1472 if (idx < 0 || idx >= dma->buf_count) {
1473 DRM_ERROR("Index %d (of %d max)\n",
1474 idx, dma->buf_count - 1);
1477 idx = array_index_nospec(idx, dma->buf_count);
1478 buf = dma->buflist[idx];
1479 if (buf->file_priv != file_priv) {
1480 DRM_ERROR("Process %d freeing buffer not owned\n",
1481 task_pid_nr(current));
1484 drm_legacy_free_buffer(dev, buf);
1491 * Maps all of the DMA buffers into client-virtual space (ioctl).
1493 * \param inode device inode.
1494 * \param file_priv DRM file private.
1495 * \param cmd command.
1496 * \param arg pointer to a drm_buf_map structure.
1497 * \return zero on success or a negative number on failure.
1499 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1500 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1501 * offset equal to 0, which drm_mmap() interprets as PCI buffers and calls
1504 int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p,
1506 int (*f)(void *, int, unsigned long,
1508 struct drm_file *file_priv)
1510 struct drm_device_dma *dma = dev->dma;
1512 unsigned long virtual;
1515 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1518 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1524 spin_lock(&dev->buf_lock);
1525 if (atomic_read(&dev->buf_alloc)) {
1526 spin_unlock(&dev->buf_lock);
1529 dev->buf_use++; /* Can't allocate more after this call */
1530 spin_unlock(&dev->buf_lock);
1532 if (*p >= dma->buf_count) {
1533 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
1534 || (drm_core_check_feature(dev, DRIVER_SG)
1535 && (dma->flags & _DRM_DMA_USE_SG))) {
1536 struct drm_local_map *map = dev->agp_buffer_map;
1537 unsigned long token = dev->agp_buffer_token;
1543 virtual = vm_mmap(file_priv->filp, 0, map->size,
1544 PROT_READ | PROT_WRITE,
1548 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
1549 PROT_READ | PROT_WRITE,
1552 if (virtual > -1024UL) {
1554 retcode = (signed long)virtual;
1557 *v = (void __user *)virtual;
1559 for (i = 0; i < dma->buf_count; i++) {
1560 if (f(data, i, virtual, dma->buflist[i]) < 0) {
1567 *p = dma->buf_count;
1568 DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode);
1573 static int map_one_buf(void *data, int idx, unsigned long virtual,
1574 struct drm_buf *buf)
1576 struct drm_buf_map *request = data;
1577 unsigned long address = virtual + buf->offset; /* *** */
1579 if (copy_to_user(&request->list[idx].idx, &buf->idx,
1580 sizeof(request->list[0].idx)))
1582 if (copy_to_user(&request->list[idx].total, &buf->total,
1583 sizeof(request->list[0].total)))
1585 if (clear_user(&request->list[idx].used, sizeof(int)))
1587 if (copy_to_user(&request->list[idx].address, &address,
1593 int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1594 struct drm_file *file_priv)
1596 struct drm_buf_map *request = data;
1598 return __drm_legacy_mapbufs(dev, data, &request->count,
1599 &request->virtual, map_one_buf,
1603 int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
1604 struct drm_file *file_priv)
1606 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1609 if (dev->driver->dma_ioctl)
1610 return dev->driver->dma_ioctl(dev, data, file_priv);
1615 struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
1617 struct drm_map_list *entry;
1619 list_for_each_entry(entry, &dev->maplist, head) {
1620 if (entry->map && entry->map->type == _DRM_SHM &&
1621 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1627 EXPORT_SYMBOL(drm_legacy_getsarea);