cache-sh2a.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. static void sh2a_flush_oc_line(unsigned long v, int way)
  23. {
  24. unsigned long addr = (v & 0x000007f0) | (way << 11);
  25. unsigned long data;
  26. data = __raw_readl(CACHE_OC_ADDRESS_ARRAY | addr);
  27. if ((data & CACHE_PHYSADDR_MASK) == (v & CACHE_PHYSADDR_MASK)) {
  28. data &= ~SH_CACHE_UPDATED;
  29. __raw_writel(data, CACHE_OC_ADDRESS_ARRAY | addr);
  30. }
  31. }
  32. static void sh2a_invalidate_line(unsigned long cache_addr, unsigned long v)
  33. {
  34. /* Set associative bit to hit all ways */
  35. unsigned long addr = (v & 0x000007f0) | SH_CACHE_ASSOC;
  36. __raw_writel((addr & CACHE_PHYSADDR_MASK), cache_addr | addr);
  37. }
  38. /*
  39. * Write back the dirty D-caches, but not invalidate them.
  40. */
  41. static void sh2a__flush_wback_region(void *start, int size)
  42. {
  43. #ifdef CONFIG_CACHE_WRITEBACK
  44. unsigned long v;
  45. unsigned long begin, end;
  46. unsigned long flags;
  47. int nr_ways;
  48. begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
  49. end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
  50. & ~(L1_CACHE_BYTES-1);
  51. nr_ways = current_cpu_data.dcache.ways;
  52. local_irq_save(flags);
  53. jump_to_uncached();
  54. /* If there are too many pages then flush the entire cache */
  55. if (((end - begin) >> PAGE_SHIFT) >= MAX_OCACHE_PAGES) {
  56. begin = CACHE_OC_ADDRESS_ARRAY;
  57. end = begin + (nr_ways * current_cpu_data.dcache.way_size);
  58. for (v = begin; v < end; v += L1_CACHE_BYTES) {
  59. unsigned long data = __raw_readl(v);
  60. if (data & SH_CACHE_UPDATED)
  61. __raw_writel(data & ~SH_CACHE_UPDATED, v);
  62. }
  63. } else {
  64. int way;
  65. for (way = 0; way < nr_ways; way++) {
  66. for (v = begin; v < end; v += L1_CACHE_BYTES)
  67. sh2a_flush_oc_line(v, way);
  68. }
  69. }
  70. back_to_cached();
  71. local_irq_restore(flags);
  72. #endif
  73. }
  74. /*
  75. * Write back the dirty D-caches and invalidate them.
  76. */
  77. static void sh2a__flush_purge_region(void *start, int size)
  78. {
  79. unsigned long v;
  80. unsigned long begin, end;
  81. unsigned long flags;
  82. begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
  83. end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
  84. & ~(L1_CACHE_BYTES-1);
  85. local_irq_save(flags);
  86. jump_to_uncached();
  87. for (v = begin; v < end; v+=L1_CACHE_BYTES) {
  88. #ifdef CONFIG_CACHE_WRITEBACK
  89. int way;
  90. int nr_ways = current_cpu_data.dcache.ways;
  91. for (way = 0; way < nr_ways; way++)
  92. sh2a_flush_oc_line(v, way);
  93. #endif
  94. sh2a_invalidate_line(CACHE_OC_ADDRESS_ARRAY, v);
  95. }
  96. back_to_cached();
  97. local_irq_restore(flags);
  98. }
  99. /*
  100. * Invalidate the D-caches, but no write back please
  101. */
  102. static void sh2a__flush_invalidate_region(void *start, int size)
  103. {
  104. unsigned long v;
  105. unsigned long begin, end;
  106. unsigned long flags;
  107. begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
  108. end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
  109. & ~(L1_CACHE_BYTES-1);
  110. local_irq_save(flags);
  111. jump_to_uncached();
  112. /* If there are too many pages then just blow the cache */
  113. if (((end - begin) >> PAGE_SHIFT) >= MAX_OCACHE_PAGES) {
  114. __raw_writel(__raw_readl(CCR) | CCR_OCACHE_INVALIDATE, CCR);
  115. } else {
  116. for (v = begin; v < end; v += L1_CACHE_BYTES)
  117. sh2a_invalidate_line(CACHE_OC_ADDRESS_ARRAY, v);
  118. }
  119. back_to_cached();
  120. local_irq_restore(flags);
  121. }
  122. /*
  123. * Write back the range of D-cache, and purge the I-cache.
  124. */
  125. static void sh2a_flush_icache_range(void *args)
  126. {
  127. struct flusher_data *data = args;
  128. unsigned long start, end;
  129. unsigned long v;
  130. unsigned long flags;
  131. start = data->addr1 & ~(L1_CACHE_BYTES-1);
  132. end = (data->addr2 + L1_CACHE_BYTES-1) & ~(L1_CACHE_BYTES-1);
  133. #ifdef CONFIG_CACHE_WRITEBACK
  134. sh2a__flush_wback_region((void *)start, end-start);
  135. #endif
  136. local_irq_save(flags);
  137. jump_to_uncached();
  138. /* I-Cache invalidate */
  139. /* If there are too many pages then just blow the cache */
  140. if (((end - start) >> PAGE_SHIFT) >= MAX_ICACHE_PAGES) {
  141. __raw_writel(__raw_readl(CCR) | CCR_ICACHE_INVALIDATE, CCR);
  142. } else {
  143. for (v = start; v < end; v += L1_CACHE_BYTES)
  144. sh2a_invalidate_line(CACHE_IC_ADDRESS_ARRAY, v);
  145. }
  146. back_to_cached();
  147. local_irq_restore(flags);
  148. }
  149. void __init sh2a_cache_init(void)
  150. {
  151. local_flush_icache_range = sh2a_flush_icache_range;
  152. __flush_wback_region = sh2a__flush_wback_region;
  153. __flush_purge_region = sh2a__flush_purge_region;
  154. __flush_invalidate_region = sh2a__flush_invalidate_region;
  155. }