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.
42 #include "mem/cache/base_cache.hh"
43 #include "mem/cache/tags/iic.hh"
44 #include "base/intmath.hh"
45 #include "sim/root.hh" // for curTick
47 #include "base/trace.hh" // for DPRINTF
52 /** Track the number of accesses to each cache set. */
55 IIC::IIC(IIC::Params
¶ms
) :
56 hashSets(params
.numSets
), blkSize(params
.blkSize
), assoc(params
.assoc
),
57 hitLatency(params
.hitLatency
), subSize(params
.subblockSize
),
58 numSub(blkSize
/subSize
),
59 trivialSize((floorLog2(params
.size
/subSize
)*numSub
)/8),
60 tagShift(floorLog2(blkSize
)), blkMask(blkSize
- 1),
61 subShift(floorLog2(subSize
)), subMask(numSub
- 1),
62 hashDelay(params
.hashDelay
),
63 numBlocks(params
.size
/subSize
),
64 numTags(hashSets
* assoc
+ params
.size
/blkSize
-1),
65 numSecondary(params
.size
/blkSize
),
67 primaryBound(hashSets
* assoc
)
72 if (blkSize
< 4 || !isPowerOf2(blkSize
)) {
73 fatal("Block size must be at least 4 and a power of 2");
75 if (hashSets
<= 0 || !isPowerOf2(hashSets
)) {
76 fatal("# of hashsets must be non-zero and a power of 2");
79 fatal("associativity must be greater than zero");
81 if (hitLatency
<= 0) {
82 fatal("access latency must be greater than zero");
84 if (numSub
*subSize
!= blkSize
) {
85 fatal("blocksize must be evenly divisible by subblock size");
89 freeSecond
= numSecondary
;
92 warmupBound
= params
.size
/blkSize
;
94 // Replacement Policy Initialization
100 // allocate data reference counters
101 dataReferenceCount
= new int[numBlocks
];
102 memset(dataReferenceCount
, 0, numBlocks
*sizeof(int));
104 // Allocate storage for both internal data and block fast access data.
105 // We allocate it as one large chunk to reduce overhead and to make
108 dataStore
= new uint8_t[(numBlocks
+ numTags
) * blkSize
];
109 dataBlks
= new uint8_t*[numBlocks
];
110 for (i
= 0; i
< numBlocks
; ++i
) {
111 dataBlks
[i
] = &dataStore
[data_index
];
113 data_index
+= subSize
;
116 assert(data_index
== numBlocks
* subSize
);
118 // allocate and init tag store
119 tagStore
= new IICTag
[numTags
];
122 // allocate and init sets
123 sets
= new IICSet
[hashSets
];
124 for (i
= 0; i
< hashSets
; ++i
) {
125 sets
[i
].assoc
= assoc
;
126 sets
[i
].tags
= new IICTag
*[assoc
];
127 sets
[i
].chain_ptr
= tagNull
;
129 for (int j
= 0; j
< assoc
; ++j
) {
130 IICTag
*tag
= &tagStore
[blkIndex
++];
131 tag
->chain_ptr
= tagNull
;
132 tag
->data_ptr
.resize(numSub
);
134 tag
->trivialData
= new uint8_t[trivialSize
];
136 sets
[i
].tags
[j
] = tag
;
138 tag
->data
= &dataStore
[data_index
];
139 data_index
+= blkSize
;
143 assert(blkIndex
== primaryBound
);
145 for (i
= primaryBound
; i
< tagNull
; i
++) {
146 tagStore
[i
].chain_ptr
= i
+1;
147 //setup data ptrs to subblocks
148 tagStore
[i
].data_ptr
.resize(numSub
);
149 tagStore
[i
].size
= blkSize
;
150 tagStore
[i
].trivialData
= new uint8_t[trivialSize
];
151 tagStore
[i
].numData
= 0;
153 tagStore
[i
].data
= &dataStore
[data_index
];
154 data_index
+= blkSize
;
156 freelist
= primaryBound
;
161 delete [] dataReferenceCount
;
167 /* register cache stats */
169 IIC::regStats(const string
&name
)
171 using namespace Stats
;
173 BaseTags::regStats(name
);
175 hitHashDepth
.init(0, 20, 1);
176 missHashDepth
.init(0, 20, 1);
177 setAccess
.init(0, hashSets
, 1);
179 /** IIC Statistics */
181 .name(name
+ ".hit_hash_depth_dist")
182 .desc("Dist. of Hash lookup depths")
187 .name(name
+ ".miss_hash_depth_dist")
188 .desc("Dist. of Hash lookup depths")
192 repl
->regStats(name
);
196 .name(name
+ ".set_access_dist")
197 .desc("Dist. of Accesses across sets")
202 .name(name
+ ".miss_depth_total")
203 .desc("Total of miss depths")
207 .name(name
+ ".hash_miss")
208 .desc("Total of misses in hash table")
212 .name(name
+ ".hit_depth_total")
213 .desc("Total of hit depths")
217 .name(name
+ ".hash_hit")
218 .desc("Total of hites in hash table")
222 // probe cache for presence of given block.
224 IIC::probe(Addr addr
) const
226 return (findBlock(addr
) != NULL
);
230 IIC::findBlock(Addr addr
, int &lat
)
232 Addr tag
= extractTag(addr
);
233 unsigned set
= hash(addr
);
236 unsigned long chain_ptr
;
239 setAccess
.sample(set
);
241 IICTag
*tag_ptr
= sets
[set
].findTag(tag
, chain_ptr
);
243 if (tag_ptr
== NULL
&& chain_ptr
!= tagNull
) {
245 tag_ptr
= secondaryChain(tag
, chain_ptr
, &secondary_depth
);
246 set_lat
+= secondary_depth
;
247 // set depth for statistics fix this later!!! egh
248 sets
[set
].depth
= set_lat
;
250 if (tag_ptr
!= NULL
) {
251 /* need to move tag into primary table */
252 // need to preserve chain: fix this egh
253 sets
[set
].tags
[assoc
-1]->chain_ptr
= tag_ptr
->chain_ptr
;
254 tagSwap(tag_ptr
- tagStore
, sets
[set
].tags
[assoc
-1] - tagStore
);
255 tag_ptr
= sets
[set
].findTag(tag
, chain_ptr
);
256 assert(tag_ptr
!=NULL
);
260 set_lat
= set_lat
* hashDelay
+ hitLatency
;
261 if (tag_ptr
!= NULL
) {
262 // IIC replacement: if this is not the first element of
264 sets
[set
].moveToHead(tag_ptr
);
266 hitHashDepth
.sample(sets
[set
].depth
);
268 hitDepthTotal
+= sets
[set
].depth
;
269 tag_ptr
->status
|= BlkReferenced
;
271 if (tag_ptr
->whenReady
> curTick
&& tag_ptr
->whenReady
- curTick
> set_lat
) {
272 lat
= tag_ptr
->whenReady
- curTick
;
275 tag_ptr
->refCount
+= 1;
278 // fall through: cache block not found, not a hit...
279 missHashDepth
.sample(sets
[set
].depth
);
281 missDepthTotal
+= sets
[set
].depth
;
289 IIC::findBlock(Addr addr
) const
291 Addr tag
= extractTag(addr
);
292 unsigned set
= hash(addr
);
294 unsigned long chain_ptr
;
296 IICTag
*tag_ptr
= sets
[set
].findTag(tag
, chain_ptr
);
297 if (tag_ptr
== NULL
&& chain_ptr
!= tagNull
) {
299 tag_ptr
= secondaryChain(tag
, chain_ptr
, &secondary_depth
);
306 IIC::findReplacement(PacketPtr
&pkt
, PacketList
&writebacks
,
307 BlkList
&compress_blocks
)
309 DPRINTF(IIC
, "Finding Replacement for %x\n", pkt
->getAddr());
310 unsigned set
= hash(pkt
->getAddr());
312 unsigned long *tmp_data
= new unsigned long[numSub
];
314 // Get a enough subblocks for a full cache line
315 for (int i
= 0; i
< numSub
; ++i
){
316 tmp_data
[i
] = getFreeDataBlock(writebacks
);
317 assert(dataReferenceCount
[tmp_data
[i
]]==0);
320 tag_ptr
= getFreeTag(set
, writebacks
);
323 for (int i
=0; i
< numSub
; ++i
) {
324 tag_ptr
->data_ptr
[i
] = tmp_data
[i
];
325 dataReferenceCount
[tag_ptr
->data_ptr
[i
]]++;
327 tag_ptr
->numData
= numSub
;
328 assert(tag_ptr
- tagStore
< primaryBound
); // make sure it is in primary
329 tag_ptr
->chain_ptr
= tagNull
;
330 sets
[set
].moveToHead(tag_ptr
);
333 list
<unsigned long> tag_indexes
;
334 repl
->doAdvance(tag_indexes
);
335 while (!tag_indexes
.empty()) {
336 if (!tagStore
[tag_indexes
.front()].isCompressed()) {
337 compress_blocks
.push_back(&tagStore
[tag_indexes
.front()]);
339 tag_indexes
.pop_front();
342 tag_ptr
->re
= (void*)repl
->add(tag_ptr
-tagStore
);
348 IIC::freeReplacementBlock(PacketList
& writebacks
)
351 unsigned long data_ptr
;
352 /* consult replacement policy */
353 tag_ptr
= &tagStore
[repl
->getRepl()];
354 assert(tag_ptr
->isValid());
356 DPRINTF(Cache
, "Replacing %x in IIC: %s\n",
357 regenerateBlkAddr(tag_ptr
->tag
,0),
358 tag_ptr
->isModified() ? "writeback" : "clean");
359 /* write back replaced block data */
360 if (tag_ptr
&& (tag_ptr
->isValid())) {
362 totalRefs
+= tag_ptr
->refCount
;
364 tag_ptr
->refCount
= 0;
366 if (tag_ptr
->isModified()) {
367 /* PacketPtr writeback =
368 buildWritebackReq(regenerateBlkAddr(tag_ptr->tag, 0),
369 tag_ptr->req->asid, tag_ptr->xc, blkSize,
373 Request
*writebackReq
= new Request(regenerateBlkAddr(tag_ptr
->tag
, 0),
375 PacketPtr writeback
= new Packet(writebackReq
, MemCmd::Writeback
,
377 writeback
->allocate();
378 memcpy(writeback
->getPtr
<uint8_t>(), tag_ptr
->data
, blkSize
);
380 writebacks
.push_back(writeback
);
384 // free the data blocks
385 for (int i
= 0; i
< tag_ptr
->numData
; ++i
) {
386 data_ptr
= tag_ptr
->data_ptr
[i
];
387 assert(dataReferenceCount
[data_ptr
]>0);
388 if (--dataReferenceCount
[data_ptr
] == 0) {
389 freeDataBlock(data_ptr
);
396 IIC::getFreeDataBlock(PacketList
& writebacks
)
398 struct IICTag
*tag_ptr
;
399 unsigned long data_ptr
;
402 /* find data block */
403 while (blkFreelist
.empty()) {
404 freeReplacementBlock(writebacks
);
407 data_ptr
= blkFreelist
.front();
408 blkFreelist
.pop_front();
409 DPRINTF(IICMore
,"Found free data at %d\n",data_ptr
);
416 IIC::getFreeTag(int set
, PacketList
& writebacks
)
418 unsigned long tag_index
;
421 tag_ptr
= sets
[set
].findFree();
422 // if no free in primary, and secondary exists
423 if (!tag_ptr
&& numSecondary
) {
424 // need to spill a tag into secondary storage
425 while (freelist
== tagNull
) {
426 // get replacements until one is in secondary
427 freeReplacementBlock(writebacks
);
430 tag_index
= freelist
;
431 freelist
= tagStore
[freelist
].chain_ptr
;
434 assert(tag_index
!= tagNull
);
435 tagSwap(tag_index
, sets
[set
].tags
[assoc
-1] - tagStore
);
436 tagStore
[tag_index
].chain_ptr
= sets
[set
].chain_ptr
;
437 sets
[set
].chain_ptr
= tag_index
;
439 tag_ptr
= sets
[set
].tags
[assoc
-1];
441 DPRINTF(IICMore
,"Found free tag at %d\n",tag_ptr
- tagStore
);
443 if (!warmedUp
&& tagsInUse
.value() >= warmupBound
) {
445 warmupCycle
= curTick
;
452 IIC::freeTag(IICTag
*tag_ptr
)
454 unsigned long tag_index
, tmp_index
;
457 // we have a tag to clear
458 DPRINTF(IICMore
,"Freeing Tag for %x\n",
459 regenerateBlkAddr(tag_ptr
->tag
,0));
462 tag_ptr
->numData
= 0;
464 tag_index
= tag_ptr
- tagStore
;
465 if (tag_index
>= primaryBound
) {
466 // tag_ptr points to secondary store
467 assert(tag_index
< tagNull
); // remove this?? egh
468 if (tag_ptr
->chain_ptr
== tagNull
) {
469 // need to fix chain list
470 unsigned tmp_set
= hash(tag_ptr
->tag
<< tagShift
);
471 if (sets
[tmp_set
].chain_ptr
== tag_index
) {
472 sets
[tmp_set
].chain_ptr
= tagNull
;
474 tmp_index
= sets
[tmp_set
].chain_ptr
;
475 while (tmp_index
!= tagNull
476 && tagStore
[tmp_index
].chain_ptr
!= tag_index
) {
477 tmp_index
= tagStore
[tmp_index
].chain_ptr
;
479 assert(tmp_index
!= tagNull
);
480 tagStore
[tmp_index
].chain_ptr
= tagNull
;
482 tag_ptr
->chain_ptr
= freelist
;
483 freelist
= tag_index
;
486 // copy next chained entry to this tag location
487 tmp_index
= tag_ptr
->chain_ptr
;
488 tagSwap(tmp_index
, tag_index
);
489 tagStore
[tmp_index
].chain_ptr
= freelist
;
490 freelist
= tmp_index
;
494 // tag_ptr in primary hash table
495 assert(tag_index
< primaryBound
);
497 unsigned tmp_set
= hash(tag_ptr
->tag
<< tagShift
);
498 if (sets
[tmp_set
].chain_ptr
!= tagNull
) { // collapse chain
499 tmp_index
= sets
[tmp_set
].chain_ptr
;
500 tagSwap(tag_index
, tmp_index
);
501 tagStore
[tmp_index
].chain_ptr
= freelist
;
502 freelist
= tmp_index
;
504 sets
[tmp_set
].chain_ptr
= tag_ptr
->chain_ptr
;
505 sets
[tmp_set
].moveToTail(tag_ptr
);
512 IIC::freeDataBlock(unsigned long data_ptr
)
514 assert(dataReferenceCount
[data_ptr
] == 0);
515 DPRINTF(IICMore
, "Freeing data at %d\n", data_ptr
);
516 blkFreelist
.push_front(data_ptr
);
519 /** Use a simple modulo hash. */
520 #define SIMPLE_HASH 0
523 IIC::hash(Addr addr
) const {
525 return extractTag(addr
) % iic_hash_size
;
527 Addr tag
, mask
, x
, y
;
528 tag
= extractTag(addr
);
529 mask
= hashSets
-1; /* assumes iic_hash_size is a power of 2 */
531 y
= (tag
>> (int)(::log((double)hashSets
)/::log((double)2))) & mask
;
532 assert (x
< hashSets
&& y
< hashSets
);
539 IICSet::moveToHead(IICTag
*tag
)
544 // write 'next' block into blks[i], moving up from MRU toward LRU
545 // until we overwrite the block we moved to head.
547 // start by setting up to write 'blk' into blks[0]
553 // swap blks[i] and next
554 IICTag
*tmp
= tags
[i
];
558 } while (next
!= tag
);
562 IICSet::moveToTail(IICTag
*tag
)
564 if (tags
[assoc
-1] == tag
)
567 // write 'next' block into blks[i], moving up from MRU toward LRU
568 // until we overwrite the block we moved to head.
570 // start by setting up to write 'blk' into blks[0]
576 // swap blks[i] and next
577 IICTag
*tmp
= tags
[i
];
581 } while (next
!= tag
);
585 IIC::tagSwap(unsigned long index1
, unsigned long index2
)
587 DPRINTF(IIC
,"Swapping tag[%d]=%x for tag[%d]=%x\n",index1
,
588 tagStore
[index1
].tag
<<tagShift
, index2
,
589 tagStore
[index2
].tag
<<tagShift
);
591 tmp_tag
= tagStore
[index1
];
592 tagStore
[index1
] = tagStore
[index2
];
593 tagStore
[index2
] = tmp_tag
;
594 if (tagStore
[index1
].isValid())
595 repl
->fixTag(tagStore
[index1
].re
, index2
, index1
);
596 if (tagStore
[index2
].isValid())
597 repl
->fixTag(tagStore
[index2
].re
, index1
, index2
);
602 IIC::secondaryChain(Addr tag
, unsigned long chain_ptr
,
606 while (chain_ptr
!= tagNull
) {
607 DPRINTF(IIC
,"Searching secondary at %d for %x\n", chain_ptr
,
609 if (tagStore
[chain_ptr
].tag
== tag
&&
610 (tagStore
[chain_ptr
].isValid())) {
612 return &tagStore
[chain_ptr
];
615 chain_ptr
= tagStore
[chain_ptr
].chain_ptr
;
622 IIC::decompressBlock(unsigned long index
)
624 IICTag
*tag_ptr
= &tagStore
[index
];
625 if (tag_ptr
->isCompressed()) {
626 // decompress the data here.
631 IIC::compressBlock(unsigned long index
)
633 IICTag
*tag_ptr
= &tagStore
[index
];
634 if (!tag_ptr
->isCompressed()) {
635 // Compress the data here.
640 IIC::invalidateBlk(IIC::BlkType
*tag_ptr
)
643 for (int i
= 0; i
< tag_ptr
->numData
; ++i
) {
644 dataReferenceCount
[tag_ptr
->data_ptr
[i
]]--;
645 if (dataReferenceCount
[tag_ptr
->data_ptr
[i
]] == 0) {
646 freeDataBlock(tag_ptr
->data_ptr
[i
]);
649 repl
->removeEntry(tag_ptr
->re
);
655 IIC::readData(IICTag
*blk
, uint8_t *data
)
657 assert(blk
->size
<= trivialSize
|| blk
->numData
> 0);
658 int data_size
= blk
->size
;
659 if (data_size
> trivialSize
) {
660 for (int i
= 0; i
< blk
->numData
; ++i
){
661 memcpy(data
+i
*subSize
,
662 &(dataBlks
[blk
->data_ptr
[i
]][0]),
663 (data_size
>subSize
)?subSize
:data_size
);
664 data_size
-= subSize
;
667 memcpy(data
,blk
->trivialData
,data_size
);
672 IIC::writeData(IICTag
*blk
, uint8_t *write_data
, int size
,
673 PacketList
& writebacks
)
675 assert(size
< blkSize
|| !blk
->isCompressed());
676 DPRINTF(IIC
, "Writing %d bytes to %x\n", size
,
678 // Find the number of subblocks needed, (round up)
679 int num_subs
= (size
+ (subSize
-1))/subSize
;
680 if (size
<= trivialSize
) {
683 assert(num_subs
<= numSub
);
684 if (num_subs
> blk
->numData
) {
685 // need to allocate more data blocks
686 for (int i
= blk
->numData
; i
< num_subs
; ++i
){
687 blk
->data_ptr
[i
] = getFreeDataBlock(writebacks
);
688 dataReferenceCount
[blk
->data_ptr
[i
]] += 1;
690 } else if (num_subs
< blk
->numData
){
691 // can free data blocks
692 for (int i
=num_subs
; i
< blk
->numData
; ++i
){
693 // decrement reference count and compare to zero
694 if (--dataReferenceCount
[blk
->data_ptr
[i
]] == 0) {
695 freeDataBlock(blk
->data_ptr
[i
]);
700 blk
->numData
= num_subs
;
702 assert(size
<= trivialSize
|| blk
->numData
> 0);
703 if (size
> trivialSize
){
704 for (int i
= 0; i
< blk
->numData
; ++i
){
705 memcpy(&dataBlks
[blk
->data_ptr
[i
]][0], write_data
+ i
*subSize
,
706 (size
>subSize
)?subSize
:size
);
710 memcpy(blk
->trivialData
,write_data
,size
);
718 for (int i
= 0; i
< numTags
; ++i
) {
719 if (tagStore
[i
].isValid()) {
720 totalRefs
+= tagStore
[i
].refCount
;