cache-feroceon-l2.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * arch/arm/mm/cache-feroceon-l2.c - Feroceon L2 cache controller support
  3. *
  4. * Copyright (C) 2008 Marvell Semiconductor
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. *
  10. * References:
  11. * - Unified Layer 2 Cache for Feroceon CPU Cores,
  12. * Document ID MV-S104858-00, Rev. A, October 23 2007.
  13. */
  14. #include <linux/init.h>
  15. #include <asm/cacheflush.h>
  16. #include <plat/cache-feroceon-l2.h>
  17. /*
  18. * Low-level cache maintenance operations.
  19. *
  20. * As well as the regular 'clean/invalidate/flush L2 cache line by
  21. * MVA' instructions, the Feroceon L2 cache controller also features
  22. * 'clean/invalidate L2 range by MVA' operations.
  23. *
  24. * Cache range operations are initiated by writing the start and
  25. * end addresses to successive cp15 registers, and process every
  26. * cache line whose first byte address lies in the inclusive range
  27. * [start:end].
  28. *
  29. * The cache range operations stall the CPU pipeline until completion.
  30. *
  31. * The range operations require two successive cp15 writes, in
  32. * between which we don't want to be preempted.
  33. */
  34. static inline void l2_clean_pa(unsigned long addr)
  35. {
  36. __asm__("mcr p15, 1, %0, c15, c9, 3" : : "r" (addr));
  37. }
  38. static inline void l2_clean_mva_range(unsigned long start, unsigned long end)
  39. {
  40. unsigned long flags;
  41. /*
  42. * Make sure 'start' and 'end' reference the same page, as
  43. * L2 is PIPT and range operations only do a TLB lookup on
  44. * the start address.
  45. */
  46. BUG_ON((start ^ end) >> PAGE_SHIFT);
  47. raw_local_irq_save(flags);
  48. __asm__("mcr p15, 1, %0, c15, c9, 4\n\t"
  49. "mcr p15, 1, %1, c15, c9, 5"
  50. : : "r" (start), "r" (end));
  51. raw_local_irq_restore(flags);
  52. }
  53. static inline void l2_clean_pa_range(unsigned long start, unsigned long end)
  54. {
  55. l2_clean_mva_range(__phys_to_virt(start), __phys_to_virt(end));
  56. }
  57. static inline void l2_clean_inv_pa(unsigned long addr)
  58. {
  59. __asm__("mcr p15, 1, %0, c15, c10, 3" : : "r" (addr));
  60. }
  61. static inline void l2_inv_pa(unsigned long addr)
  62. {
  63. __asm__("mcr p15, 1, %0, c15, c11, 3" : : "r" (addr));
  64. }
  65. static inline void l2_inv_mva_range(unsigned long start, unsigned long end)
  66. {
  67. unsigned long flags;
  68. /*
  69. * Make sure 'start' and 'end' reference the same page, as
  70. * L2 is PIPT and range operations only do a TLB lookup on
  71. * the start address.
  72. */
  73. BUG_ON((start ^ end) >> PAGE_SHIFT);
  74. raw_local_irq_save(flags);
  75. __asm__("mcr p15, 1, %0, c15, c11, 4\n\t"
  76. "mcr p15, 1, %1, c15, c11, 5"
  77. : : "r" (start), "r" (end));
  78. raw_local_irq_restore(flags);
  79. }
  80. static inline void l2_inv_pa_range(unsigned long start, unsigned long end)
  81. {
  82. l2_inv_mva_range(__phys_to_virt(start), __phys_to_virt(end));
  83. }
  84. /*
  85. * Linux primitives.
  86. *
  87. * Note that the end addresses passed to Linux primitives are
  88. * noninclusive, while the hardware cache range operations use
  89. * inclusive start and end addresses.
  90. */
  91. #define CACHE_LINE_SIZE 32
  92. #define MAX_RANGE_SIZE 1024
  93. static int l2_wt_override;
  94. static unsigned long calc_range_end(unsigned long start, unsigned long end)
  95. {
  96. unsigned long range_end;
  97. BUG_ON(start & (CACHE_LINE_SIZE - 1));
  98. BUG_ON(end & (CACHE_LINE_SIZE - 1));
  99. /*
  100. * Try to process all cache lines between 'start' and 'end'.
  101. */
  102. range_end = end;
  103. /*
  104. * Limit the number of cache lines processed at once,
  105. * since cache range operations stall the CPU pipeline
  106. * until completion.
  107. */
  108. if (range_end > start + MAX_RANGE_SIZE)
  109. range_end = start + MAX_RANGE_SIZE;
  110. /*
  111. * Cache range operations can't straddle a page boundary.
  112. */
  113. if (range_end > (start | (PAGE_SIZE - 1)) + 1)
  114. range_end = (start | (PAGE_SIZE - 1)) + 1;
  115. return range_end;
  116. }
  117. static void feroceon_l2_inv_range(unsigned long start, unsigned long end)
  118. {
  119. /*
  120. * Clean and invalidate partial first cache line.
  121. */
  122. if (start & (CACHE_LINE_SIZE - 1)) {
  123. l2_clean_inv_pa(start & ~(CACHE_LINE_SIZE - 1));
  124. start = (start | (CACHE_LINE_SIZE - 1)) + 1;
  125. }
  126. /*
  127. * Clean and invalidate partial last cache line.
  128. */
  129. if (end & (CACHE_LINE_SIZE - 1)) {
  130. l2_clean_inv_pa(end & ~(CACHE_LINE_SIZE - 1));
  131. end &= ~(CACHE_LINE_SIZE - 1);
  132. }
  133. /*
  134. * Invalidate all full cache lines between 'start' and 'end'.
  135. */
  136. while (start != end) {
  137. unsigned long range_end = calc_range_end(start, end);
  138. l2_inv_pa_range(start, range_end - CACHE_LINE_SIZE);
  139. start = range_end;
  140. }
  141. dsb();
  142. }
  143. static void feroceon_l2_clean_range(unsigned long start, unsigned long end)
  144. {
  145. /*
  146. * If L2 is forced to WT, the L2 will always be clean and we
  147. * don't need to do anything here.
  148. */
  149. if (!l2_wt_override) {
  150. start &= ~(CACHE_LINE_SIZE - 1);
  151. end = (end + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1);
  152. while (start != end) {
  153. unsigned long range_end = calc_range_end(start, end);
  154. l2_clean_pa_range(start, range_end - CACHE_LINE_SIZE);
  155. start = range_end;
  156. }
  157. }
  158. dsb();
  159. }
  160. static void feroceon_l2_flush_range(unsigned long start, unsigned long end)
  161. {
  162. start &= ~(CACHE_LINE_SIZE - 1);
  163. end = (end + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1);
  164. while (start != end) {
  165. unsigned long range_end = calc_range_end(start, end);
  166. if (!l2_wt_override)
  167. l2_clean_pa_range(start, range_end - CACHE_LINE_SIZE);
  168. l2_inv_pa_range(start, range_end - CACHE_LINE_SIZE);
  169. start = range_end;
  170. }
  171. dsb();
  172. }
  173. /*
  174. * Routines to disable and re-enable the D-cache and I-cache at run
  175. * time. These are necessary because the L2 cache can only be enabled
  176. * or disabled while the L1 Dcache and Icache are both disabled.
  177. */
  178. static int __init flush_and_disable_dcache(void)
  179. {
  180. u32 cr;
  181. cr = get_cr();
  182. if (cr & CR_C) {
  183. unsigned long flags;
  184. raw_local_irq_save(flags);
  185. flush_cache_all();
  186. set_cr(cr & ~CR_C);
  187. raw_local_irq_restore(flags);
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. static void __init enable_dcache(void)
  193. {
  194. u32 cr;
  195. cr = get_cr();
  196. set_cr(cr | CR_C);
  197. }
  198. static void __init __invalidate_icache(void)
  199. {
  200. int dummy;
  201. __asm__ __volatile__("mcr p15, 0, %0, c7, c5, 0" : "=r" (dummy));
  202. }
  203. static int __init invalidate_and_disable_icache(void)
  204. {
  205. u32 cr;
  206. cr = get_cr();
  207. if (cr & CR_I) {
  208. set_cr(cr & ~CR_I);
  209. __invalidate_icache();
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. static void __init enable_icache(void)
  215. {
  216. u32 cr;
  217. cr = get_cr();
  218. set_cr(cr | CR_I);
  219. }
  220. static inline u32 read_extra_features(void)
  221. {
  222. u32 u;
  223. __asm__("mrc p15, 1, %0, c15, c1, 0" : "=r" (u));
  224. return u;
  225. }
  226. static inline void write_extra_features(u32 u)
  227. {
  228. __asm__("mcr p15, 1, %0, c15, c1, 0" : : "r" (u));
  229. }
  230. static void __init disable_l2_prefetch(void)
  231. {
  232. u32 u;
  233. /*
  234. * Read the CPU Extra Features register and verify that the
  235. * Disable L2 Prefetch bit is set.
  236. */
  237. u = read_extra_features();
  238. if (!(u & 0x01000000)) {
  239. printk(KERN_INFO "Feroceon L2: Disabling L2 prefetch.\n");
  240. write_extra_features(u | 0x01000000);
  241. }
  242. }
  243. static void __init enable_l2(void)
  244. {
  245. u32 u;
  246. u = read_extra_features();
  247. if (!(u & 0x00400000)) {
  248. int i, d;
  249. printk(KERN_INFO "Feroceon L2: Enabling L2\n");
  250. d = flush_and_disable_dcache();
  251. i = invalidate_and_disable_icache();
  252. write_extra_features(u | 0x00400000);
  253. if (i)
  254. enable_icache();
  255. if (d)
  256. enable_dcache();
  257. }
  258. }
  259. void __init feroceon_l2_init(int __l2_wt_override)
  260. {
  261. l2_wt_override = __l2_wt_override;
  262. disable_l2_prefetch();
  263. outer_cache.inv_range = feroceon_l2_inv_range;
  264. outer_cache.clean_range = feroceon_l2_clean_range;
  265. outer_cache.flush_range = feroceon_l2_flush_range;
  266. enable_l2();
  267. printk(KERN_INFO "Feroceon L2: Cache support initialised%s.\n",
  268. l2_wt_override ? ", in WT override mode" : "");
  269. }