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: 137495 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp680075lji; Fri, 1 Jun 2018 02:06:08 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJVJau08nst27pYPMAMcR/jDC+FGmbIwZEMmItrfKOs1vxwjq0zXmiitwcLOgPmQbpTcdCL X-Received: by 2002:a62:c4dd:: with SMTP id h90-v6mr10261341pfk.86.1527843968106; Fri, 01 Jun 2018 02:06:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527843968; cv=none; d=google.com; s=arc-20160816; b=XnJf10OIi+/ZDciI9YDIlv/zOM5VNw5n1rj8fIecnkGLYgva21jUgaNwiF0730vsCO oimXfgJDKFt1UpCrmgRYcjPmE1yITTXcI9xVdSnQgN7+HNrYCU4A6J2hBN/gFonBJGSy 9/sYO26aXTtq807gI0PvpyXi65KNZheAlfp/iPenx2BPZDwesDp30L+kjjEPKD7e5/nO l9q4dJIwsaBhiOLpbLQs9xvi/xRrEu+Oxo22Iov1frUlMCH+GQtD0qSQB7WEuz9AFnx/ cQfcHLkjxGnb9WCmX24IuQXt9ifZFi30M9dMcYqgYhtV1Zf8DcCItAsN4aeymP1lrc+N bNHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=4VlYmgtL9umtcT/jbqss2fzBTqJRZHGQG3nKOJDYFUc=; b=M6vCD0nhrH2nx0uiOw1h3s163QB8yEgwo2HPrQPBA83psxnHwPI9jE/yE9fbbCedFC vJ5Femyvds1Z1avN93aOwdRLWWs98QnDtH+QjLBHZjIfjVaB/NJ6ydect21v3AT0VlTd Sj3sPM6nzE8J27C7BNG+qySNVbelesPb89JhlcTHJJGLuHmkrFwEqqcgE1sFcwtaF+Ia il6oXN3vSNJ8vOLQcb58khBXf6cumyo4sI2j8MWA0++L1DJ7wm4utQZxm49RD3fgHSnQ cUx2F9XaXQE/I9GcK8v/BfVrYYLZANZM6DZ2xh7UWkW4hWC4hlRPtINakaZnQl1ewYHx sSpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WXtG+hHv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u123-v6si9954306pfc.178.2018.06.01.02.06.07; Fri, 01 Jun 2018 02:06:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WXtG+hHv; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751390AbeFAJGD (ORCPT + 30 others); Fri, 1 Jun 2018 05:06:03 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:43344 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750919AbeFAJFw (ORCPT ); Fri, 1 Jun 2018 05:05:52 -0400 Received: by mail-pf0-f194.google.com with SMTP id j20-v6so12200511pff.10 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=rZSBU9z1r6dRwW32xcqXw87F88LX0NV9P/mB9JKl4ei5I390qZJiqxxVWwAB4Ld0ib 5Vw7sqJRND+u+s59FLCZCp7W6O+BZDi5qc0MKLA/QiSqlPWRalwSr+1aemjWa59xnwMC Uh+9wz8TgkUxi0ZOZLwUZ4KdlBtyi664gXFrbC6z42KpvE4/7VGiSwJ5STV2m+yoFrHq mYO9QK7yBIXzP6PM75SGrTJ6FE6NmzOMSU70ewQEXthgPQjfDDqA5IshN29FJDJc4hZK xQiRI9Ow5778K+Cfl1cDRa8ftGatP2O7ou+UTcBpwkXy/h2eOyGRl/F/c31rWxvJmSOR oq4w== X-Gm-Message-State: ALKqPwfDf99ahBTveBPQK5AjTzPH8tUE8jy1kJfETEeduePPR/AYXmkc BXlcog7jl9J2J6fetLMYMMZ9FlFz98Y= 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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(+) -- 1.7.9.5 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 */ /**