OSDN Git Service

Fix copy'n'paste-o in FreeBSD drawable code.
[android-x86/external-libdrm.git] / bsd-core / 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  */
33
34 #include "drmP.h"
35
36 int drm_dma_setup(drm_device_t *dev)
37 {
38
39         dev->dma = malloc(sizeof(*dev->dma), M_DRM, M_NOWAIT | M_ZERO);
40         if (dev->dma == NULL)
41                 return ENOMEM;
42
43         DRM_SPININIT(&dev->dma_lock, "drmdma");
44
45         return 0;
46 }
47
48 void drm_dma_takedown(drm_device_t *dev)
49 {
50         drm_device_dma_t  *dma = dev->dma;
51         int               i, j;
52
53         if (dma == NULL)
54                 return;
55
56                                 /* Clear dma buffers */
57         for (i = 0; i <= DRM_MAX_ORDER; i++) {
58                 if (dma->bufs[i].seg_count) {
59                         DRM_DEBUG("order %d: buf_count = %d,"
60                                   " seg_count = %d\n",
61                                   i,
62                                   dma->bufs[i].buf_count,
63                                   dma->bufs[i].seg_count);
64                         for (j = 0; j < dma->bufs[i].seg_count; j++) {
65                                 drm_pci_free(dev, dma->bufs[i].seglist[j]);
66                         }
67                         free(dma->bufs[i].seglist, M_DRM);
68                 }
69
70                 if (dma->bufs[i].buf_count) {
71                         for (j = 0; j < dma->bufs[i].buf_count; j++) {
72                                 free(dma->bufs[i].buflist[j].dev_private,
73                                     M_DRM);
74                         }
75                         free(dma->bufs[i].buflist, M_DRM);
76                 }
77         }
78
79         free(dma->buflist, M_DRM);
80         free(dma->pagelist, M_DRM);
81         free(dev->dma, M_DRM);
82         dev->dma = NULL;
83         DRM_SPINUNINIT(&dev->dma_lock);
84 }
85
86
87 void drm_free_buffer(drm_device_t *dev, drm_buf_t *buf)
88 {
89         if (!buf) return;
90
91         buf->pending  = 0;
92         buf->file_priv= NULL;
93         buf->used     = 0;
94 }
95
96 void drm_reclaim_buffers(drm_device_t *dev, struct drm_file *file_priv)
97 {
98         drm_device_dma_t *dma = dev->dma;
99         int              i;
100
101         if (!dma) return;
102         for (i = 0; i < dma->buf_count; i++) {
103                 if (dma->buflist[i]->file_priv == file_priv) {
104                         switch (dma->buflist[i]->list) {
105                         case DRM_LIST_NONE:
106                                 drm_free_buffer(dev, dma->buflist[i]);
107                                 break;
108                         case DRM_LIST_WAIT:
109                                 dma->buflist[i]->list = DRM_LIST_RECLAIM;
110                                 break;
111                         default:
112                                 /* Buffer already on hardware. */
113                                 break;
114                         }
115                 }
116         }
117 }
118
119 /* Call into the driver-specific DMA handler */
120 int drm_dma(drm_device_t *dev, void *data, struct drm_file *file_priv)
121 {
122
123         if (dev->driver.dma_ioctl) {
124                 return -dev->driver.dma_ioctl(dev, data, file_priv);
125         } else {
126                 DRM_DEBUG("DMA ioctl on driver with no dma handler\n");
127                 return EINVAL;
128         }
129 }