110623Smitch.hayenga@arm.com/*
211439SRekai.GonzalezAlberquilla@arm.com * Copyright (c) 2014-2015 ARM Limited
310623Smitch.hayenga@arm.com * All rights reserved
410623Smitch.hayenga@arm.com *
510623Smitch.hayenga@arm.com * The license below extends only to copyright in the software and shall
610623Smitch.hayenga@arm.com * not be construed as granting a license to any other intellectual
710623Smitch.hayenga@arm.com * property including but not limited to intellectual property relating
810623Smitch.hayenga@arm.com * to a hardware implementation of the functionality of the software
910623Smitch.hayenga@arm.com * licensed hereunder.  You may use the software subject to the license
1010623Smitch.hayenga@arm.com * terms below provided that you ensure that this notice is replicated
1110623Smitch.hayenga@arm.com * unmodified and in its entirety in all distributions of the software,
1210623Smitch.hayenga@arm.com * modified or unmodified, in source code or in binary form.
1310623Smitch.hayenga@arm.com *
1410623Smitch.hayenga@arm.com * Redistribution and use in source and binary forms, with or without
1510623Smitch.hayenga@arm.com * modification, are permitted provided that the following conditions are
1610623Smitch.hayenga@arm.com * met: redistributions of source code must retain the above copyright
1710623Smitch.hayenga@arm.com * notice, this list of conditions and the following disclaimer;
1810623Smitch.hayenga@arm.com * redistributions in binary form must reproduce the above copyright
1910623Smitch.hayenga@arm.com * notice, this list of conditions and the following disclaimer in the
2010623Smitch.hayenga@arm.com * documentation and/or other materials provided with the distribution;
2110623Smitch.hayenga@arm.com * neither the name of the copyright holders nor the names of its
2210623Smitch.hayenga@arm.com * contributors may be used to endorse or promote products derived from
2310623Smitch.hayenga@arm.com * this software without specific prior written permission.
2410623Smitch.hayenga@arm.com *
2510623Smitch.hayenga@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2610623Smitch.hayenga@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2710623Smitch.hayenga@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2810623Smitch.hayenga@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2910623Smitch.hayenga@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3010623Smitch.hayenga@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3110623Smitch.hayenga@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3210623Smitch.hayenga@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3310623Smitch.hayenga@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3410623Smitch.hayenga@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3510623Smitch.hayenga@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3610623Smitch.hayenga@arm.com *
3710623Smitch.hayenga@arm.com * Authors: Mitch Hayenga
3810623Smitch.hayenga@arm.com */
3910623Smitch.hayenga@arm.com
4011793Sbrandon.potter@amd.com#include "mem/cache/prefetch/queued.hh"
4111793Sbrandon.potter@amd.com
4212727Snikos.nikoleris@arm.com#include <cassert>
4312727Snikos.nikoleris@arm.com
4414013Sjavier.bueno@metempsy.com#include "arch/generic/tlb.hh"
4512727Snikos.nikoleris@arm.com#include "base/logging.hh"
4612727Snikos.nikoleris@arm.com#include "base/trace.hh"
4710623Smitch.hayenga@arm.com#include "debug/HWPrefetch.hh"
4814013Sjavier.bueno@metempsy.com#include "mem/cache/base.hh"
4912727Snikos.nikoleris@arm.com#include "mem/request.hh"
5012727Snikos.nikoleris@arm.com#include "params/QueuedPrefetcher.hh"
5110623Smitch.hayenga@arm.com
5214013Sjavier.bueno@metempsy.comvoid
5314013Sjavier.bueno@metempsy.comQueuedPrefetcher::DeferredPacket::createPkt(Addr paddr, unsigned blk_size,
5414013Sjavier.bueno@metempsy.com                                            MasterID mid, bool tag_prefetch,
5514013Sjavier.bueno@metempsy.com                                            Tick t) {
5614013Sjavier.bueno@metempsy.com    /* Create a prefetch memory request */
5714013Sjavier.bueno@metempsy.com    RequestPtr req = std::make_shared<Request>(paddr, blk_size, 0, mid);
5814013Sjavier.bueno@metempsy.com
5914013Sjavier.bueno@metempsy.com    if (pfInfo.isSecure()) {
6014013Sjavier.bueno@metempsy.com        req->setFlags(Request::SECURE);
6114013Sjavier.bueno@metempsy.com    }
6214013Sjavier.bueno@metempsy.com    req->taskId(ContextSwitchTaskId::Prefetcher);
6314013Sjavier.bueno@metempsy.com    pkt = new Packet(req, MemCmd::HardPFReq);
6414013Sjavier.bueno@metempsy.com    pkt->allocate();
6514013Sjavier.bueno@metempsy.com    if (tag_prefetch && pfInfo.hasPC()) {
6614013Sjavier.bueno@metempsy.com        // Tag prefetch packet with  accessing pc
6714013Sjavier.bueno@metempsy.com        pkt->req->setPC(pfInfo.getPC());
6814013Sjavier.bueno@metempsy.com    }
6914013Sjavier.bueno@metempsy.com    tick = t;
7014013Sjavier.bueno@metempsy.com}
7114013Sjavier.bueno@metempsy.com
7214013Sjavier.bueno@metempsy.comvoid
7314013Sjavier.bueno@metempsy.comQueuedPrefetcher::DeferredPacket::startTranslation(BaseTLB *tlb)
7414013Sjavier.bueno@metempsy.com{
7514013Sjavier.bueno@metempsy.com    assert(translationRequest != nullptr);
7614013Sjavier.bueno@metempsy.com    if (!ongoingTranslation) {
7714013Sjavier.bueno@metempsy.com        ongoingTranslation = true;
7814013Sjavier.bueno@metempsy.com        // Prefetchers only operate in Timing mode
7914013Sjavier.bueno@metempsy.com        tlb->translateTiming(translationRequest, tc, this, BaseTLB::Read);
8014013Sjavier.bueno@metempsy.com    }
8114013Sjavier.bueno@metempsy.com}
8214013Sjavier.bueno@metempsy.com
8314013Sjavier.bueno@metempsy.comvoid
8414013Sjavier.bueno@metempsy.comQueuedPrefetcher::DeferredPacket::finish(const Fault &fault,
8514013Sjavier.bueno@metempsy.com    const RequestPtr &req, ThreadContext *tc, BaseTLB::Mode mode)
8614013Sjavier.bueno@metempsy.com{
8714013Sjavier.bueno@metempsy.com    assert(ongoingTranslation);
8814013Sjavier.bueno@metempsy.com    ongoingTranslation = false;
8914013Sjavier.bueno@metempsy.com    bool failed = (fault != NoFault);
9014013Sjavier.bueno@metempsy.com    owner->translationComplete(this, failed);
9114013Sjavier.bueno@metempsy.com}
9214013Sjavier.bueno@metempsy.com
9310623Smitch.hayenga@arm.comQueuedPrefetcher::QueuedPrefetcher(const QueuedPrefetcherParams *p)
9414013Sjavier.bueno@metempsy.com    : BasePrefetcher(p), queueSize(p->queue_size),
9514013Sjavier.bueno@metempsy.com      missingTranslationQueueSize(
9614013Sjavier.bueno@metempsy.com        p->max_prefetch_requests_with_pending_translation),
9714013Sjavier.bueno@metempsy.com      latency(p->latency), queueSquash(p->queue_squash),
9814013Sjavier.bueno@metempsy.com      queueFilter(p->queue_filter), cacheSnoop(p->cache_snoop),
9914015Sjavier.bueno@metempsy.com      tagPrefetch(p->tag_prefetch),
10014015Sjavier.bueno@metempsy.com      throttleControlPct(p->throttle_control_percentage)
10110623Smitch.hayenga@arm.com{
10210623Smitch.hayenga@arm.com}
10310623Smitch.hayenga@arm.com
10410623Smitch.hayenga@arm.comQueuedPrefetcher::~QueuedPrefetcher()
10510623Smitch.hayenga@arm.com{
10610623Smitch.hayenga@arm.com    // Delete the queued prefetch packets
10710623Smitch.hayenga@arm.com    for (DeferredPacket &p : pfq) {
10810623Smitch.hayenga@arm.com        delete p.pkt;
10910623Smitch.hayenga@arm.com    }
11010623Smitch.hayenga@arm.com}
11110623Smitch.hayenga@arm.com
11214015Sjavier.bueno@metempsy.comsize_t
11314015Sjavier.bueno@metempsy.comQueuedPrefetcher::getMaxPermittedPrefetches(size_t total) const
11414015Sjavier.bueno@metempsy.com{
11514015Sjavier.bueno@metempsy.com    /**
11614015Sjavier.bueno@metempsy.com     * Throttle generated prefetches based in the accuracy of the prefetcher.
11714015Sjavier.bueno@metempsy.com     * Accuracy is computed based in the ratio of useful prefetches with
11814015Sjavier.bueno@metempsy.com     * respect to the number of issued prefetches.
11914015Sjavier.bueno@metempsy.com     *
12014015Sjavier.bueno@metempsy.com     * The throttleControlPct controls how many of the candidate addresses
12114015Sjavier.bueno@metempsy.com     * generated by the prefetcher will be finally turned into prefetch
12214015Sjavier.bueno@metempsy.com     * requests
12314015Sjavier.bueno@metempsy.com     * - If set to 100, all candidates can be discarded (one request
12414015Sjavier.bueno@metempsy.com     *   will always be allowed to be generated)
12514015Sjavier.bueno@metempsy.com     * - Setting it to 0 will disable the throttle control, so requests are
12614015Sjavier.bueno@metempsy.com     *   created for all candidates
12714015Sjavier.bueno@metempsy.com     * - If set to 60, 40% of candidates will generate a request, and the
12814015Sjavier.bueno@metempsy.com     *   remaining 60% will be generated depending on the current accuracy
12914015Sjavier.bueno@metempsy.com     */
13014015Sjavier.bueno@metempsy.com
13114015Sjavier.bueno@metempsy.com    size_t max_pfs = total;
13214015Sjavier.bueno@metempsy.com    if (total > 0 && issuedPrefetches > 0) {
13314015Sjavier.bueno@metempsy.com        size_t throttle_pfs = (total * throttleControlPct) / 100;
13414015Sjavier.bueno@metempsy.com        size_t min_pfs = (total - throttle_pfs) == 0 ?
13514015Sjavier.bueno@metempsy.com            1 : (total - throttle_pfs);
13614015Sjavier.bueno@metempsy.com        max_pfs = min_pfs + (total - min_pfs) *
13714015Sjavier.bueno@metempsy.com            usefulPrefetches / issuedPrefetches;
13814015Sjavier.bueno@metempsy.com    }
13914015Sjavier.bueno@metempsy.com    return max_pfs;
14014015Sjavier.bueno@metempsy.com}
14114015Sjavier.bueno@metempsy.com
14213416Sjavier.bueno@metempsy.comvoid
14313551Sjavier.bueno@metempsy.comQueuedPrefetcher::notify(const PacketPtr &pkt, const PrefetchInfo &pfi)
14410623Smitch.hayenga@arm.com{
14513551Sjavier.bueno@metempsy.com    Addr blk_addr = blockAddress(pfi.getAddr());
14613551Sjavier.bueno@metempsy.com    bool is_secure = pfi.isSecure();
14710623Smitch.hayenga@arm.com
14813551Sjavier.bueno@metempsy.com    // Squash queued prefetches if demand miss to same line
14913551Sjavier.bueno@metempsy.com    if (queueSquash) {
15013551Sjavier.bueno@metempsy.com        auto itr = pfq.begin();
15113551Sjavier.bueno@metempsy.com        while (itr != pfq.end()) {
15213551Sjavier.bueno@metempsy.com            if (itr->pfInfo.getAddr() == blk_addr &&
15313551Sjavier.bueno@metempsy.com                itr->pfInfo.isSecure() == is_secure) {
15413551Sjavier.bueno@metempsy.com                delete itr->pkt;
15513551Sjavier.bueno@metempsy.com                itr = pfq.erase(itr);
15613551Sjavier.bueno@metempsy.com            } else {
15713551Sjavier.bueno@metempsy.com                ++itr;
15810623Smitch.hayenga@arm.com            }
15910623Smitch.hayenga@arm.com        }
16013551Sjavier.bueno@metempsy.com    }
16110623Smitch.hayenga@arm.com
16213551Sjavier.bueno@metempsy.com    // Calculate prefetches given this access
16313551Sjavier.bueno@metempsy.com    std::vector<AddrPriority> addresses;
16413551Sjavier.bueno@metempsy.com    calculatePrefetch(pfi, addresses);
16510623Smitch.hayenga@arm.com
16614015Sjavier.bueno@metempsy.com    // Get the maximu number of prefetches that we are allowed to generate
16714015Sjavier.bueno@metempsy.com    size_t max_pfs = getMaxPermittedPrefetches(addresses.size());
16814015Sjavier.bueno@metempsy.com
16913551Sjavier.bueno@metempsy.com    // Queue up generated prefetches
17014015Sjavier.bueno@metempsy.com    size_t num_pfs = 0;
17113551Sjavier.bueno@metempsy.com    for (AddrPriority& addr_prio : addresses) {
17210623Smitch.hayenga@arm.com
17313551Sjavier.bueno@metempsy.com        // Block align prefetch address
17413551Sjavier.bueno@metempsy.com        addr_prio.first = blockAddress(addr_prio.first);
17510623Smitch.hayenga@arm.com
17614013Sjavier.bueno@metempsy.com        if (!samePage(addr_prio.first, pfi.getAddr())) {
17714013Sjavier.bueno@metempsy.com            pfSpanPage += 1;
17814013Sjavier.bueno@metempsy.com        }
17914013Sjavier.bueno@metempsy.com
18014013Sjavier.bueno@metempsy.com        bool can_cross_page = (tlb != nullptr);
18114013Sjavier.bueno@metempsy.com        if (can_cross_page || samePage(addr_prio.first, pfi.getAddr())) {
18213552Sjavier.bueno@metempsy.com            PrefetchInfo new_pfi(pfi,addr_prio.first);
18313552Sjavier.bueno@metempsy.com            pfIdentified++;
18413552Sjavier.bueno@metempsy.com            DPRINTF(HWPrefetch, "Found a pf candidate addr: %#x, "
18513552Sjavier.bueno@metempsy.com                    "inserting into prefetch queue.\n", new_pfi.getAddr());
18613552Sjavier.bueno@metempsy.com            // Create and insert the request
18713552Sjavier.bueno@metempsy.com            insert(pkt, new_pfi, addr_prio.second);
18814015Sjavier.bueno@metempsy.com            num_pfs += 1;
18914015Sjavier.bueno@metempsy.com            if (num_pfs == max_pfs) {
19014015Sjavier.bueno@metempsy.com                break;
19114015Sjavier.bueno@metempsy.com            }
19213552Sjavier.bueno@metempsy.com        } else {
19313552Sjavier.bueno@metempsy.com            DPRINTF(HWPrefetch, "Ignoring page crossing prefetch.\n");
19413552Sjavier.bueno@metempsy.com        }
19510623Smitch.hayenga@arm.com    }
19610623Smitch.hayenga@arm.com}
19710623Smitch.hayenga@arm.com
19810623Smitch.hayenga@arm.comPacketPtr
19910623Smitch.hayenga@arm.comQueuedPrefetcher::getPacket()
20010623Smitch.hayenga@arm.com{
20110623Smitch.hayenga@arm.com    DPRINTF(HWPrefetch, "Requesting a prefetch to issue.\n");
20210623Smitch.hayenga@arm.com
20310623Smitch.hayenga@arm.com    if (pfq.empty()) {
20414013Sjavier.bueno@metempsy.com        // If the queue is empty, attempt first to fill it with requests
20514013Sjavier.bueno@metempsy.com        // from the queue of missing translations
20614013Sjavier.bueno@metempsy.com        processMissingTranslations(queueSize);
20714013Sjavier.bueno@metempsy.com    }
20814013Sjavier.bueno@metempsy.com
20914013Sjavier.bueno@metempsy.com    if (pfq.empty()) {
21010623Smitch.hayenga@arm.com        DPRINTF(HWPrefetch, "No hardware prefetches available.\n");
21111484Snikos.nikoleris@arm.com        return nullptr;
21210623Smitch.hayenga@arm.com    }
21310623Smitch.hayenga@arm.com
21413551Sjavier.bueno@metempsy.com    PacketPtr pkt = pfq.front().pkt;
21510623Smitch.hayenga@arm.com    pfq.pop_front();
21610623Smitch.hayenga@arm.com
21710623Smitch.hayenga@arm.com    pfIssued++;
21813624Sjavier.bueno@metempsy.com    issuedPrefetches += 1;
21911484Snikos.nikoleris@arm.com    assert(pkt != nullptr);
22010623Smitch.hayenga@arm.com    DPRINTF(HWPrefetch, "Generating prefetch for %#x.\n", pkt->getAddr());
22114013Sjavier.bueno@metempsy.com
22214013Sjavier.bueno@metempsy.com    processMissingTranslations(queueSize - pfq.size());
22310623Smitch.hayenga@arm.com    return pkt;
22410623Smitch.hayenga@arm.com}
22510623Smitch.hayenga@arm.com
22610623Smitch.hayenga@arm.comvoid
22710623Smitch.hayenga@arm.comQueuedPrefetcher::regStats()
22810623Smitch.hayenga@arm.com{
22910623Smitch.hayenga@arm.com    BasePrefetcher::regStats();
23010623Smitch.hayenga@arm.com
23110623Smitch.hayenga@arm.com    pfIdentified
23210623Smitch.hayenga@arm.com        .name(name() + ".pfIdentified")
23310623Smitch.hayenga@arm.com        .desc("number of prefetch candidates identified");
23410623Smitch.hayenga@arm.com
23510623Smitch.hayenga@arm.com    pfBufferHit
23610623Smitch.hayenga@arm.com        .name(name() + ".pfBufferHit")
23710623Smitch.hayenga@arm.com        .desc("number of redundant prefetches already in prefetch queue");
23810623Smitch.hayenga@arm.com
23910623Smitch.hayenga@arm.com    pfInCache
24010623Smitch.hayenga@arm.com        .name(name() + ".pfInCache")
24110623Smitch.hayenga@arm.com        .desc("number of redundant prefetches already in cache/mshr dropped");
24210623Smitch.hayenga@arm.com
24310623Smitch.hayenga@arm.com    pfRemovedFull
24410623Smitch.hayenga@arm.com        .name(name() + ".pfRemovedFull")
24510623Smitch.hayenga@arm.com        .desc("number of prefetches dropped due to prefetch queue size");
24610623Smitch.hayenga@arm.com
24710623Smitch.hayenga@arm.com    pfSpanPage
24810623Smitch.hayenga@arm.com        .name(name() + ".pfSpanPage")
24914013Sjavier.bueno@metempsy.com        .desc("number of prefetches that crossed the page");
25014013Sjavier.bueno@metempsy.com}
25114013Sjavier.bueno@metempsy.com
25214013Sjavier.bueno@metempsy.com
25314013Sjavier.bueno@metempsy.comvoid
25414013Sjavier.bueno@metempsy.comQueuedPrefetcher::processMissingTranslations(unsigned max)
25514013Sjavier.bueno@metempsy.com{
25614013Sjavier.bueno@metempsy.com    unsigned count = 0;
25714013Sjavier.bueno@metempsy.com    iterator it = pfqMissingTranslation.begin();
25814013Sjavier.bueno@metempsy.com    while (it != pfqMissingTranslation.end() && count < max) {
25914013Sjavier.bueno@metempsy.com        DeferredPacket &dp = *it;
26014013Sjavier.bueno@metempsy.com        // Increase the iterator first because dp.startTranslation can end up
26114013Sjavier.bueno@metempsy.com        // calling finishTranslation, which will erase "it"
26214013Sjavier.bueno@metempsy.com        it++;
26314013Sjavier.bueno@metempsy.com        dp.startTranslation(tlb);
26414013Sjavier.bueno@metempsy.com        count += 1;
26514013Sjavier.bueno@metempsy.com    }
26614013Sjavier.bueno@metempsy.com}
26714013Sjavier.bueno@metempsy.com
26814013Sjavier.bueno@metempsy.comvoid
26914013Sjavier.bueno@metempsy.comQueuedPrefetcher::translationComplete(DeferredPacket *dp, bool failed)
27014013Sjavier.bueno@metempsy.com{
27114013Sjavier.bueno@metempsy.com    auto it = pfqMissingTranslation.begin();
27214013Sjavier.bueno@metempsy.com    while (it != pfqMissingTranslation.end()) {
27314013Sjavier.bueno@metempsy.com        if (&(*it) == dp) {
27414013Sjavier.bueno@metempsy.com            break;
27514013Sjavier.bueno@metempsy.com        }
27614013Sjavier.bueno@metempsy.com        it++;
27714013Sjavier.bueno@metempsy.com    }
27814013Sjavier.bueno@metempsy.com    assert(it != pfqMissingTranslation.end());
27914013Sjavier.bueno@metempsy.com    if (!failed) {
28014013Sjavier.bueno@metempsy.com        DPRINTF(HWPrefetch, "%s Translation of vaddr %#x succeeded: "
28114013Sjavier.bueno@metempsy.com                "paddr %#x \n", tlb->name(),
28214013Sjavier.bueno@metempsy.com                it->translationRequest->getVaddr(),
28314013Sjavier.bueno@metempsy.com                it->translationRequest->getPaddr());
28414013Sjavier.bueno@metempsy.com        Addr target_paddr = it->translationRequest->getPaddr();
28514013Sjavier.bueno@metempsy.com        // check if this prefetch is already redundant
28614013Sjavier.bueno@metempsy.com        if (cacheSnoop && (inCache(target_paddr, it->pfInfo.isSecure()) ||
28714013Sjavier.bueno@metempsy.com                    inMissQueue(target_paddr, it->pfInfo.isSecure()))) {
28814013Sjavier.bueno@metempsy.com            pfInCache++;
28914013Sjavier.bueno@metempsy.com            DPRINTF(HWPrefetch, "Dropping redundant in "
29014013Sjavier.bueno@metempsy.com                    "cache/MSHR prefetch addr:%#x\n", target_paddr);
29114013Sjavier.bueno@metempsy.com        } else {
29214013Sjavier.bueno@metempsy.com            Tick pf_time = curTick() + clockPeriod() * latency;
29314013Sjavier.bueno@metempsy.com            it->createPkt(it->translationRequest->getPaddr(), blkSize,
29414013Sjavier.bueno@metempsy.com                    masterId, tagPrefetch, pf_time);
29514013Sjavier.bueno@metempsy.com            addToQueue(pfq, *it);
29614013Sjavier.bueno@metempsy.com        }
29714013Sjavier.bueno@metempsy.com    } else {
29814013Sjavier.bueno@metempsy.com        DPRINTF(HWPrefetch, "%s Translation of vaddr %#x failed, dropping "
29914013Sjavier.bueno@metempsy.com                "prefetch request %#x \n", tlb->name(),
30014013Sjavier.bueno@metempsy.com                it->translationRequest->getVaddr());
30114013Sjavier.bueno@metempsy.com    }
30214013Sjavier.bueno@metempsy.com    pfqMissingTranslation.erase(it);
30314013Sjavier.bueno@metempsy.com}
30414013Sjavier.bueno@metempsy.com
30514013Sjavier.bueno@metempsy.combool
30614013Sjavier.bueno@metempsy.comQueuedPrefetcher::alreadyInQueue(std::list<DeferredPacket> &queue,
30714013Sjavier.bueno@metempsy.com                                 const PrefetchInfo &pfi, int32_t priority)
30814013Sjavier.bueno@metempsy.com{
30914013Sjavier.bueno@metempsy.com    bool found = false;
31014013Sjavier.bueno@metempsy.com    iterator it;
31114013Sjavier.bueno@metempsy.com    for (it = queue.begin(); it != queue.end() && !found; it++) {
31214013Sjavier.bueno@metempsy.com        found = it->pfInfo.sameAddr(pfi);
31314013Sjavier.bueno@metempsy.com    }
31414013Sjavier.bueno@metempsy.com
31514013Sjavier.bueno@metempsy.com    /* If the address is already in the queue, update priority and leave */
31614013Sjavier.bueno@metempsy.com    if (it != queue.end()) {
31714013Sjavier.bueno@metempsy.com        pfBufferHit++;
31814013Sjavier.bueno@metempsy.com        if (it->priority < priority) {
31914013Sjavier.bueno@metempsy.com            /* Update priority value and position in the queue */
32014013Sjavier.bueno@metempsy.com            it->priority = priority;
32114013Sjavier.bueno@metempsy.com            iterator prev = it;
32214013Sjavier.bueno@metempsy.com            while (prev != queue.begin()) {
32314013Sjavier.bueno@metempsy.com                prev--;
32414013Sjavier.bueno@metempsy.com                /* If the packet has higher priority, swap */
32514013Sjavier.bueno@metempsy.com                if (*it > *prev) {
32614013Sjavier.bueno@metempsy.com                    std::swap(*it, *prev);
32714013Sjavier.bueno@metempsy.com                    it = prev;
32814013Sjavier.bueno@metempsy.com                }
32914013Sjavier.bueno@metempsy.com            }
33014013Sjavier.bueno@metempsy.com            DPRINTF(HWPrefetch, "Prefetch addr already in "
33114013Sjavier.bueno@metempsy.com                "prefetch queue, priority updated\n");
33214013Sjavier.bueno@metempsy.com        } else {
33314013Sjavier.bueno@metempsy.com            DPRINTF(HWPrefetch, "Prefetch addr already in "
33414013Sjavier.bueno@metempsy.com                "prefetch queue\n");
33514013Sjavier.bueno@metempsy.com        }
33614013Sjavier.bueno@metempsy.com    }
33714013Sjavier.bueno@metempsy.com    return found;
33814013Sjavier.bueno@metempsy.com}
33914013Sjavier.bueno@metempsy.com
34014013Sjavier.bueno@metempsy.comRequestPtr
34114013Sjavier.bueno@metempsy.comQueuedPrefetcher::createPrefetchRequest(Addr addr, PrefetchInfo const &pfi,
34214013Sjavier.bueno@metempsy.com                                        PacketPtr pkt)
34314013Sjavier.bueno@metempsy.com{
34414013Sjavier.bueno@metempsy.com    RequestPtr translation_req = std::make_shared<Request>(pkt->req->getAsid(),
34514013Sjavier.bueno@metempsy.com            addr, blkSize, pkt->req->getFlags(), masterId, pfi.getPC(),
34614013Sjavier.bueno@metempsy.com            pkt->req->contextId());
34714013Sjavier.bueno@metempsy.com    translation_req->setFlags(Request::PREFETCH);
34814013Sjavier.bueno@metempsy.com    return translation_req;
34910623Smitch.hayenga@arm.com}
35011439SRekai.GonzalezAlberquilla@arm.com
35113551Sjavier.bueno@metempsy.comvoid
35213551Sjavier.bueno@metempsy.comQueuedPrefetcher::insert(const PacketPtr &pkt, PrefetchInfo &new_pfi,
35313551Sjavier.bueno@metempsy.com                         int32_t priority)
35411439SRekai.GonzalezAlberquilla@arm.com{
35511439SRekai.GonzalezAlberquilla@arm.com    if (queueFilter) {
35614013Sjavier.bueno@metempsy.com        if (alreadyInQueue(pfq, new_pfi, priority)) {
35714013Sjavier.bueno@metempsy.com            return;
35814013Sjavier.bueno@metempsy.com        }
35914013Sjavier.bueno@metempsy.com        if (alreadyInQueue(pfqMissingTranslation, new_pfi, priority)) {
36013551Sjavier.bueno@metempsy.com            return;
36111439SRekai.GonzalezAlberquilla@arm.com        }
36211439SRekai.GonzalezAlberquilla@arm.com    }
36311439SRekai.GonzalezAlberquilla@arm.com
36414013Sjavier.bueno@metempsy.com    /*
36514013Sjavier.bueno@metempsy.com     * Physical address computation
36614013Sjavier.bueno@metempsy.com     * if the prefetch is within the same page
36714013Sjavier.bueno@metempsy.com     *   using VA: add the computed stride to the original PA
36814013Sjavier.bueno@metempsy.com     *   using PA: no actions needed
36914013Sjavier.bueno@metempsy.com     * if we are page crossing
37014013Sjavier.bueno@metempsy.com     *   using VA: Create a translaion request and enqueue the corresponding
37114013Sjavier.bueno@metempsy.com     *       deferred packet to the queue of pending translations
37214013Sjavier.bueno@metempsy.com     *   using PA: use the provided VA to obtain the target VA, then attempt to
37314013Sjavier.bueno@metempsy.com     *     translate the resulting address
37414013Sjavier.bueno@metempsy.com     */
37514013Sjavier.bueno@metempsy.com
37614013Sjavier.bueno@metempsy.com    Addr orig_addr = useVirtualAddresses ?
37714013Sjavier.bueno@metempsy.com        pkt->req->getVaddr() : pkt->req->getPaddr();
37814013Sjavier.bueno@metempsy.com    bool positive_stride = new_pfi.getAddr() >= orig_addr;
37914013Sjavier.bueno@metempsy.com    Addr stride = positive_stride ?
38014013Sjavier.bueno@metempsy.com        (new_pfi.getAddr() - orig_addr) : (orig_addr - new_pfi.getAddr());
38114013Sjavier.bueno@metempsy.com
38214013Sjavier.bueno@metempsy.com    Addr target_paddr;
38314013Sjavier.bueno@metempsy.com    bool has_target_pa = false;
38414013Sjavier.bueno@metempsy.com    RequestPtr translation_req = nullptr;
38514013Sjavier.bueno@metempsy.com    if (samePage(orig_addr, new_pfi.getAddr())) {
38614013Sjavier.bueno@metempsy.com        if (useVirtualAddresses) {
38714013Sjavier.bueno@metempsy.com            // if we trained with virtual addresses,
38814013Sjavier.bueno@metempsy.com            // compute the target PA using the original PA and adding the
38914013Sjavier.bueno@metempsy.com            // prefetch stride (difference between target VA and original VA)
39014013Sjavier.bueno@metempsy.com            target_paddr = positive_stride ? (pkt->req->getPaddr() + stride) :
39114013Sjavier.bueno@metempsy.com                (pkt->req->getPaddr() - stride);
39213551Sjavier.bueno@metempsy.com        } else {
39314013Sjavier.bueno@metempsy.com            target_paddr = new_pfi.getAddr();
39414013Sjavier.bueno@metempsy.com        }
39514013Sjavier.bueno@metempsy.com        has_target_pa = true;
39614013Sjavier.bueno@metempsy.com    } else {
39714013Sjavier.bueno@metempsy.com        // Page crossing reference
39814013Sjavier.bueno@metempsy.com
39914013Sjavier.bueno@metempsy.com        // ContextID is needed for translation
40014013Sjavier.bueno@metempsy.com        if (!pkt->req->hasContextId()) {
40114013Sjavier.bueno@metempsy.com            return;
40214013Sjavier.bueno@metempsy.com        }
40314013Sjavier.bueno@metempsy.com        if (useVirtualAddresses) {
40414013Sjavier.bueno@metempsy.com            has_target_pa = false;
40514013Sjavier.bueno@metempsy.com            translation_req = createPrefetchRequest(new_pfi.getAddr(), new_pfi,
40614013Sjavier.bueno@metempsy.com                                                    pkt);
40714013Sjavier.bueno@metempsy.com        } else if (pkt->req->hasVaddr()) {
40814013Sjavier.bueno@metempsy.com            has_target_pa = false;
40914013Sjavier.bueno@metempsy.com            // Compute the target VA using req->getVaddr + stride
41014013Sjavier.bueno@metempsy.com            Addr target_vaddr = positive_stride ?
41114013Sjavier.bueno@metempsy.com                (pkt->req->getVaddr() + stride) :
41214013Sjavier.bueno@metempsy.com                (pkt->req->getVaddr() - stride);
41314013Sjavier.bueno@metempsy.com            translation_req = createPrefetchRequest(target_vaddr, new_pfi,
41414013Sjavier.bueno@metempsy.com                                                    pkt);
41514013Sjavier.bueno@metempsy.com        } else {
41614013Sjavier.bueno@metempsy.com            // Using PA for training but the request does not have a VA,
41714013Sjavier.bueno@metempsy.com            // unable to process this page crossing prefetch.
41814013Sjavier.bueno@metempsy.com            return;
41913551Sjavier.bueno@metempsy.com        }
42013551Sjavier.bueno@metempsy.com    }
42114013Sjavier.bueno@metempsy.com    if (has_target_pa && cacheSnoop &&
42214013Sjavier.bueno@metempsy.com            (inCache(target_paddr, new_pfi.isSecure()) ||
42314013Sjavier.bueno@metempsy.com            inMissQueue(target_paddr, new_pfi.isSecure()))) {
42411439SRekai.GonzalezAlberquilla@arm.com        pfInCache++;
42511439SRekai.GonzalezAlberquilla@arm.com        DPRINTF(HWPrefetch, "Dropping redundant in "
42614013Sjavier.bueno@metempsy.com                "cache/MSHR prefetch addr:%#x\n", target_paddr);
42713551Sjavier.bueno@metempsy.com        return;
42811439SRekai.GonzalezAlberquilla@arm.com    }
42911439SRekai.GonzalezAlberquilla@arm.com
43014013Sjavier.bueno@metempsy.com    /* Create the packet and find the spot to insert it */
43114013Sjavier.bueno@metempsy.com    DeferredPacket dpp(this, new_pfi, 0, priority);
43214013Sjavier.bueno@metempsy.com    if (has_target_pa) {
43314013Sjavier.bueno@metempsy.com        Tick pf_time = curTick() + clockPeriod() * latency;
43414013Sjavier.bueno@metempsy.com        dpp.createPkt(target_paddr, blkSize, masterId, tagPrefetch, pf_time);
43514013Sjavier.bueno@metempsy.com        DPRINTF(HWPrefetch, "Prefetch queued. "
43614013Sjavier.bueno@metempsy.com                "addr:%#x priority: %3d tick:%lld.\n",
43714013Sjavier.bueno@metempsy.com                new_pfi.getAddr(), priority, pf_time);
43814013Sjavier.bueno@metempsy.com        addToQueue(pfq, dpp);
43914013Sjavier.bueno@metempsy.com    } else {
44014013Sjavier.bueno@metempsy.com        // Add the translation request and try to resolve it later
44114013Sjavier.bueno@metempsy.com        dpp.setTranslationRequest(translation_req);
44214013Sjavier.bueno@metempsy.com        dpp.tc = cache->system->getThreadContext(translation_req->contextId());
44314013Sjavier.bueno@metempsy.com        DPRINTF(HWPrefetch, "Prefetch queued with no translation. "
44414013Sjavier.bueno@metempsy.com                "addr:%#x priority: %3d\n", new_pfi.getAddr(), priority);
44514013Sjavier.bueno@metempsy.com        addToQueue(pfqMissingTranslation, dpp);
44614013Sjavier.bueno@metempsy.com    }
44714013Sjavier.bueno@metempsy.com}
44811439SRekai.GonzalezAlberquilla@arm.com
44914013Sjavier.bueno@metempsy.comvoid
45014013Sjavier.bueno@metempsy.comQueuedPrefetcher::addToQueue(std::list<DeferredPacket> &queue,
45114013Sjavier.bueno@metempsy.com                             DeferredPacket &dpp)
45214013Sjavier.bueno@metempsy.com{
45311439SRekai.GonzalezAlberquilla@arm.com    /* Verify prefetch buffer space for request */
45414013Sjavier.bueno@metempsy.com    if (queue.size() == queueSize) {
45511439SRekai.GonzalezAlberquilla@arm.com        pfRemovedFull++;
45611439SRekai.GonzalezAlberquilla@arm.com        /* Lowest priority packet */
45714013Sjavier.bueno@metempsy.com        iterator it = queue.end();
45814013Sjavier.bueno@metempsy.com        panic_if (it == queue.begin(),
45914013Sjavier.bueno@metempsy.com            "Prefetch queue is both full and empty!");
46011439SRekai.GonzalezAlberquilla@arm.com        --it;
46111439SRekai.GonzalezAlberquilla@arm.com        /* Look for oldest in that level of priority */
46214013Sjavier.bueno@metempsy.com        panic_if (it == queue.begin(),
46314013Sjavier.bueno@metempsy.com            "Prefetch queue is full with 1 element!");
46411439SRekai.GonzalezAlberquilla@arm.com        iterator prev = it;
46511439SRekai.GonzalezAlberquilla@arm.com        bool cont = true;
46611439SRekai.GonzalezAlberquilla@arm.com        /* While not at the head of the queue */
46714013Sjavier.bueno@metempsy.com        while (cont && prev != queue.begin()) {
46811439SRekai.GonzalezAlberquilla@arm.com            prev--;
46911439SRekai.GonzalezAlberquilla@arm.com            /* While at the same level of priority */
47013551Sjavier.bueno@metempsy.com            cont = prev->priority == it->priority;
47111439SRekai.GonzalezAlberquilla@arm.com            if (cont)
47211439SRekai.GonzalezAlberquilla@arm.com                /* update pointer */
47311439SRekai.GonzalezAlberquilla@arm.com                it = prev;
47411439SRekai.GonzalezAlberquilla@arm.com        }
47511439SRekai.GonzalezAlberquilla@arm.com        DPRINTF(HWPrefetch, "Prefetch queue full, removing lowest priority "
47614013Sjavier.bueno@metempsy.com                            "oldest packet, addr: %#x\n",it->pfInfo.getAddr());
47711439SRekai.GonzalezAlberquilla@arm.com        delete it->pkt;
47814013Sjavier.bueno@metempsy.com        queue.erase(it);
47911439SRekai.GonzalezAlberquilla@arm.com    }
48011439SRekai.GonzalezAlberquilla@arm.com
48114013Sjavier.bueno@metempsy.com    if (queue.size() == 0) {
48214013Sjavier.bueno@metempsy.com        queue.emplace_back(dpp);
48311439SRekai.GonzalezAlberquilla@arm.com    } else {
48414013Sjavier.bueno@metempsy.com        iterator it = queue.end();
48513428Sjavier.bueno@metempsy.com        do {
48611439SRekai.GonzalezAlberquilla@arm.com            --it;
48714013Sjavier.bueno@metempsy.com        } while (it != queue.begin() && dpp > *it);
48811439SRekai.GonzalezAlberquilla@arm.com        /* If we reach the head, we have to see if the new element is new head
48911439SRekai.GonzalezAlberquilla@arm.com         * or not */
49014013Sjavier.bueno@metempsy.com        if (it == queue.begin() && dpp <= *it)
49111439SRekai.GonzalezAlberquilla@arm.com            it++;
49214013Sjavier.bueno@metempsy.com        queue.insert(it, dpp);
49311439SRekai.GonzalezAlberquilla@arm.com    }
49411439SRekai.GonzalezAlberquilla@arm.com}
495