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 /** @file drm_lock.c
32  * Implementation of the ioctls and other support code for dealing with the
33  * hardware lock.
34  *
35  * The DRM hardware lock is a shared structure between the kernel and userland.
36  *
37  * On uncontended access where the new context was the last context, the
38  * client may take the lock without dropping down into the kernel, using atomic
39  * compare-and-set.
40  *
41  * If the client finds during compare-and-set that it was not the last owner
42  * of the lock, it calls the DRM lock ioctl, which may sleep waiting for the
43  * lock, and may have side-effects of kernel-managed context switching.
44  *
45  * When the client releases the lock, if the lock is marked as being contended
46  * by another client, then the DRM unlock ioctl is called so that the
47  * contending client may be woken up.
48  */
49 
50 #include "drmP.h"
51 
52 int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
53 {
54 	struct drm_lock *lock = data;
55 	int ret = 0;
56 
57 	if (lock->context == DRM_KERNEL_CONTEXT) {
58 		DRM_ERROR("Process %d using kernel context %d\n",
59 		    DRM_CURRENTPID, lock->context);
60 		return EINVAL;
61 	}
62 
63 	DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
64 	    lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock,
65 	    lock->flags);
66 
67 	if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE) &&
68 	    lock->context < 0)
69 		return EINVAL;
70 
71 	DRM_LOCK();
72 	for (;;) {
73 		if (drm_lock_take(&dev->lock, lock->context)) {
74 			dev->lock.file_priv = file_priv;
75 			dev->lock.lock_time = jiffies;
76 			atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
77 			break;  /* Got lock */
78 		}
79 
80 		/* Contention */
81 #if defined(__FreeBSD__)
82 		ret = mtx_sleep((void *)&dev->lock.lock_queue, &dev->dev_lock,
83 		    PCATCH, "drmlk2", 0);
84 #elif   defined(__NetBSD__)
85         ret = mtsleep((void *)&dev->lock.lock_queue, PCATCH, "drmlk2", 0,
86                 &dev->dev_lock);
87 #endif
88 		if (ret != 0)
89 			break;
90 	}
91 	DRM_UNLOCK();
92 	DRM_DEBUG("%d %s\n", lock->context, ret ? "interrupted" : "has lock");
93 
94 	if (ret != 0)
95 		return ret;
96 
97 	/* XXX: Add signal blocking here */
98 
99 	if (dev->driver->dma_quiescent != NULL &&
100 	    (lock->flags & _DRM_LOCK_QUIESCENT))
101 		dev->driver->dma_quiescent(dev);
102 
103 	return 0;
104 }
105 
106 int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
107 {
108 	struct drm_lock *lock = data;
109 
110 	DRM_DEBUG("%d (pid %d) requests unlock (0x%08x), flags = 0x%08x\n",
111 	    lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock,
112 	    lock->flags);
113 
114 	if (lock->context == DRM_KERNEL_CONTEXT) {
115 		DRM_ERROR("Process %d using kernel context %d\n",
116 		    DRM_CURRENTPID, lock->context);
117 		return EINVAL;
118 	}
119 
120 	atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
121 
122 	DRM_LOCK();
123 	drm_lock_transfer(&dev->lock, DRM_KERNEL_CONTEXT);
124 
125 	if (drm_lock_free(&dev->lock, DRM_KERNEL_CONTEXT)) {
126 		DRM_ERROR("\n");
127 	}
128 	DRM_UNLOCK();
129 
130 	return 0;
131 }
132 
133 int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context)
134 {
135 	volatile unsigned int *lock = &lock_data->hw_lock->lock;
136 	unsigned int old, new;
137 
138 	do {
139 		old = *lock;
140 		if (old & _DRM_LOCK_HELD)
141 			new = old | _DRM_LOCK_CONT;
142 		else
143 			new = context | _DRM_LOCK_HELD;
144 	} while (!atomic_cmpset_int(lock, old, new));
145 
146 	if (_DRM_LOCKING_CONTEXT(old) == context) {
147 		if (old & _DRM_LOCK_HELD) {
148 			if (context != DRM_KERNEL_CONTEXT) {
149 				DRM_ERROR("%d holds heavyweight lock\n",
150 				    context);
151 			}
152 			return 0;
153 		}
154 	}
155 	if (new == (context | _DRM_LOCK_HELD)) {
156 		/* Have lock */
157 		return 1;
158 	}
159 	return 0;
160 }
161 
162 /* This takes a lock forcibly and hands it to context.	Should ONLY be used
163    inside *_unlock to give lock to kernel before calling *_dma_schedule. */
164 int drm_lock_transfer(struct drm_lock_data *lock_data, unsigned int context)
165 {
166 	volatile unsigned int *lock = &lock_data->hw_lock->lock;
167 	unsigned int old, new;
168 
169 	lock_data->file_priv = NULL;
170 	do {
171 		old = *lock;
172 		new = context | _DRM_LOCK_HELD;
173 	} while (!atomic_cmpset_int(lock, old, new));
174 
175 	return 1;
176 }
177 
178 int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
179 {
180 	volatile unsigned int *lock = &lock_data->hw_lock->lock;
181 	unsigned int old, new;
182 
183 	lock_data->file_priv = NULL;
184 	do {
185 		old = *lock;
186 		new = 0;
187 	} while (!atomic_cmpset_int(lock, old, new));
188 
189 	if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
190 		DRM_ERROR("%d freed heavyweight lock held by %d\n",
191 		    context, _DRM_LOCKING_CONTEXT(old));
192 		return 1;
193 	}
194 	DRM_WAKEUP_INT((void *)&lock_data->lock_queue);
195 	return 0;
196 }
197