lsq_unit.hh revision 2927:62f1518ae800
12330SN/A/*
22330SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
32330SN/A * All rights reserved.
42330SN/A *
52330SN/A * Redistribution and use in source and binary forms, with or without
62330SN/A * modification, are permitted provided that the following conditions are
72330SN/A * met: redistributions of source code must retain the above copyright
82330SN/A * notice, this list of conditions and the following disclaimer;
92330SN/A * redistributions in binary form must reproduce the above copyright
102330SN/A * notice, this list of conditions and the following disclaimer in the
112330SN/A * documentation and/or other materials provided with the distribution;
122330SN/A * neither the name of the copyright holders nor the names of its
132330SN/A * contributors may be used to endorse or promote products derived from
142330SN/A * this software without specific prior written permission.
152330SN/A *
162330SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172330SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182330SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192330SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202330SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212330SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222330SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232330SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242330SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252330SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262330SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272689Sktlim@umich.edu *
282689Sktlim@umich.edu * Authors: Kevin Lim
292330SN/A *          Korey Sewell
302292SN/A */
312292SN/A
322292SN/A#ifndef __CPU_O3_LSQ_UNIT_HH__
332292SN/A#define __CPU_O3_LSQ_UNIT_HH__
342980Sgblack@eecs.umich.edu
356658Snate@binkert.org#include <algorithm>
368229Snate@binkert.org#include <map>
372362SN/A#include <queue>
382680Sktlim@umich.edu
392683Sktlim@umich.edu#include "arch/faults.hh"
402683Sktlim@umich.edu#include "config/full_system.hh"
412678Sktlim@umich.edu#include "base/hashmap.hh"
422292SN/A#include "cpu/inst_seq.hh"
432292SN/A#include "mem/packet_impl.hh"
442292SN/A#include "mem/port.hh"
453548Sgblack@eecs.umich.edu
463548Sgblack@eecs.umich.edu/**
473548Sgblack@eecs.umich.edu * Class that implements the actual LQ and SQ for each specific
488902Sandreas.hansson@arm.com * thread.  Both are circular queues; load entries are freed upon
498902Sandreas.hansson@arm.com * committing, while store entries are freed once they writeback. The
502292SN/A * LSQUnit tracks if there are memory ordering violations, and also
512862Sktlim@umich.edu * detects partial load to store forwarding cases (a store only has
522862Sktlim@umich.edu * part of a load's data) that requires the load to wait until the
532330SN/A * store writes back. In the former case it holds onto the instruction
542330SN/A * until the dependence unit looks at it, and in the latter it stalls
552330SN/A * the LSQ until the store writes back. At that point the load is
562330SN/A * replayed.
572330SN/A */
582330SN/Atemplate <class Impl>
592292SN/Aclass LSQUnit {
602683Sktlim@umich.edu  protected:
612683Sktlim@umich.edu    typedef TheISA::IntReg IntReg;
626331Sgblack@eecs.umich.edu  public:
632683Sktlim@umich.edu    typedef typename Impl::Params Params;
648735Sandreas.hanson@arm.com    typedef typename Impl::O3CPU O3CPU;
653486Sktlim@umich.edu    typedef typename Impl::DynInstPtr DynInstPtr;
662862Sktlim@umich.edu    typedef typename Impl::CPUPol::IEW IEW;
672862Sktlim@umich.edu    typedef typename Impl::CPUPol::LSQ LSQ;
682862Sktlim@umich.edu    typedef typename Impl::CPUPol::IssueStruct IssueStruct;
692862Sktlim@umich.edu
705712Shsul@eecs.umich.edu  public:
712683Sktlim@umich.edu    /** Constructs an LSQ unit. init() must be called prior to use. */
725714Shsul@eecs.umich.edu    LSQUnit();
735714Shsul@eecs.umich.edu
745714Shsul@eecs.umich.edu    /** Initializes the LSQ unit with the specified number of entries. */
755714Shsul@eecs.umich.edu    void init(Params *params, LSQ *lsq_ptr, unsigned maxLQEntries,
766221Snate@binkert.org              unsigned maxSQEntries, unsigned id);
772683Sktlim@umich.edu
786221Snate@binkert.org    /** Returns the name of the LSQ unit. */
792683Sktlim@umich.edu    std::string name() const;
802683Sktlim@umich.edu
812683Sktlim@umich.edu    /** Registers statistics. */
822683Sktlim@umich.edu    void regStats();
832683Sktlim@umich.edu
848706Sandreas.hansson@arm.com    /** Sets the CPU pointer. */
858706Sandreas.hansson@arm.com    void setCPU(O3CPU *cpu_ptr);
868706Sandreas.hansson@arm.com
878706Sandreas.hansson@arm.com    /** Sets the IEW stage pointer. */
888706Sandreas.hansson@arm.com    void setIEW(IEW *iew_ptr)
898706Sandreas.hansson@arm.com    { iewStage = iew_ptr; }
908706Sandreas.hansson@arm.com
913675Sktlim@umich.edu    /** Sets the pointer to the dcache port. */
922683Sktlim@umich.edu    void setDcachePort(Port *dcache_port)
932683Sktlim@umich.edu    { dcachePort = dcache_port; }
942683Sktlim@umich.edu
952683Sktlim@umich.edu    /** Switches out LSQ unit. */
962683Sktlim@umich.edu    void switchOut();
972683Sktlim@umich.edu
982683Sktlim@umich.edu    /** Takes over from another CPU's thread. */
992683Sktlim@umich.edu    void takeOverFrom();
1003548Sgblack@eecs.umich.edu
1012683Sktlim@umich.edu    /** Returns if the LSQ is switched out. */
1028852Sandreas.hansson@arm.com    bool isSwitchedOut() { return switchedOut; }
1032690Sktlim@umich.edu
1048852Sandreas.hansson@arm.com    /** Ticks the LSQ unit, which in this case only resets the number of
1058799Sgblack@eecs.umich.edu     * used cache ports.
1062683Sktlim@umich.edu     * @todo: Move the number of used ports up to the LSQ level so it can
1072683Sktlim@umich.edu     * be shared by all LSQ units.
1088852Sandreas.hansson@arm.com     */
1092292SN/A    void tick() { usedPorts = 0; }
1102683Sktlim@umich.edu
1112683Sktlim@umich.edu    /** Inserts an instruction. */
1122683Sktlim@umich.edu    void insert(DynInstPtr &inst);
1132683Sktlim@umich.edu    /** Inserts a load instruction. */
1142683Sktlim@umich.edu    void insertLoad(DynInstPtr &load_inst);
1152683Sktlim@umich.edu    /** Inserts a store instruction. */
1162683Sktlim@umich.edu    void insertStore(DynInstPtr &store_inst);
1172683Sktlim@umich.edu
1182683Sktlim@umich.edu    /** Executes a load instruction. */
1192683Sktlim@umich.edu    Fault executeLoad(DynInstPtr &inst);
1202683Sktlim@umich.edu
1212683Sktlim@umich.edu    Fault executeLoad(int lq_idx) { panic("Not implemented"); return NoFault; }
1222683Sktlim@umich.edu    /** Executes a store instruction. */
1232683Sktlim@umich.edu    Fault executeStore(DynInstPtr &inst);
1242683Sktlim@umich.edu
1252683Sktlim@umich.edu    /** Commits the head load. */
1263673Srdreslin@umich.edu    void commitLoad();
1273486Sktlim@umich.edu    /** Commits loads older than a specific sequence number. */
1282683Sktlim@umich.edu    void commitLoads(InstSeqNum &youngest_inst);
1292683Sktlim@umich.edu
1302683Sktlim@umich.edu    /** Commits stores older than a specific sequence number. */
1315999Snate@binkert.org    void commitStores(InstSeqNum &youngest_inst);
1328834Satgutier@umich.edu
1338834Satgutier@umich.edu    /** Writes back stores. */
1348834Satgutier@umich.edu    void writebackStores();
1358834Satgutier@umich.edu
1362683Sktlim@umich.edu    /** Completes the data access that has been returned from the
1375999Snate@binkert.org     * memory system. */
1382683Sktlim@umich.edu    void completeDataAccess(PacketPtr pkt);
1392683Sktlim@umich.edu
1402683Sktlim@umich.edu    /** Clears all the entries in the LQ. */
1412683Sktlim@umich.edu    void clearLQ();
1422683Sktlim@umich.edu
1432683Sktlim@umich.edu    /** Clears all the entries in the SQ. */
1442683Sktlim@umich.edu    void clearSQ();
1452683Sktlim@umich.edu
1462683Sktlim@umich.edu    /** Resizes the LQ to a given size. */
1472683Sktlim@umich.edu    void resizeLQ(unsigned size);
1482683Sktlim@umich.edu
1492683Sktlim@umich.edu    /** Resizes the SQ to a given size. */
1503402Sktlim@umich.edu    void resizeSQ(unsigned size);
1513402Sktlim@umich.edu
1523402Sktlim@umich.edu    /** Squashes all instructions younger than a specific sequence number. */
1535714Shsul@eecs.umich.edu    void squash(const InstSeqNum &squashed_num);
1545714Shsul@eecs.umich.edu
1555714Shsul@eecs.umich.edu    /** Returns if there is a memory ordering violation. Value is reset upon
1562292SN/A     * call to getMemDepViolator().
1576221Snate@binkert.org     */
1582292SN/A    bool violation() { return memDepViolator; }
1592690Sktlim@umich.edu
1602683Sktlim@umich.edu    /** Returns the memory ordering violator. */
1612683Sktlim@umich.edu    DynInstPtr getMemDepViolator();
1622292SN/A
1632683Sktlim@umich.edu    /** Returns if a load became blocked due to the memory system. */
1642683Sktlim@umich.edu    bool loadBlocked()
1652292SN/A    { return isLoadBlocked; }
1662683Sktlim@umich.edu
1672292SN/A    /** Clears the signal that a load became blocked. */
1682292SN/A    void clearLoadBlocked()
1692292SN/A    { isLoadBlocked = false; }
1702292SN/A
1712292SN/A    /** Returns if the blocked load was handled. */
1723548Sgblack@eecs.umich.edu    bool isLoadBlockedHandled()
1738777Sgblack@eecs.umich.edu    { return loadBlockedHandled; }
1742683Sktlim@umich.edu
1758229Snate@binkert.org    /** Records the blocked load as being handled. */
1768229Snate@binkert.org    void setLoadBlockedHandled()
1778706Sandreas.hansson@arm.com    { loadBlockedHandled = true; }
1782683Sktlim@umich.edu
1798706Sandreas.hansson@arm.com    /** Returns the number of free entries (min of free LQ and SQ entries). */
1802683Sktlim@umich.edu    unsigned numFreeEntries();
1818706Sandreas.hansson@arm.com
1828706Sandreas.hansson@arm.com    /** Returns the number of loads ready to execute. */
1838852Sandreas.hansson@arm.com    int numLoadsReady();
1848852Sandreas.hansson@arm.com
1852678Sktlim@umich.edu    /** Returns the number of loads in the LQ. */
1862690Sktlim@umich.edu    int numLoads() { return loads; }
1872292SN/A
1882292SN/A    /** Returns the number of stores in the SQ. */
1892292SN/A    int numStores() { return stores; }
1902292SN/A
1912292SN/A    /** Returns if either the LQ or SQ is full. */
1922292SN/A    bool isFull() { return lqFull() || sqFull(); }
1932292SN/A
1942292SN/A    /** Returns if the LQ is full. */
1952292SN/A    bool lqFull() { return loads >= (LQEntries - 1); }
1962292SN/A
1972292SN/A    /** Returns if the SQ is full. */
1982292SN/A    bool sqFull() { return stores >= (SQEntries - 1); }
1992292SN/A
200    /** Returns the number of instructions in the LSQ. */
201    unsigned getCount() { return loads + stores; }
202
203    /** Returns if there are any stores to writeback. */
204    bool hasStoresToWB() { return storesToWB; }
205
206    /** Returns the number of stores to writeback. */
207    int numStoresToWB() { return storesToWB; }
208
209    /** Returns if the LSQ unit will writeback on this cycle. */
210    bool willWB() { return storeQueue[storeWBIdx].canWB &&
211                        !storeQueue[storeWBIdx].completed &&
212                        !isStoreBlocked; }
213
214    /** Handles doing the retry. */
215    void recvRetry();
216
217  private:
218    /** Writes back the instruction, sending it to IEW. */
219    void writeback(DynInstPtr &inst, PacketPtr pkt);
220
221    /** Handles completing the send of a store to memory. */
222    void storePostSend(Packet *pkt);
223
224    /** Completes the store at the specified index. */
225    void completeStore(int store_idx);
226
227    /** Increments the given store index (circular queue). */
228    inline void incrStIdx(int &store_idx);
229    /** Decrements the given store index (circular queue). */
230    inline void decrStIdx(int &store_idx);
231    /** Increments the given load index (circular queue). */
232    inline void incrLdIdx(int &load_idx);
233    /** Decrements the given load index (circular queue). */
234    inline void decrLdIdx(int &load_idx);
235
236  public:
237    /** Debugging function to dump instructions in the LSQ. */
238    void dumpInsts();
239
240  private:
241    /** Pointer to the CPU. */
242    O3CPU *cpu;
243
244    /** Pointer to the IEW stage. */
245    IEW *iewStage;
246
247    /** Pointer to the LSQ. */
248    LSQ *lsq;
249
250    /** Pointer to the dcache port.  Used only for sending. */
251    Port *dcachePort;
252
253    /** Derived class to hold any sender state the LSQ needs. */
254    class LSQSenderState : public Packet::SenderState
255    {
256      public:
257        /** Default constructor. */
258        LSQSenderState()
259            : noWB(false)
260        { }
261
262        /** Instruction who initiated the access to memory. */
263        DynInstPtr inst;
264        /** Whether or not it is a load. */
265        bool isLoad;
266        /** The LQ/SQ index of the instruction. */
267        int idx;
268        /** Whether or not the instruction will need to writeback. */
269        bool noWB;
270    };
271
272    /** Writeback event, specifically for when stores forward data to loads. */
273    class WritebackEvent : public Event {
274      public:
275        /** Constructs a writeback event. */
276        WritebackEvent(DynInstPtr &_inst, PacketPtr pkt, LSQUnit *lsq_ptr);
277
278        /** Processes the writeback event. */
279        void process();
280
281        /** Returns the description of this event. */
282        const char *description();
283
284      private:
285        /** Instruction whose results are being written back. */
286        DynInstPtr inst;
287
288        /** The packet that would have been sent to memory. */
289        PacketPtr pkt;
290
291        /** The pointer to the LSQ unit that issued the store. */
292        LSQUnit<Impl> *lsqPtr;
293    };
294
295  public:
296    struct SQEntry {
297        /** Constructs an empty store queue entry. */
298        SQEntry()
299            : inst(NULL), req(NULL), size(0), data(0),
300              canWB(0), committed(0), completed(0)
301        { }
302
303        /** Constructs a store queue entry for a given instruction. */
304        SQEntry(DynInstPtr &_inst)
305            : inst(_inst), req(NULL), size(0), data(0),
306              canWB(0), committed(0), completed(0)
307        { }
308
309        /** The store instruction. */
310        DynInstPtr inst;
311        /** The request for the store. */
312        RequestPtr req;
313        /** The size of the store. */
314        int size;
315        /** The store data. */
316        IntReg data;
317        /** Whether or not the store can writeback. */
318        bool canWB;
319        /** Whether or not the store is committed. */
320        bool committed;
321        /** Whether or not the store is completed. */
322        bool completed;
323    };
324
325  private:
326    /** The LSQUnit thread id. */
327    unsigned lsqID;
328
329    /** The store queue. */
330    std::vector<SQEntry> storeQueue;
331
332    /** The load queue. */
333    std::vector<DynInstPtr> loadQueue;
334
335    /** The number of LQ entries, plus a sentinel entry (circular queue).
336     *  @todo: Consider having var that records the true number of LQ entries.
337     */
338    unsigned LQEntries;
339    /** The number of SQ entries, plus a sentinel entry (circular queue).
340     *  @todo: Consider having var that records the true number of SQ entries.
341     */
342    unsigned SQEntries;
343
344    /** The number of load instructions in the LQ. */
345    int loads;
346    /** The number of store instructions in the SQ. */
347    int stores;
348    /** The number of store instructions in the SQ waiting to writeback. */
349    int storesToWB;
350
351    /** The index of the head instruction in the LQ. */
352    int loadHead;
353    /** The index of the tail instruction in the LQ. */
354    int loadTail;
355
356    /** The index of the head instruction in the SQ. */
357    int storeHead;
358    /** The index of the first instruction that may be ready to be
359     * written back, and has not yet been written back.
360     */
361    int storeWBIdx;
362    /** The index of the tail instruction in the SQ. */
363    int storeTail;
364
365    /// @todo Consider moving to a more advanced model with write vs read ports
366    /** The number of cache ports available each cycle. */
367    int cachePorts;
368
369    /** The number of used cache ports in this cycle. */
370    int usedPorts;
371
372    /** Is the LSQ switched out. */
373    bool switchedOut;
374
375    //list<InstSeqNum> mshrSeqNums;
376
377    /** Wire to read information from the issue stage time queue. */
378    typename TimeBuffer<IssueStruct>::wire fromIssue;
379
380    /** Whether or not the LSQ is stalled. */
381    bool stalled;
382    /** The store that causes the stall due to partial store to load
383     * forwarding.
384     */
385    InstSeqNum stallingStoreIsn;
386    /** The index of the above store. */
387    int stallingLoadIdx;
388
389    /** The packet that needs to be retried. */
390    PacketPtr retryPkt;
391
392    /** Whehter or not a store is blocked due to the memory system. */
393    bool isStoreBlocked;
394
395    /** Whether or not a load is blocked due to the memory system. */
396    bool isLoadBlocked;
397
398    /** Has the blocked load been handled. */
399    bool loadBlockedHandled;
400
401    /** The sequence number of the blocked load. */
402    InstSeqNum blockedLoadSeqNum;
403
404    /** The oldest load that caused a memory ordering violation. */
405    DynInstPtr memDepViolator;
406
407    // Will also need how many read/write ports the Dcache has.  Or keep track
408    // of that in stage that is one level up, and only call executeLoad/Store
409    // the appropriate number of times.
410
411    /** Total number of loads forwaded from LSQ stores. */
412    Stats::Scalar<> lsqForwLoads;
413
414    /** Total number of loads ignored due to invalid addresses. */
415    Stats::Scalar<> invAddrLoads;
416
417    /** Total number of squashed loads. */
418    Stats::Scalar<> lsqSquashedLoads;
419
420    /** Total number of responses from the memory system that are
421     * ignored due to the instruction already being squashed. */
422    Stats::Scalar<> lsqIgnoredResponses;
423
424    /** Total number of squashed stores. */
425    Stats::Scalar<> lsqSquashedStores;
426
427    /** Total number of software prefetches ignored due to invalid addresses. */
428    Stats::Scalar<> invAddrSwpfs;
429
430    /** Ready loads blocked due to partial store-forwarding. */
431    Stats::Scalar<> lsqBlockedLoads;
432
433    /** Number of loads that were rescheduled. */
434    Stats::Scalar<> lsqRescheduledLoads;
435
436    /** Number of times the LSQ is blocked due to the cache. */
437    Stats::Scalar<> lsqCacheBlocked;
438
439  public:
440    /** Executes the load at the given index. */
441    template <class T>
442    Fault read(Request *req, T &data, int load_idx);
443
444    /** Executes the store at the given index. */
445    template <class T>
446    Fault write(Request *req, T &data, int store_idx);
447
448    /** Returns the index of the head load instruction. */
449    int getLoadHead() { return loadHead; }
450    /** Returns the sequence number of the head load instruction. */
451    InstSeqNum getLoadHeadSeqNum()
452    {
453        if (loadQueue[loadHead]) {
454            return loadQueue[loadHead]->seqNum;
455        } else {
456            return 0;
457        }
458
459    }
460
461    /** Returns the index of the head store instruction. */
462    int getStoreHead() { return storeHead; }
463    /** Returns the sequence number of the head store instruction. */
464    InstSeqNum getStoreHeadSeqNum()
465    {
466        if (storeQueue[storeHead].inst) {
467            return storeQueue[storeHead].inst->seqNum;
468        } else {
469            return 0;
470        }
471
472    }
473
474    /** Returns whether or not the LSQ unit is stalled. */
475    bool isStalled()  { return stalled; }
476};
477
478template <class Impl>
479template <class T>
480Fault
481LSQUnit<Impl>::read(Request *req, T &data, int load_idx)
482{
483    DynInstPtr load_inst = loadQueue[load_idx];
484
485    assert(load_inst);
486
487    assert(!load_inst->isExecuted());
488
489    // Make sure this isn't an uncacheable access
490    // A bit of a hackish way to get uncached accesses to work only if they're
491    // at the head of the LSQ and are ready to commit (at the head of the ROB
492    // too).
493    if (req->getFlags() & UNCACHEABLE &&
494        (load_idx != loadHead || !load_inst->isAtCommit())) {
495        iewStage->rescheduleMemInst(load_inst);
496        ++lsqRescheduledLoads;
497        return TheISA::genMachineCheckFault();
498    }
499
500    // Check the SQ for any previous stores that might lead to forwarding
501    int store_idx = load_inst->sqIdx;
502
503    int store_size = 0;
504
505    DPRINTF(LSQUnit, "Read called, load idx: %i, store idx: %i, "
506            "storeHead: %i addr: %#x\n",
507            load_idx, store_idx, storeHead, req->getPaddr());
508
509#if FULL_SYSTEM
510    if (req->getFlags() & LOCKED) {
511        cpu->lockAddr = req->getPaddr();
512        cpu->lockFlag = true;
513    }
514#endif
515
516    while (store_idx != -1) {
517        // End once we've reached the top of the LSQ
518        if (store_idx == storeWBIdx) {
519            break;
520        }
521
522        // Move the index to one younger
523        if (--store_idx < 0)
524            store_idx += SQEntries;
525
526        assert(storeQueue[store_idx].inst);
527
528        store_size = storeQueue[store_idx].size;
529
530        if (store_size == 0)
531            continue;
532
533        // Check if the store data is within the lower and upper bounds of
534        // addresses that the request needs.
535        bool store_has_lower_limit =
536            req->getVaddr() >= storeQueue[store_idx].inst->effAddr;
537        bool store_has_upper_limit =
538            (req->getVaddr() + req->getSize()) <=
539            (storeQueue[store_idx].inst->effAddr + store_size);
540        bool lower_load_has_store_part =
541            req->getVaddr() < (storeQueue[store_idx].inst->effAddr +
542                           store_size);
543        bool upper_load_has_store_part =
544            (req->getVaddr() + req->getSize()) >
545            storeQueue[store_idx].inst->effAddr;
546
547        // If the store's data has all of the data needed, we can forward.
548        if (store_has_lower_limit && store_has_upper_limit) {
549            // Get shift amount for offset into the store's data.
550            int shift_amt = req->getVaddr() & (store_size - 1);
551            // @todo: Magic number, assumes byte addressing
552            shift_amt = shift_amt << 3;
553
554            // Cast this to type T?
555            data = storeQueue[store_idx].data >> shift_amt;
556
557            assert(!load_inst->memData);
558            load_inst->memData = new uint8_t[64];
559
560            memcpy(load_inst->memData, &data, req->getSize());
561
562            DPRINTF(LSQUnit, "Forwarding from store idx %i to load to "
563                    "addr %#x, data %#x\n",
564                    store_idx, req->getVaddr(), data);
565
566            PacketPtr data_pkt = new Packet(req, Packet::ReadReq, Packet::Broadcast);
567            data_pkt->dataStatic(load_inst->memData);
568
569            WritebackEvent *wb = new WritebackEvent(load_inst, data_pkt, this);
570
571            // We'll say this has a 1 cycle load-store forwarding latency
572            // for now.
573            // @todo: Need to make this a parameter.
574            wb->schedule(curTick);
575
576            ++lsqForwLoads;
577            return NoFault;
578        } else if ((store_has_lower_limit && lower_load_has_store_part) ||
579                   (store_has_upper_limit && upper_load_has_store_part) ||
580                   (lower_load_has_store_part && upper_load_has_store_part)) {
581            // This is the partial store-load forwarding case where a store
582            // has only part of the load's data.
583
584            // If it's already been written back, then don't worry about
585            // stalling on it.
586            if (storeQueue[store_idx].completed) {
587                continue;
588            }
589
590            // Must stall load and force it to retry, so long as it's the oldest
591            // load that needs to do so.
592            if (!stalled ||
593                (stalled &&
594                 load_inst->seqNum <
595                 loadQueue[stallingLoadIdx]->seqNum)) {
596                stalled = true;
597                stallingStoreIsn = storeQueue[store_idx].inst->seqNum;
598                stallingLoadIdx = load_idx;
599            }
600
601            // Tell IQ/mem dep unit that this instruction will need to be
602            // rescheduled eventually
603            iewStage->rescheduleMemInst(load_inst);
604            iewStage->decrWb(load_inst->seqNum);
605            ++lsqRescheduledLoads;
606
607            // Do not generate a writeback event as this instruction is not
608            // complete.
609            DPRINTF(LSQUnit, "Load-store forwarding mis-match. "
610                    "Store idx %i to load addr %#x\n",
611                    store_idx, req->getVaddr());
612
613            ++lsqBlockedLoads;
614            return NoFault;
615        }
616    }
617
618    // If there's no forwarding case, then go access memory
619    DPRINTF(LSQUnit, "Doing memory access for inst [sn:%lli] PC %#x\n",
620            load_inst->seqNum, load_inst->readPC());
621
622    assert(!load_inst->memData);
623    load_inst->memData = new uint8_t[64];
624
625    ++usedPorts;
626
627    PacketPtr data_pkt = new Packet(req, Packet::ReadReq, Packet::Broadcast);
628    data_pkt->dataStatic(load_inst->memData);
629
630    LSQSenderState *state = new LSQSenderState;
631    state->isLoad = true;
632    state->idx = load_idx;
633    state->inst = load_inst;
634    data_pkt->senderState = state;
635
636    // if we the cache is not blocked, do cache access
637    if (!lsq->cacheBlocked()) {
638        if (!dcachePort->sendTiming(data_pkt)) {
639            // If the access didn't succeed, tell the LSQ by setting
640            // the retry thread id.
641            lsq->setRetryTid(lsqID);
642        }
643    }
644
645    // If the cache was blocked, or has become blocked due to the access,
646    // handle it.
647    if (lsq->cacheBlocked()) {
648        ++lsqCacheBlocked;
649        // There's an older load that's already going to squash.
650        if (isLoadBlocked && blockedLoadSeqNum < load_inst->seqNum)
651            return NoFault;
652
653        // Record that the load was blocked due to memory.  This
654        // load will squash all instructions after it, be
655        // refetched, and re-executed.
656        isLoadBlocked = true;
657        loadBlockedHandled = false;
658        blockedLoadSeqNum = load_inst->seqNum;
659        // No fault occurred, even though the interface is blocked.
660        return NoFault;
661    }
662
663    if (data_pkt->result != Packet::Success) {
664        DPRINTF(LSQUnit, "LSQUnit: D-cache miss!\n");
665        DPRINTF(Activity, "Activity: ld accessing mem miss [sn:%lli]\n",
666                load_inst->seqNum);
667    } else {
668        DPRINTF(LSQUnit, "LSQUnit: D-cache hit!\n");
669        DPRINTF(Activity, "Activity: ld accessing mem hit [sn:%lli]\n",
670                load_inst->seqNum);
671    }
672
673    return NoFault;
674}
675
676template <class Impl>
677template <class T>
678Fault
679LSQUnit<Impl>::write(Request *req, T &data, int store_idx)
680{
681    assert(storeQueue[store_idx].inst);
682
683    DPRINTF(LSQUnit, "Doing write to store idx %i, addr %#x data %#x"
684            " | storeHead:%i [sn:%i]\n",
685            store_idx, req->getPaddr(), data, storeHead,
686            storeQueue[store_idx].inst->seqNum);
687
688    storeQueue[store_idx].req = req;
689    storeQueue[store_idx].size = sizeof(T);
690    storeQueue[store_idx].data = data;
691
692    // This function only writes the data to the store queue, so no fault
693    // can happen here.
694    return NoFault;
695}
696
697#endif // __CPU_O3_LSQ_UNIT_HH__
698