Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1064591ybh; Mon, 13 Jul 2020 08:29:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxUPxYY47k7YFjUbAjPviOSbYjPPo2RBH0RWOLdpQ2P7LXx0/LKs9Ro7FSVRkyEYUfxkocM X-Received: by 2002:aa7:c614:: with SMTP id h20mr91582456edq.29.1594654149599; Mon, 13 Jul 2020 08:29:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594654149; cv=none; d=google.com; s=arc-20160816; b=WQyaXhQWujxOAN6VE4HL6FdEwJ9nWXBmr54kQt5ytg0pnzfD1f8myVYS67Tm0gPeNw YHvwkDF7cdpl59SgELuTSe+2rGobb/ZZXd4kfG6AAeOMHJDDVcPoTCQ1lXlPuC6Tag3O +Z7BCOJBJPXujfF9jJ4FaaYj2W1IF1l6IF+KxR4Ebv8wjCDVEqKE7Vm+TctJQWyyLpbZ 78p04ORhlz6I34uLpHawcCapS0eSpJiL7HwjAbq/96BdxxAnuQ6pGts6bCXosnh5dBV2 w1qzE4tuMG5y1yYuK8/VOEOCE0ggxxRmtan96Drna5igxPnD9mfC/5Xdz8QL6J1/1Hts aQhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=6iEPUhiKr3M4ezuL+SwEmOApae428FvrCM4ScVSRZLg=; b=TrEyI71DJqtrg1Aq9+/B+cm8tRZRn8bnx0iFJ/tOSxAG+0FN/5NSmlIlG6JRxe/zxn WuirDkoggG1c+Ib0dC7rMbqB47PprojfzAIPUy4f/m4Io4nBzFuQngdbnWBixVufCQmm 6ZfJF8sFuIBUxjQEIXZMI9jbQp4u9ilGKQDLGI5iK4bJ5gnD7DRlj6VBBl4nWTpg8K4o d6uNRNcW3D4St73z+sE6hAV8pYH+mgXrJgQJk3kKIST0s3ojbTGgR2yj500sMXJYQqUH pJ+/wsaaqYD2jODcXA+Vuhi37v8HZp0HkzK/igm8xT441zSjdLN5o8T83Qb8PGEATjXZ FH8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="UiqZFl/K"; 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=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 25si9451933ejy.507.2020.07.13.08.28.46; Mon, 13 Jul 2020 08:29:09 -0700 (PDT) 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=@chromium.org header.s=google header.b="UiqZFl/K"; 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=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729837AbgGMP1v (ORCPT + 99 others); Mon, 13 Jul 2020 11:27:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729308AbgGMP1v (ORCPT ); Mon, 13 Jul 2020 11:27:51 -0400 Received: from mail-vs1-xe42.google.com (mail-vs1-xe42.google.com [IPv6:2607:f8b0:4864:20::e42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 397EDC061755 for ; Mon, 13 Jul 2020 08:27:51 -0700 (PDT) Received: by mail-vs1-xe42.google.com with SMTP id d11so4301094vsq.3 for ; Mon, 13 Jul 2020 08:27:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=6iEPUhiKr3M4ezuL+SwEmOApae428FvrCM4ScVSRZLg=; b=UiqZFl/KMjYG1ojWDNqz0ODKAIVixIywN3OlHnGG2fr6KkZAyWS1w80jgSiPdKvVVG aELQbZh/u6Phrtn3SsVKnGVN46QIPLDUVkxVS/PCC1UGE6+lx/g3D+bvJ29lYH6dddsA iElpGnmEacPw6L7VhftHY/BzvTuzTNaXldY5I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=6iEPUhiKr3M4ezuL+SwEmOApae428FvrCM4ScVSRZLg=; b=Peu6KEnefNpcRgHwS+cS3pG8RrPeFkOf+oKtBKHutybi2syB85H1mxJJvNzqcrbCmn 3uE0ArPj4XhxyYQVcMmoT0YaRAYbfgw7ekgj4Exmjnu3yVMSfRQVcLsXxhCHn9Zl3LjF JOFj5e4zcbtTT9jvMF2em6PBZ03YSEn6PuKMEqyJAERg0PuXnbkJfKsAIMGeCBI1zlye JAx7j7eoouNz4u27LCtzBbbTkibru8mEmjhd0vV0LViUAZ2nJEjEKwwW2zS+q/FEoZAk gtqtEvvtU2Yi9VO80u47Vj1izR44Ld4yS1kEOYJaxMwcOac1wHIdgyU0Mo0sRyA8tigo e8Vw== X-Gm-Message-State: AOAM532Gh8sLA9ivlw9a73VxxSPql7sUtXsMq9bW+E2MwTdwWl9gUdFl 47lCVeGpyQJlr9a9P9XM/y3B/wFqCHw= X-Received: by 2002:a67:2d0e:: with SMTP id t14mr20816950vst.22.1594654069663; Mon, 13 Jul 2020 08:27:49 -0700 (PDT) Received: from mail-vs1-f51.google.com (mail-vs1-f51.google.com. [209.85.217.51]) by smtp.gmail.com with ESMTPSA id c62sm2103208vkg.30.2020.07.13.08.27.48 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Jul 2020 08:27:48 -0700 (PDT) Received: by mail-vs1-f51.google.com with SMTP id j186so6793618vsd.10 for ; Mon, 13 Jul 2020 08:27:48 -0700 (PDT) X-Received: by 2002:a67:e046:: with SMTP id n6mr31587759vsl.6.1594654068105; Mon, 13 Jul 2020 08:27:48 -0700 (PDT) MIME-Version: 1.0 References: <20200708141610.1.Ie0d730120b232a86a4eac1e2909bcbec844d1766@changeid> <87lfjreo7m.wl-maz@kernel.org> <87sgdyxvh1.wl-maz@kernel.org> In-Reply-To: <87sgdyxvh1.wl-maz@kernel.org> From: Doug Anderson Date: Mon, 13 Jul 2020 08:27:36 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] pinctrl: qcom: Handle broken PDC dual edge case on sc7180 To: Marc Zyngier Cc: LinusW , Stephen Boyd , linux-arm-msm , Cheng-Yi Chiang , Lina Iyer , Andy Gross , Rajendra Nayak , Maulik Shah , Bjorn Andersson , "open list:GPIO SUBSYSTEM" , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On Sat, Jul 11, 2020 at 2:16 AM Marc Zyngier wrote: > > On Fri, 10 Jul 2020 17:10:55 +0100, > Doug Anderson wrote: > > > > Hi, > > > > On Fri, Jul 10, 2020 at 2:03 AM Marc Zyngier wrote: > > > > > > Hi Doug, > > [...] > > > > > > > > + type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING; > > > > + > > > > + raw_spin_lock_irqsave(&pctrl->lock, flags); > > > > > > What is this lock protecting you against? In both cases, you are > > > already under the irq_desc lock, with interrupts disabled. > > > > We are? I put a breakpoint when the IRQ hits and did a bt. I see > > this (I happen to be on 5.4 at the moment, so hopefully the same as > > mainline): > > > > kgdb_breakpoint+0x3c/0x74 > > msm_gpio_update_dual_edge_parent+0x58/0x17c > > msm_gpio_handle_dual_edge_parent_irq+0x1c/0x30 > > __handle_domain_irq+0x84/0xc4 > > gic_handle_irq+0x170/0x220 > > el1_irq+0xd0/0x180 > > > > I think the stack is missing a few things due to aggressive inlining > > from my compiler, so the true backtrace would be: > > > > msm_gpio_handle_dual_edge_parent_irq() > > generic_handle_irq_desc() > > generic_handle_irq() > > __handle_domain_irq() > > handle_domain_irq() > > gic_handle_irq() > > > > The first place that got the "desc" was generic_handle_irq() and it > > got it via irq_to_desc(). That doesn't seem to do any locking. Then > > generic_handle_irq_desc() just calls a function pointer so no locking > > there either. > > > > ...ah, but maybe what you're saying is that > > msm_gpio_handle_dual_edge_parent_irq() should be holding "desc->lock" > > around the call to msm_gpio_update_dual_edge_parent()? I can do that. > > No, I mentally did a fast-forward to moving this hack into the irq > flow, rather than doing before entering the flow. handle_fasteoi_irq > will take the lock, but obviously not with the current state of this > patch. > > > > > > > > > + do { > > > > + /* Set the parent to catch the next edge */ > > > > + irq_chip_set_type_parent(d, type); > > > > + > > > > + /* > > > > + * Possibly the line changed between when we last read "val" > > > > + * (and decided what edge we needed) and when set the edge. > > > > + * If the value didn't change (or changed and then changed > > > > + * back) then we're done. > > > > + */ > > > > > > If the line changed, shouldn't you actually inject a new interrupt > > > altogether? By changing the polarity more than once, you are > > > effectively loosing edges that should have triggered an interrupt. > > > > Are you sure this is needed? My understanding of edge triggered > > interrupts is that until the interrupt handler is called that all > > edges can be coalesced into a single interrupt. > > It really depends on whether the edges are semantically different, and > I'm not sure you can decide this at the interrupt controller > level. The core IRQ code doesn't give you a way to discriminate > between those, but endpoint drivers could, and could get terminally > confused if the see two rising edges without a falling edge in > between. I have added discussion about this in the commit message for v2. Hopefully it looks OK. NOTE: it's actually quite common for pinctrl hardware to only support single edge and require dual edge emulation in solftware. I think there are at least 4-5 examples that I found pretty easily. ...so I think any drivers that are expecting dual edges to come from an external pin will have code to account for this. > > It's only after the > > interrupt handler is called that it's important to capture new edges. > > So if you have this: > > > > a) Be busy processing another unrelated interrupt > > b) 5 edges happen on the line > > c) Other interrupt finishes > > d) Edge interrupt is acked and handler is called > > > > You'll only get one call to the interrupt handler even though there > > were 5 edges, right? It's only important that you queue another > > interrupt if that interrupt happens after the true interrupt handler > > (the one acting on the edge) has started. > > > > ...actually, in theory you'll get _either_ one or two calls to the > > interrupt handler depending on timing, since the above could also > > happen as: > > > > a) Be busy processing another unrelated interrupt > > b) 4 edges happen on the line > > c) Other interrupt finishes > > d) Edge interrupt is acked and ... > > e) 1 more edge happens on the line > > f) ...handler is called > > g) Edge interrupt is acked and handler is called > > > > > > As long as msm_gpio_update_dual_edge_parent() is called _before_ the > > true interrupt handler is called then what I have should be fine, > > right? > > I don't disagree with any of that, except that being fine at the > irqchip level doesn't necessarily mean being fine at the endpoint > driver level. On the other hand, the HW looks terminally broken, so > maybe it doesn't matter as the drivers will have to be written with > this limitation in mind... > > > > > > > + val = msm_readl_io(pctrl, g) & BIT(g->in_bit); > > > > + if (type == IRQ_TYPE_EDGE_RISING) { > > > > + if (!val) > > > > + break; > > > > + type = IRQ_TYPE_EDGE_FALLING; > > > > + } else if (type == IRQ_TYPE_EDGE_FALLING) { > > > > + if (val) > > > > + break; > > > > + type = IRQ_TYPE_EDGE_RISING; > > > > + } > > > > + } while (loop_limit-- > 0); > > > > + raw_spin_unlock_irqrestore(&pctrl->lock, flags); > > > > + > > > > + if (!loop_limit) > > > > + dev_err(pctrl->dev, "dual-edge irq failed to stabilize\n"); > > > > +} > > > > + > > > > +void msm_gpio_handle_dual_edge_parent_irq(struct irq_desc *desc) > > > > +{ > > > > + struct irq_data *d = &desc->irq_data; > > > > + > > > > + /* Make sure we're primed for the next edge */ > > > > + msm_gpio_update_dual_edge_parent(d); > > > > > > I would have expected this to happen on EOI or ACK, rather than before > > > the flow is actually handled, once you have told the interrupt > > > controller that you were dealing with this interrupt. > > > > Having it on Ack would be ideal, but it appears that the Ack function > > isn't called in this case. That's only called if our handler is > > handle_edge_irq() or handle_level_irq(). See more below. > > Easily fixed, see further down. > > > > > ...I'm pretty sure I don't want it on EOI. Specifically, if I did it > > on EOI then I think I _would_ need to re-queue another interrupt if an > > edge came in msm_gpio_update_dual_edge_parent(). Doing all the edge > > adjustment before calling the true interrupt handler avoids all > > that. > > Requeuing interrupts would be fine, and we have the retrigger callback > for that. This can be used when you want to support level interrupts, > but your interrupt controller only supports edge. Something similar > could be done to deal with dual edge interrupts. > > > > > > > > > + > > > > + /* Pass on to the normal interrupt handler */ > > > > + handle_fasteoi_irq(desc); > > > > > > Is that the right flow? It seems that the current code is using > > > handle_edge_irq. I guess it has been broken so far, and that this > > > patch actually fixes it by forcing a fasteoi flow... > > > > The code today only uses handle_level_irq() / handle_edge_irq() if > > "skip_wake_irqs" wasn't set for this IRQ. In the case that > > "skip_wake_irqs" wasn't set then it leaves the handler alone. I > > definitely had a hard time following all the flow and interactions > > between the pinctrl, PDC, and the GICv3 but I definitely did confirm > > that handle_fasteoi_irq() was the handler that was running when > > "skip_wake_irqs" was set before I stuck mine in the middle. > > OK. > > > I believe how things work today with the "skip_wake_irqs" case is > > that, for the most part, the pinctrl driver stays out of the way for > > setting up and handling IRQs and just passes some calls onto its > > parent (the PDC). The PDC driver is actually quite minimal. There's > > no "Ack" in there and no calls to set an IRQ handler--it seems to just > > rely on the GICv3 doing all that. It looks there is an implicit Ack > > as part of gic_handle_irq() since reading the IAR counts as an Ack. > > > > > > So to try to sum up my understanding: > > > > 1. In the case of "skip_wake_irqs" today there is no acking / handling > > code that is part of pinctrl-msm or the PDC. They just configure > > things to direct to the GICv3. > > > > 2. For my workaround I just need to make sure to intercept myself and > > prime the next edge _before_ the end-user interrupt handler gets > > called. If edges are coalesced before the end-user interrupt handler > > is called then that's OK. > > > > > > I'll await your reply before sending out the next version. Thanks > > much for all your time looking at this! > > So here are my suggestions: > > - Move your dual edge hack to the irq_ack callback > > - On detecting a dual edge interrupt, switch to the > handle_fasteoi_ack_irq flow, which will call the irq_ack callback > > - Get rid of the now useless locking OK, see how v2 looks to you. Sending it out right after I hit "Send' on this message. > I bet you could reuse some of the existing hacks, though I haven't > looked too hard because it is Saturday and this code really makes my > eyes bleed. Thanks again for all your help! For now I'll keep the code separate. While I agree that it could be unified with the existing msm_gpio_update_dual_edge_pos() function, doing so would mean: 1. Changing the logic of msm_gpio_update_dual_edge_pos() slightly. That function does things that we can't quite do as easily. For instance, it reads the current config and queries whether an interrupt is pending both of which didn't seem easy to do in my case. I think it would be fine/possible to change msm_gpio_update_dual_edge_pos() to be more like my function, but I haven't quantified if it would be worse or better performance. 2. Doing do would require an extra level of abstraction and I'm not 100% sure it's worth it. If folks want me to work on trying to unify the two let me know and I can do a v3. -Doug