mem-garnet: Integration of HeteroGarnet
[gem5.git] / src / mem / ruby / network / garnet2.0 / NetworkLink.cc
1 /*
2 * Copyright (c) 2020 Advanced Micro Devices, Inc.
3 * Copyright (c) 2020 Inria
4 * Copyright (c) 2016 Georgia Institute of Technology
5 * Copyright (c) 2008 Princeton University
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 "mem/ruby/network/garnet2.0/NetworkLink.hh"
34
35 #include "base/trace.hh"
36 #include "debug/RubyNetwork.hh"
37 #include "mem/ruby/network/garnet2.0/CreditLink.hh"
38
39 NetworkLink::NetworkLink(const Params *p)
40 : ClockedObject(p), Consumer(this), m_id(p->link_id),
41 m_type(NUM_LINK_TYPES_),
42 m_latency(p->link_latency), m_link_utilized(0),
43 m_vc_load(p->vcs_per_vnet * p->virt_nets),
44 linkBuffer(), link_consumer(nullptr),
45 link_srcQueue(nullptr)
46 {
47 int num_vnets = (p->supported_vnets).size();
48 assert(num_vnets > 0);
49 mVnets.resize(num_vnets);
50 bitWidth = p->width;
51 for (int i = 0; i < num_vnets; i++) {
52 mVnets[i] = p->supported_vnets[i];
53 }
54 DPRINTF(RubyNetwork,"Created with bitwidth:%d\n", bitWidth);
55 }
56
57 void
58 NetworkLink::setLinkConsumer(Consumer *consumer)
59 {
60 link_consumer = consumer;
61 }
62
63 void
64 NetworkLink::setSourceQueue(flitBuffer *src_queue, ClockedObject *srcClockObj)
65 {
66 link_srcQueue = src_queue;
67 src_object = srcClockObj;
68 }
69
70 void
71 NetworkLink::wakeup()
72 {
73 DPRINTF(RubyNetwork, "Woke up to transfer flits from %s\n",
74 src_object->name());
75 assert(link_srcQueue != nullptr);
76 assert(curTick() == clockEdge());
77 if (link_srcQueue->isReady(curTick())) {
78 flit *t_flit = link_srcQueue->getTopFlit();
79 DPRINTF(RubyNetwork, "Transmission will finish at %ld :%s\n",
80 clockEdge(m_latency), *t_flit);
81 if (m_type != NUM_LINK_TYPES_) {
82 // Only for assertions and debug messages
83 assert(t_flit->m_width == bitWidth);
84 assert((std::find(mVnets.begin(), mVnets.end(),
85 t_flit->get_vnet()) != mVnets.end()) ||
86 (mVnets.size() == 0));
87 }
88 t_flit->set_time(clockEdge(m_latency));
89 linkBuffer.insert(t_flit);
90 link_consumer->scheduleEventAbsolute(clockEdge(m_latency));
91 m_link_utilized++;
92 m_vc_load[t_flit->get_vc()]++;
93 }
94
95 if (!link_srcQueue->isEmpty()) {
96 scheduleEvent(Cycles(1));
97 }
98 }
99
100 void
101 NetworkLink::resetStats()
102 {
103 for (int i = 0; i < m_vc_load.size(); i++) {
104 m_vc_load[i] = 0;
105 }
106
107 m_link_utilized = 0;
108 }
109
110 NetworkLink *
111 NetworkLinkParams::create()
112 {
113 return new NetworkLink(this);
114 }
115
116 CreditLink *
117 CreditLinkParams::create()
118 {
119 return new CreditLink(this);
120 }
121
122 uint32_t
123 NetworkLink::functionalWrite(Packet *pkt)
124 {
125 return linkBuffer.functionalWrite(pkt);
126 }