drm_dma.c revision 1.1.1.1.4.2 1 1.1.1.1.4.2 rmind /**
2 1.1.1.1.4.2 rmind * \file drm_dma.c
3 1.1.1.1.4.2 rmind * DMA IOCTL and function support
4 1.1.1.1.4.2 rmind *
5 1.1.1.1.4.2 rmind * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
6 1.1.1.1.4.2 rmind * \author Gareth Hughes <gareth (at) valinux.com>
7 1.1.1.1.4.2 rmind */
8 1.1.1.1.4.2 rmind
9 1.1.1.1.4.2 rmind /*
10 1.1.1.1.4.2 rmind * Created: Fri Mar 19 14:30:16 1999 by faith (at) valinux.com
11 1.1.1.1.4.2 rmind *
12 1.1.1.1.4.2 rmind * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 1.1.1.1.4.2 rmind * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 1.1.1.1.4.2 rmind * All Rights Reserved.
15 1.1.1.1.4.2 rmind *
16 1.1.1.1.4.2 rmind * Permission is hereby granted, free of charge, to any person obtaining a
17 1.1.1.1.4.2 rmind * copy of this software and associated documentation files (the "Software"),
18 1.1.1.1.4.2 rmind * to deal in the Software without restriction, including without limitation
19 1.1.1.1.4.2 rmind * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 1.1.1.1.4.2 rmind * and/or sell copies of the Software, and to permit persons to whom the
21 1.1.1.1.4.2 rmind * Software is furnished to do so, subject to the following conditions:
22 1.1.1.1.4.2 rmind *
23 1.1.1.1.4.2 rmind * The above copyright notice and this permission notice (including the next
24 1.1.1.1.4.2 rmind * paragraph) shall be included in all copies or substantial portions of the
25 1.1.1.1.4.2 rmind * Software.
26 1.1.1.1.4.2 rmind *
27 1.1.1.1.4.2 rmind * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 1.1.1.1.4.2 rmind * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 1.1.1.1.4.2 rmind * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 1.1.1.1.4.2 rmind * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 1.1.1.1.4.2 rmind * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 1.1.1.1.4.2 rmind * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 1.1.1.1.4.2 rmind * OTHER DEALINGS IN THE SOFTWARE.
34 1.1.1.1.4.2 rmind */
35 1.1.1.1.4.2 rmind
36 1.1.1.1.4.2 rmind #include <linux/export.h>
37 1.1.1.1.4.2 rmind #include <drm/drmP.h>
38 1.1.1.1.4.2 rmind
39 1.1.1.1.4.2 rmind /**
40 1.1.1.1.4.2 rmind * Initialize the DMA data.
41 1.1.1.1.4.2 rmind *
42 1.1.1.1.4.2 rmind * \param dev DRM device.
43 1.1.1.1.4.2 rmind * \return zero on success or a negative value on failure.
44 1.1.1.1.4.2 rmind *
45 1.1.1.1.4.2 rmind * Allocate and initialize a drm_device_dma structure.
46 1.1.1.1.4.2 rmind */
47 1.1.1.1.4.2 rmind int drm_dma_setup(struct drm_device *dev)
48 1.1.1.1.4.2 rmind {
49 1.1.1.1.4.2 rmind int i;
50 1.1.1.1.4.2 rmind
51 1.1.1.1.4.2 rmind dev->dma = kzalloc(sizeof(*dev->dma), GFP_KERNEL);
52 1.1.1.1.4.2 rmind if (!dev->dma)
53 1.1.1.1.4.2 rmind return -ENOMEM;
54 1.1.1.1.4.2 rmind
55 1.1.1.1.4.2 rmind for (i = 0; i <= DRM_MAX_ORDER; i++)
56 1.1.1.1.4.2 rmind memset(&dev->dma->bufs[i], 0, sizeof(dev->dma->bufs[0]));
57 1.1.1.1.4.2 rmind
58 1.1.1.1.4.2 rmind return 0;
59 1.1.1.1.4.2 rmind }
60 1.1.1.1.4.2 rmind
61 1.1.1.1.4.2 rmind /**
62 1.1.1.1.4.2 rmind * Cleanup the DMA resources.
63 1.1.1.1.4.2 rmind *
64 1.1.1.1.4.2 rmind * \param dev DRM device.
65 1.1.1.1.4.2 rmind *
66 1.1.1.1.4.2 rmind * Free all pages associated with DMA buffers, the buffers and pages lists, and
67 1.1.1.1.4.2 rmind * finally the drm_device::dma structure itself.
68 1.1.1.1.4.2 rmind */
69 1.1.1.1.4.2 rmind void drm_dma_takedown(struct drm_device *dev)
70 1.1.1.1.4.2 rmind {
71 1.1.1.1.4.2 rmind struct drm_device_dma *dma = dev->dma;
72 1.1.1.1.4.2 rmind int i, j;
73 1.1.1.1.4.2 rmind
74 1.1.1.1.4.2 rmind if (!dma)
75 1.1.1.1.4.2 rmind return;
76 1.1.1.1.4.2 rmind
77 1.1.1.1.4.2 rmind /* Clear dma buffers */
78 1.1.1.1.4.2 rmind for (i = 0; i <= DRM_MAX_ORDER; i++) {
79 1.1.1.1.4.2 rmind if (dma->bufs[i].seg_count) {
80 1.1.1.1.4.2 rmind DRM_DEBUG("order %d: buf_count = %d,"
81 1.1.1.1.4.2 rmind " seg_count = %d\n",
82 1.1.1.1.4.2 rmind i,
83 1.1.1.1.4.2 rmind dma->bufs[i].buf_count,
84 1.1.1.1.4.2 rmind dma->bufs[i].seg_count);
85 1.1.1.1.4.2 rmind for (j = 0; j < dma->bufs[i].seg_count; j++) {
86 1.1.1.1.4.2 rmind if (dma->bufs[i].seglist[j]) {
87 1.1.1.1.4.2 rmind drm_pci_free(dev, dma->bufs[i].seglist[j]);
88 1.1.1.1.4.2 rmind }
89 1.1.1.1.4.2 rmind }
90 1.1.1.1.4.2 rmind kfree(dma->bufs[i].seglist);
91 1.1.1.1.4.2 rmind }
92 1.1.1.1.4.2 rmind if (dma->bufs[i].buf_count) {
93 1.1.1.1.4.2 rmind for (j = 0; j < dma->bufs[i].buf_count; j++) {
94 1.1.1.1.4.2 rmind kfree(dma->bufs[i].buflist[j].dev_private);
95 1.1.1.1.4.2 rmind }
96 1.1.1.1.4.2 rmind kfree(dma->bufs[i].buflist);
97 1.1.1.1.4.2 rmind }
98 1.1.1.1.4.2 rmind }
99 1.1.1.1.4.2 rmind
100 1.1.1.1.4.2 rmind kfree(dma->buflist);
101 1.1.1.1.4.2 rmind kfree(dma->pagelist);
102 1.1.1.1.4.2 rmind kfree(dev->dma);
103 1.1.1.1.4.2 rmind dev->dma = NULL;
104 1.1.1.1.4.2 rmind }
105 1.1.1.1.4.2 rmind
106 1.1.1.1.4.2 rmind /**
107 1.1.1.1.4.2 rmind * Free a buffer.
108 1.1.1.1.4.2 rmind *
109 1.1.1.1.4.2 rmind * \param dev DRM device.
110 1.1.1.1.4.2 rmind * \param buf buffer to free.
111 1.1.1.1.4.2 rmind *
112 1.1.1.1.4.2 rmind * Resets the fields of \p buf.
113 1.1.1.1.4.2 rmind */
114 1.1.1.1.4.2 rmind void drm_free_buffer(struct drm_device *dev, struct drm_buf * buf)
115 1.1.1.1.4.2 rmind {
116 1.1.1.1.4.2 rmind if (!buf)
117 1.1.1.1.4.2 rmind return;
118 1.1.1.1.4.2 rmind
119 1.1.1.1.4.2 rmind buf->waiting = 0;
120 1.1.1.1.4.2 rmind buf->pending = 0;
121 1.1.1.1.4.2 rmind buf->file_priv = NULL;
122 1.1.1.1.4.2 rmind buf->used = 0;
123 1.1.1.1.4.2 rmind }
124 1.1.1.1.4.2 rmind
125 1.1.1.1.4.2 rmind /**
126 1.1.1.1.4.2 rmind * Reclaim the buffers.
127 1.1.1.1.4.2 rmind *
128 1.1.1.1.4.2 rmind * \param file_priv DRM file private.
129 1.1.1.1.4.2 rmind *
130 1.1.1.1.4.2 rmind * Frees each buffer associated with \p file_priv not already on the hardware.
131 1.1.1.1.4.2 rmind */
132 1.1.1.1.4.2 rmind void drm_core_reclaim_buffers(struct drm_device *dev,
133 1.1.1.1.4.2 rmind struct drm_file *file_priv)
134 1.1.1.1.4.2 rmind {
135 1.1.1.1.4.2 rmind struct drm_device_dma *dma = dev->dma;
136 1.1.1.1.4.2 rmind int i;
137 1.1.1.1.4.2 rmind
138 1.1.1.1.4.2 rmind if (!dma)
139 1.1.1.1.4.2 rmind return;
140 1.1.1.1.4.2 rmind for (i = 0; i < dma->buf_count; i++) {
141 1.1.1.1.4.2 rmind if (dma->buflist[i]->file_priv == file_priv) {
142 1.1.1.1.4.2 rmind switch (dma->buflist[i]->list) {
143 1.1.1.1.4.2 rmind case DRM_LIST_NONE:
144 1.1.1.1.4.2 rmind drm_free_buffer(dev, dma->buflist[i]);
145 1.1.1.1.4.2 rmind break;
146 1.1.1.1.4.2 rmind case DRM_LIST_WAIT:
147 1.1.1.1.4.2 rmind dma->buflist[i]->list = DRM_LIST_RECLAIM;
148 1.1.1.1.4.2 rmind break;
149 1.1.1.1.4.2 rmind default:
150 1.1.1.1.4.2 rmind /* Buffer already on hardware. */
151 1.1.1.1.4.2 rmind break;
152 1.1.1.1.4.2 rmind }
153 1.1.1.1.4.2 rmind }
154 1.1.1.1.4.2 rmind }
155 1.1.1.1.4.2 rmind }
156 1.1.1.1.4.2 rmind
157 1.1.1.1.4.2 rmind EXPORT_SYMBOL(drm_core_reclaim_buffers);
158