cache.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #ifndef _MMU_CACHE_H_
  2. #define _MMU_CACHE_H_
  3. typedef struct cache_line_t
  4. {
  5. ARMword tag; /* cache line align address |
  6. bit2: last half dirty
  7. bit1: first half dirty
  8. bit0: cache valid flag
  9. */
  10. ARMword pa; /*physical address */
  11. ARMword *data; /*array of cached data */
  12. } cache_line_t;
  13. #define TAG_VALID_FLAG 0x00000001
  14. #define TAG_FIRST_HALF_DIRTY 0x00000002
  15. #define TAG_LAST_HALF_DIRTY 0x00000004
  16. /*cache set association*/
  17. typedef struct cache_set_s
  18. {
  19. cache_line_t *lines;
  20. int cycle;
  21. } cache_set_t;
  22. enum
  23. {
  24. CACHE_WRITE_BACK,
  25. CACHE_WRITE_THROUGH,
  26. };
  27. typedef struct cache_s
  28. {
  29. int width; /*bytes in a line */
  30. int way; /*way of set asscociate */
  31. int set; /*num of set */
  32. int w_mode; /*write back or write through */
  33. //int a_mode; /*alloc mode: random or round-bin*/
  34. cache_set_t *sets;
  35. /**/} cache_s;
  36. typedef struct cache_desc_s
  37. {
  38. int width;
  39. int way;
  40. int set;
  41. int w_mode;
  42. // int a_mode;
  43. } cache_desc_t;
  44. /*virtual address to cache set index*/
  45. #define va_cache_set(va, cache_t) \
  46. (((va) / (cache_t)->width) & ((cache_t)->set - 1))
  47. /*virtual address to cahce line aligned*/
  48. #define va_cache_align(va, cache_t) \
  49. ((va) & ~((cache_t)->width - 1))
  50. /*virtaul address to cache line word index*/
  51. #define va_cache_index(va, cache_t) \
  52. (((va) & ((cache_t)->width - 1)) >> WORD_SHT)
  53. /*see Page 558 in arm manual*/
  54. /*set/index format value to cache set value*/
  55. #define index_cache_set(index, cache_t) \
  56. (((index) / (cache_t)->width) & ((cache_t)->set - 1))
  57. /*************************cache********************/
  58. /* mmu cache init
  59. *
  60. * @cache_t :cache_t to init
  61. * @width :cache line width in byte
  62. * @way :way of each cache set
  63. * @set :cache set num
  64. * @w_mode :cache w_mode
  65. *
  66. * $ -1: error
  67. * 0: sucess
  68. */
  69. int
  70. mmu_cache_init (cache_s * cache_t, int width, int way, int set, int w_mode);
  71. /* free a cache_t's inner data, the ptr self is not freed,
  72. * when needed do like below:
  73. * mmu_cache_exit(cache);
  74. * free(cache_t);
  75. *
  76. * @cache_t : the cache_t to free
  77. */
  78. void mmu_cache_exit (cache_s * cache_t);
  79. /* mmu cache search
  80. *
  81. * @state :ARMul_State
  82. * @cache_t :cache_t to search
  83. * @va :virtual address
  84. *
  85. * $ NULL: no cache match
  86. * cache :cache matched
  87. * */
  88. cache_line_t *mmu_cache_search (ARMul_State * state, cache_s * cache_t,
  89. ARMword va);
  90. /* mmu cache search by set/index
  91. *
  92. * @state :ARMul_State
  93. * @cache_t :cache_t to search
  94. * @index :set/index value.
  95. *
  96. * $ NULL: no cache match
  97. * cache :cache matched
  98. * */
  99. cache_line_t *mmu_cache_search_by_index (ARMul_State * state,
  100. cache_s * cache_t, ARMword index);
  101. /* mmu cache alloc
  102. *
  103. * @state :ARMul_State
  104. * @cache_t :cache_t to alloc from
  105. * @va :virtual address that require cache alloc, need not cache aligned
  106. * @pa :physical address of va
  107. *
  108. * $ cache_alloced, always alloc OK
  109. */
  110. cache_line_t *mmu_cache_alloc (ARMul_State * state, cache_s * cache_t,
  111. ARMword va, ARMword pa);
  112. /* mmu_cache_write_back write cache data to memory
  113. *
  114. * @state:
  115. * @cache_t :cache_t of the cache line
  116. * @cache : cache line
  117. */
  118. void
  119. mmu_cache_write_back (ARMul_State * state, cache_s * cache_t,
  120. cache_line_t * cache);
  121. /* mmu_cache_clean: clean a cache of va in cache_t
  122. *
  123. * @state :ARMul_State
  124. * @cache_t :cache_t to clean
  125. * @va :virtaul address
  126. */
  127. void mmu_cache_clean (ARMul_State * state, cache_s * cache_t, ARMword va);
  128. void
  129. mmu_cache_clean_by_index (ARMul_State * state, cache_s * cache_t,
  130. ARMword index);
  131. /* mmu_cache_invalidate : invalidate a cache of va
  132. *
  133. * @state :ARMul_State
  134. * @cache_t :cache_t to invalid
  135. * @va :virt_addr to invalid
  136. */
  137. void
  138. mmu_cache_invalidate (ARMul_State * state, cache_s * cache_t, ARMword va);
  139. void
  140. mmu_cache_invalidate_by_index (ARMul_State * state, cache_s * cache_t,
  141. ARMword index);
  142. void mmu_cache_invalidate_all (ARMul_State * state, cache_s * cache_t);
  143. void
  144. mmu_cache_soft_flush (ARMul_State * state, cache_s * cache_t, ARMword pa);
  145. cache_line_t* mmu_cache_dirty_cache(ARMul_State * state, cache_s * cache_t);
  146. #endif /*_MMU_CACHE_H_*/