#include <linux/pci.h>
#include "xe_device.h"
#include "xe_gt.h"
#include "xe_log.h"
#include "xe_pci.h"
#include "xe_pm.h"
#include "xe_printk.h"
#include "xe_ras.h"
#include "xe_survivability_mode.h"
static void prepare_device_for_reset(struct pci_dev *pdev)
{
struct xe_device *xe = pdev_to_xe_device(pdev);
struct xe_gt *gt;
u8 id;
if (!atomic_xchg(&xe->wedged.flag, 1))
xe_pm_runtime_get_noresume(xe);
xe_device_set_in_reset(xe);
for_each_gt(gt, xe, id) {
xe_gt_declare_wedged(gt);
flush_work(>->wedge.worker);
}
pci_disable_device(pdev);
}
static pci_ers_result_t ras_action_to_pci_result(struct pci_dev *pdev, u8 action)
{
switch (action) {
case XE_RAS_RECOVERY_ACTION_RECOVERED:
return PCI_ERS_RESULT_RECOVERED;
case XE_RAS_RECOVERY_ACTION_RESET:
prepare_device_for_reset(pdev);
return PCI_ERS_RESULT_NEED_RESET;
case XE_RAS_RECOVERY_ACTION_DISCONNECT:
return PCI_ERS_RESULT_DISCONNECT;
default:
return PCI_ERS_RESULT_DISCONNECT;
}
}
static pci_ers_result_t xe_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
{
struct xe_device *xe = pdev_to_xe_device(pdev);
xe_info(xe, "PCI error: detected state = %d\n", state);
if (state == pci_channel_io_perm_failure)
return PCI_ERS_RESULT_DISCONNECT;
if (xe_survivability_mode_is_boot_enabled(xe) || xe_device_wedged(xe))
return PCI_ERS_RESULT_DISCONNECT;
switch (state) {
case pci_channel_io_normal:
return PCI_ERS_RESULT_CAN_RECOVER;
case pci_channel_io_frozen:
prepare_device_for_reset(pdev);
return PCI_ERS_RESULT_NEED_RESET;
default:
xe_info(xe, "PCI error: unknown state %d\n", state);
return PCI_ERS_RESULT_DISCONNECT;
}
}
static pci_ers_result_t xe_pci_error_mmio_enabled(struct pci_dev *pdev)
{
struct xe_device *xe = pdev_to_xe_device(pdev);
enum xe_ras_recovery_action action;
xe_info(xe, "PCI error: MMIO enabled\n");
action = xe_ras_process_errors(xe);
if (xe->wedged.mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) {
xe_device_declare_wedged(xe, DRM_WEDGE_RECOVERY_NONE);
return PCI_ERS_RESULT_DISCONNECT;
}
return ras_action_to_pci_result(pdev, action);
}
static pci_ers_result_t xe_pci_error_slot_reset(struct pci_dev *pdev)
{
const struct pci_device_id *ent = pci_match_id(pdev->driver->id_table, pdev);
struct xe_device *xe = pdev_to_xe_device(pdev);
int err;
xe_info(xe, "PCI error: slot reset\n");
pci_restore_state(pdev);
err = pci_enable_device(pdev);
if (err) {
xe_log_err_fatal(xe, PCI, err, "Cannot re-enable PCI device after reset\n");
return PCI_ERS_RESULT_DISCONNECT;
}
xe_device_clear_in_reset(xe);
pdev->driver->remove(pdev);
devres_release_group(&pdev->dev, xe->devres_group);
if (pdev->driver->probe(pdev, ent))
return PCI_ERS_RESULT_DISCONNECT;
xe = pdev_to_xe_device(pdev);
atomic_set(&xe->wedged.flag, 1);
return PCI_ERS_RESULT_RECOVERED;
}
static void xe_pci_error_resume(struct pci_dev *pdev)
{
struct xe_device *xe = pdev_to_xe_device(pdev);
xe_info(xe, "PCI error: resume\n");
atomic_set(&xe->wedged.flag, 0);
}
const struct pci_error_handlers xe_pci_error_handlers = {
.error_detected = xe_pci_error_detected,
.mmio_enabled = xe_pci_error_mmio_enabled,
.slot_reset = xe_pci_error_slot_reset,
.resume = xe_pci_error_resume,
}