c-octeon.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  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
  4. * for more details.
  5. *
  6. * Copyright (C) 2005-2007 Cavium Networks
  7. */
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/smp.h>
  12. #include <linux/mm.h>
  13. #include <linux/bitops.h>
  14. #include <linux/cpu.h>
  15. #include <linux/io.h>
  16. #include <asm/bcache.h>
  17. #include <asm/bootinfo.h>
  18. #include <asm/cacheops.h>
  19. #include <asm/cpu-features.h>
  20. #include <asm/page.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/r4kcache.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/war.h>
  25. #include <asm/octeon/octeon.h>
  26. unsigned long long cache_err_dcache[NR_CPUS];
  27. /**
  28. * Octeon automatically flushes the dcache on tlb changes, so
  29. * from Linux's viewpoint it acts much like a physically
  30. * tagged cache. No flushing is needed
  31. *
  32. */
  33. static void octeon_flush_data_cache_page(unsigned long addr)
  34. {
  35. /* Nothing to do */
  36. }
  37. static inline void octeon_local_flush_icache(void)
  38. {
  39. asm volatile ("synci 0($0)");
  40. }
  41. /*
  42. * Flush local I-cache for the specified range.
  43. */
  44. static void local_octeon_flush_icache_range(unsigned long start,
  45. unsigned long end)
  46. {
  47. octeon_local_flush_icache();
  48. }
  49. /**
  50. * Flush caches as necessary for all cores affected by a
  51. * vma. If no vma is supplied, all cores are flushed.
  52. *
  53. * @vma: VMA to flush or NULL to flush all icaches.
  54. */
  55. static void octeon_flush_icache_all_cores(struct vm_area_struct *vma)
  56. {
  57. extern void octeon_send_ipi_single(int cpu, unsigned int action);
  58. #ifdef CONFIG_SMP
  59. int cpu;
  60. cpumask_t mask;
  61. #endif
  62. mb();
  63. octeon_local_flush_icache();
  64. #ifdef CONFIG_SMP
  65. preempt_disable();
  66. cpu = smp_processor_id();
  67. /*
  68. * If we have a vma structure, we only need to worry about
  69. * cores it has been used on
  70. */
  71. if (vma)
  72. mask = *mm_cpumask(vma->vm_mm);
  73. else
  74. mask = *cpu_online_mask;
  75. cpumask_clear_cpu(cpu, &mask);
  76. for_each_cpu(cpu, &mask)
  77. octeon_send_ipi_single(cpu, SMP_ICACHE_FLUSH);
  78. preempt_enable();
  79. #endif
  80. }
  81. /**
  82. * Called to flush the icache on all cores
  83. */
  84. static void octeon_flush_icache_all(void)
  85. {
  86. octeon_flush_icache_all_cores(NULL);
  87. }
  88. /**
  89. * Called to flush all memory associated with a memory
  90. * context.
  91. *
  92. * @mm: Memory context to flush
  93. */
  94. static void octeon_flush_cache_mm(struct mm_struct *mm)
  95. {
  96. /*
  97. * According to the R4K version of this file, CPUs without
  98. * dcache aliases don't need to do anything here
  99. */
  100. }
  101. /**
  102. * Flush a range of kernel addresses out of the icache
  103. *
  104. */
  105. static void octeon_flush_icache_range(unsigned long start, unsigned long end)
  106. {
  107. octeon_flush_icache_all_cores(NULL);
  108. }
  109. /**
  110. * Flush the icache for a trampoline. These are used for interrupt
  111. * and exception hooking.
  112. *
  113. * @addr: Address to flush
  114. */
  115. static void octeon_flush_cache_sigtramp(unsigned long addr)
  116. {
  117. struct vm_area_struct *vma;
  118. vma = find_vma(current->mm, addr);
  119. octeon_flush_icache_all_cores(vma);
  120. }
  121. /**
  122. * Flush a range out of a vma
  123. *
  124. * @vma: VMA to flush
  125. * @start:
  126. * @end:
  127. */
  128. static void octeon_flush_cache_range(struct vm_area_struct *vma,
  129. unsigned long start, unsigned long end)
  130. {
  131. if (vma->vm_flags & VM_EXEC)
  132. octeon_flush_icache_all_cores(vma);
  133. }
  134. /**
  135. * Flush a specific page of a vma
  136. *
  137. * @vma: VMA to flush page for
  138. * @page: Page to flush
  139. * @pfn:
  140. */
  141. static void octeon_flush_cache_page(struct vm_area_struct *vma,
  142. unsigned long page, unsigned long pfn)
  143. {
  144. if (vma->vm_flags & VM_EXEC)
  145. octeon_flush_icache_all_cores(vma);
  146. }
  147. static void octeon_flush_kernel_vmap_range(unsigned long vaddr, int size)
  148. {
  149. BUG();
  150. }
  151. /**
  152. * Probe Octeon's caches
  153. *
  154. */
  155. static void __cpuinit probe_octeon(void)
  156. {
  157. unsigned long icache_size;
  158. unsigned long dcache_size;
  159. unsigned int config1;
  160. struct cpuinfo_mips *c = &current_cpu_data;
  161. config1 = read_c0_config1();
  162. switch (c->cputype) {
  163. case CPU_CAVIUM_OCTEON:
  164. case CPU_CAVIUM_OCTEON_PLUS:
  165. c->icache.linesz = 2 << ((config1 >> 19) & 7);
  166. c->icache.sets = 64 << ((config1 >> 22) & 7);
  167. c->icache.ways = 1 + ((config1 >> 16) & 7);
  168. c->icache.flags |= MIPS_CACHE_VTAG;
  169. icache_size =
  170. c->icache.sets * c->icache.ways * c->icache.linesz;
  171. c->icache.waybit = ffs(icache_size / c->icache.ways) - 1;
  172. c->dcache.linesz = 128;
  173. if (c->cputype == CPU_CAVIUM_OCTEON_PLUS)
  174. c->dcache.sets = 2; /* CN5XXX has two Dcache sets */
  175. else
  176. c->dcache.sets = 1; /* CN3XXX has one Dcache set */
  177. c->dcache.ways = 64;
  178. dcache_size =
  179. c->dcache.sets * c->dcache.ways * c->dcache.linesz;
  180. c->dcache.waybit = ffs(dcache_size / c->dcache.ways) - 1;
  181. c->options |= MIPS_CPU_PREFETCH;
  182. break;
  183. case CPU_CAVIUM_OCTEON2:
  184. c->icache.linesz = 2 << ((config1 >> 19) & 7);
  185. c->icache.sets = 8;
  186. c->icache.ways = 37;
  187. c->icache.flags |= MIPS_CACHE_VTAG;
  188. icache_size = c->icache.sets * c->icache.ways * c->icache.linesz;
  189. c->dcache.linesz = 128;
  190. c->dcache.ways = 32;
  191. c->dcache.sets = 8;
  192. dcache_size = c->dcache.sets * c->dcache.ways * c->dcache.linesz;
  193. c->options |= MIPS_CPU_PREFETCH;
  194. break;
  195. default:
  196. panic("Unsupported Cavium Networks CPU type");
  197. break;
  198. }
  199. /* compute a couple of other cache variables */
  200. c->icache.waysize = icache_size / c->icache.ways;
  201. c->dcache.waysize = dcache_size / c->dcache.ways;
  202. c->icache.sets = icache_size / (c->icache.linesz * c->icache.ways);
  203. c->dcache.sets = dcache_size / (c->dcache.linesz * c->dcache.ways);
  204. if (smp_processor_id() == 0) {
  205. pr_notice("Primary instruction cache %ldkB, %s, %d way, "
  206. "%d sets, linesize %d bytes.\n",
  207. icache_size >> 10,
  208. cpu_has_vtag_icache ?
  209. "virtually tagged" : "physically tagged",
  210. c->icache.ways, c->icache.sets, c->icache.linesz);
  211. pr_notice("Primary data cache %ldkB, %d-way, %d sets, "
  212. "linesize %d bytes.\n",
  213. dcache_size >> 10, c->dcache.ways,
  214. c->dcache.sets, c->dcache.linesz);
  215. }
  216. }
  217. /**
  218. * Setup the Octeon cache flush routines
  219. *
  220. */
  221. void __cpuinit octeon_cache_init(void)
  222. {
  223. extern unsigned long ebase;
  224. extern char except_vec2_octeon;
  225. memcpy((void *)(ebase + 0x100), &except_vec2_octeon, 0x80);
  226. octeon_flush_cache_sigtramp(ebase + 0x100);
  227. probe_octeon();
  228. shm_align_mask = PAGE_SIZE - 1;
  229. flush_cache_all = octeon_flush_icache_all;
  230. __flush_cache_all = octeon_flush_icache_all;
  231. flush_cache_mm = octeon_flush_cache_mm;
  232. flush_cache_page = octeon_flush_cache_page;
  233. flush_cache_range = octeon_flush_cache_range;
  234. flush_cache_sigtramp = octeon_flush_cache_sigtramp;
  235. flush_icache_all = octeon_flush_icache_all;
  236. flush_data_cache_page = octeon_flush_data_cache_page;
  237. flush_icache_range = octeon_flush_icache_range;
  238. local_flush_icache_range = local_octeon_flush_icache_range;
  239. __flush_kernel_vmap_range = octeon_flush_kernel_vmap_range;
  240. build_clear_page();
  241. build_copy_page();
  242. }
  243. /**
  244. * Handle a cache error exception
  245. */
  246. static void cache_parity_error_octeon(int non_recoverable)
  247. {
  248. unsigned long coreid = cvmx_get_core_num();
  249. uint64_t icache_err = read_octeon_c0_icacheerr();
  250. pr_err("Cache error exception:\n");
  251. pr_err("cp0_errorepc == %lx\n", read_c0_errorepc());
  252. if (icache_err & 1) {
  253. pr_err("CacheErr (Icache) == %llx\n",
  254. (unsigned long long)icache_err);
  255. write_octeon_c0_icacheerr(0);
  256. }
  257. if (cache_err_dcache[coreid] & 1) {
  258. pr_err("CacheErr (Dcache) == %llx\n",
  259. (unsigned long long)cache_err_dcache[coreid]);
  260. cache_err_dcache[coreid] = 0;
  261. }
  262. if (non_recoverable)
  263. panic("Can't handle cache error: nested exception");
  264. }
  265. /**
  266. * Called when the the exception is recoverable
  267. */
  268. asmlinkage void cache_parity_error_octeon_recoverable(void)
  269. {
  270. cache_parity_error_octeon(0);
  271. }
  272. /**
  273. * Called when the the exception is not recoverable
  274. */
  275. asmlinkage void cache_parity_error_octeon_non_recoverable(void)
  276. {
  277. cache_parity_error_octeon(1);
  278. }