xref: /linux/arch/s390/lib/spinlock.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
1 /*
2  *  arch/s390/lib/spinlock.c
3  *    Out of line spinlock code.
4  *
5  *    Copyright (C) IBM Corp. 2004, 2006
6  *    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 
15 int spin_retry = 1000;
16 
17 /**
18  * spin_retry= parameter
19  */
20 static 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 
27 static inline void
28 _diag44(void)
29 {
30 #ifdef CONFIG_64BIT
31 	if (MACHINE_HAS_DIAG44)
32 #endif
33 		asm volatile("diag 0,0,0x44");
34 }
35 
36 void
37 _raw_spin_lock_wait(raw_spinlock_t *lp, unsigned int pc)
38 {
39 	int count = spin_retry;
40 
41 	while (1) {
42 		if (count-- <= 0) {
43 			_diag44();
44 			count = spin_retry;
45 		}
46 		if (__raw_spin_is_locked(lp))
47 			continue;
48 		if (_raw_compare_and_swap(&lp->lock, 0, pc) == 0)
49 			return;
50 	}
51 }
52 EXPORT_SYMBOL(_raw_spin_lock_wait);
53 
54 int
55 _raw_spin_trylock_retry(raw_spinlock_t *lp, unsigned int pc)
56 {
57 	int count = spin_retry;
58 
59 	while (count-- > 0) {
60 		if (__raw_spin_is_locked(lp))
61 			continue;
62 		if (_raw_compare_and_swap(&lp->lock, 0, pc) == 0)
63 			return 1;
64 	}
65 	return 0;
66 }
67 EXPORT_SYMBOL(_raw_spin_trylock_retry);
68 
69 void
70 _raw_read_lock_wait(raw_rwlock_t *rw)
71 {
72 	unsigned int old;
73 	int count = spin_retry;
74 
75 	while (1) {
76 		if (count-- <= 0) {
77 			_diag44();
78 			count = spin_retry;
79 		}
80 		if (!__raw_read_can_lock(rw))
81 			continue;
82 		old = rw->lock & 0x7fffffffU;
83 		if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
84 			return;
85 	}
86 }
87 EXPORT_SYMBOL(_raw_read_lock_wait);
88 
89 int
90 _raw_read_trylock_retry(raw_rwlock_t *rw)
91 {
92 	unsigned int old;
93 	int count = spin_retry;
94 
95 	while (count-- > 0) {
96 		if (!__raw_read_can_lock(rw))
97 			continue;
98 		old = rw->lock & 0x7fffffffU;
99 		if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
100 			return 1;
101 	}
102 	return 0;
103 }
104 EXPORT_SYMBOL(_raw_read_trylock_retry);
105 
106 void
107 _raw_write_lock_wait(raw_rwlock_t *rw)
108 {
109 	int count = spin_retry;
110 
111 	while (1) {
112 		if (count-- <= 0) {
113 			_diag44();
114 			count = spin_retry;
115 		}
116 		if (!__raw_write_can_lock(rw))
117 			continue;
118 		if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
119 			return;
120 	}
121 }
122 EXPORT_SYMBOL(_raw_write_lock_wait);
123 
124 int
125 _raw_write_trylock_retry(raw_rwlock_t *rw)
126 {
127 	int count = spin_retry;
128 
129 	while (count-- > 0) {
130 		if (!__raw_write_can_lock(rw))
131 			continue;
132 		if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
133 			return 1;
134 	}
135 	return 0;
136 }
137 EXPORT_SYMBOL(_raw_write_trylock_retry);
138