12810SN/A/*
210623Smitch.hayenga@arm.com * Copyright (c) 2013-2014 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
4110623Smitch.hayenga@arm.com *          Mitch Hayenga
422810SN/A */
432810SN/A
442810SN/A/**
452810SN/A * @file
462810SN/A * Hardware Prefetcher Definition.
472810SN/A */
482810SN/A
4911793Sbrandon.potter@amd.com#include "mem/cache/prefetch/base.hh"
5011793Sbrandon.potter@amd.com
5112727Snikos.nikoleris@arm.com#include <cassert>
526658Snate@binkert.org
5311438SRekai.GonzalezAlberquilla@arm.com#include "base/intmath.hh"
5413416Sjavier.bueno@metempsy.com#include "cpu/base.hh"
555338Sstever@gmail.com#include "mem/cache/base.hh"
5612727Snikos.nikoleris@arm.com#include "params/BasePrefetcher.hh"
578832SAli.Saidi@ARM.com#include "sim/system.hh"
582810SN/A
5913751Sjavier.bueno@metempsy.comBasePrefetcher::PrefetchInfo::PrefetchInfo(PacketPtr pkt, Addr addr, bool miss)
6013551Sjavier.bueno@metempsy.com  : address(addr), pc(pkt->req->hasPC() ? pkt->req->getPC() : 0),
6113551Sjavier.bueno@metempsy.com    masterId(pkt->req->masterId()), validPC(pkt->req->hasPC()),
6213751Sjavier.bueno@metempsy.com    secure(pkt->isSecure()), size(pkt->req->getSize()), write(pkt->isWrite()),
6313751Sjavier.bueno@metempsy.com    paddress(pkt->req->getPaddr()), cacheMiss(miss)
6413551Sjavier.bueno@metempsy.com{
6513751Sjavier.bueno@metempsy.com    unsigned int req_size = pkt->req->getSize();
6613751Sjavier.bueno@metempsy.com    if (!write && miss) {
6713751Sjavier.bueno@metempsy.com        data = nullptr;
6813751Sjavier.bueno@metempsy.com    } else {
6913751Sjavier.bueno@metempsy.com        data = new uint8_t[req_size];
7013751Sjavier.bueno@metempsy.com        Addr offset = pkt->req->getPaddr() - pkt->getAddr();
7113751Sjavier.bueno@metempsy.com        std::memcpy(data, &(pkt->getConstPtr<uint8_t>()[offset]), req_size);
7213751Sjavier.bueno@metempsy.com    }
7313551Sjavier.bueno@metempsy.com}
7413551Sjavier.bueno@metempsy.com
7513551Sjavier.bueno@metempsy.comBasePrefetcher::PrefetchInfo::PrefetchInfo(PrefetchInfo const &pfi, Addr addr)
7613551Sjavier.bueno@metempsy.com  : address(addr), pc(pfi.pc), masterId(pfi.masterId), validPC(pfi.validPC),
7713751Sjavier.bueno@metempsy.com    secure(pfi.secure), size(pfi.size), write(pfi.write),
7813751Sjavier.bueno@metempsy.com    paddress(pfi.paddress), cacheMiss(pfi.cacheMiss), data(nullptr)
7913551Sjavier.bueno@metempsy.com{
8013551Sjavier.bueno@metempsy.com}
8113551Sjavier.bueno@metempsy.com
8213416Sjavier.bueno@metempsy.comvoid
8313416Sjavier.bueno@metempsy.comBasePrefetcher::PrefetchListener::notify(const PacketPtr &pkt)
8413416Sjavier.bueno@metempsy.com{
8513717Sivan.pizarro@metempsy.com    if (isFill) {
8613717Sivan.pizarro@metempsy.com        parent.notifyFill(pkt);
8713717Sivan.pizarro@metempsy.com    } else {
8813751Sjavier.bueno@metempsy.com        parent.probeNotify(pkt, miss);
8913717Sivan.pizarro@metempsy.com    }
9013416Sjavier.bueno@metempsy.com}
9113416Sjavier.bueno@metempsy.com
9210623Smitch.hayenga@arm.comBasePrefetcher::BasePrefetcher(const BasePrefetcherParams *p)
9313422Sodanrc@yahoo.com.br    : ClockedObject(p), listeners(), cache(nullptr), blkSize(p->block_size),
9413422Sodanrc@yahoo.com.br      lBlkSize(floorLog2(blkSize)), onMiss(p->on_miss), onRead(p->on_read),
9510623Smitch.hayenga@arm.com      onWrite(p->on_write), onData(p->on_data), onInst(p->on_inst),
9613422Sodanrc@yahoo.com.br      masterId(p->sys->getMasterId(this)), pageBytes(p->sys->getPageBytes()),
9713551Sjavier.bueno@metempsy.com      prefetchOnAccess(p->prefetch_on_access),
9813624Sjavier.bueno@metempsy.com      useVirtualAddresses(p->use_virtual_addresses), issuedPrefetches(0),
9914013Sjavier.bueno@metempsy.com      usefulPrefetches(0), tlb(nullptr)
1002810SN/A{
1012810SN/A}
1022810SN/A
1032810SN/Avoid
1042810SN/ABasePrefetcher::setCache(BaseCache *_cache)
1052810SN/A{
10610360Sandreas.hansson@arm.com    assert(!cache);
1072810SN/A    cache = _cache;
10813422Sodanrc@yahoo.com.br
10913422Sodanrc@yahoo.com.br    // If the cache has a different block size from the system's, save it
1102810SN/A    blkSize = cache->getBlockSize();
11111438SRekai.GonzalezAlberquilla@arm.com    lBlkSize = floorLog2(blkSize);
1122810SN/A}
1132810SN/A
1142810SN/Avoid
1158831Smrinmoy.ghosh@arm.comBasePrefetcher::regStats()
1162810SN/A{
11711522Sstephan.diestelhorst@arm.com    ClockedObject::regStats();
11811522Sstephan.diestelhorst@arm.com
1192810SN/A    pfIssued
12010623Smitch.hayenga@arm.com        .name(name() + ".num_hwpf_issued")
1212810SN/A        .desc("number of hwpf issued")
1222810SN/A        ;
12311438SRekai.GonzalezAlberquilla@arm.com
1242810SN/A}
1252810SN/A
12610623Smitch.hayenga@arm.combool
12713751Sjavier.bueno@metempsy.comBasePrefetcher::observeAccess(const PacketPtr &pkt, bool miss) const
12810623Smitch.hayenga@arm.com{
12910623Smitch.hayenga@arm.com    bool fetch = pkt->req->isInstFetch();
13010626SCurtis.Dunham@arm.com    bool read = pkt->isRead();
13110626SCurtis.Dunham@arm.com    bool inv = pkt->isInvalidate();
13210623Smitch.hayenga@arm.com
13310623Smitch.hayenga@arm.com    if (pkt->req->isUncacheable()) return false;
13410623Smitch.hayenga@arm.com    if (fetch && !onInst) return false;
13510623Smitch.hayenga@arm.com    if (!fetch && !onData) return false;
13610623Smitch.hayenga@arm.com    if (!fetch && read && !onRead) return false;
13710623Smitch.hayenga@arm.com    if (!fetch && !read && !onWrite) return false;
13810626SCurtis.Dunham@arm.com    if (!fetch && !read && inv) return false;
13910883Sali.jafri@arm.com    if (pkt->cmd == MemCmd::CleanEvict) return false;
14010623Smitch.hayenga@arm.com
14110623Smitch.hayenga@arm.com    if (onMiss) {
14213751Sjavier.bueno@metempsy.com        return miss;
14310623Smitch.hayenga@arm.com    }
14410623Smitch.hayenga@arm.com
14510623Smitch.hayenga@arm.com    return true;
14610623Smitch.hayenga@arm.com}
14710623Smitch.hayenga@arm.com
14810623Smitch.hayenga@arm.combool
14910623Smitch.hayenga@arm.comBasePrefetcher::inCache(Addr addr, bool is_secure) const
1503861SN/A{
15113422Sodanrc@yahoo.com.br    return cache->inCache(addr, is_secure);
1523861SN/A}
1533861SN/A
15410623Smitch.hayenga@arm.combool
15510623Smitch.hayenga@arm.comBasePrefetcher::inMissQueue(Addr addr, bool is_secure) const
1563861SN/A{
15713422Sodanrc@yahoo.com.br    return cache->inMissQueue(addr, is_secure);
1583861SN/A}
1593861SN/A
1605875Ssteve.reinhardt@amd.combool
16113624Sjavier.bueno@metempsy.comBasePrefetcher::hasBeenPrefetched(Addr addr, bool is_secure) const
16213624Sjavier.bueno@metempsy.com{
16313624Sjavier.bueno@metempsy.com    return cache->hasBeenPrefetched(addr, is_secure);
16413624Sjavier.bueno@metempsy.com}
16513624Sjavier.bueno@metempsy.com
16613624Sjavier.bueno@metempsy.combool
16710466Sandreas.hansson@arm.comBasePrefetcher::samePage(Addr a, Addr b) const
1685875Ssteve.reinhardt@amd.com{
16910466Sandreas.hansson@arm.com    return roundDown(a, pageBytes) == roundDown(b, pageBytes);
1705875Ssteve.reinhardt@amd.com}
1718831Smrinmoy.ghosh@arm.com
17211438SRekai.GonzalezAlberquilla@arm.comAddr
17311438SRekai.GonzalezAlberquilla@arm.comBasePrefetcher::blockAddress(Addr a) const
17411438SRekai.GonzalezAlberquilla@arm.com{
17513434Sjavier.bueno@metempsy.com    return a & ~((Addr)blkSize-1);
17611438SRekai.GonzalezAlberquilla@arm.com}
1778831Smrinmoy.ghosh@arm.com
17811438SRekai.GonzalezAlberquilla@arm.comAddr
17911438SRekai.GonzalezAlberquilla@arm.comBasePrefetcher::blockIndex(Addr a) const
18011438SRekai.GonzalezAlberquilla@arm.com{
18111438SRekai.GonzalezAlberquilla@arm.com    return a >> lBlkSize;
18211438SRekai.GonzalezAlberquilla@arm.com}
18311438SRekai.GonzalezAlberquilla@arm.com
18411438SRekai.GonzalezAlberquilla@arm.comAddr
18511438SRekai.GonzalezAlberquilla@arm.comBasePrefetcher::pageAddress(Addr a) const
18611438SRekai.GonzalezAlberquilla@arm.com{
18711438SRekai.GonzalezAlberquilla@arm.com    return roundDown(a, pageBytes);
18811438SRekai.GonzalezAlberquilla@arm.com}
18911438SRekai.GonzalezAlberquilla@arm.com
19011438SRekai.GonzalezAlberquilla@arm.comAddr
19111438SRekai.GonzalezAlberquilla@arm.comBasePrefetcher::pageOffset(Addr a) const
19211438SRekai.GonzalezAlberquilla@arm.com{
19311438SRekai.GonzalezAlberquilla@arm.com    return a & (pageBytes - 1);
19411438SRekai.GonzalezAlberquilla@arm.com}
19511438SRekai.GonzalezAlberquilla@arm.com
19611438SRekai.GonzalezAlberquilla@arm.comAddr
19711438SRekai.GonzalezAlberquilla@arm.comBasePrefetcher::pageIthBlockAddress(Addr page, uint32_t blockIndex) const
19811438SRekai.GonzalezAlberquilla@arm.com{
19911438SRekai.GonzalezAlberquilla@arm.com    return page + (blockIndex << lBlkSize);
20011438SRekai.GonzalezAlberquilla@arm.com}
20113416Sjavier.bueno@metempsy.com
20213416Sjavier.bueno@metempsy.comvoid
20313751Sjavier.bueno@metempsy.comBasePrefetcher::probeNotify(const PacketPtr &pkt, bool miss)
20413416Sjavier.bueno@metempsy.com{
20513416Sjavier.bueno@metempsy.com    // Don't notify prefetcher on SWPrefetch, cache maintenance
20613416Sjavier.bueno@metempsy.com    // operations or for writes that we are coaslescing.
20713416Sjavier.bueno@metempsy.com    if (pkt->cmd.isSWPrefetch()) return;
20813416Sjavier.bueno@metempsy.com    if (pkt->req->isCacheMaintenance()) return;
20913416Sjavier.bueno@metempsy.com    if (pkt->isWrite() && cache != nullptr && cache->coalesce()) return;
21013751Sjavier.bueno@metempsy.com    if (!pkt->req->hasPaddr()) {
21113751Sjavier.bueno@metempsy.com        panic("Request must have a physical address");
21213751Sjavier.bueno@metempsy.com    }
21313551Sjavier.bueno@metempsy.com
21413624Sjavier.bueno@metempsy.com    if (hasBeenPrefetched(pkt->getAddr(), pkt->isSecure())) {
21513624Sjavier.bueno@metempsy.com        usefulPrefetches += 1;
21613624Sjavier.bueno@metempsy.com    }
21713624Sjavier.bueno@metempsy.com
21813551Sjavier.bueno@metempsy.com    // Verify this access type is observed by prefetcher
21913751Sjavier.bueno@metempsy.com    if (observeAccess(pkt, miss)) {
22013551Sjavier.bueno@metempsy.com        if (useVirtualAddresses && pkt->req->hasVaddr()) {
22113751Sjavier.bueno@metempsy.com            PrefetchInfo pfi(pkt, pkt->req->getVaddr(), miss);
22213551Sjavier.bueno@metempsy.com            notify(pkt, pfi);
22313751Sjavier.bueno@metempsy.com        } else if (!useVirtualAddresses) {
22413751Sjavier.bueno@metempsy.com            PrefetchInfo pfi(pkt, pkt->req->getPaddr(), miss);
22513551Sjavier.bueno@metempsy.com            notify(pkt, pfi);
22613551Sjavier.bueno@metempsy.com        }
22713551Sjavier.bueno@metempsy.com    }
22813416Sjavier.bueno@metempsy.com}
22913416Sjavier.bueno@metempsy.com
23013416Sjavier.bueno@metempsy.comvoid
23113416Sjavier.bueno@metempsy.comBasePrefetcher::regProbeListeners()
23213416Sjavier.bueno@metempsy.com{
23313416Sjavier.bueno@metempsy.com    /**
23413416Sjavier.bueno@metempsy.com     * If no probes were added by the configuration scripts, connect to the
23513416Sjavier.bueno@metempsy.com     * parent cache using the probe "Miss". Also connect to "Hit", if the
23613416Sjavier.bueno@metempsy.com     * cache is configured to prefetch on accesses.
23713416Sjavier.bueno@metempsy.com     */
23813416Sjavier.bueno@metempsy.com    if (listeners.empty() && cache != nullptr) {
23913416Sjavier.bueno@metempsy.com        ProbeManager *pm(cache->getProbeManager());
24013751Sjavier.bueno@metempsy.com        listeners.push_back(new PrefetchListener(*this, pm, "Miss", false,
24113751Sjavier.bueno@metempsy.com                                                true));
24213751Sjavier.bueno@metempsy.com        listeners.push_back(new PrefetchListener(*this, pm, "Fill", true,
24313751Sjavier.bueno@metempsy.com                                                 false));
24413416Sjavier.bueno@metempsy.com        if (prefetchOnAccess) {
24513751Sjavier.bueno@metempsy.com            listeners.push_back(new PrefetchListener(*this, pm, "Hit", false,
24613751Sjavier.bueno@metempsy.com                                                     false));
24713416Sjavier.bueno@metempsy.com        }
24813416Sjavier.bueno@metempsy.com    }
24913416Sjavier.bueno@metempsy.com}
25013416Sjavier.bueno@metempsy.com
25113416Sjavier.bueno@metempsy.comvoid
25213416Sjavier.bueno@metempsy.comBasePrefetcher::addEventProbe(SimObject *obj, const char *name)
25313416Sjavier.bueno@metempsy.com{
25413416Sjavier.bueno@metempsy.com    ProbeManager *pm(obj->getProbeManager());
25513416Sjavier.bueno@metempsy.com    listeners.push_back(new PrefetchListener(*this, pm, name));
25613416Sjavier.bueno@metempsy.com}
25714013Sjavier.bueno@metempsy.com
25814013Sjavier.bueno@metempsy.comvoid
25914013Sjavier.bueno@metempsy.comBasePrefetcher::addTLB(BaseTLB *t)
26014013Sjavier.bueno@metempsy.com{
26114013Sjavier.bueno@metempsy.com    fatal_if(tlb != nullptr, "Only one TLB can be registered");
26214013Sjavier.bueno@metempsy.com    tlb = t;
26314013Sjavier.bueno@metempsy.com}
264