base.cc revision 6658
12810SN/A/*
22810SN/A * Copyright (c) 2005 The Regents of The University of Michigan
32810SN/A * All rights reserved.
42810SN/A *
52810SN/A * Redistribution and use in source and binary forms, with or without
62810SN/A * modification, are permitted provided that the following conditions are
72810SN/A * met: redistributions of source code must retain the above copyright
82810SN/A * notice, this list of conditions and the following disclaimer;
92810SN/A * redistributions in binary form must reproduce the above copyright
102810SN/A * notice, this list of conditions and the following disclaimer in the
112810SN/A * documentation and/or other materials provided with the distribution;
122810SN/A * neither the name of the copyright holders nor the names of its
132810SN/A * contributors may be used to endorse or promote products derived from
142810SN/A * this software without specific prior written permission.
152810SN/A *
162810SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172810SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182810SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192810SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202810SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212810SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222810SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232810SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242810SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252810SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262810SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272810SN/A *
282810SN/A * Authors: Ron Dreslinski
292810SN/A */
302810SN/A
312810SN/A/**
322810SN/A * @file
332810SN/A * Hardware Prefetcher Definition.
342810SN/A */
352810SN/A
366658Snate@binkert.org#include <list>
376658Snate@binkert.org
385875Ssteve.reinhardt@amd.com#include "arch/isa_traits.hh"
392810SN/A#include "base/trace.hh"
406658Snate@binkert.org#include "config/the_isa.hh"
415338Sstever@gmail.com#include "mem/cache/base.hh"
425338Sstever@gmail.com#include "mem/cache/prefetch/base.hh"
432814SN/A#include "mem/request.hh"
442810SN/A
455034SN/ABasePrefetcher::BasePrefetcher(const BaseCacheParams *p)
465034SN/A    : size(p->prefetcher_size), pageStop(!p->prefetch_past_page),
475034SN/A      serialSquash(p->prefetch_serial_squash),
485034SN/A      cacheCheckPush(p->prefetch_cache_check_push),
495875Ssteve.reinhardt@amd.com      onlyData(p->prefetch_data_accesses_only)
502810SN/A{
512810SN/A}
522810SN/A
532810SN/Avoid
542810SN/ABasePrefetcher::setCache(BaseCache *_cache)
552810SN/A{
562810SN/A    cache = _cache;
572810SN/A    blkSize = cache->getBlockSize();
585875Ssteve.reinhardt@amd.com    _name = cache->name() + "-pf";
592810SN/A}
602810SN/A
612810SN/Avoid
622810SN/ABasePrefetcher::regStats(const std::string &name)
632810SN/A{
642810SN/A    pfIdentified
652810SN/A        .name(name + ".prefetcher.num_hwpf_identified")
662810SN/A        .desc("number of hwpf identified")
672810SN/A        ;
682810SN/A
692810SN/A    pfMSHRHit
702810SN/A        .name(name + ".prefetcher.num_hwpf_already_in_mshr")
712810SN/A        .desc("number of hwpf that were already in mshr")
722810SN/A        ;
732810SN/A
742810SN/A    pfCacheHit
752810SN/A        .name(name + ".prefetcher.num_hwpf_already_in_cache")
762810SN/A        .desc("number of hwpf that were already in the cache")
772810SN/A        ;
782810SN/A
792810SN/A    pfBufferHit
802810SN/A        .name(name + ".prefetcher.num_hwpf_already_in_prefetcher")
812810SN/A        .desc("number of hwpf that were already in the prefetch queue")
822810SN/A        ;
832810SN/A
842810SN/A    pfRemovedFull
852810SN/A        .name(name + ".prefetcher.num_hwpf_evicted")
862810SN/A        .desc("number of hwpf removed due to no buffer left")
872810SN/A        ;
882810SN/A
892810SN/A    pfRemovedMSHR
902810SN/A        .name(name + ".prefetcher.num_hwpf_removed_MSHR_hit")
912810SN/A        .desc("number of hwpf removed because MSHR allocated")
922810SN/A        ;
932810SN/A
942810SN/A    pfIssued
952810SN/A        .name(name + ".prefetcher.num_hwpf_issued")
962810SN/A        .desc("number of hwpf issued")
972810SN/A        ;
982810SN/A
992810SN/A    pfSpanPage
1002810SN/A        .name(name + ".prefetcher.num_hwpf_span_page")
1012810SN/A        .desc("number of hwpf spanning a virtual page")
1022810SN/A        ;
1032810SN/A
1042810SN/A    pfSquashed
1052810SN/A        .name(name + ".prefetcher.num_hwpf_squashed_from_miss")
1065875Ssteve.reinhardt@amd.com        .desc("number of hwpf that got squashed due to a miss "
1075875Ssteve.reinhardt@amd.com              "aborting calculation time")
1082810SN/A        ;
1092810SN/A}
1102810SN/A
1113861SN/Ainline bool
1123861SN/ABasePrefetcher::inCache(Addr addr)
1133861SN/A{
1143861SN/A    if (cache->inCache(addr)) {
1153861SN/A        pfCacheHit++;
1163861SN/A        return true;
1173861SN/A    }
1183861SN/A    return false;
1193861SN/A}
1203861SN/A
1213861SN/Ainline bool
1223861SN/ABasePrefetcher::inMissQueue(Addr addr)
1233861SN/A{
1243861SN/A    if (cache->inMissQueue(addr)) {
1253861SN/A        pfMSHRHit++;
1263861SN/A        return true;
1273861SN/A    }
1283861SN/A    return false;
1293861SN/A}
1303861SN/A
1313349SN/APacketPtr
1322810SN/ABasePrefetcher::getPacket()
1332810SN/A{
1345875Ssteve.reinhardt@amd.com    DPRINTF(HWPrefetch, "Requesting a hw_pf to issue\n");
1352810SN/A
1362810SN/A    if (pf.empty()) {
1375875Ssteve.reinhardt@amd.com        DPRINTF(HWPrefetch, "No HW_PF found\n");
1382810SN/A        return NULL;
1392810SN/A    }
1402810SN/A
1413349SN/A    PacketPtr pkt;
1425875Ssteve.reinhardt@amd.com    bool keep_trying = false;
1432810SN/A    do {
1442810SN/A        pkt = *pf.begin();
1452810SN/A        pf.pop_front();
1462810SN/A        if (!cacheCheckPush) {
1475875Ssteve.reinhardt@amd.com            keep_trying = cache->inCache(pkt->getAddr());
1482810SN/A        }
1495875Ssteve.reinhardt@amd.com
1505875Ssteve.reinhardt@amd.com        if (keep_trying) {
1515875Ssteve.reinhardt@amd.com            DPRINTF(HWPrefetch, "addr 0x%x in cache, skipping\n",
1525875Ssteve.reinhardt@amd.com                    pkt->getAddr());
1535875Ssteve.reinhardt@amd.com            delete pkt->req;
1545875Ssteve.reinhardt@amd.com            delete pkt;
1555875Ssteve.reinhardt@amd.com        }
1565875Ssteve.reinhardt@amd.com
1572810SN/A        if (pf.empty()) {
1584628SN/A            cache->deassertMemSideBusRequest(BaseCache::Request_PF);
1595875Ssteve.reinhardt@amd.com            if (keep_trying) {
1605875Ssteve.reinhardt@amd.com                return NULL; // None left, all were in cache
1615875Ssteve.reinhardt@amd.com            }
1622810SN/A        }
1635875Ssteve.reinhardt@amd.com    } while (keep_trying);
1642810SN/A
1652810SN/A    pfIssued++;
1665875Ssteve.reinhardt@amd.com    assert(pkt != NULL);
1675875Ssteve.reinhardt@amd.com    DPRINTF(HWPrefetch, "returning 0x%x\n", pkt->getAddr());
1682810SN/A    return pkt;
1692810SN/A}
1702810SN/A
1715875Ssteve.reinhardt@amd.com
1725875Ssteve.reinhardt@amd.comTick
1735875Ssteve.reinhardt@amd.comBasePrefetcher::notify(PacketPtr &pkt, Tick time)
1742810SN/A{
1756105Ssteve.reinhardt@amd.com    if (!pkt->req->isUncacheable() && !(pkt->req->isInstFetch() && onlyData)) {
1765875Ssteve.reinhardt@amd.com        // Calculate the blk address
1775875Ssteve.reinhardt@amd.com        Addr blk_addr = pkt->getAddr() & ~(Addr)(blkSize-1);
1782810SN/A
1795875Ssteve.reinhardt@amd.com        // Check if miss is in pfq, if so remove it
1805875Ssteve.reinhardt@amd.com        std::list<PacketPtr>::iterator iter = inPrefetch(blk_addr);
1812810SN/A        if (iter != pf.end()) {
1825875Ssteve.reinhardt@amd.com            DPRINTF(HWPrefetch, "Saw a miss to a queued prefetch addr: "
1835875Ssteve.reinhardt@amd.com                    "0x%x, removing it\n", blk_addr);
1842810SN/A            pfRemovedMSHR++;
1855875Ssteve.reinhardt@amd.com            delete (*iter)->req;
1865875Ssteve.reinhardt@amd.com            delete (*iter);
1872810SN/A            pf.erase(iter);
1882810SN/A            if (pf.empty())
1894628SN/A                cache->deassertMemSideBusRequest(BaseCache::Request_PF);
1902810SN/A        }
1912810SN/A
1925875Ssteve.reinhardt@amd.com        // Remove anything in queue with delay older than time
1935875Ssteve.reinhardt@amd.com        // since everything is inserted in time order, start from end
1945875Ssteve.reinhardt@amd.com        // and work until pf.empty() or time is earlier
1955875Ssteve.reinhardt@amd.com        // This is done to emulate Aborting the previous work on a new miss
1965875Ssteve.reinhardt@amd.com        // Needed for serial calculators like GHB
1972810SN/A        if (serialSquash) {
1982810SN/A            iter = pf.end();
1992810SN/A            iter--;
2002810SN/A            while (!pf.empty() && ((*iter)->time >= time)) {
2012810SN/A                pfSquashed++;
2025875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Squashing old prefetch addr: 0x%x\n",
2035875Ssteve.reinhardt@amd.com                        (*iter)->getAddr());
2045875Ssteve.reinhardt@amd.com                delete (*iter)->req;
2055875Ssteve.reinhardt@amd.com                delete (*iter);
2065875Ssteve.reinhardt@amd.com                pf.erase(iter);
2072810SN/A                iter--;
2082810SN/A            }
2092810SN/A            if (pf.empty())
2104628SN/A                cache->deassertMemSideBusRequest(BaseCache::Request_PF);
2112810SN/A        }
2122810SN/A
2132810SN/A
2142810SN/A        std::list<Addr> addresses;
2152810SN/A        std::list<Tick> delays;
2162810SN/A        calculatePrefetch(pkt, addresses, delays);
2172810SN/A
2185875Ssteve.reinhardt@amd.com        std::list<Addr>::iterator addrIter = addresses.begin();
2195875Ssteve.reinhardt@amd.com        std::list<Tick>::iterator delayIter = delays.begin();
2205875Ssteve.reinhardt@amd.com        for (; addrIter != addresses.end(); ++addrIter, ++delayIter) {
2215875Ssteve.reinhardt@amd.com            Addr addr = *addrIter;
2225875Ssteve.reinhardt@amd.com
2232810SN/A            pfIdentified++;
2245875Ssteve.reinhardt@amd.com
2255875Ssteve.reinhardt@amd.com            DPRINTF(HWPrefetch, "Found a pf candidate addr: 0x%x, "
2265875Ssteve.reinhardt@amd.com                    "inserting into prefetch queue with delay %d time %d\n",
2275875Ssteve.reinhardt@amd.com                    addr, *delayIter, time);
2285875Ssteve.reinhardt@amd.com
2295875Ssteve.reinhardt@amd.com            // Check if it is already in the cache
2305875Ssteve.reinhardt@amd.com            if (cacheCheckPush && cache->inCache(addr)) {
2315875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Prefetch addr already in cache\n");
2325875Ssteve.reinhardt@amd.com                continue;
2335875Ssteve.reinhardt@amd.com            }
2345875Ssteve.reinhardt@amd.com
2355875Ssteve.reinhardt@amd.com            // Check if it is already in the miss_queue
2365875Ssteve.reinhardt@amd.com            if (cache->inMissQueue(addr)) {
2375875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Prefetch addr already in miss queue\n");
2385875Ssteve.reinhardt@amd.com                continue;
2395875Ssteve.reinhardt@amd.com            }
2405875Ssteve.reinhardt@amd.com
2415875Ssteve.reinhardt@amd.com            // Check if it is already in the pf buffer
2425875Ssteve.reinhardt@amd.com            if (inPrefetch(addr) != pf.end()) {
2435875Ssteve.reinhardt@amd.com                pfBufferHit++;
2445875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Prefetch addr already in pf buffer\n");
2455875Ssteve.reinhardt@amd.com                continue;
2465875Ssteve.reinhardt@amd.com            }
2475875Ssteve.reinhardt@amd.com
2485875Ssteve.reinhardt@amd.com            // create a prefetch memreq
2495875Ssteve.reinhardt@amd.com            Request *prefetchReq = new Request(*addrIter, blkSize, 0);
2505875Ssteve.reinhardt@amd.com            PacketPtr prefetch =
2515875Ssteve.reinhardt@amd.com                new Packet(prefetchReq, MemCmd::HardPFReq, Packet::Broadcast);
2522825SN/A            prefetch->allocate();
2535714Shsul@eecs.umich.edu            prefetch->req->setThreadContext(pkt->req->contextId(),
2545714Shsul@eecs.umich.edu                                            pkt->req->threadId());
2552814SN/A
2565875Ssteve.reinhardt@amd.com            prefetch->time = time + (*delayIter); // @todo ADD LATENCY HERE
2572810SN/A
2585875Ssteve.reinhardt@amd.com            // We just remove the head if we are full
2595875Ssteve.reinhardt@amd.com            if (pf.size() == size) {
2602810SN/A                pfRemovedFull++;
2615875Ssteve.reinhardt@amd.com                PacketPtr old_pkt = *pf.begin();
2625875Ssteve.reinhardt@amd.com                DPRINTF(HWPrefetch, "Prefetch queue full, "
2635875Ssteve.reinhardt@amd.com                        "removing oldest 0x%x\n", old_pkt->getAddr());
2645875Ssteve.reinhardt@amd.com                delete old_pkt->req;
2655875Ssteve.reinhardt@amd.com                delete old_pkt;
2662810SN/A                pf.pop_front();
2672810SN/A            }
2682810SN/A
2692810SN/A            pf.push_back(prefetch);
2702810SN/A        }
2712810SN/A    }
2725875Ssteve.reinhardt@amd.com
2735875Ssteve.reinhardt@amd.com    return pf.empty() ? 0 : pf.front()->time;
2742810SN/A}
2752810SN/A
2763349SN/Astd::list<PacketPtr>::iterator
2772810SN/ABasePrefetcher::inPrefetch(Addr address)
2782810SN/A{
2795875Ssteve.reinhardt@amd.com    // Guaranteed to only be one match, we always check before inserting
2803349SN/A    std::list<PacketPtr>::iterator iter;
2815875Ssteve.reinhardt@amd.com    for (iter = pf.begin(); iter != pf.end(); iter++) {
2822814SN/A        if (((*iter)->getAddr() & ~(Addr)(blkSize-1)) == address) {
2832810SN/A            return iter;
2842810SN/A        }
2852810SN/A    }
2862810SN/A    return pf.end();
2872810SN/A}
2882810SN/A
2895875Ssteve.reinhardt@amd.combool
2905875Ssteve.reinhardt@amd.comBasePrefetcher::samePage(Addr a, Addr b)
2915875Ssteve.reinhardt@amd.com{
2925875Ssteve.reinhardt@amd.com    return roundDown(a, TheISA::VMPageSize) == roundDown(b, TheISA::VMPageSize);
2935875Ssteve.reinhardt@amd.com}
294