CIRCT 22.0.0git
Loading...
Searching...
No Matches
Services.h
Go to the documentation of this file.
1//===- StdServices.h - ESI standard services C++ API ------------*- C++ -*-===//
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// The APIs in this backend are all optionally implemented. The lower level
10// ones, however, are strongly recommended. 'Services' here refers to ESI
11// services. These are standard APIs into the standard ESI services.
12//
13// DO NOT EDIT!
14// This file is distributed as part of an ESI package. The source for this file
15// should always be modified within CIRCT.
16//
17//===----------------------------------------------------------------------===//
18
19// NOLINTNEXTLINE(llvm-header-guard)
20#ifndef ESI_RUNTIME_SERVICES_H
21#define ESI_RUNTIME_SERVICES_H
22
23#include "esi/Common.h"
24#include "esi/Context.h"
25#include "esi/Ports.h"
26
27#include <cstdint>
28
29namespace esi {
30class AcceleratorConnection;
31class Engine;
32namespace services {
33
34/// Add a custom interface to a service client at a particular point in the
35/// design hierarchy.
36class ServicePort : public BundlePort {
37public:
39 virtual ~ServicePort() = default;
40 // Get a description of the service port.
41 virtual std::optional<std::string> toString() const { return std::nullopt; }
42};
43
44/// Parent class of all APIs modeled as 'services'. May or may not map to a
45/// hardware side 'service'.
46class Service {
47public:
48 using Type = const std::type_info &;
50 virtual ~Service() = default;
51
52 virtual std::string getServiceSymbol() const = 0;
53
54 /// Create a "child" service of this service. Does not have to be the same
55 /// service type, but typically is. Used when a service already exists in the
56 /// active services table, but a new one wants to replace it. Useful for cases
57 /// where the child service needs to use the parent service. Defaults to
58 /// calling the `getService` method on `AcceleratorConnection` to get the
59 /// global service, implying that the child service does not need to use the
60 /// service it is replacing.
61 virtual Service *getChildService(Service::Type service, AppIDPath id = {},
62 std::string implName = {},
63 ServiceImplDetails details = {},
64 HWClientDetails clients = {});
65
66 /// Get specialized port for this service to attach to the given appid path.
67 /// Null returns mean nothing to attach.
68 virtual BundlePort *getPort(AppIDPath id, const BundleType *type) const {
69 return nullptr;
70 }
71
73
74protected:
76};
77
78/// A service for which there are no standard services registered. Requires
79/// ports be added to the design hierarchy instead of high level interfaces like
80/// the ones in StdServices.h.
81class CustomService : public Service {
82public:
84 const ServiceImplDetails &details,
85 const HWClientDetails &clients);
86 virtual ~CustomService() = default;
87
88 virtual std::string getServiceSymbol() const override {
89 return serviceSymbol;
90 }
91 virtual BundlePort *getPort(AppIDPath id,
92 const BundleType *type) const override;
93
94protected:
95 std::string serviceSymbol;
97};
98
99/// Information about the Accelerator system.
100class SysInfo : public Service {
101public:
102 using Service::Service;
103 virtual ~SysInfo() = default;
104
105 virtual std::string getServiceSymbol() const override;
106
107 /// Get the ESI version number to check version compatibility.
108 virtual uint32_t getEsiVersion() const = 0;
109
110 /// Return the JSON-formatted system manifest.
111 virtual std::string getJsonManifest() const;
112
113 /// Return the zlib compressed JSON system manifest.
114 virtual std::vector<uint8_t> getCompressedManifest() const = 0;
115};
116
117class MMIO : public Service {
118public:
119 static constexpr std::string_view StdName = "esi.service.std.mmio";
120
121 /// Describe a region (slice) of MMIO space.
123 uint32_t base;
124 uint32_t size;
125 };
126
127 MMIO(AcceleratorConnection &, const AppIDPath &idPath,
128 const HWClientDetails &clients);
129 virtual ~MMIO() = default;
130
131 /// Read a 64-bit value from the global MMIO space.
132 virtual uint64_t read(uint32_t addr) const = 0;
133 /// Write a 64-bit value to the global MMIO space.
134 virtual void write(uint32_t addr, uint64_t data) = 0;
135 /// Get the regions of MMIO space that this service manages. Otherwise known
136 /// as the base address table.
137 const std::map<AppIDPath, RegionDescriptor> &getRegions() const {
138 return regions;
139 }
140
141 /// If the service is a MMIO service, return a region of the MMIO space which
142 /// peers into ours.
143 virtual Service *getChildService(Service::Type service, AppIDPath id = {},
144 std::string implName = {},
145 ServiceImplDetails details = {},
146 HWClientDetails clients = {}) override;
147
148 virtual std::string getServiceSymbol() const override;
149
150 /// Get a MMIO region port for a particular region descriptor.
151 virtual BundlePort *getPort(AppIDPath id,
152 const BundleType *type) const override;
153
154private:
155 /// MMIO base address table.
156 std::map<AppIDPath, RegionDescriptor> regions;
157
158public:
159 /// A "slice" of some parent MMIO space.
160 class MMIORegion : public ServicePort {
161 friend class MMIO;
163
164 public:
165 /// Get the offset (and size) of the region in the parent (usually global)
166 /// MMIO address space.
167 virtual RegionDescriptor getDescriptor() const { return desc; };
168 /// Read a 64-bit value from this region, not the global address space.
169 virtual uint64_t read(uint32_t addr) const;
170 /// Write a 64-bit value to this region, not the global address space.
171 virtual void write(uint32_t addr, uint64_t data);
172
173 virtual std::optional<std::string> toString() const override {
174 return "MMIO region " + toHex(desc.base) + " - " +
176 }
177
178 private:
181 };
182};
183
184/// Implement the SysInfo API for a standard MMIO protocol.
185class MMIOSysInfo final : public SysInfo {
186public:
187 MMIOSysInfo(const MMIO *);
188
189 /// Get the ESI version number to check version compatibility.
190 uint32_t getEsiVersion() const override;
191
192 /// Return the zlib compressed JSON system manifest.
193 virtual std::vector<uint8_t> getCompressedManifest() const override;
194
195private:
196 const MMIO *mmio;
197};
198
199class HostMem : public Service {
200public:
201 static constexpr std::string_view StdName = "esi.service.std.hostmem";
202
203 using Service::Service;
204 virtual ~HostMem() = default;
205 virtual std::string getServiceSymbol() const override;
206
207 /// RAII memory region for host memory. Automatically frees the memory when
208 /// deconstructed.
210 virtual ~HostMemRegion() = default;
211 /// Get a pointer to the host memory.
212 virtual void *getPtr() const = 0;
213 /// Sometimes the pointer the device sees is different from the pointer the
214 /// host sees. Call this functon to get the device pointer.
215 virtual void *getDevicePtr() const { return getPtr(); }
216 operator void *() const { return getPtr(); }
217 virtual std::size_t getSize() const = 0;
218 /// Flush the memory region to ensure that the device sees the latest
219 /// contents. Because some platforms require it before DMA transactions, it
220 /// is recommended to call this before any DMA on all platforms. On
221 /// platforms which don't require it, it is a cheap no-op virtual method
222 /// call.
223 virtual void flush() {}
224 };
225
226 /// Options for allocating host memory.
227 struct Options {
228 bool writeable = false;
229 bool useLargePages = false;
230 };
231
232 /// In cases where necessary, enable host memory services.
233 virtual void start() {}
234
235 /// Allocate a region of host memory in accelerator accessible address space.
236 virtual std::unique_ptr<HostMemRegion> allocate(std::size_t size,
237 Options opts) const = 0;
238
239 /// Try to make a region of host memory accessible to the accelerator. Returns
240 /// 'false' on failure. It is optional for an accelerator backend to implement
241 /// this, so client code needs to have a fallback for when this returns
242 /// 'false'. On success, it is the client's responsibility to ensure that the
243 /// memory eventually gets unmapped.
244 virtual bool mapMemory(void *ptr, std::size_t size, Options opts) const {
245 return false;
246 }
247 /// Unmap memory which was previously mapped with 'mapMemory'. Undefined
248 /// behavior when called with a pointer which was not previously mapped.
249 virtual void unmapMemory(void *ptr) const {}
250};
251
252/// Service for calling functions.
253class FuncService : public Service {
254public:
256 HWClientDetails clients);
257
258 virtual std::string getServiceSymbol() const override;
259 virtual BundlePort *getPort(AppIDPath id,
260 const BundleType *type) const override;
261
262 /// A function call which gets attached to a service port.
263 class Function : public ServicePort {
264 friend class FuncService;
265 using ServicePort::ServicePort;
266
267 public:
270
271 void connect();
272 std::future<MessageData> call(const MessageData &arg);
273
274 const esi::Type *getArgType() const {
275 return dynamic_cast<const ChannelType *>(type->findChannel("arg").first)
276 ->getInner();
277 }
278
279 const esi::Type *getResultType() const {
280 return dynamic_cast<const ChannelType *>(
281 type->findChannel("result").first)
282 ->getInner();
283 }
284
285 virtual std::optional<std::string> toString() const override {
286 const esi::Type *argType = getArgType();
287 const esi::Type *resultType = getResultType();
288 return "function " + resultType->getID() + "(" + argType->getID() + ")";
289 }
290
291 private:
292 std::mutex callMutex;
295 bool connected = false;
296 };
297
298private:
299 std::string symbol;
300};
301
302/// Service for servicing function calls from the accelerator.
303class CallService : public Service {
304public:
306 ServiceImplDetails details);
307
308 virtual std::string getServiceSymbol() const override;
309 virtual BundlePort *getPort(AppIDPath id,
310 const BundleType *type) const override;
311
312 /// A function call which gets attached to a service port.
313 class Callback : public ServicePort {
314 friend class CallService;
317
318 public:
322
323 /// Connect a callback to code which will be executed when the accelerator
324 /// invokes the callback. The 'quick' flag indicates that the callback is
325 /// sufficiently fast that it could be called in the same thread as the
326 /// port callback.
327 void connect(std::function<MessageData(const MessageData &)> callback,
328 bool quick = false);
329
330 const esi::Type *getArgType() const {
331 return dynamic_cast<const ChannelType *>(type->findChannel("arg").first)
332 ->getInner();
333 }
334
335 const esi::Type *getResultType() const {
336 return dynamic_cast<const ChannelType *>(
337 type->findChannel("result").first)
338 ->getInner();
339 }
340
341 virtual std::optional<std::string> toString() const override {
342 const esi::Type *argType = getArgType();
343 const esi::Type *resultType = getResultType();
344 return "callback " + resultType->getID() + "(" + argType->getID() + ")";
345 }
346
347 private:
351 };
352
353private:
354 std::string symbol;
355};
356
357/// Service for retrieving telemetry data from the accelerator.
358class TelemetryService : public Service {
359public:
360 static constexpr std::string_view StdName = "esi.service.std.telemetry";
361
363 ServiceImplDetails details, HWClientDetails clients);
364
365 virtual std::string getServiceSymbol() const override;
366 virtual BundlePort *getPort(AppIDPath id,
367 const BundleType *type) const override;
368
369 /// A telemetry port which gets attached to a service port.
370 class Telemetry : public ServicePort {
371 friend class TelemetryService;
373
374 public:
377
378 void connect();
379 std::future<MessageData> read();
380
381 virtual std::optional<std::string> toString() const override {
382 const esi::Type *dataType =
383 dynamic_cast<const ChannelType *>(type->findChannel("data").first)
384 ->getInner();
385 return "telemetry " + dataType->getID();
386 }
387
388 private:
391 };
392
393 const std::map<AppIDPath, Telemetry *> &getTelemetryPorts() {
394 return telemetryPorts;
395 }
396
397private:
398 mutable std::map<AppIDPath, Telemetry *> telemetryPorts;
399};
400
401/// Registry of services which can be instantiated directly by the Accelerator
402/// class if the backend doesn't do anything special with a service.
404public:
405 /// Create a service instance from the given details. Returns nullptr if
406 /// 'svcType' isn't registered.
408 Service::Type svcType, AppIDPath id,
409 std::string implName,
410 ServiceImplDetails details,
411 HWClientDetails clients);
412
413 /// Resolve a service type from a string. If the string isn't recognized,
414 /// default to CustomService.
415 static Service::Type lookupServiceType(const std::string &);
416};
417
418} // namespace services
419} // namespace esi
420
421#endif // ESI_RUNTIME_SERVICES_H
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:79
Services provide connections to 'bundles' – collections of named, unidirectional communication channe...
Definition Ports.h:226
PortMap channels
Definition Ports.h:270
const BundleType * type
Definition Ports.h:269
BundlePort(AppID id, const BundleType *type, PortMap channels)
Construct a port.
Definition Ports.cpp:22
Bundles represent a collection of channels.
Definition Types.h:81
std::pair< const Type *, Direction > findChannel(std::string name) const
Definition Types.cpp:24
Channels are the basic communication primitives.
Definition Types.h:102
A logical chunk of data representing serialized data.
Definition Common.h:105
A ChannelPort which reads data from the accelerator.
Definition Ports.h:124
Root class of the ESI type system.
Definition Types.h:33
ID getID() const
Definition Types.h:39
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:313
static Callback * get(AcceleratorConnection &acc, AppID id, const BundleType *type, WriteChannelPort &result, ReadChannelPort &arg)
Definition Services.cpp:245
virtual std::optional< std::string > toString() const override
Definition Services.h:341
const esi::Type * getArgType() const
Definition Services.h:330
const esi::Type * getResultType() const
Definition Services.h:335
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:253
AcceleratorConnection & acc
Definition Services.h:350
Service for servicing function calls from the accelerator.
Definition Services.h:303
virtual std::string getServiceSymbol() const override
Definition Services.cpp:234
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:236
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:176
virtual ~CustomService()=default
virtual std::string getServiceSymbol() const override
Definition Services.h:88
A function call which gets attached to a service port.
Definition Services.h:263
const esi::Type * getArgType() const
Definition Services.h:274
const esi::Type * getResultType() const
Definition Services.h:279
std::future< MessageData > call(const MessageData &arg)
Definition Services.cpp:218
virtual std::optional< std::string > toString() const override
Definition Services.h:285
static Function * get(AppID id, BundleType *type, WriteChannelPort &arg, ReadChannelPort &result)
Definition Services.cpp:197
Service for calling functions.
Definition Services.h:253
virtual std::string getServiceSymbol() const override
Definition Services.cpp:190
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:192
virtual ~HostMem()=default
virtual std::string getServiceSymbol() const override
Definition Services.cpp:163
virtual void start()
In cases where necessary, enable host memory services.
Definition Services.h:233
static constexpr std::string_view StdName
Definition Services.h:201
virtual std::unique_ptr< HostMemRegion > allocate(std::size_t size, Options opts) const =0
Allocate a region of host memory in accelerator accessible address space.
virtual void unmapMemory(void *ptr) const
Unmap memory which was previously mapped with 'mapMemory'.
Definition Services.h:249
virtual bool mapMemory(void *ptr, std::size_t size, Options opts) const
Try to make a region of host memory accessible to the accelerator.
Definition Services.h:244
Implement the SysInfo API for a standard MMIO protocol.
Definition Services.h:185
virtual std::vector< uint8_t > getCompressedManifest() const override
Return the zlib compressed JSON system manifest.
Definition Services.cpp:143
uint32_t getEsiVersion() const override
Get the ESI version number to check version compatibility.
Definition Services.cpp:136
A "slice" of some parent MMIO space.
Definition Services.h:160
virtual uint64_t read(uint32_t addr) const
Read a 64-bit value from this region, not the global address space.
Definition Services.cpp:122
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:127
virtual RegionDescriptor getDescriptor() const
Get the offset (and size) of the region in the parent (usually global) MMIO address space.
Definition Services.h:167
virtual std::optional< std::string > toString() const override
Definition Services.h:173
virtual uint64_t read(uint32_t addr) const =0
Read a 64-bit value from the global MMIO space.
virtual ~MMIO()=default
virtual void write(uint32_t addr, uint64_t data)=0
Write a 64-bit value to 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:84
std::map< AppIDPath, RegionDescriptor > regions
MMIO base address table.
Definition Services.h:156
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:108
const std::map< AppIDPath, RegionDescriptor > & getRegions() const
Get the regions of MMIO space that this service manages.
Definition Services.h:137
virtual std::string getServiceSymbol() const override
Definition Services.cpp:81
Add a custom interface to a service client at a particular point in the design hierarchy.
Definition Services.h:36
virtual ~ServicePort()=default
virtual std::optional< std::string > toString() const
Definition Services.h:41
Registry of services which can be instantiated directly by the Accelerator class if the backend doesn...
Definition Services.h:403
static Service::Type lookupServiceType(const std::string &)
Resolve a service type from a string.
Definition Services.cpp:350
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:333
Parent class of all APIs modeled as 'services'.
Definition Services.h:46
virtual BundlePort * getPort(AppIDPath id, const BundleType *type) const
Get specialized port for this service to attach to the given appid path.
Definition Services.h:68
virtual std::string getServiceSymbol() const =0
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
Service(AcceleratorConnection &conn)
Definition Services.h:49
virtual ~Service()=default
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 uint32_t getEsiVersion() const =0
Get the ESI version number to check version compatibility.
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
virtual ~SysInfo()=default
A telemetry port which gets attached to a service port.
Definition Services.h:370
static Telemetry * get(AppID id, BundleType *type, WriteChannelPort &get, ReadChannelPort &data)
Definition Services.cpp:301
virtual std::optional< std::string > toString() const override
Definition Services.h:381
std::future< MessageData > read()
Definition Services.cpp:322
void connect()
Connect to a particular telemetry port.
Definition Services.cpp:308
Service for retrieving telemetry data from the accelerator.
Definition Services.h:358
const std::map< AppIDPath, Telemetry * > & getTelemetryPorts()
Definition Services.h:393
std::map< AppIDPath, Telemetry * > telemetryPorts
Definition Services.h:398
static constexpr std::string_view StdName
Definition Services.h:360
virtual std::string getServiceSymbol() const override
Definition Services.cpp:284
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:288
Definition esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:100
std::string toHex(void *val)
Definition Common.cpp:37
std::map< std::string, ChannelPort & > PortMap
Definition Ports.h:29
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:99
RAII memory region for host memory.
Definition Services.h:209
virtual void * getDevicePtr() const
Sometimes the pointer the device sees is different from the pointer the host sees.
Definition Services.h:215
virtual void * getPtr() const =0
Get a pointer to the host memory.
virtual void flush()
Flush the memory region to ensure that the device sees the latest contents.
Definition Services.h:223
virtual std::size_t getSize() const =0
Options for allocating host memory.
Definition Services.h:227
Describe a region (slice) of MMIO space.
Definition Services.h:122