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_with_type(gt__, xe__, id__, typemask__) \ 135 for_each_gt((gt__), (xe__), (id__)) \ 136 for_each_if((typemask__) & BIT((gt__)->info.type)) 137 138 #define for_each_gt_on_tile(gt__, tile__, id__) \ 139 for_each_gt((gt__), (tile__)->xe, (id__)) \ 140 for_each_if((gt__)->tile == (tile__)) 141 142 static inline struct xe_force_wake *gt_to_fw(struct xe_gt *gt) 143 { 144 return >->pm.fw; 145 } 146 147 void xe_device_assert_mem_access(struct xe_device *xe); 148 149 static inline bool xe_device_has_flat_ccs(struct xe_device *xe) 150 { 151 return xe->info.has_flat_ccs; 152 } 153 154 static inline bool xe_device_has_sriov(struct xe_device *xe) 155 { 156 return xe->info.has_sriov; 157 } 158 159 static inline bool xe_device_has_msix(struct xe_device *xe) 160 { 161 return xe->irq.msix.nvec > 0; 162 } 163 164 static inline bool xe_device_has_memirq(struct xe_device *xe) 165 { 166 return GRAPHICS_VERx100(xe) >= 1250; 167 } 168 169 static inline bool xe_device_uses_memirq(struct xe_device *xe) 170 { 171 return xe_device_has_memirq(xe) && (IS_SRIOV_VF(xe) || xe_device_has_msix(xe)); 172 } 173 174 static inline bool xe_device_has_lmtt(struct xe_device *xe) 175 { 176 return IS_DGFX(xe); 177 } 178 179 static inline bool xe_device_has_mert(struct xe_device *xe) 180 { 181 return xe->info.has_mert; 182 } 183 184 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size); 185 186 void xe_device_snapshot_print(struct xe_device *xe, struct drm_printer *p); 187 188 u64 xe_device_canonicalize_addr(struct xe_device *xe, u64 address); 189 u64 xe_device_uncanonicalize_addr(struct xe_device *xe, u64 address); 190 191 bool xe_device_is_l2_flush_optimized(struct xe_device *xe); 192 void xe_device_td_flush(struct xe_device *xe); 193 void xe_device_l2_flush(struct xe_device *xe); 194 195 static inline bool xe_device_wedged(struct xe_device *xe) 196 { 197 return atomic_read(&xe->wedged.flag); 198 } 199 200 void xe_device_set_wedged_method(struct xe_device *xe, unsigned long method); 201 void xe_device_declare_wedged(struct xe_device *xe); 202 int xe_device_validate_wedged_mode(struct xe_device *xe, unsigned int mode); 203 const char *xe_wedged_mode_to_string(enum xe_wedged_mode mode); 204 205 struct xe_file *xe_file_get(struct xe_file *xef); 206 void xe_file_put(struct xe_file *xef); 207 208 int xe_is_injection_active(void); 209 210 bool xe_is_xe_file(const struct file *file); 211 212 struct xe_vm *xe_device_asid_to_vm(struct xe_device *xe, u32 asid); 213 214 /* 215 * Occasionally it is seen that the G2H worker starts running after a delay of more than 216 * a second even after being queued and activated by the Linux workqueue subsystem. This 217 * leads to G2H timeout error. The root cause of issue lies with scheduling latency of 218 * Lunarlake Hybrid CPU. Issue disappears if we disable Lunarlake atom cores from BIOS 219 * and this is beyond xe kmd. 220 * 221 * TODO: Drop this change once workqueue scheduling delay issue is fixed on LNL Hybrid CPU. 222 */ 223 #define LNL_FLUSH_WORKQUEUE(wq__) \ 224 flush_workqueue(wq__) 225 #define LNL_FLUSH_WORK(wrk__) \ 226 flush_work(wrk__) 227 228 #endif 229