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
91 int __hwspin_trylock(struct hwspinlock *hwlock, int mode, unsigned long *flags) in __hwspin_trylock() argument
95 if (WARN_ON(!hwlock || (!flags && mode == HWLOCK_IRQSTATE))) in __hwspin_trylock()
96 return -EINVAL; in __hwspin_trylock()
105 * 2. Make the hwspinlock SMP-safe (so we can take it from in __hwspin_trylock()
113 ret = spin_trylock_irqsave(&hwlock->lock, *flags); in __hwspin_trylock()
116 ret = spin_trylock_irq(&hwlock->lock); in __hwspin_trylock()
123 ret = spin_trylock(&hwlock->lock); in __hwspin_trylock()
129 return -EBUSY; in __hwspin_trylock()
132 ret = hwlock->bank->ops->trylock(hwlock); in __hwspin_trylock()
134 /* if hwlock is already taken, undo spin_trylock_* and exit */ in __hwspin_trylock()
138 spin_unlock_irqrestore(&hwlock->lock, *flags); in __hwspin_trylock()
141 spin_unlock_irq(&hwlock->lock); in __hwspin_trylock()
148 spin_unlock(&hwlock->lock); in __hwspin_trylock()
152 return -EBUSY; in __hwspin_trylock()
172 * __hwspin_lock_timeout() - lock an hwspinlock with timeout limit
173 * @hwlock: the hwspinlock to be locked
179 * This function locks the given @hwlock. If the @hwlock
185 * user need some time-consuming or sleepable operations under the hardware
189 * is handled with busy-waiting delays, hence shall not exceed few msecs.
202 * Returns 0 when the @hwlock was successfully taken, and an appropriate
203 * error code otherwise (most notably -ETIMEDOUT if the @hwlock is still
206 int __hwspin_lock_timeout(struct hwspinlock *hwlock, unsigned int to, in __hwspin_lock_timeout() argument
216 ret = __hwspin_trylock(hwlock, mode, flags); in __hwspin_lock_timeout()
217 if (ret != -EBUSY) in __hwspin_lock_timeout()
228 return -ETIMEDOUT; in __hwspin_lock_timeout()
231 return -ETIMEDOUT; in __hwspin_lock_timeout()
235 * Allow platform-specific relax handlers to prevent in __hwspin_lock_timeout()
238 if (hwlock->bank->ops->relax) in __hwspin_lock_timeout()
239 hwlock->bank->ops->relax(hwlock); in __hwspin_lock_timeout()
247 * __hwspin_unlock() - unlock a specific hwspinlock
248 * @hwlock: a previously-acquired hwspinlock which we want to unlock
254 * @hwlock must be already locked before calling this function: it is a bug
255 * to call unlock on a @hwlock that is already unlocked.
265 void __hwspin_unlock(struct hwspinlock *hwlock, int mode, unsigned long *flags) in __hwspin_unlock() argument
267 if (WARN_ON(!hwlock || (!flags && mode == HWLOCK_IRQSTATE))) in __hwspin_unlock()
284 hwlock->bank->ops->unlock(hwlock); in __hwspin_unlock()
289 spin_unlock_irqrestore(&hwlock->lock, *flags); in __hwspin_unlock()
292 spin_unlock_irq(&hwlock->lock); in __hwspin_unlock()
299 spin_unlock(&hwlock->lock); in __hwspin_unlock()
306 * of_hwspin_lock_simple_xlate - translate hwlock_spec to return a lock id
308 * @hwlock_spec: hwlock specifier as found in the device tree
314 * or -EINVAL on invalid specifier cell count.
319 if (WARN_ON(hwlock_spec->args_count != 1)) in of_hwspin_lock_simple_xlate()
320 return -EINVAL; in of_hwspin_lock_simple_xlate()
322 return hwlock_spec->args[0]; in of_hwspin_lock_simple_xlate()
326 * of_hwspin_lock_get_id() - get lock id for an OF phandle-based specific lock
327 * @np: device node from which to request the specific hwlock
328 * @index: index of the hwlock in the list of values
335 * Returns the global lock id number on success, -EPROBE_DEFER if the hwspinlock
336 * device is not yet registered, -EINVAL on invalid args specifier value or an
342 struct hwspinlock *hwlock; in of_hwspin_lock_get_id() local
348 ret = of_parse_phandle_with_args(np, "hwlocks", "#hwlock-cells", index, in of_hwspin_lock_get_id()
354 ret = -ENOENT; in of_hwspin_lock_get_id()
359 ret = -EPROBE_DEFER; in of_hwspin_lock_get_id()
362 hwlock = radix_tree_deref_slot(slot); in of_hwspin_lock_get_id()
363 if (unlikely(!hwlock)) in of_hwspin_lock_get_id()
365 if (radix_tree_deref_retry(hwlock)) { in of_hwspin_lock_get_id()
370 if (hwlock->bank->dev->of_node == args.np) { in of_hwspin_lock_get_id()
380 if (id < 0 || id >= hwlock->bank->num_locks) { in of_hwspin_lock_get_id()
381 ret = -EINVAL; in of_hwspin_lock_get_id()
384 id += hwlock->bank->base_id; in of_hwspin_lock_get_id()
393 * of_hwspin_lock_get_id_byname() - get lock id for an specified hwlock name
394 * @np: device node from which to request the specific hwlock
395 * @name: hwlock name
402 * Returns the global lock id number on success, -EPROBE_DEFER if the hwspinlock
403 * device is not yet registered, -EINVAL on invalid args specifier value or an
411 return -EINVAL; in of_hwspin_lock_get_id_byname()
413 index = of_property_match_string(np, "hwlock-names", name); in of_hwspin_lock_get_id_byname()
421 static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id) in hwspin_lock_register_single() argument
428 ret = radix_tree_insert(&hwspinlock_tree, id, hwlock); in hwspin_lock_register_single()
430 if (ret == -EEXIST) in hwspin_lock_register_single()
438 /* self-sanity check which should never fail */ in hwspin_lock_register_single()
439 WARN_ON(tmp != hwlock); in hwspin_lock_register_single()
448 struct hwspinlock *hwlock = NULL; in hwspin_lock_unregister_single() local
460 hwlock = radix_tree_delete(&hwspinlock_tree, id); in hwspin_lock_unregister_single()
461 if (!hwlock) { in hwspin_lock_unregister_single()
468 return hwlock; in hwspin_lock_unregister_single()
472 * hwspin_lock_register() - register a new hw spinlock device
479 * This function should be called from the underlying platform-specific
489 struct hwspinlock *hwlock; in hwspin_lock_register() local
492 if (!bank || !ops || !dev || !num_locks || !ops->trylock || in hwspin_lock_register()
493 !ops->unlock) { in hwspin_lock_register()
495 return -EINVAL; in hwspin_lock_register()
498 bank->dev = dev; in hwspin_lock_register()
499 bank->ops = ops; in hwspin_lock_register()
500 bank->base_id = base_id; in hwspin_lock_register()
501 bank->num_locks = num_locks; in hwspin_lock_register()
504 hwlock = &bank->lock[i]; in hwspin_lock_register()
506 spin_lock_init(&hwlock->lock); in hwspin_lock_register()
507 hwlock->bank = bank; in hwspin_lock_register()
509 ret = hwspin_lock_register_single(hwlock, base_id + i); in hwspin_lock_register()
517 while (--i >= 0) in hwspin_lock_register()
524 * hwspin_lock_unregister() - unregister an hw spinlock device
527 * This function should be called from the underlying platform-specific
536 struct hwspinlock *hwlock, *tmp; in hwspin_lock_unregister() local
539 for (i = 0; i < bank->num_locks; i++) { in hwspin_lock_unregister()
540 hwlock = &bank->lock[i]; in hwspin_lock_unregister()
542 tmp = hwspin_lock_unregister_single(bank->base_id + i); in hwspin_lock_unregister()
544 return -EBUSY; in hwspin_lock_unregister()
546 /* self-sanity check that should never fail */ in hwspin_lock_unregister()
547 WARN_ON(tmp != hwlock); in hwspin_lock_unregister()
571 * devm_hwspin_lock_unregister() - unregister an hw spinlock device for
576 * This function should be called from the underlying platform-specific
597 * devm_hwspin_lock_register() - register a new hw spinlock device for
605 * This function should be called from the underlying platform-specific
622 return -ENOMEM; in devm_hwspin_lock_register()
637 * __hwspin_lock_request() - tag an hwspinlock as used and power it up
646 static int __hwspin_lock_request(struct hwspinlock *hwlock) in __hwspin_lock_request() argument
648 struct device *dev = hwlock->bank->dev; in __hwspin_lock_request()
653 if (!try_module_get(dev->driver->owner)) { in __hwspin_lock_request()
655 return -EINVAL; in __hwspin_lock_request()
660 if (ret < 0 && ret != -EACCES) { in __hwspin_lock_request()
663 module_put(dev->driver->owner); in __hwspin_lock_request()
670 tmp = radix_tree_tag_clear(&hwspinlock_tree, hwlock_to_id(hwlock), in __hwspin_lock_request()
673 /* self-sanity check that should never fail */ in __hwspin_lock_request()
674 WARN_ON(tmp != hwlock); in __hwspin_lock_request()
680 * hwspin_lock_get_id() - retrieve id number of a given hwspinlock
681 * @hwlock: a valid hwspinlock instance
683 * Returns the id number of a given @hwlock, or -EINVAL if @hwlock is invalid.
685 int hwspin_lock_get_id(struct hwspinlock *hwlock) in hwspin_lock_get_id() argument
687 if (!hwlock) { in hwspin_lock_get_id()
688 pr_err("invalid hwlock\n"); in hwspin_lock_get_id()
689 return -EINVAL; in hwspin_lock_get_id()
692 return hwlock_to_id(hwlock); in hwspin_lock_get_id()
697 * hwspin_lock_request() - request an hwspinlock
703 * id of a given hwlock, use hwspin_lock_get_id()).
711 struct hwspinlock *hwlock; in hwspin_lock_request() local
717 ret = radix_tree_gang_lookup_tag(&hwspinlock_tree, (void **)&hwlock, in hwspin_lock_request()
721 hwlock = NULL; in hwspin_lock_request()
729 ret = __hwspin_lock_request(hwlock); in hwspin_lock_request()
731 hwlock = NULL; in hwspin_lock_request()
735 return hwlock; in hwspin_lock_request()
740 * hwspin_lock_request_specific() - request for a specific hwspinlock
754 struct hwspinlock *hwlock; in hwspin_lock_request_specific() local
760 hwlock = radix_tree_lookup(&hwspinlock_tree, id); in hwspin_lock_request_specific()
761 if (!hwlock) { in hwspin_lock_request_specific()
767 WARN_ON(hwlock_to_id(hwlock) != id); in hwspin_lock_request_specific()
773 hwlock = NULL; in hwspin_lock_request_specific()
778 ret = __hwspin_lock_request(hwlock); in hwspin_lock_request_specific()
780 hwlock = NULL; in hwspin_lock_request_specific()
784 return hwlock; in hwspin_lock_request_specific()
789 * hwspin_lock_free() - free a specific hwspinlock
790 * @hwlock: the specific hwspinlock to free
792 * This function mark @hwlock as free again.
793 * Should only be called with an @hwlock that was retrieved from
800 int hwspin_lock_free(struct hwspinlock *hwlock) in hwspin_lock_free() argument
806 if (!hwlock) { in hwspin_lock_free()
807 pr_err("invalid hwlock\n"); in hwspin_lock_free()
808 return -EINVAL; in hwspin_lock_free()
811 dev = hwlock->bank->dev; in hwspin_lock_free()
815 ret = radix_tree_tag_get(&hwspinlock_tree, hwlock_to_id(hwlock), in hwspin_lock_free()
818 dev_err(dev, "%s: hwlock is already free\n", __func__); in hwspin_lock_free()
820 ret = -EINVAL; in hwspin_lock_free()
828 tmp = radix_tree_tag_set(&hwspinlock_tree, hwlock_to_id(hwlock), in hwspin_lock_free()
832 WARN_ON(tmp != hwlock); in hwspin_lock_free()
834 module_put(dev->driver->owner); in hwspin_lock_free()
844 struct hwspinlock **hwlock = res; in devm_hwspin_lock_match() local
846 if (WARN_ON(!hwlock || !*hwlock)) in devm_hwspin_lock_match()
849 return *hwlock == data; in devm_hwspin_lock_match()
858 * devm_hwspin_lock_free() - free a specific hwspinlock for a managed device
860 * @hwlock: the specific hwspinlock to free
862 * This function mark @hwlock as free again.
863 * Should only be called with an @hwlock that was retrieved from
870 int devm_hwspin_lock_free(struct device *dev, struct hwspinlock *hwlock) in devm_hwspin_lock_free() argument
875 devm_hwspin_lock_match, hwlock); in devm_hwspin_lock_free()
883 * devm_hwspin_lock_request() - request an hwspinlock for a managed device
890 * id of a given hwlock, use hwspin_lock_get_id()).
898 struct hwspinlock **ptr, *hwlock; in devm_hwspin_lock_request() local
904 hwlock = hwspin_lock_request(); in devm_hwspin_lock_request()
905 if (hwlock) { in devm_hwspin_lock_request()
906 *ptr = hwlock; in devm_hwspin_lock_request()
912 return hwlock; in devm_hwspin_lock_request()
917 * devm_hwspin_lock_request_specific() - request for a specific hwspinlock for
934 struct hwspinlock **ptr, *hwlock; in devm_hwspin_lock_request_specific() local
940 hwlock = hwspin_lock_request_specific(id); in devm_hwspin_lock_request_specific()
941 if (hwlock) { in devm_hwspin_lock_request_specific()
942 *ptr = hwlock; in devm_hwspin_lock_request_specific()
948 return hwlock; in devm_hwspin_lock_request_specific()
954 MODULE_AUTHOR("Ohad Ben-Cohen <ohad@wizery.com>");