From patchwork Fri Jun 1 09:04:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "(Exiting) Baolin Wang" X-Patchwork-Id: 10442791 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 420C1601D3 for ; Fri, 1 Jun 2018 09:06:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3392B27BA5 for ; Fri, 1 Jun 2018 09:06:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2447D28C28; Fri, 1 Jun 2018 09:06:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A5DA27BA5 for ; Fri, 1 Jun 2018 09:06:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750831AbeFAJGB (ORCPT ); Fri, 1 Jun 2018 05:06:01 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:45248 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750830AbeFAJFw (ORCPT ); Fri, 1 Jun 2018 05:05:52 -0400 Received: by mail-pf0-f193.google.com with SMTP id c10-v6so12195092pfi.12 for ; Fri, 01 Jun 2018 02:05:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=4VlYmgtL9umtcT/jbqss2fzBTqJRZHGQG3nKOJDYFUc=; b=WXtG+hHvipgAoHPSr8X/YM8jz4nX2JCT6hAQaRWlZB9F7A3cU71Am5HlSIJFzPBoVw hiBl994Ri4TdegbKsCkAx3+9SmdvOXuHsW2f/nfF2UBBbbozfgblgJt0OGXDAbcAoz+D uSRrV5uIF94APSL+t/vFaWVYJrrM2wjmU5/58= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=4VlYmgtL9umtcT/jbqss2fzBTqJRZHGQG3nKOJDYFUc=; b=AKrIzJW9/rvG4nIZ1OwmYAkCYQRdjOjTDs9Qvy/mpO0SsZF7Se98pfVnZ5z9W/AVXo l9cmy5c2yp4QC9GdxyHArSJ8A0rlLfzv8Q+pnxxkuYTA7Rgv1/QfDTaHgrSp5+cJJvhQ nwosOjT4nVS5um2Mo/i1WuhRjgL29K2QyqYG9rqEbdzplFFfrHkz5lu9ZQp0Euxf+xB6 yoodp01IP8UBCML+x9KEx/TpMWpjAY4HIkRq1ptxCeOSl5+L7W9dFOSJKqu5mdRXrZxQ z9tA736ZObWoGIy3D+UKkyqd0re1gyP0rOExjs3dBYgCX3VKeuUjsDLCdmwQkuoFjgPV 35fQ== X-Gm-Message-State: ALKqPwcR/MJaM3jXHt7TTNYGyOM5q8IBkOzytYig1HtwlRqWr+a4d2JO uAiapTExgedKL77EmSdIVoLLgA== X-Google-Smtp-Source: ADUXVKJAJJxQJKFSTC6OwK8fGSPtdlwz5bP3UoEHm71zQSZhzbwVfTqoWAtgCT+rM6WHlDbuUU+mDQ== X-Received: by 2002:a63:9c3:: with SMTP id 186-v6mr8319380pgj.357.1527843951929; Fri, 01 Jun 2018 02:05:51 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id a4-v6sm89505351pfj.19.2018.06.01.02.05.49 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 01 Jun 2018 02:05:51 -0700 (PDT) From: Baolin Wang To: ohad@wizery.com, bjorn.andersson@linaro.org, broonie@kernel.org Cc: baolin.wang@linaro.org, linux-spi@vger.kernel.org, linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] hwspinlock: Add devm_xxx() APIs to request/free hwlock Date: Fri, 1 Jun 2018 17:04:53 +0800 Message-Id: <51d01c563a00774b4db87a01d5fdcdbc342b075c.1527843425.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1be6adeb558fada77de79a40cd5ef7dc2719c945.1527843425.git.baolin.wang@linaro.org> References: <1be6adeb558fada77de79a40cd5ef7dc2719c945.1527843425.git.baolin.wang@linaro.org> In-Reply-To: <1be6adeb558fada77de79a40cd5ef7dc2719c945.1527843425.git.baolin.wang@linaro.org> References: <1be6adeb558fada77de79a40cd5ef7dc2719c945.1527843425.git.baolin.wang@linaro.org> Sender: linux-remoteproc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-remoteproc@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- drivers/hwspinlock/hwspinlock_core.c | 110 ++++++++++++++++++++++++++++++++++ include/linux/hwspinlock.h | 22 +++++++ 2 files changed, 132 insertions(+) diff --git a/drivers/hwspinlock/hwspinlock_core.c b/drivers/hwspinlock/hwspinlock_core.c index 00bde06..6aa9ea1 100644 --- a/drivers/hwspinlock/hwspinlock_core.c +++ b/drivers/hwspinlock/hwspinlock_core.c @@ -743,6 +743,116 @@ int hwspin_lock_free(struct hwspinlock *hwlock) } 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 omap_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 f165cc8..4f1f943 100644 --- a/include/linux/hwspinlock.h +++ b/include/linux/hwspinlock.h @@ -75,6 +75,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 */ @@ -140,6 +144,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 */ /**