2 * Copyright (c) 2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Ron Dreslinski
33 * Hardware Prefetcher Definition.
38 #include "arch/isa_traits.hh"
39 #include "base/trace.hh"
40 #include "config/the_isa.hh"
41 #include "mem/cache/base.hh"
42 #include "mem/cache/prefetch/base.hh"
43 #include "mem/request.hh"
45 BasePrefetcher::BasePrefetcher(const BaseCacheParams
*p
)
46 : size(p
->prefetcher_size
), pageStop(!p
->prefetch_past_page
),
47 serialSquash(p
->prefetch_serial_squash
),
48 onlyData(p
->prefetch_data_accesses_only
)
53 BasePrefetcher::setCache(BaseCache
*_cache
)
56 blkSize
= cache
->getBlockSize();
57 _name
= cache
->name() + "-pf";
61 BasePrefetcher::regStats(const std::string
&name
)
64 .name(name
+ ".prefetcher.num_hwpf_identified")
65 .desc("number of hwpf identified")
69 .name(name
+ ".prefetcher.num_hwpf_already_in_mshr")
70 .desc("number of hwpf that were already in mshr")
74 .name(name
+ ".prefetcher.num_hwpf_already_in_cache")
75 .desc("number of hwpf that were already in the cache")
79 .name(name
+ ".prefetcher.num_hwpf_already_in_prefetcher")
80 .desc("number of hwpf that were already in the prefetch queue")
84 .name(name
+ ".prefetcher.num_hwpf_evicted")
85 .desc("number of hwpf removed due to no buffer left")
89 .name(name
+ ".prefetcher.num_hwpf_removed_MSHR_hit")
90 .desc("number of hwpf removed because MSHR allocated")
94 .name(name
+ ".prefetcher.num_hwpf_issued")
95 .desc("number of hwpf issued")
99 .name(name
+ ".prefetcher.num_hwpf_span_page")
100 .desc("number of hwpf spanning a virtual page")
104 .name(name
+ ".prefetcher.num_hwpf_squashed_from_miss")
105 .desc("number of hwpf that got squashed due to a miss "
106 "aborting calculation time")
111 BasePrefetcher::inCache(Addr addr
)
113 if (cache
->inCache(addr
)) {
121 BasePrefetcher::inMissQueue(Addr addr
)
123 if (cache
->inMissQueue(addr
)) {
131 BasePrefetcher::getPacket()
133 DPRINTF(HWPrefetch
, "Requesting a hw_pf to issue\n");
136 DPRINTF(HWPrefetch
, "No HW_PF found\n");
141 bool keep_trying
= false;
147 DPRINTF(HWPrefetch
, "addr 0x%x in cache, skipping\n",
154 cache
->deassertMemSideBusRequest(BaseCache::Request_PF
);
156 return NULL
; // None left, all were in cache
159 } while (keep_trying
);
163 DPRINTF(HWPrefetch
, "returning 0x%x\n", pkt
->getAddr());
169 BasePrefetcher::notify(PacketPtr
&pkt
, Tick time
)
171 if (!pkt
->req
->isUncacheable() && !(pkt
->req
->isInstFetch() && onlyData
)) {
172 // Calculate the blk address
173 Addr blk_addr
= pkt
->getAddr() & ~(Addr
)(blkSize
-1);
175 // Check if miss is in pfq, if so remove it
176 std::list
<PacketPtr
>::iterator iter
= inPrefetch(blk_addr
);
177 if (iter
!= pf
.end()) {
178 DPRINTF(HWPrefetch
, "Saw a miss to a queued prefetch addr: "
179 "0x%x, removing it\n", blk_addr
);
185 cache
->deassertMemSideBusRequest(BaseCache::Request_PF
);
188 // Remove anything in queue with delay older than time
189 // since everything is inserted in time order, start from end
190 // and work until pf.empty() or time is earlier
191 // This is done to emulate Aborting the previous work on a new miss
192 // Needed for serial calculators like GHB
196 while (!pf
.empty() && ((*iter
)->time
>= time
)) {
198 DPRINTF(HWPrefetch
, "Squashing old prefetch addr: 0x%x\n",
206 cache
->deassertMemSideBusRequest(BaseCache::Request_PF
);
210 std::list
<Addr
> addresses
;
211 std::list
<Tick
> delays
;
212 calculatePrefetch(pkt
, addresses
, delays
);
214 std::list
<Addr
>::iterator addrIter
= addresses
.begin();
215 std::list
<Tick
>::iterator delayIter
= delays
.begin();
216 for (; addrIter
!= addresses
.end(); ++addrIter
, ++delayIter
) {
217 Addr addr
= *addrIter
;
221 DPRINTF(HWPrefetch
, "Found a pf candidate addr: 0x%x, "
222 "inserting into prefetch queue with delay %d time %d\n",
223 addr
, *delayIter
, time
);
225 // Check if it is already in the pf buffer
226 if (inPrefetch(addr
) != pf
.end()) {
228 DPRINTF(HWPrefetch
, "Prefetch addr already in pf buffer\n");
232 // create a prefetch memreq
233 Request
*prefetchReq
= new Request(*addrIter
, blkSize
, 0);
235 new Packet(prefetchReq
, MemCmd::HardPFReq
, Packet::Broadcast
);
236 prefetch
->allocate();
237 prefetch
->req
->setThreadContext(pkt
->req
->contextId(),
238 pkt
->req
->threadId());
240 prefetch
->time
= time
+ (*delayIter
); // @todo ADD LATENCY HERE
242 // We just remove the head if we are full
243 if (pf
.size() == size
) {
245 PacketPtr old_pkt
= *pf
.begin();
246 DPRINTF(HWPrefetch
, "Prefetch queue full, "
247 "removing oldest 0x%x\n", old_pkt
->getAddr());
253 pf
.push_back(prefetch
);
257 return pf
.empty() ? 0 : pf
.front()->time
;
260 std::list
<PacketPtr
>::iterator
261 BasePrefetcher::inPrefetch(Addr address
)
263 // Guaranteed to only be one match, we always check before inserting
264 std::list
<PacketPtr
>::iterator iter
;
265 for (iter
= pf
.begin(); iter
!= pf
.end(); iter
++) {
266 if (((*iter
)->getAddr() & ~(Addr
)(blkSize
-1)) == address
) {
274 BasePrefetcher::samePage(Addr a
, Addr b
)
276 return roundDown(a
, TheISA::VMPageSize
) == roundDown(b
, TheISA::VMPageSize
);