base: Tag API methods and variables in channel_addr.hh
[gem5.git] / src / base / channel_addr.hh
1 /*
2 * Copyright (c) 2019 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 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #ifndef __BASE_CHANNEL_ADDR_HH__
39 #define __BASE_CHANNEL_ADDR_HH__
40
41 #include <ostream>
42
43 #include "base/addr_range.hh"
44
45 /**
46 * Class holding a guest address in a contiguous channel-local address
47 * space.
48 */
49 class ChannelAddr
50 {
51 public:
52 using Type = Addr;
53
54 /**
55 * Explicit constructor assigning a value.
56 *
57 * @ingroup api_channel_addr
58 * @{
59 */
60 explicit constexpr ChannelAddr(Type _a) : a(_a) { }
61
62 /** Converting back to the value type. */
63 explicit constexpr operator Type() const { return a; }
64
65 /** Converting back to the value type. */
66 constexpr Type value() const { return a; }
67
68 constexpr ChannelAddr() : a(0) { }
69
70 ChannelAddr(const AddrRange &range, Addr _a)
71 : a(range.removeIntlvBits(_a)) {}
72
73 ChannelAddr(const ChannelAddr &) = default;
74 ChannelAddr &operator=(const ChannelAddr &) = default;
75
76
77 Addr getPA(const AddrRange &range) const {
78 return range.addIntlvBits(a);
79 }
80
81 constexpr ChannelAddr operator|(const Type b) const {
82 return ChannelAddr(a | b);
83 }
84 constexpr ChannelAddr operator&(const Type b) const {
85 return ChannelAddr(a & b);
86 }
87
88 constexpr ChannelAddr operator>>(const int b) const {
89 return ChannelAddr(a >> b);
90 }
91
92 constexpr ChannelAddr operator<<(const int b) const {
93 return ChannelAddr(a << b);
94 }
95
96 constexpr ChannelAddr operator*(const Type &b) const {
97 return ChannelAddr(a * b);
98 }
99
100 constexpr ChannelAddr operator/(const Type &b) const {
101 return ChannelAddr(a / b);
102 }
103
104 constexpr ChannelAddr operator+(const Type &b) const {
105 return ChannelAddr(a + b);
106 }
107
108 constexpr ChannelAddr operator-(const Type &b) const {
109 return ChannelAddr(a - b);
110 }
111
112 constexpr ChannelAddr operator|(const ChannelAddr &b) const {
113 return ChannelAddr(a | b.a);
114 }
115
116 constexpr ChannelAddr operator&(const ChannelAddr &b) const {
117 return ChannelAddr(a & b.a);
118 }
119
120 constexpr ChannelAddr operator^(const ChannelAddr &b) const {
121 return ChannelAddr(a ^ b.a);
122 }
123
124 constexpr ChannelAddr operator+(const ChannelAddr &b) const {
125 return ChannelAddr(a + b.a);
126 }
127
128 constexpr ChannelAddr operator-(const ChannelAddr &b) const {
129 return ChannelAddr(a - b.a);
130 }
131
132 constexpr bool operator>(const ChannelAddr &b) const { return a > b.a; }
133 constexpr bool operator>=(const ChannelAddr &b) const { return a >= b.a; }
134 constexpr bool operator<(const ChannelAddr &b) const { return a < b.a; }
135 constexpr bool operator<=(const ChannelAddr &b) const { return a <= b.a; }
136 constexpr bool operator==(const ChannelAddr &b) const { return a == b.a; }
137 constexpr bool operator!=(const ChannelAddr &b) const { return a != b.a; }
138
139 /** @} */ // end of api_channel_addr
140
141 private:
142 /** Member holding the actual value. */
143 Type a;
144 };
145
146 /**
147 * The ChanneelAddrRange class describes a contiguous range of
148 * addresses in a contiguous channel-local address space.
149 */
150 class ChannelAddrRange
151 {
152 public:
153 /**
154 * @ingroup api_channel_addr
155 * @{
156 */
157 constexpr ChannelAddrRange()
158 : ChannelAddrRange(ChannelAddr(1), ChannelAddr(0)) {}
159
160 constexpr ChannelAddrRange(ChannelAddr start, ChannelAddr end)
161 : _start(start), _end(end) {}
162
163 ChannelAddrRange(AddrRange ch_range, Addr start, Addr end);
164 ChannelAddrRange(AddrRange ch_range, AddrRange range);
165
166 constexpr ChannelAddrRange(const ChannelAddrRange &) = default;
167
168 constexpr ChannelAddr size() const { return _end - _start + 1; }
169
170 constexpr bool valid() const { return _start <= _end; }
171
172 constexpr ChannelAddr start() const { return _start; }
173 constexpr ChannelAddr end() const { return _end; }
174
175 constexpr bool contains(ChannelAddr a) const {
176 return a >= _start && a <= _end;
177 }
178
179 /** @} */ // end of api_channel_addr
180
181 protected:
182 ChannelAddr _start;
183 ChannelAddr _end;
184 };
185
186 namespace std
187 {
188 template<>
189 struct hash<ChannelAddr>
190 {
191 typedef ChannelAddr argument_type;
192 typedef std::size_t result_type;
193
194 result_type
195 operator()(argument_type const &a) const noexcept {
196 return std::hash<ChannelAddr::Type>{}(
197 static_cast<argument_type::Type>(a));
198 }
199 };
200 }
201
202 /**
203 * @ingroup api_channel_addr
204 */
205 std::ostream &operator<<(std::ostream &out, const ChannelAddr &addr);
206
207 #endif // __BASE_CHANNEL_ADDR_HH__