blob: 91754ffb9203a79fc3d7005d3cf8d80905ceb1d3 [file] [log] [blame]
Martin Schwidefsky951f22d2005-07-27 11:44:57 -07001/*
2 * arch/s390/lib/spinlock.c
3 * Out of line spinlock code.
4 *
Christian Ehrhardt96567162006-03-09 17:33:49 -08005 * Copyright (C) IBM Corp. 2004, 2006
Martin Schwidefsky951f22d2005-07-27 11:44:57 -07006 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 */
8
9#include <linux/types.h>
10#include <linux/module.h>
11#include <linux/spinlock.h>
12#include <linux/init.h>
13#include <asm/io.h>
14
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070015int spin_retry = 1000;
16
17/**
18 * spin_retry= parameter
19 */
20static int __init spin_retry_setup(char *str)
21{
22 spin_retry = simple_strtoul(str, &str, 0);
23 return 1;
24}
25__setup("spin_retry=", spin_retry_setup);
26
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070027static inline void _raw_yield(void)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070028{
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070029 if (MACHINE_HAS_DIAG44)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070030 asm volatile("diag 0,0,0x44");
31}
32
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070033static inline void _raw_yield_cpu(int cpu)
34{
35 if (MACHINE_HAS_DIAG9C)
36 asm volatile("diag %0,0,0x9c"
Heiko Carstensfb380aa2010-01-13 20:44:37 +010037 : : "d" (cpu_logical_map(cpu)));
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070038 else
39 _raw_yield();
40}
41
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010042void arch_spin_lock_wait(arch_spinlock_t *lp)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070043{
44 int count = spin_retry;
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070045 unsigned int cpu = ~smp_processor_id();
Gerald Schaefer59b69782010-02-26 22:37:40 +010046 unsigned int owner;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070047
48 while (1) {
Gerald Schaefer59b69782010-02-26 22:37:40 +010049 owner = lp->owner_cpu;
50 if (!owner || smp_vcpu_scheduled(~owner)) {
51 for (count = spin_retry; count > 0; count--) {
52 if (arch_spin_is_locked(lp))
53 continue;
54 if (_raw_compare_and_swap(&lp->owner_cpu, 0,
55 cpu) == 0)
56 return;
57 }
58 if (MACHINE_IS_LPAR)
59 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070060 }
Gerald Schaefer59b69782010-02-26 22:37:40 +010061 owner = lp->owner_cpu;
62 if (owner)
63 _raw_yield_cpu(~owner);
Heiko Carstens3b4beb32008-01-26 14:11:03 +010064 if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070065 return;
66 }
67}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010068EXPORT_SYMBOL(arch_spin_lock_wait);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070069
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010070void arch_spin_lock_wait_flags(arch_spinlock_t *lp, unsigned long flags)
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010071{
72 int count = spin_retry;
73 unsigned int cpu = ~smp_processor_id();
Gerald Schaefer59b69782010-02-26 22:37:40 +010074 unsigned int owner;
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010075
76 local_irq_restore(flags);
77 while (1) {
Gerald Schaefer59b69782010-02-26 22:37:40 +010078 owner = lp->owner_cpu;
79 if (!owner || smp_vcpu_scheduled(~owner)) {
80 for (count = spin_retry; count > 0; count--) {
81 if (arch_spin_is_locked(lp))
82 continue;
83 local_irq_disable();
84 if (_raw_compare_and_swap(&lp->owner_cpu, 0,
85 cpu) == 0)
86 return;
87 local_irq_restore(flags);
88 }
89 if (MACHINE_IS_LPAR)
90 continue;
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010091 }
Gerald Schaefer59b69782010-02-26 22:37:40 +010092 owner = lp->owner_cpu;
93 if (owner)
94 _raw_yield_cpu(~owner);
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010095 local_irq_disable();
96 if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0)
97 return;
98 local_irq_restore(flags);
99 }
100}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100101EXPORT_SYMBOL(arch_spin_lock_wait_flags);
Hisashi Hifumi894cdde2008-01-26 14:11:28 +0100102
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100103int arch_spin_trylock_retry(arch_spinlock_t *lp)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700104{
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700105 unsigned int cpu = ~smp_processor_id();
106 int count;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700107
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700108 for (count = spin_retry; count > 0; count--) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100109 if (arch_spin_is_locked(lp))
Christian Ehrhardt96567162006-03-09 17:33:49 -0800110 continue;
Heiko Carstens3b4beb32008-01-26 14:11:03 +0100111 if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700112 return 1;
113 }
114 return 0;
115}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100116EXPORT_SYMBOL(arch_spin_trylock_retry);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700117
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100118void arch_spin_relax(arch_spinlock_t *lock)
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700119{
120 unsigned int cpu = lock->owner_cpu;
Gerald Schaefer59b69782010-02-26 22:37:40 +0100121 if (cpu != 0) {
122 if (MACHINE_IS_VM || MACHINE_IS_KVM ||
123 !smp_vcpu_scheduled(~cpu))
124 _raw_yield_cpu(~cpu);
125 }
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700126}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100127EXPORT_SYMBOL(arch_spin_relax);
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700128
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100129void _raw_read_lock_wait(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700130{
131 unsigned int old;
132 int count = spin_retry;
133
134 while (1) {
135 if (count-- <= 0) {
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700136 _raw_yield();
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700137 count = spin_retry;
138 }
Thomas Gleixnere5931942009-12-03 20:08:46 +0100139 if (!arch_read_can_lock(rw))
Christian Ehrhardt96567162006-03-09 17:33:49 -0800140 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700141 old = rw->lock & 0x7fffffffU;
142 if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
143 return;
144 }
145}
146EXPORT_SYMBOL(_raw_read_lock_wait);
147
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100148void _raw_read_lock_wait_flags(arch_rwlock_t *rw, unsigned long flags)
Heiko Carstensce58ae62009-06-12 10:26:22 +0200149{
150 unsigned int old;
151 int count = spin_retry;
152
153 local_irq_restore(flags);
154 while (1) {
155 if (count-- <= 0) {
156 _raw_yield();
157 count = spin_retry;
158 }
Thomas Gleixnere5931942009-12-03 20:08:46 +0100159 if (!arch_read_can_lock(rw))
Heiko Carstensce58ae62009-06-12 10:26:22 +0200160 continue;
161 old = rw->lock & 0x7fffffffU;
162 local_irq_disable();
163 if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
164 return;
165 }
166}
167EXPORT_SYMBOL(_raw_read_lock_wait_flags);
168
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100169int _raw_read_trylock_retry(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700170{
171 unsigned int old;
172 int count = spin_retry;
173
174 while (count-- > 0) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100175 if (!arch_read_can_lock(rw))
Christian Ehrhardt96567162006-03-09 17:33:49 -0800176 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700177 old = rw->lock & 0x7fffffffU;
178 if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
179 return 1;
180 }
181 return 0;
182}
183EXPORT_SYMBOL(_raw_read_trylock_retry);
184
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100185void _raw_write_lock_wait(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700186{
187 int count = spin_retry;
188
189 while (1) {
190 if (count-- <= 0) {
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700191 _raw_yield();
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700192 count = spin_retry;
193 }
Thomas Gleixnere5931942009-12-03 20:08:46 +0100194 if (!arch_write_can_lock(rw))
Christian Ehrhardt96567162006-03-09 17:33:49 -0800195 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700196 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
197 return;
198 }
199}
200EXPORT_SYMBOL(_raw_write_lock_wait);
201
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100202void _raw_write_lock_wait_flags(arch_rwlock_t *rw, unsigned long flags)
Heiko Carstensce58ae62009-06-12 10:26:22 +0200203{
204 int count = spin_retry;
205
206 local_irq_restore(flags);
207 while (1) {
208 if (count-- <= 0) {
209 _raw_yield();
210 count = spin_retry;
211 }
Thomas Gleixnere5931942009-12-03 20:08:46 +0100212 if (!arch_write_can_lock(rw))
Heiko Carstensce58ae62009-06-12 10:26:22 +0200213 continue;
214 local_irq_disable();
215 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
216 return;
217 }
218}
219EXPORT_SYMBOL(_raw_write_lock_wait_flags);
220
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100221int _raw_write_trylock_retry(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700222{
223 int count = spin_retry;
224
225 while (count-- > 0) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100226 if (!arch_write_can_lock(rw))
Christian Ehrhardt96567162006-03-09 17:33:49 -0800227 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700228 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
229 return 1;
230 }
231 return 0;
232}
233EXPORT_SYMBOL(_raw_write_trylock_retry);