Home | History | Annotate | Line # | Download | only in drm
drm_lock.c revision 1.10
      1 /*	$NetBSD: drm_lock.c,v 1.10 2021/12/19 00:57:29 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * DRM lock.  Each drm master has a heavy-weight lock to provide mutual
     34  * exclusion for access to the hardware.  The lock can be held by the
     35  * kernel or by a drm file; the kernel takes access only for unusual
     36  * purposes, with drm_idlelock_take, mainly for idling the GPU when
     37  * closing down.
     38  *
     39  * The physical memory storing the lock state is shared between
     40  * userland and kernel: the pointer at dev->master->lock->hw_lock is
     41  * mapped into both userland and kernel address spaces.  This way,
     42  * userland can try to take the hardware lock without a system call,
     43  * although if it fails then it will use the DRM_LOCK ioctl to block
     44  * atomically until the lock is available.  All this means that the
     45  * kernel must use atomic_ops to manage the lock state.
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: drm_lock.c,v 1.10 2021/12/19 00:57:29 riastradh Exp $");
     50 
     51 #include <sys/types.h>
     52 #include <sys/errno.h>
     53 #include <sys/file.h>
     54 #include <sys/systm.h>
     55 
     56 #include <drm/drmP.h>
     57 
     58 #include "../dist/drm/drm_internal.h"
     59 #include "../dist/drm/drm_legacy.h"
     60 
     61 static bool	drm_lock_acquire(struct drm_lock_data *, int);
     62 static void	drm_lock_release(struct drm_lock_data *, int);
     63 static int	drm_lock_block_signals(struct drm_device *, struct drm_lock *,
     64 		    struct drm_file *);
     65 static void	drm_lock_unblock_signals(struct drm_device *,
     66 		    struct drm_lock *, struct drm_file *);
     67 
     68 /*
     69  * Take the lock on behalf of userland.
     70  */
     71 int
     72 drm_legacy_lock(struct drm_device *dev, void *data, struct drm_file *file)
     73 {
     74 	struct drm_lock *lock_request = data;
     75 	struct drm_master *master = file->master;
     76 	int error;
     77 
     78 	/* Sanitize the drm global mutex bollocks until we get rid of it.  */
     79 	KASSERT(mutex_is_locked(&drm_global_mutex));
     80 	mutex_unlock(&drm_global_mutex);
     81 
     82 	/* Refuse to lock on behalf of the kernel.  */
     83 	if (lock_request->context == DRM_KERNEL_CONTEXT) {
     84 		error = -EINVAL;
     85 		goto out0;
     86 	}
     87 
     88 	/* Refuse to set the magic bits.  */
     89 	if (lock_request->context !=
     90 	    _DRM_LOCKING_CONTEXT(lock_request->context)) {
     91 		error = -EINVAL;
     92 		goto out0;
     93 	}
     94 
     95 	/* Count it in the file and device statistics (XXX why here?).  */
     96 	file->lock_count++;
     97 
     98 	/* Wait until the hardware lock is gone or we can acquire it.   */
     99 	spin_lock(&master->lock.spinlock);
    100 
    101 	if (master->lock.user_waiters == UINT32_MAX) {
    102 		error = -EBUSY;
    103 		goto out1;
    104 	}
    105 
    106 	master->lock.user_waiters++;
    107 	DRM_SPIN_WAIT_UNTIL(error, &master->lock.lock_queue,
    108 	    &master->lock.spinlock,
    109 	    ((master->lock.hw_lock == NULL) ||
    110 		drm_lock_acquire(&master->lock, lock_request->context)));
    111 	KASSERT(0 < master->lock.user_waiters);
    112 	master->lock.user_waiters--;
    113 	if (error)
    114 		goto out1;
    115 
    116 	/* If the lock is gone, give up.  */
    117 	if (master->lock.hw_lock == NULL) {
    118 #if 0				/* XXX Linux sends SIGTERM, but why?  */
    119 		mutex_enter(&proc_lock);
    120 		psignal(curproc, SIGTERM);
    121 		mutex_exit(&proc_lock);
    122 		error = -EINTR;
    123 #else
    124 		error = -ENXIO;
    125 #endif
    126 		goto out1;
    127 	}
    128 
    129 	/* Mark the lock as owned by file.  */
    130 	master->lock.file_priv = file;
    131 	master->lock.lock_time = jiffies; /* XXX Unused?  */
    132 
    133 	/* Block signals while the lock is held.  */
    134 	error = drm_lock_block_signals(dev, lock_request, file);
    135 	if (error)
    136 		goto fail2;
    137 
    138 	/* Enter the DMA quiescent state if requested and available.  */
    139 	/* XXX Drop the spin lock first...  */
    140 	if (ISSET(lock_request->flags, _DRM_LOCK_QUIESCENT) &&
    141 	    (dev->driver->dma_quiescent != NULL)) {
    142 		error = (*dev->driver->dma_quiescent)(dev);
    143 		if (error)
    144 			goto fail3;
    145 	}
    146 
    147 	/* Success!  */
    148 	error = 0;
    149 	goto out1;
    150 
    151 fail3:	drm_lock_unblock_signals(dev, lock_request, file);
    152 fail2:	drm_lock_release(&master->lock, lock_request->context);
    153 	master->lock.file_priv = NULL;
    154 out1:	spin_unlock(&master->lock.spinlock);
    155 out0:	mutex_lock(&drm_global_mutex);
    156 	return error;
    157 }
    158 
    159 /*
    160  * Try to relinquish a lock that userland thinks it holds, per
    161  * userland's request.  Fail if it doesn't actually hold the lock.
    162  */
    163 int
    164 drm_legacy_unlock(struct drm_device *dev, void *data, struct drm_file *file)
    165 {
    166 	struct drm_lock *lock_request = data;
    167 	struct drm_master *master = file->master;
    168 	int error;
    169 
    170 	/* Sanitize the drm global mutex bollocks until we get rid of it.  */
    171 	KASSERT(mutex_is_locked(&drm_global_mutex));
    172 	mutex_unlock(&drm_global_mutex);
    173 
    174 	/* Refuse to unlock on behalf of the kernel.  */
    175 	if (lock_request->context == DRM_KERNEL_CONTEXT) {
    176 		error = -EINVAL;
    177 		goto out0;
    178 	}
    179 
    180 	/* Lock the internal spin lock to make changes.  */
    181 	spin_lock(&master->lock.spinlock);
    182 
    183 	/* Make sure it's actually locked.  */
    184 	if (!_DRM_LOCK_IS_HELD(master->lock.hw_lock->lock)) {
    185 		error = -EINVAL;	/* XXX Right error?  */
    186 		goto out1;
    187 	}
    188 
    189 	/* Make sure it's locked in the right context.  */
    190 	if (_DRM_LOCKING_CONTEXT(master->lock.hw_lock->lock) !=
    191 	    lock_request->context) {
    192 		error = -EACCES;	/* XXX Right error?  */
    193 		goto out1;
    194 	}
    195 
    196 	/* Make sure it's locked by us.  */
    197 	if (master->lock.file_priv != file) {
    198 		error = -EACCES;	/* XXX Right error?  */
    199 		goto out1;
    200 	}
    201 
    202 	/* Actually release the lock.  */
    203 	drm_lock_release(&master->lock, lock_request->context);
    204 
    205 	/* Clear the lock's file pointer, just in case.  */
    206 	master->lock.file_priv = NULL;
    207 
    208 	/* Unblock the signals we blocked in drm_lock.  */
    209 	drm_lock_unblock_signals(dev, lock_request, file);
    210 
    211 	/* Success!  */
    212 	error = 0;
    213 
    214 out1:	spin_unlock(&master->lock.spinlock);
    215 out0:	mutex_lock(&drm_global_mutex);
    216 	return error;
    217 }
    218 
    219 /*
    220  * Try to acquire the lock.  Whether or not we acquire it, guarantee
    221  * that whoever next releases it relinquishes it to the kernel, not to
    222  * anyone else.
    223  */
    224 void
    225 drm_legacy_idlelock_take(struct drm_lock_data *lock_data)
    226 {
    227 
    228 	spin_lock(&lock_data->spinlock);
    229 	KASSERT(!lock_data->idle_has_lock);
    230 	KASSERT(lock_data->kernel_waiters < UINT32_MAX);
    231 	lock_data->kernel_waiters++;
    232 	/* Try to acquire the lock.  */
    233 	if (drm_lock_acquire(lock_data, DRM_KERNEL_CONTEXT)) {
    234 		lock_data->idle_has_lock = 1;
    235 	} else {
    236 		/*
    237 		 * Recording that there are kernel waiters will prevent
    238 		 * userland from acquiring the lock again when it is
    239 		 * next released.
    240 		 */
    241 	}
    242 	spin_unlock(&lock_data->spinlock);
    243 }
    244 
    245 /*
    246  * Release whatever drm_idlelock_take managed to acquire.
    247  */
    248 void
    249 drm_legacy_idlelock_release(struct drm_lock_data *lock_data)
    250 {
    251 
    252 	spin_lock(&lock_data->spinlock);
    253 	KASSERT(0 < lock_data->kernel_waiters);
    254 	if (--lock_data->kernel_waiters == 0) {
    255 		if (lock_data->idle_has_lock) {
    256 			/* We did acquire it.  Release it.  */
    257 			drm_lock_release(lock_data, DRM_KERNEL_CONTEXT);
    258 		}
    259 	}
    260 	spin_unlock(&lock_data->spinlock);
    261 }
    262 
    263 /*
    264  * Release the lock and free it on closing of a drm file.
    265  */
    266 void
    267 drm_legacy_lock_release(struct drm_device *dev, struct file *fp)
    268 {
    269 	struct drm_file *const file = fp->f_data;
    270 	struct drm_lock_data *const lock_data = &file->master->lock;
    271 
    272 	/* If this file has never locked anything, nothing to do.  */
    273 	if (file->lock_count == 0)
    274 		return;
    275 
    276 	spin_lock(&lock_data->spinlock);
    277 
    278 	/* If there is no lock, nothing to do.  */
    279 	if (lock_data->hw_lock == NULL)
    280 		goto out;
    281 
    282 	/* If this lock is not held, nothing to do.   */
    283 	if (!_DRM_LOCK_IS_HELD(lock_data->hw_lock->lock))
    284 		goto out;
    285 
    286 	/*
    287 	 * Otherwise, it boils down to whether this file is the owner
    288 	 * or someone else.
    289 	 *
    290 	 * XXX This is not reliable!  Userland doesn't update this when
    291 	 * it takes the lock...
    292 	 */
    293 	if (file == lock_data->file_priv)
    294 		drm_lock_release(lock_data,
    295 		    _DRM_LOCKING_CONTEXT(file->master->lock.hw_lock->lock));
    296 
    297 out:	spin_unlock(&lock_data->spinlock);
    298 }
    299 
    300 /*
    301  * Try to acquire the lock.  Return true if successful, false if not.
    302  *
    303  * This is hairy because it races with userland, and if userland
    304  * already holds the lock, we must tell it, by marking it
    305  * _DRM_LOCK_CONT (contended), that it must call ioctl(DRM_UNLOCK) to
    306  * release the lock so that we can wake waiters.
    307  *
    308  * XXX What happens if the process is interrupted?
    309  */
    310 static bool
    311 drm_lock_acquire(struct drm_lock_data *lock_data, int context)
    312 {
    313         volatile unsigned int *const lock = &lock_data->hw_lock->lock;
    314 	unsigned int old, new;
    315 
    316 	KASSERT(spin_is_locked(&lock_data->spinlock));
    317 
    318 	do {
    319 		old = *lock;
    320 		if (!_DRM_LOCK_IS_HELD(old)) {
    321 			new = (context | _DRM_LOCK_HELD);
    322 			if ((0 < lock_data->user_waiters) ||
    323 			    (0 < lock_data->kernel_waiters))
    324 				new |= _DRM_LOCK_CONT;
    325 		} else if (_DRM_LOCKING_CONTEXT(old) != context) {
    326 			new = (old | _DRM_LOCK_CONT);
    327 		} else {
    328 			DRM_ERROR("%d already holds heavyweight lock\n",
    329 			    context);
    330 			return false;
    331 		}
    332 	} while (atomic_cas_uint(lock, old, new) != old);
    333 
    334 	return !_DRM_LOCK_IS_HELD(old);
    335 }
    336 
    337 /*
    338  * Release the lock held in the given context.  Wake any waiters,
    339  * preferring kernel waiters over userland waiters.
    340  *
    341  * Lock's spinlock must be held and lock must be held in this context.
    342  */
    343 static void
    344 drm_lock_release(struct drm_lock_data *lock_data, int context)
    345 {
    346 
    347 	(void)context;		/* ignore */
    348 	KASSERT(spin_is_locked(&lock_data->spinlock));
    349 	KASSERT(_DRM_LOCK_IS_HELD(lock_data->hw_lock->lock));
    350 	KASSERT(_DRM_LOCKING_CONTEXT(lock_data->hw_lock->lock) == context);
    351 
    352 	lock_data->hw_lock->lock = 0;
    353 	DRM_SPIN_WAKEUP_ONE(&lock_data->lock_queue, &lock_data->spinlock);
    354 }
    355 
    356 /*
    357  * Block signals for a process that holds a drm lock.
    358  *
    359  * XXX It's not processes but files that hold drm locks, so blocking
    360  * signals in a process seems wrong, and it's not clear that blocking
    361  * signals automatically is remotely sensible anyway.
    362  */
    363 static int
    364 drm_lock_block_signals(struct drm_device *dev __unused,
    365     struct drm_lock *lock_request __unused, struct drm_file *file __unused)
    366 {
    367 	return 0;
    368 }
    369 
    370 /*
    371  * Unblock the signals that drm_lock_block_signals blocked.
    372  */
    373 static void
    374 drm_lock_unblock_signals(struct drm_device *dev __unused,
    375     struct drm_lock *lock_request __unused, struct drm_file *file __unused)
    376 {
    377 }
    378