Home | History | Annotate | Line # | Download | only in drm
drm_lock.c revision 1.1.2.3
      1 /*	$NetBSD: drm_lock.c,v 1.1.2.3 2013/07/24 02:38:23 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 lock to provide mutual exclusion
     34  * for access to something about the hardware (XXX what?).  The lock
     35  * can be held by the kernel or by a drm file (XXX not by a process!
     36  * and multiple processes may share a common drm file).  (XXX What
     37  * excludes different kthreads?)  The DRM_LOCK ioctl grants the lock to
     38  * the userland.  The kernel may need to steal this lock in order to
     39  * close a drm file; I believe drm_global_mutex excludes different
     40  * kernel threads from doing this simultaneously.
     41  *
     42  * XXX This description is incoherent and incomplete.
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: drm_lock.c,v 1.1.2.3 2013/07/24 02:38:23 riastradh Exp $");
     47 
     48 #include <sys/types.h>
     49 #include <sys/errno.h>
     50 #include <sys/systm.h>
     51 
     52 #include <drm/drmP.h>
     53 
     54 static bool	drm_lock_acquire(struct drm_lock_data *, int);
     55 static void	drm_lock_release(struct drm_lock_data *, int);
     56 static int	drm_lock_block_signals(struct drm_device *, struct drm_lock *,
     57 		    struct drm_file *);
     58 static void	drm_lock_unblock_signals(struct drm_device *,
     59 		    struct drm_lock *, struct drm_file *);
     60 
     61 /*
     62  * Take the lock on behalf of userland.
     63  */
     64 int
     65 drm_lock(struct drm_device *dev, void *data, struct drm_file *file)
     66 {
     67 	struct drm_lock *lock_request = data;
     68 	struct drm_master *master = file->master;
     69 	bool acquired;
     70 	int error;
     71 
     72 	KASSERT(mutex_is_locked(&drm_global_mutex));
     73 
     74 	/* Refuse to lock on behalf of the kernel.  */
     75 	if (lock_request->context == DRM_KERNEL_CONTEXT)
     76 		return -EINVAL;
     77 
     78 	/* Count it in the file and device statistics (XXX why here?).  */
     79 	file->lock_count++;
     80 	atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
     81 
     82 	/* Wait until the hardware lock is gone or we can acquire it.   */
     83 	spin_lock(&master->lock.spinlock);
     84 	DRM_SPIN_WAIT_UNTIL(error, &master->lock.lock_queue,
     85 	    &master->lock.spinlock,
     86 	    ((master->lock.hw_lock == NULL) ||
     87 		(acquired = drm_lock_acquire(&master->lock,
     88 		    lock_request->context))));
     89 	if (error)
     90 		goto out;
     91 
     92 	/* If the lock is gone, give up.  */
     93 	if (master->lock.hw_lock == NULL) {
     94 #if 0				/* XXX Linux sends SIGTERM, but why?  */
     95 		mutex_enter(proc_lock);
     96 		psignal(curproc, SIGTERM);
     97 		mutex_exit(proc_lock);
     98 		error = -EINTR;
     99 #else
    100 		error = -ENXIO;
    101 #endif
    102 		goto out;
    103 	}
    104 
    105 	/* Mark the lock as owned by file.  */
    106 	master->lock.file_priv = file;
    107 #ifndef __NetBSD__
    108 	master->lock.lock_time = jiffies; /* XXX Unused?  */
    109 #endif
    110 
    111 	/* Block signals while the lock is held.  */
    112 	error = drm_lock_block_signals(dev, lock_request, file);
    113 	if (error)
    114 		goto fail0;
    115 
    116 	/* Enter the DMA quiescent state if requested and available.  */
    117 	if (ISSET(lock_request->flags, _DRM_LOCK_QUIESCENT) &&
    118 	    (dev->driver->dma_quiescent != NULL)) {
    119 		error = (*dev->driver->dma_quiescent)(dev);
    120 		if (error)
    121 			goto fail1;
    122 	}
    123 
    124 	/* Success!  */
    125 	error = 0;
    126 	goto out;
    127 
    128 fail1:	drm_lock_unblock_signals(dev, lock_request, file);
    129 fail0:	drm_lock_release(&master->lock, lock_request->context);
    130 	master->lock.file_priv = NULL;
    131 out:	spin_unlock(&master->lock.spinlock);
    132 	return error;
    133 }
    134 
    135 /*
    136  * Try to relinquish a lock that userland thinks it holds, per
    137  * userland's request.  Fail if it doesn't actually hold the lock.
    138  */
    139 int
    140 drm_unlock(struct drm_device *dev, void *data, struct drm_file *file)
    141 {
    142 	struct drm_lock *lock_request = data;
    143 	struct drm_master *master = file->master;
    144 	int error;
    145 
    146 	KASSERT(mutex_is_locked(&drm_global_mutex));
    147 
    148 	/* Refuse to unlock on behalf of the kernel.  */
    149 	if (lock_request->context == DRM_KERNEL_CONTEXT)
    150 		return -EINVAL;
    151 
    152 	/* Count it in the device statistics.  */
    153 	atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
    154 
    155 	/* Lock the internal spin lock to make changes.  */
    156 	spin_lock(&master->lock.spinlock);
    157 
    158 	/* Make sure it's actually locked.  */
    159 	if (!_DRM_LOCK_IS_HELD(master->lock.hw_lock->lock)) {
    160 		error = -EINVAL;	/* XXX Right error?  */
    161 		goto out;
    162 	}
    163 
    164 	/* Make sure it's locked in the right context.  */
    165 	if (_DRM_LOCKING_CONTEXT(master->lock.hw_lock->lock) !=
    166 	    lock_request->context) {
    167 		error = -EACCES;	/* XXX Right error?  */
    168 		goto out;
    169 	}
    170 
    171 	/* Make sure it's locked by us.  */
    172 	if (master->lock.file_priv != file) {
    173 		error = -EACCES;	/* XXX Right error?  */
    174 		goto out;
    175 	}
    176 
    177 	/* Actually release the lock.  */
    178 	drm_lock_release(&master->lock, lock_request->context);
    179 
    180 	/* Clear the lock's file pointer, just in case.  */
    181 	master->lock.file_priv = NULL;
    182 
    183 	/* Unblock the signals we blocked in drm_lock.  */
    184 	drm_lock_unblock_signals(dev, lock_request, file);
    185 
    186 	/* Success!  */
    187 	error = 0;
    188 
    189 out:	spin_unlock(&master->lock.spinlock);
    190 	return error;
    191 }
    192 
    193 /*
    194  * Drop the lock.
    195  *
    196  * Return value is an artefact of Linux.  Caller must guarantee
    197  * preconditions; failure is fatal.
    198  */
    199 int
    200 drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
    201 {
    202 
    203 	spin_lock(&lock_data->spinlock);
    204 	drm_lock_release(lock_data, context);
    205 	spin_unlock(&lock_data->spinlock);
    206 
    207 	return 0;
    208 }
    209 
    210 /*
    211  * Take the lock for the kernel's use.
    212  *
    213  * XXX This is unimplemented because it's not clear that the Linux code
    214  * makes sense at all.  Linux's drm_idlelock_take never blocks, but it
    215  * doesn't guarantee that the kernel holds the lock on return!  For
    216  * now, I'll hope that the code paths relying on this don't matter yet.
    217  */
    218 void
    219 drm_idlelock_take(struct drm_lock_data *lock_data __unused)
    220 {
    221 	KASSERT(mutex_is_locked(&drm_global_mutex));
    222 	panic("drm_idlelock_take is not yet implemented"); /* XXX */
    223 }
    224 
    225 /*
    226  * Release the lock from the kernel.
    227  */
    228 void
    229 drm_idlelock_release(struct drm_lock_data *lock_data __unused)
    230 {
    231 	KASSERT(mutex_is_locked(&drm_global_mutex));
    232 	panic("drm_idlelock_release is not yet implemented"); /* XXX */
    233 }
    234 
    235 /*
    236  * Does this file hold this drm device's hardware lock?
    237  */
    238 int
    239 drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file)
    240 {
    241 	struct drm_lock_data *const lock_data = &file->master->lock;
    242 
    243 	/* If this file has never locked anything, then no.  */
    244 	if (file->lock_count == 0)
    245 		return 0;
    246 
    247 	/* If there is no lock, then this file doesn't hold it.  */
    248 	if (lock_data->hw_lock == NULL)
    249 		return 0;
    250 
    251 	/* If this lock is not held, then this file doesn't hold it.   */
    252 	if (!_DRM_LOCK_IS_HELD(lock_data->hw_lock->lock))
    253 		return 0;
    254 
    255 	/*
    256 	 * Otherwise, it boils down to whether this file is the owner
    257 	 * or someone else.
    258 	 */
    259 	return (file == lock_data->file_priv);
    260 }
    261 
    262 /*
    263  * Try to acquire the lock.  Return true if successful, false if not.
    264  *
    265  * Lock's spinlock must be held.
    266  */
    267 static bool
    268 drm_lock_acquire(struct drm_lock_data *lock_data, int context)
    269 {
    270 
    271 	KASSERT(spin_is_locked(&lock_data->spinlock));
    272 
    273 	/* Test and set.  */
    274 	if (_DRM_LOCK_IS_HELD(lock_data->hw_lock->lock)) {
    275 		return false;
    276 	} else {
    277 		lock_data->hw_lock->lock = (context | _DRM_LOCK_HELD);
    278 		return true;
    279 	}
    280 }
    281 
    282 /*
    283  * Release the lock held in the given context.  Wake any waiters,
    284  * preferring kernel waiters over userland waiters.
    285  *
    286  * Lock's spinlock must be held and lock must be held in this context.
    287  */
    288 static void
    289 drm_lock_release(struct drm_lock_data *lock_data, int context)
    290 {
    291 
    292 	(void)context;		/* ignore */
    293 	KASSERT(spin_is_locked(&lock_data->spinlock));
    294 	KASSERT(_DRM_LOCK_IS_HELD(lock_data->hw_lock->lock));
    295 	KASSERT(_DRM_LOCKING_CONTEXT(lock_data->hw_lock->lock) == context);
    296 
    297 	/* Are there any kernel waiters?  */
    298 	if (lock_data->n_kernel_waiters > 0) {
    299 		/* If there's a kernel waiter, grant it ownership.  */
    300 		lock_data->hw_lock->lock = (DRM_KERNEL_CONTEXT |
    301 		    _DRM_LOCK_HELD);
    302 		lock_data->n_kernel_waiters--;
    303 		DRM_SPIN_WAKEUP_ONE(&lock_data->kernel_lock_queue,
    304 		    &lock_data->spinlock);
    305 	} else {
    306 		/* Otherwise, free it up and wake someone else.  */
    307 		lock_data->hw_lock->lock = 0;
    308 		DRM_SPIN_WAKEUP_ONE(&lock_data->lock_queue,
    309 		    &lock_data->spinlock);
    310 	}
    311 }
    312 
    313 /*
    314  * Block signals for a process that holds a drm lock.
    315  *
    316  * XXX It's not processes but files that hold drm locks, so blocking
    317  * signals in a process seems wrong, and it's not clear that blocking
    318  * signals automatically is remotely sensible anyway.
    319  */
    320 static int
    321 drm_lock_block_signals(struct drm_device *dev __unused,
    322     struct drm_lock *lock_request __unused, struct drm_file *file __unused)
    323 {
    324 	return 0;
    325 }
    326 
    327 /*
    328  * Unblock the signals that drm_lock_block_signals blocked.
    329  */
    330 static void
    331 drm_lock_unblock_signals(struct drm_device *dev __unused,
    332     struct drm_lock *lock_request __unused, struct drm_file *file __unused)
    333 {
    334 }
    335