cache-sh2a.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * arch/sh/mm/cache-sh2a.c
  3. *
  4. * Copyright (C) 2008 Yoshinori Sato
  5. *
  6. * Released under the terms of the GNU GPL v2.0.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/mm.h>
  10. #include <asm/cache.h>
  11. #include <asm/addrspace.h>
  12. #include <asm/processor.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/io.h>
  15. /*
  16. * The maximum number of pages we support up to when doing ranged dcache
  17. * flushing. Anything exceeding this will simply flush the dcache in its
  18. * entirety.
  19. */
  20. #define MAX_OCACHE_PAGES 32
  21. #define MAX_ICACHE_PAGES 32
  22. #ifdef CONFIG_CACHE_WRITEBACK
  23. static void sh2a_flush_oc_line(unsigned long v, int way)
  24. {
  25. unsigned long addr = (v & 0x000007f0) | (way << 11);
  26. unsigned long data;
  27. data = __raw_readl(CACHE_OC_ADDRESS_ARRAY | addr);
  28. if ((data & CACHE_PHYSADDR_MASK) == (v & CACHE_PHYSADDR_MASK)) {
  29. data &= ~SH_CACHE_UPDATED;
  30. __raw_writel(data, CACHE_OC_ADDRESS_ARRAY | addr);
  31. }
  32. }
  33. #endif
  34. static void sh2a_invalidate_line(unsigned long cache_addr, unsigned long v)
  35. {
  36. /* Set associative bit to hit all ways */
  37. unsigned long addr = (v & 0x000007f0) | SH_CACHE_ASSOC;
  38. __raw_writel((addr & CACHE_PHYSADDR_MASK), cache_addr | addr);
  39. }
  40. /*
  41. * Write back the dirty D-caches, but not invalidate them.
  42. */
  43. static void sh2a__flush_wback_region(void *start, int size)
  44. {
  45. #ifdef CONFIG_CACHE_WRITEBACK
  46. unsigned long v;
  47. unsigned long begin, end;
  48. unsigned long flags;
  49. int nr_ways;
  50. begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
  51. end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
  52. & ~(L1_CACHE_BYTES-1);
  53. nr_ways = current_cpu_data.dcache.ways;
  54. local_irq_save(flags);
  55. jump_to_uncached();
  56. /* If there are too many pages then flush the entire cache */
  57. if (((end - begin) >> PAGE_SHIFT) >= MAX_OCACHE_PAGES) {
  58. begin = CACHE_OC_ADDRESS_ARRAY;
  59. end = begin + (nr_ways * current_cpu_data.dcache.way_size);
  60. for (v = begin; v < end; v += L1_CACHE_BYTES) {
  61. unsigned long data = __raw_readl(v);
  62. if (data & SH_CACHE_UPDATED)
  63. __raw_writel(data & ~SH_CACHE_UPDATED, v);
  64. }
  65. } else {
  66. int way;
  67. for (way = 0; way < nr_ways; way++) {
  68. for (v = begin; v < end; v += L1_CACHE_BYTES)
  69. sh2a_flush_oc_line(v, way);
  70. }
  71. }
  72. back_to_cached();
  73. local_irq_restore(flags);
  74. #endif
  75. }
  76. /*
  77. * Write back the dirty D-caches and invalidate them.
  78. */
  79. static void sh2a__flush_purge_region(void *start, int size)
  80. {
  81. unsigned long v;
  82. unsigned long begin, end;
  83. unsigned long flags;
  84. begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
  85. end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
  86. & ~(L1_CACHE_BYTES-1);
  87. local_irq_save(flags);
  88. jump_to_uncached();
  89. for (v = begin; v < end; v+=L1_CACHE_BYTES) {
  90. #ifdef CONFIG_CACHE_WRITEBACK
  91. int way;
  92. int nr_ways = current_cpu_data.dcache.ways;
  93. for (way = 0; way < nr_ways; way++)
  94. sh2a_flush_oc_line(v, way);
  95. #endif
  96. sh2a_invalidate_line(CACHE_OC_ADDRESS_ARRAY, v);
  97. }
  98. back_to_cached();
  99. local_irq_restore(flags);
  100. }
  101. /*
  102. * Invalidate the D-caches, but no write back please
  103. */
  104. static void sh2a__flush_invalidate_region(void *start, int size)
  105. {
  106. unsigned long v;
  107. unsigned long begin, end;
  108. unsigned long flags;
  109. begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
  110. end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
  111. & ~(L1_CACHE_BYTES-1);
  112. local_irq_save(flags);
  113. jump_to_uncached();
  114. /* If there are too many pages then just blow the cache */
  115. if (((end - begin) >> PAGE_SHIFT) >= MAX_OCACHE_PAGES) {
  116. __raw_writel(__raw_readl(SH_CCR) | CCR_OCACHE_INVALIDATE,
  117. SH_CCR);
  118. } else {
  119. for (v = begin; v < end; v += L1_CACHE_BYTES)
  120. sh2a_invalidate_line(CACHE_OC_ADDRESS_ARRAY, v);
  121. }
  122. back_to_cached();
  123. local_irq_restore(flags);
  124. }
  125. /*
  126. * Write back the range of D-cache, and purge the I-cache.
  127. */
  128. static void sh2a_flush_icache_range(void *args)
  129. {
  130. struct flusher_data *data = args;
  131. unsigned long start, end;
  132. unsigned long v;
  133. unsigned long flags;
  134. start = data->addr1 & ~(L1_CACHE_BYTES-1);
  135. end = (data->addr2 + L1_CACHE_BYTES-1) & ~(L1_CACHE_BYTES-1);
  136. #ifdef CONFIG_CACHE_WRITEBACK
  137. sh2a__flush_wback_region((void *)start, end-start);
  138. #endif
  139. local_irq_save(flags);
  140. jump_to_uncached();
  141. /* I-Cache invalidate */
  142. /* If there are too many pages then just blow the cache */
  143. if (((end - start) >> PAGE_SHIFT) >= MAX_ICACHE_PAGES) {
  144. __raw_writel(__raw_readl(SH_CCR) | CCR_ICACHE_INVALIDATE,
  145. SH_CCR);
  146. } else {
  147. for (v = start; v < end; v += L1_CACHE_BYTES)
  148. sh2a_invalidate_line(CACHE_IC_ADDRESS_ARRAY, v);
  149. }
  150. back_to_cached();
  151. local_irq_restore(flags);
  152. }
  153. void __init sh2a_cache_init(void)
  154. {
  155. local_flush_icache_range = sh2a_flush_icache_range;
  156. __flush_wback_region = sh2a__flush_wback_region;
  157. __flush_purge_region = sh2a__flush_purge_region;
  158. __flush_invalidate_region = sh2a__flush_invalidate_region;
  159. }