blob: 2b6f389a3133a7376c4d741594e0b6634707f561 [file] [log] [blame]
Suman Annaeebba712018-05-11 12:03:16 -05001/* SPDX-License-Identifier: GPL-2.0 */
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -08002/*
3 * Hardware spinlock public header
4 *
5 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
6 *
7 * Contact: Ohad Ben-Cohen <ohad@wizery.com>
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -08008 */
9
10#ifndef __LINUX_HWSPINLOCK_H
11#define __LINUX_HWSPINLOCK_H
12
13#include <linux/err.h>
14#include <linux/sched.h>
15
16/* hwspinlock mode argument */
17#define HWLOCK_IRQSTATE 0x01 /* Disable interrupts, save state */
18#define HWLOCK_IRQ 0x02 /* Disable interrupts, don't save state */
Baolin Wang1e6c06a2018-04-08 11:06:57 +080019#define HWLOCK_RAW 0x03
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080020
Paul Gortmaker313162d2012-01-30 11:46:54 -050021struct device;
Suman Annafb7737e2015-03-04 20:01:14 -060022struct device_node;
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080023struct hwspinlock;
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +030024struct hwspinlock_device;
25struct hwspinlock_ops;
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080026
Ohad Ben-Cohenc3c12502011-09-05 23:15:06 +030027/**
28 * struct hwspinlock_pdata - platform data for hwspinlock drivers
29 * @base_id: base id for this hwspinlock device
30 *
31 * hwspinlock devices provide system-wide hardware locks that are used
32 * by remote processors that have no other way to achieve synchronization.
33 *
34 * To achieve that, each physical lock must have a system-wide id number
35 * that is agreed upon, otherwise remote processors can't possibly assume
36 * they're using the same hardware lock.
37 *
38 * Usually boards have a single hwspinlock device, which provides several
39 * hwspinlocks, and in this case, they can be trivially numbered 0 to
40 * (num-of-locks - 1).
41 *
42 * In case boards have several hwspinlocks devices, a different base id
43 * should be used for each hwspinlock device (they can't all use 0 as
44 * a starting id!).
45 *
46 * This platform data structure should be used to provide the base id
47 * for each device (which is trivially 0 when only a single hwspinlock
48 * device exists). It can be shared between different platforms, hence
49 * its location.
50 */
51struct hwspinlock_pdata {
52 int base_id;
53};
54
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080055#if defined(CONFIG_HWSPINLOCK) || defined(CONFIG_HWSPINLOCK_MODULE)
56
Ohad Ben-Cohen300bab92011-09-06 15:39:21 +030057int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev,
58 const struct hwspinlock_ops *ops, int base_id, int num_locks);
59int hwspin_lock_unregister(struct hwspinlock_device *bank);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080060struct hwspinlock *hwspin_lock_request(void);
61struct hwspinlock *hwspin_lock_request_specific(unsigned int id);
62int hwspin_lock_free(struct hwspinlock *hwlock);
Suman Annafb7737e2015-03-04 20:01:14 -060063int of_hwspin_lock_get_id(struct device_node *np, int index);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080064int hwspin_lock_get_id(struct hwspinlock *hwlock);
65int __hwspin_lock_timeout(struct hwspinlock *, unsigned int, int,
66 unsigned long *);
67int __hwspin_trylock(struct hwspinlock *, int, unsigned long *);
68void __hwspin_unlock(struct hwspinlock *, int, unsigned long *);
Baolin Wang5560f702018-06-22 16:08:58 +080069int of_hwspin_lock_get_id_byname(struct device_node *np, const char *name);
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -080070
71#else /* !CONFIG_HWSPINLOCK */
72
73/*
74 * We don't want these functions to fail if CONFIG_HWSPINLOCK is not
75 * enabled. We prefer to silently succeed in this case, and let the
76 * code path get compiled away. This way, if CONFIG_HWSPINLOCK is not
77 * required on a given setup, users will still work.
78 *
79 * The only exception is hwspin_lock_register/hwspin_lock_unregister, with which
80 * we _do_ want users to fail (no point in registering hwspinlock instances if
81 * the framework is not available).
82 *
83 * Note: ERR_PTR(-ENODEV) will still be considered a success for NULL-checking
84 * users. Others, which care, can still check this with IS_ERR.
85 */
86static inline struct hwspinlock *hwspin_lock_request(void)
87{
88 return ERR_PTR(-ENODEV);
89}
90
91static inline struct hwspinlock *hwspin_lock_request_specific(unsigned int id)
92{
93 return ERR_PTR(-ENODEV);
94}
95
96static inline int hwspin_lock_free(struct hwspinlock *hwlock)
97{
98 return 0;
99}
100
101static inline
102int __hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to,
103 int mode, unsigned long *flags)
104{
105 return 0;
106}
107
108static inline
109int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
110{
111 return 0;
112}
113
114static inline
115void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags)
116{
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800117}
118
Suman Annafb7737e2015-03-04 20:01:14 -0600119static inline int of_hwspin_lock_get_id(struct device_node *np, int index)
120{
121 return 0;
122}
123
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800124static inline int hwspin_lock_get_id(struct hwspinlock *hwlock)
125{
126 return 0;
127}
128
Baolin Wang5560f702018-06-22 16:08:58 +0800129static inline
130int of_hwspin_lock_get_id_byname(struct device_node *np, const char *name)
131{
132 return 0;
133}
134
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800135#endif /* !CONFIG_HWSPINLOCK */
136
137/**
138 * hwspin_trylock_irqsave() - try to lock an hwspinlock, disable interrupts
139 * @hwlock: an hwspinlock which we want to trylock
140 * @flags: a pointer to where the caller's interrupt state will be saved at
141 *
142 * This function attempts to lock the underlying hwspinlock, and will
143 * immediately fail if the hwspinlock is already locked.
144 *
145 * Upon a successful return from this function, preemption and local
146 * interrupts are disabled (previous interrupts state is saved at @flags),
147 * so the caller must not sleep, and is advised to release the hwspinlock
148 * as soon as possible.
149 *
150 * Returns 0 if we successfully locked the hwspinlock, -EBUSY if
151 * the hwspinlock was already taken, and -EINVAL if @hwlock is invalid.
152 */
153static inline
154int hwspin_trylock_irqsave(struct hwspinlock *hwlock, unsigned long *flags)
155{
156 return __hwspin_trylock(hwlock, HWLOCK_IRQSTATE, flags);
157}
158
159/**
160 * hwspin_trylock_irq() - try to lock an hwspinlock, disable interrupts
161 * @hwlock: an hwspinlock which we want to trylock
162 *
163 * This function attempts to lock the underlying hwspinlock, and will
164 * immediately fail if the hwspinlock is already locked.
165 *
166 * Upon a successful return from this function, preemption and local
167 * interrupts are disabled, so the caller must not sleep, and is advised
168 * to release the hwspinlock as soon as possible.
169 *
170 * Returns 0 if we successfully locked the hwspinlock, -EBUSY if
171 * the hwspinlock was already taken, and -EINVAL if @hwlock is invalid.
172 */
173static inline int hwspin_trylock_irq(struct hwspinlock *hwlock)
174{
175 return __hwspin_trylock(hwlock, HWLOCK_IRQ, NULL);
176}
177
178/**
Baolin Wang1e6c06a2018-04-08 11:06:57 +0800179 * hwspin_trylock_raw() - attempt to lock a specific hwspinlock
180 * @hwlock: an hwspinlock which we want to trylock
181 *
182 * This function attempts to lock an hwspinlock, and will immediately fail
183 * if the hwspinlock is already taken.
184 *
185 * Caution: User must protect the routine of getting hardware lock with mutex
186 * or spinlock to avoid dead-lock, that will let user can do some time-consuming
187 * or sleepable operations under the hardware lock.
188 *
189 * Returns 0 if we successfully locked the hwspinlock, -EBUSY if
190 * the hwspinlock was already taken, and -EINVAL if @hwlock is invalid.
191 */
192static inline int hwspin_trylock_raw(struct hwspinlock *hwlock)
193{
194 return __hwspin_trylock(hwlock, HWLOCK_RAW, NULL);
195}
196
197/**
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800198 * hwspin_trylock() - attempt to lock a specific hwspinlock
199 * @hwlock: an hwspinlock which we want to trylock
200 *
201 * This function attempts to lock an hwspinlock, and will immediately fail
202 * if the hwspinlock is already taken.
203 *
204 * Upon a successful return from this function, preemption is disabled,
205 * so the caller must not sleep, and is advised to release the hwspinlock
206 * as soon as possible. This is required in order to minimize remote cores
207 * polling on the hardware interconnect.
208 *
209 * Returns 0 if we successfully locked the hwspinlock, -EBUSY if
210 * the hwspinlock was already taken, and -EINVAL if @hwlock is invalid.
211 */
212static inline int hwspin_trylock(struct hwspinlock *hwlock)
213{
214 return __hwspin_trylock(hwlock, 0, NULL);
215}
216
217/**
218 * hwspin_lock_timeout_irqsave() - lock hwspinlock, with timeout, disable irqs
219 * @hwlock: the hwspinlock to be locked
220 * @to: timeout value in msecs
221 * @flags: a pointer to where the caller's interrupt state will be saved at
222 *
223 * This function locks the underlying @hwlock. If the @hwlock
224 * is already taken, the function will busy loop waiting for it to
225 * be released, but give up when @timeout msecs have elapsed.
226 *
227 * Upon a successful return from this function, preemption and local interrupts
228 * are disabled (plus previous interrupt state is saved), so the caller must
229 * not sleep, and is advised to release the hwspinlock as soon as possible.
230 *
231 * Returns 0 when the @hwlock was successfully taken, and an appropriate
232 * error code otherwise (most notably an -ETIMEDOUT if the @hwlock is still
233 * busy after @timeout msecs). The function will never sleep.
234 */
235static inline int hwspin_lock_timeout_irqsave(struct hwspinlock *hwlock,
236 unsigned int to, unsigned long *flags)
237{
238 return __hwspin_lock_timeout(hwlock, to, HWLOCK_IRQSTATE, flags);
239}
240
241/**
242 * hwspin_lock_timeout_irq() - lock hwspinlock, with timeout, disable irqs
243 * @hwlock: the hwspinlock to be locked
244 * @to: timeout value in msecs
245 *
246 * This function locks the underlying @hwlock. If the @hwlock
247 * is already taken, the function will busy loop waiting for it to
248 * be released, but give up when @timeout msecs have elapsed.
249 *
250 * Upon a successful return from this function, preemption and local interrupts
251 * are disabled so the caller must not sleep, and is advised to release the
252 * hwspinlock as soon as possible.
253 *
254 * Returns 0 when the @hwlock was successfully taken, and an appropriate
255 * error code otherwise (most notably an -ETIMEDOUT if the @hwlock is still
256 * busy after @timeout msecs). The function will never sleep.
257 */
258static inline
259int hwspin_lock_timeout_irq(struct hwspinlock *hwlock, unsigned int to)
260{
261 return __hwspin_lock_timeout(hwlock, to, HWLOCK_IRQ, NULL);
262}
263
264/**
Baolin Wang1e6c06a2018-04-08 11:06:57 +0800265 * hwspin_lock_timeout_raw() - lock an hwspinlock with timeout limit
266 * @hwlock: the hwspinlock to be locked
267 * @to: timeout value in msecs
268 *
269 * This function locks the underlying @hwlock. If the @hwlock
270 * is already taken, the function will busy loop waiting for it to
271 * be released, but give up when @timeout msecs have elapsed.
272 *
273 * Caution: User must protect the routine of getting hardware lock with mutex
274 * or spinlock to avoid dead-lock, that will let user can do some time-consuming
275 * or sleepable operations under the hardware lock.
276 *
277 * Returns 0 when the @hwlock was successfully taken, and an appropriate
278 * error code otherwise (most notably an -ETIMEDOUT if the @hwlock is still
279 * busy after @timeout msecs). The function will never sleep.
280 */
281static inline
282int hwspin_lock_timeout_raw(struct hwspinlock *hwlock, unsigned int to)
283{
284 return __hwspin_lock_timeout(hwlock, to, HWLOCK_RAW, NULL);
285}
286
287/**
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800288 * hwspin_lock_timeout() - lock an hwspinlock with timeout limit
289 * @hwlock: the hwspinlock to be locked
290 * @to: timeout value in msecs
291 *
292 * This function locks the underlying @hwlock. If the @hwlock
293 * is already taken, the function will busy loop waiting for it to
294 * be released, but give up when @timeout msecs have elapsed.
295 *
296 * Upon a successful return from this function, preemption is disabled
297 * so the caller must not sleep, and is advised to release the hwspinlock
298 * as soon as possible.
299 * This is required in order to minimize remote cores polling on the
300 * hardware interconnect.
301 *
302 * Returns 0 when the @hwlock was successfully taken, and an appropriate
303 * error code otherwise (most notably an -ETIMEDOUT if the @hwlock is still
304 * busy after @timeout msecs). The function will never sleep.
305 */
306static inline
307int hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to)
308{
309 return __hwspin_lock_timeout(hwlock, to, 0, NULL);
310}
311
312/**
313 * hwspin_unlock_irqrestore() - unlock hwspinlock, restore irq state
314 * @hwlock: a previously-acquired hwspinlock which we want to unlock
315 * @flags: previous caller's interrupt state to restore
316 *
317 * This function will unlock a specific hwspinlock, enable preemption and
318 * restore the previous state of the local interrupts. It should be used
319 * to undo, e.g., hwspin_trylock_irqsave().
320 *
321 * @hwlock must be already locked before calling this function: it is a bug
322 * to call unlock on a @hwlock that is already unlocked.
323 */
324static inline void hwspin_unlock_irqrestore(struct hwspinlock *hwlock,
325 unsigned long *flags)
326{
327 __hwspin_unlock(hwlock, HWLOCK_IRQSTATE, flags);
328}
329
330/**
331 * hwspin_unlock_irq() - unlock hwspinlock, enable interrupts
332 * @hwlock: a previously-acquired hwspinlock which we want to unlock
333 *
334 * This function will unlock a specific hwspinlock, enable preemption and
335 * enable local interrupts. Should be used to undo hwspin_lock_irq().
336 *
337 * @hwlock must be already locked (e.g. by hwspin_trylock_irq()) before
338 * calling this function: it is a bug to call unlock on a @hwlock that is
339 * already unlocked.
340 */
341static inline void hwspin_unlock_irq(struct hwspinlock *hwlock)
342{
343 __hwspin_unlock(hwlock, HWLOCK_IRQ, NULL);
344}
345
346/**
Baolin Wang1e6c06a2018-04-08 11:06:57 +0800347 * hwspin_unlock_raw() - unlock hwspinlock
348 * @hwlock: a previously-acquired hwspinlock which we want to unlock
349 *
350 * This function will unlock a specific hwspinlock.
351 *
352 * @hwlock must be already locked (e.g. by hwspin_trylock()) before calling
353 * this function: it is a bug to call unlock on a @hwlock that is already
354 * unlocked.
355 */
356static inline void hwspin_unlock_raw(struct hwspinlock *hwlock)
357{
358 __hwspin_unlock(hwlock, HWLOCK_RAW, NULL);
359}
360
361/**
Ohad Ben-Cohenbd9a4c72011-02-17 09:52:03 -0800362 * hwspin_unlock() - unlock hwspinlock
363 * @hwlock: a previously-acquired hwspinlock which we want to unlock
364 *
365 * This function will unlock a specific hwspinlock and enable preemption
366 * back.
367 *
368 * @hwlock must be already locked (e.g. by hwspin_trylock()) before calling
369 * this function: it is a bug to call unlock on a @hwlock that is already
370 * unlocked.
371 */
372static inline void hwspin_unlock(struct hwspinlock *hwlock)
373{
374 __hwspin_unlock(hwlock, 0, NULL);
375}
376
377#endif /* __LINUX_HWSPINLOCK_H */