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 36 pci_disable_device(pdev); 37 } 38 39 static pci_ers_result_t ras_action_to_pci_result(struct pci_dev *pdev, u8 action) 40 { 41 switch (action) { 42 case XE_RAS_RECOVERY_ACTION_RECOVERED: 43 return PCI_ERS_RESULT_RECOVERED; 44 case XE_RAS_RECOVERY_ACTION_RESET: 45 prepare_device_for_reset(pdev); 46 return PCI_ERS_RESULT_NEED_RESET; 47 case XE_RAS_RECOVERY_ACTION_DISCONNECT: 48 return PCI_ERS_RESULT_DISCONNECT; 49 default: 50 return PCI_ERS_RESULT_DISCONNECT; 51 } 52 } 53 54 static pci_ers_result_t xe_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state) 55 { 56 struct xe_device *xe = pdev_to_xe_device(pdev); 57 58 xe_info(xe, "PCI error: detected state = %d\n", state); 59 60 if (state == pci_channel_io_perm_failure) 61 return PCI_ERS_RESULT_DISCONNECT; 62 63 /* If the device is already wedged or in survivability mode, do not attempt recovery */ 64 if (xe_survivability_mode_is_boot_enabled(xe) || xe_device_wedged(xe)) 65 return PCI_ERS_RESULT_DISCONNECT; 66 67 switch (state) { 68 case pci_channel_io_normal: 69 return PCI_ERS_RESULT_CAN_RECOVER; 70 case pci_channel_io_frozen: 71 prepare_device_for_reset(pdev); 72 return PCI_ERS_RESULT_NEED_RESET; 73 default: 74 xe_info(xe, "PCI error: unknown state %d\n", state); 75 return PCI_ERS_RESULT_DISCONNECT; 76 } 77 } 78 79 static pci_ers_result_t xe_pci_error_mmio_enabled(struct pci_dev *pdev) 80 { 81 struct xe_device *xe = pdev_to_xe_device(pdev); 82 enum xe_ras_recovery_action action; 83 84 xe_info(xe, "PCI error: MMIO enabled\n"); 85 action = xe_ras_process_errors(xe); 86 87 /* User wants to debug the error, prevent reset */ 88 if (xe->wedged.mode == XE_WEDGED_MODE_UPON_ANY_HANG_NO_RESET) { 89 xe_device_declare_wedged(xe); 90 return PCI_ERS_RESULT_DISCONNECT; 91 } 92 93 return ras_action_to_pci_result(pdev, action); 94 } 95 96 static pci_ers_result_t xe_pci_error_slot_reset(struct pci_dev *pdev) 97 { 98 const struct pci_device_id *ent = pci_match_id(pdev->driver->id_table, pdev); 99 struct xe_device *xe = pdev_to_xe_device(pdev); 100 int err; 101 102 xe_info(xe, "PCI error: slot reset\n"); 103 104 pci_restore_state(pdev); 105 106 err = pci_enable_device(pdev); 107 if (err) { 108 xe_log_err_fatal(xe, PCI, err, "Cannot re-enable PCI device after reset\n"); 109 return PCI_ERS_RESULT_DISCONNECT; 110 } 111 112 /* 113 * Secondary Bus Reset causes all VRAM state to be lost along with 114 * hardware state. As an initial step, re-probe the device to 115 * re-initialize the driver and hardware. 116 * TODO: optimize by re-initializing only the hardware state and re-creating 117 * kernel BOs. 118 */ 119 xe_device_clear_in_reset(xe); 120 pdev->driver->remove(pdev); 121 devres_release_group(&pdev->dev, xe->devres_group); 122 123 if (pdev->driver->probe(pdev, ent)) 124 return PCI_ERS_RESULT_DISCONNECT; 125 126 xe = pdev_to_xe_device(pdev); 127 128 /* Wedge the device to prevent I/O operations till the resume callback */ 129 atomic_set(&xe->wedged.flag, 1); 130 131 return PCI_ERS_RESULT_RECOVERED; 132 } 133 134 static void xe_pci_error_resume(struct pci_dev *pdev) 135 { 136 struct xe_device *xe = pdev_to_xe_device(pdev); 137 138 xe_info(xe, "PCI error: resume\n"); 139 140 atomic_set(&xe->wedged.flag, 0); 141 } 142 143 const struct pci_error_handlers xe_pci_error_handlers = { 144 .error_detected = xe_pci_error_detected, 145 .mmio_enabled = xe_pci_error_mmio_enabled, 146 .slot_reset = xe_pci_error_slot_reset, 147 .resume = xe_pci_error_resume, 148 }; 149