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