blob: 4a794d89942bc34261eeeee1bd8d5e5b29ebf5a3 [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 */
Eric Anholtc153f452007-09-03 12:06:45 +100043int drm_adddraw(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100045 unsigned long irqflags;
Eric Anholtc153f452007-09-03 12:06:45 +100046 struct drm_draw *draw = data;
Dave Airlied4e2cbe2007-07-17 10:55:47 +100047 int new_id = 0;
48 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Dave Airlied4e2cbe2007-07-17 10:55:47 +100050again:
51 if (idr_pre_get(&dev->drw_idr, GFP_KERNEL) == 0) {
52 DRM_ERROR("Out of memory expanding drawable idr\n");
53 return -ENOMEM;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100054 }
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100055
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100056 spin_lock_irqsave(&dev->drw_lock, irqflags);
Dave Airlied4e2cbe2007-07-17 10:55:47 +100057 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;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100061 }
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100062
63 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
64
Eric Anholtc153f452007-09-03 12:06:45 +100065 draw->handle = new_id;
Dave Airlied4e2cbe2007-07-17 10:55:47 +100066
Eric Anholtc153f452007-09-03 12:06:45 +100067 DRM_DEBUG("%d\n", draw->handle);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return 0;
70}
71
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100072/**
73 * Free drawable ID and memory to store information about it.
74 */
Eric Anholtc153f452007-09-03 12:06:45 +100075int drm_rmdraw(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Eric Anholtc153f452007-09-03 12:06:45 +100077 struct drm_draw *draw = data;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100078 unsigned long irqflags;
Zhenyu Wang786225e2008-10-17 15:48:44 +080079 struct drm_drawable_info *info;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100080
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +100081 spin_lock_irqsave(&dev->drw_lock, irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100082
Zhenyu Wang786225e2008-10-17 15:48:44 +080083 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);
Dave Airlied4e2cbe2007-07-17 10:55:47 +100087
Eric Anholtc153f452007-09-03 12:06:45 +100088 idr_remove(&dev->drw_idr, draw->handle);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100089
90 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
Eric Anholtc153f452007-09-03 12:06:45 +100091 DRM_DEBUG("%d\n", draw->handle);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +100094
Eric Anholtc153f452007-09-03 12:06:45 +100095int drm_update_drawable_info(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlied4e2cbe2007-07-17 10:55:47 +100096{
Eric Anholtc153f452007-09-03 12:06:45 +100097 struct drm_update_draw *update = data;
Dave Airlied4e2cbe2007-07-17 10:55:47 +100098 unsigned long irqflags;
Dave Airliec60ce622007-07-11 15:27:12 +100099 struct drm_clip_rect *rects;
Dave Airlied4e2cbe2007-07-17 10:55:47 +1000100 struct drm_drawable_info *info;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000101 int err;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000102
Eric Anholtc153f452007-09-03 12:06:45 +1000103 info = idr_find(&dev->drw_idr, update->handle);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000104 if (!info) {
Dave Airlied4e2cbe2007-07-17 10:55:47 +1000105 info = drm_calloc(1, sizeof(*info), DRM_MEM_BUFS);
106 if (!info)
107 return -ENOMEM;
Eric Anholtc153f452007-09-03 12:06:45 +1000108 if (IS_ERR(idr_replace(&dev->drw_idr, info, update->handle))) {
109 DRM_ERROR("No such drawable %d\n", update->handle);
Dave Airlied4e2cbe2007-07-17 10:55:47 +1000110 drm_free(info, sizeof(*info), DRM_MEM_BUFS);
111 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000112 }
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000113 }
114
Eric Anholtc153f452007-09-03 12:06:45 +1000115 switch (update->type) {
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000116 case DRM_DRAWABLE_CLIPRECTS:
Zhenyu Wang86384272008-10-17 13:15:48 +0800117 if (update->num == 0)
118 rects = NULL;
119 else if (update->num != info->num_rects) {
Eric Anholtc153f452007-09-03 12:06:45 +1000120 rects = drm_alloc(update->num * sizeof(struct drm_clip_rect),
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000121 DRM_MEM_BUFS);
122 } else
123 rects = info->rects;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000124
Eric Anholtc153f452007-09-03 12:06:45 +1000125 if (update->num && !rects) {
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000126 DRM_ERROR("Failed to allocate cliprect memory\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000127 err = -ENOMEM;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000128 goto error;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000129 }
130
Eric Anholtc153f452007-09-03 12:06:45 +1000131 if (update->num && DRM_COPY_FROM_USER(rects,
Dave Airliec60ce622007-07-11 15:27:12 +1000132 (struct drm_clip_rect __user *)
Eric Anholtc153f452007-09-03 12:06:45 +1000133 (unsigned long)update->data,
134 update->num *
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000135 sizeof(*rects))) {
136 DRM_ERROR("Failed to copy cliprects from userspace\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000137 err = -EFAULT;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000138 goto error;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000139 }
140
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000141 spin_lock_irqsave(&dev->drw_lock, irqflags);
142
143 if (rects != info->rects) {
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000144 drm_free(info->rects, info->num_rects *
Dave Airliec60ce622007-07-11 15:27:12 +1000145 sizeof(struct drm_clip_rect), DRM_MEM_BUFS);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000146 }
147
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000148 info->rects = rects;
Eric Anholtc153f452007-09-03 12:06:45 +1000149 info->num_rects = update->num;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000150
151 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
152
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000153 DRM_DEBUG("Updated %d cliprects for drawable %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000154 info->num_rects, update->handle);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000155 break;
156 default:
Eric Anholtc153f452007-09-03 12:06:45 +1000157 DRM_ERROR("Invalid update type %d\n", update->type);
Eric Anholt20caafa2007-08-25 19:22:43 +1000158 return -EINVAL;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000159 }
160
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000161 return 0;
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000162
163error:
Dave Airlied4e2cbe2007-07-17 10:55:47 +1000164 if (rects != info->rects)
Eric Anholtc153f452007-09-03 12:06:45 +1000165 drm_free(rects, update->num * sizeof(struct drm_clip_rect),
Dave Airlied4e2cbe2007-07-17 10:55:47 +1000166 DRM_MEM_BUFS);
=?utf-8?q?Michel_D=C3=A4nzer?=b03ed6f2006-10-24 23:18:49 +1000167
168 return err;
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000169}
170
171/**
172 * Caller must hold the drawable spinlock!
173 */
Dave Airlied4e2cbe2007-07-17 10:55:47 +1000174struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev, drm_drawable_t id)
175{
176 return idr_find(&dev->drw_idr, id);
=?utf-8?q?Michel_D=C3=A4nzer?=bea56792006-10-24 23:04:19 +1000177}
178EXPORT_SYMBOL(drm_get_drawable_info);
Dave Airlied4e2cbe2007-07-17 10:55:47 +1000179
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);
188 }
189
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);
197}