TEMU  4.4
The Terma Emulator
Cache.h
Go to the documentation of this file.
1 //===------------------------------------------------------------*- C++ -*-===//
2 //
3 // TEMU: The Terma Emulator
4 // (c) Terma 2015
5 // Authors: Mattias Holm <maho (at) terma.com>
6 //
7 //===----------------------------------------------------------------------===//
8 
9 /*
10  * WARNING: The interfaces defined in this header are at present
11  * considered: EXPERIMENTAL!!! In particularly, this means
12  * that there is no guaranteed API stability for the cache
13  * interfaces at the moment.
14  */
15 
16 #ifndef TEMU_CACHE_H
17 #define TEMU_CACHE_H
18 
19 #include "temu-c/Support/Objsys.h"
20 #include "temu-c/Memory/Memory.h"
21 
22 #include <stdint.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 // List of cache algorithms and corresponding numbers, numbers 0
29 // through 128 are reserved. Note that NONE implies that the cache is
30 // directly mapped, i.e. sets should be 1.
31 #define TEMU_CACHE_NONE 0
32 #define TEMU_CACHE_LRU 1
33 #define TEMU_CACHE_LRR 2
34 #define TEMU_CACHE_RND 3
35 
36 //! Caches implement the memory access interface and the cache control
37 //! interface. For invalidate and evict operation the address given is
38 //! a physical address which should be mapped to a line. For Harward
39 //! style caches, the cache model would typically implement the
40 //! interface twice, once for instructions and once for data.
41 
42 //! Invalidation: cache line will be tagged as invalid (i.e. flushed)
43 //! Eviction: cache line will be invalidated and in writeBack caches
44 //! the content will be written back to memory.
45 
46 typedef struct temu_CacheIface {
47  void (*enable)(void *Obj); //!< Enable cache
48  void (*disable)(void *Obj); //!< Disable cache
49  void (*freeze)(void *Obj); //!< Freeze cache
50 
51  void (*lockLine)(void *Obj, uint64_t Addr); //!< Lock line for addr
52  void (*unlockLine)(void *Obj, uint64_t Addr); //!< Unlock line
53 
54 
55  void (*invalidateAll)(void *Obj); //!< Invalidate entire cache
56  void (*invalidateLine)(void *Obj, uint64_t Addr); //!< Invalidate single line
57 
58  void (*evictAll)(void *Obj); //!< Evict all (i.e. spill to memory)
59  void (*evictLine)(void *Obj, uint64_t Addr); //!< Evict line
60 
61  //! Interrogation of cache properties
62  //! - A directly mapped cache has 1 way, but typically several sets
63  //! - A fully associative cache has 1 set, but typically several ways
64  //! - Total cache size in bytes is sets * ways * linesize
65  uint32_t (*getReplacementPolicy)(void *Obj);
66  //! Get number of sets
67  uint32_t (*getSets)(void *Obj);
68  //! Get number of ways
69  uint32_t (*getWays)(void *Obj);
70  //! Get line size in bytes
71  uint32_t (*getLineSize)(void *Obj);
72 
73  //! Check if an address belong to a cached line
74  int (*isValid)(void *Obj, uint64_t Addr);
75 
76  //! Access the flags data, the format is cache model specific and can
77  //! for specific models include also the tag. Typically, this is used
78  //! for diagnostic access for more accurate cache models. Note that
79  //! addr is not a physical address, but rather an address interpreted
80  //! differently for different caches. I.e. the address is in a cache
81  //! local address space.
82  uint64_t (*readFlags)(void *Obj, uint64_t Addr);
83 
84  //! Write flags data
85  void (*writeFlags)(void *Obj, uint64_t Addr, uint64_t Flags);
86 
87  //! Get cached data for the given address. The data returned comes
88  //! from RAM using a normal memory transaction in case the cache is
89  //! modelled for timing only, otherwise it comes from the cache
90  //! model. Addr is a cache local address space, which depends on the
91  //! cache implementation. The functions are intended for diagnostic
92  //! cache access which is implemented in some systems. Note that on
93  //! writes, the data will NOT be written to memory except in the case
94  //! the cache line would be evicted in an accurate cache model. All
95  //! reads from valid addresses succeed, other addresses are
96  //! undefined. writeData returns non-zero on failure (e.g. invalid
97  //! address).
98  uint64_t (*readData)(void *Obj, uint64_t Addr);
99 
100  //! Write data in cache
101  int (*writeData)(void *Obj, uint64_t Addr, uint64_t Data);
102 } temu_CacheIface;
103 #define TEMU_CACHE_IFACE_TYPE "temu::CacheIface"
104 TEMU_IFACE_REFERENCE_TYPE(temu_Cache);
105 
106 //! The cache control interface can be implemented by a cache
107 //! controller. Which may be embedded in a CPU or device
108 //! model. Typically, the assumption is that the cache controller will
109 //! turn on and off the cache and that it may need to be notified on
110 //! global eviction operations, which may or may not be reflected in
111 //! the cache controllers status registers.
112 typedef struct temu_CacheCtrlIface {
113  //! Global evict operation ongoing
114  void (*evictionInProgress)(void *Obj);
115 
116  //! Global evict operation completed
117  void (*evictionCompleted)(void *Obj);
118 } temu_CacheCtrlIface;
119 #define TEMU_CACHE_CTRL_IFACE_TYPE "temu::CacheCtrlIface"
120 TEMU_IFACE_REFERENCE_TYPE(temu_CacheCtrl);
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif /* ! TEMU_CACHE_H */
temu_CacheIface::writeData
int(* writeData)(void *Obj, uint64_t Addr, uint64_t Data)
Write data in cache.
Definition: Cache.h:101
temu_CacheIface::invalidateLine
void(* invalidateLine)(void *Obj, uint64_t Addr)
Invalidate single line.
Definition: Cache.h:56
temu_CacheCtrlIface::evictionInProgress
void(* evictionInProgress)(void *Obj)
Global evict operation ongoing.
Definition: Cache.h:114
temu_CacheIface::invalidateAll
void(* invalidateAll)(void *Obj)
Invalidate entire cache.
Definition: Cache.h:55
temu_CacheCtrlIface::evictionCompleted
void(* evictionCompleted)(void *Obj)
Global evict operation completed.
Definition: Cache.h:117
temu_CacheIface::freeze
void(* freeze)(void *Obj)
Freeze cache.
Definition: Cache.h:49
temu_CacheIface::evictAll
void(* evictAll)(void *Obj)
Evict all (i.e. spill to memory)
Definition: Cache.h:58
temu_CacheIface::enable
void(* enable)(void *Obj)
Enable cache.
Definition: Cache.h:47
temu_CacheCtrlIface
Definition: Cache.h:112
temu_CacheIface::evictLine
void(* evictLine)(void *Obj, uint64_t Addr)
Evict line.
Definition: Cache.h:59
temu_CacheIface::unlockLine
void(* unlockLine)(void *Obj, uint64_t Addr)
Unlock line.
Definition: Cache.h:52
temu_CacheIface::lockLine
void(* lockLine)(void *Obj, uint64_t Addr)
Lock line for addr.
Definition: Cache.h:51
temu_CacheIface
Definition: Cache.h:46
temu_CacheIface::isValid
int(* isValid)(void *Obj, uint64_t Addr)
Check if an address belong to a cached line.
Definition: Cache.h:74
temu_CacheIface::writeFlags
void(* writeFlags)(void *Obj, uint64_t Addr, uint64_t Flags)
Write flags data.
Definition: Cache.h:85
temu_CacheIface::disable
void(* disable)(void *Obj)
Disable cache.
Definition: Cache.h:48