diff --git a/src/runtime_src/core/edge/drm/zocl/common/zocl_ioctl.c b/src/runtime_src/core/edge/drm/zocl/common/zocl_ioctl.c index 17236415189..96305dab7a5 100644 --- a/src/runtime_src/core/edge/drm/zocl/common/zocl_ioctl.c +++ b/src/runtime_src/core/edge/drm/zocl/common/zocl_ioctl.c @@ -86,6 +86,8 @@ zocl_info_cu_ioctl(struct drm_device *ddev, void *data, struct drm_file *filp) args->paddr = addr; args->apt_idx = apt_idx; args->cu_idx = cu_idx; + //update cu size based on the apt_index + args->cu_size = apts[apt_idx].size; return 0; } diff --git a/src/runtime_src/core/edge/drm/zocl/common/zocl_xclbin.c b/src/runtime_src/core/edge/drm/zocl/common/zocl_xclbin.c index dc78a12d140..dbe63674c6a 100644 --- a/src/runtime_src/core/edge/drm/zocl/common/zocl_xclbin.c +++ b/src/runtime_src/core/edge/drm/zocl/common/zocl_xclbin.c @@ -303,6 +303,9 @@ zocl_update_apertures(struct drm_zocl_dev *zdev, struct drm_zocl_slot *slot) int total = 0; int apt_idx = 0; int i = 0; + char kname[64] = {0}; + char *kname_p = NULL; + struct kernel_info *krnl_info = NULL; /* Update aperture should only happen when loading xclbin */ if (slot->ip) @@ -336,8 +339,18 @@ zocl_update_apertures(struct drm_zocl_dev *zdev, struct drm_zocl_slot *slot) apt = &zdev->cu_subdev.apertures[apt_idx]; apt->addr = ip->m_base_address; - apt->size = CU_SIZE; - apt->prop = ip->properties; + + strncpy(kname, ip->m_name, sizeof(kname)); + kname[sizeof(kname)-1] = '\0'; + kname_p = &kname[0]; + strncpy(kname, strsep(&kname_p, ":"), sizeof(kname)); + kname[sizeof(kname)-1] = '\0'; + krnl_info = zocl_query_kernel(slot, kname); + if (krnl_info && krnl_info->range >= CU_SIZE) + apt->size = krnl_info->range; + else + apt->size = CU_SIZE; + apt->prop = ip->properties; apt->cu_idx = -1; apt->slot_idx = slot->slot_idx; } diff --git a/src/runtime_src/core/edge/drm/zocl/edge/zocl_edge_xclbin.c b/src/runtime_src/core/edge/drm/zocl/edge/zocl_edge_xclbin.c index 60fdf88a218..44139150689 100644 --- a/src/runtime_src/core/edge/drm/zocl/edge/zocl_edge_xclbin.c +++ b/src/runtime_src/core/edge/drm/zocl/edge/zocl_edge_xclbin.c @@ -373,10 +373,6 @@ zocl_xclbin_read_axlf(struct drm_zocl_dev *zdev, struct drm_zocl_axlf *axlf_obj, if (ret) goto out0; - ret = zocl_update_apertures(zdev, slot); - if (ret) - goto out0; - /* Kernels are slot specific. */ if (slot->kernels != NULL) { vfree(slot->kernels); @@ -399,6 +395,10 @@ zocl_xclbin_read_axlf(struct drm_zocl_dev *zdev, struct drm_zocl_axlf *axlf_obj, slot->kernels = kernels; } + ret = zocl_update_apertures(zdev, slot); + if (ret) + goto out0; + zocl_clear_mem_slot(zdev, slot->slot_idx); /* Initialize the memory for the new xclbin */ zocl_init_mem(zdev, slot); diff --git a/src/runtime_src/core/edge/include/zynq_ioctl.h b/src/runtime_src/core/edge/include/zynq_ioctl.h index 098dea39ad7..dd95e333f68 100644 --- a/src/runtime_src/core/edge/include/zynq_ioctl.h +++ b/src/runtime_src/core/edge/include/zynq_ioctl.h @@ -291,6 +291,7 @@ struct drm_zocl_info_cu { uint64_t paddr; int apt_idx; int cu_idx; + uint64_t cu_size; }; enum drm_zocl_ctx_code { diff --git a/src/runtime_src/core/edge/user/shim.cpp b/src/runtime_src/core/edge/user/shim.cpp index ceda5ffd43e..e99d17a5019 100644 --- a/src/runtime_src/core/edge/user/shim.cpp +++ b/src/runtime_src/core/edge/user/shim.cpp @@ -120,7 +120,7 @@ shim(unsigned index) : mCoreDevice(xrt_core::edge_linux::get_userpf_device(this, index)) , mBoardNumber(index) , mKernelClockFreq(100) - , mCuMaps(128, nullptr) + , mCuMaps(128, {nullptr, 0}) { xclLog(XRT_INFO, "%s", __func__); @@ -151,8 +151,8 @@ shim:: } for (auto p : mCuMaps) { - if (p) - (void) munmap(p, mCuMapSize); + if (p.first) + (void) munmap(p.first, p.second); } } @@ -177,16 +177,16 @@ mapKernelControl(const std::vector>& offsets) if ((offset_it->first & (~0xFF)) != (-1UL & ~0xFF)) { auto it = mKernelControl.find(offset_it->first); if (it == mKernelControl.end()) { - drm_zocl_info_cu info = {offset_it->first, -1, -1}; + drm_zocl_info_cu info = {offset_it->first, -1, -1, 0}; int result = ioctl(mKernelFD, DRM_IOCTL_ZOCL_INFO_CU, &info); if (result) { xclLog(XRT_ERROR, "%s: Failed to find CU info 0x%lx", __func__, offset_it->first); return -errno; } size_t psize = getpagesize(); - ptr = mmap(0, offset_it->second, PROT_READ | PROT_WRITE, MAP_SHARED, mKernelFD, info.apt_idx*psize); + ptr = mmap(0, info.cu_size, PROT_READ | PROT_WRITE, MAP_SHARED, mKernelFD, info.apt_idx*psize); if (ptr == MAP_FAILED) { - xclLog(XRT_ERROR, "%s: Map failed for aperture 0x%lx, size 0x%lx", __func__, offset_it->first, offset_it->second); + xclLog(XRT_ERROR, "%s: Map failed for aperture 0x%lx, size 0x%lx", __func__, offset_it->first, info.cu_size); return -1; } mKernelControl.insert(it, std::pair(offset_it->first, (uint32_t *)ptr)); @@ -766,7 +766,6 @@ xclLoadAxlf(const axlf *buffer) krnl->name[sizeof(krnl->name)-1] = '\0'; krnl->range = kernel.range; krnl->anums = kernel.args.size(); - krnl->features = 0; if (kernel.sw_reset) krnl->features |= KRNL_SW_RESET; @@ -1169,10 +1168,10 @@ xclCloseContext(const uuid_t xclbinId, unsigned int ipIndex) if (ipIndex < mCuMaps.size()) { // Make sure no MMIO register space access when CU is released. - uint32_t *p = mCuMaps[ipIndex]; + uint32_t *p = mCuMaps[ipIndex].first; if (p) { - (void) munmap(p, mCuMapSize); - mCuMaps[ipIndex] = nullptr; + (void) munmap(p, mCuMaps[ipIndex].second); + mCuMaps[ipIndex] = {nullptr, 0}; } } @@ -1196,21 +1195,22 @@ xclRegRW(bool rd, uint32_t ipIndex, uint32_t offset, uint32_t *datap) xclLog(XRT_ERROR, "%s: invalid CU index: %d", __func__, ipIndex); return -EINVAL; } - if (offset >= mCuMapSize || (offset & (sizeof(uint32_t) - 1)) != 0) { + if (offset <= 0 || (offset & (sizeof(uint32_t) - 1)) != 0) { xclLog(XRT_ERROR, "%s: invalid CU offset: %d", __func__, offset); return -EINVAL; } - if (mCuMaps[ipIndex] == nullptr) { - drm_zocl_info_cu info = {0, -1, (int)ipIndex}; + if (mCuMaps[ipIndex].first == nullptr) { + drm_zocl_info_cu info = {0, -1, (int)ipIndex, 0}; int result = ioctl(mKernelFD, DRM_IOCTL_ZOCL_INFO_CU, &info); - void *p = mmap(0, mCuMapSize, PROT_READ | PROT_WRITE, MAP_SHARED, + void *p = mmap(0, info.cu_size, PROT_READ | PROT_WRITE, MAP_SHARED, mKernelFD, info.apt_idx * getpagesize()); if (p != MAP_FAILED) - mCuMaps[ipIndex] = (uint32_t *)p; + mCuMaps[ipIndex].first = (uint32_t *)p; + mCuMaps[ipIndex].second = info.cu_size; } - uint32_t *cumap = mCuMaps[ipIndex]; + uint32_t *cumap = mCuMaps[ipIndex].first; if (cumap == nullptr) { xclLog(XRT_ERROR, "%s: can't map CU: %d", __func__, ipIndex); return -EINVAL; diff --git a/src/runtime_src/core/edge/user/shim.h b/src/runtime_src/core/edge/user/shim.h index 4ee00b1a980..5d55ebeba14 100644 --- a/src/runtime_src/core/edge/user/shim.h +++ b/src/runtime_src/core/edge/user/shim.h @@ -378,7 +378,7 @@ class shim { * Mapped CU register space for xclRegRead/Write(). We support at most * 128 CUs and each map is of 64k bytes. Does not support debug IP access. */ - std::vector mCuMaps; + std::vector> mCuMaps; const size_t mCuMapSize = 64 * 1024; std::mutex mCuMapLock; int xclRegRW(bool rd, uint32_t cu_index, uint32_t offset, uint32_t *datap);