cache.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/sh/mm/cache.c
  4. *
  5. * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
  6. * Copyright (C) 2002 - 2010 Paul Mundt
  7. */
  8. #include <linux/mm.h>
  9. #include <linux/init.h>
  10. #include <linux/mutex.h>
  11. #include <linux/fs.h>
  12. #include <linux/smp.h>
  13. #include <linux/highmem.h>
  14. #include <linux/module.h>
  15. #include <asm/mmu_context.h>
  16. #include <asm/cacheflush.h>
  17. void (*local_flush_cache_all)(void *args) = cache_noop;
  18. void (*local_flush_cache_mm)(void *args) = cache_noop;
  19. void (*local_flush_cache_dup_mm)(void *args) = cache_noop;
  20. void (*local_flush_cache_page)(void *args) = cache_noop;
  21. void (*local_flush_cache_range)(void *args) = cache_noop;
  22. void (*local_flush_dcache_page)(void *args) = cache_noop;
  23. void (*local_flush_icache_range)(void *args) = cache_noop;
  24. void (*local_flush_icache_page)(void *args) = cache_noop;
  25. void (*local_flush_cache_sigtramp)(void *args) = cache_noop;
  26. void (*__flush_wback_region)(void *start, int size);
  27. EXPORT_SYMBOL(__flush_wback_region);
  28. void (*__flush_purge_region)(void *start, int size);
  29. EXPORT_SYMBOL(__flush_purge_region);
  30. void (*__flush_invalidate_region)(void *start, int size);
  31. EXPORT_SYMBOL(__flush_invalidate_region);
  32. static inline void noop__flush_region(void *start, int size)
  33. {
  34. }
  35. static inline void cacheop_on_each_cpu(void (*func) (void *info), void *info,
  36. int wait)
  37. {
  38. preempt_disable();
  39. /* Needing IPI for cross-core flush is SHX3-specific. */
  40. #ifdef CONFIG_CPU_SHX3
  41. /*
  42. * It's possible that this gets called early on when IRQs are
  43. * still disabled due to ioremapping by the boot CPU, so don't
  44. * even attempt IPIs unless there are other CPUs online.
  45. */
  46. if (num_online_cpus() > 1)
  47. smp_call_function(func, info, wait);
  48. #endif
  49. func(info);
  50. preempt_enable();
  51. }
  52. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  53. unsigned long vaddr, void *dst, const void *src,
  54. unsigned long len)
  55. {
  56. if (boot_cpu_data.dcache.n_aliases && page_mapcount(page) &&
  57. test_bit(PG_dcache_clean, &page->flags)) {
  58. void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
  59. memcpy(vto, src, len);
  60. kunmap_coherent(vto);
  61. } else {
  62. memcpy(dst, src, len);
  63. if (boot_cpu_data.dcache.n_aliases)
  64. clear_bit(PG_dcache_clean, &page->flags);
  65. }
  66. if (vma->vm_flags & VM_EXEC)
  67. flush_cache_page(vma, vaddr, page_to_pfn(page));
  68. }
  69. void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  70. unsigned long vaddr, void *dst, const void *src,
  71. unsigned long len)
  72. {
  73. if (boot_cpu_data.dcache.n_aliases && page_mapcount(page) &&
  74. test_bit(PG_dcache_clean, &page->flags)) {
  75. void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
  76. memcpy(dst, vfrom, len);
  77. kunmap_coherent(vfrom);
  78. } else {
  79. memcpy(dst, src, len);
  80. if (boot_cpu_data.dcache.n_aliases)
  81. clear_bit(PG_dcache_clean, &page->flags);
  82. }
  83. }
  84. void copy_user_highpage(struct page *to, struct page *from,
  85. unsigned long vaddr, struct vm_area_struct *vma)
  86. {
  87. void *vfrom, *vto;
  88. vto = kmap_atomic(to);
  89. if (boot_cpu_data.dcache.n_aliases && page_mapcount(from) &&
  90. test_bit(PG_dcache_clean, &from->flags)) {
  91. vfrom = kmap_coherent(from, vaddr);
  92. copy_page(vto, vfrom);
  93. kunmap_coherent(vfrom);
  94. } else {
  95. vfrom = kmap_atomic(from);
  96. copy_page(vto, vfrom);
  97. kunmap_atomic(vfrom);
  98. }
  99. if (pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK) ||
  100. (vma->vm_flags & VM_EXEC))
  101. __flush_purge_region(vto, PAGE_SIZE);
  102. kunmap_atomic(vto);
  103. /* Make sure this page is cleared on other CPU's too before using it */
  104. smp_wmb();
  105. }
  106. EXPORT_SYMBOL(copy_user_highpage);
  107. void clear_user_highpage(struct page *page, unsigned long vaddr)
  108. {
  109. void *kaddr = kmap_atomic(page);
  110. clear_page(kaddr);
  111. if (pages_do_alias((unsigned long)kaddr, vaddr & PAGE_MASK))
  112. __flush_purge_region(kaddr, PAGE_SIZE);
  113. kunmap_atomic(kaddr);
  114. }
  115. EXPORT_SYMBOL(clear_user_highpage);
  116. void __update_cache(struct vm_area_struct *vma,
  117. unsigned long address, pte_t pte)
  118. {
  119. struct page *page;
  120. unsigned long pfn = pte_pfn(pte);
  121. if (!boot_cpu_data.dcache.n_aliases)
  122. return;
  123. page = pfn_to_page(pfn);
  124. if (pfn_valid(pfn)) {
  125. int dirty = !test_and_set_bit(PG_dcache_clean, &page->flags);
  126. if (dirty)
  127. __flush_purge_region(page_address(page), PAGE_SIZE);
  128. }
  129. }
  130. void __flush_anon_page(struct page *page, unsigned long vmaddr)
  131. {
  132. unsigned long addr = (unsigned long) page_address(page);
  133. if (pages_do_alias(addr, vmaddr)) {
  134. if (boot_cpu_data.dcache.n_aliases && page_mapcount(page) &&
  135. test_bit(PG_dcache_clean, &page->flags)) {
  136. void *kaddr;
  137. kaddr = kmap_coherent(page, vmaddr);
  138. /* XXX.. For now kunmap_coherent() does a purge */
  139. /* __flush_purge_region((void *)kaddr, PAGE_SIZE); */
  140. kunmap_coherent(kaddr);
  141. } else
  142. __flush_purge_region((void *)addr, PAGE_SIZE);
  143. }
  144. }
  145. void flush_cache_all(void)
  146. {
  147. cacheop_on_each_cpu(local_flush_cache_all, NULL, 1);
  148. }
  149. EXPORT_SYMBOL(flush_cache_all);
  150. void flush_cache_mm(struct mm_struct *mm)
  151. {
  152. if (boot_cpu_data.dcache.n_aliases == 0)
  153. return;
  154. cacheop_on_each_cpu(local_flush_cache_mm, mm, 1);
  155. }
  156. void flush_cache_dup_mm(struct mm_struct *mm)
  157. {
  158. if (boot_cpu_data.dcache.n_aliases == 0)
  159. return;
  160. cacheop_on_each_cpu(local_flush_cache_dup_mm, mm, 1);
  161. }
  162. void flush_cache_page(struct vm_area_struct *vma, unsigned long addr,
  163. unsigned long pfn)
  164. {
  165. struct flusher_data data;
  166. data.vma = vma;
  167. data.addr1 = addr;
  168. data.addr2 = pfn;
  169. cacheop_on_each_cpu(local_flush_cache_page, (void *)&data, 1);
  170. }
  171. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  172. unsigned long end)
  173. {
  174. struct flusher_data data;
  175. data.vma = vma;
  176. data.addr1 = start;
  177. data.addr2 = end;
  178. cacheop_on_each_cpu(local_flush_cache_range, (void *)&data, 1);
  179. }
  180. EXPORT_SYMBOL(flush_cache_range);
  181. void flush_dcache_page(struct page *page)
  182. {
  183. cacheop_on_each_cpu(local_flush_dcache_page, page, 1);
  184. }
  185. EXPORT_SYMBOL(flush_dcache_page);
  186. void flush_icache_range(unsigned long start, unsigned long end)
  187. {
  188. struct flusher_data data;
  189. data.vma = NULL;
  190. data.addr1 = start;
  191. data.addr2 = end;
  192. cacheop_on_each_cpu(local_flush_icache_range, (void *)&data, 1);
  193. }
  194. EXPORT_SYMBOL(flush_icache_range);
  195. void flush_icache_page(struct vm_area_struct *vma, struct page *page)
  196. {
  197. /* Nothing uses the VMA, so just pass the struct page along */
  198. cacheop_on_each_cpu(local_flush_icache_page, page, 1);
  199. }
  200. void flush_cache_sigtramp(unsigned long address)
  201. {
  202. cacheop_on_each_cpu(local_flush_cache_sigtramp, (void *)address, 1);
  203. }
  204. static void compute_alias(struct cache_info *c)
  205. {
  206. #ifdef CONFIG_MMU
  207. c->alias_mask = ((c->sets - 1) << c->entry_shift) & ~(PAGE_SIZE - 1);
  208. #else
  209. c->alias_mask = 0;
  210. #endif
  211. c->n_aliases = c->alias_mask ? (c->alias_mask >> PAGE_SHIFT) + 1 : 0;
  212. }
  213. static void __init emit_cache_params(void)
  214. {
  215. printk(KERN_NOTICE "I-cache : n_ways=%d n_sets=%d way_incr=%d\n",
  216. boot_cpu_data.icache.ways,
  217. boot_cpu_data.icache.sets,
  218. boot_cpu_data.icache.way_incr);
  219. printk(KERN_NOTICE "I-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
  220. boot_cpu_data.icache.entry_mask,
  221. boot_cpu_data.icache.alias_mask,
  222. boot_cpu_data.icache.n_aliases);
  223. printk(KERN_NOTICE "D-cache : n_ways=%d n_sets=%d way_incr=%d\n",
  224. boot_cpu_data.dcache.ways,
  225. boot_cpu_data.dcache.sets,
  226. boot_cpu_data.dcache.way_incr);
  227. printk(KERN_NOTICE "D-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
  228. boot_cpu_data.dcache.entry_mask,
  229. boot_cpu_data.dcache.alias_mask,
  230. boot_cpu_data.dcache.n_aliases);
  231. /*
  232. * Emit Secondary Cache parameters if the CPU has a probed L2.
  233. */
  234. if (boot_cpu_data.flags & CPU_HAS_L2_CACHE) {
  235. printk(KERN_NOTICE "S-cache : n_ways=%d n_sets=%d way_incr=%d\n",
  236. boot_cpu_data.scache.ways,
  237. boot_cpu_data.scache.sets,
  238. boot_cpu_data.scache.way_incr);
  239. printk(KERN_NOTICE "S-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
  240. boot_cpu_data.scache.entry_mask,
  241. boot_cpu_data.scache.alias_mask,
  242. boot_cpu_data.scache.n_aliases);
  243. }
  244. }
  245. void __init cpu_cache_init(void)
  246. {
  247. unsigned int cache_disabled = 0;
  248. #ifdef SH_CCR
  249. cache_disabled = !(__raw_readl(SH_CCR) & CCR_CACHE_ENABLE);
  250. #endif
  251. compute_alias(&boot_cpu_data.icache);
  252. compute_alias(&boot_cpu_data.dcache);
  253. compute_alias(&boot_cpu_data.scache);
  254. __flush_wback_region = noop__flush_region;
  255. __flush_purge_region = noop__flush_region;
  256. __flush_invalidate_region = noop__flush_region;
  257. /*
  258. * No flushing is necessary in the disabled cache case so we can
  259. * just keep the noop functions in local_flush_..() and __flush_..()
  260. */
  261. if (unlikely(cache_disabled))
  262. goto skip;
  263. if (boot_cpu_data.type == CPU_J2) {
  264. extern void __weak j2_cache_init(void);
  265. j2_cache_init();
  266. } else if (boot_cpu_data.family == CPU_FAMILY_SH2) {
  267. extern void __weak sh2_cache_init(void);
  268. sh2_cache_init();
  269. }
  270. if (boot_cpu_data.family == CPU_FAMILY_SH2A) {
  271. extern void __weak sh2a_cache_init(void);
  272. sh2a_cache_init();
  273. }
  274. if (boot_cpu_data.family == CPU_FAMILY_SH3) {
  275. extern void __weak sh3_cache_init(void);
  276. sh3_cache_init();
  277. if ((boot_cpu_data.type == CPU_SH7705) &&
  278. (boot_cpu_data.dcache.sets == 512)) {
  279. extern void __weak sh7705_cache_init(void);
  280. sh7705_cache_init();
  281. }
  282. }
  283. if ((boot_cpu_data.family == CPU_FAMILY_SH4) ||
  284. (boot_cpu_data.family == CPU_FAMILY_SH4A) ||
  285. (boot_cpu_data.family == CPU_FAMILY_SH4AL_DSP)) {
  286. extern void __weak sh4_cache_init(void);
  287. sh4_cache_init();
  288. if ((boot_cpu_data.type == CPU_SH7786) ||
  289. (boot_cpu_data.type == CPU_SHX3)) {
  290. extern void __weak shx3_cache_init(void);
  291. shx3_cache_init();
  292. }
  293. }
  294. skip:
  295. emit_cache_params();
  296. }