cache-feroceon-l2.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 <asm/kmap_types.h>
  17. #include <asm/fixmap.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/tlbflush.h>
  20. #include <plat/cache-feroceon-l2.h>
  21. #include "mm.h"
  22. /*
  23. * Low-level cache maintenance operations.
  24. *
  25. * As well as the regular 'clean/invalidate/flush L2 cache line by
  26. * MVA' instructions, the Feroceon L2 cache controller also features
  27. * 'clean/invalidate L2 range by MVA' operations.
  28. *
  29. * Cache range operations are initiated by writing the start and
  30. * end addresses to successive cp15 registers, and process every
  31. * cache line whose first byte address lies in the inclusive range
  32. * [start:end].
  33. *
  34. * The cache range operations stall the CPU pipeline until completion.
  35. *
  36. * The range operations require two successive cp15 writes, in
  37. * between which we don't want to be preempted.
  38. */
  39. static inline unsigned long l2_start_va(unsigned long paddr)
  40. {
  41. #ifdef CONFIG_HIGHMEM
  42. /*
  43. * Let's do our own fixmap stuff in a minimal way here.
  44. * Because range ops can't be done on physical addresses,
  45. * we simply install a virtual mapping for it only for the
  46. * TLB lookup to occur, hence no need to flush the untouched
  47. * memory mapping. This is protected with the disabling of
  48. * interrupts by the caller.
  49. */
  50. unsigned long idx = KM_L2_CACHE + KM_TYPE_NR * smp_processor_id();
  51. unsigned long vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  52. set_pte_ext(TOP_PTE(vaddr), pfn_pte(paddr >> PAGE_SHIFT, PAGE_KERNEL), 0);
  53. local_flush_tlb_kernel_page(vaddr);
  54. return vaddr + (paddr & ~PAGE_MASK);
  55. #else
  56. return __phys_to_virt(paddr);
  57. #endif
  58. }
  59. static inline void l2_clean_pa(unsigned long addr)
  60. {
  61. __asm__("mcr p15, 1, %0, c15, c9, 3" : : "r" (addr));
  62. }
  63. static inline void l2_clean_pa_range(unsigned long start, unsigned long end)
  64. {
  65. unsigned long va_start, va_end, flags;
  66. /*
  67. * Make sure 'start' and 'end' reference the same page, as
  68. * L2 is PIPT and range operations only do a TLB lookup on
  69. * the start address.
  70. */
  71. BUG_ON((start ^ end) >> PAGE_SHIFT);
  72. raw_local_irq_save(flags);
  73. va_start = l2_start_va(start);
  74. va_end = va_start + (end - start);
  75. __asm__("mcr p15, 1, %0, c15, c9, 4\n\t"
  76. "mcr p15, 1, %1, c15, c9, 5"
  77. : : "r" (va_start), "r" (va_end));
  78. raw_local_irq_restore(flags);
  79. }
  80. static inline void l2_clean_inv_pa(unsigned long addr)
  81. {
  82. __asm__("mcr p15, 1, %0, c15, c10, 3" : : "r" (addr));
  83. }
  84. static inline void l2_inv_pa(unsigned long addr)
  85. {
  86. __asm__("mcr p15, 1, %0, c15, c11, 3" : : "r" (addr));
  87. }
  88. static inline void l2_inv_pa_range(unsigned long start, unsigned long end)
  89. {
  90. unsigned long va_start, va_end, flags;
  91. /*
  92. * Make sure 'start' and 'end' reference the same page, as
  93. * L2 is PIPT and range operations only do a TLB lookup on
  94. * the start address.
  95. */
  96. BUG_ON((start ^ end) >> PAGE_SHIFT);
  97. raw_local_irq_save(flags);
  98. va_start = l2_start_va(start);
  99. va_end = va_start + (end - start);
  100. __asm__("mcr p15, 1, %0, c15, c11, 4\n\t"
  101. "mcr p15, 1, %1, c15, c11, 5"
  102. : : "r" (va_start), "r" (va_end));
  103. raw_local_irq_restore(flags);
  104. }
  105. /*
  106. * Linux primitives.
  107. *
  108. * Note that the end addresses passed to Linux primitives are
  109. * noninclusive, while the hardware cache range operations use
  110. * inclusive start and end addresses.
  111. */
  112. #define CACHE_LINE_SIZE 32
  113. #define MAX_RANGE_SIZE 1024
  114. static int l2_wt_override;
  115. static unsigned long calc_range_end(unsigned long start, unsigned long end)
  116. {
  117. unsigned long range_end;
  118. BUG_ON(start & (CACHE_LINE_SIZE - 1));
  119. BUG_ON(end & (CACHE_LINE_SIZE - 1));
  120. /*
  121. * Try to process all cache lines between 'start' and 'end'.
  122. */
  123. range_end = end;
  124. /*
  125. * Limit the number of cache lines processed at once,
  126. * since cache range operations stall the CPU pipeline
  127. * until completion.
  128. */
  129. if (range_end > start + MAX_RANGE_SIZE)
  130. range_end = start + MAX_RANGE_SIZE;
  131. /*
  132. * Cache range operations can't straddle a page boundary.
  133. */
  134. if (range_end > (start | (PAGE_SIZE - 1)) + 1)
  135. range_end = (start | (PAGE_SIZE - 1)) + 1;
  136. return range_end;
  137. }
  138. static void feroceon_l2_inv_range(unsigned long start, unsigned long end)
  139. {
  140. /*
  141. * Clean and invalidate partial first cache line.
  142. */
  143. if (start & (CACHE_LINE_SIZE - 1)) {
  144. l2_clean_inv_pa(start & ~(CACHE_LINE_SIZE - 1));
  145. start = (start | (CACHE_LINE_SIZE - 1)) + 1;
  146. }
  147. /*
  148. * Clean and invalidate partial last cache line.
  149. */
  150. if (start < end && end & (CACHE_LINE_SIZE - 1)) {
  151. l2_clean_inv_pa(end & ~(CACHE_LINE_SIZE - 1));
  152. end &= ~(CACHE_LINE_SIZE - 1);
  153. }
  154. /*
  155. * Invalidate all full cache lines between 'start' and 'end'.
  156. */
  157. while (start < end) {
  158. unsigned long range_end = calc_range_end(start, end);
  159. l2_inv_pa_range(start, range_end - CACHE_LINE_SIZE);
  160. start = range_end;
  161. }
  162. dsb();
  163. }
  164. static void feroceon_l2_clean_range(unsigned long start, unsigned long end)
  165. {
  166. /*
  167. * If L2 is forced to WT, the L2 will always be clean and we
  168. * don't need to do anything here.
  169. */
  170. if (!l2_wt_override) {
  171. start &= ~(CACHE_LINE_SIZE - 1);
  172. end = (end + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1);
  173. while (start != end) {
  174. unsigned long range_end = calc_range_end(start, end);
  175. l2_clean_pa_range(start, range_end - CACHE_LINE_SIZE);
  176. start = range_end;
  177. }
  178. }
  179. dsb();
  180. }
  181. static void feroceon_l2_flush_range(unsigned long start, unsigned long end)
  182. {
  183. start &= ~(CACHE_LINE_SIZE - 1);
  184. end = (end + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1);
  185. while (start != end) {
  186. unsigned long range_end = calc_range_end(start, end);
  187. if (!l2_wt_override)
  188. l2_clean_pa_range(start, range_end - CACHE_LINE_SIZE);
  189. l2_inv_pa_range(start, range_end - CACHE_LINE_SIZE);
  190. start = range_end;
  191. }
  192. dsb();
  193. }
  194. /*
  195. * Routines to disable and re-enable the D-cache and I-cache at run
  196. * time. These are necessary because the L2 cache can only be enabled
  197. * or disabled while the L1 Dcache and Icache are both disabled.
  198. */
  199. static int __init flush_and_disable_dcache(void)
  200. {
  201. u32 cr;
  202. cr = get_cr();
  203. if (cr & CR_C) {
  204. unsigned long flags;
  205. raw_local_irq_save(flags);
  206. flush_cache_all();
  207. set_cr(cr & ~CR_C);
  208. raw_local_irq_restore(flags);
  209. return 1;
  210. }
  211. return 0;
  212. }
  213. static void __init enable_dcache(void)
  214. {
  215. u32 cr;
  216. cr = get_cr();
  217. set_cr(cr | CR_C);
  218. }
  219. static void __init __invalidate_icache(void)
  220. {
  221. int dummy;
  222. __asm__ __volatile__("mcr p15, 0, %0, c7, c5, 0" : "=r" (dummy));
  223. }
  224. static int __init invalidate_and_disable_icache(void)
  225. {
  226. u32 cr;
  227. cr = get_cr();
  228. if (cr & CR_I) {
  229. set_cr(cr & ~CR_I);
  230. __invalidate_icache();
  231. return 1;
  232. }
  233. return 0;
  234. }
  235. static void __init enable_icache(void)
  236. {
  237. u32 cr;
  238. cr = get_cr();
  239. set_cr(cr | CR_I);
  240. }
  241. static inline u32 read_extra_features(void)
  242. {
  243. u32 u;
  244. __asm__("mrc p15, 1, %0, c15, c1, 0" : "=r" (u));
  245. return u;
  246. }
  247. static inline void write_extra_features(u32 u)
  248. {
  249. __asm__("mcr p15, 1, %0, c15, c1, 0" : : "r" (u));
  250. }
  251. static void __init disable_l2_prefetch(void)
  252. {
  253. u32 u;
  254. /*
  255. * Read the CPU Extra Features register and verify that the
  256. * Disable L2 Prefetch bit is set.
  257. */
  258. u = read_extra_features();
  259. if (!(u & 0x01000000)) {
  260. printk(KERN_INFO "Feroceon L2: Disabling L2 prefetch.\n");
  261. write_extra_features(u | 0x01000000);
  262. }
  263. }
  264. static void __init enable_l2(void)
  265. {
  266. u32 u;
  267. u = read_extra_features();
  268. if (!(u & 0x00400000)) {
  269. int i, d;
  270. printk(KERN_INFO "Feroceon L2: Enabling L2\n");
  271. d = flush_and_disable_dcache();
  272. i = invalidate_and_disable_icache();
  273. write_extra_features(u | 0x00400000);
  274. if (i)
  275. enable_icache();
  276. if (d)
  277. enable_dcache();
  278. }
  279. }
  280. void __init feroceon_l2_init(int __l2_wt_override)
  281. {
  282. l2_wt_override = __l2_wt_override;
  283. disable_l2_prefetch();
  284. outer_cache.inv_range = feroceon_l2_inv_range;
  285. outer_cache.clean_range = feroceon_l2_clean_range;
  286. outer_cache.flush_range = feroceon_l2_flush_range;
  287. enable_l2();
  288. printk(KERN_INFO "Feroceon L2: Cache support initialised%s.\n",
  289. l2_wt_override ? ", in WT override mode" : "");
  290. }