Major changes to how SimObjects are created and initialized. Almost all
[gem5.git] / src / mem / cache / coherence / coherence_protocol.hh
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Erik Hallnor
29 * Ron Dreslinski
30 * Steve Reinhardt
31 */
32
33 /**
34 * @file
35 * Declaration of CoherenceProcotol a basic coherence policy.
36 */
37 #ifndef __COHERENCE_PROTOCOL_HH__
38 #define __COHERENCE_PROTOCOL_HH__
39
40 #include <string>
41
42 #include "base/statistics.hh"
43 #include "enums/Coherence.hh"
44 #include "mem/cache/cache_blk.hh"
45 #include "mem/packet.hh"
46 #include "sim/sim_object.hh"
47
48 class BaseCache;
49 class MSHR;
50
51 /**
52 * A simple coherence policy for the memory hierarchy. Currently implements
53 * MSI, MESI, and MOESI protocols.
54 */
55 class CoherenceProtocol : public SimObject
56 {
57 public:
58 /**
59 * Contruct and initialize this policy.
60 * @param name The name of this policy.
61 * @param protocol The string representation of the protocol to use.
62 * @param doUpgrades True if bus upgrades should be used.
63 */
64 CoherenceProtocol(const std::string &name, Enums::Coherence protocol,
65 const bool doUpgrades);
66
67 /**
68 * Destructor.
69 */
70 virtual ~CoherenceProtocol() {};
71
72 /**
73 * Register statistics
74 */
75 virtual void regStats();
76
77 /**
78 * Get the proper bus command for the given command and status.
79 * @param cmd The request's command.
80 * @param status The current state of the cache block.
81 * @param mshr The MSHR matching the request.
82 * @return The proper bus command, as determined by the protocol.
83 */
84 MemCmd getBusCmd(MemCmd cmd, CacheBlk::State status,
85 MSHR *mshr = NULL);
86
87 /**
88 * Return the proper state given the current state and the bus response.
89 * @param pkt The bus response.
90 * @param oldState The current block state.
91 * @return The new state.
92 */
93 CacheBlk::State getNewState(PacketPtr &pkt,
94 CacheBlk::State oldState);
95
96 /**
97 * Handle snooped bus requests.
98 * @param cache The cache that snooped the request.
99 * @param pkt The snooped bus request.
100 * @param blk The cache block corresponding to the request, if any.
101 * @param mshr The MSHR corresponding to the request, if any.
102 * @param new_state The new coherence state of the block.
103 * @return True if the request should be satisfied locally.
104 */
105 bool handleBusRequest(BaseCache *cache, PacketPtr &pkt, CacheBlk *blk,
106 MSHR *mshr, CacheBlk::State &new_state);
107
108 protected:
109 /** Snoop function type. */
110 typedef bool (*SnoopFuncType)(BaseCache *, PacketPtr &, CacheBlk *,
111 MSHR *, CacheBlk::State&);
112
113 //
114 // Standard snoop transition functions
115 //
116
117 /**
118 * Do nothing transition.
119 */
120 static bool nullTransition(BaseCache *, PacketPtr &, CacheBlk *,
121 MSHR *, CacheBlk::State&);
122
123 /**
124 * Invalid transition, basically panic.
125 */
126 static bool invalidTransition(BaseCache *, PacketPtr &, CacheBlk *,
127 MSHR *, CacheBlk::State&);
128
129 /**
130 * Invalidate block, move to Invalid state.
131 */
132 static bool invalidateTrans(BaseCache *, PacketPtr &, CacheBlk *,
133 MSHR *, CacheBlk::State&);
134
135 /**
136 * Supply data, no state transition.
137 */
138 static bool supplyTrans(BaseCache *, PacketPtr &, CacheBlk *,
139 MSHR *, CacheBlk::State&);
140
141 /**
142 * Supply data and go to Shared state.
143 */
144 static bool supplyAndGotoSharedTrans(BaseCache *, PacketPtr &, CacheBlk *,
145 MSHR *, CacheBlk::State&);
146
147 /**
148 * Supply data and go to Owned state.
149 */
150 static bool supplyAndGotoOwnedTrans(BaseCache *, PacketPtr &, CacheBlk *,
151 MSHR *, CacheBlk::State&);
152
153 /**
154 * Invalidate block, supply data, and go to Invalid state.
155 */
156 static bool supplyAndInvalidateTrans(BaseCache *, PacketPtr &, CacheBlk *,
157 MSHR *, CacheBlk::State&);
158
159 /**
160 * Assert the shared line for a block that is shared/exclusive.
161 */
162 static bool assertShared(BaseCache *, PacketPtr &, CacheBlk *,
163 MSHR *, CacheBlk::State&);
164
165 /**
166 * Definition of protocol state transitions.
167 */
168 class StateTransition
169 {
170 friend class CoherenceProtocol;
171
172 /** The bus command of this transition. */
173 Packet::Command busCmd;
174 /** The state to transition to. */
175 int newState;
176 /** The snoop function for this transition. */
177 SnoopFuncType snoopFunc;
178
179 /**
180 * Constructor, defaults to invalid transition.
181 */
182 StateTransition();
183
184 /**
185 * Initialize bus command.
186 * @param cmd The bus command to use.
187 */
188 void onRequest(Packet::Command cmd)
189 {
190 busCmd = cmd;
191 }
192
193 /**
194 * Set the transition state.
195 * @param s The new state.
196 */
197 void onResponse(CacheBlk::State s)
198 {
199 newState = s;
200 }
201
202 /**
203 * Initialize the snoop function.
204 * @param f The new snoop function.
205 */
206 void onSnoop(SnoopFuncType f)
207 {
208 snoopFunc = f;
209 }
210 };
211
212 friend class CoherenceProtocol::StateTransition;
213
214 /** Mask to select status bits relevant to coherence protocol. */
215 static const int stateMask = BlkValid | BlkWritable | BlkDirty;
216
217 /** The Modified (M) state. */
218 static const int Modified = BlkValid | BlkWritable | BlkDirty;
219 /** The Owned (O) state. */
220 static const int Owned = BlkValid | BlkDirty;
221 /** The Exclusive (E) state. */
222 static const int Exclusive = BlkValid | BlkWritable;
223 /** The Shared (S) state. */
224 static const int Shared = BlkValid;
225 /** The Invalid (I) state. */
226 static const int Invalid = 0;
227
228 /**
229 * Maximum state encoding value (used to size transition lookup
230 * table). Could be more than number of states, depends on
231 * encoding of status bits.
232 */
233 static const int stateMax = stateMask;
234
235 /**
236 * The table of all possible transitions, organized by starting state and
237 * request command.
238 */
239 StateTransition transitionTable[stateMax+1][MemCmd::NUM_MEM_CMDS];
240
241 /**
242 * @addtogroup CoherenceStatistics
243 * @{
244 */
245 /**
246 * State accesses from parent cache.
247 */
248 Stats::Scalar<> requestCount[stateMax+1][MemCmd::NUM_MEM_CMDS];
249 /**
250 * State accesses from snooped requests.
251 */
252 Stats::Scalar<> snoopCount[stateMax+1][MemCmd::NUM_MEM_CMDS];
253 /**
254 * @}
255 */
256 };
257
258 #endif // __COHERENCE_PROTOCOL_HH__