cache-sh3.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* $Id: cache-sh3.c,v 1.6 2001/09/10 08:59:59 dwmw2 Exp $
  2.  *
  3.  *  linux/arch/sh/mm/cache-sh3.c
  4.  *
  5.  * Copyright (C) 1999, 2000  Niibe Yutaka
  6.  *
  7.  */
  8. #include <linux/init.h>
  9. #include <linux/mman.h>
  10. #include <linux/mm.h>
  11. #include <linux/threads.h>
  12. #include <asm/addrspace.h>
  13. #include <asm/page.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/processor.h>
  16. #include <asm/cache.h>
  17. #include <asm/io.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/mmu_context.h>
  21. #define CCR 0xffffffec /* Address of Cache Control Register */
  22. #define CCR_CACHE_CE 0x01 /* Cache Enable */
  23. #define CCR_CACHE_WT 0x02 /* Write-Through (for P0,U0,P3) (else writeback) */
  24. #define CCR_CACHE_CB 0x04 /* Write-Back (for P1) (else writethrough) */
  25. #define CCR_CACHE_CF 0x08 /* Cache Flush */
  26. #define CCR_CACHE_RA 0x20 /* RAM mode */
  27. #define CCR_CACHE_VAL (CCR_CACHE_CB|CCR_CACHE_CE) /* 8k-byte cache, P1-wb, enable */
  28. #define CCR_CACHE_INIT (CCR_CACHE_CF|CCR_CACHE_VAL) /* 8k-byte cache, CF, P1-wb, enable */
  29. #define CACHE_OC_ADDRESS_ARRAY 0xf0000000
  30. #define CACHE_VALID   1
  31. #define CACHE_UPDATED   2
  32. #define CACHE_PHYSADDR_MASK 0x1ffffc00
  33. /* 7709A/7729 has 16K cache (256-entry), while 7702 has only 2K(direct)
  34.    7702 is not supported (yet) */
  35. struct _cache_system_info {
  36. int way_shift;
  37. int entry_mask;
  38. int num_entries;
  39. };
  40. /* Data at BSS is cleared after setting this variable.
  41.    So, we Should not placed this variable at BSS section.
  42.    Initialize this, it is placed at data section. */
  43. static struct _cache_system_info cache_system_info = {0,};
  44. #define CACHE_OC_WAY_SHIFT (cache_system_info.way_shift)
  45. #define CACHE_OC_ENTRY_SHIFT    4
  46. #define CACHE_OC_ENTRY_MASK (cache_system_info.entry_mask)
  47. #define CACHE_OC_NUM_ENTRIES (cache_system_info.num_entries)
  48. #define CACHE_OC_NUM_WAYS 4
  49. #define CACHE_OC_ASSOC_BIT    (1<<3)
  50. /*
  51.  * Write back all the cache.
  52.  *
  53.  * For SH-4, we only need to flush (write back) Operand Cache,
  54.  * as Instruction Cache doesn't have "updated" data.
  55.  *
  56.  * Assumes that this is called in interrupt disabled context, and P2.
  57.  * Shuld be INLINE function.
  58.  */
  59. static inline void cache_wback_all(void)
  60. {
  61. unsigned long addr, data, i, j;
  62. for (i=0; i<CACHE_OC_NUM_ENTRIES; i++) {
  63. for (j=0; j<CACHE_OC_NUM_WAYS; j++) {
  64. addr = CACHE_OC_ADDRESS_ARRAY|(j<<CACHE_OC_WAY_SHIFT)|
  65. (i<<CACHE_OC_ENTRY_SHIFT);
  66. data = ctrl_inl(addr);
  67. if ((data & (CACHE_UPDATED|CACHE_VALID))
  68.     == (CACHE_UPDATED|CACHE_VALID))
  69. ctrl_outl(data & ~CACHE_UPDATED, addr);
  70. }
  71. }
  72. }
  73. static void __init
  74. detect_cpu_and_cache_system(void)
  75. {
  76. unsigned long addr0, addr1, data0, data1, data2, data3;
  77. jump_to_P2();
  78. /*
  79.  * Check if the entry shadows or not.
  80.  * When shadowed, it's 128-entry system.
  81.  * Otherwise, it's 256-entry system.
  82.  */
  83. addr0 = CACHE_OC_ADDRESS_ARRAY + (3 << 12);
  84. addr1 = CACHE_OC_ADDRESS_ARRAY + (1 << 12);
  85. /* First, write back & invalidate */
  86. data0  = ctrl_inl(addr0);
  87. ctrl_outl(data0&~(CACHE_VALID|CACHE_UPDATED), addr0);
  88. data1  = ctrl_inl(addr1);
  89. ctrl_outl(data1&~(CACHE_VALID|CACHE_UPDATED), addr1);
  90. /* Next, check if there's shadow or not */
  91. data0 = ctrl_inl(addr0);
  92. data0 ^= CACHE_VALID;
  93. ctrl_outl(data0, addr0);
  94. data1 = ctrl_inl(addr1);
  95. data2 = data1 ^ CACHE_VALID;
  96. ctrl_outl(data2, addr1);
  97. data3 = ctrl_inl(addr0);
  98. /* Lastly, invaliate them. */
  99. ctrl_outl(data0&~CACHE_VALID, addr0);
  100. ctrl_outl(data2&~CACHE_VALID, addr1);
  101. back_to_P1();
  102. if (data0 == data1 && data2 == data3) { /* Shadow */
  103. cache_system_info.way_shift = 11;
  104. cache_system_info.entry_mask = 0x7f0;
  105. cache_system_info.num_entries = 128;
  106. cpu_data->type = CPU_SH7708;
  107. } else { /* 7709A or 7729  */
  108. cache_system_info.way_shift = 12;
  109. cache_system_info.entry_mask = 0xff0;
  110. cache_system_info.num_entries = 256;
  111. cpu_data->type = CPU_SH7729;
  112. }
  113. }
  114. void __init cache_init(void)
  115. {
  116. unsigned long ccr;
  117. detect_cpu_and_cache_system();
  118. jump_to_P2();
  119. ccr = ctrl_inl(CCR);
  120. if (ccr & CCR_CACHE_CE)
  121. /*
  122.  * XXX: Should check RA here. 
  123.  * If RA was 1, we only need to flush the half of the caches.
  124.  */
  125. cache_wback_all();
  126. ctrl_outl(CCR_CACHE_INIT, CCR);
  127. back_to_P1();
  128. }
  129. /*
  130.  * Write back the dirty D-caches, but not invalidate them.
  131.  *
  132.  * Is this really worth it, or should we just alias this routine
  133.  * to __flush_purge_region too?
  134.  *
  135.  * START: Virtual Address (U0, P1, or P3)
  136.  * SIZE: Size of the region.
  137.  */
  138. void __flush_wback_region(void *start, int size)
  139. {
  140. unsigned long v, j;
  141. unsigned long begin, end;
  142. unsigned long flags;
  143. begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
  144. end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
  145. & ~(L1_CACHE_BYTES-1);
  146. for (v = begin; v < end; v+=L1_CACHE_BYTES) {
  147. for (j=0; j<CACHE_OC_NUM_WAYS; j++) {
  148. unsigned long data, addr, p;
  149. p = __pa(v);
  150. addr = CACHE_OC_ADDRESS_ARRAY|(j<<CACHE_OC_WAY_SHIFT)|
  151. (v&CACHE_OC_ENTRY_MASK);
  152. save_and_cli(flags);
  153. data = ctrl_inl(addr);
  154. if ((data & CACHE_PHYSADDR_MASK) ==
  155.     (p & CACHE_PHYSADDR_MASK)) {
  156. data &= ~CACHE_UPDATED;
  157. ctrl_outl(data, addr);
  158. restore_flags(flags);
  159. break;
  160. }
  161. restore_flags(flags);
  162. }
  163. }
  164. }
  165. /*
  166.  * Write back the dirty D-caches and invalidate them.
  167.  *
  168.  * START: Virtual Address (U0, P1, or P3)
  169.  * SIZE: Size of the region.
  170.  */
  171. void __flush_purge_region(void *start, int size)
  172. {
  173. unsigned long v;
  174. unsigned long begin, end;
  175. begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
  176. end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
  177. & ~(L1_CACHE_BYTES-1);
  178. for (v = begin; v < end; v+=L1_CACHE_BYTES) {
  179. unsigned long data, addr;
  180. data = (v & 0xfffffc00); /* _Virtual_ address, ~U, ~V */
  181. addr = CACHE_OC_ADDRESS_ARRAY | (v&CACHE_OC_ENTRY_MASK) | 
  182. CACHE_OC_ASSOC_BIT;
  183. ctrl_outl(data, addr);
  184. }
  185. }
  186. /*
  187.  * No write back please
  188.  *
  189.  * Except I don't think there's any way to avoid the writeback. So we
  190.  * just alias it to __flush_purge_region(). dwmw2.
  191.  */
  192. void __flush_invalidate_region(void *start, int size)
  193. __attribute__((alias("__flush_purge_region")));