Skip to content

Commit d05a028

Browse files
authored
Hw context level changes to add set/get freuquency apis (#9180)
Signed-off-by: Manoj Takasi <mtakasi@amd.com>
1 parent fe5eda0 commit d05a028

File tree

9 files changed

+168
-2
lines changed

9 files changed

+168
-2
lines changed

build/petalinux.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# When updating Petalinux build please file a SH ticket to retain the build
22
# https://jira.xilinx.com/secure/CreateIssue!default.jspa
3-
PETALINUX=/proj/petalinux/2025.2/petalinux-v2025.2_05300031/tool/petalinux-v2025.2-final
3+
PETALINUX=/proj/petalinux/2025.2/petalinux-v2025.2_08071216/tool/petalinux-v2025.2-final

src/runtime_src/core/common/api/xrt_hw_context.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include <limits>
2424
#include <memory>
2525

26+
namespace {
27+
static constexpr double hz_per_mhz = 1'000'000.0;
28+
}
29+
2630
namespace xrt {
2731

2832
// class hw_context_impl - insulated implemention of an xrt::hw_context
@@ -213,6 +217,43 @@ class hw_context_impl : public std::enable_shared_from_this<hw_context_impl>
213217

214218
throw std::runtime_error("no module found with given kernel name in ctx");
215219
}
220+
221+
double
222+
get_aie_freq() const
223+
{
224+
try {
225+
auto freq_hz = m_hdl->get_aie_freq();
226+
return static_cast<double>(freq_hz) / hz_per_mhz; // Convert Hz to MHz
227+
}
228+
catch (const xrt_core::error& e) {
229+
if (e.code() == std::errc::not_supported)
230+
throw std::runtime_error("get_aie_freq() API is not supported on this platform");
231+
232+
throw std::runtime_error("Failed to read AIE frequency: " + std::string(e.what()));
233+
}
234+
catch (const std::exception& e) {
235+
throw std::runtime_error("Failed to read AIE frequency: " + std::string(e.what()));
236+
}
237+
}
238+
239+
void
240+
set_aie_freq(double freq_mhz)
241+
{
242+
try {
243+
auto freq_hz = static_cast<uint64_t>(freq_mhz * hz_per_mhz);
244+
m_hdl->set_aie_freq(freq_hz);
245+
}
246+
catch (const xrt_core::error& e) {
247+
if (e.code() == std::errc::not_supported)
248+
throw std::runtime_error("set_aie_freq() API is not supported on this platform");
249+
250+
throw std::runtime_error("Failed to set AIE frequency: " + std::string(e.what()));
251+
}
252+
catch (const std::exception& e) {
253+
throw std::runtime_error("Failed to set AIE frequency: " + std::string(e.what()));
254+
}
255+
}
256+
216257
};
217258

218259
} // xrt
@@ -395,6 +436,20 @@ hw_context::
395436
////////////////////////////////////////////////////////////////
396437
namespace xrt::aie {
397438

439+
double
440+
hw_context::
441+
get_aie_freq() const
442+
{
443+
return get_handle()->get_aie_freq();
444+
}
445+
446+
void
447+
hw_context::
448+
set_aie_freq(double freq_mhz)
449+
{
450+
get_handle()->set_aie_freq(freq_mhz);
451+
}
452+
398453
void
399454
hw_context::
400455
reset_array()

src/runtime_src/core/common/shim/hwctx_handle.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ class hwctx_handle
119119
{
120120
throw xrt_core::error(std::errc::not_supported, __func__);
121121
}
122+
123+
virtual uint64_t
124+
get_aie_freq() const
125+
{
126+
throw xrt_core::error(std::errc::not_supported, __func__);
127+
}
128+
129+
virtual void
130+
set_aie_freq(uint64_t /*freq_hz */) const
131+
{
132+
throw xrt_core::error(std::errc::not_supported, __func__);
133+
}
134+
122135
};
123136

124137
} // xrt_core

src/runtime_src/core/edge/user/hwctx_object.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,17 @@ hwctx_object::reset_array() const
145145
#endif
146146
}
147147

148+
uint64_t
149+
hwctx_object::
150+
get_aie_freq() const
151+
{
152+
return m_shim->get_aie_freq(this);
153+
}
154+
155+
void
156+
hwctx_object::
157+
set_aie_freq(uint64_t freq_hz) const
158+
{
159+
return m_shim->set_aie_freq(this, freq_hz);
160+
}
148161
}

src/runtime_src/core/edge/user/hwctx_object.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ namespace zynqaie {
111111
void
112112
reset_array() const override;
113113

114+
uint64_t
115+
get_aie_freq() const override;
116+
117+
void
118+
set_aie_freq(uint64_t freq_hz)const override;
114119
#ifdef XRT_ENABLE_AIE
115120
std::shared_ptr<aie_array>
116121
get_aie_array_shared();

src/runtime_src/core/edge/user/shim.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,62 @@ openAIEContext(xrt::aie::access_mode am)
21652165
return ret ? -errno : ret;
21662166
}
21672167

2168+
uint64_t
2169+
shim::
2170+
get_aie_freq(const zynqaie::hwctx_object* hwctx_obj)
2171+
{
2172+
#ifdef XRT_ENABLE_AIE
2173+
if (!hwctx_obj)
2174+
throw xrt_core::error(-EINVAL, "Invalid hardware context object");
2175+
2176+
auto hw_ctx_id = hwctx_obj->get_slotidx();
2177+
auto partition_info = hwctx_obj->get_partition_info();
2178+
2179+
2180+
struct drm_zocl_aie_freq_scale aie_arg;
2181+
aie_arg.hw_ctx_id = hw_ctx_id;
2182+
aie_arg.partition_id = partition_info.partition_id;
2183+
aie_arg.freq = 0;
2184+
aie_arg.dir = 0;
2185+
2186+
int ret = ioctl(mKernelFD, DRM_IOCTL_ZOCL_AIE_FREQSCALE, &aie_arg);
2187+
2188+
if (ret)
2189+
throw xrt_core::error(-errno, boost::str(boost::format("Reading AIE frequency from hw_context(%d) partition(%d) failed") % hw_ctx_id % partition_info.partition_id));
2190+
2191+
return aie_arg.freq; // Return frequency in Hz
2192+
#else
2193+
throw xrt_core::error(std::errc::not_supported, "AIE is not enabled for this device");
2194+
#endif
2195+
}
2196+
2197+
void
2198+
shim::
2199+
set_aie_freq(const zynqaie::hwctx_object* hwctx_obj, uint64_t freq_hz)
2200+
{
2201+
#ifdef XRT_ENABLE_AIE
2202+
if (!hwctx_obj)
2203+
throw xrt_core::error(-EINVAL, "Invalid hardware context object");
2204+
2205+
auto hw_ctx_id = hwctx_obj->get_slotidx();
2206+
auto partition_info = hwctx_obj->get_partition_info();
2207+
2208+
struct drm_zocl_aie_freq_scale aie_arg;
2209+
aie_arg.hw_ctx_id = hw_ctx_id;
2210+
aie_arg.partition_id = partition_info.partition_id;
2211+
aie_arg.freq = freq_hz;
2212+
aie_arg.dir = 1;
2213+
2214+
int ret = ioctl(mKernelFD, DRM_IOCTL_ZOCL_AIE_FREQSCALE, &aie_arg);
2215+
2216+
if (ret)
2217+
throw xrt_core::error(-errno, boost::str(boost::format("Setting AIE frequency for hw_context(%d) partition(%d) to %lu Hz failed") % hw_ctx_id % partition_info.partition_id % freq_hz));
2218+
2219+
#else
2220+
throw xrt_core::error(std::errc::not_supported, "AIE is not enabled for this device");
2221+
#endif
2222+
}
2223+
21682224
xrt::aie::access_mode
21692225
shim::
21702226
getAIEAccessMode()

src/runtime_src/core/edge/user/shim.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ class shim {
363363
void open_graph_context(const zynqaie::hwctx_object* hwctx, const uuid_t xclbinId, unsigned int graph_id, xrt::graph::access_mode am);
364364
void close_graph_context(const zynqaie::hwctx_object* hwctx, unsigned int graph_id);
365365
int openAIEContext(xrt::aie::access_mode am);
366+
uint64_t get_aie_freq(const zynqaie::hwctx_object* hwctx_obj);
367+
void set_aie_freq(const zynqaie::hwctx_object* hwctx_obj, uint64_t freq_hz);
366368
xrt::aie::access_mode getAIEAccessMode();
367369
void setAIEAccessMode(xrt::aie::access_mode am);
368370
#endif

src/runtime_src/core/include/xrt/xrt_aie.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,28 @@ class hw_context : public xrt::hw_context
283283
*/
284284
void
285285
reset_array();
286+
/**
287+
* get_aie_freq() - Get current AIE frequency for this hardware context
288+
*
289+
* Return: Current frequency in MHz
290+
*
291+
* This function retrieves the current AIE frequency for the partition
292+
* associated with this hardware context.
293+
*/
294+
double
295+
get_aie_freq() const;
296+
297+
/**
298+
* set_aie_freq() - Set AIE frequency for this hardware context
299+
*
300+
* @freq_mhz: Frequency to set in MHz
301+
*
302+
* This function sets the AIE frequency for the partition
303+
* associated with this hardware context.
304+
*/
305+
void
306+
set_aie_freq(double /* freq_mhz*/);
307+
286308
};
287309

288310
class profiling_impl;

0 commit comments

Comments
 (0)