From patchwork Wed Jan 25 19:50:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Schneider-Pargmann X-Patchwork-Id: 48285 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp456536wrn; Wed, 25 Jan 2023 11:53:07 -0800 (PST) X-Google-Smtp-Source: AK7set8ho/Lio/wC7xx1zrWuQFrwjsEqLFt9fbrf8zJxQ6+Nx6QDC3hgXLkjUnmcemwWad3oyNCR X-Received: by 2002:a17:903:32d0:b0:196:2cc1:5082 with SMTP id i16-20020a17090332d000b001962cc15082mr3670143plr.67.1674676387318; Wed, 25 Jan 2023 11:53:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674676387; cv=none; d=google.com; s=arc-20160816; b=lic+c76lL+JgjpIm2b54LcjQlzoo6lhybOd1bnn0DxwkWyMo5/aHGYh8Wx7BZypVJ1 41+PwF8WgAmiKecYNvj8ZRrZWXcqxbxty5FZgsXYlPPY7wuK1Mv1sNifGiHhFcRF84sN 5b3zlyfdgoX5HfwlY4I+6yHEJr7XJFVTm8ibC3OIyEJAV8GZUs5sA9n37fB1DbMOKwgh aWQUcG1Ux0CbT10f9q9C7jWgFb8lTV/hhguOg/R2eQT3KBBIf9bVmD+WnR7ehjPD/Y1p GqkP89cUPO7+Hjm9GP1ce8aS7t52ljaw0emLyh6IQuCVFqThePwQ/TlL2a2HwubnJrWo NL0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=XmkLzqIv0SzR0RQcT/jEh3zGtNBbzIe5FRr1V1LtB9U=; b=R93t0NIkmOqNJKaNeZum2ejz3wHDPvaPSGB32mudlAaVRE9oSSnGDdP016Daltahc1 yHkFkqQ6H+BNPCTnwUSqGb5d2XgyfLAm/moJatKt8CkB3I4OkeNzmv+5OHNPoAYuLH0R gw/v0IUvsrIHbJ3pld2eVhUM+RRRHdniFxvJcK1LuCJpj2mfE3yWf7WFR7OtYYy3Gfxh 6g9mLbWPrrNjD7maBsDEn8rj8QMEXlFuESQ5NGO1m9CAdK9DL51Vh8w/2wCWcXAm79k2 BXLBvPBRDDYKhSGynOeoQKFGM/cmxgxIzcp5y9D69KTHsrNJ/EoKlo6T44HJG04sqxu+ xe8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20210112.gappssmtp.com header.s=20210112 header.b=5WONJCrC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k17-20020a170902761100b001787f1922a7si6274784pll.19.2023.01.25.11.52.55; Wed, 25 Jan 2023 11:53:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20210112.gappssmtp.com header.s=20210112 header.b=5WONJCrC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235995AbjAYTwA (ORCPT + 99 others); Wed, 25 Jan 2023 14:52:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235876AbjAYTve (ORCPT ); Wed, 25 Jan 2023 14:51:34 -0500 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4BFE59E70 for ; Wed, 25 Jan 2023 11:51:13 -0800 (PST) Received: by mail-ej1-x62d.google.com with SMTP id tz11so50685265ejc.0 for ; Wed, 25 Jan 2023 11:51:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XmkLzqIv0SzR0RQcT/jEh3zGtNBbzIe5FRr1V1LtB9U=; b=5WONJCrClQyWE/ihgsINqQJJeHP698AB6PAaoNVNx1j1TOqpWWfRASMZGbfmHnSRDd u0yO2bMILuIYpUHelxnK/vS/2NDe4Uw9a7LPxUHYh4cAgabe0prxA0zdxaJxnHZxNJoN SB+P1AQfXeIA+JOvSJNNhzZuPvy2t7fC5LC7x0Vrq5FY8WtTUDd/JYKcMAYIBBeXgH4s EWOFBlOctvRnJVjETp4TZW4pTl811cyqrap7rLH7f4BHCMxNlfxKKc1sNKXZfV3XYrUj zMyZquhClnG8j1XzyB819tBdPWVQrNkDLAR0+r8XMnWdrDMplDBnKKLrkmh0ZxdTQIfU mu9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XmkLzqIv0SzR0RQcT/jEh3zGtNBbzIe5FRr1V1LtB9U=; b=VnqAhz04fXGYNn1ivHl5WwuUog+d43z05se1CZ21D1wZheL5zPLdULnb57l2aj1+8O ajB/ysXlhiZ90fUMFtUzLNfsbBgLxLe+tWbG5MsFwwqY1OWmKFiso8G2OU59ZSa0m0gr Kdaw3581c3sGD6UTHtLZfUmO7VdI4lrngFuQ4E7BYvZERG6t2SXg4EpiOCz/6miBk1Np aDI4wdPPRhnY5FgB44UjCU8F3sSRg64yICz7k9VDpCUiiwbTemY99LAKnrjWrGfL739b ZrXV1zXqt68XDxpaahDVjhpXFSuJ8RwfT8k95cFdUSXVywDDVkzDOpPyHKTStPFOZl8o qrnA== X-Gm-Message-State: AFqh2ko3Dcl+SFKWe/gAGlRaS9w4IxKIQfs8Ut80uMgEIfy3RZSjAAEL db6IYlVR8VV3vrB5+7384UW5yA== X-Received: by 2002:a17:906:5acf:b0:86c:8f4a:58b4 with SMTP id x15-20020a1709065acf00b0086c8f4a58b4mr33921818ejs.13.1674676272401; Wed, 25 Jan 2023 11:51:12 -0800 (PST) Received: from blmsp.fritz.box ([2001:4091:a247:815f:ef74:e427:628a:752c]) by smtp.gmail.com with ESMTPSA id s15-20020a170906454f00b00872c0bccab2sm2778830ejq.35.2023.01.25.11.51.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jan 2023 11:51:12 -0800 (PST) From: Markus Schneider-Pargmann To: Marc Kleine-Budde , Chandrasekar Ramakrishnan , Wolfgang Grandegger Cc: Vincent MAILHOL , linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Markus Schneider-Pargmann Subject: [PATCH v2 09/18] can: m_can: Implement receive coalescing Date: Wed, 25 Jan 2023 20:50:50 +0100 Message-Id: <20230125195059.630377-10-msp@baylibre.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230125195059.630377-1-msp@baylibre.com> References: <20230125195059.630377-1-msp@baylibre.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756025467227363736?= X-GMAIL-MSGID: =?utf-8?q?1756025467227363736?= m_can offers the possibility to set an interrupt on reaching a watermark level in the receive FIFO. This can be used to implement coalescing. Unfortunately there is no hardware timeout available to trigger an interrupt if only a few messages were received within a given time. To solve this I am using a hrtimer to wake up the irq thread after x microseconds. The timer is always started if receive coalescing is enabled and new received frames were available during an interrupt. The timer is stopped if during a interrupt handling no new data was available. If the timer is started the new item interrupt is disabled and the watermark interrupt takes over. If the timer is not started again, the new item interrupt is enabled again, notifying the handler about every new item received. Signed-off-by: Markus Schneider-Pargmann --- drivers/net/can/m_can/m_can.c | 69 ++++++++++++++++++++++++++++++++--- drivers/net/can/m_can/m_can.h | 7 ++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/drivers/net/can/m_can/m_can.c b/drivers/net/can/m_can/m_can.c index 440bc0536951..a9d028ebc1f4 100644 --- a/drivers/net/can/m_can/m_can.c +++ b/drivers/net/can/m_can/m_can.c @@ -1070,15 +1070,55 @@ static int m_can_echo_tx_event(struct net_device *dev) return err; } +static void m_can_interrupt_enable(struct m_can_classdev *cdev, u32 interrupts) +{ + if (cdev->active_interrupts == interrupts) + return; + cdev->ops->write_reg(cdev, M_CAN_IE, interrupts); + cdev->active_interrupts = interrupts; +} + +static void m_can_coalescing_disable(struct m_can_classdev *cdev) +{ + u32 new_interrupts = cdev->active_interrupts | IR_RF0N; + + hrtimer_cancel(&cdev->irq_timer); + m_can_interrupt_enable(cdev, new_interrupts); +} + +static void m_can_coalescing_update(struct m_can_classdev *cdev, u32 ir) +{ + u32 new_interrupts = cdev->active_interrupts; + bool enable_timer = false; + + if (cdev->rx_coalesce_usecs_irq > 0 && (ir & (IR_RF0N | IR_RF0W))) { + enable_timer = true; + new_interrupts &= ~IR_RF0N; + } else if (!hrtimer_active(&cdev->irq_timer)) { + new_interrupts |= IR_RF0N; + } + + m_can_interrupt_enable(cdev, new_interrupts); + if (enable_timer) { + hrtimer_start(&cdev->irq_timer, + ns_to_ktime(cdev->rx_coalesce_usecs_irq * NSEC_PER_USEC), + HRTIMER_MODE_REL); + } +} + static irqreturn_t m_can_isr(int irq, void *dev_id) { struct net_device *dev = (struct net_device *)dev_id; struct m_can_classdev *cdev = netdev_priv(dev); u32 ir; - if (pm_runtime_suspended(cdev->dev)) + if (pm_runtime_suspended(cdev->dev)) { + m_can_coalescing_disable(cdev); return IRQ_NONE; + } + ir = m_can_read(cdev, M_CAN_IR); + m_can_coalescing_update(cdev, ir); if (!ir) return IRQ_NONE; @@ -1093,13 +1133,17 @@ static irqreturn_t m_can_isr(int irq, void *dev_id) * - state change IRQ * - bus error IRQ and bus error reporting */ - if ((ir & IR_RF0N) || (ir & IR_ERR_ALL_30X)) { + if (ir & (IR_RF0N | IR_RF0W | IR_ERR_ALL_30X)) { cdev->irqstatus = ir; if (!cdev->is_peripheral) { m_can_disable_all_interrupts(cdev); napi_schedule(&cdev->napi); - } else if (m_can_rx_peripheral(dev, ir) < 0) { - goto out_fail; + } else { + int pkts; + + pkts = m_can_rx_peripheral(dev, ir); + if (pkts < 0) + goto out_fail; } } @@ -1135,6 +1179,15 @@ static irqreturn_t m_can_isr(int irq, void *dev_id) return IRQ_HANDLED; } +static enum hrtimer_restart m_can_irq_timer(struct hrtimer *timer) +{ + struct m_can_classdev *cdev = container_of(timer, struct m_can_classdev, irq_timer); + + irq_wake_thread(cdev->net->irq, cdev->net); + + return HRTIMER_NORESTART; +} + static const struct can_bittiming_const m_can_bittiming_const_30X = { .name = KBUILD_MODNAME, .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */ @@ -1275,7 +1328,7 @@ static int m_can_chip_config(struct net_device *dev) /* Disable unused interrupts */ interrupts &= ~(IR_ARA | IR_ELO | IR_DRX | IR_TEFF | IR_TEFW | IR_TFE | IR_TCF | IR_HPM | IR_RF1F | IR_RF1W | IR_RF1N | - IR_RF0F | IR_RF0W); + IR_RF0F); m_can_config_endisable(cdev, true); @@ -1319,6 +1372,7 @@ static int m_can_chip_config(struct net_device *dev) /* rx fifo configuration, blocking mode, fifo size 1 */ m_can_write(cdev, M_CAN_RXF0C, + FIELD_PREP(RXFC_FWM_MASK, cdev->rx_max_coalesced_frames_irq) | FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF0].num) | cdev->mcfg[MRAM_RXF0].off); @@ -1377,7 +1431,7 @@ static int m_can_chip_config(struct net_device *dev) else interrupts &= ~(IR_ERR_LEC_31X); } - m_can_write(cdev, M_CAN_IE, interrupts); + m_can_interrupt_enable(cdev, interrupts); /* route all interrupts to INT0 */ m_can_write(cdev, M_CAN_ILS, ILS_ALL_INT0); @@ -2056,6 +2110,9 @@ int m_can_class_register(struct m_can_classdev *cdev) of_can_transceiver(cdev->net); + hrtimer_init(&cdev->irq_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + cdev->irq_timer.function = m_can_irq_timer; + dev_info(cdev->dev, "%s device registered (irq=%d, version=%d)\n", KBUILD_MODNAME, cdev->net->irq, cdev->version); diff --git a/drivers/net/can/m_can/m_can.h b/drivers/net/can/m_can/m_can.h index d8150d8128e7..35aa3c70b0a3 100644 --- a/drivers/net/can/m_can/m_can.h +++ b/drivers/net/can/m_can/m_can.h @@ -84,6 +84,8 @@ struct m_can_classdev { struct sk_buff *tx_skb; struct phy *transceiver; + struct hrtimer irq_timer; + struct m_can_ops *ops; int version; @@ -92,6 +94,11 @@ struct m_can_classdev { int pm_clock_support; int is_peripheral; + // Cached M_CAN_IE register content + u32 active_interrupts; + u32 rx_max_coalesced_frames_irq; + u32 rx_coalesce_usecs_irq; + struct mram_cfg mcfg[MRAM_CFG_NUM]; };