Debian Codegraph @

himbaechel/uarch/gowin/globals.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2021 gatecat <[email protected]>
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 "log.h"
21 #include "nextpnr.h"
22 #include "util.h"
23
24 #include <queue>
25
26 #define HIMBAECHEL_CONSTIDS "uarch/gowin/constids.inc"
27 #include "himbaechel_constids.h"
28 #include "himbaechel_helpers.h"
29
30 #include "globals.h"
31 #include "gowin.h"
32 #include "gowin_utils.h"
33
34 NEXTPNR_NAMESPACE_BEGIN
35
36 struct GowinGlobalRouter
37 {
38 Context *ctx;
39 GowinUtils gwu;
40
41 GowinGlobalRouter(Context *ctx) : ctx(ctx) { gwu.init(ctx); };
42
43 bool global_pip_available(PipId pip) const { return gwu.is_global_pip(pip) || ctx->checkPipAvail(pip); };
44
45 bool segment_wire_filter(PipId pip) const { return !gwu.is_segment_pip(pip); }
46
47 // To avoid a cycle where we connect the clock wire to the gate in
48 // the global clock system and it ends up in the global clock MUX
49 // again, we only allow connections from general-purpose wires.
50 bool clock_gate_wire_filter(PipId pip) const
51 {
52 WireId dst = ctx->getPipDstWire(pip);
53 IdString src_type = ctx->getWireType(ctx->getPipSrcWire(pip));
54 return !(gwu.wire_is_clock_gate(dst) && src_type.in(id_GLOBAL_CLK, id_TILE_CLK));
55 }
56
57 bool dcs_input_filter(PipId pip) const
58 {
59 return !ctx->getWireName(ctx->getPipSrcWire(pip))[1].in(
60 id_P16A, id_P16B, id_P16C, id_P16D, id_P17A, id_P17B, id_P17C, id_P17D, id_P26A, id_P26B, id_P26C,
61 id_P26D, id_P27A, id_P27B, id_P27C, id_P27D, id_P36A, id_P36B, id_P36C, id_P36D, id_P37A, id_P37B,
62 id_P37C, id_P37D, id_P46A, id_P46B, id_P46C, id_P46D, id_P47A, id_P47B, id_P47C, id_P47D);
63 }
64
65 // allow io->global, global->global and global->tile clock
66 bool global_pip_filter(PipId pip, WireId src_wire) const
67 {
68 auto is_local = [&](IdString wire_type) {
69 return !wire_type.in(id_GLOBAL_CLK, id_IO_O, id_IO_I, id_PLL_O, id_PLL_I, id_TILE_CLK);
70 };
71 WireId src, dst;
72 src = ctx->getPipSrcWire(pip);
73 dst = ctx->getPipDstWire(pip);
74 IdString dst_name = ctx->getWireName(dst)[1];
75 bool not_dcs_pip = dst_name != id_CLKOUT;
76 IdString src_type = ctx->getWireType(src);
77 IdString dst_type = ctx->getWireType(dst);
78 bool src_is_outpin = src_type.in(id_IO_O, id_PLL_O, id_HCLK, id_DLLDLY, id_OSCOUT);
79 bool src_valid = not_dcs_pip && (src_type == id_GLOBAL_CLK || src_is_outpin);
80 bool dst_valid = not_dcs_pip && dst_type.in(id_GLOBAL_CLK, id_TILE_CLK, id_PLL_I, id_PLL_O, id_IO_I, id_HCLK);
81
82 bool res;
83 if (src == src_wire && (src_type == id_PLL_O || (!src_is_outpin))) {
84 bool dst_is_spine = dst_name.str(ctx).rfind("SPINE", 0) == 0;
85 res = src_valid && (dst_is_spine || dst_type == id_PLL_O);
86 } else {
87 res = (src_valid && dst_valid) || (src_valid && is_local(dst_type)) || (is_local(src_type) && dst_valid);
88 }
89 if (ctx->debug && false /*&& res*/) {
90 log_info("%s <- %s [%s <- %s]\n", ctx->nameOfWire(ctx->getPipDstWire(pip)),
91 ctx->nameOfWire(ctx->getPipSrcWire(pip)), dst_type.c_str(ctx), src_type.c_str(ctx));
92 log_info(" res:%d, src_valid:%d, dst_valid:%d, src local:%d, dst local:%d, dst gate:%d\n", res, src_valid,
93 dst_valid, is_local(src_type), is_local(dst_type), gwu.wire_is_clock_gate(dst));
94 }
95 return res;
96 }
97
98 bool global_DQCE_pip_filter(PipId pip, WireId src_wire) const
99 {
100 auto is_local = [&](IdString wire_type) {
101 return !wire_type.in(id_GLOBAL_CLK, id_IO_O, id_IO_I, id_PLL_O, id_PLL_I, id_TILE_CLK);
102 };
103 auto is_dcs_input = [&](IdString wire_name) {
104 return wire_name.in(id_P16A, id_P16B, id_P16C, id_P16D, id_P17A, id_P17B, id_P17C, id_P17D, id_P26A,
105 id_P26B, id_P26C, id_P26D, id_P27A, id_P27B, id_P27C, id_P27D, id_P36A, id_P36B,
106 id_P36C, id_P36D, id_P37A, id_P37B, id_P37C, id_P37D, id_P46A, id_P46B, id_P46C,
107 id_P46D, id_P47A, id_P47B, id_P47C, id_P47D);
108 };
109
110 WireId src, dst;
111 src = ctx->getPipSrcWire(pip);
112 dst = ctx->getPipDstWire(pip);
113 IdString src_name = ctx->getWireName(dst)[1];
114 IdString dst_name = ctx->getWireName(dst)[1];
115 bool not_dcs_pip = dst_name != id_CLKOUT && !is_dcs_input(src_name);
116 IdString src_type = ctx->getWireType(src);
117 IdString dst_type = ctx->getWireType(dst);
118 bool src_valid = not_dcs_pip && src_type.in(id_GLOBAL_CLK, id_IO_O, id_PLL_O, id_HCLK);
119 bool dst_valid = not_dcs_pip && dst_type.in(id_GLOBAL_CLK, id_TILE_CLK, id_PLL_I, id_IO_I, id_HCLK);
120
121 // If DQCE is used, then the source can only connect to SPINEs as only they can be switched off/on.
122 bool res;
123 if (src == src_wire) {
124 bool dst_is_spine = (dst_name.str(ctx).rfind("SPINE", 0) == 0 || dst_name.str(ctx).rfind("PCLK", 0) == 0 ||
125 dst_name.str(ctx).rfind("LWSPINE", 0) == 0);
126 res = src_valid && dst_is_spine;
127 } else {
128 res = (src_valid && dst_valid) || (src_valid && is_local(dst_type)) || (is_local(src_type) && dst_valid);
129 }
130 if (ctx->debug && false /*res*/) {
131 log_info("%s <- %s [%s <- %s]\n", ctx->nameOfWire(ctx->getPipDstWire(pip)),
132 ctx->nameOfWire(ctx->getPipSrcWire(pip)), dst_type.c_str(ctx), src_type.c_str(ctx));
133 log_info(" res:%d, src_valid:%d, dst_valid:%d, src local:%d, dst local:%d\n", res, src_valid, dst_valid,
134 is_local(src_type), is_local(dst_type));
135 }
136 return res;
137 }
138
139 bool global_DCS_pip_filter(PipId pip, WireId src_wire) const
140 {
141 auto is_local = [&](IdString wire_type) {
142 return !wire_type.in(id_GLOBAL_CLK, id_IO_O, id_IO_I, id_PLL_O, id_PLL_I, id_TILE_CLK);
143 };
144
145 WireId src = ctx->getPipSrcWire(pip);
146 IdString src_type = ctx->getWireType(src);
147 IdString src_name = ctx->getWireName(src)[1];
148 bool src_is_spine = src_name.str(ctx).rfind("SPINE", 0) == 0;
149 IdString dst_type = ctx->getWireType(ctx->getPipDstWire(pip));
150 bool src_valid = ((!src_is_spine) && src_type.in(id_GLOBAL_CLK, id_IO_O, id_PLL_O, id_HCLK)) ||
151 src_name.in(id_SPINE6, id_SPINE7, id_SPINE14, id_SPINE15, id_SPINE22, id_SPINE23, id_SPINE30,
152 id_SPINE31);
153 bool dst_valid = dst_type.in(id_GLOBAL_CLK, id_TILE_CLK, id_PLL_I, id_PLL_O, id_IO_I, id_HCLK);
154
155 bool res = (src_valid && dst_valid) || (src_valid && is_local(dst_type)) || (is_local(src_type) && dst_valid);
156 if (ctx->debug && false /*&& res*/) {
157 log_info("%s <- %s [%s <- %s]\n", ctx->getWireName(ctx->getPipDstWire(pip)).str(ctx).c_str(),
158 ctx->getWireName(ctx->getPipSrcWire(pip)).str(ctx).c_str(), dst_type.c_str(ctx),
159 src_type.c_str(ctx));
160 log_info("res:%d, src_valid:%d, dst_valid:%d, src local:%d, dst local:%d\n", res, src_valid, dst_valid,
161 is_local(src_type), is_local(dst_type));
162 }
163 return res;
164 }
165
166 bool is_relaxed_sink(const PortRef &sink) const { return false; }
167
168 // Dedicated backwards BFS routing for global networks
169 template <typename Tfilt>
170 bool backwards_bfs_route(NetInfo *net, WireId src, WireId dst, int iter_limit, bool strict, Tfilt pip_filter,
171 std::vector<PipId> *path = nullptr)
172 {
173 // Queue of wires to visit
174 std::queue<WireId> visit;
175 // Wire -> upstream pip
176 dict<WireId, PipId> backtrace;
177
178 if (src == dst) {
179 // Nothing more to do
180 return true;
181 }
182
183 visit.push(dst);
184 backtrace[dst] = PipId();
185
186 int iter = 0;
187
188 while (!visit.empty() && (iter++ < iter_limit)) {
189 WireId cursor = visit.front();
190 visit.pop();
191 // Search uphill pips
192 for (PipId pip : ctx->getPipsUphill(cursor)) {
193 // Skip pip if unavailable, and not because it's already used for this net
194 if (!global_pip_available(pip) && ctx->getBoundPipNet(pip) != net) {
195 continue;
196 }
197 WireId prev = ctx->getPipSrcWire(pip);
198 // Ditto for the upstream wire
199 if (!ctx->checkWireAvail(prev) && ctx->getBoundWireNet(prev) != net) {
200 continue;
201 }
202 // Skip already visited wires
203 if (backtrace.count(prev)) {
204 continue;
205 }
206 // Apply our custom pip filter
207 if (!pip_filter(pip, src)) {
208 continue;
209 }
210
211 // Add to the queue
212 visit.push(prev);
213 backtrace[prev] = pip;
214 // Check if we are done yet
215 if (prev == src) {
216 goto done;
217 }
218 }
219 if (false) {
220 done:
221 break;
222 }
223 }
224
225 if (backtrace.count(src)) {
226 WireId cursor = src;
227 std::vector<PipId> pips;
228 // Create a list of pips on the routed path
229 while (true) {
230 PipId pip = backtrace.at(cursor);
231 if (pip == PipId()) {
232 break;
233 }
234 pips.push_back(pip);
235 cursor = ctx->getPipDstWire(pip);
236 }
237 // Reverse that list
238 std::reverse(pips.begin(), pips.end());
239 // Bind pips until we hit already-bound routing
240 for (PipId pip : pips) {
241 WireId dst = ctx->getPipDstWire(pip);
242 if (ctx->getBoundWireNet(dst) == net) {
243 break;
244 }
245 ctx->bindPip(pip, net, STRENGTH_LOCKED);
246 if (path != nullptr) {
247 path->push_back(pip);
248 }
249 }
250 return true;
251 } else {
252 if (strict) {
253 log_error("Failed to route net '%s' from %s to %s using dedicated routing.\n", ctx->nameOf(net),
254 ctx->nameOfWire(src), ctx->nameOfWire(dst));
255 } else {
256 log_warning("Failed to route net '%s' from %s to %s using dedicated routing.\n", ctx->nameOf(net),
257 ctx->nameOfWire(src), ctx->nameOfWire(dst));
258 return false;
259 }
260 }
261 }
262
263 enum RouteResult
264 {
265 NOT_ROUTED = 0,
266 ROUTED_PARTIALLY,
267 ROUTED_ALL
268 };
269
270 template <typename Tfilter>
271 RouteResult route_direct_net(NetInfo *net, Tfilter pip_filter, WireId aux_src = WireId(),
272 std::vector<PipId> *path = nullptr)
273 {
274 WireId src;
275 src = aux_src == WireId() ? ctx->getNetinfoSourceWire(net) : aux_src;
276 if (src == WireId()) {
277 log_error("Net '%s' has an invalid source port %s.%s\n", ctx->nameOf(net), ctx->nameOf(net->driver.cell),
278 ctx->nameOf(net->driver.port));
279 }
280
281 if (aux_src == WireId() && ctx->getBoundWireNet(src) != net) {
282 ctx->bindWire(src, net, STRENGTH_LOCKED);
283 }
284
285 RouteResult routed = NOT_ROUTED;
286 for (auto usr : net->users) {
287 WireId dst = ctx->getNetinfoSinkWire(net, usr, 0);
288 if (dst == WireId()) {
289 log_error("Net '%s' has an invalid sink port %s.%s\n", ctx->nameOf(net), ctx->nameOf(usr.cell),
290 ctx->nameOf(usr.port));
291 }
292 bool bfs_res;
293 bfs_res = backwards_bfs_route(
294 net, src, dst, 1000000, false,
295 [&](PipId pip, WireId src_wire) { return (is_relaxed_sink(usr) || pip_filter(pip, src)); }, path);
296 if (bfs_res) {
297 routed = routed == ROUTED_PARTIALLY ? routed : ROUTED_ALL;
298 } else {
299 routed = routed == NOT_ROUTED ? routed : ROUTED_PARTIALLY;
300 }
301 }
302 if (routed == NOT_ROUTED) {
303 if (aux_src == WireId()) {
304 ctx->unbindWire(src);
305 }
306 }
307 return routed;
308 }
309
310 void route_dqce_net(NetInfo *net)
311 {
312 // route net after dqce using source of CLKIN net
313 CellInfo *dqce_ci = net->driver.cell;
314
315 NetInfo *net_before_dqce = dqce_ci->getPort(id_CLKIN);
316 NPNR_ASSERT(net_before_dqce != nullptr);
317
318 PortRef driver = net_before_dqce->driver;
319 NPNR_ASSERT_MSG(gwu.driver_is_buf(driver) || gwu.driver_is_clksrc(driver),
320 stringf("The input source for %s is not a clock.", ctx->nameOf(dqce_ci)).c_str());
321 WireId src;
322 // use BUF input if there is one
323 if (gwu.driver_is_buf(driver)) {
324 src = ctx->getBelPinWire(driver.cell->bel, id_I);
325 } else {
326 src = ctx->getBelPinWire(driver.cell->bel, driver.port);
327 }
328
329 RouteResult route_result = route_direct_net(
330 net,
331 [&](PipId pip, WireId src_wire) {
332 return global_DQCE_pip_filter(pip, src) && segment_wire_filter(pip) && dcs_input_filter(pip);
333 },
334 src);
335 if (route_result == NOT_ROUTED) {
336 log_error("Can't route the %s network.\n", ctx->nameOf(net));
337 }
338 if (route_result == ROUTED_PARTIALLY) {
339 log_error("It was not possible to completely route the %s net using only global resources. This is not "
340 "allowed for DQCE managed networks.\n",
341 ctx->nameOf(net));
342 }
343
344 // In networks controlled by DQCE, the source can only connect to the
345 // "spine" wires. Here we not only check this fact, but also find out
346 // how many and what kind of "spine" wires were used for network
347 // roaming.
348 for (PipId pip : ctx->getPipsDownhill(src)) {
349 if (ctx->getBoundPipNet(pip) == nullptr) {
350 continue;
351 }
352 WireId dst = ctx->getPipDstWire(pip);
353 IdString dst_name = ctx->getWireName(dst)[1];
354 if (dst_name.str(ctx).rfind("PCLK", 0) == 0 || dst_name.str(ctx).rfind("LWSPINE", 0) == 0) {
355 // step over dummy pip
356 for (PipId next_pip : ctx->getPipsDownhill(dst)) {
357 if (ctx->getBoundPipNet(next_pip) != nullptr) {
358 ctx->unbindPip(pip);
359 src = dst;
360 break;
361 }
362 }
363 if (src == dst) {
364 break;
365 }
366 }
367 }
368 for (PipId pip : ctx->getPipsDownhill(src)) {
369 if (ctx->getBoundPipNet(pip) == nullptr) {
370 continue;
371 }
372 WireId dst = ctx->getPipDstWire(pip);
373 BelId dqce_bel = gwu.get_dqce_bel(ctx->getWireName(dst)[1]);
374 NPNR_ASSERT(dqce_bel != BelId());
375
376 // One pseudo DQCE (either logical or custom, whatever you like)
377 // can be implemented as several hardware dqce - this is because
378 // each hardware dqce can control only one "spine", that is, a bus
379 // within one quadrant. Here we find suitable hardware dqces.
380 CellInfo *hw_dqce = ctx->getBoundBelCell(dqce_bel);
381 if (ctx->debug) {
382 log_info(" use %s spine and %s bel for '%s' hw cell.\n", ctx->nameOfWire(dst),
383 ctx->nameOfBel(dqce_bel), ctx->nameOf(hw_dqce));
384 }
385
386 hw_dqce->setAttr(id_DQCE_PIP, Property(ctx->getPipName(pip).str(ctx)));
387 ctx->unbindPip(pip);
388 ctx->bindWire(dst, net, STRENGTH_LOCKED);
389
390 // The control network must connect the CE inputs of all hardware dqces.
391 dqce_ci->copyPortTo(id_CE, hw_dqce, id_CE);
392 }
393 net->driver.cell->disconnectPort(net->driver.port);
394
395 // remove the virtual DQCE
396 dqce_ci->disconnectPort(id_CLKIN);
397 dqce_ci->disconnectPort(id_CE);
398 ctx->cells.erase(dqce_ci->name);
399 }
400
401 void route_dcs_net(NetInfo *net)
402 {
403 IdString dcs_clock_input_prefix = gwu.get_dcs_prefix();
404 const char *dcs_clock_input_prefix_str = dcs_clock_input_prefix.c_str(ctx);
405 // Since CLKOUT is responsible for only one quadrant, we will do
406 // routing not from it, but from any CLK0-3 input actually connected to
407 // the clock source.
408 CellInfo *dcs_ci = net->driver.cell;
409 NetInfo *net_before_dcs;
410 PortRef driver;
411 for (int i = 0; i < 4; ++i) {
412 net_before_dcs = dcs_ci->getPort(ctx->idf("%s%d", dcs_clock_input_prefix_str, i));
413 if (net_before_dcs == nullptr) {
414 continue;
415 }
416 driver = net_before_dcs->driver;
417 if (gwu.driver_is_buf(driver) || gwu.driver_is_clksrc(driver)) {
418 break;
419 }
420 net_before_dcs = nullptr;
421 }
422 NPNR_ASSERT_MSG(net_before_dcs != nullptr, stringf("No clock inputs for %s.", ctx->nameOf(dcs_ci)).c_str());
423
424 WireId src;
425 // use BUF input if there is one
426 if (gwu.driver_is_buf(driver)) {
427 src = ctx->getBelPinWire(driver.cell->bel, id_I);
428 } else {
429 src = ctx->getBelPinWire(driver.cell->bel, driver.port);
430 }
431
432 RouteResult route_result = route_direct_net(
433 net,
434 [&](PipId pip, WireId src_wire) { return global_DCS_pip_filter(pip, src) && segment_wire_filter(pip); },
435 src);
436 if (route_result == NOT_ROUTED) {
437 log_error("Can't route the %s network.\n", ctx->nameOf(net));
438 }
439 if (route_result == ROUTED_PARTIALLY) {
440 log_error("It was not possible to completely route the %s net using only global resources. This is not "
441 "allowed for DCS managed networks.\n",
442 ctx->nameOf(net));
443 }
444
445 // In networks controlled by DCS, the source can only connect to the
446 // "spine" wires. Here we not only check this fact, but also find out
447 // how many and what kind of "spine" wires were used for network
448 // roaming.
449 for (PipId pip : ctx->getPipsDownhill(src)) {
450 if (ctx->getBoundPipNet(pip) == nullptr) {
451 continue;
452 }
453 WireId dst = ctx->getPipDstWire(pip);
454 IdString dst_name = ctx->getWireName(dst)[1];
455 if (dst_name.str(ctx).rfind("PCLK", 0) == 0 || dst_name.str(ctx).rfind("LWSPINE", 0) == 0 ||
456 dst_name.str(ctx).rfind("PLL") == 0) {
457 // step over dummy pip
458 for (PipId next_pip : ctx->getPipsDownhill(dst)) {
459 if (ctx->getBoundPipNet(next_pip) != nullptr) {
460 src = dst;
461 break;
462 }
463 }
464 if (src == dst) {
465 break;
466 }
467 }
468 }
469 for (PipId pip : ctx->getPipsDownhill(src)) {
470 if (ctx->getBoundPipNet(pip) == nullptr) {
471 continue;
472 }
473 WireId dst = ctx->getPipDstWire(pip);
474 BelId dcs_bel = gwu.get_dcs_bel(ctx->getWireName(dst)[1]);
475 NPNR_ASSERT(dcs_bel != BelId());
476
477 // One pseudo DCS (either logical or custom, whatever you like)
478 // can be implemented as several hardware dcs - this is because
479 // each hardware dcs can control only one "spine", that is, a bus
480 // within one quadrant. Here we find suitable hardware dcses.
481 CellInfo *hw_dcs = ctx->getBoundBelCell(dcs_bel);
482 if (ctx->debug) {
483 log_info(" use %s spine and %s bel for '%s' hw cell.\n", ctx->nameOfWire(dst), ctx->nameOfBel(dcs_bel),
484 ctx->nameOf(hw_dcs));
485 }
486 if (dcs_ci->attrs.count(id_DCS_MODE) != 0) {
487 hw_dcs->setAttr(id_DCS_MODE, dcs_ci->attrs.at(id_DCS_MODE));
488 } else {
489 hw_dcs->setAttr(id_DCS_MODE, Property("RISING"));
490 }
491
492 // Need to release the fake internal DCS PIP which is the only
493 // downhill pip for DCS inputs
494 PipId fake_pip = *ctx->getPipsDownhill(dst).begin();
495 WireId clkout_wire = ctx->getPipDstWire(fake_pip);
496 if (ctx->debug) {
497 log_info("fake pip:%s, CLKOUT src:%s\n", ctx->nameOfPip(fake_pip), ctx->nameOfWire(clkout_wire));
498 }
499 ctx->unbindPip(fake_pip);
500 ctx->bindWire(clkout_wire, net, STRENGTH_LOCKED);
501 ctx->unbindWire(dst);
502
503 // The input networks must bs same for all hardware dcs.
504 dcs_ci->copyPortTo(id_SELFORCE, hw_dcs, id_SELFORCE);
505 dcs_ci->copyPortBusTo(dcs_clock_input_prefix, 0, false, hw_dcs, dcs_clock_input_prefix, 0, false, 4);
506 dcs_ci->copyPortBusTo(id_CLKSEL, 0, true, hw_dcs, id_CLKSEL, 0, false, 4);
507 }
508
509 // remove the virtual DCS
510 dcs_ci->disconnectPort(id_SELFORCE);
511 dcs_ci->disconnectPort(id_CLKOUT);
512 for (int i = 0; i < 4; ++i) {
513 dcs_ci->disconnectPort(ctx->idf("CLKSEL[%d]", i));
514 dcs_ci->disconnectPort(ctx->idf("%s%d", dcs_clock_input_prefix_str, i));
515 }
516 log_info(" '%s' net was routed.\n", ctx->nameOf(net));
517 ctx->cells.erase(dcs_ci->name);
518 }
519
520 void route_dhcen_net(NetInfo *net)
521 {
522 // route net after dhcen source of CLKIN net
523 CellInfo *dhcen_ci = net->driver.cell;
524
525 NetInfo *net_before_dhcen = dhcen_ci->getPort(id_CLKIN);
526 NPNR_ASSERT(net_before_dhcen != nullptr);
527
528 PortRef driver = net_before_dhcen->driver;
529 NPNR_ASSERT_MSG(gwu.driver_is_buf(driver) || gwu.driver_is_clksrc(driver) || gwu.driver_is_mipi(driver),
530 stringf("The input source (%s:%s) for %s is not a clock.", ctx->nameOf(driver.cell),
531 driver.port.c_str(ctx), ctx->nameOf(dhcen_ci))
532 .c_str());
533
534 IdString port;
535 // use BUF input if there is one
536 if (gwu.driver_is_buf(driver)) {
537 port = id_I;
538 } else {
539 port = driver.port;
540 }
541 WireId src = ctx->getBelPinWire(driver.cell->bel, port);
542
543 std::vector<PipId> path;
544 RouteResult route_result;
545 if (gwu.driver_is_mipi(driver)) {
546 route_result = route_direct_net(
547 net, [&](PipId pip, WireId src_wire) { return segment_wire_filter(pip) && dcs_input_filter(pip); },
548 src, &path);
549 } else {
550 route_result = route_direct_net(
551 net,
552 [&](PipId pip, WireId src_wire) {
553 return global_pip_filter(pip, src) && segment_wire_filter(pip) && dcs_input_filter(pip);
554 },
555 src, &path);
556 }
557
558 if (route_result == NOT_ROUTED) {
559 log_error("Can't route the %s network.\n", ctx->nameOf(net));
560 }
561 if (route_result == ROUTED_PARTIALLY) {
562 log_error("It was not possible to completely route the %s net using only global resources. This is not "
563 "allowed for dhcen managed networks.\n",
564 ctx->nameOf(net));
565 }
566
567 // In networks controlled by dhcen we disable/enable only HCLK - if
568 // there are ordinary cells among the sinks, then they are not affected
569 // by this primitive.
570 for (PipId pip : path) {
571 // move to upper level net
572 ctx->unbindPip(pip);
573 ctx->bindPip(pip, net_before_dhcen, STRENGTH_LOCKED);
574
575 WireId dst = ctx->getPipDstWire(pip);
576 IdString side;
577 BelId dhcen_bel = gwu.get_dhcen_bel(dst, side);
578 if (dhcen_bel == BelId()) {
579 continue;
580 }
581
582 // One pseudo dhcen can be implemented as several hardware dhcen.
583 // Here we find suitable hardware dhcens.
584 CellInfo *hw_dhcen = ctx->getBoundBelCell(dhcen_bel);
585 if (ctx->debug) {
586 log_info(" use %s wire and %s bel for '%s' hw cell.\n", ctx->nameOfWire(dst),
587 ctx->nameOfBel(dhcen_bel), ctx->nameOf(hw_dhcen));
588 }
589
590 // The control network must connect the CE inputs of all hardware dhcens.
591 hw_dhcen->setAttr(id_DHCEN_USED, 1);
592 dhcen_ci->copyPortTo(id_CE, hw_dhcen, id_CE);
593 }
594 if (gwu.driver_is_mipi(driver)) {
595 ctx->bindWire(src, net_before_dhcen, STRENGTH_LOCKED);
596 }
597
598 // connect all users to upper level net
599 std::vector<PortRef> users;
600 for (auto &cell_port : net->users) {
601 users.push_back(cell_port);
602 }
603 for (PortRef &user : users) {
604 user.cell->disconnectPort(user.port);
605 user.cell->connectPort(user.port, net_before_dhcen);
606 }
607
608 // remove the virtual dhcen
609 dhcen_ci->disconnectPort(id_CLKOUT);
610 dhcen_ci->disconnectPort(id_CLKIN);
611 dhcen_ci->disconnectPort(id_CE);
612 ctx->cells.erase(dhcen_ci->name);
613 }
614
615 void route_buffered_net(NetInfo *net)
616 {
617 // a) route net after buf using the buf input as source
618 CellInfo *buf_ci = net->driver.cell;
619 WireId src = ctx->getBelPinWire(buf_ci->bel, id_I);
620
621 NetInfo *net_before_buf = buf_ci->getPort(id_I);
622 NPNR_ASSERT(net_before_buf != nullptr);
623
624 RouteResult route_result = route_direct_net(
625 net,
626 [&](PipId pip, WireId src_wire) {
627 return global_pip_filter(pip, src_wire) && segment_wire_filter(pip) && dcs_input_filter(pip);
628 },
629 src);
630 if (route_result == NOT_ROUTED) {
631 log_error("Can't route the %s net. It might be worth removing the BUFG buffer flag.\n", ctx->nameOf(net));
632 }
633
634 // b) route net before buf from whatever to the buf input
635 WireId dst = src;
636 CellInfo *true_src_ci = net_before_buf->driver.cell;
637 src = ctx->getBelPinWire(true_src_ci->bel, net_before_buf->driver.port);
638 ctx->bindWire(src, net, STRENGTH_LOCKED);
639 backwards_bfs_route(net, src, dst, 1000000, false, [&](PipId pip, WireId src_wire) {
640 return clock_gate_wire_filter(pip) && segment_wire_filter(pip) && dcs_input_filter(pip);
641 });
642 // remove net
643 buf_ci->movePortTo(id_O, true_src_ci, net_before_buf->driver.port);
644 net_before_buf->driver.cell = nullptr;
645
646 log_info(" '%s' net was routed.\n", ctx->nameOf(net));
647 }
648
649 RouteResult route_clk_net(NetInfo *net)
650 {
651 RouteResult route_result = route_direct_net(net, [&](PipId pip, WireId src_wire) {
652 return clock_gate_wire_filter(pip) && global_pip_filter(pip, src_wire) && segment_wire_filter(pip) &&
653 dcs_input_filter(pip);
654 });
655 if (route_result != NOT_ROUTED) {
656 log_info(" '%s' net was routed using global resources %s.\n", ctx->nameOf(net),
657 route_result == ROUTED_ALL ? "only" : "partially");
658 }
659 return route_result;
660 }
661
662 // segmented wires
663 enum SegmentRouteResult
664 {
665 SEG_NOT_ROUTED = 0,
666 SEG_ROUTED_TO_ANOTHER_SEGMENT,
667 SEG_ROUTED
668 };
669 // Step 0: route LBx1 -> sinks
670 SegmentRouteResult route_segmented_step0(NetInfo *ni, Loc dst_loc, WireId dst_wire, int s_idx, int s_x,
671 std::vector<PipId> &bound_pips)
672 {
673 bool routed = false;
674
675 WireId lbo_wire = ctx->getWireByName(
676 IdStringList::concat(ctx->idf("X%dY%d", s_x, dst_loc.y), ctx->idf("LBO%d", s_idx / 4)));
677 if (ctx->debug) {
678 log_info(" step 0: %s -> %s\n", ctx->nameOfWire(lbo_wire), ctx->nameOfWire(dst_wire));
679 }
680 // The DFF can currently only connect to a neighbouring LUT. Skip such networks.
681 if (ctx->getWireName(dst_wire)[1].in(id_XD0, id_XD1, id_XD2, id_XD3, id_XD4, id_XD5)) {
682 auto pips = ctx->getPipsUphill(dst_wire);
683 auto pip_it = pips.begin();
684 ++pip_it;
685 NPNR_ASSERT_MSG(!(pip_it != pips.end()), "DFFs have been given the ability to connect independently of the "
686 "neighbouring LUT. Segment routing must be corrected.\n");
687 // Connect LUT OUT to DFF IN
688 PipId pip = *pips.begin();
689 ctx->bindPip(pip, ni, STRENGTH_LOCKED);
690 bound_pips.push_back(pip);
691 return SEG_ROUTED_TO_ANOTHER_SEGMENT;
692 }
693 routed = backwards_bfs_route(
694 ni, lbo_wire, dst_wire, 1000000, false, [&](PipId pip, WireId src) { return true; }, &bound_pips);
695 return routed ? SEG_ROUTED : SEG_ROUTED_TO_ANOTHER_SEGMENT;
696 }
697
698 // Step 1: segment wire -> LBOx
699 SegmentRouteResult route_segmented_step1(NetInfo *ni, Loc dst_loc, int s_idx, int s_x,
700 std::vector<PipId> &bound_pips)
701 {
702 IdString tile = ctx->idf("X%dY%d", s_x, dst_loc.y);
703 IdString lbo_wire_name = ctx->idf("LBO%d", s_idx > 3 ? 1 : 0);
704 IdStringList pip_dst_name = IdStringList::concat(tile, lbo_wire_name);
705
706 // if we used other wire
707 IdStringList last_pip_src_name = ctx->getWireName(ctx->getPipSrcWire(bound_pips.back()));
708 if (last_pip_src_name != pip_dst_name) {
709 if (ctx->debug) {
710 log_info(" step 1: Already joined the network in another segment at %s. Skip.\n",
711 last_pip_src_name.str(ctx).c_str());
712 }
713 return SEG_ROUTED_TO_ANOTHER_SEGMENT;
714 }
715
716 IdString lt_wire_name = ctx->idf("LT0%d", s_idx > 3 ? 4 : 1);
717 PipId pip = ctx->getPipByName(IdStringList::concat(pip_dst_name, lt_wire_name));
718
719 if (ctx->debug) {
720 log_info(" step 1: %s -> %s\n", lt_wire_name.c_str(ctx), pip_dst_name.str(ctx).c_str());
721 }
722 NPNR_ASSERT(pip != PipId());
723
724 NetInfo *pip_net = ctx->getBoundPipNet(pip);
725 if (pip_net == nullptr) {
726 ctx->bindPip(pip, ni, STRENGTH_LOCKED);
727 bound_pips.push_back(pip);
728 } else {
729 if (pip_net != ni) {
730 return SEG_NOT_ROUTED;
731 }
732 }
733 return SEG_ROUTED;
734 }
735
736 // Step 2: gate wire -> segment wire
737 SegmentRouteResult route_segmented_step2(NetInfo *ni, WireId segment_wire, WireId gate_wire,
738 std::vector<PipId> &bound_pips)
739 {
740 PipId pip;
741 for (PipId down_pip : ctx->getPipsDownhill(gate_wire)) {
742 WireId dst = ctx->getPipDstWire(down_pip);
743 if (dst == segment_wire) {
744 pip = down_pip;
745 break;
746 }
747 }
748
749 NPNR_ASSERT(pip != PipId());
750 if (ctx->debug) {
751 log_info(" step 2: %s\n", ctx->nameOfPip(pip));
752 }
753 NetInfo *pip_net = ctx->getBoundPipNet(pip);
754 if (pip_net == nullptr) {
755 ctx->bindPip(pip, ni, STRENGTH_LOCKED);
756 bound_pips.push_back(pip);
757 } else {
758 if (pip_net != ni) {
759 return SEG_NOT_ROUTED;
760 }
761 }
762 return SEG_ROUTED;
763 }
764
765 // Step 3: route src -> gate wires
766 SegmentRouteResult route_segmented_step3(NetInfo *ni, pool<WireId> gate_wires, std::vector<PipId> &bound_pips,
767 pool<WireId> &bound_wires)
768 {
769 bool routed = false;
770 WireId src_wire = ctx->getNetinfoSourceWire(ni);
771 if (ctx->debug) {
772 log_info(" step 3: %s -> \n", ctx->nameOfWire(src_wire));
773 }
774 // Create a temporary small network where segment gates will be the sinks
775 IdString gate_net_name = ctx->idf("%s$gate_net$", ni->name.c_str(ctx));
776 NetInfo *gate_ni = ctx->createNet(gate_net_name);
777 std::vector<PipId> gate_bound_pips;
778 pool<WireId> gate_bound_wires;
779
780 for (WireId gatewire : gate_wires) {
781 if (ctx->debug) {
782 log_info(" %s\n", ctx->nameOfWire(gatewire));
783 }
784 routed = backwards_bfs_route(
785 gate_ni, src_wire, gatewire, 1000000, false,
786 [&](PipId pip, WireId src) { return dcs_input_filter(pip) && !gwu.is_global_pip(pip); },
787 &gate_bound_pips);
788 if (routed) {
789 // bind src
790 if (ctx->checkWireAvail(src_wire)) {
791 ctx->bindWire(src_wire, gate_ni, STRENGTH_LOCKED);
792 gate_bound_wires.insert(src_wire);
793 }
794 } else {
795 break;
796 }
797 }
798
799 // merge with original net
800 if (routed) {
801 for (PipId pip : gate_bound_pips) {
802 ctx->unbindPip(pip);
803 ctx->bindPip(pip, ni, STRENGTH_LOCKED);
804 bound_pips.push_back(pip);
805 }
806 for (WireId wire : gate_bound_wires) {
807 ctx->unbindWire(wire);
808 ctx->bindWire(wire, ni, STRENGTH_LOCKED);
809 bound_wires.insert(wire);
810 }
811 }
812
813 return routed ? SEG_ROUTED : SEG_NOT_ROUTED;
814 }
815
816 void route_segmented(std::vector<IdString> &nets)
817 {
818 if (ctx->verbose) {
819 log_info("routing segmented...\n");
820 }
821
822 struct selected_net
823 {
824 int sink_cnt;
825 std::vector<int> segs; // segments
826 dict<int, WireId> gate_wires; // from logic to segment
827 dict<int, WireId> tb_wires; // top or bottom segment wire
828 dict<int, WireId> wire_to_isolate; // this wire should be disconnected to avoid conflict
829 };
830
831 dict<IdString, selected_net> selected_nets;
832 NetInfo *vcc_net = ctx->nets.at(ctx->id("$PACKER_VCC")).get();
833 NetInfo *vss_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
834
835 auto get_port_loc = [&](PortRef &cell_wire) -> Loc {
836 BelId bel = cell_wire.cell->bel;
837 NPNR_ASSERT(bel != BelId());
838 return ctx->getBelLocation(bel);
839 };
840
841 for (IdString net_name : nets) {
842 NetInfo *ni = ctx->nets.at(net_name).get();
843
844 // We restrict the considered networks from above because networks
845 // with a large number of sinks have all chances to cross quadrant
846 // boundaries and for such large global networks it is better to
847 // use free clock wires.
848 int sinks_num = ni->users.entries();
849 if (ni->driver.cell == nullptr || sinks_num < 8 || sinks_num > 50 || ni == vcc_net || ni == vss_net) {
850 continue;
851 }
852
853 // We cut off very compact networks because regular wires will
854 // suffice for them, and using segmented ones takes up a whole
855 // column in the bank at once.
856 Loc src_loc = get_port_loc(ni->driver);
857 if (ctx->debug) {
858 log_info(" net:%s, src:(%d, %d) %s\n", ctx->nameOf(ni), src_loc.y, src_loc.x,
859 ni->driver.port.c_str(ctx));
860 }
861 int far_sink_cnt = 0;
862 for (auto sink : ni->users) {
863 Loc sink_loc = get_port_loc(sink);
864 if (ctx->debug) {
865 log_info(" sink:(%d, %d) %s\n", sink_loc.y, sink_loc.x, sink.port.c_str(ctx));
866 }
867 if (std::abs(sink_loc.x - src_loc.x) > 4 || std::abs(sink_loc.y - src_loc.y) > 4) {
868 ++far_sink_cnt;
869 }
870 }
871 if (far_sink_cnt > 10) {
872 if (ctx->debug) {
873 log_info(" far sinks:%d, net is selected for processing.\n", far_sink_cnt);
874 }
875 selected_nets[net_name].sink_cnt = far_sink_cnt;
876 }
877 }
878 // Now that we have selected candidate grids, let's put them into a
879 // structure convenient for working with each grid cell of the chip
880 // individually.
881 // Each segment "serves" a rectangular area, the width and height of
882 // which depends on the position of the tap from the horizontal
883 // "spine" wire.
884 // The areas of neighboring taps overlap, but not completely, so we'll
885 // have to handle the sinks of the nets cell by cell.
886 // Another reason why we have to work with each cell individually,
887 // instead of using the total number of sinks of a particular network
888 // in the whole rectangular area, is that it makes sense to connect the
889 // sinks that are in the immediate neighborhood of the network source
890 // with ordinary wires.
891 struct grid_net
892 {
893 IdString net;
894 int sink_cnt; // It is not currently used, but it may be useful if
895 // the network search algorithm is based on the
896 // number of sinks in the segment's service region.
897 };
898 // The largest Gowin chip to date (GW5A-138) contains 138000 LUTs,
899 // which is a rough estimate without taking into account the placement
900 // of a few LUTs in the cell gives 400 columns and 400 rows. We use
901 // the combination of row number << 16 and column number as a key.
902 auto xy_to_key = [&](int x, int y) -> uint32_t { return (y << 16) | x; };
903 std::unordered_multimap<uint32_t, grid_net> grid;
904 int min_x = ctx->getGridDimX();
905 int max_x = -1;
906 int min_y = ctx->getGridDimY();
907 int max_y = -1;
908
909 for (auto &net : selected_nets) {
910 IdString net_name = net.first;
911 NetInfo *ni = ctx->nets.at(net_name).get();
912 Loc src_loc = get_port_loc(ni->driver);
913 for (auto sink : ni->users) {
914 Loc sink_loc = get_port_loc(sink);
915 min_x = std::min(min_x, sink_loc.x);
916 max_x = std::max(max_x, sink_loc.x);
917 min_y = std::min(min_y, sink_loc.y);
918 max_y = std::max(max_y, sink_loc.y);
919
920 if (std::abs(sink_loc.x - src_loc.x) > 4 || std::abs(sink_loc.y - src_loc.y) > 4) {
921 uint32_t key = xy_to_key(sink_loc.x, sink_loc.y);
922 bool found = false;
923 if (grid.count(key)) {
924 auto net_range = grid.equal_range(key);
925 for (auto it = net_range.first; it != net_range.second; ++it) {
926 if (it->second.net == net_name) {
927 found = true;
928 ++(it->second.sink_cnt);
929 }
930 }
931 }
932 if (!found) {
933 grid_net new_cell;
934 new_cell.net = net_name;
935 new_cell.sink_cnt = 1;
936 grid.insert(std::make_pair(key, new_cell));
937 }
938 }
939 }
940 }
941 if (ctx->debug) {
942 log_info("Net grid. (%d, %d) <=> (%d, %d)\n", min_y, min_x, max_y, max_x);
943 for (auto it = grid.begin(); it != grid.end(); ++it) {
944 log_info(" (%d, %d): %s %d\n", it->first >> 16, it->first & 0xffff, it->second.net.c_str(ctx),
945 it->second.sink_cnt);
946 }
947 }
948
949 // Net -> s_idx (0 <= s_idx < 8 -indices of vertical segments)
950 dict<IdString, int> net_to_s_idx;
951
952 // We search all segmental columns, ignoring those that do not fall
953 // into the grid of networks
954 for (int s_i = 0; s_i < gwu.get_segments_count(); ++s_i) {
955 int s_x, s_idx, s_min_x, s_min_y, s_max_x, s_max_y;
956 gwu.get_segment_region(s_i, s_idx, s_x, s_min_x, s_min_y, s_max_x, s_max_y);
957 // skip empty (in sense of net sinks) segments
958 if (s_max_x < min_x || s_min_x > max_x || s_max_y < min_y || s_min_y > max_y) {
959 continue;
960 }
961 if (ctx->debug) {
962 log_info("segment:%d/%d, x:%d, (%d, %d) <=> (%d, %d)\n", s_i, s_idx, s_x, s_min_y, s_min_x, s_max_y,
963 s_max_x);
964 }
965 // Selecting networks whose sinks fall in the served region.
966 // Networks with an already assigned segment index are prioritized
967 // over the rest, among which the network with the maximum number
968 // of sinks is selected.
969 bool found_net_with_index = false;
970 IdString net;
971 int sink_cnt = 0;
972 for (int y = s_min_y; y <= s_max_y && (!found_net_with_index); ++y) {
973 for (int x = s_min_x; x <= s_max_x && (!found_net_with_index); ++x) {
974 auto net_range = grid.equal_range(xy_to_key(x, y));
975 for (auto it = net_range.first; it != net_range.second; ++it) {
976 if (net_to_s_idx.count(it->second.net)) {
977 if (net_to_s_idx.at(it->second.net) == s_idx) {
978 // far network already use our segment index - reuse it
979 found_net_with_index = true;
980 net = it->second.net;
981 sink_cnt = selected_nets.at(it->second.net).sink_cnt;
982 break;
983 }
984 continue;
985 }
986 // new net, calculate maximum sinks
987 if (selected_nets.at(it->second.net).sink_cnt > sink_cnt) {
988 sink_cnt = selected_nets.at(it->second.net).sink_cnt;
989 net = it->second.net;
990 }
991 }
992 }
993 }
994 // no suitable nets, segment is unused, skip
995 if (sink_cnt == 0) {
996 continue;
997 }
998
999 if (!found_net_with_index) {
1000 // new net
1001 if (ctx->debug) {
1002 log_info(" new net: %s, index:%d\n", net.c_str(ctx), s_idx);
1003 }
1004 net_to_s_idx[net] = s_idx;
1005 } else {
1006 // old net
1007 if (ctx->debug) {
1008 log_info(" old net: %s, index:%d\n", net.c_str(ctx), s_idx);
1009 }
1010 }
1011 selected_nets.at(net).segs.push_back(s_i);
1012 }
1013 // Sort in descending order of the number of segments used.
1014 std::multimap<int, IdString> sorted_nets;
1015 for (auto const &net_seg : net_to_s_idx) {
1016 sorted_nets.insert(std::make_pair(-selected_nets.at(net_seg.first).segs.size(), net_seg.first));
1017 }
1018
1019 // Now that we have all the segments for the networks we need to
1020 // decide which end of the segment (upper or lower) to use
1021 // depending on the distance to the network source.
1022 // This is critical because the signal in a segment can propagate
1023 // from bottom to top or top to bottom and you need to know exactly
1024 // which end to isolate.
1025 for (auto const &net_seg : sorted_nets) {
1026 IdString net = net_seg.second;
1027 NetInfo *ni = ctx->nets.at(net).get();
1028 Loc src_loc = get_port_loc(ni->driver);
1029 if (ctx->debug) {
1030 log_info("net:%s, src:(%d, %d)\n", ctx->nameOf(ni), src_loc.y, src_loc.x);
1031 }
1032 std::string wires_to_isolate;
1033 for (int s_i : selected_nets.at(net).segs) {
1034 // distances to net source
1035 Loc top_loc, bottom_loc;
1036 gwu.get_segment_wires_loc(s_i, top_loc, bottom_loc);
1037 int top_to_src = std::abs(src_loc.x - top_loc.x) + std::abs(src_loc.y - top_loc.y);
1038 int bottom_to_src = std::abs(src_loc.x - bottom_loc.x) + std::abs(src_loc.y - bottom_loc.y);
1039 if (ctx->debug) {
1040 log_info(" segment:%d, top:(%d, %d), bottom:(%d, %d) dists:%d %d\n", s_i, top_loc.y, top_loc.x,
1041 bottom_loc.y, bottom_loc.x, top_to_src, bottom_to_src);
1042 }
1043 // By selecting the top or bottom end we also select a pair of
1044 // gate wires to use.
1045 WireId tb_wire, gate_wire, top_seg_wire, bottom_seg_wire, wire_to_isolate;
1046 gwu.get_segment_wires(s_i, top_seg_wire, bottom_seg_wire);
1047 tb_wire = top_seg_wire;
1048 if (top_to_src <= bottom_to_src) {
1049 WireId gate_wire1;
1050 gwu.get_segment_top_gate_wires(s_i, gate_wire, gate_wire1);
1051 if (gate_wire == WireId()) {
1052 gate_wire = gate_wire1;
1053 }
1054 if (gate_wire == WireId()) {
1055 // This segment has no top gate wires, so we use one of the bottom ones.
1056 gwu.get_segment_bottom_gate_wires(s_i, gate_wire, gate_wire1);
1057 if (gate_wire == WireId()) {
1058 gate_wire = gate_wire1;
1059 }
1060 tb_wire = bottom_seg_wire;
1061 wire_to_isolate = top_seg_wire;
1062 NPNR_ASSERT(gate_wire != WireId()); // Completely isolated segment. The chip base is damaged.
1063 }
1064 } else {
1065 WireId gate_wire1;
1066 tb_wire = bottom_seg_wire;
1067 wire_to_isolate = top_seg_wire;
1068 gwu.get_segment_bottom_gate_wires(s_i, gate_wire, gate_wire1);
1069 if (gate_wire == WireId()) {
1070 gate_wire = gate_wire1;
1071 }
1072 if (gate_wire == WireId()) {
1073 // This segment has no top gate wires, so we use one of the bottom ones.
1074 gwu.get_segment_top_gate_wires(s_i, gate_wire, gate_wire1);
1075 if (gate_wire == WireId()) {
1076 gate_wire = gate_wire1;
1077 }
1078 tb_wire = top_seg_wire;
1079 wire_to_isolate = WireId();
1080 NPNR_ASSERT(gate_wire != WireId()); // Completely isolated segment. The chip base is damaged.
1081 }
1082 }
1083 selected_nets.at(net).tb_wires[s_i] = tb_wire;
1084 selected_nets.at(net).gate_wires[s_i] = gate_wire;
1085 // store used wires for gowin_pack
1086 if (wire_to_isolate != WireId()) {
1087 wires_to_isolate += ctx->getWireName(wire_to_isolate).str(ctx);
1088 wires_to_isolate += ";";
1089 }
1090 if (ctx->debug) {
1091 log_info(" wire:%s, gate wire:%s\n", ctx->nameOfWire(tb_wire), ctx->nameOfWire(gate_wire));
1092 }
1093 }
1094 // Laying out a route for the network.
1095 std::vector<PipId> bound_pips;
1096 pool<WireId> bound_wires;
1097 SegmentRouteResult routed = SEG_NOT_ROUTED;
1098 pool<WireId> gate_wires;
1099
1100 if (ctx->debug) {
1101 log_info(" Route\n");
1102 }
1103 for (auto usr : ni->users) {
1104 BelId dst_bel = usr.cell->bel;
1105 NPNR_ASSERT(dst_bel != BelId());
1106
1107 Loc dst_loc(ctx->getBelLocation(dst_bel));
1108 WireId dst_wire = ctx->getNetinfoSinkWire(ni, usr, 0);
1109
1110 // find segment covers dest
1111 int s_idx = -1;
1112 int s_x, s_min_x, s_min_y, s_max_x, s_max_y;
1113 WireId tb_wire, gate_wire;
1114 for (int s_i : selected_nets.at(net).segs) {
1115 int idx;
1116 gwu.get_segment_region(s_i, idx, s_x, s_min_x, s_min_y, s_max_x, s_max_y);
1117 if (dst_loc.x >= s_min_x && dst_loc.x <= s_max_x && dst_loc.y >= s_min_y && dst_loc.y <= s_max_y) {
1118 s_idx = idx;
1119 tb_wire = selected_nets.at(net).tb_wires.at(s_i);
1120 gate_wire = selected_nets.at(net).gate_wires.at(s_i);
1121 break;
1122 }
1123 }
1124 if (ctx->debug) {
1125 log_info(" segment index:%d, dst:%s\n", s_idx, ctx->nameOf(usr.cell));
1126 }
1127 // There may not be a suitable segment if the sink is close to
1128 // the source. In that case consider these sinks along with
1129 // gate wires.
1130 if (s_idx == -1) {
1131 gate_wires.insert(dst_wire);
1132 } else {
1133 // Step 0: LBx1 -> dest
1134 routed = route_segmented_step0(ni, dst_loc, dst_wire, s_idx, s_x, bound_pips);
1135 if (routed == SEG_NOT_ROUTED) {
1136 break;
1137 }
1138 if (routed == SEG_ROUTED_TO_ANOTHER_SEGMENT) {
1139 continue;
1140 }
1141 // Step 1: segment wire -> LBOx
1142 routed = route_segmented_step1(ni, dst_loc, s_idx, s_x, bound_pips);
1143 if (routed == SEG_NOT_ROUTED) {
1144 break;
1145 }
1146 if (routed == SEG_ROUTED_TO_ANOTHER_SEGMENT) {
1147 continue;
1148 }
1149 // Step 2: gate wire -> segment wire
1150 routed = route_segmented_step2(ni, tb_wire, gate_wire, bound_pips);
1151 if (routed == SEG_NOT_ROUTED) {
1152 break;
1153 }
1154 // mark gate for step 3
1155 gate_wires.insert(gate_wire);
1156 }
1157 }
1158 // Step 3: src -> gate wire
1159 routed = route_segmented_step3(ni, gate_wires, bound_pips, bound_wires);
1160 if (routed == SEG_NOT_ROUTED) {
1161 if (ctx->verbose || ctx->debug) {
1162 log_warning("Can't route net %s using segments.\n", ctx->nameOf(ni));
1163 }
1164 // unbind pips and wires
1165 for (PipId pip : bound_pips) {
1166 ctx->unbindPip(pip);
1167 }
1168 for (WireId wire : bound_wires) {
1169 ctx->unbindWire(wire);
1170 }
1171 } else {
1172 // make list of wires for isolation
1173 if (!wires_to_isolate.empty()) {
1174 ni->attrs[id_SEG_WIRES_TO_ISOLATE] = wires_to_isolate;
1175 }
1176 log_info(" '%s' is routed using segments.\n", ctx->nameOf(ni));
1177 if (ctx->debug) {
1178 log_info(" routed\n");
1179 for (PipId pip : bound_pips) {
1180 log_info(" %s\n", ctx->nameOfPip(pip));
1181 }
1182 for (WireId wire : bound_wires) {
1183 log_info(" %s\n", ctx->nameOfWire(wire));
1184 }
1185 }
1186 }
1187 }
1188 }
1189
1190 // Enable clocked spines by connecting magic wires to VCC/GND if necessary.
1191 void enable_spines(void)
1192 {
1193 if (ctx->verbose) {
1194 log_info("Check for spine select wires.\n");
1195 }
1196
1197 NetInfo *vcc_net = ctx->nets.at(ctx->id("$PACKER_VCC")).get();
1198 NetInfo *vss_net = ctx->nets.at(ctx->id("$PACKER_GND")).get();
1199
1200 std::unique_ptr<CellInfo> top_ci = gwu.create_cell(ctx->id("spine_select$top"), id_SPINE_SELECT);
1201 top_ci->pseudo_cell = std::make_unique<RegionPlug>(Loc(0, 0, 0));
1202 std::unique_ptr<CellInfo> bottom_ci = gwu.create_cell(ctx->id("spine_select$bottom"), id_SPINE_SELECT);
1203 bottom_ci->pseudo_cell = std::make_unique<RegionPlug>(Loc(0, 0, 0));
1204
1205 pool<WireId> seen_spines;
1206 dict<IdString, int> top_connections;
1207 dict<IdString, int> bottom_connections;
1208
1209 for (auto &net : ctx->nets) {
1210 const NetInfo *ni = net.second.get();
1211 for (auto &wire : ni->wires) {
1212 WireId spine = wire.first;
1213 IdString spine_name = ctx->getWireName(spine)[1];
1214 if (spine_name.str(ctx).rfind("SPINE", 0) == 0 && seen_spines.count(spine) == 0) {
1215 seen_spines.insert(spine);
1216 std::vector<std::pair<WireId, int>> wires;
1217 if (gwu.get_spine_select_wire(spine, wires)) {
1218 int sfx = 0; // To activate a single spine, it may be necessary to connect an unknown number of
1219 // wires.
1220 CellInfo *select_cell = top_ci.get();
1221 auto connections = &top_connections;
1222 if (gwu.wire_in_bottom_half(spine)) {
1223 select_cell = bottom_ci.get();
1224 connections = &bottom_connections;
1225 }
1226
1227 for (auto gate : wires) {
1228 IdString port_name = ctx->idf("%s.%d", spine_name.c_str(ctx), sfx);
1229
1230 select_cell->addInput(port_name);
1231
1232 RegionPlug *rp = dynamic_cast<RegionPlug *>(select_cell->pseudo_cell.get());
1233 rp->port_wires[port_name] = gate.first;
1234 (*connections)[port_name] = gate.second;
1235 ++sfx;
1236 if (ctx->verbose) {
1237 log_info(" %s->%s\n", port_name.c_str(ctx), ctx->nameOfWire(gate.first));
1238 }
1239 }
1240 }
1241 }
1242 }
1243 }
1244
1245 // realy connect nets
1246 if (!top_connections.empty()) {
1247 for (auto conn : top_connections) {
1248 top_ci->connectPort(conn.first, conn.second ? vcc_net : vss_net);
1249 }
1250 ctx->cells[top_ci->name] = std::move(top_ci);
1251 }
1252 if (!bottom_connections.empty()) {
1253 for (auto conn : bottom_connections) {
1254 bottom_ci->connectPort(conn.first, conn.second ? vcc_net : vss_net);
1255 }
1256 ctx->cells[bottom_ci->name] = std::move(bottom_ci);
1257 }
1258 }
1259
1260 // Route all
1261 void run(void)
1262 {
1263 log_info("Routing globals...\n");
1264
1265 std::vector<IdString> dhcen_nets, dqce_nets, dcs_nets, buf_nets, clk_nets, seg_nets;
1266
1267 // Determining the priority of network routing
1268 for (auto &net : ctx->nets) {
1269 NetInfo *ni = net.second.get();
1270 CellInfo *drv = ni->driver.cell;
1271 if (drv == nullptr || ni->users.empty()) {
1272 if (ctx->debug) {
1273 log_info("skip empty or driverless net:%s\n", ctx->nameOf(ni));
1274 }
1275 continue;
1276 }
1277 if (gwu.driver_is_buf(ni->driver)) {
1278 buf_nets.push_back(net.first);
1279 } else {
1280 if (gwu.driver_is_clksrc(ni->driver)) {
1281 clk_nets.push_back(net.first);
1282 } else {
1283 if (gwu.driver_is_dqce(ni->driver)) {
1284 dqce_nets.push_back(net.first);
1285 } else {
1286 if (gwu.driver_is_dcs(ni->driver)) {
1287 dcs_nets.push_back(net.first);
1288 } else {
1289 if (gwu.driver_is_dhcen(ni->driver)) {
1290 dhcen_nets.push_back(net.first);
1291 } else {
1292 seg_nets.push_back(net.first);
1293 }
1294 }
1295 }
1296 }
1297 }
1298 }
1299
1300 // nets with DHCEN
1301 for (IdString net_name : dhcen_nets) {
1302 NetInfo *ni = ctx->nets.at(net_name).get();
1303 if (ctx->verbose) {
1304 log_info("route dhcen net '%s'\n", ctx->nameOf(ni));
1305 }
1306 route_dhcen_net(ni);
1307 }
1308
1309 // nets with DQCE
1310 for (IdString net_name : dqce_nets) {
1311 NetInfo *ni = ctx->nets.at(net_name).get();
1312 if (ctx->verbose) {
1313 log_info("route dqce net '%s'\n", ctx->nameOf(ni));
1314 }
1315 route_dqce_net(ni);
1316 }
1317
1318 // nets with DCS
1319 for (IdString net_name : dcs_nets) {
1320 NetInfo *ni = ctx->nets.at(net_name).get();
1321 if (ctx->verbose) {
1322 log_info("route dcs net '%s'\n", ctx->nameOf(ni));
1323 }
1324 route_dcs_net(ni);
1325 }
1326
1327 // buffered nets
1328 for (IdString net_name : buf_nets) {
1329 NetInfo *ni = ctx->nets.at(net_name).get();
1330 CellInfo *drv = ni->driver.cell;
1331 if (drv == nullptr || ni->users.empty()) {
1332 if (ctx->debug) {
1333 log_info("skip empty or driverless net:%s\n", ctx->nameOf(ni));
1334 }
1335 continue;
1336 }
1337 if (ctx->verbose) {
1338 log_info("route buffered net '%s'\n", ctx->nameOf(ni));
1339 }
1340 route_buffered_net(ni);
1341 }
1342
1343 // clock nets
1344 for (IdString net_name : clk_nets) {
1345 NetInfo *ni = ctx->nets.at(net_name).get();
1346 CellInfo *drv = ni->driver.cell;
1347 if (drv == nullptr || ni->users.empty()) {
1348 if (ctx->debug) {
1349 log_info("skip empty or driverless net:%s\n", ctx->nameOf(ni));
1350 }
1351 continue;
1352 }
1353 if (ctx->verbose) {
1354 log_info("route clock net '%s', src:%s\n", ctx->nameOf(ni), ctx->nameOf(ni->driver.cell));
1355 }
1356 if (route_clk_net(ni) == NOT_ROUTED) {
1357 if (ctx->verbose) {
1358 log_info(" will try to route it as a segmented network.\n");
1359 }
1360 seg_nets.push_back(net_name);
1361 }
1362 }
1363
1364 // segmented nets
1365 if (gwu.get_segments_count() != 0) {
1366 route_segmented(seg_nets);
1367 }
1368
1369 // In some GW5 series chips, in addition to the mechanism for
1370 // enabling/disabling individual clock spines using fuses, which is
1371 // invisible to nextpnr, it is necessary to enable them by connecting
1372 // some ports of the mysterious MUX to VSS/GND.
1373 if (gwu.has_spine_enable_nets()) {
1374 enable_spines();
1375 }
1376 }
1377 };
1378
1379 void gowin_route_globals(Context *ctx)
1380 {
1381 GowinGlobalRouter router(ctx);
1382 router.run();
1383 }
1384
1385 NEXTPNR_NAMESPACE_END