cache-xsc3l2.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <linux/io.h>
  22. #include <asm/system.h>
  23. #include <asm/cputype.h>
  24. #include <asm/cacheflush.h>
  25. #define CR_L2 (1 << 26)
  26. #define CACHE_LINE_SIZE 32
  27. #define CACHE_LINE_SHIFT 5
  28. #define CACHE_WAY_PER_SET 8
  29. #define CACHE_WAY_SIZE(l2ctype) (8192 << (((l2ctype) >> 8) & 0xf))
  30. #define CACHE_SET_SIZE(l2ctype) (CACHE_WAY_SIZE(l2ctype) >> CACHE_LINE_SHIFT)
  31. static inline int xsc3_l2_present(void)
  32. {
  33. unsigned long l2ctype;
  34. __asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
  35. return !!(l2ctype & 0xf8);
  36. }
  37. static inline void xsc3_l2_clean_mva(unsigned long addr)
  38. {
  39. __asm__("mcr p15, 1, %0, c7, c11, 1" : : "r" (addr));
  40. }
  41. static inline void xsc3_l2_clean_pa(unsigned long addr)
  42. {
  43. xsc3_l2_clean_mva(__phys_to_virt(addr));
  44. }
  45. static inline void xsc3_l2_inv_mva(unsigned long addr)
  46. {
  47. __asm__("mcr p15, 1, %0, c7, c7, 1" : : "r" (addr));
  48. }
  49. static inline void xsc3_l2_inv_pa(unsigned long addr)
  50. {
  51. xsc3_l2_inv_mva(__phys_to_virt(addr));
  52. }
  53. static inline void xsc3_l2_inv_all(void)
  54. {
  55. unsigned long l2ctype, set_way;
  56. int set, way;
  57. __asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
  58. for (set = 0; set < CACHE_SET_SIZE(l2ctype); set++) {
  59. for (way = 0; way < CACHE_WAY_PER_SET; way++) {
  60. set_way = (way << 29) | (set << 5);
  61. __asm__("mcr p15, 1, %0, c7, c11, 2" : : "r"(set_way));
  62. }
  63. }
  64. dsb();
  65. }
  66. static void xsc3_l2_inv_range(unsigned long start, unsigned long end)
  67. {
  68. if (start == 0 && end == -1ul) {
  69. xsc3_l2_inv_all();
  70. return;
  71. }
  72. /*
  73. * Clean and invalidate partial first cache line.
  74. */
  75. if (start & (CACHE_LINE_SIZE - 1)) {
  76. xsc3_l2_clean_pa(start & ~(CACHE_LINE_SIZE - 1));
  77. xsc3_l2_inv_pa(start & ~(CACHE_LINE_SIZE - 1));
  78. start = (start | (CACHE_LINE_SIZE - 1)) + 1;
  79. }
  80. /*
  81. * Clean and invalidate partial last cache line.
  82. */
  83. if (end & (CACHE_LINE_SIZE - 1)) {
  84. xsc3_l2_clean_pa(end & ~(CACHE_LINE_SIZE - 1));
  85. xsc3_l2_inv_pa(end & ~(CACHE_LINE_SIZE - 1));
  86. end &= ~(CACHE_LINE_SIZE - 1);
  87. }
  88. /*
  89. * Invalidate all full cache lines between 'start' and 'end'.
  90. */
  91. while (start != end) {
  92. xsc3_l2_inv_pa(start);
  93. start += CACHE_LINE_SIZE;
  94. }
  95. dsb();
  96. }
  97. static void xsc3_l2_clean_range(unsigned long start, unsigned long end)
  98. {
  99. start &= ~(CACHE_LINE_SIZE - 1);
  100. while (start < end) {
  101. xsc3_l2_clean_pa(start);
  102. start += CACHE_LINE_SIZE;
  103. }
  104. dsb();
  105. }
  106. /*
  107. * optimize L2 flush all operation by set/way format
  108. */
  109. static inline void xsc3_l2_flush_all(void)
  110. {
  111. unsigned long l2ctype, set_way;
  112. int set, way;
  113. __asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
  114. for (set = 0; set < CACHE_SET_SIZE(l2ctype); set++) {
  115. for (way = 0; way < CACHE_WAY_PER_SET; way++) {
  116. set_way = (way << 29) | (set << 5);
  117. __asm__("mcr p15, 1, %0, c7, c15, 2" : : "r"(set_way));
  118. }
  119. }
  120. dsb();
  121. }
  122. static void xsc3_l2_flush_range(unsigned long start, unsigned long end)
  123. {
  124. if (start == 0 && end == -1ul) {
  125. xsc3_l2_flush_all();
  126. return;
  127. }
  128. start &= ~(CACHE_LINE_SIZE - 1);
  129. while (start < end) {
  130. xsc3_l2_clean_pa(start);
  131. xsc3_l2_inv_pa(start);
  132. start += CACHE_LINE_SIZE;
  133. }
  134. dsb();
  135. }
  136. static int __init xsc3_l2_init(void)
  137. {
  138. if (!cpu_is_xsc3() || !xsc3_l2_present())
  139. return 0;
  140. if (!(get_cr() & CR_L2)) {
  141. pr_info("XScale3 L2 cache enabled.\n");
  142. adjust_cr(CR_L2, CR_L2);
  143. xsc3_l2_inv_all();
  144. }
  145. outer_cache.inv_range = xsc3_l2_inv_range;
  146. outer_cache.clean_range = xsc3_l2_clean_range;
  147. outer_cache.flush_range = xsc3_l2_flush_range;
  148. return 0;
  149. }
  150. core_initcall(xsc3_l2_init);