Debian Codegraph @

himbaechel/uarch/gatemate/route_clock.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2024 The Project Peppercorn Authors.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include <functional>
21 #include <queue>
22
23 #include "gatemate.h"
24 #include "log.h"
25 #include "nextpnr_assertions.h"
26
27 #define HIMBAECHEL_CONSTIDS "uarch/gatemate/constids.inc"
28 #include "himbaechel_constids.h"
29
30 NEXTPNR_NAMESPACE_BEGIN
31
32 namespace {
33
34 struct QueuedWire
35 {
36 explicit QueuedWire(WireId wire, delay_t delay = 0) : wire{wire}, delay{delay} {};
37
38 WireId wire;
39 delay_t delay;
40
41 bool operator>(const QueuedWire &rhs) const { return this->delay > rhs.delay; }
42 };
43
44 } // namespace
45
46 void GateMateImpl::route_clock()
47 {
48 log_info("Routing clock nets...\n");
49 auto rstart = std::chrono::high_resolution_clock::now();
50
51 auto clk_nets = std::vector<NetInfo *>{};
52 auto reserved_wires = dict<WireId, IdString>{};
53
54 auto feeds_clk_port = [&](PortRef &port) {
55 return (ctx->getBelBucketForCellType(port.cell->type) == id_CPE_FF) && port.port.in(id_CLK_INT);
56 };
57
58 auto feeds_ddr_port = [&](NetInfo *net, PortRef &port) {
59 return this->ddr_nets.find(net->name) != this->ddr_nets.end() && port.port == id_D0_10;
60 };
61
62 auto pip_plane = [&](PipId pip) {
63 const auto &extra_data = *pip_extra_data(pip);
64 if (extra_data.type != PipExtra::PIP_EXTRA_MUX)
65 return uint8_t{0};
66 return extra_data.plane;
67 };
68
69 auto reserve = [&](WireId wire, NetInfo *net) {
70 for (auto pip : ctx->getPipsUphill(wire)) {
71 wire = ctx->getPipSrcWire(pip);
72 break;
73 }
74 if (ctx->debug) {
75 auto wire_name = "(uninitialized)";
76 if (wire != WireId())
77 wire_name = ctx->nameOfWire(wire);
78 log_info(" reserving wire %s\n", wire_name);
79 }
80 reserved_wires.insert({wire, net->name});
81 };
82
83 for (auto &net_pair : ctx->nets) {
84 NetInfo *net = net_pair.second.get();
85 if (!net->driver.cell)
86 continue;
87
88 bool is_clk_net = false;
89 for (auto &usr : net->users) {
90 if (feeds_clk_port(usr) || feeds_ddr_port(net, usr)) {
91 is_clk_net = true;
92
93 for (auto clk_sink_wire : ctx->getNetinfoSinkWires(net, usr))
94 reserve(clk_sink_wire, net);
95
96 auto reserve_port_if_needed = [&](IdString port_name) {
97 auto port = usr.cell->ports.find(port_name);
98 if (port != usr.cell->ports.end() && port->second.net != nullptr) {
99 auto sink_wire = ctx->getNetinfoSinkWire(port->second.net,
100 port->second.net->users.at(port->second.user_idx), 0);
101 reserve(sink_wire, port->second.net);
102 }
103 };
104
105 reserve_port_if_needed(id_EN);
106 reserve_port_if_needed(id_SR);
107 }
108 }
109
110 if (is_clk_net)
111 clk_nets.push_back(net);
112 }
113
114 for (auto clk_net : clk_nets) {
115 log_info(" routing net '%s' to %d users\n", clk_net->name.c_str(ctx), clk_net->users.entries());
116 auto src_wire = ctx->getNetinfoSourceWire(clk_net);
117 ctx->bindWire(src_wire, clk_net, STRENGTH_LOCKED);
118
119 auto sink_wires = dict<WireId, PortRef>{};
120 auto sink_wires_to_do = pool<WireId>{};
121 for (auto &usr : clk_net->users) {
122 if (!feeds_clk_port(usr) && !feeds_ddr_port(clk_net, usr))
123 continue;
124
125 auto sink_wire = ctx->getNetinfoSinkWire(clk_net, usr, 0);
126
127 sink_wires.insert({sink_wire, usr});
128 sink_wires_to_do.insert(sink_wire);
129 }
130
131 std::priority_queue<QueuedWire, std::vector<QueuedWire>, std::greater<QueuedWire>> visit;
132 dict<WireId, PipId> backtrace;
133 dict<WireId, delay_t> delay_map;
134
135 auto is_glb_clk = clk_net->driver.cell->type == id_GLBOUT;
136
137 visit.push(QueuedWire(src_wire));
138 while (!visit.empty()) {
139 QueuedWire curr = visit.top();
140 visit.pop();
141 if (sink_wires_to_do.count(curr.wire)) {
142 if (ctx->debug) {
143 auto sink_wire_name = "(uninitialized)";
144 if (curr.wire != WireId())
145 sink_wire_name = ctx->nameOfWire(curr.wire);
146 log_info(" -> %s (%.3fns)\n", sink_wire_name, ctx->getDelayNS(curr.delay));
147 }
148 sink_wires_to_do.erase(curr.wire);
149 if (sink_wires_to_do.empty())
150 break;
151 }
152
153 for (auto dh : ctx->getPipsDownhill(curr.wire)) {
154 const auto &extra_data = *pip_extra_data(dh);
155 // Allow only CINY2->COUTY2 pass through for clock router
156 if (extra_data.type == PipExtra::PIP_EXTRA_MUX && extra_data.resource != 0) {
157 if (!(extra_data.resource == PipMask::C_CY2_I && extra_data.value == 0))
158 continue;
159 }
160 if (!ctx->checkPipAvailForNet(dh, clk_net))
161 continue;
162 WireId dst = ctx->getPipDstWire(dh);
163 if (!ctx->checkWireAvail(dst) && ctx->getBoundWireNet(dst) != clk_net)
164 continue;
165 // Has this wire been reserved for another net?
166 auto reserved = reserved_wires.find(dst);
167 if (reserved != reserved_wires.end() && reserved->second != clk_net->name)
168 continue;
169 auto delay = curr.delay + ctx->getPipDelay(dh).maxDelay() + ctx->getWireDelay(dst).maxDelay() +
170 ctx->getDelayEpsilon();
171 if (backtrace.count(dst) && delay_map.at(dst) <= delay)
172 continue;
173 delay_map[dst] = delay;
174 backtrace[dst] = dh;
175 visit.push(QueuedWire(dst, delay));
176 }
177 }
178 for (auto sink_wire : sink_wires_to_do) {
179 log_info(" failed to find a route using dedicated resources. %s -> %s\n",
180 clk_net->driver.cell->name.c_str(ctx), ctx->nameOfWire(sink_wire));
181 }
182 for (auto pair : sink_wires) {
183 auto sink_wire = pair.first;
184 auto usr = pair.second;
185 auto src = sink_wire;
186
187 if (ctx->debug) {
188 auto sink_wire_name = "(uninitialized)";
189 if (sink_wire != WireId())
190 sink_wire_name = ctx->nameOfWire(sink_wire);
191 log_info(" routing arc to %s.%s (wire %s):\n", usr.cell->name.c_str(ctx), usr.port.c_str(ctx),
192 sink_wire_name);
193 }
194
195 while (backtrace.count(src)) {
196 auto uh = backtrace[src];
197 if (ctx->getBoundWireNet(src) == clk_net) {
198 if (ctx->debug)
199 log_info(" pip %s --> %s (plane %hhd)\n", ctx->nameOfPip(uh),
200 ctx->nameOfWire(src), pip_plane(uh));
201 } else if (ctx->getBoundWireNet(src) == nullptr) {
202 if (ctx->debug)
203 log_info(" bind pip %s --> %s (plane %hhd)\n", ctx->nameOfPip(uh),
204 ctx->nameOfWire(src), pip_plane(uh));
205 ctx->bindPip(uh, clk_net, is_glb_clk ? STRENGTH_LOCKED : STRENGTH_WEAK);
206 } else {
207 log_error("Can't bind pip %s because wire %s is already bound.\n", ctx->nameOfPip(uh),
208 ctx->nameOfWire(src));
209 }
210 if (src == src_wire)
211 break;
212 src = ctx->getPipSrcWire(uh);
213 }
214 }
215 }
216 auto rend = std::chrono::high_resolution_clock::now();
217 log_info("Clock router time %.02fs.\n", std::chrono::duration<float>(rend - rstart).count());
218 }
219
220 NEXTPNR_NAMESPACE_END