Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Assembly implementation of the mutex fastpath, based on atomic |
| 3 | * decrement/increment. |
| 4 | * |
| 5 | * started by Ingo Molnar: |
| 6 | * |
| 7 | * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
| 8 | */ |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 9 | #ifndef _ASM_X86_MUTEX_32_H |
| 10 | #define _ASM_X86_MUTEX_32_H |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 11 | |
Joe Perches | 16281a9 | 2008-03-04 16:46:27 -0800 | [diff] [blame] | 12 | #include <asm/alternative.h> |
Gerd Hoffmann | 9a0b581 | 2006-03-23 02:59:32 -0800 | [diff] [blame] | 13 | |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 14 | /** |
| 15 | * __mutex_fastpath_lock - try to take the lock by moving the count |
| 16 | * from 1 to a 0 value |
| 17 | * @count: pointer of type atomic_t |
| 18 | * @fn: function to call if the original value was not 1 |
| 19 | * |
| 20 | * Change the count from 1 to a value lower than 1, and call <fn> if it |
| 21 | * wasn't 1 originally. This function MUST leave the value lower than 1 |
| 22 | * even when the "1" assertion wasn't true. |
| 23 | */ |
Joe Perches | b2347fa | 2008-03-23 01:02:53 -0700 | [diff] [blame] | 24 | #define __mutex_fastpath_lock(count, fail_fn) \ |
| 25 | do { \ |
| 26 | unsigned int dummy; \ |
| 27 | \ |
| 28 | typecheck(atomic_t *, count); \ |
Harvey Harrison | 341d885 | 2008-01-30 13:31:17 +0100 | [diff] [blame] | 29 | typecheck_fn(void (*)(atomic_t *), fail_fn); \ |
Joe Perches | b2347fa | 2008-03-23 01:02:53 -0700 | [diff] [blame] | 30 | \ |
| 31 | asm volatile(LOCK_PREFIX " decl (%%eax)\n" \ |
| 32 | " jns 1f \n" \ |
| 33 | " call " #fail_fn "\n" \ |
| 34 | "1:\n" \ |
| 35 | : "=a" (dummy) \ |
| 36 | : "a" (count) \ |
| 37 | : "memory", "ecx", "edx"); \ |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 38 | } while (0) |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * __mutex_fastpath_lock_retval - try to take the lock by moving the count |
| 43 | * from 1 to a 0 value |
| 44 | * @count: pointer of type atomic_t |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 45 | * |
Maarten Lankhorst | a41b56e | 2013-06-20 13:31:05 +0200 | [diff] [blame] | 46 | * Change the count from 1 to a value lower than 1. This function returns 0 |
| 47 | * if the fastpath succeeds, or -1 otherwise. |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 48 | */ |
Maarten Lankhorst | a41b56e | 2013-06-20 13:31:05 +0200 | [diff] [blame] | 49 | static inline int __mutex_fastpath_lock_retval(atomic_t *count) |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 50 | { |
| 51 | if (unlikely(atomic_dec_return(count) < 0)) |
Maarten Lankhorst | a41b56e | 2013-06-20 13:31:05 +0200 | [diff] [blame] | 52 | return -1; |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 53 | else |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * __mutex_fastpath_unlock - try to promote the mutex from 0 to 1 |
| 59 | * @count: pointer of type atomic_t |
| 60 | * @fail_fn: function to call if the original value was not 0 |
| 61 | * |
| 62 | * try to promote the mutex from 0 to 1. if it wasn't 0, call <fail_fn>. |
| 63 | * In the failure case, this function is allowed to either set the value |
| 64 | * to 1, or to set it to a value lower than 1. |
| 65 | * |
| 66 | * If the implementation sets it to a value of lower than 1, the |
| 67 | * __mutex_slowpath_needs_to_unlock() macro needs to return 1, it needs |
| 68 | * to return 0 otherwise. |
| 69 | */ |
Joe Perches | b2347fa | 2008-03-23 01:02:53 -0700 | [diff] [blame] | 70 | #define __mutex_fastpath_unlock(count, fail_fn) \ |
| 71 | do { \ |
| 72 | unsigned int dummy; \ |
| 73 | \ |
| 74 | typecheck(atomic_t *, count); \ |
Harvey Harrison | 341d885 | 2008-01-30 13:31:17 +0100 | [diff] [blame] | 75 | typecheck_fn(void (*)(atomic_t *), fail_fn); \ |
Joe Perches | b2347fa | 2008-03-23 01:02:53 -0700 | [diff] [blame] | 76 | \ |
| 77 | asm volatile(LOCK_PREFIX " incl (%%eax)\n" \ |
| 78 | " jg 1f\n" \ |
| 79 | " call " #fail_fn "\n" \ |
| 80 | "1:\n" \ |
| 81 | : "=a" (dummy) \ |
| 82 | : "a" (count) \ |
| 83 | : "memory", "ecx", "edx"); \ |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 84 | } while (0) |
| 85 | |
| 86 | #define __mutex_slowpath_needs_to_unlock() 1 |
| 87 | |
| 88 | /** |
| 89 | * __mutex_fastpath_trylock - try to acquire the mutex, without waiting |
| 90 | * |
| 91 | * @count: pointer of type atomic_t |
| 92 | * @fail_fn: fallback function |
| 93 | * |
| 94 | * Change the count from 1 to a value lower than 1, and return 0 (failure) |
| 95 | * if it wasn't 1 originally, or return 1 (success) otherwise. This function |
| 96 | * MUST leave the value lower than 1 even when the "1" assertion wasn't true. |
| 97 | * Additionally, if the value was < 0 originally, this function must not leave |
| 98 | * it to 0 on failure. |
| 99 | */ |
Joe Perches | b2347fa | 2008-03-23 01:02:53 -0700 | [diff] [blame] | 100 | static inline int __mutex_fastpath_trylock(atomic_t *count, |
| 101 | int (*fail_fn)(atomic_t *)) |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 102 | { |
Borislav Petkov | b08ee5f | 2014-07-11 12:43:38 +0200 | [diff] [blame] | 103 | /* cmpxchg because it never induces a false contention state. */ |
Linus Torvalds | 4cec873 | 2006-01-11 15:50:47 -0800 | [diff] [blame] | 104 | if (likely(atomic_cmpxchg(count, 1, 0) == 1)) |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 105 | return 1; |
Borislav Petkov | b08ee5f | 2014-07-11 12:43:38 +0200 | [diff] [blame] | 106 | |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 107 | return 0; |
Arjan van de Ven | 2af7f59 | 2006-01-09 15:59:18 -0800 | [diff] [blame] | 108 | } |
| 109 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 110 | #endif /* _ASM_X86_MUTEX_32_H */ |