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