Lines Matching +full:hwlock +full:- +full:names
1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
7 * Contact: Ohad Ben-Cohen <ohad@wizery.com>
19 #include <linux/radix-tree.h>
36 * and provides easy-to-use API which makes the hwspinlock core code simple
54 * as the radix-tree API requires that users provide all synchronisation.
55 * A mutex is needed because we're using non-atomic radix tree allocations.
61 * __hwspin_trylock() - attempt to lock a specific hwspinlock
62 * @hwlock: an hwspinlock which we want to trylock
72 * user need some time-consuming or sleepable operations under the hardware
87 * Returns: %0 if we successfully locked the hwspinlock or -EBUSY if
92 int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags) in __hwspin_trylock() argument
96 if (WARN_ON(!hwlock || (!flags && mode == HWLOCK_IRQSTATE))) in __hwspin_trylock()
97 return -EINVAL; in __hwspin_trylock()
106 * 2. Make the hwspinlock SMP-safe (so we can take it from in __hwspin_trylock()
114 ret = spin_trylock_irqsave(&hwlock->lock, *flags); in __hwspin_trylock()
117 ret = spin_trylock_irq(&hwlock->lock); in __hwspin_trylock()
124 ret = spin_trylock(&hwlock->lock); in __hwspin_trylock()
130 return -EBUSY; in __hwspin_trylock()
133 ret = hwlock->bank->ops->trylock(hwlock); in __hwspin_trylock()
135 /* if hwlock is already taken, undo spin_trylock_* and exit */ in __hwspin_trylock()
139 spin_unlock_irqrestore(&hwlock->lock, *flags); in __hwspin_trylock()
142 spin_unlock_irq(&hwlock->lock); in __hwspin_trylock()
149 spin_unlock(&hwlock->lock); in __hwspin_trylock()
153 return -EBUSY; in __hwspin_trylock()
173 * __hwspin_lock_timeout() - lock an hwspinlock with timeout limit
174 * @hwlock: the hwspinlock to be locked
180 * This function locks the given @hwlock. If the @hwlock
186 * user need some time-consuming or sleepable operations under the hardware
190 * is handled with busy-waiting delays, hence shall not exceed few msecs.
203 * Returns: %0 when the @hwlock was successfully taken, and an appropriate
204 * error code otherwise (most notably -ETIMEDOUT if the @hwlock is still
209 int __hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to, in __hwspin_lock_timeout() argument
219 ret = __hwspin_trylock(hwlock, mode, flags); in __hwspin_lock_timeout()
220 if (ret != -EBUSY) in __hwspin_lock_timeout()
231 return -ETIMEDOUT; in __hwspin_lock_timeout()
234 return -ETIMEDOUT; in __hwspin_lock_timeout()
238 * Allow platform-specific relax handlers to prevent in __hwspin_lock_timeout()
241 if (hwlock->bank->ops->relax) in __hwspin_lock_timeout()
242 hwlock->bank->ops->relax(hwlock); in __hwspin_lock_timeout()
250 * __hwspin_unlock() - unlock a specific hwspinlock
251 * @hwlock: a previously-acquired hwspinlock which we want to unlock
257 * @hwlock must be already locked before calling this function: it is a bug
258 * to call unlock on a @hwlock that is already unlocked.
268 void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags) in __hwspin_unlock() argument
270 if (WARN_ON(!hwlock || (!flags && mode == HWLOCK_IRQSTATE))) in __hwspin_unlock()
287 hwlock->bank->ops->unlock(hwlock); in __hwspin_unlock()
292 spin_unlock_irqrestore(&hwlock->lock, *flags); in __hwspin_unlock()
295 spin_unlock_irq(&hwlock->lock); in __hwspin_unlock()
302 spin_unlock(&hwlock->lock); in __hwspin_unlock()
309 * of_hwspin_lock_simple_xlate - translate hwlock_spec to return a lock id
310 * @hwlock_spec: hwlock specifier as found in the device tree
316 * or -EINVAL on invalid specifier cell count.
321 if (WARN_ON(hwlock_spec->args_count != 1)) in of_hwspin_lock_simple_xlate()
322 return -EINVAL; in of_hwspin_lock_simple_xlate()
324 return hwlock_spec->args[0]; in of_hwspin_lock_simple_xlate()
328 * of_hwspin_lock_get_id() - get lock id for an OF phandle-based specific lock
329 * @np: device node from which to request the specific hwlock
330 * @index: index of the hwlock in the list of values
337 * Returns: the global lock id number on success, -EPROBE_DEFER if the
338 * hwspinlock device is not yet registered, -EINVAL on invalid args
345 struct hwspinlock *hwlock; in of_hwspin_lock_get_id() local
351 ret = of_parse_phandle_with_args(np, "hwlocks", "#hwlock-cells", index, in of_hwspin_lock_get_id()
357 ret = -ENOENT; in of_hwspin_lock_get_id()
362 ret = -EPROBE_DEFER; in of_hwspin_lock_get_id()
365 hwlock = radix_tree_deref_slot(slot); in of_hwspin_lock_get_id()
366 if (unlikely(!hwlock)) in of_hwspin_lock_get_id()
368 if (radix_tree_deref_retry(hwlock)) { in of_hwspin_lock_get_id()
373 if (device_match_of_node(hwlock->bank->dev, args.np)) { in of_hwspin_lock_get_id()
383 if (id < 0 || id >= hwlock->bank->num_locks) { in of_hwspin_lock_get_id()
384 ret = -EINVAL; in of_hwspin_lock_get_id()
387 id += hwlock->bank->base_id; in of_hwspin_lock_get_id()
396 * of_hwspin_lock_get_id_byname() - get lock id for an specified hwlock name
397 * @np: device node from which to request the specific hwlock
398 * @name: hwlock name
405 * Returns: the global lock id number on success, -EPROBE_DEFER if the
406 * hwspinlock device is not yet registered, -EINVAL on invalid args
415 return -EINVAL; in of_hwspin_lock_get_id_byname()
417 index = of_property_match_string(np, "hwlock-names", name); in of_hwspin_lock_get_id_byname()
425 static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id) in hwspin_lock_register_single() argument
432 ret = radix_tree_insert(&hwspinlock_tree, id, hwlock); in hwspin_lock_register_single()
434 if (ret == -EEXIST) in hwspin_lock_register_single()
442 /* self-sanity check which should never fail */ in hwspin_lock_register_single()
443 WARN_ON(tmp != hwlock); in hwspin_lock_register_single()
452 struct hwspinlock *hwlock = NULL; in hwspin_lock_unregister_single() local
464 hwlock = radix_tree_delete(&hwspinlock_tree, id); in hwspin_lock_unregister_single()
465 if (!hwlock) { in hwspin_lock_unregister_single()
472 return hwlock; in hwspin_lock_unregister_single()
476 * hwspin_lock_register() - register a new hw spinlock device
483 * This function should be called from the underlying platform-specific
493 struct hwspinlock *hwlock; in hwspin_lock_register() local
496 if (!bank || !ops || !dev || !num_locks || !ops->trylock || in hwspin_lock_register()
497 !ops->unlock) { in hwspin_lock_register()
499 return -EINVAL; in hwspin_lock_register()
502 bank->dev = dev; in hwspin_lock_register()
503 bank->ops = ops; in hwspin_lock_register()
504 bank->base_id = base_id; in hwspin_lock_register()
505 bank->num_locks = num_locks; in hwspin_lock_register()
508 hwlock = &bank->lock[i]; in hwspin_lock_register()
510 spin_lock_init(&hwlock->lock); in hwspin_lock_register()
511 hwlock->bank = bank; in hwspin_lock_register()
513 ret = hwspin_lock_register_single(hwlock, base_id + i); in hwspin_lock_register()
521 while (--i >= 0) in hwspin_lock_register()
528 * hwspin_lock_unregister() - unregister an hw spinlock device
531 * This function should be called from the underlying platform-specific
540 struct hwspinlock *hwlock, *tmp; in hwspin_lock_unregister() local
543 for (i = 0; i < bank->num_locks; i++) { in hwspin_lock_unregister()
544 hwlock = &bank->lock[i]; in hwspin_lock_unregister()
546 tmp = hwspin_lock_unregister_single(bank->base_id + i); in hwspin_lock_unregister()
548 return -EBUSY; in hwspin_lock_unregister()
550 /* self-sanity check that should never fail */ in hwspin_lock_unregister()
551 WARN_ON(tmp != hwlock); in hwspin_lock_unregister()
575 * devm_hwspin_lock_unregister() - unregister an hw spinlock device for
580 * This function should be called from the underlying platform-specific
601 * devm_hwspin_lock_register() - register a new hw spinlock device for
609 * This function should be called from the underlying platform-specific
626 return -ENOMEM; in devm_hwspin_lock_register()
641 * __hwspin_lock_request() - tag an hwspinlock as used and power it up
642 * @hwlock: the target hwspinlock
651 static int __hwspin_lock_request(struct hwspinlock *hwlock) in __hwspin_lock_request() argument
653 struct device *dev = hwlock->bank->dev; in __hwspin_lock_request()
658 if (!try_module_get(dev->driver->owner)) { in __hwspin_lock_request()
660 return -EINVAL; in __hwspin_lock_request()
665 if (ret < 0 && ret != -EACCES) { in __hwspin_lock_request()
668 module_put(dev->driver->owner); in __hwspin_lock_request()
675 tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock), in __hwspin_lock_request()
678 /* self-sanity check that should never fail */ in __hwspin_lock_request()
679 WARN_ON(tmp != hwlock); in __hwspin_lock_request()
685 * hwspin_lock_get_id() - retrieve id number of a given hwspinlock
686 * @hwlock: a valid hwspinlock instance
688 * Returns: the id number of a given @hwlock, or -EINVAL if @hwlock is invalid.
690 int hwspin_lock_get_id(struct hwspinlock *hwlock) in hwspin_lock_get_id() argument
692 if (!hwlock) { in hwspin_lock_get_id()
693 pr_err("invalid hwlock\n"); in hwspin_lock_get_id()
694 return -EINVAL; in hwspin_lock_get_id()
697 return hwlock_to_id(hwlock); in hwspin_lock_get_id()
702 * hwspin_lock_request() - request an hwspinlock
708 * id of a given hwlock, use hwspin_lock_get_id()).
716 struct hwspinlock *hwlock; in hwspin_lock_request() local
722 ret = radix_tree_gang_lookup_tag(&hwspinlock_tree, (void **)&hwlock, in hwspin_lock_request()
726 hwlock = NULL; in hwspin_lock_request()
734 ret = __hwspin_lock_request(hwlock); in hwspin_lock_request()
736 hwlock = NULL; in hwspin_lock_request()
740 return hwlock; in hwspin_lock_request()
745 * hwspin_lock_request_specific() - request for a specific hwspinlock
759 struct hwspinlock *hwlock; in hwspin_lock_request_specific() local
765 hwlock = radix_tree_lookup(&hwspinlock_tree, id); in hwspin_lock_request_specific()
766 if (!hwlock) { in hwspin_lock_request_specific()
772 WARN_ON(hwlock_to_id(hwlock) != id); in hwspin_lock_request_specific()
778 hwlock = NULL; in hwspin_lock_request_specific()
783 ret = __hwspin_lock_request(hwlock); in hwspin_lock_request_specific()
785 hwlock = NULL; in hwspin_lock_request_specific()
789 return hwlock; in hwspin_lock_request_specific()
794 * hwspin_lock_free() - free a specific hwspinlock
795 * @hwlock: the specific hwspinlock to free
797 * This function mark @hwlock as free again.
798 * Should only be called with an @hwlock that was retrieved from
805 int hwspin_lock_free(struct hwspinlock *hwlock) in hwspin_lock_free() argument
811 if (!hwlock) { in hwspin_lock_free()
812 pr_err("invalid hwlock\n"); in hwspin_lock_free()
813 return -EINVAL; in hwspin_lock_free()
816 dev = hwlock->bank->dev; in hwspin_lock_free()
820 ret = radix_tree_tag_get(&hwspinlock_tree, hwlock_to_id(hwlock), in hwspin_lock_free()
823 dev_err(dev, "%s: hwlock is already free\n", __func__); in hwspin_lock_free()
825 ret = -EINVAL; in hwspin_lock_free()
833 tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock), in hwspin_lock_free()
837 WARN_ON(tmp != hwlock); in hwspin_lock_free()
839 module_put(dev->driver->owner); in hwspin_lock_free()
849 struct hwspinlock **hwlock = res; in devm_hwspin_lock_match() local
851 if (WARN_ON(!hwlock || !*hwlock)) in devm_hwspin_lock_match()
854 return *hwlock == data; in devm_hwspin_lock_match()
863 * devm_hwspin_lock_free() - free a specific hwspinlock for a managed device
865 * @hwlock: the specific hwspinlock to free
867 * This function mark @hwlock as free again.
868 * Should only be called with an @hwlock that was retrieved from
875 int devm_hwspin_lock_free(struct device *dev, struct hwspinlock *hwlock) in devm_hwspin_lock_free() argument
880 devm_hwspin_lock_match, hwlock); in devm_hwspin_lock_free()
888 * devm_hwspin_lock_request() - request an hwspinlock for a managed device
895 * id of a given hwlock, use hwspin_lock_get_id()).
903 struct hwspinlock **ptr, *hwlock; in devm_hwspin_lock_request() local
909 hwlock = hwspin_lock_request(); in devm_hwspin_lock_request()
910 if (hwlock) { in devm_hwspin_lock_request()
911 *ptr = hwlock; in devm_hwspin_lock_request()
917 return hwlock; in devm_hwspin_lock_request()
922 * devm_hwspin_lock_request_specific() - request for a specific hwspinlock for
939 struct hwspinlock **ptr, *hwlock; in devm_hwspin_lock_request_specific() local
945 hwlock = hwspin_lock_request_specific(id); in devm_hwspin_lock_request_specific()
946 if (hwlock) { in devm_hwspin_lock_request_specific()
947 *ptr = hwlock; in devm_hwspin_lock_request_specific()
953 return hwlock; in devm_hwspin_lock_request_specific()
958 MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");