Received: by 2002:a05:7412:b130:b0:e2:908c:2ebd with SMTP id az48csp809802rdb; Fri, 17 Nov 2023 13:30:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IGZSFsK64oVQFqoJCNd8o/PZIkwqcLGq49Rqn5xYJNtm6MB/wK+mOqrXaSQjJDYAYz49WIt X-Received: by 2002:a05:6a20:2446:b0:187:b7eb:944d with SMTP id t6-20020a056a20244600b00187b7eb944dmr528802pzc.13.1700256648623; Fri, 17 Nov 2023 13:30:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700256648; cv=none; d=google.com; s=arc-20160816; b=VNHbmf5ozZpfFUzbiwIRndpqHHeij24XDDMSHN+OQWMJpmRGTDfWcuT/Iz0v74oOf6 mr3QVOORxLgmyOYG+RuhXbbP5BW92fbJRSjnGJ9taHnkxChbO+TeFS9JlxLP0QzdMat5 HN6cV5qv5Y851PVQNhYJ2CG7jPdQdx8U+xlk7MP4pwTqyANGad2iTVraDwzjdQGbYuYc LU9jp6KXL3YTg6si++1vPBEW5dlhBVA6P501usDkGZL/yBteJrw6yevUKjZAipevYNDH R8u3HJtaPW/+2Qky897yLIi7lPWmttApL4GcsDtdfOir8IuZ0pCcQc3jURCepsMsYpoJ tUDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=AIPzAeBpfFKCf/gy5vzaJ56BSGUsbLFdz2SU2WRo3X0=; fh=9CVYuTeKZZsaqutjthmBvtD0Q2REYt0VsVzn2qcF9/M=; b=YBhH1jimwoUfaU30OF6mnnaEW251p71AI/wV6xgfpZulKJ2G+Fy0FyJlHKqHrylAmn mxc8g+qIFYTBXtxZeSHAg2i2BQiziLwn/7tHEOqNMJw4f2gVxVPEVtBPrhoaiXTe3C/W unLH55qfIVSF6h5CBIhLVj3ktt12AMh9f1V9p8gpAroIHq4ifi5s6T0bP3SZhQhIQbXq g6XYnX7iVR+NYH77gwrudjOVeKscYzL4n+qHpAjfswGTDULvvO+DG1FoSBJvzH9OtuPz XXtPR+Tyt+oxYGWxuE9BT/6lRyUEF7L/UowycaSuGA0+lDomKOES4fmyWj2AtACX5XLk Y5uA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0zHNfDaX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id m8-20020a656a08000000b005ad1edab539si2975665pgu.319.2023.11.17.13.30.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Nov 2023 13:30:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0zHNfDaX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id CEF9A83271C2; Fri, 17 Nov 2023 13:29:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346354AbjKQV2t (ORCPT + 99 others); Fri, 17 Nov 2023 16:28:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235885AbjKQV2c (ORCPT ); Fri, 17 Nov 2023 16:28:32 -0500 Received: from mail-ot1-x332.google.com (mail-ot1-x332.google.com [IPv6:2607:f8b0:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7563F1BF7 for ; Fri, 17 Nov 2023 13:28:13 -0800 (PST) Received: by mail-ot1-x332.google.com with SMTP id 46e09a7af769-6ce2fc858feso1375259a34.3 for ; Fri, 17 Nov 2023 13:28:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1700256492; x=1700861292; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=AIPzAeBpfFKCf/gy5vzaJ56BSGUsbLFdz2SU2WRo3X0=; b=0zHNfDaX0kqMvQF3VGxGhlPNhN1ze/6hIeuzHNB1dDpxChP42748c5SHLokR4Ieuem hPlhq9pvE1ezma2EgvAbGiuqFwTQrxov9axawoKOPqiuhlgGKTa/lO4plk8loFCtpeLr c9aAvdDA6060J4p2oF4Wy6qdgEjwmJZtUrF76/LbdxUf47IcuTw/lyzvHsp2sCywPDaF vDPg/57YL9COZGQYVCAbE4xynTyU+YvM6l1Uxjrxs1tMmukv3ArzR8MQeaQ/lb+fSINl 0iOnNJn7p38/aLFx9v2ggQ2H2LIsd2vl1c3nXaV5w7RCurBZq127YBtEcBOYxFKyEQTm 326g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700256492; x=1700861292; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AIPzAeBpfFKCf/gy5vzaJ56BSGUsbLFdz2SU2WRo3X0=; b=TF/KjC5R9MgTx5vuWJltgRz8ETht2nfwhszwFFRbjrbdP023D6gT83GhMS3FAmLZTB PWDelxMnAeyj1uZXQUHrA5nFKE8d1gXVr0XH9Dy3oVBpdLa/cQwCPWPcEZdUhjPN84/2 pN5+20GX1zpvgrUCRdbhslm0YhJBe2BARPIRbIlOKK49NGBik4dJRLRSAMaqTCul8eA2 xOGtI/PcM6JcF5dnOWTrMb9IaKiWbd8/RUu91fIherS5gDc0aycHrwDFW8eYj23z3TvD xuFDjsBZ2Q2jB4NydSXTwbK641iwTeQbV9PFOgECwDyRI0ub27qcM/ZkttJIO2DgzMY1 EL/w== X-Gm-Message-State: AOJu0YwTtfOsu683za3sOUIFS3CFVkOywvJziU1AG4jOKq9Ek6tLocfP aYNfCU+ybwH7h8RTJSEJ/CfLqw== X-Received: by 2002:a9d:6310:0:b0:6d6:54ce:cad with SMTP id q16-20020a9d6310000000b006d654ce0cadmr434912otk.5.1700256492772; Fri, 17 Nov 2023 13:28:12 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id e2-20020a05683013c200b006d3127234d7sm365677otq.8.2023.11.17.13.28.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Nov 2023 13:28:12 -0800 (PST) From: Charlie Jenkins Date: Fri, 17 Nov 2023 13:28:01 -0800 Subject: [PATCH v11 3/5] riscv: Add checksum header MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231117-optimize_checksum-v11-3-7d9d954fe361@rivosinc.com> References: <20231117-optimize_checksum-v11-0-7d9d954fe361@rivosinc.com> In-Reply-To: <20231117-optimize_checksum-v11-0-7d9d954fe361@rivosinc.com> To: Charlie Jenkins , Palmer Dabbelt , Conor Dooley , Samuel Holland , David Laight , Xiao Wang , Evan Green , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Paul Walmsley , Albert Ou , Arnd Bergmann , Conor Dooley X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Fri, 17 Nov 2023 13:29:57 -0800 (PST) Provide checksum algorithms that have been designed to leverage riscv instructions such as rotate. In 64-bit, can take advantage of the larger register to avoid some overflow checking. Signed-off-by: Charlie Jenkins Acked-by: Conor Dooley --- arch/riscv/include/asm/checksum.h | 82 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/arch/riscv/include/asm/checksum.h b/arch/riscv/include/asm/checksum.h new file mode 100644 index 000000000000..2fcf864186e7 --- /dev/null +++ b/arch/riscv/include/asm/checksum.h @@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Checksum routines + * + * Copyright (C) 2023 Rivos Inc. + */ +#ifndef __ASM_RISCV_CHECKSUM_H +#define __ASM_RISCV_CHECKSUM_H + +#include +#include + +#define ip_fast_csum ip_fast_csum + +/* Define riscv versions of functions before importing asm-generic/checksum.h */ +#include + +/** + * Quickly compute an IP checksum with the assumption that IPv4 headers will + * always be in multiples of 32-bits, and have an ihl of at least 5. + * + * @ihl: the number of 32 bit segments and must be greater than or equal to 5. + * @iph: assumed to be word aligned given that NET_IP_ALIGN is set to 2 on + * riscv, defining IP headers to be aligned. + */ +static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl) +{ + unsigned long csum = 0; + int pos = 0; + + do { + csum += ((const unsigned int *)iph)[pos]; + if (IS_ENABLED(CONFIG_32BIT)) + csum += csum < ((const unsigned int *)iph)[pos]; + } while (++pos < ihl); + + /* + * ZBB only saves three instructions on 32-bit and five on 64-bit so not + * worth checking if supported without Alternatives. + */ + if (IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && + IS_ENABLED(CONFIG_RISCV_ALTERNATIVE)) { + unsigned long fold_temp; + + asm_volatile_goto(ALTERNATIVE("j %l[no_zbb]", "nop", 0, + RISCV_ISA_EXT_ZBB, 1) + : + : + : + : no_zbb); + + if (IS_ENABLED(CONFIG_32BIT)) { + asm(".option push \n\ + .option arch,+zbb \n\ + not %[fold_temp], %[csum] \n\ + rori %[csum], %[csum], 16 \n\ + sub %[csum], %[fold_temp], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); + } else { + asm(".option push \n\ + .option arch,+zbb \n\ + rori %[fold_temp], %[csum], 32 \n\ + add %[csum], %[fold_temp], %[csum] \n\ + srli %[csum], %[csum], 32 \n\ + not %[fold_temp], %[csum] \n\ + roriw %[csum], %[csum], 16 \n\ + subw %[csum], %[fold_temp], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); + } + return csum >> 16; + } +no_zbb: +#ifndef CONFIG_32BIT + csum += ror64(csum, 32); + csum >>= 32; +#endif + return csum_fold((__force __wsum)csum); +} + +#endif /* __ASM_RISCV_CHECKSUM_H */ -- 2.34.1