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