2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2005-2007 Cavium Networks
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
12 #include <linux/bitops.h>
13 #include <linux/cpu.h>
16 #include <asm/bcache.h>
17 #include <asm/bootinfo.h>
18 #include <asm/cacheops.h>
19 #include <asm/cpu-features.h>
21 #include <asm/pgtable.h>
22 #include <asm/r4kcache.h>
23 #include <asm/system.h>
24 #include <asm/mmu_context.h>
27 #include <asm/octeon/octeon.h>
29 unsigned long long cache_err_dcache[NR_CPUS];
32 * Octeon automatically flushes the dcache on tlb changes, so
33 * from Linux's viewpoint it acts much like a physically
34 * tagged cache. No flushing is needed
37 static void octeon_flush_data_cache_page(unsigned long addr)
42 static inline void octeon_local_flush_icache(void)
44 asm volatile ("synci 0($0)");
48 * Flush local I-cache for the specified range.
50 static void local_octeon_flush_icache_range(unsigned long start,
53 octeon_local_flush_icache();
57 * Flush caches as necessary for all cores affected by a
58 * vma. If no vma is supplied, all cores are flushed.
60 * @vma: VMA to flush or NULL to flush all icaches.
62 static void octeon_flush_icache_all_cores(struct vm_area_struct *vma)
64 extern void octeon_send_ipi_single(int cpu, unsigned int action);
71 octeon_local_flush_icache();
74 cpu = smp_processor_id();
77 * If we have a vma structure, we only need to worry about
78 * cores it has been used on
81 mask = vma->vm_mm->cpu_vm_mask;
83 mask = cpu_online_map;
85 for_each_cpu_mask(cpu, mask)
86 octeon_send_ipi_single(cpu, SMP_ICACHE_FLUSH);
94 * Called to flush the icache on all cores
96 static void octeon_flush_icache_all(void)
98 octeon_flush_icache_all_cores(NULL);
103 * Called to flush all memory associated with a memory
106 * @mm: Memory context to flush
108 static void octeon_flush_cache_mm(struct mm_struct *mm)
111 * According to the R4K version of this file, CPUs without
112 * dcache aliases don't need to do anything here
118 * Flush a range of kernel addresses out of the icache
121 static void octeon_flush_icache_range(unsigned long start, unsigned long end)
123 octeon_flush_icache_all_cores(NULL);
128 * Flush the icache for a trampoline. These are used for interrupt
129 * and exception hooking.
131 * @addr: Address to flush
133 static void octeon_flush_cache_sigtramp(unsigned long addr)
135 struct vm_area_struct *vma;
137 vma = find_vma(current->mm, addr);
138 octeon_flush_icache_all_cores(vma);
143 * Flush a range out of a vma
149 static void octeon_flush_cache_range(struct vm_area_struct *vma,
150 unsigned long start, unsigned long end)
152 if (vma->vm_flags & VM_EXEC)
153 octeon_flush_icache_all_cores(vma);
158 * Flush a specific page of a vma
160 * @vma: VMA to flush page for
161 * @page: Page to flush
164 static void octeon_flush_cache_page(struct vm_area_struct *vma,
165 unsigned long page, unsigned long pfn)
167 if (vma->vm_flags & VM_EXEC)
168 octeon_flush_icache_all_cores(vma);
173 * Probe Octeon's caches
176 static void __devinit probe_octeon(void)
178 unsigned long icache_size;
179 unsigned long dcache_size;
180 unsigned int config1;
181 struct cpuinfo_mips *c = ¤t_cpu_data;
183 switch (c->cputype) {
184 case CPU_CAVIUM_OCTEON:
185 config1 = read_c0_config1();
186 c->icache.linesz = 2 << ((config1 >> 19) & 7);
187 c->icache.sets = 64 << ((config1 >> 22) & 7);
188 c->icache.ways = 1 + ((config1 >> 16) & 7);
189 c->icache.flags |= MIPS_CACHE_VTAG;
191 c->icache.sets * c->icache.ways * c->icache.linesz;
192 c->icache.waybit = ffs(icache_size / c->icache.ways) - 1;
193 c->dcache.linesz = 128;
194 if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
195 c->dcache.sets = 1; /* CN3XXX has one Dcache set */
197 c->dcache.sets = 2; /* CN5XXX has two Dcache sets */
200 c->dcache.sets * c->dcache.ways * c->dcache.linesz;
201 c->dcache.waybit = ffs(dcache_size / c->dcache.ways) - 1;
202 c->options |= MIPS_CPU_PREFETCH;
206 panic("Unsupported Cavium Networks CPU type\n");
210 /* compute a couple of other cache variables */
211 c->icache.waysize = icache_size / c->icache.ways;
212 c->dcache.waysize = dcache_size / c->dcache.ways;
214 c->icache.sets = icache_size / (c->icache.linesz * c->icache.ways);
215 c->dcache.sets = dcache_size / (c->dcache.linesz * c->dcache.ways);
217 if (smp_processor_id() == 0) {
218 pr_notice("Primary instruction cache %ldkB, %s, %d way, "
219 "%d sets, linesize %d bytes.\n",
221 cpu_has_vtag_icache ?
222 "virtually tagged" : "physically tagged",
223 c->icache.ways, c->icache.sets, c->icache.linesz);
225 pr_notice("Primary data cache %ldkB, %d-way, %d sets, "
226 "linesize %d bytes.\n",
227 dcache_size >> 10, c->dcache.ways,
228 c->dcache.sets, c->dcache.linesz);
234 * Setup the Octeon cache flush routines
237 void __devinit octeon_cache_init(void)
239 extern unsigned long ebase;
240 extern char except_vec2_octeon;
242 memcpy((void *)(ebase + 0x100), &except_vec2_octeon, 0x80);
243 octeon_flush_cache_sigtramp(ebase + 0x100);
247 shm_align_mask = PAGE_SIZE - 1;
249 flush_cache_all = octeon_flush_icache_all;
250 __flush_cache_all = octeon_flush_icache_all;
251 flush_cache_mm = octeon_flush_cache_mm;
252 flush_cache_page = octeon_flush_cache_page;
253 flush_cache_range = octeon_flush_cache_range;
254 flush_cache_sigtramp = octeon_flush_cache_sigtramp;
255 flush_icache_all = octeon_flush_icache_all;
256 flush_data_cache_page = octeon_flush_data_cache_page;
257 flush_icache_range = octeon_flush_icache_range;
258 local_flush_icache_range = local_octeon_flush_icache_range;
265 * Handle a cache error exception
268 static void cache_parity_error_octeon(int non_recoverable)
270 unsigned long coreid = cvmx_get_core_num();
271 uint64_t icache_err = read_octeon_c0_icacheerr();
273 pr_err("Cache error exception:\n");
274 pr_err("cp0_errorepc == %lx\n", read_c0_errorepc());
275 if (icache_err & 1) {
276 pr_err("CacheErr (Icache) == %llx\n",
277 (unsigned long long)icache_err);
278 write_octeon_c0_icacheerr(0);
280 if (cache_err_dcache[coreid] & 1) {
281 pr_err("CacheErr (Dcache) == %llx\n",
282 (unsigned long long)cache_err_dcache[coreid]);
283 cache_err_dcache[coreid] = 0;
287 panic("Can't handle cache error: nested exception");
291 * Called when the the exception is not recoverable
294 asmlinkage void cache_parity_error_octeon_recoverable(void)
296 cache_parity_error_octeon(0);
300 * Called when the the exception is recoverable
303 asmlinkage void cache_parity_error_octeon_non_recoverable(void)
305 cache_parity_error_octeon(1);