c-octeon.c 7.9 KB

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