19155SN/A/*
29155SN/A * Copyright (c) 2012 Advanced Micro Devices, Inc.
39155SN/A * All rights reserved.
49155SN/A *
59155SN/A * Redistribution and use in source and binary forms, with or without
69155SN/A * modification, are permitted provided that the following conditions are
79155SN/A * met: redistributions of source code must retain the above copyright
89155SN/A * notice, this list of conditions and the following disclaimer;
99155SN/A * redistributions in binary form must reproduce the above copyright
109155SN/A * notice, this list of conditions and the following disclaimer in the
119155SN/A * documentation and/or other materials provided with the distribution;
129155SN/A * neither the name of the copyright holders nor the names of its
139155SN/A * contributors may be used to endorse or promote products derived from
149155SN/A * this software without specific prior written permission.
159155SN/A *
169155SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
179155SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
189155SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
199155SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
209155SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
219155SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
229155SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239155SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249155SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259155SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
269155SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279155SN/A *
289155SN/A * Author: Brad Beckmann
299155SN/A *
309155SN/A */
319105SN/A
3211793Sbrandon.potter@amd.com#include "mem/ruby/structures/BankedArray.hh"
3311793Sbrandon.potter@amd.com
349297SN/A#include "base/intmath.hh"
3511108Sdavid.hashe@amd.com#include "mem/ruby/system/RubySystem.hh"
369105SN/A
379297SN/ABankedArray::BankedArray(unsigned int banks, Cycles accessLatency,
3810919Sbrandon.potter@amd.com                         unsigned int startIndexBit, RubySystem *rs)
3910919Sbrandon.potter@amd.com    : m_ruby_system(rs)
409105SN/A{
419105SN/A    this->banks = banks;
429105SN/A    this->accessLatency = accessLatency;
439105SN/A    this->startIndexBit = startIndexBit;
449105SN/A
459105SN/A    if (banks != 0) {
469105SN/A        bankBits = floorLog2(banks);
479105SN/A    }
489105SN/A
499105SN/A    busyBanks.resize(banks);
509105SN/A}
519105SN/A
529105SN/Abool
5311061Snilay@cs.wisc.eduBankedArray::tryAccess(int64_t idx)
549105SN/A{
559105SN/A    if (accessLatency == 0)
569105SN/A        return true;
579105SN/A
589105SN/A    unsigned int bank = mapIndexToBank(idx);
599105SN/A    assert(bank < banks);
609105SN/A
619297SN/A    if (busyBanks[bank].endAccess >= curTick()) {
629105SN/A            return false;
6310978Sdavid.hashe@amd.com    }
6410978Sdavid.hashe@amd.com
6510978Sdavid.hashe@amd.com    return true;
6610978Sdavid.hashe@amd.com}
6710978Sdavid.hashe@amd.com
6810978Sdavid.hashe@amd.comvoid
6911061Snilay@cs.wisc.eduBankedArray::reserve(int64_t idx)
7010978Sdavid.hashe@amd.com{
7110978Sdavid.hashe@amd.com    if (accessLatency == 0)
7210978Sdavid.hashe@amd.com        return;
7310978Sdavid.hashe@amd.com
7410978Sdavid.hashe@amd.com    unsigned int bank = mapIndexToBank(idx);
7510978Sdavid.hashe@amd.com    assert(bank < banks);
7610978Sdavid.hashe@amd.com
7711321Ssteve.reinhardt@amd.com    if (busyBanks[bank].endAccess >= curTick()) {
7810978Sdavid.hashe@amd.com        if (busyBanks[bank].startAccess == curTick() &&
7910978Sdavid.hashe@amd.com             busyBanks[bank].idx == idx) {
8010978Sdavid.hashe@amd.com            // this is the same reservation (can happen when
8110978Sdavid.hashe@amd.com            // e.g., reserve the same resource for read and write)
8210978Sdavid.hashe@amd.com            return; // OK
839105SN/A        } else {
8410978Sdavid.hashe@amd.com            panic("BankedArray reservation error");
859105SN/A        }
869105SN/A    }
879105SN/A
889105SN/A    busyBanks[bank].idx = idx;
899105SN/A    busyBanks[bank].startAccess = curTick();
909297SN/A    busyBanks[bank].endAccess = curTick() +
9110919Sbrandon.potter@amd.com        (accessLatency-1) * m_ruby_system->clockPeriod();
929105SN/A}
939105SN/A
949105SN/Aunsigned int
9511061Snilay@cs.wisc.eduBankedArray::mapIndexToBank(int64_t idx)
969105SN/A{
979105SN/A    if (banks == 1) {
989105SN/A        return 0;
999105SN/A    }
1009105SN/A    return idx % banks;
1019105SN/A}
102