From patchwork Wed Dec 21 15:25:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Schneider-Pargmann X-Patchwork-Id: 35408 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp3586379wrn; Wed, 21 Dec 2022 07:27:29 -0800 (PST) X-Google-Smtp-Source: AMrXdXubRtbqUScGYztxQgQsEiLXGseK3GA4FCgbN+hcE3Xf8jdNKA7cYb0n9WdX9rGBuzi36kyJ X-Received: by 2002:a17:902:e84c:b0:189:340c:20d2 with SMTP id t12-20020a170902e84c00b00189340c20d2mr2597175plg.23.1671636448946; Wed, 21 Dec 2022 07:27:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671636448; cv=none; d=google.com; s=arc-20160816; b=mPDbzacz18EADi1K2X6u5/zNt1bRNYbBZuYk2KA/bK2kjmMvpyssXFHU1GAVJES4CV N74KJbWJk8+X8XGBpmaPm1/lp0Fdvt/cu9GbzII1aYtAOVbQJi8ND8UVrdgkOvMow5av Gwo2tjlr0AnirnLtzCdkoQsjYFTgsCHw//jaSfFhvjGV3IiQ8DkOW4ICZjvNf9mqsiXz goE5Um4juL+/cLnK8mWSbBUc7c0sYL8uQa2aX7B2XP4GkzzOMkZK3DBFaqgl7aWThSXK /aEJ16JiCgV2AuEmIwZNZr35xSosB2sHjfA0qasU3cxWkrdkNJsYekKFa50XCOYuLG9i 2U4Q== 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=ESQGtd3G8sdaW8PjvebdJG0sAEuwFJzA4JH+s3KzMiY=; b=hIMJM8MZGiJrm5xQW+W97qdOmEpIr8XtKuYPYs0FAovaJeKxMdL7S33xhjn0p/ww46 Tie5uZ+K9kjoGPtjpCPv6E9CgURWaIu2oJYVodX9YCIPUkD6dZNbznz6qPnUvioRaR7s 7WbU2jeDES60h0jQv9x+5f3RPA/lzSzShdCvwa9hZFsQrkYD+h0jynU0kNrmPDlmaDuB sFKbLpjqMD6VuzL3TJeKiacyqr3KqjvlXR0cidtS7fh+ukU3t1nc4/UuId2dozSjW/el 2sa0TD5/8NljdL0J1pI/FapeCTqzCkkhVns00BV+jbaASEdgILNjYSRgOSX8e8uUBDm5 PfFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20210112.gappssmtp.com header.s=20210112 header.b=55Qo9k3T; 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 x21-20020a170902ea9500b00189d0fa14desi15531071plb.620.2022.12.21.07.27.15; Wed, 21 Dec 2022 07:27:28 -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=55Qo9k3T; 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 S234614AbiLUP0k (ORCPT + 99 others); Wed, 21 Dec 2022 10:26:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234662AbiLUPZw (ORCPT ); Wed, 21 Dec 2022 10:25:52 -0500 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CC3E6336 for ; Wed, 21 Dec 2022 07:25:50 -0800 (PST) Received: by mail-ed1-x529.google.com with SMTP id a16so22456325edb.9 for ; Wed, 21 Dec 2022 07:25:50 -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=ESQGtd3G8sdaW8PjvebdJG0sAEuwFJzA4JH+s3KzMiY=; b=55Qo9k3Twc+84P3Y9UOgr69jkxGWxW2fbnPiMqYj7vchsGhFhbgpMlN9hMfhsN1HFU Jo0cOvCfq2KVf/iWA6KFS4T8N1p4wwllBMAnK7CQcS5PK60ej+YQMK+VZGgKROdfs5aa Wxdbjb9QxNI+kPMl0UraudiUiBu6BKEcCvKcIDsUNy3AlMgF0Gaee4AqKQNgtNSnwTYg lppI1j/MN47k4p8YDbizznSNG0HbDH1GoI7NjEA6TWC5/iVvpnVZkFDf7CdDNFB9EcSX RziafkmM/sPpDhJus5U3Ix32+GFvulBzaZweagl3u2I+O+Bx4xtFei8X+A6ujKQx3V7h t4Lg== 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=ESQGtd3G8sdaW8PjvebdJG0sAEuwFJzA4JH+s3KzMiY=; b=h7Th1hHWWGLSJ1HmVvizAsAd7NCcewoLBOyjgPXQWlK8Fxwi20P635z85qXRGGFFX/ u+szX/VM8XLH198nA57H39tUJGipeLTai4hl2RyaTz8oD7APruU/tvIe3L4HaIisQEfQ ge0IcD2u8Q+FOShBHLCeDuem5HGwt7CwrQMx5y0bFNNpL3MnftBq3OAFMnrSacj7L+Je +pn+oZD0NNsXuJ7Y6xIpo5p3rqQhf1P5sQBovGvaayiKiAM3V7MkIQfNULnEVokE28O4 DdcTKRk3E938GC59aBk8rD10dv2TAng97/vaTj5cRJnGMO7Meh/hitpp0acQNGxDAasB hJMw== X-Gm-Message-State: AFqh2koHlxrFSM2myksDW3oQXccJTXaAqsLQibZfnK3DnmhFZU3Mxwrt jA3SO4CLlDC1zctUb5tq/Zm5yA== X-Received: by 2002:a05:6402:4141:b0:469:ee22:d97a with SMTP id x1-20020a056402414100b00469ee22d97amr1846383eda.32.1671636348916; Wed, 21 Dec 2022 07:25:48 -0800 (PST) Received: from blmsp.fritz.box ([2001:4091:a245:805c:8713:84e4:2a9e:cbe8]) by smtp.gmail.com with ESMTPSA id n19-20020aa7c793000000b0045cf4f72b04sm7105428eds.94.2022.12.21.07.25.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Dec 2022 07:25:48 -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 09/18] can: m_can: Implement receive coalescing Date: Wed, 21 Dec 2022 16:25:28 +0100 Message-Id: <20221221152537.751564-10-msp@baylibre.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221221152537.751564-1-msp@baylibre.com> References: <20221221152537.751564-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?1752837861424997297?= X-GMAIL-MSGID: =?utf-8?q?1752837861424997297?= 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 9b5ad222aef7..2e664313101b 100644 --- a/drivers/net/can/m_can/m_can.c +++ b/drivers/net/can/m_can/m_can.c @@ -1060,15 +1060,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; @@ -1083,13 +1123,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; } } @@ -1125,6 +1169,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 */ @@ -1258,7 +1311,7 @@ static void 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); @@ -1302,6 +1355,7 @@ static void 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); @@ -1360,7 +1414,7 @@ static void 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); @@ -2030,6 +2084,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 d2c584232c1a..4943e1e9aff0 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]; };