1/*
2 * Copyright (c) 2008 Princeton University
3 * Copyright (c) 2016 Georgia Institute of Technology
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Niket Agarwal
30 *          Tushar Krishna
31 */
32
33
34#include "mem/ruby/network/garnet2.0/CrossbarSwitch.hh"
35
36#include "base/stl_helpers.hh"
37#include "debug/RubyNetwork.hh"
38#include "mem/ruby/network/garnet2.0/OutputUnit.hh"
39#include "mem/ruby/network/garnet2.0/Router.hh"
40
41using m5::stl_helpers::deletePointers;
42
43CrossbarSwitch::CrossbarSwitch(Router *router)
44    : Consumer(router)
45{
46    m_router = router;
47    m_num_vcs = m_router->get_num_vcs();
48    m_crossbar_activity = 0;
49}
50
51CrossbarSwitch::~CrossbarSwitch()
52{
53    deletePointers(m_switch_buffer);
54}
55
56void
57CrossbarSwitch::init()
58{
59    m_output_unit = m_router->get_outputUnit_ref();
60
61    m_num_inports = m_router->get_num_inports();
62    m_switch_buffer.resize(m_num_inports);
63    for (int i = 0; i < m_num_inports; i++) {
64        m_switch_buffer[i] = new flitBuffer();
65    }
66}
67
68/*
69 * The wakeup function of the CrossbarSwitch loops through all input ports,
70 * and sends the winning flit (from SA) out of its output port on to the
71 * output link. The output link is scheduled for wakeup in the next cycle.
72 */
73
74void
75CrossbarSwitch::wakeup()
76{
77    DPRINTF(RubyNetwork, "CrossbarSwitch at Router %d woke up "
78            "at time: %lld\n",
79            m_router->get_id(), m_router->curCycle());
80
81    for (int inport = 0; inport < m_num_inports; inport++) {
82        if (!m_switch_buffer[inport]->isReady(m_router->curCycle()))
83            continue;
84
85        flit *t_flit = m_switch_buffer[inport]->peekTopFlit();
86        if (t_flit->is_stage(ST_, m_router->curCycle())) {
87            int outport = t_flit->get_outport();
88
89            // flit performs LT_ in the next cycle
90            t_flit->advance_stage(LT_, m_router->curCycle() + Cycles(1));
91            t_flit->set_time(m_router->curCycle() + Cycles(1));
92
93            // This will take care of waking up the Network Link
94            // in the next cycle
95            m_output_unit[outport]->insert_flit(t_flit);
96            m_switch_buffer[inport]->getTopFlit();
97            m_crossbar_activity++;
98        }
99    }
100}
101
102uint32_t
103CrossbarSwitch::functionalWrite(Packet *pkt)
104{
105   uint32_t num_functional_writes = 0;
106
107   for (uint32_t i = 0; i < m_switch_buffer.size(); ++i) {
108       num_functional_writes += m_switch_buffer[i]->functionalWrite(pkt);
109   }
110
111   return num_functional_writes;
112}
113
114void
115CrossbarSwitch::resetStats()
116{
117    m_crossbar_activity = 0;
118}