xref: /netbsd-src/sys/external/bsd/vchiq/dist/interface/compat/vchi_bsd.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*-
2  * Copyright (c) 2010 Max Khon <fjoe@freebsd.org>
3  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@bluezbox.com>
4  * Copyright (c) 2013 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #ifndef __VCHI_NETBSD_H__
29 #define __VCHI_NETBSD_H__
30 
31 #include <sys/systm.h>
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/lock.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/mutex.h>
39 #include <sys/malloc.h>
40 #include <sys/proc.h>
41 #include <sys/types.h>
42 #include <sys/ioccom.h>
43 #include <sys/atomic.h>
44 #include <sys/rwlock.h>
45 #include <sys/callout.h>
46 
47 #include <linux/completion.h>
48 #include <asm/barrier.h>
49 
50 /*
51  * Copy from/to user API
52  */
53 #define copy_from_user(to, from, n)	copyin((from), (to), (n))
54 #define copy_to_user(to, from, n)	copyout((from), (to), (n))
55 
56 /*
57  * Atomic API
58  */
59 typedef volatile unsigned int atomic_t;
60 
61 #define atomic_set(p, v)	(*(p) = (v))
62 #define atomic_read(p)		(*(volatile int *)(p))
63 #define atomic_inc(p)		atomic_inc_uint(p)
64 #define atomic_dec(p)		atomic_dec_uint(p)
65 #define atomic_dec_and_test(p)	(atomic_dec_uint_nv(p) == 0)
66 #define	atomic_inc_return(v)	atomic_inc_uint_nv(v)
67 #define	atomic_dec_return(v)	atomic_dec_uint_nv(v)
68 #define atomic_add(v, p)	atomic_add_int(p, v)
69 #define atomic_sub(v, p)	atomic_add_int(p, -(v))
70 #define atomic_add_return(v, p)	atomic_add_int_nv(p, v)
71 #define atomic_sub_return(v, p)	atomic_add_int_nv(p, -(v))
72 #define atomic_xchg(p, v)	atomic_swap_uint(p, v)
73 #define atomic_cmpxchg(p, oldv, newv) atomic_cas_uint(p, oldv, newv)
74 
75 #define ATOMIC_INIT(v)		(v)
76 
77 /*
78  * Spinlock API
79  */
80 typedef kmutex_t spinlock_t;
81 
82 /*
83  * NB: Need to initialize these at attach time!
84  */
85 #define DEFINE_SPINLOCK(name)	kmutex_t name
86 
87 #define spin_lock_init(lock)	mutex_init(lock, MUTEX_DEFAULT, IPL_VM)
88 #define spin_lock_destroy(lock)	mutex_destroy(lock)
89 #define spin_lock(lock)		mutex_spin_enter(lock)
90 #define spin_unlock(lock)	mutex_spin_exit(lock)
91 
92 /*
93  * Mutex API
94  */
95 struct mutex {
96 	kmutex_t	mtx;
97 };
98 
99 #define	lmutex_init(lock)	mutex_init(&(lock)->mtx, MUTEX_DEFAULT, IPL_NONE)
100 #define lmutex_destroy(lock)	mutex_destroy(&(lock)->mtx)
101 #define	lmutex_lock(lock)	mutex_enter(&(lock)->mtx)
102 #define	lmutex_lock_interruptible(lock)	(mutex_enter(&(lock)->mtx),0)
103 #define	lmutex_unlock(lock)	mutex_exit(&(lock)->mtx)
104 
105 /*
106  * Rwlock API
107  */
108 typedef kmutex_t rwlock_t;
109 
110 #define DEFINE_RWLOCK(name)	kmutex_t name
111 
112 #define rwlock_init(rwlock)	mutex_init(rwlock, MUTEX_DEFAULT, IPL_VM)
113 #define read_lock(rwlock)	mutex_spin_enter(rwlock)
114 #define read_unlock(rwlock)	mutex_spin_exit(rwlock)
115 
116 #define write_lock(rwlock)	mutex_spin_enter(rwlock)
117 #define write_unlock(rwlock)	mutex_spin_exit(rwlock)
118 
119 #define read_lock_bh(rwlock)	read_lock(rwlock)
120 #define read_unlock_bh(rwlock)	read_unlock(rwlock)
121 #define write_lock_bh(rwlock)	write_lock(rwlock)
122 #define write_unlock_bh(rwlock)	write_unlock(rwlock)
123 
124 /*
125  * Timer API
126  */
127 struct timer_list {
128 	kmutex_t mtx;
129 	callout_t callout;
130 
131 	unsigned long expires;
132 	void (*function)(unsigned long);
133 	unsigned long data;
134 };
135 
136 void init_timer(struct timer_list *t);
137 void setup_timer(struct timer_list *t, void (*function)(unsigned long), unsigned long data);
138 void mod_timer(struct timer_list *t, unsigned long expires);
139 void add_timer(struct timer_list *t);
140 int del_timer(struct timer_list *t);
141 int del_timer_sync(struct timer_list *t);
142 
143 /*
144  * Semaphore API
145  */
146 struct semaphore {
147 	kmutex_t	mtx;
148 	kcondvar_t	cv;
149 	int		value;
150 	int		waiters;
151 };
152 
153 /*
154  * NB: Need to initialize these at attach time!
155  */
156 #define	DEFINE_SEMAPHORE(name)	struct semaphore name
157 
158 void sema_sysinit(void *arg);
159 void _sema_init(struct semaphore *s, int value);
160 void _sema_destroy(struct semaphore *s);
161 void down(struct semaphore *s);
162 int down_interruptible(struct semaphore *s);
163 int down_trylock(struct semaphore *s);
164 void up(struct semaphore *s);
165 
166 /*
167  * Logging and assertions API
168  */
169 void rlprintf(int pps, const char *fmt, ...)
170 	__printflike(2, 3);
171 
172 void
173 device_rlprintf(int pps, device_t dev, const char *fmt, ...)
174 	__printflike(3, 4);
175 
176 #define might_sleep()
177 
178 #define WARN(condition, msg)				\
179 ({							\
180 	int __ret_warn_on = !!(condition);		\
181 	if (unlikely(__ret_warn_on))			\
182 		printf((msg));				\
183 	unlikely(__ret_warn_on);			\
184 })
185 
186 
187 
188 #define WARN_ON(condition)				\
189 ({							\
190 	int __ret_warn_on = !!(condition);		\
191 	if (unlikely(__ret_warn_on))			\
192 		printf("WARN_ON: " #condition "\n");	\
193 	unlikely(__ret_warn_on);			\
194 })
195 
196 #define WARN_ON_ONCE(condition) ({			\
197 	static int __warned;				\
198 	int __ret_warn_once = !!(condition);		\
199 							\
200 	if (unlikely(__ret_warn_once))			\
201 		if (WARN_ON(!__warned))			\
202 			__warned = 1;			\
203 	unlikely(__ret_warn_once);			\
204 })
205 
206 #define BUG_ON(cond)					\
207 	do {						\
208 		if (cond)				\
209 			panic("BUG_ON: " #cond);	\
210 	} while (0)
211 
212 #define BUG()						\
213 	do {						\
214 		panic("BUG: %s:%d", __FILE__, __LINE__);	\
215 	} while (0)
216 
217 #define vchiq_static_assert(cond) CTASSERT(cond)
218 
219 #define KERN_EMERG	"<0>"	/* system is unusable			*/
220 #define KERN_ALERT	"<1>"	/* action must be taken immediately	*/
221 #define KERN_CRIT	"<2>"	/* critical conditions			*/
222 #define KERN_ERR	"<3>"	/* error conditions			*/
223 #define KERN_WARNING	"<4>"	/* warning conditions			*/
224 #define KERN_NOTICE	"<5>"	/* normal but significant condition	*/
225 #define KERN_INFO	"<6>"	/* informational			*/
226 #define KERN_DEBUG	"<7>"	/* debug-level messages			*/
227 #define KERN_CONT	""
228 
229 #define printk(fmt, args...)		printf(fmt, ##args)
230 #define vprintk(fmt, args)		vprintf(fmt, args)
231 
232 /*
233  * Malloc API
234  */
235 #define GFP_KERNEL	0
236 #define GFP_ATOMIC	0
237 
238 MALLOC_DECLARE(M_VCHI);
239 
240 #define kmalloc(size, flags)	malloc((size), M_VCHI, M_NOWAIT | M_ZERO)
241 #define kcalloc(n, size, flags)	malloc((n) * (size), M_VCHI, M_NOWAIT | M_ZERO)
242 #define kzalloc(a, b)		kcalloc(1, (a), (b))
243 #define kfree(p)		do { if (p) free(p, M_VCHI); } while (0)
244 
245 /*
246  * Kernel module API
247  */
248 #define __init
249 #define __exit
250 #define __devinit
251 #define __devexit
252 #define __devinitdata
253 
254 /*
255  * Time API
256  */
257 #if 1
258 /* emulate jiffies */
259 static inline unsigned long
260 _jiffies(void)
261 {
262 	struct timeval tv;
263 
264 	microuptime(&tv);
265 	return tvtohz(&tv);
266 }
267 
268 static inline unsigned long
269 msecs_to_jiffies(unsigned long msecs)
270 {
271 	struct timeval tv;
272 
273 	tv.tv_sec = msecs / 1000000UL;
274 	tv.tv_usec = msecs % 1000000UL;
275 	return tvtohz(&tv);
276 }
277 
278 #define jiffies			_jiffies()
279 #else
280 #define jiffies			ticks
281 #endif
282 #define HZ			hz
283 
284 #define udelay(usec)		DELAY(usec)
285 #define mdelay(msec)		DELAY((msec) * 1000)
286 
287 #define schedule_timeout(jiff)	kpause("dhdslp", false, jiff, NULL)
288 
289 #if defined(msleep)
290 #undef msleep
291 #endif
292 #define msleep(msec)		mdelay(msec)
293 
294 #define time_after(a, b)	((a) > (b))
295 #define time_after_eq(a, b)	((a) >= (b))
296 #define time_before(a, b)	time_after((b), (a))
297 
298 /*
299  * kthread API (we use lwp)
300  */
301 typedef lwp_t * VCHIQ_THREAD_T;
302 
303 VCHIQ_THREAD_T vchiq_thread_create(int (*threadfn)(void *data),
304                                    void *data,
305                                    const char namefmt[], ...);
306 void set_user_nice(VCHIQ_THREAD_T p, int nice);
307 void wake_up_process(VCHIQ_THREAD_T p);
308 
309 /*
310  * Proc APIs
311  */
312 void flush_signals(VCHIQ_THREAD_T);
313 int fatal_signal_pending(VCHIQ_THREAD_T);
314 
315 /*
316  * Misc API
317  */
318 
319 #define __user
320 
321 #define	current			curlwp
322 #define EXPORT_SYMBOL(x)
323 #define PAGE_ALIGN(addr)	round_page(addr)
324 
325 typedef	void	irqreturn_t;
326 typedef	off_t	loff_t;
327 
328 #define BCM2835_MBOX_CHAN_VCHIQ	3
329 #define bcm_mbox_write	bcmmbox_write
330 
331 #define dsb	membar_producer
332 
333 #define device_print_prettyname(dev)	device_printf((dev), "")
334 
335 #endif /* __VCHI_NETBSD_H__ */
336