xref: /dragonfly/sys/dev/drm/drm_lock.c (revision 9317c2d0)
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 ? master->lock.hw_lock->lock : -1,
183 		  lock->flags);
184 
185 	add_wait_queue(&master->lock.lock_queue, &entry);
186 	spin_lock_bh(&master->lock.spinlock);
187 	master->lock.user_waiters++;
188 	spin_unlock_bh(&master->lock.spinlock);
189 
190 	for (;;) {
191 		__set_current_state(TASK_INTERRUPTIBLE);
192 		if (!master->lock.hw_lock) {
193 			/* Device has been unregistered */
194 			send_sig(SIGTERM, current, 0);
195 			ret = -EINTR;
196 			break;
197 		}
198 		if (drm_lock_take(&master->lock, lock->context)) {
199 			master->lock.file_priv = file_priv;
200 			master->lock.lock_time = jiffies;
201 			break;	/* Got lock */
202 		}
203 
204 		/* Contention */
205 		mutex_unlock(&drm_global_mutex);
206 		schedule();
207 		mutex_lock(&drm_global_mutex);
208 		if (signal_pending(current)) {
209 			ret = -EINTR;
210 			break;
211 		}
212 	}
213 	spin_lock_bh(&master->lock.spinlock);
214 	master->lock.user_waiters--;
215 	spin_unlock_bh(&master->lock.spinlock);
216 	__set_current_state(TASK_RUNNING);
217 	remove_wait_queue(&master->lock.lock_queue, &entry);
218 
219 	DRM_DEBUG("%d %s\n", lock->context,
220 		  ret ? "interrupted" : "has lock");
221 	if (ret) return ret;
222 
223 	/* don't set the block all signals on the master process for now
224 	 * really probably not the correct answer but lets us debug xkb
225  	 * xserver for now */
226 	if (!drm_is_current_master(file_priv)) {
227 		dev->sigdata.context = lock->context;
228 		dev->sigdata.lock = master->lock.hw_lock;
229 	}
230 
231 	if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
232 	{
233 		if (dev->driver->dma_quiescent(dev)) {
234 			DRM_DEBUG("%d waiting for DMA quiescent\n",
235 				  lock->context);
236 			return -EBUSY;
237 		}
238 	}
239 #endif
240 
241 	return 0;
242 }
243 
244 /**
245  * Unlock ioctl.
246  *
247  * \param inode device inode.
248  * \param file_priv DRM file private.
249  * \param cmd command.
250  * \param arg user argument, pointing to a drm_lock structure.
251  * \return zero on success or negative number on failure.
252  *
253  * Transfer and free the lock.
254  */
255 int drm_legacy_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
256 {
257 #if 0
258 	struct drm_lock *lock = data;
259 	struct drm_master *master = file_priv->master;
260 
261 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
262 		return -EINVAL;
263 
264 	if (lock->context == DRM_KERNEL_CONTEXT) {
265 		DRM_ERROR("Process %d using kernel context %d\n",
266 			  task_pid_nr(current), lock->context);
267 		return -EINVAL;
268 	}
269 
270 	if (drm_legacy_lock_free(&master->lock, lock->context)) {
271 		/* FIXME: Should really bail out here. */
272 	}
273 #endif
274 
275 	return 0;
276 }
277 
278 #if 0
279 /**
280  * This function returns immediately and takes the hw lock
281  * with the kernel context if it is free, otherwise it gets the highest priority when and if
282  * it is eventually released.
283  *
284  * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
285  * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
286  * a deadlock, which is why the "idlelock" was invented).
287  *
288  * This should be sufficient to wait for GPU idle without
289  * having to worry about starvation.
290  */
291 
292 void drm_legacy_idlelock_take(struct drm_lock_data *lock_data)
293 {
294 	int ret;
295 
296 	spin_lock_bh(&lock_data->spinlock);
297 	lock_data->kernel_waiters++;
298 	if (!lock_data->idle_has_lock) {
299 
300 		spin_unlock_bh(&lock_data->spinlock);
301 		ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
302 		spin_lock_bh(&lock_data->spinlock);
303 
304 		if (ret == 1)
305 			lock_data->idle_has_lock = 1;
306 	}
307 	spin_unlock_bh(&lock_data->spinlock);
308 }
309 EXPORT_SYMBOL(drm_legacy_idlelock_take);
310 
311 void drm_legacy_idlelock_release(struct drm_lock_data *lock_data)
312 {
313 	unsigned int old, prev;
314 	volatile unsigned int *lock = &lock_data->hw_lock->lock;
315 
316 	spin_lock_bh(&lock_data->spinlock);
317 	if (--lock_data->kernel_waiters == 0) {
318 		if (lock_data->idle_has_lock) {
319 			do {
320 				old = *lock;
321 				prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
322 			} while (prev != old);
323 			wake_up_interruptible(&lock_data->lock_queue);
324 			lock_data->idle_has_lock = 0;
325 		}
326 	}
327 	spin_unlock_bh(&lock_data->spinlock);
328 }
329 EXPORT_SYMBOL(drm_legacy_idlelock_release);
330 
331 static int drm_legacy_i_have_hw_lock(struct drm_device *dev,
332 				     struct drm_file *file_priv)
333 {
334 	struct drm_master *master = file_priv->master;
335 	return (file_priv->lock_count && master->lock.hw_lock &&
336 		_DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
337 		master->lock.file_priv == file_priv);
338 }
339 #endif
340 
341 void drm_legacy_lock_release(struct drm_device *dev, struct file *filp)
342 {
343 #if 0
344 	struct drm_file *file_priv = filp->private_data;
345 
346 	/* if the master has gone away we can't do anything with the lock */
347 	if (!dev->master)
348 		return;
349 
350 	if (drm_legacy_i_have_hw_lock(dev, file_priv)) {
351 		DRM_DEBUG("File %p released, freeing lock for context %d\n",
352 			  filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
353 		drm_legacy_lock_free(&file_priv->master->lock,
354 				     _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
355 	}
356 #endif
357 }
358