lsq_unit.hh revision 2731
12292SN/A/* 22329SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan 32292SN/A * All rights reserved. 42292SN/A * 52292SN/A * Redistribution and use in source and binary forms, with or without 62292SN/A * modification, are permitted provided that the following conditions are 72292SN/A * met: redistributions of source code must retain the above copyright 82292SN/A * notice, this list of conditions and the following disclaimer; 92292SN/A * redistributions in binary form must reproduce the above copyright 102292SN/A * notice, this list of conditions and the following disclaimer in the 112292SN/A * documentation and/or other materials provided with the distribution; 122292SN/A * neither the name of the copyright holders nor the names of its 132292SN/A * contributors may be used to endorse or promote products derived from 142292SN/A * this software without specific prior written permission. 152292SN/A * 162292SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182292SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192292SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202292SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212292SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222292SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232292SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242292SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252292SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262292SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272689Sktlim@umich.edu * 282689Sktlim@umich.edu * Authors: Kevin Lim 292689Sktlim@umich.edu * Korey Sewell 302292SN/A */ 312292SN/A 322292SN/A#ifndef __CPU_O3_LSQ_UNIT_HH__ 332292SN/A#define __CPU_O3_LSQ_UNIT_HH__ 342292SN/A 352329SN/A#include <algorithm> 362292SN/A#include <map> 372292SN/A#include <queue> 382292SN/A 392329SN/A#include "arch/faults.hh" 402292SN/A#include "config/full_system.hh" 412292SN/A#include "base/hashmap.hh" 422292SN/A#include "cpu/inst_seq.hh" 432669Sktlim@umich.edu#include "mem/packet.hh" 442669Sktlim@umich.edu#include "mem/port.hh" 452292SN/A 462292SN/A/** 472329SN/A * Class that implements the actual LQ and SQ for each specific 482329SN/A * thread. Both are circular queues; load entries are freed upon 492329SN/A * committing, while store entries are freed once they writeback. The 502329SN/A * LSQUnit tracks if there are memory ordering violations, and also 512329SN/A * detects partial load to store forwarding cases (a store only has 522329SN/A * part of a load's data) that requires the load to wait until the 532329SN/A * store writes back. In the former case it holds onto the instruction 542329SN/A * until the dependence unit looks at it, and in the latter it stalls 552329SN/A * the LSQ until the store writes back. At that point the load is 562329SN/A * replayed. 572292SN/A */ 582292SN/Atemplate <class Impl> 592292SN/Aclass LSQUnit { 602292SN/A protected: 612292SN/A typedef TheISA::IntReg IntReg; 622292SN/A public: 632292SN/A typedef typename Impl::Params Params; 642292SN/A typedef typename Impl::FullCPU FullCPU; 652292SN/A typedef typename Impl::DynInstPtr DynInstPtr; 662292SN/A typedef typename Impl::CPUPol::IEW IEW; 672292SN/A typedef typename Impl::CPUPol::IssueStruct IssueStruct; 682292SN/A 692292SN/A public: 702292SN/A /** Constructs an LSQ unit. init() must be called prior to use. */ 712292SN/A LSQUnit(); 722292SN/A 732292SN/A /** Initializes the LSQ unit with the specified number of entries. */ 742292SN/A void init(Params *params, unsigned maxLQEntries, 752292SN/A unsigned maxSQEntries, unsigned id); 762292SN/A 772292SN/A /** Returns the name of the LSQ unit. */ 782292SN/A std::string name() const; 792292SN/A 802727Sktlim@umich.edu /** Registers statistics. */ 812727Sktlim@umich.edu void regStats(); 822727Sktlim@umich.edu 832292SN/A /** Sets the CPU pointer. */ 842669Sktlim@umich.edu void setCPU(FullCPU *cpu_ptr); 852292SN/A 862292SN/A /** Sets the IEW stage pointer. */ 872292SN/A void setIEW(IEW *iew_ptr) 882292SN/A { iewStage = iew_ptr; } 892292SN/A 902348SN/A /** Switches out LSQ unit. */ 912307SN/A void switchOut(); 922307SN/A 932348SN/A /** Takes over from another CPU's thread. */ 942307SN/A void takeOverFrom(); 952307SN/A 962348SN/A /** Returns if the LSQ is switched out. */ 972307SN/A bool isSwitchedOut() { return switchedOut; } 982307SN/A 992292SN/A /** Ticks the LSQ unit, which in this case only resets the number of 1002292SN/A * used cache ports. 1012292SN/A * @todo: Move the number of used ports up to the LSQ level so it can 1022292SN/A * be shared by all LSQ units. 1032292SN/A */ 1042292SN/A void tick() { usedPorts = 0; } 1052292SN/A 1062292SN/A /** Inserts an instruction. */ 1072292SN/A void insert(DynInstPtr &inst); 1082292SN/A /** Inserts a load instruction. */ 1092292SN/A void insertLoad(DynInstPtr &load_inst); 1102292SN/A /** Inserts a store instruction. */ 1112292SN/A void insertStore(DynInstPtr &store_inst); 1122292SN/A 1132292SN/A /** Executes a load instruction. */ 1142292SN/A Fault executeLoad(DynInstPtr &inst); 1152292SN/A 1162329SN/A Fault executeLoad(int lq_idx) { panic("Not implemented"); return NoFault; } 1172292SN/A /** Executes a store instruction. */ 1182292SN/A Fault executeStore(DynInstPtr &inst); 1192292SN/A 1202292SN/A /** Commits the head load. */ 1212292SN/A void commitLoad(); 1222292SN/A /** Commits loads older than a specific sequence number. */ 1232292SN/A void commitLoads(InstSeqNum &youngest_inst); 1242292SN/A 1252292SN/A /** Commits stores older than a specific sequence number. */ 1262292SN/A void commitStores(InstSeqNum &youngest_inst); 1272292SN/A 1282292SN/A /** Writes back stores. */ 1292292SN/A void writebackStores(); 1302292SN/A 1312669Sktlim@umich.edu void completeDataAccess(PacketPtr pkt); 1322669Sktlim@umich.edu 1332292SN/A /** Clears all the entries in the LQ. */ 1342292SN/A void clearLQ(); 1352292SN/A 1362292SN/A /** Clears all the entries in the SQ. */ 1372292SN/A void clearSQ(); 1382292SN/A 1392292SN/A /** Resizes the LQ to a given size. */ 1402292SN/A void resizeLQ(unsigned size); 1412292SN/A 1422292SN/A /** Resizes the SQ to a given size. */ 1432292SN/A void resizeSQ(unsigned size); 1442292SN/A 1452292SN/A /** Squashes all instructions younger than a specific sequence number. */ 1462292SN/A void squash(const InstSeqNum &squashed_num); 1472292SN/A 1482292SN/A /** Returns if there is a memory ordering violation. Value is reset upon 1492292SN/A * call to getMemDepViolator(). 1502292SN/A */ 1512292SN/A bool violation() { return memDepViolator; } 1522292SN/A 1532292SN/A /** Returns the memory ordering violator. */ 1542292SN/A DynInstPtr getMemDepViolator(); 1552292SN/A 1562329SN/A /** Returns if a load became blocked due to the memory system. */ 1572292SN/A bool loadBlocked() 1582292SN/A { return isLoadBlocked; } 1592292SN/A 1602348SN/A /** Clears the signal that a load became blocked. */ 1612292SN/A void clearLoadBlocked() 1622292SN/A { isLoadBlocked = false; } 1632292SN/A 1642348SN/A /** Returns if the blocked load was handled. */ 1652292SN/A bool isLoadBlockedHandled() 1662292SN/A { return loadBlockedHandled; } 1672292SN/A 1682348SN/A /** Records the blocked load as being handled. */ 1692292SN/A void setLoadBlockedHandled() 1702292SN/A { loadBlockedHandled = true; } 1712292SN/A 1722292SN/A /** Returns the number of free entries (min of free LQ and SQ entries). */ 1732292SN/A unsigned numFreeEntries(); 1742292SN/A 1752292SN/A /** Returns the number of loads ready to execute. */ 1762292SN/A int numLoadsReady(); 1772292SN/A 1782292SN/A /** Returns the number of loads in the LQ. */ 1792292SN/A int numLoads() { return loads; } 1802292SN/A 1812292SN/A /** Returns the number of stores in the SQ. */ 1822292SN/A int numStores() { return stores; } 1832292SN/A 1842292SN/A /** Returns if either the LQ or SQ is full. */ 1852292SN/A bool isFull() { return lqFull() || sqFull(); } 1862292SN/A 1872292SN/A /** Returns if the LQ is full. */ 1882292SN/A bool lqFull() { return loads >= (LQEntries - 1); } 1892292SN/A 1902292SN/A /** Returns if the SQ is full. */ 1912292SN/A bool sqFull() { return stores >= (SQEntries - 1); } 1922292SN/A 1932292SN/A /** Returns the number of instructions in the LSQ. */ 1942292SN/A unsigned getCount() { return loads + stores; } 1952292SN/A 1962292SN/A /** Returns if there are any stores to writeback. */ 1972292SN/A bool hasStoresToWB() { return storesToWB; } 1982292SN/A 1992292SN/A /** Returns the number of stores to writeback. */ 2002292SN/A int numStoresToWB() { return storesToWB; } 2012292SN/A 2022292SN/A /** Returns if the LSQ unit will writeback on this cycle. */ 2032292SN/A bool willWB() { return storeQueue[storeWBIdx].canWB && 2042678Sktlim@umich.edu !storeQueue[storeWBIdx].completed && 2052678Sktlim@umich.edu !isStoreBlocked; } 2062292SN/A 2072292SN/A private: 2082698Sktlim@umich.edu /** Writes back the instruction, sending it to IEW. */ 2092678Sktlim@umich.edu void writeback(DynInstPtr &inst, PacketPtr pkt); 2102678Sktlim@umich.edu 2112698Sktlim@umich.edu /** Handles completing the send of a store to memory. */ 2122693Sktlim@umich.edu void storePostSend(Packet *pkt); 2132693Sktlim@umich.edu 2142292SN/A /** Completes the store at the specified index. */ 2152292SN/A void completeStore(int store_idx); 2162292SN/A 2172693Sktlim@umich.edu /** Handles doing the retry. */ 2182693Sktlim@umich.edu void recvRetry(); 2192693Sktlim@umich.edu 2202292SN/A /** Increments the given store index (circular queue). */ 2212292SN/A inline void incrStIdx(int &store_idx); 2222292SN/A /** Decrements the given store index (circular queue). */ 2232292SN/A inline void decrStIdx(int &store_idx); 2242292SN/A /** Increments the given load index (circular queue). */ 2252292SN/A inline void incrLdIdx(int &load_idx); 2262292SN/A /** Decrements the given load index (circular queue). */ 2272292SN/A inline void decrLdIdx(int &load_idx); 2282292SN/A 2292329SN/A public: 2302329SN/A /** Debugging function to dump instructions in the LSQ. */ 2312329SN/A void dumpInsts(); 2322329SN/A 2332292SN/A private: 2342292SN/A /** Pointer to the CPU. */ 2352292SN/A FullCPU *cpu; 2362292SN/A 2372292SN/A /** Pointer to the IEW stage. */ 2382292SN/A IEW *iewStage; 2392292SN/A 2402698Sktlim@umich.edu /** Pointer to memory object. */ 2412669Sktlim@umich.edu MemObject *mem; 2422669Sktlim@umich.edu 2432698Sktlim@umich.edu /** DcachePort class for this LSQ Unit. Handles doing the 2442698Sktlim@umich.edu * communication with the cache/memory. 2452698Sktlim@umich.edu * @todo: Needs to be moved to the LSQ level and have some sort 2462698Sktlim@umich.edu * of arbitration. 2472698Sktlim@umich.edu */ 2482669Sktlim@umich.edu class DcachePort : public Port 2492669Sktlim@umich.edu { 2502669Sktlim@umich.edu protected: 2512698Sktlim@umich.edu /** Pointer to CPU. */ 2522669Sktlim@umich.edu FullCPU *cpu; 2532698Sktlim@umich.edu /** Pointer to LSQ. */ 2542669Sktlim@umich.edu LSQUnit *lsq; 2552669Sktlim@umich.edu 2562669Sktlim@umich.edu public: 2572698Sktlim@umich.edu /** Default constructor. */ 2582669Sktlim@umich.edu DcachePort(FullCPU *_cpu, LSQUnit *_lsq) 2592669Sktlim@umich.edu : Port(_lsq->name() + "-dport"), cpu(_cpu), lsq(_lsq) 2602669Sktlim@umich.edu { } 2612669Sktlim@umich.edu 2622669Sktlim@umich.edu protected: 2632698Sktlim@umich.edu /** Atomic version of receive. Panics. */ 2642669Sktlim@umich.edu virtual Tick recvAtomic(PacketPtr pkt); 2652669Sktlim@umich.edu 2662698Sktlim@umich.edu /** Functional version of receive. Panics. */ 2672669Sktlim@umich.edu virtual void recvFunctional(PacketPtr pkt); 2682669Sktlim@umich.edu 2692698Sktlim@umich.edu /** Receives status change. Other than range changing, panics. */ 2702669Sktlim@umich.edu virtual void recvStatusChange(Status status); 2712669Sktlim@umich.edu 2722698Sktlim@umich.edu /** Returns the address ranges of this device. */ 2732669Sktlim@umich.edu virtual void getDeviceAddressRanges(AddrRangeList &resp, 2742669Sktlim@umich.edu AddrRangeList &snoop) 2752669Sktlim@umich.edu { resp.clear(); snoop.clear(); } 2762669Sktlim@umich.edu 2772698Sktlim@umich.edu /** Timing version of receive. Handles writing back and 2782698Sktlim@umich.edu * completing the load or store that has returned from 2792698Sktlim@umich.edu * memory. */ 2802669Sktlim@umich.edu virtual bool recvTiming(PacketPtr pkt); 2812669Sktlim@umich.edu 2822698Sktlim@umich.edu /** Handles doing a retry of the previous send. */ 2832669Sktlim@umich.edu virtual void recvRetry(); 2842669Sktlim@umich.edu }; 2852669Sktlim@umich.edu 2862292SN/A /** Pointer to the D-cache. */ 2872669Sktlim@umich.edu DcachePort *dcachePort; 2882292SN/A 2892698Sktlim@umich.edu /** Derived class to hold any sender state the LSQ needs. */ 2902678Sktlim@umich.edu class LSQSenderState : public Packet::SenderState 2912678Sktlim@umich.edu { 2922678Sktlim@umich.edu public: 2932698Sktlim@umich.edu /** Default constructor. */ 2942678Sktlim@umich.edu LSQSenderState() 2952678Sktlim@umich.edu : noWB(false) 2962678Sktlim@umich.edu { } 2972678Sktlim@umich.edu 2982698Sktlim@umich.edu /** Instruction who initiated the access to memory. */ 2992678Sktlim@umich.edu DynInstPtr inst; 3002698Sktlim@umich.edu /** Whether or not it is a load. */ 3012678Sktlim@umich.edu bool isLoad; 3022698Sktlim@umich.edu /** The LQ/SQ index of the instruction. */ 3032678Sktlim@umich.edu int idx; 3042698Sktlim@umich.edu /** Whether or not the instruction will need to writeback. */ 3052678Sktlim@umich.edu bool noWB; 3062678Sktlim@umich.edu }; 3072678Sktlim@umich.edu 3082698Sktlim@umich.edu /** Writeback event, specifically for when stores forward data to loads. */ 3092678Sktlim@umich.edu class WritebackEvent : public Event { 3102678Sktlim@umich.edu public: 3112678Sktlim@umich.edu /** Constructs a writeback event. */ 3122678Sktlim@umich.edu WritebackEvent(DynInstPtr &_inst, PacketPtr pkt, LSQUnit *lsq_ptr); 3132678Sktlim@umich.edu 3142678Sktlim@umich.edu /** Processes the writeback event. */ 3152678Sktlim@umich.edu void process(); 3162678Sktlim@umich.edu 3172678Sktlim@umich.edu /** Returns the description of this event. */ 3182678Sktlim@umich.edu const char *description(); 3192678Sktlim@umich.edu 3202678Sktlim@umich.edu private: 3212698Sktlim@umich.edu /** Instruction whose results are being written back. */ 3222678Sktlim@umich.edu DynInstPtr inst; 3232678Sktlim@umich.edu 3242698Sktlim@umich.edu /** The packet that would have been sent to memory. */ 3252678Sktlim@umich.edu PacketPtr pkt; 3262678Sktlim@umich.edu 3272678Sktlim@umich.edu /** The pointer to the LSQ unit that issued the store. */ 3282678Sktlim@umich.edu LSQUnit<Impl> *lsqPtr; 3292678Sktlim@umich.edu }; 3302678Sktlim@umich.edu 3312292SN/A public: 3322292SN/A struct SQEntry { 3332292SN/A /** Constructs an empty store queue entry. */ 3342292SN/A SQEntry() 3352292SN/A : inst(NULL), req(NULL), size(0), data(0), 3362292SN/A canWB(0), committed(0), completed(0) 3372292SN/A { } 3382292SN/A 3392292SN/A /** Constructs a store queue entry for a given instruction. */ 3402292SN/A SQEntry(DynInstPtr &_inst) 3412292SN/A : inst(_inst), req(NULL), size(0), data(0), 3422292SN/A canWB(0), committed(0), completed(0) 3432292SN/A { } 3442292SN/A 3452292SN/A /** The store instruction. */ 3462292SN/A DynInstPtr inst; 3472669Sktlim@umich.edu /** The request for the store. */ 3482669Sktlim@umich.edu RequestPtr req; 3492292SN/A /** The size of the store. */ 3502292SN/A int size; 3512292SN/A /** The store data. */ 3522292SN/A IntReg data; 3532292SN/A /** Whether or not the store can writeback. */ 3542292SN/A bool canWB; 3552292SN/A /** Whether or not the store is committed. */ 3562292SN/A bool committed; 3572292SN/A /** Whether or not the store is completed. */ 3582292SN/A bool completed; 3592292SN/A }; 3602329SN/A 3612292SN/A private: 3622292SN/A /** The LSQUnit thread id. */ 3632292SN/A unsigned lsqID; 3642292SN/A 3652292SN/A /** The store queue. */ 3662292SN/A std::vector<SQEntry> storeQueue; 3672292SN/A 3682292SN/A /** The load queue. */ 3692292SN/A std::vector<DynInstPtr> loadQueue; 3702292SN/A 3712329SN/A /** The number of LQ entries, plus a sentinel entry (circular queue). 3722329SN/A * @todo: Consider having var that records the true number of LQ entries. 3732329SN/A */ 3742292SN/A unsigned LQEntries; 3752329SN/A /** The number of SQ entries, plus a sentinel entry (circular queue). 3762329SN/A * @todo: Consider having var that records the true number of SQ entries. 3772329SN/A */ 3782292SN/A unsigned SQEntries; 3792292SN/A 3802292SN/A /** The number of load instructions in the LQ. */ 3812292SN/A int loads; 3822329SN/A /** The number of store instructions in the SQ. */ 3832292SN/A int stores; 3842292SN/A /** The number of store instructions in the SQ waiting to writeback. */ 3852292SN/A int storesToWB; 3862292SN/A 3872292SN/A /** The index of the head instruction in the LQ. */ 3882292SN/A int loadHead; 3892292SN/A /** The index of the tail instruction in the LQ. */ 3902292SN/A int loadTail; 3912292SN/A 3922292SN/A /** The index of the head instruction in the SQ. */ 3932292SN/A int storeHead; 3942329SN/A /** The index of the first instruction that may be ready to be 3952329SN/A * written back, and has not yet been written back. 3962292SN/A */ 3972292SN/A int storeWBIdx; 3982292SN/A /** The index of the tail instruction in the SQ. */ 3992292SN/A int storeTail; 4002292SN/A 4012292SN/A /// @todo Consider moving to a more advanced model with write vs read ports 4022292SN/A /** The number of cache ports available each cycle. */ 4032292SN/A int cachePorts; 4042292SN/A 4052292SN/A /** The number of used cache ports in this cycle. */ 4062292SN/A int usedPorts; 4072292SN/A 4082348SN/A /** Is the LSQ switched out. */ 4092307SN/A bool switchedOut; 4102307SN/A 4112292SN/A //list<InstSeqNum> mshrSeqNums; 4122292SN/A 4132292SN/A /** Wire to read information from the issue stage time queue. */ 4142292SN/A typename TimeBuffer<IssueStruct>::wire fromIssue; 4152292SN/A 4162292SN/A /** Whether or not the LSQ is stalled. */ 4172292SN/A bool stalled; 4182292SN/A /** The store that causes the stall due to partial store to load 4192292SN/A * forwarding. 4202292SN/A */ 4212292SN/A InstSeqNum stallingStoreIsn; 4222292SN/A /** The index of the above store. */ 4232292SN/A int stallingLoadIdx; 4242292SN/A 4252698Sktlim@umich.edu /** The packet that needs to be retried. */ 4262698Sktlim@umich.edu PacketPtr retryPkt; 4272693Sktlim@umich.edu 4282698Sktlim@umich.edu /** Whehter or not a store is blocked due to the memory system. */ 4292678Sktlim@umich.edu bool isStoreBlocked; 4302678Sktlim@umich.edu 4312329SN/A /** Whether or not a load is blocked due to the memory system. */ 4322292SN/A bool isLoadBlocked; 4332292SN/A 4342348SN/A /** Has the blocked load been handled. */ 4352292SN/A bool loadBlockedHandled; 4362292SN/A 4372348SN/A /** The sequence number of the blocked load. */ 4382292SN/A InstSeqNum blockedLoadSeqNum; 4392292SN/A 4402292SN/A /** The oldest load that caused a memory ordering violation. */ 4412292SN/A DynInstPtr memDepViolator; 4422292SN/A 4432292SN/A // Will also need how many read/write ports the Dcache has. Or keep track 4442292SN/A // of that in stage that is one level up, and only call executeLoad/Store 4452292SN/A // the appropriate number of times. 4462292SN/A 4472727Sktlim@umich.edu /** Total number of loads forwaded from LSQ stores. */ 4482727Sktlim@umich.edu Stats::Scalar<> lsqForwLoads; 4492307SN/A 4502727Sktlim@umich.edu /** Total number of loads ignored due to invalid addresses. */ 4512727Sktlim@umich.edu Stats::Scalar<> invAddrLoads; 4522307SN/A 4532727Sktlim@umich.edu /** Total number of squashed loads. */ 4542727Sktlim@umich.edu Stats::Scalar<> lsqSquashedLoads; 4552307SN/A 4562727Sktlim@umich.edu /** Total number of responses from the memory system that are 4572727Sktlim@umich.edu * ignored due to the instruction already being squashed. */ 4582727Sktlim@umich.edu Stats::Scalar<> lsqIgnoredResponses; 4592307SN/A 4602727Sktlim@umich.edu /** Total number of squashed stores. */ 4612727Sktlim@umich.edu Stats::Scalar<> lsqSquashedStores; 4622727Sktlim@umich.edu 4632727Sktlim@umich.edu /** Total number of software prefetches ignored due to invalid addresses. */ 4642727Sktlim@umich.edu Stats::Scalar<> invAddrSwpfs; 4652727Sktlim@umich.edu 4662727Sktlim@umich.edu /** Ready loads blocked due to partial store-forwarding. */ 4672727Sktlim@umich.edu Stats::Scalar<> lsqBlockedLoads; 4682727Sktlim@umich.edu 4692727Sktlim@umich.edu /** Number of loads that were rescheduled. */ 4702727Sktlim@umich.edu Stats::Scalar<> lsqRescheduledLoads; 4712727Sktlim@umich.edu 4722727Sktlim@umich.edu /** Number of times the LSQ is blocked due to the cache. */ 4732727Sktlim@umich.edu Stats::Scalar<> lsqCacheBlocked; 4742727Sktlim@umich.edu 4752292SN/A public: 4762292SN/A /** Executes the load at the given index. */ 4772292SN/A template <class T> 4782669Sktlim@umich.edu Fault read(Request *req, T &data, int load_idx); 4792292SN/A 4802292SN/A /** Executes the store at the given index. */ 4812292SN/A template <class T> 4822669Sktlim@umich.edu Fault write(Request *req, T &data, int store_idx); 4832292SN/A 4842292SN/A /** Returns the index of the head load instruction. */ 4852292SN/A int getLoadHead() { return loadHead; } 4862292SN/A /** Returns the sequence number of the head load instruction. */ 4872292SN/A InstSeqNum getLoadHeadSeqNum() 4882292SN/A { 4892292SN/A if (loadQueue[loadHead]) { 4902292SN/A return loadQueue[loadHead]->seqNum; 4912292SN/A } else { 4922292SN/A return 0; 4932292SN/A } 4942292SN/A 4952292SN/A } 4962292SN/A 4972292SN/A /** Returns the index of the head store instruction. */ 4982292SN/A int getStoreHead() { return storeHead; } 4992292SN/A /** Returns the sequence number of the head store instruction. */ 5002292SN/A InstSeqNum getStoreHeadSeqNum() 5012292SN/A { 5022292SN/A if (storeQueue[storeHead].inst) { 5032292SN/A return storeQueue[storeHead].inst->seqNum; 5042292SN/A } else { 5052292SN/A return 0; 5062292SN/A } 5072292SN/A 5082292SN/A } 5092292SN/A 5102292SN/A /** Returns whether or not the LSQ unit is stalled. */ 5112292SN/A bool isStalled() { return stalled; } 5122292SN/A}; 5132292SN/A 5142292SN/Atemplate <class Impl> 5152292SN/Atemplate <class T> 5162292SN/AFault 5172669Sktlim@umich.eduLSQUnit<Impl>::read(Request *req, T &data, int load_idx) 5182292SN/A{ 5192669Sktlim@umich.edu DynInstPtr load_inst = loadQueue[load_idx]; 5202292SN/A 5212669Sktlim@umich.edu assert(load_inst); 5222669Sktlim@umich.edu 5232669Sktlim@umich.edu assert(!load_inst->isExecuted()); 5242292SN/A 5252292SN/A // Make sure this isn't an uncacheable access 5262292SN/A // A bit of a hackish way to get uncached accesses to work only if they're 5272292SN/A // at the head of the LSQ and are ready to commit (at the head of the ROB 5282292SN/A // too). 5292669Sktlim@umich.edu if (req->getFlags() & UNCACHEABLE && 5302731Sktlim@umich.edu (load_idx != loadHead || !load_inst->isAtCommit())) { 5312669Sktlim@umich.edu iewStage->rescheduleMemInst(load_inst); 5322727Sktlim@umich.edu ++lsqRescheduledLoads; 5332292SN/A return TheISA::genMachineCheckFault(); 5342292SN/A } 5352292SN/A 5362292SN/A // Check the SQ for any previous stores that might lead to forwarding 5372669Sktlim@umich.edu int store_idx = load_inst->sqIdx; 5382292SN/A 5392292SN/A int store_size = 0; 5402292SN/A 5412292SN/A DPRINTF(LSQUnit, "Read called, load idx: %i, store idx: %i, " 5422292SN/A "storeHead: %i addr: %#x\n", 5432669Sktlim@umich.edu load_idx, store_idx, storeHead, req->getPaddr()); 5442292SN/A 5452693Sktlim@umich.edu#if FULL_SYSTEM 5462669Sktlim@umich.edu if (req->getFlags() & LOCKED) { 5472669Sktlim@umich.edu cpu->lockAddr = req->getPaddr(); 5482292SN/A cpu->lockFlag = true; 5492292SN/A } 5502292SN/A#endif 5512292SN/A 5522292SN/A while (store_idx != -1) { 5532292SN/A // End once we've reached the top of the LSQ 5542292SN/A if (store_idx == storeWBIdx) { 5552292SN/A break; 5562292SN/A } 5572292SN/A 5582292SN/A // Move the index to one younger 5592292SN/A if (--store_idx < 0) 5602292SN/A store_idx += SQEntries; 5612292SN/A 5622292SN/A assert(storeQueue[store_idx].inst); 5632292SN/A 5642292SN/A store_size = storeQueue[store_idx].size; 5652292SN/A 5662292SN/A if (store_size == 0) 5672292SN/A continue; 5682292SN/A 5692292SN/A // Check if the store data is within the lower and upper bounds of 5702292SN/A // addresses that the request needs. 5712292SN/A bool store_has_lower_limit = 5722669Sktlim@umich.edu req->getVaddr() >= storeQueue[store_idx].inst->effAddr; 5732292SN/A bool store_has_upper_limit = 5742669Sktlim@umich.edu (req->getVaddr() + req->getSize()) <= 5752669Sktlim@umich.edu (storeQueue[store_idx].inst->effAddr + store_size); 5762292SN/A bool lower_load_has_store_part = 5772669Sktlim@umich.edu req->getVaddr() < (storeQueue[store_idx].inst->effAddr + 5782292SN/A store_size); 5792292SN/A bool upper_load_has_store_part = 5802669Sktlim@umich.edu (req->getVaddr() + req->getSize()) > 5812669Sktlim@umich.edu storeQueue[store_idx].inst->effAddr; 5822292SN/A 5832292SN/A // If the store's data has all of the data needed, we can forward. 5842292SN/A if (store_has_lower_limit && store_has_upper_limit) { 5852329SN/A // Get shift amount for offset into the store's data. 5862669Sktlim@umich.edu int shift_amt = req->getVaddr() & (store_size - 1); 5872329SN/A // @todo: Magic number, assumes byte addressing 5882292SN/A shift_amt = shift_amt << 3; 5892292SN/A 5902292SN/A // Cast this to type T? 5912292SN/A data = storeQueue[store_idx].data >> shift_amt; 5922292SN/A 5932669Sktlim@umich.edu assert(!load_inst->memData); 5942669Sktlim@umich.edu load_inst->memData = new uint8_t[64]; 5952292SN/A 5962669Sktlim@umich.edu memcpy(load_inst->memData, &data, req->getSize()); 5972292SN/A 5982292SN/A DPRINTF(LSQUnit, "Forwarding from store idx %i to load to " 5992292SN/A "addr %#x, data %#x\n", 6002693Sktlim@umich.edu store_idx, req->getVaddr(), data); 6012678Sktlim@umich.edu 6022678Sktlim@umich.edu PacketPtr data_pkt = new Packet(req, Packet::ReadReq, Packet::Broadcast); 6032678Sktlim@umich.edu data_pkt->dataStatic(load_inst->memData); 6042678Sktlim@umich.edu 6052678Sktlim@umich.edu WritebackEvent *wb = new WritebackEvent(load_inst, data_pkt, this); 6062292SN/A 6072292SN/A // We'll say this has a 1 cycle load-store forwarding latency 6082292SN/A // for now. 6092292SN/A // @todo: Need to make this a parameter. 6102292SN/A wb->schedule(curTick); 6112678Sktlim@umich.edu 6122727Sktlim@umich.edu ++lsqForwLoads; 6132292SN/A return NoFault; 6142292SN/A } else if ((store_has_lower_limit && lower_load_has_store_part) || 6152292SN/A (store_has_upper_limit && upper_load_has_store_part) || 6162292SN/A (lower_load_has_store_part && upper_load_has_store_part)) { 6172292SN/A // This is the partial store-load forwarding case where a store 6182292SN/A // has only part of the load's data. 6192292SN/A 6202292SN/A // If it's already been written back, then don't worry about 6212292SN/A // stalling on it. 6222292SN/A if (storeQueue[store_idx].completed) { 6232292SN/A continue; 6242292SN/A } 6252292SN/A 6262292SN/A // Must stall load and force it to retry, so long as it's the oldest 6272292SN/A // load that needs to do so. 6282292SN/A if (!stalled || 6292292SN/A (stalled && 6302669Sktlim@umich.edu load_inst->seqNum < 6312292SN/A loadQueue[stallingLoadIdx]->seqNum)) { 6322292SN/A stalled = true; 6332292SN/A stallingStoreIsn = storeQueue[store_idx].inst->seqNum; 6342292SN/A stallingLoadIdx = load_idx; 6352292SN/A } 6362292SN/A 6372292SN/A // Tell IQ/mem dep unit that this instruction will need to be 6382292SN/A // rescheduled eventually 6392669Sktlim@umich.edu iewStage->rescheduleMemInst(load_inst); 6402727Sktlim@umich.edu ++lsqRescheduledLoads; 6412292SN/A 6422292SN/A // Do not generate a writeback event as this instruction is not 6432292SN/A // complete. 6442292SN/A DPRINTF(LSQUnit, "Load-store forwarding mis-match. " 6452292SN/A "Store idx %i to load addr %#x\n", 6462669Sktlim@umich.edu store_idx, req->getVaddr()); 6472292SN/A 6482727Sktlim@umich.edu ++lsqBlockedLoads; 6492292SN/A return NoFault; 6502292SN/A } 6512292SN/A } 6522292SN/A 6532292SN/A // If there's no forwarding case, then go access memory 6542669Sktlim@umich.edu DPRINTF(LSQUnit, "Doing functional access for inst [sn:%lli] PC %#x\n", 6552669Sktlim@umich.edu load_inst->seqNum, load_inst->readPC()); 6562292SN/A 6572669Sktlim@umich.edu assert(!load_inst->memData); 6582669Sktlim@umich.edu load_inst->memData = new uint8_t[64]; 6592292SN/A 6602292SN/A ++usedPorts; 6612292SN/A 6622669Sktlim@umich.edu DPRINTF(LSQUnit, "Doing timing access for inst PC %#x\n", 6632669Sktlim@umich.edu load_inst->readPC()); 6642669Sktlim@umich.edu 6652669Sktlim@umich.edu PacketPtr data_pkt = new Packet(req, Packet::ReadReq, Packet::Broadcast); 6662669Sktlim@umich.edu data_pkt->dataStatic(load_inst->memData); 6672669Sktlim@umich.edu 6682678Sktlim@umich.edu LSQSenderState *state = new LSQSenderState; 6692678Sktlim@umich.edu state->isLoad = true; 6702678Sktlim@umich.edu state->idx = load_idx; 6712678Sktlim@umich.edu state->inst = load_inst; 6722678Sktlim@umich.edu data_pkt->senderState = state; 6732678Sktlim@umich.edu 6742292SN/A // if we have a cache, do cache access too 6752669Sktlim@umich.edu if (!dcachePort->sendTiming(data_pkt)) { 6762727Sktlim@umich.edu ++lsqCacheBlocked; 6772669Sktlim@umich.edu // There's an older load that's already going to squash. 6782669Sktlim@umich.edu if (isLoadBlocked && blockedLoadSeqNum < load_inst->seqNum) 6792669Sktlim@umich.edu return NoFault; 6802292SN/A 6812669Sktlim@umich.edu // Record that the load was blocked due to memory. This 6822669Sktlim@umich.edu // load will squash all instructions after it, be 6832669Sktlim@umich.edu // refetched, and re-executed. 6842669Sktlim@umich.edu isLoadBlocked = true; 6852669Sktlim@umich.edu loadBlockedHandled = false; 6862669Sktlim@umich.edu blockedLoadSeqNum = load_inst->seqNum; 6872669Sktlim@umich.edu // No fault occurred, even though the interface is blocked. 6882669Sktlim@umich.edu return NoFault; 6892292SN/A } 6902292SN/A 6912669Sktlim@umich.edu if (data_pkt->result != Packet::Success) { 6922669Sktlim@umich.edu DPRINTF(LSQUnit, "LSQUnit: D-cache miss!\n"); 6932669Sktlim@umich.edu DPRINTF(Activity, "Activity: ld accessing mem miss [sn:%lli]\n", 6942669Sktlim@umich.edu load_inst->seqNum); 6952669Sktlim@umich.edu } else { 6962669Sktlim@umich.edu DPRINTF(LSQUnit, "LSQUnit: D-cache hit!\n"); 6972669Sktlim@umich.edu DPRINTF(Activity, "Activity: ld accessing mem hit [sn:%lli]\n", 6982669Sktlim@umich.edu load_inst->seqNum); 6992669Sktlim@umich.edu } 7002669Sktlim@umich.edu 7012669Sktlim@umich.edu return NoFault; 7022292SN/A} 7032292SN/A 7042292SN/Atemplate <class Impl> 7052292SN/Atemplate <class T> 7062292SN/AFault 7072669Sktlim@umich.eduLSQUnit<Impl>::write(Request *req, T &data, int store_idx) 7082292SN/A{ 7092292SN/A assert(storeQueue[store_idx].inst); 7102292SN/A 7112292SN/A DPRINTF(LSQUnit, "Doing write to store idx %i, addr %#x data %#x" 7122292SN/A " | storeHead:%i [sn:%i]\n", 7132669Sktlim@umich.edu store_idx, req->getPaddr(), data, storeHead, 7142292SN/A storeQueue[store_idx].inst->seqNum); 7152329SN/A 7162292SN/A storeQueue[store_idx].req = req; 7172292SN/A storeQueue[store_idx].size = sizeof(T); 7182292SN/A storeQueue[store_idx].data = data; 7192329SN/A 7202292SN/A // This function only writes the data to the store queue, so no fault 7212292SN/A // can happen here. 7222292SN/A return NoFault; 7232292SN/A} 7242292SN/A 7252292SN/A#endif // __CPU_O3_LSQ_UNIT_HH__ 726