blob: 8d76403fcf89b5900f02f6084312049791b8dc4e [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
42void _raw_spin_lock_wait(raw_spinlock_t *lp, unsigned int pc)
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 }
Christian Ehrhardt96567162006-03-09 17:33:49 -080054 if (__raw_spin_is_locked(lp))
55 continue;
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070056 if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0) {
57 lp->owner_pc = pc;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070058 return;
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070059 }
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070060 }
61}
62EXPORT_SYMBOL(_raw_spin_lock_wait);
63
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070064int _raw_spin_trylock_retry(raw_spinlock_t *lp, unsigned int pc)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070065{
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070066 unsigned int cpu = ~smp_processor_id();
67 int count;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070068
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070069 for (count = spin_retry; count > 0; count--) {
Christian Ehrhardt96567162006-03-09 17:33:49 -080070 if (__raw_spin_is_locked(lp))
71 continue;
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070072 if (_raw_compare_and_swap(&lp->owner_cpu, 0, cpu) == 0) {
73 lp->owner_pc = pc;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070074 return 1;
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070075 }
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070076 }
77 return 0;
78}
79EXPORT_SYMBOL(_raw_spin_trylock_retry);
80
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070081void _raw_spin_relax(raw_spinlock_t *lock)
82{
83 unsigned int cpu = lock->owner_cpu;
84 if (cpu != 0)
85 _raw_yield_cpu(~cpu);
86}
87EXPORT_SYMBOL(_raw_spin_relax);
88
89void _raw_read_lock_wait(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070090{
91 unsigned int old;
92 int count = spin_retry;
93
94 while (1) {
95 if (count-- <= 0) {
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -070096 _raw_yield();
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070097 count = spin_retry;
98 }
Christian Ehrhardt96567162006-03-09 17:33:49 -080099 if (!__raw_read_can_lock(rw))
100 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700101 old = rw->lock & 0x7fffffffU;
102 if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
103 return;
104 }
105}
106EXPORT_SYMBOL(_raw_read_lock_wait);
107
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700108int _raw_read_trylock_retry(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700109{
110 unsigned int old;
111 int count = spin_retry;
112
113 while (count-- > 0) {
Christian Ehrhardt96567162006-03-09 17:33:49 -0800114 if (!__raw_read_can_lock(rw))
115 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700116 old = rw->lock & 0x7fffffffU;
117 if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
118 return 1;
119 }
120 return 0;
121}
122EXPORT_SYMBOL(_raw_read_trylock_retry);
123
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700124void _raw_write_lock_wait(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700125{
126 int count = spin_retry;
127
128 while (1) {
129 if (count-- <= 0) {
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700130 _raw_yield();
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700131 count = spin_retry;
132 }
Christian Ehrhardt96567162006-03-09 17:33:49 -0800133 if (!__raw_write_can_lock(rw))
134 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700135 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
136 return;
137 }
138}
139EXPORT_SYMBOL(_raw_write_lock_wait);
140
Martin Schwidefsky3c1fcfe2006-09-30 23:27:45 -0700141int _raw_write_trylock_retry(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700142{
143 int count = spin_retry;
144
145 while (count-- > 0) {
Christian Ehrhardt96567162006-03-09 17:33:49 -0800146 if (!__raw_write_can_lock(rw))
147 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700148 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
149 return 1;
150 }
151 return 0;
152}
153EXPORT_SYMBOL(_raw_write_trylock_retry);