cache-feroceon-l2.c 8.1 KB

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