Home | History | Annotate | Line # | Download | only in drm
drm_lock.c revision 1.1.2.2
      1  1.1.2.1  riastrad /*	$NetBSD: drm_lock.c,v 1.1.2.2 2013/07/24 02:35:35 riastradh Exp $	*/
      2  1.1.2.1  riastrad 
      3  1.1.2.1  riastrad /*-
      4  1.1.2.1  riastrad  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.1.2.1  riastrad  * All rights reserved.
      6  1.1.2.1  riastrad  *
      7  1.1.2.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.1  riastrad  * by Taylor R. Campbell.
      9  1.1.2.1  riastrad  *
     10  1.1.2.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1.2.1  riastrad  * are met:
     13  1.1.2.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.1  riastrad  *
     19  1.1.2.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1.2.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1.2.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1.2.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1.2.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1.2.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1.2.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1.2.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1.2.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1.2.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1.2.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1.2.1  riastrad  */
     31  1.1.2.1  riastrad 
     32  1.1.2.1  riastrad /*
     33  1.1.2.1  riastrad  * DRM lock.  Each drm master has a lock to provide mutual exclusion
     34  1.1.2.1  riastrad  * for access to something about the hardware (XXX what?).  The lock
     35  1.1.2.1  riastrad  * can be held by the kernel or by a drm file (XXX not by a process!
     36  1.1.2.1  riastrad  * and multiple processes may share a common drm file).  (XXX What
     37  1.1.2.1  riastrad  * excludes different kthreads?)  The DRM_LOCK ioctl grants the lock to
     38  1.1.2.1  riastrad  * the userland.  The kernel may need to steal this lock in order to
     39  1.1.2.1  riastrad  * close a drm file; I believe drm_global_mutex excludes different
     40  1.1.2.1  riastrad  * kernel threads from doing this simultaneously.
     41  1.1.2.1  riastrad  *
     42  1.1.2.1  riastrad  * XXX This description is incoherent and incomplete.
     43  1.1.2.1  riastrad  */
     44  1.1.2.1  riastrad 
     45  1.1.2.1  riastrad #include <sys/cdefs.h>
     46  1.1.2.1  riastrad __KERNEL_RCSID(0, "$NetBSD: drm_lock.c,v 1.1.2.2 2013/07/24 02:35:35 riastradh Exp $");
     47  1.1.2.1  riastrad 
     48  1.1.2.1  riastrad #include <sys/types.h>
     49  1.1.2.1  riastrad #include <sys/errno.h>
     50  1.1.2.1  riastrad #include <sys/systm.h>
     51  1.1.2.1  riastrad 
     52  1.1.2.1  riastrad #include <drm/drmP.h>
     53  1.1.2.1  riastrad 
     54  1.1.2.1  riastrad static bool	drm_lock_acquire(struct drm_lock_data *, int);
     55  1.1.2.1  riastrad static void	drm_lock_release(struct drm_lock_data *, int);
     56  1.1.2.1  riastrad static int	drm_lock_block_signals(struct drm_device *, struct drm_lock *,
     57  1.1.2.1  riastrad 		    struct drm_file *);
     58  1.1.2.1  riastrad static void	drm_lock_unblock_signals(struct drm_device *,
     59  1.1.2.1  riastrad 		    struct drm_lock *, struct drm_file *);
     60  1.1.2.1  riastrad 
     61  1.1.2.1  riastrad /*
     62  1.1.2.1  riastrad  * Take the lock on behalf of userland.
     63  1.1.2.1  riastrad  */
     64  1.1.2.1  riastrad int
     65  1.1.2.1  riastrad drm_lock(struct drm_device *dev, void *data, struct drm_file *file)
     66  1.1.2.1  riastrad {
     67  1.1.2.1  riastrad 	struct drm_lock *lock_request = data;
     68  1.1.2.1  riastrad 	struct drm_master *master = file->master;
     69  1.1.2.1  riastrad 	bool acquired;
     70  1.1.2.1  riastrad 	int error;
     71  1.1.2.1  riastrad 
     72  1.1.2.1  riastrad 	KASSERT(mutex_is_locked(&drm_global_mutex));
     73  1.1.2.1  riastrad 
     74  1.1.2.1  riastrad 	/* Refuse to lock on behalf of the kernel.  */
     75  1.1.2.1  riastrad 	if (lock_request->context == DRM_KERNEL_CONTEXT)
     76  1.1.2.1  riastrad 		return -EINVAL;
     77  1.1.2.1  riastrad 
     78  1.1.2.1  riastrad 	/* Count it in the file and device statistics (XXX why here?).  */
     79  1.1.2.1  riastrad 	file->lock_count++;
     80  1.1.2.1  riastrad 	atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
     81  1.1.2.1  riastrad 
     82  1.1.2.1  riastrad 	/* Wait until the hardware lock is gone or we can acquire it.   */
     83  1.1.2.1  riastrad 	spin_lock(&master->lock.spinlock);
     84  1.1.2.1  riastrad 	DRM_SPIN_WAIT_UNTIL(error, &master->lock.lock_queue,
     85  1.1.2.1  riastrad 	    &master->lock.spinlock,
     86  1.1.2.1  riastrad 	    ((master->lock.hw_lock == NULL) ||
     87  1.1.2.1  riastrad 		(acquired = drm_lock_acquire(&master->lock,
     88  1.1.2.1  riastrad 		    lock_request->context))));
     89  1.1.2.1  riastrad 	if (error)
     90  1.1.2.1  riastrad 		goto out;
     91  1.1.2.1  riastrad 
     92  1.1.2.1  riastrad 	/* If the lock is gone, give up.  */
     93  1.1.2.1  riastrad 	if (master->lock.hw_lock == NULL) {
     94  1.1.2.1  riastrad #if 0				/* XXX Linux sends SIGTERM, but why?  */
     95  1.1.2.1  riastrad 		mutex_enter(proc_lock);
     96  1.1.2.1  riastrad 		psignal(curproc, SIGTERM);
     97  1.1.2.1  riastrad 		mutex_exit(proc_lock);
     98  1.1.2.1  riastrad 		error = -EINTR;
     99  1.1.2.1  riastrad #else
    100  1.1.2.1  riastrad 		error = -ENXIO;
    101  1.1.2.1  riastrad #endif
    102  1.1.2.1  riastrad 		goto out;
    103  1.1.2.1  riastrad 	}
    104  1.1.2.1  riastrad 
    105  1.1.2.1  riastrad 	/* Mark the lock as owned by file.  */
    106  1.1.2.1  riastrad 	master->lock.file_priv = file;
    107  1.1.2.1  riastrad #ifndef __NetBSD__
    108  1.1.2.1  riastrad 	master->lock.lock_time = jiffies; /* XXX Unused?  */
    109  1.1.2.1  riastrad #endif
    110  1.1.2.1  riastrad 
    111  1.1.2.1  riastrad 	/* Block signals while the lock is held.  */
    112  1.1.2.1  riastrad 	error = drm_lock_block_signals(dev, lock_request, file);
    113  1.1.2.1  riastrad 	if (error)
    114  1.1.2.1  riastrad 		goto fail0;
    115  1.1.2.1  riastrad 
    116  1.1.2.1  riastrad 	/* Enter the DMA quiescent state if requested and available.  */
    117  1.1.2.1  riastrad 	if (ISSET(lock_request->flags, _DRM_LOCK_QUIESCENT) &&
    118  1.1.2.1  riastrad 	    (dev->driver->dma_quiescent != NULL)) {
    119  1.1.2.1  riastrad 		error = (*dev->driver->dma_quiescent)(dev);
    120  1.1.2.1  riastrad 		if (error)
    121  1.1.2.1  riastrad 			goto fail1;
    122  1.1.2.1  riastrad 	}
    123  1.1.2.1  riastrad 
    124  1.1.2.1  riastrad 	/* Success!  */
    125  1.1.2.1  riastrad 	error = 0;
    126  1.1.2.1  riastrad 	goto out;
    127  1.1.2.1  riastrad 
    128  1.1.2.1  riastrad fail1:	drm_lock_unblock_signals(dev, lock_request, file);
    129  1.1.2.1  riastrad fail0:	drm_lock_release(&master->lock, lock_request->context);
    130  1.1.2.1  riastrad 	master->lock.file_priv = NULL;
    131  1.1.2.1  riastrad out:	spin_unlock(&master->lock.spinlock);
    132  1.1.2.1  riastrad 	return error;
    133  1.1.2.1  riastrad }
    134  1.1.2.1  riastrad 
    135  1.1.2.1  riastrad /*
    136  1.1.2.1  riastrad  * Try to relinquish a lock that userland thinks it holds, per
    137  1.1.2.1  riastrad  * userland's request.  Fail if it doesn't actually hold the lock.
    138  1.1.2.1  riastrad  */
    139  1.1.2.1  riastrad int
    140  1.1.2.1  riastrad drm_unlock(struct drm_device *dev, void *data, struct drm_file *file)
    141  1.1.2.1  riastrad {
    142  1.1.2.1  riastrad 	struct drm_lock *lock_request = data;
    143  1.1.2.1  riastrad 	struct drm_master *master = file->master;
    144  1.1.2.1  riastrad 	int error;
    145  1.1.2.1  riastrad 
    146  1.1.2.1  riastrad 	KASSERT(mutex_is_locked(&drm_global_mutex));
    147  1.1.2.1  riastrad 
    148  1.1.2.1  riastrad 	/* Refuse to unlock on behalf of the kernel.  */
    149  1.1.2.1  riastrad 	if (lock_request->context == DRM_KERNEL_CONTEXT)
    150  1.1.2.1  riastrad 		return -EINVAL;
    151  1.1.2.1  riastrad 
    152  1.1.2.1  riastrad 	/* Count it in the device statistics.  */
    153  1.1.2.1  riastrad 	atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
    154  1.1.2.1  riastrad 
    155  1.1.2.1  riastrad 	/* Lock the internal spin lock to make changes.  */
    156  1.1.2.1  riastrad 	spin_lock(&master->lock.spinlock);
    157  1.1.2.1  riastrad 
    158  1.1.2.1  riastrad 	/* Make sure it's actually locked.  */
    159  1.1.2.1  riastrad 	if (!_DRM_LOCK_IS_HELD(master->lock.hw_lock->lock)) {
    160  1.1.2.1  riastrad 		error = -EINVAL;	/* XXX Right error?  */
    161  1.1.2.1  riastrad 		goto out;
    162  1.1.2.1  riastrad 	}
    163  1.1.2.1  riastrad 
    164  1.1.2.1  riastrad 	/* Make sure it's locked in the right context.  */
    165  1.1.2.1  riastrad 	if (_DRM_LOCKING_CONTEXT(master->lock.hw_lock->lock) !=
    166  1.1.2.1  riastrad 	    lock_request->context) {
    167  1.1.2.1  riastrad 		error = -EACCES;	/* XXX Right error?  */
    168  1.1.2.1  riastrad 		goto out;
    169  1.1.2.1  riastrad 	}
    170  1.1.2.1  riastrad 
    171  1.1.2.1  riastrad 	/* Make sure it's locked by us.  */
    172  1.1.2.1  riastrad 	if (master->lock.file_priv != file) {
    173  1.1.2.1  riastrad 		error = -EACCES;	/* XXX Right error?  */
    174  1.1.2.1  riastrad 		goto out;
    175  1.1.2.1  riastrad 	}
    176  1.1.2.1  riastrad 
    177  1.1.2.1  riastrad 	/* Actually release the lock.  */
    178  1.1.2.1  riastrad 	drm_lock_release(&master->lock, lock_request->context);
    179  1.1.2.1  riastrad 
    180  1.1.2.1  riastrad 	/* Clear the lock's file pointer, just in case.  */
    181  1.1.2.1  riastrad 	master->lock.file_priv = NULL;
    182  1.1.2.1  riastrad 
    183  1.1.2.1  riastrad 	/* Unblock the signals we blocked in drm_lock.  */
    184  1.1.2.1  riastrad 	drm_lock_unblock_signals(dev, lock_request, file);
    185  1.1.2.1  riastrad 
    186  1.1.2.1  riastrad 	/* Success!  */
    187  1.1.2.1  riastrad 	error = 0;
    188  1.1.2.1  riastrad 
    189  1.1.2.1  riastrad out:	spin_unlock(&master->lock.spinlock);
    190  1.1.2.1  riastrad 	return error;
    191  1.1.2.1  riastrad }
    192  1.1.2.1  riastrad 
    193  1.1.2.1  riastrad /*
    194  1.1.2.2  riastrad  * Drop the lock.
    195  1.1.2.2  riastrad  *
    196  1.1.2.2  riastrad  * Return value is an artefact of Linux.  Caller must guarantee
    197  1.1.2.2  riastrad  * preconditions; failure is fatal.
    198  1.1.2.2  riastrad  */
    199  1.1.2.2  riastrad int
    200  1.1.2.2  riastrad drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
    201  1.1.2.2  riastrad {
    202  1.1.2.2  riastrad 
    203  1.1.2.2  riastrad 	spin_lock(&lock_data->spinlock);
    204  1.1.2.2  riastrad 	drm_lock_release(lock_data, context);
    205  1.1.2.2  riastrad 	spin_unlock(&lock_data->spinlock);
    206  1.1.2.2  riastrad 
    207  1.1.2.2  riastrad 	return 0;
    208  1.1.2.2  riastrad }
    209  1.1.2.2  riastrad 
    210  1.1.2.2  riastrad /*
    211  1.1.2.1  riastrad  * Take the lock for the kernel's use.
    212  1.1.2.1  riastrad  */
    213  1.1.2.1  riastrad void
    214  1.1.2.1  riastrad drm_idlelock_take(struct drm_lock_data *lock_data __unused)
    215  1.1.2.1  riastrad {
    216  1.1.2.1  riastrad 	KASSERT(mutex_is_locked(&drm_global_mutex));
    217  1.1.2.1  riastrad 	panic("drm_idlelock_take is not yet implemented"); /* XXX */
    218  1.1.2.1  riastrad }
    219  1.1.2.1  riastrad 
    220  1.1.2.1  riastrad /*
    221  1.1.2.1  riastrad  * Release the lock from the kernel.
    222  1.1.2.1  riastrad  */
    223  1.1.2.1  riastrad void
    224  1.1.2.1  riastrad drm_idlelock_release(struct drm_lock_data *lock_data __unused)
    225  1.1.2.1  riastrad {
    226  1.1.2.1  riastrad 	KASSERT(mutex_is_locked(&drm_global_mutex));
    227  1.1.2.1  riastrad 	panic("drm_idlelock_release is not yet implemented"); /* XXX */
    228  1.1.2.1  riastrad }
    229  1.1.2.1  riastrad 
    230  1.1.2.1  riastrad /*
    231  1.1.2.2  riastrad  * Does this file hold this drm device's hardware lock?
    232  1.1.2.2  riastrad  */
    233  1.1.2.2  riastrad int
    234  1.1.2.2  riastrad drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file)
    235  1.1.2.2  riastrad {
    236  1.1.2.2  riastrad 	struct drm_lock_data *const lock_data = &file->master->lock;
    237  1.1.2.2  riastrad 
    238  1.1.2.2  riastrad 	/* If this file has never locked anything, then no.  */
    239  1.1.2.2  riastrad 	if (file->lock_count == 0)
    240  1.1.2.2  riastrad 		return 0;
    241  1.1.2.2  riastrad 
    242  1.1.2.2  riastrad 	/* If there is no lock, then this file doesn't hold it.  */
    243  1.1.2.2  riastrad 	if (lock_data->hw_lock == NULL)
    244  1.1.2.2  riastrad 		return 0;
    245  1.1.2.2  riastrad 
    246  1.1.2.2  riastrad 	/* If this lock is not held, then this file doesn't hold it.   */
    247  1.1.2.2  riastrad 	if (!_DRM_LOCK_IS_HELD(lock_data->hw_lock->lock))
    248  1.1.2.2  riastrad 		return 0;
    249  1.1.2.2  riastrad 
    250  1.1.2.2  riastrad 	/*
    251  1.1.2.2  riastrad 	 * Otherwise, it boils down to whether this file is the owner
    252  1.1.2.2  riastrad 	 * or someone else.
    253  1.1.2.2  riastrad 	 */
    254  1.1.2.2  riastrad 	return (file == lock_data->file_priv);
    255  1.1.2.2  riastrad }
    256  1.1.2.2  riastrad 
    257  1.1.2.2  riastrad /*
    258  1.1.2.1  riastrad  * Try to acquire the lock.  Return true if successful, false if not.
    259  1.1.2.1  riastrad  *
    260  1.1.2.1  riastrad  * Lock's spinlock must be held.
    261  1.1.2.1  riastrad  */
    262  1.1.2.1  riastrad static bool
    263  1.1.2.1  riastrad drm_lock_acquire(struct drm_lock_data *lock_data, int context)
    264  1.1.2.1  riastrad {
    265  1.1.2.1  riastrad 
    266  1.1.2.1  riastrad 	KASSERT(spin_is_locked(&lock_data->spinlock));
    267  1.1.2.1  riastrad 
    268  1.1.2.1  riastrad 	/* Test and set.  */
    269  1.1.2.1  riastrad 	if (_DRM_LOCK_IS_HELD(lock_data->hw_lock->lock)) {
    270  1.1.2.1  riastrad 		return false;
    271  1.1.2.1  riastrad 	} else {
    272  1.1.2.1  riastrad 		lock_data->hw_lock->lock = (context | _DRM_LOCK_HELD);
    273  1.1.2.1  riastrad 		return true;
    274  1.1.2.1  riastrad 	}
    275  1.1.2.1  riastrad }
    276  1.1.2.1  riastrad 
    277  1.1.2.1  riastrad /*
    278  1.1.2.1  riastrad  * Release the lock held in the given context.  Wake any waiters,
    279  1.1.2.1  riastrad  * preferring kernel waiters over userland waiters.
    280  1.1.2.1  riastrad  *
    281  1.1.2.1  riastrad  * Lock's spinlock must be held and lock must be held in this context.
    282  1.1.2.1  riastrad  */
    283  1.1.2.1  riastrad static void
    284  1.1.2.1  riastrad drm_lock_release(struct drm_lock_data *lock_data, int context)
    285  1.1.2.1  riastrad {
    286  1.1.2.1  riastrad 
    287  1.1.2.1  riastrad 	(void)context;		/* ignore */
    288  1.1.2.1  riastrad 	KASSERT(spin_is_locked(&lock_data->spinlock));
    289  1.1.2.1  riastrad 	KASSERT(_DRM_LOCK_IS_HELD(lock_data->hw_lock->lock));
    290  1.1.2.1  riastrad 	KASSERT(_DRM_LOCKING_CONTEXT(lock_data->hw_lock->lock) == context);
    291  1.1.2.1  riastrad 
    292  1.1.2.1  riastrad 	/* Are there any kernel waiters?  */
    293  1.1.2.1  riastrad 	if (lock_data->n_kernel_waiters > 0) {
    294  1.1.2.1  riastrad 		/* If there's a kernel waiter, grant it ownership.  */
    295  1.1.2.1  riastrad 		lock_data->hw_lock->lock = (DRM_KERNEL_CONTEXT |
    296  1.1.2.1  riastrad 		    _DRM_LOCK_HELD);
    297  1.1.2.1  riastrad 		lock_data->n_kernel_waiters--;
    298  1.1.2.1  riastrad 		DRM_SPIN_WAKEUP_ONE(&lock_data->kernel_lock_queue,
    299  1.1.2.1  riastrad 		    &lock_data->spinlock);
    300  1.1.2.1  riastrad 	} else {
    301  1.1.2.1  riastrad 		/* Otherwise, free it up and wake someone else.  */
    302  1.1.2.1  riastrad 		lock_data->hw_lock->lock = 0;
    303  1.1.2.1  riastrad 		DRM_SPIN_WAKEUP_ONE(&lock_data->lock_queue,
    304  1.1.2.1  riastrad 		    &lock_data->spinlock);
    305  1.1.2.1  riastrad 	}
    306  1.1.2.1  riastrad }
    307  1.1.2.1  riastrad 
    308  1.1.2.1  riastrad /*
    309  1.1.2.1  riastrad  * Block signals for a process that holds a drm lock.
    310  1.1.2.1  riastrad  *
    311  1.1.2.1  riastrad  * XXX It's not processes but files that hold drm locks, so blocking
    312  1.1.2.1  riastrad  * signals in a process seems wrong, and it's not clear that blocking
    313  1.1.2.1  riastrad  * signals automatically is remotely sensible anyway.
    314  1.1.2.1  riastrad  */
    315  1.1.2.1  riastrad static int
    316  1.1.2.1  riastrad drm_lock_block_signals(struct drm_device *dev __unused,
    317  1.1.2.1  riastrad     struct drm_lock *lock_request __unused, struct drm_file *file __unused)
    318  1.1.2.1  riastrad {
    319  1.1.2.1  riastrad 	return 0;
    320  1.1.2.1  riastrad }
    321  1.1.2.1  riastrad 
    322  1.1.2.1  riastrad /*
    323  1.1.2.1  riastrad  * Unblock the signals that drm_lock_block_signals blocked.
    324  1.1.2.1  riastrad  */
    325  1.1.2.1  riastrad static void
    326  1.1.2.1  riastrad drm_lock_unblock_signals(struct drm_device *dev __unused,
    327  1.1.2.1  riastrad     struct drm_lock *lock_request __unused, struct drm_file *file __unused)
    328  1.1.2.1  riastrad {
    329  1.1.2.1  riastrad }
    330