c-octeon.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/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/cpu-type.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. down_read(&current->mm->mmap_sem);
  122. vma = find_vma(current->mm, addr);
  123. octeon_flush_icache_all_cores(vma);
  124. up_read(&current->mm->mmap_sem);
  125. }
  126. /**
  127. * Flush a range out of a vma
  128. *
  129. * @vma: VMA to flush
  130. * @start:
  131. * @end:
  132. */
  133. static void octeon_flush_cache_range(struct vm_area_struct *vma,
  134. unsigned long start, unsigned long end)
  135. {
  136. if (vma->vm_flags & VM_EXEC)
  137. octeon_flush_icache_all_cores(vma);
  138. }
  139. /**
  140. * Flush a specific page of a vma
  141. *
  142. * @vma: VMA to flush page for
  143. * @page: Page to flush
  144. * @pfn:
  145. */
  146. static void octeon_flush_cache_page(struct vm_area_struct *vma,
  147. unsigned long page, unsigned long pfn)
  148. {
  149. if (vma->vm_flags & VM_EXEC)
  150. octeon_flush_icache_all_cores(vma);
  151. }
  152. static void octeon_flush_kernel_vmap_range(unsigned long vaddr, int size)
  153. {
  154. BUG();
  155. }
  156. /**
  157. * Probe Octeon's caches
  158. *
  159. */
  160. static void probe_octeon(void)
  161. {
  162. unsigned long icache_size;
  163. unsigned long dcache_size;
  164. unsigned int config1;
  165. struct cpuinfo_mips *c = &current_cpu_data;
  166. int cputype = current_cpu_type();
  167. config1 = read_c0_config1();
  168. switch (cputype) {
  169. case CPU_CAVIUM_OCTEON:
  170. case CPU_CAVIUM_OCTEON_PLUS:
  171. c->icache.linesz = 2 << ((config1 >> 19) & 7);
  172. c->icache.sets = 64 << ((config1 >> 22) & 7);
  173. c->icache.ways = 1 + ((config1 >> 16) & 7);
  174. c->icache.flags |= MIPS_CACHE_VTAG;
  175. icache_size =
  176. c->icache.sets * c->icache.ways * c->icache.linesz;
  177. c->icache.waybit = ffs(icache_size / c->icache.ways) - 1;
  178. c->dcache.linesz = 128;
  179. if (cputype == CPU_CAVIUM_OCTEON_PLUS)
  180. c->dcache.sets = 2; /* CN5XXX has two Dcache sets */
  181. else
  182. c->dcache.sets = 1; /* CN3XXX has one Dcache set */
  183. c->dcache.ways = 64;
  184. dcache_size =
  185. c->dcache.sets * c->dcache.ways * c->dcache.linesz;
  186. c->dcache.waybit = ffs(dcache_size / c->dcache.ways) - 1;
  187. c->options |= MIPS_CPU_PREFETCH;
  188. break;
  189. case CPU_CAVIUM_OCTEON2:
  190. c->icache.linesz = 2 << ((config1 >> 19) & 7);
  191. c->icache.sets = 8;
  192. c->icache.ways = 37;
  193. c->icache.flags |= MIPS_CACHE_VTAG;
  194. icache_size = c->icache.sets * c->icache.ways * c->icache.linesz;
  195. c->dcache.linesz = 128;
  196. c->dcache.ways = 32;
  197. c->dcache.sets = 8;
  198. dcache_size = c->dcache.sets * c->dcache.ways * c->dcache.linesz;
  199. c->options |= MIPS_CPU_PREFETCH;
  200. break;
  201. case CPU_CAVIUM_OCTEON3:
  202. c->icache.linesz = 128;
  203. c->icache.sets = 16;
  204. c->icache.ways = 39;
  205. c->icache.flags |= MIPS_CACHE_VTAG;
  206. icache_size = c->icache.sets * c->icache.ways * c->icache.linesz;
  207. c->dcache.linesz = 128;
  208. c->dcache.ways = 32;
  209. c->dcache.sets = 8;
  210. dcache_size = c->dcache.sets * c->dcache.ways * c->dcache.linesz;
  211. c->options |= MIPS_CPU_PREFETCH;
  212. break;
  213. default:
  214. panic("Unsupported Cavium Networks CPU type");
  215. break;
  216. }
  217. /* compute a couple of other cache variables */
  218. c->icache.waysize = icache_size / c->icache.ways;
  219. c->dcache.waysize = dcache_size / c->dcache.ways;
  220. c->icache.sets = icache_size / (c->icache.linesz * c->icache.ways);
  221. c->dcache.sets = dcache_size / (c->dcache.linesz * c->dcache.ways);
  222. if (smp_processor_id() == 0) {
  223. pr_notice("Primary instruction cache %ldkB, %s, %d way, "
  224. "%d sets, linesize %d bytes.\n",
  225. icache_size >> 10,
  226. cpu_has_vtag_icache ?
  227. "virtually tagged" : "physically tagged",
  228. c->icache.ways, c->icache.sets, c->icache.linesz);
  229. pr_notice("Primary data cache %ldkB, %d-way, %d sets, "
  230. "linesize %d bytes.\n",
  231. dcache_size >> 10, c->dcache.ways,
  232. c->dcache.sets, c->dcache.linesz);
  233. }
  234. }
  235. static void octeon_cache_error_setup(void)
  236. {
  237. extern char except_vec2_octeon;
  238. set_handler(0x100, &except_vec2_octeon, 0x80);
  239. }
  240. /**
  241. * Setup the Octeon cache flush routines
  242. *
  243. */
  244. void octeon_cache_init(void)
  245. {
  246. probe_octeon();
  247. shm_align_mask = PAGE_SIZE - 1;
  248. flush_cache_all = octeon_flush_icache_all;
  249. __flush_cache_all = octeon_flush_icache_all;
  250. flush_cache_mm = octeon_flush_cache_mm;
  251. flush_cache_page = octeon_flush_cache_page;
  252. flush_cache_range = octeon_flush_cache_range;
  253. flush_cache_sigtramp = octeon_flush_cache_sigtramp;
  254. flush_icache_all = octeon_flush_icache_all;
  255. flush_data_cache_page = octeon_flush_data_cache_page;
  256. flush_icache_range = octeon_flush_icache_range;
  257. local_flush_icache_range = local_octeon_flush_icache_range;
  258. __flush_kernel_vmap_range = octeon_flush_kernel_vmap_range;
  259. build_clear_page();
  260. build_copy_page();
  261. board_cache_error_setup = octeon_cache_error_setup;
  262. }
  263. /*
  264. * Handle a cache error exception
  265. */
  266. static RAW_NOTIFIER_HEAD(co_cache_error_chain);
  267. int register_co_cache_error_notifier(struct notifier_block *nb)
  268. {
  269. return raw_notifier_chain_register(&co_cache_error_chain, nb);
  270. }
  271. EXPORT_SYMBOL_GPL(register_co_cache_error_notifier);
  272. int unregister_co_cache_error_notifier(struct notifier_block *nb)
  273. {
  274. return raw_notifier_chain_unregister(&co_cache_error_chain, nb);
  275. }
  276. EXPORT_SYMBOL_GPL(unregister_co_cache_error_notifier);
  277. static void co_cache_error_call_notifiers(unsigned long val)
  278. {
  279. int rv = raw_notifier_call_chain(&co_cache_error_chain, val, NULL);
  280. if ((rv & ~NOTIFY_STOP_MASK) != NOTIFY_OK) {
  281. u64 dcache_err;
  282. unsigned long coreid = cvmx_get_core_num();
  283. u64 icache_err = read_octeon_c0_icacheerr();
  284. if (val) {
  285. dcache_err = cache_err_dcache[coreid];
  286. cache_err_dcache[coreid] = 0;
  287. } else {
  288. dcache_err = read_octeon_c0_dcacheerr();
  289. }
  290. pr_err("Core%lu: Cache error exception:\n", coreid);
  291. pr_err("cp0_errorepc == %lx\n", read_c0_errorepc());
  292. if (icache_err & 1) {
  293. pr_err("CacheErr (Icache) == %llx\n",
  294. (unsigned long long)icache_err);
  295. write_octeon_c0_icacheerr(0);
  296. }
  297. if (dcache_err & 1) {
  298. pr_err("CacheErr (Dcache) == %llx\n",
  299. (unsigned long long)dcache_err);
  300. }
  301. }
  302. }
  303. /*
  304. * Called when the the exception is recoverable
  305. */
  306. asmlinkage void cache_parity_error_octeon_recoverable(void)
  307. {
  308. co_cache_error_call_notifiers(0);
  309. }
  310. /**
  311. * Called when the the exception is not recoverable
  312. */
  313. asmlinkage void cache_parity_error_octeon_non_recoverable(void)
  314. {
  315. co_cache_error_call_notifiers(1);
  316. panic("Can't handle cache error: nested exception");
  317. }