GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / arm64 / lib / memset.S
1 /*
2  * Copyright (C) 2013 ARM Ltd.
3  * Copyright (C) 2013 Linaro.
4  *
5  * This code is based on glibc cortex strings work originally authored by Linaro
6  * and re-licensed under GPLv2 for the Linux kernel. The original code can
7  * be found @
8  *
9  * http://bazaar.launchpad.net/~linaro-toolchain-dev/cortex-strings/trunk/
10  * files/head:/src/aarch64/
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 #include <linux/linkage.h>
26 #include <asm/assembler.h>
27 #include <asm/cache.h>
28
29 /*
30  * Fill in the buffer with character c (alignment handled by the hardware)
31  *
32  * Parameters:
33  *      x0 - buf
34  *      x1 - c
35  *      x2 - n
36  * Returns:
37  *      x0 - buf
38  */
39
40 dstin           .req    x0
41 val             .req    w1
42 count           .req    x2
43 tmp1            .req    x3
44 tmp1w           .req    w3
45 tmp2            .req    x4
46 tmp2w           .req    w4
47 zva_len_x       .req    x5
48 zva_len         .req    w5
49 zva_bits_x      .req    x6
50
51 A_l             .req    x7
52 A_lw            .req    w7
53 dst             .req    x8
54 tmp3w           .req    w9
55 tmp3            .req    x9
56
57 ENTRY(__memset)
58 WEAK(memset)
59         mov     dst, dstin      /* Preserve return value.  */
60         and     A_lw, val, #255
61         orr     A_lw, A_lw, A_lw, lsl #8
62         orr     A_lw, A_lw, A_lw, lsl #16
63         orr     A_l, A_l, A_l, lsl #32
64
65         cmp     count, #15
66         b.hi    .Lover16_proc
67         /*All store maybe are non-aligned..*/
68         tbz     count, #3, 1f
69         str     A_l, [dst], #8
70 1:
71         tbz     count, #2, 2f
72         str     A_lw, [dst], #4
73 2:
74         tbz     count, #1, 3f
75         strh    A_lw, [dst], #2
76 3:
77         tbz     count, #0, 4f
78         strb    A_lw, [dst]
79 4:
80         ret
81
82 .Lover16_proc:
83         /*Whether  the start address is aligned with 16.*/
84         neg     tmp2, dst
85         ands    tmp2, tmp2, #15
86         b.eq    .Laligned
87 /*
88 * The count is not less than 16, we can use stp to store the start 16 bytes,
89 * then adjust the dst aligned with 16.This process will make the current
90 * memory address at alignment boundary.
91 */
92         stp     A_l, A_l, [dst] /*non-aligned store..*/
93         /*make the dst aligned..*/
94         sub     count, count, tmp2
95         add     dst, dst, tmp2
96
97 .Laligned:
98         cbz     A_l, .Lzero_mem
99
100 .Ltail_maybe_long:
101         cmp     count, #64
102         b.ge    .Lnot_short
103 .Ltail63:
104         ands    tmp1, count, #0x30
105         b.eq    3f
106         cmp     tmp1w, #0x20
107         b.eq    1f
108         b.lt    2f
109         stp     A_l, A_l, [dst], #16
110 1:
111         stp     A_l, A_l, [dst], #16
112 2:
113         stp     A_l, A_l, [dst], #16
114 /*
115 * The last store length is less than 16,use stp to write last 16 bytes.
116 * It will lead some bytes written twice and the access is non-aligned.
117 */
118 3:
119         ands    count, count, #15
120         cbz     count, 4f
121         add     dst, dst, count
122         stp     A_l, A_l, [dst, #-16]   /* Repeat some/all of last store. */
123 4:
124         ret
125
126         /*
127         * Critical loop. Start at a new cache line boundary. Assuming
128         * 64 bytes per line, this ensures the entire loop is in one line.
129         */
130         .p2align        L1_CACHE_SHIFT
131 .Lnot_short:
132         sub     dst, dst, #16/* Pre-bias.  */
133         sub     count, count, #64
134 1:
135         stp     A_l, A_l, [dst, #16]
136         stp     A_l, A_l, [dst, #32]
137         stp     A_l, A_l, [dst, #48]
138         stp     A_l, A_l, [dst, #64]!
139         subs    count, count, #64
140         b.ge    1b
141         tst     count, #0x3f
142         add     dst, dst, #16
143         b.ne    .Ltail63
144 .Lexitfunc:
145         ret
146
147         /*
148         * For zeroing memory, check to see if we can use the ZVA feature to
149         * zero entire 'cache' lines.
150         */
151 .Lzero_mem:
152         cmp     count, #63
153         b.le    .Ltail63
154         /*
155         * For zeroing small amounts of memory, it's not worth setting up
156         * the line-clear code.
157         */
158         cmp     count, #128
159         b.lt    .Lnot_short /*count is at least  128 bytes*/
160
161         mrs     tmp1, dczid_el0
162         tbnz    tmp1, #4, .Lnot_short
163         mov     tmp3w, #4
164         and     zva_len, tmp1w, #15     /* Safety: other bits reserved.  */
165         lsl     zva_len, tmp3w, zva_len
166
167         ands    tmp3w, zva_len, #63
168         /*
169         * ensure the zva_len is not less than 64.
170         * It is not meaningful to use ZVA if the block size is less than 64.
171         */
172         b.ne    .Lnot_short
173 .Lzero_by_line:
174         /*
175         * Compute how far we need to go to become suitably aligned. We're
176         * already at quad-word alignment.
177         */
178         cmp     count, zva_len_x
179         b.lt    .Lnot_short             /* Not enough to reach alignment.  */
180         sub     zva_bits_x, zva_len_x, #1
181         neg     tmp2, dst
182         ands    tmp2, tmp2, zva_bits_x
183         b.eq    2f                      /* Already aligned.  */
184         /* Not aligned, check that there's enough to copy after alignment.*/
185         sub     tmp1, count, tmp2
186         /*
187         * grantee the remain length to be ZVA is bigger than 64,
188         * avoid to make the 2f's process over mem range.*/
189         cmp     tmp1, #64
190         ccmp    tmp1, zva_len_x, #8, ge /* NZCV=0b1000 */
191         b.lt    .Lnot_short
192         /*
193         * We know that there's at least 64 bytes to zero and that it's safe
194         * to overrun by 64 bytes.
195         */
196         mov     count, tmp1
197 1:
198         stp     A_l, A_l, [dst]
199         stp     A_l, A_l, [dst, #16]
200         stp     A_l, A_l, [dst, #32]
201         subs    tmp2, tmp2, #64
202         stp     A_l, A_l, [dst, #48]
203         add     dst, dst, #64
204         b.ge    1b
205         /* We've overrun a bit, so adjust dst downwards.*/
206         add     dst, dst, tmp2
207 2:
208         sub     count, count, zva_len_x
209 3:
210         dc      zva, dst
211         add     dst, dst, zva_len_x
212         subs    count, count, zva_len_x
213         b.ge    3b
214         ands    count, count, zva_bits_x
215         b.ne    .Ltail_maybe_long
216         ret
217 ENDPIPROC(memset)
218 ENDPROC(__memset)