1/* $NetBSD: cpu_in_cksum.S,v 1.13 2022/10/20 06:58:38 skrll Exp $ */ 2 3/* 4 * Copyright 2003 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Steve C. Woodford for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38/* 39 * Hand-optimised in_cksum() and in4_cksum() implementations for ARM/Xscale 40 */ 41 42#include <machine/asm.h> 43RCSID("$NetBSD: cpu_in_cksum.S,v 1.13 2022/10/20 06:58:38 skrll Exp $") 44 45#include "assym.h" 46 47/* 48 * int cpu_in_cksum(struct mbuf *m, int len, int off, uint32_t initial_sum) 49 * 50 * Entry: 51 * r0 m 52 * r1 len 53 * r2 off 54 * r3 initial_sum 55 * 56 * Function wide register usage 57 * r8 accumulated sum 58 * r9 remaining length to parse 59 * ip pointer to next mbuf 60 */ 61/* LINTSTUB: Func: int cpu_in_cksum(struct mbuf *, int, int, uint32_t) */ 62ENTRY(cpu_in_cksum) 63 push {r4-r11,lr} 64 65 mov r8, r3 /* Accumulate sum in r8 */ 66 mov r9, r1 /* save len in r9 */ 67 mov ip, r0 /* set ip to the current mbuf */ 68 69.Lin_cksum_skip_loop: 70 ldr r1, [ip, #(M_LEN)] 71 ldr r0, [ip, #(M_DATA)] 72 ldr ip, [ip, #(M_NEXT)] 73.Lin_cksum_skip_entry: 74 subs r2, r2, r1 /* offset = offset - mbuf length */ 75 ble .Lin_cksum_skip_done /* if offset has gone negative start with this mbuf */ 76 cmp ip, #0x00 77 bne .Lin_cksum_skip_loop 78 b .Lin_cksum_whoops 79 80.Lin_cksum_skip_done: 81 add r0, r2, r0 /* data += offset (offset is < 0) */ 82 add r0, r0, r1 /* data += length of mbuf */ 83 /* data == start of data to cksum */ 84 rsb r1, r2, #0x00 /* length = remainder of mbuf to read */ 85 mov r10, #0x00 86 b .Lin_cksum_entry 87 88.Lin_cksum_loop: 89 ldr r1, [ip, #(M_LEN)] 90 ldr r0, [ip, #(M_DATA)] 91 ldr ip, [ip, #(M_NEXT)] 92.Lin_cksum_entry: 93 cmp r9, r1 94#ifdef __thumb__ 95 bge 1f 96 mov r1, r9 97#else 98 movlt r1, r9 99#endif 1001: sub r9, r9, r1 101 eor r11, r10, r0 102 add r10, r10, r1 103 adds r2, r1, #0x00 104#ifdef __thumb__ 105 it ne 106#endif 107 blne _ASM_LABEL(arm_cksumdata) 108 tst r11, #0x01 109#ifdef __thumb__ 110 it ne 111#endif 112 movne r2, r2, ror #8 113 adds r8, r8, r2 114 adc r8, r8, #0x00 115 cmp ip, #00 116 bne .Lin_cksum_loop 117 118#ifdef __thumb__ 119 mov r0, r8 120 lsls r2, r0, #16 121 adds r0, r0, r2 122 bcc 1f 123 adds r0, r0, #65536 1241: mvns r0, r0 125 lsrs r0, r0, #16 126#else 127 adds r8, r8, r8, lsl #16 128 addcs r8, r8, #65536 129 mvn r0, r8 130 lsr r0, r0, #16 131#endif 132 pop {r4-r11, pc} 133 134.Lin_cksum_whoops: 135 adr r0, .Lin_cksum_whoops_str 136 bl _C_LABEL(panic) 137.Lin_cksum_whoops_str: 138 .asciz "in_cksum: out of mbufs\n" 139 .p2align 5 140END(cpu_in_cksum) 141 142 143/* 144 * The main in*_cksum() workhorse... 145 * 146 * Entry parameters: 147 * r0 Pointer to buffer 148 * r1 Buffer length 149 * lr Return address 150 * 151 * Returns: 152 * r2 Accumulated 32-bit sum 153 * 154 * Clobbers: 155 * r0-r7 156 */ 157/* LINTSTUB: Ignore */ 158ASENTRY_NP(arm_cksumdata) 159#ifdef __XSCALE__ 160 pld [r0] /* Pre-fetch the start of the buffer */ 161#endif 162 movs r2, #0 163 164 /* We first have to word-align the buffer. */ 165 ands r7, r0, #0x03 166 beq .Lcksumdata_wordaligned 167 eors r0, r0, r7 /* r0 is word aligned */ 168 ldr r2, [r0], #0x04 169#ifdef __thumb__ 170 movs r4, r7 171 lsls r4, r4, #3 172#else 173 lsl r4, r7, #3 174#endif 175#if defined(__ARMEB__) 176 lsls r2, r2, r4 177 lsrs r2, r2, r4 178#else 179 lsrs r2, r2, r4 180 lsls r2, r2, r4 181#endif 182 rsb r7, r7, #0x04 183 subs r1, r1, r7 /* Enough bytes left to make it? */ 184 bgt .Lcksumdata_wordaligned 185 RETc(eq) /* done */ 186 adds r1, r1, r7 /* undo sub */ 187 subs r7, r7, r1 188 lsls r7, r7, #3 189#if defined(__ARMEB__) 190 lsrs r2, r2, r7 191 lsls r2, r2, r7 192#else 193 lsls r2, r2, r7 194 lsrs r2, r2, r7 195#endif 196 RET /* done */ 197 198 /* Buffer is now word aligned */ 199.Lcksumdata_wordaligned: 200#ifdef __XSCALE__ 201 cmp r1, #0x04 /* Less than 4 bytes left? */ 202 blt .Lcksumdata_endgame /* Yup */ 203 204 /* Now quad-align, if necessary */ 205 ands r7, r0, #0x04 206 ldrne r7, [r0], #0x04 207 subne r1, r1, #0x04 208 subs r1, r1, #0x40 209 blt .Lcksumdata_bigloop_end /* Note: C flag clear if branch taken */ 210 211 /* 212 * Buffer is now quad aligned. Sum 64 bytes at a time. 213 * Note: First ldrd is hoisted above the loop, together with 214 * setting r6 to zero to avoid stalling for results in the 215 * loop. (r7 is live, from above). 216 */ 217 ldrd r4, r5, [r0], #0x08 218 mov r6, #0x00 219.Lcksumdata_bigloop: 220 pld [r0, #0x18] 221 adds r2, r2, r6 222 adcs r2, r2, r7 223 ldrd r6, r7, [r0], #0x08 224 adcs r2, r2, r4 225 adcs r2, r2, r5 226 ldrd r4, r5, [r0], #0x08 227 adcs r2, r2, r6 228 adcs r2, r2, r7 229 ldrd r6, r7, [r0], #0x08 230 adcs r2, r2, r4 231 adcs r2, r2, r5 232 ldrd r4, r5, [r0], #0x08 233 adcs r2, r2, r6 234 adcs r2, r2, r7 235 pld [r0, #0x18] 236 ldrd r6, r7, [r0], #0x08 237 adcs r2, r2, r4 238 adcs r2, r2, r5 239 ldrd r4, r5, [r0], #0x08 240 adcs r2, r2, r6 241 adcs r2, r2, r7 242 ldrd r6, r7, [r0], #0x08 243 adcs r2, r2, r4 244 adcs r2, r2, r5 245 adcs r2, r2, #0x00 246 subs r1, r1, #0x40 247 ldrdge r4, r5, [r0], #0x08 248 bge .Lcksumdata_bigloop 249 250 adds r2, r2, r6 /* r6/r7 still need summing */ 251.Lcksumdata_bigloop_end: 252 adcs r2, r2, r7 253 adcs r2, r2, #0x00 254 255#else /* !__XSCALE__ */ 256 257 subs r1, r1, #0x40 258 blt .Lcksumdata_bigloop_end 259 260.Lcksumdata_bigloop: 261 ldmia r0!, {r3, r4, r5, r6} 262 adds r2, r2, r3 263 adcs r2, r2, r4 264 adcs r2, r2, r5 265 ldmia r0!, {r3, r4, r5, r7} 266 adcs r2, r2, r6 267 adcs r2, r2, r3 268 adcs r2, r2, r4 269 adcs r2, r2, r5 270 ldmia r0!, {r3, r4, r5, r6} 271 adcs r2, r2, r7 272 adcs r2, r2, r3 273 adcs r2, r2, r4 274 adcs r2, r2, r5 275 ldmia r0!, {r3, r4, r5, r7} 276 adcs r2, r2, r6 277 adcs r2, r2, r3 278 adcs r2, r2, r4 279 adcs r2, r2, r5 280 adcs r2, r2, r7 281 adcs r2, r2, #0x00 282 subs r1, r1, #0x40 283 bge .Lcksumdata_bigloop 284.Lcksumdata_bigloop_end: 285#endif 286 287 adds r1, r1, #0x40 288 RETc(eq) 289 cmp r1, #0x20 290 291#ifdef __XSCALE__ 292 ldrdge r4, r5, [r0], #0x08 /* Avoid stalling pld and result */ 293 blt .Lcksumdata_less_than_32 294 pld [r0, #0x18] 295 ldrd r6, r7, [r0], #0x08 296 adds r2, r2, r4 297 adcs r2, r2, r5 298 ldrd r4, r5, [r0], #0x08 299 adcs r2, r2, r6 300 adcs r2, r2, r7 301 ldrd r6, r7, [r0], #0x08 302 adcs r2, r2, r4 303 adcs r2, r2, r5 304 adcs r2, r2, r6 /* XXX: Unavoidable result stall */ 305 adcs r2, r2, r7 306#else 307 blt .Lcksumdata_less_than_32 308 ldmia r0!, {r3, r4, r5, r6} 309 adds r2, r2, r3 310 adcs r2, r2, r4 311 adcs r2, r2, r5 312 ldmia r0!, {r3, r4, r5, r7} 313 adcs r2, r2, r6 314 adcs r2, r2, r3 315 adcs r2, r2, r4 316 adcs r2, r2, r5 317 adcs r2, r2, r7 318#endif 319 adcs r2, r2, #0x00 320 subs r1, r1, #0x20 321 RETc(eq) 322 323.Lcksumdata_less_than_32: 324 /* There are less than 32 bytes left */ 325 and r3, r1, #0x18 326 rsb r4, r3, #0x18 327 subs r1, r1, r3 328 adds r4, r4, r4, lsr #1 /* Side effect: Clear carry flag */ 329#ifdef __thumb__ 330 it ne 331#endif 332 addne pc, pc, r4 333 334/* 335 * Note: We use ldm here, even on Xscale, since the combined issue/result 336 * latencies for ldm and ldrd are the same. Using ldm avoids needless #ifdefs. 337 */ 338 /* At least 24 bytes remaining... */ 339 ldmia r0!, {r4, r5} 340 nop 341 adcs r2, r2, r4 342 adcs r2, r2, r5 343 344 /* At least 16 bytes remaining... */ 345 ldmia r0!, {r4, r5} 346 adcs r2, r2, r4 347 adcs r2, r2, r5 348 349 /* At least 8 bytes remaining... */ 350 ldmia r0!, {r4, r5} 351 adcs r2, r2, r4 352 adcs r2, r2, r5 353 354 /* Less than 8 bytes remaining... */ 355 adcs r2, r2, #0x00 356 subs r1, r1, #0x04 357 blt .Lcksumdata_lessthan4 358 359 ldr r4, [r0], #0x04 360 subs r1, r1, #0x04 361 adds r2, r2, r4 362 adcs r2, r2, #0x00 363 364 /* Deal with < 4 bytes remaining */ 365.Lcksumdata_lessthan4: 366 adds r1, r1, #0x04 367 RETc(eq) 368 369 /* Deal with 1 to 3 remaining bytes, possibly misaligned */ 370.Lcksumdata_endgame: 371 ldr r3, [r0] /* Fetch last word */ 372 rsb r1, r1, #4 /* get discard amount */ 373 lsl r1, r1, #3 /* turn it into bits */ 374#ifdef __ARMEB__ 375 lsr r3, r3, r1 /* discard least significant bits */ 376 lsl r3, r3, r1 /* shift back filling with zeros */ 377#else 378 lsl r3, r3, r1 /* discard least significant bits */ 379 lsr r3, r3, r1 /* shift back filling with zeros */ 380#endif 381 adds r2, r2, r3 382 adcs r2, r2, #0x00 383 RET 384ASEND(arm_cksumdata) 385