Home | History | Annotate | Line # | Download | only in cortex
      1 /*	$NetBSD: cpu_in_cksum_neon.c,v 1.2 2025/06/19 22:00:54 andvar Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas of 3am Software Foundry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 
     34 __KERNEL_RCSID(0, "$NetBSD: cpu_in_cksum_neon.c,v 1.2 2025/06/19 22:00:54 andvar Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/cpu.h>
     38 #include <sys/mbuf.h>
     39 
     40 #include <netinet/in.h>
     41 #include <netinet/ip.h>
     42 
     43 uint32_t cpu_in_cksum_neon(const void *, size_t);
     44 uint32_t cpu_in_cksum_neon_v4hdr(const void *);
     45 
     46 int
     47 cpu_in_cksum(struct mbuf *m, int len, int off, uint32_t initial_sum)
     48 {
     49 	uint32_t csum = initial_sum;
     50 	int odd = 0;
     51 
     52 	/*
     53 	 * Taken control of the NEON PCU.
     54 	 */
     55 	vfp_hijack();
     56 
     57 	/*
     58 	 * Fast path for the normal ip_header
     59 	 */
     60 	if (off == 0
     61 	    && csum == 0
     62 	    && len == sizeof(struct ip)
     63 	    && ((uintptr_t)m->m_data & 3) == 0
     64 	    && m->m_len >= len) {
     65 		csum = cpu_in_cksum_neon_v4hdr(m->m_data);
     66 
     67 		/*
     68 		 * We are now down with NEON.
     69 		 */
     70 		vfp_surrender();
     71 
     72 		if (csum == 0x10000)	/* note 0x10000 - 0xffff == 1 */
     73 			return 1;
     74 		return csum == 0 ? 0xffff : csum;	/* never return 0. */
     75 	}
     76 
     77 	/*
     78 	 * Skip the initial mbufs
     79 	 */
     80 	while (m->m_len >= off) {
     81 		m = m->m_next;
     82 		off -= m->m_len;
     83 		KASSERT(m != NULL);
     84 	}
     85 
     86 	for (; len > 0; m = m->m_next, off = 0) {
     87 		KASSERT(m != NULL);
     88 		int dlen = MIN(m->m_len - off, len);
     89 		const void *dptr = m->m_data + off;
     90 		/*
     91 		 * This routine will add based on the memory layout so
     92 		 * if the previous len was odd or the this buffer starts
     93 		 * on an odd address, shift the csum by 8 so its properly
     94 		 * aligned.  It will be taken care of when we do the final
     95 		 * checksum fold.
     96 		 */
     97 		uint32_t tmpsum = cpu_in_cksum_neon(dptr, dlen);
     98 		if (odd ^ ((uint32_t)dptr & 1))
     99 			tmpsum <<= 8;
    100 		/*
    101 		 * Accumulate checksum, folding will be done later
    102 		 */
    103 		csum += tmpsum;
    104 		odd ^= dlen & 1;
    105 		len -= dlen;
    106 	}
    107 
    108 	/*
    109 	 * We are now down with NEON.
    110 	 */
    111 	vfp_surrender();
    112 
    113 	/*
    114 	 * Time to fold the checksum
    115 	 */
    116 	csum = (csum >> 16) + (csum & 0xffff);
    117 	/*
    118 	 * Now it could be 0x1xxxx so fold again
    119 	 */
    120 	csum = (csum >> 16) + (csum & 0xffff);
    121 
    122 	KASSERT(csum <= 0x10000);
    123 	if (csum == 0x10000)	/* note 0x10000 - 0xffff == 1 */
    124 		return 1;
    125 	return csum == 0 ? 0xffff : csum;	/* never return 0. */
    126 }
    127