vchi_bsd.h revision 1.19
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 <linux/types.h>
49#include <asm/barrier.h>
50
51/*
52 * Copy from/to user API
53 */
54#define copy_from_user(to, from, n)	copyin((from), (to), (n))
55#define copy_to_user(to, from, n)	copyout((from), (to), (n))
56
57/*
58 * Atomic API
59 */
60typedef volatile unsigned int atomic_t;
61
62#define atomic_set(p, v)	(*(p) = (v))
63#define atomic_read(p)		(*(volatile int *)(p))
64#define atomic_inc(p)		atomic_inc_uint(p)
65#define atomic_dec(p)		atomic_dec_uint(p)
66#define atomic_dec_and_test(p)	(atomic_dec_uint_nv(p) == 0)
67#define	atomic_inc_return(v)	atomic_inc_uint_nv(v)
68#define	atomic_dec_return(v)	atomic_dec_uint_nv(v)
69#define atomic_add(v, p)	atomic_add_int(p, v)
70#define atomic_sub(v, p)	atomic_add_int(p, -(v))
71#define atomic_add_return(v, p)	atomic_add_int_nv(p, v)
72#define atomic_sub_return(v, p)	atomic_add_int_nv(p, -(v))
73#define atomic_xchg(p, v)	atomic_swap_uint(p, v)
74#define atomic_cmpxchg(p, oldv, newv) atomic_cas_uint(p, oldv, newv)
75
76#define ATOMIC_INIT(v)		(v)
77
78/*
79 * Spinlock API
80 */
81typedef kmutex_t spinlock_t;
82
83/*
84 * NB: Need to initialize these at attach time!
85 */
86#define DEFINE_SPINLOCK(name)	kmutex_t name
87
88#define spin_lock_init(lock)	mutex_init(lock, MUTEX_DEFAULT, IPL_VM)
89#define spin_lock_destroy(lock)	mutex_destroy(lock)
90#define spin_lock(lock)		mutex_spin_enter(lock)
91#define spin_unlock(lock)	mutex_spin_exit(lock)
92
93/*
94 * Mutex API
95 */
96struct mutex {
97	kmutex_t	mtx;
98};
99
100#define	lmutex_init(lock)	mutex_init(&(lock)->mtx, MUTEX_DEFAULT, IPL_NONE)
101#define lmutex_destroy(lock)	mutex_destroy(&(lock)->mtx)
102#define	lmutex_lock(lock)	mutex_enter(&(lock)->mtx)
103#define	lmutex_lock_interruptible(lock)	(mutex_enter(&(lock)->mtx),0)
104#define	lmutex_unlock(lock)	mutex_exit(&(lock)->mtx)
105
106/*
107 * Rwlock API
108 */
109typedef kmutex_t rwlock_t;
110
111#define DEFINE_RWLOCK(name)	kmutex_t name
112
113#define rwlock_init(rwlock)	mutex_init(rwlock, MUTEX_DEFAULT, IPL_VM)
114#define read_lock(rwlock)	mutex_spin_enter(rwlock)
115#define read_unlock(rwlock)	mutex_spin_exit(rwlock)
116
117#define write_lock(rwlock)	mutex_spin_enter(rwlock)
118#define write_unlock(rwlock)	mutex_spin_exit(rwlock)
119
120#define read_lock_bh(rwlock)	read_lock(rwlock)
121#define read_unlock_bh(rwlock)	read_unlock(rwlock)
122#define write_lock_bh(rwlock)	write_lock(rwlock)
123#define write_unlock_bh(rwlock)	write_unlock(rwlock)
124
125/*
126 * Timer API
127 */
128struct timer_list {
129	kmutex_t mtx;
130	callout_t callout;
131
132	unsigned long expires;
133	void (*function)(unsigned long);
134	unsigned long data;
135};
136
137void init_timer(struct timer_list *t);
138void setup_timer(struct timer_list *t, void (*function)(unsigned long), unsigned long data);
139void mod_timer(struct timer_list *t, unsigned long expires);
140void add_timer(struct timer_list *t);
141int del_timer(struct timer_list *t);
142int del_timer_sync(struct timer_list *t);
143
144/*
145 * Semaphore API
146 */
147struct semaphore {
148	kmutex_t	mtx;
149	kcondvar_t	cv;
150	int		value;
151	int		waiters;
152};
153
154/*
155 * NB: Need to initialize these at attach time!
156 */
157#define	DEFINE_SEMAPHORE(name)	struct semaphore name
158
159void sema_sysinit(void *arg);
160void _sema_init(struct semaphore *s, int value);
161void _sema_destroy(struct semaphore *s);
162void down(struct semaphore *s);
163int down_interruptible(struct semaphore *s);
164int down_trylock(struct semaphore *s);
165void up(struct semaphore *s);
166
167/*
168 * Logging and assertions API
169 */
170void rlprintf(int pps, const char *fmt, ...)
171	__printflike(2, 3);
172
173void
174device_rlprintf(int pps, device_t dev, const char *fmt, ...)
175	__printflike(3, 4);
176
177#define vchiq_static_assert(cond) CTASSERT(cond)
178
179/*
180 * Kernel module API
181 */
182#define __init
183#define __exit
184#define __devinit
185#define __devexit
186#define __devinitdata
187
188/*
189 * Time API
190 */
191#if 1
192/* emulate jiffies */
193static inline unsigned long
194_jiffies(void)
195{
196	struct timeval tv;
197
198	microuptime(&tv);
199	return tvtohz(&tv);
200}
201
202static inline unsigned long
203msecs_to_jiffies(unsigned long msecs)
204{
205	struct timeval tv;
206
207	tv.tv_sec = msecs / 1000000UL;
208	tv.tv_usec = msecs % 1000000UL;
209	return tvtohz(&tv);
210}
211
212#define jiffies			_jiffies()
213#else
214#define jiffies			ticks
215#endif
216#define HZ			hz
217
218#define udelay(usec)		DELAY(usec)
219#define mdelay(msec)		DELAY((msec) * 1000)
220
221#define schedule_timeout(jiff)	kpause("dhdslp", false, jiff, NULL)
222
223#if defined(msleep)
224#undef msleep
225#endif
226#define msleep(msec)		mdelay(msec)
227
228#define time_after(a, b)	((a) > (b))
229#define time_after_eq(a, b)	((a) >= (b))
230#define time_before(a, b)	time_after((b), (a))
231
232/*
233 * kthread API (we use lwp)
234 */
235typedef lwp_t * VCHIQ_THREAD_T;
236
237VCHIQ_THREAD_T vchiq_thread_create(int (*threadfn)(void *data),
238                                   void *data,
239                                   const char namefmt[], ...);
240void set_user_nice(VCHIQ_THREAD_T p, int nice);
241void wake_up_process(VCHIQ_THREAD_T p);
242
243/*
244 * Proc APIs
245 */
246void flush_signals(VCHIQ_THREAD_T);
247int fatal_signal_pending(VCHIQ_THREAD_T);
248
249/*
250 * Misc API
251 */
252
253#define __user
254
255#define	current			curlwp
256#define PAGE_ALIGN(addr)	round_page(addr)
257
258typedef	void	irqreturn_t;
259
260#define BCM2835_MBOX_CHAN_VCHIQ	3
261#define bcm_mbox_write	bcmmbox_write
262
263#define device_print_prettyname(dev)	device_printf((dev), "")
264
265#endif /* __VCHI_NETBSD_H__ */
266