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