cea3d6529dccd1537cc31749dbeeb7cf3d35473c
[gem5.git] / src / mem / cache / tags / sector_blk.cc
1 /**
2 * Copyright (c) 2018 Inria
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28
29 /** @file
30 * Implementation of a simple sector block class. Each sector consists of a
31 * sequence of cache blocks that may or may not be present in the cache.
32 */
33
34 #include "mem/cache/tags/sector_blk.hh"
35
36 #include <cassert>
37
38 #include "base/cprintf.hh"
39 #include "base/logging.hh"
40
41 void
42 SectorSubBlk::setSectorBlock(SectorBlk* sector_blk)
43 {
44 assert(sector_blk != nullptr);
45 _sectorBlk = sector_blk;
46 }
47
48 const SectorBlk*
49 SectorSubBlk::getSectorBlock() const
50 {
51 return _sectorBlk;
52 }
53
54 void
55 SectorSubBlk::setSectorOffset(const int sector_offset)
56 {
57 _sectorOffset = sector_offset;
58 }
59
60 int
61 SectorSubBlk::getSectorOffset() const
62 {
63 return _sectorOffset;
64 }
65
66 Addr
67 SectorSubBlk::getTag() const
68 {
69 return _sectorBlk->getTag();
70 }
71
72 void
73 SectorSubBlk::setValid()
74 {
75 CacheBlk::setValid();
76 _sectorBlk->validateSubBlk();
77 }
78
79 void
80 SectorSubBlk::setSecure()
81 {
82 CacheBlk::setSecure();
83 _sectorBlk->setSecure();
84 }
85
86 void
87 SectorSubBlk::invalidate()
88 {
89 CacheBlk::invalidate();
90 _sectorBlk->invalidateSubBlk();
91 }
92
93 void
94 SectorSubBlk::insert(const Addr tag, const bool is_secure,
95 const int src_master_ID, const uint32_t task_ID)
96 {
97 // Make sure it is not overwriting another sector
98 panic_if((_sectorBlk && _sectorBlk->isValid()) &&
99 ((_sectorBlk->getTag() != tag) ||
100 (_sectorBlk->isSecure() != is_secure)),
101 "Overwriting valid sector!");
102
103 CacheBlk::insert(tag, is_secure, src_master_ID, task_ID);
104
105 // Set sector tag
106 _sectorBlk->setTag(tag);
107 }
108
109 std::string
110 SectorSubBlk::print() const
111 {
112 return csprintf("%s sector offset: %#x", CacheBlk::print(),
113 getSectorOffset());
114 }
115
116 SectorBlk::SectorBlk()
117 : ReplaceableEntry(), _validCounter(0), _tag(MaxAddr), _secureBit(false)
118 {
119 }
120
121 bool
122 SectorBlk::isValid() const
123 {
124 // If any of the blocks in the sector is valid, so is the sector
125 return _validCounter > 0;
126 }
127
128 uint8_t
129 SectorBlk::getNumValid() const
130 {
131 return _validCounter;
132 }
133
134 bool
135 SectorBlk::isSecure() const
136 {
137 // If any of the valid blocks in the sector is secure, so is the sector
138 return _secureBit;
139 }
140
141 void
142 SectorBlk::setTag(const Addr tag)
143 {
144 _tag = tag;
145 }
146
147 Addr
148 SectorBlk::getTag() const
149 {
150 return _tag;
151 }
152
153 void
154 SectorBlk::validateSubBlk()
155 {
156 _validCounter++;
157 }
158
159 void
160 SectorBlk::invalidateSubBlk()
161 {
162 // If all sub-blocks have been invalidated, the sector becomes invalid,
163 // so clear secure bit
164 if (--_validCounter == 0) {
165 _secureBit = false;
166 }
167 }
168
169 void
170 SectorBlk::setSecure()
171 {
172 _secureBit = true;
173 }
174
175 void
176 SectorBlk::setPosition(const uint32_t set, const uint32_t way)
177 {
178 ReplaceableEntry::setPosition(set, way);
179 for (auto& blk : blks) {
180 blk->setPosition(set, way);
181 }
182 }