#include <drm/drm_managed.h>
#include "regs/xe_guc_regs.h"
#include "regs/xe_regs.h"
#include "xe_gt.h"
#include "xe_gt_sriov_pf.h"
#include "xe_gt_sriov_pf_config.h"
#include "xe_gt_sriov_pf_control.h"
#include "xe_gt_sriov_pf_helpers.h"
#include "xe_gt_sriov_pf_migration.h"
#include "xe_gt_sriov_pf_policy.h"
#include "xe_gt_sriov_pf_service.h"
#include "xe_gt_sriov_printk.h"
#include "xe_guc_submit.h"
#include "xe_mmio.h"
#include "xe_pm.h"
static void pf_worker_restart_func(struct work_struct *w);
static int pf_alloc_metadata(struct xe_gt *gt)
{
unsigned int num_vfs = xe_gt_sriov_pf_get_totalvfs(gt);
gt->sriov.pf.vfs = drmm_kcalloc(>_to_xe(gt)->drm, 1 + num_vfs,
sizeof(*gt->sriov.pf.vfs), GFP_KERNEL);
if (!gt->sriov.pf.vfs)
return -ENOMEM;
return 0;
}
static void pf_init_workers(struct xe_gt *gt)
{
xe_gt_assert(gt, IS_SRIOV_PF(gt_to_xe(gt)));
INIT_WORK(>->sriov.pf.workers.restart, pf_worker_restart_func);
}
static void pf_fini_workers(struct xe_gt *gt)
{
xe_gt_assert(gt, IS_SRIOV_PF(gt_to_xe(gt)));
if (disable_work_sync(>->sriov.pf.workers.restart)) {
xe_gt_sriov_dbg_verbose(gt, "pending restart disabled!\n");
xe_pm_runtime_put(gt_to_xe(gt));
}
}
int xe_gt_sriov_pf_init_early(struct xe_gt *gt)
{
int err;
err = pf_alloc_metadata(gt);
if (err)
return err;
err = xe_gt_sriov_pf_service_init(gt);
if (err)
return err;
err = xe_gt_sriov_pf_control_init(gt);
if (err)
return err;
pf_init_workers(gt);
return 0;
}
static void pf_fini_action(void *arg)
{
struct xe_gt *gt = arg;
pf_fini_workers(gt);
}
static int pf_init_late(struct xe_gt *gt)
{
struct xe_device *xe = gt_to_xe(gt);
xe_gt_assert(gt, IS_SRIOV_PF(xe));
return devm_add_action_or_reset(xe->drm.dev, pf_fini_action, gt);
}
int xe_gt_sriov_pf_init(struct xe_gt *gt)
{
int err;
err = xe_gt_sriov_pf_config_init(gt);
if (err)
return err;
xe_gt_sriov_pf_policy_init(gt);
err = xe_gt_sriov_pf_migration_init(gt);
if (err)
return err;
err = pf_init_late(gt);
if (err)
return err;
return 0;
}
static bool pf_needs_enable_ggtt_guest_update(struct xe_device *xe)
{
return GRAPHICS_VERx100(xe) == 1200;
}
static void pf_enable_ggtt_guest_update(struct xe_gt *gt)
{
xe_mmio_write32(>->mmio, VIRTUAL_CTRL_REG, GUEST_GTT_UPDATE_EN);
}
void xe_gt_sriov_pf_init_hw(struct xe_gt *gt)
{
if (pf_needs_enable_ggtt_guest_update(gt_to_xe(gt)))
pf_enable_ggtt_guest_update(gt);
xe_gt_sriov_pf_service_update(gt);
}
static void pf_clear_vf_scratch_regs(struct xe_gt *gt, unsigned int vfid)
{
struct xe_mmio mmio;
int n;
xe_mmio_init_vf_view(&mmio, >->mmio, vfid);
if (xe_gt_is_media_type(gt)) {
for (n = 0; n < MED_VF_SW_FLAG_COUNT; n++)
xe_mmio_write32(&mmio, MED_VF_SW_FLAG(n), 0);
} else {
for (n = 0; n < VF_SW_FLAG_COUNT; n++)
xe_mmio_write32(&mmio, VF_SW_FLAG(n), 0);
}
}
void xe_gt_sriov_pf_sanitize_hw(struct xe_gt *gt, unsigned int vfid)
{
xe_gt_assert(gt, IS_SRIOV_PF(gt_to_xe(gt)));
pf_clear_vf_scratch_regs(gt, vfid);
}
static void pf_cancel_restart(struct xe_gt *gt)
{
xe_gt_assert(gt, IS_SRIOV_PF(gt_to_xe(gt)));
if (cancel_work_sync(>->sriov.pf.workers.restart)) {
xe_gt_sriov_dbg_verbose(gt, "pending restart canceled!\n");
xe_pm_runtime_put(gt_to_xe(gt));
}
}
void xe_gt_sriov_pf_stop_prepare(struct xe_gt *gt)
{
pf_cancel_restart(gt);
}
static void pf_restart(struct xe_gt *gt)
{
struct xe_device *xe = gt_to_xe(gt);
xe_gt_assert(gt, !xe_pm_runtime_suspended(xe));
xe_gt_sriov_pf_config_restart(gt);
xe_gt_sriov_pf_control_restart(gt);
xe_pm_runtime_put(xe);
xe_gt_sriov_dbg(gt, "restart completed\n");
}
static void pf_worker_restart_func(struct work_struct *w)
{
struct xe_gt *gt = container_of(w, typeof(*gt), sriov.pf.workers.restart);
pf_restart(gt);
}
static void pf_queue_restart(struct xe_gt *gt)
{
struct xe_device *xe = gt_to_xe(gt);
xe_gt_assert(gt, IS_SRIOV_PF(xe));
xe_pm_runtime_get_noresume(xe);
if (!queue_work(xe->sriov.wq, >->sriov.pf.workers.restart)) {
xe_gt_sriov_dbg(gt, "restart already in queue!\n");
xe_pm_runtime_put(xe);
}
}
void xe_gt_sriov_pf_restart(struct xe_gt *gt)
{
pf_queue_restart(gt);
}
static void pf_flush_restart(struct xe_gt *gt)
{
xe_gt_assert(gt, IS_SRIOV_PF(gt_to_xe(gt)));
flush_work(>->sriov.pf.workers.restart);
}
int xe_gt_sriov_pf_wait_ready(struct xe_gt *gt)
{
if (xe_guc_read_stopped(>->uc.guc))
return -EBUSY;
pf_flush_restart(gt);
return 0;
}
bool xe_gt_sriov_pf_sched_groups_enabled(struct xe_gt *gt)
{
xe_gt_assert(gt, IS_SRIOV_PF(gt_to_xe(gt)));
return xe_gt_sriov_pf_policy_sched_groups_enabled(gt);
}