Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp1254238rdb; Wed, 20 Sep 2023 04:20:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHVuXta8N+H8151+7jaQjtV3Dc94U6avIf4ERFgGnP82jOSlYiJDMzcd5CFQkryz+vPpbtM X-Received: by 2002:a05:6a20:3206:b0:155:5c28:ea6c with SMTP id hl6-20020a056a20320600b001555c28ea6cmr1763700pzc.25.1695208824442; Wed, 20 Sep 2023 04:20:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695208824; cv=none; d=google.com; s=arc-20160816; b=K7WbFof7TWUeIoibq83aYVWrir+fDUZKGrZqIQ4RUnjryrTpgsblxtEHz7wRiTM8dc P2c+kDDElWRSbtQfaqUF2jZSqZwcWY3/EcSX8XEI11iYIQb9/WT0CU6tey5FMQa8JDKw k5lX6LiqAe+7ixuz5z9ZMo90Zx8iUNiMJ29jn4HldeiJe59SsR0gU1DcPsdHrYQRAjzu BSFWOZkf5hr+TlNxWgeF8R8VD29bBFcbwmDQ+da4OTcg1hMDpxdBsTB1NNzORxde6Ewm QphK/LBgHM8Lw2+jmw5AQBwZ65XdqF5qqsDTEnqosQTVA9CEedGuvTWkiQSEKE6pltg2 j8JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=2vemYptFZRyHrcUvDDZoUTIBBuXPoFZTtANkKENYksg=; fh=Rz/CrMLw8BDymOAuYZNV9mT+rhs8T0we00ftm8HcW38=; b=sfL5tlX47lzlnKsEpfbKUmBz+bTy+bff5dJ7zzts5CRuu9VUZsc5/TgSNqMxpTJep+ ed+tn2fePGL48N2YgRnpnfecDAq5i9Ljyy9tlDab+QT8V23Se/8tAsxJA3Ny+Emuu+H8 ewFB8Xgt8vmuPknKtCcgTTf1xUhs+LuNK9BXOK4YuBzioqTM4CHdzoM6ZwOq+d2TGAKc 021VaQV2D9I2ERjC1d9xdgBjQ82XyXWPJ8kn984VHNpR7ns672lY3tVjenq8nlrqnF+X aw/IS6ki/Opsy0spQ9MHRRHe1RNZfNveWlRdU60hQytYqeBVEQtmJcIj6L/MSCdhQfBQ UzbQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id q9-20020a17090a9f4900b0025979e8c246si1327578pjv.70.2023.09.20.04.20.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Sep 2023 04:20:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 134CF80FCBED; Wed, 20 Sep 2023 04:03:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234335AbjITLDO (ORCPT + 99 others); Wed, 20 Sep 2023 07:03:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232488AbjITLDM (ORCPT ); Wed, 20 Sep 2023 07:03:12 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93F2A9E; Wed, 20 Sep 2023 04:03:06 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74A94C433C7; Wed, 20 Sep 2023 11:03:03 +0000 (UTC) Date: Wed, 20 Sep 2023 12:03:00 +0100 From: Catalin Marinas To: Serge Semin Cc: Jan Bottorff , Yann Sionneau , Wolfram Sang , Yann Sionneau , Will Deacon , Jarkko Nikula , Andy Shevchenko , Mika Westerberg , Jan Dabros , Andi Shyti , Philipp Zabel , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] i2c: designware: Fix corrupted memory seen in the ISR Message-ID: References: <37e10c3d-b5ab-75ec-3c96-76e15eb9bef8@sionneau.net> <9de89e14-35bd-415d-97f1-4b6db1258997@os.amperecomputing.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 20 Sep 2023 04:03:23 -0700 (PDT) On Wed, Sep 20, 2023 at 12:05:50AM +0300, Serge Semin wrote: > On Tue, Sep 19, 2023 at 11:54:10AM -0700, Jan Bottorff wrote: > > On 9/19/2023 7:51 AM, Catalin Marinas wrote: > > > While smp_* is ok, it really depends on what the regmap_write() does. Is > > > it a write to a shared peripheral (if not, you may need a DSB)? Does the > > > regmap_write() caller know this? That's why I think having the barrier > > > in dw_reg_write() is better. > > > > > > If you do want to stick to a fix in i2c_dw_xfer_init(), you could go for > > > dma_wmb(). While this is not strictly DMA, it's sharing data with > > > another coherent agent (a different CPU in this instance). The smp_wmb() > > > is more about communication via memory not involving I/O. But this still > > > assumes that the caller knows regmap_write() ends up with an I/O > > > write*() (potentially relaxed). > > Catalin, thank you very much for your messages. The situation is much > clearer now. I should have noted that we indeed talking about > different memory types: Normal and Device memories. Anyway to sum it up > AFAICS the next situation is happening: > 1. some data is updated, > 2. IRQ is activated by means of writel_relaxed() to the > DW_IC_INTR_MASK register. > 3. IRQ is raised and being handled, but the data updated in 1. looked > as corrupted. > > (Jan, correct me if I'm wrong.) > > Since ARM doesn't "guarantee ordering between memory accesses to > different devices, or usually between accesses of different memory > types", most likely the CPU core changes 1. and 2. places > occasionally. So in that case the IRQ handler just doesn't see the > updated data. What needs to be done is to make sure that 2. always > happens after 1. is completed. Outer Shareability domain write-barrier > (DMB(oshst)) does that. Am I right? That's why it's utilized in the > __io_bw() and __dma_wmb() macros implementation. Yes. > Wolfram, Jan seeing the root cause of the problem is in using the > '_relaxed' accessors for the controller CSRs I assume that the problem > might be more generic than just for ARMs, since based on [1] these > accessors "do not guarantee ordering with respect to locking, _normal_ > memory accesses or delay() loops." So theoretically the problem might > get to be met on any other arch if it implements the semantic with the > relaxed normal and device memory accesses execution. > > [1] "KERNEL I/O BARRIER EFFECTS" Documentation/memory-barriers.txt > > If so we need to have give up from using the _relaxed accessors at for > the CSRs which may cause a side effect like IRQ raising. Instead the > normal IO write should be utilized which "wait for the completion of > all prior writes to memory either issued by, or propagated to, the > same thread." [1] Thus I'd suggest the next fix for the problem: > > --- a/drivers/i2c/busses/i2c-designware-common.c > +++ b/drivers/i2c/busses/i2c-designware-common.c > @@ -72,7 +72,10 @@ static int dw_reg_write(void *context, unsigned int reg, unsigned int val) > { > struct dw_i2c_dev *dev = context; > > - writel_relaxed(val, dev->base + reg); > + if (reg == DW_IC_INTR_MASK) > + writel(val, dev->base + reg); > + else > + writel_relaxed(val, dev->base + reg); > > return 0; > } > > (and similar changes for dw_reg_write_swab() and dw_reg_write_word().) This should work as well. -- Catalin