mshr.hh revision 11375
12SN/A/*
21762SN/A * Copyright (c) 2012-2013, 2015-2016 ARM Limited
32SN/A * All rights reserved.
42SN/A *
52SN/A * The license below extends only to copyright in the software and shall
62SN/A * not be construed as granting a license to any other intellectual
72SN/A * property including but not limited to intellectual property relating
82SN/A * to a hardware implementation of the functionality of the software
92SN/A * licensed hereunder.  You may use the software subject to the license
102SN/A * terms below provided that you ensure that this notice is replicated
112SN/A * unmodified and in its entirety in all distributions of the software,
122SN/A * modified or unmodified, in source code or in binary form.
132SN/A *
142SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
152SN/A * All rights reserved.
162SN/A *
172SN/A * Redistribution and use in source and binary forms, with or without
182SN/A * modification, are permitted provided that the following conditions are
192SN/A * met: redistributions of source code must retain the above copyright
202SN/A * notice, this list of conditions and the following disclaimer;
212SN/A * redistributions in binary form must reproduce the above copyright
222SN/A * notice, this list of conditions and the following disclaimer in the
232SN/A * documentation and/or other materials provided with the distribution;
242SN/A * neither the name of the copyright holders nor the names of its
252SN/A * contributors may be used to endorse or promote products derived from
262SN/A * this software without specific prior written permission.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
311464SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
321464SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372972Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
384182Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392984Sgblack@eecs.umich.edu *
402741Sksewell@umich.edu * Authors: Erik Hallnor
4156SN/A */
422439SN/A
4356SN/A/**
442410SN/A * @file
452980Sgblack@eecs.umich.edu * Miss Status and Handling Register (MSHR) declaration.
461717SN/A */
472SN/A
482SN/A#ifndef __MEM_CACHE_MSHR_HH__
491061SN/A#define __MEM_CACHE_MSHR_HH__
502296SN/A
512296SN/A#include <list>
522680Sktlim@umich.edu
53705SN/A#include "base/printable.hh"
542623SN/A#include "mem/cache/queue_entry.hh"
551464SN/A
565597Sgblack@eecs.umich.educlass Cache;
575597Sgblack@eecs.umich.edu
585597Sgblack@eecs.umich.edu/**
595597Sgblack@eecs.umich.edu * Miss Status and handling Register. This class keeps all the information
601061SN/A * needed to handle a cache miss including a list of target requests.
612296SN/A * @sa  \ref gem5MemorySystem "gem5 Memory System"
622296SN/A */
632312SN/Aclass MSHR : public QueueEntry, public Printable
64716SN/A{
652623SN/A
662623SN/A    /**
671128SN/A     * Consider the queues friends to avoid making everything public.
682SN/A     */
694572Sacolyte@umich.edu    template<typename Entry>
702SN/A    friend class Queue;
712SN/A    friend class MSHRQueue;
722SN/A
732SN/A  private:
742SN/A
755677Sgblack@eecs.umich.edu    /** Flag set by downstream caches */
763271Sgblack@eecs.umich.edu    bool downstreamPending;
775665Sgblack@eecs.umich.edu
785665Sgblack@eecs.umich.edu    /**
795665Sgblack@eecs.umich.edu     * Here we use one flag to track both if:
805665Sgblack@eecs.umich.edu     *
815665Sgblack@eecs.umich.edu     * 1. We are going to become owner or not, i.e., we will get the
825665Sgblack@eecs.umich.edu     * block in an ownership state (Owned or Modified) with BlkDirty
835665Sgblack@eecs.umich.edu     * set. This determines whether or not we are going to become the
845665Sgblack@eecs.umich.edu     * responder and ordering point for future requests that we snoop.
855665Sgblack@eecs.umich.edu     *
865665Sgblack@eecs.umich.edu     * 2. We know that we are going to get a writable block, i.e. we
875665Sgblack@eecs.umich.edu     * will get the block in writable state (Exclusive or Modified
885665Sgblack@eecs.umich.edu     * state) with BlkWritable set. That determines whether additional
895665Sgblack@eecs.umich.edu     * targets with needsWritable set will be able to be satisfied, or
905665Sgblack@eecs.umich.edu     * if not should be put on the deferred list to possibly wait for
915665Sgblack@eecs.umich.edu     * another request that does give us writable access.
925665Sgblack@eecs.umich.edu     *
935665Sgblack@eecs.umich.edu     * Condition 2 is actually just a shortcut that saves us from
945665Sgblack@eecs.umich.edu     * possibly building a deferred target list and calling
955665Sgblack@eecs.umich.edu     * promoteWritable() every time we get a writable block. Condition
965665Sgblack@eecs.umich.edu     * 1, tracking ownership, is what is important. However, we never
972SN/A     * receive ownership without marking the block dirty, and
982SN/A     * consequently use pendingModified to track both ownership and
992SN/A     * writability rather than having separate pendingDirty and
1002SN/A     * pendingWritable flags.
1012SN/A     */
1022SN/A    bool pendingModified;
1032SN/A
1042SN/A    /** Did we snoop an invalidate while waiting for data? */
1052SN/A    bool postInvalidate;
1062SN/A
1072SN/A    /** Did we snoop a read while waiting for data? */
1082SN/A    bool postDowngrade;
1092SN/A
1102SN/A  public:
1112SN/A
1122SN/A    /** True if the entry is just a simple forward from an upper level */
1132SN/A    bool isForward;
1142SN/A
1152SN/A    class Target {
1162SN/A      public:
1172SN/A
1182SN/A        enum Source {
119753SN/A            FromCPU,
1202SN/A            FromSnoop,
1212SN/A            FromPrefetcher
1222SN/A        };
123512SN/A
124512SN/A        const Tick recvTime;  //!< Time when request was received (for stats)
125512SN/A        const Tick readyTime; //!< Time when request is ready to be serviced
126512SN/A        const Counter order;  //!< Global order (for memory consistency mgmt)
127512SN/A        const PacketPtr pkt;  //!< Pending request packet.
128512SN/A        const Source source;  //!< Request from cpu, memory, or prefetcher?
1292SN/A        const bool markedPending; //!< Did we mark upstream MSHR
1302SN/A                                  //!< as downstreamPending?
1315543Ssaidi@eecs.umich.edu
1322SN/A        Target(PacketPtr _pkt, Tick _readyTime, Counter _order,
1335543Ssaidi@eecs.umich.edu               Source _source, bool _markedPending)
1345543Ssaidi@eecs.umich.edu            : recvTime(curTick()), readyTime(_readyTime), order(_order),
1352SN/A              pkt(_pkt), source(_source), markedPending(_markedPending)
1365543Ssaidi@eecs.umich.edu        {}
1375543Ssaidi@eecs.umich.edu    };
1385543Ssaidi@eecs.umich.edu
1392336SN/A    class TargetList : public std::list<Target> {
1405222Sksewell@umich.edu
1415543Ssaidi@eecs.umich.edu      public:
1425543Ssaidi@eecs.umich.edu        bool needsWritable;
143593SN/A        bool hasUpgrade;
1442SN/A
1455543Ssaidi@eecs.umich.edu        TargetList();
1465543Ssaidi@eecs.umich.edu        void resetFlags() { needsWritable = hasUpgrade = false; }
1475543Ssaidi@eecs.umich.edu        bool isReset() const { return !needsWritable && !hasUpgrade; }
1485543Ssaidi@eecs.umich.edu        void add(PacketPtr pkt, Tick readyTime, Counter order,
1495543Ssaidi@eecs.umich.edu                 Target::Source source, bool markPending);
1505543Ssaidi@eecs.umich.edu
1515543Ssaidi@eecs.umich.edu        /**
1522SN/A         * Convert upgrades to the equivalent request if the cache line they
1532135SN/A         * refer to would have been invalid (Upgrade -> ReadEx, SC* -> Fail).
1542135SN/A         * Used to rejig ordering between targets waiting on an MSHR. */
1555543Ssaidi@eecs.umich.edu        void replaceUpgrades();
156512SN/A
1575543Ssaidi@eecs.umich.edu        void clearDownstreamPending();
158512SN/A        bool checkFunctional(PacketPtr pkt);
1592103SN/A        void print(std::ostream &os, int verbosity,
1602103SN/A                   const std::string &prefix) const;
1615543Ssaidi@eecs.umich.edu    };
1625543Ssaidi@eecs.umich.edu
1635222Sksewell@umich.edu    /** A list of MSHRs. */
1642SN/A    typedef std::list<MSHR *> List;
165725SN/A    /** MSHR list iterator. */
1662336SN/A    typedef List::iterator Iterator;
1672227SN/A
1682336SN/A    /** Keep track of whether we should allocate on fill or not */
1692336SN/A    bool allocOnFill;
170725SN/A
1714828Sgblack@eecs.umich.edu    /** The pending* and post* flags are only valid if inService is
1724828Sgblack@eecs.umich.edu     *  true.  Using the accessor functions lets us detect if these
1734828Sgblack@eecs.umich.edu     *  flags are accessed improperly.
1743271Sgblack@eecs.umich.edu     */
1754539Sgblack@eecs.umich.edu
1765543Ssaidi@eecs.umich.edu    /** True if we need to get a writable copy of the block. */
1775543Ssaidi@eecs.umich.edu    bool needsWritable() const { return targets.needsWritable; }
1785543Ssaidi@eecs.umich.edu
1795543Ssaidi@eecs.umich.edu    bool isPendingModified() const {
1803271Sgblack@eecs.umich.edu        assert(inService); return pendingModified;
1815543Ssaidi@eecs.umich.edu    }
1825222Sksewell@umich.edu
1833271Sgblack@eecs.umich.edu    bool hasPostInvalidate() const {
1842SN/A        assert(inService); return postInvalidate;
1852SN/A    }
1862SN/A
1872SN/A    bool hasPostDowngrade() const {
1882SN/A        assert(inService); return postDowngrade;
1892SN/A    }
1902SN/A
1912SN/A    bool sendPacket(Cache &cache);
1922SN/A
1932SN/A  private:
1942SN/A
1952SN/A    /**
1962SN/A     * Pointer to this MSHR on the ready list.
1972SN/A     * @sa MissQueue, MSHRQueue::readyList
1982SN/A     */
1992SN/A    Iterator readyIter;
2002SN/A
2012SN/A    /**
2022SN/A     * Pointer to this MSHR on the allocated list.
2032SN/A     * @sa MissQueue, MSHRQueue::allocatedList
2042SN/A     */
2052SN/A    Iterator allocIter;
2062SN/A
2072SN/A    /** List of all requests that match the address */
2082SN/A    TargetList targets;
2092SN/A
2102SN/A    TargetList deferredTargets;
2112SN/A
2122SN/A  public:
2132SN/A
2142SN/A    /**
2152SN/A     * Allocate a miss to this MSHR.
2162SN/A     * @param blk_addr The address of the block.
2172SN/A     * @param blk_size The number of bytes to request.
2182SN/A     * @param pkt The original miss.
2192SN/A     * @param when_ready When should the MSHR be ready to act upon.
2202SN/A     * @param _order The logical order of this MSHR
2212SN/A     * @param alloc_on_fill Should the cache allocate a block on fill
2222SN/A     */
2232SN/A    void allocate(Addr blk_addr, unsigned blk_size, PacketPtr pkt,
2242SN/A                  Tick when_ready, Counter _order, bool alloc_on_fill);
2252SN/A
2262SN/A    void markInService(bool pending_modified_resp);
2272SN/A
2282SN/A    void clearDownstreamPending();
2292SN/A
2302SN/A    /**
2312SN/A     * Mark this MSHR as free.
2322SN/A     */
2332SN/A    void deallocate();
2342SN/A
2352SN/A    /**
2362SN/A     * Add a request to the list of targets.
2372SN/A     * @param target The target.
2382SN/A     */
2392SN/A    void allocateTarget(PacketPtr target, Tick when, Counter order,
2402SN/A                        bool alloc_on_fill);
2415543Ssaidi@eecs.umich.edu    bool handleSnoop(PacketPtr target, Counter order);
2422SN/A
2435543Ssaidi@eecs.umich.edu    /** A simple constructor. */
2445543Ssaidi@eecs.umich.edu    MSHR();
2455543Ssaidi@eecs.umich.edu
2465543Ssaidi@eecs.umich.edu    /**
2472SN/A     * Returns the current number of allocated targets.
2482SN/A     * @return The current number of allocated targets.
249593SN/A     */
2502SN/A    int getNumTargets() const
2515543Ssaidi@eecs.umich.edu    { return targets.size() + deferredTargets.size(); }
2525543Ssaidi@eecs.umich.edu
2532SN/A    /**
2545543Ssaidi@eecs.umich.edu     * Returns true if there are targets left.
2555543Ssaidi@eecs.umich.edu     * @return true if there are targets
2565543Ssaidi@eecs.umich.edu     */
2575543Ssaidi@eecs.umich.edu    bool hasTargets() const { return !targets.empty(); }
2582SN/A
2595543Ssaidi@eecs.umich.edu    /**
2605543Ssaidi@eecs.umich.edu     * Returns a reference to the first target.
2612935Sksewell@umich.edu     * @return A pointer to the first target.
2622SN/A     */
2632SN/A    Target *getTarget()
2642103SN/A    {
2652103SN/A        assert(hasTargets());
2662103SN/A        return &targets.front();
2672103SN/A    }
2682103SN/A
269512SN/A    /**
270512SN/A     * Pop first target.
271725SN/A     */
2722296SN/A    void popTarget()
2732336SN/A    {
2742312SN/A        targets.pop_front();
2754828Sgblack@eecs.umich.edu    }
2764539Sgblack@eecs.umich.edu
2774539Sgblack@eecs.umich.edu    bool promoteDeferredTargets();
2783271Sgblack@eecs.umich.edu
2794539Sgblack@eecs.umich.edu    void promoteWritable();
2804539Sgblack@eecs.umich.edu
2813271Sgblack@eecs.umich.edu    bool checkFunctional(PacketPtr pkt);
2823271Sgblack@eecs.umich.edu
2832SN/A    /**
2842SN/A     * Prints the contents of this MSHR for debugging.
2855335Shines@cs.fsu.edu     */
2862SN/A    void print(std::ostream &os,
2872SN/A               int verbosity = 0,
2882SN/A               const std::string &prefix = "") const;
2892SN/A    /**
2902SN/A     * A no-args wrapper of print(std::ostream...)  meant to be
2912SN/A     * invoked from DPRINTFs avoiding string overheads in fast mode
2922SN/A     *
2932SN/A     * @return string with mshr fields + [deferred]targets
2942SN/A     */
2952SN/A    std::string print() const;
2962SN/A};
2972SN/A
2982SN/A#endif // __MEM_CACHE_MSHR_HH__
2992SN/A