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>
29 * $FreeBSD: src/sys/dev/drm2/drm_lock.c,v 1.1 2012/05/22 11:07:44 kib Exp $
33 * Implementation of the ioctls and other support code for dealing with the
36 * The DRM hardware lock is a shared structure between the kernel and userland.
38 * On uncontended access where the new context was the last context, the
39 * client may take the lock without dropping down into the kernel, using atomic
42 * If the client finds during compare-and-set that it was not the last owner
43 * of the lock, it calls the DRM lock ioctl, which may sleep waiting for the
44 * lock, and may have side-effects of kernel-managed context switching.
46 * When the client releases the lock, if the lock is marked as being contended
47 * by another client, then the DRM unlock ioctl is called so that the
48 * contending client may be woken up.
52 #include "drm_legacy.h"
54 int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
56 struct drm_lock *lock = data;
59 if (lock->context == DRM_KERNEL_CONTEXT) {
60 DRM_ERROR("Process %d using kernel context %d\n",
61 DRM_CURRENTPID, lock->context);
65 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
66 lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock,
69 if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE) &&
75 if (drm_lock_take(&dev->lock, lock->context)) {
76 dev->lock.file_priv = file_priv;
77 dev->lock.lock_time = jiffies;
78 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
83 ret = DRM_LOCK_SLEEP(dev, &dev->lock.lock_queue,
91 DRM_DEBUG("restarting syscall\n");
93 DRM_DEBUG("%d %s\n", lock->context,
94 ret ? "interrupted" : "has lock");
99 /* XXX: Add signal blocking here */
101 if (dev->driver->dma_quiescent != NULL &&
102 (lock->flags & _DRM_LOCK_QUIESCENT))
103 dev->driver->dma_quiescent(dev);
108 int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
110 struct drm_lock *lock = data;
112 DRM_DEBUG("%d (pid %d) requests unlock (0x%08x), flags = 0x%08x\n",
113 lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock,
116 if (lock->context == DRM_KERNEL_CONTEXT) {
117 DRM_ERROR("Process %d using kernel context %d\n",
118 DRM_CURRENTPID, lock->context);
122 atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
125 drm_lock_transfer(&dev->lock, DRM_KERNEL_CONTEXT);
127 if (drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT)) {
135 int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context)
137 volatile unsigned int *lock = &lock_data->hw_lock->lock;
138 unsigned int old, new;
142 if (old & _DRM_LOCK_HELD)
143 new = old | _DRM_LOCK_CONT;
145 new = context | _DRM_LOCK_HELD;
146 } while (!atomic_cmpset_int(lock, old, new));
148 if (_DRM_LOCKING_CONTEXT(old) == context) {
149 if (old & _DRM_LOCK_HELD) {
150 if (context != DRM_KERNEL_CONTEXT) {
151 DRM_ERROR("%d holds heavyweight lock\n",
157 if (new == (context | _DRM_LOCK_HELD)) {
164 /* This takes a lock forcibly and hands it to context. Should ONLY be used
165 inside *_unlock to give lock to kernel before calling *_dma_schedule. */
166 int drm_lock_transfer(struct drm_lock_data *lock_data, unsigned int context)
168 volatile unsigned int *lock = &lock_data->hw_lock->lock;
169 unsigned int old, new;
171 lock_data->file_priv = NULL;
174 new = context | _DRM_LOCK_HELD;
175 } while (!atomic_cmpset_int(lock, old, new));
180 int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
182 volatile unsigned int *lock = &lock_data->hw_lock->lock;
183 unsigned int old, new;
185 lock_data->file_priv = NULL;
189 } while (!atomic_cmpset_int(lock, old, new));
191 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
192 DRM_ERROR("%d freed heavyweight lock held by %d\n",
193 context, _DRM_LOCKING_CONTEXT(old));
197 wake_up_interruptible(&lock_data->lock_queue);