#include <kunit/test.h>
#include <linux/firmware.h>
#include "dc.h"
#include "dc/inc/core_types.h"
#include "dc/inc/hw/dmcu.h"
#include "dc/inc/hw/abm.h"
#include "amdgpu_mode.h"
#include "amdgpu_dm.h"
#include "dal_asic_id.h"
#include "dm_services.h"
#include "dmub/dmub_srv.h"
#include "amdgpu_dm_dmub.h"
#define DM_TEST_FW_INST_CONST_BYTES (PSP_HEADER_BYTES_256 + 1024)
#define DM_TEST_FW_SIZE (DM_TEST_FW_INST_CONST_BYTES + 1024)
static void dummy_callback(struct amdgpu_device *adev,
struct dmub_notification *notify)
{
}
static bool dm_test_dmub_supported(struct dmub_srv *dmub)
{
return true;
}
static bool dm_test_dmub_unsupported(struct dmub_srv *dmub)
{
return false;
}
static bool dm_test_dmub_hw_initialized(struct dmub_srv *dmub)
{
return true;
}
static union dmub_fw_boot_status dm_test_dmub_fw_ready(struct dmub_srv *dmub)
{
union dmub_fw_boot_status status = { 0 };
status.bits.dal_fw = 1;
status.bits.mailbox_rdy = 1;
return status;
}
static union dmub_fw_boot_status dm_test_dmub_fw_not_ready(struct dmub_srv *dmub)
{
union dmub_fw_boot_status status = { 0 };
return status;
}
static void dm_test_dmub_init_reg_offsets(struct dmub_srv *dmub,
struct dc_context *ctx)
{
}
static bool dm_test_dmcu_init(struct dmcu *dmcu)
{
return true;
}
static bool dm_test_dmcu_is_initialized(struct dmcu *dmcu)
{
return true;
}
static const struct dmcu_funcs dm_test_dmcu_funcs = {
.dmcu_init = dm_test_dmcu_init,
.is_dmcu_initialized = dm_test_dmcu_is_initialized,
};
static struct dmub_srv *dm_test_alloc_dmub_srv(struct kunit *test)
{
struct dmub_srv *dmub_srv;
dmub_srv = kunit_kzalloc(test, sizeof(*dmub_srv), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmub_srv);
dmub_srv->sw_init = true;
dmub_srv->hw_init = true;
dmub_srv->power_state = DMUB_POWER_STATE_D0;
dmub_srv->hw_funcs.is_supported = dm_test_dmub_supported;
dmub_srv->hw_funcs.is_hw_init = dm_test_dmub_hw_initialized;
dmub_srv->hw_funcs.get_fw_status = dm_test_dmub_fw_ready;
dmub_srv->hw_funcs.init_reg_offsets = dm_test_dmub_init_reg_offsets;
return dmub_srv;
}
static const struct firmware *dm_test_alloc_dmub_fw(struct kunit *test)
{
struct dmcub_firmware_header_v1_0 *hdr;
struct firmware *fw;
u8 *data;
fw = kunit_kzalloc(test, sizeof(*fw), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fw);
data = kunit_kzalloc(test, DM_TEST_FW_SIZE, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, data);
hdr = (struct dmcub_firmware_header_v1_0 *)data;
hdr->header.ucode_array_offset_bytes = cpu_to_le32(0);
hdr->header.ucode_version = cpu_to_le32(DMUB_FW_VERSION(9, 9, 9));
hdr->inst_const_bytes = cpu_to_le32(DM_TEST_FW_INST_CONST_BYTES);
hdr->bss_data_bytes = cpu_to_le32(0);
fw->size = DM_TEST_FW_SIZE;
fw->data = data;
return fw;
}
static void dm_test_register_dmub_notify_callback_null_callback(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
KUNIT_EXPECT_FALSE(test, dm_register_dmub_notify_callback(adev,
DMUB_NOTIFICATION_AUX_REPLY, NULL, false));
}
static void dm_test_register_dmub_notify_callback_type_out_of_range(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
KUNIT_EXPECT_FALSE(test, dm_register_dmub_notify_callback(adev,
AMDGPU_DMUB_NOTIFICATION_MAX, dummy_callback, false));
}
static void dm_test_register_dmub_notify_callback_valid(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
KUNIT_EXPECT_TRUE(test, dm_register_dmub_notify_callback(adev,
DMUB_NOTIFICATION_AUX_REPLY, dummy_callback, true));
KUNIT_EXPECT_TRUE(test,
adev->dm.dmub_callback[DMUB_NOTIFICATION_AUX_REPLY] == dummy_callback);
KUNIT_EXPECT_TRUE(test,
adev->dm.dmub_thread_offload[DMUB_NOTIFICATION_AUX_REPLY]);
}
static void dm_test_register_dmub_notify_callback_offload_false(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
KUNIT_EXPECT_TRUE(test, dm_register_dmub_notify_callback(adev,
DMUB_NOTIFICATION_HPD, dummy_callback, false));
KUNIT_EXPECT_TRUE(test,
adev->dm.dmub_callback[DMUB_NOTIFICATION_HPD] == dummy_callback);
KUNIT_EXPECT_FALSE(test,
adev->dm.dmub_thread_offload[DMUB_NOTIFICATION_HPD]);
}
static void dm_test_dmub_aux_setconfig_callback_copies_and_completes(struct kunit *test)
{
struct amdgpu_device *adev;
struct dmub_notification *dm_notify;
struct dmub_notification notify = {};
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
dm_notify = kunit_kzalloc(test, sizeof(*dm_notify), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dm_notify);
init_completion(&adev->dm.dmub_aux_transfer_done);
adev->dm.dmub_notify = dm_notify;
notify.type = DMUB_NOTIFICATION_AUX_REPLY;
notify.result = AUX_RET_SUCCESS;
notify.aux_reply.command = 0xA5;
notify.aux_reply.length = 3;
notify.aux_reply.data[0] = 0x11;
notify.aux_reply.data[1] = 0x22;
notify.aux_reply.data[2] = 0x33;
dm_dmub_aux_setconfig_callback(adev, ¬ify);
KUNIT_EXPECT_EQ(test, dm_notify->type, notify.type);
KUNIT_EXPECT_EQ(test, dm_notify->result, notify.result);
KUNIT_EXPECT_EQ(test, dm_notify->aux_reply.command, notify.aux_reply.command);
KUNIT_EXPECT_EQ(test, dm_notify->aux_reply.length, notify.aux_reply.length);
KUNIT_EXPECT_EQ(test, dm_notify->aux_reply.data[0], notify.aux_reply.data[0]);
KUNIT_EXPECT_EQ(test, dm_notify->aux_reply.data[1], notify.aux_reply.data[1]);
KUNIT_EXPECT_EQ(test, dm_notify->aux_reply.data[2], notify.aux_reply.data[2]);
KUNIT_EXPECT_TRUE(test, completion_done(&adev->dm.dmub_aux_transfer_done));
}
static void dm_test_dmub_aux_setconfig_callback_non_aux_no_complete(struct kunit *test)
{
struct amdgpu_device *adev;
struct dmub_notification *dm_notify;
struct dmub_notification notify = {};
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
dm_notify = kunit_kzalloc(test, sizeof(*dm_notify), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dm_notify);
init_completion(&adev->dm.dmub_aux_transfer_done);
adev->dm.dmub_notify = dm_notify;
notify.type = DMUB_NOTIFICATION_HPD;
notify.result = AUX_RET_ERROR_TIMEOUT;
dm_dmub_aux_setconfig_callback(adev, ¬ify);
KUNIT_EXPECT_EQ(test, dm_notify->type, notify.type);
KUNIT_EXPECT_FALSE(test, completion_done(&adev->dm.dmub_aux_transfer_done));
}
static void dm_test_dmub_aux_setconfig_callback_aux_with_null_dm_notify(struct kunit *test)
{
struct amdgpu_device *adev;
struct dmub_notification notify = {};
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
init_completion(&adev->dm.dmub_aux_transfer_done);
adev->dm.dmub_notify = NULL;
notify.type = DMUB_NOTIFICATION_AUX_REPLY;
dm_dmub_aux_setconfig_callback(adev, ¬ify);
KUNIT_EXPECT_TRUE(test, completion_done(&adev->dm.dmub_aux_transfer_done));
}
static void dm_test_dmub_aux_setconfig_callback_set_config_reply(struct kunit *test)
{
struct amdgpu_device *adev;
struct dmub_notification *dm_notify;
struct dmub_notification notify = {};
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
dm_notify = kunit_kzalloc(test, sizeof(*dm_notify), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dm_notify);
init_completion(&adev->dm.dmub_aux_transfer_done);
adev->dm.dmub_notify = dm_notify;
notify.type = DMUB_NOTIFICATION_SET_CONFIG_REPLY;
notify.sc_status = SET_CONFIG_RX_TIMEOUT;
dm_dmub_aux_setconfig_callback(adev, ¬ify);
KUNIT_EXPECT_EQ(test, dm_notify->type, notify.type);
KUNIT_EXPECT_EQ(test, dm_notify->sc_status, notify.sc_status);
KUNIT_EXPECT_FALSE(test, completion_done(&adev->dm.dmub_aux_transfer_done));
}
static void dm_test_dmub_aux_fused_io_callback_copies_reply_and_completes(struct kunit *test)
{
struct amdgpu_device *adev;
struct dmub_notification notify = {};
struct dmub_cmd_fused_request *reply;
u32 reply_ddc_line;
u32 notify_ddc_line;
u32 reply_address;
u32 notify_address;
u32 reply_length;
u32 notify_length;
uint8_t ddc_line = 2;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
init_completion(&adev->dm.fused_io[ddc_line].replied);
notify.fused_request.identifier = 0x34;
notify.fused_request.status = FUSED_REQUEST_STATUS_SUCCESS;
notify.fused_request.u.aux.ddc_line = ddc_line;
notify.fused_request.u.aux.address = 0x50;
notify.fused_request.u.aux.length = 4;
dm_dmub_aux_fused_io_callback(adev, ¬ify);
KUNIT_EXPECT_TRUE(test, completion_done(&adev->dm.fused_io[ddc_line].replied));
reply = (struct dmub_cmd_fused_request *)adev->dm.fused_io[ddc_line].reply_data;
reply_ddc_line = reply->u.aux.ddc_line;
notify_ddc_line = notify.fused_request.u.aux.ddc_line;
reply_address = reply->u.aux.address;
notify_address = notify.fused_request.u.aux.address;
reply_length = reply->u.aux.length;
notify_length = notify.fused_request.u.aux.length;
KUNIT_EXPECT_EQ(test, reply->identifier, notify.fused_request.identifier);
KUNIT_EXPECT_EQ(test, reply->status, notify.fused_request.status);
KUNIT_EXPECT_EQ(test, reply_ddc_line, notify_ddc_line);
KUNIT_EXPECT_EQ(test, reply_address, notify_address);
KUNIT_EXPECT_EQ(test, reply_length, notify_length);
}
static void dm_test_dmub_aux_fused_io_callback_max_ddc_line(struct kunit *test)
{
struct amdgpu_device *adev;
struct dmub_notification notify = {};
struct dmub_cmd_fused_request *reply;
u32 reply_ddc_line;
u32 notify_ddc_line;
uint8_t ddc_line;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
ddc_line = ARRAY_SIZE(adev->dm.fused_io) - 1;
init_completion(&adev->dm.fused_io[ddc_line].replied);
notify.fused_request.identifier = 0x56;
notify.fused_request.status = FUSED_REQUEST_STATUS_SUCCESS;
notify.fused_request.u.aux.ddc_line = ddc_line;
notify.fused_request.u.aux.address = 0x50;
notify.fused_request.u.aux.length = 1;
dm_dmub_aux_fused_io_callback(adev, ¬ify);
KUNIT_EXPECT_TRUE(test, completion_done(&adev->dm.fused_io[ddc_line].replied));
reply = (struct dmub_cmd_fused_request *)adev->dm.fused_io[ddc_line].reply_data;
reply_ddc_line = reply->u.aux.ddc_line;
notify_ddc_line = notify.fused_request.u.aux.ddc_line;
KUNIT_EXPECT_EQ(test, reply->identifier, notify.fused_request.identifier);
KUNIT_EXPECT_EQ(test, reply_ddc_line, notify_ddc_line);
}
static void dm_test_dmub_aux_fused_io_callback_null_args(struct kunit *test)
{
struct dmub_notification notify = {};
dm_dmub_aux_fused_io_callback(NULL, ¬ify);
}
static void dm_test_get_default_ips_mode_dcn35(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 5, 0);
KUNIT_EXPECT_EQ(test, dm_get_default_ips_mode(adev),
DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF);
}
static void dm_test_get_default_ips_mode_dcn351(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 5, 1);
KUNIT_EXPECT_EQ(test, dm_get_default_ips_mode(adev),
DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF);
}
static void dm_test_get_default_ips_mode_dcn36(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 6, 0);
KUNIT_EXPECT_EQ(test, dm_get_default_ips_mode(adev),
DMUB_IPS_RCG_IN_ACTIVE_IPS2_IN_OFF);
}
static void dm_test_get_default_ips_mode_older_than_dcn35(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 2, 0);
KUNIT_EXPECT_EQ(test, dm_get_default_ips_mode(adev),
DMUB_IPS_DISABLE_ALL);
}
static void dm_test_get_default_ips_mode_newer_default(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(4, 0, 1);
KUNIT_EXPECT_EQ(test, dm_get_default_ips_mode(adev),
DMUB_IPS_ENABLE);
}
static struct amdgpu_device *dm_test_alloc_adev_with_dc(struct kunit *test)
{
struct amdgpu_device *adev;
struct dc_context *ctx;
struct dc *dc;
struct resource_pool *res_pool;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);
res_pool = kunit_kzalloc(test, sizeof(*res_pool), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res_pool);
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
dc->res_pool = res_pool;
dc->ctx = ctx;
ctx->dc = dc;
ctx->driver_context = adev;
adev->dm.dc = dc;
return adev;
}
static struct amdgpu_device *dm_test_alloc_adev_with_dmub(struct kunit *test)
{
struct amdgpu_device *adev;
struct dmub_srv_fb_info *fb_info;
int i;
adev = dm_test_alloc_adev_with_dc(test);
fb_info = kunit_kzalloc(test, sizeof(*fb_info), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fb_info);
fb_info->num_fb = DMUB_WINDOW_TOTAL;
for (i = 0; i < DMUB_WINDOW_TOTAL; i++) {
fb_info->fb[i].size = PAGE_SIZE;
fb_info->fb[i].cpu_addr = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fb_info->fb[i].cpu_addr);
}
adev->dm.dmub_srv = dm_test_alloc_dmub_srv(test);
adev->dm.dmub_fb_info = fb_info;
adev->dm.dmub_fw = dm_test_alloc_dmub_fw(test);
adev->bios = kunit_kzalloc(test, 4, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev->bios);
adev->bios_size = 4;
adev->dm.fw_inst_size = 0;
return adev;
}
static void dm_test_dmub_hw_init_no_dmub_srv(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);
adev->dm.dmub_srv = NULL;
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
}
static void dm_test_dmub_hw_init_no_fb_info(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);
struct dmub_srv *dmub_srv;
dmub_srv = kunit_kzalloc(test, sizeof(*dmub_srv), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmub_srv);
adev->dm.dmub_srv = dmub_srv;
adev->dm.dmub_fb_info = NULL;
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), -EINVAL);
}
static void dm_test_dmub_hw_init_no_firmware(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);
struct dmub_srv *dmub_srv;
struct dmub_srv_fb_info *fb_info;
dmub_srv = kunit_kzalloc(test, sizeof(*dmub_srv), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmub_srv);
fb_info = kunit_kzalloc(test, sizeof(*fb_info), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fb_info);
adev->dm.dmub_srv = dmub_srv;
adev->dm.dmub_fb_info = fb_info;
adev->dm.dmub_fw = NULL;
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), -EINVAL);
}
static void dm_test_dmub_hw_init_success_fake_dmub(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
KUNIT_EXPECT_TRUE(test, adev->dm.dmub_srv->hw_init);
KUNIT_EXPECT_NOT_NULL(test, adev->dm.dc->ctx->dmub_srv);
}
static void dm_test_dmub_hw_init_no_hw_support(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
adev->dm.dmub_srv->hw_funcs.is_supported = dm_test_dmub_unsupported;
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
KUNIT_EXPECT_NULL(test, adev->dm.dc->ctx->dmub_srv);
}
static void dm_test_dmub_hw_init_bss_data(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
struct dmcub_firmware_header_v1_0 *hdr;
hdr = (struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
hdr->bss_data_bytes = cpu_to_le32(16);
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
KUNIT_EXPECT_TRUE(test, adev->dm.dmub_srv->hw_init);
}
static void dm_test_dmub_hw_init_hw_init_fails(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
adev->dm.dmub_fb_info->num_fb = 0;
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), -EINVAL);
}
static void dm_test_dmub_hw_init_auto_load_timeout(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
adev->dm.dmub_srv->hw_funcs.get_fw_status = dm_test_dmub_fw_not_ready;
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
KUNIT_EXPECT_NOT_NULL(test, adev->dm.dc->ctx->dmub_srv);
}
static void dm_test_dmub_hw_init_apu_dpia_dcn35(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 5, 0);
adev->dm.dc->caps.is_apu = true;
adev->dm.dc->res_pool->usb4_dpia_count = 1;
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
KUNIT_EXPECT_TRUE(test, adev->dm.dmub_srv->hw_init);
}
static void dm_test_dmub_hw_init_sanity_checks_dcn31(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 1, 2);
adev->dm.dmcub_fw_version = DMUB_FW_VERSION(4, 0, 10);
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
KUNIT_EXPECT_TRUE(test, adev->dm.dc->debug.sanity_checks);
}
static void dm_test_dmub_hw_init_sanity_checks_dcn314(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 1, 4);
adev->dm.dmcub_fw_version = DMUB_FW_VERSION(4, 0, 10);
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
KUNIT_EXPECT_TRUE(test, adev->dm.dc->debug.sanity_checks);
}
static void dm_test_dmub_hw_init_dmcu_abm(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
struct dmcu *dmcu;
struct abm *abm;
dmcu = kunit_kzalloc(test, sizeof(*dmcu), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmcu);
abm = kunit_kzalloc(test, sizeof(*abm), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, abm);
dmcu->funcs = &dm_test_dmcu_funcs;
dmcu->psp_version = 0x12345678;
adev->dm.dc->res_pool->dmcu = dmcu;
adev->dm.dc->res_pool->abm = abm;
KUNIT_EXPECT_EQ(test, dm_dmub_hw_init(adev), 0);
KUNIT_EXPECT_TRUE(test, abm->dmcu_is_running);
}
static void dm_test_dmub_hw_resume_no_dmub_srv(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);
adev->dm.dmub_srv = NULL;
dm_dmub_hw_resume(adev);
}
static void dm_test_dmub_hw_resume_initialized_dmub(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);
adev->dm.dmub_srv = dm_test_alloc_dmub_srv(test);
dm_dmub_hw_resume(adev);
}
static void dm_test_dmub_hw_resume_full_init(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
adev->dm.dmub_srv->hw_init = false;
dm_dmub_hw_resume(adev);
KUNIT_EXPECT_NOT_NULL(test, adev->dm.dc->ctx->dmub_srv);
}
static void dm_test_dmub_hw_resume_init_check_failed(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dmub(test);
adev->dm.dmub_srv->sw_init = false;
dm_dmub_hw_resume(adev);
}
static void dm_test_dmub_hw_resume_auto_load_timeout(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);
adev->dm.dmub_srv = dm_test_alloc_dmub_srv(test);
adev->dm.dmub_srv->hw_funcs.get_fw_status = dm_test_dmub_fw_not_ready;
dm_dmub_hw_resume(adev);
}
static void dm_test_dmub_sw_init_unsupported_asic(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(1, 0, 0);
KUNIT_EXPECT_EQ(test, dm_dmub_sw_init(adev), 0);
}
static void dm_test_init_microcode_unsupported_asic(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(1, 0, 0);
KUNIT_EXPECT_EQ(test, dm_init_microcode(adev), 0);
}
static void dm_test_dmub_get_vbios_bounding_box_default_null(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 5, 0);
KUNIT_EXPECT_NULL(test, dm_dmub_get_vbios_bounding_box(adev));
}
static void dm_test_execute_dmub_cmd_null_dmub_srv(struct kunit *test)
{
struct amdgpu_device *adev;
struct dc_context *ctx;
union dmub_rb_cmd *cmd;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
cmd = kunit_kzalloc(test, sizeof(*cmd), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cmd);
spin_lock_init(&adev->dm.dmub_lock);
ctx->driver_context = adev;
ctx->dmub_srv = NULL;
KUNIT_EXPECT_FALSE(test,
dm_execute_dmub_cmd(ctx, cmd, DM_DMUB_WAIT_TYPE_NO_WAIT));
}
static void dm_test_process_dmub_aux_transfer_sync_engine_acquire(struct kunit *test)
{
struct amdgpu_device *adev;
struct dc_context *ctx;
struct dc *dc;
struct aux_payload *payload;
struct dmub_notification *notify;
enum aux_return_code_type result = AUX_RET_SUCCESS;
int ret;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);
payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, payload);
notify = kunit_kzalloc(test, sizeof(*notify), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, notify);
dc->link_count = 0;
ctx->dc = dc;
ctx->driver_context = adev;
adev->dm.dmub_notify = notify;
mutex_init(&adev->dm.dpia_aux_lock);
init_completion(&adev->dm.dmub_aux_transfer_done);
ret = amdgpu_dm_process_dmub_aux_transfer_sync(ctx, 0, payload, &result);
KUNIT_EXPECT_EQ(test, ret, -1);
KUNIT_EXPECT_EQ(test, result, AUX_RET_ERROR_ENGINE_ACQUIRE);
}
static void dm_test_process_dmub_aux_transfer_sync_protocol_error(struct kunit *test)
{
struct amdgpu_device *adev;
struct dc_context *ctx;
struct dc_context *dc_ctx;
struct dc *dc;
struct dc_link *link;
struct ddc_service *ddc;
struct aux_payload *payload;
struct dmub_notification *notify;
enum aux_return_code_type result = AUX_RET_SUCCESS;
int ret;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
dc_ctx = kunit_kzalloc(test, sizeof(*dc_ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc_ctx);
dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);
link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, link);
ddc = kunit_kzalloc(test, sizeof(*ddc), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ddc);
payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, payload);
notify = kunit_kzalloc(test, sizeof(*notify), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, notify);
link->ddc = ddc;
dc->ctx = dc_ctx;
dc->link_count = 1;
dc->links[0] = link;
dc_ctx->dc = dc;
dc_ctx->driver_context = adev;
dc_ctx->dmub_srv = NULL;
ctx->dc = dc;
ctx->driver_context = adev;
spin_lock_init(&adev->dm.dmub_lock);
adev->dm.dmub_notify = notify;
mutex_init(&adev->dm.dpia_aux_lock);
init_completion(&adev->dm.dmub_aux_transfer_done);
complete(&adev->dm.dmub_aux_transfer_done);
notify->result = AUX_RET_ERROR_PROTOCOL_ERROR;
ret = amdgpu_dm_process_dmub_aux_transfer_sync(ctx, 0, payload, &result);
KUNIT_EXPECT_EQ(test, ret, -1);
KUNIT_EXPECT_EQ(test, result, AUX_RET_ERROR_PROTOCOL_ERROR);
}
static void dm_test_process_dmub_aux_transfer_sync_copies_data(struct kunit *test)
{
struct amdgpu_device *adev;
struct dc_context *ctx;
struct dc_context *dc_ctx;
struct dc *dc;
struct dc_link *link;
struct ddc_service *ddc;
struct aux_payload *payload;
struct dmub_notification *notify;
enum aux_return_code_type result = AUX_RET_ERROR_UNKNOWN;
u8 data[4] = { 0 };
u8 reply = 0;
int ret;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
dc_ctx = kunit_kzalloc(test, sizeof(*dc_ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc_ctx);
dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);
link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, link);
ddc = kunit_kzalloc(test, sizeof(*ddc), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ddc);
payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, payload);
notify = kunit_kzalloc(test, sizeof(*notify), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, notify);
link->ddc = ddc;
dc->ctx = dc_ctx;
dc->link_count = 1;
dc->links[0] = link;
dc_ctx->dc = dc;
dc_ctx->driver_context = adev;
dc_ctx->dmub_srv = NULL;
ctx->dc = dc;
ctx->driver_context = adev;
spin_lock_init(&adev->dm.dmub_lock);
adev->dm.dmub_notify = notify;
mutex_init(&adev->dm.dpia_aux_lock);
init_completion(&adev->dm.dmub_aux_transfer_done);
complete(&adev->dm.dmub_aux_transfer_done);
payload->data = data;
payload->reply = &reply;
payload->length = sizeof(data);
notify->result = AUX_RET_SUCCESS;
notify->aux_reply.command = 0xA4;
notify->aux_reply.length = 3;
notify->aux_reply.data[0] = 0x11;
notify->aux_reply.data[1] = 0x22;
notify->aux_reply.data[2] = 0x33;
ret = amdgpu_dm_process_dmub_aux_transfer_sync(ctx, 0, payload, &result);
KUNIT_EXPECT_EQ(test, ret, 3);
KUNIT_EXPECT_EQ(test, result, AUX_RET_SUCCESS);
KUNIT_EXPECT_EQ(test, reply, 0xA);
KUNIT_EXPECT_EQ(test, data[0], 0x11);
KUNIT_EXPECT_EQ(test, data[1], 0x22);
KUNIT_EXPECT_EQ(test, data[2], 0x33);
}
static void dm_test_process_dmub_aux_transfer_sync_zero_length(struct kunit *test)
{
struct amdgpu_device *adev;
struct dc_context *ctx;
struct dc_context *dc_ctx;
struct dc *dc;
struct dc_link *link;
struct ddc_service *ddc;
struct aux_payload *payload;
struct dmub_notification *notify;
enum aux_return_code_type result = AUX_RET_ERROR_UNKNOWN;
u8 data[4] = { 0 };
u8 reply = 0;
int ret;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
dc_ctx = kunit_kzalloc(test, sizeof(*dc_ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc_ctx);
dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);
link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, link);
ddc = kunit_kzalloc(test, sizeof(*ddc), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ddc);
payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, payload);
notify = kunit_kzalloc(test, sizeof(*notify), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, notify);
link->ddc = ddc;
dc->ctx = dc_ctx;
dc->link_count = 1;
dc->links[0] = link;
dc_ctx->dc = dc;
dc_ctx->driver_context = adev;
dc_ctx->dmub_srv = NULL;
ctx->dc = dc;
ctx->driver_context = adev;
spin_lock_init(&adev->dm.dmub_lock);
adev->dm.dmub_notify = notify;
mutex_init(&adev->dm.dpia_aux_lock);
init_completion(&adev->dm.dmub_aux_transfer_done);
complete(&adev->dm.dmub_aux_transfer_done);
payload->data = data;
payload->reply = &reply;
payload->length = sizeof(data);
notify->result = AUX_RET_SUCCESS;
notify->aux_reply.command = 0x03;
notify->aux_reply.length = 0;
ret = amdgpu_dm_process_dmub_aux_transfer_sync(ctx, 0, payload, &result);
KUNIT_EXPECT_EQ(test, ret, 0);
KUNIT_EXPECT_EQ(test, result, AUX_RET_SUCCESS);
KUNIT_EXPECT_EQ(test, reply, 0x3);
}
static void dm_test_process_dmub_set_config_sync_unknown_error(struct kunit *test)
{
struct amdgpu_device *adev;
struct dc_context *ctx;
struct dc_context *dc_ctx;
struct dc *dc;
struct dc_link *link;
struct set_config_cmd_payload *payload;
struct dmub_notification *notify;
enum set_config_status result = SET_CONFIG_PENDING;
int ret;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
dc_ctx = kunit_kzalloc(test, sizeof(*dc_ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc_ctx);
dc = kunit_kzalloc(test, sizeof(*dc), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dc);
link = kunit_kzalloc(test, sizeof(*link), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, link);
payload = kunit_kzalloc(test, sizeof(*payload), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, payload);
notify = kunit_kzalloc(test, sizeof(*notify), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, notify);
dc->ctx = dc_ctx;
dc_ctx->dmub_srv = NULL;
dc->links[0] = link;
ctx->dc = dc;
ctx->driver_context = adev;
adev->dm.dmub_notify = notify;
mutex_init(&adev->dm.dpia_aux_lock);
init_completion(&adev->dm.dmub_aux_transfer_done);
ret = amdgpu_dm_process_dmub_set_config_sync(ctx, 0, payload, &result);
KUNIT_EXPECT_EQ(test, ret, 0);
KUNIT_EXPECT_EQ(test, result, SET_CONFIG_UNKNOWN_ERROR);
}
static void dm_test_abort_fused_io_no_dmub_srv(struct kunit *test)
{
struct amdgpu_device *adev;
struct dc_context *ctx;
struct dmub_cmd_fused_request *req;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
req = kunit_kzalloc(test, sizeof(*req), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, req);
spin_lock_init(&adev->dm.dmub_lock);
ctx->driver_context = adev;
ctx->dmub_srv = NULL;
abort_fused_io(ctx, req);
}
#define DM_TEST_GPINT_REG (0x34c0 + 0x01f8)
#define DM_TEST_GPINT_STATUS_MASK 0xF0000000U
struct dm_test_cgs_ctx {
struct cgs_device dev;
u32 last_write;
unsigned int write_calls;
unsigned int read_calls;
unsigned int last_write_offset;
unsigned int last_read_offset;
unsigned int fail_on_write;
};
static struct dm_test_cgs_ctx dm_test_cgs;
static uint32_t dm_test_cgs_read_register(struct cgs_device *cgs_device,
unsigned int offset)
{
dm_test_cgs.read_calls++;
dm_test_cgs.last_read_offset = offset;
if (dm_test_cgs.fail_on_write == dm_test_cgs.write_calls)
return ~dm_test_cgs.last_write;
return dm_test_cgs.last_write & ~DM_TEST_GPINT_STATUS_MASK;
}
static void dm_test_cgs_write_register(struct cgs_device *cgs_device,
unsigned int offset, uint32_t value)
{
dm_test_cgs.write_calls++;
dm_test_cgs.last_write_offset = offset;
dm_test_cgs.last_write = value;
}
static const struct cgs_ops dm_test_cgs_ops = {
.read_register = dm_test_cgs_read_register,
.write_register = dm_test_cgs_write_register,
};
#define DM_TEST_FAKE_GPU_ADDR 0x1234ABCD0000ULL
struct dm_test_bo_ctx {
void *cpu_ptr;
int create_ret;
unsigned int create_calls;
unsigned int free_calls;
unsigned long create_size;
u32 create_domain;
};
static struct dm_test_bo_ctx dm_test_bo;
static int dm_test_bo_create_kernel(struct amdgpu_device *adev, unsigned long size,
int align, u32 domain, struct amdgpu_bo **bo_ptr,
u64 *gpu_addr, void **cpu_addr)
{
dm_test_bo.create_calls++;
dm_test_bo.create_size = size;
dm_test_bo.create_domain = domain;
if (dm_test_bo.create_ret)
return dm_test_bo.create_ret;
*gpu_addr = DM_TEST_FAKE_GPU_ADDR;
*cpu_addr = dm_test_bo.cpu_ptr;
return 0;
}
static void dm_test_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr, void **cpu_addr)
{
dm_test_bo.free_calls++;
*bo = NULL;
*gpu_addr = 0;
*cpu_addr = NULL;
}
static const struct amdgpu_dm_services_kunit_ops dm_test_services_ops = {
.bo_create_kernel = dm_test_bo_create_kernel,
.bo_free_kernel = dm_test_bo_free_kernel,
};
static char dm_test_ucode_name[64];
static unsigned int dm_test_ucode_calls;
static int dm_test_ucode_ret;
static __printf(4, 5) int dm_test_ucode_request(struct amdgpu_device *adev,
const struct firmware **fw,
enum amdgpu_ucode_required required,
const char *fmt, ...)
{
va_list args;
dm_test_ucode_calls++;
va_start(args, fmt);
vsnprintf(dm_test_ucode_name, sizeof(dm_test_ucode_name), fmt, args);
va_end(args);
return dm_test_ucode_ret;
}
static const struct amdgpu_dm_dmub_kunit_ops dm_test_dmub_ops = {
.bo_create_kernel = dm_test_bo_create_kernel,
.ucode_request = dm_test_ucode_request,
};
static int dm_test_dmub_hw_access_init(struct kunit *test)
{
void *cpu_ptr;
cpu_ptr = kunit_kzalloc(test, PAGE_SIZE, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cpu_ptr);
dm_test_bo = (struct dm_test_bo_ctx) { .cpu_ptr = cpu_ptr };
dm_test_cgs = (struct dm_test_cgs_ctx) { .dev.ops = &dm_test_cgs_ops };
dm_test_ucode_name[0] = '\0';
dm_test_ucode_calls = 0;
dm_test_ucode_ret = 0;
amdgpu_dm_services_kunit_set_ops(&dm_test_services_ops);
amdgpu_dm_dmub_kunit_set_ops(&dm_test_dmub_ops);
return 0;
}
static void dm_test_dmub_hw_access_exit(struct kunit *test)
{
amdgpu_dm_services_kunit_set_ops(NULL);
amdgpu_dm_dmub_kunit_set_ops(NULL);
}
static struct amdgpu_device *dm_test_alloc_adev_with_cgs(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
adev->dm.cgs_device = &dm_test_cgs.dev;
INIT_LIST_HEAD(&adev->dm.da_list);
return adev;
}
static void dm_test_dmub_reg_write_then_read(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_dc(test);
struct dc_perf_trace *perf_trace;
perf_trace = kunit_kzalloc(test, sizeof(*perf_trace), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, perf_trace);
adev->dm.dc->ctx->cgs_device = &dm_test_cgs.dev;
adev->dm.dc->ctx->perf_trace = perf_trace;
amdgpu_dm_dmub_reg_write(adev, 0x1234, 0x0BADF00D);
KUNIT_EXPECT_EQ(test, dm_test_cgs.write_calls, 1U);
KUNIT_EXPECT_EQ(test, dm_test_cgs.last_write_offset, 0x1234U);
KUNIT_EXPECT_EQ(test, dm_test_cgs.last_write, 0x0BADF00DU);
KUNIT_EXPECT_EQ(test, amdgpu_dm_dmub_reg_read(adev, 0x1234), 0x0BADF00DU);
KUNIT_EXPECT_EQ(test, dm_test_cgs.read_calls, 1U);
KUNIT_EXPECT_EQ(test, dm_test_cgs.last_read_offset, 0x1234U);
}
static void dm_test_dmub_get_vbios_bounding_box_success(struct kunit *test)
{
static const u32 ip_versions[] = {
IP_VERSION(4, 0, 1),
IP_VERSION(4, 2, 0),
IP_VERSION(4, 2, 1),
IP_VERSION(6, 0, 0),
};
struct amdgpu_device *adev = dm_test_alloc_adev_with_cgs(test);
unsigned int i;
for (i = 0; i < ARRAY_SIZE(ip_versions); i++) {
void *bb;
adev->ip_versions[DCE_HWIP][0] = ip_versions[i];
bb = dm_dmub_get_vbios_bounding_box(adev);
KUNIT_EXPECT_PTR_EQ_MSG(test, bb, dm_test_bo.cpu_ptr,
"IP version 0x%08x", ip_versions[i]);
KUNIT_EXPECT_GT_MSG(test, dm_test_bo.create_size, 0UL,
"IP version 0x%08x", ip_versions[i]);
KUNIT_EXPECT_EQ(test, dm_test_bo.create_domain,
(u32)AMDGPU_GEM_DOMAIN_GTT);
KUNIT_EXPECT_EQ(test, dm_test_cgs.last_write_offset,
(unsigned int)DM_TEST_GPINT_REG);
KUNIT_EXPECT_EQ(test, dm_test_cgs.write_calls, 5 * (i + 1));
dm_free_gpu_mem(adev, DC_MEM_ALLOC_TYPE_GART, bb);
}
}
static void dm_test_dmub_get_vbios_bounding_box_alloc_fails(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_cgs(test);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(6, 0, 0);
dm_test_bo.create_ret = -ENOMEM;
KUNIT_EXPECT_NULL(test, dm_dmub_get_vbios_bounding_box(adev));
KUNIT_EXPECT_EQ(test, dm_test_cgs.write_calls, 0U);
}
static void dm_test_dmub_get_vbios_bounding_box_addr_timeout(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_cgs(test);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(6, 0, 0);
dm_test_cgs.fail_on_write = 1;
KUNIT_EXPECT_NULL(test, dm_dmub_get_vbios_bounding_box(adev));
KUNIT_EXPECT_EQ(test, dm_test_cgs.write_calls, 1U);
KUNIT_EXPECT_EQ(test, dm_test_bo.free_calls, 1U);
KUNIT_EXPECT_TRUE(test, list_empty(&adev->dm.da_list));
}
static void dm_test_dmub_get_vbios_bounding_box_copy_timeout(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_cgs(test);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(6, 0, 0);
dm_test_cgs.fail_on_write = 5;
KUNIT_EXPECT_NULL(test, dm_dmub_get_vbios_bounding_box(adev));
KUNIT_EXPECT_EQ(test, dm_test_cgs.write_calls, 5U);
KUNIT_EXPECT_EQ(test, dm_test_bo.free_calls, 1U);
KUNIT_EXPECT_TRUE(test, list_empty(&adev->dm.da_list));
}
struct dm_test_ucode_case {
u32 dce_version;
u32 gc_version;
u32 external_rev_id;
const char *fw_name;
};
static const struct dm_test_ucode_case dm_test_ucode_cases[] = {
{ IP_VERSION(2, 1, 0), 0, 0, FIRMWARE_RENOIR_DMUB },
{ IP_VERSION(2, 1, 0), 0, GREEN_SARDINE_A0, FIRMWARE_GREEN_SARDINE_DMUB },
{ IP_VERSION(3, 0, 0), IP_VERSION(10, 3, 0), 0, FIRMWARE_SIENNA_CICHLID_DMUB },
{ IP_VERSION(3, 0, 0), IP_VERSION(10, 3, 2), 0, FIRMWARE_NAVY_FLOUNDER_DMUB },
{ IP_VERSION(3, 0, 1), 0, 0, FIRMWARE_VANGOGH_DMUB },
{ IP_VERSION(3, 0, 2), 0, 0, FIRMWARE_DIMGREY_CAVEFISH_DMUB },
{ IP_VERSION(3, 0, 3), 0, 0, FIRMWARE_BEIGE_GOBY_DMUB },
{ IP_VERSION(3, 1, 2), 0, 0, FIRMWARE_YELLOW_CARP_DMUB },
{ IP_VERSION(3, 1, 3), 0, 0, FIRMWARE_YELLOW_CARP_DMUB },
{ IP_VERSION(3, 1, 4), 0, 0, FIRMWARE_DCN_314_DMUB },
{ IP_VERSION(3, 1, 5), 0, 0, FIRMWARE_DCN_315_DMUB },
{ IP_VERSION(3, 1, 6), 0, 0, FIRMWARE_DCN316_DMUB },
{ IP_VERSION(3, 2, 0), 0, 0, FIRMWARE_DCN_V3_2_0_DMCUB },
{ IP_VERSION(3, 2, 1), 0, 0, FIRMWARE_DCN_V3_2_1_DMCUB },
{ IP_VERSION(3, 5, 0), 0, 0, FIRMWARE_DCN_35_DMUB },
{ IP_VERSION(3, 5, 1), 0, 0, FIRMWARE_DCN_351_DMUB },
{ IP_VERSION(3, 6, 0), 0, 0, FIRMWARE_DCN_36_DMUB },
{ IP_VERSION(4, 0, 1), 0, 0, FIRMWARE_DCN_401_DMUB },
{ IP_VERSION(4, 2, 0), 0, 0, FIRMWARE_DCN_42_DMUB },
{ IP_VERSION(4, 2, 1), 0, 0, FIRMWARE_DCN_42B_DMUB },
{ IP_VERSION(6, 0, 0), 0, 0, FIRMWARE_DCN_60_DMUB },
};
static void dm_test_init_microcode_fw_names(struct kunit *test)
{
struct amdgpu_device *adev;
unsigned int i;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
for (i = 0; i < ARRAY_SIZE(dm_test_ucode_cases); i++) {
const struct dm_test_ucode_case *c = &dm_test_ucode_cases[i];
adev->ip_versions[DCE_HWIP][0] = c->dce_version;
adev->ip_versions[GC_HWIP][0] = c->gc_version;
adev->external_rev_id = c->external_rev_id;
dm_test_ucode_name[0] = '\0';
KUNIT_EXPECT_EQ_MSG(test, dm_init_microcode(adev), 0,
"IP version 0x%08x", c->dce_version);
KUNIT_EXPECT_STREQ_MSG(test, dm_test_ucode_name, c->fw_name,
"IP version 0x%08x", c->dce_version);
}
KUNIT_EXPECT_EQ(test, dm_test_ucode_calls,
(unsigned int)ARRAY_SIZE(dm_test_ucode_cases));
}
static void dm_test_init_microcode_request_fails(struct kunit *test)
{
struct amdgpu_device *adev;
adev = kunit_kzalloc(test, sizeof(*adev), GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 5, 0);
dm_test_ucode_ret = -ENOENT;
KUNIT_EXPECT_EQ(test, dm_init_microcode(adev), -ENOENT);
}
static struct amdgpu_device *dm_test_alloc_adev_for_sw_init(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_with_cgs(test);
adev->dm.dmub_fw = dm_test_alloc_dmub_fw(test);
adev->bios = kunit_kzalloc(test, 4, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adev->bios);
adev->bios_size = 4;
return adev;
}
static void dm_test_free_sw_init(struct amdgpu_device *adev)
{
kfree(adev->dm.dmub_srv);
kfree(adev->dm.dmub_fb_info);
adev->dm.dmub_srv = NULL;
adev->dm.dmub_fb_info = NULL;
}
static void dm_test_dmub_sw_init_asic_mapping(struct kunit *test)
{
static const u32 ip_versions[] = {
IP_VERSION(2, 1, 0), IP_VERSION(3, 0, 0), IP_VERSION(3, 0, 1),
IP_VERSION(3, 0, 2), IP_VERSION(3, 0, 3), IP_VERSION(3, 1, 2),
IP_VERSION(3, 1, 3), IP_VERSION(3, 1, 4), IP_VERSION(3, 1, 5),
IP_VERSION(3, 1, 6), IP_VERSION(3, 2, 0), IP_VERSION(3, 2, 1),
IP_VERSION(3, 5, 0), IP_VERSION(3, 5, 1), IP_VERSION(3, 6, 0),
IP_VERSION(4, 0, 1), IP_VERSION(4, 2, 0), IP_VERSION(4, 2, 1),
IP_VERSION(6, 0, 0),
};
struct amdgpu_device *adev = dm_test_alloc_adev_for_sw_init(test);
unsigned int i;
dm_test_bo.create_ret = -ENOMEM;
for (i = 0; i < ARRAY_SIZE(ip_versions); i++) {
adev->ip_versions[DCE_HWIP][0] = ip_versions[i];
KUNIT_EXPECT_EQ_MSG(test, dm_dmub_sw_init(adev), -ENOMEM,
"IP version 0x%08x", ip_versions[i]);
dm_test_free_sw_init(adev);
}
KUNIT_EXPECT_EQ(test, dm_test_bo.create_calls, (unsigned int)ARRAY_SIZE(ip_versions));
}
static void dm_test_dmub_sw_init_gtt_only_asic(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_for_sw_init(test);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 2, 0);
dm_test_bo.create_ret = -ENOMEM;
KUNIT_EXPECT_EQ(test, dm_dmub_sw_init(adev), -ENOMEM);
KUNIT_EXPECT_EQ(test, dm_test_bo.create_domain, (u32)AMDGPU_GEM_DOMAIN_GTT);
dm_test_free_sw_init(adev);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(3, 5, 0);
KUNIT_EXPECT_EQ(test, dm_dmub_sw_init(adev), -ENOMEM);
KUNIT_EXPECT_EQ(test, dm_test_bo.create_domain,
(u32)(AMDGPU_GEM_DOMAIN_GTT | AMDGPU_GEM_DOMAIN_VRAM));
dm_test_free_sw_init(adev);
}
static void dm_test_dmub_sw_init_success(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_for_sw_init(test);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(6, 0, 0);
KUNIT_EXPECT_EQ(test, dm_dmub_sw_init(adev), 0);
KUNIT_EXPECT_NOT_NULL(test, adev->dm.dmub_srv);
KUNIT_EXPECT_NOT_NULL(test, adev->dm.dmub_fb_info);
KUNIT_EXPECT_NOT_NULL(test, adev->dm.bb_from_dmub);
KUNIT_EXPECT_EQ(test, adev->dm.dmcub_fw_version, DMUB_FW_VERSION(9, 9, 9));
KUNIT_EXPECT_EQ(test, adev->dm.fw_inst_size,
(u32)(DM_TEST_FW_INST_CONST_BYTES - PSP_HEADER_BYTES_256 -
PSP_FOOTER_BYTES_256));
dm_free_gpu_mem(adev, DC_MEM_ALLOC_TYPE_GART, adev->dm.bb_from_dmub);
dm_test_free_sw_init(adev);
}
static void dm_test_dmub_sw_init_bss_data(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_for_sw_init(test);
struct dmcub_firmware_header_v1_0 *hdr;
hdr = (struct dmcub_firmware_header_v1_0 *)adev->dm.dmub_fw->data;
hdr->bss_data_bytes = cpu_to_le32(512);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(6, 0, 0);
KUNIT_EXPECT_EQ(test, dm_dmub_sw_init(adev), 0);
KUNIT_EXPECT_NOT_NULL(test, adev->dm.dmub_fb_info);
dm_free_gpu_mem(adev, DC_MEM_ALLOC_TYPE_GART, adev->dm.bb_from_dmub);
dm_test_free_sw_init(adev);
}
static void dm_test_dmub_sw_init_psp_load(struct kunit *test)
{
struct amdgpu_device *adev = dm_test_alloc_adev_for_sw_init(test);
adev->ip_versions[DCE_HWIP][0] = IP_VERSION(6, 0, 0);
adev->firmware.load_type = AMDGPU_FW_LOAD_PSP;
KUNIT_EXPECT_EQ(test, dm_dmub_sw_init(adev), 0);
KUNIT_EXPECT_EQ(test, adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].ucode_id,
AMDGPU_UCODE_ID_DMCUB);
KUNIT_EXPECT_PTR_EQ(test, adev->firmware.ucode[AMDGPU_UCODE_ID_DMCUB].fw,
adev->dm.dmub_fw);
KUNIT_EXPECT_EQ(test, adev->firmware.fw_size,
(u32)ALIGN(DM_TEST_FW_INST_CONST_BYTES, PAGE_SIZE));
dm_free_gpu_mem(adev, DC_MEM_ALLOC_TYPE_GART, adev->dm.bb_from_dmub);
dm_test_free_sw_init(adev);
}
static struct kunit_case amdgpu_dm_dmub_tests[] = {
KUNIT_CASE(dm_test_register_dmub_notify_callback_null_callback),
KUNIT_CASE(dm_test_register_dmub_notify_callback_type_out_of_range),
KUNIT_CASE(dm_test_register_dmub_notify_callback_valid),
KUNIT_CASE(dm_test_register_dmub_notify_callback_offload_false),
KUNIT_CASE(dm_test_dmub_aux_setconfig_callback_copies_and_completes),
KUNIT_CASE(dm_test_dmub_aux_setconfig_callback_non_aux_no_complete),
KUNIT_CASE(dm_test_dmub_aux_setconfig_callback_aux_with_null_dm_notify),
KUNIT_CASE(dm_test_dmub_aux_setconfig_callback_set_config_reply),
KUNIT_CASE(dm_test_dmub_aux_fused_io_callback_copies_reply_and_completes),
KUNIT_CASE(dm_test_dmub_aux_fused_io_callback_max_ddc_line),
KUNIT_CASE(dm_test_dmub_aux_fused_io_callback_null_args),
KUNIT_CASE(dm_test_get_default_ips_mode_dcn35),
KUNIT_CASE(dm_test_get_default_ips_mode_dcn351),
KUNIT_CASE(dm_test_get_default_ips_mode_dcn36),
KUNIT_CASE(dm_test_get_default_ips_mode_older_than_dcn35),
KUNIT_CASE(dm_test_get_default_ips_mode_newer_default),
KUNIT_CASE(dm_test_dmub_hw_init_no_dmub_srv),
KUNIT_CASE(dm_test_dmub_hw_init_no_fb_info),
KUNIT_CASE(dm_test_dmub_hw_init_no_firmware),
KUNIT_CASE(dm_test_dmub_hw_init_success_fake_dmub),
KUNIT_CASE(dm_test_dmub_hw_init_no_hw_support),
KUNIT_CASE(dm_test_dmub_hw_init_bss_data),
KUNIT_CASE(dm_test_dmub_hw_init_hw_init_fails),
KUNIT_CASE(dm_test_dmub_hw_init_auto_load_timeout),
KUNIT_CASE(dm_test_dmub_hw_init_apu_dpia_dcn35),
KUNIT_CASE(dm_test_dmub_hw_init_sanity_checks_dcn31),
KUNIT_CASE(dm_test_dmub_hw_init_sanity_checks_dcn314),
KUNIT_CASE(dm_test_dmub_hw_init_dmcu_abm),
KUNIT_CASE(dm_test_dmub_hw_resume_no_dmub_srv),
KUNIT_CASE(dm_test_dmub_hw_resume_initialized_dmub),
KUNIT_CASE(dm_test_dmub_hw_resume_full_init),
KUNIT_CASE(dm_test_dmub_hw_resume_init_check_failed),
KUNIT_CASE(dm_test_dmub_hw_resume_auto_load_timeout),
KUNIT_CASE(dm_test_dmub_sw_init_unsupported_asic),
KUNIT_CASE(dm_test_init_microcode_unsupported_asic),
KUNIT_CASE(dm_test_dmub_get_vbios_bounding_box_default_null),
KUNIT_CASE(dm_test_execute_dmub_cmd_null_dmub_srv),
KUNIT_CASE(dm_test_process_dmub_aux_transfer_sync_engine_acquire),
KUNIT_CASE(dm_test_process_dmub_aux_transfer_sync_protocol_error),
KUNIT_CASE(dm_test_process_dmub_aux_transfer_sync_copies_data),
KUNIT_CASE(dm_test_process_dmub_aux_transfer_sync_zero_length),
KUNIT_CASE(dm_test_process_dmub_set_config_sync_unknown_error),
KUNIT_CASE(dm_test_abort_fused_io_no_dmub_srv),
{}
};
static struct kunit_suite amdgpu_dm_dmub_test_suite = {
.name = "amdgpu_dm_dmub",
.test_cases = amdgpu_dm_dmub_tests,
};
static struct kunit_case amdgpu_dm_dmub_hw_access_tests[] = {
KUNIT_CASE(dm_test_dmub_reg_write_then_read),
KUNIT_CASE(dm_test_dmub_get_vbios_bounding_box_success),
KUNIT_CASE(dm_test_dmub_get_vbios_bounding_box_alloc_fails),
KUNIT_CASE(dm_test_dmub_get_vbios_bounding_box_addr_timeout),
KUNIT_CASE(dm_test_dmub_get_vbios_bounding_box_copy_timeout),
KUNIT_CASE(dm_test_init_microcode_fw_names),
KUNIT_CASE(dm_test_init_microcode_request_fails),
KUNIT_CASE(dm_test_dmub_sw_init_asic_mapping),
KUNIT_CASE(dm_test_dmub_sw_init_gtt_only_asic),
KUNIT_CASE(dm_test_dmub_sw_init_success),
KUNIT_CASE(dm_test_dmub_sw_init_bss_data),
KUNIT_CASE(dm_test_dmub_sw_init_psp_load),
{}
};
static struct kunit_suite amdgpu_dm_dmub_hw_access_test_suite = {
.name = "amdgpu_dm_dmub_hw_access",
.init = dm_test_dmub_hw_access_init,
.exit = dm_test_dmub_hw_access_exit,
.test_cases = amdgpu_dm_dmub_hw_access_tests,
};
kunit_test_suites(&amdgpu_dm_dmub_test_suite,
&amdgpu_dm_dmub_hw_access_test_suite);
MODULE_AUTHOR("AMD");
MODULE_DESCRIPTION("KUnit tests for amdgpu_dm_dmub");
MODULE_LICENSE("Dual MIT/GPL"