xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/via/via_video.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: via_video.c,v 1.8 2021/12/18 23:45:44 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2005 Thomas Hellstrom. 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, sub license,
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
14  * next paragraph) shall be included in all copies or substantial portions
15  * of the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHOR(S), AND/OR THE COPYRIGHT HOLDER(S) 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 OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Author: Thomas Hellstrom 2005.
26  *
27  * Video and XvMC related functions.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: via_video.c,v 1.8 2021/12/18 23:45:44 riastradh Exp $");
32 
33 #include <drm/drm_device.h>
34 #include <drm/via_drm.h>
35 
36 #include "via_drv.h"
37 
via_init_futex(drm_via_private_t * dev_priv)38 void via_init_futex(drm_via_private_t *dev_priv)
39 {
40 	unsigned int i;
41 
42 	DRM_DEBUG("\n");
43 
44 	for (i = 0; i < VIA_NR_XVMC_LOCKS; ++i) {
45 #ifdef __NetBSD__
46 		spin_lock_init(&dev_priv->decoder_lock[i]);
47 		DRM_INIT_WAITQUEUE(&dev_priv->decoder_queue[i], "viadec");
48 #else
49 		init_waitqueue_head(&(dev_priv->decoder_queue[i]));
50 #endif
51 		XVMCLOCKPTR(dev_priv->sarea_priv, i)->lock = 0;
52 	}
53 }
54 
via_cleanup_futex(drm_via_private_t * dev_priv)55 void via_cleanup_futex(drm_via_private_t *dev_priv)
56 {
57 #ifdef __NetBSD__
58 	unsigned i;
59 
60 	for (i = 0; i < VIA_NR_XVMC_LOCKS; ++i) {
61 		DRM_DESTROY_WAITQUEUE(&dev_priv->decoder_queue[i]);
62 		spin_lock_destroy(&dev_priv->decoder_lock[i]);
63 	}
64 #endif
65 }
66 
via_release_futex(drm_via_private_t * dev_priv,int context)67 void via_release_futex(drm_via_private_t *dev_priv, int context)
68 {
69 	unsigned int i;
70 	volatile int *lock;
71 
72 	if (!dev_priv->sarea_priv)
73 		return;
74 
75 	for (i = 0; i < VIA_NR_XVMC_LOCKS; ++i) {
76 		lock = (volatile int *)XVMCLOCKPTR(dev_priv->sarea_priv, i);
77 		if ((_DRM_LOCKING_CONTEXT(*lock) == context)) {
78 			if (_DRM_LOCK_IS_HELD(*lock)
79 			    && (*lock & _DRM_LOCK_CONT)) {
80 #ifdef __NetBSD__
81 				spin_lock(&dev_priv->decoder_lock[i]);
82 				DRM_SPIN_WAKEUP_ALL(&dev_priv->decoder_queue[i],
83 				    &dev_priv->decoder_lock[i]);
84 				spin_unlock(&dev_priv->decoder_lock[i]);
85 #else
86 				wake_up(&(dev_priv->decoder_queue[i]));
87 #endif
88 			}
89 			*lock = 0;
90 		}
91 	}
92 }
93 
via_decoder_futex(struct drm_device * dev,void * data,struct drm_file * file_priv)94 int via_decoder_futex(struct drm_device *dev, void *data, struct drm_file *file_priv)
95 {
96 	drm_via_futex_t *fx = data;
97 	volatile int *lock;
98 	drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
99 	drm_via_sarea_t *sAPriv = dev_priv->sarea_priv;
100 	int ret = 0;
101 
102 	DRM_DEBUG("\n");
103 
104 	if (fx->lock >= VIA_NR_XVMC_LOCKS)
105 		return -EFAULT;
106 
107 	lock = (volatile int *)XVMCLOCKPTR(sAPriv, fx->lock);
108 
109 	switch (fx->func) {
110 	case VIA_FUTEX_WAIT:
111 #ifdef __NetBSD__
112 		spin_lock(&dev_priv->decoder_lock[fx->lock]);
113 		DRM_SPIN_WAIT_ON(ret, &dev_priv->decoder_queue[fx->lock],
114 		    &dev_priv->decoder_lock[fx->lock],
115 		    (fx->ms / 10) * (HZ / 100),
116 		    *lock != fx->val);
117 		spin_unlock(&dev_priv->decoder_lock[fx->lock]);
118 #else
119 		VIA_WAIT_ON(ret, dev_priv->decoder_queue[fx->lock],
120 			    (fx->ms / 10) * (HZ / 100), *lock != fx->val);
121 #endif
122 		return ret;
123 	case VIA_FUTEX_WAKE:
124 #ifdef __NetBSD__
125 		spin_lock(&dev_priv->decoder_lock[fx->lock]);
126 		DRM_SPIN_WAKEUP_ALL(&dev_priv->decoder_queue[fx->lock],
127 		    &dev_priv->decoder_lock[fx->lock]);
128 		spin_unlock(&dev_priv->decoder_lock[fx->lock]);
129 #else
130 		wake_up(&(dev_priv->decoder_queue[fx->lock]));
131 #endif
132 		return 0;
133 	}
134 	return 0;
135 }
136