gem5 v24.0.0.0
Loading...
Searching...
No Matches
cache_blk.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2018, 2023-2024 ARM Limited
3 * All rights reserved.
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2020 Inria
15 * Copyright (c) 2003-2005 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
46#ifndef __MEM_CACHE_CACHE_BLK_HH__
47#define __MEM_CACHE_CACHE_BLK_HH__
48
49#include <cassert>
50#include <cstdint>
51#include <iosfwd>
52#include <limits>
53#include <list>
54#include <string>
55
56#include "base/printable.hh"
57#include "base/types.hh"
59#include "mem/packet.hh"
60#include "mem/request.hh"
61#include "sim/cur_tick.hh"
62
63namespace gem5
64{
65
71class CacheBlk : public TaggedEntry
72{
73 public:
78 enum CoherenceBits : unsigned
79 {
88 DirtyBit = 0x08,
89
94 AllBits = 0x0E,
95 };
96
104 uint8_t *data = nullptr;
105
111
112 protected:
117 class Lock
118 {
119 public:
120 ContextID contextId; // locking context
121 Addr lowAddr; // low address of lock range
122 Addr highAddr; // high address of lock range
123
124 // check for matching execution context, and an address that
125 // is within the lock
126 bool matches(const RequestPtr &req) const
127 {
128 Addr req_low = req->getPaddr();
129 Addr req_high = req_low + req->getSize() -1;
130 return (contextId == req->contextId()) &&
131 (req_low >= lowAddr) && (req_high <= highAddr);
132 }
133
134 // check if a request is intersecting and thus invalidating the lock
135 bool intersects(const RequestPtr &req) const
136 {
137 Addr req_low = req->getPaddr();
138 Addr req_high = req_low + req->getSize() - 1;
139
140 return (req_low <= highAddr) && (req_high >= lowAddr);
141 }
142
143 Lock(const RequestPtr &req)
144 : contextId(req->contextId()),
145 lowAddr(req->getPaddr()),
146 highAddr(lowAddr + req->getSize() - 1)
147 {
148 }
149 };
150
154
155 public:
157 {
158 invalidate();
159 }
160
161 CacheBlk(const CacheBlk&) = delete;
162 CacheBlk& operator=(const CacheBlk&) = delete;
163 CacheBlk(const CacheBlk&&) = delete;
172 virtual CacheBlk&
174 {
175 // Copying an entry into a valid one would imply in skipping all
176 // replacement steps, so it cannot be allowed
177 assert(!isValid());
178 assert(other.isValid());
179
180 insert(other.getTag(), other.isSecure());
181
182 if (other.wasPrefetched()) {
184 }
185 setCoherenceBits(other.coherence);
186 setTaskId(other.getTaskId());
187 setPartitionId(other.getPartitionId());
189 setRefCount(other.getRefCount());
190 setSrcRequestorId(other.getSrcRequestorId());
191 std::swap(lockList, other.lockList);
192
193 other.invalidate();
194
195 return *this;
196 }
197 virtual ~CacheBlk() {};
198
202 virtual void invalidate() override
203 {
205
208
210 setPartitionId(std::numeric_limits<uint64_t>::max());
212 setRefCount(0);
214 lockList.clear();
215 }
216
222 void
224 {
225 assert(isValid());
226 coherence |= bits;
227 }
228
234 void clearCoherenceBits(unsigned bits) { coherence &= ~bits; }
235
241 bool
242 isSet(unsigned bits) const
243 {
244 return isValid() && (coherence & bits);
245 }
246
252 bool wasPrefetched() const { return _prefetched; }
253
258 void clearPrefetched() { _prefetched = false; }
259
261 void setPrefetched() { _prefetched = true; }
262
269 {
270 assert(whenReady != MaxTick);
271 return whenReady;
272 }
273
281 void setWhenReady(const Tick tick)
282 {
283 assert(tick >= _tickInserted);
284 whenReady = tick;
285 }
286
288 uint32_t getTaskId() const { return _taskId; }
289
291 uint32_t getSrcRequestorId() const { return _srcRequestorId; }
292
294 uint64_t getPartitionId() const { return _partitionId; }
295
297 unsigned getRefCount() const { return _refCount; }
298
301
307 Tick
308 getAge() const
309 {
310 assert(_tickInserted <= curTick());
311 return curTick() - _tickInserted;
312 }
313
326 void insert(const Addr tag, const bool is_secure,
327 const int src_requestor_ID, const uint32_t task_ID,
328 const uint64_t partition_id);
330
336 {
337 assert(pkt->isLLSC());
338 auto l = lockList.begin();
339 while (l != lockList.end()) {
340 if (l->intersects(pkt->req))
341 l = lockList.erase(l);
342 else
343 ++l;
344 }
345
346 lockList.emplace_front(pkt->req);
347 }
348
353 void clearLoadLocks(const RequestPtr &req)
354 {
355 auto l = lockList.begin();
356 while (l != lockList.end()) {
357 if (l->intersects(req) && l->contextId != req->contextId()) {
358 l = lockList.erase(l);
359 } else {
360 ++l;
361 }
362 }
363 }
364
371 std::string
372 print() const override
373 {
398 unsigned state =
399 isSet(WritableBit) << 2 | isSet(DirtyBit) << 1 | isValid();
400 char s = '?';
401 switch (state) {
402 case 0b111: s = 'M'; break;
403 case 0b011: s = 'O'; break;
404 case 0b101: s = 'E'; break;
405 case 0b001: s = 'S'; break;
406 case 0b000: s = 'I'; break;
407 default: s = 'T'; break; // @TODO add other types
408 }
409 return csprintf("state: %x (%c) writable: %d readable: %d "
410 "dirty: %d prefetched: %d | %s", coherence, s,
413 }
414
421 {
422 assert(pkt->isWrite());
423
424 // common case
425 if (!pkt->isLLSC() && lockList.empty())
426 return true;
427
428 const RequestPtr &req = pkt->req;
429
430 if (pkt->isLLSC()) {
431 // it's a store conditional... have to check for matching
432 // load locked.
433 bool success = false;
434
435 auto l = lockList.begin();
436 while (!success && l != lockList.end()) {
437 if (l->matches(pkt->req)) {
438 // it's a store conditional, and as far as the
439 // memory system can tell, the requesting
440 // context's lock is still valid.
441 success = true;
442 lockList.erase(l);
443 } else {
444 ++l;
445 }
446 }
447
448 req->setExtraData(success ? 1 : 0);
449 // clear any intersected locks from other contexts (our LL
450 // should already have cleared them)
451 clearLoadLocks(req);
452 return success;
453 } else {
454 // a normal write, if there is any lock not from this
455 // context we clear the list, thus for a private cache we
456 // never clear locks on normal writes
457 clearLoadLocks(req);
458 return true;
459 }
460 }
461
462 protected:
464 unsigned coherence;
465
466 // The following setters have been marked as protected because their
467 // respective variables should only be modified at 2 moments:
468 // invalidation and insertion. Because of that, they shall only be
469 // called by the functions that perform those actions.
470
472 void setTaskId(const uint32_t task_id) { _taskId = task_id; }
473
475 void setSrcRequestorId(const uint32_t id) { _srcRequestorId = id; }
476
478 void
479 setPartitionId(const uint64_t partitionId) { _partitionId = partitionId; }
480
482 void setRefCount(const unsigned count) { _refCount = count; }
483
486
487 private:
489 uint32_t _taskId = 0;
490
493
495 /* This ID is used to enforce resource partitioning policies */
496 uint64_t _partitionId;
497
499 unsigned _refCount = 0;
500
506
508 bool _prefetched = 0;
509};
510
516class TempCacheBlk final : public CacheBlk
517{
518 private:
523
524 public:
529 TempCacheBlk(unsigned size) : CacheBlk()
530 {
531 data = new uint8_t[size];
532 }
533 TempCacheBlk(const TempCacheBlk&) = delete;
534 using CacheBlk::operator=;
536 ~TempCacheBlk() { delete [] data; };
537
541 void invalidate() override {
543
544 _addr = MaxAddr;
545 }
546
547 void
548 insert(const Addr addr, const bool is_secure) override
549 {
550 CacheBlk::insert(addr, is_secure);
551 _addr = addr;
552 }
553
559 Addr getAddr() const
560 {
561 return _addr;
562 }
563};
564
572{
574 public:
577 void print(std::ostream &o, int verbosity = 0,
578 const std::string &prefix = "") const;
579};
580
581} // namespace gem5
582
583#endif //__MEM_CACHE_CACHE_BLK_HH__
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
Simple class to provide virtual print() method on cache blocks without allocating a vtable pointer fo...
Definition cache_blk.hh:572
void print(std::ostream &o, int verbosity=0, const std::string &prefix="") const
Definition cache_blk.cc:76
CacheBlkPrintWrapper(CacheBlk *_blk)
Definition cache_blk.hh:575
Represents that the indicated thread context has a "lock" on the block, in the LL/SC sense.
Definition cache_blk.hh:118
Lock(const RequestPtr &req)
Definition cache_blk.hh:143
bool matches(const RequestPtr &req) const
Definition cache_blk.hh:126
bool intersects(const RequestPtr &req) const
Definition cache_blk.hh:135
A Basic Cache block.
Definition cache_blk.hh:72
virtual CacheBlk & operator=(CacheBlk &&other)
Move assignment operator.
Definition cache_blk.hh:173
void setWhenReady(const Tick tick)
Set tick at which block's data will be available for access.
Definition cache_blk.hh:281
CoherenceBits
Cache block's enum listing the supported coherence bits.
Definition cache_blk.hh:79
@ ReadableBit
Read permission.
Definition cache_blk.hh:86
@ AllBits
Helper enum value that includes all other bits.
Definition cache_blk.hh:94
@ WritableBit
write permission
Definition cache_blk.hh:81
@ DirtyBit
dirty (modified)
Definition cache_blk.hh:88
Tick whenReady
Which curTick() will this block be accessible.
Definition cache_blk.hh:110
uint32_t getSrcRequestorId() const
Get the requestor id associated to this block.
Definition cache_blk.hh:291
int _srcRequestorId
holds the source requestor ID for this block.
Definition cache_blk.hh:492
Tick getWhenReady() const
Get tick at which block's data will be available for access.
Definition cache_blk.hh:268
void clearPrefetched()
Clear the prefetching bit.
Definition cache_blk.hh:258
unsigned _refCount
Number of references to this block since it was brought in.
Definition cache_blk.hh:499
void setPrefetched()
Marks this blocks as a recently prefetched block.
Definition cache_blk.hh:261
void insert(const Addr tag, const bool is_secure, const int src_requestor_ID, const uint32_t task_ID, const uint64_t partition_id)
Set member variables when a block insertion occurs.
Definition cache_blk.cc:50
Tick getAge() const
Get the block's age, that is, the number of ticks since its insertion.
Definition cache_blk.hh:308
unsigned getRefCount() const
Get the number of references to this block since insertion.
Definition cache_blk.hh:297
uint64_t getPartitionId() const
Getter for _partitionId.
Definition cache_blk.hh:294
CacheBlk & operator=(const CacheBlk &)=delete
std::string print() const override
Pretty-print tag, set and way, and interpret state bits to readable form including mapping to a MOESI...
Definition cache_blk.hh:372
bool isSet(unsigned bits) const
Checks the given coherence bits are set.
Definition cache_blk.hh:242
void setTaskId(const uint32_t task_id)
Set the task id value.
Definition cache_blk.hh:472
bool _prefetched
Whether this block is an unaccessed hardware prefetch.
Definition cache_blk.hh:508
void setTickInserted()
Set the current tick as this block's insertion tick.
Definition cache_blk.hh:485
CacheBlk(const CacheBlk &&)=delete
uint64_t _partitionId
Partition ID of the activity that allocated this block.
Definition cache_blk.hh:496
virtual void invalidate() override
Invalidate the block and clear all state.
Definition cache_blk.hh:202
uint32_t _taskId
Task Id associated with this block.
Definition cache_blk.hh:489
bool checkWrite(PacketPtr pkt)
Handle interaction of load-locked operations and stores.
Definition cache_blk.hh:420
void clearCoherenceBits(unsigned bits)
Clear the corresponding coherence bits.
Definition cache_blk.hh:234
void clearLoadLocks(const RequestPtr &req)
Clear the any load lock that intersect the request, and is from a different context.
Definition cache_blk.hh:353
void increaseRefCount()
Get the number of references to this block since insertion.
Definition cache_blk.hh:300
uint32_t getTaskId() const
Get the task id associated to this block.
Definition cache_blk.hh:288
void setPartitionId(const uint64_t partitionId)
Setter for _partitionId.
Definition cache_blk.hh:479
void trackLoadLocked(PacketPtr pkt)
Track the fact that a local locked was issued to the block.
Definition cache_blk.hh:335
bool wasPrefetched() const
Check if this block was the result of a hardware prefetch, yet to be touched.
Definition cache_blk.hh:252
CacheBlk(const CacheBlk &)=delete
virtual ~CacheBlk()
Definition cache_blk.hh:197
void setSrcRequestorId(const uint32_t id)
Set the source requestor id.
Definition cache_blk.hh:475
void setRefCount(const unsigned count)
Set the number of references to this block since insertion.
Definition cache_blk.hh:482
std::list< Lock > lockList
List of thread contexts that have performed a load-locked (LL) on the block since the last store.
Definition cache_blk.hh:153
uint8_t * data
Contains a copy of the data in this block for easy access.
Definition cache_blk.hh:104
unsigned coherence
The current coherence status of this block.
Definition cache_blk.hh:464
Tick _tickInserted
Tick on which the block was inserted in the cache.
Definition cache_blk.hh:505
void setCoherenceBits(unsigned bits)
Sets the corresponding coherence bits.
Definition cache_blk.hh:223
Addr tag
The entry's tag.
virtual bool isValid() const
Checks if the entry is valid.
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition packet.hh:295
bool isWrite() const
Definition packet.hh:594
RequestPtr req
A pointer to the original request.
Definition packet.hh:377
bool isLLSC() const
Definition packet.hh:620
Abstract base class for objects which support being printed to a stream for debugging.
Definition printable.hh:48
@ invldRequestorId
Invalid requestor id for assertion checking only.
Definition request.hh:286
A tagged entry is an entry containing a tag.
void invalidate() override
Invalidate the block.
std::string print() const override
Prints relevant information about this entry.
virtual void insert(const Addr tag, const bool is_secure)
Insert the block by assigning it a tag and marking it valid.
Special instance of CacheBlk for use with tempBlk that deals with its block address regeneration.
Definition cache_blk.hh:517
void insert(const Addr addr, const bool is_secure) override
Insert the block by assigning it a tag and marking it valid.
Definition cache_blk.hh:548
Addr getAddr() const
Get block's address.
Definition cache_blk.hh:559
TempCacheBlk(unsigned size)
Creates a temporary cache block, with its own storage.
Definition cache_blk.hh:529
void invalidate() override
Invalidate the block and clear all state.
Definition cache_blk.hh:541
TempCacheBlk & operator=(const TempCacheBlk &)=delete
Addr _addr
Copy of the block's address, used to regenerate tempBlock's address.
Definition cache_blk.hh:522
TempCacheBlk(const TempCacheBlk &)=delete
STL list class.
Definition stl.hh:51
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition bitfield.hh:79
atomic_var_t state
Definition helpers.cc:211
Bitfield< 4 > s
Bitfield< 33 > id
Bitfield< 5 > l
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
std::shared_ptr< Request > RequestPtr
Definition request.hh:94
Tick curTick()
The universal simulation clock.
Definition cur_tick.hh:46
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
uint64_t Tick
Tick count type.
Definition types.hh:58
const Tick MaxTick
Definition types.hh:60
int ContextID
Globally unique thread context ID.
Definition types.hh:239
std::string csprintf(const char *format, const Args &...args)
Definition cprintf.hh:161
const Addr MaxAddr
Definition types.hh:171
Declaration of the Packet class.
Declaration of a request, the overall memory request consisting of the parts of the request that are ...

Generated on Tue Jun 18 2024 16:24:04 for gem5 by doxygen 1.11.0