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