16700Snate@binkert.org/*
212065Snikos.nikoleris@arm.com * Copyright (c) 2017 ARM Limited
312065Snikos.nikoleris@arm.com * All rights reserved.
412065Snikos.nikoleris@arm.com *
512065Snikos.nikoleris@arm.com * The license below extends only to copyright in the software and shall
612065Snikos.nikoleris@arm.com * not be construed as granting a license to any other intellectual
712065Snikos.nikoleris@arm.com * property including but not limited to intellectual property relating
812065Snikos.nikoleris@arm.com * to a hardware implementation of the functionality of the software
912065Snikos.nikoleris@arm.com * licensed hereunder.  You may use the software subject to the license
1012065Snikos.nikoleris@arm.com * terms below provided that you ensure that this notice is replicated
1112065Snikos.nikoleris@arm.com * unmodified and in its entirety in all distributions of the software,
1212065Snikos.nikoleris@arm.com * modified or unmodified, in source code or in binary form.
1312065Snikos.nikoleris@arm.com *
146700Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
156700Snate@binkert.org * All rights reserved.
166700Snate@binkert.org *
176700Snate@binkert.org * Redistribution and use in source and binary forms, with or without
186700Snate@binkert.org * modification, are permitted provided that the following conditions are
196700Snate@binkert.org * met: redistributions of source code must retain the above copyright
206700Snate@binkert.org * notice, this list of conditions and the following disclaimer;
216700Snate@binkert.org * redistributions in binary form must reproduce the above copyright
226700Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
236700Snate@binkert.org * documentation and/or other materials provided with the distribution;
246700Snate@binkert.org * neither the name of the copyright holders nor the names of its
256700Snate@binkert.org * contributors may be used to endorse or promote products derived from
266700Snate@binkert.org * this software without specific prior written permission.
276700Snate@binkert.org *
286700Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
296700Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
306700Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
316700Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
326700Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
336700Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
346700Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
356700Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
366700Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
376700Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
386700Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
396700Snate@binkert.org */
406285Snate@binkert.org
4111793Sbrandon.potter@amd.com#include "mem/ruby/network/Network.hh"
4211793Sbrandon.potter@amd.com
4312334Sgabeblack@google.com#include "base/logging.hh"
4412065Snikos.nikoleris@arm.com#include "mem/ruby/common/MachineID.hh"
459594Snilay@cs.wisc.edu#include "mem/ruby/network/BasicLink.hh"
4611108Sdavid.hashe@amd.com#include "mem/ruby/system/RubySystem.hh"
476285Snate@binkert.org
489275Snilay@cs.wisc.eduuint32_t Network::m_virtual_networks;
499275Snilay@cs.wisc.eduuint32_t Network::m_control_msg_size;
509275Snilay@cs.wisc.eduuint32_t Network::m_data_msg_size;
519275Snilay@cs.wisc.edu
526876Ssteve.reinhardt@amd.comNetwork::Network(const Params *p)
539465Snilay@cs.wisc.edu    : ClockedObject(p)
546285Snate@binkert.org{
556876Ssteve.reinhardt@amd.com    m_virtual_networks = p->number_of_virtual_networks;
566876Ssteve.reinhardt@amd.com    m_control_msg_size = p->control_msg_size;
576876Ssteve.reinhardt@amd.com
586881SBrad.Beckmann@amd.com    // Total nodes/controllers in network
596881SBrad.Beckmann@amd.com    // Must make sure this is called after the State Machine constructors
607054Snate@binkert.org    m_nodes = MachineType_base_number(MachineType_NUM);
616881SBrad.Beckmann@amd.com    assert(m_nodes != 0);
629594Snilay@cs.wisc.edu    assert(m_virtual_networks != 0);
636881SBrad.Beckmann@amd.com
649594Snilay@cs.wisc.edu    m_topology_ptr = new Topology(p->routers.size(), p->ext_links,
659594Snilay@cs.wisc.edu                                  p->int_links);
6610303Snilay@cs.wisc.edu
6710303Snilay@cs.wisc.edu    // Allocate to and from queues
6810303Snilay@cs.wisc.edu    // Queues that are getting messages from protocol
6910303Snilay@cs.wisc.edu    m_toNetQueues.resize(m_nodes);
7010303Snilay@cs.wisc.edu
7110303Snilay@cs.wisc.edu    // Queues that are feeding the protocol
7210303Snilay@cs.wisc.edu    m_fromNetQueues.resize(m_nodes);
7310303Snilay@cs.wisc.edu
7410303Snilay@cs.wisc.edu    m_ordered.resize(m_virtual_networks);
7511113Sjoe.gross@amd.com    m_vnet_type_names.resize(m_virtual_networks);
7610303Snilay@cs.wisc.edu
7710303Snilay@cs.wisc.edu    for (int i = 0; i < m_virtual_networks; i++) {
7810303Snilay@cs.wisc.edu        m_ordered[i] = false;
7910303Snilay@cs.wisc.edu    }
8010303Snilay@cs.wisc.edu
8110918Sbrandon.potter@amd.com    params()->ruby_system->registerNetwork(this);
826881SBrad.Beckmann@amd.com
836881SBrad.Beckmann@amd.com    // Initialize the controller's network pointers
849594Snilay@cs.wisc.edu    for (std::vector<BasicExtLink*>::const_iterator i = p->ext_links.begin();
859594Snilay@cs.wisc.edu         i != p->ext_links.end(); ++i) {
869594Snilay@cs.wisc.edu        BasicExtLink *ext_link = (*i);
879594Snilay@cs.wisc.edu        AbstractController *abs_cntrl = ext_link->params()->ext_node;
889594Snilay@cs.wisc.edu        abs_cntrl->initNetworkPtr(this);
8912065Snikos.nikoleris@arm.com        const AddrRangeList &ranges = abs_cntrl->getAddrRanges();
9012065Snikos.nikoleris@arm.com        if (!ranges.empty()) {
9112065Snikos.nikoleris@arm.com            MachineID mid = abs_cntrl->getMachineID();
9212065Snikos.nikoleris@arm.com            AddrMapNode addr_map_node = {
9312065Snikos.nikoleris@arm.com                .id = mid.getNum(),
9412065Snikos.nikoleris@arm.com                .ranges = ranges
9512065Snikos.nikoleris@arm.com            };
9612065Snikos.nikoleris@arm.com            addrMap.emplace(mid.getType(), addr_map_node);
9712065Snikos.nikoleris@arm.com        }
989594Snilay@cs.wisc.edu    }
999863Snilay@cs.wisc.edu
1009863Snilay@cs.wisc.edu    // Register a callback function for combining the statistics
1019863Snilay@cs.wisc.edu    Stats::registerDumpCallback(new StatsCallback(this));
10211113Sjoe.gross@amd.com
10311113Sjoe.gross@amd.com    for (auto &it : dynamic_cast<Network *>(this)->params()->ext_links) {
10411113Sjoe.gross@amd.com        it->params()->ext_node->initNetQueues();
10511113Sjoe.gross@amd.com    }
1066285Snate@binkert.org}
1076285Snate@binkert.org
10810303Snilay@cs.wisc.eduNetwork::~Network()
10910303Snilay@cs.wisc.edu{
11010303Snilay@cs.wisc.edu    for (int node = 0; node < m_nodes; node++) {
11110311Snilay@cs.wisc.edu
11210303Snilay@cs.wisc.edu        // Delete the Message Buffers
11310311Snilay@cs.wisc.edu        for (auto& it : m_toNetQueues[node]) {
11410370Snilay@cs.wisc.edu            delete it;
11510311Snilay@cs.wisc.edu        }
11610311Snilay@cs.wisc.edu
11710311Snilay@cs.wisc.edu        for (auto& it : m_fromNetQueues[node]) {
11810370Snilay@cs.wisc.edu            delete it;
11910303Snilay@cs.wisc.edu        }
12010303Snilay@cs.wisc.edu    }
12110303Snilay@cs.wisc.edu
12210303Snilay@cs.wisc.edu    delete m_topology_ptr;
12310303Snilay@cs.wisc.edu}
12410303Snilay@cs.wisc.edu
1257054Snate@binkert.orgvoid
1267054Snate@binkert.orgNetwork::init()
1276285Snate@binkert.org{
1287054Snate@binkert.org    m_data_msg_size = RubySystem::getBlockSizeBytes() + m_control_msg_size;
1296285Snate@binkert.org}
1306493STushar.Krishna@amd.com
1319275Snilay@cs.wisc.eduuint32_t
1327054Snate@binkert.orgNetwork::MessageSizeType_to_int(MessageSizeType size_type)
1336493STushar.Krishna@amd.com{
1347054Snate@binkert.org    switch(size_type) {
1357054Snate@binkert.org      case MessageSizeType_Control:
1367054Snate@binkert.org      case MessageSizeType_Request_Control:
1377054Snate@binkert.org      case MessageSizeType_Reissue_Control:
1387054Snate@binkert.org      case MessageSizeType_Response_Control:
1397054Snate@binkert.org      case MessageSizeType_Writeback_Control:
1407548SBrad.Beckmann@amd.com      case MessageSizeType_Broadcast_Control:
1417904SBrad.Beckmann@amd.com      case MessageSizeType_Multicast_Control:
1427054Snate@binkert.org      case MessageSizeType_Forwarded_Control:
1437054Snate@binkert.org      case MessageSizeType_Invalidate_Control:
1447054Snate@binkert.org      case MessageSizeType_Unblock_Control:
1457054Snate@binkert.org      case MessageSizeType_Persistent_Control:
1467054Snate@binkert.org      case MessageSizeType_Completion_Control:
1477054Snate@binkert.org        return m_control_msg_size;
1487054Snate@binkert.org      case MessageSizeType_Data:
1497054Snate@binkert.org      case MessageSizeType_Response_Data:
1507054Snate@binkert.org      case MessageSizeType_ResponseLocal_Data:
1517054Snate@binkert.org      case MessageSizeType_ResponseL2hit_Data:
1527054Snate@binkert.org      case MessageSizeType_Writeback_Data:
1537054Snate@binkert.org        return m_data_msg_size;
1547054Snate@binkert.org      default:
1557805Snilay@cs.wisc.edu        panic("Invalid range for type MessageSizeType");
1567054Snate@binkert.org        break;
1577054Snate@binkert.org    }
1586493STushar.Krishna@amd.com}
15911113Sjoe.gross@amd.com
16011113Sjoe.gross@amd.comvoid
16111113Sjoe.gross@amd.comNetwork::checkNetworkAllocation(NodeID id, bool ordered,
16211113Sjoe.gross@amd.com                                        int network_num,
16311113Sjoe.gross@amd.com                                        std::string vnet_type)
16411113Sjoe.gross@amd.com{
16511113Sjoe.gross@amd.com    fatal_if(id >= m_nodes, "Node ID is out of range");
16611113Sjoe.gross@amd.com    fatal_if(network_num >= m_virtual_networks, "Network id is out of range");
16711113Sjoe.gross@amd.com
16811113Sjoe.gross@amd.com    if (ordered) {
16911113Sjoe.gross@amd.com        m_ordered[network_num] = true;
17011113Sjoe.gross@amd.com    }
17111113Sjoe.gross@amd.com
17211113Sjoe.gross@amd.com    m_vnet_type_names[network_num] = vnet_type;
17311113Sjoe.gross@amd.com}
17411113Sjoe.gross@amd.com
17511113Sjoe.gross@amd.com
17611113Sjoe.gross@amd.comvoid
17711113Sjoe.gross@amd.comNetwork::setToNetQueue(NodeID id, bool ordered, int network_num,
17811113Sjoe.gross@amd.com                                 std::string vnet_type, MessageBuffer *b)
17911113Sjoe.gross@amd.com{
18011113Sjoe.gross@amd.com    checkNetworkAllocation(id, ordered, network_num, vnet_type);
18111113Sjoe.gross@amd.com    while (m_toNetQueues[id].size() <= network_num) {
18211113Sjoe.gross@amd.com        m_toNetQueues[id].push_back(nullptr);
18311113Sjoe.gross@amd.com    }
18411113Sjoe.gross@amd.com    m_toNetQueues[id][network_num] = b;
18511113Sjoe.gross@amd.com}
18611113Sjoe.gross@amd.com
18711113Sjoe.gross@amd.comvoid
18811113Sjoe.gross@amd.comNetwork::setFromNetQueue(NodeID id, bool ordered, int network_num,
18911113Sjoe.gross@amd.com                                   std::string vnet_type, MessageBuffer *b)
19011113Sjoe.gross@amd.com{
19111113Sjoe.gross@amd.com    checkNetworkAllocation(id, ordered, network_num, vnet_type);
19211113Sjoe.gross@amd.com    while (m_fromNetQueues[id].size() <= network_num) {
19311113Sjoe.gross@amd.com        m_fromNetQueues[id].push_back(nullptr);
19411113Sjoe.gross@amd.com    }
19511113Sjoe.gross@amd.com    m_fromNetQueues[id][network_num] = b;
19611113Sjoe.gross@amd.com}
19712065Snikos.nikoleris@arm.com
19812065Snikos.nikoleris@arm.comNodeID
19912065Snikos.nikoleris@arm.comNetwork::addressToNodeID(Addr addr, MachineType mtype)
20012065Snikos.nikoleris@arm.com{
20112065Snikos.nikoleris@arm.com    // Look through the address maps for entries with matching machine
20212065Snikos.nikoleris@arm.com    // type to get the responsible node for this address.
20312065Snikos.nikoleris@arm.com    const auto &matching_ranges = addrMap.equal_range(mtype);
20412065Snikos.nikoleris@arm.com    for (auto it = matching_ranges.first; it != matching_ranges.second; it++) {
20512065Snikos.nikoleris@arm.com        AddrMapNode &node = it->second;
20612065Snikos.nikoleris@arm.com        auto &ranges = node.ranges;
20712065Snikos.nikoleris@arm.com        for (AddrRange &range: ranges) {
20812065Snikos.nikoleris@arm.com            if (range.contains(addr)) {
20912065Snikos.nikoleris@arm.com                return node.id;
21012065Snikos.nikoleris@arm.com            }
21112065Snikos.nikoleris@arm.com        }
21212065Snikos.nikoleris@arm.com    }
21312065Snikos.nikoleris@arm.com    return MachineType_base_count(mtype);
21412065Snikos.nikoleris@arm.com}
215