]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_dma.c
This commit was generated by cvs2svn to compensate for changes in r147353,
[FreeBSD/FreeBSD.git] / sys / dev / drm / drm_dma.c
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  * $FreeBSD$
33  */
34
35 #include "dev/drm/drmP.h"
36
37 int drm_dma_setup(drm_device_t *dev)
38 {
39
40         dev->dma = malloc(sizeof(*dev->dma), M_DRM, M_NOWAIT | M_ZERO);
41         if (dev->dma == NULL)
42                 return DRM_ERR(ENOMEM);
43
44         DRM_SPININIT(dev->dma_lock, "drmdma");
45
46         return 0;
47 }
48
49 void drm_dma_takedown(drm_device_t *dev)
50 {
51         drm_device_dma_t  *dma = dev->dma;
52         int               i, j;
53
54         if (dma == NULL)
55                 return;
56
57                                 /* Clear dma buffers */
58         for (i = 0; i <= DRM_MAX_ORDER; i++) {
59                 if (dma->bufs[i].seg_count) {
60                         DRM_DEBUG("order %d: buf_count = %d,"
61                                   " seg_count = %d\n",
62                                   i,
63                                   dma->bufs[i].buf_count,
64                                   dma->bufs[i].seg_count);
65                         for (j = 0; j < dma->bufs[i].seg_count; j++) {
66                                 drm_pci_free(dev, dma->bufs[i].buf_size,
67                                     (void *)dma->bufs[i].seglist[j],
68                                     dma->bufs[i].seglist_bus[j]);
69                         }
70                         free(dma->bufs[i].seglist, M_DRM);
71                         free(dma->bufs[i].seglist_bus, M_DRM);
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                                     M_DRM);
78                         }
79                         free(dma->bufs[i].buflist, M_DRM);
80                 }
81         }
82
83         free(dma->buflist, M_DRM);
84         free(dma->pagelist, M_DRM);
85         free(dev->dma, M_DRM);
86         dev->dma = NULL;
87         DRM_SPINUNINIT(dev->dma_lock);
88 }
89
90
91 void drm_free_buffer(drm_device_t *dev, drm_buf_t *buf)
92 {
93         if (!buf) return;
94
95         buf->pending  = 0;
96         buf->filp     = NULL;
97         buf->used     = 0;
98 }
99
100 void drm_reclaim_buffers(drm_device_t *dev, DRMFILE filp)
101 {
102         drm_device_dma_t *dma = dev->dma;
103         int              i;
104
105         if (!dma) return;
106         for (i = 0; i < dma->buf_count; i++) {
107                 if (dma->buflist[i]->filp == filp) {
108                         switch (dma->buflist[i]->list) {
109                         case DRM_LIST_NONE:
110                                 drm_free_buffer(dev, dma->buflist[i]);
111                                 break;
112                         case DRM_LIST_WAIT:
113                                 dma->buflist[i]->list = DRM_LIST_RECLAIM;
114                                 break;
115                         default:
116                                 /* Buffer already on hardware. */
117                                 break;
118                         }
119                 }
120         }
121 }
122
123 /* Call into the driver-specific DMA handler */
124 int drm_dma(DRM_IOCTL_ARGS)
125 {
126         DRM_DEVICE;
127
128         if (dev->dma_ioctl) {
129                 return dev->dma_ioctl(kdev, cmd, data, flags, p, filp);
130         } else {
131                 DRM_DEBUG("DMA ioctl on driver with no dma handler\n");
132                 return EINVAL;
133         }
134 }