2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Erik Hallnor
35 * Declaration of CoherenceProcotol a basic coherence policy.
37 #ifndef __COHERENCE_PROTOCOL_HH__
38 #define __COHERENCE_PROTOCOL_HH__
42 #include "sim/sim_object.hh"
43 #include "mem/packet.hh"
44 #include "mem/cache/cache_blk.hh"
45 #include "base/statistics.hh"
51 * A simple coherence policy for the memory hierarchy. Currently implements
52 * MSI, MESI, and MOESI protocols.
54 class CoherenceProtocol : public SimObject
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.
63 CoherenceProtocol(const std::string &name, const std::string &protocol,
64 const bool doUpgrades);
69 virtual ~CoherenceProtocol() {};
74 virtual void regStats();
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.
83 Packet::Command getBusCmd(Packet::Command cmd, CacheBlk::State status,
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.
92 CacheBlk::State getNewState(PacketPtr &pkt,
93 CacheBlk::State oldState);
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.
104 bool handleBusRequest(BaseCache *cache, PacketPtr &pkt, CacheBlk *blk,
105 MSHR *mshr, CacheBlk::State &new_state);
108 /** Snoop function type. */
109 typedef bool (*SnoopFuncType)(BaseCache *, PacketPtr &, CacheBlk *,
110 MSHR *, CacheBlk::State&);
113 // Standard snoop transition functions
117 * Do nothing transition.
119 static bool nullTransition(BaseCache *, PacketPtr &, CacheBlk *,
120 MSHR *, CacheBlk::State&);
123 * Invalid transition, basically panic.
125 static bool invalidTransition(BaseCache *, PacketPtr &, CacheBlk *,
126 MSHR *, CacheBlk::State&);
129 * Invalidate block, move to Invalid state.
131 static bool invalidateTrans(BaseCache *, PacketPtr &, CacheBlk *,
132 MSHR *, CacheBlk::State&);
135 * Supply data, no state transition.
137 static bool supplyTrans(BaseCache *, PacketPtr &, CacheBlk *,
138 MSHR *, CacheBlk::State&);
141 * Supply data and go to Shared state.
143 static bool supplyAndGotoSharedTrans(BaseCache *, PacketPtr &, CacheBlk *,
144 MSHR *, CacheBlk::State&);
147 * Supply data and go to Owned state.
149 static bool supplyAndGotoOwnedTrans(BaseCache *, PacketPtr &, CacheBlk *,
150 MSHR *, CacheBlk::State&);
153 * Invalidate block, supply data, and go to Invalid state.
155 static bool supplyAndInvalidateTrans(BaseCache *, PacketPtr &, CacheBlk *,
156 MSHR *, CacheBlk::State&);
159 * Assert the shared line for a block that is shared/exclusive.
161 static bool assertShared(BaseCache *, PacketPtr &, CacheBlk *,
162 MSHR *, CacheBlk::State&);
165 * Definition of protocol state transitions.
167 class StateTransition
169 friend class CoherenceProtocol;
171 /** The bus command of this transition. */
172 Packet::Command busCmd;
173 /** The state to transition to. */
175 /** The snoop function for this transition. */
176 SnoopFuncType snoopFunc;
179 * Constructor, defaults to invalid transition.
184 * Initialize bus command.
185 * @param cmd The bus command to use.
187 void onRequest(Packet::Command cmd)
193 * Set the transition state.
194 * @param s The new state.
196 void onResponse(CacheBlk::State s)
202 * Initialize the snoop function.
203 * @param f The new snoop function.
205 void onSnoop(SnoopFuncType f)
211 friend class CoherenceProtocol::StateTransition;
213 /** Mask to select status bits relevant to coherence protocol. */
214 static const int stateMask = BlkValid | BlkWritable | BlkDirty;
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;
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.
232 static const int stateMax = stateMask;
235 * The table of all possible transitions, organized by starting state and
238 StateTransition transitionTable[stateMax+1][NUM_MEM_CMDS];
241 * @addtogroup CoherenceStatistics
245 * State accesses from parent cache.
247 Stats::Scalar<> requestCount[stateMax+1][NUM_MEM_CMDS];
249 * State accesses from snooped requests.
251 Stats::Scalar<> snoopCount[stateMax+1][NUM_MEM_CMDS];
257 #endif // __COHERENCE_PROTOCOL_HH__