1# Copyright (c) 2016 Georgia Institute of Technology
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Tushar Krishna
28
29from __future__ import print_function
30from __future__ import absolute_import
31
32import math
33import m5
34from m5.objects import *
35from m5.defines import buildEnv
36from m5.util import addToPath, fatal
37
38def define_options(parser):
39    # By default, ruby uses the simple timing cpu
40    parser.set_defaults(cpu_type="TimingSimpleCPU")
41
42    parser.add_option("--topology", type="string", default="Crossbar",
43                      help="check configs/topologies for complete set")
44    parser.add_option("--mesh-rows", type="int", default=0,
45                      help="the number of rows in the mesh topology")
46    parser.add_option("--network", type="choice", default="simple",
47                      choices=['simple', 'garnet2.0'],
48                      help="'simple'|'garnet2.0'")
49    parser.add_option("--router-latency", action="store", type="int",
50                      default=1,
51                      help="""number of pipeline stages in the garnet router.
52                            Has to be >= 1.
53                            Can be over-ridden on a per router basis
54                            in the topology file.""")
55    parser.add_option("--link-latency", action="store", type="int", default=1,
56                      help="""latency of each link the simple/garnet networks.
57                            Has to be >= 1.
58                            Can be over-ridden on a per link basis
59                            in the topology file.""")
60    parser.add_option("--link-width-bits", action="store", type="int",
61                      default=128,
62                      help="width in bits for all links inside garnet.")
63    parser.add_option("--vcs-per-vnet", action="store", type="int", default=4,
64                      help="""number of virtual channels per virtual network
65                            inside garnet network.""")
66    parser.add_option("--routing-algorithm", action="store", type="int",
67                      default=0,
68                      help="""routing algorithm in network.
69                            0: weight-based table
70                            1: XY (for Mesh. see garnet2.0/RoutingUnit.cc)
71                            2: Custom (see garnet2.0/RoutingUnit.cc""")
72    parser.add_option("--network-fault-model", action="store_true",
73                      default=False,
74                      help="""enable network fault model:
75                            see src/mem/ruby/network/fault_model/""")
76    parser.add_option("--garnet-deadlock-threshold", action="store",
77                      type="int", default=50000,
78                      help="network-level deadlock threshold.")
79
80
81def create_network(options, ruby):
82
83    # Set the network classes based on the command line options
84    if options.network == "garnet2.0":
85        NetworkClass = GarnetNetwork
86        IntLinkClass = GarnetIntLink
87        ExtLinkClass = GarnetExtLink
88        RouterClass = GarnetRouter
89        InterfaceClass = GarnetNetworkInterface
90
91    else:
92        NetworkClass = SimpleNetwork
93        IntLinkClass = SimpleIntLink
94        ExtLinkClass = SimpleExtLink
95        RouterClass = Switch
96        InterfaceClass = None
97
98    # Instantiate the network object
99    # so that the controllers can connect to it.
100    network = NetworkClass(ruby_system = ruby, topology = options.topology,
101            routers = [], ext_links = [], int_links = [], netifs = [])
102
103    return (network, IntLinkClass, ExtLinkClass, RouterClass, InterfaceClass)
104
105def init_network(options, network, InterfaceClass):
106
107    if options.network == "garnet2.0":
108        network.num_rows = options.mesh_rows
109        network.vcs_per_vnet = options.vcs_per_vnet
110        network.ni_flit_size = options.link_width_bits / 8
111        network.routing_algorithm = options.routing_algorithm
112        network.garnet_deadlock_threshold = options.garnet_deadlock_threshold
113
114    if options.network == "simple":
115        network.setup_buffers()
116
117    if InterfaceClass != None:
118        netifs = [InterfaceClass(id=i) \
119                  for (i,n) in enumerate(network.ext_links)]
120        network.netifs = netifs
121
122    if options.network_fault_model:
123        assert(options.network == "garnet2.0")
124        network.enable_fault_model = True
125        network.fault_model = FaultModel()
126