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