cache-sh7705.c 5.0 KB

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