sc-mips.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2006 Chris Dearman (chris@mips.com),
  4. */
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <asm/cpu-type.h>
  10. #include <asm/mipsregs.h>
  11. #include <asm/bcache.h>
  12. #include <asm/cacheops.h>
  13. #include <asm/page.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/mmu_context.h>
  16. #include <asm/r4kcache.h>
  17. #include <asm/mips-cps.h>
  18. #include <asm/bootinfo.h>
  19. /*
  20. * MIPS32/MIPS64 L2 cache handling
  21. */
  22. /*
  23. * Writeback and invalidate the secondary cache before DMA.
  24. */
  25. static void mips_sc_wback_inv(unsigned long addr, unsigned long size)
  26. {
  27. blast_scache_range(addr, addr + size);
  28. }
  29. /*
  30. * Invalidate the secondary cache before DMA.
  31. */
  32. static void mips_sc_inv(unsigned long addr, unsigned long size)
  33. {
  34. unsigned long lsize = cpu_scache_line_size();
  35. unsigned long almask = ~(lsize - 1);
  36. cache_op(Hit_Writeback_Inv_SD, addr & almask);
  37. cache_op(Hit_Writeback_Inv_SD, (addr + size - 1) & almask);
  38. blast_inv_scache_range(addr, addr + size);
  39. }
  40. static void mips_sc_enable(void)
  41. {
  42. /* L2 cache is permanently enabled */
  43. }
  44. static void mips_sc_disable(void)
  45. {
  46. /* L2 cache is permanently enabled */
  47. }
  48. static void mips_sc_prefetch_enable(void)
  49. {
  50. unsigned long pftctl;
  51. if (mips_cm_revision() < CM_REV_CM2_5)
  52. return;
  53. /*
  54. * If there is one or more L2 prefetch unit present then enable
  55. * prefetching for both code & data, for all ports.
  56. */
  57. pftctl = read_gcr_l2_pft_control();
  58. if (pftctl & CM_GCR_L2_PFT_CONTROL_NPFT) {
  59. pftctl &= ~CM_GCR_L2_PFT_CONTROL_PAGEMASK;
  60. pftctl |= PAGE_MASK & CM_GCR_L2_PFT_CONTROL_PAGEMASK;
  61. pftctl |= CM_GCR_L2_PFT_CONTROL_PFTEN;
  62. write_gcr_l2_pft_control(pftctl);
  63. set_gcr_l2_pft_control_b(CM_GCR_L2_PFT_CONTROL_B_PORTID |
  64. CM_GCR_L2_PFT_CONTROL_B_CEN);
  65. }
  66. }
  67. static void mips_sc_prefetch_disable(void)
  68. {
  69. if (mips_cm_revision() < CM_REV_CM2_5)
  70. return;
  71. clear_gcr_l2_pft_control(CM_GCR_L2_PFT_CONTROL_PFTEN);
  72. clear_gcr_l2_pft_control_b(CM_GCR_L2_PFT_CONTROL_B_PORTID |
  73. CM_GCR_L2_PFT_CONTROL_B_CEN);
  74. }
  75. static bool mips_sc_prefetch_is_enabled(void)
  76. {
  77. unsigned long pftctl;
  78. if (mips_cm_revision() < CM_REV_CM2_5)
  79. return false;
  80. pftctl = read_gcr_l2_pft_control();
  81. if (!(pftctl & CM_GCR_L2_PFT_CONTROL_NPFT))
  82. return false;
  83. return !!(pftctl & CM_GCR_L2_PFT_CONTROL_PFTEN);
  84. }
  85. static struct bcache_ops mips_sc_ops = {
  86. .bc_enable = mips_sc_enable,
  87. .bc_disable = mips_sc_disable,
  88. .bc_wback_inv = mips_sc_wback_inv,
  89. .bc_inv = mips_sc_inv,
  90. .bc_prefetch_enable = mips_sc_prefetch_enable,
  91. .bc_prefetch_disable = mips_sc_prefetch_disable,
  92. .bc_prefetch_is_enabled = mips_sc_prefetch_is_enabled,
  93. };
  94. /*
  95. * Check if the L2 cache controller is activated on a particular platform.
  96. * MTI's L2 controller and the L2 cache controller of Broadcom's BMIPS
  97. * cores both use c0_config2's bit 12 as "L2 Bypass" bit, that is the
  98. * cache being disabled. However there is no guarantee for this to be
  99. * true on all platforms. In an act of stupidity the spec defined bits
  100. * 12..15 as implementation defined so below function will eventually have
  101. * to be replaced by a platform specific probe.
  102. */
  103. static inline int mips_sc_is_activated(struct cpuinfo_mips *c)
  104. {
  105. unsigned int config2 = read_c0_config2();
  106. unsigned int tmp;
  107. /* Check the bypass bit (L2B) */
  108. switch (current_cpu_type()) {
  109. case CPU_34K:
  110. case CPU_74K:
  111. case CPU_1004K:
  112. case CPU_1074K:
  113. case CPU_INTERAPTIV:
  114. case CPU_PROAPTIV:
  115. case CPU_P5600:
  116. case CPU_BMIPS5000:
  117. case CPU_QEMU_GENERIC:
  118. case CPU_P6600:
  119. if (config2 & (1 << 12))
  120. return 0;
  121. }
  122. tmp = (config2 >> 4) & 0x0f;
  123. if (0 < tmp && tmp <= 7)
  124. c->scache.linesz = 2 << tmp;
  125. else
  126. return 0;
  127. return 1;
  128. }
  129. static int __init mips_sc_probe_cm3(void)
  130. {
  131. struct cpuinfo_mips *c = &current_cpu_data;
  132. unsigned long cfg = read_gcr_l2_config();
  133. unsigned long sets, line_sz, assoc;
  134. if (cfg & CM_GCR_L2_CONFIG_BYPASS)
  135. return 0;
  136. sets = cfg & CM_GCR_L2_CONFIG_SET_SIZE;
  137. sets >>= __ffs(CM_GCR_L2_CONFIG_SET_SIZE);
  138. if (sets)
  139. c->scache.sets = 64 << sets;
  140. line_sz = cfg & CM_GCR_L2_CONFIG_LINE_SIZE;
  141. line_sz >>= __ffs(CM_GCR_L2_CONFIG_LINE_SIZE);
  142. if (line_sz)
  143. c->scache.linesz = 2 << line_sz;
  144. assoc = cfg & CM_GCR_L2_CONFIG_ASSOC;
  145. assoc >>= __ffs(CM_GCR_L2_CONFIG_ASSOC);
  146. c->scache.ways = assoc + 1;
  147. c->scache.waysize = c->scache.sets * c->scache.linesz;
  148. c->scache.waybit = __ffs(c->scache.waysize);
  149. if (c->scache.linesz) {
  150. c->scache.flags &= ~MIPS_CACHE_NOT_PRESENT;
  151. c->options |= MIPS_CPU_INCLUSIVE_CACHES;
  152. return 1;
  153. }
  154. return 0;
  155. }
  156. static inline int __init mips_sc_probe(void)
  157. {
  158. struct cpuinfo_mips *c = &current_cpu_data;
  159. unsigned int config1, config2;
  160. unsigned int tmp;
  161. /* Mark as not present until probe completed */
  162. c->scache.flags |= MIPS_CACHE_NOT_PRESENT;
  163. if (mips_cm_revision() >= CM_REV_CM3)
  164. return mips_sc_probe_cm3();
  165. /* Ignore anything but MIPSxx processors */
  166. if (!(c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M32R2 |
  167. MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R1 |
  168. MIPS_CPU_ISA_M64R2 | MIPS_CPU_ISA_M64R6)))
  169. return 0;
  170. /* Does this MIPS32/MIPS64 CPU have a config2 register? */
  171. config1 = read_c0_config1();
  172. if (!(config1 & MIPS_CONF_M))
  173. return 0;
  174. config2 = read_c0_config2();
  175. if (!mips_sc_is_activated(c))
  176. return 0;
  177. tmp = (config2 >> 8) & 0x0f;
  178. if (tmp <= 7)
  179. c->scache.sets = 64 << tmp;
  180. else
  181. return 0;
  182. tmp = (config2 >> 0) & 0x0f;
  183. if (tmp <= 7)
  184. c->scache.ways = tmp + 1;
  185. else
  186. return 0;
  187. /*
  188. * According to config2 it would be 5-ways, but that is contradicted
  189. * by all documentation.
  190. */
  191. if (current_cpu_type() == CPU_JZRISC &&
  192. mips_machtype == MACH_INGENIC_JZ4770)
  193. c->scache.ways = 4;
  194. c->scache.waysize = c->scache.sets * c->scache.linesz;
  195. c->scache.waybit = __ffs(c->scache.waysize);
  196. c->scache.flags &= ~MIPS_CACHE_NOT_PRESENT;
  197. return 1;
  198. }
  199. int mips_sc_init(void)
  200. {
  201. int found = mips_sc_probe();
  202. if (found) {
  203. mips_sc_enable();
  204. mips_sc_prefetch_enable();
  205. bcops = &mips_sc_ops;
  206. }
  207. return found;
  208. }