cache.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Cache support for the FRV simulator
  2. Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3. Contributed by Red Hat.
  4. This file is part of the GNU Simulators.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #ifndef CACHE_H
  16. #define CACHE_H
  17. /* A representation of a set-associative cache with LRU replacement,
  18. cache line locking, non-blocking support and multiple read ports. */
  19. /* An enumeration of cache pipeline request kinds. */
  20. typedef enum
  21. {
  22. req_load,
  23. req_store,
  24. req_invalidate,
  25. req_flush,
  26. req_preload,
  27. req_unlock,
  28. req_WAR
  29. } FRV_CACHE_REQUEST_KIND;
  30. /* The cache pipeline requests. */
  31. typedef struct {
  32. int preload;
  33. int lock;
  34. } FRV_CACHE_WAR_REQUEST;
  35. typedef struct {
  36. char *data;
  37. int length;
  38. } FRV_CACHE_STORE_REQUEST;
  39. typedef struct {
  40. int flush;
  41. int all;
  42. } FRV_CACHE_INVALIDATE_REQUEST;
  43. typedef struct {
  44. int lock;
  45. int length;
  46. } FRV_CACHE_PRELOAD_REQUEST;
  47. /* A cache pipeline request. */
  48. typedef struct frv_cache_request
  49. {
  50. struct frv_cache_request *next;
  51. struct frv_cache_request *prev;
  52. FRV_CACHE_REQUEST_KIND kind;
  53. unsigned reqno;
  54. unsigned priority;
  55. SI address;
  56. union {
  57. FRV_CACHE_STORE_REQUEST store;
  58. FRV_CACHE_INVALIDATE_REQUEST invalidate;
  59. FRV_CACHE_PRELOAD_REQUEST preload;
  60. FRV_CACHE_WAR_REQUEST WAR;
  61. } u;
  62. } FRV_CACHE_REQUEST;
  63. /* The buffer for returning data to the caller. */
  64. typedef struct {
  65. unsigned reqno;
  66. SI address;
  67. char *data;
  68. int valid;
  69. } FRV_CACHE_RETURN_BUFFER;
  70. /* The status of flush requests. */
  71. typedef struct {
  72. unsigned reqno;
  73. SI address;
  74. int valid;
  75. } FRV_CACHE_FLUSH_STATUS;
  76. /* Communicate status of requests to the caller. */
  77. typedef struct {
  78. FRV_CACHE_FLUSH_STATUS flush;
  79. FRV_CACHE_RETURN_BUFFER return_buffer;
  80. } FRV_CACHE_STATUS;
  81. /* A cache pipeline stage. */
  82. typedef struct {
  83. FRV_CACHE_REQUEST *request;
  84. } FRV_CACHE_STAGE;
  85. enum {
  86. FIRST_STAGE,
  87. A_STAGE = FIRST_STAGE, /* Addressing stage */
  88. I_STAGE, /* Interference stage */
  89. LAST_STAGE = I_STAGE,
  90. FRV_CACHE_STAGES
  91. };
  92. /* Representation of the WAR register. */
  93. typedef struct {
  94. unsigned reqno;
  95. unsigned priority;
  96. SI address;
  97. int preload;
  98. int lock;
  99. int latency;
  100. int valid;
  101. } FRV_CACHE_WAR;
  102. /* A cache pipeline. */
  103. #define NUM_WARS 2
  104. typedef struct {
  105. FRV_CACHE_REQUEST *requests;
  106. FRV_CACHE_STAGE stages[FRV_CACHE_STAGES];
  107. FRV_CACHE_WAR WAR[NUM_WARS];
  108. FRV_CACHE_STATUS status;
  109. } FRV_CACHE_PIPELINE;
  110. enum {LS, LD, FRV_CACHE_PIPELINES};
  111. /* Representation of the xARS registers. */
  112. typedef struct {
  113. int pipe;
  114. unsigned reqno;
  115. unsigned priority;
  116. SI address;
  117. int preload;
  118. int lock;
  119. int valid;
  120. } FRV_CACHE_ARS;
  121. /* A cache tag. */
  122. typedef struct {
  123. USI tag; /* Address tag. */
  124. int lru; /* Lower values indicates less recently used. */
  125. char *line; /* Points to storage for line in data_storage. */
  126. char dirty; /* line has been written to since last stored? */
  127. char locked; /* line is locked? */
  128. char valid; /* tag is valid? */
  129. } FRV_CACHE_TAG;
  130. /* Cache statistics. */
  131. typedef struct {
  132. unsigned long accesses; /* number of cache accesses. */
  133. unsigned long hits; /* number of cache hits. */
  134. } FRV_CACHE_STATISTICS;
  135. /* The cache itself.
  136. Notes:
  137. - line_size must be a power of 2
  138. - sets must be a power of 2
  139. - ways must be a power of 2
  140. */
  141. typedef struct {
  142. SIM_CPU *cpu;
  143. unsigned configured_ways; /* Number of ways configured in each set. */
  144. unsigned configured_sets; /* Number of sets configured in the cache. */
  145. unsigned ways; /* Number of ways in each set. */
  146. unsigned sets; /* Number of sets in the cache. */
  147. unsigned line_size; /* Size of each cache line. */
  148. unsigned memory_latency; /* Latency of main memory in cycles. */
  149. FRV_CACHE_TAG *tag_storage; /* Storage for tags. */
  150. char *data_storage; /* Storage for data (cache lines). */
  151. FRV_CACHE_PIPELINE pipeline[2]; /* Cache pipelines. */
  152. FRV_CACHE_ARS BARS; /* BARS register. */
  153. FRV_CACHE_ARS NARS; /* BARS register. */
  154. FRV_CACHE_STATISTICS statistics; /* Operation statistics. */
  155. } FRV_CACHE;
  156. /* The tags are stored by ways within sets in order to make computations
  157. easier. */
  158. #define CACHE_TAG(cache, set, way) ( \
  159. & ((cache)->tag_storage[(set) * (cache)->ways + (way)]) \
  160. )
  161. /* Compute the address tag corresponding to the given address. */
  162. #define CACHE_ADDRESS_TAG(cache, address) ( \
  163. (address) & ~(((cache)->line_size * (cache)->sets) - 1) \
  164. )
  165. /* Determine the index at which the set containing this tag starts. */
  166. #define CACHE_TAG_SET_START(cache, tag) ( \
  167. ((tag) - (cache)->tag_storage) & ~((cache)->ways - 1) \
  168. )
  169. /* Determine the number of the set which this cache tag is in. */
  170. #define CACHE_TAG_SET_NUMBER(cache, tag) ( \
  171. CACHE_TAG_SET_START ((cache), (tag)) / (cache)->ways \
  172. )
  173. #define CACHE_RETURN_DATA(cache, slot, address, mode, N) ( \
  174. T2H_##N (*(mode *)(& (cache)->pipeline[slot].status.return_buffer.data \
  175. [((address) & ((cache)->line_size - 1))])) \
  176. )
  177. #define CACHE_RETURN_DATA_ADDRESS(cache, slot, address, N) ( \
  178. ((void *)& (cache)->pipeline[slot].status.return_buffer.data[(address) \
  179. & ((cache)->line_size - 1)]) \
  180. )
  181. #define DATA_CROSSES_CACHE_LINE(cache, address, size) ( \
  182. ((address) & ((cache)->line_size - 1)) + (size) > (cache)->line_size \
  183. )
  184. #define CACHE_INITIALIZED(cache) ((cache)->data_storage != NULL)
  185. /* These functions are used to initialize and terminate a cache. */
  186. void
  187. frv_cache_init (SIM_CPU *, FRV_CACHE *);
  188. void
  189. frv_cache_term (FRV_CACHE *);
  190. void
  191. frv_cache_reconfigure (SIM_CPU *, FRV_CACHE *);
  192. int
  193. frv_cache_enabled (FRV_CACHE *);
  194. /* These functions are used to operate the cache in non-cycle-accurate mode.
  195. Each request is handled individually and immediately using the current
  196. cache internal state. */
  197. int
  198. frv_cache_read (FRV_CACHE *, int, SI);
  199. int
  200. frv_cache_write (FRV_CACHE *, SI, char *, unsigned);
  201. int
  202. frv_cache_preload (FRV_CACHE *, SI, USI, int);
  203. int
  204. frv_cache_invalidate (FRV_CACHE *, SI, int);
  205. int
  206. frv_cache_invalidate_all (FRV_CACHE *, int);
  207. /* These functions are used to operate the cache in cycle-accurate mode.
  208. The internal operation of the cache is simulated down to the cycle level. */
  209. #define NO_REQNO 0xffffffff
  210. void
  211. frv_cache_request_load (FRV_CACHE *, unsigned, SI, int);
  212. void
  213. frv_cache_request_store (FRV_CACHE *, SI, int, char *, unsigned);
  214. void
  215. frv_cache_request_invalidate (FRV_CACHE *, unsigned, SI, int, int, int);
  216. void
  217. frv_cache_request_preload (FRV_CACHE *, SI, int, int, int);
  218. void
  219. frv_cache_request_unlock (FRV_CACHE *, SI, int);
  220. void
  221. frv_cache_run (FRV_CACHE *, int);
  222. int
  223. frv_cache_data_in_buffer (FRV_CACHE*, int, SI, unsigned);
  224. int
  225. frv_cache_data_flushed (FRV_CACHE*, int, SI, unsigned);
  226. int
  227. frv_cache_read_passive_SI (FRV_CACHE *, SI, SI *);
  228. #endif /* CACHE_H */