cache-sh7705.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/pagemap.h>
  17. #include <linux/threads.h>
  18. #include <asm/addrspace.h>
  19. #include <asm/page.h>
  20. #include <asm/processor.h>
  21. #include <asm/cache.h>
  22. #include <asm/io.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/cacheflush.h>
  26. /*
  27. * The 32KB cache on the SH7705 suffers from the same synonym problem
  28. * as SH4 CPUs
  29. */
  30. static inline void cache_wback_all(void)
  31. {
  32. unsigned long ways, waysize, addrstart;
  33. ways = current_cpu_data.dcache.ways;
  34. waysize = current_cpu_data.dcache.sets;
  35. waysize <<= current_cpu_data.dcache.entry_shift;
  36. addrstart = CACHE_OC_ADDRESS_ARRAY;
  37. do {
  38. unsigned long addr;
  39. for (addr = addrstart;
  40. addr < addrstart + waysize;
  41. addr += current_cpu_data.dcache.linesz) {
  42. unsigned long data;
  43. int v = SH_CACHE_UPDATED | SH_CACHE_VALID;
  44. data = __raw_readl(addr);
  45. if ((data & v) == v)
  46. __raw_writel(data & ~v, addr);
  47. }
  48. addrstart += current_cpu_data.dcache.way_incr;
  49. } while (--ways);
  50. }
  51. /*
  52. * Write back the range of D-cache, and purge the I-cache.
  53. *
  54. * Called from kernel/module.c:sys_init_module and routine for a.out format.
  55. */
  56. static void sh7705_flush_icache_range(void *args)
  57. {
  58. struct flusher_data *data = args;
  59. unsigned long start, end;
  60. start = data->addr1;
  61. end = data->addr2;
  62. __flush_wback_region((void *)start, end - start);
  63. }
  64. /*
  65. * Writeback&Invalidate the D-cache of the page
  66. */
  67. static void __flush_dcache_page(unsigned long phys)
  68. {
  69. unsigned long ways, waysize, addrstart;
  70. unsigned long flags;
  71. phys |= SH_CACHE_VALID;
  72. /*
  73. * Here, phys is the physical address of the page. We check all the
  74. * tags in the cache for those with the same page number as this page
  75. * (by masking off the lowest 2 bits of the 19-bit tag; these bits are
  76. * derived from the offset within in the 4k page). Matching valid
  77. * entries are invalidated.
  78. *
  79. * Since 2 bits of the cache index are derived from the virtual page
  80. * number, knowing this would reduce the number of cache entries to be
  81. * searched by a factor of 4. However this function exists to deal with
  82. * potential cache aliasing, therefore the optimisation is probably not
  83. * possible.
  84. */
  85. local_irq_save(flags);
  86. jump_to_uncached();
  87. ways = current_cpu_data.dcache.ways;
  88. waysize = current_cpu_data.dcache.sets;
  89. waysize <<= current_cpu_data.dcache.entry_shift;
  90. addrstart = CACHE_OC_ADDRESS_ARRAY;
  91. do {
  92. unsigned long addr;
  93. for (addr = addrstart;
  94. addr < addrstart + waysize;
  95. addr += current_cpu_data.dcache.linesz) {
  96. unsigned long data;
  97. data = __raw_readl(addr) & (0x1ffffC00 | SH_CACHE_VALID);
  98. if (data == phys) {
  99. data &= ~(SH_CACHE_VALID | SH_CACHE_UPDATED);
  100. __raw_writel(data, addr);
  101. }
  102. }
  103. addrstart += current_cpu_data.dcache.way_incr;
  104. } while (--ways);
  105. back_to_cached();
  106. local_irq_restore(flags);
  107. }
  108. /*
  109. * Write back & invalidate the D-cache of the page.
  110. * (To avoid "alias" issues)
  111. */
  112. static void sh7705_flush_dcache_folio(void *arg)
  113. {
  114. struct folio *folio = arg;
  115. struct address_space *mapping = folio_flush_mapping(folio);
  116. if (mapping && !mapping_mapped(mapping))
  117. clear_bit(PG_dcache_clean, &folio->flags);
  118. else {
  119. unsigned long pfn = folio_pfn(folio);
  120. unsigned int i, nr = folio_nr_pages(folio);
  121. for (i = 0; i < nr; i++)
  122. __flush_dcache_page((pfn + i) * PAGE_SIZE);
  123. }
  124. }
  125. static void sh7705_flush_cache_all(void *args)
  126. {
  127. unsigned long flags;
  128. local_irq_save(flags);
  129. jump_to_uncached();
  130. cache_wback_all();
  131. back_to_cached();
  132. local_irq_restore(flags);
  133. }
  134. /*
  135. * Write back and invalidate I/D-caches for the page.
  136. *
  137. * ADDRESS: Virtual Address (U0 address)
  138. */
  139. static void sh7705_flush_cache_page(void *args)
  140. {
  141. struct flusher_data *data = args;
  142. unsigned long pfn = data->addr2;
  143. __flush_dcache_page(pfn << PAGE_SHIFT);
  144. }
  145. /*
  146. * This is called when a page-cache page is about to be mapped into a
  147. * user process' address space. It offers an opportunity for a
  148. * port to ensure d-cache/i-cache coherency if necessary.
  149. *
  150. * Not entirely sure why this is necessary on SH3 with 32K cache but
  151. * without it we get occasional "Memory fault" when loading a program.
  152. */
  153. static void sh7705_flush_icache_folio(void *arg)
  154. {
  155. struct folio *folio = arg;
  156. __flush_purge_region(folio_address(folio), folio_size(folio));
  157. }
  158. void __init sh7705_cache_init(void)
  159. {
  160. local_flush_icache_range = sh7705_flush_icache_range;
  161. local_flush_dcache_folio = sh7705_flush_dcache_folio;
  162. local_flush_cache_all = sh7705_flush_cache_all;
  163. local_flush_cache_mm = sh7705_flush_cache_all;
  164. local_flush_cache_dup_mm = sh7705_flush_cache_all;
  165. local_flush_cache_range = sh7705_flush_cache_all;
  166. local_flush_cache_page = sh7705_flush_cache_page;
  167. local_flush_icache_folio = sh7705_flush_icache_folio;
  168. }