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/system.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/war.h>
  26. #include <asm/octeon/octeon.h>
  27. unsigned long long cache_err_dcache[NR_CPUS];
  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. * Flush caches as necessary for all cores affected by a
  52. * 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_map;
  76. cpu_clear(cpu, mask);
  77. for_each_cpu_mask(cpu, mask)
  78. octeon_send_ipi_single(cpu, SMP_ICACHE_FLUSH);
  79. preempt_enable();
  80. #endif
  81. }
  82. /**
  83. * Called to flush the icache on all cores
  84. */
  85. static void octeon_flush_icache_all(void)
  86. {
  87. octeon_flush_icache_all_cores(NULL);
  88. }
  89. /**
  90. * Called to flush all memory associated with a memory
  91. * context.
  92. *
  93. * @mm: Memory context to flush
  94. */
  95. static void octeon_flush_cache_mm(struct mm_struct *mm)
  96. {
  97. /*
  98. * According to the R4K version of this file, CPUs without
  99. * dcache aliases don't need to do anything here
  100. */
  101. }
  102. /**
  103. * Flush a range of kernel addresses out of the icache
  104. *
  105. */
  106. static void octeon_flush_icache_range(unsigned long start, unsigned long end)
  107. {
  108. octeon_flush_icache_all_cores(NULL);
  109. }
  110. /**
  111. * Flush the icache for a trampoline. These are used for interrupt
  112. * and exception hooking.
  113. *
  114. * @addr: Address to flush
  115. */
  116. static void octeon_flush_cache_sigtramp(unsigned long addr)
  117. {
  118. struct vm_area_struct *vma;
  119. vma = find_vma(current->mm, addr);
  120. octeon_flush_icache_all_cores(vma);
  121. }
  122. /**
  123. * Flush a range out of a vma
  124. *
  125. * @vma: VMA to flush
  126. * @start:
  127. * @end:
  128. */
  129. static void octeon_flush_cache_range(struct vm_area_struct *vma,
  130. unsigned long start, unsigned long end)
  131. {
  132. if (vma->vm_flags & VM_EXEC)
  133. octeon_flush_icache_all_cores(vma);
  134. }
  135. /**
  136. * Flush a specific page of a vma
  137. *
  138. * @vma: VMA to flush page for
  139. * @page: Page to flush
  140. * @pfn:
  141. */
  142. static void octeon_flush_cache_page(struct vm_area_struct *vma,
  143. unsigned long page, unsigned long pfn)
  144. {
  145. if (vma->vm_flags & VM_EXEC)
  146. octeon_flush_icache_all_cores(vma);
  147. }
  148. static void octeon_flush_kernel_vmap_range(unsigned long vaddr, int size)
  149. {
  150. BUG();
  151. }
  152. /**
  153. * Probe Octeon's caches
  154. *
  155. */
  156. static void __cpuinit probe_octeon(void)
  157. {
  158. unsigned long icache_size;
  159. unsigned long dcache_size;
  160. unsigned int config1;
  161. struct cpuinfo_mips *c = &current_cpu_data;
  162. config1 = read_c0_config1();
  163. switch (c->cputype) {
  164. case CPU_CAVIUM_OCTEON:
  165. case CPU_CAVIUM_OCTEON_PLUS:
  166. c->icache.linesz = 2 << ((config1 >> 19) & 7);
  167. c->icache.sets = 64 << ((config1 >> 22) & 7);
  168. c->icache.ways = 1 + ((config1 >> 16) & 7);
  169. c->icache.flags |= MIPS_CACHE_VTAG;
  170. icache_size =
  171. c->icache.sets * c->icache.ways * c->icache.linesz;
  172. c->icache.waybit = ffs(icache_size / c->icache.ways) - 1;
  173. c->dcache.linesz = 128;
  174. if (c->cputype == CPU_CAVIUM_OCTEON_PLUS)
  175. c->dcache.sets = 2; /* CN5XXX has two Dcache sets */
  176. else
  177. c->dcache.sets = 1; /* CN3XXX has one Dcache set */
  178. c->dcache.ways = 64;
  179. dcache_size =
  180. c->dcache.sets * c->dcache.ways * c->dcache.linesz;
  181. c->dcache.waybit = ffs(dcache_size / c->dcache.ways) - 1;
  182. c->options |= MIPS_CPU_PREFETCH;
  183. break;
  184. case CPU_CAVIUM_OCTEON2:
  185. c->icache.linesz = 2 << ((config1 >> 19) & 7);
  186. c->icache.sets = 8;
  187. c->icache.ways = 37;
  188. c->icache.flags |= MIPS_CACHE_VTAG;
  189. icache_size = c->icache.sets * c->icache.ways * c->icache.linesz;
  190. c->dcache.linesz = 128;
  191. c->dcache.ways = 32;
  192. c->dcache.sets = 8;
  193. dcache_size = c->dcache.sets * c->dcache.ways * c->dcache.linesz;
  194. c->options |= MIPS_CPU_PREFETCH;
  195. break;
  196. default:
  197. panic("Unsupported Cavium Networks CPU type\n");
  198. break;
  199. }
  200. /* compute a couple of other cache variables */
  201. c->icache.waysize = icache_size / c->icache.ways;
  202. c->dcache.waysize = dcache_size / c->dcache.ways;
  203. c->icache.sets = icache_size / (c->icache.linesz * c->icache.ways);
  204. c->dcache.sets = dcache_size / (c->dcache.linesz * c->dcache.ways);
  205. if (smp_processor_id() == 0) {
  206. pr_notice("Primary instruction cache %ldkB, %s, %d way, "
  207. "%d sets, linesize %d bytes.\n",
  208. icache_size >> 10,
  209. cpu_has_vtag_icache ?
  210. "virtually tagged" : "physically tagged",
  211. c->icache.ways, c->icache.sets, c->icache.linesz);
  212. pr_notice("Primary data cache %ldkB, %d-way, %d sets, "
  213. "linesize %d bytes.\n",
  214. dcache_size >> 10, c->dcache.ways,
  215. c->dcache.sets, c->dcache.linesz);
  216. }
  217. }
  218. /**
  219. * Setup the Octeon cache flush routines
  220. *
  221. */
  222. void __cpuinit octeon_cache_init(void)
  223. {
  224. extern unsigned long ebase;
  225. extern char except_vec2_octeon;
  226. memcpy((void *)(ebase + 0x100), &except_vec2_octeon, 0x80);
  227. octeon_flush_cache_sigtramp(ebase + 0x100);
  228. probe_octeon();
  229. shm_align_mask = PAGE_SIZE - 1;
  230. flush_cache_all = octeon_flush_icache_all;
  231. __flush_cache_all = octeon_flush_icache_all;
  232. flush_cache_mm = octeon_flush_cache_mm;
  233. flush_cache_page = octeon_flush_cache_page;
  234. flush_cache_range = octeon_flush_cache_range;
  235. flush_cache_sigtramp = octeon_flush_cache_sigtramp;
  236. flush_icache_all = octeon_flush_icache_all;
  237. flush_data_cache_page = octeon_flush_data_cache_page;
  238. flush_icache_range = octeon_flush_icache_range;
  239. local_flush_icache_range = local_octeon_flush_icache_range;
  240. __flush_kernel_vmap_range = octeon_flush_kernel_vmap_range;
  241. build_clear_page();
  242. build_copy_page();
  243. }
  244. /**
  245. * Handle a cache error exception
  246. */
  247. static void cache_parity_error_octeon(int non_recoverable)
  248. {
  249. unsigned long coreid = cvmx_get_core_num();
  250. uint64_t icache_err = read_octeon_c0_icacheerr();
  251. pr_err("Cache error exception:\n");
  252. pr_err("cp0_errorepc == %lx\n", read_c0_errorepc());
  253. if (icache_err & 1) {
  254. pr_err("CacheErr (Icache) == %llx\n",
  255. (unsigned long long)icache_err);
  256. write_octeon_c0_icacheerr(0);
  257. }
  258. if (cache_err_dcache[coreid] & 1) {
  259. pr_err("CacheErr (Dcache) == %llx\n",
  260. (unsigned long long)cache_err_dcache[coreid]);
  261. cache_err_dcache[coreid] = 0;
  262. }
  263. if (non_recoverable)
  264. panic("Can't handle cache error: nested exception");
  265. }
  266. /**
  267. * Called when the the exception is recoverable
  268. */
  269. asmlinkage void cache_parity_error_octeon_recoverable(void)
  270. {
  271. cache_parity_error_octeon(0);
  272. }
  273. /**
  274. * Called when the the exception is not recoverable
  275. */
  276. asmlinkage void cache_parity_error_octeon_non_recoverable(void)
  277. {
  278. cache_parity_error_octeon(1);
  279. }