Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3944466pxb; Mon, 8 Feb 2021 04:16:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJzaz8oeZENk2w5aG22ds6IYt6JlLAE+biAztbxj1s4rEM+425IaZKfI9XQPExSkpTLP2U/X X-Received: by 2002:a05:6402:2216:: with SMTP id cq22mr16845968edb.125.1612786574599; Mon, 08 Feb 2021 04:16:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612786574; cv=none; d=google.com; s=arc-20160816; b=WiXFxmXmkwvr16YnMGdUSfCD9Inhv0PHRXYfDt/ASTffKMrlXp5VphqiGk3rejOg35 PTRoHdP9epKm3CK2sYsEd7beEMejkgYMLbEhV4XKy+UoeuyuRsTkpdcm/nqMsDHcCMN0 /h/PB437whbtluiF1qVyr3IHWDBkC19HLePpAyFf7Pcv35iLuvu6vJ/i47+HvnAC6fiS rlXG026zjMUwbdao5gItDkd2biUISPHen1tq7SQkpkKZvFVTpHkjIyNY8VpR9QRCaeVI OGtiRbGztmCscEi0bG8fsVF+Z9d76DD2gPIMYPZnna9JcYK6wPQWCRtZS4bhPksVnpgS fwBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from :dkim-signature; bh=LfB7ydJjgNU6qdDbdeb61u1uczQgf3qmcl9sbfbMLTw=; b=hoT3SZHs6neIiSbEjU+ZCm5zWfWEcrEe47V7zwRaxE7nUWY3oDWw6quC6wdcOU4lEb x1CSHYwQoj39LATb/ruRpq1EmVF7BPheW/gBTvM9zh6S7yOsYQYiJuxYo8ZF3XsCXVvP zdWKM9FpiYNylslKOAeDjH5B+v/XMODKNLfXLNI6LQHlXW3R3PX2ZoX8LQcXsYoA4L3S AUkAWYCN+bNt4dCGVNOL9gBqj7m+LZeGI8Cbj9VKZ4dk5V6jQaIbQUbrqgAHmDlPoxgj NrqVTopd4zjaxSYlfy1jJavknYiKuj68hY5FuTH4c5bc3V9iWhsezIL4j1PSJtpQb2O4 2Vfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MDKZtz8S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g22si11354684edw.133.2021.02.08.04.15.51; Mon, 08 Feb 2021 04:16:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MDKZtz8S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232064AbhBHMOT (ORCPT + 99 others); Mon, 8 Feb 2021 07:14:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232881AbhBHLwl (ORCPT ); Mon, 8 Feb 2021 06:52:41 -0500 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5C14C06174A; Mon, 8 Feb 2021 03:52:00 -0800 (PST) Received: by mail-pf1-x436.google.com with SMTP id 18so7415121pfz.3; Mon, 08 Feb 2021 03:52:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=LfB7ydJjgNU6qdDbdeb61u1uczQgf3qmcl9sbfbMLTw=; b=MDKZtz8SgocSr4SOIJfB/BuBwfQNX+yKBUQHzKIH820YxZIfw4/k4N6e9sePUmkpEV slyVXhcajaz8SG8pfj61dGT7lPhBk0rigFwpOQj1h19c8jwN5IKV5/2euUO6WcWv6UvS qtnTo+ouGZAVCu6YABj6Vu3fbUnclja4EEQ/PknHrH8bDx/wvanRU6fejESTRl7isVM8 tDQvEGCf9WuTPJD30fhaIusQ+eyW1CD+G9iRRi7T01SdiB6aVeIwURD8A2klRf/QohKQ V/ijd1FU5wWA0vna+qRg+XC7rx03+ztsRgj6sa3N64EOqA0WQsHiTBDb0PSi08rhY+oY L3aQ== 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; bh=LfB7ydJjgNU6qdDbdeb61u1uczQgf3qmcl9sbfbMLTw=; b=eBFErr1JS8yBL5HEyHwqpAmaY3oHYtU90PjoSBXVWp+VVT0BJyxhzDABtfPcNDskz4 7NIG+2A8fiEfw8aIEmTebiQe2El+B2oaoYywuZTPJYmH67pwAj6dMBYoLdyQEFNntrez 7+Ckf15PobWZOiSLo1N/0cmEvHSUSzwH/NUm+w5LFx4ErqFNd2Mi6C2gh7cyBtHTextT jBjge9EHYJRtGIZNd+s413GZ32Npav/NFpYSGdwAkv5aTwgPxO/qnzRr72Ub2ZKCWo+L k54cmGTDimYbq3h/Z3zoj8SvbGM/T3uyxtViV0YsZ4FbFQuJ7xfxytmN9AJrGzi5S2if x20g== X-Gm-Message-State: AOAM530merGo3hdXSWAKVXx9gsh4OdA0gxxwXyCv2Bc5nrTmV+YBEGzp 6HFQTlJqjHWsa2pxm9kTTB4z5Zzs2jWjGA== X-Received: by 2002:a63:d257:: with SMTP id t23mr8645306pgi.290.1612785120501; Mon, 08 Feb 2021 03:52:00 -0800 (PST) Received: from lenovo.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id a9sm12875564pfr.204.2021.02.08.03.51.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Feb 2021 03:51:59 -0800 (PST) From: Orson Zhai To: Rob Herring , Baolin Wang , Jassi Brar , Chunyan Zhang Cc: Mark Brown , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, haidong.yao@unisoc.com, Orson Zhai Subject: [PATCH 1/3] mailbox: sprd: Introduce refcnt when clients requests/free channels Date: Mon, 8 Feb 2021 19:51:02 +0800 Message-Id: <1612785064-3072-1-git-send-email-orsonzhai@gmail.com> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Orson Zhai Unisoc mailbox has no way to be enabled/disabled for any single channel. They can only be set to startup or shutdown as a whole device at same time. Add a variable to count references to avoid mailbox FIFO being reset unexpectedly when clients are requesting or freeing channels. Also add a lock to dismiss possible conflicts from register r/w in different startup or shutdown threads. Fixes: ca27fc26cd22 ("mailbox: sprd: Add Spreadtrum mailbox driver") Signed-off-by: Orson Zhai --- drivers/mailbox/sprd-mailbox.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/drivers/mailbox/sprd-mailbox.c b/drivers/mailbox/sprd-mailbox.c index f6fab24..e606f52 100644 --- a/drivers/mailbox/sprd-mailbox.c +++ b/drivers/mailbox/sprd-mailbox.c @@ -60,6 +60,8 @@ struct sprd_mbox_priv { struct clk *clk; u32 outbox_fifo_depth; + struct mutex lock; + u32 refcnt; struct mbox_chan chan[SPRD_MBOX_CHAN_MAX]; }; @@ -215,18 +217,22 @@ static int sprd_mbox_startup(struct mbox_chan *chan) struct sprd_mbox_priv *priv = to_sprd_mbox_priv(chan->mbox); u32 val; - /* Select outbox FIFO mode and reset the outbox FIFO status */ - writel(0x0, priv->outbox_base + SPRD_MBOX_FIFO_RST); + mutex_lock(&priv->lock); + if (priv->refcnt++ == 0) { + /* Select outbox FIFO mode and reset the outbox FIFO status */ + writel(0x0, priv->outbox_base + SPRD_MBOX_FIFO_RST); - /* Enable inbox FIFO overflow and delivery interrupt */ - val = readl(priv->inbox_base + SPRD_MBOX_IRQ_MSK); - val &= ~(SPRD_INBOX_FIFO_OVERFLOW_IRQ | SPRD_INBOX_FIFO_DELIVER_IRQ); - writel(val, priv->inbox_base + SPRD_MBOX_IRQ_MSK); + /* Enable inbox FIFO overflow and delivery interrupt */ + val = readl(priv->inbox_base + SPRD_MBOX_IRQ_MSK); + val &= ~(SPRD_INBOX_FIFO_OVERFLOW_IRQ | SPRD_INBOX_FIFO_DELIVER_IRQ); + writel(val, priv->inbox_base + SPRD_MBOX_IRQ_MSK); - /* Enable outbox FIFO not empty interrupt */ - val = readl(priv->outbox_base + SPRD_MBOX_IRQ_MSK); - val &= ~SPRD_OUTBOX_FIFO_NOT_EMPTY_IRQ; - writel(val, priv->outbox_base + SPRD_MBOX_IRQ_MSK); + /* Enable outbox FIFO not empty interrupt */ + val = readl(priv->outbox_base + SPRD_MBOX_IRQ_MSK); + val &= ~SPRD_OUTBOX_FIFO_NOT_EMPTY_IRQ; + writel(val, priv->outbox_base + SPRD_MBOX_IRQ_MSK); + } + mutex_unlock(&priv->lock); return 0; } @@ -235,9 +241,13 @@ static void sprd_mbox_shutdown(struct mbox_chan *chan) { struct sprd_mbox_priv *priv = to_sprd_mbox_priv(chan->mbox); - /* Disable inbox & outbox interrupt */ - writel(SPRD_INBOX_FIFO_IRQ_MASK, priv->inbox_base + SPRD_MBOX_IRQ_MSK); - writel(SPRD_OUTBOX_FIFO_IRQ_MASK, priv->outbox_base + SPRD_MBOX_IRQ_MSK); + mutex_lock(&priv->lock); + if (--priv->refcnt == 0) { + /* Disable inbox & outbox interrupt */ + writel(SPRD_INBOX_FIFO_IRQ_MASK, priv->inbox_base + SPRD_MBOX_IRQ_MSK); + writel(SPRD_OUTBOX_FIFO_IRQ_MASK, priv->outbox_base + SPRD_MBOX_IRQ_MSK); + } + mutex_unlock(&priv->lock); } static const struct mbox_chan_ops sprd_mbox_ops = { @@ -266,6 +276,8 @@ static int sprd_mbox_probe(struct platform_device *pdev) return -ENOMEM; priv->dev = dev; + priv->refcnt = 0; + mutex_init(&priv->lock); /* * The Spreadtrum mailbox uses an inbox to send messages to the target -- 2.7.4