/*1* Copyright 2010 Tilera Corporation. All Rights Reserved.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation, version 2.6*7* This program is distributed in the hope that it will be useful, but8* WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or10* NON INFRINGEMENT. See the GNU General Public License for11* more details.12*/1314#include <arch/chip.h>1516#include <linux/types.h>17#include <linux/string.h>18#include <linux/module.h>1920#undef memset2122void *memset(void *s, int c, size_t n)23{24uint32_t *out32;25int n32;26uint32_t v16, v32;27uint8_t *out8 = s;28#if !CHIP_HAS_WH64()29int ahead32;30#else31int to_align32;32#endif3334/* Experimentation shows that a trivial tight loop is a win up until35* around a size of 20, where writing a word at a time starts to win.36*/37#define BYTE_CUTOFF 203839#if BYTE_CUTOFF < 340/* This must be at least at least this big, or some code later41* on doesn't work.42*/43#error "BYTE_CUTOFF is too small"44#endif4546if (n < BYTE_CUTOFF) {47/* Strangely, this turns out to be the tightest way to48* write this loop.49*/50if (n != 0) {51do {52/* Strangely, combining these into one line53* performs worse.54*/55*out8 = c;56out8++;57} while (--n != 0);58}5960return s;61}6263#if !CHIP_HAS_WH64()64/* Use a spare issue slot to start prefetching the first cache65* line early. This instruction is free as the store can be buried66* in otherwise idle issue slots doing ALU ops.67*/68__insn_prefetch(out8);6970/* We prefetch the end so that a short memset that spans two cache71* lines gets some prefetching benefit. Again we believe this is free72* to issue.73*/74__insn_prefetch(&out8[n - 1]);75#endif /* !CHIP_HAS_WH64() */767778/* Align 'out8'. We know n >= 3 so this won't write past the end. */79while (((uintptr_t) out8 & 3) != 0) {80*out8++ = c;81--n;82}8384/* Align 'n'. */85while (n & 3)86out8[--n] = c;8788out32 = (uint32_t *) out8;89n32 = n >> 2;9091/* Tile input byte out to 32 bits. */92v16 = __insn_intlb(c, c);93v32 = __insn_intlh(v16, v16);9495/* 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)9798#if !CHIP_HAS_WH64()99100ahead32 = CACHE_LINE_SIZE_IN_WORDS;101102/* We already prefetched the first and last cache lines, so103* we only need to do more prefetching if we are storing104* to more than two cache lines.105*/106if (n32 > CACHE_LINE_SIZE_IN_WORDS * 2) {107int i;108109/* Prefetch the next several cache lines.110* This is the setup code for the software-pipelined111* loop below.112*/113#define MAX_PREFETCH 5114ahead32 = n32 & -CACHE_LINE_SIZE_IN_WORDS;115if (ahead32 > MAX_PREFETCH * CACHE_LINE_SIZE_IN_WORDS)116ahead32 = MAX_PREFETCH * CACHE_LINE_SIZE_IN_WORDS;117118for (i = CACHE_LINE_SIZE_IN_WORDS;119i < ahead32; i += CACHE_LINE_SIZE_IN_WORDS)120__insn_prefetch(&out32[i]);121}122123if (n32 > ahead32) {124while (1) {125int j;126127/* Prefetch by reading one word several cache lines128* ahead. Since loads are non-blocking this will129* cause the full cache line to be read while we are130* finishing earlier cache lines. Using a store131* here causes microarchitectural performance132* problems where a victimizing store miss goes to133* the head of the retry FIFO and locks the pipe for134* a few cycles. So a few subsequent stores in this135* loop go into the retry FIFO, and then later136* stores see other stores to the same cache line137* are already in the retry FIFO and themselves go138* into the retry FIFO, filling it up and grinding139* to a halt waiting for the original miss to be140* satisfied.141*/142__insn_prefetch(&out32[ahead32]);143144#if CACHE_LINE_SIZE_IN_WORDS % 4 != 0145#error "Unhandled CACHE_LINE_SIZE_IN_WORDS"146#endif147148n32 -= CACHE_LINE_SIZE_IN_WORDS;149150/* Save icache space by only partially unrolling151* this loop.152*/153for (j = CACHE_LINE_SIZE_IN_WORDS / 4; j > 0; j--) {154*out32++ = v32;155*out32++ = v32;156*out32++ = v32;157*out32++ = v32;158}159160/* To save compiled code size, reuse this loop even161* when we run out of prefetching to do by dropping162* ahead32 down.163*/164if (n32 <= ahead32) {165/* Not even a full cache line left,166* so stop now.167*/168if (n32 < CACHE_LINE_SIZE_IN_WORDS)169break;170171/* Choose a small enough value that we don't172* prefetch past the end. There's no sense173* in touching cache lines we don't have to.174*/175ahead32 = CACHE_LINE_SIZE_IN_WORDS - 1;176}177}178}179180#else /* CHIP_HAS_WH64() */181182/* Determine how many words we need to emit before the 'out32'183* pointer becomes aligned modulo the cache line size.184*/185to_align32 =186(-((uintptr_t)out32 >> 2)) & (CACHE_LINE_SIZE_IN_WORDS - 1);187188/* Only bother aligning and using wh64 if there is at least189* one full cache line to process. This check also prevents190* overrunning the end of the buffer with alignment words.191*/192if (to_align32 <= n32 - CACHE_LINE_SIZE_IN_WORDS) {193int lines_left;194195/* Align out32 mod the cache line size so we can use wh64. */196n32 -= to_align32;197for (; to_align32 != 0; to_align32--) {198*out32 = v32;199out32++;200}201202/* Use unsigned divide to turn this into a right shift. */203lines_left = (unsigned)n32 / CACHE_LINE_SIZE_IN_WORDS;204205do {206/* Only wh64 a few lines at a time, so we don't207* exceed the maximum number of victim lines.208*/209int x = ((lines_left < CHIP_MAX_OUTSTANDING_VICTIMS())210? lines_left211: CHIP_MAX_OUTSTANDING_VICTIMS());212uint32_t *wh = out32;213int i = x;214int j;215216lines_left -= x;217218do {219__insn_wh64(wh);220wh += CACHE_LINE_SIZE_IN_WORDS;221} while (--i);222223for (j = x * (CACHE_LINE_SIZE_IN_WORDS / 4);224j != 0; j--) {225*out32++ = v32;226*out32++ = v32;227*out32++ = v32;228*out32++ = v32;229}230} while (lines_left != 0);231232/* We processed all full lines above, so only this many233* words remain to be processed.234*/235n32 &= CACHE_LINE_SIZE_IN_WORDS - 1;236}237238#endif /* CHIP_HAS_WH64() */239240/* Now handle any leftover values. */241if (n32 != 0) {242do {243*out32 = v32;244out32++;245} while (--n32 != 0);246}247248return s;249}250EXPORT_SYMBOL(memset);251252253