base.cc revision 10052
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
505875Ssteve.reinhardt@amd.com#include "arch/isa_traits.hh"
512810SN/A#include "base/trace.hh"
526658Snate@binkert.org#include "config/the_isa.hh"
538232Snate@binkert.org#include "debug/HWPrefetch.hh"
548229Snate@binkert.org#include "mem/cache/prefetch/base.hh"
555338Sstever@gmail.com#include "mem/cache/base.hh"
562814SN/A#include "mem/request.hh"
578832SAli.Saidi@ARM.com#include "sim/system.hh"
582810SN/A
598831Smrinmoy.ghosh@arm.comBasePrefetcher::BasePrefetcher(const Params *p)
609288Sandreas.hansson@arm.com    : ClockedObject(p), size(p->size), latency(p->latency), degree(p->degree),
618832SAli.Saidi@ARM.com      useMasterId(p->use_master_id), pageStop(!p->cross_pages),
628832SAli.Saidi@ARM.com      serialSquash(p->serial_squash), onlyData(p->data_accesses_only),
6310052Smitch.hayenga+gem5@gmail.com      onMissOnly(p->on_miss_only), onReadOnly(p->on_read_only),
6410052Smitch.hayenga+gem5@gmail.com      onPrefetch(p->on_prefetch), system(p->sys),
6510052Smitch.hayenga+gem5@gmail.com      masterId(system->getMasterId(name()))
662810SN/A{
672810SN/A}
682810SN/A
692810SN/Avoid
702810SN/ABasePrefetcher::setCache(BaseCache *_cache)
712810SN/A{
722810SN/A    cache = _cache;
732810SN/A    blkSize = cache->getBlockSize();
742810SN/A}
752810SN/A
762810SN/Avoid
778831Smrinmoy.ghosh@arm.comBasePrefetcher::regStats()
782810SN/A{
792810SN/A    pfIdentified
808831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_identified")
812810SN/A        .desc("number of hwpf identified")
822810SN/A        ;
832810SN/A
842810SN/A    pfMSHRHit
858831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_already_in_mshr")
862810SN/A        .desc("number of hwpf that were already in mshr")
872810SN/A        ;
882810SN/A
892810SN/A    pfCacheHit
908831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_already_in_cache")
912810SN/A        .desc("number of hwpf that were already in the cache")
922810SN/A        ;
932810SN/A
942810SN/A    pfBufferHit
958831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_already_in_prefetcher")
962810SN/A        .desc("number of hwpf that were already in the prefetch queue")
972810SN/A        ;
982810SN/A
992810SN/A    pfRemovedFull
1008831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_evicted")
1012810SN/A        .desc("number of hwpf removed due to no buffer left")
1022810SN/A        ;
1032810SN/A
1042810SN/A    pfRemovedMSHR
1058831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_removed_MSHR_hit")
1062810SN/A        .desc("number of hwpf removed because MSHR allocated")
1072810SN/A        ;
1082810SN/A
1092810SN/A    pfIssued
1108831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_issued")
1112810SN/A        .desc("number of hwpf issued")
1122810SN/A        ;
1132810SN/A
1142810SN/A    pfSpanPage
1158831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_span_page")
1162810SN/A        .desc("number of hwpf spanning a virtual page")
1172810SN/A        ;
1182810SN/A
1192810SN/A    pfSquashed
1208831Smrinmoy.ghosh@arm.com        .name(name() + ".prefetcher.num_hwpf_squashed_from_miss")
1215875Ssteve.reinhardt@amd.com        .desc("number of hwpf that got squashed due to a miss "
1225875Ssteve.reinhardt@amd.com              "aborting calculation time")
1232810SN/A        ;
1242810SN/A}
1252810SN/A
1263861SN/Ainline bool
12710028SGiacomo.Gabrielli@arm.comBasePrefetcher::inCache(Addr addr, bool is_secure)
1283861SN/A{
12910028SGiacomo.Gabrielli@arm.com    if (cache->inCache(addr, is_secure)) {
1303861SN/A        pfCacheHit++;
1313861SN/A        return true;
1323861SN/A    }
1333861SN/A    return false;
1343861SN/A}
1353861SN/A
1363861SN/Ainline bool
13710028SGiacomo.Gabrielli@arm.comBasePrefetcher::inMissQueue(Addr addr, bool is_secure)
1383861SN/A{
13910028SGiacomo.Gabrielli@arm.com    if (cache->inMissQueue(addr, is_secure)) {
1403861SN/A        pfMSHRHit++;
1413861SN/A        return true;
1423861SN/A    }
1433861SN/A    return false;
1443861SN/A}
1453861SN/A
1463349SN/APacketPtr
1472810SN/ABasePrefetcher::getPacket()
1482810SN/A{
1495875Ssteve.reinhardt@amd.com    DPRINTF(HWPrefetch, "Requesting a hw_pf to issue\n");
1502810SN/A
1512810SN/A    if (pf.empty()) {
1525875Ssteve.reinhardt@amd.com        DPRINTF(HWPrefetch, "No HW_PF found\n");
1532810SN/A        return NULL;
1542810SN/A    }
1552810SN/A
1569546Sandreas.hansson@arm.com    PacketPtr pkt = pf.begin()->pkt;
1578509SAli.Saidi@ARM.com    while (!pf.empty()) {
1589546Sandreas.hansson@arm.com        pkt = pf.begin()->pkt;
1592810SN/A        pf.pop_front();
1605875Ssteve.reinhardt@amd.com
1618509SAli.Saidi@ARM.com        Addr blk_addr = pkt->getAddr() & ~(Addr)(blkSize-1);
16210028SGiacomo.Gabrielli@arm.com        bool is_secure = pkt->isSecure();
1638509SAli.Saidi@ARM.com
16410028SGiacomo.Gabrielli@arm.com        if (!inCache(blk_addr, is_secure) && !inMissQueue(blk_addr, is_secure))
1658509SAli.Saidi@ARM.com            // we found a prefetch, return it
1668509SAli.Saidi@ARM.com            break;
1678509SAli.Saidi@ARM.com
16810028SGiacomo.Gabrielli@arm.com        DPRINTF(HWPrefetch, "addr 0x%x (%s) in cache, skipping\n",
16910028SGiacomo.Gabrielli@arm.com                pkt->getAddr(), is_secure ? "s" : "ns");
1708509SAli.Saidi@ARM.com        delete pkt->req;
1718509SAli.Saidi@ARM.com        delete pkt;
1725875Ssteve.reinhardt@amd.com
1732810SN/A        if (pf.empty()) {
1744628SN/A            cache->deassertMemSideBusRequest(BaseCache::Request_PF);
1758509SAli.Saidi@ARM.com            return NULL; // None left, all were in cache
1762810SN/A        }
1778509SAli.Saidi@ARM.com    }
1782810SN/A
1792810SN/A    pfIssued++;
1805875Ssteve.reinhardt@amd.com    assert(pkt != NULL);
18110028SGiacomo.Gabrielli@arm.com    DPRINTF(HWPrefetch, "returning 0x%x (%s)\n", pkt->getAddr(),
18210028SGiacomo.Gabrielli@arm.com            pkt->isSecure() ? "s" : "ns");
1832810SN/A    return pkt;
1842810SN/A}
1852810SN/A
1865875Ssteve.reinhardt@amd.com
1875875Ssteve.reinhardt@amd.comTick
1889546Sandreas.hansson@arm.comBasePrefetcher::notify(PacketPtr &pkt, Tick tick)
1892810SN/A{
19010052Smitch.hayenga+gem5@gmail.com    // Don't consult the prefetcher if any of the following conditons are true
19110052Smitch.hayenga+gem5@gmail.com    // 1) The request is uncacheable
19210052Smitch.hayenga+gem5@gmail.com    // 2) The request is a fetch, but we are only prefeching data
19310052Smitch.hayenga+gem5@gmail.com    // 3) The request is a cache hit, but we are only training on misses
19410052Smitch.hayenga+gem5@gmail.com    // 4) THe request is a write, but we are only training on reads
19510052Smitch.hayenga+gem5@gmail.com    if (!pkt->req->isUncacheable() && !(pkt->req->isInstFetch() && onlyData) &&
19610052Smitch.hayenga+gem5@gmail.com        !(onMissOnly && inCache(pkt->getAddr(), true)) &&
19710052Smitch.hayenga+gem5@gmail.com        !(onReadOnly && !pkt->isRead())) {
1985875Ssteve.reinhardt@amd.com        // Calculate the blk address
1995875Ssteve.reinhardt@amd.com        Addr blk_addr = pkt->getAddr() & ~(Addr)(blkSize-1);
20010028SGiacomo.Gabrielli@arm.com        bool is_secure = pkt->isSecure();
2012810SN/A
2025875Ssteve.reinhardt@amd.com        // Check if miss is in pfq, if so remove it
20310028SGiacomo.Gabrielli@arm.com        std::list<DeferredPacket>::iterator iter = inPrefetch(blk_addr,
20410028SGiacomo.Gabrielli@arm.com                                                              is_secure);
2052810SN/A        if (iter != pf.end()) {
2065875Ssteve.reinhardt@amd.com            DPRINTF(HWPrefetch, "Saw a miss to a queued prefetch addr: "
20710028SGiacomo.Gabrielli@arm.com                    "0x%x (%s), removing it\n", blk_addr,
20810028SGiacomo.Gabrielli@arm.com                    is_secure ? "s" : "ns");
2092810SN/A            pfRemovedMSHR++;
2109546Sandreas.hansson@arm.com            delete iter->pkt->req;
2119546Sandreas.hansson@arm.com            delete iter->pkt;
2128991SAli.Saidi@ARM.com            iter = pf.erase(iter);
2132810SN/A            if (pf.empty())
2144628SN/A                cache->deassertMemSideBusRequest(BaseCache::Request_PF);
2152810SN/A        }
2162810SN/A
2175875Ssteve.reinhardt@amd.com        // Remove anything in queue with delay older than time
2185875Ssteve.reinhardt@amd.com        // since everything is inserted in time order, start from end
2195875Ssteve.reinhardt@amd.com        // and work until pf.empty() or time is earlier
2205875Ssteve.reinhardt@amd.com        // This is done to emulate Aborting the previous work on a new miss
2215875Ssteve.reinhardt@amd.com        // Needed for serial calculators like GHB
2222810SN/A        if (serialSquash) {
2232810SN/A            iter = pf.end();
2248991SAli.Saidi@ARM.com            if (iter != pf.begin())
2258991SAli.Saidi@ARM.com                iter--;
2269546Sandreas.hansson@arm.com            while (!pf.empty() && iter->tick >= tick) {
2272810SN/A                pfSquashed++;
2285875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Squashing old prefetch addr: 0x%x\n",
2299546Sandreas.hansson@arm.com                        iter->pkt->getAddr());
2309546Sandreas.hansson@arm.com                delete iter->pkt->req;
2319546Sandreas.hansson@arm.com                delete iter->pkt;
2328991SAli.Saidi@ARM.com                iter = pf.erase(iter);
2338991SAli.Saidi@ARM.com                if (iter != pf.begin())
2348991SAli.Saidi@ARM.com                    iter--;
2352810SN/A            }
2362810SN/A            if (pf.empty())
2374628SN/A                cache->deassertMemSideBusRequest(BaseCache::Request_PF);
2382810SN/A        }
2392810SN/A
2402810SN/A
2412810SN/A        std::list<Addr> addresses;
2429288Sandreas.hansson@arm.com        std::list<Cycles> delays;
2432810SN/A        calculatePrefetch(pkt, addresses, delays);
2442810SN/A
2455875Ssteve.reinhardt@amd.com        std::list<Addr>::iterator addrIter = addresses.begin();
2469288Sandreas.hansson@arm.com        std::list<Cycles>::iterator delayIter = delays.begin();
2475875Ssteve.reinhardt@amd.com        for (; addrIter != addresses.end(); ++addrIter, ++delayIter) {
2485875Ssteve.reinhardt@amd.com            Addr addr = *addrIter;
2495875Ssteve.reinhardt@amd.com
2502810SN/A            pfIdentified++;
2515875Ssteve.reinhardt@amd.com
2525875Ssteve.reinhardt@amd.com            DPRINTF(HWPrefetch, "Found a pf candidate addr: 0x%x, "
2535875Ssteve.reinhardt@amd.com                    "inserting into prefetch queue with delay %d time %d\n",
2545875Ssteve.reinhardt@amd.com                    addr, *delayIter, time);
2555875Ssteve.reinhardt@amd.com
2565875Ssteve.reinhardt@amd.com            // Check if it is already in the pf buffer
25710028SGiacomo.Gabrielli@arm.com            if (inPrefetch(addr, is_secure) != pf.end()) {
2585875Ssteve.reinhardt@amd.com                pfBufferHit++;
2595875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Prefetch addr already in pf buffer\n");
2605875Ssteve.reinhardt@amd.com                continue;
2615875Ssteve.reinhardt@amd.com            }
2625875Ssteve.reinhardt@amd.com
2635875Ssteve.reinhardt@amd.com            // create a prefetch memreq
2648832SAli.Saidi@ARM.com            Request *prefetchReq = new Request(*addrIter, blkSize, 0, masterId);
26510028SGiacomo.Gabrielli@arm.com            if (is_secure)
26610028SGiacomo.Gabrielli@arm.com                prefetchReq->setFlags(Request::SECURE);
26710024Sdam.sunwoo@arm.com            prefetchReq->taskId(ContextSwitchTaskId::Prefetcher);
2685875Ssteve.reinhardt@amd.com            PacketPtr prefetch =
2698949Sandreas.hansson@arm.com                new Packet(prefetchReq, MemCmd::HardPFReq);
2702825SN/A            prefetch->allocate();
2715714Shsul@eecs.umich.edu            prefetch->req->setThreadContext(pkt->req->contextId(),
2725714Shsul@eecs.umich.edu                                            pkt->req->threadId());
2732814SN/A
27410052Smitch.hayenga+gem5@gmail.com            // Tag orefetch reqeuests with corresponding PC to train lower
27510052Smitch.hayenga+gem5@gmail.com            // cache-level prefetchers
27610052Smitch.hayenga+gem5@gmail.com            if (onPrefetch && pkt->req->hasPC())
27710052Smitch.hayenga+gem5@gmail.com                prefetch->req->setPC(pkt->req->getPC());
27810052Smitch.hayenga+gem5@gmail.com
2795875Ssteve.reinhardt@amd.com            // We just remove the head if we are full
2805875Ssteve.reinhardt@amd.com            if (pf.size() == size) {
2812810SN/A                pfRemovedFull++;
2829546Sandreas.hansson@arm.com                PacketPtr old_pkt = pf.begin()->pkt;
2835875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Prefetch queue full, "
2845875Ssteve.reinhardt@amd.com                        "removing oldest 0x%x\n", old_pkt->getAddr());
2855875Ssteve.reinhardt@amd.com                delete old_pkt->req;
2865875Ssteve.reinhardt@amd.com                delete old_pkt;
2872810SN/A                pf.pop_front();
2882810SN/A            }
2892810SN/A
2909546Sandreas.hansson@arm.com            pf.push_back(DeferredPacket(tick + clockPeriod() * *delayIter,
2919546Sandreas.hansson@arm.com                                        prefetch));
2922810SN/A        }
2932810SN/A    }
2945875Ssteve.reinhardt@amd.com
2959546Sandreas.hansson@arm.com    return pf.empty() ? 0 : pf.front().tick;
2962810SN/A}
2972810SN/A
2989546Sandreas.hansson@arm.comstd::list<BasePrefetcher::DeferredPacket>::iterator
29910028SGiacomo.Gabrielli@arm.comBasePrefetcher::inPrefetch(Addr address, bool is_secure)
3002810SN/A{
3015875Ssteve.reinhardt@amd.com    // Guaranteed to only be one match, we always check before inserting
3029546Sandreas.hansson@arm.com    std::list<DeferredPacket>::iterator iter;
3035875Ssteve.reinhardt@amd.com    for (iter = pf.begin(); iter != pf.end(); iter++) {
30410028SGiacomo.Gabrielli@arm.com        if (((*iter).pkt->getAddr() & ~(Addr)(blkSize-1)) == address &&
30510028SGiacomo.Gabrielli@arm.com            (*iter).pkt->isSecure() == is_secure) {
3062810SN/A            return iter;
3072810SN/A        }
3082810SN/A    }
3092810SN/A    return pf.end();
3102810SN/A}
3112810SN/A
3125875Ssteve.reinhardt@amd.combool
3135875Ssteve.reinhardt@amd.comBasePrefetcher::samePage(Addr a, Addr b)
3145875Ssteve.reinhardt@amd.com{
3155875Ssteve.reinhardt@amd.com    return roundDown(a, TheISA::VMPageSize) == roundDown(b, TheISA::VMPageSize);
3165875Ssteve.reinhardt@amd.com}
3178831Smrinmoy.ghosh@arm.com
3188831Smrinmoy.ghosh@arm.com
319