xref: /freebsd/sys/dev/drm2/drm_context.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1*592ffb21SWarner Losh /**
2*592ffb21SWarner Losh  * \file drm_context.c
3*592ffb21SWarner Losh  * IOCTLs for generic contexts
4*592ffb21SWarner Losh  *
5*592ffb21SWarner Losh  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6*592ffb21SWarner Losh  * \author Gareth Hughes <gareth@valinux.com>
7*592ffb21SWarner Losh  */
8*592ffb21SWarner Losh 
9*592ffb21SWarner Losh /*
10*592ffb21SWarner Losh  * Created: Fri Nov 24 18:31:37 2000 by gareth@valinux.com
11*592ffb21SWarner Losh  *
12*592ffb21SWarner Losh  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13*592ffb21SWarner Losh  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14*592ffb21SWarner Losh  * All Rights Reserved.
15*592ffb21SWarner Losh  *
16*592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
17*592ffb21SWarner Losh  * copy of this software and associated documentation files (the "Software"),
18*592ffb21SWarner Losh  * to deal in the Software without restriction, including without limitation
19*592ffb21SWarner Losh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20*592ffb21SWarner Losh  * and/or sell copies of the Software, and to permit persons to whom the
21*592ffb21SWarner Losh  * Software is furnished to do so, subject to the following conditions:
22*592ffb21SWarner Losh  *
23*592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the next
24*592ffb21SWarner Losh  * paragraph) shall be included in all copies or substantial portions of the
25*592ffb21SWarner Losh  * Software.
26*592ffb21SWarner Losh  *
27*592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28*592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29*592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30*592ffb21SWarner Losh  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31*592ffb21SWarner Losh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32*592ffb21SWarner Losh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33*592ffb21SWarner Losh  * OTHER DEALINGS IN THE SOFTWARE.
34*592ffb21SWarner Losh  */
35*592ffb21SWarner Losh 
36*592ffb21SWarner Losh #include <sys/cdefs.h>
37*592ffb21SWarner Losh /*
38*592ffb21SWarner Losh  * ChangeLog:
39*592ffb21SWarner Losh  *  2001-11-16	Torsten Duwe <duwe@caldera.de>
40*592ffb21SWarner Losh  *		added context constructor/destructor hooks,
41*592ffb21SWarner Losh  *		needed by SiS driver's memory management.
42*592ffb21SWarner Losh  */
43*592ffb21SWarner Losh 
44*592ffb21SWarner Losh #include <dev/drm2/drmP.h>
45*592ffb21SWarner Losh 
46*592ffb21SWarner Losh /******************************************************************/
47*592ffb21SWarner Losh /** \name Context bitmap support */
48*592ffb21SWarner Losh /*@{*/
49*592ffb21SWarner Losh 
50*592ffb21SWarner Losh /**
51*592ffb21SWarner Losh  * Free a handle from the context bitmap.
52*592ffb21SWarner Losh  *
53*592ffb21SWarner Losh  * \param dev DRM device.
54*592ffb21SWarner Losh  * \param ctx_handle context handle.
55*592ffb21SWarner Losh  *
56*592ffb21SWarner Losh  * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
57*592ffb21SWarner Losh  * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
58*592ffb21SWarner Losh  * lock.
59*592ffb21SWarner Losh  */
drm_ctxbitmap_free(struct drm_device * dev,int ctx_handle)60*592ffb21SWarner Losh void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
61*592ffb21SWarner Losh {
62*592ffb21SWarner Losh 	if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP ||
63*592ffb21SWarner Losh 	    dev->ctx_bitmap == NULL) {
64*592ffb21SWarner Losh 		DRM_ERROR("Attempt to free invalid context handle: %d\n",
65*592ffb21SWarner Losh 		   ctx_handle);
66*592ffb21SWarner Losh 		return;
67*592ffb21SWarner Losh 	}
68*592ffb21SWarner Losh 
69*592ffb21SWarner Losh 	DRM_LOCK(dev);
70*592ffb21SWarner Losh 	clear_bit(ctx_handle, dev->ctx_bitmap);
71*592ffb21SWarner Losh 	dev->context_sareas[ctx_handle] = NULL;
72*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
73*592ffb21SWarner Losh }
74*592ffb21SWarner Losh 
75*592ffb21SWarner Losh /**
76*592ffb21SWarner Losh  * Context bitmap allocation.
77*592ffb21SWarner Losh  *
78*592ffb21SWarner Losh  * \param dev DRM device.
79*592ffb21SWarner Losh  * \return (non-negative) context handle on success or a negative number on failure.
80*592ffb21SWarner Losh  *
81*592ffb21SWarner Losh  * Allocate a new idr from drm_device::ctx_idr while holding the
82*592ffb21SWarner Losh  * drm_device::struct_mutex lock.
83*592ffb21SWarner Losh  */
drm_ctxbitmap_next(struct drm_device * dev)84*592ffb21SWarner Losh static int drm_ctxbitmap_next(struct drm_device * dev)
85*592ffb21SWarner Losh {
86*592ffb21SWarner Losh 	int bit;
87*592ffb21SWarner Losh 
88*592ffb21SWarner Losh 	if (dev->ctx_bitmap == NULL)
89*592ffb21SWarner Losh 		return -1;
90*592ffb21SWarner Losh 
91*592ffb21SWarner Losh 	DRM_LOCK(dev);
92*592ffb21SWarner Losh 	bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP);
93*592ffb21SWarner Losh 	if (bit >= DRM_MAX_CTXBITMAP) {
94*592ffb21SWarner Losh 		DRM_UNLOCK(dev);
95*592ffb21SWarner Losh 		return -1;
96*592ffb21SWarner Losh 	}
97*592ffb21SWarner Losh 
98*592ffb21SWarner Losh 	set_bit(bit, dev->ctx_bitmap);
99*592ffb21SWarner Losh 	DRM_DEBUG("bit : %d\n", bit);
100*592ffb21SWarner Losh 	if ((bit+1) > dev->max_context) {
101*592ffb21SWarner Losh 		struct drm_local_map **ctx_sareas;
102*592ffb21SWarner Losh 		int max_ctx = (bit+1);
103*592ffb21SWarner Losh 
104*592ffb21SWarner Losh 		ctx_sareas = realloc(dev->context_sareas,
105*592ffb21SWarner Losh 		    max_ctx * sizeof(*dev->context_sareas),
106*592ffb21SWarner Losh 		    DRM_MEM_SAREA, M_NOWAIT);
107*592ffb21SWarner Losh 		if (ctx_sareas == NULL) {
108*592ffb21SWarner Losh 			clear_bit(bit, dev->ctx_bitmap);
109*592ffb21SWarner Losh 			DRM_DEBUG("failed to allocate bit : %d\n", bit);
110*592ffb21SWarner Losh 			DRM_UNLOCK(dev);
111*592ffb21SWarner Losh 			return -1;
112*592ffb21SWarner Losh 		}
113*592ffb21SWarner Losh 		dev->max_context = max_ctx;
114*592ffb21SWarner Losh 		dev->context_sareas = ctx_sareas;
115*592ffb21SWarner Losh 		dev->context_sareas[bit] = NULL;
116*592ffb21SWarner Losh 	}
117*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
118*592ffb21SWarner Losh 	return bit;
119*592ffb21SWarner Losh }
120*592ffb21SWarner Losh 
121*592ffb21SWarner Losh /**
122*592ffb21SWarner Losh  * Context bitmap initialization.
123*592ffb21SWarner Losh  *
124*592ffb21SWarner Losh  * \param dev DRM device.
125*592ffb21SWarner Losh  *
126*592ffb21SWarner Losh  * Initialise the drm_device::ctx_idr
127*592ffb21SWarner Losh  */
drm_ctxbitmap_init(struct drm_device * dev)128*592ffb21SWarner Losh int drm_ctxbitmap_init(struct drm_device * dev)
129*592ffb21SWarner Losh {
130*592ffb21SWarner Losh 	int i;
131*592ffb21SWarner Losh    	int temp;
132*592ffb21SWarner Losh 
133*592ffb21SWarner Losh 	DRM_LOCK(dev);
134*592ffb21SWarner Losh 	dev->ctx_bitmap = malloc(PAGE_SIZE, DRM_MEM_CTXBITMAP,
135*592ffb21SWarner Losh 	    M_NOWAIT | M_ZERO);
136*592ffb21SWarner Losh 	if (dev->ctx_bitmap == NULL) {
137*592ffb21SWarner Losh 		DRM_UNLOCK(dev);
138*592ffb21SWarner Losh 		return ENOMEM;
139*592ffb21SWarner Losh 	}
140*592ffb21SWarner Losh 	dev->context_sareas = NULL;
141*592ffb21SWarner Losh 	dev->max_context = -1;
142*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
143*592ffb21SWarner Losh 
144*592ffb21SWarner Losh 	for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
145*592ffb21SWarner Losh 		temp = drm_ctxbitmap_next(dev);
146*592ffb21SWarner Losh 		DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
147*592ffb21SWarner Losh 	}
148*592ffb21SWarner Losh 
149*592ffb21SWarner Losh 	return 0;
150*592ffb21SWarner Losh }
151*592ffb21SWarner Losh 
152*592ffb21SWarner Losh /**
153*592ffb21SWarner Losh  * Context bitmap cleanup.
154*592ffb21SWarner Losh  *
155*592ffb21SWarner Losh  * \param dev DRM device.
156*592ffb21SWarner Losh  *
157*592ffb21SWarner Losh  * Free all idr members using drm_ctx_sarea_free helper function
158*592ffb21SWarner Losh  * while holding the drm_device::struct_mutex lock.
159*592ffb21SWarner Losh  */
drm_ctxbitmap_cleanup(struct drm_device * dev)160*592ffb21SWarner Losh void drm_ctxbitmap_cleanup(struct drm_device * dev)
161*592ffb21SWarner Losh {
162*592ffb21SWarner Losh 	DRM_LOCK(dev);
163*592ffb21SWarner Losh 	if (dev->context_sareas != NULL)
164*592ffb21SWarner Losh 		free(dev->context_sareas, DRM_MEM_SAREA);
165*592ffb21SWarner Losh 	free(dev->ctx_bitmap, DRM_MEM_CTXBITMAP);
166*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
167*592ffb21SWarner Losh }
168*592ffb21SWarner Losh 
169*592ffb21SWarner Losh /*@}*/
170*592ffb21SWarner Losh 
171*592ffb21SWarner Losh /******************************************************************/
172*592ffb21SWarner Losh /** \name Per Context SAREA Support */
173*592ffb21SWarner Losh /*@{*/
174*592ffb21SWarner Losh 
175*592ffb21SWarner Losh /**
176*592ffb21SWarner Losh  * Get per-context SAREA.
177*592ffb21SWarner Losh  *
178*592ffb21SWarner Losh  * \param inode device inode.
179*592ffb21SWarner Losh  * \param file_priv DRM file private.
180*592ffb21SWarner Losh  * \param cmd command.
181*592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx_priv_map structure.
182*592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
183*592ffb21SWarner Losh  *
184*592ffb21SWarner Losh  * Gets the map from drm_device::ctx_idr with the handle specified and
185*592ffb21SWarner Losh  * returns its handle.
186*592ffb21SWarner Losh  */
drm_getsareactx(struct drm_device * dev,void * data,struct drm_file * file_priv)187*592ffb21SWarner Losh int drm_getsareactx(struct drm_device *dev, void *data,
188*592ffb21SWarner Losh 		    struct drm_file *file_priv)
189*592ffb21SWarner Losh {
190*592ffb21SWarner Losh 	struct drm_ctx_priv_map *request = data;
191*592ffb21SWarner Losh 	struct drm_local_map *map;
192*592ffb21SWarner Losh 
193*592ffb21SWarner Losh 	DRM_LOCK(dev);
194*592ffb21SWarner Losh 	if (dev->max_context < 0 ||
195*592ffb21SWarner Losh 	    request->ctx_id >= (unsigned) dev->max_context) {
196*592ffb21SWarner Losh 		DRM_UNLOCK(dev);
197*592ffb21SWarner Losh 		return EINVAL;
198*592ffb21SWarner Losh 	}
199*592ffb21SWarner Losh 
200*592ffb21SWarner Losh 	map = dev->context_sareas[request->ctx_id];
201*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
202*592ffb21SWarner Losh 
203*592ffb21SWarner Losh 	request->handle = (void *)map->handle;
204*592ffb21SWarner Losh 
205*592ffb21SWarner Losh 	return 0;
206*592ffb21SWarner Losh }
207*592ffb21SWarner Losh 
208*592ffb21SWarner Losh /**
209*592ffb21SWarner Losh  * Set per-context SAREA.
210*592ffb21SWarner Losh  *
211*592ffb21SWarner Losh  * \param inode device inode.
212*592ffb21SWarner Losh  * \param file_priv DRM file private.
213*592ffb21SWarner Losh  * \param cmd command.
214*592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx_priv_map structure.
215*592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
216*592ffb21SWarner Losh  *
217*592ffb21SWarner Losh  * Searches the mapping specified in \p arg and update the entry in
218*592ffb21SWarner Losh  * drm_device::ctx_idr with it.
219*592ffb21SWarner Losh  */
drm_setsareactx(struct drm_device * dev,void * data,struct drm_file * file_priv)220*592ffb21SWarner Losh int drm_setsareactx(struct drm_device *dev, void *data,
221*592ffb21SWarner Losh 		    struct drm_file *file_priv)
222*592ffb21SWarner Losh {
223*592ffb21SWarner Losh 	struct drm_ctx_priv_map *request = data;
224*592ffb21SWarner Losh 	struct drm_local_map *map = NULL;
225*592ffb21SWarner Losh 	struct drm_map_list *r_list = NULL;
226*592ffb21SWarner Losh 
227*592ffb21SWarner Losh 	DRM_LOCK(dev);
228*592ffb21SWarner Losh 	list_for_each_entry(r_list, &dev->maplist, head) {
229*592ffb21SWarner Losh 		if (r_list->map
230*592ffb21SWarner Losh 		    && r_list->user_token == (unsigned long) request->handle) {
231*592ffb21SWarner Losh 			if (dev->max_context < 0)
232*592ffb21SWarner Losh 				goto bad;
233*592ffb21SWarner Losh 			if (request->ctx_id >= (unsigned) dev->max_context)
234*592ffb21SWarner Losh 				goto bad;
235*592ffb21SWarner Losh 			dev->context_sareas[request->ctx_id] = map;
236*592ffb21SWarner Losh 			DRM_UNLOCK(dev);
237*592ffb21SWarner Losh 			return 0;
238*592ffb21SWarner Losh 		}
239*592ffb21SWarner Losh 	}
240*592ffb21SWarner Losh 
241*592ffb21SWarner Losh bad:
242*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
243*592ffb21SWarner Losh 	return EINVAL;
244*592ffb21SWarner Losh }
245*592ffb21SWarner Losh 
246*592ffb21SWarner Losh /*@}*/
247*592ffb21SWarner Losh 
248*592ffb21SWarner Losh /******************************************************************/
249*592ffb21SWarner Losh /** \name The actual DRM context handling routines */
250*592ffb21SWarner Losh /*@{*/
251*592ffb21SWarner Losh 
252*592ffb21SWarner Losh /**
253*592ffb21SWarner Losh  * Switch context.
254*592ffb21SWarner Losh  *
255*592ffb21SWarner Losh  * \param dev DRM device.
256*592ffb21SWarner Losh  * \param old old context handle.
257*592ffb21SWarner Losh  * \param new new context handle.
258*592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
259*592ffb21SWarner Losh  *
260*592ffb21SWarner Losh  * Attempt to set drm_device::context_flag.
261*592ffb21SWarner Losh  */
drm_context_switch(struct drm_device * dev,int old,int new)262*592ffb21SWarner Losh static int drm_context_switch(struct drm_device * dev, int old, int new)
263*592ffb21SWarner Losh {
264*592ffb21SWarner Losh 	if (test_and_set_bit(0, &dev->context_flag)) {
265*592ffb21SWarner Losh 		DRM_ERROR("Reentering -- FIXME\n");
266*592ffb21SWarner Losh 		return -EBUSY;
267*592ffb21SWarner Losh 	}
268*592ffb21SWarner Losh 
269*592ffb21SWarner Losh 	DRM_DEBUG("Context switch from %d to %d\n", old, new);
270*592ffb21SWarner Losh 
271*592ffb21SWarner Losh 	if (new == dev->last_context) {
272*592ffb21SWarner Losh 		clear_bit(0, &dev->context_flag);
273*592ffb21SWarner Losh 		return 0;
274*592ffb21SWarner Losh 	}
275*592ffb21SWarner Losh 
276*592ffb21SWarner Losh 	return 0;
277*592ffb21SWarner Losh }
278*592ffb21SWarner Losh 
279*592ffb21SWarner Losh /**
280*592ffb21SWarner Losh  * Complete context switch.
281*592ffb21SWarner Losh  *
282*592ffb21SWarner Losh  * \param dev DRM device.
283*592ffb21SWarner Losh  * \param new new context handle.
284*592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
285*592ffb21SWarner Losh  *
286*592ffb21SWarner Losh  * Updates drm_device::last_context and drm_device::last_switch. Verifies the
287*592ffb21SWarner Losh  * hardware lock is held, clears the drm_device::context_flag and wakes up
288*592ffb21SWarner Losh  * drm_device::context_wait.
289*592ffb21SWarner Losh  */
drm_context_switch_complete(struct drm_device * dev,struct drm_file * file_priv,int new)290*592ffb21SWarner Losh static int drm_context_switch_complete(struct drm_device *dev,
291*592ffb21SWarner Losh 				       struct drm_file *file_priv, int new)
292*592ffb21SWarner Losh {
293*592ffb21SWarner Losh 	dev->last_context = new;	/* PRE/POST: This is the _only_ writer. */
294*592ffb21SWarner Losh 	dev->last_switch = jiffies;
295*592ffb21SWarner Losh 
296*592ffb21SWarner Losh 	if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
297*592ffb21SWarner Losh 		DRM_ERROR("Lock isn't held after context switch\n");
298*592ffb21SWarner Losh 	}
299*592ffb21SWarner Losh 
300*592ffb21SWarner Losh 	/* If a context switch is ever initiated
301*592ffb21SWarner Losh 	   when the kernel holds the lock, release
302*592ffb21SWarner Losh 	   that lock here. */
303*592ffb21SWarner Losh 	clear_bit(0, &dev->context_flag);
304*592ffb21SWarner Losh 	wakeup(&dev->context_wait);
305*592ffb21SWarner Losh 
306*592ffb21SWarner Losh 	return 0;
307*592ffb21SWarner Losh }
308*592ffb21SWarner Losh 
309*592ffb21SWarner Losh /**
310*592ffb21SWarner Losh  * Reserve contexts.
311*592ffb21SWarner Losh  *
312*592ffb21SWarner Losh  * \param inode device inode.
313*592ffb21SWarner Losh  * \param file_priv DRM file private.
314*592ffb21SWarner Losh  * \param cmd command.
315*592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx_res structure.
316*592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
317*592ffb21SWarner Losh  */
drm_resctx(struct drm_device * dev,void * data,struct drm_file * file_priv)318*592ffb21SWarner Losh int drm_resctx(struct drm_device *dev, void *data,
319*592ffb21SWarner Losh 	       struct drm_file *file_priv)
320*592ffb21SWarner Losh {
321*592ffb21SWarner Losh 	struct drm_ctx_res *res = data;
322*592ffb21SWarner Losh 	struct drm_ctx ctx;
323*592ffb21SWarner Losh 	int i;
324*592ffb21SWarner Losh 
325*592ffb21SWarner Losh 	if (res->count >= DRM_RESERVED_CONTEXTS) {
326*592ffb21SWarner Losh 		memset(&ctx, 0, sizeof(ctx));
327*592ffb21SWarner Losh 		for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
328*592ffb21SWarner Losh 			ctx.handle = i;
329*592ffb21SWarner Losh 			if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
330*592ffb21SWarner Losh 				return -EFAULT;
331*592ffb21SWarner Losh 		}
332*592ffb21SWarner Losh 	}
333*592ffb21SWarner Losh 	res->count = DRM_RESERVED_CONTEXTS;
334*592ffb21SWarner Losh 
335*592ffb21SWarner Losh 	return 0;
336*592ffb21SWarner Losh }
337*592ffb21SWarner Losh 
338*592ffb21SWarner Losh /**
339*592ffb21SWarner Losh  * Add context.
340*592ffb21SWarner Losh  *
341*592ffb21SWarner Losh  * \param inode device inode.
342*592ffb21SWarner Losh  * \param file_priv DRM file private.
343*592ffb21SWarner Losh  * \param cmd command.
344*592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx structure.
345*592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
346*592ffb21SWarner Losh  *
347*592ffb21SWarner Losh  * Get a new handle for the context and copy to userspace.
348*592ffb21SWarner Losh  */
drm_addctx(struct drm_device * dev,void * data,struct drm_file * file_priv)349*592ffb21SWarner Losh int drm_addctx(struct drm_device *dev, void *data,
350*592ffb21SWarner Losh 	       struct drm_file *file_priv)
351*592ffb21SWarner Losh {
352*592ffb21SWarner Losh 	struct drm_ctx_list *ctx_entry;
353*592ffb21SWarner Losh 	struct drm_ctx *ctx = data;
354*592ffb21SWarner Losh 
355*592ffb21SWarner Losh 	ctx->handle = drm_ctxbitmap_next(dev);
356*592ffb21SWarner Losh 	if (ctx->handle == DRM_KERNEL_CONTEXT) {
357*592ffb21SWarner Losh 		/* Skip kernel's context and get a new one. */
358*592ffb21SWarner Losh 		ctx->handle = drm_ctxbitmap_next(dev);
359*592ffb21SWarner Losh 	}
360*592ffb21SWarner Losh 	DRM_DEBUG("%d\n", ctx->handle);
361*592ffb21SWarner Losh 	if (ctx->handle == -1) {
362*592ffb21SWarner Losh 		DRM_DEBUG("Not enough free contexts.\n");
363*592ffb21SWarner Losh 		/* Should this return -EBUSY instead? */
364*592ffb21SWarner Losh 		return -ENOMEM;
365*592ffb21SWarner Losh 	}
366*592ffb21SWarner Losh 
367*592ffb21SWarner Losh 	ctx_entry = malloc(sizeof(*ctx_entry), DRM_MEM_CTXBITMAP, M_NOWAIT);
368*592ffb21SWarner Losh 	if (!ctx_entry) {
369*592ffb21SWarner Losh 		DRM_DEBUG("out of memory\n");
370*592ffb21SWarner Losh 		return -ENOMEM;
371*592ffb21SWarner Losh 	}
372*592ffb21SWarner Losh 
373*592ffb21SWarner Losh 	INIT_LIST_HEAD(&ctx_entry->head);
374*592ffb21SWarner Losh 	ctx_entry->handle = ctx->handle;
375*592ffb21SWarner Losh 	ctx_entry->tag = file_priv;
376*592ffb21SWarner Losh 
377*592ffb21SWarner Losh 	DRM_LOCK(dev);
378*592ffb21SWarner Losh 	list_add(&ctx_entry->head, &dev->ctxlist);
379*592ffb21SWarner Losh 	++dev->ctx_count;
380*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
381*592ffb21SWarner Losh 
382*592ffb21SWarner Losh 	return 0;
383*592ffb21SWarner Losh }
384*592ffb21SWarner Losh 
drm_modctx(struct drm_device * dev,void * data,struct drm_file * file_priv)385*592ffb21SWarner Losh int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
386*592ffb21SWarner Losh {
387*592ffb21SWarner Losh 	/* This does nothing */
388*592ffb21SWarner Losh 	return 0;
389*592ffb21SWarner Losh }
390*592ffb21SWarner Losh 
391*592ffb21SWarner Losh /**
392*592ffb21SWarner Losh  * Get context.
393*592ffb21SWarner Losh  *
394*592ffb21SWarner Losh  * \param inode device inode.
395*592ffb21SWarner Losh  * \param file_priv DRM file private.
396*592ffb21SWarner Losh  * \param cmd command.
397*592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx structure.
398*592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
399*592ffb21SWarner Losh  */
drm_getctx(struct drm_device * dev,void * data,struct drm_file * file_priv)400*592ffb21SWarner Losh int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
401*592ffb21SWarner Losh {
402*592ffb21SWarner Losh 	struct drm_ctx *ctx = data;
403*592ffb21SWarner Losh 
404*592ffb21SWarner Losh 	/* This is 0, because we don't handle any context flags */
405*592ffb21SWarner Losh 	ctx->flags = 0;
406*592ffb21SWarner Losh 
407*592ffb21SWarner Losh 	return 0;
408*592ffb21SWarner Losh }
409*592ffb21SWarner Losh 
410*592ffb21SWarner Losh /**
411*592ffb21SWarner Losh  * Switch context.
412*592ffb21SWarner Losh  *
413*592ffb21SWarner Losh  * \param inode device inode.
414*592ffb21SWarner Losh  * \param file_priv DRM file private.
415*592ffb21SWarner Losh  * \param cmd command.
416*592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx structure.
417*592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
418*592ffb21SWarner Losh  *
419*592ffb21SWarner Losh  * Calls context_switch().
420*592ffb21SWarner Losh  */
drm_switchctx(struct drm_device * dev,void * data,struct drm_file * file_priv)421*592ffb21SWarner Losh int drm_switchctx(struct drm_device *dev, void *data,
422*592ffb21SWarner Losh 		  struct drm_file *file_priv)
423*592ffb21SWarner Losh {
424*592ffb21SWarner Losh 	struct drm_ctx *ctx = data;
425*592ffb21SWarner Losh 
426*592ffb21SWarner Losh 	DRM_DEBUG("%d\n", ctx->handle);
427*592ffb21SWarner Losh 	return drm_context_switch(dev, dev->last_context, ctx->handle);
428*592ffb21SWarner Losh }
429*592ffb21SWarner Losh 
430*592ffb21SWarner Losh /**
431*592ffb21SWarner Losh  * New context.
432*592ffb21SWarner Losh  *
433*592ffb21SWarner Losh  * \param inode device inode.
434*592ffb21SWarner Losh  * \param file_priv DRM file private.
435*592ffb21SWarner Losh  * \param cmd command.
436*592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx structure.
437*592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
438*592ffb21SWarner Losh  *
439*592ffb21SWarner Losh  * Calls context_switch_complete().
440*592ffb21SWarner Losh  */
drm_newctx(struct drm_device * dev,void * data,struct drm_file * file_priv)441*592ffb21SWarner Losh int drm_newctx(struct drm_device *dev, void *data,
442*592ffb21SWarner Losh 	       struct drm_file *file_priv)
443*592ffb21SWarner Losh {
444*592ffb21SWarner Losh 	struct drm_ctx *ctx = data;
445*592ffb21SWarner Losh 
446*592ffb21SWarner Losh 	DRM_DEBUG("%d\n", ctx->handle);
447*592ffb21SWarner Losh 	drm_context_switch_complete(dev, file_priv, ctx->handle);
448*592ffb21SWarner Losh 
449*592ffb21SWarner Losh 	return 0;
450*592ffb21SWarner Losh }
451*592ffb21SWarner Losh 
452*592ffb21SWarner Losh /**
453*592ffb21SWarner Losh  * Remove context.
454*592ffb21SWarner Losh  *
455*592ffb21SWarner Losh  * \param inode device inode.
456*592ffb21SWarner Losh  * \param file_priv DRM file private.
457*592ffb21SWarner Losh  * \param cmd command.
458*592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx structure.
459*592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
460*592ffb21SWarner Losh  *
461*592ffb21SWarner Losh  * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
462*592ffb21SWarner Losh  */
drm_rmctx(struct drm_device * dev,void * data,struct drm_file * file_priv)463*592ffb21SWarner Losh int drm_rmctx(struct drm_device *dev, void *data,
464*592ffb21SWarner Losh 	      struct drm_file *file_priv)
465*592ffb21SWarner Losh {
466*592ffb21SWarner Losh 	struct drm_ctx *ctx = data;
467*592ffb21SWarner Losh 
468*592ffb21SWarner Losh 	DRM_DEBUG("%d\n", ctx->handle);
469*592ffb21SWarner Losh 	if (ctx->handle != DRM_KERNEL_CONTEXT) {
470*592ffb21SWarner Losh 		if (dev->driver->context_dtor)
471*592ffb21SWarner Losh 			dev->driver->context_dtor(dev, ctx->handle);
472*592ffb21SWarner Losh 		drm_ctxbitmap_free(dev, ctx->handle);
473*592ffb21SWarner Losh 	}
474*592ffb21SWarner Losh 
475*592ffb21SWarner Losh 	DRM_LOCK(dev);
476*592ffb21SWarner Losh 	if (!list_empty(&dev->ctxlist)) {
477*592ffb21SWarner Losh 		struct drm_ctx_list *pos, *n;
478*592ffb21SWarner Losh 
479*592ffb21SWarner Losh 		list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
480*592ffb21SWarner Losh 			if (pos->handle == ctx->handle) {
481*592ffb21SWarner Losh 				list_del(&pos->head);
482*592ffb21SWarner Losh 				free(pos, DRM_MEM_CTXBITMAP);
483*592ffb21SWarner Losh 				--dev->ctx_count;
484*592ffb21SWarner Losh 			}
485*592ffb21SWarner Losh 		}
486*592ffb21SWarner Losh 	}
487*592ffb21SWarner Losh 	DRM_UNLOCK(dev);
488*592ffb21SWarner Losh 
489*592ffb21SWarner Losh 	return 0;
490*592ffb21SWarner Losh }
491*592ffb21SWarner Losh 
492*592ffb21SWarner Losh /*@}*/
493