Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Hardware spinlock framework |
| 3 | * |
| 4 | * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com |
| 5 | * |
| 6 | * Contact: Ohad Ben-Cohen <ohad@wizery.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/radix-tree.h> |
| 27 | #include <linux/hwspinlock.h> |
| 28 | #include <linux/pm_runtime.h> |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 29 | #include <linux/mutex.h> |
Suman Anna | fb7737e | 2015-03-04 20:01:14 -0600 | [diff] [blame] | 30 | #include <linux/of.h> |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 31 | |
| 32 | #include "hwspinlock_internal.h" |
| 33 | |
| 34 | /* radix tree tags */ |
| 35 | #define HWSPINLOCK_UNUSED (0) /* tags an hwspinlock as unused */ |
| 36 | |
| 37 | /* |
| 38 | * A radix tree is used to maintain the available hwspinlock instances. |
| 39 | * The tree associates hwspinlock pointers with their integer key id, |
| 40 | * and provides easy-to-use API which makes the hwspinlock core code simple |
| 41 | * and easy to read. |
| 42 | * |
| 43 | * Radix trees are quick on lookups, and reasonably efficient in terms of |
| 44 | * storage, especially with high density usages such as this framework |
| 45 | * requires (a continuous range of integer keys, beginning with zero, is |
| 46 | * used as the ID's of the hwspinlock instances). |
| 47 | * |
| 48 | * The radix tree API supports tagging items in the tree, which this |
| 49 | * framework uses to mark unused hwspinlock instances (see the |
| 50 | * HWSPINLOCK_UNUSED tag above). As a result, the process of querying the |
| 51 | * tree, looking for an unused hwspinlock instance, is now reduced to a |
| 52 | * single radix tree API call. |
| 53 | */ |
| 54 | static RADIX_TREE(hwspinlock_tree, GFP_KERNEL); |
| 55 | |
| 56 | /* |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 57 | * Synchronization of access to the tree is achieved using this mutex, |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 58 | * as the radix-tree API requires that users provide all synchronisation. |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 59 | * A mutex is needed because we're using non-atomic radix tree allocations. |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 60 | */ |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 61 | static DEFINE_MUTEX(hwspinlock_tree_lock); |
| 62 | |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * __hwspin_trylock() - attempt to lock a specific hwspinlock |
| 66 | * @hwlock: an hwspinlock which we want to trylock |
| 67 | * @mode: controls whether local interrupts are disabled or not |
| 68 | * @flags: a pointer where the caller's interrupt state will be saved at (if |
| 69 | * requested) |
| 70 | * |
| 71 | * This function attempts to lock an hwspinlock, and will immediately |
| 72 | * fail if the hwspinlock is already taken. |
| 73 | * |
Baolin Wang | 1e6c06a | 2018-04-08 11:06:57 +0800 | [diff] [blame^] | 74 | * Caution: If the mode is HWLOCK_RAW, that means user must protect the routine |
| 75 | * of getting hardware lock with mutex or spinlock. Since in some scenarios, |
| 76 | * user need some time-consuming or sleepable operations under the hardware |
| 77 | * lock, they need one sleepable lock (like mutex) to protect the operations. |
| 78 | * |
| 79 | * If the mode is not HWLOCK_RAW, upon a successful return from this function, |
| 80 | * preemption (and possibly interrupts) is disabled, so the caller must not |
| 81 | * sleep, and is advised to release the hwspinlock as soon as possible. This is |
| 82 | * required in order to minimize remote cores polling on the hardware |
| 83 | * interconnect. |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 84 | * |
| 85 | * The user decides whether local interrupts are disabled or not, and if yes, |
| 86 | * whether he wants their previous state to be saved. It is up to the user |
| 87 | * to choose the appropriate @mode of operation, exactly the same way users |
| 88 | * should decide between spin_trylock, spin_trylock_irq and |
| 89 | * spin_trylock_irqsave. |
| 90 | * |
| 91 | * Returns 0 if we successfully locked the hwspinlock or -EBUSY if |
| 92 | * the hwspinlock was already taken. |
| 93 | * This function will never sleep. |
| 94 | */ |
| 95 | int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags) |
| 96 | { |
| 97 | int ret; |
| 98 | |
| 99 | BUG_ON(!hwlock); |
| 100 | BUG_ON(!flags && mode == HWLOCK_IRQSTATE); |
| 101 | |
| 102 | /* |
| 103 | * This spin_lock{_irq, _irqsave} serves three purposes: |
| 104 | * |
| 105 | * 1. Disable preemption, in order to minimize the period of time |
| 106 | * in which the hwspinlock is taken. This is important in order |
| 107 | * to minimize the possible polling on the hardware interconnect |
| 108 | * by a remote user of this lock. |
| 109 | * 2. Make the hwspinlock SMP-safe (so we can take it from |
| 110 | * additional contexts on the local host). |
| 111 | * 3. Ensure that in_atomic/might_sleep checks catch potential |
| 112 | * problems with hwspinlock usage (e.g. scheduler checks like |
| 113 | * 'scheduling while atomic' etc.) |
| 114 | */ |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 115 | switch (mode) { |
| 116 | case HWLOCK_IRQSTATE: |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 117 | ret = spin_trylock_irqsave(&hwlock->lock, *flags); |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 118 | break; |
| 119 | case HWLOCK_IRQ: |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 120 | ret = spin_trylock_irq(&hwlock->lock); |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 121 | break; |
Baolin Wang | 1e6c06a | 2018-04-08 11:06:57 +0800 | [diff] [blame^] | 122 | case HWLOCK_RAW: |
| 123 | ret = 1; |
| 124 | break; |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 125 | default: |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 126 | ret = spin_trylock(&hwlock->lock); |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 127 | break; |
| 128 | } |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 129 | |
| 130 | /* is lock already taken by another context on the local cpu ? */ |
| 131 | if (!ret) |
| 132 | return -EBUSY; |
| 133 | |
| 134 | /* try to take the hwspinlock device */ |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 135 | ret = hwlock->bank->ops->trylock(hwlock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 136 | |
| 137 | /* if hwlock is already taken, undo spin_trylock_* and exit */ |
| 138 | if (!ret) { |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 139 | switch (mode) { |
| 140 | case HWLOCK_IRQSTATE: |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 141 | spin_unlock_irqrestore(&hwlock->lock, *flags); |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 142 | break; |
| 143 | case HWLOCK_IRQ: |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 144 | spin_unlock_irq(&hwlock->lock); |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 145 | break; |
Baolin Wang | 1e6c06a | 2018-04-08 11:06:57 +0800 | [diff] [blame^] | 146 | case HWLOCK_RAW: |
| 147 | /* Nothing to do */ |
| 148 | break; |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 149 | default: |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 150 | spin_unlock(&hwlock->lock); |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 151 | break; |
| 152 | } |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 153 | |
| 154 | return -EBUSY; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * We can be sure the other core's memory operations |
| 159 | * are observable to us only _after_ we successfully take |
| 160 | * the hwspinlock, and we must make sure that subsequent memory |
| 161 | * operations (both reads and writes) will not be reordered before |
| 162 | * we actually took the hwspinlock. |
| 163 | * |
| 164 | * Note: the implicit memory barrier of the spinlock above is too |
| 165 | * early, so we need this additional explicit memory barrier. |
| 166 | */ |
| 167 | mb(); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | EXPORT_SYMBOL_GPL(__hwspin_trylock); |
| 172 | |
| 173 | /** |
| 174 | * __hwspin_lock_timeout() - lock an hwspinlock with timeout limit |
| 175 | * @hwlock: the hwspinlock to be locked |
| 176 | * @timeout: timeout value in msecs |
| 177 | * @mode: mode which controls whether local interrupts are disabled or not |
| 178 | * @flags: a pointer to where the caller's interrupt state will be saved at (if |
| 179 | * requested) |
| 180 | * |
| 181 | * This function locks the given @hwlock. If the @hwlock |
| 182 | * is already taken, the function will busy loop waiting for it to |
| 183 | * be released, but give up after @timeout msecs have elapsed. |
| 184 | * |
Baolin Wang | 1e6c06a | 2018-04-08 11:06:57 +0800 | [diff] [blame^] | 185 | * Caution: If the mode is HWLOCK_RAW, that means user must protect the routine |
| 186 | * of getting hardware lock with mutex or spinlock. Since in some scenarios, |
| 187 | * user need some time-consuming or sleepable operations under the hardware |
| 188 | * lock, they need one sleepable lock (like mutex) to protect the operations. |
| 189 | * |
| 190 | * If the mode is not HWLOCK_RAW, upon a successful return from this function, |
| 191 | * preemption is disabled (and possibly local interrupts, too), so the caller |
| 192 | * must not sleep, and is advised to release the hwspinlock as soon as possible. |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 193 | * This is required in order to minimize remote cores polling on the |
| 194 | * hardware interconnect. |
| 195 | * |
| 196 | * The user decides whether local interrupts are disabled or not, and if yes, |
| 197 | * whether he wants their previous state to be saved. It is up to the user |
| 198 | * to choose the appropriate @mode of operation, exactly the same way users |
| 199 | * should decide between spin_lock, spin_lock_irq and spin_lock_irqsave. |
| 200 | * |
| 201 | * Returns 0 when the @hwlock was successfully taken, and an appropriate |
| 202 | * error code otherwise (most notably -ETIMEDOUT if the @hwlock is still |
| 203 | * busy after @timeout msecs). The function will never sleep. |
| 204 | */ |
| 205 | int __hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to, |
| 206 | int mode, unsigned long *flags) |
| 207 | { |
| 208 | int ret; |
| 209 | unsigned long expire; |
| 210 | |
| 211 | expire = msecs_to_jiffies(to) + jiffies; |
| 212 | |
| 213 | for (;;) { |
| 214 | /* Try to take the hwspinlock */ |
| 215 | ret = __hwspin_trylock(hwlock, mode, flags); |
| 216 | if (ret != -EBUSY) |
| 217 | break; |
| 218 | |
| 219 | /* |
| 220 | * The lock is already taken, let's check if the user wants |
| 221 | * us to try again |
| 222 | */ |
| 223 | if (time_is_before_eq_jiffies(expire)) |
| 224 | return -ETIMEDOUT; |
| 225 | |
| 226 | /* |
| 227 | * Allow platform-specific relax handlers to prevent |
| 228 | * hogging the interconnect (no sleeping, though) |
| 229 | */ |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 230 | if (hwlock->bank->ops->relax) |
| 231 | hwlock->bank->ops->relax(hwlock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return ret; |
| 235 | } |
| 236 | EXPORT_SYMBOL_GPL(__hwspin_lock_timeout); |
| 237 | |
| 238 | /** |
| 239 | * __hwspin_unlock() - unlock a specific hwspinlock |
| 240 | * @hwlock: a previously-acquired hwspinlock which we want to unlock |
| 241 | * @mode: controls whether local interrupts needs to be restored or not |
| 242 | * @flags: previous caller's interrupt state to restore (if requested) |
| 243 | * |
| 244 | * This function will unlock a specific hwspinlock, enable preemption and |
| 245 | * (possibly) enable interrupts or restore their previous state. |
| 246 | * @hwlock must be already locked before calling this function: it is a bug |
| 247 | * to call unlock on a @hwlock that is already unlocked. |
| 248 | * |
| 249 | * The user decides whether local interrupts should be enabled or not, and |
| 250 | * if yes, whether he wants their previous state to be restored. It is up |
| 251 | * to the user to choose the appropriate @mode of operation, exactly the |
| 252 | * same way users decide between spin_unlock, spin_unlock_irq and |
| 253 | * spin_unlock_irqrestore. |
| 254 | * |
| 255 | * The function will never sleep. |
| 256 | */ |
| 257 | void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags) |
| 258 | { |
| 259 | BUG_ON(!hwlock); |
| 260 | BUG_ON(!flags && mode == HWLOCK_IRQSTATE); |
| 261 | |
| 262 | /* |
| 263 | * We must make sure that memory operations (both reads and writes), |
| 264 | * done before unlocking the hwspinlock, will not be reordered |
| 265 | * after the lock is released. |
| 266 | * |
| 267 | * That's the purpose of this explicit memory barrier. |
| 268 | * |
| 269 | * Note: the memory barrier induced by the spin_unlock below is too |
| 270 | * late; the other core is going to access memory soon after it will |
| 271 | * take the hwspinlock, and by then we want to be sure our memory |
| 272 | * operations are already observable. |
| 273 | */ |
| 274 | mb(); |
| 275 | |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 276 | hwlock->bank->ops->unlock(hwlock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 277 | |
| 278 | /* Undo the spin_trylock{_irq, _irqsave} called while locking */ |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 279 | switch (mode) { |
| 280 | case HWLOCK_IRQSTATE: |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 281 | spin_unlock_irqrestore(&hwlock->lock, *flags); |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 282 | break; |
| 283 | case HWLOCK_IRQ: |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 284 | spin_unlock_irq(&hwlock->lock); |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 285 | break; |
Baolin Wang | 1e6c06a | 2018-04-08 11:06:57 +0800 | [diff] [blame^] | 286 | case HWLOCK_RAW: |
| 287 | /* Nothing to do */ |
| 288 | break; |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 289 | default: |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 290 | spin_unlock(&hwlock->lock); |
Baolin Wang | 66742b1 | 2018-04-08 11:06:56 +0800 | [diff] [blame] | 291 | break; |
| 292 | } |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 293 | } |
| 294 | EXPORT_SYMBOL_GPL(__hwspin_unlock); |
| 295 | |
Suman Anna | fb7737e | 2015-03-04 20:01:14 -0600 | [diff] [blame] | 296 | /** |
| 297 | * of_hwspin_lock_simple_xlate - translate hwlock_spec to return a lock id |
| 298 | * @bank: the hwspinlock device bank |
| 299 | * @hwlock_spec: hwlock specifier as found in the device tree |
| 300 | * |
| 301 | * This is a simple translation function, suitable for hwspinlock platform |
| 302 | * drivers that only has a lock specifier length of 1. |
| 303 | * |
| 304 | * Returns a relative index of the lock within a specified bank on success, |
| 305 | * or -EINVAL on invalid specifier cell count. |
| 306 | */ |
| 307 | static inline int |
| 308 | of_hwspin_lock_simple_xlate(const struct of_phandle_args *hwlock_spec) |
| 309 | { |
| 310 | if (WARN_ON(hwlock_spec->args_count != 1)) |
| 311 | return -EINVAL; |
| 312 | |
| 313 | return hwlock_spec->args[0]; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * of_hwspin_lock_get_id() - get lock id for an OF phandle-based specific lock |
| 318 | * @np: device node from which to request the specific hwlock |
| 319 | * @index: index of the hwlock in the list of values |
| 320 | * |
| 321 | * This function provides a means for DT users of the hwspinlock module to |
| 322 | * get the global lock id of a specific hwspinlock using the phandle of the |
| 323 | * hwspinlock device, so that it can be requested using the normal |
| 324 | * hwspin_lock_request_specific() API. |
| 325 | * |
| 326 | * Returns the global lock id number on success, -EPROBE_DEFER if the hwspinlock |
| 327 | * device is not yet registered, -EINVAL on invalid args specifier value or an |
| 328 | * appropriate error as returned from the OF parsing of the DT client node. |
| 329 | */ |
| 330 | int of_hwspin_lock_get_id(struct device_node *np, int index) |
| 331 | { |
| 332 | struct of_phandle_args args; |
| 333 | struct hwspinlock *hwlock; |
| 334 | struct radix_tree_iter iter; |
| 335 | void **slot; |
| 336 | int id; |
| 337 | int ret; |
| 338 | |
| 339 | ret = of_parse_phandle_with_args(np, "hwlocks", "#hwlock-cells", index, |
| 340 | &args); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | |
| 344 | /* Find the hwspinlock device: we need its base_id */ |
| 345 | ret = -EPROBE_DEFER; |
| 346 | rcu_read_lock(); |
| 347 | radix_tree_for_each_slot(slot, &hwspinlock_tree, &iter, 0) { |
| 348 | hwlock = radix_tree_deref_slot(slot); |
| 349 | if (unlikely(!hwlock)) |
| 350 | continue; |
Matthew Wilcox | b76ba4a | 2016-05-20 17:03:01 -0700 | [diff] [blame] | 351 | if (radix_tree_deref_retry(hwlock)) { |
Matthew Wilcox | c6400ba | 2016-02-02 16:57:55 -0800 | [diff] [blame] | 352 | slot = radix_tree_iter_retry(&iter); |
| 353 | continue; |
| 354 | } |
Suman Anna | fb7737e | 2015-03-04 20:01:14 -0600 | [diff] [blame] | 355 | |
| 356 | if (hwlock->bank->dev->of_node == args.np) { |
| 357 | ret = 0; |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | rcu_read_unlock(); |
| 362 | if (ret < 0) |
| 363 | goto out; |
| 364 | |
| 365 | id = of_hwspin_lock_simple_xlate(&args); |
| 366 | if (id < 0 || id >= hwlock->bank->num_locks) { |
| 367 | ret = -EINVAL; |
| 368 | goto out; |
| 369 | } |
| 370 | id += hwlock->bank->base_id; |
| 371 | |
| 372 | out: |
| 373 | of_node_put(args.np); |
| 374 | return ret ? ret : id; |
| 375 | } |
| 376 | EXPORT_SYMBOL_GPL(of_hwspin_lock_get_id); |
| 377 | |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 378 | static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id) |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 379 | { |
| 380 | struct hwspinlock *tmp; |
| 381 | int ret; |
| 382 | |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 383 | mutex_lock(&hwspinlock_tree_lock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 384 | |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 385 | ret = radix_tree_insert(&hwspinlock_tree, id, hwlock); |
| 386 | if (ret) { |
| 387 | if (ret == -EEXIST) |
| 388 | pr_err("hwspinlock id %d already exists!\n", id); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 389 | goto out; |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 390 | } |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 391 | |
| 392 | /* mark this hwspinlock as available */ |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 393 | tmp = radix_tree_tag_set(&hwspinlock_tree, id, HWSPINLOCK_UNUSED); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 394 | |
| 395 | /* self-sanity check which should never fail */ |
| 396 | WARN_ON(tmp != hwlock); |
| 397 | |
| 398 | out: |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 399 | mutex_unlock(&hwspinlock_tree_lock); |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 400 | return 0; |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 401 | } |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 402 | |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 403 | static struct hwspinlock *hwspin_lock_unregister_single(unsigned int id) |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 404 | { |
| 405 | struct hwspinlock *hwlock = NULL; |
| 406 | int ret; |
| 407 | |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 408 | mutex_lock(&hwspinlock_tree_lock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 409 | |
| 410 | /* make sure the hwspinlock is not in use (tag is set) */ |
| 411 | ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED); |
| 412 | if (ret == 0) { |
| 413 | pr_err("hwspinlock %d still in use (or not present)\n", id); |
| 414 | goto out; |
| 415 | } |
| 416 | |
| 417 | hwlock = radix_tree_delete(&hwspinlock_tree, id); |
| 418 | if (!hwlock) { |
| 419 | pr_err("failed to delete hwspinlock %d\n", id); |
| 420 | goto out; |
| 421 | } |
| 422 | |
| 423 | out: |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 424 | mutex_unlock(&hwspinlock_tree_lock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 425 | return hwlock; |
| 426 | } |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 427 | |
| 428 | /** |
| 429 | * hwspin_lock_register() - register a new hw spinlock device |
| 430 | * @bank: the hwspinlock device, which usually provides numerous hw locks |
| 431 | * @dev: the backing device |
| 432 | * @ops: hwspinlock handlers for this device |
| 433 | * @base_id: id of the first hardware spinlock in this bank |
| 434 | * @num_locks: number of hwspinlocks provided by this device |
| 435 | * |
| 436 | * This function should be called from the underlying platform-specific |
| 437 | * implementation, to register a new hwspinlock device instance. |
| 438 | * |
| 439 | * Should be called from a process context (might sleep) |
| 440 | * |
| 441 | * Returns 0 on success, or an appropriate error code on failure |
| 442 | */ |
| 443 | int hwspin_lock_register(struct hwspinlock_device *bank, struct device *dev, |
| 444 | const struct hwspinlock_ops *ops, int base_id, int num_locks) |
| 445 | { |
| 446 | struct hwspinlock *hwlock; |
| 447 | int ret = 0, i; |
| 448 | |
| 449 | if (!bank || !ops || !dev || !num_locks || !ops->trylock || |
| 450 | !ops->unlock) { |
| 451 | pr_err("invalid parameters\n"); |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | |
| 455 | bank->dev = dev; |
| 456 | bank->ops = ops; |
| 457 | bank->base_id = base_id; |
| 458 | bank->num_locks = num_locks; |
| 459 | |
| 460 | for (i = 0; i < num_locks; i++) { |
| 461 | hwlock = &bank->lock[i]; |
| 462 | |
| 463 | spin_lock_init(&hwlock->lock); |
| 464 | hwlock->bank = bank; |
| 465 | |
Shinya Kuribayashi | 476a7eeb | 2012-07-07 13:37:42 +0300 | [diff] [blame] | 466 | ret = hwspin_lock_register_single(hwlock, base_id + i); |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 467 | if (ret) |
| 468 | goto reg_failed; |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | |
| 473 | reg_failed: |
| 474 | while (--i >= 0) |
Shinya Kuribayashi | 476a7eeb | 2012-07-07 13:37:42 +0300 | [diff] [blame] | 475 | hwspin_lock_unregister_single(base_id + i); |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 476 | return ret; |
| 477 | } |
| 478 | EXPORT_SYMBOL_GPL(hwspin_lock_register); |
| 479 | |
| 480 | /** |
| 481 | * hwspin_lock_unregister() - unregister an hw spinlock device |
| 482 | * @bank: the hwspinlock device, which usually provides numerous hw locks |
| 483 | * |
| 484 | * This function should be called from the underlying platform-specific |
| 485 | * implementation, to unregister an existing (and unused) hwspinlock. |
| 486 | * |
| 487 | * Should be called from a process context (might sleep) |
| 488 | * |
| 489 | * Returns 0 on success, or an appropriate error code on failure |
| 490 | */ |
| 491 | int hwspin_lock_unregister(struct hwspinlock_device *bank) |
| 492 | { |
| 493 | struct hwspinlock *hwlock, *tmp; |
| 494 | int i; |
| 495 | |
| 496 | for (i = 0; i < bank->num_locks; i++) { |
| 497 | hwlock = &bank->lock[i]; |
| 498 | |
| 499 | tmp = hwspin_lock_unregister_single(bank->base_id + i); |
| 500 | if (!tmp) |
| 501 | return -EBUSY; |
| 502 | |
| 503 | /* self-sanity check that should never fail */ |
| 504 | WARN_ON(tmp != hwlock); |
| 505 | } |
| 506 | |
| 507 | return 0; |
| 508 | } |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 509 | EXPORT_SYMBOL_GPL(hwspin_lock_unregister); |
| 510 | |
| 511 | /** |
| 512 | * __hwspin_lock_request() - tag an hwspinlock as used and power it up |
| 513 | * |
| 514 | * This is an internal function that prepares an hwspinlock instance |
| 515 | * before it is given to the user. The function assumes that |
| 516 | * hwspinlock_tree_lock is taken. |
| 517 | * |
| 518 | * Returns 0 or positive to indicate success, and a negative value to |
| 519 | * indicate an error (with the appropriate error code) |
| 520 | */ |
| 521 | static int __hwspin_lock_request(struct hwspinlock *hwlock) |
| 522 | { |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 523 | struct device *dev = hwlock->bank->dev; |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 524 | struct hwspinlock *tmp; |
| 525 | int ret; |
| 526 | |
| 527 | /* prevent underlying implementation from being removed */ |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 528 | if (!try_module_get(dev->driver->owner)) { |
| 529 | dev_err(dev, "%s: can't get owner\n", __func__); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 530 | return -EINVAL; |
| 531 | } |
| 532 | |
| 533 | /* notify PM core that power is now needed */ |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 534 | ret = pm_runtime_get_sync(dev); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 535 | if (ret < 0) { |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 536 | dev_err(dev, "%s: can't power on device\n", __func__); |
Li Fei | c10b90d | 2013-04-05 21:20:36 +0800 | [diff] [blame] | 537 | pm_runtime_put_noidle(dev); |
| 538 | module_put(dev->driver->owner); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | /* mark hwspinlock as used, should not fail */ |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 543 | tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock), |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 544 | HWSPINLOCK_UNUSED); |
| 545 | |
| 546 | /* self-sanity check that should never fail */ |
| 547 | WARN_ON(tmp != hwlock); |
| 548 | |
| 549 | return ret; |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * hwspin_lock_get_id() - retrieve id number of a given hwspinlock |
| 554 | * @hwlock: a valid hwspinlock instance |
| 555 | * |
| 556 | * Returns the id number of a given @hwlock, or -EINVAL if @hwlock is invalid. |
| 557 | */ |
| 558 | int hwspin_lock_get_id(struct hwspinlock *hwlock) |
| 559 | { |
| 560 | if (!hwlock) { |
| 561 | pr_err("invalid hwlock\n"); |
| 562 | return -EINVAL; |
| 563 | } |
| 564 | |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 565 | return hwlock_to_id(hwlock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 566 | } |
| 567 | EXPORT_SYMBOL_GPL(hwspin_lock_get_id); |
| 568 | |
| 569 | /** |
| 570 | * hwspin_lock_request() - request an hwspinlock |
| 571 | * |
| 572 | * This function should be called by users of the hwspinlock device, |
| 573 | * in order to dynamically assign them an unused hwspinlock. |
| 574 | * Usually the user of this lock will then have to communicate the lock's id |
| 575 | * to the remote core before it can be used for synchronization (to get the |
| 576 | * id of a given hwlock, use hwspin_lock_get_id()). |
| 577 | * |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 578 | * Should be called from a process context (might sleep) |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 579 | * |
| 580 | * Returns the address of the assigned hwspinlock, or NULL on error |
| 581 | */ |
| 582 | struct hwspinlock *hwspin_lock_request(void) |
| 583 | { |
| 584 | struct hwspinlock *hwlock; |
| 585 | int ret; |
| 586 | |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 587 | mutex_lock(&hwspinlock_tree_lock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 588 | |
| 589 | /* look for an unused lock */ |
| 590 | ret = radix_tree_gang_lookup_tag(&hwspinlock_tree, (void **)&hwlock, |
| 591 | 0, 1, HWSPINLOCK_UNUSED); |
| 592 | if (ret == 0) { |
| 593 | pr_warn("a free hwspinlock is not available\n"); |
| 594 | hwlock = NULL; |
| 595 | goto out; |
| 596 | } |
| 597 | |
| 598 | /* sanity check that should never fail */ |
| 599 | WARN_ON(ret > 1); |
| 600 | |
| 601 | /* mark as used and power up */ |
| 602 | ret = __hwspin_lock_request(hwlock); |
| 603 | if (ret < 0) |
| 604 | hwlock = NULL; |
| 605 | |
| 606 | out: |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 607 | mutex_unlock(&hwspinlock_tree_lock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 608 | return hwlock; |
| 609 | } |
| 610 | EXPORT_SYMBOL_GPL(hwspin_lock_request); |
| 611 | |
| 612 | /** |
| 613 | * hwspin_lock_request_specific() - request for a specific hwspinlock |
| 614 | * @id: index of the specific hwspinlock that is requested |
| 615 | * |
| 616 | * This function should be called by users of the hwspinlock module, |
| 617 | * in order to assign them a specific hwspinlock. |
| 618 | * Usually early board code will be calling this function in order to |
| 619 | * reserve specific hwspinlock ids for predefined purposes. |
| 620 | * |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 621 | * Should be called from a process context (might sleep) |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 622 | * |
| 623 | * Returns the address of the assigned hwspinlock, or NULL on error |
| 624 | */ |
| 625 | struct hwspinlock *hwspin_lock_request_specific(unsigned int id) |
| 626 | { |
| 627 | struct hwspinlock *hwlock; |
| 628 | int ret; |
| 629 | |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 630 | mutex_lock(&hwspinlock_tree_lock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 631 | |
| 632 | /* make sure this hwspinlock exists */ |
| 633 | hwlock = radix_tree_lookup(&hwspinlock_tree, id); |
| 634 | if (!hwlock) { |
| 635 | pr_warn("hwspinlock %u does not exist\n", id); |
| 636 | goto out; |
| 637 | } |
| 638 | |
| 639 | /* sanity check (this shouldn't happen) */ |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 640 | WARN_ON(hwlock_to_id(hwlock) != id); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 641 | |
| 642 | /* make sure this hwspinlock is unused */ |
| 643 | ret = radix_tree_tag_get(&hwspinlock_tree, id, HWSPINLOCK_UNUSED); |
| 644 | if (ret == 0) { |
| 645 | pr_warn("hwspinlock %u is already in use\n", id); |
| 646 | hwlock = NULL; |
| 647 | goto out; |
| 648 | } |
| 649 | |
| 650 | /* mark as used and power up */ |
| 651 | ret = __hwspin_lock_request(hwlock); |
| 652 | if (ret < 0) |
| 653 | hwlock = NULL; |
| 654 | |
| 655 | out: |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 656 | mutex_unlock(&hwspinlock_tree_lock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 657 | return hwlock; |
| 658 | } |
| 659 | EXPORT_SYMBOL_GPL(hwspin_lock_request_specific); |
| 660 | |
| 661 | /** |
| 662 | * hwspin_lock_free() - free a specific hwspinlock |
| 663 | * @hwlock: the specific hwspinlock to free |
| 664 | * |
| 665 | * This function mark @hwlock as free again. |
| 666 | * Should only be called with an @hwlock that was retrieved from |
| 667 | * an earlier call to omap_hwspin_lock_request{_specific}. |
| 668 | * |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 669 | * Should be called from a process context (might sleep) |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 670 | * |
| 671 | * Returns 0 on success, or an appropriate error code on failure |
| 672 | */ |
| 673 | int hwspin_lock_free(struct hwspinlock *hwlock) |
| 674 | { |
Wei Yongjun | e352614 | 2012-09-10 12:52:02 +0800 | [diff] [blame] | 675 | struct device *dev; |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 676 | struct hwspinlock *tmp; |
| 677 | int ret; |
| 678 | |
| 679 | if (!hwlock) { |
| 680 | pr_err("invalid hwlock\n"); |
| 681 | return -EINVAL; |
| 682 | } |
| 683 | |
Wei Yongjun | e352614 | 2012-09-10 12:52:02 +0800 | [diff] [blame] | 684 | dev = hwlock->bank->dev; |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 685 | mutex_lock(&hwspinlock_tree_lock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 686 | |
| 687 | /* make sure the hwspinlock is used */ |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 688 | ret = radix_tree_tag_get(&hwspinlock_tree, hwlock_to_id(hwlock), |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 689 | HWSPINLOCK_UNUSED); |
| 690 | if (ret == 1) { |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 691 | dev_err(dev, "%s: hwlock is already free\n", __func__); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 692 | dump_stack(); |
| 693 | ret = -EINVAL; |
| 694 | goto out; |
| 695 | } |
| 696 | |
| 697 | /* notify the underlying device that power is not needed */ |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 698 | ret = pm_runtime_put(dev); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 699 | if (ret < 0) |
| 700 | goto out; |
| 701 | |
| 702 | /* mark this hwspinlock as available */ |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 703 | tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock), |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 704 | HWSPINLOCK_UNUSED); |
| 705 | |
| 706 | /* sanity check (this shouldn't happen) */ |
| 707 | WARN_ON(tmp != hwlock); |
| 708 | |
Ohad Ben-Cohen | 300bab9 | 2011-09-06 15:39:21 +0300 | [diff] [blame] | 709 | module_put(dev->driver->owner); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 710 | |
| 711 | out: |
Juan Gutierrez | 93b465c | 2011-09-06 09:30:16 +0300 | [diff] [blame] | 712 | mutex_unlock(&hwspinlock_tree_lock); |
Ohad Ben-Cohen | bd9a4c7 | 2011-02-17 09:52:03 -0800 | [diff] [blame] | 713 | return ret; |
| 714 | } |
| 715 | EXPORT_SYMBOL_GPL(hwspin_lock_free); |
| 716 | |
| 717 | MODULE_LICENSE("GPL v2"); |
| 718 | MODULE_DESCRIPTION("Hardware spinlock interface"); |
| 719 | MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>"); |