Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/s390/lib/spinlock.c |
| 3 | * Out of line spinlock code. |
| 4 | * |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame^] | 5 | * Copyright (C) IBM Corp. 2004, 2006 |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 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 | |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 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 | { |
Martin Schwidefsky | 347a8dc | 2006-01-06 00:19:28 -0800 | [diff] [blame] | 30 | #ifdef CONFIG_64BIT |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 31 | if (MACHINE_HAS_DIAG44) |
| 32 | #endif |
| 33 | asm volatile("diag 0,0,0x44"); |
| 34 | } |
| 35 | |
| 36 | void |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 37 | _raw_spin_lock_wait(raw_spinlock_t *lp, unsigned int pc) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 38 | { |
| 39 | int count = spin_retry; |
| 40 | |
| 41 | while (1) { |
| 42 | if (count-- <= 0) { |
| 43 | _diag44(); |
| 44 | count = spin_retry; |
| 45 | } |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame^] | 46 | if (__raw_spin_is_locked(lp)) |
| 47 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 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 |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 55 | _raw_spin_trylock_retry(raw_spinlock_t *lp, unsigned int pc) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 56 | { |
| 57 | int count = spin_retry; |
| 58 | |
| 59 | while (count-- > 0) { |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame^] | 60 | if (__raw_spin_is_locked(lp)) |
| 61 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 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 |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 70 | _raw_read_lock_wait(raw_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 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 | } |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame^] | 80 | if (!__raw_read_can_lock(rw)) |
| 81 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 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 |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 90 | _raw_read_trylock_retry(raw_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 91 | { |
| 92 | unsigned int old; |
| 93 | int count = spin_retry; |
| 94 | |
| 95 | while (count-- > 0) { |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame^] | 96 | if (!__raw_read_can_lock(rw)) |
| 97 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 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 |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 107 | _raw_write_lock_wait(raw_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 108 | { |
| 109 | int count = spin_retry; |
| 110 | |
| 111 | while (1) { |
| 112 | if (count-- <= 0) { |
| 113 | _diag44(); |
| 114 | count = spin_retry; |
| 115 | } |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame^] | 116 | if (!__raw_write_can_lock(rw)) |
| 117 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 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 |
Ingo Molnar | fb1c8f9 | 2005-09-10 00:25:56 -0700 | [diff] [blame] | 125 | _raw_write_trylock_retry(raw_rwlock_t *rw) |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 126 | { |
| 127 | int count = spin_retry; |
| 128 | |
| 129 | while (count-- > 0) { |
Christian Ehrhardt | 9656716 | 2006-03-09 17:33:49 -0800 | [diff] [blame^] | 130 | if (!__raw_write_can_lock(rw)) |
| 131 | continue; |
Martin Schwidefsky | 951f22d | 2005-07-27 11:44:57 -0700 | [diff] [blame] | 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); |