CIRCT  20.0.0git
Xrt.cpp
Go to the documentation of this file.
1 //===- Xrt.cpp - ESI XRT device backend -------------------------*- 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 // 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/backends/Xrt.h"
17 #include "esi/Services.h"
18 
19 // XRT includes
20 #include "experimental/xrt_bo.h"
21 #include "experimental/xrt_device.h"
22 #include "experimental/xrt_ip.h"
23 #include "experimental/xrt_xclbin.h"
24 
25 #include <fstream>
26 #include <iostream>
27 #include <set>
28 
29 using namespace esi;
30 using namespace esi::services;
31 using namespace esi::backends::xrt;
32 
33 /// Parse the connection std::string and instantiate the accelerator. Connection
34 /// std::string format:
35 /// <xclbin>[:<device_id>]
36 /// wherein <device_id> is in BDF format.
37 std::unique_ptr<AcceleratorConnection>
38 XrtAccelerator::connect(Context &ctxt, std::string connectionString) {
39  std::string xclbin;
40  std::string device_id;
41 
42  size_t colon = connectionString.find(':');
43  if (colon == std::string::npos) {
44  xclbin = connectionString;
45  } else {
46  xclbin = connectionString.substr(0, colon);
47  device_id = connectionString.substr(colon + 1);
48  }
49 
50  return make_unique<XrtAccelerator>(ctxt, xclbin, device_id);
51 }
52 
54  constexpr static char kernel[] = "esi_kernel";
55 
56  Impl(std::string xclbin, std::string device_id) {
57  if (device_id.empty())
58  device = ::xrt::device(0);
59  else
60  device = ::xrt::device(device_id);
61 
62  auto uuid = device.load_xclbin(xclbin);
63  ip = ::xrt::ip(device, uuid, kernel);
64  }
65 
66  std::map<std::string, ChannelPort &> requestChannelsFor(AppIDPath,
67  const BundleType *) {
68  throw std::runtime_error("XRT does not support channel communication yet");
69  }
70 
71  ::xrt::device device;
72  ::xrt::ip ip;
73 };
74 
75 /// Construct and connect to a cosim server.
76 XrtAccelerator::XrtAccelerator(Context &ctxt, std::string xclbin,
77  std::string device_id)
79  impl = make_unique<Impl>(xclbin, device_id);
80 }
82 
83 namespace {
84 class XrtMMIO : public MMIO {
85 public:
86  XrtMMIO(::xrt::ip &ip) : ip(ip) {}
87 
88  uint64_t read(uint32_t addr) const override {
89  auto lo = static_cast<uint64_t>(ip.read_register(addr));
90  auto hi = static_cast<uint64_t>(ip.read_register(addr + 0x4));
91  return (hi << 32) | lo;
92  }
93  void write(uint32_t addr, uint64_t data) override {
94  ip.write_register(addr, data);
95  ip.write_register(addr + 0x4, data >> 32);
96  }
97 
98 private:
99  ::xrt::ip &ip;
100 };
101 } // namespace
102 
103 std::map<std::string, ChannelPort &>
105  const BundleType *bundleType) {
106  return impl->requestChannelsFor(idPath, bundleType);
107 }
108 
110  std::string implName,
111  const ServiceImplDetails &details,
112  const HWClientDetails &clients) {
113  if (svcType == typeid(MMIO))
114  return new XrtMMIO(impl->ip);
115  else if (svcType == typeid(SysInfo))
116  return new MMIOSysInfo(getService<MMIO>());
117  return nullptr;
118 }
119 
REGISTER_ACCELERATOR("xrt", backends::xrt::XrtAccelerator)
Abstract class representing a connection to an accelerator.
Definition: Accelerator.h:78
virtual void disconnect()
Disconnect from the accelerator cleanly.
Bundles represent a collection of channels.
Definition: Types.h:44
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Definition: Context.h:31
Connect to an ESI simulation.
Definition: Xrt.h:31
std::map< std::string, ChannelPort & > requestChannelsFor(AppIDPath, const BundleType *) override
Request the host side channel ports for a particular instance (identified by the AppID path).
Definition: Xrt.cpp:104
std::unique_ptr< Impl > impl
Definition: Xrt.h:53
virtual Service * createService(Service::Type service, AppIDPath path, std::string implName, const ServiceImplDetails &details, const HWClientDetails &clients) override
Called by getServiceImpl exclusively.
Definition: Xrt.cpp:109
Implement the SysInfo API for a standard MMIO protocol.
Definition: Services.h:180
Parent class of all APIs modeled as 'services'.
Definition: Services.h:45
const std::type_info & Type
Definition: Services.h:47
Information about the Accelerator system.
Definition: Services.h:93
def connect(destination, source)
Definition: support.py:37
Definition: esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition: Common.h:87
std::vector< HWClientDetail > HWClientDetails
Definition: Common.h:86
std::map< std::string, ChannelPort & > requestChannelsFor(AppIDPath, const BundleType *)
Definition: Xrt.cpp:66
Impl(std::string xclbin, std::string device_id)
Definition: Xrt.cpp:56