1 1.1 jmcneill /*- 2 1.1 jmcneill * Copyright (c) 2010 Max Khon <fjoe (at) freebsd.org> 3 1.1 jmcneill * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo (at) bluezbox.com> 4 1.1 jmcneill * Copyright (c) 2013 Jared D. McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 jmcneill * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 jmcneill * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 jmcneill * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 jmcneill * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 jmcneill * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 jmcneill * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 jmcneill * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 jmcneill * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jmcneill * SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill #ifndef __VCHI_NETBSD_H__ 29 1.1 jmcneill #define __VCHI_NETBSD_H__ 30 1.1 jmcneill 31 1.1 jmcneill #include <sys/systm.h> 32 1.1 jmcneill #include <sys/param.h> 33 1.1 jmcneill #include <sys/bus.h> 34 1.1 jmcneill #include <sys/conf.h> 35 1.1 jmcneill #include <sys/lock.h> 36 1.1 jmcneill #include <sys/kernel.h> 37 1.1 jmcneill #include <sys/kthread.h> 38 1.1 jmcneill #include <sys/mutex.h> 39 1.1 jmcneill #include <sys/malloc.h> 40 1.1 jmcneill #include <sys/proc.h> 41 1.1 jmcneill #include <sys/types.h> 42 1.1 jmcneill #include <sys/ioccom.h> 43 1.1 jmcneill #include <sys/atomic.h> 44 1.1 jmcneill #include <sys/rwlock.h> 45 1.1 jmcneill #include <sys/callout.h> 46 1.1 jmcneill 47 1.7 skrll #include <linux/completion.h> 48 1.19 martin #include <linux/types.h> 49 1.12 maya #include <asm/barrier.h> 50 1.7 skrll 51 1.1 jmcneill /* 52 1.1 jmcneill * Copy from/to user API 53 1.1 jmcneill */ 54 1.1 jmcneill #define copy_from_user(to, from, n) copyin((from), (to), (n)) 55 1.1 jmcneill #define copy_to_user(to, from, n) copyout((from), (to), (n)) 56 1.1 jmcneill 57 1.1 jmcneill /* 58 1.1 jmcneill * Atomic API 59 1.1 jmcneill */ 60 1.1 jmcneill typedef volatile unsigned int atomic_t; 61 1.1 jmcneill 62 1.1 jmcneill #define atomic_set(p, v) (*(p) = (v)) 63 1.3 skrll #define atomic_read(p) (*(volatile int *)(p)) 64 1.1 jmcneill #define atomic_inc(p) atomic_inc_uint(p) 65 1.1 jmcneill #define atomic_dec(p) atomic_dec_uint(p) 66 1.1 jmcneill #define atomic_dec_and_test(p) (atomic_dec_uint_nv(p) == 0) 67 1.1 jmcneill #define atomic_inc_return(v) atomic_inc_uint_nv(v) 68 1.1 jmcneill #define atomic_dec_return(v) atomic_dec_uint_nv(v) 69 1.1 jmcneill #define atomic_add(v, p) atomic_add_int(p, v) 70 1.1 jmcneill #define atomic_sub(v, p) atomic_add_int(p, -(v)) 71 1.1 jmcneill #define atomic_add_return(v, p) atomic_add_int_nv(p, v) 72 1.1 jmcneill #define atomic_sub_return(v, p) atomic_add_int_nv(p, -(v)) 73 1.1 jmcneill #define atomic_xchg(p, v) atomic_swap_uint(p, v) 74 1.2 jmcneill #define atomic_cmpxchg(p, oldv, newv) atomic_cas_uint(p, oldv, newv) 75 1.1 jmcneill 76 1.1 jmcneill #define ATOMIC_INIT(v) (v) 77 1.1 jmcneill 78 1.1 jmcneill /* 79 1.1 jmcneill * Spinlock API 80 1.1 jmcneill */ 81 1.1 jmcneill typedef kmutex_t spinlock_t; 82 1.1 jmcneill 83 1.1 jmcneill /* 84 1.1 jmcneill * NB: Need to initialize these at attach time! 85 1.1 jmcneill */ 86 1.1 jmcneill #define DEFINE_SPINLOCK(name) kmutex_t name 87 1.1 jmcneill 88 1.14 skrll #define spin_lock_init(lock) mutex_init(lock, MUTEX_DEFAULT, IPL_VM) 89 1.1 jmcneill #define spin_lock_destroy(lock) mutex_destroy(lock) 90 1.1 jmcneill #define spin_lock(lock) mutex_spin_enter(lock) 91 1.1 jmcneill #define spin_unlock(lock) mutex_spin_exit(lock) 92 1.1 jmcneill 93 1.1 jmcneill /* 94 1.1 jmcneill * Mutex API 95 1.1 jmcneill */ 96 1.1 jmcneill struct mutex { 97 1.1 jmcneill kmutex_t mtx; 98 1.1 jmcneill }; 99 1.1 jmcneill 100 1.1 jmcneill #define lmutex_init(lock) mutex_init(&(lock)->mtx, MUTEX_DEFAULT, IPL_NONE) 101 1.1 jmcneill #define lmutex_destroy(lock) mutex_destroy(&(lock)->mtx) 102 1.1 jmcneill #define lmutex_lock(lock) mutex_enter(&(lock)->mtx) 103 1.1 jmcneill #define lmutex_lock_interruptible(lock) (mutex_enter(&(lock)->mtx),0) 104 1.1 jmcneill #define lmutex_unlock(lock) mutex_exit(&(lock)->mtx) 105 1.1 jmcneill 106 1.1 jmcneill /* 107 1.1 jmcneill * Rwlock API 108 1.1 jmcneill */ 109 1.3 skrll typedef kmutex_t rwlock_t; 110 1.3 skrll 111 1.3 skrll #define DEFINE_RWLOCK(name) kmutex_t name 112 1.1 jmcneill 113 1.14 skrll #define rwlock_init(rwlock) mutex_init(rwlock, MUTEX_DEFAULT, IPL_VM) 114 1.3 skrll #define read_lock(rwlock) mutex_spin_enter(rwlock) 115 1.3 skrll #define read_unlock(rwlock) mutex_spin_exit(rwlock) 116 1.3 skrll 117 1.3 skrll #define write_lock(rwlock) mutex_spin_enter(rwlock) 118 1.3 skrll #define write_unlock(rwlock) mutex_spin_exit(rwlock) 119 1.1 jmcneill 120 1.1 jmcneill #define read_lock_bh(rwlock) read_lock(rwlock) 121 1.1 jmcneill #define read_unlock_bh(rwlock) read_unlock(rwlock) 122 1.1 jmcneill #define write_lock_bh(rwlock) write_lock(rwlock) 123 1.1 jmcneill #define write_unlock_bh(rwlock) write_unlock(rwlock) 124 1.1 jmcneill 125 1.1 jmcneill /* 126 1.1 jmcneill * Timer API 127 1.1 jmcneill */ 128 1.1 jmcneill struct timer_list { 129 1.1 jmcneill kmutex_t mtx; 130 1.1 jmcneill callout_t callout; 131 1.1 jmcneill 132 1.1 jmcneill unsigned long expires; 133 1.1 jmcneill void (*function)(unsigned long); 134 1.1 jmcneill unsigned long data; 135 1.1 jmcneill }; 136 1.1 jmcneill 137 1.1 jmcneill void init_timer(struct timer_list *t); 138 1.1 jmcneill void setup_timer(struct timer_list *t, void (*function)(unsigned long), unsigned long data); 139 1.1 jmcneill void mod_timer(struct timer_list *t, unsigned long expires); 140 1.1 jmcneill void add_timer(struct timer_list *t); 141 1.1 jmcneill int del_timer(struct timer_list *t); 142 1.1 jmcneill int del_timer_sync(struct timer_list *t); 143 1.1 jmcneill 144 1.1 jmcneill /* 145 1.1 jmcneill * Semaphore API 146 1.1 jmcneill */ 147 1.1 jmcneill struct semaphore { 148 1.1 jmcneill kmutex_t mtx; 149 1.1 jmcneill kcondvar_t cv; 150 1.1 jmcneill int value; 151 1.1 jmcneill int waiters; 152 1.1 jmcneill }; 153 1.1 jmcneill 154 1.1 jmcneill /* 155 1.1 jmcneill * NB: Need to initialize these at attach time! 156 1.1 jmcneill */ 157 1.1 jmcneill #define DEFINE_SEMAPHORE(name) struct semaphore name 158 1.1 jmcneill 159 1.1 jmcneill void sema_sysinit(void *arg); 160 1.1 jmcneill void _sema_init(struct semaphore *s, int value); 161 1.1 jmcneill void _sema_destroy(struct semaphore *s); 162 1.1 jmcneill void down(struct semaphore *s); 163 1.1 jmcneill int down_interruptible(struct semaphore *s); 164 1.1 jmcneill int down_trylock(struct semaphore *s); 165 1.1 jmcneill void up(struct semaphore *s); 166 1.1 jmcneill 167 1.1 jmcneill /* 168 1.1 jmcneill * Logging and assertions API 169 1.1 jmcneill */ 170 1.1 jmcneill void rlprintf(int pps, const char *fmt, ...) 171 1.1 jmcneill __printflike(2, 3); 172 1.1 jmcneill 173 1.1 jmcneill void 174 1.1 jmcneill device_rlprintf(int pps, device_t dev, const char *fmt, ...) 175 1.1 jmcneill __printflike(3, 4); 176 1.1 jmcneill 177 1.1 jmcneill #define vchiq_static_assert(cond) CTASSERT(cond) 178 1.1 jmcneill 179 1.1 jmcneill /* 180 1.1 jmcneill * Kernel module API 181 1.1 jmcneill */ 182 1.1 jmcneill #define __init 183 1.1 jmcneill #define __exit 184 1.1 jmcneill #define __devinit 185 1.1 jmcneill #define __devexit 186 1.1 jmcneill #define __devinitdata 187 1.1 jmcneill 188 1.1 jmcneill /* 189 1.1 jmcneill * Time API 190 1.1 jmcneill */ 191 1.1 jmcneill #if 1 192 1.1 jmcneill /* emulate jiffies */ 193 1.5 skrll static inline unsigned long 194 1.5 skrll _jiffies(void) 195 1.1 jmcneill { 196 1.1 jmcneill struct timeval tv; 197 1.1 jmcneill 198 1.1 jmcneill microuptime(&tv); 199 1.1 jmcneill return tvtohz(&tv); 200 1.1 jmcneill } 201 1.1 jmcneill 202 1.5 skrll static inline unsigned long 203 1.5 skrll msecs_to_jiffies(unsigned long msecs) 204 1.1 jmcneill { 205 1.1 jmcneill struct timeval tv; 206 1.1 jmcneill 207 1.1 jmcneill tv.tv_sec = msecs / 1000000UL; 208 1.1 jmcneill tv.tv_usec = msecs % 1000000UL; 209 1.1 jmcneill return tvtohz(&tv); 210 1.1 jmcneill } 211 1.1 jmcneill 212 1.1 jmcneill #define jiffies _jiffies() 213 1.1 jmcneill #else 214 1.1 jmcneill #define jiffies ticks 215 1.1 jmcneill #endif 216 1.1 jmcneill #define HZ hz 217 1.1 jmcneill 218 1.1 jmcneill #define udelay(usec) DELAY(usec) 219 1.1 jmcneill #define mdelay(msec) DELAY((msec) * 1000) 220 1.1 jmcneill 221 1.1 jmcneill #define schedule_timeout(jiff) kpause("dhdslp", false, jiff, NULL) 222 1.1 jmcneill 223 1.1 jmcneill #if defined(msleep) 224 1.1 jmcneill #undef msleep 225 1.1 jmcneill #endif 226 1.1 jmcneill #define msleep(msec) mdelay(msec) 227 1.1 jmcneill 228 1.1 jmcneill #define time_after(a, b) ((a) > (b)) 229 1.1 jmcneill #define time_after_eq(a, b) ((a) >= (b)) 230 1.1 jmcneill #define time_before(a, b) time_after((b), (a)) 231 1.1 jmcneill 232 1.1 jmcneill /* 233 1.3 skrll * kthread API (we use lwp) 234 1.1 jmcneill */ 235 1.1 jmcneill typedef lwp_t * VCHIQ_THREAD_T; 236 1.1 jmcneill 237 1.1 jmcneill VCHIQ_THREAD_T vchiq_thread_create(int (*threadfn)(void *data), 238 1.1 jmcneill void *data, 239 1.1 jmcneill const char namefmt[], ...); 240 1.1 jmcneill void set_user_nice(VCHIQ_THREAD_T p, int nice); 241 1.1 jmcneill void wake_up_process(VCHIQ_THREAD_T p); 242 1.1 jmcneill 243 1.1 jmcneill /* 244 1.1 jmcneill * Proc APIs 245 1.1 jmcneill */ 246 1.1 jmcneill void flush_signals(VCHIQ_THREAD_T); 247 1.1 jmcneill int fatal_signal_pending(VCHIQ_THREAD_T); 248 1.1 jmcneill 249 1.1 jmcneill /* 250 1.1 jmcneill * Misc API 251 1.1 jmcneill */ 252 1.1 jmcneill 253 1.1 jmcneill #define __user 254 1.1 jmcneill 255 1.1 jmcneill #define current curlwp 256 1.1 jmcneill #define PAGE_ALIGN(addr) round_page(addr) 257 1.1 jmcneill 258 1.1 jmcneill typedef void irqreturn_t; 259 1.1 jmcneill 260 1.1 jmcneill #define BCM2835_MBOX_CHAN_VCHIQ 3 261 1.1 jmcneill #define bcm_mbox_write bcmmbox_write 262 1.1 jmcneill 263 1.1 jmcneill #define device_print_prettyname(dev) device_printf((dev), "") 264 1.1 jmcneill 265 1.1 jmcneill #endif /* __VCHI_NETBSD_H__ */ 266