⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drm_bufs.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 3 页
字号:
/** * \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(drm_device_t * dev, unsigned int resource){	return pci_resource_start(dev->pdev, resource);}EXPORT_SYMBOL(drm_get_resource_start);unsigned long drm_get_resource_len(drm_device_t * dev, unsigned int resource){	return pci_resource_len(dev->pdev, resource);}EXPORT_SYMBOL(drm_get_resource_len);static drm_map_list_t *drm_find_matching_map(drm_device_t * dev,					     drm_local_map_t * map){	struct list_head *list;	list_for_each(list, &dev->maplist->head) {		drm_map_list_t *entry = list_entry(list, drm_map_list_t, head);		if (entry->map && map->type == entry->map->type &&		    entry->map->offset == map->offset) {			return entry;		}	}	return NULL;}/* * Used to allocate 32-bit handles for mappings. */#define START_RANGE 0x10000000#define END_RANGE 0x40000000#ifdef _LP64static __inline__ unsigned int HandleID(unsigned long lhandle,					drm_device_t * dev){	static unsigned int map32_handle = START_RANGE;	unsigned int hash;	if (lhandle & 0xffffffff00000000) {		hash = map32_handle;		map32_handle += PAGE_SIZE;		if (map32_handle > END_RANGE)			map32_handle = START_RANGE;	} else		hash = lhandle;	while (1) {		drm_map_list_t *_entry;		list_for_each_entry(_entry, &dev->maplist->head, head) {			if (_entry->user_token == hash)				break;		}		if (&_entry->head == &dev->maplist->head)			return hash;		hash += PAGE_SIZE;		map32_handle += PAGE_SIZE;	}}#else# define HandleID(x,dev) (unsigned int)(x)#endif/** * Ioctl to specify a range of memory that is available for mapping by a non-root process. * * \param inode device inode. * \param filp file pointer. * \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(drm_device_t * dev, unsigned int offset,			   unsigned int size, drm_map_type_t type,			   drm_map_flags_t flags, drm_map_list_t ** maplist){	drm_map_t *map;	drm_map_list_t *list;	drm_dma_handle_t *dmah;	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 < 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 = drm_ioremap(map->offset, map->size, dev);		break;	case _DRM_SHM:		map->handle = vmalloc_32(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:		if (drm_core_has_AGP(dev)) {#ifdef __alpha__			map->offset += dev->hose->mem_space->start;#endif			map->offset += dev->agp->base;			map->mtrr = dev->agp->agp_mtrr;	/* for getmap */		}		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) {		drm_free(map, sizeof(*map), DRM_MEM_MAPS);		return -EINVAL;	}	memset(list, 0, sizeof(*list));	list->map = map;	down(&dev->struct_sem);	list_add(&list->head, &dev->maplist->head);	/* Assign a 32-bit handle */	/* We do it here so that dev->struct_sem protects the increment */	list->user_token = HandleID(map->type == _DRM_SHM				    ? (unsigned long)map->handle				    : map->offset, dev);	up(&dev->struct_sem);	*maplist = list;	return 0;}int drm_addmap(drm_device_t * dev, unsigned int offset,	       unsigned int size, drm_map_type_t type,	       drm_map_flags_t flags, drm_local_map_t ** map_ptr){	drm_map_list_t *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 inode *inode, struct file *filp,		     unsigned int cmd, unsigned long arg){	drm_file_t *priv = filp->private_data;	drm_device_t *dev = priv->head->dev;	drm_map_t map;	drm_map_list_t *maplist;	drm_map_t __user *argp = (void __user *)arg;	int err;	if (!(filp->f_mode & 3))		return -EACCES;	/* Require read/write */	if (copy_from_user(&map, argp, sizeof(map))) {		return -EFAULT;	}	err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,			      &maplist);	if (err)		return err;	if (copy_to_user(argp, maplist->map, sizeof(drm_map_t)))		return -EFAULT;	/* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */	if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))		return -EFAULT;	return 0;}/** * Remove a map private from list and deallocate resources if the mapping * isn't in use. * * \param inode device inode. * \param filp file pointer. * \param cmd command. * \param arg pointer to a drm_map_t 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(drm_device_t * dev, drm_local_map_t * map){	struct list_head *list;	drm_map_list_t *r_list = NULL;	drm_dma_handle_t dmah;	/* Find the list entry for the map and remove it */	list_for_each(list, &dev->maplist->head) {		r_list = list_entry(list, drm_map_list_t, head);		if (r_list->map == map) {			list_del(list);			drm_free(list, sizeof(*list), DRM_MEM_MAPS);			break;		}	}	/* List has wrapped around to the head pointer, or it's empty and we	 * didn't find anything.	 */	if (list == (&dev->maplist->head)) {		return -EINVAL;	}	switch (map->type) {	case _DRM_REGISTERS:		drm_ioremapfree(map->handle, map->size, dev);		/* 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;}EXPORT_SYMBOL(drm_rmmap_locked);int drm_rmmap(drm_device_t * dev, drm_local_map_t * map){	int ret;	down(&dev->struct_sem);	ret = drm_rmmap_locked(dev, map);	up(&dev->struct_sem);	return ret;}EXPORT_SYMBOL(drm_rmmap);/* 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 inode *inode, struct file *filp,		    unsigned int cmd, unsigned long arg){	drm_file_t *priv = filp->private_data;	drm_device_t *dev = priv->head->dev;	drm_map_t request;	drm_local_map_t *map = NULL;	struct list_head *list;	int ret;	if (copy_from_user(&request, (drm_map_t __user *) arg, sizeof(request))) {		return -EFAULT;	}	down(&dev->struct_sem);	list_for_each(list, &dev->maplist->head) {		drm_map_list_t *r_list = list_entry(list, drm_map_list_t, 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 == (&dev->maplist->head)) {		up(&dev->struct_sem);		return -EINVAL;	}	if (!map)		return -EINVAL;	/* Register and framebuffer maps are permanent */	if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {		up(&dev->struct_sem);		return 0;	}	ret = drm_rmmap_locked(dev, map);	up(&dev->struct_sem);	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(drm_device_t * dev, drm_buf_entry_t * entry){	int i;	if (entry->seg_count) {		for (i = 0; i < entry->seg_count; i++) {			if (entry->seglist[i]) {				drm_free_pages(entry->seglist[i],					       entry->page_order, DRM_MEM_DMA);			}		}		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 drm_device_t to which the buffers are to be added. * \param request pointer to a drm_buf_desc_t 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. */int drm_addbufs_agp(drm_device_t * dev, drm_buf_desc_t * request){	drm_device_dma_t *dma = dev->dma;	drm_buf_entry_t *entry;	drm_buf_t *buf;	unsigned long offset;	unsigned long agp_offset;	int count;	int order;	int size;	int alignment;	int page_order;	int total;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -