2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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.
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Gareth Hughes <gareth@valinux.com>
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * Implementation of the ioctls and other support code for dealing with the
38 * The DRM hardware lock is a shared structure between the kernel and userland.
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
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.
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.
53 #include "dev/drm/drmP.h"
55 int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
57 struct drm_lock *lock = data;
60 if (lock->context == DRM_KERNEL_CONTEXT) {
61 DRM_ERROR("Process %d using kernel context %d\n",
62 DRM_CURRENTPID, lock->context);
66 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
67 lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock,
70 if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE) &&
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]);
84 ret = mtx_sleep((void *)&dev->lock.lock_queue, &dev->dev_lock,
85 PZERO | PCATCH, "drmlk2", 0);
90 DRM_DEBUG("%d %s\n", lock->context, ret ? "interrupted" : "has lock");
95 /* XXX: Add signal blocking here */
97 if (dev->driver->dma_quiescent != NULL &&
98 (lock->flags & _DRM_LOCK_QUIESCENT))
99 dev->driver->dma_quiescent(dev);
104 int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
106 struct drm_lock *lock = data;
108 if (lock->context == DRM_KERNEL_CONTEXT) {
109 DRM_ERROR("Process %d using kernel context %d\n",
110 DRM_CURRENTPID, lock->context);
113 /* Check that the context unlock being requested actually matches
114 * who currently holds the lock.
116 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||
117 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) != lock->context)
120 DRM_SPINLOCK(&dev->tsk_lock);
121 if (dev->locked_task_call != NULL) {
122 dev->locked_task_call(dev);
123 dev->locked_task_call = NULL;
125 DRM_SPINUNLOCK(&dev->tsk_lock);
127 atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
130 drm_lock_transfer(&dev->lock, DRM_KERNEL_CONTEXT);
132 if (drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT)) {
140 int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context)
142 volatile unsigned int *lock = &lock_data->hw_lock->lock;
143 unsigned int old, new;
147 if (old & _DRM_LOCK_HELD)
148 new = old | _DRM_LOCK_CONT;
150 new = context | _DRM_LOCK_HELD;
151 } while (!atomic_cmpset_int(lock, old, new));
153 if (_DRM_LOCKING_CONTEXT(old) == context) {
154 if (old & _DRM_LOCK_HELD) {
155 if (context != DRM_KERNEL_CONTEXT) {
156 DRM_ERROR("%d holds heavyweight lock\n",
162 if (new == (context | _DRM_LOCK_HELD)) {
169 /* This takes a lock forcibly and hands it to context. Should ONLY be used
170 inside *_unlock to give lock to kernel before calling *_dma_schedule. */
171 int drm_lock_transfer(struct drm_lock_data *lock_data, unsigned int context)
173 volatile unsigned int *lock = &lock_data->hw_lock->lock;
174 unsigned int old, new;
176 lock_data->file_priv = NULL;
179 new = context | _DRM_LOCK_HELD;
180 } while (!atomic_cmpset_int(lock, old, new));
185 int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
187 volatile unsigned int *lock = &lock_data->hw_lock->lock;
188 unsigned int old, new;
190 lock_data->file_priv = NULL;
194 } while (!atomic_cmpset_int(lock, old, new));
196 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
197 DRM_ERROR("%d freed heavyweight lock held by %d\n",
198 context, _DRM_LOCKING_CONTEXT(old));
201 DRM_WAKEUP_INT((void *)&lock_data->lock_queue);