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