xref: /openbsd-src/sys/dev/pci/drm/drm_lock.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*-
2  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Rickard E. (Rik) Faith <faith@valinux.com>
27  *    Gareth Hughes <gareth@valinux.com>
28  *
29  */
30 
31 /** @file drm_lock.c
32  * Implementation of the ioctls and other support code for dealing with the
33  * hardware lock.
34  *
35  * The DRM hardware lock is a shared structure between the kernel and userland.
36  *
37  * On uncontended access where the new context was the last context, the
38  * client may take the lock without dropping down into the kernel, using atomic
39  * compare-and-set.
40  *
41  * If the client finds during compare-and-set that it was not the last owner
42  * of the lock, it calls the DRM lock ioctl, which may sleep waiting for the
43  * lock, and may have side-effects of kernel-managed context switching.
44  *
45  * When the client releases the lock, if the lock is marked as being contended
46  * by another client, then the DRM unlock ioctl is called so that the
47  * contending client may be woken up.
48  */
49 
50 #include "drmP.h"
51 
52 int
53 drm_lock_take(struct drm_lock_data *lock_data, unsigned int context)
54 {
55 	volatile unsigned int	*lock = &lock_data->hw_lock->lock;
56 	unsigned int		 old, new;
57 
58 	do {
59 		old = *lock;
60 		if (old & _DRM_LOCK_HELD)
61 			new = old | _DRM_LOCK_CONT;
62 		else
63 			new = context | _DRM_LOCK_HELD;
64 	} while (!atomic_cmpset_int(lock, old, new));
65 
66 	if (_DRM_LOCKING_CONTEXT(old) == context && _DRM_LOCK_IS_HELD(old)) {
67 		if (context != DRM_KERNEL_CONTEXT)
68 			DRM_ERROR("%d holds heavyweight lock\n", context);
69 		return (0);
70 	}
71 	/* If the lock wasn't held before, it's ours */
72 	return (!_DRM_LOCK_IS_HELD(old));
73 }
74 
75 int
76 drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
77 {
78 	volatile unsigned int	*lock = &lock_data->hw_lock->lock;
79 	unsigned int		 old, new;
80 
81 	mtx_enter(&lock_data->spinlock);
82 	lock_data->file_priv = NULL;
83 	do {
84 		old  = *lock;
85 		new  = 0;
86 	} while (!atomic_cmpset_int(lock, old, new));
87 	mtx_leave(&lock_data->spinlock);
88 
89 	if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
90 		DRM_ERROR("%d freed heavyweight lock held by %d\n",
91 			  context, _DRM_LOCKING_CONTEXT(old));
92 		return 1;
93 	}
94 	wakeup(lock_data);
95 	return 0;
96 }
97 
98 int
99 drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
100 {
101         struct drm_lock	*lock = data;
102         int		 ret = 0;
103 
104         if (lock->context == DRM_KERNEL_CONTEXT) {
105                 DRM_ERROR("Process %d using kernel context %d\n",
106 		    DRM_CURRENTPID, lock->context);
107                 return EINVAL;
108         }
109 
110         DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
111 	    lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock,
112 	    lock->flags);
113 
114 	mtx_enter(&dev->lock.spinlock);
115 	for (;;) {
116 		if (drm_lock_take(&dev->lock, lock->context)) {
117 			dev->lock.file_priv = file_priv;
118 			break;  /* Got lock */
119 		}
120 
121 		/* Contention */
122 		ret = msleep(&dev->lock, &dev->lock.spinlock,
123 		    PZERO | PCATCH, "drmlkq", 0);
124 		if (ret != 0)
125 			break;
126 	}
127 	mtx_leave(&dev->lock.spinlock);
128 	DRM_DEBUG("%d %s\n", lock->context, ret ? "interrupted" : "has lock");
129 
130 	if (ret != 0)
131 		return ret;
132 
133 	/* XXX: Add signal blocking here */
134 
135 	if (dev->driver->dma_quiescent != NULL &&
136 	    (lock->flags & _DRM_LOCK_QUIESCENT))
137 		dev->driver->dma_quiescent(dev);
138 
139 	return 0;
140 }
141 
142 int
143 drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
144 {
145 	struct drm_lock	*lock = data;
146 
147 	if (lock->context == DRM_KERNEL_CONTEXT) {
148 		DRM_ERROR("Process %d using kernel context %d\n",
149 		    DRM_CURRENTPID, lock->context);
150 		return EINVAL;
151 	}
152 	/* Check that the context unlock being requested actually matches
153 	 * who currently holds the lock.
154 	 */
155 	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||
156 	    _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) != lock->context)
157 		return EINVAL;
158 
159 	if (drm_lock_free(&dev->lock, lock->context)) {
160 		DRM_ERROR("\n");
161 	}
162 
163 	return 0;
164 }
165