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