5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 * \author Michel Dänzer <michel@tungstengraphics.com>
11 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
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.
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:
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
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.
41 * Allocate drawable ID and memory to store information about it.
43 int drm_adddraw(DRM_IOCTL_ARGS
)
46 unsigned long irqflags
;
52 if (idr_pre_get(&dev
->drw_idr
, GFP_KERNEL
) == 0) {
53 DRM_ERROR("Out of memory expanding drawable idr\n");
57 spin_lock_irqsave(&dev
->drw_lock
, irqflags
);
58 ret
= idr_get_new_above(&dev
->drw_idr
, NULL
, 1, &new_id
);
60 spin_unlock_irqrestore(&dev
->drw_lock
, irqflags
);
64 spin_unlock_irqrestore(&dev
->drw_lock
, irqflags
);
68 DRM_DEBUG("%d\n", draw
.handle
);
70 DRM_COPY_TO_USER_IOCTL((struct drm_draw __user
*)data
, draw
, sizeof(draw
));
76 * Free drawable ID and memory to store information about it.
78 int drm_rmdraw(DRM_IOCTL_ARGS
)
82 unsigned long irqflags
;
84 DRM_COPY_FROM_USER_IOCTL(draw
, (struct drm_draw __user
*) data
,
87 spin_lock_irqsave(&dev
->drw_lock
, irqflags
);
89 drm_free(drm_get_drawable_info(dev
, draw
.handle
),
90 sizeof(struct drm_drawable_info
), DRM_MEM_BUFS
);
92 idr_remove(&dev
->drw_idr
, draw
.handle
);
94 spin_unlock_irqrestore(&dev
->drw_lock
, irqflags
);
95 DRM_DEBUG("%d\n", draw
.handle
);
99 int drm_update_drawable_info(DRM_IOCTL_ARGS
)
102 struct drm_update_draw update
;
103 unsigned long irqflags
;
104 struct drm_clip_rect
*rects
;
105 struct drm_drawable_info
*info
;
108 DRM_COPY_FROM_USER_IOCTL(update
, (struct drm_update_draw __user
*) data
,
111 info
= idr_find(&dev
->drw_idr
, update
.handle
);
113 info
= drm_calloc(1, sizeof(*info
), DRM_MEM_BUFS
);
116 if (IS_ERR(idr_replace(&dev
->drw_idr
, info
, update
.handle
))) {
117 DRM_ERROR("No such drawable %d\n", update
.handle
);
118 drm_free(info
, sizeof(*info
), DRM_MEM_BUFS
);
123 switch (update
.type
) {
124 case DRM_DRAWABLE_CLIPRECTS
:
125 if (update
.num
!= info
->num_rects
) {
126 rects
= drm_alloc(update
.num
* sizeof(struct drm_clip_rect
),
131 if (update
.num
&& !rects
) {
132 DRM_ERROR("Failed to allocate cliprect memory\n");
137 if (update
.num
&& DRM_COPY_FROM_USER(rects
,
138 (struct drm_clip_rect __user
*)
139 (unsigned long)update
.data
,
142 DRM_ERROR("Failed to copy cliprects from userspace\n");
147 spin_lock_irqsave(&dev
->drw_lock
, irqflags
);
149 if (rects
!= info
->rects
) {
150 drm_free(info
->rects
, info
->num_rects
*
151 sizeof(struct drm_clip_rect
), DRM_MEM_BUFS
);
155 info
->num_rects
= update
.num
;
157 spin_unlock_irqrestore(&dev
->drw_lock
, irqflags
);
159 DRM_DEBUG("Updated %d cliprects for drawable %d\n",
160 info
->num_rects
, update
.handle
);
163 DRM_ERROR("Invalid update type %d\n", update
.type
);
170 if (rects
!= info
->rects
)
171 drm_free(rects
, update
.num
* sizeof(struct drm_clip_rect
),
178 * Caller must hold the drawable spinlock!
180 struct drm_drawable_info
*drm_get_drawable_info(struct drm_device
*dev
, drm_drawable_t id
)
182 return idr_find(&dev
->drw_idr
, id
);
184 EXPORT_SYMBOL(drm_get_drawable_info
);
186 static int drm_drawable_free(int idr
, void *p
, void *data
)
188 struct drm_drawable_info
*info
= p
;
191 drm_free(info
->rects
, info
->num_rects
*
192 sizeof(struct drm_clip_rect
), DRM_MEM_BUFS
);
193 drm_free(info
, sizeof(*info
), DRM_MEM_BUFS
);
199 void drm_drawable_free_all(struct drm_device
*dev
)
201 idr_for_each(&dev
->drw_idr
, drm_drawable_free
, NULL
);
202 idr_remove_all(&dev
->drw_idr
);