1*4882a593Smuzhiyun/* SPDX-License-Identifier: GPL-2.0 */ 2*4882a593Smuzhiyun/* 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Optmized version of the standard do_csum() function 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * Return: a 64bit quantity containing the 16bit Internet checksum 7*4882a593Smuzhiyun * 8*4882a593Smuzhiyun * Inputs: 9*4882a593Smuzhiyun * in0: address of buffer to checksum (char *) 10*4882a593Smuzhiyun * in1: length of the buffer (int) 11*4882a593Smuzhiyun * 12*4882a593Smuzhiyun * Copyright (C) 1999, 2001-2002 Hewlett-Packard Co 13*4882a593Smuzhiyun * Stephane Eranian <eranian@hpl.hp.com> 14*4882a593Smuzhiyun * 15*4882a593Smuzhiyun * 02/04/22 Ken Chen <kenneth.w.chen@intel.com> 16*4882a593Smuzhiyun * Data locality study on the checksum buffer. 17*4882a593Smuzhiyun * More optimization cleanup - remove excessive stop bits. 18*4882a593Smuzhiyun * 02/04/08 David Mosberger <davidm@hpl.hp.com> 19*4882a593Smuzhiyun * More cleanup and tuning. 20*4882a593Smuzhiyun * 01/04/18 Jun Nakajima <jun.nakajima@intel.com> 21*4882a593Smuzhiyun * Clean up and optimize and the software pipeline, loading two 22*4882a593Smuzhiyun * back-to-back 8-byte words per loop. Clean up the initialization 23*4882a593Smuzhiyun * for the loop. Support the cases where load latency = 1 or 2. 24*4882a593Smuzhiyun * Set CONFIG_IA64_LOAD_LATENCY to 1 or 2 (default). 25*4882a593Smuzhiyun */ 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun#include <asm/asmmacro.h> 28*4882a593Smuzhiyun 29*4882a593Smuzhiyun// 30*4882a593Smuzhiyun// Theory of operations: 31*4882a593Smuzhiyun// The goal is to go as quickly as possible to the point where 32*4882a593Smuzhiyun// we can checksum 16 bytes/loop. Before reaching that point we must 33*4882a593Smuzhiyun// take care of incorrect alignment of first byte. 34*4882a593Smuzhiyun// 35*4882a593Smuzhiyun// The code hereafter also takes care of the "tail" part of the buffer 36*4882a593Smuzhiyun// before entering the core loop, if any. The checksum is a sum so it 37*4882a593Smuzhiyun// allows us to commute operations. So we do the "head" and "tail" 38*4882a593Smuzhiyun// first to finish at full speed in the body. Once we get the head and 39*4882a593Smuzhiyun// tail values, we feed them into the pipeline, very handy initialization. 40*4882a593Smuzhiyun// 41*4882a593Smuzhiyun// Of course we deal with the special case where the whole buffer fits 42*4882a593Smuzhiyun// into one 8 byte word. In this case we have only one entry in the pipeline. 43*4882a593Smuzhiyun// 44*4882a593Smuzhiyun// We use a (LOAD_LATENCY+2)-stage pipeline in the loop to account for 45*4882a593Smuzhiyun// possible load latency and also to accommodate for head and tail. 46*4882a593Smuzhiyun// 47*4882a593Smuzhiyun// The end of the function deals with folding the checksum from 64bits 48*4882a593Smuzhiyun// down to 16bits taking care of the carry. 49*4882a593Smuzhiyun// 50*4882a593Smuzhiyun// This version avoids synchronization in the core loop by also using a 51*4882a593Smuzhiyun// pipeline for the accumulation of the checksum in resultx[] (x=1,2). 52*4882a593Smuzhiyun// 53*4882a593Smuzhiyun// wordx[] (x=1,2) 54*4882a593Smuzhiyun// |---| 55*4882a593Smuzhiyun// | | 0 : new value loaded in pipeline 56*4882a593Smuzhiyun// |---| 57*4882a593Smuzhiyun// | | - : in transit data 58*4882a593Smuzhiyun// |---| 59*4882a593Smuzhiyun// | | LOAD_LATENCY : current value to add to checksum 60*4882a593Smuzhiyun// |---| 61*4882a593Smuzhiyun// | | LOAD_LATENCY+1 : previous value added to checksum 62*4882a593Smuzhiyun// |---| (previous iteration) 63*4882a593Smuzhiyun// 64*4882a593Smuzhiyun// resultx[] (x=1,2) 65*4882a593Smuzhiyun// |---| 66*4882a593Smuzhiyun// | | 0 : initial value 67*4882a593Smuzhiyun// |---| 68*4882a593Smuzhiyun// | | LOAD_LATENCY-1 : new checksum 69*4882a593Smuzhiyun// |---| 70*4882a593Smuzhiyun// | | LOAD_LATENCY : previous value of checksum 71*4882a593Smuzhiyun// |---| 72*4882a593Smuzhiyun// | | LOAD_LATENCY+1 : final checksum when out of the loop 73*4882a593Smuzhiyun// |---| 74*4882a593Smuzhiyun// 75*4882a593Smuzhiyun// 76*4882a593Smuzhiyun// See RFC1071 "Computing the Internet Checksum" for various techniques for 77*4882a593Smuzhiyun// calculating the Internet checksum. 78*4882a593Smuzhiyun// 79*4882a593Smuzhiyun// NOT YET DONE: 80*4882a593Smuzhiyun// - Maybe another algorithm which would take care of the folding at the 81*4882a593Smuzhiyun// end in a different manner 82*4882a593Smuzhiyun// - Work with people more knowledgeable than me on the network stack 83*4882a593Smuzhiyun// to figure out if we could not split the function depending on the 84*4882a593Smuzhiyun// type of packet or alignment we get. Like the ip_fast_csum() routine 85*4882a593Smuzhiyun// where we know we have at least 20bytes worth of data to checksum. 86*4882a593Smuzhiyun// - Do a better job of handling small packets. 87*4882a593Smuzhiyun// - Note on prefetching: it was found that under various load, i.e. ftp read/write, 88*4882a593Smuzhiyun// nfs read/write, the L1 cache hit rate is at 60% and L2 cache hit rate is at 99.8% 89*4882a593Smuzhiyun// on the data that buffer points to (partly because the checksum is often preceded by 90*4882a593Smuzhiyun// a copy_from_user()). This finding indiate that lfetch will not be beneficial since 91*4882a593Smuzhiyun// the data is already in the cache. 92*4882a593Smuzhiyun// 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun#define saved_pfs r11 95*4882a593Smuzhiyun#define hmask r16 96*4882a593Smuzhiyun#define tmask r17 97*4882a593Smuzhiyun#define first1 r18 98*4882a593Smuzhiyun#define firstval r19 99*4882a593Smuzhiyun#define firstoff r20 100*4882a593Smuzhiyun#define last r21 101*4882a593Smuzhiyun#define lastval r22 102*4882a593Smuzhiyun#define lastoff r23 103*4882a593Smuzhiyun#define saved_lc r24 104*4882a593Smuzhiyun#define saved_pr r25 105*4882a593Smuzhiyun#define tmp1 r26 106*4882a593Smuzhiyun#define tmp2 r27 107*4882a593Smuzhiyun#define tmp3 r28 108*4882a593Smuzhiyun#define carry1 r29 109*4882a593Smuzhiyun#define carry2 r30 110*4882a593Smuzhiyun#define first2 r31 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun#define buf in0 113*4882a593Smuzhiyun#define len in1 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun#define LOAD_LATENCY 2 // XXX fix me 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun#if (LOAD_LATENCY != 1) && (LOAD_LATENCY != 2) 118*4882a593Smuzhiyun# error "Only 1 or 2 is supported/tested for LOAD_LATENCY." 119*4882a593Smuzhiyun#endif 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun#define PIPE_DEPTH (LOAD_LATENCY+2) 122*4882a593Smuzhiyun#define ELD p[LOAD_LATENCY] // end of load 123*4882a593Smuzhiyun#define ELD_1 p[LOAD_LATENCY+1] // and next stage 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun// unsigned long do_csum(unsigned char *buf,long len) 126*4882a593Smuzhiyun 127*4882a593SmuzhiyunGLOBAL_ENTRY(do_csum) 128*4882a593Smuzhiyun .prologue 129*4882a593Smuzhiyun .save ar.pfs, saved_pfs 130*4882a593Smuzhiyun alloc saved_pfs=ar.pfs,2,16,0,16 131*4882a593Smuzhiyun .rotr word1[4], word2[4],result1[LOAD_LATENCY+2],result2[LOAD_LATENCY+2] 132*4882a593Smuzhiyun .rotp p[PIPE_DEPTH], pC1[2], pC2[2] 133*4882a593Smuzhiyun mov ret0=r0 // in case we have zero length 134*4882a593Smuzhiyun cmp.lt p0,p6=r0,len // check for zero length or negative (32bit len) 135*4882a593Smuzhiyun ;; 136*4882a593Smuzhiyun add tmp1=buf,len // last byte's address 137*4882a593Smuzhiyun .save pr, saved_pr 138*4882a593Smuzhiyun mov saved_pr=pr // preserve predicates (rotation) 139*4882a593Smuzhiyun(p6) br.ret.spnt.many rp // return if zero or negative length 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun mov hmask=-1 // initialize head mask 142*4882a593Smuzhiyun tbit.nz p15,p0=buf,0 // is buf an odd address? 143*4882a593Smuzhiyun and first1=-8,buf // 8-byte align down address of first1 element 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun and firstoff=7,buf // how many bytes off for first1 element 146*4882a593Smuzhiyun mov tmask=-1 // initialize tail mask 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun ;; 149*4882a593Smuzhiyun adds tmp2=-1,tmp1 // last-1 150*4882a593Smuzhiyun and lastoff=7,tmp1 // how many bytes off for last element 151*4882a593Smuzhiyun ;; 152*4882a593Smuzhiyun sub tmp1=8,lastoff // complement to lastoff 153*4882a593Smuzhiyun and last=-8,tmp2 // address of word containing last byte 154*4882a593Smuzhiyun ;; 155*4882a593Smuzhiyun sub tmp3=last,first1 // tmp3=distance from first1 to last 156*4882a593Smuzhiyun .save ar.lc, saved_lc 157*4882a593Smuzhiyun mov saved_lc=ar.lc // save lc 158*4882a593Smuzhiyun cmp.eq p8,p9=last,first1 // everything fits in one word ? 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun ld8 firstval=[first1],8 // load, ahead of time, "first1" word 161*4882a593Smuzhiyun and tmp1=7, tmp1 // make sure that if tmp1==8 -> tmp1=0 162*4882a593Smuzhiyun shl tmp2=firstoff,3 // number of bits 163*4882a593Smuzhiyun ;; 164*4882a593Smuzhiyun(p9) ld8 lastval=[last] // load, ahead of time, "last" word, if needed 165*4882a593Smuzhiyun shl tmp1=tmp1,3 // number of bits 166*4882a593Smuzhiyun(p9) adds tmp3=-8,tmp3 // effectively loaded 167*4882a593Smuzhiyun ;; 168*4882a593Smuzhiyun(p8) mov lastval=r0 // we don't need lastval if first1==last 169*4882a593Smuzhiyun shl hmask=hmask,tmp2 // build head mask, mask off [0,first1off[ 170*4882a593Smuzhiyun shr.u tmask=tmask,tmp1 // build tail mask, mask off ]8,lastoff] 171*4882a593Smuzhiyun ;; 172*4882a593Smuzhiyun .body 173*4882a593Smuzhiyun#define count tmp3 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun(p8) and hmask=hmask,tmask // apply tail mask to head mask if 1 word only 176*4882a593Smuzhiyun(p9) and word2[0]=lastval,tmask // mask last it as appropriate 177*4882a593Smuzhiyun shr.u count=count,3 // how many 8-byte? 178*4882a593Smuzhiyun ;; 179*4882a593Smuzhiyun // If count is odd, finish this 8-byte word so that we can 180*4882a593Smuzhiyun // load two back-to-back 8-byte words per loop thereafter. 181*4882a593Smuzhiyun and word1[0]=firstval,hmask // and mask it as appropriate 182*4882a593Smuzhiyun tbit.nz p10,p11=count,0 // if (count is odd) 183*4882a593Smuzhiyun ;; 184*4882a593Smuzhiyun(p8) mov result1[0]=word1[0] 185*4882a593Smuzhiyun(p9) add result1[0]=word1[0],word2[0] 186*4882a593Smuzhiyun ;; 187*4882a593Smuzhiyun cmp.ltu p6,p0=result1[0],word1[0] // check the carry 188*4882a593Smuzhiyun cmp.eq.or.andcm p8,p0=0,count // exit if zero 8-byte 189*4882a593Smuzhiyun ;; 190*4882a593Smuzhiyun(p6) adds result1[0]=1,result1[0] 191*4882a593Smuzhiyun(p8) br.cond.dptk .do_csum_exit // if (within an 8-byte word) 192*4882a593Smuzhiyun(p11) br.cond.dptk .do_csum16 // if (count is even) 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun // Here count is odd. 195*4882a593Smuzhiyun ld8 word1[1]=[first1],8 // load an 8-byte word 196*4882a593Smuzhiyun cmp.eq p9,p10=1,count // if (count == 1) 197*4882a593Smuzhiyun adds count=-1,count // loaded an 8-byte word 198*4882a593Smuzhiyun ;; 199*4882a593Smuzhiyun add result1[0]=result1[0],word1[1] 200*4882a593Smuzhiyun ;; 201*4882a593Smuzhiyun cmp.ltu p6,p0=result1[0],word1[1] 202*4882a593Smuzhiyun ;; 203*4882a593Smuzhiyun(p6) adds result1[0]=1,result1[0] 204*4882a593Smuzhiyun(p9) br.cond.sptk .do_csum_exit // if (count == 1) exit 205*4882a593Smuzhiyun // Fall through to calculate the checksum, feeding result1[0] as 206*4882a593Smuzhiyun // the initial value in result1[0]. 207*4882a593Smuzhiyun // 208*4882a593Smuzhiyun // Calculate the checksum loading two 8-byte words per loop. 209*4882a593Smuzhiyun // 210*4882a593Smuzhiyun.do_csum16: 211*4882a593Smuzhiyun add first2=8,first1 212*4882a593Smuzhiyun shr.u count=count,1 // we do 16 bytes per loop 213*4882a593Smuzhiyun ;; 214*4882a593Smuzhiyun adds count=-1,count 215*4882a593Smuzhiyun mov carry1=r0 216*4882a593Smuzhiyun mov carry2=r0 217*4882a593Smuzhiyun brp.loop.imp 1f,2f 218*4882a593Smuzhiyun ;; 219*4882a593Smuzhiyun mov ar.ec=PIPE_DEPTH 220*4882a593Smuzhiyun mov ar.lc=count // set lc 221*4882a593Smuzhiyun mov pr.rot=1<<16 222*4882a593Smuzhiyun // result1[0] must be initialized in advance. 223*4882a593Smuzhiyun mov result2[0]=r0 224*4882a593Smuzhiyun ;; 225*4882a593Smuzhiyun .align 32 226*4882a593Smuzhiyun1: 227*4882a593Smuzhiyun(ELD_1) cmp.ltu pC1[0],p0=result1[LOAD_LATENCY],word1[LOAD_LATENCY+1] 228*4882a593Smuzhiyun(pC1[1])adds carry1=1,carry1 229*4882a593Smuzhiyun(ELD_1) cmp.ltu pC2[0],p0=result2[LOAD_LATENCY],word2[LOAD_LATENCY+1] 230*4882a593Smuzhiyun(pC2[1])adds carry2=1,carry2 231*4882a593Smuzhiyun(ELD) add result1[LOAD_LATENCY-1]=result1[LOAD_LATENCY],word1[LOAD_LATENCY] 232*4882a593Smuzhiyun(ELD) add result2[LOAD_LATENCY-1]=result2[LOAD_LATENCY],word2[LOAD_LATENCY] 233*4882a593Smuzhiyun2: 234*4882a593Smuzhiyun(p[0]) ld8 word1[0]=[first1],16 235*4882a593Smuzhiyun(p[0]) ld8 word2[0]=[first2],16 236*4882a593Smuzhiyun br.ctop.sptk 1b 237*4882a593Smuzhiyun ;; 238*4882a593Smuzhiyun // Since len is a 32-bit value, carry cannot be larger than a 64-bit value. 239*4882a593Smuzhiyun(pC1[1])adds carry1=1,carry1 // since we miss the last one 240*4882a593Smuzhiyun(pC2[1])adds carry2=1,carry2 241*4882a593Smuzhiyun ;; 242*4882a593Smuzhiyun add result1[LOAD_LATENCY+1]=result1[LOAD_LATENCY+1],carry1 243*4882a593Smuzhiyun add result2[LOAD_LATENCY+1]=result2[LOAD_LATENCY+1],carry2 244*4882a593Smuzhiyun ;; 245*4882a593Smuzhiyun cmp.ltu p6,p0=result1[LOAD_LATENCY+1],carry1 246*4882a593Smuzhiyun cmp.ltu p7,p0=result2[LOAD_LATENCY+1],carry2 247*4882a593Smuzhiyun ;; 248*4882a593Smuzhiyun(p6) adds result1[LOAD_LATENCY+1]=1,result1[LOAD_LATENCY+1] 249*4882a593Smuzhiyun(p7) adds result2[LOAD_LATENCY+1]=1,result2[LOAD_LATENCY+1] 250*4882a593Smuzhiyun ;; 251*4882a593Smuzhiyun add result1[0]=result1[LOAD_LATENCY+1],result2[LOAD_LATENCY+1] 252*4882a593Smuzhiyun ;; 253*4882a593Smuzhiyun cmp.ltu p6,p0=result1[0],result2[LOAD_LATENCY+1] 254*4882a593Smuzhiyun ;; 255*4882a593Smuzhiyun(p6) adds result1[0]=1,result1[0] 256*4882a593Smuzhiyun ;; 257*4882a593Smuzhiyun.do_csum_exit: 258*4882a593Smuzhiyun // 259*4882a593Smuzhiyun // now fold 64 into 16 bits taking care of carry 260*4882a593Smuzhiyun // that's not very good because it has lots of sequentiality 261*4882a593Smuzhiyun // 262*4882a593Smuzhiyun mov tmp3=0xffff 263*4882a593Smuzhiyun zxt4 tmp1=result1[0] 264*4882a593Smuzhiyun shr.u tmp2=result1[0],32 265*4882a593Smuzhiyun ;; 266*4882a593Smuzhiyun add result1[0]=tmp1,tmp2 267*4882a593Smuzhiyun ;; 268*4882a593Smuzhiyun and tmp1=result1[0],tmp3 269*4882a593Smuzhiyun shr.u tmp2=result1[0],16 270*4882a593Smuzhiyun ;; 271*4882a593Smuzhiyun add result1[0]=tmp1,tmp2 272*4882a593Smuzhiyun ;; 273*4882a593Smuzhiyun and tmp1=result1[0],tmp3 274*4882a593Smuzhiyun shr.u tmp2=result1[0],16 275*4882a593Smuzhiyun ;; 276*4882a593Smuzhiyun add result1[0]=tmp1,tmp2 277*4882a593Smuzhiyun ;; 278*4882a593Smuzhiyun and tmp1=result1[0],tmp3 279*4882a593Smuzhiyun shr.u tmp2=result1[0],16 280*4882a593Smuzhiyun ;; 281*4882a593Smuzhiyun add ret0=tmp1,tmp2 282*4882a593Smuzhiyun mov pr=saved_pr,0xffffffffffff0000 283*4882a593Smuzhiyun ;; 284*4882a593Smuzhiyun // if buf was odd then swap bytes 285*4882a593Smuzhiyun mov ar.pfs=saved_pfs // restore ar.ec 286*4882a593Smuzhiyun(p15) mux1 ret0=ret0,@rev // reverse word 287*4882a593Smuzhiyun ;; 288*4882a593Smuzhiyun mov ar.lc=saved_lc 289*4882a593Smuzhiyun(p15) shr.u ret0=ret0,64-16 // + shift back to position = swap bytes 290*4882a593Smuzhiyun br.ret.sptk.many rp 291*4882a593Smuzhiyun 292*4882a593Smuzhiyun// I (Jun Nakajima) wrote an equivalent code (see below), but it was 293*4882a593Smuzhiyun// not much better than the original. So keep the original there so that 294*4882a593Smuzhiyun// someone else can challenge. 295*4882a593Smuzhiyun// 296*4882a593Smuzhiyun// shr.u word1[0]=result1[0],32 297*4882a593Smuzhiyun// zxt4 result1[0]=result1[0] 298*4882a593Smuzhiyun// ;; 299*4882a593Smuzhiyun// add result1[0]=result1[0],word1[0] 300*4882a593Smuzhiyun// ;; 301*4882a593Smuzhiyun// zxt2 result2[0]=result1[0] 302*4882a593Smuzhiyun// extr.u word1[0]=result1[0],16,16 303*4882a593Smuzhiyun// shr.u carry1=result1[0],32 304*4882a593Smuzhiyun// ;; 305*4882a593Smuzhiyun// add result2[0]=result2[0],word1[0] 306*4882a593Smuzhiyun// ;; 307*4882a593Smuzhiyun// add result2[0]=result2[0],carry1 308*4882a593Smuzhiyun// ;; 309*4882a593Smuzhiyun// extr.u ret0=result2[0],16,16 310*4882a593Smuzhiyun// ;; 311*4882a593Smuzhiyun// add ret0=ret0,result2[0] 312*4882a593Smuzhiyun// ;; 313*4882a593Smuzhiyun// zxt2 ret0=ret0 314*4882a593Smuzhiyun// mov ar.pfs=saved_pfs // restore ar.ec 315*4882a593Smuzhiyun// mov pr=saved_pr,0xffffffffffff0000 316*4882a593Smuzhiyun// ;; 317*4882a593Smuzhiyun// // if buf was odd then swap bytes 318*4882a593Smuzhiyun// mov ar.lc=saved_lc 319*4882a593Smuzhiyun//(p15) mux1 ret0=ret0,@rev // reverse word 320*4882a593Smuzhiyun// ;; 321*4882a593Smuzhiyun//(p15) shr.u ret0=ret0,64-16 // + shift back to position = swap bytes 322*4882a593Smuzhiyun// br.ret.sptk.many rp 323*4882a593Smuzhiyun 324*4882a593SmuzhiyunEND(do_csum) 325