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