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