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