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.
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_agpsupport.h>
44 #include <drm/drm_device.h>
45 #include <drm/drm_drv.h>
46 #include <drm/drm_file.h>
47 #include <drm/drm_print.h>
49 #include "drm_legacy.h"
52 static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
53 struct drm_local_map *map)
55 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))
79 default: /* Make gcc happy */
82 if (entry->map->offset == map->offset)
89 static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
90 unsigned long user_token, int hashed_handle, int shm)
92 int use_hashed_handle, shift;
95 #if (BITS_PER_LONG == 64)
96 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
97 #elif (BITS_PER_LONG == 32)
98 use_hashed_handle = hashed_handle;
100 #error Unsupported long size. Neither 64 nor 32 bits.
103 if (!use_hashed_handle) {
105 hash->key = user_token >> PAGE_SHIFT;
106 ret = drm_ht_insert_item(&dev->map_hash, hash);
112 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
113 if (shm && (SHMLBA > PAGE_SIZE)) {
114 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
116 /* For shared memory, we have to preserve the SHMLBA
117 * bits of the eventual vma->vm_pgoff value during
118 * mmap(). Otherwise we run into cache aliasing problems
119 * on some platforms. On these platforms, the pgoff of
120 * a mmap() request is used to pick a suitable virtual
121 * address for the mmap() region such that it will not
122 * cause cache aliasing problems.
124 * Therefore, make sure the SHMLBA relevant bits of the
125 * hash value we use are equal to those in the original
126 * kernel virtual address.
129 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
132 return drm_ht_just_insert_please(&dev->map_hash, hash,
133 user_token, 32 - PAGE_SHIFT - 3,
138 * Core function to create a range of memory available for mapping by a
141 * Adjusts the memory offset to its absolute value according to the mapping
142 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
143 * applicable and if supported by the kernel.
145 static int drm_addmap_core(struct drm_device *dev, resource_size_t offset,
146 unsigned int size, enum drm_map_type type,
147 enum drm_map_flags flags,
148 struct drm_map_list **maplist)
150 struct drm_local_map *map;
151 struct drm_map_list *list;
152 unsigned long user_token;
155 map = kmalloc(sizeof(*map), GFP_KERNEL);
159 map->offset = offset;
164 /* Only allow shared memory to be removable since we only keep enough
165 * book keeping information about shared memory to allow for removal
166 * when processes fork.
168 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
172 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
173 (unsigned long long)map->offset, map->size, map->type);
175 /* page-align _DRM_SHM maps. They are allocated here so there is no security
176 * hole created by that and it works around various broken drivers that use
177 * a non-aligned quantity to map the SAREA. --BenH
179 if (map->type == _DRM_SHM)
180 map->size = PAGE_ALIGN(map->size);
182 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
191 case _DRM_FRAME_BUFFER:
192 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
193 if (map->offset + (map->size-1) < map->offset ||
194 map->offset < virt_to_phys(high_memory)) {
199 /* Some drivers preinitialize some maps, without the X Server
200 * needing to be aware of it. Therefore, we just return success
201 * when the server tries to create a duplicate map.
203 list = drm_find_matching_map(dev, map);
205 if (list->map->size != map->size) {
206 DRM_DEBUG("Matching maps of type %d with "
207 "mismatched sizes, (%ld vs %ld)\n",
208 map->type, map->size,
210 list->map->size = map->size;
218 if (map->type == _DRM_FRAME_BUFFER ||
219 (map->flags & _DRM_WRITE_COMBINING)) {
221 arch_phys_wc_add(map->offset, map->size);
223 if (map->type == _DRM_REGISTERS) {
224 if (map->flags & _DRM_WRITE_COMBINING)
225 map->handle = ioremap_wc(map->offset,
228 map->handle = ioremap(map->offset, map->size);
237 list = drm_find_matching_map(dev, map);
239 if (list->map->size != map->size) {
240 DRM_DEBUG("Matching maps of type %d with "
241 "mismatched sizes, (%ld vs %ld)\n",
242 map->type, map->size, list->map->size);
243 list->map->size = map->size;
250 map->handle = vmalloc_user(map->size);
251 DRM_DEBUG("%lu %d %p\n",
252 map->size, order_base_2(map->size), map->handle);
257 map->offset = (unsigned long)map->handle;
258 if (map->flags & _DRM_CONTAINS_LOCK) {
259 /* Prevent a 2nd X Server from creating a 2nd lock */
260 if (dev->master->lock.hw_lock != NULL) {
265 dev->sigdata.lock = dev->master->lock.hw_lock = map->handle; /* Pointer to lock */
269 struct drm_agp_mem *entry;
277 map->offset += dev->hose->mem_space->start;
279 /* In some cases (i810 driver), user space may have already
280 * added the AGP base itself, because dev->agp->base previously
281 * only got set during AGP enable. So, only add the base
282 * address if the map's offset isn't already within the
285 if (map->offset < dev->agp->base ||
286 map->offset > dev->agp->base +
287 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
288 map->offset += dev->agp->base;
290 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
292 /* This assumes the DRM is in total control of AGP space.
293 * It's not always the case as AGP can be in the control
294 * of user space (i.e. i810 driver). So this loop will get
295 * skipped and we double check that dev->agp->memory is
296 * actually set as well as being invalid before EPERM'ing
298 list_for_each_entry(entry, &dev->agp->memory, head) {
299 if ((map->offset >= entry->bound) &&
300 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
305 if (!list_empty(&dev->agp->memory) && !valid) {
309 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
310 (unsigned long long)map->offset, map->size);
314 case _DRM_SCATTER_GATHER:
319 map->offset += (unsigned long)dev->sg->virtual;
321 case _DRM_CONSISTENT:
322 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
323 * As we're limiting the address to 2^32-1 (or less),
324 * casting it down to 32 bits is no problem, but we
325 * need to point to a 64bit variable first. */
326 map->handle = dma_alloc_coherent(&dev->pdev->dev,
340 list = kzalloc(sizeof(*list), GFP_KERNEL);
342 if (map->type == _DRM_REGISTERS)
343 iounmap(map->handle);
349 mutex_lock(&dev->struct_mutex);
350 list_add(&list->head, &dev->maplist);
352 /* Assign a 32-bit handle */
353 /* We do it here so that dev->struct_mutex protects the increment */
354 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
356 ret = drm_map_handle(dev, &list->hash, user_token, 0,
357 (map->type == _DRM_SHM));
359 if (map->type == _DRM_REGISTERS)
360 iounmap(map->handle);
363 mutex_unlock(&dev->struct_mutex);
367 list->user_token = list->hash.key << PAGE_SHIFT;
368 mutex_unlock(&dev->struct_mutex);
370 if (!(map->flags & _DRM_DRIVER))
371 list->master = dev->master;
376 int drm_legacy_addmap(struct drm_device *dev, resource_size_t offset,
377 unsigned int size, enum drm_map_type type,
378 enum drm_map_flags flags, struct drm_local_map **map_ptr)
380 struct drm_map_list *list;
383 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
385 *map_ptr = list->map;
388 EXPORT_SYMBOL(drm_legacy_addmap);
390 struct drm_local_map *drm_legacy_findmap(struct drm_device *dev,
393 struct drm_map_list *_entry;
394 list_for_each_entry(_entry, &dev->maplist, head)
395 if (_entry->user_token == token)
399 EXPORT_SYMBOL(drm_legacy_findmap);
402 * Ioctl to specify a range of memory that is available for mapping by a
405 * \param inode device inode.
406 * \param file_priv DRM file private.
407 * \param cmd command.
408 * \param arg pointer to a drm_map structure.
409 * \return zero on success or a negative value on error.
412 int drm_legacy_addmap_ioctl(struct drm_device *dev, void *data,
413 struct drm_file *file_priv)
415 struct drm_map *map = data;
416 struct drm_map_list *maplist;
419 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
422 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
423 !drm_core_check_feature(dev, DRIVER_LEGACY))
426 err = drm_addmap_core(dev, map->offset, map->size, map->type,
427 map->flags, &maplist);
432 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
433 map->handle = (void *)(unsigned long)maplist->user_token;
436 * It appears that there are no users of this value whatsoever --
437 * drmAddMap just discards it. Let's not encourage its use.
438 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
439 * it's not a real mtrr index anymore.)
447 * Get a mapping information.
449 * \param inode device inode.
450 * \param file_priv DRM file private.
451 * \param cmd command.
452 * \param arg user argument, pointing to a drm_map structure.
454 * \return zero on success or a negative number on failure.
456 * Searches for the mapping with the specified offset and copies its information
459 int drm_legacy_getmap_ioctl(struct drm_device *dev, void *data,
460 struct drm_file *file_priv)
462 struct drm_map *map = data;
463 struct drm_map_list *r_list = NULL;
464 struct list_head *list;
468 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
469 !drm_core_check_feature(dev, DRIVER_LEGACY))
477 mutex_lock(&dev->struct_mutex);
478 list_for_each(list, &dev->maplist) {
480 r_list = list_entry(list, struct drm_map_list, head);
485 if (!r_list || !r_list->map) {
486 mutex_unlock(&dev->struct_mutex);
490 map->offset = r_list->map->offset;
491 map->size = r_list->map->size;
492 map->type = r_list->map->type;
493 map->flags = r_list->map->flags;
494 map->handle = (void *)(unsigned long) r_list->user_token;
495 map->mtrr = arch_phys_wc_index(r_list->map->mtrr);
497 mutex_unlock(&dev->struct_mutex);
503 * Remove a map private from list and deallocate resources if the mapping
506 * Searches the map on drm_device::maplist, removes it from the list, see if
507 * it's being used, and free any associated resource (such as MTRR's) if it's not
510 * \sa drm_legacy_addmap
512 int drm_legacy_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
514 struct drm_map_list *r_list = NULL, *list_t;
516 struct drm_master *master;
518 /* Find the list entry for the map and remove it */
519 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
520 if (r_list->map == map) {
521 master = r_list->master;
522 list_del(&r_list->head);
523 drm_ht_remove_key(&dev->map_hash,
524 r_list->user_token >> PAGE_SHIFT);
536 iounmap(map->handle);
538 case _DRM_FRAME_BUFFER:
539 arch_phys_wc_del(map->mtrr);
544 if (dev->sigdata.lock == master->lock.hw_lock)
545 dev->sigdata.lock = NULL;
546 master->lock.hw_lock = NULL; /* SHM removed */
547 master->lock.file_priv = NULL;
548 wake_up_interruptible_all(&master->lock.lock_queue);
552 case _DRM_SCATTER_GATHER:
554 case _DRM_CONSISTENT:
555 dma_free_coherent(&dev->pdev->dev,
565 EXPORT_SYMBOL(drm_legacy_rmmap_locked);
567 void drm_legacy_rmmap(struct drm_device *dev, struct drm_local_map *map)
569 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
570 !drm_core_check_feature(dev, DRIVER_LEGACY))
573 mutex_lock(&dev->struct_mutex);
574 drm_legacy_rmmap_locked(dev, map);
575 mutex_unlock(&dev->struct_mutex);
577 EXPORT_SYMBOL(drm_legacy_rmmap);
579 void drm_legacy_master_rmmaps(struct drm_device *dev, struct drm_master *master)
581 struct drm_map_list *r_list, *list_temp;
583 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
586 mutex_lock(&dev->struct_mutex);
587 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
588 if (r_list->master == master) {
589 drm_legacy_rmmap_locked(dev, r_list->map);
593 mutex_unlock(&dev->struct_mutex);
596 void drm_legacy_rmmaps(struct drm_device *dev)
598 struct drm_map_list *r_list, *list_temp;
600 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
601 drm_legacy_rmmap(dev, r_list->map);
604 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
605 * the last close of the device, and this is necessary for cleanup when things
606 * exit uncleanly. Therefore, having userland manually remove mappings seems
607 * like a pointless exercise since they're going away anyway.
609 * One use case might be after addmap is allowed for normal users for SHM and
610 * gets used by drivers that the server doesn't need to care about. This seems
613 * \param inode device inode.
614 * \param file_priv DRM file private.
615 * \param cmd command.
616 * \param arg pointer to a struct drm_map structure.
617 * \return zero on success or a negative value on error.
619 int drm_legacy_rmmap_ioctl(struct drm_device *dev, void *data,
620 struct drm_file *file_priv)
622 struct drm_map *request = data;
623 struct drm_local_map *map = NULL;
624 struct drm_map_list *r_list;
627 if (!drm_core_check_feature(dev, DRIVER_KMS_LEGACY_CONTEXT) &&
628 !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)
675 if (entry->seg_count) {
676 for (i = 0; i < entry->seg_count; i++) {
677 if (entry->seglist[i]) {
678 drm_pci_free(dev, entry->seglist[i]);
681 kfree(entry->seglist);
683 entry->seg_count = 0;
686 if (entry->buf_count) {
687 for (i = 0; i < entry->buf_count; i++) {
688 kfree(entry->buflist[i].dev_private);
690 kfree(entry->buflist);
692 entry->buf_count = 0;
696 #if IS_ENABLED(CONFIG_AGP)
698 * Add AGP buffers for DMA transfers.
700 * \param dev struct drm_device to which the buffers are to be added.
701 * \param request pointer to a struct drm_buf_desc describing the request.
702 * \return zero on success or a negative number on failure.
704 * After some sanity checks creates a drm_buf structure for each buffer and
705 * reallocates the buffer list of the same size order to accommodate the new
708 int drm_legacy_addbufs_agp(struct drm_device *dev,
709 struct drm_buf_desc *request)
711 struct drm_device_dma *dma = dev->dma;
712 struct drm_buf_entry *entry;
713 struct drm_agp_mem *agp_entry;
715 unsigned long offset;
716 unsigned long agp_offset;
725 struct drm_buf **temp_buflist;
730 count = request->count;
731 order = order_base_2(request->size);
734 alignment = (request->flags & _DRM_PAGE_ALIGN)
735 ? PAGE_ALIGN(size) : size;
736 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
737 total = PAGE_SIZE << page_order;
740 agp_offset = dev->agp->base + request->agp_start;
742 DRM_DEBUG("count: %d\n", count);
743 DRM_DEBUG("order: %d\n", order);
744 DRM_DEBUG("size: %d\n", size);
745 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
746 DRM_DEBUG("alignment: %d\n", alignment);
747 DRM_DEBUG("page_order: %d\n", page_order);
748 DRM_DEBUG("total: %d\n", total);
750 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
753 /* Make sure buffers are located in AGP memory that we own */
755 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
756 if ((agp_offset >= agp_entry->bound) &&
757 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
762 if (!list_empty(&dev->agp->memory) && !valid) {
763 DRM_DEBUG("zone invalid\n");
766 spin_lock(&dev->buf_lock);
768 spin_unlock(&dev->buf_lock);
771 atomic_inc(&dev->buf_alloc);
772 spin_unlock(&dev->buf_lock);
774 mutex_lock(&dev->struct_mutex);
775 entry = &dma->bufs[order];
776 if (entry->buf_count) {
777 mutex_unlock(&dev->struct_mutex);
778 atomic_dec(&dev->buf_alloc);
779 return -ENOMEM; /* May only call once for each order */
782 if (count < 0 || count > 4096) {
783 mutex_unlock(&dev->struct_mutex);
784 atomic_dec(&dev->buf_alloc);
788 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
789 if (!entry->buflist) {
790 mutex_unlock(&dev->struct_mutex);
791 atomic_dec(&dev->buf_alloc);
795 entry->buf_size = size;
796 entry->page_order = page_order;
800 while (entry->buf_count < count) {
801 buf = &entry->buflist[entry->buf_count];
802 buf->idx = dma->buf_count + entry->buf_count;
803 buf->total = alignment;
807 buf->offset = (dma->byte_count + offset);
808 buf->bus_address = agp_offset + offset;
809 buf->address = (void *)(agp_offset + offset);
813 buf->file_priv = NULL;
815 buf->dev_priv_size = dev->driver->dev_priv_size;
816 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
817 if (!buf->dev_private) {
818 /* Set count correctly so we free the proper amount. */
819 entry->buf_count = count;
820 drm_cleanup_buf_error(dev, entry);
821 mutex_unlock(&dev->struct_mutex);
822 atomic_dec(&dev->buf_alloc);
826 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
830 byte_count += PAGE_SIZE << page_order;
833 DRM_DEBUG("byte_count: %d\n", byte_count);
835 temp_buflist = krealloc(dma->buflist,
836 (dma->buf_count + entry->buf_count) *
837 sizeof(*dma->buflist), GFP_KERNEL);
839 /* Free the entry because it isn't valid */
840 drm_cleanup_buf_error(dev, entry);
841 mutex_unlock(&dev->struct_mutex);
842 atomic_dec(&dev->buf_alloc);
845 dma->buflist = temp_buflist;
847 for (i = 0; i < entry->buf_count; i++) {
848 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
851 dma->buf_count += entry->buf_count;
852 dma->seg_count += entry->seg_count;
853 dma->page_count += byte_count >> PAGE_SHIFT;
854 dma->byte_count += byte_count;
856 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
857 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
859 mutex_unlock(&dev->struct_mutex);
861 request->count = entry->buf_count;
862 request->size = size;
864 dma->flags = _DRM_DMA_USE_AGP;
866 atomic_dec(&dev->buf_alloc);
869 EXPORT_SYMBOL(drm_legacy_addbufs_agp);
870 #endif /* CONFIG_AGP */
872 int drm_legacy_addbufs_pci(struct drm_device *dev,
873 struct drm_buf_desc *request)
875 struct drm_device_dma *dma = dev->dma;
881 struct drm_buf_entry *entry;
882 drm_dma_handle_t *dmah;
885 unsigned long offset;
889 unsigned long *temp_pagelist;
890 struct drm_buf **temp_buflist;
892 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
898 if (!capable(CAP_SYS_ADMIN))
901 count = request->count;
902 order = order_base_2(request->size);
905 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
906 request->count, request->size, size, order);
908 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
911 alignment = (request->flags & _DRM_PAGE_ALIGN)
912 ? PAGE_ALIGN(size) : size;
913 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
914 total = PAGE_SIZE << page_order;
916 spin_lock(&dev->buf_lock);
918 spin_unlock(&dev->buf_lock);
921 atomic_inc(&dev->buf_alloc);
922 spin_unlock(&dev->buf_lock);
924 mutex_lock(&dev->struct_mutex);
925 entry = &dma->bufs[order];
926 if (entry->buf_count) {
927 mutex_unlock(&dev->struct_mutex);
928 atomic_dec(&dev->buf_alloc);
929 return -ENOMEM; /* May only call once for each order */
932 if (count < 0 || count > 4096) {
933 mutex_unlock(&dev->struct_mutex);
934 atomic_dec(&dev->buf_alloc);
938 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
939 if (!entry->buflist) {
940 mutex_unlock(&dev->struct_mutex);
941 atomic_dec(&dev->buf_alloc);
945 entry->seglist = kcalloc(count, sizeof(*entry->seglist), GFP_KERNEL);
946 if (!entry->seglist) {
947 kfree(entry->buflist);
948 mutex_unlock(&dev->struct_mutex);
949 atomic_dec(&dev->buf_alloc);
953 /* Keep the original pagelist until we know all the allocations
956 temp_pagelist = kmalloc_array(dma->page_count + (count << page_order),
957 sizeof(*dma->pagelist),
959 if (!temp_pagelist) {
960 kfree(entry->buflist);
961 kfree(entry->seglist);
962 mutex_unlock(&dev->struct_mutex);
963 atomic_dec(&dev->buf_alloc);
966 memcpy(temp_pagelist,
967 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
968 DRM_DEBUG("pagelist: %d entries\n",
969 dma->page_count + (count << page_order));
971 entry->buf_size = size;
972 entry->page_order = page_order;
976 while (entry->buf_count < count) {
978 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
981 /* Set count correctly so we free the proper amount. */
982 entry->buf_count = count;
983 entry->seg_count = count;
984 drm_cleanup_buf_error(dev, entry);
985 kfree(temp_pagelist);
986 mutex_unlock(&dev->struct_mutex);
987 atomic_dec(&dev->buf_alloc);
990 entry->seglist[entry->seg_count++] = dmah;
991 for (i = 0; i < (1 << page_order); i++) {
992 DRM_DEBUG("page %d @ 0x%08lx\n",
993 dma->page_count + page_count,
994 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
995 temp_pagelist[dma->page_count + page_count++]
996 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
999 offset + size <= total && entry->buf_count < count;
1000 offset += alignment, ++entry->buf_count) {
1001 buf = &entry->buflist[entry->buf_count];
1002 buf->idx = dma->buf_count + entry->buf_count;
1003 buf->total = alignment;
1006 buf->offset = (dma->byte_count + byte_count + offset);
1007 buf->address = (void *)(dmah->vaddr + offset);
1008 buf->bus_address = dmah->busaddr + offset;
1012 buf->file_priv = NULL;
1014 buf->dev_priv_size = dev->driver->dev_priv_size;
1015 buf->dev_private = kzalloc(buf->dev_priv_size,
1017 if (!buf->dev_private) {
1018 /* Set count correctly so we free the proper amount. */
1019 entry->buf_count = count;
1020 entry->seg_count = count;
1021 drm_cleanup_buf_error(dev, entry);
1022 kfree(temp_pagelist);
1023 mutex_unlock(&dev->struct_mutex);
1024 atomic_dec(&dev->buf_alloc);
1028 DRM_DEBUG("buffer %d @ %p\n",
1029 entry->buf_count, buf->address);
1031 byte_count += PAGE_SIZE << page_order;
1034 temp_buflist = krealloc(dma->buflist,
1035 (dma->buf_count + entry->buf_count) *
1036 sizeof(*dma->buflist), GFP_KERNEL);
1037 if (!temp_buflist) {
1038 /* Free the entry because it isn't valid */
1039 drm_cleanup_buf_error(dev, entry);
1040 kfree(temp_pagelist);
1041 mutex_unlock(&dev->struct_mutex);
1042 atomic_dec(&dev->buf_alloc);
1045 dma->buflist = temp_buflist;
1047 for (i = 0; i < entry->buf_count; i++) {
1048 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1051 /* No allocations failed, so now we can replace the original pagelist
1054 if (dma->page_count) {
1055 kfree(dma->pagelist);
1057 dma->pagelist = temp_pagelist;
1059 dma->buf_count += entry->buf_count;
1060 dma->seg_count += entry->seg_count;
1061 dma->page_count += entry->seg_count << page_order;
1062 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
1064 mutex_unlock(&dev->struct_mutex);
1066 request->count = entry->buf_count;
1067 request->size = size;
1069 if (request->flags & _DRM_PCI_BUFFER_RO)
1070 dma->flags = _DRM_DMA_USE_PCI_RO;
1072 atomic_dec(&dev->buf_alloc);
1076 EXPORT_SYMBOL(drm_legacy_addbufs_pci);
1078 static int drm_legacy_addbufs_sg(struct drm_device *dev,
1079 struct drm_buf_desc *request)
1081 struct drm_device_dma *dma = dev->dma;
1082 struct drm_buf_entry *entry;
1083 struct drm_buf *buf;
1084 unsigned long offset;
1085 unsigned long agp_offset;
1094 struct drm_buf **temp_buflist;
1096 if (!drm_core_check_feature(dev, DRIVER_SG))
1102 if (!capable(CAP_SYS_ADMIN))
1105 count = request->count;
1106 order = order_base_2(request->size);
1109 alignment = (request->flags & _DRM_PAGE_ALIGN)
1110 ? PAGE_ALIGN(size) : size;
1111 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1112 total = PAGE_SIZE << page_order;
1115 agp_offset = request->agp_start;
1117 DRM_DEBUG("count: %d\n", count);
1118 DRM_DEBUG("order: %d\n", order);
1119 DRM_DEBUG("size: %d\n", size);
1120 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1121 DRM_DEBUG("alignment: %d\n", alignment);
1122 DRM_DEBUG("page_order: %d\n", page_order);
1123 DRM_DEBUG("total: %d\n", total);
1125 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1128 spin_lock(&dev->buf_lock);
1130 spin_unlock(&dev->buf_lock);
1133 atomic_inc(&dev->buf_alloc);
1134 spin_unlock(&dev->buf_lock);
1136 mutex_lock(&dev->struct_mutex);
1137 entry = &dma->bufs[order];
1138 if (entry->buf_count) {
1139 mutex_unlock(&dev->struct_mutex);
1140 atomic_dec(&dev->buf_alloc);
1141 return -ENOMEM; /* May only call once for each order */
1144 if (count < 0 || count > 4096) {
1145 mutex_unlock(&dev->struct_mutex);
1146 atomic_dec(&dev->buf_alloc);
1150 entry->buflist = kcalloc(count, sizeof(*entry->buflist), GFP_KERNEL);
1151 if (!entry->buflist) {
1152 mutex_unlock(&dev->struct_mutex);
1153 atomic_dec(&dev->buf_alloc);
1157 entry->buf_size = size;
1158 entry->page_order = page_order;
1162 while (entry->buf_count < count) {
1163 buf = &entry->buflist[entry->buf_count];
1164 buf->idx = dma->buf_count + entry->buf_count;
1165 buf->total = alignment;
1169 buf->offset = (dma->byte_count + offset);
1170 buf->bus_address = agp_offset + offset;
1171 buf->address = (void *)(agp_offset + offset
1172 + (unsigned long)dev->sg->virtual);
1176 buf->file_priv = NULL;
1178 buf->dev_priv_size = dev->driver->dev_priv_size;
1179 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
1180 if (!buf->dev_private) {
1181 /* Set count correctly so we free the proper amount. */
1182 entry->buf_count = count;
1183 drm_cleanup_buf_error(dev, entry);
1184 mutex_unlock(&dev->struct_mutex);
1185 atomic_dec(&dev->buf_alloc);
1189 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1191 offset += alignment;
1193 byte_count += PAGE_SIZE << page_order;
1196 DRM_DEBUG("byte_count: %d\n", byte_count);
1198 temp_buflist = krealloc(dma->buflist,
1199 (dma->buf_count + entry->buf_count) *
1200 sizeof(*dma->buflist), GFP_KERNEL);
1201 if (!temp_buflist) {
1202 /* Free the entry because it isn't valid */
1203 drm_cleanup_buf_error(dev, entry);
1204 mutex_unlock(&dev->struct_mutex);
1205 atomic_dec(&dev->buf_alloc);
1208 dma->buflist = temp_buflist;
1210 for (i = 0; i < entry->buf_count; i++) {
1211 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1214 dma->buf_count += entry->buf_count;
1215 dma->seg_count += entry->seg_count;
1216 dma->page_count += byte_count >> PAGE_SHIFT;
1217 dma->byte_count += byte_count;
1219 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1220 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1222 mutex_unlock(&dev->struct_mutex);
1224 request->count = entry->buf_count;
1225 request->size = size;
1227 dma->flags = _DRM_DMA_USE_SG;
1229 atomic_dec(&dev->buf_alloc);
1234 * Add buffers for DMA transfers (ioctl).
1236 * \param inode device inode.
1237 * \param file_priv DRM file private.
1238 * \param cmd command.
1239 * \param arg pointer to a struct drm_buf_desc request.
1240 * \return zero on success or a negative number on failure.
1242 * According with the memory type specified in drm_buf_desc::flags and the
1243 * build options, it dispatches the call either to addbufs_agp(),
1244 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1245 * PCI memory respectively.
1247 int drm_legacy_addbufs(struct drm_device *dev, void *data,
1248 struct drm_file *file_priv)
1250 struct drm_buf_desc *request = data;
1253 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1256 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1259 #if IS_ENABLED(CONFIG_AGP)
1260 if (request->flags & _DRM_AGP_BUFFER)
1261 ret = drm_legacy_addbufs_agp(dev, request);
1264 if (request->flags & _DRM_SG_BUFFER)
1265 ret = drm_legacy_addbufs_sg(dev, request);
1266 else if (request->flags & _DRM_FB_BUFFER)
1269 ret = drm_legacy_addbufs_pci(dev, request);
1275 * Get information about the buffer mappings.
1277 * This was originally mean for debugging purposes, or by a sophisticated
1278 * client library to determine how best to use the available buffers (e.g.,
1279 * large buffers can be used for image transfer).
1281 * \param inode device inode.
1282 * \param file_priv DRM file private.
1283 * \param cmd command.
1284 * \param arg pointer to a drm_buf_info structure.
1285 * \return zero on success or a negative number on failure.
1287 * Increments drm_device::buf_use while holding the drm_device::buf_lock
1288 * lock, preventing of allocating more buffers after this call. Information
1289 * about each requested buffer is then copied into user space.
1291 int __drm_legacy_infobufs(struct drm_device *dev,
1293 int (*f)(void *, int, struct drm_buf_entry *))
1295 struct drm_device_dma *dma = dev->dma;
1299 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1302 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1308 spin_lock(&dev->buf_lock);
1309 if (atomic_read(&dev->buf_alloc)) {
1310 spin_unlock(&dev->buf_lock);
1313 ++dev->buf_use; /* Can't allocate more after this call */
1314 spin_unlock(&dev->buf_lock);
1316 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1317 if (dma->bufs[i].buf_count)
1321 DRM_DEBUG("count = %d\n", count);
1324 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1325 struct drm_buf_entry *from = &dma->bufs[i];
1326 if (from->buf_count) {
1327 if (f(data, count, from) < 0)
1329 DRM_DEBUG("%d %d %d %d %d\n",
1331 dma->bufs[i].buf_count,
1332 dma->bufs[i].buf_size,
1333 dma->bufs[i].low_mark,
1334 dma->bufs[i].high_mark);
1344 static int copy_one_buf(void *data, int count, struct drm_buf_entry *from)
1346 struct drm_buf_info *request = data;
1347 struct drm_buf_desc __user *to = &request->list[count];
1348 struct drm_buf_desc v = {.count = from->buf_count,
1349 .size = from->buf_size,
1350 .low_mark = from->low_mark,
1351 .high_mark = from->high_mark};
1353 if (copy_to_user(to, &v, offsetof(struct drm_buf_desc, flags)))
1358 int drm_legacy_infobufs(struct drm_device *dev, void *data,
1359 struct drm_file *file_priv)
1361 struct drm_buf_info *request = data;
1362 return __drm_legacy_infobufs(dev, data, &request->count, copy_one_buf);
1366 * Specifies a low and high water mark for buffer allocation
1368 * \param inode device inode.
1369 * \param file_priv DRM file private.
1370 * \param cmd command.
1371 * \param arg a pointer to a drm_buf_desc structure.
1372 * \return zero on success or a negative number on failure.
1374 * Verifies that the size order is bounded between the admissible orders and
1375 * updates the respective drm_device_dma::bufs entry low and high water mark.
1377 * \note This ioctl is deprecated and mostly never used.
1379 int drm_legacy_markbufs(struct drm_device *dev, void *data,
1380 struct drm_file *file_priv)
1382 struct drm_device_dma *dma = dev->dma;
1383 struct drm_buf_desc *request = data;
1385 struct drm_buf_entry *entry;
1387 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1390 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1396 DRM_DEBUG("%d, %d, %d\n",
1397 request->size, request->low_mark, request->high_mark);
1398 order = order_base_2(request->size);
1399 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1401 entry = &dma->bufs[order];
1403 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1405 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1408 entry->low_mark = request->low_mark;
1409 entry->high_mark = request->high_mark;
1415 * Unreserve the buffers in list, previously reserved using drmDMA.
1417 * \param inode device inode.
1418 * \param file_priv DRM file private.
1419 * \param cmd command.
1420 * \param arg pointer to a drm_buf_free structure.
1421 * \return zero on success or a negative number on failure.
1423 * Calls free_buffer() for each used buffer.
1424 * This function is primarily used for debugging.
1426 int drm_legacy_freebufs(struct drm_device *dev, void *data,
1427 struct drm_file *file_priv)
1429 struct drm_device_dma *dma = dev->dma;
1430 struct drm_buf_free *request = data;
1433 struct drm_buf *buf;
1435 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1438 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1444 DRM_DEBUG("%d\n", request->count);
1445 for (i = 0; i < request->count; i++) {
1446 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1448 if (idx < 0 || idx >= dma->buf_count) {
1449 DRM_ERROR("Index %d (of %d max)\n",
1450 idx, dma->buf_count - 1);
1453 idx = array_index_nospec(idx, dma->buf_count);
1454 buf = dma->buflist[idx];
1455 if (buf->file_priv != file_priv) {
1456 DRM_ERROR("Process %d freeing buffer not owned\n",
1457 task_pid_nr(current));
1460 drm_legacy_free_buffer(dev, buf);
1467 * Maps all of the DMA buffers into client-virtual space (ioctl).
1469 * \param inode device inode.
1470 * \param file_priv DRM file private.
1471 * \param cmd command.
1472 * \param arg pointer to a drm_buf_map structure.
1473 * \return zero on success or a negative number on failure.
1475 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1476 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1477 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1480 int __drm_legacy_mapbufs(struct drm_device *dev, void *data, int *p,
1482 int (*f)(void *, int, unsigned long,
1484 struct drm_file *file_priv)
1486 struct drm_device_dma *dma = dev->dma;
1488 unsigned long virtual;
1491 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1494 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1500 spin_lock(&dev->buf_lock);
1501 if (atomic_read(&dev->buf_alloc)) {
1502 spin_unlock(&dev->buf_lock);
1505 dev->buf_use++; /* Can't allocate more after this call */
1506 spin_unlock(&dev->buf_lock);
1508 if (*p >= dma->buf_count) {
1509 if ((dev->agp && (dma->flags & _DRM_DMA_USE_AGP))
1510 || (drm_core_check_feature(dev, DRIVER_SG)
1511 && (dma->flags & _DRM_DMA_USE_SG))) {
1512 struct drm_local_map *map = dev->agp_buffer_map;
1513 unsigned long token = dev->agp_buffer_token;
1519 virtual = vm_mmap(file_priv->filp, 0, map->size,
1520 PROT_READ | PROT_WRITE,
1524 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
1525 PROT_READ | PROT_WRITE,
1528 if (virtual > -1024UL) {
1530 retcode = (signed long)virtual;
1533 *v = (void __user *)virtual;
1535 for (i = 0; i < dma->buf_count; i++) {
1536 if (f(data, i, virtual, dma->buflist[i]) < 0) {
1543 *p = dma->buf_count;
1544 DRM_DEBUG("%d buffers, retcode = %d\n", *p, retcode);
1549 static int map_one_buf(void *data, int idx, unsigned long virtual,
1550 struct drm_buf *buf)
1552 struct drm_buf_map *request = data;
1553 unsigned long address = virtual + buf->offset; /* *** */
1555 if (copy_to_user(&request->list[idx].idx, &buf->idx,
1556 sizeof(request->list[0].idx)))
1558 if (copy_to_user(&request->list[idx].total, &buf->total,
1559 sizeof(request->list[0].total)))
1561 if (clear_user(&request->list[idx].used, sizeof(int)))
1563 if (copy_to_user(&request->list[idx].address, &address,
1569 int drm_legacy_mapbufs(struct drm_device *dev, void *data,
1570 struct drm_file *file_priv)
1572 struct drm_buf_map *request = data;
1573 return __drm_legacy_mapbufs(dev, data, &request->count,
1574 &request->virtual, map_one_buf,
1578 int drm_legacy_dma_ioctl(struct drm_device *dev, void *data,
1579 struct drm_file *file_priv)
1581 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1584 if (dev->driver->dma_ioctl)
1585 return dev->driver->dma_ioctl(dev, data, file_priv);
1590 struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev)
1592 struct drm_map_list *entry;
1594 list_for_each_entry(entry, &dev->maplist, head) {
1595 if (entry->map && entry->map->type == _DRM_SHM &&
1596 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1602 EXPORT_SYMBOL(drm_legacy_getsarea);