Rework drm_atomic.h for correctness and readability:

- Use macros where appropriate.
 - Remove code for other operating systems -- we don't need it.
 - In atomic_read/atomic_set, cast to volatile.
 - Misc. cleanup.
This commit is contained in:
bjs 2008-05-28 04:51:20 +00:00
parent fa7a018146
commit 91174b68e0

View File

@ -1,86 +1,70 @@
/* $NetBSD: drm_atomic.h,v 1.5 2008/05/18 03:49:08 bjs Exp $ */
/**
* \file drm_atomic.h
* Atomic operations used in the DRM which may or may not be provided by the OS.
*
* \author Eric Anholt <anholt@FreeBSD.org>
*/
/* $NetBSD: drm_atomic.h,v 1.6 2008/05/28 04:51:20 bjs Exp $ */
/*-
* Copyright 2004 Eric Anholt
* All Rights Reserved.
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran, Blair Sadewitz, et. al.
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* XXX This code is essentially a frob of the atomic operations provided by
* the Linux kernel at the time the DRM was ported.
*/
#include <sys/cdefs.h>
/*
__FBSDID("$FreeBSD: src/sys/dev/drm/drm_atomic.h,v 1.2 2005/11/28 23:13:52 anholt Exp $");
*/
__KERNEL_RCSID(0, "$NetBSD: drm_atomic.h,v 1.6 2008/05/28 04:51:20 bjs Exp $");
/* Many of these implementations are rather fake, but good enough. */
typedef u_int32_t atomic_t;
#ifdef __FreeBSD__
#define atomic_set(p, v) (*(p) = (v))
#define atomic_read(p) (*(p))
#define atomic_inc(p) atomic_add_int(p, 1)
#define atomic_dec(p) atomic_subtract_int(p, 1)
#define atomic_add(n, p) atomic_add_int(p, n)
#define atomic_sub(n, p) atomic_subtract_int(p, n)
#elif defined(__NetBSD__)
#include <sys/atomic.h>
#define atomic_set(p, v) (*(p) = (volatile uint32_t)(v))
#define atomic_read(p) (*(volatile uint32_t *)p)
#define atomic_inc(p) atomic_inc_32(p)
#define atomic_dec(p) atomic_dec_32(p)
#define atomic_add(n, p) atomic_add_32(p, n)
#define atomic_sub(n, p) atomic_add_32(p, -n)
#define atomic_add_int(p, v) atomic_inc_32(p, v)
#define atomic_set_int(p, bits) atomic_or_32(p, bits)
#define atomic_clear_int(p, bits) atomic_and_32(p, ~(bits))
#else /* __NetBSD__ */
/* FIXME */
#define atomic_set(p, v) (*(p) = (v))
#define atomic_read(p) (*(p))
#define atomic_inc(p) (*(p) += 1)
#define atomic_dec(p) (*(p) -= 1)
#define atomic_add(n, p) (*(p) += (n))
#define atomic_sub(n, p) (*(p) -= (n))
/* FIXME */
#define atomic_add_int(p, v) *(p) += v
#define atomic_subtract_int(p, v) *(p) -= v
#define atomic_set_int(p, bits) *(p) |= (bits)
#define atomic_clear_int(p, bits) *(p) &= ~(bits)
#endif /* !__FreeBSD__ */
#ifdef __NetBSD__
static inline int
atomic_cmpset_int(volatile int *dst, int old, int new)
{
return atomic_cas_uint(dst, old, new) == old;
}
typedef uint32_t atomic_t;
static inline atomic_t
#define atomic_set(p, v) (*((volatile uint32_t *)p) = (v))
#define atomic_read(p) (*((volatile uint32_t *)p))
#define atomic_inc(p) atomic_inc_uint(p)
#define atomic_dec(p) atomic_dec_uint(p)
#define atomic_add(v, p) atomic_add_int(p, v)
#define atomic_sub(v, p) atomic_add_int(p, -(v))
#define atomic_set_int(p, bits) atomic_or_uint(p, bits)
#define atomic_clear_int(p, bits) atomic_and_uint(p, ~(bits))
#define atomic_cmpset_int(p, o, n) \
((old == atomic_cas_uint(p, o, n)) ? 1 : 0)
#define set_bit(b, p) \
atomic_set_int(((volatile uint32_t *)(void *)p) + (b >> 5), \
(1 << (b & 0x1f)))
#define clear_bit(b, p) \
atomic_clear_int(((volatile uint32_t *)(void *)p) + (b >> 5), \
(1 << (b & 0x1f)))
#define test_bit(b, p) \
(((volatile uint32_t *)(void *)p)[bit >> 5] & (1 << (bit & 0x1f)))
static inline uint32_t
test_and_set_bit(int b, volatile void *p)
{
volatile uint32_t *val;
@ -93,81 +77,12 @@ test_and_set_bit(int b, volatile void *p)
old = *val;
if ((old & mask) != 0)
break;
} while (atomic_cas_32(val, old, old | mask) != old);
} while (atomic_cas_uint(val, old, old | mask) != old);
return old & mask;
}
#else
#if !defined(__FreeBSD_version) || (__FreeBSD_version < 500000)
#if defined(__i386__)
/* The extra atomic functions from 5.0 haven't been merged to 4.x */
static __inline int
atomic_cmpset_int(volatile u_int *dst, u_int exp, u_int src)
{
int res = exp;
__asm __volatile (
" lock ; "
" cmpxchgl %1,%2 ; "
" setz %%al ; "
" movzbl %%al,%0 ; "
"1: "
"# atomic_cmpset_int"
: "+a" (res) /* 0 (result) */
: "r" (src), /* 1 */
"m" (*(dst)) /* 2 */
: "memory");
return (res);
}
#else /* __i386__ */
static __inline int
atomic_cmpset_int(__volatile__ int *dst, int old, int new)
{
int s = splhigh();
if (*dst==old) {
*dst = new;
splx(s);
return 1;
}
splx(s);
return 0;
}
#endif /* !__i386__ */
static __inline atomic_t
test_and_set_bit(int b, volatile void *p)
{
int s = splhigh();
unsigned int m = 1<<b;
unsigned int r = *(volatile int *)p & m;
*(volatile int *)p |= m;
splx(s);
return r;
}
#endif /* !__FreeBSD_version || __FreeBSD_version < 500000 */
#endif
static __inline void
clear_bit(int b, volatile void *p)
{
atomic_clear_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
}
static __inline void
set_bit(int b, volatile void *p)
{
atomic_set_int(((volatile int *)p) + (b >> 5), 1 << (b & 0x1f));
}
static __inline int
test_bit(int b, volatile void *p)
{
return ((volatile int *)p)[b >> 5] & (1 << (b & 0x1f));
}
static __inline int
static inline int
find_first_zero_bit(volatile void *p, int maxbit)
{
int b;