xref: /openbmc/linux/arch/s390/lib/spinlock.c (revision 87c2ce3b)
1 /*
2  *  arch/s390/lib/spinlock.c
3  *    Out of line spinlock code.
4  *
5  *  S390 version
6  *    Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
8  */
9 
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/spinlock.h>
13 #include <linux/init.h>
14 #include <asm/io.h>
15 
16 atomic_t spin_retry_counter;
17 int spin_retry = 1000;
18 
19 /**
20  * spin_retry= parameter
21  */
22 static int __init spin_retry_setup(char *str)
23 {
24 	spin_retry = simple_strtoul(str, &str, 0);
25 	return 1;
26 }
27 __setup("spin_retry=", spin_retry_setup);
28 
29 static inline void
30 _diag44(void)
31 {
32 #ifdef CONFIG_64BIT
33 	if (MACHINE_HAS_DIAG44)
34 #endif
35 		asm volatile("diag 0,0,0x44");
36 }
37 
38 void
39 _raw_spin_lock_wait(raw_spinlock_t *lp, unsigned int pc)
40 {
41 	int count = spin_retry;
42 
43 	while (1) {
44 		if (count-- <= 0) {
45 			_diag44();
46 			count = spin_retry;
47 		}
48 		atomic_inc(&spin_retry_counter);
49 		if (_raw_compare_and_swap(&lp->lock, 0, pc) == 0)
50 			return;
51 	}
52 }
53 EXPORT_SYMBOL(_raw_spin_lock_wait);
54 
55 int
56 _raw_spin_trylock_retry(raw_spinlock_t *lp, unsigned int pc)
57 {
58 	int count = spin_retry;
59 
60 	while (count-- > 0) {
61 		atomic_inc(&spin_retry_counter);
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 		atomic_inc(&spin_retry_counter);
81 		old = rw->lock & 0x7fffffffU;
82 		if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
83 			return;
84 	}
85 }
86 EXPORT_SYMBOL(_raw_read_lock_wait);
87 
88 int
89 _raw_read_trylock_retry(raw_rwlock_t *rw)
90 {
91 	unsigned int old;
92 	int count = spin_retry;
93 
94 	while (count-- > 0) {
95 		atomic_inc(&spin_retry_counter);
96 		old = rw->lock & 0x7fffffffU;
97 		if (_raw_compare_and_swap(&rw->lock, old, old + 1) == old)
98 			return 1;
99 	}
100 	return 0;
101 }
102 EXPORT_SYMBOL(_raw_read_trylock_retry);
103 
104 void
105 _raw_write_lock_wait(raw_rwlock_t *rw)
106 {
107 	int count = spin_retry;
108 
109 	while (1) {
110 		if (count-- <= 0) {
111 			_diag44();
112 			count = spin_retry;
113 		}
114 		atomic_inc(&spin_retry_counter);
115 		if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
116 			return;
117 	}
118 }
119 EXPORT_SYMBOL(_raw_write_lock_wait);
120 
121 int
122 _raw_write_trylock_retry(raw_rwlock_t *rw)
123 {
124 	int count = spin_retry;
125 
126 	while (count-- > 0) {
127 		atomic_inc(&spin_retry_counter);
128 		if (_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)
129 			return 1;
130 	}
131 	return 0;
132 }
133 EXPORT_SYMBOL(_raw_write_trylock_retry);
134