Chris Lew | 63b2d6b | 2016-08-01 10:59:49 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2008-2009, 2011, 2013-2015 The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | #ifndef __LINUX_REMOTE_SPINLOCK_H |
| 14 | #define __LINUX_REMOTE_SPINLOCK_H |
| 15 | |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/msm_remote_spinlock.h> |
| 18 | |
| 19 | /* Grabbing a local spin lock before going for a remote lock has several |
| 20 | * advantages: |
| 21 | * 1. Get calls to preempt enable/disable and IRQ save/restore for free. |
| 22 | * 2. For UP kernel, there is no overhead. |
| 23 | * 3. Reduces the possibility of executing the remote spin lock code. This is |
| 24 | * especially useful when the remote CPUs' mutual exclusion instructions |
| 25 | * don't work with the local CPUs' instructions. In such cases, one has to |
| 26 | * use software based mutex algorithms (e.g. Lamport's bakery algorithm) |
| 27 | * which could get expensive when the no. of contending CPUs is high. |
| 28 | * 4. In the case of software based mutex algorithm the exection time will be |
| 29 | * smaller since the no. of contending CPUs is reduced by having just one |
| 30 | * contender for all the local CPUs. |
| 31 | * 5. Get most of the spin lock debug features for free. |
| 32 | * 6. The code will continue to work "gracefully" even when the remote spin |
| 33 | * lock code is stubbed out for debug purposes or when there is no remote |
| 34 | * CPU in some board/machine types. |
| 35 | */ |
| 36 | typedef struct { |
| 37 | spinlock_t local; |
| 38 | _remote_spinlock_t remote; |
| 39 | } remote_spinlock_t; |
| 40 | |
| 41 | #define remote_spin_lock_init(lock, id) \ |
| 42 | ({ \ |
| 43 | spin_lock_init(&((lock)->local)); \ |
| 44 | _remote_spin_lock_init(id, &((lock)->remote)); \ |
| 45 | }) |
| 46 | #define remote_spin_lock(lock) \ |
| 47 | do { \ |
| 48 | spin_lock(&((lock)->local)); \ |
| 49 | _remote_spin_lock(&((lock)->remote)); \ |
| 50 | } while (0) |
| 51 | #define remote_spin_unlock(lock) \ |
| 52 | do { \ |
| 53 | _remote_spin_unlock(&((lock)->remote)); \ |
| 54 | spin_unlock(&((lock)->local)); \ |
| 55 | } while (0) |
| 56 | #define remote_spin_lock_irqsave(lock, flags) \ |
| 57 | do { \ |
| 58 | spin_lock_irqsave(&((lock)->local), flags); \ |
| 59 | _remote_spin_lock(&((lock)->remote)); \ |
| 60 | } while (0) |
| 61 | #define remote_spin_unlock_irqrestore(lock, flags) \ |
| 62 | do { \ |
| 63 | _remote_spin_unlock(&((lock)->remote)); \ |
| 64 | spin_unlock_irqrestore(&((lock)->local), flags); \ |
| 65 | } while (0) |
| 66 | #define remote_spin_trylock(lock) \ |
| 67 | ({ \ |
| 68 | spin_trylock(&((lock)->local)) \ |
| 69 | ? _remote_spin_trylock(&((lock)->remote)) \ |
| 70 | ? 1 \ |
| 71 | : ({ spin_unlock(&((lock)->local)); 0; }) \ |
| 72 | : 0; \ |
| 73 | }) |
| 74 | #define remote_spin_trylock_irqsave(lock, flags) \ |
| 75 | ({ \ |
| 76 | spin_trylock_irqsave(&((lock)->local), flags) \ |
| 77 | ? _remote_spin_trylock(&((lock)->remote)) \ |
| 78 | ? 1 \ |
| 79 | : ({ spin_unlock_irqrestore(&((lock)->local), flags); \ |
| 80 | 0; }) \ |
| 81 | : 0; \ |
| 82 | }) |
| 83 | #define remote_spin_lock_rlock_id(lock, tid) \ |
| 84 | _remote_spin_lock_rlock_id(&((lock)->remote), tid) |
| 85 | |
| 86 | #define remote_spin_unlock_rlock(lock) \ |
| 87 | _remote_spin_unlock_rlock(&((lock)->remote)) |
| 88 | |
| 89 | #define remote_spin_release(lock, pid) \ |
| 90 | _remote_spin_release(&((lock)->remote), pid) |
| 91 | |
| 92 | #define remote_spin_release_all(pid) \ |
| 93 | _remote_spin_release_all(pid) |
| 94 | |
| 95 | #define remote_spin_owner(lock) \ |
| 96 | _remote_spin_owner(&((lock)->remote)) |
| 97 | |
| 98 | #define remote_spin_get_hw_spinlocks_element(lock) \ |
| 99 | _remote_spin_get_hw_spinlocks_element(&((lock)->remote)) |
| 100 | #endif |