Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1712773imu; Wed, 28 Nov 2018 13:55:51 -0800 (PST) X-Google-Smtp-Source: AFSGD/WHwzNKe1Jb3iFP4FwTVmn/HDSAQ6UVqalRGCvz6rKtjYDeLw7G243JT+tIDR8YnYGy7Yq3 X-Received: by 2002:a17:902:780a:: with SMTP id p10mr39717005pll.54.1543442151427; Wed, 28 Nov 2018 13:55:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543442151; cv=none; d=google.com; s=arc-20160816; b=uerXHFf9pXEuiJnOSmrn/HkWDUNdRtKCVhYc7TYFavGMXOiwFdv+jgxdLv5huXZa6p dkc/4wqUuIO0/6hKjix0B2hkH5GNsnVxoG/32zQzTq4IYp3PKZvrBHJTcllKpdoKwRTt cE7LUJzEJTQWYw0Yaf7SgqlFYMcrgKRIfsU29QIh3PEAyiKrieh6HtpdDk8YdH5SYSI7 TguhY8W1MZZM+XYbfypGWITzng2Vw/g6pjQiax/oAnZQUOfglbXiKU/uMM8w5uGW6ZYO WOMJfzYEIk8mUY6BKFb2PmY7AptpFz7+uIcwxO7aIQz8tyto9i/Q1hO6U39N1qHJ+uuj ybQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=sXEj+PXbn5+ZF1R74vQGU/EYlKa72oVl8p0DVGsSvkY=; b=sRDLmB38r5okr2GYSgWicnEXerh8KKBV9b+gnTZvY4TIFHHJ6zVzI2EJF5pfFxycLf hWnggChcZ2EUOxQ6dOEVSTKOI96ggkdcZ8Apm3saxTSUAFZCwUjxyw1PGJ0S6QVzgP69 6+fKIU0s8B8qOY+zv56/4UNu7INxJ0IQB+3pot/hBetVzvFztXqP7nxOXyjKATbfpbDG bkWKra2T4BF5gxSJoQQVNmWyTtUsbwjDX/a4IirqXsKwD2y/gQ617HjiTm+mPGjTfhW/ 0STzHTNlRRZ5M2Y2VNPEgVqEcLnMplXci9mTtsHJQ5FsUdjFSzCl6mMfrd9i2OuhHPKY G1yg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h32si8656921pgh.276.2018.11.28.13.55.35; Wed, 28 Nov 2018 13:55:51 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726414AbeK2I5s (ORCPT + 99 others); Thu, 29 Nov 2018 03:57:48 -0500 Received: from sauhun.de ([88.99.104.3]:41670 "EHLO pokefinder.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726307AbeK2I5s (ORCPT ); Thu, 29 Nov 2018 03:57:48 -0500 Received: from localhost (pd95c76c9.dip0.t-ipconnect.de [217.92.118.201]) by pokefinder.org (Postfix) with ESMTPSA id 292FC2E3542; Wed, 28 Nov 2018 22:54:41 +0100 (CET) Date: Wed, 28 Nov 2018 22:54:40 +0100 From: Wolfram Sang To: Jae Hyun Yoo Cc: Brendan Higgins , Rob Herring , Joel Stanley , Benjamin Herrenschmidt , Mark Rutland , Andrew Jeffery , linux-i2c@vger.kernel.org, openbmc@lists.ozlabs.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-aspeed@lists.ozlabs.org, linux-kernel@vger.kernel.org, Jarkko Nikula , James Feist , Vernon Mauery Subject: Re: [PATCH i2c-next v9 5/5] i2c: aspeed: Add bus idle waiting logic for multi-master use cases Message-ID: <20181128215434.GA1666@kunai> References: <20181030210917.32711-1-jae.hyun.yoo@linux.intel.com> <20181030210917.32711-6-jae.hyun.yoo@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181030210917.32711-6-jae.hyun.yoo@linux.intel.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 30, 2018 at 02:09:16PM -0700, Jae Hyun Yoo wrote: > In multi-master environment, this driver's master cannot know > exactly when a peer master sends data to this driver's slave so a > case can be happened that this master tries to send data through > the master_xfer function but slave data from peer master is still > being processed by this driver. So, that I get this correct: your IP core cannot detect a bus busy state when its own slave address is acessed? Well, I know HW can have bugs, but I still wonder because a bus is busy as soon as another START has been detected, independent of which device is going to be accessed. Even if so, why can't you use a mutex to prevent any master_xfer while slave_xfer is ongoing? Shouldn't that be enough according to the above description? Why do you need the delay? I might be missing something... > To prevent any state corruption in the case, this patch adds > checking code if any slave operation is ongoing and it waits up to > the bus timeout duration before starting a master_xfer operation. > > Signed-off-by: Jae Hyun Yoo > Reviewed-by: Brendan Higgins > --- > drivers/i2c/busses/i2c-aspeed.c | 55 ++++++++++++++++++++++++--------- > 1 file changed, 40 insertions(+), 15 deletions(-) > > diff --git a/drivers/i2c/busses/i2c-aspeed.c b/drivers/i2c/busses/i2c-aspeed.c > index 833b6b6a4c7e..30c3ab3a4844 100644 > --- a/drivers/i2c/busses/i2c-aspeed.c > +++ b/drivers/i2c/busses/i2c-aspeed.c > @@ -12,6 +12,7 @@ > > #include > #include > +#include > #include > #include > #include > @@ -115,6 +116,9 @@ > /* 0x18 : I2CD Slave Device Address Register */ > #define ASPEED_I2CD_DEV_ADDR_MASK GENMASK(6, 0) > > +/* Busy checking */ > +#define ASPEED_I2C_BUS_BUSY_CHECK_INTERVAL_US (10 * 1000) > + > enum aspeed_i2c_master_state { > ASPEED_I2C_MASTER_INACTIVE, > ASPEED_I2C_MASTER_START, > @@ -156,6 +160,8 @@ struct aspeed_i2c_bus { > int cmd_err; > /* Protected only by i2c_lock_bus */ > int master_xfer_result; > + /* Multi-master */ > + bool multi_master; > #if IS_ENABLED(CONFIG_I2C_SLAVE) > struct i2c_client *slave; > enum aspeed_i2c_slave_state slave_state; > @@ -596,27 +602,44 @@ static irqreturn_t aspeed_i2c_bus_irq(int irq, void *dev_id) > return irq_remaining ? IRQ_NONE : IRQ_HANDLED; > } > > +static int aspeed_i2c_check_bus_busy(struct aspeed_i2c_bus *bus) > +{ > + unsigned long timeout; > + > + if (bus->multi_master) { > + might_sleep(); > + /* Initialize it only when multi_master is set */ > + timeout = jiffies + bus->adap.timeout; > + } > + > + for (;;) { > + if (!(readl(bus->base + ASPEED_I2C_CMD_REG) & > + ASPEED_I2CD_BUS_BUSY_STS)) > +#if IS_ENABLED(CONFIG_I2C_SLAVE) > + if (bus->slave_state == ASPEED_I2C_SLAVE_STOP) > +#endif > + return 0; > + if (!bus->multi_master) > + break; > + if (time_after(jiffies, timeout)) > + break; > + usleep_range((ASPEED_I2C_BUS_BUSY_CHECK_INTERVAL_US >> 2) + 1, > + ASPEED_I2C_BUS_BUSY_CHECK_INTERVAL_US); > + } > + > + return aspeed_i2c_recover_bus(bus); > +} > + > static int aspeed_i2c_master_xfer(struct i2c_adapter *adap, > struct i2c_msg *msgs, int num) > { > struct aspeed_i2c_bus *bus = i2c_get_adapdata(adap); > unsigned long time_left, flags; > - int ret = 0; > > - spin_lock_irqsave(&bus->lock, flags); > - bus->cmd_err = 0; > - > - /* If bus is busy, attempt recovery. We assume a single master > - * environment. > - */ > - if (readl(bus->base + ASPEED_I2C_CMD_REG) & ASPEED_I2CD_BUS_BUSY_STS) { > - spin_unlock_irqrestore(&bus->lock, flags); > - ret = aspeed_i2c_recover_bus(bus); > - if (ret) > - return ret; > - spin_lock_irqsave(&bus->lock, flags); > - } > + if (aspeed_i2c_check_bus_busy(bus)) > + return -EAGAIN; > > + spin_lock_irqsave(&bus->lock, flags); > bus->cmd_err = 0; > bus->msgs = msgs; > bus->msgs_index = 0; > @@ -827,7 +850,9 @@ static int aspeed_i2c_init(struct aspeed_i2c_bus *bus, > if (ret < 0) > return ret; > > - if (!of_property_read_bool(pdev->dev.of_node, "multi-master")) > + if (of_property_read_bool(pdev->dev.of_node, "multi-master")) > + bus->multi_master = true; > + else > fun_ctrl_reg |= ASPEED_I2CD_MULTI_MASTER_DIS; > > /* Enable Master Mode */ > -- > 2.19.1 >