162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * McKinley-optimized version of copy_page().
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2002 Hewlett-Packard Co
662306a36Sopenharmony_ci *	David Mosberger <davidm@hpl.hp.com>
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Inputs:
962306a36Sopenharmony_ci *	in0:	address of target page
1062306a36Sopenharmony_ci *	in1:	address of source page
1162306a36Sopenharmony_ci * Output:
1262306a36Sopenharmony_ci *	no return value
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * General idea:
1562306a36Sopenharmony_ci *	- use regular loads and stores to prefetch data to avoid consuming M-slot just for
1662306a36Sopenharmony_ci *	  lfetches => good for in-cache performance
1762306a36Sopenharmony_ci *	- avoid l2 bank-conflicts by not storing into the same 16-byte bank within a single
1862306a36Sopenharmony_ci *	  cycle
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * Principle of operation:
2162306a36Sopenharmony_ci *	First, note that L1 has a line-size of 64 bytes and L2 a line-size of 128 bytes.
2262306a36Sopenharmony_ci *	To avoid secondary misses in L2, we prefetch both source and destination with a line-size
2362306a36Sopenharmony_ci *	of 128 bytes.  When both of these lines are in the L2 and the first half of the
2462306a36Sopenharmony_ci *	source line is in L1, we start copying the remaining words.  The second half of the
2562306a36Sopenharmony_ci *	source line is prefetched in an earlier iteration, so that by the time we start
2662306a36Sopenharmony_ci *	accessing it, it's also present in the L1.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci *	We use a software-pipelined loop to control the overall operation.  The pipeline
2962306a36Sopenharmony_ci *	has 2*PREFETCH_DIST+K stages.  The first PREFETCH_DIST stages are used for prefetching
3062306a36Sopenharmony_ci *	source cache-lines.  The second PREFETCH_DIST stages are used for prefetching destination
3162306a36Sopenharmony_ci *	cache-lines, the last K stages are used to copy the cache-line words not copied by
3262306a36Sopenharmony_ci *	the prefetches.  The four relevant points in the pipelined are called A, B, C, D:
3362306a36Sopenharmony_ci *	p[A] is TRUE if a source-line should be prefetched, p[B] is TRUE if a destination-line
3462306a36Sopenharmony_ci *	should be prefetched, p[C] is TRUE if the second half of an L2 line should be brought
3562306a36Sopenharmony_ci *	into L1D and p[D] is TRUE if a cacheline needs to be copied.
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci *	This all sounds very complicated, but thanks to the modulo-scheduled loop support,
3862306a36Sopenharmony_ci *	the resulting code is very regular and quite easy to follow (once you get the idea).
3962306a36Sopenharmony_ci *
4062306a36Sopenharmony_ci *	As a secondary optimization, the first 2*PREFETCH_DIST iterations are implemented
4162306a36Sopenharmony_ci *	as the separate .prefetch_loop.  Logically, this loop performs exactly like the
4262306a36Sopenharmony_ci *	main-loop (.line_copy), but has all known-to-be-predicated-off instructions removed,
4362306a36Sopenharmony_ci *	so that each loop iteration is faster (again, good for cached case).
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci *	When reading the code, it helps to keep the following picture in mind:
4662306a36Sopenharmony_ci *
4762306a36Sopenharmony_ci *	       word 0 word 1
4862306a36Sopenharmony_ci *            +------+------+---
4962306a36Sopenharmony_ci *	      |	v[x] | 	t1  | ^
5062306a36Sopenharmony_ci *	      |	t2   |	t3  | |
5162306a36Sopenharmony_ci *	      |	t4   |	t5  | |
5262306a36Sopenharmony_ci *	      |	t6   |	t7  | | 128 bytes
5362306a36Sopenharmony_ci *     	      |	n[y] | 	t9  | |	(L2 cache line)
5462306a36Sopenharmony_ci *	      |	t10  | 	t11 | |
5562306a36Sopenharmony_ci *	      |	t12  | 	t13 | |
5662306a36Sopenharmony_ci *	      |	t14  | 	t15 | v
5762306a36Sopenharmony_ci *	      +------+------+---
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci *	Here, v[x] is copied by the (memory) prefetch.  n[y] is loaded at p[C]
6062306a36Sopenharmony_ci *	to fetch the second-half of the L2 cache line into L1, and the tX words are copied in
6162306a36Sopenharmony_ci *	an order that avoids bank conflicts.
6262306a36Sopenharmony_ci */
6362306a36Sopenharmony_ci#include <linux/export.h>
6462306a36Sopenharmony_ci#include <asm/asmmacro.h>
6562306a36Sopenharmony_ci#include <asm/page.h>
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#define PREFETCH_DIST	8		// McKinley sustains 16 outstanding L2 misses (8 ld, 8 st)
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci#define src0		r2
7062306a36Sopenharmony_ci#define src1		r3
7162306a36Sopenharmony_ci#define dst0		r9
7262306a36Sopenharmony_ci#define dst1		r10
7362306a36Sopenharmony_ci#define src_pre_mem	r11
7462306a36Sopenharmony_ci#define dst_pre_mem	r14
7562306a36Sopenharmony_ci#define src_pre_l2	r15
7662306a36Sopenharmony_ci#define dst_pre_l2	r16
7762306a36Sopenharmony_ci#define t1		r17
7862306a36Sopenharmony_ci#define t2		r18
7962306a36Sopenharmony_ci#define t3		r19
8062306a36Sopenharmony_ci#define t4		r20
8162306a36Sopenharmony_ci#define t5		t1	// alias!
8262306a36Sopenharmony_ci#define t6		t2	// alias!
8362306a36Sopenharmony_ci#define t7		t3	// alias!
8462306a36Sopenharmony_ci#define t9		t5	// alias!
8562306a36Sopenharmony_ci#define t10		t4	// alias!
8662306a36Sopenharmony_ci#define t11		t7	// alias!
8762306a36Sopenharmony_ci#define t12		t6	// alias!
8862306a36Sopenharmony_ci#define t14		t10	// alias!
8962306a36Sopenharmony_ci#define t13		r21
9062306a36Sopenharmony_ci#define t15		r22
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#define saved_lc	r23
9362306a36Sopenharmony_ci#define saved_pr	r24
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci#define	A	0
9662306a36Sopenharmony_ci#define B	(PREFETCH_DIST)
9762306a36Sopenharmony_ci#define C	(B + PREFETCH_DIST)
9862306a36Sopenharmony_ci#define D	(C + 3)
9962306a36Sopenharmony_ci#define N	(D + 1)
10062306a36Sopenharmony_ci#define Nrot	((N + 7) & ~7)
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciGLOBAL_ENTRY(copy_page)
10362306a36Sopenharmony_ci	.prologue
10462306a36Sopenharmony_ci	alloc r8 = ar.pfs, 2, Nrot-2, 0, Nrot
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	.rotr v[2*PREFETCH_DIST], n[D-C+1]
10762306a36Sopenharmony_ci	.rotp p[N]
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	.save ar.lc, saved_lc
11062306a36Sopenharmony_ci	mov saved_lc = ar.lc
11162306a36Sopenharmony_ci	.save pr, saved_pr
11262306a36Sopenharmony_ci	mov saved_pr = pr
11362306a36Sopenharmony_ci	.body
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	mov src_pre_mem = in1
11662306a36Sopenharmony_ci	mov pr.rot = 0x10000
11762306a36Sopenharmony_ci	mov ar.ec = 1				// special unrolled loop
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	mov dst_pre_mem = in0
12062306a36Sopenharmony_ci	mov ar.lc = 2*PREFETCH_DIST - 1
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	add src_pre_l2 = 8*8, in1
12362306a36Sopenharmony_ci	add dst_pre_l2 = 8*8, in0
12462306a36Sopenharmony_ci	add src0 = 8, in1			// first t1 src
12562306a36Sopenharmony_ci	add src1 = 3*8, in1			// first t3 src
12662306a36Sopenharmony_ci	add dst0 = 8, in0			// first t1 dst
12762306a36Sopenharmony_ci	add dst1 = 3*8, in0			// first t3 dst
12862306a36Sopenharmony_ci	mov t1 = (PAGE_SIZE/128) - (2*PREFETCH_DIST) - 1
12962306a36Sopenharmony_ci	nop.m 0
13062306a36Sopenharmony_ci	nop.i 0
13162306a36Sopenharmony_ci	;;
13262306a36Sopenharmony_ci	// same as .line_copy loop, but with all predicated-off instructions removed:
13362306a36Sopenharmony_ci.prefetch_loop:
13462306a36Sopenharmony_ci(p[A])	ld8 v[A] = [src_pre_mem], 128		// M0
13562306a36Sopenharmony_ci(p[B])	st8 [dst_pre_mem] = v[B], 128		// M2
13662306a36Sopenharmony_ci	br.ctop.sptk .prefetch_loop
13762306a36Sopenharmony_ci	;;
13862306a36Sopenharmony_ci	cmp.eq p16, p0 = r0, r0			// reset p16 to 1 (br.ctop cleared it to zero)
13962306a36Sopenharmony_ci	mov ar.lc = t1				// with 64KB pages, t1 is too big to fit in 8 bits!
14062306a36Sopenharmony_ci	mov ar.ec = N				// # of stages in pipeline
14162306a36Sopenharmony_ci	;;
14262306a36Sopenharmony_ci.line_copy:
14362306a36Sopenharmony_ci(p[D])	ld8 t2 = [src0], 3*8			// M0
14462306a36Sopenharmony_ci(p[D])	ld8 t4 = [src1], 3*8			// M1
14562306a36Sopenharmony_ci(p[B])	st8 [dst_pre_mem] = v[B], 128		// M2 prefetch dst from memory
14662306a36Sopenharmony_ci(p[D])	st8 [dst_pre_l2] = n[D-C], 128		// M3 prefetch dst from L2
14762306a36Sopenharmony_ci	;;
14862306a36Sopenharmony_ci(p[A])	ld8 v[A] = [src_pre_mem], 128		// M0 prefetch src from memory
14962306a36Sopenharmony_ci(p[C])	ld8 n[0] = [src_pre_l2], 128		// M1 prefetch src from L2
15062306a36Sopenharmony_ci(p[D])	st8 [dst0] =  t1, 8			// M2
15162306a36Sopenharmony_ci(p[D])	st8 [dst1] =  t3, 8			// M3
15262306a36Sopenharmony_ci	;;
15362306a36Sopenharmony_ci(p[D])	ld8  t5 = [src0], 8
15462306a36Sopenharmony_ci(p[D])	ld8  t7 = [src1], 3*8
15562306a36Sopenharmony_ci(p[D])	st8 [dst0] =  t2, 3*8
15662306a36Sopenharmony_ci(p[D])	st8 [dst1] =  t4, 3*8
15762306a36Sopenharmony_ci	;;
15862306a36Sopenharmony_ci(p[D])	ld8  t6 = [src0], 3*8
15962306a36Sopenharmony_ci(p[D])	ld8 t10 = [src1], 8
16062306a36Sopenharmony_ci(p[D])	st8 [dst0] =  t5, 8
16162306a36Sopenharmony_ci(p[D])	st8 [dst1] =  t7, 3*8
16262306a36Sopenharmony_ci	;;
16362306a36Sopenharmony_ci(p[D])	ld8  t9 = [src0], 3*8
16462306a36Sopenharmony_ci(p[D])	ld8 t11 = [src1], 3*8
16562306a36Sopenharmony_ci(p[D])	st8 [dst0] =  t6, 3*8
16662306a36Sopenharmony_ci(p[D])	st8 [dst1] = t10, 8
16762306a36Sopenharmony_ci	;;
16862306a36Sopenharmony_ci(p[D])	ld8 t12 = [src0], 8
16962306a36Sopenharmony_ci(p[D])	ld8 t14 = [src1], 8
17062306a36Sopenharmony_ci(p[D])	st8 [dst0] =  t9, 3*8
17162306a36Sopenharmony_ci(p[D])	st8 [dst1] = t11, 3*8
17262306a36Sopenharmony_ci	;;
17362306a36Sopenharmony_ci(p[D])	ld8 t13 = [src0], 4*8
17462306a36Sopenharmony_ci(p[D])	ld8 t15 = [src1], 4*8
17562306a36Sopenharmony_ci(p[D])	st8 [dst0] = t12, 8
17662306a36Sopenharmony_ci(p[D])	st8 [dst1] = t14, 8
17762306a36Sopenharmony_ci	;;
17862306a36Sopenharmony_ci(p[D-1])ld8  t1 = [src0], 8
17962306a36Sopenharmony_ci(p[D-1])ld8  t3 = [src1], 8
18062306a36Sopenharmony_ci(p[D])	st8 [dst0] = t13, 4*8
18162306a36Sopenharmony_ci(p[D])	st8 [dst1] = t15, 4*8
18262306a36Sopenharmony_ci	br.ctop.sptk .line_copy
18362306a36Sopenharmony_ci	;;
18462306a36Sopenharmony_ci	mov ar.lc = saved_lc
18562306a36Sopenharmony_ci	mov pr = saved_pr, -1
18662306a36Sopenharmony_ci	br.ret.sptk.many rp
18762306a36Sopenharmony_ciEND(copy_page)
18862306a36Sopenharmony_ciEXPORT_SYMBOL(copy_page)
189