drm: Add 32-bit compatibility for DRM_IOCTL_UPDATE_DRAW.
[deliverable/linux.git] / drivers / gpu / drm / drm_drawable.c
CommitLineData
1da177e4 1/**
b5e89ed5 2 * \file drm_drawable.c
1da177e4
LT
3 * IOCTLs for drawables
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
bea5679f 7 * \author Michel Dänzer <michel@tungstengraphics.com>
1da177e4
LT
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.
b03ed6f2 15 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, North Dakota.
1da177e4
LT
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
b03ed6f2
MCA
40/**
41 * Allocate drawable ID and memory to store information about it.
42 */
c153f45f 43int drm_adddraw(struct drm_device *dev, void *data, struct drm_file *file_priv)
1da177e4 44{
bea5679f 45 unsigned long irqflags;
c153f45f 46 struct drm_draw *draw = data;
d4e2cbe9
DA
47 int new_id = 0;
48 int ret;
1da177e4 49
d4e2cbe9
DA
50again:
51 if (idr_pre_get(&dev->drw_idr, GFP_KERNEL) == 0) {
52 DRM_ERROR("Out of memory expanding drawable idr\n");
53 return -ENOMEM;
bea5679f
MCA
54 }
55
b03ed6f2 56 spin_lock_irqsave(&dev->drw_lock, irqflags);
d4e2cbe9
DA
57 ret = idr_get_new_above(&dev->drw_idr, NULL, 1, &new_id);
58 if (ret == -EAGAIN) {
59 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
60 goto again;
b03ed6f2 61 }
bea5679f
MCA
62
63 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
64
c153f45f 65 draw->handle = new_id;
d4e2cbe9 66
c153f45f 67 DRM_DEBUG("%d\n", draw->handle);
bea5679f 68
1da177e4
LT
69 return 0;
70}
71
b03ed6f2
MCA
72/**
73 * Free drawable ID and memory to store information about it.
74 */
c153f45f 75int drm_rmdraw(struct drm_device *dev, void *data, struct drm_file *file_priv)
1da177e4 76{
c153f45f 77 struct drm_draw *draw = data;
bea5679f 78 unsigned long irqflags;
786225eb 79 struct drm_drawable_info *info;
bea5679f 80
b03ed6f2
MCA
81 spin_lock_irqsave(&dev->drw_lock, irqflags);
82
786225eb
ZW
83 info = drm_get_drawable_info(dev, draw->handle);
84 drm_free(info->rects, info->num_rects * sizeof(struct drm_clip_rect),
85 DRM_MEM_BUFS);
86 drm_free(info, sizeof(struct drm_drawable_info), DRM_MEM_BUFS);
b03ed6f2 87
c153f45f 88 idr_remove(&dev->drw_idr, draw->handle);
bea5679f 89
d4e2cbe9 90 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
c153f45f 91 DRM_DEBUG("%d\n", draw->handle);
bea5679f
MCA
92 return 0;
93}
94
c153f45f 95int drm_update_drawable_info(struct drm_device *dev, void *data, struct drm_file *file_priv)
d4e2cbe9 96{
c153f45f 97 struct drm_update_draw *update = data;
d4e2cbe9 98 unsigned long irqflags;
c60ce623 99 struct drm_clip_rect *rects;
d4e2cbe9 100 struct drm_drawable_info *info;
b03ed6f2 101 int err;
bea5679f 102
c153f45f 103 info = idr_find(&dev->drw_idr, update->handle);
bea5679f 104 if (!info) {
d4e2cbe9
DA
105 info = drm_calloc(1, sizeof(*info), DRM_MEM_BUFS);
106 if (!info)
107 return -ENOMEM;
c153f45f
EA
108 if (IS_ERR(idr_replace(&dev->drw_idr, info, update->handle))) {
109 DRM_ERROR("No such drawable %d\n", update->handle);
d4e2cbe9
DA
110 drm_free(info, sizeof(*info), DRM_MEM_BUFS);
111 return -EINVAL;
bea5679f 112 }
bea5679f
MCA
113 }
114
c153f45f 115 switch (update->type) {
bea5679f 116 case DRM_DRAWABLE_CLIPRECTS:
86384273
ZW
117 if (update->num == 0)
118 rects = NULL;
119 else if (update->num != info->num_rects) {
c153f45f 120 rects = drm_alloc(update->num * sizeof(struct drm_clip_rect),
b03ed6f2
MCA
121 DRM_MEM_BUFS);
122 } else
123 rects = info->rects;
124
c153f45f 125 if (update->num && !rects) {
b03ed6f2 126 DRM_ERROR("Failed to allocate cliprect memory\n");
20caafa6 127 err = -ENOMEM;
b03ed6f2 128 goto error;
bea5679f
MCA
129 }
130
c153f45f 131 if (update->num && DRM_COPY_FROM_USER(rects,
c60ce623 132 (struct drm_clip_rect __user *)
c153f45f
EA
133 (unsigned long)update->data,
134 update->num *
b03ed6f2
MCA
135 sizeof(*rects))) {
136 DRM_ERROR("Failed to copy cliprects from userspace\n");
20caafa6 137 err = -EFAULT;
b03ed6f2 138 goto error;
bea5679f
MCA
139 }
140
b03ed6f2
MCA
141 spin_lock_irqsave(&dev->drw_lock, irqflags);
142
143 if (rects != info->rects) {
bea5679f 144 drm_free(info->rects, info->num_rects *
c60ce623 145 sizeof(struct drm_clip_rect), DRM_MEM_BUFS);
bea5679f
MCA
146 }
147
b03ed6f2 148 info->rects = rects;
c153f45f 149 info->num_rects = update->num;
b03ed6f2
MCA
150
151 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
152
bea5679f 153 DRM_DEBUG("Updated %d cliprects for drawable %d\n",
c153f45f 154 info->num_rects, update->handle);
bea5679f
MCA
155 break;
156 default:
c153f45f 157 DRM_ERROR("Invalid update type %d\n", update->type);
20caafa6 158 return -EINVAL;
bea5679f
MCA
159 }
160
bea5679f 161 return 0;
b03ed6f2
MCA
162
163error:
d4e2cbe9 164 if (rects != info->rects)
c153f45f 165 drm_free(rects, update->num * sizeof(struct drm_clip_rect),
d4e2cbe9 166 DRM_MEM_BUFS);
b03ed6f2
MCA
167
168 return err;
bea5679f
MCA
169}
170
171/**
172 * Caller must hold the drawable spinlock!
173 */
d4e2cbe9
DA
174struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev, drm_drawable_t id)
175{
176 return idr_find(&dev->drw_idr, id);
177}
178EXPORT_SYMBOL(drm_get_drawable_info);
179
180static int drm_drawable_free(int idr, void *p, void *data)
181{
182 struct drm_drawable_info *info = p;
183
184 if (info) {
185 drm_free(info->rects, info->num_rects *
186 sizeof(struct drm_clip_rect), DRM_MEM_BUFS);
187 drm_free(info, sizeof(*info), DRM_MEM_BUFS);
bea5679f
MCA
188 }
189
d4e2cbe9
DA
190 return 0;
191}
192
193void drm_drawable_free_all(struct drm_device *dev)
194{
195 idr_for_each(&dev->drw_idr, drm_drawable_free, NULL);
196 idr_remove_all(&dev->drw_idr);
1da177e4 197}
This page took 0.363179 seconds and 5 git commands to generate.