16145Snate@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 *
146145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
156145Snate@binkert.org * All rights reserved.
166145Snate@binkert.org *
176145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
186145Snate@binkert.org * modification, are permitted provided that the following conditions are
196145Snate@binkert.org * met: redistributions of source code must retain the above copyright
206145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
216145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
226145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
236145Snate@binkert.org * documentation and/or other materials provided with the distribution;
246145Snate@binkert.org * neither the name of the copyright holders nor the names of its
256145Snate@binkert.org * contributors may be used to endorse or promote products derived from
266145Snate@binkert.org * this software without specific prior written permission.
276145Snate@binkert.org *
286145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
296145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
306145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
316145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
326145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
336145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
346145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
356145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
366145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
376145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
386145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
396145Snate@binkert.org */
406145Snate@binkert.org
416145Snate@binkert.org/*
427054Snate@binkert.org * The Network class is the base class for classes that implement the
437054Snate@binkert.org * interconnection network between components (processor/cache
447054Snate@binkert.org * components and memory/directory components).  The interconnection
457054Snate@binkert.org * network as described here is not a physical network, but a
467054Snate@binkert.org * programming concept used to implement all communication between
477054Snate@binkert.org * components.  Thus parts of this 'network' will model the on-chip
487054Snate@binkert.org * connections between cache controllers and directory controllers as
497054Snate@binkert.org * well as the links between chip and network switches.
507054Snate@binkert.org */
516145Snate@binkert.org
527054Snate@binkert.org#ifndef __MEM_RUBY_NETWORK_NETWORK_HH__
537054Snate@binkert.org#define __MEM_RUBY_NETWORK_NETWORK_HH__
546145Snate@binkert.org
557055Snate@binkert.org#include <iostream>
567055Snate@binkert.org#include <string>
5712065Snikos.nikoleris@arm.com#include <unordered_map>
587454Snate@binkert.org#include <vector>
597055Snate@binkert.org
6012065Snikos.nikoleris@arm.com#include "base/addr_range.hh"
6112065Snikos.nikoleris@arm.com#include "base/types.hh"
6212065Snikos.nikoleris@arm.com#include "mem/packet.hh"
6313784Sgabeblack@google.com#include "mem/port.hh"
6412065Snikos.nikoleris@arm.com#include "mem/ruby/common/MachineID.hh"
658645Snilay@cs.wisc.edu#include "mem/ruby/common/TypeDefines.hh"
669594Snilay@cs.wisc.edu#include "mem/ruby/network/Topology.hh"
6713784Sgabeblack@google.com#include "mem/ruby/network/dummy_port.hh"
6814184Sgabeblack@google.com#include "mem/ruby/protocol/LinkDirection.hh"
6914184Sgabeblack@google.com#include "mem/ruby/protocol/MessageSizeType.hh"
707054Snate@binkert.org#include "params/RubyNetwork.hh"
719465Snilay@cs.wisc.edu#include "sim/clocked_object.hh"
726145Snate@binkert.org
736145Snate@binkert.orgclass NetDest;
746145Snate@binkert.orgclass MessageBuffer;
756145Snate@binkert.org
769465Snilay@cs.wisc.educlass Network : public ClockedObject
777054Snate@binkert.org{
787054Snate@binkert.org  public:
796876Ssteve.reinhardt@amd.com    typedef RubyNetworkParams Params;
806876Ssteve.reinhardt@amd.com    Network(const Params *p);
819594Snilay@cs.wisc.edu    const Params * params() const
8210917Sbrandon.potter@amd.com    { return dynamic_cast<const Params *>(_params); }
836145Snate@binkert.org
8410303Snilay@cs.wisc.edu    virtual ~Network();
8513799SAndrea.Mondelli@ucf.edu    void init() override;
866145Snate@binkert.org
879497Snilay@cs.wisc.edu    static uint32_t getNumberOfVirtualNetworks() { return m_virtual_networks; }
8810303Snilay@cs.wisc.edu    int getNumNodes() const { return m_nodes; }
8910303Snilay@cs.wisc.edu
909275Snilay@cs.wisc.edu    static uint32_t MessageSizeType_to_int(MessageSizeType size_type);
916493STushar.Krishna@amd.com
927054Snate@binkert.org    // returns the queue requested for the given component
9311113Sjoe.gross@amd.com    void setToNetQueue(NodeID id, bool ordered, int netNumber,
9411113Sjoe.gross@amd.com                               std::string vnet_type, MessageBuffer *b);
9510311Snilay@cs.wisc.edu    virtual void setFromNetQueue(NodeID id, bool ordered, int netNumber,
9611113Sjoe.gross@amd.com                                 std::string vnet_type, MessageBuffer *b);
9711113Sjoe.gross@amd.com
9811113Sjoe.gross@amd.com    virtual void checkNetworkAllocation(NodeID id, bool ordered,
9911113Sjoe.gross@amd.com        int network_num, std::string vnet_type);
1006145Snate@binkert.org
10111663Stushar@ece.gatech.edu    virtual void makeExtOutLink(SwitchID src, NodeID dest, BasicLink* link,
1029799Snilay@cs.wisc.edu                             const NetDest& routing_table_entry) = 0;
10311663Stushar@ece.gatech.edu    virtual void makeExtInLink(NodeID src, SwitchID dest, BasicLink* link,
1049799Snilay@cs.wisc.edu                            const NetDest& routing_table_entry) = 0;
1058257SBrad.Beckmann@amd.com    virtual void makeInternalLink(SwitchID src, SwitchID dest, BasicLink* link,
10611664Stushar@ece.gatech.edu                                  const NetDest& routing_table_entry,
10711664Stushar@ece.gatech.edu                                  PortDirection src_outport,
10811664Stushar@ece.gatech.edu                                  PortDirection dst_inport) = 0;
1096145Snate@binkert.org
1109863Snilay@cs.wisc.edu    virtual void collateStats() = 0;
1117055Snate@binkert.org    virtual void print(std::ostream& out) const = 0;
1126145Snate@binkert.org
1139302Snilay@cs.wisc.edu    /*
1149302Snilay@cs.wisc.edu     * Virtual functions for functionally reading and writing packets in
1159302Snilay@cs.wisc.edu     * the network. Each network needs to implement these for functional
1169302Snilay@cs.wisc.edu     * accesses to work correctly.
1179302Snilay@cs.wisc.edu     */
1189302Snilay@cs.wisc.edu    virtual bool functionalRead(Packet *pkt)
1199302Snilay@cs.wisc.edu    { fatal("Functional read not implemented.\n"); }
1209302Snilay@cs.wisc.edu    virtual uint32_t functionalWrite(Packet *pkt)
1219302Snilay@cs.wisc.edu    { fatal("Functional write not implemented.\n"); }
1229302Snilay@cs.wisc.edu
12312065Snikos.nikoleris@arm.com    /**
12412065Snikos.nikoleris@arm.com     * Map an address to the correct NodeID
12512065Snikos.nikoleris@arm.com     *
12612065Snikos.nikoleris@arm.com     * This function traverses the global address map to find the
12712065Snikos.nikoleris@arm.com     * NodeID that corresponds to the given address and the type of
12812065Snikos.nikoleris@arm.com     * the destination. For example for a request to a directory this
12912065Snikos.nikoleris@arm.com     * function will return the NodeID of the right directory.
13012065Snikos.nikoleris@arm.com     *
13112065Snikos.nikoleris@arm.com     * @param the destination address
13212065Snikos.nikoleris@arm.com     * @param the type of the destination
13312065Snikos.nikoleris@arm.com     * @return the NodeID of the destination
13412065Snikos.nikoleris@arm.com     */
13512065Snikos.nikoleris@arm.com    NodeID addressToNodeID(Addr addr, MachineType mtype);
13612065Snikos.nikoleris@arm.com
13713784Sgabeblack@google.com    Port &
13813784Sgabeblack@google.com    getPort(const std::string &, PortID idx=InvalidPortID) override
13913784Sgabeblack@google.com    {
14013784Sgabeblack@google.com        return RubyDummyPort::instance();
14113784Sgabeblack@google.com    }
14213784Sgabeblack@google.com
1437054Snate@binkert.org  protected:
1447054Snate@binkert.org    // Private copy constructor and assignment operator
1457054Snate@binkert.org    Network(const Network& obj);
1467054Snate@binkert.org    Network& operator=(const Network& obj);
1476145Snate@binkert.org
1489863Snilay@cs.wisc.edu    uint32_t m_nodes;
1499275Snilay@cs.wisc.edu    static uint32_t m_virtual_networks;
15011113Sjoe.gross@amd.com    std::vector<std::string> m_vnet_type_names;
1517054Snate@binkert.org    Topology* m_topology_ptr;
1529275Snilay@cs.wisc.edu    static uint32_t m_control_msg_size;
1539275Snilay@cs.wisc.edu    static uint32_t m_data_msg_size;
1549863Snilay@cs.wisc.edu
15510082Snilay@cs.wisc.edu    // vector of queues from the components
15610370Snilay@cs.wisc.edu    std::vector<std::vector<MessageBuffer*> > m_toNetQueues;
15710370Snilay@cs.wisc.edu    std::vector<std::vector<MessageBuffer*> > m_fromNetQueues;
15810082Snilay@cs.wisc.edu    std::vector<bool> m_ordered;
15910082Snilay@cs.wisc.edu
1609863Snilay@cs.wisc.edu  private:
1619863Snilay@cs.wisc.edu    //! Callback class used for collating statistics from all the
1629863Snilay@cs.wisc.edu    //! controller of this type.
1639863Snilay@cs.wisc.edu    class StatsCallback : public Callback
1649863Snilay@cs.wisc.edu    {
1659863Snilay@cs.wisc.edu      private:
1669863Snilay@cs.wisc.edu        Network *ctr;
1679863Snilay@cs.wisc.edu
1689863Snilay@cs.wisc.edu      public:
1699863Snilay@cs.wisc.edu        virtual ~StatsCallback() {}
1709863Snilay@cs.wisc.edu
1719863Snilay@cs.wisc.edu        StatsCallback(Network *_ctr)
1729863Snilay@cs.wisc.edu            : ctr(_ctr)
1739863Snilay@cs.wisc.edu        {
1749863Snilay@cs.wisc.edu        }
1759863Snilay@cs.wisc.edu
1769863Snilay@cs.wisc.edu        void process() {ctr->collateStats();}
1779863Snilay@cs.wisc.edu    };
17812065Snikos.nikoleris@arm.com
17912065Snikos.nikoleris@arm.com    // Global address map
18012065Snikos.nikoleris@arm.com    struct AddrMapNode {
18112065Snikos.nikoleris@arm.com        NodeID id;
18212065Snikos.nikoleris@arm.com        AddrRangeList ranges;
18312065Snikos.nikoleris@arm.com    };
18412065Snikos.nikoleris@arm.com    std::unordered_multimap<MachineType, AddrMapNode> addrMap;
1856145Snate@binkert.org};
1866145Snate@binkert.org
1877055Snate@binkert.orginline std::ostream&
1887055Snate@binkert.orgoperator<<(std::ostream& out, const Network& obj)
1896145Snate@binkert.org{
1907054Snate@binkert.org    obj.print(out);
1917055Snate@binkert.org    out << std::flush;
1927054Snate@binkert.org    return out;
1936145Snate@binkert.org}
1946145Snate@binkert.org
1957054Snate@binkert.org#endif // __MEM_RUBY_NETWORK_NETWORK_HH__
196