blob: b9b7958a226a75a5787129404b1eb525c95832b4 [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
27static inline void
28_diag44(void)
29{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -080030#ifdef CONFIG_64BIT
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070031 if (MACHINE_HAS_DIAG44)
32#endif
33 asm volatile("diag 0,0,0x44");
34}
35
36void
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070037_raw_spin_lock_wait(raw_spinlock_t *lp, unsigned int pc)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070038{
39 int count = spin_retry;
40
41 while (1) {
42 if (count-- <= 0) {
43 _diag44();
44 count = spin_retry;
45 }
Christian Ehrhardt96567162006-03-09 17:33:49 -080046 if (__raw_spin_is_locked(lp))
47 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070048 if (_raw_compare_and_swap(&lp->lock, 0, pc) == 0)
49 return;
50 }
51}
52EXPORT_SYMBOL(_raw_spin_lock_wait);
53
54int
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070055_raw_spin_trylock_retry(raw_spinlock_t *lp, unsigned int pc)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070056{
57 int count = spin_retry;
58
59 while (count-- > 0) {
Christian Ehrhardt96567162006-03-09 17:33:49 -080060 if (__raw_spin_is_locked(lp))
61 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070062 if (_raw_compare_and_swap(&lp->lock, 0, pc) == 0)
63 return 1;
64 }
65 return 0;
66}
67EXPORT_SYMBOL(_raw_spin_trylock_retry);
68
69void
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070070_raw_read_lock_wait(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070071{
72 unsigned int old;
73 int count = spin_retry;
74
75 while (1) {
76 if (count-- <= 0) {
77 _diag44();
78 count = spin_retry;
79 }
Christian Ehrhardt96567162006-03-09 17:33:49 -080080 if (!__raw_read_can_lock(rw))
81 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070082 old = rw->lock & 0x7fffffffU;
83 if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
84 return;
85 }
86}
87EXPORT_SYMBOL(_raw_read_lock_wait);
88
89int
Ingo Molnarfb1c8f92005-09-10 00:25:56 -070090_raw_read_trylock_retry(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070091{
92 unsigned int old;
93 int count = spin_retry;
94
95 while (count-- > 0) {
Christian Ehrhardt96567162006-03-09 17:33:49 -080096 if (!__raw_read_can_lock(rw))
97 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -070098 old = rw->lock & 0x7fffffffU;
99 if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
100 return 1;
101 }
102 return 0;
103}
104EXPORT_SYMBOL(_raw_read_trylock_retry);
105
106void
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700107_raw_write_lock_wait(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700108{
109 int count = spin_retry;
110
111 while (1) {
112 if (count-- <= 0) {
113 _diag44();
114 count = spin_retry;
115 }
Christian Ehrhardt96567162006-03-09 17:33:49 -0800116 if (!__raw_write_can_lock(rw))
117 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700118 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
119 return;
120 }
121}
122EXPORT_SYMBOL(_raw_write_lock_wait);
123
124int
Ingo Molnarfb1c8f92005-09-10 00:25:56 -0700125_raw_write_trylock_retry(raw_rwlock_t *rw)
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700126{
127 int count = spin_retry;
128
129 while (count-- > 0) {
Christian Ehrhardt96567162006-03-09 17:33:49 -0800130 if (!__raw_write_can_lock(rw))
131 continue;
Martin Schwidefsky951f22d2005-07-27 11:44:57 -0700132 if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
133 return 1;
134 }
135 return 0;
136}
137EXPORT_SYMBOL(_raw_write_trylock_retry);