From patchwork Thu Feb 2 10:43:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Machon X-Patchwork-Id: 51848 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp164262wrn; Thu, 2 Feb 2023 02:47:33 -0800 (PST) X-Google-Smtp-Source: AK7set+29Ut3kFIv6rJntpk5ujj+1HIRWZYV6hjA5skGbrxZ4jgIq1JcSsQT4JIzGwzxYe6Nh803 X-Received: by 2002:a05:6402:f24:b0:4a0:d950:9dc4 with SMTP id i36-20020a0564020f2400b004a0d9509dc4mr6278736eda.40.1675334853163; Thu, 02 Feb 2023 02:47:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675334853; cv=none; d=google.com; s=arc-20160816; b=NPTFw4TuU/lGag1OgBeF2oyli0Tngy8KnewVahzd+gLnxKeF45iMZq9DvQypicAY/y 0Tukg2PIw8yqkKW+x59fLl4rGMzjzG7weiGa/gAwfrRllIocZ2b8DcLbZbCDrGRSWTcL lEhupfEQNoXBMxH9ygkgt7OnyGqXEGUWE3RW2cp0DNPX9N+CYlLQCxLNaslKUvwKt/2O 5izJH6lK259kJTQgtKH2eGdtctXE1momcgXvD1UuqV9P5e8bQDvNlrXRzHO/XkipbE88 MvswCYC0+zGrWdrjhDGMZEOt9NaGqACi0Rhok784Aij5rhpnW8SMEBCydPFG5VjXjTA0 rK2w== 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=n3o6Qub1pkCRk+lnTxsT+wFv56cisNNYmUdY3sLkP2Y=; b=EdNuKZczItYmy3b5UvL4bSIZ9kUXr3v9KoNz0c5312WgjkYTlzKiT0OC6B389E9cF5 bNKRkCa/x3cSM1F3n0gPaWZOzdoKdf7d5wflU5Zt32PB2BPpk0+8CJhi+o6iqybcfltA 89wA5NRbk2eyxa74HwFRnse8ui/QszjKQghKWlNfL7pspOghN+z5X48btKltJrrYU9EL 8+PFT3NvDzYISD5dXVzeSSIwDHhOZo05THf6qf58WrvWjAK/RkBb+jSfBJlN043a6dNc kRWmtCGeP+nqMf2uYdni4T3Qv8UnQes8awnXZX2ivWp1cajWrTw7eKiJXZ+pADpe4ZxV cVxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microchip.com header.s=mchp header.b=o8yYbE3k; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y17-20020a056402441100b0049ffab53ab4si32928421eda.631.2023.02.02.02.47.08; Thu, 02 Feb 2023 02:47:33 -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=@microchip.com header.s=mchp header.b=o8yYbE3k; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232732AbjBBKpK (ORCPT + 99 others); Thu, 2 Feb 2023 05:45:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232801AbjBBKot (ORCPT ); Thu, 2 Feb 2023 05:44:49 -0500 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.153.233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE75F8CA95; Thu, 2 Feb 2023 02:44:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1675334683; x=1706870683; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eYSUSNvcMtkcencWWMzkog0EFZDpyw2n8vcZMXE9pIg=; b=o8yYbE3k09OY2O53aqJlzzZjc2gKO0r6p8T7QXOdmd5mX7X5IIR7XOtR DpuSM/vb2Dk+srdNtQYRq4lHJ8WpXuTroT/VT/O/y6m6NF9HGNC97XdrW GWWF6m+JIgxrZyq+1hQ0mgDj4CyF5+GrRPWd4jJ+vzIH5SvP9D/wNeO+7 Pvjc9BPMi7bMn4U5/CW597clgAkwCxbh294vKkbqVWsiJ2sdmHUvrNKS+ cu3y1xvXZIBvv+ndOFi7wUHp1KRXA4sjwef5WjO6OxqSCehu7dB66425O LFeQA9AqB/D8jBP7j6u2UnLEEhu/IHjMbmJbkYWh13uT5tOmw0YsDOqw0 w==; X-IronPort-AV: E=Sophos;i="5.97,267,1669100400"; d="scan'208";a="199297152" Received: from unknown (HELO email.microchip.com) ([170.129.1.10]) by esa5.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 02 Feb 2023 03:44:43 -0700 Received: from chn-vm-ex02.mchp-main.com (10.10.85.144) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Thu, 2 Feb 2023 03:44:41 -0700 Received: from DEN-LT-70577.microchip.com (10.10.115.15) by chn-vm-ex02.mchp-main.com (10.10.85.144) with Microsoft SMTP Server id 15.1.2507.16 via Frontend Transport; Thu, 2 Feb 2023 03:44:38 -0700 From: Daniel Machon To: CC: , , , , , , , , , , , , , , , , , Subject: [PATCH net-next 06/10] net: microchip: sparx5: add function for calculating PTP basetime Date: Thu, 2 Feb 2023 11:43:51 +0100 Message-ID: <20230202104355.1612823-7-daniel.machon@microchip.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230202104355.1612823-1-daniel.machon@microchip.com> References: <20230202104355.1612823-1-daniel.machon@microchip.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham 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?1756715919334788121?= X-GMAIL-MSGID: =?utf-8?q?1756715919334788121?= Add a new function for calculating PTP basetime, required by the stream gate scheduler to calculate gate state (open / close). Signed-off-by: Daniel Machon Reviewed-by: Simon Horman --- .../ethernet/microchip/sparx5/sparx5_main.h | 5 ++ .../ethernet/microchip/sparx5/sparx5_ptp.c | 3 +- .../ethernet/microchip/sparx5/sparx5_qos.c | 57 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_main.h b/drivers/net/ethernet/microchip/sparx5/sparx5_main.h index 709cad534f50..fd71b2ede49a 100644 --- a/drivers/net/ethernet/microchip/sparx5/sparx5_main.h +++ b/drivers/net/ethernet/microchip/sparx5/sparx5_main.h @@ -396,6 +396,7 @@ int sparx5_ptp_txtstamp_request(struct sparx5_port *port, void sparx5_ptp_txtstamp_release(struct sparx5_port *port, struct sk_buff *skb); irqreturn_t sparx5_ptp_irq_handler(int irq, void *args); +int sparx5_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts); /* sparx5_vcap_impl.c */ int sparx5_vcap_init(struct sparx5 *sparx5); @@ -481,6 +482,10 @@ int sparx5_psfp_fm_add(struct sparx5 *sparx5, u32 uidx, struct sparx5_psfp_fm *fm, u32 *id); int sparx5_psfp_fm_del(struct sparx5 *sparx5, u32 id); +/* sparx5_qos.c */ +void sparx5_new_base_time(struct sparx5 *sparx5, const u32 cycle_time, + const ktime_t org_base_time, ktime_t *new_base_time); + /* Clock period in picoseconds */ static inline u32 sparx5_clk_period(enum sparx5_core_clockfreq cclock) { diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c b/drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c index 0ed1ea7727c5..af85d66248b2 100644 --- a/drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c +++ b/drivers/net/ethernet/microchip/sparx5/sparx5_ptp.c @@ -476,8 +476,7 @@ static int sparx5_ptp_settime64(struct ptp_clock_info *ptp, return 0; } -static int sparx5_ptp_gettime64(struct ptp_clock_info *ptp, - struct timespec64 *ts) +int sparx5_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts) { struct sparx5_phc *phc = container_of(ptp, struct sparx5_phc, info); struct sparx5 *sparx5 = phc->sparx5; diff --git a/drivers/net/ethernet/microchip/sparx5/sparx5_qos.c b/drivers/net/ethernet/microchip/sparx5/sparx5_qos.c index 379e540e5e6a..ebfdbbf0a1ce 100644 --- a/drivers/net/ethernet/microchip/sparx5/sparx5_qos.c +++ b/drivers/net/ethernet/microchip/sparx5/sparx5_qos.c @@ -9,6 +9,63 @@ #include "sparx5_main.h" #include "sparx5_qos.h" +/* Calculate new base_time based on cycle_time. + * + * The hardware requires a base_time that is always in the future. + * We define threshold_time as current_time + (2 * cycle_time). + * If base_time is below threshold_time this function recalculates it to be in + * the interval: + * threshold_time <= base_time < (threshold_time + cycle_time) + * + * A very simple algorithm could be like this: + * new_base_time = org_base_time + N * cycle_time + * using the lowest N so (new_base_time >= threshold_time + */ +void sparx5_new_base_time(struct sparx5 *sparx5, const u32 cycle_time, + const ktime_t org_base_time, ktime_t *new_base_time) +{ + ktime_t current_time, threshold_time, new_time; + struct timespec64 ts; + u64 nr_of_cycles_p2; + u64 nr_of_cycles; + u64 diff_time; + + new_time = org_base_time; + + sparx5_ptp_gettime64(&sparx5->phc[SPARX5_PHC_PORT].info, &ts); + current_time = timespec64_to_ktime(ts); + threshold_time = current_time + (2 * cycle_time); + diff_time = threshold_time - new_time; + nr_of_cycles = div_u64(diff_time, cycle_time); + nr_of_cycles_p2 = 1; /* Use 2^0 as start value */ + + if (new_time >= threshold_time) { + *new_base_time = new_time; + return; + } + + /* Calculate the smallest power of 2 (nr_of_cycles_p2) + * that is larger than nr_of_cycles. + */ + while (nr_of_cycles_p2 < nr_of_cycles) + nr_of_cycles_p2 <<= 1; /* Next (higher) power of 2 */ + + /* Add as big chunks (power of 2 * cycle_time) + * as possible for each power of 2 + */ + while (nr_of_cycles_p2) { + if (new_time < threshold_time) { + new_time += cycle_time * nr_of_cycles_p2; + while (new_time < threshold_time) + new_time += cycle_time * nr_of_cycles_p2; + new_time -= cycle_time * nr_of_cycles_p2; + } + nr_of_cycles_p2 >>= 1; /* Next (lower) power of 2 */ + } + new_time += cycle_time; + *new_base_time = new_time; +} + /* Max rates for leak groups */ static const u32 spx5_hsch_max_group_rate[SPX5_HSCH_LEAK_GRP_CNT] = { 1048568, /* 1.049 Gbps */