Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp597629rdb; Tue, 31 Oct 2023 17:19:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFvn1O3MmuSimhM4JVQwmr8pHPn1C7lFdxNyK+nkTgKFtqF4MbyA9bPaen/rYDCumPzbiTK X-Received: by 2002:a54:4015:0:b0:3a7:b094:8f2 with SMTP id x21-20020a544015000000b003a7b09408f2mr16201536oie.45.1698797985878; Tue, 31 Oct 2023 17:19:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698797985; cv=none; d=google.com; s=arc-20160816; b=bSf3qr7ay6nC0/JjnILdCjxyjeN8yzvR/+624BdaAz8+Dhqal5Nf3yCgAU+xem6NWc s8FzMBRo6ME5I7arbvzD6ivh6URKRRi+HD2aRkans1jTXPt0pXgunQYzIIhTQjn9Af6h WtNuJARLFGA+2a3LaKEAwBMSmdaaeOLoBVAim1WbKXWngEPbN0f0o6rSarZ31ACn5HRY lUUGElg2AEKrVImWUtd07Y55HPm1yfCi7MBlB2JJd52UW3yyBctuKjAOHG2uGiF5Oc38 gL9FeuXoJwA8pB3W/h9cJRBcWgx9CW72deFUAjR7iECoJ/9ELcDeuzz/si8g4C6/AHJM ypOA== 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=Ed3AceHMZkBHW3Y7G0fWRXKUFOz+aro++Yp4kYAVXuk=; fh=9CVYuTeKZZsaqutjthmBvtD0Q2REYt0VsVzn2qcF9/M=; b=D3YWKW3bXHjbPUWxY/cNmQzcgUEsoCOVscAuR0vdjUUWs5V1U2bQ4IEzpTA4rE+gAS GMv2SsHz0MPXE+z4i56XG9jqN3qH/pKclMXmpteKB0e+mM0kUtO/9QOjpbpdHV58y09P fwjPdXt3IU6jB1tfP4VqCzzlfrhdqJlZBgf7SiYduz9sAB7FNLhhJhnGsIK28JRpm+Nk buIR4Sdt18mBAO9EVfQfj4aELcjvAv4LfX+/mhXo5nQ6gcYzXuOEaofMODp8mrraEJfz ro1H2CMi8fPslYCdFO/dDaXLlHJYgoZ2A1u5YL9UTMlkmCho7NQ7YgfXEItsJJAHQEuk iLpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=b3wrNZMi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id h25-20020a633859000000b00584b2a790absi1843289pgn.821.2023.10.31.17.19.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 17:19:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=b3wrNZMi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 3B5DD80BA7F1; Tue, 31 Oct 2023 17:19:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376891AbjKAATd (ORCPT + 99 others); Tue, 31 Oct 2023 20:19:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345065AbjKAATP (ORCPT ); Tue, 31 Oct 2023 20:19:15 -0400 Received: from mail-oi1-x231.google.com (mail-oi1-x231.google.com [IPv6:2607:f8b0:4864:20::231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 590D1F4 for ; Tue, 31 Oct 2023 17:19:13 -0700 (PDT) Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3b2e4107f47so4303404b6e.2 for ; Tue, 31 Oct 2023 17:19:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698797952; x=1699402752; 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=Ed3AceHMZkBHW3Y7G0fWRXKUFOz+aro++Yp4kYAVXuk=; b=b3wrNZMigBwzBB3l8ZnunB5lH/gUOTU2Y8snqzkVKaDFBMGoV2sJhzUsQ9o3PkgQqg vM9jekAH2ZzZ7IdNreldtwnvaYPpoig/GvTFtItqh7S5YVM0MEMscd1F6NjCH6qZ3h8O 9aBchPyc9HkYezwwEQAJyIdLI59l1wmpYEP4644NQkQXwaiSzBh/Oe+xoJwzLeeJdHoT 2gO122+WdzbaFG32iZPRO9VlJCQyplRdO63wLiP74anWSmNBURPA6v3AB8Tayc3SS7Hd rEYvlrTTbSY4FnZKdYIyOqzF6gi+1Z4HyqBxXvxfrXzmwcUJegtWQ0vMNKsJ9i1LSq/u EgDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698797952; x=1699402752; 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=Ed3AceHMZkBHW3Y7G0fWRXKUFOz+aro++Yp4kYAVXuk=; b=B1XUH1jvffn06G8gCg8/vtm2K5tdcnqlkCyD0RbFJo8bCgGj+yQwsxNMR7BXYCn6Ic O153Sm3EVhpirkCUkav8iZyxRvaINeg4z8/tJ7tjrKLJ4QQ1kSd3cd4hGRQEQB0bNxy8 VG1e2fKoKEsVMjeST8Gr6woCAiRtOHvn5cnbXJcBOZ0Si435T2yBBobILffW7v2djG4+ cAO5P2UAMv9DkfWaYJ/wTHB4rQLcR3l0WTebPSliUXc9Hw84AvJGh6CTytlmAJkl8aou kK33nkS16/iLnTx7ODhu35wcObveVafFfgEItZ9ZwBBQ0tode5jTDtzABNqpArMcv1qs ZEKA== X-Gm-Message-State: AOJu0YyBrttWpBUb97Wr/+JzQt6lNLNZxJ+K6NsCfeNZ0D/7IV3TUUTZ 8Wpc9WVr2kUyWyvU/htJ7e8XLw== X-Received: by 2002:a05:6808:1293:b0:3b0:daf8:954 with SMTP id a19-20020a056808129300b003b0daf80954mr20274613oiw.49.1698797952751; Tue, 31 Oct 2023 17:19:12 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id n21-20020aca2415000000b003af638fd8e4sm65309oic.55.2023.10.31.17.19.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 17:19:12 -0700 (PDT) From: Charlie Jenkins Date: Tue, 31 Oct 2023 17:18:53 -0700 Subject: [PATCH v9 3/5] riscv: Checksum header MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20231031-optimize_checksum-v9-3-ea018e69b229@rivosinc.com> References: <20231031-optimize_checksum-v9-0-ea018e69b229@rivosinc.com> In-Reply-To: <20231031-optimize_checksum-v9-0-ea018e69b229@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]); Tue, 31 Oct 2023 17:19:40 -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 | 81 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/arch/riscv/include/asm/checksum.h b/arch/riscv/include/asm/checksum.h new file mode 100644 index 000000000000..3d77cac338fe --- /dev/null +++ b/arch/riscv/include/asm/checksum.h @@ -0,0 +1,81 @@ +/* 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 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 += ror64(csum, 32); + csum >>= 32; +#endif + return csum_fold((__force __wsum)csum); +} + +#endif /* __ASM_RISCV_CHECKSUM_H */ -- 2.34.1