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