drm_lock.c revision 189869
1/*-
2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 *    Rickard E. (Rik) Faith <faith@valinux.com>
27 *    Gareth Hughes <gareth@valinux.com>
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/dev/drm/drm_lock.c 189869 2009-03-16 08:19:11Z rnoland $");
33
34/** @file drm_lock.c
35 * Implementation of the ioctls and other support code for dealing with the
36 * hardware lock.
37 *
38 * The DRM hardware lock is a shared structure between the kernel and userland.
39 *
40 * On uncontended access where the new context was the last context, the
41 * client may take the lock without dropping down into the kernel, using atomic
42 * compare-and-set.
43 *
44 * If the client finds during compare-and-set that it was not the last owner
45 * of the lock, it calls the DRM lock ioctl, which may sleep waiting for the
46 * lock, and may have side-effects of kernel-managed context switching.
47 *
48 * When the client releases the lock, if the lock is marked as being contended
49 * by another client, then the DRM unlock ioctl is called so that the
50 * contending client may be woken up.
51 */
52
53#include "dev/drm/drmP.h"
54
55int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
56{
57	struct drm_lock *lock = data;
58	int ret = 0;
59
60	if (lock->context == DRM_KERNEL_CONTEXT) {
61		DRM_ERROR("Process %d using kernel context %d\n",
62		    DRM_CURRENTPID, lock->context);
63		return EINVAL;
64	}
65
66	DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
67	    lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock,
68	    lock->flags);
69
70	if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE) &&
71	    lock->context < 0)
72		return EINVAL;
73
74	DRM_LOCK();
75	for (;;) {
76		if (drm_lock_take(&dev->lock, lock->context)) {
77			dev->lock.file_priv = file_priv;
78			dev->lock.lock_time = jiffies;
79			atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
80			break;  /* Got lock */
81		}
82
83		/* Contention */
84		ret = mtx_sleep((void *)&dev->lock.lock_queue, &dev->dev_lock,
85		    PCATCH, "drmlk2", 0);
86		if (ret != 0)
87			break;
88	}
89	DRM_UNLOCK();
90	DRM_DEBUG("%d %s\n", lock->context, ret ? "interrupted" : "has lock");
91
92	if (ret != 0)
93		return ret;
94
95	/* XXX: Add signal blocking here */
96
97	if (dev->driver->dma_quiescent != NULL &&
98	    (lock->flags & _DRM_LOCK_QUIESCENT))
99		dev->driver->dma_quiescent(dev);
100
101	return 0;
102}
103
104int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
105{
106	struct drm_lock *lock = data;
107
108	DRM_DEBUG("%d (pid %d) requests unlock (0x%08x), flags = 0x%08x\n",
109	    lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock,
110	    lock->flags);
111
112	if (lock->context == DRM_KERNEL_CONTEXT) {
113		DRM_ERROR("Process %d using kernel context %d\n",
114		    DRM_CURRENTPID, lock->context);
115		return EINVAL;
116	}
117
118	atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
119
120	DRM_LOCK();
121	drm_lock_transfer(&dev->lock, DRM_KERNEL_CONTEXT);
122
123	if (drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT)) {
124		DRM_ERROR("\n");
125	}
126	DRM_UNLOCK();
127
128	return 0;
129}
130
131int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context)
132{
133	volatile unsigned int *lock = &lock_data->hw_lock->lock;
134	unsigned int old, new;
135
136	do {
137		old = *lock;
138		if (old & _DRM_LOCK_HELD)
139			new = old | _DRM_LOCK_CONT;
140		else
141			new = context | _DRM_LOCK_HELD;
142	} while (!atomic_cmpset_int(lock, old, new));
143
144	if (_DRM_LOCKING_CONTEXT(old) == context) {
145		if (old & _DRM_LOCK_HELD) {
146			if (context != DRM_KERNEL_CONTEXT) {
147				DRM_ERROR("%d holds heavyweight lock\n",
148				    context);
149			}
150			return 0;
151		}
152	}
153	if (new == (context | _DRM_LOCK_HELD)) {
154		/* Have lock */
155		return 1;
156	}
157	return 0;
158}
159
160/* This takes a lock forcibly and hands it to context.	Should ONLY be used
161   inside *_unlock to give lock to kernel before calling *_dma_schedule. */
162int drm_lock_transfer(struct drm_lock_data *lock_data, unsigned int context)
163{
164	volatile unsigned int *lock = &lock_data->hw_lock->lock;
165	unsigned int old, new;
166
167	lock_data->file_priv = NULL;
168	do {
169		old = *lock;
170		new = context | _DRM_LOCK_HELD;
171	} while (!atomic_cmpset_int(lock, old, new));
172
173	return 1;
174}
175
176int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
177{
178	volatile unsigned int *lock = &lock_data->hw_lock->lock;
179	unsigned int old, new;
180
181	lock_data->file_priv = NULL;
182	do {
183		old = *lock;
184		new = 0;
185	} while (!atomic_cmpset_int(lock, old, new));
186
187	if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
188		DRM_ERROR("%d freed heavyweight lock held by %d\n",
189		    context, _DRM_LOCKING_CONTEXT(old));
190		return 1;
191	}
192	DRM_WAKEUP_INT((void *)&lock_data->lock_queue);
193	return 0;
194}
195