cache-feroceon-l2.c 8.1 KB

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