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) (*(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 */ 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 129 /* 130 * Mutex API 131 */ 132 struct 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 */ 145 typedef 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 */ 164 struct 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 173 void init_timer(struct timer_list *t); 174 void setup_timer(struct timer_list *t, void (*function)(unsigned long), unsigned long data); 175 void mod_timer(struct timer_list *t, unsigned long expires); 176 void add_timer(struct timer_list *t); 177 int del_timer(struct timer_list *t); 178 int del_timer_sync(struct timer_list *t); 179 180 /* 181 * Completion API 182 */ 183 struct completion { 184 kcondvar_t cv; 185 kmutex_t lock; 186 int done; 187 }; 188 189 void init_completion(struct completion *c); 190 void destroy_completion(struct completion *c); 191 int try_wait_for_completion(struct completion *); 192 int wait_for_completion_interruptible(struct completion *); 193 int wait_for_completion_interruptible_timeout(struct completion *, unsigned long ticks); 194 int wait_for_completion_killable(struct completion *); 195 void wait_for_completion(struct completion *c); 196 int wait_for_completion_timeout(struct completion *c, unsigned long timeout); 197 void complete(struct completion *c); 198 void complete_all(struct completion *c); 199 200 #define INIT_COMPLETION(x) do {(x).done = 0;} while(0) 201 202 /* 203 * Semaphore API 204 */ 205 struct 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 217 void sema_sysinit(void *arg); 218 void _sema_init(struct semaphore *s, int value); 219 void _sema_destroy(struct semaphore *s); 220 void down(struct semaphore *s); 221 int down_interruptible(struct semaphore *s); 222 int down_trylock(struct semaphore *s); 223 void up(struct semaphore *s); 224 225 /* 226 * Logging and assertions API 227 */ 228 void rlprintf(int pps, const char *fmt, ...) 229 __printflike(2, 3); 230 231 void 232 device_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 297 MALLOC_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 */ 318 static inline unsigned long _jiffies(void) 319 { 320 struct timeval tv; 321 322 microuptime(&tv); 323 return tvtohz(&tv); 324 } 325 326 static inline unsigned long msecs_to_jiffies(unsigned long msecs) 327 { 328 struct timeval tv; 329 330 tv.tv_sec = msecs / 1000000UL; 331 tv.tv_usec = msecs % 1000000UL; 332 return tvtohz(&tv); 333 } 334 335 #define jiffies _jiffies() 336 #else 337 #define jiffies ticks 338 #endif 339 #define HZ hz 340 341 #define udelay(usec) DELAY(usec) 342 #define mdelay(msec) DELAY((msec) * 1000) 343 344 #define schedule_timeout(jiff) kpause("dhdslp", false, jiff, NULL) 345 346 #if defined(msleep) 347 #undef msleep 348 #endif 349 #define msleep(msec) mdelay(msec) 350 351 #define time_after(a, b) ((a) > (b)) 352 #define time_after_eq(a, b) ((a) >= (b)) 353 #define time_before(a, b) time_after((b), (a)) 354 355 /* 356 * kthread API (we use lwp) 357 */ 358 typedef lwp_t * VCHIQ_THREAD_T; 359 360 VCHIQ_THREAD_T vchiq_thread_create(int (*threadfn)(void *data), 361 void *data, 362 const char namefmt[], ...); 363 void set_user_nice(VCHIQ_THREAD_T p, int nice); 364 void wake_up_process(VCHIQ_THREAD_T p); 365 366 /* 367 * Proc APIs 368 */ 369 void flush_signals(VCHIQ_THREAD_T); 370 int fatal_signal_pending(VCHIQ_THREAD_T); 371 372 /* 373 * Misc API 374 */ 375 376 #define __user 377 378 #define current curlwp 379 #define EXPORT_SYMBOL(x) 380 #define PAGE_ALIGN(addr) round_page(addr) 381 382 typedef void irqreturn_t; 383 typedef off_t loff_t; 384 385 #define BCM2835_MBOX_CHAN_VCHIQ 3 386 #define bcm_mbox_write bcmmbox_write 387 388 #define rmb membar_consumer 389 #define wmb membar_producer 390 #define dsb membar_producer 391 392 #define device_print_prettyname(dev) device_printf((dev), "") 393 394 #endif /* __VCHI_NETBSD_H__ */ 395