cache-sh7705.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * arch/sh/mm/cache-sh7705.c
  3. *
  4. * Copyright (C) 1999, 2000 Niibe Yutaka
  5. * Copyright (C) 2004 Alex Song
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. *
  11. */
  12. #include <linux/init.h>
  13. #include <linux/mman.h>
  14. #include <linux/mm.h>
  15. #include <linux/fs.h>
  16. #include <linux/threads.h>
  17. #include <asm/addrspace.h>
  18. #include <asm/page.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/processor.h>
  21. #include <asm/cache.h>
  22. #include <asm/io.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/pgalloc.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/cacheflush.h>
  27. /*
  28. * The 32KB cache on the SH7705 suffers from the same synonym problem
  29. * as SH4 CPUs
  30. */
  31. static inline void cache_wback_all(void)
  32. {
  33. unsigned long ways, waysize, addrstart;
  34. ways = current_cpu_data.dcache.ways;
  35. waysize = current_cpu_data.dcache.sets;
  36. waysize <<= current_cpu_data.dcache.entry_shift;
  37. addrstart = CACHE_OC_ADDRESS_ARRAY;
  38. do {
  39. unsigned long addr;
  40. for (addr = addrstart;
  41. addr < addrstart + waysize;
  42. addr += current_cpu_data.dcache.linesz) {
  43. unsigned long data;
  44. int v = SH_CACHE_UPDATED | SH_CACHE_VALID;
  45. data = ctrl_inl(addr);
  46. if ((data & v) == v)
  47. ctrl_outl(data & ~v, addr);
  48. }
  49. addrstart += current_cpu_data.dcache.way_incr;
  50. } while (--ways);
  51. }
  52. /*
  53. * Write back the range of D-cache, and purge the I-cache.
  54. *
  55. * Called from kernel/module.c:sys_init_module and routine for a.out format.
  56. */
  57. static void sh7705_flush_icache_range(unsigned long start, unsigned long end)
  58. {
  59. __flush_wback_region((void *)start, end - start);
  60. }
  61. /*
  62. * Writeback&Invalidate the D-cache of the page
  63. */
  64. static void __flush_dcache_page(unsigned long phys)
  65. {
  66. unsigned long ways, waysize, addrstart;
  67. unsigned long flags;
  68. phys |= SH_CACHE_VALID;
  69. /*
  70. * Here, phys is the physical address of the page. We check all the
  71. * tags in the cache for those with the same page number as this page
  72. * (by masking off the lowest 2 bits of the 19-bit tag; these bits are
  73. * derived from the offset within in the 4k page). Matching valid
  74. * entries are invalidated.
  75. *
  76. * Since 2 bits of the cache index are derived from the virtual page
  77. * number, knowing this would reduce the number of cache entries to be
  78. * searched by a factor of 4. However this function exists to deal with
  79. * potential cache aliasing, therefore the optimisation is probably not
  80. * possible.
  81. */
  82. local_irq_save(flags);
  83. jump_to_uncached();
  84. ways = current_cpu_data.dcache.ways;
  85. waysize = current_cpu_data.dcache.sets;
  86. waysize <<= current_cpu_data.dcache.entry_shift;
  87. addrstart = CACHE_OC_ADDRESS_ARRAY;
  88. do {
  89. unsigned long addr;
  90. for (addr = addrstart;
  91. addr < addrstart + waysize;
  92. addr += current_cpu_data.dcache.linesz) {
  93. unsigned long data;
  94. data = ctrl_inl(addr) & (0x1ffffC00 | SH_CACHE_VALID);
  95. if (data == phys) {
  96. data &= ~(SH_CACHE_VALID | SH_CACHE_UPDATED);
  97. ctrl_outl(data, addr);
  98. }
  99. }
  100. addrstart += current_cpu_data.dcache.way_incr;
  101. } while (--ways);
  102. back_to_cached();
  103. local_irq_restore(flags);
  104. }
  105. /*
  106. * Write back & invalidate the D-cache of the page.
  107. * (To avoid "alias" issues)
  108. */
  109. static void sh7705_flush_dcache_page(struct page *page)
  110. {
  111. struct address_space *mapping = page_mapping(page);
  112. if (mapping && !mapping_mapped(mapping))
  113. set_bit(PG_dcache_dirty, &page->flags);
  114. else
  115. __flush_dcache_page(PHYSADDR(page_address(page)));
  116. }
  117. static void sh7705_flush_cache_all(void)
  118. {
  119. unsigned long flags;
  120. local_irq_save(flags);
  121. jump_to_uncached();
  122. cache_wback_all();
  123. back_to_cached();
  124. local_irq_restore(flags);
  125. }
  126. static void sh7705_flush_cache_mm(struct mm_struct *mm)
  127. {
  128. /* Is there any good way? */
  129. /* XXX: possibly call flush_cache_range for each vm area */
  130. flush_cache_all();
  131. }
  132. /*
  133. * Write back and invalidate D-caches.
  134. *
  135. * START, END: Virtual Address (U0 address)
  136. *
  137. * NOTE: We need to flush the _physical_ page entry.
  138. * Flushing the cache lines for U0 only isn't enough.
  139. * We need to flush for P1 too, which may contain aliases.
  140. */
  141. static void sh7705_flush_cache_range(struct vm_area_struct *vma,
  142. unsigned long start, unsigned long end)
  143. {
  144. /*
  145. * We could call flush_cache_page for the pages of these range,
  146. * but it's not efficient (scan the caches all the time...).
  147. *
  148. * We can't use A-bit magic, as there's the case we don't have
  149. * valid entry on TLB.
  150. */
  151. flush_cache_all();
  152. }
  153. /*
  154. * Write back and invalidate I/D-caches for the page.
  155. *
  156. * ADDRESS: Virtual Address (U0 address)
  157. */
  158. static void sh7705_flush_cache_page(struct vm_area_struct *vma,
  159. unsigned long address, unsigned long pfn)
  160. {
  161. __flush_dcache_page(pfn << PAGE_SHIFT);
  162. }
  163. /*
  164. * This is called when a page-cache page is about to be mapped into a
  165. * user process' address space. It offers an opportunity for a
  166. * port to ensure d-cache/i-cache coherency if necessary.
  167. *
  168. * Not entirely sure why this is necessary on SH3 with 32K cache but
  169. * without it we get occasional "Memory fault" when loading a program.
  170. */
  171. static void sh7705_flush_icache_page(struct vm_area_struct *vma,
  172. struct page *page)
  173. {
  174. __flush_purge_region(page_address(page), PAGE_SIZE);
  175. }
  176. void __init sh7705_cache_init(void)
  177. {
  178. flush_icache_range = sh7705_flush_icache_range;
  179. flush_dcache_page = sh7705_flush_dcache_page;
  180. flush_cache_all = sh7705_flush_cache_all;
  181. flush_cache_mm = sh7705_flush_cache_mm;
  182. flush_cache_dup_mm = sh7705_flush_cache_mm;
  183. flush_cache_range = sh7705_flush_cache_range;
  184. flush_cache_page = sh7705_flush_cache_page;
  185. flush_icache_page = sh7705_flush_icache_page;
  186. }