Debian Codegraph @

himbaechel/uarch/xilinx/pack_clocking.cc

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2019-2023 Myrtle Shah <[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 <algorithm>
21 #include <boost/optional.hpp>
22 #include <iterator>
23 #include <queue>
24 #include <unordered_set>
25 #include "chain_utils.h"
26 #include "design_utils.h"
27 #include "extra_data.h"
28 #include "log.h"
29 #include "nextpnr.h"
30 #include "pack.h"
31 #include "pins.h"
32
33 #define HIMBAECHEL_CONSTIDS "uarch/xilinx/constids.inc"
34 #include "himbaechel_constids.h"
35
36 NEXTPNR_NAMESPACE_BEGIN
37
38 BelId XilinxPacker::find_bel_with_short_route(WireId source, IdString beltype, IdString belpin)
39 {
40 if (source == WireId())
41 return BelId();
42 const size_t max_visit = 50000; // effort/runtime tradeoff
43 pool<WireId> visited;
44 std::queue<WireId> visit;
45 visit.push(source);
46 while (!visit.empty() && visited.size() < max_visit) {
47 WireId cursor = visit.front();
48 visit.pop();
49 for (auto bp : ctx->getWireBelPins(cursor))
50 if (bp.pin == belpin && ctx->getBelType(bp.bel) == beltype && ctx->checkBelAvail(bp.bel))
51 return bp.bel;
52 for (auto pip : ctx->getPipsDownhill(cursor)) {
53 WireId dst = ctx->getPipDstWire(pip);
54 if (visited.count(dst))
55 continue;
56 visit.push(dst);
57 visited.insert(dst);
58 }
59 }
60 return BelId();
61 }
62
63 void XilinxPacker::try_preplace(CellInfo *cell, IdString port)
64 {
65 if (cell->attrs.count(id_BEL) || cell->bel != BelId())
66 return;
67 NetInfo *n = cell->getPort(port);
68 if (n == nullptr || n->driver.cell == nullptr)
69 return;
70 CellInfo *drv = n->driver.cell;
71 BelId drv_bel = drv->bel;
72 if (drv_bel == BelId())
73 return;
74 WireId drv_wire = ctx->getBelPinWire(drv_bel, n->driver.port);
75 if (drv_wire == WireId())
76 return;
77 BelId tgt = find_bel_with_short_route(drv_wire, cell->type, port);
78 if (tgt != BelId()) {
79 ctx->bindBel(tgt, cell, STRENGTH_LOCKED);
80 log_info(" Constrained %s '%s' to bel '%s' based on dedicated routing\n", cell->type.c_str(ctx),
81 ctx->nameOf(cell), ctx->nameOfBel(tgt));
82 }
83 }
84
85 void XilinxPacker::preplace_unique(CellInfo *cell)
86 {
87 if (cell->attrs.count(id_BEL) || cell->bel != BelId())
88 return;
89 for (auto bel : ctx->getBels()) {
90 if (ctx->checkBelAvail(bel) && ctx->getBelType(bel) == cell->type) {
91 ctx->bindBel(bel, cell, STRENGTH_LOCKED);
92 return;
93 }
94 }
95 }
96
97 void XC7Packer::prepare_clocking()
98 {
99 log_info("Preparing clocking...\n");
100 dict<IdString, IdString> upgrade;
101 upgrade[id_MMCME2_BASE] = id_MMCME2_ADV;
102 upgrade[id_PLLE2_BASE] = id_PLLE2_ADV;
103
104 for (auto &cell : ctx->cells) {
105 CellInfo *ci = cell.second.get();
106 if (upgrade.count(ci->type)) {
107 IdString new_type = upgrade.at(ci->type);
108 ci->type = new_type;
109 } else if (ci->type == id_BUFG) {
110 ci->type = id_BUFGCTRL;
111 ci->renamePort(id_I, id_I0);
112 tie_port(ci, "CE0", true, true);
113 tie_port(ci, "S0", true, true);
114 tie_port(ci, "S1", false, true);
115 tie_port(ci, "IGNORE0", true, true);
116 } else if (ci->type == id_BUFGCE) {
117 ci->type = id_BUFGCTRL;
118 ci->renamePort(id_I, id_I0);
119 ci->renamePort(id_CE, id_CE0);
120 tie_port(ci, "S0", true, true);
121 tie_port(ci, "S1", false, true);
122 tie_port(ci, "IGNORE0", true, true);
123 }
124 }
125 }
126
127 void XC7Packer::pack_plls()
128 {
129 log_info("Packing PLLs...\n");
130
131 auto set_default = [](CellInfo *ci, IdString param, const Property &value) {
132 if (!ci->params.count(param))
133 ci->params[param] = value;
134 };
135
136 dict<IdString, XFormRule> pll_rules;
137 pll_rules[id_MMCME2_ADV].new_type = id_MMCME2_ADV_MMCME2_ADV;
138 pll_rules[id_PLLE2_ADV].new_type = id_PLLE2_ADV_PLLE2_ADV;
139 generic_xform(pll_rules);
140 for (auto &cell : ctx->cells) {
141 CellInfo *ci = cell.second.get();
142 // Preplace PLLs to make use of dedicated/short routing paths
143 if (ci->type.in(id_MMCM_MMCM_TOP, id_PLL_PLL_TOP))
144 try_preplace(ci, id_CLKIN1);
145 if (ci->type == id_MMCM_MMCM_TOP) {
146 // Fixup parameters
147 for (int i = 1; i <= 2; i++)
148 set_default(ci, ctx->idf("CLKIN%d_PERIOD", i), Property("0.0"));
149 for (int i = 0; i <= 6; i++) {
150 set_default(ci, ctx->idf("CLKOUT%d_CASCADE", i), Property("FALSE"));
151 set_default(ci, ctx->idf("CLKOUT%d_DIVIDE", i), Property(1));
152 set_default(ci, ctx->idf("CLKOUT%d_DUTY_CYCLE", i), Property("0.5"));
153 set_default(ci, ctx->idf("CLKOUT%d_PHASE", i), Property(0));
154 set_default(ci, ctx->idf("CLKOUT%d_USE_FINE_PS", i), Property("FALSE"));
155 }
156 set_default(ci, id_COMPENSATION, Property("INTERNAL"));
157
158 // Fixup routing
159 if (str_or_default(ci->params, id_COMPENSATION, "INTERNAL") == "INTERNAL") {
160 ci->disconnectPort(id_CLKFBIN);
161 ci->connectPort(id_CLKFBIN, ctx->nets.at(ctx->id("$PACKER_VCC_NET")).get());
162 }
163 }
164 }
165 }
166
167 void XC7Packer::pack_gbs()
168 {
169 log_info("Packing global buffers...\n");
170 dict<IdString, XFormRule> gb_rules;
171 gb_rules[id_BUFGCTRL].new_type = id_BUFGCTRL;
172 gb_rules[id_BUFGCTRL].new_type = id_BUFGCTRL;
173
174 generic_xform(gb_rules);
175
176 // Make sure prerequisites are set up first
177 for (auto &cell : ctx->cells) {
178 CellInfo *ci = cell.second.get();
179 if (ci->type == id_PS7_PS7)
180 preplace_unique(ci);
181 if (ci->type.in(id_PSEUDO_GND, id_PSEUDO_VCC))
182 preplace_unique(ci);
183 }
184
185 // Preplace global buffers to make use of dedicated/short routing
186 for (auto &cell : ctx->cells) {
187 CellInfo *ci = cell.second.get();
188 if (ci->type == id_BUFGCTRL)
189 try_preplace(ci, id_I0);
190 if (ci->type == id_BUFG_BUFG)
191 try_preplace(ci, id_I);
192 }
193 }
194
195 void XC7Packer::pack_clocking()
196 {
197 pack_plls();
198 pack_gbs();
199 }
200
201 void XilinxImpl::route_clocks()
202 {
203 log_info("Routing global clocks...\n");
204 // Special pass for faster routing of global clock psuedo-net
205 for (auto &net : ctx->nets) {
206 NetInfo *clk_net = net.second.get();
207 if (!clk_net->driver.cell)
208 continue;
209
210 // check if we have a global clock net, skip otherwise
211 bool is_global = false;
212 if ((clk_net->driver.cell->type.in(id_BUFGCTRL, id_BUFCE_BUFG_PS, id_BUFCE_BUFCE, id_BUFGCE_DIV_BUFGCE_DIV)) &&
213 clk_net->driver.port == id_O)
214 is_global = true;
215 else if (clk_net->driver.cell->type == id_PLLE2_ADV_PLLE2_ADV && clk_net->users.entries() == 1 &&
216 ((*clk_net->users.begin()).cell->type.in(id_BUFGCTRL, id_BUFCE_BUFCE, id_BUFGCE_DIV_BUFGCE_DIV)))
217 is_global = true;
218 else if (clk_net->users.entries() == 1 && (*clk_net->users.begin()).cell->type == id_PLLE2_ADV_PLLE2_ADV &&
219 (*clk_net->users.begin()).port == id_CLKIN1)
220 is_global = true;
221 if (!is_global)
222 continue;
223
224 log_info(" routing clock '%s'\n", clk_net->name.c_str(ctx));
225 ctx->bindWire(ctx->getNetinfoSourceWire(clk_net), clk_net, STRENGTH_LOCKED);
226
227 for (auto &usr : clk_net->users) {
228 std::queue<WireId> visit;
229 dict<WireId, PipId> backtrace;
230 WireId dest = WireId();
231
232 auto sink_wire = ctx->getNetinfoSinkWire(clk_net, usr, 0);
233 if (ctx->debug) {
234 auto sink_wire_name = "(uninitialized)";
235 if (sink_wire != WireId())
236 sink_wire_name = ctx->nameOfWire(sink_wire);
237 log_info(" routing arc to %s.%s (wire %s):\n", usr.cell->name.c_str(ctx), usr.port.c_str(ctx),
238 sink_wire_name);
239 }
240
241 visit.push(sink_wire);
242 while (!visit.empty()) {
243 WireId curr = visit.front();
244 visit.pop();
245 if (ctx->getBoundWireNet(curr) == clk_net) {
246 dest = curr;
247 break;
248 }
249 for (auto uh : ctx->getPipsUphill(curr)) {
250 if (!ctx->checkPipAvail(uh))
251 continue;
252 WireId src = ctx->getPipSrcWire(uh);
253 if (backtrace.count(src))
254 continue;
255 IdString intent = ctx->getWireType(src);
256 if (intent.in(id_NODE_DOUBLE, id_NODE_HLONG, id_NODE_HQUAD, id_NODE_VLONG, id_NODE_VQUAD,
257 id_NODE_SINGLE, id_NODE_CLE_OUTPUT, id_NODE_OPTDELAY, id_BENTQUAD, id_DOUBLE,
258 id_HLONG, id_HQUAD, id_OPTDELAY, id_SINGLE, id_VLONG, id_VLONG12, id_VQUAD,
259 id_PINBOUNCE))
260 continue;
261 if (!ctx->checkWireAvail(src) && ctx->getBoundWireNet(src) != clk_net)
262 continue;
263 backtrace[src] = uh;
264 visit.push(src);
265 }
266 }
267 if (dest == WireId()) {
268 log_info(" failed to find a route using dedicated resources.\n");
269 if (clk_net->users.entries() == 1 && (*clk_net->users.begin()).cell->type == id_PLLE2_ADV_PLLE2_ADV &&
270 (*clk_net->users.begin()).port == id_CLKIN1) {
271 // Due to some missing pips, currently special case more lenient solution
272 std::queue<WireId> empty;
273 std::swap(visit, empty);
274 backtrace.clear();
275 visit.push(sink_wire);
276 while (!visit.empty()) {
277 WireId curr = visit.front();
278 visit.pop();
279 if (ctx->getBoundWireNet(curr) == clk_net) {
280 dest = curr;
281 break;
282 }
283 for (auto uh : ctx->getPipsUphill(curr)) {
284 if (!ctx->checkPipAvail(uh))
285 continue;
286 WireId src = ctx->getPipSrcWire(uh);
287 if (backtrace.count(src))
288 continue;
289 if (!ctx->checkWireAvail(src) && ctx->getBoundWireNet(src) != clk_net)
290 continue;
291 backtrace[src] = uh;
292 visit.push(src);
293 }
294 }
295 if (dest == WireId())
296 continue;
297 } else {
298 continue;
299 }
300 }
301 while (backtrace.count(dest)) {
302 auto uh = backtrace[dest];
303 dest = ctx->getPipDstWire(uh);
304 if (ctx->getBoundWireNet(dest) == clk_net) {
305 NPNR_ASSERT(clk_net->wires.at(dest).pip == uh);
306 break;
307 }
308 if (ctx->debug)
309 log_info(" bind pip %s --> %s\n", ctx->nameOfPip(uh), ctx->nameOfWire(dest));
310 ctx->bindPip(uh, clk_net, STRENGTH_LOCKED);
311 }
312 }
313 }
314 #if 0
315 for (auto& net : nets) {
316 NetInfo *ni = net.second.get();
317 for (auto &usr : ni->users) {
318 if (usr.cell->type != id_BUFGCTRL || usr.port != id_I0)
319 continue;
320 WireId dst = getCtx()->getNetinfoSinkWire(ni, usr, 0);
321 std::queue<WireId> visit;
322 visit.push(dst);
323 int i = 0;
324 while(!visit.empty() && i < 5000) {
325 WireId curr = visit.front();
326 visit.pop();
327 log(" %s\n", nameOfWire(curr));
328 for (auto pip : getPipsUphill(curr)) {
329 auto &pd = locInfo(pip).pip_data[pip.index];
330 log_info(" p %s sr %s (t %d s %d sv %d)\n", nameOfPip(pip), nameOfWire(getPipSrcWire(pip)), pd.flags, pd.site, pd.site_variant);
331 if (!checkPipAvail(pip)) {
332 log(" p unavail\n");
333 continue;
334 }
335 WireId src = getPipSrcWire(pip);
336 if (!checkWireAvail(src)) {
337 log(" w unavail (%s)\n", nameOf(getBoundWireNet(src)));
338 continue;
339 }
340 log_info(" p %s s %s\n", nameOfPip(pip), nameOfWire(src));
341 visit.push(src);
342 }
343 ++i;
344 }
345 }
346 }
347 #endif
348 }
349
350 NEXTPNR_NAMESPACE_END