blob: 10754a3756684900ea1570cc1c21b5d6bc800240 [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"
37 : : "d" (__cpu_logical_map[cpu]));
38 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();
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070046
47 while (1) {
48 if (count-- <= 0) {
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070049 unsigned int owner = lp->owner_cpu;
50 if (owner != 0)
51 _raw_yield_cpu(~owner);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070052 count = spin_retry;
53 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010054 if (arch_spin_is_locked(lp))
Christian Ehrhardt96567162006-03-09 17:33:49 -080055 continue;
Heiko Carstens3b4beb32008-01-26 14:11:03 +010056 if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070057 return;
58 }
59}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010060EXPORT_SYMBOL(arch_spin_lock_wait);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070061
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010062void arch_spin_lock_wait_flags(arch_spinlock_t *lp, unsigned long flags)
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010063{
64 int count = spin_retry;
65 unsigned int cpu = ~smp_processor_id();
66
67 local_irq_restore(flags);
68 while (1) {
69 if (count-- <= 0) {
70 unsigned int owner = lp->owner_cpu;
71 if (owner != 0)
72 _raw_yield_cpu(~owner);
73 count = spin_retry;
74 }
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010075 if (arch_spin_is_locked(lp))
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010076 continue;
77 local_irq_disable();
78 if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0)
79 return;
80 local_irq_restore(flags);
81 }
82}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010083EXPORT_SYMBOL(arch_spin_lock_wait_flags);
Hisashi Hifumi894cdde2008-01-26 14:11:28 +010084
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010085int arch_spin_trylock_retry(arch_spinlock_t *lp)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070086{
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070087 unsigned int cpu = ~smp_processor_id();
88 int count;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070089
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070090 for (count = spin_retry; count > 0; count--) {
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010091 if (arch_spin_is_locked(lp))
Christian Ehrhardt96567162006-03-09 17:33:49 -080092 continue;
Heiko Carstens3b4beb32008-01-26 14:11:03 +010093 if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070094 return 1;
95 }
96 return 0;
97}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +010098EXPORT_SYMBOL(arch_spin_trylock_retry);
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070099
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100100void arch_spin_relax(arch_spinlock_t *lock)
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700101{
102 unsigned int cpu = lock->owner_cpu;
103 if (cpu != 0)
104 _raw_yield_cpu(~cpu);
105}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100106EXPORT_SYMBOL(arch_spin_relax);
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700107
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100108void _raw_read_lock_wait(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700109{
110 unsigned int old;
111 int count = spin_retry;
112
113 while (1) {
114 if (count-- <= 0) {
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700115 _raw_yield();
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700116 count = spin_retry;
117 }
Thomas Gleixnere5931942009-12-03 20:08:46 +0100118 if (!arch_read_can_lock(rw))
Christian Ehrhardt96567162006-03-09 17:33:49 -0800119 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700120 old = rw->lock & 0x7fffffffU;
121 if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
122 return;
123 }
124}
125EXPORT_SYMBOL(_raw_read_lock_wait);
126
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100127void _raw_read_lock_wait_flags(arch_rwlock_t *rw, unsigned long flags)
Heiko Carstensce58ae62009-06-12 10:26:22 +0200128{
129 unsigned int old;
130 int count = spin_retry;
131
132 local_irq_restore(flags);
133 while (1) {
134 if (count-- <= 0) {
135 _raw_yield();
136 count = spin_retry;
137 }
Thomas Gleixnere5931942009-12-03 20:08:46 +0100138 if (!arch_read_can_lock(rw))
Heiko Carstensce58ae62009-06-12 10:26:22 +0200139 continue;
140 old = rw->lock & 0x7fffffffU;
141 local_irq_disable();
142 if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
143 return;
144 }
145}
146EXPORT_SYMBOL(_raw_read_lock_wait_flags);
147
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100148int _raw_read_trylock_retry(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700149{
150 unsigned int old;
151 int count = spin_retry;
152
153 while (count-- > 0) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100154 if (!arch_read_can_lock(rw))
Christian Ehrhardt96567162006-03-09 17:33:49 -0800155 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700156 old = rw->lock & 0x7fffffffU;
157 if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
158 return 1;
159 }
160 return 0;
161}
162EXPORT_SYMBOL(_raw_read_trylock_retry);
163
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100164void _raw_write_lock_wait(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700165{
166 int count = spin_retry;
167
168 while (1) {
169 if (count-- <= 0) {
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700170 _raw_yield();
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700171 count = spin_retry;
172 }
Thomas Gleixnere5931942009-12-03 20:08:46 +0100173 if (!arch_write_can_lock(rw))
Christian Ehrhardt96567162006-03-09 17:33:49 -0800174 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700175 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
176 return;
177 }
178}
179EXPORT_SYMBOL(_raw_write_lock_wait);
180
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100181void _raw_write_lock_wait_flags(arch_rwlock_t *rw, unsigned long flags)
Heiko Carstensce58ae62009-06-12 10:26:22 +0200182{
183 int count = spin_retry;
184
185 local_irq_restore(flags);
186 while (1) {
187 if (count-- <= 0) {
188 _raw_yield();
189 count = spin_retry;
190 }
Thomas Gleixnere5931942009-12-03 20:08:46 +0100191 if (!arch_write_can_lock(rw))
Heiko Carstensce58ae62009-06-12 10:26:22 +0200192 continue;
193 local_irq_disable();
194 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
195 return;
196 }
197}
198EXPORT_SYMBOL(_raw_write_lock_wait_flags);
199
Thomas Gleixnerfb3a6bb2009-12-03 20:01:19 +0100200int _raw_write_trylock_retry(arch_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700201{
202 int count = spin_retry;
203
204 while (count-- > 0) {
Thomas Gleixnere5931942009-12-03 20:08:46 +0100205 if (!arch_write_can_lock(rw))
Christian Ehrhardt96567162006-03-09 17:33:49 -0800206 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700207 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
208 return 1;
209 }
210 return 0;
211}
212EXPORT_SYMBOL(_raw_write_trylock_retry);