cache-feroceon-l2.c 9.0 KB

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