1 /* r128_context.c -- IOCTLs for r128 contexts -*- linux-c -*-
2 * Created: Mon Dec 13 09:51:35 1999 by faith@precisioninsight.com
4 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
27 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
31 #define __NO_VERSION__
35 extern drm_ctx_t r128_res_ctx
;
37 static int r128_alloc_queue(drm_device_t
*dev
)
39 return drm_ctxbitmap_next(dev
);
42 int r128_context_switch(drm_device_t
*dev
, int old
, int new)
46 atomic_inc(&dev
->total_ctx
);
48 if (test_and_set_bit(0, &dev
->context_flag
)) {
49 DRM_ERROR("Reentering -- FIXME\n");
54 dev
->ctx_start
= get_cycles();
57 DRM_DEBUG("Context switch from %d to %d\n", old
, new);
59 if (new == dev
->last_context
) {
60 clear_bit(0, &dev
->context_flag
);
64 if (drm_flags
& DRM_FLAG_NOCTX
) {
65 r128_context_switch_complete(dev
, new);
67 sprintf(buf
, "C %d %d\n", old
, new);
68 drm_write_string(dev
, buf
);
74 int r128_context_switch_complete(drm_device_t
*dev
, int new)
76 dev
->last_context
= new; /* PRE/POST: This is the _only_ writer. */
77 dev
->last_switch
= jiffies
;
79 if (!_DRM_LOCK_IS_HELD(dev
->lock
.hw_lock
->lock
)) {
80 DRM_ERROR("Lock isn't held after context switch\n");
83 /* If a context switch is ever initiated
84 when the kernel holds the lock, release
87 atomic_inc(&dev
->histo
.ctx
[drm_histogram_slot(get_cycles()
91 clear_bit(0, &dev
->context_flag
);
92 wake_up(&dev
->context_wait
);
98 int r128_resctx(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
105 DRM_DEBUG("%d\n", DRM_RESERVED_CONTEXTS
);
106 copy_from_user_ret(&res
, (drm_ctx_res_t
*)arg
, sizeof(res
), -EFAULT
);
107 if (res
.count
>= DRM_RESERVED_CONTEXTS
) {
108 memset(&ctx
, 0, sizeof(ctx
));
109 for (i
= 0; i
< DRM_RESERVED_CONTEXTS
; i
++) {
111 copy_to_user_ret(&res
.contexts
[i
],
117 res
.count
= DRM_RESERVED_CONTEXTS
;
118 copy_to_user_ret((drm_ctx_res_t
*)arg
, &res
, sizeof(res
), -EFAULT
);
123 int r128_addctx(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
126 drm_file_t
*priv
= filp
->private_data
;
127 drm_device_t
*dev
= priv
->dev
;
130 copy_from_user_ret(&ctx
, (drm_ctx_t
*)arg
, sizeof(ctx
), -EFAULT
);
131 if ((ctx
.handle
= r128_alloc_queue(dev
)) == DRM_KERNEL_CONTEXT
) {
132 /* Skip kernel's context and get a new one. */
133 ctx
.handle
= r128_alloc_queue(dev
);
135 DRM_DEBUG("%d\n", ctx
.handle
);
136 if (ctx
.handle
== -1) {
137 DRM_DEBUG("Not enough free contexts.\n");
138 /* Should this return -EBUSY instead? */
142 copy_to_user_ret((drm_ctx_t
*)arg
, &ctx
, sizeof(ctx
), -EFAULT
);
146 int r128_modctx(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
151 copy_from_user_ret(&ctx
, (drm_ctx_t
*)arg
, sizeof(ctx
), -EFAULT
);
152 if (ctx
.flags
==_DRM_CONTEXT_PRESERVED
)
153 r128_res_ctx
.handle
=ctx
.handle
;
157 int r128_getctx(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
162 copy_from_user_ret(&ctx
, (drm_ctx_t
*)arg
, sizeof(ctx
), -EFAULT
);
163 /* This is 0, because we don't hanlde any context flags */
165 copy_to_user_ret((drm_ctx_t
*)arg
, &ctx
, sizeof(ctx
), -EFAULT
);
169 int r128_switchctx(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
172 drm_file_t
*priv
= filp
->private_data
;
173 drm_device_t
*dev
= priv
->dev
;
176 copy_from_user_ret(&ctx
, (drm_ctx_t
*)arg
, sizeof(ctx
), -EFAULT
);
177 DRM_DEBUG("%d\n", ctx
.handle
);
178 return r128_context_switch(dev
, dev
->last_context
, ctx
.handle
);
181 int r128_newctx(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
184 drm_file_t
*priv
= filp
->private_data
;
185 drm_device_t
*dev
= priv
->dev
;
188 copy_from_user_ret(&ctx
, (drm_ctx_t
*)arg
, sizeof(ctx
), -EFAULT
);
189 DRM_DEBUG("%d\n", ctx
.handle
);
190 r128_context_switch_complete(dev
, ctx
.handle
);
195 int r128_rmctx(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
198 drm_file_t
*priv
= filp
->private_data
;
199 drm_device_t
*dev
= priv
->dev
;
202 copy_from_user_ret(&ctx
, (drm_ctx_t
*)arg
, sizeof(ctx
), -EFAULT
);
203 DRM_DEBUG("%d\n", ctx
.handle
);
204 drm_ctxbitmap_free(dev
, ctx
.handle
);