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@valinux.com> 27 * Gareth Hughes <gareth@valinux.com> 28 * 29 * $FreeBSD: head/sys/dev/drm2/drm_dma.c 235783 2012-05-22 11:07:44Z kib $ 30 */ 31 32 /** @file drm_dma.c 33 * Support code for DMA buffer management. 34 * 35 * The implementation used to be significantly more complicated, but the 36 * complexity has been moved into the drivers as different buffer management 37 * schemes evolved. 38 */ 39 40 #include <drm/drmP.h> 41 42 int drm_dma_setup(struct drm_device *dev) 43 { 44 45 dev->dma = kmalloc(sizeof(*dev->dma), DRM_MEM_DRIVER, M_NOWAIT | M_ZERO); 46 if (dev->dma == NULL) 47 return ENOMEM; 48 49 spin_init(&dev->dma_lock); 50 51 return 0; 52 } 53 54 void drm_dma_takedown(struct drm_device *dev) 55 { 56 drm_device_dma_t *dma = dev->dma; 57 int i, j; 58 59 if (dma == NULL) 60 return; 61 62 /* Clear dma buffers */ 63 for (i = 0; i <= DRM_MAX_ORDER; i++) { 64 if (dma->bufs[i].seg_count) { 65 DRM_DEBUG("order %d: buf_count = %d," 66 " seg_count = %d\n", i, dma->bufs[i].buf_count, 67 dma->bufs[i].seg_count); 68 for (j = 0; j < dma->bufs[i].seg_count; j++) { 69 drm_pci_free(dev, dma->bufs[i].seglist[j]); 70 } 71 drm_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 drm_free(dma->bufs[i].buflist[j].dev_private, 77 DRM_MEM_BUFS); 78 } 79 drm_free(dma->bufs[i].buflist, DRM_MEM_BUFS); 80 } 81 } 82 83 drm_free(dma->buflist, DRM_MEM_BUFS); 84 drm_free(dma->pagelist, DRM_MEM_PAGES); 85 drm_free(dev->dma, DRM_MEM_DRIVER); 86 dev->dma = NULL; 87 spin_uninit(&dev->dma_lock); 88 } 89 90 91 void drm_free_buffer(struct drm_device *dev, drm_buf_t *buf) 92 { 93 if (!buf) 94 return; 95 96 buf->pending = 0; 97 buf->file_priv= NULL; 98 buf->used = 0; 99 } 100 101 void drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv) 102 { 103 drm_device_dma_t *dma = dev->dma; 104 int i; 105 106 if (!dma) 107 return; 108 109 for (i = 0; i < dma->buf_count; i++) { 110 if (dma->buflist[i]->file_priv == file_priv) { 111 switch (dma->buflist[i]->list) { 112 case DRM_LIST_NONE: 113 drm_free_buffer(dev, dma->buflist[i]); 114 break; 115 case DRM_LIST_WAIT: 116 dma->buflist[i]->list = DRM_LIST_RECLAIM; 117 break; 118 default: 119 /* Buffer already on hardware. */ 120 break; 121 } 122 } 123 } 124 } 125 126 /* Call into the driver-specific DMA handler */ 127 int drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv) 128 { 129 130 if (dev->driver->dma_ioctl) { 131 /* shared code returns -errno */ 132 return -dev->driver->dma_ioctl(dev, data, file_priv); 133 } else { 134 DRM_DEBUG("DMA ioctl on driver with no dma handler\n"); 135 return EINVAL; 136 } 137 } 138