vchi_bsd.h revision 1.6
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
57static __inline int
58test_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
75static __inline__ int
76test_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 */
96typedef volatile unsigned int atomic_t;
97
98#define atomic_set(p, v)	(*(p) = (v))
99#define atomic_read(p)		(*(volatile int *)(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 */
117typedef 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
129/*
130 * Mutex API
131 */
132struct mutex {
133	kmutex_t	mtx;
134};
135
136#define	lmutex_init(lock)	mutex_init(&(lock)->mtx, MUTEX_DEFAULT, IPL_NONE)
137#define lmutex_destroy(lock)	mutex_destroy(&(lock)->mtx)
138#define	lmutex_lock(lock)	mutex_enter(&(lock)->mtx)
139#define	lmutex_lock_interruptible(lock)	(mutex_enter(&(lock)->mtx),0)
140#define	lmutex_unlock(lock)	mutex_exit(&(lock)->mtx)
141
142/*
143 * Rwlock API
144 */
145typedef kmutex_t rwlock_t;
146
147#define DEFINE_RWLOCK(name)	kmutex_t name
148
149#define rwlock_init(rwlock)	mutex_init(rwlock, MUTEX_DEFAULT, IPL_VM)
150#define read_lock(rwlock)	mutex_spin_enter(rwlock)
151#define read_unlock(rwlock)	mutex_spin_exit(rwlock)
152
153#define write_lock(rwlock)	mutex_spin_enter(rwlock)
154#define write_unlock(rwlock)	mutex_spin_exit(rwlock)
155
156#define read_lock_bh(rwlock)	read_lock(rwlock)
157#define read_unlock_bh(rwlock)	read_unlock(rwlock)
158#define write_lock_bh(rwlock)	write_lock(rwlock)
159#define write_unlock_bh(rwlock)	write_unlock(rwlock)
160
161/*
162 * Timer API
163 */
164struct timer_list {
165	kmutex_t mtx;
166	callout_t callout;
167
168	unsigned long expires;
169	void (*function)(unsigned long);
170	unsigned long data;
171};
172
173void init_timer(struct timer_list *t);
174void setup_timer(struct timer_list *t, void (*function)(unsigned long), unsigned long data);
175void mod_timer(struct timer_list *t, unsigned long expires);
176void add_timer(struct timer_list *t);
177int del_timer(struct timer_list *t);
178int del_timer_sync(struct timer_list *t);
179
180/*
181 * Completion API
182 */
183struct completion {
184	kcondvar_t cv;
185	kmutex_t lock;
186	int done;
187};
188
189void init_completion(struct completion *c);
190void destroy_completion(struct completion *c);
191int try_wait_for_completion(struct completion *);
192int wait_for_completion_interruptible(struct completion *);
193int wait_for_completion_interruptible_timeout(struct completion *, unsigned long ticks);
194int wait_for_completion_killable(struct completion *);
195void wait_for_completion(struct completion *c);
196int wait_for_completion_timeout(struct completion *c, unsigned long timeout);
197void complete(struct completion *c);
198void complete_all(struct completion *c);
199
200#define	INIT_COMPLETION(x)	do {(x).done = 0;} while(0)
201
202/*
203 * Semaphore API
204 */
205struct semaphore {
206	kmutex_t	mtx;
207	kcondvar_t	cv;
208	int		value;
209	int		waiters;
210};
211
212/*
213 * NB: Need to initialize these at attach time!
214 */
215#define	DEFINE_SEMAPHORE(name)	struct semaphore name
216
217void sema_sysinit(void *arg);
218void _sema_init(struct semaphore *s, int value);
219void _sema_destroy(struct semaphore *s);
220void down(struct semaphore *s);
221int down_interruptible(struct semaphore *s);
222int down_trylock(struct semaphore *s);
223void up(struct semaphore *s);
224
225/*
226 * Logging and assertions API
227 */
228void rlprintf(int pps, const char *fmt, ...)
229	__printflike(2, 3);
230
231void
232device_rlprintf(int pps, device_t dev, const char *fmt, ...)
233	__printflike(3, 4);
234
235#define might_sleep()
236
237#define WARN(condition, msg)				\
238({							\
239	int __ret_warn_on = !!(condition);		\
240	if (unlikely(__ret_warn_on))			\
241		printf((msg));				\
242	unlikely(__ret_warn_on);			\
243})
244
245
246
247#define WARN_ON(condition)				\
248({							\
249	int __ret_warn_on = !!(condition);		\
250	if (unlikely(__ret_warn_on))			\
251		printf("WARN_ON: " #condition "\n");	\
252	unlikely(__ret_warn_on);			\
253})
254
255#define WARN_ON_ONCE(condition) ({			\
256	static int __warned;				\
257	int __ret_warn_once = !!(condition);		\
258							\
259	if (unlikely(__ret_warn_once))			\
260		if (WARN_ON(!__warned))			\
261			__warned = 1;			\
262	unlikely(__ret_warn_once);			\
263})
264
265#define BUG_ON(cond)					\
266	do {						\
267		if (cond)				\
268			panic("BUG_ON: " #cond);	\
269	} while (0)
270
271#define BUG()						\
272	do {						\
273		panic("BUG: %s:%d", __FILE__, __LINE__);	\
274	} while (0)
275
276#define vchiq_static_assert(cond) CTASSERT(cond)
277
278#define KERN_EMERG	"<0>"	/* system is unusable			*/
279#define KERN_ALERT	"<1>"	/* action must be taken immediately	*/
280#define KERN_CRIT	"<2>"	/* critical conditions			*/
281#define KERN_ERR	"<3>"	/* error conditions			*/
282#define KERN_WARNING	"<4>"	/* warning conditions			*/
283#define KERN_NOTICE	"<5>"	/* normal but significant condition	*/
284#define KERN_INFO	"<6>"	/* informational			*/
285#define KERN_DEBUG	"<7>"	/* debug-level messages			*/
286#define KERN_CONT	""
287
288#define printk(fmt, args...)		printf(fmt, ##args)
289#define vprintk(fmt, args)		vprintf(fmt, args)
290
291/*
292 * Malloc API
293 */
294#define GFP_KERNEL	0
295#define GFP_ATOMIC	0
296
297MALLOC_DECLARE(M_VCHI);
298
299#define kmalloc(size, flags)	malloc((size), M_VCHI, M_NOWAIT | M_ZERO)
300#define kcalloc(n, size, flags)	malloc((n) * (size), M_VCHI, M_NOWAIT | M_ZERO)
301#define kzalloc(a, b)		kcalloc(1, (a), (b))
302#define kfree(p)		do { if (p) free(p, M_VCHI); } while (0)
303
304/*
305 * Kernel module API
306 */
307#define __init
308#define __exit
309#define __devinit
310#define __devexit
311#define __devinitdata
312
313/*
314 * Time API
315 */
316#if 1
317/* emulate jiffies */
318static inline unsigned long
319_jiffies(void)
320{
321	struct timeval tv;
322
323	microuptime(&tv);
324	return tvtohz(&tv);
325}
326
327static inline unsigned long
328msecs_to_jiffies(unsigned long msecs)
329{
330	struct timeval tv;
331
332	tv.tv_sec = msecs / 1000000UL;
333	tv.tv_usec = msecs % 1000000UL;
334	return tvtohz(&tv);
335}
336
337#define jiffies			_jiffies()
338#else
339#define jiffies			ticks
340#endif
341#define HZ			hz
342
343#define udelay(usec)		DELAY(usec)
344#define mdelay(msec)		DELAY((msec) * 1000)
345
346#define schedule_timeout(jiff)	kpause("dhdslp", false, jiff, NULL)
347
348#if defined(msleep)
349#undef msleep
350#endif
351#define msleep(msec)		mdelay(msec)
352
353#define time_after(a, b)	((a) > (b))
354#define time_after_eq(a, b)	((a) >= (b))
355#define time_before(a, b)	time_after((b), (a))
356
357/*
358 * kthread API (we use lwp)
359 */
360typedef lwp_t * VCHIQ_THREAD_T;
361
362VCHIQ_THREAD_T vchiq_thread_create(int (*threadfn)(void *data),
363                                   void *data,
364                                   const char namefmt[], ...);
365void set_user_nice(VCHIQ_THREAD_T p, int nice);
366void wake_up_process(VCHIQ_THREAD_T p);
367
368/*
369 * Proc APIs
370 */
371void flush_signals(VCHIQ_THREAD_T);
372int fatal_signal_pending(VCHIQ_THREAD_T);
373
374/*
375 * Misc API
376 */
377
378#define __user
379
380#define	current			curlwp
381#define EXPORT_SYMBOL(x)
382#define PAGE_ALIGN(addr)	round_page(addr)
383
384typedef	void	irqreturn_t;
385typedef	off_t	loff_t;
386
387#define BCM2835_MBOX_CHAN_VCHIQ	3
388#define bcm_mbox_write	bcmmbox_write
389
390#define rmb	membar_consumer
391#define wmb	membar_producer
392#define dsb	membar_producer
393
394#define smp_mb	membar_producer
395#define smp_rmb	membar_consumer
396#define smp_wmb	membar_producer
397
398#define device_print_prettyname(dev)	device_printf((dev), "")
399
400#endif /* __VCHI_NETBSD_H__ */
401