     1	#ifndef __ASM_SPINLOCK_H
     2	#define __ASM_SPINLOCK_H
     3	
     4	#include <asm/atomic.h>
     5	#include <asm/rwlock.h>
     6	#include <asm/page.h>
     7	#include <linux/config.h>
     8	#include <linux/stringify.h>
     9	
    10	extern int printk(const char * fmt, ...)
    11		__attribute__ ((format (printf, 1, 2)));
    12	
    13	/* It seems that people are forgetting to
    14	 * initialize their spinlocks properly, tsk tsk.
    15	 * Remember to turn this off in 2.4. -ben
    16	 */
    17	#if defined(CONFIG_DEBUG_SPINLOCK)
    18	#define SPINLOCK_DEBUG	1
    19	#else
    20	#define SPINLOCK_DEBUG	0
    21	#endif
    22	
    23	/*
    24	 * Your basic SMP spinlocks, allowing only a single CPU anywhere
    25	 */
    26	
    27	typedef struct {
    28		volatile unsigned int lock;
    29	#if SPINLOCK_DEBUG
    30		unsigned magic;
    31	#endif
    32	} spinlock_t;
    33	
    34	#define SPINLOCK_MAGIC	0xdead4ead
    35	
    36	#if SPINLOCK_DEBUG
    37	#define SPINLOCK_MAGIC_INIT	, SPINLOCK_MAGIC
    38	#else
    39	#define SPINLOCK_MAGIC_INIT	/* */
    40	#endif
    41	
    42	#define SPIN_LOCK_UNLOCKED (spinlock_t) { 1 SPINLOCK_MAGIC_INIT }
    43	
    44	#define spin_lock_init(x)	do { *(x) = SPIN_LOCK_UNLOCKED; } while(0)
    45	
    46	/*
    47	 * Simple spin lock operations.  There are two variants, one clears IRQ's
    48	 * on the local processor, one does not.
    49	 *
    50	 * We make no fairness assumptions. They have a cost.
    51	 */
    52	
    53	#define spin_is_locked(x)	(*(volatile char *)(&(x)->lock) <= 0)
    54	#define spin_unlock_wait(x)	do { barrier(); } while(spin_is_locked(x))
    55	
    56	#define spin_lock_string \
    57		"\n1:\t" \
    58		"lock ; decb %0\n\t" \
    59		"js 2f\n" \
    60		".subsection 1\n" \
    61		".ifndef _text_lock_" __stringify(KBUILD_BASENAME) "\n" \
    62		"_text_lock_" __stringify(KBUILD_BASENAME) ":\n" \
    63		".endif\n" \
    64		"2:\t" \
    65		"cmpb $0,%0\n\t" \
    66		"rep;nop\n\t" \
    67		"jle 2b\n\t" \
    68		"jmp 1b\n" \
    69		".subsection 0\n"
    70	
    71	/*
    72	 * This works. Despite all the confusion.
    73	 * (except on PPro SMP or if we are using OOSTORE)
    74	 * (PPro errata 66, 92)
    75	 */
    76	 
    77	#if !defined(CONFIG_X86_OOSTORE) && !defined(CONFIG_X86_PPRO_FENCE)
    78	
    79	#define spin_unlock_string \
    80		"movb $1,%0" \
    81			:"=m" (lock->lock) : : "memory"
    82	
    83	
    84	static inline void spin_unlock(spinlock_t *lock)
    85	{
    86	#if SPINLOCK_DEBUG
    87		if (lock->magic != SPINLOCK_MAGIC)
    88			BUG();
    89		if (!spin_is_locked(lock))
    90			BUG();
    91	#endif
    92		__asm__ __volatile__(
    93			spin_unlock_string
    94		);
    95	}
    96	
    97	#else
    98	
    99	#define spin_unlock_string \
   100		"xchgb %b0, %1" \
   101			:"=q" (oldval), "=m" (lock->lock) \
   102			:"0" (oldval) : "memory"
   103	
   104	static inline void spin_unlock(spinlock_t *lock)
   105	{
   106		char oldval = 1;
   107	#if SPINLOCK_DEBUG
   108		if (lock->magic != SPINLOCK_MAGIC)
   109			BUG();
   110		if (!spin_is_locked(lock))
   111			BUG();
   112	#endif
   113		__asm__ __volatile__(
   114			spin_unlock_string
   115		);
   116	}
   117	
   118	#endif
   119	
   120	static inline int spin_trylock(spinlock_t *lock)
   121	{
   122		char oldval;
   123		__asm__ __volatile__(
   124			"xchgb %b0,%1"
   125			:"=q" (oldval), "=m" (lock->lock)
   126			:"0" (0) : "memory");
   127		return oldval > 0;
   128	}
   129	
   130	static inline void spin_lock(spinlock_t *lock)
   131	{
   132	#if SPINLOCK_DEBUG
   133		__label__ here;
   134	here:
   135		if (lock->magic != SPINLOCK_MAGIC) {
   136	printk("eip: %p\n", &&here);
   137			BUG();
   138		}
   139	#endif
   140		__asm__ __volatile__(
   141			spin_lock_string
   142			:"=m" (lock->lock) : : "memory");
   143	}
   144	
   145	
   146	/*
   147	 * Read-write spinlocks, allowing multiple readers
   148	 * but only one writer.
   149	 *
   150	 * NOTE! it is quite common to have readers in interrupts
   151	 * but no interrupt writers. For those circumstances we
   152	 * can "mix" irq-safe locks - any writer needs to get a
   153	 * irq-safe write-lock, but readers can get non-irqsafe
   154	 * read-locks.
   155	 */
   156	typedef struct {
   157		volatile unsigned int lock;
   158	#if SPINLOCK_DEBUG
   159		unsigned magic;
   160	#endif
   161	} rwlock_t;
   162	
   163	#define RWLOCK_MAGIC	0xdeaf1eed
   164	
   165	#if SPINLOCK_DEBUG
   166	#define RWLOCK_MAGIC_INIT	, RWLOCK_MAGIC
   167	#else
   168	#define RWLOCK_MAGIC_INIT	/* */
   169	#endif
   170	
   171	#define RW_LOCK_UNLOCKED (rwlock_t) { RW_LOCK_BIAS RWLOCK_MAGIC_INIT }
   172	
   173	#define rwlock_init(x)	do { *(x) = RW_LOCK_UNLOCKED; } while(0)
   174	
   175	/*
   176	 * On x86, we implement read-write locks as a 32-bit counter
   177	 * with the high bit (sign) being the "contended" bit.
   178	 *
   179	 * The inline assembly is non-obvious. Think about it.
   180	 *
   181	 * Changed to use the same technique as rw semaphores.  See
   182	 * semaphore.h for details.  -ben
   183	 */
   184	/* the spinlock helpers are in arch/i386/kernel/semaphore.c */
   185	
   186	static inline void read_lock(rwlock_t *rw)
   187	{
   188	#if SPINLOCK_DEBUG
   189		if (rw->magic != RWLOCK_MAGIC)
   190			BUG();
   191	#endif
   192		__build_read_lock(rw, "__read_lock_failed");
   193	}
   194	
   195	static inline void write_lock(rwlock_t *rw)
   196	{
   197	#if SPINLOCK_DEBUG
   198		if (rw->magic != RWLOCK_MAGIC)
   199			BUG();
   200	#endif
   201		__build_write_lock(rw, "__write_lock_failed");
   202	}
   203	
   204	#define read_unlock(rw)		asm volatile("lock ; incl %0" :"=m" ((rw)->lock) : : "memory")
   205	#define write_unlock(rw)	asm volatile("lock ; addl $" RW_LOCK_BIAS_STR ",%0":"=m" ((rw)->lock) : : "memory")
   206	
   207	static inline int write_trylock(rwlock_t *lock)
   208	{
   209		atomic_t *count = (atomic_t *)lock;
   210		if (atomic_sub_and_test(RW_LOCK_BIAS, count))
   211			return 1;
   212		atomic_add(RW_LOCK_BIAS, count);
   213		return 0;
   214	}
   215	
   216	#endif /* __ASM_SPINLOCK_H */
