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