sc-rm7k.c 5.4 KB

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