/*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 <asm/page.h>15#include <asm/cacheflush.h>16#include <arch/icache.h>17#include <arch/spr_def.h>181920void __flush_icache_range(unsigned long start, unsigned long end)21{22invalidate_icache((const void *)start, end - start, PAGE_SIZE);23}242526/* Force a load instruction to issue. */27static inline void force_load(char *p)28{29*(volatile char *)p;30}3132/*33* Flush and invalidate a VA range that is homed remotely on a single34* core (if "!hfh") or homed via hash-for-home (if "hfh"), waiting35* until the memory controller holds the flushed values.36*/37void finv_buffer_remote(void *buffer, size_t size, int hfh)38{39char *p, *base;40size_t step_size, load_count;41const unsigned long STRIPE_WIDTH = 8192;42#ifdef __tilegx__43/*44* On TILE-Gx, we must disable the dstream prefetcher before doing45* a cache flush; otherwise, we could end up with data in the cache46* that we don't want there. Note that normally we'd do an mf47* after the SPR write to disabling the prefetcher, but we do one48* below, before any further loads, so there's no need to do it49* here.50*/51uint_reg_t old_dstream_pf = __insn_mfspr(SPR_DSTREAM_PF);52__insn_mtspr(SPR_DSTREAM_PF, 0);53#endif5455/*56* Flush and invalidate the buffer out of the local L1/L257* and request the home cache to flush and invalidate as well.58*/59__finv_buffer(buffer, size);6061/*62* Wait for the home cache to acknowledge that it has processed63* all the flush-and-invalidate requests. This does not mean64* that the flushed data has reached the memory controller yet,65* but it does mean the home cache is processing the flushes.66*/67__insn_mf();6869/*70* Issue a load to the last cache line, which can't complete71* until all the previously-issued flushes to the same memory72* controller have also completed. If we weren't striping73* memory, that one load would be sufficient, but since we may74* be, we also need to back up to the last load issued to75* another memory controller, which would be the point where76* we crossed an 8KB boundary (the granularity of striping77* across memory controllers). Keep backing up and doing this78* until we are before the beginning of the buffer, or have79* hit all the controllers.80*81* If we are flushing a hash-for-home buffer, it's even worse.82* Each line may be homed on a different tile, and each tile83* may have up to four lines that are on different84* controllers. So as we walk backwards, we have to touch85* enough cache lines to satisfy these constraints. In86* practice this ends up being close enough to "load from87* every cache line on a full memory stripe on each88* controller" that we simply do that, to simplify the logic.89*90* FIXME: See bug 9535 for some issues with this code.91*/92if (hfh) {93step_size = L2_CACHE_BYTES;94load_count = (STRIPE_WIDTH / L2_CACHE_BYTES) *95(1 << CHIP_LOG_NUM_MSHIMS());96} else {97step_size = STRIPE_WIDTH;98load_count = (1 << CHIP_LOG_NUM_MSHIMS());99}100101/* Load the last byte of the buffer. */102p = (char *)buffer + size - 1;103force_load(p);104105/* Bump down to the end of the previous stripe or cache line. */106p -= step_size;107p = (char *)((unsigned long)p | (step_size - 1));108109/* Figure out how far back we need to go. */110base = p - (step_size * (load_count - 2));111if ((long)base < (long)buffer)112base = buffer;113114/*115* Fire all the loads we need. The MAF only has eight entries116* so we can have at most eight outstanding loads, so we117* unroll by that amount.118*/119#pragma unroll 8120for (; p >= base; p -= step_size)121force_load(p);122123/*124* Repeat, but with inv's instead of loads, to get rid of the125* data we just loaded into our own cache and the old home L3.126* No need to unroll since inv's don't target a register.127*/128p = (char *)buffer + size - 1;129__insn_inv(p);130p -= step_size;131p = (char *)((unsigned long)p | (step_size - 1));132for (; p >= base; p -= step_size)133__insn_inv(p);134135/* Wait for the load+inv's (and thus finvs) to have completed. */136__insn_mf();137138#ifdef __tilegx__139/* Reenable the prefetcher. */140__insn_mtspr(SPR_DSTREAM_PF, old_dstream_pf);141#endif142}143144145