drm_bufs.c
来自「linux 内核源代码」· C语言 代码 · 共 1,597 行 · 第 1/3 页
C
1,597 行
/** * \file drm_bufs.c * Generic buffer template * * \author Rickard E. (Rik) Faith <faith@valinux.com> * \author Gareth Hughes <gareth@valinux.com> *//* * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com * * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */#include <linux/vmalloc.h>#include "drmP.h"unsigned long drm_get_resource_start(struct drm_device *dev, unsigned int resource){ return pci_resource_start(dev->pdev, resource);}EXPORT_SYMBOL(drm_get_resource_start);unsigned long drm_get_resource_len(struct drm_device *dev, unsigned int resource){ return pci_resource_len(dev->pdev, resource);}EXPORT_SYMBOL(drm_get_resource_len);static struct drm_map_list *drm_find_matching_map(struct drm_device *dev, drm_local_map_t *map){ struct drm_map_list *entry; list_for_each_entry(entry, &dev->maplist, head) { if (entry->map && map->type == entry->map->type && ((entry->map->offset == map->offset) || (map->type == _DRM_SHM && map->flags==_DRM_CONTAINS_LOCK))) { return entry; } } return NULL;}static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash, unsigned long user_token, int hashed_handle){ int use_hashed_handle;#if (BITS_PER_LONG == 64) use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);#elif (BITS_PER_LONG == 32) use_hashed_handle = hashed_handle;#else#error Unsupported long size. Neither 64 nor 32 bits.#endif if (!use_hashed_handle) { int ret; hash->key = user_token >> PAGE_SHIFT; ret = drm_ht_insert_item(&dev->map_hash, hash); if (ret != -EINVAL) return ret; } return drm_ht_just_insert_please(&dev->map_hash, hash, user_token, 32 - PAGE_SHIFT - 3, 0, DRM_MAP_HASH_OFFSET >> PAGE_SHIFT);}/** * Ioctl to specify a range of memory that is available for mapping by a non-root process. * * \param inode device inode. * \param file_priv DRM file private. * \param cmd command. * \param arg pointer to a drm_map structure. * \return zero on success or a negative value on error. * * Adjusts the memory offset to its absolute value according to the mapping * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where * applicable and if supported by the kernel. */static int drm_addmap_core(struct drm_device * dev, unsigned int offset, unsigned int size, enum drm_map_type type, enum drm_map_flags flags, struct drm_map_list ** maplist){ struct drm_map *map; struct drm_map_list *list; drm_dma_handle_t *dmah; unsigned long user_token; int ret; map = drm_alloc(sizeof(*map), DRM_MEM_MAPS); if (!map) return -ENOMEM; map->offset = offset; map->size = size; map->flags = flags; map->type = type; /* Only allow shared memory to be removable since we only keep enough * book keeping information about shared memory to allow for removal * when processes fork. */ if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) { drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -EINVAL; } DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n", map->offset, map->size, map->type); if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) { drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -EINVAL; } map->mtrr = -1; map->handle = NULL; switch (map->type) { case _DRM_REGISTERS: case _DRM_FRAME_BUFFER:#if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) if (map->offset + (map->size-1) < map->offset || map->offset < virt_to_phys(high_memory)) { drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -EINVAL; }#endif#ifdef __alpha__ map->offset += dev->hose->mem_space->start;#endif /* Some drivers preinitialize some maps, without the X Server * needing to be aware of it. Therefore, we just return success * when the server tries to create a duplicate map. */ list = drm_find_matching_map(dev, map); if (list != NULL) { if (list->map->size != map->size) { DRM_DEBUG("Matching maps of type %d with " "mismatched sizes, (%ld vs %ld)\n", map->type, map->size, list->map->size); list->map->size = map->size; } drm_free(map, sizeof(*map), DRM_MEM_MAPS); *maplist = list; return 0; } if (drm_core_has_MTRR(dev)) { if (map->type == _DRM_FRAME_BUFFER || (map->flags & _DRM_WRITE_COMBINING)) { map->mtrr = mtrr_add(map->offset, map->size, MTRR_TYPE_WRCOMB, 1); } } if (map->type == _DRM_REGISTERS) { map->handle = ioremap(map->offset, map->size); if (!map->handle) { drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -ENOMEM; } } break; case _DRM_SHM: list = drm_find_matching_map(dev, map); if (list != NULL) { if(list->map->size != map->size) { DRM_DEBUG("Matching maps of type %d with " "mismatched sizes, (%ld vs %ld)\n", map->type, map->size, list->map->size); list->map->size = map->size; } drm_free(map, sizeof(*map), DRM_MEM_MAPS); *maplist = list; return 0; } map->handle = vmalloc_user(map->size); DRM_DEBUG("%lu %d %p\n", map->size, drm_order(map->size), map->handle); if (!map->handle) { drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -ENOMEM; } map->offset = (unsigned long)map->handle; if (map->flags & _DRM_CONTAINS_LOCK) { /* Prevent a 2nd X Server from creating a 2nd lock */ if (dev->lock.hw_lock != NULL) { vfree(map->handle); drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -EBUSY; } dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */ } break; case _DRM_AGP: { struct drm_agp_mem *entry; int valid = 0; if (!drm_core_has_AGP(dev)) { drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -EINVAL; }#ifdef __alpha__ map->offset += dev->hose->mem_space->start;#endif /* Note: dev->agp->base may actually be 0 when the DRM * is not in control of AGP space. But if user space is * it should already have added the AGP base itself. */ map->offset += dev->agp->base; map->mtrr = dev->agp->agp_mtrr; /* for getmap */ /* This assumes the DRM is in total control of AGP space. * It's not always the case as AGP can be in the control * of user space (i.e. i810 driver). So this loop will get * skipped and we double check that dev->agp->memory is * actually set as well as being invalid before EPERM'ing */ list_for_each_entry(entry, &dev->agp->memory, head) { if ((map->offset >= entry->bound) && (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) { valid = 1; break; } } if (!list_empty(&dev->agp->memory) && !valid) { drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -EPERM; } DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size); break; } case _DRM_SCATTER_GATHER: if (!dev->sg) { drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -EINVAL; } map->offset += (unsigned long)dev->sg->virtual; break; case _DRM_CONSISTENT: /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G, * As we're limiting the address to 2^32-1 (or less), * casting it down to 32 bits is no problem, but we * need to point to a 64bit variable first. */ dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL); if (!dmah) { drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -ENOMEM; } map->handle = dmah->vaddr; map->offset = (unsigned long)dmah->busaddr; kfree(dmah); break; default: drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -EINVAL; } list = drm_alloc(sizeof(*list), DRM_MEM_MAPS); if (!list) { if (map->type == _DRM_REGISTERS) iounmap(map->handle); drm_free(map, sizeof(*map), DRM_MEM_MAPS); return -EINVAL; } memset(list, 0, sizeof(*list)); list->map = map; mutex_lock(&dev->struct_mutex); list_add(&list->head, &dev->maplist); /* Assign a 32-bit handle */ /* We do it here so that dev->struct_mutex protects the increment */ user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle : map->offset; ret = drm_map_handle(dev, &list->hash, user_token, 0); if (ret) { if (map->type == _DRM_REGISTERS) iounmap(map->handle); drm_free(map, sizeof(*map), DRM_MEM_MAPS); drm_free(list, sizeof(*list), DRM_MEM_MAPS); mutex_unlock(&dev->struct_mutex); return ret; } list->user_token = list->hash.key << PAGE_SHIFT; mutex_unlock(&dev->struct_mutex); *maplist = list; return 0; }int drm_addmap(struct drm_device * dev, unsigned int offset, unsigned int size, enum drm_map_type type, enum drm_map_flags flags, drm_local_map_t ** map_ptr){ struct drm_map_list *list; int rc; rc = drm_addmap_core(dev, offset, size, type, flags, &list); if (!rc) *map_ptr = list->map; return rc;}EXPORT_SYMBOL(drm_addmap);int drm_addmap_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv){ struct drm_map *map = data; struct drm_map_list *maplist; int err; if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP)) return -EPERM; err = drm_addmap_core(dev, map->offset, map->size, map->type, map->flags, &maplist); if (err) return err; /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */ map->handle = (void *)(unsigned long)maplist->user_token; return 0;}/** * Remove a map private from list and deallocate resources if the mapping * isn't in use. * * \param inode device inode. * \param file_priv DRM file private. * \param cmd command. * \param arg pointer to a struct drm_map structure. * \return zero on success or a negative value on error. * * Searches the map on drm_device::maplist, removes it from the list, see if * its being used, and free any associate resource (such as MTRR's) if it's not * being on use. * * \sa drm_addmap */int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map){ struct drm_map_list *r_list = NULL, *list_t; drm_dma_handle_t dmah; int found = 0; /* Find the list entry for the map and remove it */ list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) { if (r_list->map == map) { list_del(&r_list->head); drm_ht_remove_key(&dev->map_hash, r_list->user_token >> PAGE_SHIFT); drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS); found = 1; break; } } if (!found) return -EINVAL; switch (map->type) { case _DRM_REGISTERS: iounmap(map->handle); /* FALLTHROUGH */ case _DRM_FRAME_BUFFER: if (drm_core_has_MTRR(dev) && map->mtrr >= 0) { int retcode; retcode = mtrr_del(map->mtrr, map->offset, map->size); DRM_DEBUG("mtrr_del=%d\n", retcode); } break; case _DRM_SHM: vfree(map->handle); break; case _DRM_AGP: case _DRM_SCATTER_GATHER: break; case _DRM_CONSISTENT: dmah.vaddr = map->handle; dmah.busaddr = map->offset; dmah.size = map->size; __drm_pci_free(dev, &dmah); break; } drm_free(map, sizeof(*map), DRM_MEM_MAPS); return 0;}int drm_rmmap(struct drm_device *dev, drm_local_map_t *map){ int ret; mutex_lock(&dev->struct_mutex); ret = drm_rmmap_locked(dev, map); mutex_unlock(&dev->struct_mutex); return ret;}/* The rmmap ioctl appears to be unnecessary. All mappings are torn down on * the last close of the device, and this is necessary for cleanup when things * exit uncleanly. Therefore, having userland manually remove mappings seems * like a pointless exercise since they're going away anyway. * * One use case might be after addmap is allowed for normal users for SHM and * gets used by drivers that the server doesn't need to care about. This seems * unlikely. */int drm_rmmap_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv){ struct drm_map *request = data; drm_local_map_t *map = NULL; struct drm_map_list *r_list; int ret; mutex_lock(&dev->struct_mutex); list_for_each_entry(r_list, &dev->maplist, head) { if (r_list->map && r_list->user_token == (unsigned long)request->handle && r_list->map->flags & _DRM_REMOVABLE) { map = r_list->map; break; } } /* List has wrapped around to the head pointer, or its empty we didn't * find anything. */ if (list_empty(&dev->maplist) || !map) { mutex_unlock(&dev->struct_mutex); return -EINVAL; } /* Register and framebuffer maps are permanent */ if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) { mutex_unlock(&dev->struct_mutex); return 0; } ret = drm_rmmap_locked(dev, map); mutex_unlock(&dev->struct_mutex); return ret;}/** * Cleanup after an error on one of the addbufs() functions. * * \param dev DRM device. * \param entry buffer entry where the error occurred. * * Frees any pages and buffers associated with the given entry. */static void drm_cleanup_buf_error(struct drm_device * dev, struct drm_buf_entry * entry){ int i; if (entry->seg_count) { for (i = 0; i < entry->seg_count; i++) { if (entry->seglist[i]) { drm_pci_free(dev, entry->seglist[i]); } } drm_free(entry->seglist, entry->seg_count * sizeof(*entry->seglist), DRM_MEM_SEGS); entry->seg_count = 0; } if (entry->buf_count) { for (i = 0; i < entry->buf_count; i++) { if (entry->buflist[i].dev_private) { drm_free(entry->buflist[i].dev_private, entry->buflist[i].dev_priv_size, DRM_MEM_BUFS); } } drm_free(entry->buflist, entry->buf_count * sizeof(*entry->buflist), DRM_MEM_BUFS); entry->buf_count = 0; }}#if __OS_HAS_AGP/** * Add AGP buffers for DMA transfers. * * \param dev struct drm_device to which the buffers are to be added. * \param request pointer to a struct drm_buf_desc describing the request. * \return zero on success or a negative number on failure. * * After some sanity checks creates a drm_buf structure for each buffer and * reallocates the buffer list of the same size order to accommodate the new * buffers.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?