18c2ecf20Sopenharmony_ci/***********************license start*************** 28c2ecf20Sopenharmony_ci * Author: Cavium Networks 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Contact: support@caviumnetworks.com 58c2ecf20Sopenharmony_ci * This file is part of the OCTEON SDK 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright (c) 2003-2008 Cavium Networks 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This file is free software; you can redistribute it and/or modify 108c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License, Version 2, as 118c2ecf20Sopenharmony_ci * published by the Free Software Foundation. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * This file is distributed in the hope that it will be useful, but 148c2ecf20Sopenharmony_ci * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty 158c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or 168c2ecf20Sopenharmony_ci * NONINFRINGEMENT. See the GNU General Public License for more 178c2ecf20Sopenharmony_ci * details. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License 208c2ecf20Sopenharmony_ci * along with this file; if not, write to the Free Software 218c2ecf20Sopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 228c2ecf20Sopenharmony_ci * or visit http://www.gnu.org/licenses/. 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * This file may also be available under a different license from Cavium. 258c2ecf20Sopenharmony_ci * Contact Cavium Networks for more information 268c2ecf20Sopenharmony_ci ***********************license end**************************************/ 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/** 298c2ecf20Sopenharmony_ci * Implementation of spinlocks for Octeon CVMX. Although similar in 308c2ecf20Sopenharmony_ci * function to Linux kernel spinlocks, they are not compatible. 318c2ecf20Sopenharmony_ci * Octeon CVMX spinlocks are only used to synchronize with the boot 328c2ecf20Sopenharmony_ci * monitor and other non-Linux programs running in the system. 338c2ecf20Sopenharmony_ci */ 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#ifndef __CVMX_SPINLOCK_H__ 368c2ecf20Sopenharmony_ci#define __CVMX_SPINLOCK_H__ 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-asm.h> 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci/* Spinlocks for Octeon */ 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci/* define these to enable recursive spinlock debugging */ 438c2ecf20Sopenharmony_ci/*#define CVMX_SPINLOCK_DEBUG */ 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/** 468c2ecf20Sopenharmony_ci * Spinlocks for Octeon CVMX 478c2ecf20Sopenharmony_ci */ 488c2ecf20Sopenharmony_citypedef struct { 498c2ecf20Sopenharmony_ci volatile uint32_t value; 508c2ecf20Sopenharmony_ci} cvmx_spinlock_t; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/* note - macros not expanded in inline ASM, so values hardcoded */ 538c2ecf20Sopenharmony_ci#define CVMX_SPINLOCK_UNLOCKED_VAL 0 548c2ecf20Sopenharmony_ci#define CVMX_SPINLOCK_LOCKED_VAL 1 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci#define CVMX_SPINLOCK_UNLOCKED_INITIALIZER {CVMX_SPINLOCK_UNLOCKED_VAL} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/** 598c2ecf20Sopenharmony_ci * Initialize a spinlock 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * @lock: Lock to initialize 628c2ecf20Sopenharmony_ci */ 638c2ecf20Sopenharmony_cistatic inline void cvmx_spinlock_init(cvmx_spinlock_t *lock) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci lock->value = CVMX_SPINLOCK_UNLOCKED_VAL; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci/** 698c2ecf20Sopenharmony_ci * Return non-zero if the spinlock is currently locked 708c2ecf20Sopenharmony_ci * 718c2ecf20Sopenharmony_ci * @lock: Lock to check 728c2ecf20Sopenharmony_ci * Returns Non-zero if locked 738c2ecf20Sopenharmony_ci */ 748c2ecf20Sopenharmony_cistatic inline int cvmx_spinlock_locked(cvmx_spinlock_t *lock) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci return lock->value != CVMX_SPINLOCK_UNLOCKED_VAL; 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci/** 808c2ecf20Sopenharmony_ci * Releases lock 818c2ecf20Sopenharmony_ci * 828c2ecf20Sopenharmony_ci * @lock: pointer to lock structure 838c2ecf20Sopenharmony_ci */ 848c2ecf20Sopenharmony_cistatic inline void cvmx_spinlock_unlock(cvmx_spinlock_t *lock) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci CVMX_SYNCWS; 878c2ecf20Sopenharmony_ci lock->value = 0; 888c2ecf20Sopenharmony_ci CVMX_SYNCWS; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/** 928c2ecf20Sopenharmony_ci * Attempts to take the lock, but does not spin if lock is not available. 938c2ecf20Sopenharmony_ci * May take some time to acquire the lock even if it is available 948c2ecf20Sopenharmony_ci * due to the ll/sc not succeeding. 958c2ecf20Sopenharmony_ci * 968c2ecf20Sopenharmony_ci * @lock: pointer to lock structure 978c2ecf20Sopenharmony_ci * 988c2ecf20Sopenharmony_ci * Returns 0: lock successfully taken 998c2ecf20Sopenharmony_ci * 1: lock not taken, held by someone else 1008c2ecf20Sopenharmony_ci * These return values match the Linux semantics. 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistatic inline unsigned int cvmx_spinlock_trylock(cvmx_spinlock_t *lock) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci unsigned int tmp; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci __asm__ __volatile__(".set noreorder \n" 1088c2ecf20Sopenharmony_ci "1: ll %[tmp], %[val] \n" 1098c2ecf20Sopenharmony_ci /* if lock held, fail immediately */ 1108c2ecf20Sopenharmony_ci " bnez %[tmp], 2f \n" 1118c2ecf20Sopenharmony_ci " li %[tmp], 1 \n" 1128c2ecf20Sopenharmony_ci " sc %[tmp], %[val] \n" 1138c2ecf20Sopenharmony_ci " beqz %[tmp], 1b \n" 1148c2ecf20Sopenharmony_ci " li %[tmp], 0 \n" 1158c2ecf20Sopenharmony_ci "2: \n" 1168c2ecf20Sopenharmony_ci ".set reorder \n" : 1178c2ecf20Sopenharmony_ci [val] "+m"(lock->value), [tmp] "=&r"(tmp) 1188c2ecf20Sopenharmony_ci : : "memory"); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci return tmp != 0; /* normalize to 0 or 1 */ 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci/** 1248c2ecf20Sopenharmony_ci * Gets lock, spins until lock is taken 1258c2ecf20Sopenharmony_ci * 1268c2ecf20Sopenharmony_ci * @lock: pointer to lock structure 1278c2ecf20Sopenharmony_ci */ 1288c2ecf20Sopenharmony_cistatic inline void cvmx_spinlock_lock(cvmx_spinlock_t *lock) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci unsigned int tmp; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci __asm__ __volatile__(".set noreorder \n" 1338c2ecf20Sopenharmony_ci "1: ll %[tmp], %[val] \n" 1348c2ecf20Sopenharmony_ci " bnez %[tmp], 1b \n" 1358c2ecf20Sopenharmony_ci " li %[tmp], 1 \n" 1368c2ecf20Sopenharmony_ci " sc %[tmp], %[val] \n" 1378c2ecf20Sopenharmony_ci " beqz %[tmp], 1b \n" 1388c2ecf20Sopenharmony_ci " nop \n" 1398c2ecf20Sopenharmony_ci ".set reorder \n" : 1408c2ecf20Sopenharmony_ci [val] "+m"(lock->value), [tmp] "=&r"(tmp) 1418c2ecf20Sopenharmony_ci : : "memory"); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci/** ******************************************************************** 1468c2ecf20Sopenharmony_ci * Bit spinlocks 1478c2ecf20Sopenharmony_ci * These spinlocks use a single bit (bit 31) of a 32 bit word for locking. 1488c2ecf20Sopenharmony_ci * The rest of the bits in the word are left undisturbed. This enables more 1498c2ecf20Sopenharmony_ci * compact data structures as only 1 bit is consumed for the lock. 1508c2ecf20Sopenharmony_ci * 1518c2ecf20Sopenharmony_ci */ 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci/** 1548c2ecf20Sopenharmony_ci * Gets lock, spins until lock is taken 1558c2ecf20Sopenharmony_ci * Preserves the low 31 bits of the 32 bit 1568c2ecf20Sopenharmony_ci * word used for the lock. 1578c2ecf20Sopenharmony_ci * 1588c2ecf20Sopenharmony_ci * 1598c2ecf20Sopenharmony_ci * @word: word to lock bit 31 of 1608c2ecf20Sopenharmony_ci */ 1618c2ecf20Sopenharmony_cistatic inline void cvmx_spinlock_bit_lock(uint32_t *word) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci unsigned int tmp; 1648c2ecf20Sopenharmony_ci unsigned int sav; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci __asm__ __volatile__(".set noreorder \n" 1678c2ecf20Sopenharmony_ci ".set noat \n" 1688c2ecf20Sopenharmony_ci "1: ll %[tmp], %[val] \n" 1698c2ecf20Sopenharmony_ci " bbit1 %[tmp], 31, 1b \n" 1708c2ecf20Sopenharmony_ci " li $at, 1 \n" 1718c2ecf20Sopenharmony_ci " ins %[tmp], $at, 31, 1 \n" 1728c2ecf20Sopenharmony_ci " sc %[tmp], %[val] \n" 1738c2ecf20Sopenharmony_ci " beqz %[tmp], 1b \n" 1748c2ecf20Sopenharmony_ci " nop \n" 1758c2ecf20Sopenharmony_ci ".set at \n" 1768c2ecf20Sopenharmony_ci ".set reorder \n" : 1778c2ecf20Sopenharmony_ci [val] "+m"(*word), [tmp] "=&r"(tmp), [sav] "=&r"(sav) 1788c2ecf20Sopenharmony_ci : : "memory"); 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci} 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci/** 1838c2ecf20Sopenharmony_ci * Attempts to get lock, returns immediately with success/failure 1848c2ecf20Sopenharmony_ci * Preserves the low 31 bits of the 32 bit 1858c2ecf20Sopenharmony_ci * word used for the lock. 1868c2ecf20Sopenharmony_ci * 1878c2ecf20Sopenharmony_ci * 1888c2ecf20Sopenharmony_ci * @word: word to lock bit 31 of 1898c2ecf20Sopenharmony_ci * Returns 0: lock successfully taken 1908c2ecf20Sopenharmony_ci * 1: lock not taken, held by someone else 1918c2ecf20Sopenharmony_ci * These return values match the Linux semantics. 1928c2ecf20Sopenharmony_ci */ 1938c2ecf20Sopenharmony_cistatic inline unsigned int cvmx_spinlock_bit_trylock(uint32_t *word) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci unsigned int tmp; 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci __asm__ __volatile__(".set noreorder\n\t" 1988c2ecf20Sopenharmony_ci ".set noat\n" 1998c2ecf20Sopenharmony_ci "1: ll %[tmp], %[val] \n" 2008c2ecf20Sopenharmony_ci /* if lock held, fail immediately */ 2018c2ecf20Sopenharmony_ci " bbit1 %[tmp], 31, 2f \n" 2028c2ecf20Sopenharmony_ci " li $at, 1 \n" 2038c2ecf20Sopenharmony_ci " ins %[tmp], $at, 31, 1 \n" 2048c2ecf20Sopenharmony_ci " sc %[tmp], %[val] \n" 2058c2ecf20Sopenharmony_ci " beqz %[tmp], 1b \n" 2068c2ecf20Sopenharmony_ci " li %[tmp], 0 \n" 2078c2ecf20Sopenharmony_ci "2: \n" 2088c2ecf20Sopenharmony_ci ".set at \n" 2098c2ecf20Sopenharmony_ci ".set reorder \n" : 2108c2ecf20Sopenharmony_ci [val] "+m"(*word), [tmp] "=&r"(tmp) 2118c2ecf20Sopenharmony_ci : : "memory"); 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci return tmp != 0; /* normalize to 0 or 1 */ 2148c2ecf20Sopenharmony_ci} 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci/** 2178c2ecf20Sopenharmony_ci * Releases bit lock 2188c2ecf20Sopenharmony_ci * 2198c2ecf20Sopenharmony_ci * Unconditionally clears bit 31 of the lock word. Note that this is 2208c2ecf20Sopenharmony_ci * done non-atomically, as this implementation assumes that the rest 2218c2ecf20Sopenharmony_ci * of the bits in the word are protected by the lock. 2228c2ecf20Sopenharmony_ci * 2238c2ecf20Sopenharmony_ci * @word: word to unlock bit 31 in 2248c2ecf20Sopenharmony_ci */ 2258c2ecf20Sopenharmony_cistatic inline void cvmx_spinlock_bit_unlock(uint32_t *word) 2268c2ecf20Sopenharmony_ci{ 2278c2ecf20Sopenharmony_ci CVMX_SYNCWS; 2288c2ecf20Sopenharmony_ci *word &= ~(1UL << 31); 2298c2ecf20Sopenharmony_ci CVMX_SYNCWS; 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci#endif /* __CVMX_SPINLOCK_H__ */ 233