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_BSD_H__ 29 #define __VCHI_BSD_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/sx.h> 40 #include <sys/sema.h> 41 #include <sys/malloc.h> 42 #include <sys/proc.h> 43 #include <sys/types.h> 44 #include <sys/ioccom.h> 45 46 /* 47 * Copy from/to user API 48 */ 49 #define copy_from_user(to, from, n) copyin((from), (to), (n)) 50 #define copy_to_user(to, from, n) copyout((from), (to), (n)) 51 52 /* 53 * Bit API 54 */ 55 56 static __inline int 57 test_and_set_bit(int nr, volatile void *addr) 58 { 59 int val; 60 61 do { 62 val = *(volatile int *) addr; 63 } while (atomic_cmpset_int(addr, val, val | (1 << nr)) == 0); 64 return (val & (1 << nr)); 65 } 66 67 static __inline__ 68 int test_and_clear_bit(int nr, volatile void *addr) 69 { 70 int val; 71 72 do { 73 val = *(volatile int *) addr; 74 } while (atomic_cmpset_int(addr, val, val & ~(1 << nr)) == 0); 75 return (val & (1 << nr)); 76 } 77 78 /* 79 * Atomic API 80 */ 81 typedef volatile unsigned atomic_t; 82 83 #define atomic_set(p, v) (*(p) = (v)) 84 #define atomic_read(p) (*(p)) 85 #define atomic_inc(p) atomic_add_int(p, 1) 86 #define atomic_dec(p) atomic_subtract_int(p, 1) 87 #define atomic_dec_and_test(p) (atomic_fetchadd_int(p, -1) == 1) 88 #define atomic_inc_return(v) atomic_add_return(1, (v)) 89 #define atomic_dec_return(v) atomic_sub_return(1, (v)) 90 #define atomic_add(v, p) atomic_add_int(p, v) 91 #define atomic_sub(v, p) atomic_subtract_int(p, v) 92 93 #define ATOMIC_INIT(v) (v) 94 95 static inline int 96 atomic_add_return(int i, atomic_t *v) 97 { 98 return i + atomic_fetchadd_int(v, i); 99 } 100 101 static inline int 102 atomic_sub_return(int i, atomic_t *v) 103 { 104 return atomic_fetchadd_int(v, -i) - i; 105 } 106 107 static inline int 108 atomic_cmpxchg(atomic_t *v, int oldv, int newv) 109 { 110 if (atomic_cmpset_rel_int(v, oldv, newv)) 111 return newv; 112 else 113 return *v; 114 } 115 116 static inline int 117 atomic_xchg(atomic_t *v, int newv) 118 { 119 int oldv; 120 if (newv == 0) 121 return atomic_readandclear_int(v); 122 else { 123 do { 124 oldv = atomic_load_acq_int(v); 125 } while (!atomic_cmpset_rel_int(v, oldv, newv)); 126 } 127 128 return (oldv); 129 } 130 131 /* 132 * Spinlock API 133 */ 134 typedef struct mtx spinlock_t; 135 136 #define DEFINE_SPINLOCK(name) \ 137 struct mtx name 138 #define spin_lock_init(lock) mtx_init(lock, "VCHI spinlock " # lock, NULL, MTX_DEF) 139 #define spin_lock_destroy(lock) mtx_destroy(lock) 140 #define spin_lock(lock) mtx_lock(lock) 141 #define spin_unlock(lock) mtx_unlock(lock) 142 #define spin_lock_bh(lock) spin_lock(lock) 143 #define spin_unlock_bh(lock) spin_unlock(lock) 144 145 /* 146 * Mutex API 147 */ 148 struct mutex { 149 struct mtx mtx; 150 }; 151 152 #define lmutex_init(lock) mtx_init(&(lock)->mtx, #lock, NULL, MTX_DEF) 153 #define lmutex_lock(lock) mtx_lock(&(lock)->mtx) 154 #define lmutex_lock_interruptible(lock) (mtx_lock(&(lock)->mtx),0) 155 #define lmutex_unlock(lock) mtx_unlock(&(lock)->mtx) 156 #define lmutex_destroy(lock) mtx_destroy(&(lock)->mtx) 157 158 /* 159 * Rwlock API 160 */ 161 typedef struct sx rwlock_t; 162 163 #if defined(SX_ADAPTIVESPIN) && !defined(SX_NOADAPTIVE) 164 #define SX_NOADAPTIVE SX_ADAPTIVESPIN 165 #endif 166 167 #define DEFINE_RWLOCK(name) \ 168 struct sx name; \ 169 SX_SYSINIT(name, &name, #name) 170 #define rwlock_init(rwlock) sx_init_flags(rwlock, "VCHI rwlock", SX_NOADAPTIVE) 171 #define read_lock(rwlock) sx_slock(rwlock) 172 #define read_unlock(rwlock) sx_sunlock(rwlock) 173 174 #define write_lock(rwlock) sx_xlock(rwlock) 175 #define write_unlock(rwlock) sx_xunlock(rwlock) 176 #define write_lock_irqsave(rwlock, flags) \ 177 do { \ 178 sx_xlock(rwlock); \ 179 (void) &(flags); \ 180 } while (0) 181 #define write_unlock_irqrestore(rwlock, flags) \ 182 sx_xunlock(rwlock) 183 184 #define read_lock_bh(rwlock) sx_slock(rwlock) 185 #define read_unlock_bh(rwlock) sx_sunlock(rwlock) 186 #define write_lock_bh(rwlock) sx_xlock(rwlock) 187 #define write_unlock_bh(rwlock) sx_xunlock(rwlock) 188 189 /* 190 * Timer API 191 */ 192 struct timer_list { 193 struct mtx mtx; 194 struct callout callout; 195 196 unsigned long expires; 197 void (*function)(unsigned long); 198 unsigned long data; 199 }; 200 201 void init_timer(struct timer_list *t); 202 void setup_timer(struct timer_list *t, void (*function)(unsigned long), unsigned long data); 203 void mod_timer(struct timer_list *t, unsigned long expires); 204 void add_timer(struct timer_list *t); 205 int del_timer(struct timer_list *t); 206 int del_timer_sync(struct timer_list *t); 207 208 /* 209 * Completion API 210 */ 211 struct completion { 212 struct cv cv; 213 struct mtx lock; 214 int done; 215 }; 216 217 void init_completion(struct completion *c); 218 void destroy_completion(struct completion *c); 219 int try_wait_for_completion(struct completion *); 220 int wait_for_completion_interruptible(struct completion *); 221 int wait_for_completion_interruptible_timeout(struct completion *, unsigned long ticks); 222 int wait_for_completion_killable(struct completion *); 223 void wait_for_completion(struct completion *c); 224 void complete(struct completion *c); 225 void complete_all(struct completion *c); 226 void INIT_COMPLETION_locked(struct completion *c); 227 228 #define INIT_COMPLETION(x) INIT_COMPLETION_locked(&(x)) 229 230 /* 231 * Semaphore API 232 */ 233 struct semaphore { 234 struct mtx mtx; 235 struct cv cv; 236 int value; 237 int waiters; 238 }; 239 240 #define DEFINE_SEMAPHORE(name) \ 241 struct semaphore name; \ 242 SYSINIT(name##_sema_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 243 sema_sysinit, &name); \ 244 SYSUNINIT(name##_sema_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 245 _sema_destroy, __DEVOLATILE(void *, &(name))) 246 247 void sema_sysinit(void *arg); 248 void _sema_init(struct semaphore *s, int value); 249 void _sema_destroy(struct semaphore *s); 250 void down(struct semaphore *s); 251 int down_interruptible(struct semaphore *s); 252 int down_trylock(struct semaphore *s); 253 void up(struct semaphore *s); 254 255 /* 256 * Logging and assertions API 257 */ 258 void rlprintf(int pps, const char *fmt, ...) 259 __printflike(2, 3); 260 261 void 262 device_rlprintf(int pps, device_t dev, const char *fmt, ...) 263 __printflike(3, 4); 264 265 #define might_sleep() 266 267 #define WARN(condition, msg) \ 268 ({ \ 269 int __ret_warn_on = !!(condition); \ 270 if (unlikely(__ret_warn_on)) \ 271 printf((msg)); \ 272 unlikely(__ret_warn_on); \ 273 }) 274 275 276 277 #define WARN_ON(condition) \ 278 ({ \ 279 int __ret_warn_on = !!(condition); \ 280 if (unlikely(__ret_warn_on)) \ 281 printf("WARN_ON: " #condition "\n"); \ 282 unlikely(__ret_warn_on); \ 283 }) 284 285 #define WARN_ON_ONCE(condition) ({ \ 286 static int __warned; \ 287 int __ret_warn_once = !!(condition); \ 288 \ 289 if (unlikely(__ret_warn_once)) \ 290 if (WARN_ON(!__warned)) \ 291 __warned = 1; \ 292 unlikely(__ret_warn_once); \ 293 }) 294 295 #define BUG_ON(cond) \ 296 do { \ 297 if (cond) \ 298 panic("BUG_ON: " #cond); \ 299 } while (0) 300 301 #define BUG() \ 302 do { \ 303 panic("BUG: %s:%d", __FILE__, __LINE__); \ 304 } while (0) 305 306 #define vchiq_static_assert(cond) CTASSERT(cond) 307 308 #define KERN_EMERG "<0>" /* system is unusable */ 309 #define KERN_ALERT "<1>" /* action must be taken immediately */ 310 #define KERN_CRIT "<2>" /* critical conditions */ 311 #define KERN_ERR "<3>" /* error conditions */ 312 #define KERN_WARNING "<4>" /* warning conditions */ 313 #define KERN_NOTICE "<5>" /* normal but significant condition */ 314 #define KERN_INFO "<6>" /* informational */ 315 #define KERN_DEBUG "<7>" /* debug-level messages */ 316 #define KERN_CONT "" 317 318 #define printk(fmt, args...) printf(fmt, ##args) 319 #define vprintk(fmt, args) vprintf(fmt, args) 320 321 /* 322 * Malloc API 323 */ 324 #define GFP_KERNEL 0 325 #define GFP_ATOMIC 0 326 327 MALLOC_DECLARE(M_VCHI); 328 329 #define kmalloc(size, flags) malloc((size), M_VCHI, M_NOWAIT | M_ZERO) 330 #define kcalloc(n, size, flags) malloc((n) * (size), M_VCHI, M_NOWAIT | M_ZERO) 331 #define kzalloc(a, b) kcalloc(1, (a), (b)) 332 #define kfree(p) free(p, M_VCHI) 333 334 /* 335 * Kernel module API 336 */ 337 #define __init 338 #define __exit 339 #define __devinit 340 #define __devexit 341 #define __devinitdata 342 343 /* 344 * Time API 345 */ 346 #if 1 347 /* emulate jiffies */ 348 static inline unsigned long 349 _jiffies(void) 350 { 351 struct timeval tv; 352 353 microuptime(&tv); 354 return tvtohz(&tv); 355 } 356 357 static inline unsigned long 358 msecs_to_jiffies(unsigned long msecs) 359 { 360 struct timeval tv; 361 362 tv.tv_sec = msecs / 1000000UL; 363 tv.tv_usec = msecs % 1000000UL; 364 return tvtohz(&tv); 365 } 366 367 #define jiffies _jiffies() 368 #else 369 #define jiffies ticks 370 #endif 371 #define HZ hz 372 373 #define udelay(usec) DELAY(usec) 374 #define mdelay(msec) DELAY((msec) * 1000) 375 376 #define schedule_timeout(jiff) pause("dhdslp", jiff) 377 378 #if defined(msleep) 379 #undef msleep 380 #endif 381 #define msleep(msec) mdelay(msec) 382 383 #define time_after(a, b) ((a) > (b)) 384 #define time_after_eq(a, b) ((a) >= (b)) 385 #define time_before(a, b) time_after((b), (a)) 386 387 /* 388 * kthread API (we use proc) 389 */ 390 typedef struct proc * VCHIQ_THREAD_T; 391 392 VCHIQ_THREAD_T vchiq_thread_create(int (*threadfn)(void *data), 393 void *data, 394 const char namefmt[], ...); 395 void set_user_nice(VCHIQ_THREAD_T p, int nice); 396 void wake_up_process(VCHIQ_THREAD_T p); 397 398 /* 399 * Proc APIs 400 */ 401 void flush_signals(VCHIQ_THREAD_T); 402 int fatal_signal_pending(VCHIQ_THREAD_T); 403 404 /* 405 * mbox API 406 */ 407 void bcm_mbox_write(int channel, uint32_t data); 408 409 /* 410 * Misc API 411 */ 412 413 #define ENODATA EINVAL 414 415 #define __user 416 417 #define likely(x) __builtin_expect(!!(x), 1) 418 #define unlikely(x) __builtin_expect(!!(x), 0) 419 #define current curproc 420 #define EXPORT_SYMBOL(x) 421 #define PAGE_ALIGN(addr) round_page(addr) 422 423 typedef void irqreturn_t; 424 typedef off_t loff_t; 425 426 #define BCM2835_MBOX_CHAN_VCHIQ 3 427 428 #define smp_mb wmb 429 #define smp_rmb rmb 430 #define smp_wmb wmb 431 432 #define device_print_prettyname(dev) device_printf((dev), "") 433 434 #endif /* __VCHI_BSD_H__ */ 435