1/*****************************************************************************
2 *                                McPAT
3 *                      SOFTWARE LICENSE AGREEMENT
4 *            Copyright 2012 Hewlett-Packard Development Company, L.P.
5 *            Copyright (c) 2010-2013 Advanced Micro Devices, Inc.
6 *                          All Rights Reserved
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer;
12 * redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution;
15 * neither the name of the copyright holders nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 ***************************************************************************/
32
33#include <algorithm>
34#include <cassert>
35#include <cmath>
36#include <iostream>
37#include <string>
38
39#include "basic_circuit.h"
40#include "common.h"
41#include "const.h"
42#include "io.h"
43#include "noc.h"
44#include "parameter.h"
45
46OnChipNetwork::OnChipNetwork(XMLNode* _xml_data, int ithNoC_,
47                             InputParameter* interface_ip_)
48    : McPATComponent(_xml_data), router(NULL), link_bus(NULL), ithNoC(ithNoC_),
49      interface_ip(*interface_ip_), link_bus_exist(false),
50      router_exist(false) {
51    name = "On-Chip Network";
52    set_param_stats();
53    local_result = init_interface(&interface_ip, name);
54    scktRatio = g_tp.sckt_co_eff;
55
56    // TODO: Routers and links should be children of the NOC component
57    if (noc_params.type) {
58        init_router();
59    } else {
60        init_link_bus();
61    }
62}
63
64void OnChipNetwork::init_router() {
65    router = new Router(noc_params.flit_size,
66                        noc_params.virtual_channel_per_port *
67                        noc_params.input_buffer_entries_per_vc,
68                        noc_params.virtual_channel_per_port,
69                        &(g_tp.peri_global),
70                        noc_params.input_ports, noc_params.output_ports,
71                        noc_params.M_traffic_pattern);
72    // TODO: Make a router class within McPAT that descends from McPATComponent
73    // children.push_back(router);
74    area.set_area(area.get_area() + router->area.get_area() *
75                  noc_params.total_nodes);
76
77    double long_channel_device_reduction = longer_channel_device_reduction(Uncore_device);
78    router->power.readOp.longer_channel_leakage          = router->power.readOp.leakage * long_channel_device_reduction;
79    router->buffer.power.readOp.longer_channel_leakage   = router->buffer.power.readOp.leakage * long_channel_device_reduction;
80    router->crossbar.power.readOp.longer_channel_leakage = router->crossbar.power.readOp.leakage * long_channel_device_reduction;
81    router->arbiter.power.readOp.longer_channel_leakage  = router->arbiter.power.readOp.leakage * long_channel_device_reduction;
82    router_exist = true;
83}
84
85void OnChipNetwork::init_link_bus() {
86    if (noc_params.type) {
87        link_name = "Links";
88    } else {
89        link_name = "Bus";
90    }
91
92    interface_ip.throughput = noc_params.link_throughput /
93        noc_params.clockRate;
94    interface_ip.latency = noc_params.link_latency / noc_params.clockRate;
95
96    link_len /= (noc_params.horizontal_nodes + noc_params.vertical_nodes) / 2;
97
98    if (noc_params.total_nodes > 1) {
99        //All links are shared by neighbors
100        link_len /= 2;
101    }
102    link_bus = new Interconnect(xml_data, "Link", Uncore_device,
103                                noc_params.link_base_width,
104                                noc_params.link_base_height,
105                                noc_params.flit_size, link_len, &interface_ip,
106                                noc_params.link_start_wiring_level,
107                                noc_params.clockRate, true/*pipelinable*/,
108                                noc_params.route_over_perc);
109    children.push_back(link_bus);
110
111    link_bus_exist = true;
112}
113
114// TODO: This should use the McPATComponent::computeEnergy function to
115// recursively calculate energy of routers and links and then add
116void OnChipNetwork::computeEnergy() {
117    double pppm_t[4]    = {1, 1, 1, 1};
118
119    // Initialize stats for TDP
120    tdp_stats.reset();
121    tdp_stats.readAc.access = noc_stats.duty_cycle;
122    if (router_exist) {
123        // TODO: Define a regression to exercise routers
124        // TODO: Clean this up: it is too invasive and breaks abstraction
125        set_pppm(pppm_t, 1 * tdp_stats.readAc.access, 1, 1, 1);
126        router->power = router->power * pppm_t;
127        set_pppm(pppm_t, noc_params.total_nodes,
128                noc_params.total_nodes,
129                noc_params.total_nodes,
130                noc_params.total_nodes);
131    }
132    if (link_bus_exist) {
133        if (noc_params.type) {
134            link_bus->int_params.active_ports = noc_params.min_ports - 1;
135        } else {
136            link_bus->int_params.active_ports = noc_params.min_ports;
137        }
138        link_bus->int_stats.duty_cycle =
139            noc_params.M_traffic_pattern * noc_stats.duty_cycle;
140
141        // TODO: Decide how to roll multiple routers into a single top-level
142        // NOC module. I would prefer not to, but it might be a nice feature
143        set_pppm(pppm_t, noc_params.total_nodes,
144                 noc_params.total_nodes,
145                 noc_params.total_nodes,
146                 noc_params.total_nodes);
147    }
148
149    // Initialize stats for runtime energy and power
150    rtp_stats.reset();
151    rtp_stats.readAc.access = noc_stats.total_access;
152    set_pppm(pppm_t, 1, 0 , 0, 0);
153    if (router_exist) {
154        // TODO: Move this to a McPATComponent parent class of Router
155        router->buffer.rt_power.readOp.dynamic =
156            (router->buffer.power.readOp.dynamic +
157             router->buffer.power.writeOp.dynamic) * rtp_stats.readAc.access;
158        router->crossbar.rt_power.readOp.dynamic =
159            router->crossbar.power.readOp.dynamic * rtp_stats.readAc.access;
160        router->arbiter.rt_power.readOp.dynamic =
161            router->arbiter.power.readOp.dynamic * rtp_stats.readAc.access;
162
163        router->rt_power = router->rt_power +
164            (router->buffer.rt_power + router->crossbar.rt_power +
165             router->arbiter.rt_power) * pppm_t +
166            router->power * pppm_lkg;//TDP power must be calculated first!
167    }
168    if (link_bus_exist) {
169        link_bus->int_stats.accesses = noc_stats.total_access;
170    }
171
172    // Recursively compute energy
173    McPATComponent::computeEnergy();
174}
175
176void OnChipNetwork::set_param_stats() {
177    // TODO: Remove this or move initialization elsewhere
178    memset(&noc_params, 0, sizeof(OnChipNetworkParameters));
179
180    int num_children = xml_data->nChildNode("param");
181    int i;
182    int mat_type;
183    for (i = 0; i < num_children; i++) {
184        XMLNode* paramNode = xml_data->getChildNodePtr("param", &i);
185        XMLCSTR node_name = paramNode->getAttribute("name");
186        XMLCSTR value = paramNode->getAttribute("value");
187
188        if (!node_name)
189            warnMissingParamName(paramNode->getAttribute("id"));
190
191        ASSIGN_INT_IF("type", noc_params.type);
192        ASSIGN_FP_IF("clockrate", noc_params.clockRate);
193        ASSIGN_INT_IF("flit_bits", noc_params.flit_size);
194        ASSIGN_FP_IF("link_len", link_len);
195        ASSIGN_FP_IF("link_throughput", noc_params.link_throughput);
196        ASSIGN_FP_IF("link_latency", noc_params.link_latency);
197        ASSIGN_INT_IF("input_ports", noc_params.input_ports);
198        ASSIGN_INT_IF("output_ports", noc_params.output_ports);
199        ASSIGN_INT_IF("global_linked_ports", noc_params.global_linked_ports);
200        ASSIGN_INT_IF("horizontal_nodes", noc_params.horizontal_nodes);
201        ASSIGN_INT_IF("vertical_nodes", noc_params.vertical_nodes);
202        ASSIGN_FP_IF("chip_coverage", noc_params.chip_coverage);
203        ASSIGN_FP_IF("link_routing_over_percentage",
204                     noc_params.route_over_perc);
205        ASSIGN_INT_IF("has_global_link", noc_params.has_global_link);
206        ASSIGN_INT_IF("virtual_channel_per_port",
207                      noc_params.virtual_channel_per_port);
208        ASSIGN_INT_IF("input_buffer_entries_per_vc",
209                      noc_params.input_buffer_entries_per_vc);
210        ASSIGN_FP_IF("M_traffic_pattern", noc_params.M_traffic_pattern);
211        ASSIGN_FP_IF("link_base_width", noc_params.link_base_width);
212        ASSIGN_FP_IF("link_base_height", noc_params.link_base_height);
213        ASSIGN_INT_IF("link_start_wiring_level",
214                      noc_params.link_start_wiring_level);
215        ASSIGN_INT_IF("wire_mat_type", mat_type);
216        ASSIGN_ENUM_IF("wire_type", interface_ip.wt, Wire_type);
217
218        else {
219            warnUnrecognizedParam(node_name);
220        }
221    }
222
223    // Change from MHz to Hz
224    noc_params.clockRate *= 1e6;
225
226    interface_ip.wire_is_mat_type = mat_type;
227    interface_ip.wire_os_mat_type = mat_type;
228
229    num_children = xml_data->nChildNode("stat");
230    for (i = 0; i < num_children; i++) {
231        XMLNode* statNode = xml_data->getChildNodePtr("stat", &i);
232        XMLCSTR node_name = statNode->getAttribute("name");
233        XMLCSTR value = statNode->getAttribute("value");
234
235        if (!node_name)
236            warnMissingStatName(statNode->getAttribute("id"));
237
238        ASSIGN_FP_IF("duty_cycle", noc_stats.duty_cycle);
239        ASSIGN_FP_IF("total_accesses", noc_stats.total_access);
240
241        else {
242            warnUnrecognizedStat(node_name);
243        }
244    }
245
246    clockRate = noc_params.clockRate;
247    noc_params.min_ports =
248        min(noc_params.input_ports, noc_params.output_ports);
249    if (noc_params.type) {
250        noc_params.global_linked_ports = (noc_params.input_ports - 1) +
251            (noc_params.output_ports - 1);
252    }
253    noc_params.total_nodes =
254        noc_params.horizontal_nodes * noc_params.vertical_nodes;
255
256    assert(noc_params.chip_coverage <= 1);
257    assert(noc_params.route_over_perc <= 1);
258    assert(link_len > 0);
259}
260
261OnChipNetwork ::~OnChipNetwork() {
262
263    if (router) {
264        delete router;
265        router = 0;
266    }
267    if (link_bus) {
268        delete link_bus;
269        link_bus = 0;
270    }
271}
272