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