blob: e39846f30c093ceb422c4d23e2803ffb91c2b386 [file] [log] [blame]
Eric Holmbergd3511d62013-01-02 14:37:09 -07001/* Copyright (c) 2008-2009, 2011, 2013 The Linux Foundation.
2 * All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14#ifndef __LINUX_REMOTE_SPINLOCK_H
15#define __LINUX_REMOTE_SPINLOCK_H
16
17#include <linux/spinlock.h>
18#include <linux/mutex.h>
19
20#include <asm/remote_spinlock.h>
21
22/* Grabbing a local spin lock before going for a remote lock has several
23 * advantages:
24 * 1. Get calls to preempt enable/disable and IRQ save/restore for free.
25 * 2. For UP kernel, there is no overhead.
26 * 3. Reduces the possibility of executing the remote spin lock code. This is
27 * especially useful when the remote CPUs' mutual exclusion instructions
28 * don't work with the local CPUs' instructions. In such cases, one has to
29 * use software based mutex algorithms (e.g. Lamport's bakery algorithm)
30 * which could get expensive when the no. of contending CPUs is high.
31 * 4. In the case of software based mutex algorithm the exection time will be
32 * smaller since the no. of contending CPUs is reduced by having just one
33 * contender for all the local CPUs.
34 * 5. Get most of the spin lock debug features for free.
35 * 6. The code will continue to work "gracefully" even when the remote spin
36 * lock code is stubbed out for debug purposes or when there is no remote
37 * CPU in some board/machine types.
38 */
39typedef struct {
40 spinlock_t local;
41 _remote_spinlock_t remote;
42} remote_spinlock_t;
43
44#define remote_spin_lock_init(lock, id) \
45 ({ \
46 spin_lock_init(&((lock)->local)); \
47 _remote_spin_lock_init(id, &((lock)->remote)); \
48 })
49#define remote_spin_lock(lock) \
50 do { \
51 spin_lock(&((lock)->local)); \
52 _remote_spin_lock(&((lock)->remote)); \
53 } while (0)
54#define remote_spin_unlock(lock) \
55 do { \
56 _remote_spin_unlock(&((lock)->remote)); \
57 spin_unlock(&((lock)->local)); \
58 } while (0)
59#define remote_spin_lock_irqsave(lock, flags) \
60 do { \
61 spin_lock_irqsave(&((lock)->local), flags); \
62 _remote_spin_lock(&((lock)->remote)); \
63 } while (0)
64#define remote_spin_unlock_irqrestore(lock, flags) \
65 do { \
66 _remote_spin_unlock(&((lock)->remote)); \
67 spin_unlock_irqrestore(&((lock)->local), flags); \
68 } while (0)
69#define remote_spin_trylock(lock) \
70 ({ \
71 spin_trylock(&((lock)->local)) \
72 ? _remote_spin_trylock(&((lock)->remote)) \
73 ? 1 \
74 : ({ spin_unlock(&((lock)->local)); 0; }) \
75 : 0; \
76 })
77#define remote_spin_trylock_irqsave(lock, flags) \
78 ({ \
79 spin_trylock_irqsave(&((lock)->local), flags) \
80 ? _remote_spin_trylock(&((lock)->remote)) \
81 ? 1 \
82 : ({ spin_unlock_irqrestore(&((lock)->local), flags); \
83 0; }) \
84 : 0; \
85 })
86
Eric Holmbergf9cfa8e2011-09-23 14:29:11 -060087#define remote_spin_release(lock, pid) \
88 _remote_spin_release(&((lock)->remote), pid)
89
90#define remote_spin_release_all(pid) \
91 _remote_spin_release_all(pid)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092
Eric Holmbergd3511d62013-01-02 14:37:09 -070093#define remote_spin_owner(lock) \
94 _remote_spin_owner(&((lock)->remote))
95
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096typedef struct {
97 struct mutex local;
98 _remote_mutex_t remote;
99} remote_mutex_t;
100
101#define remote_mutex_init(lock, id) \
102 ({ \
103 mutex_init(&((lock)->local)); \
104 _remote_mutex_init(id, &((lock)->remote)); \
105 })
106#define remote_mutex_lock(lock) \
107 do { \
108 mutex_lock(&((lock)->local)); \
109 _remote_mutex_lock(&((lock)->remote)); \
110 } while (0)
111#define remote_mutex_trylock(lock) \
112 ({ \
113 mutex_trylock(&((lock)->local)) \
114 ? _remote_mutex_trylock(&((lock)->remote)) \
115 ? 1 \
116 : ({mutex_unlock(&((lock)->local)); 0; }) \
117 : 0; \
118 })
119#define remote_mutex_unlock(lock) \
120 do { \
121 _remote_mutex_unlock(&((lock)->remote)); \
122 mutex_unlock(&((lock)->local)); \
123 } while (0)
124
125#endif