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