sh: Fix dcache flushing for N-way write-through caches.
[deliverable/linux.git] / arch / sh / mm / cache-sh4.c
index 5cfe08dbb59ed0997c4451459577423b8713e817..b36a9c986a58d065915600e8672236ed8bf950e6 100644 (file)
 #define MAX_DCACHE_PAGES       64      /* XXX: Tune for ways */
 #define MAX_ICACHE_PAGES       32
 
+static void __flush_dcache_segment_writethrough(unsigned long start,
+                                               unsigned long extent);
 static void __flush_dcache_segment_1way(unsigned long start,
                                        unsigned long extent);
 static void __flush_dcache_segment_2way(unsigned long start,
                                        unsigned long extent);
 static void __flush_dcache_segment_4way(unsigned long start,
                                        unsigned long extent);
-
 static void __flush_cache_4096(unsigned long addr, unsigned long phys,
                               unsigned long exec_offset);
 
@@ -95,10 +96,17 @@ static void __init emit_cache_params(void)
  */
 void __init p3_cache_init(void)
 {
+       unsigned int wt_enabled = !!(__raw_readl(CCR) & CCR_CACHE_WT);
+
        compute_alias(&boot_cpu_data.icache);
        compute_alias(&boot_cpu_data.dcache);
        compute_alias(&boot_cpu_data.scache);
 
+       if (wt_enabled) {
+               __flush_dcache_segment_fn = __flush_dcache_segment_writethrough;
+               goto out;
+       }
+
        switch (boot_cpu_data.dcache.ways) {
        case 1:
                __flush_dcache_segment_fn = __flush_dcache_segment_1way;
@@ -114,6 +122,7 @@ void __init p3_cache_init(void)
                break;
        }
 
+out:
        emit_cache_params();
 }
 
@@ -581,7 +590,49 @@ static void __flush_cache_4096(unsigned long addr, unsigned long phys,
  * Break the 1, 2 and 4 way variants of this out into separate functions to
  * avoid nearly all the overhead of having the conditional stuff in the function
  * bodies (+ the 1 and 2 way cases avoid saving any registers too).
+ *
+ * We want to eliminate unnecessary bus transactions, so this code uses
+ * a non-obvious technique.
+ *
+ * Loop over a cache way sized block of, one cache line at a time. For each
+ * line, use movca.a to cause the current cache line contents to be written
+ * back, but without reading anything from main memory. However this has the
+ * side effect that the cache is now caching that memory location. So follow
+ * this with a cache invalidate to mark the cache line invalid. And do all
+ * this with interrupts disabled, to avoid the cache line being accidently
+ * evicted while it is holding garbage.
+ *
+ * This also breaks in a number of circumstances:
+ * - if there are modifications to the region of memory just above
+ *   empty_zero_page (for example because a breakpoint has been placed
+ *   there), then these can be lost.
+ *
+ *   This is because the the memory address which the cache temporarily
+ *   caches in the above description is empty_zero_page. So the
+ *   movca.l hits the cache (it is assumed that it misses, or at least
+ *   isn't dirty), modifies the line and then invalidates it, losing the
+ *   required change.
+ *
+ * - If caches are disabled or configured in write-through mode, then
+ *   the movca.l writes garbage directly into memory.
  */
+static void __flush_dcache_segment_writethrough(unsigned long start,
+                                               unsigned long extent_per_way)
+{
+       unsigned long addr;
+       int i;
+
+       addr = CACHE_OC_ADDRESS_ARRAY | (start & cpu_data->dcache.entry_mask);
+
+       while (extent_per_way) {
+               for (i = 0; i < cpu_data->dcache.ways; i++)
+                       __raw_writel(0, addr + cpu_data->dcache.way_incr * i);
+
+               addr += cpu_data->dcache.linesz;
+               extent_per_way -= cpu_data->dcache.linesz;
+       }
+}
+
 static void __flush_dcache_segment_1way(unsigned long start,
                                        unsigned long extent_per_way)
 {
This page took 0.025595 seconds and 5 git commands to generate.