mshr.hh revision 9725
1/*
2 * Copyright (c) 2012-2013 ARM Limited
3 * All rights reserved.
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Erik Hallnor
41 */
42
43/**
44 * @file
45 * Miss Status and Handling Register (MSHR) declaration.
46 */
47
48#ifndef __MSHR_HH__
49#define __MSHR_HH__
50
51#include <list>
52
53#include "base/printable.hh"
54#include "mem/packet.hh"
55
56class CacheBlk;
57class MSHRQueue;
58
59/**
60 * Miss Status and handling Register. This class keeps all the information
61 * needed to handle a cache miss including a list of target requests.
62 * @sa  \ref gem5MemorySystem "gem5 Memory System"
63 */
64class MSHR : public Packet::SenderState, public Printable
65{
66
67    /**
68     * Consider the MSHRQueue a friend to avoid making everything public
69     */
70    friend class MSHRQueue;
71
72  private:
73
74    /** Cycle when ready to issue */
75    Tick readyTime;
76
77    /** True if the request is uncacheable */
78    bool _isUncacheable;
79
80    /** Flag set by downstream caches */
81    bool downstreamPending;
82
83    /** Will we have a dirty copy after this request? */
84    bool pendingDirty;
85
86    /** Did we snoop an invalidate while waiting for data? */
87    bool postInvalidate;
88
89    /** Did we snoop a read while waiting for data? */
90    bool postDowngrade;
91
92  public:
93
94    class Target {
95      public:
96
97        enum Source {
98            FromCPU,
99            FromSnoop,
100            FromPrefetcher
101        };
102
103        Tick recvTime;  //!< Time when request was received (for stats)
104        Tick readyTime; //!< Time when request is ready to be serviced
105        Counter order;  //!< Global order (for memory consistency mgmt)
106        PacketPtr pkt;  //!< Pending request packet.
107        Source source;  //!< Did request come from cpu, memory, or prefetcher?
108        bool markedPending; //!< Did we mark upstream MSHR
109                            //!<  as downstreamPending?
110
111        Target(PacketPtr _pkt, Tick _readyTime, Counter _order,
112               Source _source, bool _markedPending)
113            : recvTime(curTick()), readyTime(_readyTime), order(_order),
114              pkt(_pkt), source(_source), markedPending(_markedPending)
115        {}
116    };
117
118    class TargetList : public std::list<Target> {
119        /** Target list iterator. */
120        typedef std::list<Target>::iterator Iterator;
121        typedef std::list<Target>::const_iterator ConstIterator;
122
123      public:
124        bool needsExclusive;
125        bool hasUpgrade;
126
127        TargetList();
128        void resetFlags() { needsExclusive = hasUpgrade = false; }
129        bool isReset()    { return !needsExclusive && !hasUpgrade; }
130        void add(PacketPtr pkt, Tick readyTime, Counter order,
131                 Target::Source source, bool markPending);
132        void replaceUpgrades();
133        void clearDownstreamPending();
134        bool checkFunctional(PacketPtr pkt);
135        void print(std::ostream &os, int verbosity,
136                   const std::string &prefix) const;
137    };
138
139    /** A list of MSHRs. */
140    typedef std::list<MSHR *> List;
141    /** MSHR list iterator. */
142    typedef List::iterator Iterator;
143    /** MSHR list const_iterator. */
144    typedef List::const_iterator ConstIterator;
145
146    /** Pointer to queue containing this MSHR. */
147    MSHRQueue *queue;
148
149    /** Order number assigned by the miss queue. */
150    Counter order;
151
152    /** Address of the request. */
153    Addr addr;
154
155    /** Size of the request. */
156    int size;
157
158    /** True if the request has been sent to the bus. */
159    bool inService;
160
161    /** True if the request is just a simple forward from an upper level */
162    bool isForward;
163
164    /** The pending* and post* flags are only valid if inService is
165     *  true.  Using the accessor functions lets us detect if these
166     *  flags are accessed improperly.
167     */
168
169    /** True if we need to get an exclusive copy of the block. */
170    bool needsExclusive() const { return targets.needsExclusive; }
171
172    bool isPendingDirty() const {
173        assert(inService); return pendingDirty;
174    }
175
176    bool hasPostInvalidate() const {
177        assert(inService); return postInvalidate;
178    }
179
180    bool hasPostDowngrade() const {
181        assert(inService); return postDowngrade;
182    }
183
184    /** Thread number of the miss. */
185    ThreadID threadNum;
186
187  private:
188
189    /** Data buffer (if needed).  Currently used only for pending
190     * upgrade handling. */
191    uint8_t *data;
192
193    /**
194     * Pointer to this MSHR on the ready list.
195     * @sa MissQueue, MSHRQueue::readyList
196     */
197    Iterator readyIter;
198
199    /**
200     * Pointer to this MSHR on the allocated list.
201     * @sa MissQueue, MSHRQueue::allocatedList
202     */
203    Iterator allocIter;
204
205    /** List of all requests that match the address */
206    TargetList targets;
207
208    TargetList deferredTargets;
209
210  public:
211
212    bool isUncacheable() const { return _isUncacheable; }
213
214    /**
215     * Allocate a miss to this MSHR.
216     * @param cmd The requesting command.
217     * @param addr The address of the miss.
218     * @param asid The address space id of the miss.
219     * @param size The number of bytes to request.
220     * @param pkt  The original miss.
221     */
222    void allocate(Addr addr, int size, PacketPtr pkt,
223                  Tick when, Counter _order);
224
225    bool markInService(PacketPtr pkt);
226
227    void clearDownstreamPending();
228
229    /**
230     * Mark this MSHR as free.
231     */
232    void deallocate();
233
234    /**
235     * Add a request to the list of targets.
236     * @param target The target.
237     */
238    void allocateTarget(PacketPtr target, Tick when, Counter order);
239    bool handleSnoop(PacketPtr target, Counter order);
240
241    /** A simple constructor. */
242    MSHR();
243
244    /**
245     * Returns the current number of allocated targets.
246     * @return The current number of allocated targets.
247     */
248    int getNumTargets() const
249    { return targets.size() + deferredTargets.size(); }
250
251    /**
252     * Returns true if there are targets left.
253     * @return true if there are targets
254     */
255    bool hasTargets() const { return !targets.empty(); }
256
257    /**
258     * Returns a reference to the first target.
259     * @return A pointer to the first target.
260     */
261    Target *getTarget()
262    {
263        assert(hasTargets());
264        return &targets.front();
265    }
266
267    /**
268     * Pop first target.
269     */
270    void popTarget()
271    {
272        targets.pop_front();
273    }
274
275    bool isForwardNoResponse() const
276    {
277        if (getNumTargets() != 1)
278            return false;
279        const Target *tgt = &targets.front();
280        return tgt->source == Target::FromCPU && !tgt->pkt->needsResponse();
281    }
282
283    bool promoteDeferredTargets();
284
285    void handleFill(Packet *pkt, CacheBlk *blk);
286
287    bool checkFunctional(PacketPtr pkt);
288
289    /**
290     * Prints the contents of this MSHR for debugging.
291     */
292    void print(std::ostream &os,
293               int verbosity = 0,
294               const std::string &prefix = "") const;
295    /**
296     * A no-args wrapper of print(std::ostream...)  meant to be
297     * invoked from DPRINTFs avoiding string overheads in fast mode
298     *
299     * @return string with mshr fields + [deferred]targets
300     */
301    std::string print() const;
302};
303
304#endif //__MSHR_HH__
305