David S. Miller | 24f287e | 2007-10-15 16:41:44 -0700 | [diff] [blame] | 1 | /* atomic.S: These things are too big to do inline. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David S. Miller | 193d2aa | 2012-11-09 19:37:59 -0800 | [diff] [blame] | 3 | * Copyright (C) 1999, 2007 2012 David S. Miller (davem@davemloft.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
David S. Miller | 8695c37 | 2012-05-11 20:33:22 -0700 | [diff] [blame] | 6 | #include <linux/linkage.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <asm/asi.h> |
David S. Miller | 24f287e | 2007-10-15 16:41:44 -0700 | [diff] [blame] | 8 | #include <asm/backoff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | .text |
| 11 | |
| 12 | /* Two versions of the atomic routines, one that |
| 13 | * does not return a value and does not perform |
| 14 | * memory barriers, and a second which returns |
| 15 | * a value and does the barriers. |
| 16 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 18 | #define ATOMIC_OP(op) \ |
| 19 | ENTRY(atomic_##op) /* %o0 = increment, %o1 = atomic_ptr */ \ |
| 20 | BACKOFF_SETUP(%o2); \ |
| 21 | 1: lduw [%o1], %g1; \ |
| 22 | op %g1, %o0, %g7; \ |
| 23 | cas [%o1], %g1, %g7; \ |
| 24 | cmp %g1, %g7; \ |
| 25 | bne,pn %icc, BACKOFF_LABEL(2f, 1b); \ |
| 26 | nop; \ |
| 27 | retl; \ |
| 28 | nop; \ |
| 29 | 2: BACKOFF_SPIN(%o2, %o3, 1b); \ |
| 30 | ENDPROC(atomic_##op); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 32 | #define ATOMIC_OP_RETURN(op) \ |
| 33 | ENTRY(atomic_##op##_return) /* %o0 = increment, %o1 = atomic_ptr */ \ |
| 34 | BACKOFF_SETUP(%o2); \ |
| 35 | 1: lduw [%o1], %g1; \ |
| 36 | op %g1, %o0, %g7; \ |
| 37 | cas [%o1], %g1, %g7; \ |
| 38 | cmp %g1, %g7; \ |
| 39 | bne,pn %icc, BACKOFF_LABEL(2f, 1b); \ |
Peter Zijlstra | caa17d4 | 2014-09-02 11:40:16 +0200 | [diff] [blame] | 40 | op %g1, %o0, %g1; \ |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 41 | retl; \ |
| 42 | sra %g1, 0, %o0; \ |
| 43 | 2: BACKOFF_SPIN(%o2, %o3, 1b); \ |
| 44 | ENDPROC(atomic_##op##_return); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 46 | #define ATOMIC_OPS(op) ATOMIC_OP(op) ATOMIC_OP_RETURN(op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 48 | ATOMIC_OPS(add) |
| 49 | ATOMIC_OPS(sub) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 51 | #undef ATOMIC_OPS |
| 52 | #undef ATOMIC_OP_RETURN |
| 53 | #undef ATOMIC_OP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 55 | #define ATOMIC64_OP(op) \ |
| 56 | ENTRY(atomic64_##op) /* %o0 = increment, %o1 = atomic_ptr */ \ |
| 57 | BACKOFF_SETUP(%o2); \ |
| 58 | 1: ldx [%o1], %g1; \ |
| 59 | op %g1, %o0, %g7; \ |
| 60 | casx [%o1], %g1, %g7; \ |
| 61 | cmp %g1, %g7; \ |
| 62 | bne,pn %xcc, BACKOFF_LABEL(2f, 1b); \ |
| 63 | nop; \ |
| 64 | retl; \ |
| 65 | nop; \ |
| 66 | 2: BACKOFF_SPIN(%o2, %o3, 1b); \ |
| 67 | ENDPROC(atomic64_##op); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 69 | #define ATOMIC64_OP_RETURN(op) \ |
| 70 | ENTRY(atomic64_##op##_return) /* %o0 = increment, %o1 = atomic_ptr */ \ |
| 71 | BACKOFF_SETUP(%o2); \ |
| 72 | 1: ldx [%o1], %g1; \ |
| 73 | op %g1, %o0, %g7; \ |
| 74 | casx [%o1], %g1, %g7; \ |
| 75 | cmp %g1, %g7; \ |
| 76 | bne,pn %xcc, BACKOFF_LABEL(2f, 1b); \ |
| 77 | nop; \ |
| 78 | retl; \ |
Peter Zijlstra | caa17d4 | 2014-09-02 11:40:16 +0200 | [diff] [blame] | 79 | op %g1, %o0, %o0; \ |
Peter Zijlstra | 4f3316c | 2014-03-26 18:29:28 +0100 | [diff] [blame] | 80 | 2: BACKOFF_SPIN(%o2, %o3, 1b); \ |
| 81 | ENDPROC(atomic64_##op##_return); |
| 82 | |
| 83 | #define ATOMIC64_OPS(op) ATOMIC64_OP(op) ATOMIC64_OP_RETURN(op) |
| 84 | |
| 85 | ATOMIC64_OPS(add) |
| 86 | ATOMIC64_OPS(sub) |
| 87 | |
| 88 | #undef ATOMIC64_OPS |
| 89 | #undef ATOMIC64_OP_RETURN |
| 90 | #undef ATOMIC64_OP |
David S. Miller | 193d2aa | 2012-11-09 19:37:59 -0800 | [diff] [blame] | 91 | |
| 92 | ENTRY(atomic64_dec_if_positive) /* %o0 = atomic_ptr */ |
| 93 | BACKOFF_SETUP(%o2) |
| 94 | 1: ldx [%o0], %g1 |
| 95 | brlez,pn %g1, 3f |
| 96 | sub %g1, 1, %g7 |
| 97 | casx [%o0], %g1, %g7 |
| 98 | cmp %g1, %g7 |
| 99 | bne,pn %xcc, BACKOFF_LABEL(2f, 1b) |
| 100 | nop |
| 101 | 3: retl |
| 102 | sub %g1, 1, %o0 |
| 103 | 2: BACKOFF_SPIN(%o2, %o3, 1b) |
| 104 | ENDPROC(atomic64_dec_if_positive) |