xref: /netbsd-src/sys/arch/arm/arm/cpu_in_cksum.S (revision 51c5f9b7c2b2cc93506078d2cab158634a65201f)
1/*	$NetBSD: cpu_in_cksum.S,v 1.6 2013/08/18 15:58:19 matt 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.6 2013/08/18 15:58:19 matt 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	blt	.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(.L_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, lr}
133	RET
134
135.Lin_cksum_whoops:
136	adr	r0, .Lin_cksum_whoops_str
137	bl	_C_LABEL(panic)
138.Lin_cksum_whoops_str:
139	.asciz	"in_cksum: out of mbufs\n"
140	.align	5
141END(cpu_in_cksum)
142
143
144/*
145 * The main in*_cksum() workhorse...
146 *
147 * Entry parameters:
148 *	r0	Pointer to buffer
149 *	r1	Buffer length
150 *	lr	Return address
151 *
152 * Returns:
153 *	r2	Accumulated 32-bit sum
154 *
155 * Clobbers:
156 *	r0-r7
157 */
158/* LINTSTUB: Ignore */
159ASENTRY_NP(.L_cksumdata)
160#ifdef __PROG26
161	str	lr, [sp, #-4]!		/* for SVC26 mode */
162#endif
163#ifdef __XSCALE__
164	pld	[r0]			/* Pre-fetch the start of the buffer */
165#endif
166	movs	r2, #0
167
168	/* We first have to word-align the buffer.  */
169	ands	r7, r0, #0x03
170	beq	.Lcksumdata_wordaligned
171	eors	r0, r0, r7		/* r0 is word aligned */
172	ldr	r2, [r0], #0x04
173#ifdef __thumb__
174	movs	r4, r7
175	lsls	r4, r4, #3
176#else
177	lsl	r4, r7, #3
178#endif
179#if defined(__ARMEB__)
180	lsls	r2, r2, r4
181	lsrs	r2, r2, r4
182#else
183	lsrs	r2, r2, r4
184	lsls	r2, r2, r4
185#endif
186	rsb	r7, r7, #0x04
187	subs	r1, r1, r7		/* Enough bytes left to make it? */
188	bgt	.Lcksumdata_wordaligned
189#ifdef __PROG26
190	ldreq	pc, [sp], #4		/* done */
191#else
192	RETc(eq)			/* done */
193#endif
194	adds	r7, r7, r1		/* undo sub */
195	adds	r7, r7, r1		/* r7 = offset + len */
196	rsb	r7, r7, #4
197	lsls	r7, r7, #3
198#if defined(__ARMEB__)
199	lsrs	r2, r2, r7
200	lsls	r2, r2, r7
201#else
202	lsls	r2, r2, r7
203	lsrs	r2, r2, r7
204#endif
205#ifdef __PROG26
206	ldr	pc, [sp], #4		/* done */
207#else
208	RET				/* done */
209#endif
210
211	/* Buffer is now word aligned */
212.Lcksumdata_wordaligned:
213#ifdef __XSCALE__
214	cmp	r1, #0x04		/* Less than 4 bytes left? */
215	blt	.Lcksumdata_endgame	/* Yup */
216
217	/* Now quad-align, if necessary */
218	ands	r7, r0, #0x04
219	ldrne	r7, [r0], #0x04
220	subne	r1, r1, #0x04
221	subs	r1, r1, #0x40
222	blt	.Lcksumdata_bigloop_end	/* Note: C flag clear if branch taken */
223
224	/*
225	 * Buffer is now quad aligned. Sum 64 bytes at a time.
226	 * Note: First ldrd is hoisted above the loop, together with
227	 * setting r6 to zero to avoid stalling for results in the
228	 * loop. (r7 is live, from above).
229	 */
230	ldrd	r4, [r0], #0x08
231	mov	r6, #0x00
232.Lcksumdata_bigloop:
233	pld	[r0, #0x18]
234	adds	r2, r2, r6
235	adcs	r2, r2, r7
236	ldrd	r6, [r0], #0x08
237	adcs	r2, r2, r4
238	adcs	r2, r2, r5
239	ldrd	r4, [r0], #0x08
240	adcs	r2, r2, r6
241	adcs	r2, r2, r7
242	ldrd	r6, [r0], #0x08
243	adcs	r2, r2, r4
244	adcs	r2, r2, r5
245	ldrd	r4, [r0], #0x08
246	adcs	r2, r2, r6
247	adcs	r2, r2, r7
248	pld	[r0, #0x18]
249	ldrd	r6, [r0], #0x08
250	adcs	r2, r2, r4
251	adcs	r2, r2, r5
252	ldrd	r4, [r0], #0x08
253	adcs	r2, r2, r6
254	adcs	r2, r2, r7
255	ldrd	r6, [r0], #0x08
256	adcs	r2, r2, r4
257	adcs	r2, r2, r5
258	adcs	r2, r2, #0x00
259	subs	r1, r1, #0x40
260	ldrdge	r4, [r0], #0x08
261	bge	.Lcksumdata_bigloop
262
263	adds	r2, r2, r6		/* r6/r7 still need summing */
264.Lcksumdata_bigloop_end:
265	adcs	r2, r2, r7
266	adcs	r2, r2, #0x00
267
268#else	/* !__XSCALE__ */
269
270	subs	r1, r1, #0x40
271	blt	.Lcksumdata_bigloop_end
272
273.Lcksumdata_bigloop:
274	ldmia	r0!, {r3, r4, r5, r6}
275	adds	r2, r2, r3
276	adcs	r2, r2, r4
277	adcs	r2, r2, r5
278	ldmia	r0!, {r3, r4, r5, r7}
279	adcs	r2, r2, r6
280	adcs	r2, r2, r3
281	adcs	r2, r2, r4
282	adcs	r2, r2, r5
283	ldmia	r0!, {r3, r4, r5, r6}
284	adcs	r2, r2, r7
285	adcs	r2, r2, r3
286	adcs	r2, r2, r4
287	adcs	r2, r2, r5
288	ldmia	r0!, {r3, r4, r5, r7}
289	adcs	r2, r2, r6
290	adcs	r2, r2, r3
291	adcs	r2, r2, r4
292	adcs	r2, r2, r5
293	adcs	r2, r2, r7
294	adcs	r2, r2, #0x00
295	subs	r1, r1, #0x40
296	bge	.Lcksumdata_bigloop
297.Lcksumdata_bigloop_end:
298#endif
299
300	adds	r1, r1, #0x40
301#ifdef __PROG26
302	ldreq	pc, [sp], #4
303#else
304	RETc(eq)
305#endif
306	cmp	r1, #0x20
307
308#ifdef __XSCALE__
309	ldrdge	r4, [r0], #0x08		/* Avoid stalling pld and result */
310	blt	.Lcksumdata_less_than_32
311	pld	[r0, #0x18]
312	ldrd	r6, [r0], #0x08
313	adds	r2, r2, r4
314	adcs	r2, r2, r5
315	ldrd	r4, [r0], #0x08
316	adcs	r2, r2, r6
317	adcs	r2, r2, r7
318	ldrd	r6, [r0], #0x08
319	adcs	r2, r2, r4
320	adcs	r2, r2, r5
321	adcs	r2, r2, r6		/* XXX: Unavoidable result stall */
322	adcs	r2, r2, r7
323#else
324	blt	.Lcksumdata_less_than_32
325	ldmia	r0!, {r3, r4, r5, r6}
326	adds	r2, r2, r3
327	adcs	r2, r2, r4
328	adcs	r2, r2, r5
329	ldmia	r0!, {r3, r4, r5, r7}
330	adcs	r2, r2, r6
331	adcs	r2, r2, r3
332	adcs	r2, r2, r4
333	adcs	r2, r2, r5
334	adcs	r2, r2, r7
335#endif
336	adcs	r2, r2, #0x00
337	subs	r1, r1, #0x20
338#ifdef __PROG26
339	ldreq	pc, [sp], #4
340#else
341	RETc(eq)
342#endif
343
344.Lcksumdata_less_than_32:
345	/* There are less than 32 bytes left */
346	and	r3, r1, #0x18
347	rsb	r4, r3, #0x18
348	subs	r1, r1, r3
349	adds	r4, r4, r4, lsr #1	/* Side effect: Clear carry flag */
350#ifdef __thumb__
351	it	ne
352#endif
353	addne	pc, pc, r4
354
355/*
356 * Note: We use ldm here, even on Xscale, since the combined issue/result
357 * latencies for ldm and ldrd are the same. Using ldm avoids needless #ifdefs.
358 */
359	/* At least 24 bytes remaining... */
360	ldmia	r0!, {r4, r5}
361	nop
362	adcs	r2, r2, r4
363	adcs	r2, r2, r5
364
365	/* At least 16 bytes remaining... */
366	ldmia	r0!, {r4, r5}
367	adcs	r2, r2, r4
368	adcs	r2, r2, r5
369
370	/* At least 8 bytes remaining... */
371	ldmia	r0!, {r4, r5}
372	adcs	r2, r2, r4
373	adcs	r2, r2, r5
374
375	/* Less than 8 bytes remaining... */
376	adcs	r2, r2, #0x00
377	subs	r1, r1, #0x04
378	blt	.Lcksumdata_lessthan4
379
380	ldr	r4, [r0], #0x04
381	subs	r1, r1, #0x04
382	adds	r2, r2, r4
383	adcs	r2, r2, #0x00
384
385	/* Deal with < 4 bytes remaining */
386.Lcksumdata_lessthan4:
387	adds	r1, r1, #0x04
388#ifdef __PROG26
389	ldreq	pc, [sp], #4
390#else
391	RETc(eq)
392#endif
393
394	/* Deal with 1 to 3 remaining bytes, possibly misaligned */
395.Lcksumdata_endgame:
396	ldr	r3, [r0]		/* Fetch last word */
397	rsb	r1, r1, #4		/* get discard amount */
398	lsl	r1, r1, #3		/* turn it into bits */
399#ifdef __ARMEB__
400	lsr	r3, r3, r1		/* discard least significant bits */
401	lsl	r3, r3, r1		/* shift back filling with zeros */
402#else
403	lsl	r3, r3, r1		/* discard least significant bits */
404	lsr	r3, r3, r1		/* shift back filling with zeros */
405#endif
406	adds	r2, r2, r3
407	adcs	r2, r2, #0x00
408#ifdef __PROG26
409	ldr	pc, [sp], #4
410#else
411	RET
412#endif
413ASEND(.L_cksumdata)
414