18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * McKinley-optimized version of copy_page(). 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2002 Hewlett-Packard Co 68c2ecf20Sopenharmony_ci * David Mosberger <davidm@hpl.hp.com> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Inputs: 98c2ecf20Sopenharmony_ci * in0: address of target page 108c2ecf20Sopenharmony_ci * in1: address of source page 118c2ecf20Sopenharmony_ci * Output: 128c2ecf20Sopenharmony_ci * no return value 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * General idea: 158c2ecf20Sopenharmony_ci * - use regular loads and stores to prefetch data to avoid consuming M-slot just for 168c2ecf20Sopenharmony_ci * lfetches => good for in-cache performance 178c2ecf20Sopenharmony_ci * - avoid l2 bank-conflicts by not storing into the same 16-byte bank within a single 188c2ecf20Sopenharmony_ci * cycle 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * Principle of operation: 218c2ecf20Sopenharmony_ci * First, note that L1 has a line-size of 64 bytes and L2 a line-size of 128 bytes. 228c2ecf20Sopenharmony_ci * To avoid secondary misses in L2, we prefetch both source and destination with a line-size 238c2ecf20Sopenharmony_ci * of 128 bytes. When both of these lines are in the L2 and the first half of the 248c2ecf20Sopenharmony_ci * source line is in L1, we start copying the remaining words. The second half of the 258c2ecf20Sopenharmony_ci * source line is prefetched in an earlier iteration, so that by the time we start 268c2ecf20Sopenharmony_ci * accessing it, it's also present in the L1. 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * We use a software-pipelined loop to control the overall operation. The pipeline 298c2ecf20Sopenharmony_ci * has 2*PREFETCH_DIST+K stages. The first PREFETCH_DIST stages are used for prefetching 308c2ecf20Sopenharmony_ci * source cache-lines. The second PREFETCH_DIST stages are used for prefetching destination 318c2ecf20Sopenharmony_ci * cache-lines, the last K stages are used to copy the cache-line words not copied by 328c2ecf20Sopenharmony_ci * the prefetches. The four relevant points in the pipelined are called A, B, C, D: 338c2ecf20Sopenharmony_ci * p[A] is TRUE if a source-line should be prefetched, p[B] is TRUE if a destination-line 348c2ecf20Sopenharmony_ci * should be prefetched, p[C] is TRUE if the second half of an L2 line should be brought 358c2ecf20Sopenharmony_ci * into L1D and p[D] is TRUE if a cacheline needs to be copied. 368c2ecf20Sopenharmony_ci * 378c2ecf20Sopenharmony_ci * This all sounds very complicated, but thanks to the modulo-scheduled loop support, 388c2ecf20Sopenharmony_ci * the resulting code is very regular and quite easy to follow (once you get the idea). 398c2ecf20Sopenharmony_ci * 408c2ecf20Sopenharmony_ci * As a secondary optimization, the first 2*PREFETCH_DIST iterations are implemented 418c2ecf20Sopenharmony_ci * as the separate .prefetch_loop. Logically, this loop performs exactly like the 428c2ecf20Sopenharmony_ci * main-loop (.line_copy), but has all known-to-be-predicated-off instructions removed, 438c2ecf20Sopenharmony_ci * so that each loop iteration is faster (again, good for cached case). 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * When reading the code, it helps to keep the following picture in mind: 468c2ecf20Sopenharmony_ci * 478c2ecf20Sopenharmony_ci * word 0 word 1 488c2ecf20Sopenharmony_ci * +------+------+--- 498c2ecf20Sopenharmony_ci * | v[x] | t1 | ^ 508c2ecf20Sopenharmony_ci * | t2 | t3 | | 518c2ecf20Sopenharmony_ci * | t4 | t5 | | 528c2ecf20Sopenharmony_ci * | t6 | t7 | | 128 bytes 538c2ecf20Sopenharmony_ci * | n[y] | t9 | | (L2 cache line) 548c2ecf20Sopenharmony_ci * | t10 | t11 | | 558c2ecf20Sopenharmony_ci * | t12 | t13 | | 568c2ecf20Sopenharmony_ci * | t14 | t15 | v 578c2ecf20Sopenharmony_ci * +------+------+--- 588c2ecf20Sopenharmony_ci * 598c2ecf20Sopenharmony_ci * Here, v[x] is copied by the (memory) prefetch. n[y] is loaded at p[C] 608c2ecf20Sopenharmony_ci * to fetch the second-half of the L2 cache line into L1, and the tX words are copied in 618c2ecf20Sopenharmony_ci * an order that avoids bank conflicts. 628c2ecf20Sopenharmony_ci */ 638c2ecf20Sopenharmony_ci#include <asm/asmmacro.h> 648c2ecf20Sopenharmony_ci#include <asm/page.h> 658c2ecf20Sopenharmony_ci#include <asm/export.h> 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci#define PREFETCH_DIST 8 // McKinley sustains 16 outstanding L2 misses (8 ld, 8 st) 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci#define src0 r2 708c2ecf20Sopenharmony_ci#define src1 r3 718c2ecf20Sopenharmony_ci#define dst0 r9 728c2ecf20Sopenharmony_ci#define dst1 r10 738c2ecf20Sopenharmony_ci#define src_pre_mem r11 748c2ecf20Sopenharmony_ci#define dst_pre_mem r14 758c2ecf20Sopenharmony_ci#define src_pre_l2 r15 768c2ecf20Sopenharmony_ci#define dst_pre_l2 r16 778c2ecf20Sopenharmony_ci#define t1 r17 788c2ecf20Sopenharmony_ci#define t2 r18 798c2ecf20Sopenharmony_ci#define t3 r19 808c2ecf20Sopenharmony_ci#define t4 r20 818c2ecf20Sopenharmony_ci#define t5 t1 // alias! 828c2ecf20Sopenharmony_ci#define t6 t2 // alias! 838c2ecf20Sopenharmony_ci#define t7 t3 // alias! 848c2ecf20Sopenharmony_ci#define t9 t5 // alias! 858c2ecf20Sopenharmony_ci#define t10 t4 // alias! 868c2ecf20Sopenharmony_ci#define t11 t7 // alias! 878c2ecf20Sopenharmony_ci#define t12 t6 // alias! 888c2ecf20Sopenharmony_ci#define t14 t10 // alias! 898c2ecf20Sopenharmony_ci#define t13 r21 908c2ecf20Sopenharmony_ci#define t15 r22 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci#define saved_lc r23 938c2ecf20Sopenharmony_ci#define saved_pr r24 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci#define A 0 968c2ecf20Sopenharmony_ci#define B (PREFETCH_DIST) 978c2ecf20Sopenharmony_ci#define C (B + PREFETCH_DIST) 988c2ecf20Sopenharmony_ci#define D (C + 3) 998c2ecf20Sopenharmony_ci#define N (D + 1) 1008c2ecf20Sopenharmony_ci#define Nrot ((N + 7) & ~7) 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ciGLOBAL_ENTRY(copy_page) 1038c2ecf20Sopenharmony_ci .prologue 1048c2ecf20Sopenharmony_ci alloc r8 = ar.pfs, 2, Nrot-2, 0, Nrot 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci .rotr v[2*PREFETCH_DIST], n[D-C+1] 1078c2ecf20Sopenharmony_ci .rotp p[N] 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci .save ar.lc, saved_lc 1108c2ecf20Sopenharmony_ci mov saved_lc = ar.lc 1118c2ecf20Sopenharmony_ci .save pr, saved_pr 1128c2ecf20Sopenharmony_ci mov saved_pr = pr 1138c2ecf20Sopenharmony_ci .body 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci mov src_pre_mem = in1 1168c2ecf20Sopenharmony_ci mov pr.rot = 0x10000 1178c2ecf20Sopenharmony_ci mov ar.ec = 1 // special unrolled loop 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci mov dst_pre_mem = in0 1208c2ecf20Sopenharmony_ci mov ar.lc = 2*PREFETCH_DIST - 1 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci add src_pre_l2 = 8*8, in1 1238c2ecf20Sopenharmony_ci add dst_pre_l2 = 8*8, in0 1248c2ecf20Sopenharmony_ci add src0 = 8, in1 // first t1 src 1258c2ecf20Sopenharmony_ci add src1 = 3*8, in1 // first t3 src 1268c2ecf20Sopenharmony_ci add dst0 = 8, in0 // first t1 dst 1278c2ecf20Sopenharmony_ci add dst1 = 3*8, in0 // first t3 dst 1288c2ecf20Sopenharmony_ci mov t1 = (PAGE_SIZE/128) - (2*PREFETCH_DIST) - 1 1298c2ecf20Sopenharmony_ci nop.m 0 1308c2ecf20Sopenharmony_ci nop.i 0 1318c2ecf20Sopenharmony_ci ;; 1328c2ecf20Sopenharmony_ci // same as .line_copy loop, but with all predicated-off instructions removed: 1338c2ecf20Sopenharmony_ci.prefetch_loop: 1348c2ecf20Sopenharmony_ci(p[A]) ld8 v[A] = [src_pre_mem], 128 // M0 1358c2ecf20Sopenharmony_ci(p[B]) st8 [dst_pre_mem] = v[B], 128 // M2 1368c2ecf20Sopenharmony_ci br.ctop.sptk .prefetch_loop 1378c2ecf20Sopenharmony_ci ;; 1388c2ecf20Sopenharmony_ci cmp.eq p16, p0 = r0, r0 // reset p16 to 1 (br.ctop cleared it to zero) 1398c2ecf20Sopenharmony_ci mov ar.lc = t1 // with 64KB pages, t1 is too big to fit in 8 bits! 1408c2ecf20Sopenharmony_ci mov ar.ec = N // # of stages in pipeline 1418c2ecf20Sopenharmony_ci ;; 1428c2ecf20Sopenharmony_ci.line_copy: 1438c2ecf20Sopenharmony_ci(p[D]) ld8 t2 = [src0], 3*8 // M0 1448c2ecf20Sopenharmony_ci(p[D]) ld8 t4 = [src1], 3*8 // M1 1458c2ecf20Sopenharmony_ci(p[B]) st8 [dst_pre_mem] = v[B], 128 // M2 prefetch dst from memory 1468c2ecf20Sopenharmony_ci(p[D]) st8 [dst_pre_l2] = n[D-C], 128 // M3 prefetch dst from L2 1478c2ecf20Sopenharmony_ci ;; 1488c2ecf20Sopenharmony_ci(p[A]) ld8 v[A] = [src_pre_mem], 128 // M0 prefetch src from memory 1498c2ecf20Sopenharmony_ci(p[C]) ld8 n[0] = [src_pre_l2], 128 // M1 prefetch src from L2 1508c2ecf20Sopenharmony_ci(p[D]) st8 [dst0] = t1, 8 // M2 1518c2ecf20Sopenharmony_ci(p[D]) st8 [dst1] = t3, 8 // M3 1528c2ecf20Sopenharmony_ci ;; 1538c2ecf20Sopenharmony_ci(p[D]) ld8 t5 = [src0], 8 1548c2ecf20Sopenharmony_ci(p[D]) ld8 t7 = [src1], 3*8 1558c2ecf20Sopenharmony_ci(p[D]) st8 [dst0] = t2, 3*8 1568c2ecf20Sopenharmony_ci(p[D]) st8 [dst1] = t4, 3*8 1578c2ecf20Sopenharmony_ci ;; 1588c2ecf20Sopenharmony_ci(p[D]) ld8 t6 = [src0], 3*8 1598c2ecf20Sopenharmony_ci(p[D]) ld8 t10 = [src1], 8 1608c2ecf20Sopenharmony_ci(p[D]) st8 [dst0] = t5, 8 1618c2ecf20Sopenharmony_ci(p[D]) st8 [dst1] = t7, 3*8 1628c2ecf20Sopenharmony_ci ;; 1638c2ecf20Sopenharmony_ci(p[D]) ld8 t9 = [src0], 3*8 1648c2ecf20Sopenharmony_ci(p[D]) ld8 t11 = [src1], 3*8 1658c2ecf20Sopenharmony_ci(p[D]) st8 [dst0] = t6, 3*8 1668c2ecf20Sopenharmony_ci(p[D]) st8 [dst1] = t10, 8 1678c2ecf20Sopenharmony_ci ;; 1688c2ecf20Sopenharmony_ci(p[D]) ld8 t12 = [src0], 8 1698c2ecf20Sopenharmony_ci(p[D]) ld8 t14 = [src1], 8 1708c2ecf20Sopenharmony_ci(p[D]) st8 [dst0] = t9, 3*8 1718c2ecf20Sopenharmony_ci(p[D]) st8 [dst1] = t11, 3*8 1728c2ecf20Sopenharmony_ci ;; 1738c2ecf20Sopenharmony_ci(p[D]) ld8 t13 = [src0], 4*8 1748c2ecf20Sopenharmony_ci(p[D]) ld8 t15 = [src1], 4*8 1758c2ecf20Sopenharmony_ci(p[D]) st8 [dst0] = t12, 8 1768c2ecf20Sopenharmony_ci(p[D]) st8 [dst1] = t14, 8 1778c2ecf20Sopenharmony_ci ;; 1788c2ecf20Sopenharmony_ci(p[D-1])ld8 t1 = [src0], 8 1798c2ecf20Sopenharmony_ci(p[D-1])ld8 t3 = [src1], 8 1808c2ecf20Sopenharmony_ci(p[D]) st8 [dst0] = t13, 4*8 1818c2ecf20Sopenharmony_ci(p[D]) st8 [dst1] = t15, 4*8 1828c2ecf20Sopenharmony_ci br.ctop.sptk .line_copy 1838c2ecf20Sopenharmony_ci ;; 1848c2ecf20Sopenharmony_ci mov ar.lc = saved_lc 1858c2ecf20Sopenharmony_ci mov pr = saved_pr, -1 1868c2ecf20Sopenharmony_ci br.ret.sptk.many rp 1878c2ecf20Sopenharmony_ciEND(copy_page) 1888c2ecf20Sopenharmony_ciEXPORT_SYMBOL(copy_page) 189