CIRCT  19.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 }
81 
82 namespace {
83 class XrtMMIO : public MMIO {
84 public:
85  XrtMMIO(::xrt::ip &ip) : ip(ip) {}
86 
87  uint64_t read(uint32_t addr) const override {
88  auto lo = static_cast<uint64_t>(ip.read_register(addr));
89  auto hi = static_cast<uint64_t>(ip.read_register(addr + 0x4));
90  return (hi << 32) | lo;
91  }
92  void write(uint32_t addr, uint64_t data) override {
93  ip.write_register(addr, data);
94  ip.write_register(addr + 0x4, data >> 32);
95  }
96 
97 private:
98  ::xrt::ip &ip;
99 };
100 } // namespace
101 
102 std::map<std::string, ChannelPort &>
104  const BundleType *bundleType) {
105  return impl->requestChannelsFor(idPath, bundleType);
106 }
107 
109  std::string implName,
110  const ServiceImplDetails &details,
111  const HWClientDetails &clients) {
112  if (svcType == typeid(MMIO))
113  return new XrtMMIO(impl->ip);
114  else if (svcType == typeid(SysInfo))
115  return new MMIOSysInfo(getService<MMIO>());
116  return nullptr;
117 }
118 
REGISTER_ACCELERATOR("xrt", backends::xrt::XrtAccelerator)
Abstract class representing a connection to an accelerator.
Definition: Accelerator.h:78
Bundles represent a collection of channels.
Definition: Types.h:44
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Definition: Context.h:30
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:103
std::unique_ptr< Impl > impl
Definition: Xrt.h:52
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:108
Implement the SysInfo API for a standard MMIO protocol.
Definition: Services.h:102
Parent class of all APIs modeled as 'services'.
Definition: Services.h:42
const std::type_info & Type
Definition: Services.h:44
Information about the Accelerator system.
Definition: Services.h:77
def connect(destination, source)
Definition: support.py:37
Definition: esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition: Common.h:81
std::vector< HWClientDetail > HWClientDetails
Definition: Common.h:80
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