Skip to content

Commit

Permalink
Merge pull request #34 from maxzhen/sync
Browse files Browse the repository at this point in the history
Update source code
  • Loading branch information
maxzhen authored Apr 11, 2024
2 parents 37961e1 + fbe3ebc commit 5fd5cce
Show file tree
Hide file tree
Showing 41 changed files with 811,646 additions and 345 deletions.
9 changes: 8 additions & 1 deletion CMake/pkg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ install(DIRECTORY ${AMDXDNA_BINS_DIR}/firmware/
COMPONENT ${XDNA_COMPONENT}
FILES_MATCHING
PATTERN "*.sbin"
PATTERN "*.xclbin"
PATTERN "download_raw" EXCLUDE
)

install(DIRECTORY ${AMDXDNA_BINS_DIR}/download_raw/xbutil_validate/bins/
DESTINATION xrt/${XDNA_COMPONENT}/bins
COMPONENT ${XDNA_COMPONENT}
FILES_MATCHING
PATTERN "*.xclbin"
PATTERN "*.txt"
)

configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/CMake/config/postinst.in
${CMAKE_CURRENT_BINARY_DIR}/package/postinst
Expand Down
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2022-2023, Advanced Micro Devices, Inc. All rights reserved.
# Copyright (C) 2022-2024, Advanced Micro Devices, Inc. All rights reserved.

cmake_minimum_required(VERSION 3.19.0)
project("amd-xdna")
Expand Down Expand Up @@ -29,6 +29,22 @@ add_subdirectory(src)
# By default, build/build.sh downloads binaries to build/amdxdna_bins/
set(AMDXDNA_BINS_DIR ${CMAKE_BINARY_DIR}/../amdxdna_bins)

# Add common NPU support files
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tools/bins/dpu_sequence/
DESTINATION xrt/${XDNA_COMPONENT}/bins/dpu_sequence
COMPONENT ${XDNA_COMPONENT}
FILES_MATCHING
PATTERN "*.txt" # dpu sequences are txt files
)

# Add Phoenix NPU support files
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tools/bins/
DESTINATION xrt/${XDNA_COMPONENT}/bins
COMPONENT ${XDNA_COMPONENT}
FILES_MATCHING
PATTERN "*.xclbin"
)

set(XRT_SUBMOD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xrt)
set(XRT_SUBMOD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/xrt)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ A: Yes. For example, if you have 6.6-rc1 linux header install on your build mach
### Q: I'm developing amdxdna.ko driver module. How to enable XDNA_DBG() print?

A: XDNA_DBG() relies on Linux's CONFIG_DYNAMIC_DEBUG framework, see Linux's [dynamic debug howto page](https://www.kernel.org/doc/html/v6.5/admin-guide/dynamic-debug-howto.html) for details.
TL;DR, run `sudo insmod amdxdna.ko dyndbg==p` to enable XDNA_DBG() globally.
TL;DR, run `sudo insmod amdxdna.ko dyndbg=+pf` to enable XDNA_DBG() globally, where +pf means enable debug printing and print the function name.

### Q: When install XRT plugin DEB package, apt-get/dpkg tool failed. What to do next?

Expand Down
42 changes: 8 additions & 34 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,6 @@ build_example()
cd ..
}

download_xclbins()
{
#TODO: figure out the validate.xclbin location
local firmware_dir=${DOWNLOAD_BINS_DIR}/firmware

jq -c '.xclbins[]' "$INFO_JSON" |
while IFS= read -r line; do
local device=$(echo $line | jq -r '.device')
local pci_dev_id=$(echo $line | jq -r '.pci_device_id')
local pci_rev_id=$(echo $line | jq -r '.pci_revision_id')
local name=$(echo $line | jq -r '.name')
local url=$(echo $line | jq -r '.url')

if [[ -z "$url" ]]; then
echo "Empty URL for $device xclbin $name"
exit 1
fi

echo "Download $device XCLBIN $name :"
if [ ! -d "${firmware_dir}/${pci_dev_id}_${pci_rev_id}" ]; then
mkdir -p ${firmware_dir}/${pci_dev_id}_${pci_rev_id}
fi
wget -O ${firmware_dir}/${pci_dev_id}_${pci_rev_id}/$name $url

done
}

download_npufws()
{
local firmware_dir=${DOWNLOAD_BINS_DIR}/firmware
Expand All @@ -129,9 +102,10 @@ download_npufws()
fi

echo "Download $device NPUFW version $version:"
if [ ! -d "${firmware_dir}/${pci_dev_id}_${pci_rev_id}" ]; then
mkdir -p ${firmware_dir}/${pci_dev_id}_${pci_rev_id}
if [ -d "${firmware_dir}/${pci_dev_id}_${pci_rev_id}" ]; then
rm ${firmware_dir}/${pci_dev_id}_${pci_rev_id}
fi
mkdir -p ${firmware_dir}/${pci_dev_id}_${pci_rev_id}
wget -O ${firmware_dir}/${pci_dev_id}_${pci_rev_id}/$fw_name $url

done
Expand Down Expand Up @@ -246,9 +220,13 @@ if [[ $clean == 1 ]]; then
exit 0
fi

if [[ $example == 1 ]]; then
build_example
exit 0
fi

if [[ $package == 1 ]]; then
download_npufws
download_xclbins
package_targets $DEBUG_BUILD_TYPE
package_targets $RELEASE_BUILD_TYPE
exit 0
Expand All @@ -262,8 +240,4 @@ if [[ $debug == 1 ]]; then
build_targets $DEBUG_BUILD_TYPE
fi

if [[ $example == 1 ]]; then
build_example
fi

# vim: ts=2 sw=2
1 change: 0 additions & 1 deletion src/driver/CMake/config/dkms.conf.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
PACKAGE_NAME=@XDNA_DKMS_PKG_NAME@
PACKAGE_VERSION="@XRT_PLUGIN_VERSION_STRING@"
# Requires at least Linux 6.8 to compile
BUILD_EXCLUSIVE_KERNEL_MIN=6.8
MAKE[0]="cd driver/@XDNA_DRV_DIR@; make KERNEL_SRC=${kernel_source_dir}; cd ../.."
CLEAN="cd driver/@XDNA_DRV_DIR@; make clean KERNEL_SRC=${kernel_source_dir}; cd ../.."
Expand Down
75 changes: 52 additions & 23 deletions src/driver/amdxdna/amdxdna_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,43 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct dr
struct amdxdna_dev *xdna = to_xdna_dev(dev);
struct amdxdna_hwctx *hwctx;
int ret, idx;
u32 buf_size;
void *buf;
u64 val;

if (args->param_type >= DRM_AMDXDNA_HWCTX_CONFIG_NUM) {
XDNA_ERR(xdna, "Invalid HW context param type: %d", args->param_type);
if (!xdna->dev_info->ops->hwctx_config)
return -EOPNOTSUPP;

val = args->param_val;
buf_size = args->param_val_size;

switch (args->param_type) {
case DRM_AMDXDNA_HWCTX_CONFIG_CU:
/* For those types that param_val is pointer */
if (buf_size > PAGE_SIZE) {
XDNA_ERR(xdna, "Config CU param buffer too large");
return -E2BIG;
}

/* Hwctx needs to keep buf */
buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
if (!buf)
return -ENOMEM;

if (copy_from_user(buf, u64_to_user_ptr(val), buf_size)) {
kfree(buf);
return -EFAULT;
}

break;
case DRM_AMDXDNA_HWCTX_ASSIGN_DBG_BUF:
case DRM_AMDXDNA_HWCTX_REMOVE_DBG_BUF:
/* For those types that param_val is a value */
buf = NULL;
buf_size = 0;
break;
default:
XDNA_DBG(xdna, "Unknown HW context config type %d", args->param_type);
return -EINVAL;
}

Expand All @@ -255,17 +289,12 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct dr
goto unlock_srcu;
}

if (!xdna->dev_info->ops->hwctx_config) {
ret = -EOPNOTSUPP;
goto unlock_srcu;
}

mutex_lock(&xdna->dev_lock);
ret = xdna->dev_info->ops->hwctx_config(hwctx, args->param_type, args->param_val,
args->param_val_size);
ret = xdna->dev_info->ops->hwctx_config(hwctx, args->param_type, val, buf, buf_size);
mutex_unlock(&xdna->dev_lock);

unlock_srcu:
kfree(buf);
srcu_read_unlock(&client->hwctx_srcu, idx);
return ret;
}
Expand Down Expand Up @@ -320,21 +349,21 @@ amdxdna_arg_bos_lookup(struct amdxdna_client *client,
}
abo = to_xdna_obj(gobj);

spin_lock(&abo->lock);
mutex_lock(&abo->lock);
if (abo->pinned) {
spin_unlock(&abo->lock);
mutex_unlock(&abo->lock);
job->bos[i] = gobj;
continue;
}

ret = amdxdna_gem_pin_nolock(abo);
if (ret) {
spin_unlock(&abo->lock);
mutex_unlock(&abo->lock);
drm_gem_object_put(gobj);
goto put_shmem_bo;
}
abo->pinned = true;
spin_unlock(&abo->lock);
mutex_unlock(&abo->lock);

job->bos[i] = gobj;
}
Expand All @@ -346,11 +375,12 @@ amdxdna_arg_bos_lookup(struct amdxdna_client *client,
return ret;
}

static int amdxdna_cmds_submit(struct amdxdna_client *client, u32 hwctx_id,
struct amdxdna_gem_obj *cmd_bo, u32 cmd_bo_cnt,
u32 *bo_hdls, u32 bo_cnt, u64 *seq)
static int amdxdna_cmds_submit(struct amdxdna_client *client,
struct amdxdna_gem_obj *cmd_bo, u32 *bo_hdls,
struct amdxdna_drm_exec_cmd *args)
{
struct amdxdna_dev *xdna = client->xdna;
u32 bo_cnt = args->arg_bo_count;
struct amdxdna_sched_job *job;
struct amdxdna_hwctx *hwctx;
u32 *cu_mask;
Expand All @@ -368,10 +398,10 @@ static int amdxdna_cmds_submit(struct amdxdna_client *client, u32 hwctx_id,
}

idx = srcu_read_lock(&client->hwctx_srcu);
hwctx = idr_find(&client->hwctx_idr, hwctx_id);
hwctx = idr_find(&client->hwctx_idr, args->hwctx);
if (!hwctx) {
XDNA_DBG(xdna, "PID %d failed to get hwctx %d",
client->pid, hwctx_id);
client->pid, args->hwctx);
ret = -EINVAL;
goto unlock_srcu;
}
Expand Down Expand Up @@ -421,7 +451,7 @@ static int amdxdna_cmds_submit(struct amdxdna_client *client, u32 hwctx_id,
}
kref_init(&job->refcnt);

ret = xdna->dev_info->ops->cmd_submit(hwctx, job, seq);
ret = xdna->dev_info->ops->cmd_submit(hwctx, job, &args->seq);
if (ret)
goto put_fence;

Expand All @@ -432,7 +462,7 @@ static int amdxdna_cmds_submit(struct amdxdna_client *client, u32 hwctx_id,
* For here we can unlock SRCU.
*/
srcu_read_unlock(&client->hwctx_srcu, idx);
trace_xdna_job(job->hwctx->name, "job pushed", *seq);
trace_xdna_job(hwctx->name, "job pushed", args->seq);

return 0;

Expand Down Expand Up @@ -493,14 +523,13 @@ int amdxdna_drm_exec_cmd_ioctl(struct drm_device *dev, void *data, struct drm_fi
goto free_bo_hdls;
}

if(to_gobj(cmd_bo)->size < sizeof(struct amdxdna_cmd)) {
if (to_gobj(cmd_bo)->size < sizeof(struct amdxdna_cmd)) {
XDNA_DBG(xdna, "Bad cmd BO size: %ld", to_gobj(cmd_bo)->size);
ret = -EINVAL;
goto put_cmd_bo;
}

ret = amdxdna_cmds_submit(client, args->hwctx, cmd_bo, args->cmd_bo_count,
bo_hdls, args->arg_bo_count, &args->seq);
ret = amdxdna_cmds_submit(client, cmd_bo, bo_hdls, args);
if (ret) {
XDNA_DBG(xdna, "Submit cmds failed, ret %d", ret);
goto put_cmd_bo;
Expand Down
8 changes: 8 additions & 0 deletions src/driver/amdxdna/amdxdna_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ struct amdxdna_hwctx {
u32 num_tiles;
u32 mem_size;
u32 fw_ctx_id;
u32 col_list_len;
u32 *col_list;
u32 start_col;
u32 num_col;
#define HWCTX_STAT_INIT 0
Expand Down Expand Up @@ -87,6 +89,12 @@ struct amdxdna_sched_job {
struct drm_gem_object *bos[] __counted_by(bo_cnt);
};

static inline u32 amdxdna_hwctx_col_map(struct amdxdna_hwctx *hwctx)
{
return GENMASK(hwctx->start_col + hwctx->num_col - 1,
hwctx->start_col);
}

void amdxdna_job_put(struct amdxdna_sched_job *job);

void amdxdna_hwctx_remove_all(struct amdxdna_client *client);
Expand Down
24 changes: 23 additions & 1 deletion src/driver/amdxdna/amdxdna_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,33 @@ static int amdxdna_drm_get_info_ioctl(struct drm_device *dev, void *data, struct
{
struct amdxdna_drm_get_info *args = data;
struct amdxdna_dev *xdna = to_xdna_dev(dev);
int ret;

if (!xdna->dev_info->ops->get_info)
return -EOPNOTSUPP;

XDNA_DBG(xdna, "Request parameter %u", args->param);
return xdna->dev_info->ops->get_info(xdna, args);
mutex_lock(&xdna->dev_lock);
ret = xdna->dev_info->ops->get_info(xdna, args);
mutex_unlock(&xdna->dev_lock);
return ret;
}

static int amdxdna_drm_set_state_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
{
struct amdxdna_drm_set_state *args = data;
struct amdxdna_dev *xdna = to_xdna_dev(dev);
int ret = 0;

if (!xdna->dev_info->ops->set_state)
return -EOPNOTSUPP;

XDNA_DBG(xdna, "Request parameter %u", args->param);
mutex_lock(&xdna->dev_lock);
ret = xdna->dev_info->ops->set_state(xdna, args);
mutex_unlock(&xdna->dev_lock);

return ret;
}

static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = {
Expand All @@ -179,6 +200,7 @@ static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = {
DRM_IOCTL_DEF_DRV(AMDXDNA_WAIT_CMD, amdxdna_drm_wait_cmd_ioctl, 0),
/* Query */
DRM_IOCTL_DEF_DRV(AMDXDNA_GET_INFO, amdxdna_drm_get_info_ioctl, 0),
DRM_IOCTL_DEF_DRV(AMDXDNA_SET_STATE, amdxdna_drm_set_state_ioctl, DRM_ROOT_ONLY),
};

static const struct file_operations amdxdna_fops = {
Expand Down
3 changes: 2 additions & 1 deletion src/driver/amdxdna/amdxdna_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ struct amdxdna_dev_ops {
int (*resume)(struct amdxdna_dev *xdna);
void (*suspend)(struct amdxdna_dev *xdna);
int (*get_info)(struct amdxdna_dev *xdna, struct amdxdna_drm_get_info *args);
int (*set_state)(struct amdxdna_dev *xdna, struct amdxdna_drm_set_state *args);
int (*mmap)(struct amdxdna_dev *xdna, struct vm_area_struct *vma);
void (*debugfs)(struct amdxdna_dev *xdna);

int (*hwctx_init)(struct amdxdna_hwctx *hwctx);
void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);
int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, u32 size);
int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size);
void (*hwctx_suspend)(struct amdxdna_hwctx *hwctx);
void (*hwctx_resume)(struct amdxdna_hwctx *hwctx);
int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);
Expand Down
Loading

0 comments on commit 5fd5cce

Please sign in to comment.