Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp1009509rdb; Tue, 19 Sep 2023 18:07:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF4iq3tg5pkSH+GW/g+FDlj8rEzoPUTZe+8eS7tv9/gTeAYdD6l6M93NjEfs246oQUJbNKj X-Received: by 2002:a05:6808:1a1a:b0:3a7:8725:f37c with SMTP id bk26-20020a0568081a1a00b003a78725f37cmr1335125oib.10.1695172045949; Tue, 19 Sep 2023 18:07:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695172045; cv=none; d=google.com; s=arc-20160816; b=LDYpLNMTDFf+r40wmb3LBHkE321n5Rl0S3i5Aj/vzH6jpYyf8vTlMNzWkp5CGosM97 7OZaiZfSd8D34Q/1Rp0WKOS/itZqQYQlybnjIIkf90pFqfgTWdGdURWxMo16hAYQJ9cU xkM1ga1o+VaJOkgIluC/9BhifFjlMewPwCpmkU2K772dShB61TrSe4+3z1ucTY4Ha+SO 9lBlg+aqqqkfu7Lu66eldCasi92wKzbb9M5lA/ZO2PhCpj5FQ916+bMHgBTP+A0ZirR5 MiH9eKYGnBygkSnEAMPpkcUmD6zLwvewO59M/1HwwEwmrKKdKtjUp9TREQOLGRE6SdwS mStQ== 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=1k75qxIvoyjoXO7i5f1b9o5ew6JdXdHy21zFkpaW4Nc=; fh=OdLM5KLdAYlMPhaKzudfbUgmxJEOsES1wczB6MEdoso=; b=Un0Oovxa47pExqJUHY3ieAz7/k8XJtkpcLPPXnYSHUqJ8VMv1/IxVUsRyO8k0LW/23 77AjsdqQ2mJbYD7ufOsBOw8m//h9EeNRRQWjpKKn6ncCkcLT7aqLB0dAwB2IV7iZbOTZ AAI+Lfwu255BuJz72NiTjPH8yq6EHSaZztpi+r7+1IZ2FN4KLulEunOUAydwyxQj4j5n UjB4t5x/s5aTuyCqRZMIN3PHCHvc8g6MHWI+pI7G6DiAjquREcfE21qOM8gNr8Sq5kW/ M1KZWJcY+9B/bqBN2Y1IxUocncxYkOtRSMTL2Bce8CzeVzTv+kyO9rEztDYDXFi/Yv4W CFpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=IFhhoGu+; 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 e20-20020a63d954000000b0056a9544e894si944516pgj.397.2023.09.19.18.07.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 18:07: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=IFhhoGu+; 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 02A8181E9A66; Tue, 19 Sep 2023 11:45:12 -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 S232400AbjISSpM (ORCPT + 99 others); Tue, 19 Sep 2023 14:45:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232103AbjISSpG (ORCPT ); Tue, 19 Sep 2023 14:45:06 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 073B59D for ; Tue, 19 Sep 2023 11:45:01 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-565e54cb93aso3526368a12.3 for ; Tue, 19 Sep 2023 11:45:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695149100; x=1695753900; 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=1k75qxIvoyjoXO7i5f1b9o5ew6JdXdHy21zFkpaW4Nc=; b=IFhhoGu+M64abnPeiZDAzqKOKytYu/j/DOCTSfKK3hqT/ryiV1Vs33YRGUxrnXgiLT XWuHBzhPUUNEE+ke4klLvt7lJMpVlMDFxdyXdCp1zXIZq4xvBQjUT9B/u0GsQZbd/l6D BczNw4C9Vq3McNMhhZ7I3MSPJQXvqcmYJaLSiiioJsL3kcx6525uEh380j52n0uxj+D6 tQjO6HCb0jMVHgiXUVwp+5k1NuhIcB9Dpyyjo8n8og+4UVBXrpjNi/9tT5pujVwRZ9qj 3RUpKesaV5hDu47nYMxAcSDhjb2ZCrtWmp+EEdZylptKWT/5/Kgb255jxo/889OVQYa4 HPcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695149100; x=1695753900; 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=1k75qxIvoyjoXO7i5f1b9o5ew6JdXdHy21zFkpaW4Nc=; b=h4XdOX9boHyRT8uSDC/L7FVmfwV1EvzGRczdYQTWsnjZoOo/mLhphISSCqnI86msuC qhHFeJVMn20GlAXSlv+4QpnXYxvDywLDEgJAkxlj2pm7SO9XOP20yMghmOznaGjBnqBx hXDRU4lMd1YVVYun8UgHVPmXDqKn3Km39e1tMVcDXVdPF7lgUATbWq5pKulsIEy9JEB7 ZSLCHqD4FgwnocuZQylXvTKnWIvjgbFT9FP5W5ahZBzFwjhDbBKPVG1inYVcV3QCOg9c /kEScW1NQI84umTsoi51oAlegen/b09CV/HCBS908sWDZ+KcJKCbpf2tbc972ocGZJiS eNKw== X-Gm-Message-State: AOJu0Yybpj2qEtglDBrv0cEqPR49HXx6Vgg6iAKmj02WvWHTkhyyVYNo ymLw9uv0nWhNigkV+tyhZBosCg== X-Received: by 2002:a17:90a:f308:b0:26f:4685:5b69 with SMTP id ca8-20020a17090af30800b0026f46855b69mr617613pjb.7.1695149100481; Tue, 19 Sep 2023 11:45:00 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id m5-20020a17090b068500b0026309d57724sm3876846pjz.39.2023.09.19.11.44.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 11:45:00 -0700 (PDT) From: Charlie Jenkins Date: Tue, 19 Sep 2023 11:44:31 -0700 Subject: [PATCH v7 2/4] riscv: Checksum header MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230919-optimize_checksum-v7-2-06c7d0ddd5d6@rivosinc.com> References: <20230919-optimize_checksum-v7-0-06c7d0ddd5d6@rivosinc.com> In-Reply-To: <20230919-optimize_checksum-v7-0-06c7d0ddd5d6@rivosinc.com> To: Charlie Jenkins , Palmer Dabbelt , Conor Dooley , Samuel Holland , David Laight , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Paul Walmsley , Albert Ou , Arnd Bergmann 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=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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 19 Sep 2023 11:45:12 -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 --- arch/riscv/include/asm/checksum.h | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/arch/riscv/include/asm/checksum.h b/arch/riscv/include/asm/checksum.h new file mode 100644 index 000000000000..dc0dd89f2a13 --- /dev/null +++ b/arch/riscv/include/asm/checksum.h @@ -0,0 +1,79 @@ +/* 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 + +#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. + */ +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