xref: /dragonfly/sys/dev/drm/drm_lock.c (revision d147c943)
1 /**
2  * \file drm_lock.c
3  * IOCTLs for locking
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8 
9 /*
10  * Created: Tue Feb  2 08:37:54 1999 by faith@valinux.com
11  *
12  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14  * All Rights Reserved.
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files (the "Software"),
18  * to deal in the Software without restriction, including without limitation
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20  * and/or sell copies of the Software, and to permit persons to whom the
21  * Software is furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice (including the next
24  * paragraph) shall be included in all copies or substantial portions of the
25  * Software.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  * OTHER DEALINGS IN THE SOFTWARE.
34  */
35 
36 #include <linux/export.h>
37 #include <drm/drmP.h>
38 #include "drm_legacy.h"
39 #include "drm_internal.h"
40 
41 #if 0
42 static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
43 
44 /**
45  * Take the heavyweight lock.
46  *
47  * \param lock lock pointer.
48  * \param context locking context.
49  * \return one if the lock is held, or zero otherwise.
50  *
51  * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
52  */
53 static
54 int drm_lock_take(struct drm_lock_data *lock_data,
55 		  unsigned int context)
56 {
57 	unsigned int old, new, prev;
58 	volatile unsigned int *lock = &lock_data->hw_lock->lock;
59 
60 	spin_lock_bh(&lock_data->spinlock);
61 	do {
62 		old = *lock;
63 		if (old & _DRM_LOCK_HELD)
64 			new = old | _DRM_LOCK_CONT;
65 		else {
66 			new = context | _DRM_LOCK_HELD |
67 				((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
68 				 _DRM_LOCK_CONT : 0);
69 		}
70 		prev = cmpxchg(lock, old, new);
71 	} while (prev != old);
72 	spin_unlock_bh(&lock_data->spinlock);
73 
74 	if (_DRM_LOCKING_CONTEXT(old) == context) {
75 		if (old & _DRM_LOCK_HELD) {
76 			if (context != DRM_KERNEL_CONTEXT) {
77 				DRM_ERROR("%d holds heavyweight lock\n",
78 					  context);
79 			}
80 			return 0;
81 		}
82 	}
83 
84 	if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
85 		/* Have lock */
86 		return 1;
87 	}
88 	return 0;
89 }
90 
91 /**
92  * This takes a lock forcibly and hands it to context.	Should ONLY be used
93  * inside *_unlock to give lock to kernel before calling *_dma_schedule.
94  *
95  * \param dev DRM device.
96  * \param lock lock pointer.
97  * \param context locking context.
98  * \return always one.
99  *
100  * Resets the lock file pointer.
101  * Marks the lock as held by the given context, via the \p cmpxchg instruction.
102  */
103 static int drm_lock_transfer(struct drm_lock_data *lock_data,
104 			     unsigned int context)
105 {
106 	unsigned int old, new, prev;
107 	volatile unsigned int *lock = &lock_data->hw_lock->lock;
108 
109 	lock_data->file_priv = NULL;
110 	do {
111 		old = *lock;
112 		new = context | _DRM_LOCK_HELD;
113 		prev = cmpxchg(lock, old, new);
114 	} while (prev != old);
115 	return 1;
116 }
117 
118 static int drm_legacy_lock_free(struct drm_lock_data *lock_data,
119 				unsigned int context)
120 {
121 	unsigned int old, new, prev;
122 	volatile unsigned int *lock = &lock_data->hw_lock->lock;
123 
124 	spin_lock_bh(&lock_data->spinlock);
125 	if (lock_data->kernel_waiters != 0) {
126 		drm_lock_transfer(lock_data, 0);
127 		lock_data->idle_has_lock = 1;
128 		spin_unlock_bh(&lock_data->spinlock);
129 		return 1;
130 	}
131 	spin_unlock_bh(&lock_data->spinlock);
132 
133 	do {
134 		old = *lock;
135 		new = _DRM_LOCKING_CONTEXT(old);
136 		prev = cmpxchg(lock, old, new);
137 	} while (prev != old);
138 
139 	if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
140 		DRM_ERROR("%d freed heavyweight lock held by %d\n",
141 			  context, _DRM_LOCKING_CONTEXT(old));
142 		return 1;
143 	}
144 	wake_up_interruptible(&lock_data->lock_queue);
145 	return 0;
146 }
147 #endif
148 
149 /**
150  * Lock ioctl.
151  *
152  * \param inode device inode.
153  * \param file_priv DRM file private.
154  * \param cmd command.
155  * \param arg user argument, pointing to a drm_lock structure.
156  * \return zero on success or negative number on failure.
157  *
158  * Add the current task to the lock wait queue, and attempt to take to lock.
159  */
160 int drm_legacy_lock(struct drm_device *dev, void *data,
161 		    struct drm_file *file_priv)
162 {
163 #if 0
164 	DECLARE_WAITQUEUE(entry, current);
165 	struct drm_lock *lock = data;
166 	struct drm_master *master = file_priv->master;
167 	int ret = 0;
168 
169 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
170 		return -EINVAL;
171 
172 	++file_priv->lock_count;
173 
174 	if (lock->context == DRM_KERNEL_CONTEXT) {
175 		DRM_ERROR("Process %d using kernel context %d\n",
176 			  task_pid_nr(current), lock->context);
177 		return -EINVAL;
178 	}
179 
180 	DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
181 		  lock->context, task_pid_nr(current),
182 		  master->lock.hw_lock->lock, lock->flags);
183 
184 	add_wait_queue(&master->lock.lock_queue, &entry);
185 	spin_lock_bh(&master->lock.spinlock);
186 	master->lock.user_waiters++;
187 	spin_unlock_bh(&master->lock.spinlock);
188 
189 	for (;;) {
190 		__set_current_state(TASK_INTERRUPTIBLE);
191 		if (!master->lock.hw_lock) {
192 			/* Device has been unregistered */
193 			send_sig(SIGTERM, current, 0);
194 			ret = -EINTR;
195 			break;
196 		}
197 		if (drm_lock_take(&master->lock, lock->context)) {
198 			master->lock.file_priv = file_priv;
199 			master->lock.lock_time = jiffies;
200 			break;	/* Got lock */
201 		}
202 
203 		/* Contention */
204 		mutex_unlock(&drm_global_mutex);
205 		schedule();
206 		mutex_lock(&drm_global_mutex);
207 		if (signal_pending(current)) {
208 			ret = -EINTR;
209 			break;
210 		}
211 	}
212 	spin_lock_bh(&master->lock.spinlock);
213 	master->lock.user_waiters--;
214 	spin_unlock_bh(&master->lock.spinlock);
215 	__set_current_state(TASK_RUNNING);
216 	remove_wait_queue(&master->lock.lock_queue, &entry);
217 
218 	DRM_DEBUG("%d %s\n", lock->context,
219 		  ret ? "interrupted" : "has lock");
220 	if (ret) return ret;
221 
222 	/* don't set the block all signals on the master process for now
223 	 * really probably not the correct answer but lets us debug xkb
224  	 * xserver for now */
225 	if (!drm_is_current_master(file_priv)) {
226 		dev->sigdata.context = lock->context;
227 		dev->sigdata.lock = master->lock.hw_lock;
228 	}
229 
230 	if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
231 	{
232 		if (dev->driver->dma_quiescent(dev)) {
233 			DRM_DEBUG("%d waiting for DMA quiescent\n",
234 				  lock->context);
235 			return -EBUSY;
236 		}
237 	}
238 #endif
239 
240 	return 0;
241 }
242 
243 /**
244  * Unlock ioctl.
245  *
246  * \param inode device inode.
247  * \param file_priv DRM file private.
248  * \param cmd command.
249  * \param arg user argument, pointing to a drm_lock structure.
250  * \return zero on success or negative number on failure.
251  *
252  * Transfer and free the lock.
253  */
254 int drm_legacy_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
255 {
256 #if 0
257 	struct drm_lock *lock = data;
258 	struct drm_master *master = file_priv->master;
259 
260 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
261 		return -EINVAL;
262 
263 	if (lock->context == DRM_KERNEL_CONTEXT) {
264 		DRM_ERROR("Process %d using kernel context %d\n",
265 			  task_pid_nr(current), lock->context);
266 		return -EINVAL;
267 	}
268 
269 	if (drm_legacy_lock_free(&master->lock, lock->context)) {
270 		/* FIXME: Should really bail out here. */
271 	}
272 #endif
273 
274 	return 0;
275 }
276 
277 #if 0
278 /**
279  * This function returns immediately and takes the hw lock
280  * with the kernel context if it is free, otherwise it gets the highest priority when and if
281  * it is eventually released.
282  *
283  * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
284  * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
285  * a deadlock, which is why the "idlelock" was invented).
286  *
287  * This should be sufficient to wait for GPU idle without
288  * having to worry about starvation.
289  */
290 
291 void drm_legacy_idlelock_take(struct drm_lock_data *lock_data)
292 {
293 	int ret;
294 
295 	spin_lock_bh(&lock_data->spinlock);
296 	lock_data->kernel_waiters++;
297 	if (!lock_data->idle_has_lock) {
298 
299 		spin_unlock_bh(&lock_data->spinlock);
300 		ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
301 		spin_lock_bh(&lock_data->spinlock);
302 
303 		if (ret == 1)
304 			lock_data->idle_has_lock = 1;
305 	}
306 	spin_unlock_bh(&lock_data->spinlock);
307 }
308 EXPORT_SYMBOL(drm_legacy_idlelock_take);
309 
310 void drm_legacy_idlelock_release(struct drm_lock_data *lock_data)
311 {
312 	unsigned int old, prev;
313 	volatile unsigned int *lock = &lock_data->hw_lock->lock;
314 
315 	spin_lock_bh(&lock_data->spinlock);
316 	if (--lock_data->kernel_waiters == 0) {
317 		if (lock_data->idle_has_lock) {
318 			do {
319 				old = *lock;
320 				prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
321 			} while (prev != old);
322 			wake_up_interruptible(&lock_data->lock_queue);
323 			lock_data->idle_has_lock = 0;
324 		}
325 	}
326 	spin_unlock_bh(&lock_data->spinlock);
327 }
328 EXPORT_SYMBOL(drm_legacy_idlelock_release);
329 
330 static int drm_legacy_i_have_hw_lock(struct drm_device *dev,
331 				     struct drm_file *file_priv)
332 {
333 	struct drm_master *master = file_priv->master;
334 	return (file_priv->lock_count && master->lock.hw_lock &&
335 		_DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
336 		master->lock.file_priv == file_priv);
337 }
338 #endif
339 
340 void drm_legacy_lock_release(struct drm_device *dev, struct file *filp)
341 {
342 #if 0
343 	struct drm_file *file_priv = filp->private_data;
344 
345 	/* if the master has gone away we can't do anything with the lock */
346 	if (!dev->master)
347 		return;
348 
349 	if (drm_legacy_i_have_hw_lock(dev, file_priv)) {
350 		DRM_DEBUG("File %p released, freeing lock for context %d\n",
351 			  filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
352 		drm_legacy_lock_free(&file_priv->master->lock,
353 				     _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
354 	}
355 #endif
356 }
357