xref: /dragonfly/sys/dev/drm/drm_dma.c (revision 8a7bdfea)
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  * $DragonFly: src/sys/dev/drm/drm_dma.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
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 "drmP.h"
41 
42 int drm_dma_setup(drm_device_t *dev)
43 {
44 
45 	dev->dma = malloc(sizeof(*dev->dma), M_DRM, M_NOWAIT | M_ZERO);
46 	if (dev->dma == NULL)
47 		return ENOMEM;
48 
49 	DRM_SPININIT(&dev->dma_lock, "drmdma");
50 
51 	return 0;
52 }
53 
54 void drm_dma_takedown(drm_device_t *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",
67 				  i,
68 				  dma->bufs[i].buf_count,
69 				  dma->bufs[i].seg_count);
70 			for (j = 0; j < dma->bufs[i].seg_count; j++) {
71 				drm_pci_free(dev, dma->bufs[i].seglist[j]);
72 			}
73 			free(dma->bufs[i].seglist, M_DRM);
74 		}
75 
76 	   	if (dma->bufs[i].buf_count) {
77 		   	for (j = 0; j < dma->bufs[i].buf_count; j++) {
78 				free(dma->bufs[i].buflist[j].dev_private,
79 				    M_DRM);
80 			}
81 		   	free(dma->bufs[i].buflist, M_DRM);
82 		}
83 	}
84 
85 	free(dma->buflist, M_DRM);
86 	free(dma->pagelist, M_DRM);
87 	free(dev->dma, M_DRM);
88 	dev->dma = NULL;
89 	DRM_SPINUNINIT(&dev->dma_lock);
90 }
91 
92 
93 void drm_free_buffer(drm_device_t *dev, drm_buf_t *buf)
94 {
95 	if (!buf) return;
96 
97 	buf->pending  = 0;
98 	buf->file_priv= NULL;
99 	buf->used     = 0;
100 }
101 
102 void drm_reclaim_buffers(drm_device_t *dev, struct drm_file *file_priv)
103 {
104 	drm_device_dma_t *dma = dev->dma;
105 	int		 i;
106 
107 	if (!dma) return;
108 	for (i = 0; i < dma->buf_count; i++) {
109 		if (dma->buflist[i]->file_priv == file_priv) {
110 			switch (dma->buflist[i]->list) {
111 			case DRM_LIST_NONE:
112 				drm_free_buffer(dev, dma->buflist[i]);
113 				break;
114 			case DRM_LIST_WAIT:
115 				dma->buflist[i]->list = DRM_LIST_RECLAIM;
116 				break;
117 			default:
118 				/* Buffer already on hardware. */
119 				break;
120 			}
121 		}
122 	}
123 }
124 
125 /* Call into the driver-specific DMA handler */
126 int drm_dma(drm_device_t *dev, void *data, struct drm_file *file_priv)
127 {
128 
129 	if (dev->driver.dma_ioctl) {
130 		/* shared code returns -errno */
131 		return -dev->driver.dma_ioctl(dev, data, file_priv);
132 	} else {
133 		DRM_DEBUG("DMA ioctl on driver with no dma handler\n");
134 		return EINVAL;
135 	}
136 }
137