c-octeon.c 8.7 KB

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