sc-rm7k.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sc-rm7k.c: RM7000 cache management functions.
  4. *
  5. * Copyright (C) 1997, 2001, 2003, 2004 Ralf Baechle ([email protected])
  6. */
  7. #undef DEBUG
  8. #include <linux/kernel.h>
  9. #include <linux/mm.h>
  10. #include <linux/bitops.h>
  11. #include <asm/addrspace.h>
  12. #include <asm/bcache.h>
  13. #include <asm/cacheops.h>
  14. #include <asm/mipsregs.h>
  15. #include <asm/processor.h>
  16. #include <asm/sections.h>
  17. #include <asm/cacheflush.h> /* for run_uncached() */
  18. /* Primary cache parameters. */
  19. #define sc_lsize 32
  20. #define tc_pagesize (32*128)
  21. /* Secondary cache parameters. */
  22. #define scache_size (256*1024) /* Fixed to 256KiB on RM7000 */
  23. /* Tertiary cache parameters */
  24. #define tc_lsize 32
  25. extern unsigned long icache_way_size, dcache_way_size;
  26. static unsigned long tcache_size;
  27. #include <asm/r4kcache.h>
  28. static int rm7k_tcache_init;
  29. /*
  30. * Writeback and invalidate the primary cache dcache before DMA.
  31. * (XXX These need to be fixed ...)
  32. */
  33. static void rm7k_sc_wback_inv(unsigned long addr, unsigned long size)
  34. {
  35. unsigned long end, a;
  36. pr_debug("rm7k_sc_wback_inv[%08lx,%08lx]", addr, size);
  37. /* Catch bad driver code */
  38. BUG_ON(size == 0);
  39. blast_scache_range(addr, addr + size);
  40. if (!rm7k_tcache_init)
  41. return;
  42. a = addr & ~(tc_pagesize - 1);
  43. end = (addr + size - 1) & ~(tc_pagesize - 1);
  44. while(1) {
  45. invalidate_tcache_page(a); /* Page_Invalidate_T */
  46. if (a == end)
  47. break;
  48. a += tc_pagesize;
  49. }
  50. }
  51. static void rm7k_sc_inv(unsigned long addr, unsigned long size)
  52. {
  53. unsigned long end, a;
  54. pr_debug("rm7k_sc_inv[%08lx,%08lx]", addr, size);
  55. /* Catch bad driver code */
  56. BUG_ON(size == 0);
  57. blast_inv_scache_range(addr, addr + size);
  58. if (!rm7k_tcache_init)
  59. return;
  60. a = addr & ~(tc_pagesize - 1);
  61. end = (addr + size - 1) & ~(tc_pagesize - 1);
  62. while(1) {
  63. invalidate_tcache_page(a); /* Page_Invalidate_T */
  64. if (a == end)
  65. break;
  66. a += tc_pagesize;
  67. }
  68. }
  69. static void blast_rm7k_tcache(void)
  70. {
  71. unsigned long start = CKSEG0ADDR(0);
  72. unsigned long end = start + tcache_size;
  73. write_c0_taglo(0);
  74. while (start < end) {
  75. cache_op(Page_Invalidate_T, start);
  76. start += tc_pagesize;
  77. }
  78. }
  79. /*
  80. * This function is executed in uncached address space.
  81. */
  82. static void __rm7k_tc_enable(void)
  83. {
  84. int i;
  85. set_c0_config(RM7K_CONF_TE);
  86. write_c0_taglo(0);
  87. write_c0_taghi(0);
  88. for (i = 0; i < tcache_size; i += tc_lsize)
  89. cache_op(Index_Store_Tag_T, CKSEG0ADDR(i));
  90. }
  91. static void rm7k_tc_enable(void)
  92. {
  93. if (read_c0_config() & RM7K_CONF_TE)
  94. return;
  95. BUG_ON(tcache_size == 0);
  96. run_uncached(__rm7k_tc_enable);
  97. }
  98. /*
  99. * This function is executed in uncached address space.
  100. */
  101. static void __rm7k_sc_enable(void)
  102. {
  103. int i;
  104. set_c0_config(RM7K_CONF_SE);
  105. write_c0_taglo(0);
  106. write_c0_taghi(0);
  107. for (i = 0; i < scache_size; i += sc_lsize)
  108. cache_op(Index_Store_Tag_SD, CKSEG0ADDR(i));
  109. }
  110. static void rm7k_sc_enable(void)
  111. {
  112. if (read_c0_config() & RM7K_CONF_SE)
  113. return;
  114. pr_info("Enabling secondary cache...\n");
  115. run_uncached(__rm7k_sc_enable);
  116. if (rm7k_tcache_init)
  117. rm7k_tc_enable();
  118. }
  119. static void rm7k_tc_disable(void)
  120. {
  121. unsigned long flags;
  122. local_irq_save(flags);
  123. blast_rm7k_tcache();
  124. clear_c0_config(RM7K_CONF_TE);
  125. local_irq_restore(flags);
  126. }
  127. static void rm7k_sc_disable(void)
  128. {
  129. clear_c0_config(RM7K_CONF_SE);
  130. if (rm7k_tcache_init)
  131. rm7k_tc_disable();
  132. }
  133. static struct bcache_ops rm7k_sc_ops = {
  134. .bc_enable = rm7k_sc_enable,
  135. .bc_disable = rm7k_sc_disable,
  136. .bc_wback_inv = rm7k_sc_wback_inv,
  137. .bc_inv = rm7k_sc_inv
  138. };
  139. /*
  140. * This is a probing function like the one found in c-r4k.c, we look for the
  141. * wrap around point with different addresses.
  142. */
  143. static void __probe_tcache(void)
  144. {
  145. unsigned long flags, addr, begin, end, pow2;
  146. begin = (unsigned long) &_stext;
  147. begin &= ~((8 * 1024 * 1024) - 1);
  148. end = begin + (8 * 1024 * 1024);
  149. local_irq_save(flags);
  150. set_c0_config(RM7K_CONF_TE);
  151. /* Fill size-multiple lines with a valid tag */
  152. pow2 = (256 * 1024);
  153. for (addr = begin; addr <= end; addr = (begin + pow2)) {
  154. unsigned long *p = (unsigned long *) addr;
  155. __asm__ __volatile__("nop" : : "r" (*p));
  156. pow2 <<= 1;
  157. }
  158. /* Load first line with a 0 tag, to check after */
  159. write_c0_taglo(0);
  160. write_c0_taghi(0);
  161. cache_op(Index_Store_Tag_T, begin);
  162. /* Look for the wrap-around */
  163. pow2 = (512 * 1024);
  164. for (addr = begin + (512 * 1024); addr <= end; addr = begin + pow2) {
  165. cache_op(Index_Load_Tag_T, addr);
  166. if (!read_c0_taglo())
  167. break;
  168. pow2 <<= 1;
  169. }
  170. addr -= begin;
  171. tcache_size = addr;
  172. clear_c0_config(RM7K_CONF_TE);
  173. local_irq_restore(flags);
  174. }
  175. void rm7k_sc_init(void)
  176. {
  177. struct cpuinfo_mips *c = &current_cpu_data;
  178. unsigned int config = read_c0_config();
  179. if ((config & RM7K_CONF_SC))
  180. return;
  181. c->scache.linesz = sc_lsize;
  182. c->scache.ways = 4;
  183. c->scache.waybit= __ffs(scache_size / c->scache.ways);
  184. c->scache.waysize = scache_size / c->scache.ways;
  185. c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
  186. printk(KERN_INFO "Secondary cache size %dK, linesize %d bytes.\n",
  187. (scache_size >> 10), sc_lsize);
  188. if (!(config & RM7K_CONF_SE))
  189. rm7k_sc_enable();
  190. bcops = &rm7k_sc_ops;
  191. /*
  192. * While we're at it let's deal with the tertiary cache.
  193. */
  194. rm7k_tcache_init = 0;
  195. tcache_size = 0;
  196. if (config & RM7K_CONF_TC)
  197. return;
  198. /*
  199. * No efficient way to ask the hardware for the size of the tcache,
  200. * so must probe for it.
  201. */
  202. run_uncached(__probe_tcache);
  203. rm7k_tc_enable();
  204. rm7k_tcache_init = 1;
  205. c->tcache.linesz = tc_lsize;
  206. c->tcache.ways = 1;
  207. pr_info("Tertiary cache size %ldK.\n", (tcache_size >> 10));
  208. }