2 * Copyright (c) 2002-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: Erik Hallnor
33 * Definitions of the Indirect Index Cache tagstore.
41 #include "base/intmath.hh"
42 #include "base/trace.hh"
43 #include "mem/cache/base.hh"
44 #include "mem/cache/tags/iic.hh"
45 #include "sim/core.hh"
49 /** Track the number of accesses to each cache set. */
52 IIC::IIC(IIC::Params
¶ms
) :
53 hashSets(params
.numSets
), blkSize(params
.blkSize
), assoc(params
.assoc
),
54 hitLatency(params
.hitLatency
), subSize(params
.subblockSize
),
55 numSub(blkSize
/subSize
),
56 trivialSize((floorLog2(params
.size
/subSize
)*numSub
)/8),
57 tagShift(floorLog2(blkSize
)), blkMask(blkSize
- 1),
58 subShift(floorLog2(subSize
)), subMask(numSub
- 1),
59 hashDelay(params
.hashDelay
),
60 numTags(hashSets
* assoc
+ params
.size
/blkSize
-1),
61 numSecondary(params
.size
/blkSize
),
63 primaryBound(hashSets
* assoc
)
66 if (blkSize
< 4 || !isPowerOf2(blkSize
)) {
67 fatal("Block size must be at least 4 and a power of 2");
69 if (hashSets
<= 0 || !isPowerOf2(hashSets
)) {
70 fatal("# of hashsets must be non-zero and a power of 2");
73 fatal("associativity must be greater than zero");
75 if (hitLatency
<= 0) {
76 fatal("access latency must be greater than zero");
78 if (numSub
*subSize
!= blkSize
) {
79 fatal("blocksize must be evenly divisible by subblock size");
83 freeSecond
= numSecondary
;
86 warmupBound
= params
.size
/blkSize
;
87 numBlocks
= params
.size
/subSize
;
89 // Replacement Policy Initialization
95 // allocate data reference counters
96 dataReferenceCount
= new int[numBlocks
];
97 memset(dataReferenceCount
, 0, numBlocks
*sizeof(int));
99 // Allocate storage for both internal data and block fast access data.
100 // We allocate it as one large chunk to reduce overhead and to make
102 unsigned data_index
= 0;
103 dataStore
= new uint8_t[(numBlocks
+ numTags
) * blkSize
];
104 dataBlks
= new uint8_t*[numBlocks
];
105 for (unsigned i
= 0; i
< numBlocks
; ++i
) {
106 dataBlks
[i
] = &dataStore
[data_index
];
108 data_index
+= subSize
;
111 assert(data_index
== numBlocks
* subSize
);
113 // allocate and init tag store
114 tagStore
= new IICTag
[numTags
];
116 unsigned blkIndex
= 0;
117 // allocate and init sets
118 sets
= new IICSet
[hashSets
];
119 for (unsigned i
= 0; i
< hashSets
; ++i
) {
120 sets
[i
].assoc
= assoc
;
121 sets
[i
].tags
= new IICTag
*[assoc
];
122 sets
[i
].chain_ptr
= tagNull
;
124 for (unsigned j
= 0; j
< assoc
; ++j
) {
125 IICTag
*tag
= &tagStore
[blkIndex
++];
126 tag
->chain_ptr
= tagNull
;
127 tag
->data_ptr
.resize(numSub
);
129 tag
->trivialData
= new uint8_t[trivialSize
];
131 sets
[i
].tags
[j
] = tag
;
133 tag
->data
= &dataStore
[data_index
];
134 data_index
+= blkSize
;
138 assert(blkIndex
== primaryBound
);
140 for (unsigned i
= primaryBound
; i
< tagNull
; i
++) {
141 tagStore
[i
].chain_ptr
= i
+1;
142 //setup data ptrs to subblocks
143 tagStore
[i
].data_ptr
.resize(numSub
);
144 tagStore
[i
].size
= blkSize
;
145 tagStore
[i
].trivialData
= new uint8_t[trivialSize
];
146 tagStore
[i
].numData
= 0;
148 tagStore
[i
].data
= &dataStore
[data_index
];
149 data_index
+= blkSize
;
151 freelist
= primaryBound
;
156 delete [] dataReferenceCount
;
162 /* register cache stats */
164 IIC::regStats(const string
&name
)
166 using namespace Stats
;
168 BaseTags::regStats(name
);
170 hitHashDepth
.init(0, 20, 1);
171 missHashDepth
.init(0, 20, 1);
172 setAccess
.init(0, hashSets
, 1);
174 /** IIC Statistics */
176 .name(name
+ ".hit_hash_depth_dist")
177 .desc("Dist. of Hash lookup depths")
182 .name(name
+ ".miss_hash_depth_dist")
183 .desc("Dist. of Hash lookup depths")
187 repl
->regStats(name
);
191 .name(name
+ ".set_access_dist")
192 .desc("Dist. of Accesses across sets")
197 .name(name
+ ".miss_depth_total")
198 .desc("Total of miss depths")
202 .name(name
+ ".hash_miss")
203 .desc("Total of misses in hash table")
207 .name(name
+ ".hit_depth_total")
208 .desc("Total of hit depths")
212 .name(name
+ ".hash_hit")
213 .desc("Total of hites in hash table")
219 IIC::accessBlock(Addr addr
, int &lat
, int context_src
)
221 Addr tag
= extractTag(addr
);
222 unsigned set
= hash(addr
);
225 unsigned long chain_ptr
= tagNull
;
228 setAccess
.sample(set
);
230 IICTag
*tag_ptr
= sets
[set
].findTag(tag
, chain_ptr
);
232 if (tag_ptr
== NULL
&& chain_ptr
!= tagNull
) {
234 tag_ptr
= secondaryChain(tag
, chain_ptr
, &secondary_depth
);
235 set_lat
+= secondary_depth
;
236 // set depth for statistics fix this later!!! egh
237 sets
[set
].depth
= set_lat
;
239 if (tag_ptr
!= NULL
) {
240 /* need to move tag into primary table */
241 // need to preserve chain: fix this egh
242 sets
[set
].tags
[assoc
-1]->chain_ptr
= tag_ptr
->chain_ptr
;
243 tagSwap(tag_ptr
- tagStore
, sets
[set
].tags
[assoc
-1] - tagStore
);
244 tag_ptr
= sets
[set
].findTag(tag
, chain_ptr
);
245 assert(tag_ptr
!=NULL
);
249 set_lat
= set_lat
* hashDelay
+ hitLatency
;
250 if (tag_ptr
!= NULL
) {
251 // IIC replacement: if this is not the first element of
253 sets
[set
].moveToHead(tag_ptr
);
255 hitHashDepth
.sample(sets
[set
].depth
);
257 hitDepthTotal
+= sets
[set
].depth
;
258 tag_ptr
->status
|= BlkReferenced
;
260 if (tag_ptr
->whenReady
> curTick
&& tag_ptr
->whenReady
- curTick
> set_lat
) {
261 lat
= tag_ptr
->whenReady
- curTick
;
264 tag_ptr
->refCount
+= 1;
267 // fall through: cache block not found, not a hit...
268 missHashDepth
.sample(sets
[set
].depth
);
270 missDepthTotal
+= sets
[set
].depth
;
278 IIC::findBlock(Addr addr
) const
280 Addr tag
= extractTag(addr
);
281 unsigned set
= hash(addr
);
283 unsigned long chain_ptr
= tagNull
;
285 IICTag
*tag_ptr
= sets
[set
].findTag(tag
, chain_ptr
);
286 if (tag_ptr
== NULL
&& chain_ptr
!= tagNull
) {
288 tag_ptr
= secondaryChain(tag
, chain_ptr
, &secondary_depth
);
295 IIC::findVictim(Addr addr
, PacketList
&writebacks
)
297 DPRINTF(IIC
, "Finding Replacement for %x\n", addr
);
298 unsigned set
= hash(addr
);
300 unsigned long *tmp_data
= new unsigned long[numSub
];
302 // Get a enough subblocks for a full cache line
303 for (unsigned i
= 0; i
< numSub
; ++i
){
304 tmp_data
[i
] = getFreeDataBlock(writebacks
);
305 assert(dataReferenceCount
[tmp_data
[i
]]==0);
308 tag_ptr
= getFreeTag(set
, writebacks
);
311 for (unsigned i
= 0; i
< numSub
; ++i
) {
312 tag_ptr
->data_ptr
[i
] = tmp_data
[i
];
313 dataReferenceCount
[tag_ptr
->data_ptr
[i
]]++;
315 tag_ptr
->numData
= numSub
;
316 assert(tag_ptr
- tagStore
< primaryBound
); // make sure it is in primary
317 tag_ptr
->chain_ptr
= tagNull
;
318 sets
[set
].moveToHead(tag_ptr
);
321 list
<unsigned long> tag_indexes
;
322 repl
->doAdvance(tag_indexes
);
324 while (!tag_indexes.empty()) {
325 if (!tagStore[tag_indexes.front()].isCompressed()) {
326 compress_blocks.push_back(&tagStore[tag_indexes.front()]);
328 tag_indexes.pop_front();
332 tag_ptr
->re
= (void*)repl
->add(tag_ptr
-tagStore
);
338 IIC::insertBlock(Addr addr
, BlkType
* blk
, int context_src
)
343 IIC::freeReplacementBlock(PacketList
& writebacks
)
346 unsigned long data_ptr
;
347 /* consult replacement policy */
348 tag_ptr
= &tagStore
[repl
->getRepl()];
349 assert(tag_ptr
->isValid());
351 DPRINTF(Cache
, "Replacing %x in IIC: %s\n",
352 regenerateBlkAddr(tag_ptr
->tag
,0),
353 tag_ptr
->isDirty() ? "writeback" : "clean");
354 /* write back replaced block data */
355 if (tag_ptr
&& (tag_ptr
->isValid())) {
357 totalRefs
+= tag_ptr
->refCount
;
359 tag_ptr
->refCount
= 0;
361 if (tag_ptr
->isDirty()) {
362 /* PacketPtr writeback =
363 buildWritebackReq(regenerateBlkAddr(tag_ptr->tag, 0),
364 tag_ptr->req->asid, tag_ptr->xc, blkSize,
368 Request
*writebackReq
= new Request(regenerateBlkAddr(tag_ptr
->tag
, 0),
370 PacketPtr writeback
= new Packet(writebackReq
, MemCmd::Writeback
,
372 writeback
->allocate();
373 memcpy(writeback
->getPtr
<uint8_t>(), tag_ptr
->data
, blkSize
);
375 writebacks
.push_back(writeback
);
379 // free the data blocks
380 for (int i
= 0; i
< tag_ptr
->numData
; ++i
) {
381 data_ptr
= tag_ptr
->data_ptr
[i
];
382 assert(dataReferenceCount
[data_ptr
]>0);
383 if (--dataReferenceCount
[data_ptr
] == 0) {
384 freeDataBlock(data_ptr
);
391 IIC::getFreeDataBlock(PacketList
& writebacks
)
393 struct IICTag
*tag_ptr
;
394 unsigned long data_ptr
;
397 /* find data block */
398 while (blkFreelist
.empty()) {
399 freeReplacementBlock(writebacks
);
402 data_ptr
= blkFreelist
.front();
403 blkFreelist
.pop_front();
404 DPRINTF(IICMore
,"Found free data at %d\n",data_ptr
);
411 IIC::getFreeTag(int set
, PacketList
& writebacks
)
413 unsigned long tag_index
;
416 tag_ptr
= sets
[set
].findFree();
417 // if no free in primary, and secondary exists
418 if (!tag_ptr
&& numSecondary
) {
419 // need to spill a tag into secondary storage
420 while (freelist
== tagNull
) {
421 // get replacements until one is in secondary
422 freeReplacementBlock(writebacks
);
425 tag_index
= freelist
;
426 freelist
= tagStore
[freelist
].chain_ptr
;
429 assert(tag_index
!= tagNull
);
430 tagSwap(tag_index
, sets
[set
].tags
[assoc
-1] - tagStore
);
431 tagStore
[tag_index
].chain_ptr
= sets
[set
].chain_ptr
;
432 sets
[set
].chain_ptr
= tag_index
;
434 tag_ptr
= sets
[set
].tags
[assoc
-1];
436 DPRINTF(IICMore
,"Found free tag at %d\n",tag_ptr
- tagStore
);
438 if (!warmedUp
&& tagsInUse
.value() >= warmupBound
) {
440 warmupCycle
= curTick
;
447 IIC::freeTag(IICTag
*tag_ptr
)
449 unsigned long tag_index
, tmp_index
;
452 // we have a tag to clear
453 DPRINTF(IICMore
,"Freeing Tag for %x\n",
454 regenerateBlkAddr(tag_ptr
->tag
,0));
457 tag_ptr
->numData
= 0;
459 tag_index
= tag_ptr
- tagStore
;
460 if (tag_index
>= primaryBound
) {
461 // tag_ptr points to secondary store
462 assert(tag_index
< tagNull
); // remove this?? egh
463 if (tag_ptr
->chain_ptr
== tagNull
) {
464 // need to fix chain list
465 unsigned tmp_set
= hash(tag_ptr
->tag
<< tagShift
);
466 if (sets
[tmp_set
].chain_ptr
== tag_index
) {
467 sets
[tmp_set
].chain_ptr
= tagNull
;
469 tmp_index
= sets
[tmp_set
].chain_ptr
;
470 while (tmp_index
!= tagNull
471 && tagStore
[tmp_index
].chain_ptr
!= tag_index
) {
472 tmp_index
= tagStore
[tmp_index
].chain_ptr
;
474 assert(tmp_index
!= tagNull
);
475 tagStore
[tmp_index
].chain_ptr
= tagNull
;
477 tag_ptr
->chain_ptr
= freelist
;
478 freelist
= tag_index
;
481 // copy next chained entry to this tag location
482 tmp_index
= tag_ptr
->chain_ptr
;
483 tagSwap(tmp_index
, tag_index
);
484 tagStore
[tmp_index
].chain_ptr
= freelist
;
485 freelist
= tmp_index
;
489 // tag_ptr in primary hash table
490 assert(tag_index
< primaryBound
);
492 unsigned tmp_set
= hash(tag_ptr
->tag
<< tagShift
);
493 if (sets
[tmp_set
].chain_ptr
!= tagNull
) { // collapse chain
494 tmp_index
= sets
[tmp_set
].chain_ptr
;
495 tagSwap(tag_index
, tmp_index
);
496 tagStore
[tmp_index
].chain_ptr
= freelist
;
497 freelist
= tmp_index
;
499 sets
[tmp_set
].chain_ptr
= tag_ptr
->chain_ptr
;
500 sets
[tmp_set
].moveToTail(tag_ptr
);
507 IIC::freeDataBlock(unsigned long data_ptr
)
509 assert(dataReferenceCount
[data_ptr
] == 0);
510 DPRINTF(IICMore
, "Freeing data at %d\n", data_ptr
);
511 blkFreelist
.push_front(data_ptr
);
514 /** Use a simple modulo hash. */
515 #define SIMPLE_HASH 0
518 IIC::hash(Addr addr
) const {
520 return extractTag(addr
) % iic_hash_size
;
522 Addr tag
, mask
, x
, y
;
523 tag
= extractTag(addr
);
524 mask
= hashSets
-1; /* assumes iic_hash_size is a power of 2 */
526 y
= (tag
>> (int)(::log((double)hashSets
)/::log((double)2))) & mask
;
527 assert (x
< hashSets
&& y
< hashSets
);
534 IICSet::moveToHead(IICTag
*tag
)
539 // write 'next' block into blks[i], moving up from MRU toward LRU
540 // until we overwrite the block we moved to head.
542 // start by setting up to write 'blk' into blks[0]
548 // swap blks[i] and next
549 IICTag
*tmp
= tags
[i
];
553 } while (next
!= tag
);
557 IICSet::moveToTail(IICTag
*tag
)
559 if (tags
[assoc
-1] == tag
)
562 // write 'next' block into blks[i], moving up from MRU toward LRU
563 // until we overwrite the block we moved to head.
565 // start by setting up to write 'blk' into blks[0]
571 // swap blks[i] and next
572 IICTag
*tmp
= tags
[i
];
576 } while (next
!= tag
);
580 IIC::tagSwap(unsigned long index1
, unsigned long index2
)
582 DPRINTF(IIC
,"Swapping tag[%d]=%x for tag[%d]=%x\n",index1
,
583 tagStore
[index1
].tag
<<tagShift
, index2
,
584 tagStore
[index2
].tag
<<tagShift
);
586 tmp_tag
= tagStore
[index1
];
587 tagStore
[index1
] = tagStore
[index2
];
588 tagStore
[index2
] = tmp_tag
;
589 if (tagStore
[index1
].isValid())
590 repl
->fixTag(tagStore
[index1
].re
, index2
, index1
);
591 if (tagStore
[index2
].isValid())
592 repl
->fixTag(tagStore
[index2
].re
, index1
, index2
);
597 IIC::secondaryChain(Addr tag
, unsigned long chain_ptr
,
601 while (chain_ptr
!= tagNull
) {
602 DPRINTF(IIC
,"Searching secondary at %d for %x\n", chain_ptr
,
604 if (tagStore
[chain_ptr
].tag
== tag
&&
605 (tagStore
[chain_ptr
].isValid())) {
607 return &tagStore
[chain_ptr
];
610 chain_ptr
= tagStore
[chain_ptr
].chain_ptr
;
617 IIC::invalidateBlk(IIC::BlkType
*tag_ptr
)
620 for (int i
= 0; i
< tag_ptr
->numData
; ++i
) {
621 dataReferenceCount
[tag_ptr
->data_ptr
[i
]]--;
622 if (dataReferenceCount
[tag_ptr
->data_ptr
[i
]] == 0) {
623 freeDataBlock(tag_ptr
->data_ptr
[i
]);
626 repl
->removeEntry(tag_ptr
->re
);
634 for (int i
= 0; i
< numTags
; i
++){
635 tagStore
[i
].clearLoadLocks();
642 for (unsigned i
= 0; i
< numTags
; ++i
) {
643 if (tagStore
[i
].isValid()) {
644 totalRefs
+= tagStore
[i
].refCount
;