xref: /netbsd-src/sys/external/bsd/vchiq/dist/interface/compat/vchi_bsd.h (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
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 
49 /*
50  * Copy from/to user API
51  */
52 #define copy_from_user(to, from, n)	copyin((from), (to), (n))
53 #define copy_to_user(to, from, n)	copyout((from), (to), (n))
54 
55 /*
56  * Bit API
57  */
58 
59 static __inline int
60 test_and_set_bit(int nr, volatile void *addr)
61 {
62 	volatile uint32_t *val;
63 	uint32_t mask, old;
64 
65 	val = (volatile uint32_t *)addr;
66 	mask = 1 << nr;
67 
68 	do {
69 		old = *val;
70 		if ((old & mask) != 0)
71 			break;
72 	} while (atomic_cas_uint(val, old, old | mask) != old);
73 
74 	return old & mask;
75 }
76 
77 static __inline__ int
78 test_and_clear_bit(int nr, volatile void *addr)
79 {
80 	volatile uint32_t *val;
81 	uint32_t mask, old;
82 
83 	val = (volatile uint32_t *)addr;
84 	mask = 1 << nr;
85 
86 	do {
87 		old = *val;
88 		if ((old & mask) == 0)
89 			break;
90 	} while (atomic_cas_uint(val, old, old & ~mask) != old);
91 
92 	return old & mask;
93 }
94 
95 /*
96  * Atomic API
97  */
98 typedef volatile unsigned int atomic_t;
99 
100 #define atomic_set(p, v)	(*(p) = (v))
101 #define atomic_read(p)		(*(volatile int *)(p))
102 #define atomic_inc(p)		atomic_inc_uint(p)
103 #define atomic_dec(p)		atomic_dec_uint(p)
104 #define atomic_dec_and_test(p)	(atomic_dec_uint_nv(p) == 0)
105 #define	atomic_inc_return(v)	atomic_inc_uint_nv(v)
106 #define	atomic_dec_return(v)	atomic_dec_uint_nv(v)
107 #define atomic_add(v, p)	atomic_add_int(p, v)
108 #define atomic_sub(v, p)	atomic_add_int(p, -(v))
109 #define atomic_add_return(v, p)	atomic_add_int_nv(p, v)
110 #define atomic_sub_return(v, p)	atomic_add_int_nv(p, -(v))
111 #define atomic_xchg(p, v)	atomic_swap_uint(p, v)
112 #define atomic_cmpxchg(p, oldv, newv) atomic_cas_uint(p, oldv, newv)
113 
114 #define ATOMIC_INIT(v)		(v)
115 
116 /*
117  * Spinlock API
118  */
119 typedef kmutex_t spinlock_t;
120 
121 /*
122  * NB: Need to initialize these at attach time!
123  */
124 #define DEFINE_SPINLOCK(name)	kmutex_t name
125 
126 #define spin_lock_init(lock)	mutex_init(lock, MUTEX_DEFAULT, IPL_VM)
127 #define spin_lock_destroy(lock)	mutex_destroy(lock)
128 #define spin_lock(lock)		mutex_spin_enter(lock)
129 #define spin_unlock(lock)	mutex_spin_exit(lock)
130 
131 /*
132  * Mutex API
133  */
134 struct mutex {
135 	kmutex_t	mtx;
136 };
137 
138 #define	lmutex_init(lock)	mutex_init(&(lock)->mtx, MUTEX_DEFAULT, IPL_NONE)
139 #define lmutex_destroy(lock)	mutex_destroy(&(lock)->mtx)
140 #define	lmutex_lock(lock)	mutex_enter(&(lock)->mtx)
141 #define	lmutex_lock_interruptible(lock)	(mutex_enter(&(lock)->mtx),0)
142 #define	lmutex_unlock(lock)	mutex_exit(&(lock)->mtx)
143 
144 /*
145  * Rwlock API
146  */
147 typedef kmutex_t rwlock_t;
148 
149 #define DEFINE_RWLOCK(name)	kmutex_t name
150 
151 #define rwlock_init(rwlock)	mutex_init(rwlock, MUTEX_DEFAULT, IPL_VM)
152 #define read_lock(rwlock)	mutex_spin_enter(rwlock)
153 #define read_unlock(rwlock)	mutex_spin_exit(rwlock)
154 
155 #define write_lock(rwlock)	mutex_spin_enter(rwlock)
156 #define write_unlock(rwlock)	mutex_spin_exit(rwlock)
157 
158 #define read_lock_bh(rwlock)	read_lock(rwlock)
159 #define read_unlock_bh(rwlock)	read_unlock(rwlock)
160 #define write_lock_bh(rwlock)	write_lock(rwlock)
161 #define write_unlock_bh(rwlock)	write_unlock(rwlock)
162 
163 /*
164  * Timer API
165  */
166 struct timer_list {
167 	kmutex_t mtx;
168 	callout_t callout;
169 
170 	unsigned long expires;
171 	void (*function)(unsigned long);
172 	unsigned long data;
173 };
174 
175 void init_timer(struct timer_list *t);
176 void setup_timer(struct timer_list *t, void (*function)(unsigned long), unsigned long data);
177 void mod_timer(struct timer_list *t, unsigned long expires);
178 void add_timer(struct timer_list *t);
179 int del_timer(struct timer_list *t);
180 int del_timer_sync(struct timer_list *t);
181 
182 /*
183  * Semaphore API
184  */
185 struct semaphore {
186 	kmutex_t	mtx;
187 	kcondvar_t	cv;
188 	int		value;
189 	int		waiters;
190 };
191 
192 /*
193  * NB: Need to initialize these at attach time!
194  */
195 #define	DEFINE_SEMAPHORE(name)	struct semaphore name
196 
197 void sema_sysinit(void *arg);
198 void _sema_init(struct semaphore *s, int value);
199 void _sema_destroy(struct semaphore *s);
200 void down(struct semaphore *s);
201 int down_interruptible(struct semaphore *s);
202 int down_trylock(struct semaphore *s);
203 void up(struct semaphore *s);
204 
205 /*
206  * Logging and assertions API
207  */
208 void rlprintf(int pps, const char *fmt, ...)
209 	__printflike(2, 3);
210 
211 void
212 device_rlprintf(int pps, device_t dev, const char *fmt, ...)
213 	__printflike(3, 4);
214 
215 #define might_sleep()
216 
217 #define WARN(condition, msg)				\
218 ({							\
219 	int __ret_warn_on = !!(condition);		\
220 	if (unlikely(__ret_warn_on))			\
221 		printf((msg));				\
222 	unlikely(__ret_warn_on);			\
223 })
224 
225 
226 
227 #define WARN_ON(condition)				\
228 ({							\
229 	int __ret_warn_on = !!(condition);		\
230 	if (unlikely(__ret_warn_on))			\
231 		printf("WARN_ON: " #condition "\n");	\
232 	unlikely(__ret_warn_on);			\
233 })
234 
235 #define WARN_ON_ONCE(condition) ({			\
236 	static int __warned;				\
237 	int __ret_warn_once = !!(condition);		\
238 							\
239 	if (unlikely(__ret_warn_once))			\
240 		if (WARN_ON(!__warned))			\
241 			__warned = 1;			\
242 	unlikely(__ret_warn_once);			\
243 })
244 
245 #define BUG_ON(cond)					\
246 	do {						\
247 		if (cond)				\
248 			panic("BUG_ON: " #cond);	\
249 	} while (0)
250 
251 #define BUG()						\
252 	do {						\
253 		panic("BUG: %s:%d", __FILE__, __LINE__);	\
254 	} while (0)
255 
256 #define vchiq_static_assert(cond) CTASSERT(cond)
257 
258 #define KERN_EMERG	"<0>"	/* system is unusable			*/
259 #define KERN_ALERT	"<1>"	/* action must be taken immediately	*/
260 #define KERN_CRIT	"<2>"	/* critical conditions			*/
261 #define KERN_ERR	"<3>"	/* error conditions			*/
262 #define KERN_WARNING	"<4>"	/* warning conditions			*/
263 #define KERN_NOTICE	"<5>"	/* normal but significant condition	*/
264 #define KERN_INFO	"<6>"	/* informational			*/
265 #define KERN_DEBUG	"<7>"	/* debug-level messages			*/
266 #define KERN_CONT	""
267 
268 #define printk(fmt, args...)		printf(fmt, ##args)
269 #define vprintk(fmt, args)		vprintf(fmt, args)
270 
271 /*
272  * Malloc API
273  */
274 #define GFP_KERNEL	0
275 #define GFP_ATOMIC	0
276 
277 MALLOC_DECLARE(M_VCHI);
278 
279 #define kmalloc(size, flags)	malloc((size), M_VCHI, M_NOWAIT | M_ZERO)
280 #define kcalloc(n, size, flags)	malloc((n) * (size), M_VCHI, M_NOWAIT | M_ZERO)
281 #define kzalloc(a, b)		kcalloc(1, (a), (b))
282 #define kfree(p)		do { if (p) free(p, M_VCHI); } while (0)
283 
284 /*
285  * Kernel module API
286  */
287 #define __init
288 #define __exit
289 #define __devinit
290 #define __devexit
291 #define __devinitdata
292 
293 /*
294  * Time API
295  */
296 #if 1
297 /* emulate jiffies */
298 static inline unsigned long
299 _jiffies(void)
300 {
301 	struct timeval tv;
302 
303 	microuptime(&tv);
304 	return tvtohz(&tv);
305 }
306 
307 static inline unsigned long
308 msecs_to_jiffies(unsigned long msecs)
309 {
310 	struct timeval tv;
311 
312 	tv.tv_sec = msecs / 1000000UL;
313 	tv.tv_usec = msecs % 1000000UL;
314 	return tvtohz(&tv);
315 }
316 
317 #define jiffies			_jiffies()
318 #else
319 #define jiffies			ticks
320 #endif
321 #define HZ			hz
322 
323 #define udelay(usec)		DELAY(usec)
324 #define mdelay(msec)		DELAY((msec) * 1000)
325 
326 #define schedule_timeout(jiff)	kpause("dhdslp", false, jiff, NULL)
327 
328 #if defined(msleep)
329 #undef msleep
330 #endif
331 #define msleep(msec)		mdelay(msec)
332 
333 #define time_after(a, b)	((a) > (b))
334 #define time_after_eq(a, b)	((a) >= (b))
335 #define time_before(a, b)	time_after((b), (a))
336 
337 /*
338  * kthread API (we use lwp)
339  */
340 typedef lwp_t * VCHIQ_THREAD_T;
341 
342 VCHIQ_THREAD_T vchiq_thread_create(int (*threadfn)(void *data),
343                                    void *data,
344                                    const char namefmt[], ...);
345 void set_user_nice(VCHIQ_THREAD_T p, int nice);
346 void wake_up_process(VCHIQ_THREAD_T p);
347 
348 /*
349  * Proc APIs
350  */
351 void flush_signals(VCHIQ_THREAD_T);
352 int fatal_signal_pending(VCHIQ_THREAD_T);
353 
354 /*
355  * Misc API
356  */
357 
358 #define __user
359 
360 #define	current			curlwp
361 #define EXPORT_SYMBOL(x)
362 #define PAGE_ALIGN(addr)	round_page(addr)
363 
364 typedef	void	irqreturn_t;
365 typedef	off_t	loff_t;
366 
367 #define BCM2835_MBOX_CHAN_VCHIQ	3
368 #define bcm_mbox_write	bcmmbox_write
369 
370 #define rmb	membar_consumer
371 #define wmb	membar_producer
372 #define dsb	membar_producer
373 
374 #define smp_mb	membar_producer
375 #define smp_rmb	membar_consumer
376 #define smp_wmb	membar_producer
377 
378 #define device_print_prettyname(dev)	device_printf((dev), "")
379 
380 #endif /* __VCHI_NETBSD_H__ */
381