18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Checksum functions for Hexagon
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*  This was derived from arch/alpha/lib/checksum.c  */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/string.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
158c2ecf20Sopenharmony_ci#include <net/checksum.h>
168c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
178c2ecf20Sopenharmony_ci#include <asm/intrinsics.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*  Vector value operations  */
218c2ecf20Sopenharmony_ci#define SIGN(x, y)	((0x8000ULL*x)<<y)
228c2ecf20Sopenharmony_ci#define CARRY(x, y)	((0x0002ULL*x)<<y)
238c2ecf20Sopenharmony_ci#define SELECT(x, y)	((0x0001ULL*x)<<y)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define VR_NEGATE(a, b, c, d)	(SIGN(a, 48) + SIGN(b, 32) + SIGN(c, 16) \
268c2ecf20Sopenharmony_ci	+ SIGN(d, 0))
278c2ecf20Sopenharmony_ci#define VR_CARRY(a, b, c, d)	(CARRY(a, 48) + CARRY(b, 32) + CARRY(c, 16) \
288c2ecf20Sopenharmony_ci	+ CARRY(d, 0))
298c2ecf20Sopenharmony_ci#define VR_SELECT(a, b, c, d)	(SELECT(a, 48) + SELECT(b, 32) + SELECT(c, 16) \
308c2ecf20Sopenharmony_ci	+ SELECT(d, 0))
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* optimized HEXAGON V3 intrinsic version */
348c2ecf20Sopenharmony_cistatic inline unsigned short from64to16(u64 x)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	u64 sum;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	sum = HEXAGON_P_vrmpyh_PP(x^VR_NEGATE(1, 1, 1, 1),
398c2ecf20Sopenharmony_ci			     VR_SELECT(1, 1, 1, 1));
408c2ecf20Sopenharmony_ci	sum += VR_CARRY(0, 0, 1, 0);
418c2ecf20Sopenharmony_ci	sum = HEXAGON_P_vrmpyh_PP(sum, VR_SELECT(0, 0, 1, 1));
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	return 0xFFFF & sum;
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/*
478c2ecf20Sopenharmony_ci * computes the checksum of the TCP/UDP pseudo-header
488c2ecf20Sopenharmony_ci * returns a 16-bit checksum, already complemented.
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci__sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
518c2ecf20Sopenharmony_ci			  __u32 len, __u8 proto, __wsum sum)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	return (__force __sum16)~from64to16(
548c2ecf20Sopenharmony_ci		(__force u64)saddr + (__force u64)daddr +
558c2ecf20Sopenharmony_ci		(__force u64)sum + ((len + proto) << 8));
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci__wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
598c2ecf20Sopenharmony_ci			  __u32 len, __u8 proto, __wsum sum)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	u64 result;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	result = (__force u64)saddr + (__force u64)daddr +
648c2ecf20Sopenharmony_ci		 (__force u64)sum + ((len + proto) << 8);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	/* Fold down to 32-bits so we don't lose in the typedef-less
678c2ecf20Sopenharmony_ci	   network stack.  */
688c2ecf20Sopenharmony_ci	/* 64 to 33 */
698c2ecf20Sopenharmony_ci	result = (result & 0xffffffffUL) + (result >> 32);
708c2ecf20Sopenharmony_ci	/* 33 to 32 */
718c2ecf20Sopenharmony_ci	result = (result & 0xffffffffUL) + (result >> 32);
728c2ecf20Sopenharmony_ci	return (__force __wsum)result;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ciEXPORT_SYMBOL(csum_tcpudp_nofold);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/*
778c2ecf20Sopenharmony_ci * Do a 64-bit checksum on an arbitrary memory area..
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci * This isn't a great routine, but it's not _horrible_ either. The
808c2ecf20Sopenharmony_ci * inner loop could be unrolled a bit further, and there are better
818c2ecf20Sopenharmony_ci * ways to do the carry, but this is reasonable.
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* optimized HEXAGON intrinsic version, with over read fixed */
858c2ecf20Sopenharmony_ciunsigned int do_csum(const void *voidptr, int len)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	u64 sum0, sum1, x0, x1, *ptr8_o, *ptr8_e, *ptr8;
888c2ecf20Sopenharmony_ci	int i, start, mid, end, mask;
898c2ecf20Sopenharmony_ci	const char *ptr = voidptr;
908c2ecf20Sopenharmony_ci	unsigned short *ptr2;
918c2ecf20Sopenharmony_ci	unsigned int *ptr4;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (len <= 0)
948c2ecf20Sopenharmony_ci		return 0;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	start = 0xF & (16-(((int) ptr) & 0xF)) ;
978c2ecf20Sopenharmony_ci	mask  = 0x7fffffffUL >> HEXAGON_R_cl0_R(len);
988c2ecf20Sopenharmony_ci	start = start & mask ;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	mid = len - start;
1018c2ecf20Sopenharmony_ci	end = mid & 0xF;
1028c2ecf20Sopenharmony_ci	mid = mid>>4;
1038c2ecf20Sopenharmony_ci	sum0 = mid << 18;
1048c2ecf20Sopenharmony_ci	sum1 = 0;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	if (start & 1)
1078c2ecf20Sopenharmony_ci		sum0 += (u64) (ptr[0] << 8);
1088c2ecf20Sopenharmony_ci	ptr2 = (unsigned short *) &ptr[start & 1];
1098c2ecf20Sopenharmony_ci	if (start & 2)
1108c2ecf20Sopenharmony_ci		sum1 += (u64) ptr2[0];
1118c2ecf20Sopenharmony_ci	ptr4 = (unsigned int *) &ptr[start & 3];
1128c2ecf20Sopenharmony_ci	if (start & 4) {
1138c2ecf20Sopenharmony_ci		sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
1148c2ecf20Sopenharmony_ci			VR_NEGATE(0, 0, 1, 1)^((u64)ptr4[0]),
1158c2ecf20Sopenharmony_ci			VR_SELECT(0, 0, 1, 1));
1168c2ecf20Sopenharmony_ci		sum0 += VR_SELECT(0, 0, 1, 0);
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci	ptr8 = (u64 *) &ptr[start & 7];
1198c2ecf20Sopenharmony_ci	if (start & 8) {
1208c2ecf20Sopenharmony_ci		sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
1218c2ecf20Sopenharmony_ci			VR_NEGATE(1, 1, 1, 1)^(ptr8[0]),
1228c2ecf20Sopenharmony_ci			VR_SELECT(1, 1, 1, 1));
1238c2ecf20Sopenharmony_ci		sum1 += VR_CARRY(0, 0, 1, 0);
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci	ptr8_o = (u64 *) (ptr + start);
1268c2ecf20Sopenharmony_ci	ptr8_e = (u64 *) (ptr + start + 8);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (mid) {
1298c2ecf20Sopenharmony_ci		x0 = *ptr8_e; ptr8_e += 2;
1308c2ecf20Sopenharmony_ci		x1 = *ptr8_o; ptr8_o += 2;
1318c2ecf20Sopenharmony_ci		if (mid > 1)
1328c2ecf20Sopenharmony_ci			for (i = 0; i < mid-1; i++) {
1338c2ecf20Sopenharmony_ci				sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
1348c2ecf20Sopenharmony_ci					x0^VR_NEGATE(1, 1, 1, 1),
1358c2ecf20Sopenharmony_ci					VR_SELECT(1, 1, 1, 1));
1368c2ecf20Sopenharmony_ci				sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
1378c2ecf20Sopenharmony_ci					x1^VR_NEGATE(1, 1, 1, 1),
1388c2ecf20Sopenharmony_ci					VR_SELECT(1, 1, 1, 1));
1398c2ecf20Sopenharmony_ci				x0 = *ptr8_e; ptr8_e += 2;
1408c2ecf20Sopenharmony_ci				x1 = *ptr8_o; ptr8_o += 2;
1418c2ecf20Sopenharmony_ci			}
1428c2ecf20Sopenharmony_ci		sum0 = HEXAGON_P_vrmpyhacc_PP(sum0, x0^VR_NEGATE(1, 1, 1, 1),
1438c2ecf20Sopenharmony_ci			VR_SELECT(1, 1, 1, 1));
1448c2ecf20Sopenharmony_ci		sum1 = HEXAGON_P_vrmpyhacc_PP(sum1, x1^VR_NEGATE(1, 1, 1, 1),
1458c2ecf20Sopenharmony_ci			VR_SELECT(1, 1, 1, 1));
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	ptr4 = (unsigned int *) &ptr[start + (mid * 16) + (end & 8)];
1498c2ecf20Sopenharmony_ci	if (end & 4) {
1508c2ecf20Sopenharmony_ci		sum1 = HEXAGON_P_vrmpyhacc_PP(sum1,
1518c2ecf20Sopenharmony_ci			VR_NEGATE(0, 0, 1, 1)^((u64)ptr4[0]),
1528c2ecf20Sopenharmony_ci			VR_SELECT(0, 0, 1, 1));
1538c2ecf20Sopenharmony_ci		sum1 += VR_SELECT(0, 0, 1, 0);
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci	ptr2 = (unsigned short *) &ptr[start + (mid * 16) + (end & 12)];
1568c2ecf20Sopenharmony_ci	if (end & 2)
1578c2ecf20Sopenharmony_ci		sum0 += (u64) ptr2[0];
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	if (end & 1)
1608c2ecf20Sopenharmony_ci		sum1 += (u64) ptr[start + (mid * 16) + (end & 14)];
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	ptr8 = (u64 *) &ptr[start + (mid * 16)];
1638c2ecf20Sopenharmony_ci	if (end & 8) {
1648c2ecf20Sopenharmony_ci		sum0 = HEXAGON_P_vrmpyhacc_PP(sum0,
1658c2ecf20Sopenharmony_ci			VR_NEGATE(1, 1, 1, 1)^(ptr8[0]),
1668c2ecf20Sopenharmony_ci			VR_SELECT(1, 1, 1, 1));
1678c2ecf20Sopenharmony_ci		sum0 += VR_CARRY(0, 0, 1, 0);
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci	sum0 = HEXAGON_P_vrmpyh_PP((sum0+sum1)^VR_NEGATE(0, 0, 0, 1),
1708c2ecf20Sopenharmony_ci		VR_SELECT(0, 0, 1, 1));
1718c2ecf20Sopenharmony_ci	sum0 += VR_NEGATE(0, 0, 0, 1);
1728c2ecf20Sopenharmony_ci	sum0 = HEXAGON_P_vrmpyh_PP(sum0, VR_SELECT(0, 0, 1, 1));
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	if (start & 1)
1758c2ecf20Sopenharmony_ci		sum0 = (sum0 << 8) | (0xFF & (sum0 >> 8));
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	return 0xFFFF & sum0;
1788c2ecf20Sopenharmony_ci}
179