blob: de37d5f7456357867965e0881e262a1cdbab7cad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_drawable.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * IOCTLs for drawables
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +10007 * \author Michel Dänzer <michel@tungstengraphics.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10/*
11 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
12 *
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100015 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, North Dakota.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * All Rights Reserved.
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a
19 * copy of this software and associated documentation files (the "Software"),
20 * to deal in the Software without restriction, including without limitation
21 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 * and/or sell copies of the Software, and to permit persons to whom the
23 * Software is furnished to do so, subject to the following conditions:
24 *
25 * The above copyright notice and this permission notice (including the next
26 * paragraph) shall be included in all copies or substantial portions of the
27 * Software.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
33 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 * OTHER DEALINGS IN THE SOFTWARE.
36 */
37
38#include "drmP.h"
39
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100040/**
41 * Allocate drawable ID and memory to store information about it.
42 */
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100043int drm_adddraw(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100045 DRM_DEVICE;
46 unsigned long irqflags;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100047 int i, j;
48 u32 *bitfield = dev->drw_bitfield;
49 unsigned int bitfield_length = dev->drw_bitfield_length;
50 drm_drawable_info_t **info = dev->drw_info;
51 unsigned int info_length = dev->drw_info_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 drm_draw_t draw;
53
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100054 for (i = 0, j = 0; i < bitfield_length; i++) {
55 if (bitfield[i] == ~0)
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100056 continue;
57
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100058 for (; j < 8 * sizeof(*bitfield); j++)
59 if (!(bitfield[i] & (1 << j)))
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100060 goto done;
61 }
62done:
63
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100064 if (i == bitfield_length) {
65 bitfield_length++;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100066
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100067 bitfield = drm_alloc(bitfield_length * sizeof(*bitfield),
68 DRM_MEM_BUFS);
69
70 if (!bitfield) {
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100071 DRM_ERROR("Failed to allocate new drawable bitfield\n");
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100072 return DRM_ERR(ENOMEM);
73 }
74
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100075 if (8 * sizeof(*bitfield) * bitfield_length > info_length) {
76 info_length += 8 * sizeof(*bitfield);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100077
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100078 info = drm_alloc(info_length * sizeof(*info),
79 DRM_MEM_BUFS);
80
81 if (!info) {
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100082 DRM_ERROR("Failed to allocate new drawable info"
83 " array\n");
84
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100085 drm_free(bitfield,
86 bitfield_length * sizeof(*bitfield),
87 DRM_MEM_BUFS);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100088 return DRM_ERR(ENOMEM);
89 }
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100090 }
91
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100092 bitfield[i] = 0;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100093 }
94
=?utf-8?q?Michel_D=C3=A4nzer?=cdec2f82006-10-24 23:20:15 +100095 draw.handle = i * 8 * sizeof(*bitfield) + j + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 DRM_DEBUG("%d\n", draw.handle);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100097
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100098 spin_lock_irqsave(&dev->drw_lock, irqflags);
99
100 bitfield[i] |= 1 << j;
=?utf-8?q?Michel_D=C3=A4nzer?=cdec2f82006-10-24 23:20:15 +1000101 info[draw.handle - 1] = NULL;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000102
103 if (bitfield != dev->drw_bitfield) {
104 memcpy(bitfield, dev->drw_bitfield, dev->drw_bitfield_length *
105 sizeof(*bitfield));
106 drm_free(dev->drw_bitfield, sizeof(*bitfield) *
107 dev->drw_bitfield_length, DRM_MEM_BUFS);
108 dev->drw_bitfield = bitfield;
109 dev->drw_bitfield_length = bitfield_length;
110 }
111
112 if (info != dev->drw_info) {
113 memcpy(info, dev->drw_info, dev->drw_info_length *
114 sizeof(*info));
115 drm_free(dev->drw_info, sizeof(*info) * dev->drw_info_length,
116 DRM_MEM_BUFS);
117 dev->drw_info = info;
118 dev->drw_info_length = info_length;
119 }
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000120
121 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
122
123 DRM_COPY_TO_USER_IOCTL((drm_draw_t __user *)data, draw, sizeof(draw));
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126}
127
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000128/**
129 * Free drawable ID and memory to store information about it.
130 */
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000131int drm_rmdraw(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000133 DRM_DEVICE;
134 drm_draw_t draw;
=?utf-8?q?Felix_K=C3=BChling?=507c0182006-10-24 23:28:23 +1000135 int id, idx;
136 unsigned int shift;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000137 unsigned long irqflags;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000138 u32 *bitfield = dev->drw_bitfield;
139 unsigned int bitfield_length = dev->drw_bitfield_length;
140 drm_drawable_info_t **info = dev->drw_info;
141 unsigned int info_length = dev->drw_info_length;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000142
143 DRM_COPY_FROM_USER_IOCTL(draw, (drm_draw_t __user *) data,
144 sizeof(draw));
145
=?utf-8?q?Michel_D=C3=A4nzer?=cdec2f82006-10-24 23:20:15 +1000146 id = draw.handle - 1;
147 idx = id / (8 * sizeof(*bitfield));
148 shift = id % (8 * sizeof(*bitfield));
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000149
=?utf-8?q?Michel_D=C3=A4nzer?=cdec2f82006-10-24 23:20:15 +1000150 if (idx < 0 || idx >= bitfield_length ||
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000151 !(bitfield[idx] & (1 << shift))) {
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000152 DRM_DEBUG("No such drawable %d\n", draw.handle);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000153 return 0;
154 }
155
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000156 spin_lock_irqsave(&dev->drw_lock, irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000157
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000158 bitfield[idx] &= ~(1 << shift);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000159
160 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
161
=?utf-8?q?Michel_D=C3=A4nzer?=cdec2f82006-10-24 23:20:15 +1000162 if (info[id]) {
163 drm_free(info[id]->rects, info[id]->num_rects *
164 sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
165 drm_free(info[id], sizeof(**info), DRM_MEM_BUFS);
166 }
167
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000168 /* Can we shrink the arrays? */
169 if (idx == bitfield_length - 1) {
170 while (idx >= 0 && !bitfield[idx])
171 --idx;
172
173 bitfield_length = idx + 1;
174
=?utf-8?q?Michel_D=C3=A4nzer?=cdec2f82006-10-24 23:20:15 +1000175 if (idx != id / (8 * sizeof(*bitfield)))
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000176 bitfield = drm_alloc(bitfield_length *
177 sizeof(*bitfield), DRM_MEM_BUFS);
178
179 if (!bitfield && bitfield_length) {
180 bitfield = dev->drw_bitfield;
181 bitfield_length = dev->drw_bitfield_length;
182 }
183 }
184
185 if (bitfield != dev->drw_bitfield) {
186 info_length = 8 * sizeof(*bitfield) * bitfield_length;
187
188 info = drm_alloc(info_length * sizeof(*info), DRM_MEM_BUFS);
189
190 if (!info && info_length) {
191 info = dev->drw_info;
192 info_length = dev->drw_info_length;
193 }
194
195 spin_lock_irqsave(&dev->drw_lock, irqflags);
196
197 memcpy(bitfield, dev->drw_bitfield, bitfield_length *
198 sizeof(*bitfield));
199 drm_free(dev->drw_bitfield, sizeof(*bitfield) *
200 dev->drw_bitfield_length, DRM_MEM_BUFS);
201 dev->drw_bitfield = bitfield;
202 dev->drw_bitfield_length = bitfield_length;
203
204 if (info != dev->drw_info) {
205 memcpy(info, dev->drw_info, info_length *
206 sizeof(*info));
207 drm_free(dev->drw_info, sizeof(*info) *
208 dev->drw_info_length, DRM_MEM_BUFS);
209 dev->drw_info = info;
210 dev->drw_info_length = info_length;
211 }
212
213 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
214 }
215
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000216 DRM_DEBUG("%d\n", draw.handle);
217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000219
220int drm_update_drawable_info(DRM_IOCTL_ARGS) {
221 DRM_DEVICE;
222 drm_update_draw_t update;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000223 unsigned int id, idx, shift;
224 u32 *bitfield = dev->drw_bitfield;
225 unsigned long irqflags, bitfield_length = dev->drw_bitfield_length;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000226 drm_drawable_info_t *info;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000227 drm_clip_rect_t *rects;
228 int err;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000229
230 DRM_COPY_FROM_USER_IOCTL(update, (drm_update_draw_t __user *) data,
231 sizeof(update));
232
=?utf-8?q?Michel_D=C3=A4nzer?=cdec2f82006-10-24 23:20:15 +1000233 id = update.handle - 1;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000234 idx = id / (8 * sizeof(*bitfield));
235 shift = id % (8 * sizeof(*bitfield));
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000236
=?utf-8?q?Michel_D=C3=A4nzer?=cdec2f82006-10-24 23:20:15 +1000237 if (idx < 0 || idx >= bitfield_length ||
238 !(bitfield[idx] & (1 << shift))) {
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000239 DRM_ERROR("No such drawable %d\n", update.handle);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000240 return DRM_ERR(EINVAL);
241 }
242
243 info = dev->drw_info[id];
244
245 if (!info) {
246 info = drm_calloc(1, sizeof(drm_drawable_info_t), DRM_MEM_BUFS);
247
248 if (!info) {
249 DRM_ERROR("Failed to allocate drawable info memory\n");
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000250 return DRM_ERR(ENOMEM);
251 }
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000252 }
253
254 switch (update.type) {
255 case DRM_DRAWABLE_CLIPRECTS:
256 if (update.num != info->num_rects) {
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000257 rects = drm_alloc(update.num * sizeof(drm_clip_rect_t),
258 DRM_MEM_BUFS);
259 } else
260 rects = info->rects;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000261
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000262 if (update.num && !rects) {
263 DRM_ERROR("Failed to allocate cliprect memory\n");
264 err = DRM_ERR(ENOMEM);
265 goto error;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000266 }
267
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000268 if (update.num && DRM_COPY_FROM_USER(rects,
269 (drm_clip_rect_t __user *)
270 (unsigned long)update.data,
271 update.num *
272 sizeof(*rects))) {
273 DRM_ERROR("Failed to copy cliprects from userspace\n");
274 err = DRM_ERR(EFAULT);
275 goto error;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000276 }
277
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000278 spin_lock_irqsave(&dev->drw_lock, irqflags);
279
280 if (rects != info->rects) {
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000281 drm_free(info->rects, info->num_rects *
282 sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000283 }
284
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000285 info->rects = rects;
286 info->num_rects = update.num;
287 dev->drw_info[id] = info;
288
289 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
290
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000291 DRM_DEBUG("Updated %d cliprects for drawable %d\n",
292 info->num_rects, id);
293 break;
294 default:
295 DRM_ERROR("Invalid update type %d\n", update.type);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000296 return DRM_ERR(EINVAL);
297 }
298
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000299 return 0;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000300
301error:
302 if (!dev->drw_info[id])
303 drm_free(info, sizeof(*info), DRM_MEM_BUFS);
304 else if (rects != dev->drw_info[id]->rects)
305 drm_free(rects, update.num *
306 sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
307
308 return err;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000309}
310
311/**
312 * Caller must hold the drawable spinlock!
313 */
314drm_drawable_info_t *drm_get_drawable_info(drm_device_t *dev, drm_drawable_t id) {
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000315 u32 *bitfield = dev->drw_bitfield;
=?utf-8?q?Michel_D=C3=A4nzer?=cdec2f82006-10-24 23:20:15 +1000316 unsigned int idx, shift;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000317
=?utf-8?q?Michel_D=C3=A4nzer?=cdec2f82006-10-24 23:20:15 +1000318 id--;
319 idx = id / (8 * sizeof(*bitfield));
320 shift = id % (8 * sizeof(*bitfield));
321
322 if (idx < 0 || idx >= dev->drw_bitfield_length ||
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000323 !(bitfield[idx] & (1 << shift))) {
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000324 DRM_DEBUG("No such drawable %d\n", id);
325 return NULL;
326 }
327
328 return dev->drw_info[id];
329}
330EXPORT_SYMBOL(drm_get_drawable_info);