xref: /linux/drivers/gpu/drm/xe/xe_device.h (revision 170aafe35cb98e0f3fbacb446ea86389fbce22ea)
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 
13 static inline struct xe_device *to_xe_device(const struct drm_device *dev)
14 {
15 	return container_of(dev, struct xe_device, drm);
16 }
17 
18 static inline struct xe_device *pdev_to_xe_device(struct pci_dev *pdev)
19 {
20 	return pci_get_drvdata(pdev);
21 }
22 
23 static inline struct xe_device *ttm_to_xe_device(struct ttm_device *ttm)
24 {
25 	return container_of(ttm, struct xe_device, ttm);
26 }
27 
28 struct xe_device *xe_device_create(struct pci_dev *pdev,
29 				   const struct pci_device_id *ent);
30 int xe_device_probe_early(struct xe_device *xe);
31 int xe_device_probe(struct xe_device *xe);
32 void xe_device_remove(struct xe_device *xe);
33 void xe_device_shutdown(struct xe_device *xe);
34 
35 void xe_device_wmb(struct xe_device *xe);
36 
37 static inline struct xe_file *to_xe_file(const struct drm_file *file)
38 {
39 	return file->driver_priv;
40 }
41 
42 static inline struct xe_tile *xe_device_get_root_tile(struct xe_device *xe)
43 {
44 	return &xe->tiles[0];
45 }
46 
47 #define XE_MAX_GT_PER_TILE 2
48 
49 static inline struct xe_gt *xe_tile_get_gt(struct xe_tile *tile, u8 gt_id)
50 {
51 	if (drm_WARN_ON(&tile_to_xe(tile)->drm, gt_id >= XE_MAX_GT_PER_TILE))
52 		gt_id = 0;
53 
54 	return gt_id ? tile->media_gt : tile->primary_gt;
55 }
56 
57 static inline struct xe_gt *xe_device_get_gt(struct xe_device *xe, u8 gt_id)
58 {
59 	struct xe_tile *root_tile = xe_device_get_root_tile(xe);
60 	struct xe_gt *gt;
61 
62 	/*
63 	 * FIXME: This only works for now because multi-tile and standalone
64 	 * media are mutually exclusive on the platforms we have today.
65 	 *
66 	 * id => GT mapping may change once we settle on how we want to handle
67 	 * our UAPI.
68 	 */
69 	if (MEDIA_VER(xe) >= 13) {
70 		gt = xe_tile_get_gt(root_tile, gt_id);
71 	} else {
72 		if (drm_WARN_ON(&xe->drm, gt_id >= XE_MAX_TILES_PER_DEVICE))
73 			gt_id = 0;
74 
75 		gt = xe->tiles[gt_id].primary_gt;
76 	}
77 
78 	if (!gt)
79 		return NULL;
80 
81 	drm_WARN_ON(&xe->drm, gt->info.id != gt_id);
82 	drm_WARN_ON(&xe->drm, gt->info.type == XE_GT_TYPE_UNINITIALIZED);
83 
84 	return gt;
85 }
86 
87 /*
88  * Provide a GT structure suitable for performing non-GT MMIO operations against
89  * the primary tile.  Primarily intended for early tile initialization, display
90  * handling, top-most interrupt enable/disable, etc.  Since anything using the
91  * MMIO handle returned by this function doesn't need GSI offset translation,
92  * we'll return the primary GT from the root tile.
93  *
94  * FIXME: Fix the driver design so that 'gt' isn't the target of all MMIO
95  * operations.
96  *
97  * Returns the primary gt of the root tile.
98  */
99 static inline struct xe_gt *xe_root_mmio_gt(struct xe_device *xe)
100 {
101 	return xe_device_get_root_tile(xe)->primary_gt;
102 }
103 
104 static inline bool xe_device_uc_enabled(struct xe_device *xe)
105 {
106 	return !xe->info.force_execlist;
107 }
108 
109 #define for_each_tile(tile__, xe__, id__) \
110 	for ((id__) = 0; (id__) < (xe__)->info.tile_count; (id__)++) \
111 		for_each_if((tile__) = &(xe__)->tiles[(id__)])
112 
113 #define for_each_remote_tile(tile__, xe__, id__) \
114 	for ((id__) = 1; (id__) < (xe__)->info.tile_count; (id__)++) \
115 		for_each_if((tile__) = &(xe__)->tiles[(id__)])
116 
117 /*
118  * FIXME: This only works for now since multi-tile and standalone media
119  * happen to be mutually exclusive.  Future platforms may change this...
120  */
121 #define for_each_gt(gt__, xe__, id__) \
122 	for ((id__) = 0; (id__) < (xe__)->info.gt_count; (id__)++) \
123 		for_each_if((gt__) = xe_device_get_gt((xe__), (id__)))
124 
125 static inline struct xe_force_wake *gt_to_fw(struct xe_gt *gt)
126 {
127 	return &gt->mmio.fw;
128 }
129 
130 void xe_device_assert_mem_access(struct xe_device *xe);
131 
132 static inline bool xe_device_in_fault_mode(struct xe_device *xe)
133 {
134 	return xe->usm.num_vm_in_fault_mode != 0;
135 }
136 
137 static inline bool xe_device_in_non_fault_mode(struct xe_device *xe)
138 {
139 	return xe->usm.num_vm_in_non_fault_mode != 0;
140 }
141 
142 static inline bool xe_device_has_flat_ccs(struct xe_device *xe)
143 {
144 	return xe->info.has_flat_ccs;
145 }
146 
147 static inline bool xe_device_has_sriov(struct xe_device *xe)
148 {
149 	return xe->info.has_sriov;
150 }
151 
152 static inline bool xe_device_has_memirq(struct xe_device *xe)
153 {
154 	return GRAPHICS_VERx100(xe) >= 1250;
155 }
156 
157 u32 xe_device_ccs_bytes(struct xe_device *xe, u64 size);
158 
159 void xe_device_snapshot_print(struct xe_device *xe, struct drm_printer *p);
160 
161 u64 xe_device_canonicalize_addr(struct xe_device *xe, u64 address);
162 u64 xe_device_uncanonicalize_addr(struct xe_device *xe, u64 address);
163 
164 void xe_device_td_flush(struct xe_device *xe);
165 void xe_device_l2_flush(struct xe_device *xe);
166 
167 static inline bool xe_device_wedged(struct xe_device *xe)
168 {
169 	return atomic_read(&xe->wedged.flag);
170 }
171 
172 void xe_device_declare_wedged(struct xe_device *xe);
173 
174 struct xe_file *xe_file_get(struct xe_file *xef);
175 void xe_file_put(struct xe_file *xef);
176 
177 #endif
178