#ifndef _ASM_BSP_ATOMIC_H_
#define _ASM_BSP_ATOMIC_H_

/* Copied from include/asm-mips/atomic.h */

#ifdef __SMP__
typedef struct { volatile int counter; } atomic_t;
#else
typedef struct { int counter; } atomic_t;
#endif

#ifdef __KERNEL__
#define ATOMIC_INIT(i)	{ (i) }

#define atomic_read(v)		((v)->counter)
#define atomic_set(v,i)		(((v)->counter) = (i))

#include <asm/system.h>

/*
 * This implementation is only atomic with respect to
 * interrupts.  BSP doesn't have HW support for atomic operations.
 */
extern __inline__ void atomic_add(int i, volatile atomic_t * v)
{
	int	flags;

	save_flags(flags);
	cli();
	v->counter += i;
	restore_flags(flags);
}

extern __inline__ void atomic_sub(int i, volatile atomic_t * v)
{
	int	flags;

	save_flags(flags);
	cli();
	v->counter -= i;
	restore_flags(flags);
}

extern __inline__ int atomic_add_return(int i, atomic_t * v)
{
	int	temp, flags;

	save_flags(flags);
	cli();
	temp = v->counter;
	temp += i;
	v->counter = temp;
	restore_flags(flags);

	return temp;
}

extern __inline__ int atomic_sub_return(int i, atomic_t * v)
{
	int	temp, flags;

	save_flags(flags);
	cli();
	temp = v->counter;
	temp -= i;
	v->counter = temp;
	restore_flags(flags);

	return temp;
}

extern __inline__ int atomic_inc_and_test_greater_zero(volatile atomic_t *v)
{
	int	flags;
        int     result;
        
	save_flags(flags);
	cli();
	v->counter++;
        result= v->counter > 0;
	restore_flags(flags);
        
        return result;
}


#define atomic_dec_return(v) atomic_sub_return(1,(v))
#define atomic_inc_return(v) atomic_add_return(1,(v))

#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)

#define atomic_inc(v) atomic_add(1,(v))
#define atomic_dec(v) atomic_sub(1,(v))
#endif /* __KERNEL__ */

#endif	/* _ASM_BSP_ATOMIC_H_ */
