1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2026 Intel Corporation 4 */ 5 6 #include <linux/pci.h> 7 8 #include "xe_device.h" 9 #include "xe_gt.h" 10 #include "xe_log.h" 11 #include "xe_pci.h" 12 #include "xe_pm.h" 13 #include "xe_printk.h" 14 #include "xe_ras.h" 15 #include "xe_survivability_mode.h" 16 17 static void prepare_device_for_reset(struct pci_dev *pdev) 18 { 19 struct xe_device *xe = pdev_to_xe_device(pdev); 20 struct xe_gt *gt; 21 u8 id; 22 23 /* 24 * Wedge the device to prevent userspace access but do not send the uevent. 25 * xe_device_wedged_fini() releases runtime pm if wedged flag is set, so acquire a runtime 26 * pm reference to avoid underflow. 27 */ 28 if (!atomic_xchg(&xe->wedged.flag, 1)) 29 xe_pm_runtime_get_noresume(xe); 30 31 xe_device_set_in_reset(xe); 32 33 for_each_gt(gt, xe, id) { 34 xe_gt_declare_wedged(gt); 35 flush_work(>->wedge.worker); 36 } 37 38 pci_disable_device(pdev); 39 } 40 41 static pci_ers_result_t ras_action_to_pci_result(struct pci_dev *pdev, u8 action) 42 { 43 switch (action) { 44 case XE_RAS_RECOVERY_ACTION_RECOVERED: 45 return PCI_ERS_RESULT_RECOVERED; 46 case XE_RAS_RECOVERY_ACTION_RESET: 47 prepare_device_for_reset(pdev); 48 return PCI_ERS_RESULT_NEED_RESET; 49 case XE_RAS_RECOVERY_ACTION_DISCONNECT: 50 return PCI_ERS_RESULT_DISCONNECT; 51 default: 52 return PCI_ERS_RESULT_DISCONNECT; 53 } 54 } 55 56 static pci_ers_result_t xe_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state) 57 { 58 struct xe_device *xe = pdev_to_xe_device(pdev); 59 60 xe_info(xe, "PCI error: detected state = %d\n", state); 61 62 if (state == pci_channel_io_perm_failure) 63 return PCI_ERS_RESULT_DISCONNECT; 64 65 /* If the device is already wedged or in survivability mode, do not attempt recovery */ 66 if (xe_survivability_mode_is_boot_enabled(xe) || xe_device_wedged(xe)) 67 return PCI_ERS_RESULT_DISCONNECT; 68 69 switch (state) { 70 case pci_channel_io_normal: 71 return PCI_ERS_RESULT_CAN_RECOVER; 72 case pci_channel_io_frozen: 73 prepare_device_for_reset(pdev); 74 return PCI_ERS_RESULT_NEED_RESET; 75 default: 76 xe_info(xe, "PCI error: unknown state %d\n", state); 77 return PCI_ERS_RESULT_DISCONNECT; 78 } 79 } 80 81 static pci_ers_result_t xe_pci_error_mmio_enabled(struct pci_dev *pdev) 82 { 83 struct xe_device *xe = pdev_to_xe_device(pdev); 84 enum xe_ras_recovery_action action; 85 86 xe_info(xe, "PCI error: MMIO enabled\n"); 87 action = xe_ras_process_errors(xe); 88 89 /* User wants to debug the error, prevent reset */ 90 if (xe->wedged.mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) { 91 xe_device_declare_wedged(xe, DRM_WEDGE_RECOVERY_NONE); 92 return PCI_ERS_RESULT_DISCONNECT; 93 } 94 95 return ras_action_to_pci_result(pdev, action); 96 } 97 98 static pci_ers_result_t xe_pci_error_slot_reset(struct pci_dev *pdev) 99 { 100 const struct pci_device_id *ent = pci_match_id(pdev->driver->id_table, pdev); 101 struct xe_device *xe = pdev_to_xe_device(pdev); 102 int err; 103 104 xe_info(xe, "PCI error: slot reset\n"); 105 106 pci_restore_state(pdev); 107 108 err = pci_enable_device(pdev); 109 if (err) { 110 xe_log_err_fatal(xe, PCI, err, "Cannot re-enable PCI device after reset\n"); 111 return PCI_ERS_RESULT_DISCONNECT; 112 } 113 114 /* 115 * Secondary Bus Reset causes all VRAM state to be lost along with 116 * hardware state. As an initial step, re-probe the device to 117 * re-initialize the driver and hardware. 118 * TODO: optimize by re-initializing only the hardware state and re-creating 119 * kernel BOs. 120 */ 121 xe_device_clear_in_reset(xe); 122 pdev->driver->remove(pdev); 123 devres_release_group(&pdev->dev, xe->devres_group); 124 125 if (pdev->driver->probe(pdev, ent)) 126 return PCI_ERS_RESULT_DISCONNECT; 127 128 xe = pdev_to_xe_device(pdev); 129 130 /* Wedge the device to prevent I/O operations till the resume callback */ 131 atomic_set(&xe->wedged.flag, 1); 132 133 return PCI_ERS_RESULT_RECOVERED; 134 } 135 136 static void xe_pci_error_resume(struct pci_dev *pdev) 137 { 138 struct xe_device *xe = pdev_to_xe_device(pdev); 139 140 xe_info(xe, "PCI error: resume\n"); 141 142 atomic_set(&xe->wedged.flag, 0); 143 } 144 145 const struct pci_error_handlers xe_pci_error_handlers = { 146 .error_detected = xe_pci_error_detected, 147 .mmio_enabled = xe_pci_error_mmio_enabled, 148 .slot_reset = xe_pci_error_slot_reset, 149 .resume = xe_pci_error_resume, 150 }; 151