radeon_bufs.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:9k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* radeon_bufs.c -- IOCTLs to manage buffers -*- linux-c -*-
  2.  *
  3.  * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
  4.  * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
  5.  * All Rights Reserved.
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a
  8.  * copy of this software and associated documentation files (the "Software"),
  9.  * to deal in the Software without restriction, including without limitation
  10.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11.  * and/or sell copies of the Software, and to permit persons to whom the
  12.  * Software is furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the next
  15.  * paragraph) shall be included in all copies or substantial portions of the
  16.  * Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  21.  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24.  * DEALINGS IN THE SOFTWARE.
  25.  *
  26.  * Authors: Kevin E. Martin <martin@valinux.com>
  27.  *          Rickard E. (Rik) Faith <faith@valinux.com>
  28.  *     Jeff Hartmann <jhartmann@valinux.com>
  29.  *
  30.  */
  31. #define __NO_VERSION__
  32. #include <linux/config.h>
  33. #include "drmP.h"
  34. #include "radeon_drv.h"
  35. #include "linux/un.h"
  36. #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
  37. int radeon_addbufs_agp(struct inode *inode, struct file *filp,
  38.        unsigned int cmd, unsigned long arg)
  39. {
  40. drm_file_t       *priv = filp->private_data;
  41. drm_device_t     *dev  = priv->dev;
  42. drm_device_dma_t *dma  = dev->dma;
  43. drm_buf_desc_t    request;
  44. drm_buf_entry_t  *entry;
  45. drm_buf_t        *buf;
  46. unsigned long     offset;
  47. unsigned long     agp_offset;
  48. int               count;
  49. int               order;
  50. int               size;
  51. int               alignment;
  52. int               page_order;
  53. int               total;
  54. int               byte_count;
  55. int               i;
  56. if (!dma) return -EINVAL;
  57. if (copy_from_user(&request, (drm_buf_desc_t *)arg, sizeof(request)))
  58. return -EFAULT;
  59. count      = request.count;
  60. order      = drm_order(request.size);
  61. size       = 1 << order;
  62. alignment  = (request.flags & _DRM_PAGE_ALIGN) ? PAGE_ALIGN(size):size;
  63. page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
  64. total      = PAGE_SIZE << page_order;
  65. byte_count = 0;
  66. agp_offset = dev->agp->base + request.agp_start;
  67. DRM_DEBUG("count:      %dn",  count);
  68. DRM_DEBUG("order:      %dn",  order);
  69. DRM_DEBUG("size:       %dn",  size);
  70. DRM_DEBUG("agp_offset: %ldn", agp_offset);
  71. DRM_DEBUG("alignment:  %dn",  alignment);
  72. DRM_DEBUG("page_order: %dn",  page_order);
  73. DRM_DEBUG("total:      %dn",  total);
  74. if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) return -EINVAL;
  75. if (dev->queue_count) return -EBUSY; /* Not while in use */
  76. spin_lock(&dev->count_lock);
  77. if (dev->buf_use) {
  78. spin_unlock(&dev->count_lock);
  79. return -EBUSY;
  80. }
  81. atomic_inc(&dev->buf_alloc);
  82. spin_unlock(&dev->count_lock);
  83. down(&dev->struct_sem);
  84. entry = &dma->bufs[order];
  85. if (entry->buf_count) {
  86. up(&dev->struct_sem);
  87. atomic_dec(&dev->buf_alloc);
  88. return -ENOMEM; /* May only call once for each order */
  89. }
  90. /* Might be too low a limit. XFree folks need to fix this properly */
  91. if(count < 0 || count > 4096)
  92. {
  93. up(&dev->struct_sem);
  94. atomic_dec(&dev->buf_alloc);
  95. return -EINVAL;
  96. }
  97. entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
  98.    DRM_MEM_BUFS);
  99. if (!entry->buflist) {
  100. up(&dev->struct_sem);
  101. atomic_dec(&dev->buf_alloc);
  102. return -ENOMEM;
  103. }
  104. memset(entry->buflist, 0, count * sizeof(*entry->buflist));
  105. entry->buf_size   = size;
  106. entry->page_order = page_order;
  107. offset            = 0;
  108. for (offset = 0;
  109.      entry->buf_count < count;
  110.      offset += alignment, ++entry->buf_count) {
  111. buf          = &entry->buflist[entry->buf_count];
  112. buf->idx     = dma->buf_count + entry->buf_count;
  113. buf->total   = alignment;
  114. buf->order   = order;
  115. buf->used    = 0;
  116. buf->offset  = (dma->byte_count + offset);
  117. buf->address = (void *)(agp_offset + offset);
  118. buf->next    = NULL;
  119. buf->waiting = 0;
  120. buf->pending = 0;
  121. init_waitqueue_head(&buf->dma_wait);
  122. buf->pid     = 0;
  123. buf->dev_priv_size = sizeof(drm_radeon_buf_priv_t);
  124. buf->dev_private   = drm_alloc(sizeof(drm_radeon_buf_priv_t),
  125.        DRM_MEM_BUFS);
  126.                 if (!buf->dev_private) {
  127.                         up(&dev->struct_sem);
  128.                         atomic_dec(&dev->buf_alloc);
  129.                         return -ENOMEM;
  130.                 }
  131. memset(buf->dev_private, 0, buf->dev_priv_size);
  132. #if DRM_DMA_HISTOGRAM
  133. buf->time_queued     = 0;
  134. buf->time_dispatched = 0;
  135. buf->time_completed  = 0;
  136. buf->time_freed      = 0;
  137. #endif
  138. byte_count += PAGE_SIZE << page_order;
  139. DRM_DEBUG("buffer %d @ %pn",
  140.   entry->buf_count, buf->address);
  141. }
  142. DRM_DEBUG("byte_count: %dn", byte_count);
  143. dma->buflist = drm_realloc(dma->buflist,
  144.    dma->buf_count * sizeof(*dma->buflist),
  145.    (dma->buf_count + entry->buf_count)
  146.    * sizeof(*dma->buflist),
  147.    DRM_MEM_BUFS);
  148. for (i = dma->buf_count; i < dma->buf_count + entry->buf_count; i++)
  149. dma->buflist[i] = &entry->buflist[i - dma->buf_count];
  150. dma->buf_count  += entry->buf_count;
  151. dma->byte_count += byte_count;
  152. drm_freelist_create(&entry->freelist, entry->buf_count);
  153. for (i = 0; i < entry->buf_count; i++) {
  154. drm_freelist_put(dev, &entry->freelist, &entry->buflist[i]);
  155. }
  156. up(&dev->struct_sem);
  157. request.count = entry->buf_count;
  158. request.size  = size;
  159. if (copy_to_user((drm_buf_desc_t *)arg, &request, sizeof(request)))
  160. return -EFAULT;
  161. dma->flags = _DRM_DMA_USE_AGP;
  162. atomic_dec(&dev->buf_alloc);
  163. return 0;
  164. }
  165. #endif
  166. int radeon_addbufs(struct inode *inode, struct file *filp, unsigned int cmd,
  167.    unsigned long arg)
  168. {
  169. drm_file_t *priv = filp->private_data;
  170. drm_device_t *dev = priv->dev;
  171. drm_radeon_private_t *dev_priv = dev->dev_private;
  172. drm_buf_desc_t request;
  173. if (!dev_priv || dev_priv->is_pci) return -EINVAL;
  174. if (copy_from_user(&request, (drm_buf_desc_t *)arg, sizeof(request)))
  175. return -EFAULT;
  176. #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
  177. if (request.flags & _DRM_AGP_BUFFER)
  178. return radeon_addbufs_agp(inode, filp, cmd, arg);
  179. else
  180. #endif
  181. return -EINVAL;
  182. }
  183. int radeon_mapbufs(struct inode *inode, struct file *filp, unsigned int cmd,
  184.    unsigned long arg)
  185. {
  186. drm_file_t *priv = filp->private_data;
  187. drm_device_t *dev = priv->dev;
  188. drm_radeon_private_t *dev_priv = dev->dev_private;
  189. drm_device_dma_t *dma = dev->dma;
  190. int  retcode = 0;
  191. const int  zero = 0;
  192. unsigned long  virtual;
  193. unsigned long  address;
  194. drm_buf_map_t  request;
  195. int  i;
  196. if (!dma || !dev_priv || dev_priv->is_pci) return -EINVAL;
  197. DRM_DEBUG("n");
  198. spin_lock(&dev->count_lock);
  199. if (atomic_read(&dev->buf_alloc)) {
  200. spin_unlock(&dev->count_lock);
  201. return -EBUSY;
  202. }
  203. ++dev->buf_use; /* Can't allocate more after this call */
  204. spin_unlock(&dev->count_lock);
  205. if (copy_from_user(&request, (drm_buf_map_t *)arg, sizeof(request)))
  206. return -EFAULT;
  207. if (request.count >= dma->buf_count) {
  208. if (dma->flags & _DRM_DMA_USE_AGP) {
  209. drm_map_t *map;
  210. map = dev_priv->buffers;
  211. if (!map) {
  212. retcode = -EINVAL;
  213. goto done;
  214. }
  215. down_write(&current->mm->mmap_sem);
  216. virtual = do_mmap(filp, 0, map->size,
  217.   PROT_READ|PROT_WRITE,
  218.   MAP_SHARED,
  219.   (unsigned long)map->offset);
  220. up_write(&current->mm->mmap_sem);
  221. } else {
  222. down_write(&current->mm->mmap_sem);
  223. virtual = do_mmap(filp, 0, dma->byte_count,
  224.   PROT_READ|PROT_WRITE, MAP_SHARED, 0);
  225. up_write(&current->mm->mmap_sem);
  226. }
  227. if (virtual > -1024UL) {
  228. /* Real error */
  229. retcode = (signed long)virtual;
  230. goto done;
  231. }
  232. request.virtual = (void *)virtual;
  233. for (i = 0; i < dma->buf_count; i++) {
  234. if (copy_to_user(&request.list[i].idx,
  235.  &dma->buflist[i]->idx,
  236.  sizeof(request.list[0].idx))) {
  237. retcode = -EFAULT;
  238. goto done;
  239. }
  240. if (copy_to_user(&request.list[i].total,
  241.  &dma->buflist[i]->total,
  242.  sizeof(request.list[0].total))) {
  243. retcode = -EFAULT;
  244. goto done;
  245. }
  246. if (copy_to_user(&request.list[i].used,
  247.  &zero,
  248.  sizeof(zero))) {
  249. retcode = -EFAULT;
  250. goto done;
  251. }
  252. address = virtual + dma->buflist[i]->offset;
  253. if (copy_to_user(&request.list[i].address,
  254.  &address,
  255.  sizeof(address))) {
  256. retcode = -EFAULT;
  257. goto done;
  258. }
  259. }
  260. }
  261.  done:
  262. request.count = dma->buf_count;
  263. DRM_DEBUG("%d buffers, retcode = %dn", request.count, retcode);
  264. if (copy_to_user((drm_buf_map_t *)arg, &request, sizeof(request)))
  265. return -EFAULT;
  266. return retcode;
  267. }