xref: /freebsd/sys/dev/drm2/drm_context.c (revision 685dc743)
1592ffb21SWarner Losh /**
2592ffb21SWarner Losh  * \file drm_context.c
3592ffb21SWarner Losh  * IOCTLs for generic contexts
4592ffb21SWarner Losh  *
5592ffb21SWarner Losh  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6592ffb21SWarner Losh  * \author Gareth Hughes <gareth@valinux.com>
7592ffb21SWarner Losh  */
8592ffb21SWarner Losh 
9592ffb21SWarner Losh /*
10592ffb21SWarner Losh  * Created: Fri Nov 24 18:31:37 2000 by gareth@valinux.com
11592ffb21SWarner Losh  *
12592ffb21SWarner Losh  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13592ffb21SWarner Losh  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14592ffb21SWarner Losh  * All Rights Reserved.
15592ffb21SWarner Losh  *
16592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
17592ffb21SWarner Losh  * copy of this software and associated documentation files (the "Software"),
18592ffb21SWarner Losh  * to deal in the Software without restriction, including without limitation
19592ffb21SWarner Losh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20592ffb21SWarner Losh  * and/or sell copies of the Software, and to permit persons to whom the
21592ffb21SWarner Losh  * Software is furnished to do so, subject to the following conditions:
22592ffb21SWarner Losh  *
23592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the next
24592ffb21SWarner Losh  * paragraph) shall be included in all copies or substantial portions of the
25592ffb21SWarner Losh  * Software.
26592ffb21SWarner Losh  *
27592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30592ffb21SWarner Losh  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31592ffb21SWarner Losh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32592ffb21SWarner Losh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33592ffb21SWarner Losh  * OTHER DEALINGS IN THE SOFTWARE.
34592ffb21SWarner Losh  */
35592ffb21SWarner Losh 
36592ffb21SWarner Losh #include <sys/cdefs.h>
37592ffb21SWarner Losh /*
38592ffb21SWarner Losh  * ChangeLog:
39592ffb21SWarner Losh  *  2001-11-16	Torsten Duwe <duwe@caldera.de>
40592ffb21SWarner Losh  *		added context constructor/destructor hooks,
41592ffb21SWarner Losh  *		needed by SiS driver's memory management.
42592ffb21SWarner Losh  */
43592ffb21SWarner Losh 
44592ffb21SWarner Losh #include <dev/drm2/drmP.h>
45592ffb21SWarner Losh 
46592ffb21SWarner Losh /******************************************************************/
47592ffb21SWarner Losh /** \name Context bitmap support */
48592ffb21SWarner Losh /*@{*/
49592ffb21SWarner Losh 
50592ffb21SWarner Losh /**
51592ffb21SWarner Losh  * Free a handle from the context bitmap.
52592ffb21SWarner Losh  *
53592ffb21SWarner Losh  * \param dev DRM device.
54592ffb21SWarner Losh  * \param ctx_handle context handle.
55592ffb21SWarner Losh  *
56592ffb21SWarner Losh  * Clears the bit specified by \p ctx_handle in drm_device::ctx_bitmap and the entry
57592ffb21SWarner Losh  * in drm_device::ctx_idr, while holding the drm_device::struct_mutex
58592ffb21SWarner Losh  * lock.
59592ffb21SWarner Losh  */
drm_ctxbitmap_free(struct drm_device * dev,int ctx_handle)60592ffb21SWarner Losh void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
61592ffb21SWarner Losh {
62592ffb21SWarner Losh 	if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP ||
63592ffb21SWarner Losh 	    dev->ctx_bitmap == NULL) {
64592ffb21SWarner Losh 		DRM_ERROR("Attempt to free invalid context handle: %d\n",
65592ffb21SWarner Losh 		   ctx_handle);
66592ffb21SWarner Losh 		return;
67592ffb21SWarner Losh 	}
68592ffb21SWarner Losh 
69592ffb21SWarner Losh 	DRM_LOCK(dev);
70592ffb21SWarner Losh 	clear_bit(ctx_handle, dev->ctx_bitmap);
71592ffb21SWarner Losh 	dev->context_sareas[ctx_handle] = NULL;
72592ffb21SWarner Losh 	DRM_UNLOCK(dev);
73592ffb21SWarner Losh }
74592ffb21SWarner Losh 
75592ffb21SWarner Losh /**
76592ffb21SWarner Losh  * Context bitmap allocation.
77592ffb21SWarner Losh  *
78592ffb21SWarner Losh  * \param dev DRM device.
79592ffb21SWarner Losh  * \return (non-negative) context handle on success or a negative number on failure.
80592ffb21SWarner Losh  *
81592ffb21SWarner Losh  * Allocate a new idr from drm_device::ctx_idr while holding the
82592ffb21SWarner Losh  * drm_device::struct_mutex lock.
83592ffb21SWarner Losh  */
drm_ctxbitmap_next(struct drm_device * dev)84592ffb21SWarner Losh static int drm_ctxbitmap_next(struct drm_device * dev)
85592ffb21SWarner Losh {
86592ffb21SWarner Losh 	int bit;
87592ffb21SWarner Losh 
88592ffb21SWarner Losh 	if (dev->ctx_bitmap == NULL)
89592ffb21SWarner Losh 		return -1;
90592ffb21SWarner Losh 
91592ffb21SWarner Losh 	DRM_LOCK(dev);
92592ffb21SWarner Losh 	bit = find_first_zero_bit(dev->ctx_bitmap, DRM_MAX_CTXBITMAP);
93592ffb21SWarner Losh 	if (bit >= DRM_MAX_CTXBITMAP) {
94592ffb21SWarner Losh 		DRM_UNLOCK(dev);
95592ffb21SWarner Losh 		return -1;
96592ffb21SWarner Losh 	}
97592ffb21SWarner Losh 
98592ffb21SWarner Losh 	set_bit(bit, dev->ctx_bitmap);
99592ffb21SWarner Losh 	DRM_DEBUG("bit : %d\n", bit);
100592ffb21SWarner Losh 	if ((bit+1) > dev->max_context) {
101592ffb21SWarner Losh 		struct drm_local_map **ctx_sareas;
102592ffb21SWarner Losh 		int max_ctx = (bit+1);
103592ffb21SWarner Losh 
104592ffb21SWarner Losh 		ctx_sareas = realloc(dev->context_sareas,
105592ffb21SWarner Losh 		    max_ctx * sizeof(*dev->context_sareas),
106592ffb21SWarner Losh 		    DRM_MEM_SAREA, M_NOWAIT);
107592ffb21SWarner Losh 		if (ctx_sareas == NULL) {
108592ffb21SWarner Losh 			clear_bit(bit, dev->ctx_bitmap);
109592ffb21SWarner Losh 			DRM_DEBUG("failed to allocate bit : %d\n", bit);
110592ffb21SWarner Losh 			DRM_UNLOCK(dev);
111592ffb21SWarner Losh 			return -1;
112592ffb21SWarner Losh 		}
113592ffb21SWarner Losh 		dev->max_context = max_ctx;
114592ffb21SWarner Losh 		dev->context_sareas = ctx_sareas;
115592ffb21SWarner Losh 		dev->context_sareas[bit] = NULL;
116592ffb21SWarner Losh 	}
117592ffb21SWarner Losh 	DRM_UNLOCK(dev);
118592ffb21SWarner Losh 	return bit;
119592ffb21SWarner Losh }
120592ffb21SWarner Losh 
121592ffb21SWarner Losh /**
122592ffb21SWarner Losh  * Context bitmap initialization.
123592ffb21SWarner Losh  *
124592ffb21SWarner Losh  * \param dev DRM device.
125592ffb21SWarner Losh  *
126592ffb21SWarner Losh  * Initialise the drm_device::ctx_idr
127592ffb21SWarner Losh  */
drm_ctxbitmap_init(struct drm_device * dev)128592ffb21SWarner Losh int drm_ctxbitmap_init(struct drm_device * dev)
129592ffb21SWarner Losh {
130592ffb21SWarner Losh 	int i;
131592ffb21SWarner Losh    	int temp;
132592ffb21SWarner Losh 
133592ffb21SWarner Losh 	DRM_LOCK(dev);
134592ffb21SWarner Losh 	dev->ctx_bitmap = malloc(PAGE_SIZE, DRM_MEM_CTXBITMAP,
135592ffb21SWarner Losh 	    M_NOWAIT | M_ZERO);
136592ffb21SWarner Losh 	if (dev->ctx_bitmap == NULL) {
137592ffb21SWarner Losh 		DRM_UNLOCK(dev);
138592ffb21SWarner Losh 		return ENOMEM;
139592ffb21SWarner Losh 	}
140592ffb21SWarner Losh 	dev->context_sareas = NULL;
141592ffb21SWarner Losh 	dev->max_context = -1;
142592ffb21SWarner Losh 	DRM_UNLOCK(dev);
143592ffb21SWarner Losh 
144592ffb21SWarner Losh 	for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
145592ffb21SWarner Losh 		temp = drm_ctxbitmap_next(dev);
146592ffb21SWarner Losh 		DRM_DEBUG("drm_ctxbitmap_init : %d\n", temp);
147592ffb21SWarner Losh 	}
148592ffb21SWarner Losh 
149592ffb21SWarner Losh 	return 0;
150592ffb21SWarner Losh }
151592ffb21SWarner Losh 
152592ffb21SWarner Losh /**
153592ffb21SWarner Losh  * Context bitmap cleanup.
154592ffb21SWarner Losh  *
155592ffb21SWarner Losh  * \param dev DRM device.
156592ffb21SWarner Losh  *
157592ffb21SWarner Losh  * Free all idr members using drm_ctx_sarea_free helper function
158592ffb21SWarner Losh  * while holding the drm_device::struct_mutex lock.
159592ffb21SWarner Losh  */
drm_ctxbitmap_cleanup(struct drm_device * dev)160592ffb21SWarner Losh void drm_ctxbitmap_cleanup(struct drm_device * dev)
161592ffb21SWarner Losh {
162592ffb21SWarner Losh 	DRM_LOCK(dev);
163592ffb21SWarner Losh 	if (dev->context_sareas != NULL)
164592ffb21SWarner Losh 		free(dev->context_sareas, DRM_MEM_SAREA);
165592ffb21SWarner Losh 	free(dev->ctx_bitmap, DRM_MEM_CTXBITMAP);
166592ffb21SWarner Losh 	DRM_UNLOCK(dev);
167592ffb21SWarner Losh }
168592ffb21SWarner Losh 
169592ffb21SWarner Losh /*@}*/
170592ffb21SWarner Losh 
171592ffb21SWarner Losh /******************************************************************/
172592ffb21SWarner Losh /** \name Per Context SAREA Support */
173592ffb21SWarner Losh /*@{*/
174592ffb21SWarner Losh 
175592ffb21SWarner Losh /**
176592ffb21SWarner Losh  * Get per-context SAREA.
177592ffb21SWarner Losh  *
178592ffb21SWarner Losh  * \param inode device inode.
179592ffb21SWarner Losh  * \param file_priv DRM file private.
180592ffb21SWarner Losh  * \param cmd command.
181592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx_priv_map structure.
182592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
183592ffb21SWarner Losh  *
184592ffb21SWarner Losh  * Gets the map from drm_device::ctx_idr with the handle specified and
185592ffb21SWarner Losh  * returns its handle.
186592ffb21SWarner Losh  */
drm_getsareactx(struct drm_device * dev,void * data,struct drm_file * file_priv)187592ffb21SWarner Losh int drm_getsareactx(struct drm_device *dev, void *data,
188592ffb21SWarner Losh 		    struct drm_file *file_priv)
189592ffb21SWarner Losh {
190592ffb21SWarner Losh 	struct drm_ctx_priv_map *request = data;
191592ffb21SWarner Losh 	struct drm_local_map *map;
192592ffb21SWarner Losh 
193592ffb21SWarner Losh 	DRM_LOCK(dev);
194592ffb21SWarner Losh 	if (dev->max_context < 0 ||
195592ffb21SWarner Losh 	    request->ctx_id >= (unsigned) dev->max_context) {
196592ffb21SWarner Losh 		DRM_UNLOCK(dev);
197592ffb21SWarner Losh 		return EINVAL;
198592ffb21SWarner Losh 	}
199592ffb21SWarner Losh 
200592ffb21SWarner Losh 	map = dev->context_sareas[request->ctx_id];
201592ffb21SWarner Losh 	DRM_UNLOCK(dev);
202592ffb21SWarner Losh 
203592ffb21SWarner Losh 	request->handle = (void *)map->handle;
204592ffb21SWarner Losh 
205592ffb21SWarner Losh 	return 0;
206592ffb21SWarner Losh }
207592ffb21SWarner Losh 
208592ffb21SWarner Losh /**
209592ffb21SWarner Losh  * Set per-context SAREA.
210592ffb21SWarner Losh  *
211592ffb21SWarner Losh  * \param inode device inode.
212592ffb21SWarner Losh  * \param file_priv DRM file private.
213592ffb21SWarner Losh  * \param cmd command.
214592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx_priv_map structure.
215592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
216592ffb21SWarner Losh  *
217592ffb21SWarner Losh  * Searches the mapping specified in \p arg and update the entry in
218592ffb21SWarner Losh  * drm_device::ctx_idr with it.
219592ffb21SWarner Losh  */
drm_setsareactx(struct drm_device * dev,void * data,struct drm_file * file_priv)220592ffb21SWarner Losh int drm_setsareactx(struct drm_device *dev, void *data,
221592ffb21SWarner Losh 		    struct drm_file *file_priv)
222592ffb21SWarner Losh {
223592ffb21SWarner Losh 	struct drm_ctx_priv_map *request = data;
224592ffb21SWarner Losh 	struct drm_local_map *map = NULL;
225592ffb21SWarner Losh 	struct drm_map_list *r_list = NULL;
226592ffb21SWarner Losh 
227592ffb21SWarner Losh 	DRM_LOCK(dev);
228592ffb21SWarner Losh 	list_for_each_entry(r_list, &dev->maplist, head) {
229592ffb21SWarner Losh 		if (r_list->map
230592ffb21SWarner Losh 		    && r_list->user_token == (unsigned long) request->handle) {
231592ffb21SWarner Losh 			if (dev->max_context < 0)
232592ffb21SWarner Losh 				goto bad;
233592ffb21SWarner Losh 			if (request->ctx_id >= (unsigned) dev->max_context)
234592ffb21SWarner Losh 				goto bad;
235592ffb21SWarner Losh 			dev->context_sareas[request->ctx_id] = map;
236592ffb21SWarner Losh 			DRM_UNLOCK(dev);
237592ffb21SWarner Losh 			return 0;
238592ffb21SWarner Losh 		}
239592ffb21SWarner Losh 	}
240592ffb21SWarner Losh 
241592ffb21SWarner Losh bad:
242592ffb21SWarner Losh 	DRM_UNLOCK(dev);
243592ffb21SWarner Losh 	return EINVAL;
244592ffb21SWarner Losh }
245592ffb21SWarner Losh 
246592ffb21SWarner Losh /*@}*/
247592ffb21SWarner Losh 
248592ffb21SWarner Losh /******************************************************************/
249592ffb21SWarner Losh /** \name The actual DRM context handling routines */
250592ffb21SWarner Losh /*@{*/
251592ffb21SWarner Losh 
252592ffb21SWarner Losh /**
253592ffb21SWarner Losh  * Switch context.
254592ffb21SWarner Losh  *
255592ffb21SWarner Losh  * \param dev DRM device.
256592ffb21SWarner Losh  * \param old old context handle.
257592ffb21SWarner Losh  * \param new new context handle.
258592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
259592ffb21SWarner Losh  *
260592ffb21SWarner Losh  * Attempt to set drm_device::context_flag.
261592ffb21SWarner Losh  */
drm_context_switch(struct drm_device * dev,int old,int new)262592ffb21SWarner Losh static int drm_context_switch(struct drm_device * dev, int old, int new)
263592ffb21SWarner Losh {
264592ffb21SWarner Losh 	if (test_and_set_bit(0, &dev->context_flag)) {
265592ffb21SWarner Losh 		DRM_ERROR("Reentering -- FIXME\n");
266592ffb21SWarner Losh 		return -EBUSY;
267592ffb21SWarner Losh 	}
268592ffb21SWarner Losh 
269592ffb21SWarner Losh 	DRM_DEBUG("Context switch from %d to %d\n", old, new);
270592ffb21SWarner Losh 
271592ffb21SWarner Losh 	if (new == dev->last_context) {
272592ffb21SWarner Losh 		clear_bit(0, &dev->context_flag);
273592ffb21SWarner Losh 		return 0;
274592ffb21SWarner Losh 	}
275592ffb21SWarner Losh 
276592ffb21SWarner Losh 	return 0;
277592ffb21SWarner Losh }
278592ffb21SWarner Losh 
279592ffb21SWarner Losh /**
280592ffb21SWarner Losh  * Complete context switch.
281592ffb21SWarner Losh  *
282592ffb21SWarner Losh  * \param dev DRM device.
283592ffb21SWarner Losh  * \param new new context handle.
284592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
285592ffb21SWarner Losh  *
286592ffb21SWarner Losh  * Updates drm_device::last_context and drm_device::last_switch. Verifies the
287592ffb21SWarner Losh  * hardware lock is held, clears the drm_device::context_flag and wakes up
288592ffb21SWarner Losh  * drm_device::context_wait.
289592ffb21SWarner Losh  */
drm_context_switch_complete(struct drm_device * dev,struct drm_file * file_priv,int new)290592ffb21SWarner Losh static int drm_context_switch_complete(struct drm_device *dev,
291592ffb21SWarner Losh 				       struct drm_file *file_priv, int new)
292592ffb21SWarner Losh {
293592ffb21SWarner Losh 	dev->last_context = new;	/* PRE/POST: This is the _only_ writer. */
294592ffb21SWarner Losh 	dev->last_switch = jiffies;
295592ffb21SWarner Losh 
296592ffb21SWarner Losh 	if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
297592ffb21SWarner Losh 		DRM_ERROR("Lock isn't held after context switch\n");
298592ffb21SWarner Losh 	}
299592ffb21SWarner Losh 
300592ffb21SWarner Losh 	/* If a context switch is ever initiated
301592ffb21SWarner Losh 	   when the kernel holds the lock, release
302592ffb21SWarner Losh 	   that lock here. */
303592ffb21SWarner Losh 	clear_bit(0, &dev->context_flag);
304592ffb21SWarner Losh 	wakeup(&dev->context_wait);
305592ffb21SWarner Losh 
306592ffb21SWarner Losh 	return 0;
307592ffb21SWarner Losh }
308592ffb21SWarner Losh 
309592ffb21SWarner Losh /**
310592ffb21SWarner Losh  * Reserve contexts.
311592ffb21SWarner Losh  *
312592ffb21SWarner Losh  * \param inode device inode.
313592ffb21SWarner Losh  * \param file_priv DRM file private.
314592ffb21SWarner Losh  * \param cmd command.
315592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx_res structure.
316592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
317592ffb21SWarner Losh  */
drm_resctx(struct drm_device * dev,void * data,struct drm_file * file_priv)318592ffb21SWarner Losh int drm_resctx(struct drm_device *dev, void *data,
319592ffb21SWarner Losh 	       struct drm_file *file_priv)
320592ffb21SWarner Losh {
321592ffb21SWarner Losh 	struct drm_ctx_res *res = data;
322592ffb21SWarner Losh 	struct drm_ctx ctx;
323592ffb21SWarner Losh 	int i;
324592ffb21SWarner Losh 
325592ffb21SWarner Losh 	if (res->count >= DRM_RESERVED_CONTEXTS) {
326592ffb21SWarner Losh 		memset(&ctx, 0, sizeof(ctx));
327592ffb21SWarner Losh 		for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
328592ffb21SWarner Losh 			ctx.handle = i;
329592ffb21SWarner Losh 			if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
330592ffb21SWarner Losh 				return -EFAULT;
331592ffb21SWarner Losh 		}
332592ffb21SWarner Losh 	}
333592ffb21SWarner Losh 	res->count = DRM_RESERVED_CONTEXTS;
334592ffb21SWarner Losh 
335592ffb21SWarner Losh 	return 0;
336592ffb21SWarner Losh }
337592ffb21SWarner Losh 
338592ffb21SWarner Losh /**
339592ffb21SWarner Losh  * Add context.
340592ffb21SWarner Losh  *
341592ffb21SWarner Losh  * \param inode device inode.
342592ffb21SWarner Losh  * \param file_priv DRM file private.
343592ffb21SWarner Losh  * \param cmd command.
344592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx structure.
345592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
346592ffb21SWarner Losh  *
347592ffb21SWarner Losh  * Get a new handle for the context and copy to userspace.
348592ffb21SWarner Losh  */
drm_addctx(struct drm_device * dev,void * data,struct drm_file * file_priv)349592ffb21SWarner Losh int drm_addctx(struct drm_device *dev, void *data,
350592ffb21SWarner Losh 	       struct drm_file *file_priv)
351592ffb21SWarner Losh {
352592ffb21SWarner Losh 	struct drm_ctx_list *ctx_entry;
353592ffb21SWarner Losh 	struct drm_ctx *ctx = data;
354592ffb21SWarner Losh 
355592ffb21SWarner Losh 	ctx->handle = drm_ctxbitmap_next(dev);
356592ffb21SWarner Losh 	if (ctx->handle == DRM_KERNEL_CONTEXT) {
357592ffb21SWarner Losh 		/* Skip kernel's context and get a new one. */
358592ffb21SWarner Losh 		ctx->handle = drm_ctxbitmap_next(dev);
359592ffb21SWarner Losh 	}
360592ffb21SWarner Losh 	DRM_DEBUG("%d\n", ctx->handle);
361592ffb21SWarner Losh 	if (ctx->handle == -1) {
362592ffb21SWarner Losh 		DRM_DEBUG("Not enough free contexts.\n");
363592ffb21SWarner Losh 		/* Should this return -EBUSY instead? */
364592ffb21SWarner Losh 		return -ENOMEM;
365592ffb21SWarner Losh 	}
366592ffb21SWarner Losh 
367592ffb21SWarner Losh 	ctx_entry = malloc(sizeof(*ctx_entry), DRM_MEM_CTXBITMAP, M_NOWAIT);
368592ffb21SWarner Losh 	if (!ctx_entry) {
369592ffb21SWarner Losh 		DRM_DEBUG("out of memory\n");
370592ffb21SWarner Losh 		return -ENOMEM;
371592ffb21SWarner Losh 	}
372592ffb21SWarner Losh 
373592ffb21SWarner Losh 	INIT_LIST_HEAD(&ctx_entry->head);
374592ffb21SWarner Losh 	ctx_entry->handle = ctx->handle;
375592ffb21SWarner Losh 	ctx_entry->tag = file_priv;
376592ffb21SWarner Losh 
377592ffb21SWarner Losh 	DRM_LOCK(dev);
378592ffb21SWarner Losh 	list_add(&ctx_entry->head, &dev->ctxlist);
379592ffb21SWarner Losh 	++dev->ctx_count;
380592ffb21SWarner Losh 	DRM_UNLOCK(dev);
381592ffb21SWarner Losh 
382592ffb21SWarner Losh 	return 0;
383592ffb21SWarner Losh }
384592ffb21SWarner Losh 
drm_modctx(struct drm_device * dev,void * data,struct drm_file * file_priv)385592ffb21SWarner Losh int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
386592ffb21SWarner Losh {
387592ffb21SWarner Losh 	/* This does nothing */
388592ffb21SWarner Losh 	return 0;
389592ffb21SWarner Losh }
390592ffb21SWarner Losh 
391592ffb21SWarner Losh /**
392592ffb21SWarner Losh  * Get context.
393592ffb21SWarner Losh  *
394592ffb21SWarner Losh  * \param inode device inode.
395592ffb21SWarner Losh  * \param file_priv DRM file private.
396592ffb21SWarner Losh  * \param cmd command.
397592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx structure.
398592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
399592ffb21SWarner Losh  */
drm_getctx(struct drm_device * dev,void * data,struct drm_file * file_priv)400592ffb21SWarner Losh int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
401592ffb21SWarner Losh {
402592ffb21SWarner Losh 	struct drm_ctx *ctx = data;
403592ffb21SWarner Losh 
404592ffb21SWarner Losh 	/* This is 0, because we don't handle any context flags */
405592ffb21SWarner Losh 	ctx->flags = 0;
406592ffb21SWarner Losh 
407592ffb21SWarner Losh 	return 0;
408592ffb21SWarner Losh }
409592ffb21SWarner Losh 
410592ffb21SWarner Losh /**
411592ffb21SWarner Losh  * Switch context.
412592ffb21SWarner Losh  *
413592ffb21SWarner Losh  * \param inode device inode.
414592ffb21SWarner Losh  * \param file_priv DRM file private.
415592ffb21SWarner Losh  * \param cmd command.
416592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx structure.
417592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
418592ffb21SWarner Losh  *
419592ffb21SWarner Losh  * Calls context_switch().
420592ffb21SWarner Losh  */
drm_switchctx(struct drm_device * dev,void * data,struct drm_file * file_priv)421592ffb21SWarner Losh int drm_switchctx(struct drm_device *dev, void *data,
422592ffb21SWarner Losh 		  struct drm_file *file_priv)
423592ffb21SWarner Losh {
424592ffb21SWarner Losh 	struct drm_ctx *ctx = data;
425592ffb21SWarner Losh 
426592ffb21SWarner Losh 	DRM_DEBUG("%d\n", ctx->handle);
427592ffb21SWarner Losh 	return drm_context_switch(dev, dev->last_context, ctx->handle);
428592ffb21SWarner Losh }
429592ffb21SWarner Losh 
430592ffb21SWarner Losh /**
431592ffb21SWarner Losh  * New context.
432592ffb21SWarner Losh  *
433592ffb21SWarner Losh  * \param inode device inode.
434592ffb21SWarner Losh  * \param file_priv DRM file private.
435592ffb21SWarner Losh  * \param cmd command.
436592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx structure.
437592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
438592ffb21SWarner Losh  *
439592ffb21SWarner Losh  * Calls context_switch_complete().
440592ffb21SWarner Losh  */
drm_newctx(struct drm_device * dev,void * data,struct drm_file * file_priv)441592ffb21SWarner Losh int drm_newctx(struct drm_device *dev, void *data,
442592ffb21SWarner Losh 	       struct drm_file *file_priv)
443592ffb21SWarner Losh {
444592ffb21SWarner Losh 	struct drm_ctx *ctx = data;
445592ffb21SWarner Losh 
446592ffb21SWarner Losh 	DRM_DEBUG("%d\n", ctx->handle);
447592ffb21SWarner Losh 	drm_context_switch_complete(dev, file_priv, ctx->handle);
448592ffb21SWarner Losh 
449592ffb21SWarner Losh 	return 0;
450592ffb21SWarner Losh }
451592ffb21SWarner Losh 
452592ffb21SWarner Losh /**
453592ffb21SWarner Losh  * Remove context.
454592ffb21SWarner Losh  *
455592ffb21SWarner Losh  * \param inode device inode.
456592ffb21SWarner Losh  * \param file_priv DRM file private.
457592ffb21SWarner Losh  * \param cmd command.
458592ffb21SWarner Losh  * \param arg user argument pointing to a drm_ctx structure.
459592ffb21SWarner Losh  * \return zero on success or a negative number on failure.
460592ffb21SWarner Losh  *
461592ffb21SWarner Losh  * If not the special kernel context, calls ctxbitmap_free() to free the specified context.
462592ffb21SWarner Losh  */
drm_rmctx(struct drm_device * dev,void * data,struct drm_file * file_priv)463592ffb21SWarner Losh int drm_rmctx(struct drm_device *dev, void *data,
464592ffb21SWarner Losh 	      struct drm_file *file_priv)
465592ffb21SWarner Losh {
466592ffb21SWarner Losh 	struct drm_ctx *ctx = data;
467592ffb21SWarner Losh 
468592ffb21SWarner Losh 	DRM_DEBUG("%d\n", ctx->handle);
469592ffb21SWarner Losh 	if (ctx->handle != DRM_KERNEL_CONTEXT) {
470592ffb21SWarner Losh 		if (dev->driver->context_dtor)
471592ffb21SWarner Losh 			dev->driver->context_dtor(dev, ctx->handle);
472592ffb21SWarner Losh 		drm_ctxbitmap_free(dev, ctx->handle);
473592ffb21SWarner Losh 	}
474592ffb21SWarner Losh 
475592ffb21SWarner Losh 	DRM_LOCK(dev);
476592ffb21SWarner Losh 	if (!list_empty(&dev->ctxlist)) {
477592ffb21SWarner Losh 		struct drm_ctx_list *pos, *n;
478592ffb21SWarner Losh 
479592ffb21SWarner Losh 		list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
480592ffb21SWarner Losh 			if (pos->handle == ctx->handle) {
481592ffb21SWarner Losh 				list_del(&pos->head);
482592ffb21SWarner Losh 				free(pos, DRM_MEM_CTXBITMAP);
483592ffb21SWarner Losh 				--dev->ctx_count;
484592ffb21SWarner Losh 			}
485592ffb21SWarner Losh 		}
486592ffb21SWarner Losh 	}
487592ffb21SWarner Losh 	DRM_UNLOCK(dev);
488592ffb21SWarner Losh 
489592ffb21SWarner Losh 	return 0;
490592ffb21SWarner Losh }
491592ffb21SWarner Losh 
492592ffb21SWarner Losh /*@}*/
493