C++ Run-time Library
Loading...
Searching...
No Matches
platform.h
Go to the documentation of this file.
1// Copyright 2018 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#pragma once
16
17#include <dlfcn.h>
18#include <memory>
19#include <string>
20#include <cassert>
21
22#include "fletcher/status.h"
23
24#if defined(__MACH__)
25#define DYLIB_EXT ".dylib"
26#else
27#define DYLIB_EXT ".so"
28#endif
29
30namespace fletcher {
31
33class Platform {
34 public:
37 if (!terminated) {
38 platformTerminate(terminate_data);
39 }
40 }
41
49 static Status Make(const std::string &name, std::shared_ptr<Platform> *platform_out, bool quiet = true);
50
57 static Status Make(std::shared_ptr<Platform> *platform_out, bool quiet = true);
58
60 std::string name();
61
63 Status MmioToString(std::string *str, uint64_t start, uint64_t stop, bool quiet = false);
64
66 [[nodiscard]] Status Init() const { return Status(platformInit(init_data)); }
67
74 [[nodiscard]] Status WriteMMIO(uint64_t offset, uint32_t value) const { return Status(platformWriteMMIO(offset, value)); }
75
82 Status ReadMMIO(uint64_t offset, uint32_t *value) const { return Status(platformReadMMIO(offset, value)); }
83
90 Status ReadMMIO64(uint64_t offset, uint64_t *value);
91
98 Status DeviceMalloc(da_t *device_address, const int64_t size) const {
99 return Status(platformDeviceMalloc(device_address, size));
100 }
101
107 [[nodiscard]] Status DeviceFree(const da_t device_address) const { return Status(platformDeviceFree(device_address)); }
108
116 Status CopyHostToDevice(const uint8_t *host_source, const da_t device_destination, const int64_t size) const {
117 return Status(platformCopyHostToDevice(host_source, device_destination, size));
118 }
119
127 Status CopyDeviceToHost(const da_t device_source, uint8_t *host_destination, const int64_t size) const {
128 return Status(platformCopyDeviceToHost(device_source, host_destination, size));
129 }
130
139 Status PrepareHostBuffer(const uint8_t *host_source, da_t *device_destination, int64_t size, bool *alloced) const {
140 assert(platformPrepareHostBuffer != nullptr);
141 int ll_alloced = 0;
142 auto stat = platformPrepareHostBuffer(host_source, device_destination, size, &ll_alloced);
143 *alloced = ll_alloced == 1;
144 return Status(stat);
145 }
146
154 Status CacheHostBuffer(const uint8_t *host_source, da_t *device_destination, const int64_t size) const {
155 assert(platformCacheHostBuffer != nullptr);
156 return Status(platformCacheHostBuffer(host_source, device_destination, size));
157 }
158
164 assert(platformTerminate != nullptr);
165 terminated = true;
166 return Status(platformTerminate(terminate_data));
167 }
168
170 void *init_data = nullptr;
172 void *terminate_data = nullptr;
173
174 private:
175 // Functions to be linked:
176 fstatus_t (*platformGetName)(char *name, size_t size) = nullptr;
177 fstatus_t (*platformInit)(void *arg) = nullptr;
178 fstatus_t (*platformWriteMMIO)(uint64_t offset, uint32_t value) = nullptr;
179 fstatus_t (*platformReadMMIO)(uint64_t offset, uint32_t *value) = nullptr;
180 fstatus_t (*platformDeviceMalloc)(da_t *device_address, int64_t size) = nullptr;
181 fstatus_t (*platformDeviceFree)(da_t device_address) = nullptr;
182 fstatus_t (*platformCopyHostToDevice)(const uint8_t *host_source, da_t device_destination, int64_t size) = nullptr;
183 fstatus_t (*platformCopyDeviceToHost)(const da_t device_source, uint8_t *host_destination, int64_t size) = nullptr;
184 fstatus_t (*platformPrepareHostBuffer)(const uint8_t *host_source,
185 da_t *device_destination,
186 int64_t size,
187 int *alloced) = nullptr;
188 fstatus_t (*platformCacheHostBuffer)(const uint8_t *host_source, da_t *device_destination, int64_t size) = nullptr;
189 fstatus_t (*platformTerminate)(void *arg) = nullptr;
190
192 Status Link(void *handle, bool quiet = true);
193
195 bool terminated = false;
196};
197
198} // namespace fletcher
A Fletcher Platform. Links during run-time and abstracts access to lower-level platform-specific libr...
Definition platform.h:33
Status ReadMMIO(uint64_t offset, uint32_t *value) const
Read from an MMIO register.
Definition platform.h:82
Status CacheHostBuffer(const uint8_t *host_source, da_t *device_destination, const int64_t size) const
Cache a memory region of the host for use by the device. Always causes an allocation and copy.
Definition platform.h:154
Status DeviceFree(const da_t device_address) const
Free a previously allocated memory region on the device.
Definition platform.h:107
Status WriteMMIO(uint64_t offset, uint32_t value) const
Write to an MMIO register.
Definition platform.h:74
void * terminate_data
Data for platform termination.
Definition platform.h:172
Status Init() const
Initialize the platform.
Definition platform.h:66
std::string name()
Return the name of the platform.
Status CopyHostToDevice(const uint8_t *host_source, const da_t device_destination, const int64_t size) const
Copy data from host memory to device memory.
Definition platform.h:116
~Platform()
Platform destructor.
Definition platform.h:36
static Status Make(const std::string &name, std::shared_ptr< Platform > *platform_out, bool quiet=true)
Create a new platform instance.
Status ReadMMIO64(uint64_t offset, uint64_t *value)
Read 64 bit value from two successive 32 bit MMIO registers. The lower register will go to the lower ...
Status CopyDeviceToHost(const da_t device_source, uint8_t *host_destination, const int64_t size) const
Copy data from device memory to host memory.
Definition platform.h:127
Status PrepareHostBuffer(const uint8_t *host_source, da_t *device_destination, int64_t size, bool *alloced) const
Prepare a memory region of the host for use by the device. May or may not involve a copy (see MemType...
Definition platform.h:139
static Status Make(std::shared_ptr< Platform > *platform_out, bool quiet=true)
Create a new platform by attempting to autodetect the platform driver.
void * init_data
Data for platform initialization.
Definition platform.h:170
Status MmioToString(std::string *str, uint64_t start, uint64_t stop, bool quiet=false)
Print the contents of the MMIO registers within some range.
Status DeviceMalloc(da_t *device_address, const int64_t size) const
Allocate a region of memory on the device.
Definition platform.h:98
Status Terminate()
Terminate the platform.
Definition platform.h:163
Contains all Fletcher classes and functions for use in run-time applications.
Definition api.h:34
Status return value of all Fletcher run-time functions.
Definition status.h:25