1 /* BEGIN CSTYLED */ 2 3 /* 4 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. 5 */ 6 7 /* 8 * \file drm_atomic.h 9 * Atomic operations used in the DRM which may or may not be provided by the OS. 10 * 11 * \author Eric Anholt <anholt@FreeBSD.org> 12 */ 13 14 /* 15 * Copyright 2004 Eric Anholt 16 * Copyright (c) 2009, 2012, Intel Corporation. 17 * All Rights Reserved. 18 * 19 * Permission is hereby granted, free of charge, to any person obtaining a 20 * copy of this software and associated documentation files (the "Software"), 21 * to deal in the Software without restriction, including without limitation 22 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 23 * and/or sell copies of the Software, and to permit persons to whom the 24 * Software is furnished to do so, subject to the following conditions: 25 * 26 * The above copyright notice and this permission notice (including the next 27 * paragraph) shall be included in all copies or substantial portions of the 28 * Software. 29 * 30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 33 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 34 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 35 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 36 * OTHER DEALINGS IN THE SOFTWARE. 37 */ 38 39 /* Many of these implementations are rather fake, but good enough. */ 40 41 42 43 #ifndef _SYS_DRM_ATOMIC_H_ 44 #define _SYS_DRM_ATOMIC_H_ 45 46 #ifdef __cplusplus 47 extern "C" { 48 #endif 49 50 #include <sys/atomic.h> 51 52 #ifdef __LINT__ 53 #undef inline 54 #define inline 55 #endif 56 typedef uint32_t atomic_t; 57 58 #define atomic_set(p, v) (*(p) = (v)) 59 #define atomic_read(p) (*(p)) 60 #define atomic_inc(p) atomic_add_int(p, 1) 61 #define atomic_dec(p) atomic_dec_uint(p) 62 #define atomic_dec_and_test(p) \ 63 ((0 == atomic_dec_32_nv(p)) ? 1 : 0) 64 #define atomic_add(n, p) atomic_add_int(p, n) 65 #define atomic_add_return(n, p) (atomic_add_int(p, n), *p) 66 #define atomic_sub(n, p) atomic_add_int(p, -n) 67 #define atomic_set_int(p, bits) atomic_or_uint(p, bits) 68 #define atomic_clear_int(p, bits) atomic_and_uint(p, ~(bits)) 69 #define atomic_cmpset_int(p, c, n) \ 70 ((c == atomic_cas_uint(p, c, n)) ? 1 : 0) 71 #define atomic_clear_mask(mask, p) (*(p) &= ~mask) 72 #define atomic_set_mask(mask, p) (*(p) |= mask) 73 #define atomic_inc_not_zero(p) \ 74 if (atomic_read(p) != 0) \ 75 atomic_inc(p); 76 77 #define set_bit(b, p) \ 78 atomic_set_int(((volatile uint_t *)(void *)p) + (b >> 5), \ 79 1 << (b & 0x1f)) 80 81 #define clear_bit(b, p) \ 82 atomic_clear_int(((volatile uint_t *)(void *)p) + (b >> 5), \ 83 1 << (b & 0x1f)) 84 85 #define test_bit(b, p) \ 86 (((volatile uint_t *)(void *)p)[b >> 5] & (1 << (b & 0x1f))) 87 88 /* 89 * Note: this routine doesn't return old value. It return 90 * 0 when succeeds, or -1 when fails. 91 */ 92 #ifdef _LP64 93 #define test_and_set_bit(b, p) \ 94 atomic_set_long_excl(((ulong_t *)(void *)p) + (b >> 6), (b & 0x3f)) 95 #else 96 #define test_and_set_bit(b, p) \ 97 atomic_set_long_excl(((ulong_t *)(void *)p) + (b >> 5), (b & 0x1f)) 98 #endif 99 100 #ifdef __cplusplus 101 } 102 #endif 103 104 #endif /* _SYS_DRM_ATOMIC_H_ */ 105