Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting AWS changes to XRT Master Branch #8786

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions src/runtime_src/core/common/drv/include/kds_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ void kds_reset(struct kds_sched *kds);
int kds_cfg_update(struct kds_sched *kds);
void kds_cus_irq_enable(struct kds_sched *kds, bool enable);
int is_bad_state(struct kds_sched *kds);
u32 kds_get_open_clients(struct kds_sched *kds, pid_t **plist);
u32 kds_live_clients(struct kds_sched *kds, pid_t **plist);
u32 kds_live_clients_nolock(struct kds_sched *kds, pid_t **plist);
int kds_add_cu(struct kds_sched *kds, struct xrt_cu *xcu);
Expand Down
31 changes: 31 additions & 0 deletions src/runtime_src/core/common/drv/kds_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,37 @@ int is_bad_state(struct kds_sched *kds)
return kds->bad_state;
}

u32 kds_get_open_clients(struct kds_sched *kds, pid_t **plist)
{
const struct list_head *ptr;
struct kds_client *client;
pid_t *pl = NULL;
u32 count = kds->num_client;
u32 i = 0;

if (count == 0 || plist == NULL)
return 0;

mutex_lock(&kds->lock);
/* Collect list of PIDs of active client */
pl = (pid_t *)vmalloc(sizeof(pid_t) * count);
if (pl == NULL)
return 0;

/* Get Open clients */
list_for_each(ptr, &kds->clients) {
client = list_entry(ptr, struct kds_client, link);
pl[i] = pid_nr(client->pid);
i++;
}

*plist = pl;
mutex_unlock(&kds->lock);

return count;
}


u32 kds_live_clients(struct kds_sched *kds, pid_t **plist)
{
u32 count = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,10 @@ static int __init awsmgmt_init(void)

printk(KERN_INFO DRV_NAME " init()\n");
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
xrt_class = class_create(THIS_MODULE, DRV_NAME);
awsmgmt_class = class_create(THIS_MODULE, DRV_NAME);
#else
awsmgmt_class = class_create(DRV_NAME);
#endif
#endif
if (IS_ERR(awsmgmt_class))
return PTR_ERR(awsmgmt_class);
res = alloc_chrdev_region(&awsmgmt_devnode, AWSMGMT_MINOR_BASE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,25 @@ static const char *get_uuid_from_firmware(struct platform_device *pdev,
return NULL;
}

static void set_vbnv_name(struct platform_device *pdev)
{
xdev_handle_t xdev = xocl_get_xdev(pdev);
char sh_version[9];
struct feature_rom *rom = platform_get_drvdata(pdev);
uint32_t idcode = xocl_icap_get_data(xdev, IDCODE); /* Get the shell version from AWS interface */

/* return if the instance is F1 as its already updated in feature_rom_probe(),
* The F1 instance is static and is not going to change in future.
*/
if (idcode == 0x04261818)
return;

snprintf(sh_version, sizeof(sh_version), "%lx", idcode);
memset(rom->header.VBNVName, 0, sizeof(rom->header.VBNVName));
strncpy(rom->header.VBNVName, AWS_F2_XDMA_SHELL_NAME, strlen(AWS_F2_XDMA_SHELL_NAME));
strncpy(&rom->header.VBNVName[32], sh_version, sizeof(sh_version)-1);
}

static inline bool is_multi_rp(struct feature_rom *rom)
{
return strlen(rom->uuid) > 0;
Expand Down Expand Up @@ -581,6 +600,7 @@ static struct xocl_rom_funcs rom_ops = {
.load_firmware = load_firmware,
.passthrough_virtualization_on = passthrough_virtualization_on,
.get_uuid = get_uuid,
.set_vbnv_name = set_vbnv_name,
};

static int get_header_from_peer(struct feature_rom *rom)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ int xocl_kds_reconfig(struct xocl_dev *xdev);
int xocl_cu_map_addr(struct xocl_dev *xdev, u32 cu_idx,
struct drm_file *filp, unsigned long size, u32 *addrp);
u32 xocl_kds_live_clients(struct xocl_dev *xdev, pid_t **plist);
u32 xocl_kds_get_open_clients(struct xocl_dev *xdev, pid_t **plist);
int xocl_kds_update(struct xocl_dev *xdev, struct drm_xocl_kds kds_cfg);
void xocl_kds_cus_enable(struct xocl_dev *xdev);
void xocl_kds_cus_disable(struct xocl_dev *xdev);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ int xocl_refresh_subdevs(struct xocl_dev *xdev)
if (ret)
goto failed;

/* Update AWS shell version (vbnv name of rom subdev) as peer communication is established */
if (xocl_is_aws(xdev))
xocl_rom_set_vbnv_name(xdev);

if (!offset)
checksum = resp->checksum;

Expand Down
77 changes: 49 additions & 28 deletions src/runtime_src/core/pcie/driver/linux/xocl/userpf/xocl_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include <linux/eventfd.h>
#include <linux/uuid.h>
#include <linux/delay.h>
#include <linux/fdtable.h>
#include <linux/fs.h>

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
#include <linux/hashtable.h>
#endif
Expand Down Expand Up @@ -530,30 +533,33 @@ xocl_resolver(struct xocl_dev *xdev, struct axlf *axlf, xuid_t *xclbin_id,
return ret;
}

/* This is a Workaround function for AWS F2 to reset the clock registers.
* This function also incurs a delay of 10seconds to work around AWS ocl timeout issue.
* These changes will be removed once the issue is addressed in AWS F2 instance.
*/
static void aws_reset_clock_registers(xdev_handle_t xdev)
/* This function closes all the FDs associated with DRM */
void xocl_close_drm_render_fds(pid_t pid)
{
struct xocl_dev_core *core = XDEV(xdev);
resource_size_t bar0_clk1, bar0_clk2;
void __iomem *vbar0_clk1, *vbar0_clk2;

userpf_info(xdev, "AWS F2 WA, waiting to reset clock registers after Load ");
msleep(10000);

bar0_clk1 = pci_resource_start(core->pdev, 0) + 0x4058014;
bar0_clk2 = pci_resource_start(core->pdev, 0) + 0x4058010;
vbar0_clk1 = ioremap_nocache(bar0_clk1, 32);
vbar0_clk2 = ioremap_nocache(bar0_clk2, 32);

iowrite32(0, vbar0_clk1);
iowrite32(0, vbar0_clk2);

iounmap(vbar0_clk1);
iounmap(vbar0_clk2);
return;
struct fdtable *fdt;
struct file *file;
int fd;
struct pid *client_pid = find_get_pid(pid);
struct task_struct *task = pid_task(client_pid, PIDTYPE_PID);

if (!task || !task->files)
{
return;
}

spin_lock(&task->files->file_lock);
fdt = files_fdtable(task->files);

for (fd = 0; fd < fdt->max_fds; fd++) {
file = files_lookup_fd_raw(task->files, fd);
if (file && file->f_path.dentry) {
const char *path = file->f_path.dentry->d_name.name;
if (strstr(path, "renderD") != NULL) {
filp_close(file, task->files);
}
}
}
spin_unlock(&task->files->file_lock);
}

int
Expand Down Expand Up @@ -665,6 +671,20 @@ xocl_read_axlf_helper(struct xocl_drm *drm_p, struct drm_xocl_axlf *axlf_ptr,
if (err)
goto out_done;
}
/* For a preload AWS device id, PCI rescan is performed post axlf load,
* so, close all the DRM Render FDs opened during the context
* */
if((core->pdev->device == 0x1042/*AWS F1*/) || (core->pdev->device == 0x9048/*AWS F2*/))
{
pid_t *plist = NULL;
u32 i=0, clients =xocl_kds_get_open_clients(xdev, &plist);
for (i = 0; i < clients; i++)
{
xocl_close_drm_render_fds(plist[i]);
}
/* Free the pid list allocated by kds core. */
vfree(plist);
}

/* All contexts are closed. No outstanding commands */
axlf_obj = XDEV(xdev)->axlf_obj[slot_id];
Expand Down Expand Up @@ -793,11 +813,12 @@ xocl_read_axlf_helper(struct xocl_drm *drm_p, struct drm_xocl_axlf *axlf_ptr,
}
else {
userpf_info(xdev, "Loaded xclbin %pUb", &bin_obj.m_header.uuid);
/* Work around added for AWS F2 Instance to perform delay and reset clock registers */
if(core->pdev->device == 0xf010)
{
aws_reset_clock_registers(xdev);
}
/* Work around added for AWS F2 Instance to perform delay */
if(core->pdev->device == 0xf010)
{
userpf_info(xdev, "AWS F2 WA, waiting after AFI Load ");
msleep(10000);
}
}

out_done:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,11 @@ u32 xocl_kds_live_clients(struct xocl_dev *xdev, pid_t **plist)
return kds_live_clients(&XDEV(xdev)->kds, plist);
}

u32 xocl_kds_get_open_clients(struct xocl_dev *xdev, pid_t **plist)
{
return kds_get_open_clients(&XDEV(xdev)->kds, plist);
}

static int xocl_kds_get_mem_idx(struct xocl_dev *xdev, int ip_index,
uint32_t slot_id)
{
Expand Down
3 changes: 3 additions & 0 deletions src/runtime_src/core/pcie/driver/linux/xocl/xocl_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ struct xocl_rom_funcs {
size_t *fw_size);
bool (*passthrough_virtualization_on)(struct platform_device *pdev);
char *(*get_uuid)(struct platform_device *pdev);
void (*set_vbnv_name)(struct platform_device *pdev);
};

#define ROM_DEV(xdev) \
Expand Down Expand Up @@ -761,6 +762,8 @@ struct xocl_rom_funcs {
ROM_OPS(xdev)->passthrough_virtualization_on(ROM_DEV(xdev)) : false)
#define xocl_rom_get_uuid(xdev) \
(ROM_CB(xdev, get_uuid) ? ROM_OPS(xdev)->get_uuid(ROM_DEV(xdev)) : NULL)
#define xocl_rom_set_vbnv_name(xdev) \
(ROM_CB(xdev, set_vbnv_name) ? ROM_OPS(xdev)->set_vbnv_name(ROM_DEV(xdev)) : NULL)

/* version_ctrl callbacks */
struct xocl_version_ctrl_funcs {
Expand Down
64 changes: 57 additions & 7 deletions src/runtime_src/core/pcie/tools/cloud-daemon/aws/aws_dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,28 @@ static void awsPciRescan(int index)
//tell the user xclbin load ioctl can be re-issued now
xrt_core::pci::get_dev(index)->sysfs_put("", "dev_hotplug_done", err, 1);
}
#endif

/*
This thread sets the clock frequencies dynamically using aws_clkgen_set_dynamic() API.
The intention to set these frequencies after image load is to perform clock module reset
in the back ground which is a work around required to overcome the double image load Hang issue
observed on the AWS F2 Instance.

NOTE: This Thread has to run Asynchronously in order to avoid the double load Instance Hang issue that
is seen on F2 instance. The delay 10 secs can be removed once the double load issue on AWS instance is
addressed by AWS team.
*/
static void aws_clkgen_set_dynamic_thread(int slot_id, uint32_t clk_b_freq, uint32_t clk_c_freq, uint32_t clk_hbm_freq)
{
int ret = 0;

std::this_thread::sleep_for(std::chrono::seconds(10)); // Wait for 10 seconds
ret = aws_clkgen_set_dynamic(slot_id, 125 /*default max */, clk_b_freq,
clk_c_freq, clk_hbm_freq, 0);
if (ret != 0)
std::cerr << "aws_clkgen_set_dynamic_freq failed" << std::endl;
}
#endif
/*
* On AWS F1, fpga user PF without xclbin being loaded (cleared) has different
* device id (0x1042) than that of the user PF with xclbin being loaded (0xf010)
Expand Down Expand Up @@ -533,6 +553,25 @@ int AwsDev::awsLoadXclBin(const xclBin *buffer)
opt.afi_id = afi_id;
opt.slot_id = mBoardNumber;
retVal = fpga_mgmt_load_local_image_with_options(&opt);

if(imageInfoOld.spec.map[FPGA_APP_PF].device_id == 0xf010)
{
const clock_freq_topology* clockSection = get_clock_freq_from_axlf(axlfbuffer);
uint32_t clock_mains[4];

if(clockSection) {
for(int32_t i = 0; i < clockSection->m_count; i++) {
const struct clock_freq* clk = &(clockSection->m_clock_freq[i]);
clock_mains[i] = clk->m_freq_Mhz;
}
}
syslog(LOG_ERR, "WA to skip id=%x", imageInfoOld.spec.map[FPGA_APP_PF].device_id);
#ifndef INTERNAL_TESTING_FOR_AWS
auto future = std::async(std::launch::async, aws_clkgen_set_dynamic_thread, mBoardNumber, clock_mains[2]/*clk_extra_b0*/, clock_mains[3]/*clk_extra_c0*/, clock_mains[1]/*hbm*/);
#endif
return 0;
}

if (retVal == FPGA_ERR_DRAM_DATA_RETENTION_NOT_POSSIBLE ||
retVal == FPGA_ERR_DRAM_DATA_RETENTION_FAILED ||
retVal == FPGA_ERR_DRAM_DATA_RETENTION_SETUP_FAILED) {
Expand Down Expand Up @@ -595,13 +634,14 @@ int AwsDev::awsGetIcap(xcl_pr_region *data)
#else
fpga_mgmt_image_info imageInfo;
fpga_mgmt_describe_local_image( mBoardNumber, &imageInfo, 0 );
FIELD(data, freq, 0) = imageInfo.metrics.clocks[0].frequency[0] / 1000000;
FIELD(data, freq, 1) = imageInfo.metrics.clocks[1].frequency[0] / 1000000;
FIELD(data, freq, 2) = imageInfo.metrics.clocks[2].frequency[0] / 1000000;
FIELD(data, freq_cntr, 0) = imageInfo.metrics.clocks[0].frequency[0] / 1000;
FIELD(data, freq_cntr, 1) = imageInfo.metrics.clocks[1].frequency[0] / 1000;
FIELD(data, freq_cntr, 2) = imageInfo.metrics.clocks[2].frequency[0] / 1000;
FIELD(data, freq, 0) = imageInfo.metrics.f2_metrics.clocks[0].frequency[0] / 1000000;
FIELD(data, freq, 1) = imageInfo.metrics.f2_metrics.clocks[1].frequency[0] / 1000000;
FIELD(data, freq, 2) = imageInfo.metrics.f2_metrics.clocks[2].frequency[0] / 1000000;
FIELD(data, freq_cntr, 0) = imageInfo.metrics.f2_metrics.clocks[0].frequency[0] / 1000;
FIELD(data, freq_cntr, 1) = imageInfo.metrics.f2_metrics.clocks[1].frequency[0] / 1000;
FIELD(data, freq_cntr, 2) = imageInfo.metrics.f2_metrics.clocks[2].frequency[0] / 1000;
data->data_retention = 1;
data->idcode = imageInfo.sh_version;
#endif
//do we need to save uuid of xclbin loaded so that we can return xclbin uuid here?
//seems not. we check afi before load new xclbin.
Expand Down Expand Up @@ -778,4 +818,14 @@ char *AwsDev::get_afi_from_axlf(const axlf *buffer)
return nullptr;
return afid;
}

const clock_freq_topology* AwsDev::get_clock_freq_from_axlf(const axlf *buffer)
{
const axlf_section_header *clk_header = xclbin::get_axlf_section(buffer, CLOCK_FREQ_TOPOLOGY);
char *clk_topo = const_cast<char *>(reinterpret_cast<const char *>(buffer));
clk_topo += clk_header->m_sectionOffset;

return (reinterpret_cast<const clock_freq_topology*>(clk_topo));
}

#endif
2 changes: 2 additions & 0 deletions src/runtime_src/core/pcie/tools/cloud-daemon/aws/aws_dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#ifndef INTERNAL_TESTING_FOR_AWS
#include "fpga_pci.h"
#include "fpga_mgmt.h"
#include "fpga_clkgen.h"
#include "hal/fpga_common.h"
#endif

Expand Down Expand Up @@ -115,6 +116,7 @@ class AwsDev
#else
int sleepUntilLoaded( const std::string &afi, fpga_mgmt_image_info* new_afi );
char* get_afi_from_axlf(const axlf * buffer);
const clock_freq_topology* get_clock_freq_from_axlf(const axlf *buffer);
int index;
#endif
};
Expand Down
Loading