2 * Copyright (c) 2012, 2014, 2017-2019 ARM Limited
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.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * Authors: Nathan Binkert
45 #ifndef __BASE_ADDR_RANGE_HH__
46 #define __BASE_ADDR_RANGE_HH__
52 #include "base/bitfield.hh"
53 #include "base/cprintf.hh"
54 #include "base/logging.hh"
55 #include "base/types.hh"
58 * The AddrRange class encapsulates an address range, and supports a
59 * number of tests to check if two ranges intersect, if a range
60 * contains a specific address etc. Besides a basic range, the
61 * AddrRange also support interleaved ranges, to stripe across cache
62 * banks, or memory controllers. The interleaving is implemented by
63 * allowing a number of bits of the address, at an arbitrary bit
64 * position, to be used as interleaving bits with an associated
65 * matching value. In addition, to prevent uniformly strided address
66 * patterns from a very biased interleaving, we also allow XOR-based
67 * hashing by specifying a set of bits to XOR with before matching.
69 * The AddrRange is also able to coalesce a number of interleaved
70 * ranges to a contiguous range.
77 /// Private fields for the start and end of the range
78 /// _start is the beginning of the range (inclusive).
79 /// _end is not part of the range.
84 * Each mask determines the bits we need to xor to get one bit of
85 * sel. The first (0) mask is used to get the LSB and the last for
88 std::vector<Addr> masks;
90 /** The value to compare sel with. */
96 : _start(1), _end(0), intlvMatch(0)
100 * Construct an address range
102 * If the user provides a non empty vector of masks then the
103 * address range is interleaved. Each mask determines a set of
104 * bits that are xored to determine one bit of the sel value,
105 * starting from the least significant bit (i.e., masks[0]
106 * determines the least significant bit of sel, ...). If sel
107 * matches the provided _intlv_match then the address a is in the
110 * For example if the input mask is
111 * _masks = { 1 << 8 | 1 << 11 | 1 << 13,
112 * 1 << 15 | 1 << 17 | 1 << 19}
114 * Then a belongs to the address range if
117 * sel == _intlv_match
119 * sel[0] = a[8] ^ a[11] ^ a[13]
120 * sel[1] = a[15] ^ a[17] ^ a[19]
122 * @param _start The start address of this range
123 * @param _end The end address of this range (not included in the range)
124 * @param _masks The input vector of masks
125 * @param intlv_match The matching value of the xor operations
127 AddrRange(Addr _start, Addr _end, const std::vector<Addr> &_masks,
128 uint8_t _intlv_match)
129 : _start(_start), _end(_end), masks(_masks),
130 intlvMatch(_intlv_match)
133 fatal_if(!masks.empty() && _intlv_match >= ULL(1) << masks.size(),
134 "Match value %d does not fit in %d interleaving bits\n",
135 _intlv_match, masks.size());
139 * Legacy constructor of AddrRange
141 * If the user provides a non-zero value in _intlv_high_bit the
142 * address range is interleaved.
144 * An address a belongs to the address range if
147 * sel == _intlv_match
150 * sel1 = a[_intlv_low_bit:_intlv_high_bit]
151 * sel2 = a[_xor_low_bit:_xor_high_bit]
152 * _intlv_low_bit = _intlv_high_bit - intv_bits
153 * _xor_low_bit = _xor_high_bit - intv_bits
155 * @param _start The start address of this range
156 * @param _end The end address of this range (not included in the range)
157 * @param _intlv_high_bit The MSB of the intlv bits (disabled if 0)
158 * @param _xor_high_bit The MSB of the xor bit (disabled if 0)
159 * @param _intlv_bits the size, in bits, of the intlv and xor bits
160 * @param intlv_match The matching value of the xor operations
162 AddrRange(Addr _start, Addr _end, uint8_t _intlv_high_bit,
163 uint8_t _xor_high_bit, uint8_t _intlv_bits,
164 uint8_t _intlv_match)
165 : _start(_start), _end(_end), masks(_intlv_bits),
166 intlvMatch(_intlv_match)
169 fatal_if(_intlv_bits && _intlv_match >= ULL(1) << _intlv_bits,
170 "Match value %d does not fit in %d interleaving bits\n",
171 _intlv_match, _intlv_bits);
173 // ignore the XOR bits if not interleaving
174 if (_intlv_bits && _xor_high_bit) {
175 if (_xor_high_bit == _intlv_high_bit) {
176 fatal("XOR and interleave high bit must be different\n");
177 } else if (_xor_high_bit > _intlv_high_bit) {
178 if ((_xor_high_bit - _intlv_high_bit) < _intlv_bits)
179 fatal("XOR and interleave high bit must be at least "
180 "%d bits apart\n", _intlv_bits);
182 if ((_intlv_high_bit - _xor_high_bit) < _intlv_bits) {
183 fatal("Interleave and XOR high bit must be at least "
184 "%d bits apart\n", _intlv_bits);
189 for (auto i = 0; i < _intlv_bits; i++) {
190 uint8_t bit1 = _intlv_high_bit - i;
191 Addr mask = (1ULL << bit1);
193 uint8_t bit2 = _xor_high_bit - i;
194 mask |= (1ULL << bit2);
196 masks[_intlv_bits - i - 1] = mask;
200 AddrRange(Addr _start, Addr _end)
201 : _start(_start), _end(_end), intlvMatch(0)
205 * Create an address range by merging a collection of interleaved
208 * @param ranges Interleaved ranges to be merged
210 AddrRange(const std::vector<AddrRange>& ranges)
211 : _start(1), _end(0), intlvMatch(0)
213 if (!ranges.empty()) {
214 // get the values from the first one and check the others
215 _start = ranges.front()._start;
216 _end = ranges.front()._end;
217 masks = ranges.front().masks;
218 intlvMatch = ranges.front().intlvMatch;
220 // either merge if got all ranges or keep this equal to the single
222 if (ranges.size() > 1) {
224 if (ranges.size() != (ULL(1) << masks.size()))
225 fatal("Got %d ranges spanning %d interleaving bits\n",
226 ranges.size(), masks.size());
229 for (const auto& r : ranges) {
231 fatal("Can only merge ranges with the same start, end "
232 "and interleaving bits, %s %s\n", to_string(),
235 if (r.intlvMatch != match)
236 fatal("Expected interleave match %d but got %d when "
237 "merging\n", match, r.intlvMatch);
246 * Determine if the range is interleaved or not.
248 * @return true if interleaved
250 bool interleaved() const { return masks.size() > 0; }
253 * Determing the interleaving granularity of the range.
255 * @return The size of the regions created by the interleaving bits
257 uint64_t granularity() const
260 auto combined_mask = 0;
261 for (auto mask: masks) {
262 combined_mask |= mask;
264 const uint8_t lowest_bit = ctz64(combined_mask);
265 return ULL(1) << lowest_bit;
272 * Determine the number of interleaved address stripes this range
275 * @return The number of stripes spanned by the interleaving bits
277 uint32_t stripes() const { return ULL(1) << masks.size(); }
280 * Get the size of the address range. For a case where
281 * interleaving is used we make the simplifying assumption that
282 * the size is a divisible by the size of the interleaving slice.
286 return (_end - _start) >> masks.size();
290 * Determine if the range is valid.
292 bool valid() const { return _start <= _end; }
295 * Get the start address of the range.
297 Addr start() const { return _start; }
300 * Get the end address of the range.
302 Addr end() const { return _end; }
305 * Get a string representation of the range. This could
306 * alternatively be implemented as a operator<<, but at the moment
307 * that seems like overkill.
309 std::string to_string() const
313 for (int i = 0; i < masks.size(); i++) {
315 Addr mask = masks[i];
317 auto bit = ctz64(mask);
318 mask &= ~(1ULL << bit);
319 str += csprintf("a[%d]^", bit);
321 str += csprintf("\b=%d", bits(intlvMatch, i));
323 return csprintf("[%#llx:%#llx]%s", _start, _end, str);
325 return csprintf("[%#llx:%#llx]", _start, _end);
330 * Determine if another range merges with the current one, i.e. if
331 * they are part of the same contigous range and have the same
334 * @param r Range to evaluate merging with
335 * @return true if the two ranges would merge
337 bool mergesWith(const AddrRange& r) const
339 return r._start == _start && r._end == _end &&
344 * Determine if another range intersects this one, i.e. if there
345 * is an address that is both in this range and the other
346 * range. No check is made to ensure either range is valid.
348 * @param r Range to intersect with
349 * @return true if the intersection of the two ranges is not empty
351 bool intersects(const AddrRange& r) const
353 if (_start >= r._end || _end <= r._start)
354 // start with the simple case of no overlap at all,
355 // applicable even if we have interleaved ranges
357 else if (!interleaved() && !r.interleaved())
358 // if neither range is interleaved, we are done
361 // now it gets complicated, focus on the cases we care about
363 // keep it simple and check if the address is within
365 return contains(r.start());
366 else if (mergesWith(r))
367 // restrict the check to ranges that belong to the
369 return intlvMatch == r.intlvMatch;
371 panic("Cannot test intersection of %s and %s\n",
372 to_string(), r.to_string());
376 * Determine if this range is a subset of another range, i.e. if
377 * every address in this range is also in the other range. No
378 * check is made to ensure either range is valid.
380 * @param r Range to compare with
381 * @return true if the this range is a subset of the other one
383 bool isSubset(const AddrRange& r) const
386 panic("Cannot test subset of interleaved range %s\n", to_string());
388 // This address range is not interleaved and therefore it
389 // suffices to check the upper bound, the lower bound and
390 // whether it would fit in a continuous segment of the input
392 if (r.interleaved()) {
393 return r.contains(_start) && r.contains(_end) &&
394 size() <= r.granularity();
396 return _start >= r._start && _end <= r._end;
401 * Determine if the range contains an address.
403 * @param a Address to compare with
404 * @return true if the address is in the range
406 bool contains(const Addr& a) const
408 // check if the address is in the range and if there is either
409 // no interleaving, or with interleaving also if the selected
410 // bits from the address match the interleaving value
411 bool in_range = a >= _start && a < _end;
414 for (int i = 0; i < masks.size(); i++) {
415 Addr masked = a & masks[i];
416 // The result of an xor operation is 1 if the number
417 // of bits set is odd or 0 othersize, thefore it
418 // suffices to count the number of bits set to
419 // determine the i-th bit of sel.
420 sel |= (popCount(masked) % 2) << i;
422 return sel == intlvMatch;
428 * Remove the interleaving bits from an input address.
430 * This function returns a new address in a continous range [
431 * start, start + size / intlv_bits). We can achieve this by
432 * discarding the LSB in each mask.
434 * e.g., if the input address is of the form:
435 * ------------------------------------
436 * | a_high | x1 | a_mid | x0 | a_low |
437 * ------------------------------------
438 * where x0 is the LSB set in masks[0]
439 * and x1 is the LSB set in masks[1]
441 * this function will return:
442 * ---------------------------------
443 * | 0 | a_high | a_mid | a_low |
444 * ---------------------------------
446 * @param a the input address
447 * @return the new address
449 inline Addr removeIntlvBits(Addr a) const
451 // Get the LSB set from each mask
452 int masks_lsb[masks.size()];
453 for (int i = 0; i < masks.size(); i++) {
454 masks_lsb[i] = ctz64(masks[i]);
457 // we need to sort the list of bits we will discard as we
458 // discard them one by one starting.
459 std::sort(masks_lsb, masks_lsb + masks.size());
461 for (int i = 0; i < masks.size(); i++) {
462 const int intlv_bit = masks_lsb[i];
464 // on every iteration we remove one bit from the input
465 // address, and therefore the lowest invtl_bit has
466 // also shifted to the right by i positions.
467 a = insertBits(a >> 1, intlv_bit - i - 1, 0, a);
476 * This method adds the interleaving bits removed by
479 inline Addr addIntlvBits(Addr a) const
481 // Get the LSB set from each mask
482 int masks_lsb[masks.size()];
483 for (int i = 0; i < masks.size(); i++) {
484 masks_lsb[i] = ctz64(masks[i]);
487 // Add bits one-by-one from the LSB side.
488 std::sort(masks_lsb, masks_lsb + masks.size());
489 for (int i = 0; i < masks.size(); i++) {
490 const int intlv_bit = masks_lsb[i];
492 // on every iteration we add one bit from the input
493 // address, and therefore the lowest invtl_bit has
494 // also shifted to the left by i positions.
495 a = insertBits(a << 1, intlv_bit + i - 1, 0, a);
501 for (int i = 0; i < masks.size(); i++) {
502 const int lsb = ctz64(masks[i]);
503 const Addr intlv_bit = bits(intlvMatch, i);
504 // Calculate the mask ignoring the LSB
505 const Addr masked = a & masks[i] & ~(1 << lsb);
506 // Set the LSB of the mask to whatever satisfies the selector bit
507 a = insertBits(a, lsb, intlv_bit ^ popCount(masked));
514 * Determine the offset of an address within the range.
516 * This function returns the offset of the given address from the
517 * starting address discarding any bits that are used for
518 * interleaving. This way we can convert the input address to a
519 * new unique address in a continuous range that starts from 0.
521 * @param the input address
522 * @return the flat offset in the address range
524 Addr getOffset(const Addr& a) const
526 bool in_range = a >= _start && a < _end;
531 return removeIntlvBits(a) - removeIntlvBits(_start);
538 * Less-than operator used to turn an STL map into a binary search
539 * tree of non-overlapping address ranges.
541 * @param r Range to compare with
542 * @return true if the start address is less than that of the other range
544 bool operator<(const AddrRange& r) const
546 if (_start != r._start)
547 return _start < r._start;
549 // for now assume that the end is also the same, and that
550 // we are looking at the same interleaving bits
551 return intlvMatch < r.intlvMatch;
554 bool operator==(const AddrRange& r) const
556 if (_start != r._start) return false;
557 if (_end != r._end) return false;
558 if (masks != r.masks) return false;
559 if (intlvMatch != r.intlvMatch) return false;
564 bool operator!=(const AddrRange& r) const
566 return !(*this == r);
571 * Convenience typedef for a collection of address ranges
573 typedef std::list<AddrRange> AddrRangeList;
576 RangeEx(Addr start, Addr end)
577 { return AddrRange(start, end); }
580 RangeIn(Addr start, Addr end)
581 { return AddrRange(start, end + 1); }
584 RangeSize(Addr start, Addr size)
585 { return AddrRange(start, start + size); }
587 #endif // __BASE_ADDR_RANGE_HH__