xref: /linux/drivers/hwspinlock/hwspinlock_internal.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1eebba71eSSuman Anna /* SPDX-License-Identifier: GPL-2.0 */
2bd9a4c7dSOhad Ben-Cohen /*
3bd9a4c7dSOhad Ben-Cohen  * Hardware spinlocks internal header
4bd9a4c7dSOhad Ben-Cohen  *
5bd9a4c7dSOhad Ben-Cohen  * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
6bd9a4c7dSOhad Ben-Cohen  *
7bd9a4c7dSOhad Ben-Cohen  * Contact: Ohad Ben-Cohen <ohad@wizery.com>
8bd9a4c7dSOhad Ben-Cohen  */
9bd9a4c7dSOhad Ben-Cohen 
10bd9a4c7dSOhad Ben-Cohen #ifndef __HWSPINLOCK_HWSPINLOCK_H
11bd9a4c7dSOhad Ben-Cohen #define __HWSPINLOCK_HWSPINLOCK_H
12bd9a4c7dSOhad Ben-Cohen 
13bd9a4c7dSOhad Ben-Cohen #include <linux/spinlock.h>
14bd9a4c7dSOhad Ben-Cohen #include <linux/device.h>
15bd9a4c7dSOhad Ben-Cohen 
16300bab97SOhad Ben-Cohen struct hwspinlock_device;
17300bab97SOhad Ben-Cohen 
18bd9a4c7dSOhad Ben-Cohen /**
19bd9a4c7dSOhad Ben-Cohen  * struct hwspinlock_ops - platform-specific hwspinlock handlers
20bd9a4c7dSOhad Ben-Cohen  *
21bd9a4c7dSOhad Ben-Cohen  * @trylock: make a single attempt to take the lock. returns 0 on
22bd9a4c7dSOhad Ben-Cohen  *	     failure and true on success. may _not_ sleep.
23bd9a4c7dSOhad Ben-Cohen  * @unlock:  release the lock. always succeed. may _not_ sleep.
24*7c327d56SRichard Maina  * @bust:    optional, platform-specific bust handler, called by hwspinlock
25*7c327d56SRichard Maina  *	     core to bust a specific lock.
26bd9a4c7dSOhad Ben-Cohen  * @relax:   optional, platform-specific relax handler, called by hwspinlock
27bd9a4c7dSOhad Ben-Cohen  *	     core while spinning on a lock, between two successive
28bd9a4c7dSOhad Ben-Cohen  *	     invocations of @trylock. may _not_ sleep.
29bd9a4c7dSOhad Ben-Cohen  */
30bd9a4c7dSOhad Ben-Cohen struct hwspinlock_ops {
31bd9a4c7dSOhad Ben-Cohen 	int (*trylock)(struct hwspinlock *lock);
32bd9a4c7dSOhad Ben-Cohen 	void (*unlock)(struct hwspinlock *lock);
33*7c327d56SRichard Maina 	int (*bust)(struct hwspinlock *lock, unsigned int id);
34bd9a4c7dSOhad Ben-Cohen 	void (*relax)(struct hwspinlock *lock);
35bd9a4c7dSOhad Ben-Cohen };
36bd9a4c7dSOhad Ben-Cohen 
37bd9a4c7dSOhad Ben-Cohen /**
38bd9a4c7dSOhad Ben-Cohen  * struct hwspinlock - this struct represents a single hwspinlock instance
39300bab97SOhad Ben-Cohen  * @bank: the hwspinlock_device structure which owns this lock
40bd9a4c7dSOhad Ben-Cohen  * @lock: initialized and used by hwspinlock core
41300bab97SOhad Ben-Cohen  * @priv: private data, owned by the underlying platform-specific hwspinlock drv
42bd9a4c7dSOhad Ben-Cohen  */
43bd9a4c7dSOhad Ben-Cohen struct hwspinlock {
44300bab97SOhad Ben-Cohen 	struct hwspinlock_device *bank;
45300bab97SOhad Ben-Cohen 	spinlock_t lock;
46300bab97SOhad Ben-Cohen 	void *priv;
47300bab97SOhad Ben-Cohen };
48300bab97SOhad Ben-Cohen 
49300bab97SOhad Ben-Cohen /**
50300bab97SOhad Ben-Cohen  * struct hwspinlock_device - a device which usually spans numerous hwspinlocks
51300bab97SOhad Ben-Cohen  * @dev: underlying device, will be used to invoke runtime PM api
52300bab97SOhad Ben-Cohen  * @ops: platform-specific hwspinlock handlers
53300bab97SOhad Ben-Cohen  * @base_id: id index of the first lock in this device
54300bab97SOhad Ben-Cohen  * @num_locks: number of locks in this device
55300bab97SOhad Ben-Cohen  * @lock: dynamically allocated array of 'struct hwspinlock'
56300bab97SOhad Ben-Cohen  */
57300bab97SOhad Ben-Cohen struct hwspinlock_device {
58bd9a4c7dSOhad Ben-Cohen 	struct device *dev;
59bd9a4c7dSOhad Ben-Cohen 	const struct hwspinlock_ops *ops;
60300bab97SOhad Ben-Cohen 	int base_id;
61300bab97SOhad Ben-Cohen 	int num_locks;
62ef17f519SGustavo A. R. Silva 	struct hwspinlock lock[];
63bd9a4c7dSOhad Ben-Cohen };
64bd9a4c7dSOhad Ben-Cohen 
hwlock_to_id(struct hwspinlock * hwlock)65300bab97SOhad Ben-Cohen static inline int hwlock_to_id(struct hwspinlock *hwlock)
66300bab97SOhad Ben-Cohen {
67300bab97SOhad Ben-Cohen 	int local_id = hwlock - &hwlock->bank->lock[0];
68300bab97SOhad Ben-Cohen 
69300bab97SOhad Ben-Cohen 	return hwlock->bank->base_id + local_id;
70300bab97SOhad Ben-Cohen }
71300bab97SOhad Ben-Cohen 
72bd9a4c7dSOhad Ben-Cohen #endif /* __HWSPINLOCK_HWSPINLOCK_H */
73