blob: b14b1ba5bf9c328959bea3a14643fbb87e4ae7b1 [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 *
14 * 32-bit SMP spinlocks.
15 */
16
17#ifndef _ASM_TILE_SPINLOCK_32_H
18#define _ASM_TILE_SPINLOCK_32_H
19
Arun Sharma600634972011-07-26 16:09:06 -070020#include <linux/atomic.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040021#include <asm/page.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040022#include <linux/compiler.h>
23
24/*
25 * We only use even ticket numbers so the '1' inserted by a tns is
26 * an unambiguous "ticket is busy" flag.
27 */
28#define TICKET_QUANTUM 2
29
30
31/*
32 * SMP ticket spinlocks, allowing only a single CPU anywhere
33 *
34 * (the type definitions are in asm/spinlock_types.h)
35 */
36static inline int arch_spin_is_locked(arch_spinlock_t *lock)
37{
38 /*
39 * Note that even if a new ticket is in the process of being
40 * acquired, so lock->next_ticket is 1, it's still reasonable
41 * to claim the lock is held, since it will be momentarily
42 * if not already. There's no need to wait for a "valid"
43 * lock->next_ticket to become available.
Chris Metcalf627ae542015-04-28 13:00:42 -040044 * Use READ_ONCE() to ensure that calling this in a loop is OK.
Chris Metcalf867e3592010-05-28 23:09:12 -040045 */
Chris Metcalf627ae542015-04-28 13:00:42 -040046 int curr = READ_ONCE(lock->current_ticket);
47 int next = READ_ONCE(lock->next_ticket);
48
49 return next != curr;
Chris Metcalf867e3592010-05-28 23:09:12 -040050}
51
52void arch_spin_lock(arch_spinlock_t *lock);
53
54/* We cannot take an interrupt after getting a ticket, so don't enable them. */
55#define arch_spin_lock_flags(lock, flags) arch_spin_lock(lock)
56
57int arch_spin_trylock(arch_spinlock_t *lock);
58
59static inline void arch_spin_unlock(arch_spinlock_t *lock)
60{
61 /* For efficiency, overlap fetching the old ticket with the wmb(). */
62 int old_ticket = lock->current_ticket;
63 wmb(); /* guarantee anything modified under the lock is visible */
64 lock->current_ticket = old_ticket + TICKET_QUANTUM;
65}
66
67void arch_spin_unlock_wait(arch_spinlock_t *lock);
68
69/*
70 * Read-write spinlocks, allowing multiple readers
71 * but only one writer.
72 *
73 * We use a "tns/store-back" technique on a single word to manage
74 * the lock state, looping around to retry if the tns returns 1.
75 */
76
77/* Internal layout of the word; do not use. */
78#define _WR_NEXT_SHIFT 8
79#define _WR_CURR_SHIFT 16
80#define _WR_WIDTH 8
81#define _RD_COUNT_SHIFT 24
82#define _RD_COUNT_WIDTH 8
83
Chris Metcalf867e3592010-05-28 23:09:12 -040084/**
85 * arch_read_can_lock() - would read_trylock() succeed?
86 */
87static inline int arch_read_can_lock(arch_rwlock_t *rwlock)
88{
89 return (rwlock->lock << _RD_COUNT_WIDTH) == 0;
90}
91
92/**
93 * arch_write_can_lock() - would write_trylock() succeed?
94 */
95static inline int arch_write_can_lock(arch_rwlock_t *rwlock)
96{
97 return rwlock->lock == 0;
98}
99
100/**
101 * arch_read_lock() - acquire a read lock.
102 */
Chris Metcalf3c5ead52011-03-01 13:30:15 -0500103void arch_read_lock(arch_rwlock_t *rwlock);
Chris Metcalf867e3592010-05-28 23:09:12 -0400104
105/**
Chris Metcalf3c5ead52011-03-01 13:30:15 -0500106 * arch_write_lock() - acquire a write lock.
Chris Metcalf867e3592010-05-28 23:09:12 -0400107 */
Chris Metcalf3c5ead52011-03-01 13:30:15 -0500108void arch_write_lock(arch_rwlock_t *rwlock);
Chris Metcalf867e3592010-05-28 23:09:12 -0400109
110/**
111 * arch_read_trylock() - try to acquire a read lock.
112 */
Chris Metcalf3c5ead52011-03-01 13:30:15 -0500113int arch_read_trylock(arch_rwlock_t *rwlock);
Chris Metcalf867e3592010-05-28 23:09:12 -0400114
115/**
116 * arch_write_trylock() - try to acquire a write lock.
117 */
Chris Metcalf3c5ead52011-03-01 13:30:15 -0500118int arch_write_trylock(arch_rwlock_t *rwlock);
Chris Metcalf867e3592010-05-28 23:09:12 -0400119
120/**
121 * arch_read_unlock() - release a read lock.
122 */
Chris Metcalf3c5ead52011-03-01 13:30:15 -0500123void arch_read_unlock(arch_rwlock_t *rwlock);
Chris Metcalf867e3592010-05-28 23:09:12 -0400124
125/**
126 * arch_write_unlock() - release a write lock.
127 */
Chris Metcalf3c5ead52011-03-01 13:30:15 -0500128void arch_write_unlock(arch_rwlock_t *rwlock);
Chris Metcalf867e3592010-05-28 23:09:12 -0400129
130#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
131#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
132
133#endif /* _ASM_TILE_SPINLOCK_32_H */