cache-feroceon-l2.c 8.9 KB

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