Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1467789rdh; Fri, 27 Oct 2023 15:44:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGFBzOj/3MajSsjHEn2iPJEZt4Qfbsr++fLoHBEA3LGbMebuoOph8tJsN/my+AG4UBALBWc X-Received: by 2002:a17:902:c409:b0:1c9:f418:c07b with SMTP id k9-20020a170902c40900b001c9f418c07bmr4657861plk.66.1698446665757; Fri, 27 Oct 2023 15:44:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698446665; cv=none; d=google.com; s=arc-20160816; b=ad4lyJNn1+pRu93yzmlUMVnZIETeH8NpEXK0lkNFdzX6vmoPw6oh+/hrBpB2PnG/Iy lv8bUNf/RVyV0RT8ynPaKE823my+v/Qql1fpThoj6AoqZNux1VJrFsB/MCM1jx9Xvqsb L6O/mYfpz8tgEBv6TygktheIHJhPowx0W/9pEh6YqDitELCE7S20qNn4Hvq8/wRVufh0 mC0ONv/K2Hx0/hJvfCFsHuDsMJVMuUgnldiF9wDKgP0Vr/jYVLy6+FRyI5Ub+wGW0IgU zg9C/ZeNKIFciz9ngY6OKL7HLuqpAaEF+4+YkDezniQoFfsUBVZaMmMBIy/NwF0MQE3u 7Egw== 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=8BafBaWBWsOb19tamkke5Dj16vu64sm5n6mJ/bBKmhE=; fh=9CVYuTeKZZsaqutjthmBvtD0Q2REYt0VsVzn2qcF9/M=; b=OYzRT+gputEtYEYWVw8/bc0AFE8sgtp9aWUJ/oVTM7Ua59d+jm5GYTv/pMptuXAg2/ c64Y8aJYZZYw0Wxn3sJ571RhUKRMNLv7BzyjEw43DIurj/qdW34KGfoy7AlMzsI6ms9w 7GTLeZnV9m1oVVudWqLVqSw0pRbPqGZKlPWu1i86idZfthpTK3EdkjOaXUzJrNVmVOOA SXuAO8mtzmuDGqWtIZLmfGNsysR7B2Wruk6YgYXyJuXarC589zLa52eNgyS1JFRBuF94 sulsb+2RNaZ0xPmlAMxwiUIxiHOYHUxQ5ZFpWMmaKYWnJtUUOF9+KvEGr1V4QMf4sqpY c92Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=U82BWtjS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id p3-20020a170902f08300b001c5db1e47c3si1648821pla.553.2023.10.27.15.44.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 15:44:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=U82BWtjS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 721E382E25CE; Fri, 27 Oct 2023 15:44:24 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346715AbjJ0WoQ (ORCPT + 99 others); Fri, 27 Oct 2023 18:44:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346689AbjJ0WoG (ORCPT ); Fri, 27 Oct 2023 18:44:06 -0400 Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 564671BB for ; Fri, 27 Oct 2023 15:44:04 -0700 (PDT) Received: by mail-ot1-x32c.google.com with SMTP id 46e09a7af769-6ce31c4a653so1643145a34.3 for ; Fri, 27 Oct 2023 15:44:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698446643; x=1699051443; 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=8BafBaWBWsOb19tamkke5Dj16vu64sm5n6mJ/bBKmhE=; b=U82BWtjSiTn44LxbapxKT7cw7F5zwvydxrFJTKUH1krGBEWIjJA4yAEZg0naLq0c1F Tzo/+sWiO9xTW8MTEdZhfSkgH3e/ZQDUQ2Hq1NMqT5Mo/Gw+BSmnQJ6u0cM6ycOkA8aM GfPLXZLX9pGf5TlBr3aHoEimthKUnyEkOkdFGqQLLwjDSgRzBpMSYf4m4gSTZaIoEM2d phviU7XmIGZiCeHmC4oj1UCOX+LKyMIjL0OliBaoyExcBBJNavpq6e0zXNwy1Guftc8w 5oHHcN/bHaFNH0R8wpVx2ch50n5Bw0v+wRY1kMS++H62y7/e012zAaKsy1nmeiG1aJfZ VTGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698446643; x=1699051443; 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=8BafBaWBWsOb19tamkke5Dj16vu64sm5n6mJ/bBKmhE=; b=AuVSOuw2AxdH9OZo6h5bfyalVpZALVFmUYU59s0037DMUF1a6+jGhbXugaMe4Y6Ro5 mO2H1BbI5yj6ZWkIiddP3h+rwfZtJbKDfxIiIV3tVoJyerJOHnm1wisvHmR0NfuTmZpm yHP0Gjg570IcBB5Pi9WED7a6KVMJejhlc6SBN8+ALJB8qvn+oTXKKabMFDagBHk/TSmZ IJ99oQFk6dDwo3Dssqft9V0eIbc7NLzijFtdfB005S8jW5sh/E2hXHC87+AwmbyLVE3V +2hiWAnDUAaL5mryExIloepmm8k73h3HTBO4n66+le3Ju603hwZkgV2mv/ggV7LyLqG2 yScg== X-Gm-Message-State: AOJu0YwWUHDI+Qnj/HE+x5zWfkLq14+q9LOGB5AVX8SUu8TFA8JUgt0n A79k0v1elvYL0wHdb6wKphFijIGkCCtL9VCfcX0= X-Received: by 2002:a05:6830:925:b0:6bf:1444:966d with SMTP id v37-20020a056830092500b006bf1444966dmr4123246ott.1.1698446643442; Fri, 27 Oct 2023 15:44:03 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t15-20020a9d748f000000b006c61c098d38sm448564otk.21.2023.10.27.15.44.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 15:44:02 -0700 (PDT) From: Charlie Jenkins Date: Fri, 27 Oct 2023 15:43:53 -0700 Subject: [PATCH v8 3/5] riscv: Checksum header MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231027-optimize_checksum-v8-3-feb7101d128d@rivosinc.com> References: <20231027-optimize_checksum-v8-0-feb7101d128d@rivosinc.com> In-Reply-To: <20231027-optimize_checksum-v8-0-feb7101d128d@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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 27 Oct 2023 15:44:24 -0700 (PDT) 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 | 92 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/arch/riscv/include/asm/checksum.h b/arch/riscv/include/asm/checksum.h new file mode 100644 index 000000000000..9fd4b1b80641 --- /dev/null +++ b/arch/riscv/include/asm/checksum.h @@ -0,0 +1,92 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * IP 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 + +extern unsigned int do_csum(const unsigned char *buff, int len); +#define do_csum do_csum + +/* Default version is sufficient for 32 bit */ +#ifdef CONFIG_64BIT +#define _HAVE_ARCH_IPV6_CSUM +__sum16 csum_ipv6_magic(const struct in6_addr *saddr, + const struct in6_addr *daddr, + __u32 len, __u8 proto, __wsum sum); +#endif + +/* 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 is the number of 32 bit segments and must be greater than or equal to 5. + * @iph is 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 += (csum >> 32) | (csum << 32); + csum >>= 32; +#endif + return csum_fold((__force __wsum)csum); +} + +#endif /* __ASM_RISCV_CHECKSUM_H */ -- 2.42.0