arch/tile: Various cleanups.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / tile / lib / memset_32.c
blobd014c1fbcbc28816af4925b72d121c1d201edf0a
1 /*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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.
15 #include <arch/chip.h>
17 #include <linux/types.h>
18 #include <linux/string.h>
19 #include <linux/module.h>
22 void *memset(void *s, int c, size_t n)
24 uint32_t *out32;
25 int n32;
26 uint32_t v16, v32;
27 uint8_t *out8 = s;
28 #if !CHIP_HAS_WH64()
29 int ahead32;
30 #else
31 int to_align32;
32 #endif
34 /* Experimentation shows that a trivial tight loop is a win up until
35 * around a size of 20, where writing a word at a time starts to win.
37 #define BYTE_CUTOFF 20
39 #if BYTE_CUTOFF < 3
40 /* This must be at least at least this big, or some code later
41 * on doesn't work.
43 #error "BYTE_CUTOFF is too small"
44 #endif
46 if (n < BYTE_CUTOFF) {
47 /* Strangely, this turns out to be the tightest way to
48 * write this loop.
50 if (n != 0) {
51 do {
52 /* Strangely, combining these into one line
53 * performs worse.
55 *out8 = c;
56 out8++;
57 } while (--n != 0);
60 return s;
63 #if !CHIP_HAS_WH64()
64 /* Use a spare issue slot to start prefetching the first cache
65 * line early. This instruction is free as the store can be buried
66 * in otherwise idle issue slots doing ALU ops.
68 __insn_prefetch(out8);
70 /* We prefetch the end so that a short memset that spans two cache
71 * lines gets some prefetching benefit. Again we believe this is free
72 * to issue.
74 __insn_prefetch(&out8[n - 1]);
75 #endif /* !CHIP_HAS_WH64() */
78 /* Align 'out8'. We know n >= 3 so this won't write past the end. */
79 while (((uintptr_t) out8 & 3) != 0) {
80 *out8++ = c;
81 --n;
84 /* Align 'n'. */
85 while (n & 3)
86 out8[--n] = c;
88 out32 = (uint32_t *) out8;
89 n32 = n >> 2;
91 /* Tile input byte out to 32 bits. */
92 v16 = __insn_intlb(c, c);
93 v32 = __insn_intlh(v16, v16);
95 /* This must be at least 8 or the following loop doesn't work. */
96 #define CACHE_LINE_SIZE_IN_WORDS (CHIP_L2_LINE_SIZE() / 4)
98 #if !CHIP_HAS_WH64()
100 ahead32 = CACHE_LINE_SIZE_IN_WORDS;
102 /* We already prefetched the first and last cache lines, so
103 * we only need to do more prefetching if we are storing
104 * to more than two cache lines.
106 if (n32 > CACHE_LINE_SIZE_IN_WORDS * 2) {
107 int i;
109 /* Prefetch the next several cache lines.
110 * This is the setup code for the software-pipelined
111 * loop below.
113 #define MAX_PREFETCH 5
114 ahead32 = n32 & -CACHE_LINE_SIZE_IN_WORDS;
115 if (ahead32 > MAX_PREFETCH * CACHE_LINE_SIZE_IN_WORDS)
116 ahead32 = MAX_PREFETCH * CACHE_LINE_SIZE_IN_WORDS;
118 for (i = CACHE_LINE_SIZE_IN_WORDS;
119 i < ahead32; i += CACHE_LINE_SIZE_IN_WORDS)
120 __insn_prefetch(&out32[i]);
123 if (n32 > ahead32) {
124 while (1) {
125 int j;
127 /* Prefetch by reading one word several cache lines
128 * ahead. Since loads are non-blocking this will
129 * cause the full cache line to be read while we are
130 * finishing earlier cache lines. Using a store
131 * here causes microarchitectural performance
132 * problems where a victimizing store miss goes to
133 * the head of the retry FIFO and locks the pipe for
134 * a few cycles. So a few subsequent stores in this
135 * loop go into the retry FIFO, and then later
136 * stores see other stores to the same cache line
137 * are already in the retry FIFO and themselves go
138 * into the retry FIFO, filling it up and grinding
139 * to a halt waiting for the original miss to be
140 * satisfied.
142 __insn_prefetch(&out32[ahead32]);
144 #if CACHE_LINE_SIZE_IN_WORDS % 4 != 0
145 #error "Unhandled CACHE_LINE_SIZE_IN_WORDS"
146 #endif
148 n32 -= CACHE_LINE_SIZE_IN_WORDS;
150 /* Save icache space by only partially unrolling
151 * this loop.
153 for (j = CACHE_LINE_SIZE_IN_WORDS / 4; j > 0; j--) {
154 *out32++ = v32;
155 *out32++ = v32;
156 *out32++ = v32;
157 *out32++ = v32;
160 /* To save compiled code size, reuse this loop even
161 * when we run out of prefetching to do by dropping
162 * ahead32 down.
164 if (n32 <= ahead32) {
165 /* Not even a full cache line left,
166 * so stop now.
168 if (n32 < CACHE_LINE_SIZE_IN_WORDS)
169 break;
171 /* Choose a small enough value that we don't
172 * prefetch past the end. There's no sense
173 * in touching cache lines we don't have to.
175 ahead32 = CACHE_LINE_SIZE_IN_WORDS - 1;
180 #else /* CHIP_HAS_WH64() */
182 /* Determine how many words we need to emit before the 'out32'
183 * pointer becomes aligned modulo the cache line size.
185 to_align32 =
186 (-((uintptr_t)out32 >> 2)) & (CACHE_LINE_SIZE_IN_WORDS - 1);
188 /* Only bother aligning and using wh64 if there is at least
189 * one full cache line to process. This check also prevents
190 * overrunning the end of the buffer with alignment words.
192 if (to_align32 <= n32 - CACHE_LINE_SIZE_IN_WORDS) {
193 int lines_left;
195 /* Align out32 mod the cache line size so we can use wh64. */
196 n32 -= to_align32;
197 for (; to_align32 != 0; to_align32--) {
198 *out32 = v32;
199 out32++;
202 /* Use unsigned divide to turn this into a right shift. */
203 lines_left = (unsigned)n32 / CACHE_LINE_SIZE_IN_WORDS;
205 do {
206 /* Only wh64 a few lines at a time, so we don't
207 * exceed the maximum number of victim lines.
209 int x = ((lines_left < CHIP_MAX_OUTSTANDING_VICTIMS())
210 ? lines_left
211 : CHIP_MAX_OUTSTANDING_VICTIMS());
212 uint32_t *wh = out32;
213 int i = x;
214 int j;
216 lines_left -= x;
218 do {
219 __insn_wh64(wh);
220 wh += CACHE_LINE_SIZE_IN_WORDS;
221 } while (--i);
223 for (j = x * (CACHE_LINE_SIZE_IN_WORDS / 4);
224 j != 0; j--) {
225 *out32++ = v32;
226 *out32++ = v32;
227 *out32++ = v32;
228 *out32++ = v32;
230 } while (lines_left != 0);
232 /* We processed all full lines above, so only this many
233 * words remain to be processed.
235 n32 &= CACHE_LINE_SIZE_IN_WORDS - 1;
238 #endif /* CHIP_HAS_WH64() */
240 /* Now handle any leftover values. */
241 if (n32 != 0) {
242 do {
243 *out32 = v32;
244 out32++;
245 } while (--n32 != 0);
248 return s;
250 EXPORT_SYMBOL(memset);