cache-sh4.c 9.8 KB

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