Fletchgen
The Fletcher Design Generator
kernel.cc
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "fletchgen/kernel.h"
16 
17 #include <cerata/api.h>
18 #include <utility>
19 #include <string>
20 #include <vector>
21 
22 #include "fletchgen/basic_types.h"
23 #include "fletchgen/mmio.h"
24 #include "fletchgen/array.h"
25 #include "fletchgen/external.h"
26 
27 namespace fletchgen {
28 
29 static void CopyFieldPorts(Component *kernel, const RecordBatch &record_batch, FieldPort::Function fun) {
30  // Add Arrow field derived ports with some function.
31  auto field_ports = record_batch.GetFieldPorts(fun);
32  cerata::NodeMap rebinding;
33  for (const auto &fp : field_ports) {
34  // Create a copy and invert for the Kernel
35  auto copied_port = dynamic_cast<FieldPort *>(fp->CopyOnto(kernel, fp->name(), &rebinding));
36  copied_port->Reverse();
37  }
38 }
39 
40 Kernel::Kernel(std::string name,
41  const std::vector<std::shared_ptr<RecordBatch>> &recordbatches,
42  const std::shared_ptr<Component> &mmio)
43  : Component(std::move(name)) {
44 
45  // Add clock/reset
46  Add(port("kcd", cr(), Port::Dir::IN, kernel_cd()));
47 
48  auto iw = index_width();
49  auto tw = tag_width();
50 
51  Add({iw, tw});
52 
53  // Add ports going to/from RecordBatches.
54  for (const auto &r : recordbatches) {
55  // Copy over the Arrow data and unlock stream ports.
56  CopyFieldPorts(this, *r, FieldPort::Function::ARROW);
57  CopyFieldPorts(this, *r, FieldPort::Function::UNLOCK);
58 
59  // The command stream at the kernel interface enjoys some simplification towards the user; the buffer addresses
60  // in the ctrl field are hidden.
61  // We create new command ports based on the command ports of the RecordBatch, but leave out the ctrl field.
62  auto rb_cmds = r->GetFieldPorts(FieldPort::Function::COMMAND);
63  for (auto &rb_cmd : rb_cmds) {
64  // Next, make a simplified version of the command stream for the kernel user.
65  auto kernel_cmd =
66  command_port(rb_cmd->fletcher_schema_, rb_cmd->field_, iw, tw, std::nullopt, kernel_cd());
67  kernel_cmd->Reverse();
68  Add(kernel_cmd);
69  }
70  }
71 
72  // Add ports from mmio
73  for (const auto &p : mmio->GetAll<Port>()) {
74  // Only copy over mmio ports that have the kernel function.
75  auto mmio_port = dynamic_cast<MmioPort *>(p);
76  if (mmio_port != nullptr) {
77  if (ExposeToKernel(mmio_port->reg.function)) {
78  auto kernel_port = std::dynamic_pointer_cast<MmioPort>(p->Copy());
79  kernel_port->Reverse();
80  kernel_port->SetName(mmio_port->reg.name);
81  Add(kernel_port);
82  }
83  }
84  }
85 
86  // Add custom I/O
87  auto ext = external();
88  if (ext) {
89  Add(cerata::port("ext", ext.value(), Port::Dir::OUT));
90  }
91 }
92 
93 std::shared_ptr<Kernel> kernel(const std::string &name,
94  const std::vector<std::shared_ptr<RecordBatch>> &recordbatches,
95  const std::shared_ptr<Component> &mmio) {
96  return std::make_shared<Kernel>(name, recordbatches, mmio);
97 }
98 
99 } // namespace fletchgen
Contains all classes and functions related to Fletchgen.
Definition: array.cc:29
std::shared_ptr< MmioPort > mmio_port(Port::Dir dir, const MmioReg &reg, const std::shared_ptr< ClockDomain > &domain)
Create an mmio port.
Definition: mmio.cc:58
std::shared_ptr< ClockDomain > kernel_cd()
Fletcher accelerator clock domain.
Definition: basic_types.cc:62
std::shared_ptr< Type > cr()
Fletcher clock/reset;.
Definition: basic_types.cc:73
std::shared_ptr< Kernel > kernel(const std::string &name, const std::vector< std::shared_ptr< RecordBatch >> &recordbatches, const std::shared_ptr< Component > &mmio)
Make a kernel component based on RecordBatch and MMIO components.
Definition: kernel.cc:93
bool ExposeToKernel(MmioFunction fun)
Return true if an mmio register's function must cause it to be exposed to the user kernel.
Definition: mmio.cc:165
std::shared_ptr< RecordBatch > record_batch(const std::string &name, const std::shared_ptr< FletcherSchema > &fletcher_schema, const fletcher::RecordBatchDescription &batch_desc)
Make a new RecordBatch(Reader/Writer) component, based on a Fletcher schema.
Definition: recordbatch.cc:190
std::shared_ptr< FieldPort > command_port(const std::shared_ptr< FletcherSchema > &schema, const std::shared_ptr< arrow::Field > &field, const std::shared_ptr< Node > &index_width, const std::shared_ptr< Node > &tag_width, std::optional< std::shared_ptr< Node >> addr_width, const std::shared_ptr< ClockDomain > &domain)
Construct a field-derived command port.
Definition: recordbatch.cc:218
std::shared_ptr< Component > mmio(const std::vector< fletcher::RecordBatchDescription > &batches, const std::vector< MmioReg > &regs, Axi4LiteSpec axi_spec)
Generate the MMIO component for the nucleus.
Definition: mmio.cc:62
Function
Enumeration of FieldPort functions.
Definition: recordbatch.h:54
Kernel(std::string name, const std::vector< std::shared_ptr< RecordBatch >> &recordbatches, const std::shared_ptr< Component > &mmio)
Construct a new kernel.
Definition: kernel.cc:40
A port on the vhdmmio component. Remembers what register spec it came from.
Definition: mmio.h:105