18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * arch/alpha/lib/ev6-copy_user.S
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * 21264 version contributed by Rick Gorton <rick.gorton@alpha-processor.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copy to/from user space, handling exceptions as we go..  This
88c2ecf20Sopenharmony_ci * isn't exactly pretty.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This is essentially the same as "memcpy()", but with a few twists.
118c2ecf20Sopenharmony_ci * Notably, we have to make sure that $0 is always up-to-date and
128c2ecf20Sopenharmony_ci * contains the right "bytes left to copy" value (and that it is updated
138c2ecf20Sopenharmony_ci * only _after_ a successful copy). There is also some rather minor
148c2ecf20Sopenharmony_ci * exception setup stuff..
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * Much of the information about 21264 scheduling/coding comes from:
178c2ecf20Sopenharmony_ci *	Compiler Writer's Guide for the Alpha 21264
188c2ecf20Sopenharmony_ci *	abbreviated as 'CWG' in other comments here
198c2ecf20Sopenharmony_ci *	ftp.digital.com/pub/Digital/info/semiconductor/literature/dsc-library.html
208c2ecf20Sopenharmony_ci * Scheduling notation:
218c2ecf20Sopenharmony_ci *	E	- either cluster
228c2ecf20Sopenharmony_ci *	U	- upper subcluster; U0 - subcluster U0; U1 - subcluster U1
238c2ecf20Sopenharmony_ci *	L	- lower subcluster; L0 - subcluster L0; L1 - subcluster L1
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <asm/export.h>
278c2ecf20Sopenharmony_ci/* Allow an exception for an insn; exit if we get one.  */
288c2ecf20Sopenharmony_ci#define EXI(x,y...)			\
298c2ecf20Sopenharmony_ci	99: x,##y;			\
308c2ecf20Sopenharmony_ci	.section __ex_table,"a";	\
318c2ecf20Sopenharmony_ci	.long 99b - .;			\
328c2ecf20Sopenharmony_ci	lda $31, $exitin-99b($31);	\
338c2ecf20Sopenharmony_ci	.previous
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define EXO(x,y...)			\
368c2ecf20Sopenharmony_ci	99: x,##y;			\
378c2ecf20Sopenharmony_ci	.section __ex_table,"a";	\
388c2ecf20Sopenharmony_ci	.long 99b - .;			\
398c2ecf20Sopenharmony_ci	lda $31, $exitout-99b($31);	\
408c2ecf20Sopenharmony_ci	.previous
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	.set noat
438c2ecf20Sopenharmony_ci	.align 4
448c2ecf20Sopenharmony_ci	.globl __copy_user
458c2ecf20Sopenharmony_ci	.ent __copy_user
468c2ecf20Sopenharmony_ci				# Pipeline info: Slotting & Comments
478c2ecf20Sopenharmony_ci__copy_user:
488c2ecf20Sopenharmony_ci	.prologue 0
498c2ecf20Sopenharmony_ci	mov $18, $0		# .. .. .. E
508c2ecf20Sopenharmony_ci	subq $18, 32, $1	# .. .. E. ..	: Is this going to be a small copy?
518c2ecf20Sopenharmony_ci	nop			# .. E  .. ..
528c2ecf20Sopenharmony_ci	beq $18, $zerolength	# U  .. .. ..	: U L U L
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	and $16,7,$3		# .. .. .. E	: is leading dest misalignment
558c2ecf20Sopenharmony_ci	ble $1, $onebyteloop	# .. .. U  ..	: 1st branch : small amount of data
568c2ecf20Sopenharmony_ci	beq $3, $destaligned	# .. U  .. ..	: 2nd (one cycle fetcher stall)
578c2ecf20Sopenharmony_ci	subq $3, 8, $3		# E  .. .. ..	: L U U L : trip counter
588c2ecf20Sopenharmony_ci/*
598c2ecf20Sopenharmony_ci * The fetcher stall also hides the 1 cycle cross-cluster stall for $3 (L --> U)
608c2ecf20Sopenharmony_ci * This loop aligns the destination a byte at a time
618c2ecf20Sopenharmony_ci * We know we have at least one trip through this loop
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_ci$aligndest:
648c2ecf20Sopenharmony_ci	EXI( ldbu $1,0($17) )	# .. .. .. L	: Keep loads separate from stores
658c2ecf20Sopenharmony_ci	addq $16,1,$16		# .. .. E  ..	: Section 3.8 in the CWG
668c2ecf20Sopenharmony_ci	addq $3,1,$3		# .. E  .. ..	:
678c2ecf20Sopenharmony_ci	nop			# E  .. .. ..	: U L U L
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/*
708c2ecf20Sopenharmony_ci * the -1 is to compensate for the inc($16) done in a previous quadpack
718c2ecf20Sopenharmony_ci * which allows us zero dependencies within either quadpack in the loop
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_ci	EXO( stb $1,-1($16) )	# .. .. .. L	:
748c2ecf20Sopenharmony_ci	addq $17,1,$17		# .. .. E  ..	: Section 3.8 in the CWG
758c2ecf20Sopenharmony_ci	subq $0,1,$0		# .. E  .. ..	:
768c2ecf20Sopenharmony_ci	bne $3, $aligndest	# U  .. .. ..	: U L U L
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/*
798c2ecf20Sopenharmony_ci * If we fell through into here, we have a minimum of 33 - 7 bytes
808c2ecf20Sopenharmony_ci * If we arrived via branch, we have a minimum of 32 bytes
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ci$destaligned:
838c2ecf20Sopenharmony_ci	and $17,7,$1		# .. .. .. E	: Check _current_ source alignment
848c2ecf20Sopenharmony_ci	bic $0,7,$4		# .. .. E  ..	: number bytes as a quadword loop
858c2ecf20Sopenharmony_ci	EXI( ldq_u $3,0($17) )	# .. L  .. ..	: Forward fetch for fallthrough code
868c2ecf20Sopenharmony_ci	beq $1,$quadaligned	# U  .. .. ..	: U L U L
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/*
898c2ecf20Sopenharmony_ci * In the worst case, we've just executed an ldq_u here from 0($17)
908c2ecf20Sopenharmony_ci * and we'll repeat it once if we take the branch
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* Misaligned quadword loop - not unrolled.  Leave it that way. */
948c2ecf20Sopenharmony_ci$misquad:
958c2ecf20Sopenharmony_ci	EXI( ldq_u $2,8($17) )	# .. .. .. L	:
968c2ecf20Sopenharmony_ci	subq $4,8,$4		# .. .. E  ..	:
978c2ecf20Sopenharmony_ci	extql $3,$17,$3		# .. U  .. ..	:
988c2ecf20Sopenharmony_ci	extqh $2,$17,$1		# U  .. .. ..	: U U L L
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	bis $3,$1,$1		# .. .. .. E	:
1018c2ecf20Sopenharmony_ci	EXO( stq $1,0($16) )	# .. .. L  ..	:
1028c2ecf20Sopenharmony_ci	addq $17,8,$17		# .. E  .. ..	:
1038c2ecf20Sopenharmony_ci	subq $0,8,$0		# E  .. .. ..	: U L L U
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	addq $16,8,$16		# .. .. .. E	:
1068c2ecf20Sopenharmony_ci	bis $2,$2,$3		# .. .. E  ..	:
1078c2ecf20Sopenharmony_ci	nop			# .. E  .. ..	:
1088c2ecf20Sopenharmony_ci	bne $4,$misquad		# U  .. .. ..	: U L U L
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	nop			# .. .. .. E
1118c2ecf20Sopenharmony_ci	nop			# .. .. E  ..
1128c2ecf20Sopenharmony_ci	nop			# .. E  .. ..
1138c2ecf20Sopenharmony_ci	beq $0,$zerolength	# U  .. .. ..	: U L U L
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/* We know we have at least one trip through the byte loop */
1168c2ecf20Sopenharmony_ci	EXI ( ldbu $2,0($17) )	# .. .. .. L	: No loads in the same quad
1178c2ecf20Sopenharmony_ci	addq $16,1,$16		# .. .. E  ..	: as the store (Section 3.8 in CWG)
1188c2ecf20Sopenharmony_ci	nop			# .. E  .. ..	:
1198c2ecf20Sopenharmony_ci	br $31, $dirtyentry	# L0 .. .. ..	: L U U L
1208c2ecf20Sopenharmony_ci/* Do the trailing byte loop load, then hop into the store part of the loop */
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/*
1238c2ecf20Sopenharmony_ci * A minimum of (33 - 7) bytes to do a quad at a time.
1248c2ecf20Sopenharmony_ci * Based upon the usage context, it's worth the effort to unroll this loop
1258c2ecf20Sopenharmony_ci * $0 - number of bytes to be moved
1268c2ecf20Sopenharmony_ci * $4 - number of bytes to move as quadwords
1278c2ecf20Sopenharmony_ci * $16 is current destination address
1288c2ecf20Sopenharmony_ci * $17 is current source address
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_ci$quadaligned:
1318c2ecf20Sopenharmony_ci	subq	$4, 32, $2	# .. .. .. E	: do not unroll for small stuff
1328c2ecf20Sopenharmony_ci	nop			# .. .. E  ..
1338c2ecf20Sopenharmony_ci	nop			# .. E  .. ..
1348c2ecf20Sopenharmony_ci	blt	$2, $onequad	# U  .. .. ..	: U L U L
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/*
1378c2ecf20Sopenharmony_ci * There is a significant assumption here that the source and destination
1388c2ecf20Sopenharmony_ci * addresses differ by more than 32 bytes.  In this particular case, a
1398c2ecf20Sopenharmony_ci * sparsity of registers further bounds this to be a minimum of 8 bytes.
1408c2ecf20Sopenharmony_ci * But if this isn't met, then the output result will be incorrect.
1418c2ecf20Sopenharmony_ci * Furthermore, due to a lack of available registers, we really can't
1428c2ecf20Sopenharmony_ci * unroll this to be an 8x loop (which would enable us to use the wh64
1438c2ecf20Sopenharmony_ci * instruction memory hint instruction).
1448c2ecf20Sopenharmony_ci */
1458c2ecf20Sopenharmony_ci$unroll4:
1468c2ecf20Sopenharmony_ci	EXI( ldq $1,0($17) )	# .. .. .. L
1478c2ecf20Sopenharmony_ci	EXI( ldq $2,8($17) )	# .. .. L  ..
1488c2ecf20Sopenharmony_ci	subq	$4,32,$4	# .. E  .. ..
1498c2ecf20Sopenharmony_ci	nop			# E  .. .. ..	: U U L L
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	addq	$17,16,$17	# .. .. .. E
1528c2ecf20Sopenharmony_ci	EXO( stq $1,0($16) )	# .. .. L  ..
1538c2ecf20Sopenharmony_ci	EXO( stq $2,8($16) )	# .. L  .. ..
1548c2ecf20Sopenharmony_ci	subq	$0,16,$0	# E  .. .. ..	: U L L U
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	addq	$16,16,$16	# .. .. .. E
1578c2ecf20Sopenharmony_ci	EXI( ldq $1,0($17) )	# .. .. L  ..
1588c2ecf20Sopenharmony_ci	EXI( ldq $2,8($17) )	# .. L  .. ..
1598c2ecf20Sopenharmony_ci	subq	$4, 32, $3	# E  .. .. ..	: U U L L : is there enough for another trip?
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	EXO( stq $1,0($16) )	# .. .. .. L
1628c2ecf20Sopenharmony_ci	EXO( stq $2,8($16) )	# .. .. L  ..
1638c2ecf20Sopenharmony_ci	subq	$0,16,$0	# .. E  .. ..
1648c2ecf20Sopenharmony_ci	addq	$17,16,$17	# E  .. .. ..	: U L L U
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	nop			# .. .. .. E
1678c2ecf20Sopenharmony_ci	nop			# .. .. E  ..
1688c2ecf20Sopenharmony_ci	addq	$16,16,$16	# .. E  .. ..
1698c2ecf20Sopenharmony_ci	bgt	$3,$unroll4	# U  .. .. ..	: U L U L
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	nop
1728c2ecf20Sopenharmony_ci	nop
1738c2ecf20Sopenharmony_ci	nop
1748c2ecf20Sopenharmony_ci	beq	$4, $noquads
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci$onequad:
1778c2ecf20Sopenharmony_ci	EXI( ldq $1,0($17) )
1788c2ecf20Sopenharmony_ci	subq	$4,8,$4
1798c2ecf20Sopenharmony_ci	addq	$17,8,$17
1808c2ecf20Sopenharmony_ci	nop
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	EXO( stq $1,0($16) )
1838c2ecf20Sopenharmony_ci	subq	$0,8,$0
1848c2ecf20Sopenharmony_ci	addq	$16,8,$16
1858c2ecf20Sopenharmony_ci	bne	$4,$onequad
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci$noquads:
1888c2ecf20Sopenharmony_ci	nop
1898c2ecf20Sopenharmony_ci	nop
1908c2ecf20Sopenharmony_ci	nop
1918c2ecf20Sopenharmony_ci	beq $0,$zerolength
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/*
1948c2ecf20Sopenharmony_ci * For small copies (or the tail of a larger copy), do a very simple byte loop.
1958c2ecf20Sopenharmony_ci * There's no point in doing a lot of complex alignment calculations to try to
1968c2ecf20Sopenharmony_ci * to quadword stuff for a small amount of data.
1978c2ecf20Sopenharmony_ci *	$0 - remaining number of bytes left to copy
1988c2ecf20Sopenharmony_ci *	$16 - current dest addr
1998c2ecf20Sopenharmony_ci *	$17 - current source addr
2008c2ecf20Sopenharmony_ci */
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci$onebyteloop:
2038c2ecf20Sopenharmony_ci	EXI ( ldbu $2,0($17) )	# .. .. .. L	: No loads in the same quad
2048c2ecf20Sopenharmony_ci	addq $16,1,$16		# .. .. E  ..	: as the store (Section 3.8 in CWG)
2058c2ecf20Sopenharmony_ci	nop			# .. E  .. ..	:
2068c2ecf20Sopenharmony_ci	nop			# E  .. .. ..	: U L U L
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci$dirtyentry:
2098c2ecf20Sopenharmony_ci/*
2108c2ecf20Sopenharmony_ci * the -1 is to compensate for the inc($16) done in a previous quadpack
2118c2ecf20Sopenharmony_ci * which allows us zero dependencies within either quadpack in the loop
2128c2ecf20Sopenharmony_ci */
2138c2ecf20Sopenharmony_ci	EXO ( stb $2,-1($16) )	# .. .. .. L	:
2148c2ecf20Sopenharmony_ci	addq $17,1,$17		# .. .. E  ..	: quadpack as the load
2158c2ecf20Sopenharmony_ci	subq $0,1,$0		# .. E  .. ..	: change count _after_ copy
2168c2ecf20Sopenharmony_ci	bgt $0,$onebyteloop	# U  .. .. ..	: U L U L
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci$zerolength:
2198c2ecf20Sopenharmony_ci$exitin:
2208c2ecf20Sopenharmony_ci$exitout:			# Destination for exception recovery(?)
2218c2ecf20Sopenharmony_ci	nop			# .. .. .. E
2228c2ecf20Sopenharmony_ci	nop			# .. .. E  ..
2238c2ecf20Sopenharmony_ci	nop			# .. E  .. ..
2248c2ecf20Sopenharmony_ci	ret $31,($26),1		# L0 .. .. ..	: L U L U
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	.end __copy_user
2278c2ecf20Sopenharmony_ci	EXPORT_SYMBOL(__copy_user)
228