Loading [MathJax]/extensions/tex2jax.js
CIRCT 21.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Services.cpp
Go to the documentation of this file.
1//===- StdServices.cpp - implementations of std services ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// DO NOT EDIT!
10// This file is distributed as part of an ESI package. The source for this file
11// should always be modified within CIRCT
12// (lib/dialect/ESI/runtime/cpp/lib/backends/Cosim.cpp).
13//
14//===----------------------------------------------------------------------===//
15
16#include "esi/Services.h"
17#include "esi/Accelerator.h"
18#include "esi/Engines.h"
19
20#include "zlib.h"
21
22#include <cassert>
23#include <stdexcept>
24
25using namespace esi;
26using namespace esi::services;
27
29 std::string implName,
30 ServiceImplDetails details,
31 HWClientDetails clients) {
32 return conn.getService(service, id, implName, details, clients);
33}
34
35std::string SysInfo::getServiceSymbol() const { return "__builtin_SysInfo"; }
36
37// Allocate 10MB for the uncompressed manifest. This should be plenty.
38constexpr uint32_t MAX_MANIFEST_SIZE = 10 << 20;
39/// Get the compressed manifest, uncompress, and return it.
40std::string SysInfo::getJsonManifest() const {
41 std::vector<uint8_t> compressed = getCompressedManifest();
42 std::vector<Bytef> dst(MAX_MANIFEST_SIZE);
43 uLongf dstSize = MAX_MANIFEST_SIZE;
44 int rc =
45 uncompress(dst.data(), &dstSize, compressed.data(), compressed.size());
46 if (rc != Z_OK)
47 throw std::runtime_error("zlib uncompress failed with rc=" +
48 std::to_string(rc));
49 return std::string(reinterpret_cast<char *>(dst.data()), dstSize);
50}
51
52//===----------------------------------------------------------------------===//
53// MMIO class implementations.
54//===----------------------------------------------------------------------===//
55
57 : Service(conn) {
58 for (const HWClientDetail &client : clients) {
59 auto offsetIter = client.implOptions.find("offset");
60 if (offsetIter == client.implOptions.end())
61 throw std::runtime_error("MMIO client missing 'offset' option");
62 Constant offset = std::any_cast<Constant>(offsetIter->second);
63 uint64_t offsetVal = std::any_cast<uint64_t>(offset.value);
64 if (offsetVal >= 1ull << 32)
65 throw std::runtime_error("MMIO client offset mustn't exceed 32 bits");
66
67 auto sizeIter = client.implOptions.find("size");
68 if (sizeIter == client.implOptions.end())
69 throw std::runtime_error("MMIO client missing 'size' option");
70 Constant size = std::any_cast<Constant>(sizeIter->second);
71 uint64_t sizeVal = std::any_cast<uint64_t>(size.value);
72 if (sizeVal >= 1ull << 32)
73 throw std::runtime_error("MMIO client size mustn't exceed 32 bits");
74 regions[client.relPath] =
75 RegionDescriptor{(uint32_t)offsetVal, (uint32_t)sizeVal};
76 }
77}
78
79std::string MMIO::getServiceSymbol() const {
80 return std::string(MMIO::StdName);
81}
83 auto regionIter = regions.find(id);
84 if (regionIter == regions.end())
85 return nullptr;
86 return new MMIORegion(id.back(), const_cast<MMIO *>(this),
87 regionIter->second);
88}
89
90namespace {
91class MMIOPassThrough : public MMIO {
92public:
93 MMIOPassThrough(const HWClientDetails &clients, MMIO *parent)
94 : MMIO(parent->getConnection(), clients), parent(parent) {}
95 uint64_t read(uint32_t addr) const override { return parent->read(addr); }
96 void write(uint32_t addr, uint64_t data) override {
97 parent->write(addr, data);
98 }
99
100private:
101 MMIO *parent;
102};
103} // namespace
104
106 std::string implName, ServiceImplDetails details,
107 HWClientDetails clients) {
108 if (service != typeid(MMIO))
109 return Service::getChildService(service, id, implName, details, clients);
110 return new MMIOPassThrough(clients, this);
111}
112
113//===----------------------------------------------------------------------===//
114// MMIO Region service port class implementations.
115//===----------------------------------------------------------------------===//
116
118 : ServicePort(id, nullptr, {}), parent(parent), desc(desc) {}
119uint64_t MMIO::MMIORegion::read(uint32_t addr) const {
120 if (addr >= desc.size)
121 throw std::runtime_error("MMIO read out of bounds: " + toHex(addr));
122 return parent->read(desc.base + addr);
123}
124void MMIO::MMIORegion::write(uint32_t addr, uint64_t data) {
125 if (addr >= desc.size)
126 throw std::runtime_error("MMIO write out of bounds: " + toHex(addr));
127 parent->write(desc.base + addr, data);
128}
129
131 : SysInfo(mmio->getConnection()), mmio(mmio) {}
132
134 uint64_t reg;
135 if ((reg = mmio->read(MetadataOffset)) != MagicNumber)
136 throw std::runtime_error("Invalid magic number: " + toHex(reg));
137 return mmio->read(MetadataOffset + 8);
138}
139
140std::vector<uint8_t> MMIOSysInfo::getCompressedManifest() const {
141 uint64_t version = getEsiVersion();
142 if (version != 0)
143 throw std::runtime_error("Unsupported ESI header version: " +
144 std::to_string(version));
145 uint64_t manifestPtr = mmio->read(MetadataOffset + 0x10);
146 uint64_t size = mmio->read(manifestPtr);
147 uint64_t numWords = (size + 7) / 8;
148 std::vector<uint64_t> manifestWords(numWords);
149 for (size_t i = 0; i < numWords; ++i)
150 manifestWords[i] = mmio->read(manifestPtr + 8 + (i * 8));
151
152 std::vector<uint8_t> manifest;
153 for (size_t i = 0; i < size; ++i) {
154 uint64_t word = manifestWords[i / 8];
155 manifest.push_back(word >> (8 * (i % 8)));
156 }
157 return manifest;
158}
159
160std::string HostMem::getServiceSymbol() const { return "__builtin_HostMem"; }
161
163 const ServiceImplDetails &details,
164 const HWClientDetails &clients)
165 : Service(conn), id(idPath) {
166 if (auto f = details.find("service"); f != details.end()) {
167 serviceSymbol = std::any_cast<std::string>(f->second);
168 // Strip off initial '@'.
169 serviceSymbol = serviceSymbol.substr(1);
170 }
171}
172
174 return new BundlePort(id.back(), type,
175 conn.getEngineMapFor(id).requestPorts(id, type));
176}
177
179 ServiceImplDetails details, HWClientDetails clients)
180 : Service(conn) {
181
182 if (auto f = details.find("service"); f != details.end())
183 // Strip off initial '@'.
184 symbol = std::any_cast<std::string>(f->second).substr(1);
185}
186
187std::string FuncService::getServiceSymbol() const { return symbol; }
188
190 return new Function(id.back(), type,
191 conn.getEngineMapFor(id).requestPorts(id, type));
192}
193
195 WriteChannelPort &arg,
196 ReadChannelPort &result) {
197 return new Function(
198 id, type, {{std::string("arg"), arg}, {std::string("result"), result}});
199 return nullptr;
200}
201
203 if (channels.size() != 2)
204 throw std::runtime_error("FuncService must have exactly two channels");
205 arg = &getRawWrite("arg");
206 arg->connect();
207 result = &getRawRead("result");
208 result->connect();
209}
210
211std::future<MessageData>
213 std::scoped_lock<std::mutex> lock(callMutex);
214 arg->write(argData);
215 return result->readAsync();
216}
217
219 ServiceImplDetails details)
220 : Service(acc) {
221 if (auto f = details.find("service"); f != details.end())
222 // Strip off initial '@'.
223 symbol = std::any_cast<std::string>(f->second).substr(1);
224}
225
226std::string CallService::getServiceSymbol() const { return symbol; }
227
229 return new Callback(conn, id.back(), type,
230 conn.getEngineMapFor(id).requestPorts(id, type));
231}
232
234 const BundleType *type, PortMap channels)
235 : ServicePort(id, type, channels), acc(acc) {}
236
238 AppID id, BundleType *type,
239 WriteChannelPort &result,
240 ReadChannelPort &arg) {
241 return new Callback(acc, id, type, {{"arg", arg}, {"result", result}});
242}
243
245 std::function<MessageData(const MessageData &)> callback, bool quick) {
246 if (channels.size() != 2)
247 throw std::runtime_error("CallService must have exactly two channels");
248 result = &getRawWrite("result");
249 result->connect();
250 arg = &getRawRead("arg");
251 if (quick) {
252 // If it's quick, we can just call the callback directly.
253 arg->connect([this, callback](MessageData argMsg) -> bool {
254 MessageData resultMsg = callback(std::move(argMsg));
255 this->result->write(std::move(resultMsg));
256 return true;
257 });
258 } else {
259 // If it's not quick, we need to use the service thread.
260 arg->connect();
261 acc.getServiceThread()->addListener(
262 {arg}, [this, callback](ReadChannelPort *, MessageData argMsg) -> void {
263 MessageData resultMsg = callback(std::move(argMsg));
264 this->result->write(std::move(resultMsg));
265 });
266 }
267}
268
270 Service::Type svcType, AppIDPath id,
271 std::string implName,
272 ServiceImplDetails details,
273 HWClientDetails clients) {
274 // TODO: Add a proper registration mechanism.
275 if (svcType == typeid(FuncService))
276 return new FuncService(id, *acc, details, clients);
277 if (svcType == typeid(CallService))
278 return new CallService(*acc, id, details);
279 if (svcType == typeid(CustomService))
280 return new CustomService(id, *acc, details, clients);
281 return nullptr;
282}
283
285 // TODO: Add a proper registration mechanism.
286 if (svcName == "esi.service.std.func")
287 return typeid(FuncService);
288 if (svcName == "esi.service.std.call")
289 return typeid(CallService);
290 if (svcName == MMIO::StdName)
291 return typeid(MMIO);
292 if (svcName == HostMem::StdName)
293 return typeid(HostMem);
294 return typeid(CustomService);
295}
constexpr uint32_t MAX_MANIFEST_SIZE
Definition Services.cpp:38
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:79
ServiceClass * getService(AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Get a typed reference to a particular service type.
virtual const BundleEngineMap & getEngineMapFor(AppIDPath id)
PortMap requestPorts(const AppIDPath &idPath, const BundleType *bundleType) const
Request ports for all the channels in a bundle.
Definition Engines.cpp:424
Services provide connections to 'bundles' – collections of named, unidirectional communication channe...
Definition Ports.h:226
const BundleType * type
Definition Ports.h:269
Bundles represent a collection of channels.
Definition Types.h:44
A logical chunk of data representing serialized data.
Definition Common.h:103
A ChannelPort which reads data from the accelerator.
Definition Ports.h:124
A ChannelPort which sends data to the accelerator.
Definition Ports.h:77
A function call which gets attached to a service port.
Definition Services.h:304
static Callback * get(AcceleratorConnection &acc, AppID id, BundleType *type, WriteChannelPort &result, ReadChannelPort &arg)
Definition Services.cpp:237
Callback(AcceleratorConnection &acc, AppID id, const BundleType *, PortMap channels)
Definition Services.cpp:233
void connect(std::function< MessageData(const MessageData &)> callback, bool quick=false)
Connect a callback to code which will be executed when the accelerator invokes the callback.
Definition Services.cpp:244
Service for servicing function calls from the accelerator.
Definition Services.h:294
virtual std::string getServiceSymbol() const override
Definition Services.cpp:226
CallService(AcceleratorConnection &acc, AppIDPath id, ServiceImplDetails details)
Definition Services.cpp:218
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get specialized port for this service to attach to the given appid path.
Definition Services.cpp:228
A service for which there are no standard services registered.
Definition Services.h:81
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get specialized port for this service to attach to the given appid path.
Definition Services.cpp:173
CustomService(AppIDPath idPath, AcceleratorConnection &, const ServiceImplDetails &details, const HWClientDetails &clients)
Definition Services.cpp:162
A function call which gets attached to a service port.
Definition Services.h:262
std::future< MessageData > call(const MessageData &arg)
Definition Services.cpp:212
static Function * get(AppID id, BundleType *type, WriteChannelPort &arg, ReadChannelPort &result)
Definition Services.cpp:194
Service for calling functions.
Definition Services.h:252
virtual std::string getServiceSymbol() const override
Definition Services.cpp:187
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get specialized port for this service to attach to the given appid path.
Definition Services.cpp:189
FuncService(AppIDPath id, AcceleratorConnection &, ServiceImplDetails details, HWClientDetails clients)
Definition Services.cpp:178
virtual std::string getServiceSymbol() const override
Definition Services.cpp:160
static constexpr std::string_view StdName
Definition Services.h:200
virtual std::vector< uint8_t > getCompressedManifest() const override
Return the zlib compressed JSON system manifest.
Definition Services.cpp:140
uint32_t getEsiVersion() const override
Get the ESI version number to check version compatibility.
Definition Services.cpp:133
A "slice" of some parent MMIO space.
Definition Services.h:159
virtual uint64_t read(uint32_t addr) const
Read a 64-bit value from this region, not the global address space.
Definition Services.cpp:119
MMIORegion(AppID id, MMIO *parent, RegionDescriptor desc)
Definition Services.cpp:117
virtual void write(uint32_t addr, uint64_t data)
Write a 64-bit value to this region, not the global address space.
Definition Services.cpp:124
virtual uint64_t read(uint32_t addr) const =0
Read a 64-bit value from the global MMIO space.
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const override
Get a MMIO region port for a particular region descriptor.
Definition Services.cpp:82
std::map< AppIDPath, RegionDescriptor > regions
MMIO base address table.
Definition Services.h:155
static constexpr std::string_view StdName
Definition Services.h:119
virtual Service * getChildService(Service::Type service, AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={}) override
If the service is a MMIO service, return a region of the MMIO space which peers into ours.
Definition Services.cpp:105
MMIO(AcceleratorConnection &, const HWClientDetails &clients)
Definition Services.cpp:56
virtual std::string getServiceSymbol() const override
Definition Services.cpp:79
Add a custom interface to a service client at a particular point in the design hierarchy.
Definition Services.h:36
static Service::Type lookupServiceType(const std::string &)
Resolve a service type from a string.
Definition Services.cpp:284
static Service * createService(AcceleratorConnection *acc, Service::Type svcType, AppIDPath id, std::string implName, ServiceImplDetails details, HWClientDetails clients)
Create a service instance from the given details.
Definition Services.cpp:269
Parent class of all APIs modeled as 'services'.
Definition Services.h:46
AcceleratorConnection & getConnection() const
Definition Services.h:72
const std::type_info & Type
Definition Services.h:48
virtual Service * getChildService(Service::Type service, AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Create a "child" service of this service.
Definition Services.cpp:28
AcceleratorConnection & conn
Definition Services.h:75
Information about the Accelerator system.
Definition Services.h:100
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
Definition Services.cpp:40
virtual std::vector< uint8_t > getCompressedManifest() const =0
Return the zlib compressed JSON system manifest.
virtual std::string getServiceSymbol() const override
Definition Services.cpp:35
Definition esi.py:1
constexpr uint64_t MagicNumber
Definition Accelerator.h:48
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:98
std::string toHex(void *val)
Definition Common.cpp:37
constexpr uint32_t MetadataOffset
Definition Accelerator.h:45
std::map< std::string, ChannelPort & > PortMap
Definition Ports.h:29
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:97
std::any value
Definition Common.h:58
A description of a hardware client.
Definition Common.h:91
Describe a region (slice) of MMIO space.
Definition Services.h:122