From patchwork Thu Dec 26 12:12:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Srinivas Neeli X-Patchwork-Id: 1215482 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=xilinx.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=xilinx.onmicrosoft.com header.i=@xilinx.onmicrosoft.com header.b="tBAiD52D"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 47k8476Tnlz9sR8 for ; Thu, 26 Dec 2019 23:13:11 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726513AbfLZMMo (ORCPT ); Thu, 26 Dec 2019 07:12:44 -0500 Received: from mail-bn8nam12on2079.outbound.protection.outlook.com ([40.107.237.79]:39137 "EHLO NAM12-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726236AbfLZMMn (ORCPT ); Thu, 26 Dec 2019 07:12:43 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ShyIz+cJRl4mbIZWDhOQmL4HrDZvRnE79m3RxlVzCZQA8lTUxSTJQaWEytibI2Gy5Fe/9qAzD5RpNNH6LaSmKEtrNz/KIe1HsNxB2vbrW2nxglHap+bm2XQAQ9DT27qvVhYJcqaZ4UACRVp80eyYzcSb8gZEKVFkDVoHY1LgbFXwnUA+WhilfADVrLtjygkaND+MBNAWBB9+twsG28Si5DQKiiyKBBvuQyaW33RhejLDRLk4MpC1HugJFBY7WMox9BaQ6CTF0dGroq9qbJkW2rOv4aavkMCjk0D4udiS2ZXKmnDHedV/hb9wunf8JaQ465xTaJo1XaKXXirlzotmFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1fyYbeD7xkDdqJGMSDyInYDzwHTHRUCsJpqN5Zx6nqM=; b=KEPXzLJyBONixNYKZPlUmBPQblk+mcxzxbuDs+JpSKbmgpAA1/DFwOdpvrwutS4kESP2axAWNXBKT2aV9QwAEmA5tu07K4iMJy5sZqW2iPoO8NR8m7erDfsORLQjm5Yx4o1CUd+JOotfD3rW9a+OWZZnKXCylk9uU+55y1Ax4o0yGskE/7VRgTE18UfwNsCfC1SP27p3pxSPdvZ84fXN4v+LM9KKOrdRF4akT6KMbr2Ht+RHZ7VtY9QuwJbUvNGDrVlRG2gi7NwpdNO9bzUvxRwmrZ7+gbMsJX+zXNdvafknuanKjOPZ3YDduj2SM1BgX/Fb4t4ITDXeeEIRkWNz6g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 149.199.60.83) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=xilinx.com; dmarc=bestguesspass action=none header.from=xilinx.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xilinx.onmicrosoft.com; s=selector2-xilinx-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1fyYbeD7xkDdqJGMSDyInYDzwHTHRUCsJpqN5Zx6nqM=; b=tBAiD52Dj8+AyWmDIgQPebG19lswLHYlKsElYE7nNW7UHHgKI4YgYwKZYW6g7f0WMx0GCBIDQWZ78FQPoZPrc9PQpoiTPt7Re3XtTQ7GIJWU3HVQLJ8gAxOWLUwzt/BgV+ce8CP8ejg/vQ/vwW5y5PK53BMbUQ2NDv3FGtRT6vM= Received: from SN4PR0201CA0026.namprd02.prod.outlook.com (2603:10b6:803:2e::12) by BYAPR02MB5527.namprd02.prod.outlook.com (2603:10b6:a03:a3::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2581.11; Thu, 26 Dec 2019 12:12:37 +0000 Received: from SN1NAM02FT050.eop-nam02.prod.protection.outlook.com (2a01:111:f400:7e44::209) by SN4PR0201CA0026.outlook.office365.com (2603:10b6:803:2e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2581.11 via Frontend Transport; Thu, 26 Dec 2019 12:12:37 +0000 Authentication-Results: spf=pass (sender IP is 149.199.60.83) smtp.mailfrom=xilinx.com; vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=bestguesspass action=none header.from=xilinx.com; Received-SPF: Pass (protection.outlook.com: domain of xilinx.com designates 149.199.60.83 as permitted sender) receiver=protection.outlook.com; client-ip=149.199.60.83; helo=xsj-pvapsmtpgw01; Received: from xsj-pvapsmtpgw01 (149.199.60.83) by SN1NAM02FT050.mail.protection.outlook.com (10.152.72.128) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.2559.14 via Frontend Transport; Thu, 26 Dec 2019 12:12:36 +0000 Received: from unknown-38-66.xilinx.com ([149.199.38.66] helo=xsj-pvapsmtp01) by xsj-pvapsmtpgw01 with esmtp (Exim 4.63) (envelope-from ) id 1ikS0K-0003JR-Af; Thu, 26 Dec 2019 04:12:36 -0800 Received: from [127.0.0.1] (helo=localhost) by xsj-pvapsmtp01 with smtp (Exim 4.63) (envelope-from ) id 1ikS0F-0007wQ-6y; Thu, 26 Dec 2019 04:12:31 -0800 Received: from xsj-pvapsmtp01 (smtp-fallback.xilinx.com [149.199.38.66] (may be forged)) by xsj-smtp-dlp1.xlnx.xilinx.com (8.13.8/8.13.1) with ESMTP id xBQCCQ58023019; Thu, 26 Dec 2019 04:12:26 -0800 Received: from [10.140.6.6] (helo=xhdappanad40.xilinx.com) by xsj-pvapsmtp01 with esmtp (Exim 4.63) (envelope-from ) id 1ikS0A-0007v9-0x; Thu, 26 Dec 2019 04:12:26 -0800 From: Srinivas Neeli To: linus.walleij@linaro.org, bgolaszewski@baylibre.com, michal.simek@xilinx.com, shubhrajyoti.datta@xilinx.com Cc: linux-gpio@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, git@xilinx.com Subject: [PATCH 2/8] gpio: zynq: protect direction in/out with a spinlock Date: Thu, 26 Dec 2019 17:42:12 +0530 Message-Id: <1577362338-28744-3-git-send-email-srinivas.neeli@xilinx.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1577362338-28744-1-git-send-email-srinivas.neeli@xilinx.com> References: <1577362338-28744-1-git-send-email-srinivas.neeli@xilinx.com> X-RCIS-Action: ALLOW X-TM-AS-Product-Ver: IMSS-7.1.0.1224-8.2.0.1013-23620.005 X-TM-AS-User-Approved-Sender: Yes;Yes X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:149.199.60.83; IPV:; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(4636009)(136003)(396003)(376002)(346002)(39860400002)(199004)(189003)(81156014)(4326008)(44832011)(2616005)(9786002)(186003)(336012)(8936002)(426003)(478600001)(6636002)(2906002)(8676002)(81166006)(5660300002)(7696005)(107886003)(70586007)(36756003)(26005)(6666004)(70206006)(316002)(356004)(42866002); DIR:OUT; SFP:1101; SCL:1; SRVR:BYAPR02MB5527; H:xsj-pvapsmtpgw01; FPR:; SPF:Pass; LANG:en; PTR:unknown-60-83.xilinx.com; A:1; MX:1; MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4f47e128-54ec-42f0-9b51-08d789fce588 X-MS-TrafficTypeDiagnostic: BYAPR02MB5527: X-Microsoft-Antispam-PRVS: X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-Forefront-PRVS: 02638D901B X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NVB8339yRNb5Ot1cZnBUh3gdyYg0CLeKuU4X56M81vDSnodJblrPgFswpiRc5wID67V0I24wzQ6mskzeODErWOXRA1/dhQCWT4vY6pe0isa+O7K1RUMTj7JKppGXpSuMkE/iJiwIRrT8FBiY9T464fM8n5o8mhrcTyI8m8/0n1+/I7ZIsUKIvH6htQ7/cE6Qiaeg9O2m9amEAMTMV4OJGmOiofuE5hM8IEPXLSDORC6c8DiN54aQeszmFNRFtr5lAAONaxH/q6UeT/+aOQXyCMQZPNoar7U+r6cbvVr+Egu9VmLGRqgZAJ/fHKotjM3PfNEB1uBeRWjT+R9rff5fmmXdiay1O7xHEUOGj7Ns5HFogNOQXb0+sWlp2oItnGYAgGpdDxL09KAboRYflqiMJjUAzsEpQriKOkJa8zgR4rvMmDPNaSBvtar67MQvpKJpkV3cWCDG69kxA5D+YMuYHoEhWAwZJsrjShM2KF7b7zs= X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Dec 2019 12:12:36.9711 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4f47e128-54ec-42f0-9b51-08d789fce588 X-MS-Exchange-CrossTenant-Id: 657af505-d5df-48d0-8300-c31994686c5c X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=657af505-d5df-48d0-8300-c31994686c5c; Ip=[149.199.60.83]; Helo=[xsj-pvapsmtpgw01] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR02MB5527 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Glenn Langedock Fix race condition when changing the direction (in/out) of the GPIO pin. The read-modify-write sequence (as coded in the driver) isn't atomic and requires synchronization (spinlock). Signed-off-by: Glenn Langedock Signed-off-by: Michal Simek Signed-off-by: Srinivas Neeli --- drivers/gpio/gpio-zynq.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/gpio/gpio-zynq.c b/drivers/gpio/gpio-zynq.c index 05ba16fffdad..9c8b8a397a26 100644 --- a/drivers/gpio/gpio-zynq.c +++ b/drivers/gpio/gpio-zynq.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -116,6 +117,7 @@ struct gpio_regs { * @irq: interrupt for the GPIO device * @p_data: pointer to platform data * @context: context registers + * @dirlock: lock used for direction in/out synchronization */ struct zynq_gpio { struct gpio_chip chip; @@ -124,6 +126,7 @@ struct zynq_gpio { int irq; const struct zynq_platform_data *p_data; struct gpio_regs context; + spinlock_t dirlock; /*lock used for direction in/out synchronization */ }; /** @@ -297,6 +300,7 @@ static int zynq_gpio_dir_in(struct gpio_chip *chip, unsigned int pin) { u32 reg; unsigned int bank_num, bank_pin_num; + unsigned long flags; struct zynq_gpio *gpio = gpiochip_get_data(chip); zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num, gpio); @@ -310,9 +314,11 @@ static int zynq_gpio_dir_in(struct gpio_chip *chip, unsigned int pin) return -EINVAL; /* clear the bit in direction mode reg to set the pin as input */ + spin_lock_irqsave(&gpio->dirlock, flags); reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num)); reg &= ~BIT(bank_pin_num); writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num)); + spin_unlock_irqrestore(&gpio->dirlock, flags); return 0; } @@ -334,11 +340,13 @@ static int zynq_gpio_dir_out(struct gpio_chip *chip, unsigned int pin, { u32 reg; unsigned int bank_num, bank_pin_num; + unsigned long flags; struct zynq_gpio *gpio = gpiochip_get_data(chip); zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num, gpio); /* set the GPIO pin as output */ + spin_lock_irqsave(&gpio->dirlock, flags); reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num)); reg |= BIT(bank_pin_num); writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num)); @@ -347,6 +355,7 @@ static int zynq_gpio_dir_out(struct gpio_chip *chip, unsigned int pin, reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_OUTEN_OFFSET(bank_num)); reg |= BIT(bank_pin_num); writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_OUTEN_OFFSET(bank_num)); + spin_unlock_irqrestore(&gpio->dirlock, flags); /* set the state of the pin */ zynq_gpio_set_value(chip, pin, state); @@ -885,6 +894,8 @@ static int zynq_gpio_probe(struct platform_device *pdev) return ret; } + spin_lock_init(&gpio->dirlock); + pm_runtime_set_active(&pdev->dev); pm_runtime_enable(&pdev->dev); ret = pm_runtime_get_sync(&pdev->dev);