P4C
The P4 Compiler
Loading...
Searching...
No Matches
device.h
1
18
19#ifndef BACKENDS_TOFINO_BF_P4C_SPECS_DEVICE_H_
20#define BACKENDS_TOFINO_BF_P4C_SPECS_DEVICE_H_
21
22#include "backends/tofino/bf-p4c/specs/arch_spec.h"
23#include "backends/tofino/bf-p4c/specs/gress.h"
24#include "backends/tofino/bf-p4c/specs/mau_spec.h"
25#include "backends/tofino/bf-p4c/specs/parde_spec.h"
26#include "backends/tofino/bf-p4c/specs/phv_spec.h"
27#include "backends/tofino/bf-p4c/specs/power_spec.h"
28#include "lib/cstring.h"
29#include "lib/exceptions.h"
30#include "lib/range.h"
31
32class Device {
33 protected:
34 struct Options {
35 float phv_scale_factor = 1.0;
36 bool no_tagalong = false;
37 bool tof2lab44_workaround = false;
38 } options_;
39
40 public:
41 enum Device_t {
42 TOFINO,
43 JBAY,
44 };
52 static void init(cstring name, Options options);
53
54 static const Device &get() {
55 BUG_CHECK(instance_ != nullptr, "Target device not initialized!");
56 return *instance_;
57 }
58
59 static Device_t currentDevice() { return Device::get().device_type(); }
60 static cstring name() { return Device::get().get_name(); }
61
62 static const PhvSpec &phvSpec() { return Device::get().getPhvSpec(); }
63 static const PardeSpec &pardeSpec() { return Device::get().getPardeSpec(); }
64 struct GatewaySpec {
65 int PhvBytes;
66 int HashBits;
67 int PredicateBits;
68 int MaxRows;
69 bool SupportXor;
70 bool SupportRange;
71 int ExactShifts;
72 bool ByteSwizzle; // is the a byte swizzle between ixbar and gateway
73 int PerByteMatch; // lower bytes are shared per row, with 1 bit match per row
74 unsigned XorByteSlots;
75 };
76
77 static const GatewaySpec &gatewaySpec() { return Device::get().getGatewaySpec(); }
79 bool CmpMask; // are cmp oprerands maskable?
80 std::vector<cstring> CmpUnits;
81 int MaxSize;
82 int MaxDualSize;
83 int MaxPhvInputWidth;
84 int MaxInstructions;
85 int MaxInstructionConstWidth;
86 int MinInstructionConstValue;
87 int MaxInstructionConstValue;
88 int OutputWords;
89 bool DivModUnit;
90 bool FastClear;
91 int MaxRegfileRows;
92
93 cstring cmpUnit(unsigned idx) const;
94 };
95 static const StatefulAluSpec &statefulAluSpec() { return Device::get().getStatefulAluSpec(); }
96 static const ArchSpec &archSpec() { return Device::get().getArchSpec(); }
97 static const MauPowerSpec &mauPowerSpec() { return Device::get().getMauPowerSpec(); }
98 static const MauSpec &mauSpec() { return Device::get().getMauSpec(); }
99 static const IXBarSpec &ixbarSpec() { return Device::get().getMauSpec().getIXBarSpec(); }
100 static const IMemSpec &imemSpec() { return Device::get().getMauSpec().getIMemSpec(); }
101 static int numPipes() { return Device::get().getNumPipes(); }
102 static int numStages() {
103 return (numStagesRuntimeOverride_ != 0) ? numStagesRuntimeOverride_
104 : Device::get().getNumStages();
105 }
106 static void overrideNumStages(int num);
107 static int numLongBranchTags() { return Device::get().getLongBranchTags(); }
108 static bool hasLongBranches() { return numLongBranchTags() > 0; }
109 static int alwaysRunIMemAddr() { return Device::get().getAlwaysRunIMemAddr(); }
110 static bool hasAlwaysRunInstr() { return alwaysRunIMemAddr() >= 0; }
111 static unsigned maxCloneId(gress_t gress) { return Device::get().getMaxCloneId(gress); }
112 static gress_t maxGress() { return Device::get().getMaxGress(); }
113 static RangeIter<gress_t> allGresses() { return Range(INGRESS, maxGress()); }
114 static unsigned maxResubmitId() { return Device::get().getMaxResubmitId(); }
115 static unsigned maxDigestId() { return Device::get().getMaxDigestId(); }
116 /* type is 'int' because width_bits() is defined as 'int' in ir/base.def */
117 static int mirrorTypeWidth() { return Device::get().getMirrorTypeWidth(); }
118 static int cloneSessionIdWidth() { return Device::get().getCloneSessionIdWidth(); }
119 static int queueIdWidth() { return Device::get().getQueueIdWidth(); }
120 static int portBitWidth() { return Device::get().getPortBitWidth(); }
121 static int maxParserMatchBits() { return Device::get().getMaxParserMatchBits(); }
122 static unsigned maxDigestSizeInBytes() { return Device::get().getMaxDigestSizeInBytes(); }
123 static int numParsersPerPipe() { return 18; }
124 static int numMaxChannels() { return Device::get().getNumMaxChannels(); }
125 static int numClots() { return pardeSpec().numClotsPerGress(); }
126 static int isMemoryCoreSplit() { return Device::get().getIfMemoryCoreSplit(); }
127 static bool hasCompareInstructions() { return Device::get().getHasCompareInstructions(); }
128 static int numLogTablesPerStage() { return Device::get().getNumLogTablesPerStage(); }
129 static bool hasIngressDeparser() { return Device::get().getHasIngressDeparser(); }
130 static bool hasEgressParser() { return Device::get().getHasEgressParser(); }
131 static bool hasGhostThread() { return Device::get().getHasGhostThread(); }
132 static bool threadsSharePipe(gress_t a, gress_t b) {
133 return Device::get().getThreadsSharePipe(a, b);
134 }
135 static bool hasMirrorIOSelect() { return Device::get().getHasMirrorIOSelect(); }
136 static bool hasMetadataPOV() { return Device::get().getHasMetadataPOV(); }
137 static int sramMinPackEntries() { return Device::get().getSramMinPackEntries(); }
138 static int sramMaxPackEntries() { return Device::get().getSramMaxPackEntries(); }
139 static int sramMaxPackEntriesPerRow() { return Device::get().getSramMaxPackEntriesPerRow(); }
140 /* sramColumnAdjust is applicable for Tofino targets where the left and right side columns in
141 * SRAMs are different. The columns are still present in address space but not actually usable.
142 * This results in an adjustment required for column numbering.
143 */
144 static int sramColumnAdjust() { return Device::get().getSramColumnAdjust(); }
145 static int metaGlobalTimestampStart() { return Device::get().getMetaGlobalTimestampStart(); }
146 static int metaGlobalTimestampLen() { return Device::get().getMetaGlobalTimestampLen(); }
147 static int metaGlobalVersionStart() { return Device::get().getMetaGlobalVersionStart(); }
148 static int metaGlobalVersionLen() { return Device::get().getMetaGlobalVersionLen(); }
149 static unsigned int egressIntrinsicMetadataMinLen() {
150 return Device::get().getEgressIntrinsicMetadataMinLen();
151 }
152
153 protected:
154 explicit Device(Options options, cstring name) : options_(options), name_(name) {}
155
156 virtual Device_t device_type() const = 0;
157 virtual cstring get_name() const = 0;
158
159 virtual const PhvSpec &getPhvSpec() const = 0;
160 virtual const PardeSpec &getPardeSpec() const = 0;
161 virtual const GatewaySpec &getGatewaySpec() const = 0;
162 virtual const StatefulAluSpec &getStatefulAluSpec() const = 0;
163 virtual const MauPowerSpec &getMauPowerSpec() const = 0;
164 virtual const MauSpec &getMauSpec() const = 0;
165 virtual const ArchSpec &getArchSpec() const = 0;
166 virtual int getNumPipes() const = 0;
167 virtual int getNumPortsPerPipe() const = 0;
168 virtual int getNumChannelsPerPort() const = 0;
169 virtual int getNumStages() const = 0;
170 virtual int getLongBranchTags() const = 0;
171 virtual int getAlwaysRunIMemAddr() const = 0;
172 virtual unsigned getMaxCloneId(gress_t) const = 0;
173 virtual gress_t getMaxGress() const = 0;
174 virtual unsigned getMaxResubmitId() const = 0;
175 virtual unsigned getMaxDigestId() const = 0;
176 virtual unsigned getMaxDigestSizeInBytes() const = 0;
177 virtual int getMirrorTypeWidth() const = 0;
178 virtual int getCloneSessionIdWidth() const = 0;
179 virtual int getQueueIdWidth() const = 0;
180 virtual int getPortBitWidth() const = 0;
181 virtual int getMaxParserMatchBits() const = 0;
182 virtual int getNumMaxChannels() const = 0;
183 virtual bool getIfMemoryCoreSplit() const = 0;
184 virtual bool getHasCompareInstructions() const = 0;
185 virtual int getNumLogTablesPerStage() const = 0;
186 virtual bool getHasIngressDeparser() const = 0;
187 virtual bool getHasEgressParser() const = 0;
188 virtual bool getHasGhostThread() const = 0;
189 virtual bool getThreadsSharePipe(gress_t a, gress_t b) const = 0;
190 virtual bool getHasMirrorIOSelect() const = 0;
191 virtual bool getHasMetadataPOV() const = 0;
192 virtual int getSramMinPackEntries() const = 0;
193 virtual int getSramMaxPackEntries() const = 0;
194 virtual int getSramMaxPackEntriesPerRow() const = 0;
195 virtual int getSramColumnAdjust() const = 0;
196 virtual int getMetaGlobalTimestampStart() const = 0;
197 virtual int getMetaGlobalTimestampLen() const = 0;
198 virtual int getMetaGlobalVersionStart() const = 0;
199 virtual int getMetaGlobalVersionLen() const = 0;
200 virtual unsigned int getEgressIntrinsicMetadataMinLen() const = 0;
201
202 private:
203 static Device *instance_;
204 static int numStagesRuntimeOverride_;
205 cstring name_;
206};
207
208class TofinoDevice : public Device {
209 const TofinoPhvSpec phv_;
210 const TofinoPardeSpec parde_;
211 const TofinoMauPowerSpec mau_power_;
212 const TofinoMauSpec mau_;
213 const TofinoArchSpec arch_;
214
215 public:
216 explicit TofinoDevice(Options options)
217 : Device(options, "Tofino"_cs),
218 phv_(options.phv_scale_factor, options.no_tagalong),
219 parde_() {}
220 Device::Device_t device_type() const override { return Device::TOFINO; }
221 cstring get_name() const override { return "Tofino"_cs; }
222 int getNumPipes() const override { return 4; }
223 int getNumPortsPerPipe() const override { return 4; }
224 int getNumChannelsPerPort() const override { return 18; }
225 int getNumStages() const override { return 12; }
226 int getLongBranchTags() const override { return 0; }
227 int getAlwaysRunIMemAddr() const override { return -1; }
228 unsigned getMaxCloneId(gress_t gress) const override {
229 switch (gress) {
230 case INGRESS:
231 return 7; // one id reserved for IBuf
232 case EGRESS:
233 return 8;
234 default:
235 return 8;
236 }
237 }
238 gress_t getMaxGress() const override { return EGRESS; }
239 unsigned getMaxResubmitId() const override { return 8; }
240 unsigned getMaxDigestId() const override { return 8; }
241 int getMirrorTypeWidth() const override { return 3; }
242 int getCloneSessionIdWidth() const override { return 10; }
243 int getQueueIdWidth() const override { return 5; }
244 int getPortBitWidth() const override { return 9; }
245 int getMaxParserMatchBits() const override { return 32; }
246 int getNumMaxChannels() const override {
247 return getNumPipes() * getNumPortsPerPipe() * getNumChannelsPerPort();
248 }
249 unsigned getMaxDigestSizeInBytes() const override { return (384 / 8); }
250
251 const PhvSpec &getPhvSpec() const override { return phv_; }
252 const PardeSpec &getPardeSpec() const override { return parde_; }
253 const GatewaySpec &getGatewaySpec() const override;
254 const StatefulAluSpec &getStatefulAluSpec() const override;
255 const MauPowerSpec &getMauPowerSpec() const override { return mau_power_; }
256 const MauSpec &getMauSpec() const override { return mau_; }
257 const ArchSpec &getArchSpec() const override { return arch_; }
258 bool getIfMemoryCoreSplit() const override { return false; }
259 bool getHasCompareInstructions() const override { return false; }
260 int getNumLogTablesPerStage() const override { return 16; }
261 bool getHasIngressDeparser() const override { return true; }
262 bool getHasEgressParser() const override { return true; }
263 bool getHasGhostThread() const override { return false; };
264 bool getThreadsSharePipe(gress_t, gress_t) const override { return true; }
265 bool getHasMirrorIOSelect() const override { return false; }
266 bool getHasMetadataPOV() const override { return false; }
267 int getSramMinPackEntries() const override { return 1; }
268 int getSramMaxPackEntries() const override { return 9; }
269 int getSramMaxPackEntriesPerRow() const override { return 5; }
270 int getMetaGlobalTimestampStart() const override { return 432; }
271 int getMetaGlobalTimestampLen() const override { return 48; }
272 int getMetaGlobalVersionStart() const override { return 480; }
273 int getMetaGlobalVersionLen() const override { return 32; }
274 int getSramColumnAdjust() const override { return 2; }
275 unsigned int getEgressIntrinsicMetadataMinLen() const override { return 2; }
276};
277
278class JBayDevice : public Device {
279 const JBayPhvSpec phv_;
280 const JBayPardeSpec parde_;
281 const JBayMauPowerSpec mau_power_;
282 const JBayMauSpec mau_;
283 const JBayArchSpec arch_;
284
285 protected:
286#ifdef EMU_OVERRIDE_STAGE_COUNT
287 const int NUM_MAU_STAGES = EMU_OVERRIDE_STAGE_COUNT;
288#else
289 const int NUM_MAU_STAGES = 20;
290#endif
291
292 public:
293 explicit JBayDevice(Options options)
294 : Device(options, "Tofino2"_cs),
295 phv_(options.phv_scale_factor),
296 parde_(options.tof2lab44_workaround) {}
297 Device::Device_t device_type() const override { return Device::JBAY; }
298 cstring get_name() const override { return "Tofino2"_cs; }
299 int getNumPipes() const override { return 4; }
300 int getNumPortsPerPipe() const override { return 4; }
301 int getNumChannelsPerPort() const override { return 18; }
302 int getNumStages() const override { return NUM_MAU_STAGES; }
303 int getLongBranchTags() const override { return 8; }
304 int getAlwaysRunIMemAddr() const override { return 63; }
305 unsigned getMaxCloneId(gress_t /* gress */) const override { return 16; }
306 gress_t getMaxGress() const override { return GHOST; }
307 unsigned getMaxResubmitId() const override { return 8; }
308 unsigned getMaxDigestId() const override { return 8; }
309 unsigned getMaxDigestSizeInBytes() const override { return (384 / 8); }
310 int getMirrorTypeWidth() const override { return 4; }
311 int getCloneSessionIdWidth() const override { return 8; }
312 int getQueueIdWidth() const override { return 7; }
313 int getPortBitWidth() const override { return 9; }
314 int getMaxParserMatchBits() const override { return 32; }
315 int getNumMaxChannels() const override {
316 return getNumPipes() * getNumPortsPerPipe() * getNumChannelsPerPort();
317 }
318
319 const PhvSpec &getPhvSpec() const override { return phv_; }
320 const PardeSpec &getPardeSpec() const override { return parde_; }
321 const GatewaySpec &getGatewaySpec() const override;
322 const StatefulAluSpec &getStatefulAluSpec() const override;
323 const MauSpec &getMauSpec() const override { return mau_; }
324 const MauPowerSpec &getMauPowerSpec() const override { return mau_power_; }
325 const ArchSpec &getArchSpec() const override { return arch_; }
326 bool getIfMemoryCoreSplit() const override { return true; }
327 bool getHasCompareInstructions() const override { return true; }
328 int getNumLogTablesPerStage() const override { return 16; }
329 bool getHasIngressDeparser() const override { return true; }
330 bool getHasEgressParser() const override { return true; }
331 bool getHasGhostThread() const override { return true; };
332 bool getThreadsSharePipe(gress_t, gress_t) const override { return true; }
333 bool getHasMirrorIOSelect() const override { return true; }
334 bool getHasMetadataPOV() const override { return true; }
335 int getSramMinPackEntries() const override { return 1; }
336 int getSramMaxPackEntries() const override { return 9; }
337 int getSramMaxPackEntriesPerRow() const override { return 5; }
338 int getMetaGlobalTimestampStart() const override { return 400; }
339 int getMetaGlobalTimestampLen() const override { return 48; }
340 int getMetaGlobalVersionStart() const override { return 448; }
341 int getMetaGlobalVersionLen() const override { return 32; }
342 int getSramColumnAdjust() const override { return 2; }
343 unsigned int getEgressIntrinsicMetadataMinLen() const override { return 8; }
344};
345
348#if BAREFOOT_INTERNAL
349class JBayHDevice : public JBayDevice {
350 public:
351 int getNumStages() const override { return 6; }
352 cstring get_name() const override { return "Tofino2H"_cs; }
353
354 explicit JBayHDevice(Options options) : JBayDevice(options) {}
355};
356#endif /* BAREFOOT_INTERNAL */
357
358class JBayMDevice : public JBayDevice {
359 public:
360 int getNumStages() const override { return 12; }
361 cstring get_name() const override { return "Tofino2M"_cs; }
362
363 explicit JBayMDevice(Options options) : JBayDevice(options) {}
364};
365
366class JBayUDevice : public JBayDevice {
367 public:
368 int getNumStages() const override { return 20; }
369 cstring get_name() const override { return "Tofino2U"_cs; }
370
371 explicit JBayUDevice(Options options) : JBayDevice(options) {}
372};
373
374class JBayA0Device : public JBayDevice {
375 public:
376 const JBayA0PardeSpec parde_;
377 const PardeSpec &getPardeSpec() const override { return parde_; }
378 cstring get_name() const override { return "Tofino2A0"_cs; }
379
380 explicit JBayA0Device(Options options)
381 : JBayDevice(options), parde_(options.tof2lab44_workaround) {}
382};
383
384#endif /* BACKENDS_TOFINO_BF_P4C_SPECS_DEVICE_H_ */
Definition tofino/bf-p4c/specs/arch_spec.h:46
Definition device.h:32
static void init(cstring name, Options options)
Definition device.cpp:28
Definition device.h:64
Definition device.h:34
Definition parde_spec.h:674
Definition tofino/bf-p4c/specs/arch_spec.h:154
Definition device.h:278
Definition power_spec.h:149
Definition mau_spec.h:227
Definition parde_spec.h:577
Definition phv_spec.h:358
Definition power_spec.h:32
Definition mau_spec.h:110
Definition cstring.h:85
Definition parde_spec.h:379
Definition phv_spec.h:41
Definition tofino/bf-p4c/specs/arch_spec.h:149
Definition power_spec.h:93
Definition mau_spec.h:217
Definition parde_spec.h:504
Definition phv_spec.h:320
Definition device.h:78