GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / tile / lib / spinlock_32.c
blob485e24d62c6bfd5658727d3873ab488e27732d8d
1 /*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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.
15 #include <linux/spinlock.h>
16 #include <linux/module.h>
17 #include <asm/processor.h>
19 #include "spinlock_common.h"
21 void arch_spin_lock(arch_spinlock_t *lock)
23 int my_ticket;
24 int iterations = 0;
25 int delta;
27 while ((my_ticket = __insn_tns((void *)&lock->next_ticket)) & 1)
28 delay_backoff(iterations++);
30 /* Increment the next ticket number, implicitly releasing tns lock. */
31 lock->next_ticket = my_ticket + TICKET_QUANTUM;
33 /* Wait until it's our turn. */
34 while ((delta = my_ticket - lock->current_ticket) != 0)
35 relax((128 / CYCLES_PER_RELAX_LOOP) * delta);
37 EXPORT_SYMBOL(arch_spin_lock);
39 int arch_spin_trylock(arch_spinlock_t *lock)
42 * Grab a ticket; no need to retry if it's busy, we'll just
43 * treat that the same as "locked", since someone else
44 * will lock it momentarily anyway.
46 int my_ticket = __insn_tns((void *)&lock->next_ticket);
48 if (my_ticket == lock->current_ticket) {
49 /* Not currently locked, so lock it by keeping this ticket. */
50 lock->next_ticket = my_ticket + TICKET_QUANTUM;
51 /* Success! */
52 return 1;
55 if (!(my_ticket & 1)) {
56 /* Release next_ticket. */
57 lock->next_ticket = my_ticket;
60 return 0;
62 EXPORT_SYMBOL(arch_spin_trylock);
64 void arch_spin_unlock_wait(arch_spinlock_t *lock)
66 u32 iterations = 0;
67 while (arch_spin_is_locked(lock))
68 delay_backoff(iterations++);
70 EXPORT_SYMBOL(arch_spin_unlock_wait);
73 * The low byte is always reserved to be the marker for a "tns" operation
74 * since the low bit is set to "1" by a tns. The next seven bits are
75 * zeroes. The next byte holds the "next" writer value, i.e. the ticket
76 * available for the next task that wants to write. The third byte holds
77 * the current writer value, i.e. the writer who holds the current ticket.
78 * If current == next == 0, there are no interested writers.
80 #define WR_NEXT_SHIFT _WR_NEXT_SHIFT
81 #define WR_CURR_SHIFT _WR_CURR_SHIFT
82 #define WR_WIDTH _WR_WIDTH
83 #define WR_MASK ((1 << WR_WIDTH) - 1)
86 * The last eight bits hold the active reader count. This has to be
87 * zero before a writer can start to write.
89 #define RD_COUNT_SHIFT _RD_COUNT_SHIFT
90 #define RD_COUNT_WIDTH _RD_COUNT_WIDTH
91 #define RD_COUNT_MASK ((1 << RD_COUNT_WIDTH) - 1)
94 /* Lock the word, spinning until there are no tns-ers. */
95 static inline u32 get_rwlock(arch_rwlock_t *rwlock)
97 u32 iterations = 0;
98 for (;;) {
99 u32 val = __insn_tns((int *)&rwlock->lock);
100 if (unlikely(val & 1)) {
101 delay_backoff(iterations++);
102 continue;
104 return val;
108 int arch_read_trylock_slow(arch_rwlock_t *rwlock)
110 u32 val = get_rwlock(rwlock);
111 int locked = (val << RD_COUNT_WIDTH) == 0;
112 rwlock->lock = val + (locked << RD_COUNT_SHIFT);
113 return locked;
115 EXPORT_SYMBOL(arch_read_trylock_slow);
117 void arch_read_unlock_slow(arch_rwlock_t *rwlock)
119 u32 val = get_rwlock(rwlock);
120 rwlock->lock = val - (1 << RD_COUNT_SHIFT);
122 EXPORT_SYMBOL(arch_read_unlock_slow);
124 void arch_write_unlock_slow(arch_rwlock_t *rwlock, u32 val)
126 u32 eq, mask = 1 << WR_CURR_SHIFT;
127 while (unlikely(val & 1)) {
128 /* Limited backoff since we are the highest-priority task. */
129 relax(4);
130 val = __insn_tns((int *)&rwlock->lock);
132 val = __insn_addb(val, mask);
133 eq = __insn_seqb(val, val << (WR_CURR_SHIFT - WR_NEXT_SHIFT));
134 val = __insn_mz(eq & mask, val);
135 rwlock->lock = val;
137 EXPORT_SYMBOL(arch_write_unlock_slow);
140 * We spin until everything but the reader bits (which are in the high
141 * part of the word) are zero, i.e. no active or waiting writers, no tns.
143 * ISSUE: This approach can permanently starve readers. A reader who sees
144 * a writer could instead take a ticket lock (just like a writer would),
145 * and atomically enter read mode (with 1 reader) when it gets the ticket.
146 * This way both readers and writers will always make forward progress
147 * in a finite time.
149 void arch_read_lock_slow(arch_rwlock_t *rwlock, u32 val)
151 u32 iterations = 0;
152 do {
153 if (!(val & 1))
154 rwlock->lock = val;
155 delay_backoff(iterations++);
156 val = __insn_tns((int *)&rwlock->lock);
157 } while ((val << RD_COUNT_WIDTH) != 0);
158 rwlock->lock = val + (1 << RD_COUNT_SHIFT);
160 EXPORT_SYMBOL(arch_read_lock_slow);
162 void arch_write_lock_slow(arch_rwlock_t *rwlock, u32 val)
165 * The trailing underscore on this variable (and curr_ below)
166 * reminds us that the high bits are garbage; we mask them out
167 * when we compare them.
169 u32 my_ticket_;
171 /* Take out the next ticket; this will also stop would-be readers. */
172 if (val & 1)
173 val = get_rwlock(rwlock);
174 rwlock->lock = __insn_addb(val, 1 << WR_NEXT_SHIFT);
176 /* Extract my ticket value from the original word. */
177 my_ticket_ = val >> WR_NEXT_SHIFT;
180 * Wait until the "current" field matches our ticket, and
181 * there are no remaining readers.
183 for (;;) {
184 u32 curr_ = val >> WR_CURR_SHIFT;
185 u32 readers = val >> RD_COUNT_SHIFT;
186 u32 delta = ((my_ticket_ - curr_) & WR_MASK) + !!readers;
187 if (likely(delta == 0))
188 break;
190 /* Delay based on how many lock-holders are still out there. */
191 relax((256 / CYCLES_PER_RELAX_LOOP) * delta);
194 * Get a non-tns value to check; we don't need to tns
195 * it ourselves. Since we're not tns'ing, we retry
196 * more rapidly to get a valid value.
198 while ((val = rwlock->lock) & 1)
199 relax(4);
202 EXPORT_SYMBOL(arch_write_lock_slow);
204 int __tns_atomic_acquire(atomic_t *lock)
206 int ret;
207 u32 iterations = 0;
209 BUG_ON(__insn_mfspr(SPR_INTERRUPT_CRITICAL_SECTION));
210 __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 1);
212 while ((ret = __insn_tns((void *)&lock->counter)) == 1)
213 delay_backoff(iterations++);
214 return ret;
217 void __tns_atomic_release(atomic_t *p, int v)
219 p->counter = v;
220 __insn_mtspr(SPR_INTERRUPT_CRITICAL_SECTION, 0);