base.cc revision 10466
12810SN/A/*
29546Sandreas.hansson@arm.com * Copyright (c) 2013 ARM Limited
39546Sandreas.hansson@arm.com * All rights reserved.
49546Sandreas.hansson@arm.com *
59546Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
69546Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
79546Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
89546Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
99546Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
109546Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
119546Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
129546Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
139546Sandreas.hansson@arm.com *
142810SN/A * Copyright (c) 2005 The Regents of The University of Michigan
152810SN/A * All rights reserved.
162810SN/A *
172810SN/A * Redistribution and use in source and binary forms, with or without
182810SN/A * modification, are permitted provided that the following conditions are
192810SN/A * met: redistributions of source code must retain the above copyright
202810SN/A * notice, this list of conditions and the following disclaimer;
212810SN/A * redistributions in binary form must reproduce the above copyright
222810SN/A * notice, this list of conditions and the following disclaimer in the
232810SN/A * documentation and/or other materials provided with the distribution;
242810SN/A * neither the name of the copyright holders nor the names of its
252810SN/A * contributors may be used to endorse or promote products derived from
262810SN/A * this software without specific prior written permission.
272810SN/A *
282810SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292810SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302810SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312810SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322810SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332810SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342810SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352810SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362810SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372810SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382810SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392810SN/A *
402810SN/A * Authors: Ron Dreslinski
412810SN/A */
422810SN/A
432810SN/A/**
442810SN/A * @file
452810SN/A * Hardware Prefetcher Definition.
462810SN/A */
472810SN/A
486658Snate@binkert.org#include <list>
496658Snate@binkert.org
502810SN/A#include "base/trace.hh"
518232Snate@binkert.org#include "debug/HWPrefetch.hh"
528229Snate@binkert.org#include "mem/cache/prefetch/base.hh"
535338Sstever@gmail.com#include "mem/cache/base.hh"
542814SN/A#include "mem/request.hh"
558832SAli.Saidi@ARM.com#include "sim/system.hh"
562810SN/A
578831Smrinmoy.ghosh@arm.comBasePrefetcher::BasePrefetcher(const Params *p)
5810360Sandreas.hansson@arm.com    : ClockedObject(p), size(p->size), cache(nullptr), blkSize(0),
5910360Sandreas.hansson@arm.com      latency(p->latency), degree(p->degree),
608832SAli.Saidi@ARM.com      useMasterId(p->use_master_id), pageStop(!p->cross_pages),
618832SAli.Saidi@ARM.com      serialSquash(p->serial_squash), onlyData(p->data_accesses_only),
6210052Smitch.hayenga+gem5@gmail.com      onMissOnly(p->on_miss_only), onReadOnly(p->on_read_only),
6310052Smitch.hayenga+gem5@gmail.com      onPrefetch(p->on_prefetch), system(p->sys),
6410466Sandreas.hansson@arm.com      masterId(system->getMasterId(name())),
6510466Sandreas.hansson@arm.com      pageBytes(system->getPageBytes())
662810SN/A{
672810SN/A}
682810SN/A
692810SN/Avoid
702810SN/ABasePrefetcher::setCache(BaseCache *_cache)
712810SN/A{
7210360Sandreas.hansson@arm.com    assert(!cache);
732810SN/A    cache = _cache;
742810SN/A    blkSize = cache->getBlockSize();
752810SN/A}
762810SN/A
772810SN/Avoid
788831Smrinmoy.ghosh@arm.comBasePrefetcher::regStats()
792810SN/A{
802810SN/A    pfIdentified
818831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_identified")
822810SN/A        .desc("number of hwpf identified")
832810SN/A        ;
842810SN/A
852810SN/A    pfMSHRHit
868831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_already_in_mshr")
872810SN/A        .desc("number of hwpf that were already in mshr")
882810SN/A        ;
892810SN/A
902810SN/A    pfCacheHit
918831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_already_in_cache")
922810SN/A        .desc("number of hwpf that were already in the cache")
932810SN/A        ;
942810SN/A
952810SN/A    pfBufferHit
968831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_already_in_prefetcher")
972810SN/A        .desc("number of hwpf that were already in the prefetch queue")
982810SN/A        ;
992810SN/A
1002810SN/A    pfRemovedFull
1018831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_evicted")
1022810SN/A        .desc("number of hwpf removed due to no buffer left")
1032810SN/A        ;
1042810SN/A
1052810SN/A    pfRemovedMSHR
1068831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_removed_MSHR_hit")
1072810SN/A        .desc("number of hwpf removed because MSHR allocated")
1082810SN/A        ;
1092810SN/A
1102810SN/A    pfIssued
1118831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_issued")
1122810SN/A        .desc("number of hwpf issued")
1132810SN/A        ;
1142810SN/A
1152810SN/A    pfSpanPage
1168831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_span_page")
1172810SN/A        .desc("number of hwpf spanning a virtual page")
1182810SN/A        ;
1192810SN/A
1202810SN/A    pfSquashed
1218831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_squashed_from_miss")
1225875Ssteve.reinhardt@amd.com        .desc("number of hwpf that got squashed due to a miss "
1235875Ssteve.reinhardt@amd.com              "aborting calculation time")
1242810SN/A        ;
1252810SN/A}
1262810SN/A
1273861SN/Ainline bool
12810028SGiacomo.Gabrielli@arm.comBasePrefetcher::inCache(Addr addr, bool is_secure)
1293861SN/A{
13010028SGiacomo.Gabrielli@arm.com    if (cache->inCache(addr, is_secure)) {
1313861SN/A        pfCacheHit++;
1323861SN/A        return true;
1333861SN/A    }
1343861SN/A    return false;
1353861SN/A}
1363861SN/A
1373861SN/Ainline bool
13810028SGiacomo.Gabrielli@arm.comBasePrefetcher::inMissQueue(Addr addr, bool is_secure)
1393861SN/A{
14010028SGiacomo.Gabrielli@arm.com    if (cache->inMissQueue(addr, is_secure)) {
1413861SN/A        pfMSHRHit++;
1423861SN/A        return true;
1433861SN/A    }
1443861SN/A    return false;
1453861SN/A}
1463861SN/A
1473349SN/APacketPtr
1482810SN/ABasePrefetcher::getPacket()
1492810SN/A{
1505875Ssteve.reinhardt@amd.com    DPRINTF(HWPrefetch, "Requesting a hw_pf to issue\n");
1512810SN/A
1522810SN/A    if (pf.empty()) {
1535875Ssteve.reinhardt@amd.com        DPRINTF(HWPrefetch, "No HW_PF found\n");
1542810SN/A        return NULL;
1552810SN/A    }
1562810SN/A
1579546Sandreas.hansson@arm.com    PacketPtr pkt = pf.begin()->pkt;
1588509SAli.Saidi@ARM.com    while (!pf.empty()) {
1599546Sandreas.hansson@arm.com        pkt = pf.begin()->pkt;
1602810SN/A        pf.pop_front();
1615875Ssteve.reinhardt@amd.com
1628509SAli.Saidi@ARM.com        Addr blk_addr = pkt->getAddr() & ~(Addr)(blkSize-1);
16310028SGiacomo.Gabrielli@arm.com        bool is_secure = pkt->isSecure();
1648509SAli.Saidi@ARM.com
16510028SGiacomo.Gabrielli@arm.com        if (!inCache(blk_addr, is_secure) && !inMissQueue(blk_addr, is_secure))
1668509SAli.Saidi@ARM.com            // we found a prefetch, return it
1678509SAli.Saidi@ARM.com            break;
1688509SAli.Saidi@ARM.com
16910028SGiacomo.Gabrielli@arm.com        DPRINTF(HWPrefetch, "addr 0x%x (%s) in cache, skipping\n",
17010028SGiacomo.Gabrielli@arm.com                pkt->getAddr(), is_secure ? "s" : "ns");
1718509SAli.Saidi@ARM.com        delete pkt->req;
1728509SAli.Saidi@ARM.com        delete pkt;
1735875Ssteve.reinhardt@amd.com
1742810SN/A        if (pf.empty()) {
1754628SN/A            cache->deassertMemSideBusRequest(BaseCache::Request_PF);
1768509SAli.Saidi@ARM.com            return NULL; // None left, all were in cache
1772810SN/A        }
1788509SAli.Saidi@ARM.com    }
1792810SN/A
1802810SN/A    pfIssued++;
1815875Ssteve.reinhardt@amd.com    assert(pkt != NULL);
18210028SGiacomo.Gabrielli@arm.com    DPRINTF(HWPrefetch, "returning 0x%x (%s)\n", pkt->getAddr(),
18310028SGiacomo.Gabrielli@arm.com            pkt->isSecure() ? "s" : "ns");
1842810SN/A    return pkt;
1852810SN/A}
1862810SN/A
1875875Ssteve.reinhardt@amd.com
1885875Ssteve.reinhardt@amd.comTick
1899546Sandreas.hansson@arm.comBasePrefetcher::notify(PacketPtr &pkt, Tick tick)
1902810SN/A{
19110052Smitch.hayenga+gem5@gmail.com    // Don't consult the prefetcher if any of the following conditons are true
19210052Smitch.hayenga+gem5@gmail.com    // 1) The request is uncacheable
19310052Smitch.hayenga+gem5@gmail.com    // 2) The request is a fetch, but we are only prefeching data
19410052Smitch.hayenga+gem5@gmail.com    // 3) The request is a cache hit, but we are only training on misses
19510052Smitch.hayenga+gem5@gmail.com    // 4) THe request is a write, but we are only training on reads
19610052Smitch.hayenga+gem5@gmail.com    if (!pkt->req->isUncacheable() && !(pkt->req->isInstFetch() && onlyData) &&
19710052Smitch.hayenga+gem5@gmail.com        !(onMissOnly && inCache(pkt->getAddr(), true)) &&
19810052Smitch.hayenga+gem5@gmail.com        !(onReadOnly && !pkt->isRead())) {
1995875Ssteve.reinhardt@amd.com        // Calculate the blk address
2005875Ssteve.reinhardt@amd.com        Addr blk_addr = pkt->getAddr() & ~(Addr)(blkSize-1);
20110028SGiacomo.Gabrielli@arm.com        bool is_secure = pkt->isSecure();
2022810SN/A
2035875Ssteve.reinhardt@amd.com        // Check if miss is in pfq, if so remove it
20410028SGiacomo.Gabrielli@arm.com        std::list<DeferredPacket>::iterator iter = inPrefetch(blk_addr,
20510028SGiacomo.Gabrielli@arm.com                                                              is_secure);
2062810SN/A        if (iter != pf.end()) {
2075875Ssteve.reinhardt@amd.com            DPRINTF(HWPrefetch, "Saw a miss to a queued prefetch addr: "
20810028SGiacomo.Gabrielli@arm.com                    "0x%x (%s), removing it\n", blk_addr,
20910028SGiacomo.Gabrielli@arm.com                    is_secure ? "s" : "ns");
2102810SN/A            pfRemovedMSHR++;
2119546Sandreas.hansson@arm.com            delete iter->pkt->req;
2129546Sandreas.hansson@arm.com            delete iter->pkt;
2138991SAli.Saidi@ARM.com            iter = pf.erase(iter);
2142810SN/A            if (pf.empty())
2154628SN/A                cache->deassertMemSideBusRequest(BaseCache::Request_PF);
2162810SN/A        }
2172810SN/A
2185875Ssteve.reinhardt@amd.com        // Remove anything in queue with delay older than time
2195875Ssteve.reinhardt@amd.com        // since everything is inserted in time order, start from end
2205875Ssteve.reinhardt@amd.com        // and work until pf.empty() or time is earlier
2215875Ssteve.reinhardt@amd.com        // This is done to emulate Aborting the previous work on a new miss
2225875Ssteve.reinhardt@amd.com        // Needed for serial calculators like GHB
2232810SN/A        if (serialSquash) {
2242810SN/A            iter = pf.end();
2258991SAli.Saidi@ARM.com            if (iter != pf.begin())
2268991SAli.Saidi@ARM.com                iter--;
2279546Sandreas.hansson@arm.com            while (!pf.empty() && iter->tick >= tick) {
2282810SN/A                pfSquashed++;
2295875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Squashing old prefetch addr: 0x%x\n",
2309546Sandreas.hansson@arm.com                        iter->pkt->getAddr());
2319546Sandreas.hansson@arm.com                delete iter->pkt->req;
2329546Sandreas.hansson@arm.com                delete iter->pkt;
2338991SAli.Saidi@ARM.com                iter = pf.erase(iter);
2348991SAli.Saidi@ARM.com                if (iter != pf.begin())
2358991SAli.Saidi@ARM.com                    iter--;
2362810SN/A            }
2372810SN/A            if (pf.empty())
2384628SN/A                cache->deassertMemSideBusRequest(BaseCache::Request_PF);
2392810SN/A        }
2402810SN/A
2412810SN/A
2422810SN/A        std::list<Addr> addresses;
2439288Sandreas.hansson@arm.com        std::list<Cycles> delays;
2442810SN/A        calculatePrefetch(pkt, addresses, delays);
2452810SN/A
2465875Ssteve.reinhardt@amd.com        std::list<Addr>::iterator addrIter = addresses.begin();
2479288Sandreas.hansson@arm.com        std::list<Cycles>::iterator delayIter = delays.begin();
2485875Ssteve.reinhardt@amd.com        for (; addrIter != addresses.end(); ++addrIter, ++delayIter) {
2495875Ssteve.reinhardt@amd.com            Addr addr = *addrIter;
2505875Ssteve.reinhardt@amd.com
2512810SN/A            pfIdentified++;
2525875Ssteve.reinhardt@amd.com
2535875Ssteve.reinhardt@amd.com            DPRINTF(HWPrefetch, "Found a pf candidate addr: 0x%x, "
2545875Ssteve.reinhardt@amd.com                    "inserting into prefetch queue with delay %d time %d\n",
2555875Ssteve.reinhardt@amd.com                    addr, *delayIter, time);
2565875Ssteve.reinhardt@amd.com
2575875Ssteve.reinhardt@amd.com            // Check if it is already in the pf buffer
25810028SGiacomo.Gabrielli@arm.com            if (inPrefetch(addr, is_secure) != pf.end()) {
2595875Ssteve.reinhardt@amd.com                pfBufferHit++;
2605875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Prefetch addr already in pf buffer\n");
2615875Ssteve.reinhardt@amd.com                continue;
2625875Ssteve.reinhardt@amd.com            }
2635875Ssteve.reinhardt@amd.com
2645875Ssteve.reinhardt@amd.com            // create a prefetch memreq
2658832SAli.Saidi@ARM.com            Request *prefetchReq = new Request(*addrIter, blkSize, 0, masterId);
26610028SGiacomo.Gabrielli@arm.com            if (is_secure)
26710028SGiacomo.Gabrielli@arm.com                prefetchReq->setFlags(Request::SECURE);
26810024Sdam.sunwoo@arm.com            prefetchReq->taskId(ContextSwitchTaskId::Prefetcher);
2695875Ssteve.reinhardt@amd.com            PacketPtr prefetch =
2708949Sandreas.hansson@arm.com                new Packet(prefetchReq, MemCmd::HardPFReq);
2712825SN/A            prefetch->allocate();
2725714Shsul@eecs.umich.edu            prefetch->req->setThreadContext(pkt->req->contextId(),
2735714Shsul@eecs.umich.edu                                            pkt->req->threadId());
2742814SN/A
27510052Smitch.hayenga+gem5@gmail.com            // Tag orefetch reqeuests with corresponding PC to train lower
27610052Smitch.hayenga+gem5@gmail.com            // cache-level prefetchers
27710052Smitch.hayenga+gem5@gmail.com            if (onPrefetch && pkt->req->hasPC())
27810052Smitch.hayenga+gem5@gmail.com                prefetch->req->setPC(pkt->req->getPC());
27910052Smitch.hayenga+gem5@gmail.com
2805875Ssteve.reinhardt@amd.com            // We just remove the head if we are full
2815875Ssteve.reinhardt@amd.com            if (pf.size() == size) {
2822810SN/A                pfRemovedFull++;
2839546Sandreas.hansson@arm.com                PacketPtr old_pkt = pf.begin()->pkt;
2845875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Prefetch queue full, "
2855875Ssteve.reinhardt@amd.com                        "removing oldest 0x%x\n", old_pkt->getAddr());
2865875Ssteve.reinhardt@amd.com                delete old_pkt->req;
2875875Ssteve.reinhardt@amd.com                delete old_pkt;
2882810SN/A                pf.pop_front();
2892810SN/A            }
2902810SN/A
2919546Sandreas.hansson@arm.com            pf.push_back(DeferredPacket(tick + clockPeriod() * *delayIter,
2929546Sandreas.hansson@arm.com                                        prefetch));
2932810SN/A        }
2942810SN/A    }
2955875Ssteve.reinhardt@amd.com
2969546Sandreas.hansson@arm.com    return pf.empty() ? 0 : pf.front().tick;
2972810SN/A}
2982810SN/A
2999546Sandreas.hansson@arm.comstd::list<BasePrefetcher::DeferredPacket>::iterator
30010028SGiacomo.Gabrielli@arm.comBasePrefetcher::inPrefetch(Addr address, bool is_secure)
3012810SN/A{
3025875Ssteve.reinhardt@amd.com    // Guaranteed to only be one match, we always check before inserting
3039546Sandreas.hansson@arm.com    std::list<DeferredPacket>::iterator iter;
3045875Ssteve.reinhardt@amd.com    for (iter = pf.begin(); iter != pf.end(); iter++) {
30510028SGiacomo.Gabrielli@arm.com        if (((*iter).pkt->getAddr() & ~(Addr)(blkSize-1)) == address &&
30610028SGiacomo.Gabrielli@arm.com            (*iter).pkt->isSecure() == is_secure) {
3072810SN/A            return iter;
3082810SN/A        }
3092810SN/A    }
3102810SN/A    return pf.end();
3112810SN/A}
3122810SN/A
3135875Ssteve.reinhardt@amd.combool
31410466Sandreas.hansson@arm.comBasePrefetcher::samePage(Addr a, Addr b) const
3155875Ssteve.reinhardt@amd.com{
31610466Sandreas.hansson@arm.com    return roundDown(a, pageBytes) == roundDown(b, pageBytes);
3175875Ssteve.reinhardt@amd.com}
3188831Smrinmoy.ghosh@arm.com
3198831Smrinmoy.ghosh@arm.com
320