/*1* Copyright 2011 Tilera Corporation. All Rights Reserved.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation, version 2.6*7* This program is distributed in the hope that it will be useful, but8* WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or10* NON INFRINGEMENT. See the GNU General Public License for11* more details.12*/1314#include <linux/spinlock.h>15#include <linux/module.h>16#include <asm/processor.h>1718#include "spinlock_common.h"1920/*21* Read the spinlock value without allocating in our cache and without22* causing an invalidation to another cpu with a copy of the cacheline.23* This is important when we are spinning waiting for the lock.24*/25static inline u32 arch_spin_read_noalloc(void *lock)26{27return atomic_cmpxchg((atomic_t *)lock, -1, -1);28}2930/*31* Wait until the high bits (current) match my ticket.32* If we notice the overflow bit set on entry, we clear it.33*/34void arch_spin_lock_slow(arch_spinlock_t *lock, u32 my_ticket)35{36if (unlikely(my_ticket & __ARCH_SPIN_NEXT_OVERFLOW)) {37__insn_fetchand4(&lock->lock, ~__ARCH_SPIN_NEXT_OVERFLOW);38my_ticket &= ~__ARCH_SPIN_NEXT_OVERFLOW;39}4041for (;;) {42u32 val = arch_spin_read_noalloc(lock);43u32 delta = my_ticket - arch_spin_current(val);44if (delta == 0)45return;46relax((128 / CYCLES_PER_RELAX_LOOP) * delta);47}48}49EXPORT_SYMBOL(arch_spin_lock_slow);5051/*52* Check the lock to see if it is plausible, and try to get it with cmpxchg().53*/54int arch_spin_trylock(arch_spinlock_t *lock)55{56u32 val = arch_spin_read_noalloc(lock);57if (unlikely(arch_spin_current(val) != arch_spin_next(val)))58return 0;59return cmpxchg(&lock->lock, val, (val + 1) & ~__ARCH_SPIN_NEXT_OVERFLOW)60== val;61}62EXPORT_SYMBOL(arch_spin_trylock);6364void arch_spin_unlock_wait(arch_spinlock_t *lock)65{66u32 iterations = 0;67while (arch_spin_is_locked(lock))68delay_backoff(iterations++);69}70EXPORT_SYMBOL(arch_spin_unlock_wait);7172/*73* If the read lock fails due to a writer, we retry periodically74* until the value is positive and we write our incremented reader count.75*/76void __read_lock_failed(arch_rwlock_t *rw)77{78u32 val;79int iterations = 0;80do {81delay_backoff(iterations++);82val = __insn_fetchaddgez4(&rw->lock, 1);83} while (unlikely(arch_write_val_locked(val)));84}85EXPORT_SYMBOL(__read_lock_failed);8687/*88* If we failed because there were readers, clear the "writer" bit89* so we don't block additional readers. Otherwise, there was another90* writer anyway, so our "fetchor" made no difference. Then wait,91* issuing periodic fetchor instructions, till we get the lock.92*/93void __write_lock_failed(arch_rwlock_t *rw, u32 val)94{95int iterations = 0;96do {97if (!arch_write_val_locked(val))98val = __insn_fetchand4(&rw->lock, ~__WRITE_LOCK_BIT);99delay_backoff(iterations++);100val = __insn_fetchor4(&rw->lock, __WRITE_LOCK_BIT);101} while (val != 0);102}103EXPORT_SYMBOL(__write_lock_failed);104105106