Merge ktlim@zizzer:/bk/newmem
[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 "sim/sim_object.hh"
43 #include "mem/packet.hh"
44 #include "mem/cache/cache_blk.hh"
45 #include "base/statistics.hh"
46
47 class BaseCache;
48 class MSHR;
49
50 /**
51 * A simple coherence policy for the memory hierarchy. Currently implements
52 * MSI, MESI, and MOESI protocols.
53 */
54 class CoherenceProtocol : public SimObject
55 {
56 public:
57 /**
58 * Contruct and initialize this policy.
59 * @param name The name of this policy.
60 * @param protocol The string representation of the protocol to use.
61 * @param doUpgrades True if bus upgrades should be used.
62 */
63 CoherenceProtocol(const std::string &name, const std::string &protocol,
64 const bool doUpgrades);
65
66 /**
67 * Destructor.
68 */
69 virtual ~CoherenceProtocol() {};
70
71 /**
72 * Register statistics
73 */
74 virtual void regStats();
75
76 /**
77 * Get the proper bus command for the given command and status.
78 * @param cmd The request's command.
79 * @param status The current state of the cache block.
80 * @param mshr The MSHR matching the request.
81 * @return The proper bus command, as determined by the protocol.
82 */
83 Packet::Command getBusCmd(Packet::Command cmd, CacheBlk::State status,
84 MSHR *mshr = NULL);
85
86 /**
87 * Return the proper state given the current state and the bus response.
88 * @param pkt The bus response.
89 * @param oldState The current block state.
90 * @return The new state.
91 */
92 CacheBlk::State getNewState(PacketPtr &pkt,
93 CacheBlk::State oldState);
94
95 /**
96 * Handle snooped bus requests.
97 * @param cache The cache that snooped the request.
98 * @param pkt The snooped bus request.
99 * @param blk The cache block corresponding to the request, if any.
100 * @param mshr The MSHR corresponding to the request, if any.
101 * @param new_state The new coherence state of the block.
102 * @return True if the request should be satisfied locally.
103 */
104 bool handleBusRequest(BaseCache *cache, PacketPtr &pkt, CacheBlk *blk,
105 MSHR *mshr, CacheBlk::State &new_state);
106
107 protected:
108 /** Snoop function type. */
109 typedef bool (*SnoopFuncType)(BaseCache *, PacketPtr &, CacheBlk *,
110 MSHR *, CacheBlk::State&);
111
112 //
113 // Standard snoop transition functions
114 //
115
116 /**
117 * Do nothing transition.
118 */
119 static bool nullTransition(BaseCache *, PacketPtr &, CacheBlk *,
120 MSHR *, CacheBlk::State&);
121
122 /**
123 * Invalid transition, basically panic.
124 */
125 static bool invalidTransition(BaseCache *, PacketPtr &, CacheBlk *,
126 MSHR *, CacheBlk::State&);
127
128 /**
129 * Invalidate block, move to Invalid state.
130 */
131 static bool invalidateTrans(BaseCache *, PacketPtr &, CacheBlk *,
132 MSHR *, CacheBlk::State&);
133
134 /**
135 * Supply data, no state transition.
136 */
137 static bool supplyTrans(BaseCache *, PacketPtr &, CacheBlk *,
138 MSHR *, CacheBlk::State&);
139
140 /**
141 * Supply data and go to Shared state.
142 */
143 static bool supplyAndGotoSharedTrans(BaseCache *, PacketPtr &, CacheBlk *,
144 MSHR *, CacheBlk::State&);
145
146 /**
147 * Supply data and go to Owned state.
148 */
149 static bool supplyAndGotoOwnedTrans(BaseCache *, PacketPtr &, CacheBlk *,
150 MSHR *, CacheBlk::State&);
151
152 /**
153 * Invalidate block, supply data, and go to Invalid state.
154 */
155 static bool supplyAndInvalidateTrans(BaseCache *, PacketPtr &, CacheBlk *,
156 MSHR *, CacheBlk::State&);
157
158 /**
159 * Assert the shared line for a block that is shared/exclusive.
160 */
161 static bool assertShared(BaseCache *, PacketPtr &, CacheBlk *,
162 MSHR *, CacheBlk::State&);
163
164 /**
165 * Definition of protocol state transitions.
166 */
167 class StateTransition
168 {
169 friend class CoherenceProtocol;
170
171 /** The bus command of this transition. */
172 Packet::Command busCmd;
173 /** The state to transition to. */
174 int newState;
175 /** The snoop function for this transition. */
176 SnoopFuncType snoopFunc;
177
178 /**
179 * Constructor, defaults to invalid transition.
180 */
181 StateTransition();
182
183 /**
184 * Initialize bus command.
185 * @param cmd The bus command to use.
186 */
187 void onRequest(Packet::Command cmd)
188 {
189 busCmd = cmd;
190 }
191
192 /**
193 * Set the transition state.
194 * @param s The new state.
195 */
196 void onResponse(CacheBlk::State s)
197 {
198 newState = s;
199 }
200
201 /**
202 * Initialize the snoop function.
203 * @param f The new snoop function.
204 */
205 void onSnoop(SnoopFuncType f)
206 {
207 snoopFunc = f;
208 }
209 };
210
211 friend class CoherenceProtocol::StateTransition;
212
213 /** Mask to select status bits relevant to coherence protocol. */
214 static const int stateMask = BlkValid | BlkWritable | BlkDirty;
215
216 /** The Modified (M) state. */
217 static const int Modified = BlkValid | BlkWritable | BlkDirty;
218 /** The Owned (O) state. */
219 static const int Owned = BlkValid | BlkDirty;
220 /** The Exclusive (E) state. */
221 static const int Exclusive = BlkValid | BlkWritable;
222 /** The Shared (S) state. */
223 static const int Shared = BlkValid;
224 /** The Invalid (I) state. */
225 static const int Invalid = 0;
226
227 /**
228 * Maximum state encoding value (used to size transition lookup
229 * table). Could be more than number of states, depends on
230 * encoding of status bits.
231 */
232 static const int stateMax = stateMask;
233
234 /**
235 * The table of all possible transitions, organized by starting state and
236 * request command.
237 */
238 StateTransition transitionTable[stateMax+1][NUM_MEM_CMDS];
239
240 /**
241 * @addtogroup CoherenceStatistics
242 * @{
243 */
244 /**
245 * State accesses from parent cache.
246 */
247 Stats::Scalar<> requestCount[stateMax+1][NUM_MEM_CMDS];
248 /**
249 * State accesses from snooped requests.
250 */
251 Stats::Scalar<> snoopCount[stateMax+1][NUM_MEM_CMDS];
252 /**
253 * @}
254 */
255 };
256
257 #endif // __COHERENCE_PROTOCOL_HH__