xref: /freebsd/sys/arm/arm/in_cksum_arm.S (revision 81ad6265)
1/*	$NetBSD: in_cksum_arm.S,v 1.2 2003/09/23 10:01:36 scw 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/*
40 * Hand-optimised in_cksum() and in4_cksum() implementations for ARM/armv5e
41 */
42
43#include "opt_inet.h"
44
45#include <machine/asm.h>
46#include "assym.inc"
47__FBSDID("$FreeBSD$");
48
49	.syntax	unified
50/*
51 * int in_cksum(struct mbuf *m, int len)
52 *
53 * Entry:
54 *	r0	m
55 *	r1	len
56 *
57 * NOTE: Assumes 'm' is *never* NULL.
58 */
59/* LINTSTUB: Func: int in_cksum(struct mbuf *, int) */
60ENTRY(in_cksum)
61	stmfd	sp!, {r4-r11,lr}
62	mov	r8, #0x00
63	mov	r9, r1
64	mov	r10, #0x00
65	mov	ip, r0
66
67.Lin_cksum_loop:
68	ldr	r1, [ip, #(M_LEN)]
69	ldr	r0, [ip, #(M_DATA)]
70	ldr	ip, [ip, #(M_NEXT)]
71.Lin_cksum_entry4:
72	cmp	r9, r1
73	movlt	r1, r9
74	sub	r9, r9, r1
75	eor	r11, r10, r0
76	add	r10, r10, r1
77	adds	r2, r1, #0x00
78	blne	_ASM_LABEL(L_cksumdata)
79	tst	r11, #0x01
80	movne	r2, r2, ror #8
81	adds	r8, r8, r2
82	adc	r8, r8, #0x00
83	cmp	ip, #0x00
84	bne	.Lin_cksum_loop
85
86	mov	r1, #0xff
87	orr	r1, r1, #0xff00
88	and	r0, r8, r1
89	add	r0, r0, r8, lsr #16
90	add	r0, r0, r0, lsr #16
91	and	r0, r0, r1
92	eor	r0, r0, r1
93	ldmfd	sp!, {r4-r11,pc}
94END(in_cksum)
95
96ENTRY(do_cksum)
97	stmfd	sp!, {r4-r7, lr}
98	bl	L_cksumdata
99	mov	r0, r2
100	ldmfd	sp!, {r4-r7, pc}
101END(do_cksum)
102
103/*
104 * The main in*_cksum() workhorse...
105 *
106 * Entry parameters:
107 *	r0	Pointer to buffer
108 *	r1	Buffer length
109 *	lr	Return address
110 *
111 * Returns:
112 *	r2	Accumulated 32-bit sum
113 *
114 * Clobbers:
115 *	r0-r7
116 */
117/* LINTSTUB: Ignore */
118ASENTRY_NP(L_cksumdata)
119	pld	[r0]			/* Pre-fetch the start of the buffer */
120	mov	r2, #0
121
122	/* We first have to word-align the buffer.  */
123	ands	r7, r0, #0x03
124	beq	.Lcksumdata_wordaligned
125	rsb	r7, r7, #0x04
126	cmp	r1, r7			/* Enough bytes left to make it? */
127	blt	.Lcksumdata_endgame
128	cmp	r7, #0x02
129	ldrb	r4, [r0], #0x01		/* Fetch 1st byte */
130	ldrbge	r5, [r0], #0x01		/* Fetch 2nd byte */
131	movlt	r5, #0x00
132	ldrbgt	r6, [r0], #0x01		/* Fetch 3rd byte */
133	movle	r6, #0x00
134
135	/* Combine the three bytes depending on endianness and alignment */
136	orreq	r2, r4, r5, lsl #8
137	orreq	r2, r2, r6, lsl #16
138	orrne	r2, r5, r4, lsl #8
139	orrne	r2, r2, r6, lsl #24
140	subs	r1, r1, r7		/* Update length */
141	RETeq				/* All done? */
142
143	/* Buffer is now word aligned */
144.Lcksumdata_wordaligned:
145	cmp	r1, #0x04		/* Less than 4 bytes left? */
146	blt	.Lcksumdata_endgame	/* Yup */
147
148	/* Now quad-align, if necessary */
149	ands	r7, r0, #0x04
150	ldrne	r7, [r0], #0x04
151	subne	r1, r1, #0x04
152	subs	r1, r1, #0x40
153	blt	.Lcksumdata_bigloop_end	/* Note: C flag clear if branch taken */
154
155	/*
156	 * Buffer is now quad aligned. Sum 64 bytes at a time.
157	 * Note: First ldrd is hoisted above the loop, together with
158	 * setting r6 to zero to avoid stalling for results in the
159	 * loop. (r7 is live, from above).
160	 */
161	ldrd	r4, [r0], #0x08
162	mov	r6, #0x00
163.Lcksumdata_bigloop:
164	pld	[r0, #0x18]
165	adds	r2, r2, r6
166	adcs	r2, r2, r7
167	ldrd	r6, [r0], #0x08
168	adcs	r2, r2, r4
169	adcs	r2, r2, r5
170	ldrd	r4, [r0], #0x08
171	adcs	r2, r2, r6
172	adcs	r2, r2, r7
173	ldrd	r6, [r0], #0x08
174	adcs	r2, r2, r4
175	adcs	r2, r2, r5
176	ldrd	r4, [r0], #0x08
177	adcs	r2, r2, r6
178	adcs	r2, r2, r7
179	pld	[r0, #0x18]
180	ldrd	r6, [r0], #0x08
181	adcs	r2, r2, r4
182	adcs	r2, r2, r5
183	ldrd	r4, [r0], #0x08
184	adcs	r2, r2, r6
185	adcs	r2, r2, r7
186	ldrd	r6, [r0], #0x08
187	adcs	r2, r2, r4
188	adcs	r2, r2, r5
189	adc	r2, r2, #0x00
190	subs	r1, r1, #0x40
191	ldrdge	r4, [r0], #0x08
192	bge	.Lcksumdata_bigloop
193
194	adds	r2, r2, r6		/* r6/r7 still need summing */
195.Lcksumdata_bigloop_end:
196	adcs	r2, r2, r7
197	adc	r2, r2, #0x00
198
199	adds	r1, r1, #0x40
200	RETeq
201	cmp	r1, #0x20
202
203	ldrdge	r4, [r0], #0x08		/* Avoid stalling pld and result */
204	blt	.Lcksumdata_less_than_32
205	pld	[r0, #0x18]
206	ldrd	r6, [r0], #0x08
207	adds	r2, r2, r4
208	adcs	r2, r2, r5
209	ldrd	r4, [r0], #0x08
210	adcs	r2, r2, r6
211	adcs	r2, r2, r7
212	ldrd	r6, [r0], #0x08
213	adcs	r2, r2, r4
214	adcs	r2, r2, r5
215	adcs	r2, r2, r6		/* XXX: Unavoidable result stall */
216	adcs	r2, r2, r7
217	adc	r2, r2, #0x00
218	subs	r1, r1, #0x20
219	RETeq
220
221.Lcksumdata_less_than_32:
222	/* There are less than 32 bytes left */
223	and	r3, r1, #0x18
224	rsb	r4, r3, #0x18
225	sub	r1, r1, r3
226	adds	r4, r4, r4, lsr #1	/* Side effect: Clear carry flag */
227	addne	pc, pc, r4
228	nop
229
230/*
231 * Note: We use ldm here, even on armv5e, since the combined issue/result
232 * latencies for ldm and ldrd are the same. Using ldm avoids needless #ifdefs.
233 */
234	/* At least 24 bytes remaining... */
235	ldmia	r0!, {r4, r5}
236	adcs	r2, r2, r4
237	adcs	r2, r2, r5
238
239	/* At least 16 bytes remaining... */
240	ldmia	r0!, {r4, r5}
241	adcs	r2, r2, r4
242	adcs	r2, r2, r5
243
244	/* At least 8 bytes remaining... */
245	ldmia	r0!, {r4, r5}
246	adcs	r2, r2, r4
247	adcs	r2, r2, r5
248
249	/* Less than 8 bytes remaining... */
250	adc	r2, r2, #0x00
251	subs	r1, r1, #0x04
252	blt	.Lcksumdata_lessthan4
253
254	ldr	r4, [r0], #0x04
255	sub	r1, r1, #0x04
256	adds	r2, r2, r4
257	adc	r2, r2, #0x00
258
259	/* Deal with < 4 bytes remaining */
260.Lcksumdata_lessthan4:
261	adds	r1, r1, #0x04
262	RETeq
263
264	/* Deal with 1 to 3 remaining bytes, possibly misaligned */
265.Lcksumdata_endgame:
266	ldrb	r3, [r0]		/* Fetch first byte */
267	cmp	r1, #0x02
268	ldrbge	r4, [r0, #0x01]		/* Fetch 2nd and 3rd as necessary */
269	movlt	r4, #0x00
270	ldrbgt	r5, [r0, #0x02]
271	movle	r5, #0x00
272	/* Combine the three bytes depending on endianness and alignment */
273	tst	r0, #0x01
274	orreq	r3, r3, r4, lsl #8
275	orreq	r3, r3, r5, lsl #16
276	orrne	r3, r4, r3, lsl #8
277	orrne	r3, r3, r5, lsl #24
278	adds	r2, r2, r3
279	adc	r2, r2, #0x00
280	RET
281END(L_cksumdata)
282
283