1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2014-2019 Intel Corporation
4  */
5 
6 #ifndef _INTEL_UC_FW_H_
7 #define _INTEL_UC_FW_H_
8 
9 #include <linux/sizes.h>
10 #include <linux/types.h>
11 #include "intel_uc_fw_abi.h"
12 #include "intel_device_info.h"
13 #include "i915_gem.h"
14 #include "i915_vma.h"
15 
16 struct drm_printer;
17 struct drm_i915_private;
18 struct intel_gt;
19 
20 /* Home of GuC, HuC and DMC firmwares */
21 #define INTEL_UC_FIRMWARE_URL "https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/tree/i915"
22 
23 /*
24  * +------------+---------------------------------------------------+
25  * |   PHASE    |           FIRMWARE STATUS TRANSITIONS             |
26  * +============+===================================================+
27  * |            |               UNINITIALIZED                       |
28  * +------------+-               /   |   \                         -+
29  * |            |   DISABLED <--/    |    \--> NOT_SUPPORTED        |
30  * | init_early |                    V                              |
31  * |            |                 SELECTED                          |
32  * +------------+-               /   |   \                         -+
33  * |            |    MISSING <--/    |    \--> ERROR                |
34  * |   fetch    |                    V                              |
35  * |            |                 AVAILABLE                         |
36  * +------------+-                   |   \                         -+
37  * |            |                    |    \--> INIT FAIL            |
38  * |   init     |                    V                              |
39  * |            |        /------> LOADABLE <----<-----------\       |
40  * +------------+-       \         /    \        \           \     -+
41  * |            |    LOAD FAIL <--<      \--> TRANSFERRED     \     |
42  * |   upload   |                  \           /   \          /     |
43  * |            |                   \---------/     \--> RUNNING    |
44  * +------------+---------------------------------------------------+
45  */
46 
47 enum intel_uc_fw_status {
48 	INTEL_UC_FIRMWARE_NOT_SUPPORTED = -1, /* no uc HW */
49 	INTEL_UC_FIRMWARE_UNINITIALIZED = 0, /* used to catch checks done too early */
50 	INTEL_UC_FIRMWARE_DISABLED, /* disabled */
51 	INTEL_UC_FIRMWARE_SELECTED, /* selected the blob we want to load */
52 	INTEL_UC_FIRMWARE_MISSING, /* blob not found on the system */
53 	INTEL_UC_FIRMWARE_ERROR, /* invalid format or version */
54 	INTEL_UC_FIRMWARE_AVAILABLE, /* blob found and copied in mem */
55 	INTEL_UC_FIRMWARE_INIT_FAIL, /* failed to prepare fw objects for load */
56 	INTEL_UC_FIRMWARE_LOADABLE, /* all fw-required objects are ready */
57 	INTEL_UC_FIRMWARE_LOAD_FAIL, /* failed to xfer or init/auth the fw */
58 	INTEL_UC_FIRMWARE_TRANSFERRED, /* dma xfer done */
59 	INTEL_UC_FIRMWARE_RUNNING /* init/auth done */
60 };
61 
62 enum intel_uc_fw_type {
63 	INTEL_UC_FW_TYPE_GUC = 0,
64 	INTEL_UC_FW_TYPE_HUC,
65 	INTEL_UC_FW_TYPE_GSC,
66 };
67 #define INTEL_UC_FW_NUM_TYPES 3
68 
69 struct intel_uc_fw_ver {
70 	u32 major;
71 	u32 minor;
72 	u32 patch;
73 };
74 
75 /*
76  * The firmware build process will generate a version header file with major and
77  * minor version defined. The versions are built into CSS header of firmware.
78  * i915 kernel driver set the minimal firmware version required per platform.
79  */
80 struct intel_uc_fw_file {
81 	const char *path;
82 	struct intel_uc_fw_ver ver;
83 };
84 
85 /*
86  * This structure encapsulates all the data needed during the process
87  * of fetching, caching, and loading the firmware image into the uC.
88  */
89 struct intel_uc_fw {
90 	enum intel_uc_fw_type type;
91 	union {
92 		const enum intel_uc_fw_status status;
93 		enum intel_uc_fw_status __status; /* no accidental overwrites */
94 	};
95 	struct intel_uc_fw_file file_wanted;
96 	struct intel_uc_fw_file file_selected;
97 	bool user_overridden;
98 	size_t size;
99 	struct drm_i915_gem_object *obj;
100 
101 	/**
102 	 * @needs_ggtt_mapping: indicates whether the fw object needs to be
103 	 * pinned to ggtt. If true, the fw is pinned at init time and unpinned
104 	 * during driver unload.
105 	 */
106 	bool needs_ggtt_mapping;
107 
108 	/**
109 	 * @vma_res: A vma resource used in binding the uc fw to ggtt. The fw is
110 	 * pinned in a reserved area of the ggtt (above the maximum address
111 	 * usable by GuC); therefore, we can't use the normal vma functions to
112 	 * do the pinning and we instead use this resource to do so.
113 	 */
114 	struct i915_vma_resource vma_res;
115 	struct i915_vma *rsa_data;
116 
117 	u32 rsa_size;
118 	u32 ucode_size;
119 	u32 private_data_size;
120 
121 	u32 dma_start_offset;
122 
123 	bool has_gsc_headers;
124 };
125 
126 /*
127  * When we load the uC binaries, we pin them in a reserved section at the top of
128  * the GGTT, which is ~18 MBs. On multi-GT systems where the GTs share the GGTT,
129  * we also need to make sure that each binary is pinned to a unique location
130  * during load, because the different GT can go through the FW load at the same
131  * time (see uc_fw_ggtt_offset() for details).
132  * Given that the available space is much greater than what is required by the
133  * binaries, to keep things simple instead of dynamically partitioning the
134  * reserved section to make space for all the blobs we can just reserve a static
135  * chunk for each binary.
136  */
137 #define INTEL_UC_RSVD_GGTT_PER_FW SZ_2M
138 
139 #ifdef CONFIG_DRM_I915_DEBUG_GUC
140 void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
141 			       enum intel_uc_fw_status status);
142 #else
143 static inline void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
144 					     enum intel_uc_fw_status status)
145 {
146 	uc_fw->__status = status;
147 }
148 #endif
149 
150 static inline
151 const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status)
152 {
153 	switch (status) {
154 	case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
155 		return "N/A";
156 	case INTEL_UC_FIRMWARE_UNINITIALIZED:
157 		return "UNINITIALIZED";
158 	case INTEL_UC_FIRMWARE_DISABLED:
159 		return "DISABLED";
160 	case INTEL_UC_FIRMWARE_SELECTED:
161 		return "SELECTED";
162 	case INTEL_UC_FIRMWARE_MISSING:
163 		return "MISSING";
164 	case INTEL_UC_FIRMWARE_ERROR:
165 		return "ERROR";
166 	case INTEL_UC_FIRMWARE_AVAILABLE:
167 		return "AVAILABLE";
168 	case INTEL_UC_FIRMWARE_INIT_FAIL:
169 		return "INIT FAIL";
170 	case INTEL_UC_FIRMWARE_LOADABLE:
171 		return "LOADABLE";
172 	case INTEL_UC_FIRMWARE_LOAD_FAIL:
173 		return "LOAD FAIL";
174 	case INTEL_UC_FIRMWARE_TRANSFERRED:
175 		return "TRANSFERRED";
176 	case INTEL_UC_FIRMWARE_RUNNING:
177 		return "RUNNING";
178 	}
179 	return "<invalid>";
180 }
181 
182 static inline int intel_uc_fw_status_to_error(enum intel_uc_fw_status status)
183 {
184 	switch (status) {
185 	case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
186 		return -ENODEV;
187 	case INTEL_UC_FIRMWARE_UNINITIALIZED:
188 		return -EACCES;
189 	case INTEL_UC_FIRMWARE_DISABLED:
190 		return -EPERM;
191 	case INTEL_UC_FIRMWARE_MISSING:
192 		return -ENOENT;
193 	case INTEL_UC_FIRMWARE_ERROR:
194 		return -ENOEXEC;
195 	case INTEL_UC_FIRMWARE_INIT_FAIL:
196 	case INTEL_UC_FIRMWARE_LOAD_FAIL:
197 		return -EIO;
198 	case INTEL_UC_FIRMWARE_SELECTED:
199 		return -ESTALE;
200 	case INTEL_UC_FIRMWARE_AVAILABLE:
201 	case INTEL_UC_FIRMWARE_LOADABLE:
202 	case INTEL_UC_FIRMWARE_TRANSFERRED:
203 	case INTEL_UC_FIRMWARE_RUNNING:
204 		return 0;
205 	}
206 	return -EINVAL;
207 }
208 
209 static inline const char *intel_uc_fw_type_repr(enum intel_uc_fw_type type)
210 {
211 	switch (type) {
212 	case INTEL_UC_FW_TYPE_GUC:
213 		return "GuC";
214 	case INTEL_UC_FW_TYPE_HUC:
215 		return "HuC";
216 	case INTEL_UC_FW_TYPE_GSC:
217 		return "GSC";
218 	}
219 	return "uC";
220 }
221 
222 static inline enum intel_uc_fw_status
223 __intel_uc_fw_status(struct intel_uc_fw *uc_fw)
224 {
225 	/* shouldn't call this before checking hw/blob availability */
226 	GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED);
227 	return uc_fw->status;
228 }
229 
230 static inline bool intel_uc_fw_is_supported(struct intel_uc_fw *uc_fw)
231 {
232 	return __intel_uc_fw_status(uc_fw) != INTEL_UC_FIRMWARE_NOT_SUPPORTED;
233 }
234 
235 static inline bool intel_uc_fw_is_enabled(struct intel_uc_fw *uc_fw)
236 {
237 	return __intel_uc_fw_status(uc_fw) > INTEL_UC_FIRMWARE_DISABLED;
238 }
239 
240 static inline bool intel_uc_fw_is_available(struct intel_uc_fw *uc_fw)
241 {
242 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_AVAILABLE;
243 }
244 
245 static inline bool intel_uc_fw_is_loadable(struct intel_uc_fw *uc_fw)
246 {
247 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_LOADABLE;
248 }
249 
250 static inline bool intel_uc_fw_is_loaded(struct intel_uc_fw *uc_fw)
251 {
252 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_TRANSFERRED;
253 }
254 
255 static inline bool intel_uc_fw_is_running(struct intel_uc_fw *uc_fw)
256 {
257 	return __intel_uc_fw_status(uc_fw) == INTEL_UC_FIRMWARE_RUNNING;
258 }
259 
260 static inline bool intel_uc_fw_is_overridden(const struct intel_uc_fw *uc_fw)
261 {
262 	return uc_fw->user_overridden;
263 }
264 
265 static inline void intel_uc_fw_sanitize(struct intel_uc_fw *uc_fw)
266 {
267 	if (intel_uc_fw_is_loaded(uc_fw))
268 		intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_LOADABLE);
269 }
270 
271 static inline u32 __intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
272 {
273 	return sizeof(struct uc_css_header) + uc_fw->ucode_size;
274 }
275 
276 /**
277  * intel_uc_fw_get_upload_size() - Get size of firmware needed to be uploaded.
278  * @uc_fw: uC firmware.
279  *
280  * Get the size of the firmware and header that will be uploaded to WOPCM.
281  *
282  * Return: Upload firmware size, or zero on firmware fetch failure.
283  */
284 static inline u32 intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
285 {
286 	if (!intel_uc_fw_is_available(uc_fw))
287 		return 0;
288 
289 	return __intel_uc_fw_get_upload_size(uc_fw);
290 }
291 
292 void intel_uc_fw_init_early(struct intel_uc_fw *uc_fw,
293 			    enum intel_uc_fw_type type,
294 			    bool needs_ggtt_mapping);
295 int intel_uc_fw_fetch(struct intel_uc_fw *uc_fw);
296 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw);
297 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, u32 offset, u32 dma_flags);
298 int intel_uc_fw_init(struct intel_uc_fw *uc_fw);
299 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw);
300 void intel_uc_fw_resume_mapping(struct intel_uc_fw *uc_fw);
301 size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len);
302 int intel_uc_fw_mark_load_failed(struct intel_uc_fw *uc_fw, int err);
303 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p);
304 
305 #endif
306