1 /*- 2 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. 3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: 26 * Rickard E. (Rik) Faith <faith (at) valinux.com> 27 * Gareth Hughes <gareth (at) valinux.com> 28 * 29 */ 30 31 /** @file drm_dma.c 32 * Support code for DMA buffer management. 33 * 34 * The implementation used to be significantly more complicated, but the 35 * complexity has been moved into the drivers as different buffer management 36 * schemes evolved. 37 */ 38 39 #include "drmP.h" 40 41 int drm_dma_setup(struct drm_device *dev) 42 { 43 44 dev->dma = malloc(sizeof(*dev->dma), DRM_MEM_DRIVER, M_NOWAIT | M_ZERO); 45 if (dev->dma == NULL) 46 return ENOMEM; 47 48 DRM_SPININIT(&dev->dma_lock, "drmdma"); 49 50 return 0; 51 } 52 53 void drm_dma_takedown(struct drm_device *dev) 54 { 55 drm_device_dma_t *dma = dev->dma; 56 int i, j; 57 58 if (dma == NULL) 59 return; 60 61 /* Clear dma buffers */ 62 for (i = 0; i <= DRM_MAX_ORDER; i++) { 63 if (dma->bufs[i].seg_count) { 64 DRM_DEBUG("order %d: buf_count = %d," 65 " seg_count = %d\n", i, dma->bufs[i].buf_count, 66 dma->bufs[i].seg_count); 67 for (j = 0; j < dma->bufs[i].seg_count; j++) { 68 drm_pci_free(dev, dma->bufs[i].seglist[j]); 69 } 70 if (dma->bufs[i].seglist) 71 free(dma->bufs[i].seglist, DRM_MEM_SEGS); 72 } 73 74 if (dma->bufs[i].buf_count) { 75 for (j = 0; j < dma->bufs[i].buf_count; j++) { 76 free(dma->bufs[i].buflist[j].dev_private, 77 DRM_MEM_BUFS); 78 } 79 if (dma->bufs[i].buflist) 80 free(dma->bufs[i].buflist, DRM_MEM_BUFS); 81 } 82 } 83 84 if (dma->buflist) 85 free(dma->buflist, DRM_MEM_BUFS); 86 if (dma->pagelist) 87 free(dma->pagelist, DRM_MEM_PAGES); 88 if (dev->dma) 89 free(dev->dma, DRM_MEM_DRIVER); 90 dev->dma = NULL; 91 DRM_SPINUNINIT(&dev->dma_lock); 92 } 93 94 95 void drm_free_buffer(struct drm_device *dev, drm_buf_t *buf) 96 { 97 if (!buf) 98 return; 99 100 buf->pending = 0; 101 buf->file_priv= NULL; 102 buf->used = 0; 103 } 104 105 void drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv) 106 { 107 drm_device_dma_t *dma = dev->dma; 108 int i; 109 110 if (!dma) 111 return; 112 113 for (i = 0; i < dma->buf_count; i++) { 114 if (dma->buflist[i]->file_priv == file_priv) { 115 switch (dma->buflist[i]->list) { 116 case DRM_LIST_NONE: 117 drm_free_buffer(dev, dma->buflist[i]); 118 break; 119 case DRM_LIST_WAIT: 120 dma->buflist[i]->list = DRM_LIST_RECLAIM; 121 break; 122 default: 123 /* Buffer already on hardware. */ 124 break; 125 } 126 } 127 } 128 } 129 130 /* Call into the driver-specific DMA handler */ 131 int drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv) 132 { 133 134 if (dev->driver->dma_ioctl) { 135 /* shared code returns -errno */ 136 return -dev->driver->dma_ioctl(dev, data, file_priv); 137 } else { 138 DRM_DEBUG("DMA ioctl on driver with no dma handler\n"); 139 return EINVAL; 140 } 141 } 142