1 /* SPDX-License-Identifier: MIT */ 2 /* 3 * Copyright © 2021 Intel Corporation 4 */ 5 6 #ifndef _XE_DEVICE_H_ 7 #define _XE_DEVICE_H_ 8 9 #include <drm/drm_util.h> 10 11 #include "xe_device_types.h" 12 #include "xe_gt_types.h" 13 #include "xe_sriov.h" 14 15 struct xe_vm; 16 17 static inline struct xe_device *to_xe_device(const struct drm_device *dev) 18 { 19 return container_of(dev, struct xe_device, drm); 20 } 21 22 static inline struct xe_device *kdev_to_xe_device(struct device *kdev) 23 { 24 struct drm_device *drm = dev_get_drvdata(kdev); 25 26 return drm ? to_xe_device(drm) : NULL; 27 } 28 29 static inline struct xe_device *pdev_to_xe_device(struct pci_dev *pdev) 30 { 31 struct drm_device *drm = pci_get_drvdata(pdev); 32 33 return drm ? to_xe_device(drm) : NULL; 34 } 35 36 static inline struct xe_device *xe_device_const_cast(const struct xe_device *xe) 37 { 38 return (struct xe_device *)xe; 39 } 40 41 static inline struct xe_device *ttm_to_xe_device(struct ttm_device *ttm) 42 { 43 return container_of(ttm, struct xe_device, ttm); 44 } 45 46 struct xe_device *xe_device_create(struct pci_dev *pdev, 47 const struct pci_device_id *ent); 48 int xe_device_probe_early(struct xe_device *xe); 49 int xe_device_probe(struct xe_device *xe); 50 void xe_device_remove(struct xe_device *xe); 51 void xe_device_shutdown(struct xe_device *xe); 52 53 void xe_device_wmb(struct xe_device *xe); 54 55 static inline struct xe_file *to_xe_file(const struct drm_file *file) 56 { 57 return file->driver_priv; 58 } 59 60 static inline struct xe_tile *xe_device_get_root_tile(struct xe_device *xe) 61 { 62 return &xe->tiles[0]; 63 } 64 65 static inline struct xe_gt *xe_device_get_gt(struct xe_device *xe, u8 gt_id) 66 { 67 struct xe_tile *tile; 68 struct xe_gt *gt; 69 70 if (gt_id >= xe->info.tile_count * xe->info.max_gt_per_tile) 71 return NULL; 72 73 tile = &xe->tiles[gt_id / xe->info.max_gt_per_tile]; 74 switch (gt_id % xe->info.max_gt_per_tile) { 75 default: 76 xe_assert(xe, false); 77 fallthrough; 78 case 0: 79 gt = tile->primary_gt; 80 break; 81 case 1: 82 gt = tile->media_gt; 83 break; 84 } 85 86 if (!gt) 87 return NULL; 88 89 drm_WARN_ON(&xe->drm, gt->info.id != gt_id); 90 drm_WARN_ON(&xe->drm, gt->info.type == XE_GT_TYPE_UNINITIALIZED); 91 92 return gt; 93 } 94 95 /* 96 * Provide a GT structure suitable for performing non-GT MMIO operations against 97 * the primary tile. Primarily intended for early tile initialization, display 98 * handling, top-most interrupt enable/disable, etc. Since anything using the 99 * MMIO handle returned by this function doesn't need GSI offset translation, 100 * we'll return the primary GT from the root tile. 101 * 102 * FIXME: Fix the driver design so that 'gt' isn't the target of all MMIO 103 * operations. 104 * 105 * Returns the primary gt of the root tile. 106 */ 107 static inline struct xe_gt *xe_root_mmio_gt(struct xe_device *xe) 108 { 109 return xe_device_get_root_tile(xe)->primary_gt; 110 } 111 112 static inline struct xe_mmio *xe_root_tile_mmio(struct xe_device *xe) 113 { 114 return &xe->tiles[0].mmio; 115 } 116 117 static inline bool xe_device_uc_enabled(struct xe_device *xe) 118 { 119 return !xe->info.force_execlist; 120 } 121 122 #define for_each_tile(tile__, xe__, id__) \ 123 for ((id__) = 0; (id__) < (xe__)->info.tile_count; (id__)++) \ 124 for_each_if((tile__) = &(xe__)->tiles[(id__)]) 125 126 #define for_each_remote_tile(tile__, xe__, id__) \ 127 for ((id__) = 1; (id__) < (xe__)->info.tile_count; (id__)++) \ 128 for_each_if((tile__) = &(xe__)->tiles[(id__)]) 129 130 #define for_each_gt(gt__, xe__, id__) \ 131 for ((id__) = 0; (id__) < (xe__)->info.tile_count * (xe__)->info.max_gt_per_tile; (id__)++) \ 132 for_each_if((gt__) = xe_device_get_gt((xe__), (id__))) 133 134 #define for_each_gt_on_tile(gt__, tile__, id__) \ 135 for_each_gt((gt__), (tile__)->xe, (id__)) \ 136 for_each_if((gt__)->tile == (tile__)) 137 138 static inline struct xe_force_wake *gt_to_fw(struct xe_gt *gt) 139 { 140 return >->pm.fw; 141 } 142 143 void xe_device_assert_mem_access(struct xe_device *xe); 144 145 static inline bool xe_device_has_flat_ccs(struct xe_device *xe) 146 { 147 return xe->info.has_flat_ccs; 148 } 149 150 static inline bool xe_device_has_sriov(struct xe_device *xe) 151 { 152 return xe->info.has_sriov; 153 } 154 155 static inline bool xe_device_has_msix(struct xe_device *xe) 156 { 157 return xe->irq.msix.nvec > 0; 158 } 159 160 static inline bool xe_device_has_memirq(struct xe_device *xe) 161 { 162 return GRAPHICS_VERx100(xe) >= 1250; 163 } 164 165 static inline bool xe_device_uses_memirq(struct xe_device *xe) 166 { 167 return xe_device_has_memirq(xe) && (IS_SRIOV_VF(xe) || xe_device_has_msix(xe)); 168 } 169 170 static inline bool xe_device_has_lmtt(struct xe_device *xe) 171 { 172 return IS_DGFX(xe); 173 } 174 175 static inline bool xe_device_has_mert(struct xe_device *xe) 176 { 177 return xe->info.has_mert; 178 } 179 180 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size); 181 182 void xe_device_snapshot_print(struct xe_device *xe, struct drm_printer *p); 183 184 u64 xe_device_canonicalize_addr(struct xe_device *xe, u64 address); 185 u64 xe_device_uncanonicalize_addr(struct xe_device *xe, u64 address); 186 187 void xe_device_td_flush(struct xe_device *xe); 188 void xe_device_l2_flush(struct xe_device *xe); 189 190 static inline bool xe_device_wedged(struct xe_device *xe) 191 { 192 return atomic_read(&xe->wedged.flag); 193 } 194 195 void xe_device_set_wedged_method(struct xe_device *xe, unsigned long method); 196 void xe_device_declare_wedged(struct xe_device *xe); 197 int xe_device_validate_wedged_mode(struct xe_device *xe, unsigned int mode); 198 const char *xe_wedged_mode_to_string(enum xe_wedged_mode mode); 199 200 struct xe_file *xe_file_get(struct xe_file *xef); 201 void xe_file_put(struct xe_file *xef); 202 203 int xe_is_injection_active(void); 204 205 bool xe_is_xe_file(const struct file *file); 206 207 struct xe_vm *xe_device_asid_to_vm(struct xe_device *xe, u32 asid); 208 209 /* 210 * Occasionally it is seen that the G2H worker starts running after a delay of more than 211 * a second even after being queued and activated by the Linux workqueue subsystem. This 212 * leads to G2H timeout error. The root cause of issue lies with scheduling latency of 213 * Lunarlake Hybrid CPU. Issue disappears if we disable Lunarlake atom cores from BIOS 214 * and this is beyond xe kmd. 215 * 216 * TODO: Drop this change once workqueue scheduling delay issue is fixed on LNL Hybrid CPU. 217 */ 218 #define LNL_FLUSH_WORKQUEUE(wq__) \ 219 flush_workqueue(wq__) 220 #define LNL_FLUSH_WORK(wrk__) \ 221 flush_work(wrk__) 222 223 #endif 224