From 5560f70cad996e7d90d1c141bcbca0df214eefc9 Mon Sep 17 00:00:00 2001 From: Baolin Wang Date: Fri, 22 Jun 2018 16:08:58 +0800 Subject: hwspinlock: Add one new API to support getting a specific hwlock by the name The hardware spinlock binding already supplied the 'hwlock-names' property to match and get a specific hwlock, but did not supply one API for users to get a specific hwlock by the hwlock name. So this patch introduces one API to support this requirement. Signed-off-by: Baolin Wang Signed-off-by: Bjorn Andersson --- drivers/hwspinlock/hwspinlock_core.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'drivers') diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwspinlock_core.c index d16e6a3d38e8..bea358604bb2 100644 --- a/drivers/hwspinlock/hwspinlock_core.c +++ b/drivers/hwspinlock/hwspinlock_core.c @@ -367,6 +367,35 @@ out: } EXPORT_SYMBOL_GPL(of_hwspin_lock_get_id); +/** + * of_hwspin_lock_get_id_byname() - get lock id for an specified hwlock name + * @np: device node from which to request the specific hwlock + * @name: hwlock name + * + * This function provides a means for DT users of the hwspinlock module to + * get the global lock id of a specific hwspinlock using the specified name of + * the hwspinlock device, so that it can be requested using the normal + * hwspin_lock_request_specific() API. + * + * Returns the global lock id number on success, -EPROBE_DEFER if the hwspinlock + * device is not yet registered, -EINVAL on invalid args specifier value or an + * appropriate error as returned from the OF parsing of the DT client node. + */ +int of_hwspin_lock_get_id_byname(struct device_node *np, const char *name) +{ + int index; + + if (!name) + return -EINVAL; + + index = of_property_match_string(np, "hwlock-names", name); + if (index < 0) + return index; + + return of_hwspin_lock_get_id(np, index); +} +EXPORT_SYMBOL_GPL(of_hwspin_lock_get_id_byname); + static int hwspin_lock_register_single(struct hwspinlock *hwlock, int id) { struct hwspinlock *tmp; -- cgit v1.2.3 From 4f1acd758b08d93306d13010c74d38dd8d9f5768 Mon Sep 17 00:00:00 2001 From: Baolin Wang Date: Fri, 22 Jun 2018 16:08:59 +0800 Subject: hwspinlock: Add devm_xxx() APIs to request/free hwlock This patch introduces some devm_xxx() APIs to help to request or free the hwlocks, which will help to simplify the cleanup code for drivers requesting one hwlock, ensuring that the hwlock is automatically freed whenever the device is unbound. Signed-off-by: Baolin Wang Signed-off-by: Bjorn Andersson --- drivers/hwspinlock/hwspinlock_core.c | 110 +++++++++++++++++++++++++++++++++++ include/linux/hwspinlock.h | 22 +++++++ 2 files changed, 132 insertions(+) (limited to 'drivers') diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwspinlock_core.c index bea358604bb2..d542b6fbf0f2 100644 --- a/drivers/hwspinlock/hwspinlock_core.c +++ b/drivers/hwspinlock/hwspinlock_core.c @@ -735,6 +735,116 @@ out: } EXPORT_SYMBOL_GPL(hwspin_lock_free); +static int devm_hwspin_lock_match(struct device *dev, void *res, void *data) +{ + struct hwspinlock **hwlock = res; + + if (WARN_ON(!hwlock || !*hwlock)) + return 0; + + return *hwlock == data; +} + +static void devm_hwspin_lock_release(struct device *dev, void *res) +{ + hwspin_lock_free(*(struct hwspinlock **)res); +} + +/** + * devm_hwspin_lock_free() - free a specific hwspinlock for a managed device + * @dev: the device to free the specific hwspinlock + * @hwlock: the specific hwspinlock to free + * + * This function mark @hwlock as free again. + * Should only be called with an @hwlock that was retrieved from + * an earlier call to hwspin_lock_request{_specific}. + * + * Should be called from a process context (might sleep) + * + * Returns 0 on success, or an appropriate error code on failure + */ +int devm_hwspin_lock_free(struct device *dev, struct hwspinlock *hwlock) +{ + int ret; + + ret = devres_release(dev, devm_hwspin_lock_release, + devm_hwspin_lock_match, hwlock); + WARN_ON(ret); + + return ret; +} +EXPORT_SYMBOL_GPL(devm_hwspin_lock_free); + +/** + * devm_hwspin_lock_request() - request an hwspinlock for a managed device + * @dev: the device to request an hwspinlock + * + * This function should be called by users of the hwspinlock device, + * in order to dynamically assign them an unused hwspinlock. + * Usually the user of this lock will then have to communicate the lock's id + * to the remote core before it can be used for synchronization (to get the + * id of a given hwlock, use hwspin_lock_get_id()). + * + * Should be called from a process context (might sleep) + * + * Returns the address of the assigned hwspinlock, or NULL on error + */ +struct hwspinlock *devm_hwspin_lock_request(struct device *dev) +{ + struct hwspinlock **ptr, *hwlock; + + ptr = devres_alloc(devm_hwspin_lock_release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return ERR_PTR(-ENOMEM); + + hwlock = hwspin_lock_request(); + if (!IS_ERR(hwlock)) { + *ptr = hwlock; + devres_add(dev, ptr); + } else { + devres_free(ptr); + } + + return hwlock; +} +EXPORT_SYMBOL_GPL(devm_hwspin_lock_request); + +/** + * devm_hwspin_lock_request_specific() - request for a specific hwspinlock for + * a managed device + * @dev: the device to request the specific hwspinlock + * @id: index of the specific hwspinlock that is requested + * + * This function should be called by users of the hwspinlock module, + * in order to assign them a specific hwspinlock. + * Usually early board code will be calling this function in order to + * reserve specific hwspinlock ids for predefined purposes. + * + * Should be called from a process context (might sleep) + * + * Returns the address of the assigned hwspinlock, or NULL on error + */ +struct hwspinlock *devm_hwspin_lock_request_specific(struct device *dev, + unsigned int id) +{ + struct hwspinlock **ptr, *hwlock; + + ptr = devres_alloc(devm_hwspin_lock_release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return ERR_PTR(-ENOMEM); + + hwlock = hwspin_lock_request_specific(id); + if (!IS_ERR(hwlock)) { + *ptr = hwlock; + devres_add(dev, ptr); + } else { + devres_free(ptr); + } + + return hwlock; +} +EXPORT_SYMBOL_GPL(devm_hwspin_lock_request_specific); + MODULE_LICENSE("GPL v2"); MODULE_DESCRIPTION("Hardware spinlock interface"); MODULE_AUTHOR("Ohad Ben-Cohen "); diff --git a/include/linux/hwspinlock.h b/include/linux/hwspinlock.h index 2b6f389a3133..dfd05938a3cb 100644 --- a/include/linux/hwspinlock.h +++ b/include/linux/hwspinlock.h @@ -67,6 +67,10 @@ int __hwspin_lock_timeout(struct hwspinlock *, unsigned int, int, int __hwspin_trylock(struct hwspinlock *, int, unsigned long *); void __hwspin_unlock(struct hwspinlock *, int, unsigned long *); int of_hwspin_lock_get_id_byname(struct device_node *np, const char *name); +int devm_hwspin_lock_free(struct device *dev, struct hwspinlock *hwlock); +struct hwspinlock *devm_hwspin_lock_request(struct device *dev); +struct hwspinlock *devm_hwspin_lock_request_specific(struct device *dev, + unsigned int id); #else /* !CONFIG_HWSPINLOCK */ @@ -132,6 +136,24 @@ int of_hwspin_lock_get_id_byname(struct device_node *np, const char *name) return 0; } +static inline +int devm_hwspin_lock_free(struct device *dev, struct hwspinlock *hwlock) +{ + return 0; +} + +static inline struct hwspinlock *devm_hwspin_lock_request(struct device *dev) +{ + return ERR_PTR(-ENODEV); +} + +static inline +struct hwspinlock *devm_hwspin_lock_request_specific(struct device *dev, + unsigned int id) +{ + return ERR_PTR(-ENODEV); +} + #endif /* !CONFIG_HWSPINLOCK */ /** -- cgit v1.2.3 From c102780acdbc22e5f664cfaf760eaccffbd6c385 Mon Sep 17 00:00:00 2001 From: Baolin Wang Date: Fri, 22 Jun 2018 16:09:00 +0800 Subject: hwspinlock: Add devm_xxx() APIs to register/unregister one hwlock controller This patch introduces devm_hwspin_lock_register() and devm_hwspin_lock_unregister() interfaces to help to register or unregister one hardware spinlock controller, that will help to simplify the cleanup code for hwspinlock drivers. Signed-off-by: Baolin Wang Signed-off-by: Bjorn Andersson --- drivers/hwspinlock/hwspinlock_core.c | 82 ++++++++++++++++++++++++++++++++++++ include/linux/hwspinlock.h | 6 +++ 2 files changed, 88 insertions(+) (limited to 'drivers') diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwspinlock_core.c index d542b6fbf0f2..ba27465c9d9b 100644 --- a/drivers/hwspinlock/hwspinlock_core.c +++ b/drivers/hwspinlock/hwspinlock_core.c @@ -529,6 +529,88 @@ int hwspin_lock_unregister(struct hwspinlock_device *bank) } EXPORT_SYMBOL_GPL(hwspin_lock_unregister); +static void devm_hwspin_lock_unreg(struct device *dev, void *res) +{ + hwspin_lock_unregister(*(struct hwspinlock_device **)res); +} + +static int devm_hwspin_lock_device_match(struct device *dev, void *res, + void *data) +{ + struct hwspinlock_device **bank = res; + + if (WARN_ON(!bank || !*bank)) + return 0; + + return *bank == data; +} + +/** + * devm_hwspin_lock_unregister() - unregister an hw spinlock device for + * a managed device + * @dev: the backing device + * @bank: the hwspinlock device, which usually provides numerous hw locks + * + * This function should be called from the underlying platform-specific + * implementation, to unregister an existing (and unused) hwspinlock. + * + * Should be called from a process context (might sleep) + * + * Returns 0 on success, or an appropriate error code on failure + */ +int devm_hwspin_lock_unregister(struct device *dev, + struct hwspinlock_device *bank) +{ + int ret; + + ret = devres_release(dev, devm_hwspin_lock_unreg, + devm_hwspin_lock_device_match, bank); + WARN_ON(ret); + + return ret; +} +EXPORT_SYMBOL_GPL(devm_hwspin_lock_unregister); + +/** + * devm_hwspin_lock_register() - register a new hw spinlock device for + * a managed device + * @dev: the backing device + * @bank: the hwspinlock device, which usually provides numerous hw locks + * @ops: hwspinlock handlers for this device + * @base_id: id of the first hardware spinlock in this bank + * @num_locks: number of hwspinlocks provided by this device + * + * This function should be called from the underlying platform-specific + * implementation, to register a new hwspinlock device instance. + * + * Should be called from a process context (might sleep) + * + * Returns 0 on success, or an appropriate error code on failure + */ +int devm_hwspin_lock_register(struct device *dev, + struct hwspinlock_device *bank, + const struct hwspinlock_ops *ops, + int base_id, int num_locks) +{ + struct hwspinlock_device **ptr; + int ret; + + ptr = devres_alloc(devm_hwspin_lock_unreg, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return -ENOMEM; + + ret = hwspin_lock_register(bank, dev, ops, base_id, num_locks); + if (!ret) { + *ptr = bank; + devres_add(dev, ptr); + } else { + devres_free(ptr); + } + + return ret; +} +EXPORT_SYMBOL_GPL(devm_hwspin_lock_register); + /** * __hwspin_lock_request() - tag an hwspinlock as used and power it up * diff --git a/include/linux/hwspinlock.h b/include/linux/hwspinlock.h index dfd05938a3cb..94064cce6962 100644 --- a/include/linux/hwspinlock.h +++ b/include/linux/hwspinlock.h @@ -71,6 +71,12 @@ int devm_hwspin_lock_free(struct device *dev, struct hwspinlock *hwlock); struct hwspinlock *devm_hwspin_lock_request(struct device *dev); struct hwspinlock *devm_hwspin_lock_request_specific(struct device *dev, unsigned int id); +int devm_hwspin_lock_unregister(struct device *dev, + struct hwspinlock_device *bank); +int devm_hwspin_lock_register(struct device *dev, + struct hwspinlock_device *bank, + const struct hwspinlock_ops *ops, + int base_id, int num_locks); #else /* !CONFIG_HWSPINLOCK */ -- cgit v1.2.3 From 38ce606533385e56b9216cab2e1128930652c315 Mon Sep 17 00:00:00 2001 From: Baolin Wang Date: Fri, 22 Jun 2018 16:09:02 +0800 Subject: hwspinlock: Fix one comment mistake Fix one comment mistake with correct function names. Signed-off-by: Baolin Wang Signed-off-by: Bjorn Andersson --- drivers/hwspinlock/hwspinlock_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwspinlock_core.c index ba27465c9d9b..e16d648c30f3 100644 --- a/drivers/hwspinlock/hwspinlock_core.c +++ b/drivers/hwspinlock/hwspinlock_core.c @@ -767,7 +767,7 @@ EXPORT_SYMBOL_GPL(hwspin_lock_request_specific); * * This function mark @hwlock as free again. * Should only be called with an @hwlock that was retrieved from - * an earlier call to omap_hwspin_lock_request{_specific}. + * an earlier call to hwspin_lock_request{_specific}. * * Should be called from a process context (might sleep) * -- cgit v1.2.3 From d4942c119c978f1c6649212d2e42696bef43dd27 Mon Sep 17 00:00:00 2001 From: Baolin Wang Date: Fri, 22 Jun 2018 16:09:04 +0800 Subject: spi: sprd: Replace of_hwspin_lock_get_id() with of_hwspin_lock_get_id_byname() Now the hwlock core has supplied new function to get a specific hwlock id by one hwlock name, which is more clear for users. So change to use of_hwspin_lock_get_id_byname(). Acked-by: Mark Brown Signed-off-by: Baolin Wang Signed-off-by: Bjorn Andersson --- drivers/spi/spi-sprd-adi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/spi/spi-sprd-adi.c b/drivers/spi/spi-sprd-adi.c index 197d4b0d81af..2d4c0419e208 100644 --- a/drivers/spi/spi-sprd-adi.c +++ b/drivers/spi/spi-sprd-adi.c @@ -459,7 +459,7 @@ static int sprd_adi_probe(struct platform_device *pdev) sadi->slave_pbase = res->start + ADI_SLAVE_OFFSET; sadi->ctlr = ctlr; sadi->dev = &pdev->dev; - ret = of_hwspin_lock_get_id(np, 0); + ret = of_hwspin_lock_get_id_byname(np, "adi"); if (ret < 0) { dev_err(&pdev->dev, "can not get the hardware spinlock\n"); goto put_ctlr; -- cgit v1.2.3 From c8d049896729423a979d2838175d251b9cfee3b5 Mon Sep 17 00:00:00 2001 From: Baolin Wang Date: Fri, 22 Jun 2018 16:09:05 +0800 Subject: spi: sprd: Change to use devm_hwspin_lock_request_specific() Change to use devm_hwspin_lock_request_specific() instead of freeing the hwlock explicitly when unbound the device. Acked-by: Mark Brown Signed-off-by: Baolin Wang Signed-off-by: Bjorn Andersson --- drivers/spi/spi-sprd-adi.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'drivers') diff --git a/drivers/spi/spi-sprd-adi.c b/drivers/spi/spi-sprd-adi.c index 2d4c0419e208..df5960bddfe6 100644 --- a/drivers/spi/spi-sprd-adi.c +++ b/drivers/spi/spi-sprd-adi.c @@ -465,7 +465,7 @@ static int sprd_adi_probe(struct platform_device *pdev) goto put_ctlr; } - sadi->hwlock = hwspin_lock_request_specific(ret); + sadi->hwlock = devm_hwspin_lock_request_specific(&pdev->dev, ret); if (!sadi->hwlock) { ret = -ENXIO; goto put_ctlr; @@ -483,7 +483,7 @@ static int sprd_adi_probe(struct platform_device *pdev) ret = devm_spi_register_controller(&pdev->dev, ctlr); if (ret) { dev_err(&pdev->dev, "failed to register SPI controller\n"); - goto free_hwlock; + goto put_ctlr; } sadi->restart_handler.notifier_call = sprd_adi_restart_handler; @@ -491,13 +491,11 @@ static int sprd_adi_probe(struct platform_device *pdev) ret = register_restart_handler(&sadi->restart_handler); if (ret) { dev_err(&pdev->dev, "can not register restart handler\n"); - goto free_hwlock; + goto put_ctlr; } return 0; -free_hwlock: - hwspin_lock_free(sadi->hwlock); put_ctlr: spi_controller_put(ctlr); return ret; @@ -509,7 +507,6 @@ static int sprd_adi_remove(struct platform_device *pdev) struct sprd_adi *sadi = spi_controller_get_devdata(ctlr); unregister_restart_handler(&sadi->restart_handler); - hwspin_lock_free(sadi->hwlock); return 0; } -- cgit v1.2.3 From ddb34f480d1b8051bc18e6ff22e93a6c9a33f94f Mon Sep 17 00:00:00 2001 From: Baolin Wang Date: Thu, 28 Jun 2018 10:32:21 +0800 Subject: hwspinlock: Fix incorrect return pointers The commit 4f1acd758b08 ("hwspinlock: Add devm_xxx() APIs to request/free hwlock") introduces one bug, that will return one error pointer if failed to request one hwlock, but we expect NULL pointer on error for consumers. This patch will fix this issue. Reported-by: Dan Carpenter Signed-off-by: Baolin Wang Signed-off-by: Bjorn Andersson --- drivers/hwspinlock/hwspinlock_core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'drivers') diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwspinlock_core.c index e16d648c30f3..2bad40d42210 100644 --- a/drivers/hwspinlock/hwspinlock_core.c +++ b/drivers/hwspinlock/hwspinlock_core.c @@ -877,10 +877,10 @@ struct hwspinlock *devm_hwspin_lock_request(struct device *dev) ptr = devres_alloc(devm_hwspin_lock_release, sizeof(*ptr), GFP_KERNEL); if (!ptr) - return ERR_PTR(-ENOMEM); + return NULL; hwlock = hwspin_lock_request(); - if (!IS_ERR(hwlock)) { + if (hwlock) { *ptr = hwlock; devres_add(dev, ptr); } else { @@ -913,10 +913,10 @@ struct hwspinlock *devm_hwspin_lock_request_specific(struct device *dev, ptr = devres_alloc(devm_hwspin_lock_release, sizeof(*ptr), GFP_KERNEL); if (!ptr) - return ERR_PTR(-ENOMEM); + return NULL; hwlock = hwspin_lock_request_specific(id); - if (!IS_ERR(hwlock)) { + if (hwlock) { *ptr = hwlock; devres_add(dev, ptr); } else { -- cgit v1.2.3