Commit | Line | Data |
---|---|---|
18aecc2b CM |
1 | /* |
2 | * Copyright 2011 Tilera Corporation. All Rights Reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation, version 2. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | |
11 | * NON INFRINGEMENT. See the GNU General Public License for | |
12 | * more details. | |
13 | */ | |
14 | ||
15 | #include <linux/types.h> | |
16 | #include <linux/string.h> | |
17 | #include <linux/module.h> | |
18aecc2b CM |
18 | /* EXPORT_SYMBOL() is in arch/tile/lib/exports.c since this should be asm. */ |
19 | ||
20 | /* Must be 8 bytes in size. */ | |
c53c70a9 | 21 | #define op_t uint64_t |
18aecc2b | 22 | |
c53c70a9 CM |
23 | /* Threshold value for when to enter the unrolled loops. */ |
24 | #define OP_T_THRES 16 | |
25 | ||
26 | #if CHIP_L2_LINE_SIZE() != 64 | |
27 | #error "Assumes 64 byte line size" | |
18aecc2b CM |
28 | #endif |
29 | ||
30 | /* How many cache lines ahead should we prefetch? */ | |
c53c70a9 | 31 | #define PREFETCH_LINES_AHEAD 4 |
18aecc2b CM |
32 | |
33 | /* | |
34 | * Provide "base versions" of load and store for the normal code path. | |
35 | * The kernel provides other versions for userspace copies. | |
36 | */ | |
37 | #define ST(p, v) (*(p) = (v)) | |
38 | #define LD(p) (*(p)) | |
39 | ||
40 | #ifndef USERCOPY_FUNC | |
41 | #define ST1 ST | |
42 | #define ST2 ST | |
43 | #define ST4 ST | |
44 | #define ST8 ST | |
45 | #define LD1 LD | |
46 | #define LD2 LD | |
47 | #define LD4 LD | |
48 | #define LD8 LD | |
49 | #define RETVAL dstv | |
50 | void *memcpy(void *__restrict dstv, const void *__restrict srcv, size_t n) | |
51 | #else | |
52 | /* | |
53 | * Special kernel version will provide implementation of the LDn/STn | |
54 | * macros to return a count of uncopied bytes due to mm fault. | |
55 | */ | |
56 | #define RETVAL 0 | |
57 | int USERCOPY_FUNC(void *__restrict dstv, const void *__restrict srcv, size_t n) | |
58 | #endif | |
59 | { | |
60 | char *__restrict dst1 = (char *)dstv; | |
61 | const char *__restrict src1 = (const char *)srcv; | |
62 | const char *__restrict src1_end; | |
63 | const char *__restrict prefetch; | |
c53c70a9 CM |
64 | op_t *__restrict dst8; /* 8-byte pointer to destination memory. */ |
65 | op_t final; /* Final bytes to write to trailing word, if any */ | |
18aecc2b CM |
66 | long i; |
67 | ||
68 | if (n < 16) { | |
69 | for (; n; n--) | |
70 | ST1(dst1++, LD1(src1++)); | |
71 | return RETVAL; | |
72 | } | |
73 | ||
74 | /* | |
75 | * Locate the end of source memory we will copy. Don't | |
76 | * prefetch past this. | |
77 | */ | |
78 | src1_end = src1 + n - 1; | |
79 | ||
80 | /* Prefetch ahead a few cache lines, but not past the end. */ | |
81 | prefetch = src1; | |
82 | for (i = 0; i < PREFETCH_LINES_AHEAD; i++) { | |
83 | __insn_prefetch(prefetch); | |
84 | prefetch += CHIP_L2_LINE_SIZE(); | |
c53c70a9 | 85 | prefetch = (prefetch < src1_end) ? prefetch : src1; |
18aecc2b CM |
86 | } |
87 | ||
88 | /* Copy bytes until dst is word-aligned. */ | |
c53c70a9 | 89 | for (; (uintptr_t)dst1 & (sizeof(op_t) - 1); n--) |
18aecc2b CM |
90 | ST1(dst1++, LD1(src1++)); |
91 | ||
92 | /* 8-byte pointer to destination memory. */ | |
c53c70a9 CM |
93 | dst8 = (op_t *)dst1; |
94 | ||
95 | if (__builtin_expect((uintptr_t)src1 & (sizeof(op_t) - 1), 0)) { | |
96 | /* Unaligned copy. */ | |
97 | ||
98 | op_t tmp0 = 0, tmp1 = 0, tmp2, tmp3; | |
99 | const op_t *src8 = (const op_t *) ((uintptr_t)src1 & | |
100 | -sizeof(op_t)); | |
101 | const void *srci = (void *)src1; | |
102 | int m; | |
103 | ||
104 | m = (CHIP_L2_LINE_SIZE() << 2) - | |
105 | (((uintptr_t)dst8) & ((CHIP_L2_LINE_SIZE() << 2) - 1)); | |
106 | m = (n < m) ? n : m; | |
107 | m /= sizeof(op_t); | |
108 | ||
109 | /* Copy until 'dst' is cache-line-aligned. */ | |
110 | n -= (sizeof(op_t) * m); | |
111 | ||
112 | switch (m % 4) { | |
113 | case 0: | |
114 | if (__builtin_expect(!m, 0)) | |
115 | goto _M0; | |
116 | tmp1 = LD8(src8++); | |
117 | tmp2 = LD8(src8++); | |
118 | goto _8B3; | |
119 | case 2: | |
120 | m += 2; | |
121 | tmp3 = LD8(src8++); | |
122 | tmp0 = LD8(src8++); | |
123 | goto _8B1; | |
124 | case 3: | |
125 | m += 1; | |
126 | tmp2 = LD8(src8++); | |
127 | tmp3 = LD8(src8++); | |
128 | goto _8B2; | |
129 | case 1: | |
130 | m--; | |
131 | tmp0 = LD8(src8++); | |
132 | tmp1 = LD8(src8++); | |
133 | if (__builtin_expect(!m, 0)) | |
134 | goto _8B0; | |
135 | } | |
136 | ||
137 | do { | |
138 | tmp2 = LD8(src8++); | |
139 | tmp0 = __insn_dblalign(tmp0, tmp1, srci); | |
140 | ST8(dst8++, tmp0); | |
141 | _8B3: | |
142 | tmp3 = LD8(src8++); | |
143 | tmp1 = __insn_dblalign(tmp1, tmp2, srci); | |
144 | ST8(dst8++, tmp1); | |
145 | _8B2: | |
146 | tmp0 = LD8(src8++); | |
147 | tmp2 = __insn_dblalign(tmp2, tmp3, srci); | |
148 | ST8(dst8++, tmp2); | |
149 | _8B1: | |
150 | tmp1 = LD8(src8++); | |
151 | tmp3 = __insn_dblalign(tmp3, tmp0, srci); | |
152 | ST8(dst8++, tmp3); | |
153 | m -= 4; | |
154 | } while (m); | |
155 | ||
156 | _8B0: | |
157 | tmp0 = __insn_dblalign(tmp0, tmp1, srci); | |
158 | ST8(dst8++, tmp0); | |
159 | src8--; | |
160 | ||
161 | _M0: | |
162 | if (__builtin_expect(n >= CHIP_L2_LINE_SIZE(), 0)) { | |
163 | op_t tmp4, tmp5, tmp6, tmp7, tmp8; | |
164 | ||
165 | prefetch = ((const char *)src8) + | |
166 | CHIP_L2_LINE_SIZE() * PREFETCH_LINES_AHEAD; | |
167 | ||
168 | for (tmp0 = LD8(src8++); n >= CHIP_L2_LINE_SIZE(); | |
169 | n -= CHIP_L2_LINE_SIZE()) { | |
170 | /* Prefetch and advance to next line to | |
171 | prefetch, but don't go past the end. */ | |
172 | __insn_prefetch(prefetch); | |
173 | ||
174 | /* Make sure prefetch got scheduled | |
175 | earlier. */ | |
176 | __asm__ ("" : : : "memory"); | |
177 | ||
178 | prefetch += CHIP_L2_LINE_SIZE(); | |
179 | prefetch = (prefetch < src1_end) ? prefetch : | |
180 | (const char *) src8; | |
181 | ||
182 | tmp1 = LD8(src8++); | |
183 | tmp2 = LD8(src8++); | |
184 | tmp3 = LD8(src8++); | |
185 | tmp4 = LD8(src8++); | |
186 | tmp5 = LD8(src8++); | |
187 | tmp6 = LD8(src8++); | |
188 | tmp7 = LD8(src8++); | |
189 | tmp8 = LD8(src8++); | |
190 | ||
191 | tmp0 = __insn_dblalign(tmp0, tmp1, srci); | |
192 | tmp1 = __insn_dblalign(tmp1, tmp2, srci); | |
193 | tmp2 = __insn_dblalign(tmp2, tmp3, srci); | |
194 | tmp3 = __insn_dblalign(tmp3, tmp4, srci); | |
195 | tmp4 = __insn_dblalign(tmp4, tmp5, srci); | |
196 | tmp5 = __insn_dblalign(tmp5, tmp6, srci); | |
197 | tmp6 = __insn_dblalign(tmp6, tmp7, srci); | |
198 | tmp7 = __insn_dblalign(tmp7, tmp8, srci); | |
199 | ||
200 | __insn_wh64(dst8); | |
201 | ||
202 | ST8(dst8++, tmp0); | |
203 | ST8(dst8++, tmp1); | |
204 | ST8(dst8++, tmp2); | |
205 | ST8(dst8++, tmp3); | |
206 | ST8(dst8++, tmp4); | |
207 | ST8(dst8++, tmp5); | |
208 | ST8(dst8++, tmp6); | |
209 | ST8(dst8++, tmp7); | |
210 | ||
211 | tmp0 = tmp8; | |
212 | } | |
213 | src8--; | |
214 | } | |
215 | ||
216 | /* Copy the rest 8-byte chunks. */ | |
217 | if (n >= sizeof(op_t)) { | |
218 | tmp0 = LD8(src8++); | |
219 | for (; n >= sizeof(op_t); n -= sizeof(op_t)) { | |
220 | tmp1 = LD8(src8++); | |
221 | tmp0 = __insn_dblalign(tmp0, tmp1, srci); | |
222 | ST8(dst8++, tmp0); | |
223 | tmp0 = tmp1; | |
224 | } | |
225 | src8--; | |
18aecc2b CM |
226 | } |
227 | ||
228 | if (n == 0) | |
229 | return RETVAL; | |
230 | ||
c53c70a9 CM |
231 | tmp0 = LD8(src8++); |
232 | tmp1 = ((const char *)src8 <= src1_end) | |
233 | ? LD8((op_t *)src8) : 0; | |
234 | final = __insn_dblalign(tmp0, tmp1, srci); | |
18aecc2b | 235 | |
18aecc2b CM |
236 | } else { |
237 | /* Aligned copy. */ | |
238 | ||
c53c70a9 | 239 | const op_t *__restrict src8 = (const op_t *)src1; |
18aecc2b CM |
240 | |
241 | /* src8 and dst8 are both word-aligned. */ | |
242 | if (n >= CHIP_L2_LINE_SIZE()) { | |
243 | /* Copy until 'dst' is cache-line-aligned. */ | |
244 | for (; (uintptr_t)dst8 & (CHIP_L2_LINE_SIZE() - 1); | |
c53c70a9 | 245 | n -= sizeof(op_t)) |
18aecc2b CM |
246 | ST8(dst8++, LD8(src8++)); |
247 | ||
248 | for (; n >= CHIP_L2_LINE_SIZE(); ) { | |
c53c70a9 CM |
249 | op_t tmp0, tmp1, tmp2, tmp3; |
250 | op_t tmp4, tmp5, tmp6, tmp7; | |
18aecc2b CM |
251 | |
252 | /* | |
253 | * Prefetch and advance to next line | |
c53c70a9 CM |
254 | * to prefetch, but don't go past the |
255 | * end. | |
18aecc2b CM |
256 | */ |
257 | __insn_prefetch(prefetch); | |
c53c70a9 CM |
258 | |
259 | /* Make sure prefetch got scheduled | |
260 | earlier. */ | |
261 | __asm__ ("" : : : "memory"); | |
262 | ||
18aecc2b | 263 | prefetch += CHIP_L2_LINE_SIZE(); |
c53c70a9 | 264 | prefetch = (prefetch < src1_end) ? prefetch : |
18aecc2b CM |
265 | (const char *)src8; |
266 | ||
267 | /* | |
c53c70a9 CM |
268 | * Do all the loads before wh64. This |
269 | * is necessary if [src8, src8+7] and | |
270 | * [dst8, dst8+7] share the same cache | |
271 | * line and dst8 <= src8, as can be | |
272 | * the case when called from memmove, | |
273 | * or with code tested on x86 whose | |
274 | * memcpy always works with forward | |
275 | * copies. | |
18aecc2b | 276 | */ |
c53c70a9 CM |
277 | tmp0 = LD8(src8++); |
278 | tmp1 = LD8(src8++); | |
279 | tmp2 = LD8(src8++); | |
280 | tmp3 = LD8(src8++); | |
281 | tmp4 = LD8(src8++); | |
282 | tmp5 = LD8(src8++); | |
283 | tmp6 = LD8(src8++); | |
284 | tmp7 = LD8(src8++); | |
285 | ||
286 | /* wh64 and wait for tmp7 load completion. */ | |
287 | __asm__ ("move %0, %0; wh64 %1\n" | |
288 | : : "r"(tmp7), "r"(dst8)); | |
18aecc2b | 289 | |
c53c70a9 CM |
290 | ST8(dst8++, tmp0); |
291 | ST8(dst8++, tmp1); | |
292 | ST8(dst8++, tmp2); | |
293 | ST8(dst8++, tmp3); | |
294 | ST8(dst8++, tmp4); | |
295 | ST8(dst8++, tmp5); | |
296 | ST8(dst8++, tmp6); | |
297 | ST8(dst8++, tmp7); | |
298 | ||
299 | n -= CHIP_L2_LINE_SIZE(); | |
18aecc2b | 300 | } |
c53c70a9 CM |
301 | #if CHIP_L2_LINE_SIZE() != 64 |
302 | # error "Fix code that assumes particular L2 cache line size." | |
303 | #endif | |
18aecc2b CM |
304 | } |
305 | ||
c53c70a9 | 306 | for (; n >= sizeof(op_t); n -= sizeof(op_t)) |
18aecc2b CM |
307 | ST8(dst8++, LD8(src8++)); |
308 | ||
309 | if (__builtin_expect(n == 0, 1)) | |
310 | return RETVAL; | |
311 | ||
312 | final = LD8(src8); | |
313 | } | |
314 | ||
315 | /* n != 0 if we get here. Write out any trailing bytes. */ | |
316 | dst1 = (char *)dst8; | |
1efea40d | 317 | #ifndef __BIG_ENDIAN__ |
18aecc2b CM |
318 | if (n & 4) { |
319 | ST4((uint32_t *)dst1, final); | |
320 | dst1 += 4; | |
321 | final >>= 32; | |
322 | n &= 3; | |
323 | } | |
324 | if (n & 2) { | |
325 | ST2((uint16_t *)dst1, final); | |
326 | dst1 += 2; | |
327 | final >>= 16; | |
328 | n &= 1; | |
329 | } | |
330 | if (n) | |
331 | ST1((uint8_t *)dst1, final); | |
1efea40d CM |
332 | #else |
333 | if (n & 4) { | |
334 | ST4((uint32_t *)dst1, final >> 32); | |
335 | dst1 += 4; | |
336 | } | |
337 | else | |
338 | { | |
339 | final >>= 32; | |
340 | } | |
341 | if (n & 2) { | |
342 | ST2((uint16_t *)dst1, final >> 16); | |
343 | dst1 += 2; | |
344 | } | |
345 | else | |
346 | { | |
347 | final >>= 16; | |
348 | } | |
349 | if (n & 1) | |
350 | ST1((uint8_t *)dst1, final >> 8); | |
351 | #endif | |
18aecc2b CM |
352 | |
353 | return RETVAL; | |
354 | } | |
355 | ||
18aecc2b CM |
356 | #ifdef USERCOPY_FUNC |
357 | #undef ST1 | |
358 | #undef ST2 | |
359 | #undef ST4 | |
360 | #undef ST8 | |
361 | #undef LD1 | |
362 | #undef LD2 | |
363 | #undef LD4 | |
364 | #undef LD8 | |
365 | #undef USERCOPY_FUNC | |
366 | #endif |