]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/drm/drm_drawable.c
drm: Change first valid DRM drawable ID to be 1 instead of 0.
[linux-2.6-omap-h63xx.git] / drivers / char / drm / drm_drawable.c
1 /**
2  * \file drm_drawable.c
3  * IOCTLs for drawables
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  * \author Michel Dänzer <michel@tungstengraphics.com>
8  */
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.
15  * Copyright 2006 Tungsten Graphics, Inc., Bismarck, North Dakota.
16  * 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
40 /**
41  * Allocate drawable ID and memory to store information about it.
42  */
43 int drm_adddraw(DRM_IOCTL_ARGS)
44 {
45         DRM_DEVICE;
46         unsigned long irqflags;
47         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;
52         drm_draw_t draw;
53
54         for (i = 0, j = 0; i < bitfield_length; i++) {
55                 if (bitfield[i] == ~0)
56                         continue;
57
58                 for (; j < 8 * sizeof(*bitfield); j++)
59                         if (!(bitfield[i] & (1 << j)))
60                                 goto done;
61         }
62 done:
63
64         if (i == bitfield_length) {
65                 bitfield_length++;
66
67                 bitfield = drm_alloc(bitfield_length * sizeof(*bitfield),
68                                      DRM_MEM_BUFS);
69
70                 if (!bitfield) {
71                         DRM_ERROR("Failed to allocate new drawable bitfield\n");
72                         return DRM_ERR(ENOMEM);
73                 }
74
75                 if (8 * sizeof(*bitfield) * bitfield_length > info_length) {
76                         info_length += 8 * sizeof(*bitfield);
77
78                         info = drm_alloc(info_length * sizeof(*info),
79                                          DRM_MEM_BUFS);
80
81                         if (!info) {
82                                 DRM_ERROR("Failed to allocate new drawable info"
83                                           " array\n");
84
85                                 drm_free(bitfield,
86                                          bitfield_length * sizeof(*bitfield),
87                                          DRM_MEM_BUFS);
88                                 return DRM_ERR(ENOMEM);
89                         }
90                 }
91
92                 bitfield[i] = 0;
93         }
94
95         draw.handle = i * 8 * sizeof(*bitfield) + j + 1;
96         DRM_DEBUG("%d\n", draw.handle);
97
98         spin_lock_irqsave(&dev->drw_lock, irqflags);
99
100         bitfield[i] |= 1 << j;
101         info[draw.handle - 1] = NULL;
102
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         }
120
121         spin_unlock_irqrestore(&dev->drw_lock, irqflags);
122
123         DRM_COPY_TO_USER_IOCTL((drm_draw_t __user *)data, draw, sizeof(draw));
124
125         return 0;
126 }
127
128 /**
129  * Free drawable ID and memory to store information about it.
130  */
131 int drm_rmdraw(DRM_IOCTL_ARGS)
132 {
133         DRM_DEVICE;
134         drm_draw_t draw;
135         unsigned int id, idx, shift;
136         unsigned long irqflags;
137         u32 *bitfield = dev->drw_bitfield;
138         unsigned int bitfield_length = dev->drw_bitfield_length;
139         drm_drawable_info_t **info = dev->drw_info;
140         unsigned int info_length = dev->drw_info_length;
141
142         DRM_COPY_FROM_USER_IOCTL(draw, (drm_draw_t __user *) data,
143                                  sizeof(draw));
144
145         id = draw.handle - 1;
146         idx = id / (8 * sizeof(*bitfield));
147         shift = id % (8 * sizeof(*bitfield));
148
149         if (idx < 0 || idx >= bitfield_length ||
150             !(bitfield[idx] & (1 << shift))) {
151                 DRM_DEBUG("No such drawable %d\n", draw.handle);
152                 return 0;
153         }
154
155         spin_lock_irqsave(&dev->drw_lock, irqflags);
156
157         bitfield[idx] &= ~(1 << shift);
158
159         spin_unlock_irqrestore(&dev->drw_lock, irqflags);
160
161         if (info[id]) {
162                 drm_free(info[id]->rects, info[id]->num_rects *
163                          sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
164                 drm_free(info[id], sizeof(**info), DRM_MEM_BUFS);
165         }
166
167         /* Can we shrink the arrays? */
168         if (idx == bitfield_length - 1) {
169                 while (idx >= 0 && !bitfield[idx])
170                         --idx;
171
172                 bitfield_length = idx + 1;
173
174                 if (idx != id / (8 * sizeof(*bitfield)))
175                         bitfield = drm_alloc(bitfield_length *
176                                              sizeof(*bitfield), DRM_MEM_BUFS);
177
178                 if (!bitfield && bitfield_length) {
179                         bitfield = dev->drw_bitfield;
180                         bitfield_length = dev->drw_bitfield_length;
181                 }
182         }
183
184         if (bitfield != dev->drw_bitfield) {
185                 info_length = 8 * sizeof(*bitfield) * bitfield_length;
186
187                 info = drm_alloc(info_length * sizeof(*info), DRM_MEM_BUFS);
188
189                 if (!info && info_length) {
190                         info = dev->drw_info;
191                         info_length = dev->drw_info_length;
192                 }
193
194                 spin_lock_irqsave(&dev->drw_lock, irqflags);
195
196                 memcpy(bitfield, dev->drw_bitfield, bitfield_length *
197                        sizeof(*bitfield));
198                 drm_free(dev->drw_bitfield, sizeof(*bitfield) *
199                          dev->drw_bitfield_length, DRM_MEM_BUFS);
200                 dev->drw_bitfield = bitfield;
201                 dev->drw_bitfield_length = bitfield_length;
202
203                 if (info != dev->drw_info) {
204                         memcpy(info, dev->drw_info, info_length *
205                                sizeof(*info));
206                         drm_free(dev->drw_info, sizeof(*info) *
207                                  dev->drw_info_length, DRM_MEM_BUFS);
208                         dev->drw_info = info;
209                         dev->drw_info_length = info_length;
210                 }
211
212                 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
213         }
214
215         DRM_DEBUG("%d\n", draw.handle);
216         return 0;
217 }
218
219 int drm_update_drawable_info(DRM_IOCTL_ARGS) {
220         DRM_DEVICE;
221         drm_update_draw_t update;
222         unsigned int id, idx, shift;
223         u32 *bitfield = dev->drw_bitfield;
224         unsigned long irqflags, bitfield_length = dev->drw_bitfield_length;
225         drm_drawable_info_t *info;
226         drm_clip_rect_t *rects;
227         int err;
228
229         DRM_COPY_FROM_USER_IOCTL(update, (drm_update_draw_t __user *) data,
230                                  sizeof(update));
231
232         id = update.handle - 1;
233         idx = id / (8 * sizeof(*bitfield));
234         shift = id % (8 * sizeof(*bitfield));
235
236         if (idx < 0 || idx >= bitfield_length ||
237             !(bitfield[idx] & (1 << shift))) {
238                 DRM_ERROR("No such drawable %d\n", update.handle);
239                 return DRM_ERR(EINVAL);
240         }
241
242         info = dev->drw_info[id];
243
244         if (!info) {
245                 info = drm_calloc(1, sizeof(drm_drawable_info_t), DRM_MEM_BUFS);
246
247                 if (!info) {
248                         DRM_ERROR("Failed to allocate drawable info memory\n");
249                         return DRM_ERR(ENOMEM);
250                 }
251         }
252
253         switch (update.type) {
254         case DRM_DRAWABLE_CLIPRECTS:
255                 if (update.num != info->num_rects) {
256                         rects = drm_alloc(update.num * sizeof(drm_clip_rect_t),
257                                          DRM_MEM_BUFS);
258                 } else
259                         rects = info->rects;
260
261                 if (update.num && !rects) {
262                         DRM_ERROR("Failed to allocate cliprect memory\n");
263                         err = DRM_ERR(ENOMEM);
264                         goto error;
265                 }
266
267                 if (update.num && DRM_COPY_FROM_USER(rects,
268                                                      (drm_clip_rect_t __user *)
269                                                      (unsigned long)update.data,
270                                                      update.num *
271                                                      sizeof(*rects))) {
272                         DRM_ERROR("Failed to copy cliprects from userspace\n");
273                         err = DRM_ERR(EFAULT);
274                         goto error;
275                 }
276
277                 spin_lock_irqsave(&dev->drw_lock, irqflags);
278
279                 if (rects != info->rects) {
280                         drm_free(info->rects, info->num_rects *
281                                  sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
282                 }
283
284                 info->rects = rects;
285                 info->num_rects = update.num;
286                 dev->drw_info[id] = info;
287
288                 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
289
290                 DRM_DEBUG("Updated %d cliprects for drawable %d\n",
291                           info->num_rects, id);
292                 break;
293         default:
294                 DRM_ERROR("Invalid update type %d\n", update.type);
295                 return DRM_ERR(EINVAL);
296         }
297
298         return 0;
299
300 error:
301         if (!dev->drw_info[id])
302                 drm_free(info, sizeof(*info), DRM_MEM_BUFS);
303         else if (rects != dev->drw_info[id]->rects)
304                 drm_free(rects, update.num *
305                          sizeof(drm_clip_rect_t), DRM_MEM_BUFS);
306
307         return err;
308 }
309
310 /**
311  * Caller must hold the drawable spinlock!
312  */
313 drm_drawable_info_t *drm_get_drawable_info(drm_device_t *dev, drm_drawable_t id) {
314         u32 *bitfield = dev->drw_bitfield;
315         unsigned int idx, shift;
316
317         id--;
318         idx = id / (8 * sizeof(*bitfield));
319         shift = id % (8 * sizeof(*bitfield));
320
321         if (idx < 0 || idx >= dev->drw_bitfield_length ||
322             !(bitfield[idx] & (1 << shift))) {
323                 DRM_DEBUG("No such drawable %d\n", id);
324                 return NULL;
325         }
326
327         return dev->drw_info[id];
328 }
329 EXPORT_SYMBOL(drm_get_drawable_info);