cache-sh4.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * arch/sh/mm/cache-sh4.c
  3. *
  4. * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
  5. * Copyright (C) 2001 - 2009 Paul Mundt
  6. * Copyright (C) 2003 Richard Curnow
  7. * Copyright (c) 2007 STMicroelectronics (R&D) Ltd.
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/mm.h>
  15. #include <linux/io.h>
  16. #include <linux/mutex.h>
  17. #include <linux/fs.h>
  18. #include <linux/highmem.h>
  19. #include <linux/pagemap.h>
  20. #include <asm/mmu_context.h>
  21. #include <asm/cache_insns.h>
  22. #include <asm/cacheflush.h>
  23. /*
  24. * The maximum number of pages we support up to when doing ranged dcache
  25. * flushing. Anything exceeding this will simply flush the dcache in its
  26. * entirety.
  27. */
  28. #define MAX_ICACHE_PAGES 32
  29. static void __flush_cache_one(unsigned long addr, unsigned long phys,
  30. unsigned long exec_offset);
  31. /*
  32. * Write back the range of D-cache, and purge the I-cache.
  33. *
  34. * Called from kernel/module.c:sys_init_module and routine for a.out format,
  35. * signal handler code and kprobes code
  36. */
  37. static void sh4_flush_icache_range(void *args)
  38. {
  39. struct flusher_data *data = args;
  40. unsigned long start, end;
  41. unsigned long flags, v;
  42. int i;
  43. start = data->addr1;
  44. end = data->addr2;
  45. /* If there are too many pages then just blow away the caches */
  46. if (((end - start) >> PAGE_SHIFT) >= MAX_ICACHE_PAGES) {
  47. local_flush_cache_all(NULL);
  48. return;
  49. }
  50. /*
  51. * Selectively flush d-cache then invalidate the i-cache.
  52. * This is inefficient, so only use this for small ranges.
  53. */
  54. start &= ~(L1_CACHE_BYTES-1);
  55. end += L1_CACHE_BYTES-1;
  56. end &= ~(L1_CACHE_BYTES-1);
  57. local_irq_save(flags);
  58. jump_to_uncached();
  59. for (v = start; v < end; v += L1_CACHE_BYTES) {
  60. unsigned long icacheaddr;
  61. int j, n;
  62. __ocbwb(v);
  63. icacheaddr = CACHE_IC_ADDRESS_ARRAY | (v &
  64. cpu_data->icache.entry_mask);
  65. /* Clear i-cache line valid-bit */
  66. n = boot_cpu_data.icache.n_aliases;
  67. for (i = 0; i < cpu_data->icache.ways; i++) {
  68. for (j = 0; j < n; j++)
  69. __raw_writel(0, icacheaddr + (j * PAGE_SIZE));
  70. icacheaddr += cpu_data->icache.way_incr;
  71. }
  72. }
  73. back_to_cached();
  74. local_irq_restore(flags);
  75. }
  76. static inline void flush_cache_one(unsigned long start, unsigned long phys)
  77. {
  78. unsigned long flags, exec_offset = 0;
  79. /*
  80. * All types of SH-4 require PC to be uncached to operate on the I-cache.
  81. * Some types of SH-4 require PC to be uncached to operate on the D-cache.
  82. */
  83. if ((boot_cpu_data.flags & CPU_HAS_P2_FLUSH_BUG) ||
  84. (start < CACHE_OC_ADDRESS_ARRAY))
  85. exec_offset = cached_to_uncached;
  86. local_irq_save(flags);
  87. __flush_cache_one(start, phys, exec_offset);
  88. local_irq_restore(flags);
  89. }
  90. /*
  91. * Write back & invalidate the D-cache of the page.
  92. * (To avoid "alias" issues)
  93. */
  94. static void sh4_flush_dcache_folio(void *arg)
  95. {
  96. struct folio *folio = arg;
  97. #ifndef CONFIG_SMP
  98. struct address_space *mapping = folio_flush_mapping(folio);
  99. if (mapping && !mapping_mapped(mapping))
  100. clear_bit(PG_dcache_clean, &folio->flags);
  101. else
  102. #endif
  103. {
  104. unsigned long pfn = folio_pfn(folio);
  105. unsigned long addr = (unsigned long)folio_address(folio);
  106. unsigned int i, nr = folio_nr_pages(folio);
  107. for (i = 0; i < nr; i++) {
  108. flush_cache_one(CACHE_OC_ADDRESS_ARRAY |
  109. (addr & shm_align_mask),
  110. pfn * PAGE_SIZE);
  111. addr += PAGE_SIZE;
  112. pfn++;
  113. }
  114. }
  115. wmb();
  116. }
  117. /* TODO: Selective icache invalidation through IC address array.. */
  118. static void flush_icache_all(void)
  119. {
  120. unsigned long flags, ccr;
  121. local_irq_save(flags);
  122. jump_to_uncached();
  123. /* Flush I-cache */
  124. ccr = __raw_readl(SH_CCR);
  125. ccr |= CCR_CACHE_ICI;
  126. __raw_writel(ccr, SH_CCR);
  127. /*
  128. * back_to_cached() will take care of the barrier for us, don't add
  129. * another one!
  130. */
  131. back_to_cached();
  132. local_irq_restore(flags);
  133. }
  134. static void flush_dcache_all(void)
  135. {
  136. unsigned long addr, end_addr, entry_offset;
  137. end_addr = CACHE_OC_ADDRESS_ARRAY +
  138. (current_cpu_data.dcache.sets <<
  139. current_cpu_data.dcache.entry_shift) *
  140. current_cpu_data.dcache.ways;
  141. entry_offset = 1 << current_cpu_data.dcache.entry_shift;
  142. for (addr = CACHE_OC_ADDRESS_ARRAY; addr < end_addr; ) {
  143. __raw_writel(0, addr); addr += entry_offset;
  144. __raw_writel(0, addr); addr += entry_offset;
  145. __raw_writel(0, addr); addr += entry_offset;
  146. __raw_writel(0, addr); addr += entry_offset;
  147. __raw_writel(0, addr); addr += entry_offset;
  148. __raw_writel(0, addr); addr += entry_offset;
  149. __raw_writel(0, addr); addr += entry_offset;
  150. __raw_writel(0, addr); addr += entry_offset;
  151. }
  152. }
  153. static void sh4_flush_cache_all(void *unused)
  154. {
  155. flush_dcache_all();
  156. flush_icache_all();
  157. }
  158. /*
  159. * Note : (RPC) since the caches are physically tagged, the only point
  160. * of flush_cache_mm for SH-4 is to get rid of aliases from the
  161. * D-cache. The assumption elsewhere, e.g. flush_cache_range, is that
  162. * lines can stay resident so long as the virtual address they were
  163. * accessed with (hence cache set) is in accord with the physical
  164. * address (i.e. tag). It's no different here.
  165. *
  166. * Caller takes mm->mmap_lock.
  167. */
  168. static void sh4_flush_cache_mm(void *arg)
  169. {
  170. struct mm_struct *mm = arg;
  171. if (cpu_context(smp_processor_id(), mm) == NO_CONTEXT)
  172. return;
  173. flush_dcache_all();
  174. }
  175. /*
  176. * Write back and invalidate I/D-caches for the page.
  177. *
  178. * ADDR: Virtual Address (U0 address)
  179. * PFN: Physical page number
  180. */
  181. static void sh4_flush_cache_page(void *args)
  182. {
  183. struct flusher_data *data = args;
  184. struct vm_area_struct *vma;
  185. struct page *page;
  186. unsigned long address, pfn, phys;
  187. int map_coherent = 0;
  188. pmd_t *pmd;
  189. pte_t *pte;
  190. void *vaddr;
  191. vma = data->vma;
  192. address = data->addr1 & PAGE_MASK;
  193. pfn = data->addr2;
  194. phys = pfn << PAGE_SHIFT;
  195. page = pfn_to_page(pfn);
  196. if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT)
  197. return;
  198. pmd = pmd_off(vma->vm_mm, address);
  199. pte = pte_offset_kernel(pmd, address);
  200. /* If the page isn't present, there is nothing to do here. */
  201. if (!(pte_val(*pte) & _PAGE_PRESENT))
  202. return;
  203. if ((vma->vm_mm == current->active_mm))
  204. vaddr = NULL;
  205. else {
  206. struct folio *folio = page_folio(page);
  207. /*
  208. * Use kmap_coherent or kmap_atomic to do flushes for
  209. * another ASID than the current one.
  210. */
  211. map_coherent = (current_cpu_data.dcache.n_aliases &&
  212. test_bit(PG_dcache_clean, folio_flags(folio, 0)) &&
  213. page_mapped(page));
  214. if (map_coherent)
  215. vaddr = kmap_coherent(page, address);
  216. else
  217. vaddr = kmap_atomic(page);
  218. address = (unsigned long)vaddr;
  219. }
  220. flush_cache_one(CACHE_OC_ADDRESS_ARRAY |
  221. (address & shm_align_mask), phys);
  222. if (vma->vm_flags & VM_EXEC)
  223. flush_icache_all();
  224. if (vaddr) {
  225. if (map_coherent)
  226. kunmap_coherent(vaddr);
  227. else
  228. kunmap_atomic(vaddr);
  229. }
  230. }
  231. /*
  232. * Write back and invalidate D-caches.
  233. *
  234. * START, END: Virtual Address (U0 address)
  235. *
  236. * NOTE: We need to flush the _physical_ page entry.
  237. * Flushing the cache lines for U0 only isn't enough.
  238. * We need to flush for P1 too, which may contain aliases.
  239. */
  240. static void sh4_flush_cache_range(void *args)
  241. {
  242. struct flusher_data *data = args;
  243. struct vm_area_struct *vma;
  244. unsigned long start, end;
  245. vma = data->vma;
  246. start = data->addr1;
  247. end = data->addr2;
  248. if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT)
  249. return;
  250. /*
  251. * If cache is only 4k-per-way, there are never any 'aliases'. Since
  252. * the cache is physically tagged, the data can just be left in there.
  253. */
  254. if (boot_cpu_data.dcache.n_aliases == 0)
  255. return;
  256. flush_dcache_all();
  257. if (vma->vm_flags & VM_EXEC)
  258. flush_icache_all();
  259. }
  260. /**
  261. * __flush_cache_one
  262. *
  263. * @addr: address in memory mapped cache array
  264. * @phys: P1 address to flush (has to match tags if addr has 'A' bit
  265. * set i.e. associative write)
  266. * @exec_offset: set to 0x20000000 if flush has to be executed from P2
  267. * region else 0x0
  268. *
  269. * The offset into the cache array implied by 'addr' selects the
  270. * 'colour' of the virtual address range that will be flushed. The
  271. * operation (purge/write-back) is selected by the lower 2 bits of
  272. * 'phys'.
  273. */
  274. static void __flush_cache_one(unsigned long addr, unsigned long phys,
  275. unsigned long exec_offset)
  276. {
  277. int way_count;
  278. unsigned long base_addr = addr;
  279. struct cache_info *dcache;
  280. unsigned long way_incr;
  281. unsigned long a, ea, p;
  282. unsigned long temp_pc;
  283. dcache = &boot_cpu_data.dcache;
  284. /* Write this way for better assembly. */
  285. way_count = dcache->ways;
  286. way_incr = dcache->way_incr;
  287. /*
  288. * Apply exec_offset (i.e. branch to P2 if required.).
  289. *
  290. * FIXME:
  291. *
  292. * If I write "=r" for the (temp_pc), it puts this in r6 hence
  293. * trashing exec_offset before it's been added on - why? Hence
  294. * "=&r" as a 'workaround'
  295. */
  296. asm volatile("mov.l 1f, %0\n\t"
  297. "add %1, %0\n\t"
  298. "jmp @%0\n\t"
  299. "nop\n\t"
  300. ".balign 4\n\t"
  301. "1: .long 2f\n\t"
  302. "2:\n" : "=&r" (temp_pc) : "r" (exec_offset));
  303. /*
  304. * We know there will be >=1 iteration, so write as do-while to avoid
  305. * pointless nead-of-loop check for 0 iterations.
  306. */
  307. do {
  308. ea = base_addr + PAGE_SIZE;
  309. a = base_addr;
  310. p = phys;
  311. do {
  312. *(volatile unsigned long *)a = p;
  313. /*
  314. * Next line: intentionally not p+32, saves an add, p
  315. * will do since only the cache tag bits need to
  316. * match.
  317. */
  318. *(volatile unsigned long *)(a+32) = p;
  319. a += 64;
  320. p += 64;
  321. } while (a < ea);
  322. base_addr += way_incr;
  323. } while (--way_count != 0);
  324. }
  325. /*
  326. * SH-4 has virtually indexed and physically tagged cache.
  327. */
  328. void __init sh4_cache_init(void)
  329. {
  330. printk("PVR=%08x CVR=%08x PRR=%08x\n",
  331. __raw_readl(CCN_PVR),
  332. __raw_readl(CCN_CVR),
  333. __raw_readl(CCN_PRR));
  334. local_flush_icache_range = sh4_flush_icache_range;
  335. local_flush_dcache_folio = sh4_flush_dcache_folio;
  336. local_flush_cache_all = sh4_flush_cache_all;
  337. local_flush_cache_mm = sh4_flush_cache_mm;
  338. local_flush_cache_dup_mm = sh4_flush_cache_mm;
  339. local_flush_cache_page = sh4_flush_cache_page;
  340. local_flush_cache_range = sh4_flush_cache_range;
  341. sh4__flush_region_init();
  342. }