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 "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"
52 * A simple coherence policy for the memory hierarchy. Currently implements
53 * MSI, MESI, and MOESI protocols.
55 class CoherenceProtocol : public SimObject
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.
64 CoherenceProtocol(const std::string &name, Enums::Coherence protocol,
65 const bool doUpgrades);
70 virtual ~CoherenceProtocol() {};
75 virtual void regStats();
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.
84 MemCmd getBusCmd(MemCmd cmd, CacheBlk::State status,
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.
93 CacheBlk::State getNewState(PacketPtr &pkt,
94 CacheBlk::State oldState);
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.
105 bool handleBusRequest(BaseCache *cache, PacketPtr &pkt, CacheBlk *blk,
106 MSHR *mshr, CacheBlk::State &new_state);
109 /** Snoop function type. */
110 typedef bool (*SnoopFuncType)(BaseCache *, PacketPtr &, CacheBlk *,
111 MSHR *, CacheBlk::State&);
114 // Standard snoop transition functions
118 * Do nothing transition.
120 static bool nullTransition(BaseCache *, PacketPtr &, CacheBlk *,
121 MSHR *, CacheBlk::State&);
124 * Invalid transition, basically panic.
126 static bool invalidTransition(BaseCache *, PacketPtr &, CacheBlk *,
127 MSHR *, CacheBlk::State&);
130 * Invalidate block, move to Invalid state.
132 static bool invalidateTrans(BaseCache *, PacketPtr &, CacheBlk *,
133 MSHR *, CacheBlk::State&);
136 * Supply data, no state transition.
138 static bool supplyTrans(BaseCache *, PacketPtr &, CacheBlk *,
139 MSHR *, CacheBlk::State&);
142 * Supply data and go to Shared state.
144 static bool supplyAndGotoSharedTrans(BaseCache *, PacketPtr &, CacheBlk *,
145 MSHR *, CacheBlk::State&);
148 * Supply data and go to Owned state.
150 static bool supplyAndGotoOwnedTrans(BaseCache *, PacketPtr &, CacheBlk *,
151 MSHR *, CacheBlk::State&);
154 * Invalidate block, supply data, and go to Invalid state.
156 static bool supplyAndInvalidateTrans(BaseCache *, PacketPtr &, CacheBlk *,
157 MSHR *, CacheBlk::State&);
160 * Assert the shared line for a block that is shared/exclusive.
162 static bool assertShared(BaseCache *, PacketPtr &, CacheBlk *,
163 MSHR *, CacheBlk::State&);
166 * Definition of protocol state transitions.
168 class StateTransition
170 friend class CoherenceProtocol;
172 /** The bus command of this transition. */
173 Packet::Command busCmd;
174 /** The state to transition to. */
176 /** The snoop function for this transition. */
177 SnoopFuncType snoopFunc;
180 * Constructor, defaults to invalid transition.
185 * Initialize bus command.
186 * @param cmd The bus command to use.
188 void onRequest(Packet::Command cmd)
194 * Set the transition state.
195 * @param s The new state.
197 void onResponse(CacheBlk::State s)
203 * Initialize the snoop function.
204 * @param f The new snoop function.
206 void onSnoop(SnoopFuncType f)
212 friend class CoherenceProtocol::StateTransition;
214 /** Mask to select status bits relevant to coherence protocol. */
215 static const int stateMask = BlkValid | BlkWritable | BlkDirty;
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;
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.
233 static const int stateMax = stateMask;
236 * The table of all possible transitions, organized by starting state and
239 StateTransition transitionTable[stateMax+1][MemCmd::NUM_MEM_CMDS];
242 * @addtogroup CoherenceStatistics
246 * State accesses from parent cache.
248 Stats::Scalar<> requestCount[stateMax+1][MemCmd::NUM_MEM_CMDS];
250 * State accesses from snooped requests.
252 Stats::Scalar<> snoopCount[stateMax+1][MemCmd::NUM_MEM_CMDS];
258 #endif // __COHERENCE_PROTOCOL_HH__