18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * arch/alpha/lib/ev6-csum_ipv6_magic.S
48c2ecf20Sopenharmony_ci * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * unsigned short csum_ipv6_magic(struct in6_addr *saddr,
78c2ecf20Sopenharmony_ci *                                struct in6_addr *daddr,
88c2ecf20Sopenharmony_ci *                                __u32 len,
98c2ecf20Sopenharmony_ci *                                unsigned short proto,
108c2ecf20Sopenharmony_ci *                                unsigned int csum);
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Much of the information about 21264 scheduling/coding comes from:
138c2ecf20Sopenharmony_ci *	Compiler Writer's Guide for the Alpha 21264
148c2ecf20Sopenharmony_ci *	abbreviated as 'CWG' in other comments here
158c2ecf20Sopenharmony_ci *	ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
168c2ecf20Sopenharmony_ci * Scheduling notation:
178c2ecf20Sopenharmony_ci *	E	- either cluster
188c2ecf20Sopenharmony_ci *	U	- upper subcluster; U0 - subcluster U0; U1 - subcluster U1
198c2ecf20Sopenharmony_ci *	L	- lower subcluster; L0 - subcluster L0; L1 - subcluster L1
208c2ecf20Sopenharmony_ci * Try not to change the actual algorithm if possible for consistency.
218c2ecf20Sopenharmony_ci * Determining actual stalls (other than slotting) doesn't appear to be easy to do.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * unsigned short csum_ipv6_magic(struct in6_addr *saddr,
248c2ecf20Sopenharmony_ci *                                struct in6_addr *daddr,
258c2ecf20Sopenharmony_ci *                                __u32 len,
268c2ecf20Sopenharmony_ci *                                unsigned short proto,
278c2ecf20Sopenharmony_ci *                                unsigned int csum);
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * Swap <proto> (takes form 0xaabb)
308c2ecf20Sopenharmony_ci * Then shift it left by 48, so result is:
318c2ecf20Sopenharmony_ci *	0xbbaa0000 00000000
328c2ecf20Sopenharmony_ci * Then turn it back into a sign extended 32-bit item
338c2ecf20Sopenharmony_ci *	0xbbaa0000
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * Swap <len> (an unsigned int) using Mike Burrows' 7-instruction sequence
368c2ecf20Sopenharmony_ci * (we can't hide the 3-cycle latency of the unpkbw in the 6-instruction sequence)
378c2ecf20Sopenharmony_ci * Assume input takes form 0xAABBCCDD
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * Finally, original 'folding' approach is to split the long into 4 unsigned shorts
408c2ecf20Sopenharmony_ci * add 4 ushorts, resulting in ushort/carry
418c2ecf20Sopenharmony_ci * add carry bits + ushort --> ushort
428c2ecf20Sopenharmony_ci * add carry bits + ushort --> ushort (in case the carry results in an overflow)
438c2ecf20Sopenharmony_ci * Truncate to a ushort.  (took 13 instructions)
448c2ecf20Sopenharmony_ci * From doing some testing, using the approach in checksum.c:from64to16()
458c2ecf20Sopenharmony_ci * results in the same outcome:
468c2ecf20Sopenharmony_ci * split into 2 uints, add those, generating a ulong
478c2ecf20Sopenharmony_ci * add the 3 low ushorts together, generating a uint
488c2ecf20Sopenharmony_ci * a final add of the 2 lower ushorts
498c2ecf20Sopenharmony_ci * truncating the result.
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * Misalignment handling added by Ivan Kokshaysky <ink@jurassic.park.msu.ru>
528c2ecf20Sopenharmony_ci * The cost is 16 instructions (~8 cycles), including two extra loads which
538c2ecf20Sopenharmony_ci * may cause additional delay in rare cases (load-load replay traps).
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#include <asm/export.h>
578c2ecf20Sopenharmony_ci	.globl csum_ipv6_magic
588c2ecf20Sopenharmony_ci	.align 4
598c2ecf20Sopenharmony_ci	.ent csum_ipv6_magic
608c2ecf20Sopenharmony_ci	.frame $30,0,$26,0
618c2ecf20Sopenharmony_cicsum_ipv6_magic:
628c2ecf20Sopenharmony_ci	.prologue 0
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	ldq_u	$0,0($16)	# L : Latency: 3
658c2ecf20Sopenharmony_ci	inslh	$18,7,$4	# U : 0000000000AABBCC
668c2ecf20Sopenharmony_ci	ldq_u	$1,8($16)	# L : Latency: 3
678c2ecf20Sopenharmony_ci	sll	$19,8,$7	# U : U L U L : 0x00000000 00aabb00
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	and	$16,7,$6	# E : src misalignment
708c2ecf20Sopenharmony_ci	ldq_u	$5,15($16)	# L : Latency: 3
718c2ecf20Sopenharmony_ci	zapnot	$20,15,$20	# U : zero extend incoming csum
728c2ecf20Sopenharmony_ci	ldq_u	$2,0($17)	# L : U L U L : Latency: 3
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	extql	$0,$6,$0	# U :
758c2ecf20Sopenharmony_ci	extqh	$1,$6,$22	# U :
768c2ecf20Sopenharmony_ci	ldq_u	$3,8($17)	# L : Latency: 3
778c2ecf20Sopenharmony_ci	sll	$19,24,$19	# U : U U L U : 0x000000aa bb000000
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	cmoveq	$6,$31,$22	# E : src aligned?
808c2ecf20Sopenharmony_ci	ldq_u	$23,15($17)	# L : Latency: 3
818c2ecf20Sopenharmony_ci	inswl	$18,3,$18	# U : 000000CCDD000000
828c2ecf20Sopenharmony_ci	addl	$19,$7,$19	# E : U L U L : <sign bits>bbaabb00
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	or	$0,$22,$0	# E : 1st src word complete
858c2ecf20Sopenharmony_ci	extql	$1,$6,$1	# U :
868c2ecf20Sopenharmony_ci	or	$18,$4,$18	# E : 000000CCDDAABBCC
878c2ecf20Sopenharmony_ci	extqh	$5,$6,$5	# U : L U L U
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	and	$17,7,$6	# E : dst misalignment
908c2ecf20Sopenharmony_ci	extql	$2,$6,$2	# U :
918c2ecf20Sopenharmony_ci	or	$1,$5,$1	# E : 2nd src word complete
928c2ecf20Sopenharmony_ci	extqh	$3,$6,$22	# U : L U L U :
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	cmoveq	$6,$31,$22	# E : dst aligned?
958c2ecf20Sopenharmony_ci	extql	$3,$6,$3	# U :
968c2ecf20Sopenharmony_ci	addq	$20,$0,$20	# E : begin summing the words
978c2ecf20Sopenharmony_ci	extqh	$23,$6,$23	# U : L U L U :
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	srl	$18,16,$4	# U : 0000000000CCDDAA
1008c2ecf20Sopenharmony_ci	or	$2,$22,$2	# E : 1st dst word complete
1018c2ecf20Sopenharmony_ci	zap	$19,0x3,$19	# U : <sign bits>bbaa0000
1028c2ecf20Sopenharmony_ci	or	$3,$23,$3	# E : U L U L : 2nd dst word complete
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	cmpult	$20,$0,$0	# E :
1058c2ecf20Sopenharmony_ci	addq	$20,$1,$20	# E :
1068c2ecf20Sopenharmony_ci	zapnot	$18,0xa,$18	# U : 00000000DD00BB00
1078c2ecf20Sopenharmony_ci	zap	$4,0xa,$4	# U : U U L L : 0000000000CC00AA
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	or	$18,$4,$18	# E : 00000000DDCCBBAA
1108c2ecf20Sopenharmony_ci	nop			# E :
1118c2ecf20Sopenharmony_ci	cmpult	$20,$1,$1	# E :
1128c2ecf20Sopenharmony_ci	addq	$20,$2,$20	# E : U L U L
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	cmpult	$20,$2,$2	# E :
1158c2ecf20Sopenharmony_ci	addq	$20,$3,$20	# E :
1168c2ecf20Sopenharmony_ci	cmpult	$20,$3,$3	# E : (1 cycle stall on $20)
1178c2ecf20Sopenharmony_ci	addq	$20,$18,$20	# E : U L U L (1 cycle stall on $20)
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	cmpult	$20,$18,$18	# E :
1208c2ecf20Sopenharmony_ci	addq	$20,$19,$20	# E : (1 cycle stall on $20)
1218c2ecf20Sopenharmony_ci	addq	$0,$1,$0	# E : merge the carries back into the csum
1228c2ecf20Sopenharmony_ci	addq	$2,$3,$2	# E :
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	cmpult	$20,$19,$19	# E :
1258c2ecf20Sopenharmony_ci	addq	$18,$19,$18	# E : (1 cycle stall on $19)
1268c2ecf20Sopenharmony_ci	addq	$0,$2,$0	# E :
1278c2ecf20Sopenharmony_ci	addq	$20,$18,$20	# E : U L U L :
1288c2ecf20Sopenharmony_ci		/* (1 cycle stall on $18, 2 cycles on $20) */
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	addq	$0,$20,$0	# E :
1318c2ecf20Sopenharmony_ci	zapnot	$0,15,$1	# U : Start folding output (1 cycle stall on $0)
1328c2ecf20Sopenharmony_ci	nop			# E :
1338c2ecf20Sopenharmony_ci	srl	$0,32,$0	# U : U L U L : (1 cycle stall on $0)
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	addq	$1,$0,$1	# E : Finished generating ulong
1368c2ecf20Sopenharmony_ci	extwl	$1,2,$2		# U : ushort[1] (1 cycle stall on $1)
1378c2ecf20Sopenharmony_ci	zapnot	$1,3,$0		# U : ushort[0] (1 cycle stall on $1)
1388c2ecf20Sopenharmony_ci	extwl	$1,4,$1		# U : ushort[2] (1 cycle stall on $1)
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	addq	$0,$2,$0	# E
1418c2ecf20Sopenharmony_ci	addq	$0,$1,$3	# E : Finished generating uint
1428c2ecf20Sopenharmony_ci		/* (1 cycle stall on $0) */
1438c2ecf20Sopenharmony_ci	extwl	$3,2,$1		# U : ushort[1] (1 cycle stall on $3)
1448c2ecf20Sopenharmony_ci	nop			# E : L U L U
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	addq	$1,$3,$0	# E : Final carry
1478c2ecf20Sopenharmony_ci	not	$0,$4		# E : complement (1 cycle stall on $0)
1488c2ecf20Sopenharmony_ci	zapnot	$4,3,$0		# U : clear upper garbage bits
1498c2ecf20Sopenharmony_ci		/* (1 cycle stall on $4) */
1508c2ecf20Sopenharmony_ci	ret			# L0 : L U L U
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	.end csum_ipv6_magic
1538c2ecf20Sopenharmony_ci	EXPORT_SYMBOL(csum_ipv6_magic)
154