cache-xsc3l2.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * arch/arm/mm/cache-xsc3l2.c - XScale3 L2 cache controller support
  3. *
  4. * Copyright (C) 2007 ARM Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/spinlock.h>
  21. #include <asm/system.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/io.h>
  24. #define CR_L2 (1 << 26)
  25. #define CACHE_LINE_SIZE 32
  26. #define CACHE_LINE_SHIFT 5
  27. #define CACHE_WAY_PER_SET 8
  28. #define CACHE_WAY_SIZE(l2ctype) (8192 << (((l2ctype) >> 8) & 0xf))
  29. #define CACHE_SET_SIZE(l2ctype) (CACHE_WAY_SIZE(l2ctype) >> CACHE_LINE_SHIFT)
  30. static inline int xsc3_l2_present(void)
  31. {
  32. unsigned long l2ctype;
  33. __asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
  34. return !!(l2ctype & 0xf8);
  35. }
  36. static inline void xsc3_l2_clean_mva(unsigned long addr)
  37. {
  38. __asm__("mcr p15, 1, %0, c7, c11, 1" : : "r" (addr));
  39. }
  40. static inline void xsc3_l2_clean_pa(unsigned long addr)
  41. {
  42. xsc3_l2_clean_mva(__phys_to_virt(addr));
  43. }
  44. static inline void xsc3_l2_inv_mva(unsigned long addr)
  45. {
  46. __asm__("mcr p15, 1, %0, c7, c7, 1" : : "r" (addr));
  47. }
  48. static inline void xsc3_l2_inv_pa(unsigned long addr)
  49. {
  50. xsc3_l2_inv_mva(__phys_to_virt(addr));
  51. }
  52. static inline void xsc3_l2_inv_all(void)
  53. {
  54. unsigned long l2ctype, set_way;
  55. int set, way;
  56. __asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
  57. for (set = 0; set < CACHE_SET_SIZE(l2ctype); set++) {
  58. for (way = 0; way < CACHE_WAY_PER_SET; way++) {
  59. set_way = (way << 29) | (set << 5);
  60. __asm__("mcr p15, 1, %0, c7, c11, 2" : : "r"(set_way));
  61. }
  62. }
  63. dsb();
  64. }
  65. static void xsc3_l2_inv_range(unsigned long start, unsigned long end)
  66. {
  67. if (start == 0 && end == -1ul) {
  68. xsc3_l2_inv_all();
  69. return;
  70. }
  71. /*
  72. * Clean and invalidate partial first cache line.
  73. */
  74. if (start & (CACHE_LINE_SIZE - 1)) {
  75. xsc3_l2_clean_pa(start & ~(CACHE_LINE_SIZE - 1));
  76. xsc3_l2_inv_pa(start & ~(CACHE_LINE_SIZE - 1));
  77. start = (start | (CACHE_LINE_SIZE - 1)) + 1;
  78. }
  79. /*
  80. * Clean and invalidate partial last cache line.
  81. */
  82. if (end & (CACHE_LINE_SIZE - 1)) {
  83. xsc3_l2_clean_pa(end & ~(CACHE_LINE_SIZE - 1));
  84. xsc3_l2_inv_pa(end & ~(CACHE_LINE_SIZE - 1));
  85. end &= ~(CACHE_LINE_SIZE - 1);
  86. }
  87. /*
  88. * Invalidate all full cache lines between 'start' and 'end'.
  89. */
  90. while (start != end) {
  91. xsc3_l2_inv_pa(start);
  92. start += CACHE_LINE_SIZE;
  93. }
  94. dsb();
  95. }
  96. static void xsc3_l2_clean_range(unsigned long start, unsigned long end)
  97. {
  98. start &= ~(CACHE_LINE_SIZE - 1);
  99. while (start < end) {
  100. xsc3_l2_clean_pa(start);
  101. start += CACHE_LINE_SIZE;
  102. }
  103. dsb();
  104. }
  105. /*
  106. * optimize L2 flush all operation by set/way format
  107. */
  108. static inline void xsc3_l2_flush_all(void)
  109. {
  110. unsigned long l2ctype, set_way;
  111. int set, way;
  112. __asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
  113. for (set = 0; set < CACHE_SET_SIZE(l2ctype); set++) {
  114. for (way = 0; way < CACHE_WAY_PER_SET; way++) {
  115. set_way = (way << 29) | (set << 5);
  116. __asm__("mcr p15, 1, %0, c7, c15, 2" : : "r"(set_way));
  117. }
  118. }
  119. dsb();
  120. }
  121. static void xsc3_l2_flush_range(unsigned long start, unsigned long end)
  122. {
  123. if (start == 0 && end == -1ul) {
  124. xsc3_l2_flush_all();
  125. return;
  126. }
  127. start &= ~(CACHE_LINE_SIZE - 1);
  128. while (start < end) {
  129. xsc3_l2_clean_pa(start);
  130. xsc3_l2_inv_pa(start);
  131. start += CACHE_LINE_SIZE;
  132. }
  133. dsb();
  134. }
  135. static int __init xsc3_l2_init(void)
  136. {
  137. if (!cpu_is_xsc3() || !xsc3_l2_present())
  138. return 0;
  139. if (!(get_cr() & CR_L2)) {
  140. pr_info("XScale3 L2 cache enabled.\n");
  141. adjust_cr(CR_L2, CR_L2);
  142. xsc3_l2_inv_all();
  143. }
  144. outer_cache.inv_range = xsc3_l2_inv_range;
  145. outer_cache.clean_range = xsc3_l2_clean_range;
  146. outer_cache.flush_range = xsc3_l2_flush_range;
  147. return 0;
  148. }
  149. core_initcall(xsc3_l2_init);