c-octeon.c 8.8 KB

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