xref: /openbsd-src/sys/dev/pci/drm/drm_context.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /* $OpenBSD: drm_context.c,v 1.15 2011/06/02 18:22:00 weerd Exp $ */
2 /*-
3  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
4  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  *
26  * Authors:
27  *    Rickard E. (Rik) Faith <faith@valinux.com>
28  *    Gareth Hughes <gareth@valinux.com>
29  *
30  */
31 
32 /** @file drm_context.c
33  * Implementation of the context management ioctls.
34  */
35 
36 #include "drmP.h"
37 
38 /* ================================================================
39  * Context bitmap support
40  */
41 
42 void
43 drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle)
44 {
45 	if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP ||
46 	    dev->ctx_bitmap == NULL) {
47 		DRM_ERROR("Attempt to free invalid context handle: %d\n",
48 		   ctx_handle);
49 		return;
50 	}
51 
52 	DRM_LOCK();
53 	clear_bit(ctx_handle, dev->ctx_bitmap);
54 	DRM_UNLOCK();
55 	return;
56 }
57 
58 int
59 drm_ctxbitmap_next(struct drm_device *dev)
60 {
61 	int	bit;
62 
63 	if (dev->ctx_bitmap == NULL)
64 		return (-1);
65 
66 	DRM_LOCK();
67 	bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP);
68 	if (bit >= DRM_MAX_CTXBITMAP) {
69 		DRM_UNLOCK();
70 		return (-1);
71 	}
72 
73 	set_bit(bit, dev->ctx_bitmap);
74 	DRM_DEBUG("drm_ctxbitmap_next bit : %d\n", bit);
75 	DRM_UNLOCK();
76 	return (bit);
77 }
78 
79 int
80 drm_ctxbitmap_init(struct drm_device *dev)
81 {
82 	atomic_t	*bitmap;
83 	int		 i, temp;
84 
85 
86 	if ((bitmap = drm_calloc(1, PAGE_SIZE)) == NULL)
87 		return (ENOMEM);
88 	DRM_LOCK();
89 	dev->ctx_bitmap = bitmap;
90 	DRM_UNLOCK();
91 
92 	for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
93 		temp = drm_ctxbitmap_next(dev);
94 	   	DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
95 	}
96 
97 	return (0);
98 }
99 
100 void
101 drm_ctxbitmap_cleanup(struct drm_device *dev)
102 {
103 	atomic_t *bitmap;
104 
105 	DRM_LOCK();
106 	bitmap = dev->ctx_bitmap;
107 	dev->ctx_bitmap = NULL;
108 	DRM_UNLOCK();
109 	drm_free(bitmap);
110 }
111 
112 int
113 drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
114 {
115 	struct drm_ctx_res	*res = data;
116 	struct drm_ctx		 ctx;
117 	int			 i;
118 
119 	if (res->count >= DRM_RESERVED_CONTEXTS) {
120 		bzero(&ctx, sizeof(ctx));
121 		for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
122 			ctx.handle = i;
123 			if (DRM_COPY_TO_USER(&res->contexts[i],
124 			    &ctx, sizeof(ctx)))
125 				return (EFAULT);
126 		}
127 	}
128 	res->count = DRM_RESERVED_CONTEXTS;
129 
130 	return (0);
131 }
132 
133 int
134 drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
135 {
136 	struct drm_ctx	*ctx = data;
137 
138 	ctx->handle = drm_ctxbitmap_next(dev);
139 	if (ctx->handle == DRM_KERNEL_CONTEXT) {
140 		/* Skip kernel's context and get a new one. */
141 		ctx->handle = drm_ctxbitmap_next(dev);
142 	}
143 	DRM_DEBUG("%d\n", ctx->handle);
144 	if (ctx->handle == -1) {
145 		DRM_DEBUG("Not enough free contexts.\n");
146 		/* Should this return -EBUSY instead? */
147 		return (ENOMEM);
148 	}
149 
150 	return (0);
151 }
152 
153 int
154 drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
155 {
156 	struct drm_ctx	*ctx = data;
157 
158 	/* This is 0, because we don't handle any context flags */
159 	ctx->flags = 0;
160 
161 	return (0);
162 }
163 
164 int
165 drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
166 {
167 	struct drm_ctx	*ctx = data;
168 
169 	DRM_DEBUG("%d\n", ctx->handle);
170 	if (ctx->handle != DRM_KERNEL_CONTEXT)
171 		drm_ctxbitmap_free(dev, ctx->handle);
172 
173 	return (0);
174 }
175