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 	 * @dummy: A vma used in binding the uc fw to ggtt. We can't define this
103 	 * vma on the stack as it can lead to a stack overflow, so we define it
104 	 * here. Safe to have 1 copy per uc fw because the binding is single
105 	 * threaded as it done during driver load (inherently single threaded)
106 	 * or during a GT reset (mutex guarantees single threaded).
107 	 */
108 	struct i915_vma_resource dummy;
109 	struct i915_vma *rsa_data;
110 
111 	u32 rsa_size;
112 	u32 ucode_size;
113 	u32 private_data_size;
114 
115 	bool loaded_via_gsc;
116 };
117 
118 /*
119  * When we load the uC binaries, we pin them in a reserved section at the top of
120  * the GGTT, which is ~18 MBs. On multi-GT systems where the GTs share the GGTT,
121  * we also need to make sure that each binary is pinned to a unique location
122  * during load, because the different GT can go through the FW load at the same
123  * time (see uc_fw_ggtt_offset() for details).
124  * Given that the available space is much greater than what is required by the
125  * binaries, to keep things simple instead of dynamically partitioning the
126  * reserved section to make space for all the blobs we can just reserve a static
127  * chunk for each binary.
128  */
129 #define INTEL_UC_RSVD_GGTT_PER_FW SZ_2M
130 
131 #ifdef CONFIG_DRM_I915_DEBUG_GUC
132 void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
133 			       enum intel_uc_fw_status status);
134 #else
135 static inline void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
136 					     enum intel_uc_fw_status status)
137 {
138 	uc_fw->__status = status;
139 }
140 #endif
141 
142 static inline
143 const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status)
144 {
145 	switch (status) {
146 	case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
147 		return "N/A";
148 	case INTEL_UC_FIRMWARE_UNINITIALIZED:
149 		return "UNINITIALIZED";
150 	case INTEL_UC_FIRMWARE_DISABLED:
151 		return "DISABLED";
152 	case INTEL_UC_FIRMWARE_SELECTED:
153 		return "SELECTED";
154 	case INTEL_UC_FIRMWARE_MISSING:
155 		return "MISSING";
156 	case INTEL_UC_FIRMWARE_ERROR:
157 		return "ERROR";
158 	case INTEL_UC_FIRMWARE_AVAILABLE:
159 		return "AVAILABLE";
160 	case INTEL_UC_FIRMWARE_INIT_FAIL:
161 		return "INIT FAIL";
162 	case INTEL_UC_FIRMWARE_LOADABLE:
163 		return "LOADABLE";
164 	case INTEL_UC_FIRMWARE_LOAD_FAIL:
165 		return "LOAD FAIL";
166 	case INTEL_UC_FIRMWARE_TRANSFERRED:
167 		return "TRANSFERRED";
168 	case INTEL_UC_FIRMWARE_RUNNING:
169 		return "RUNNING";
170 	}
171 	return "<invalid>";
172 }
173 
174 static inline int intel_uc_fw_status_to_error(enum intel_uc_fw_status status)
175 {
176 	switch (status) {
177 	case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
178 		return -ENODEV;
179 	case INTEL_UC_FIRMWARE_UNINITIALIZED:
180 		return -EACCES;
181 	case INTEL_UC_FIRMWARE_DISABLED:
182 		return -EPERM;
183 	case INTEL_UC_FIRMWARE_MISSING:
184 		return -ENOENT;
185 	case INTEL_UC_FIRMWARE_ERROR:
186 		return -ENOEXEC;
187 	case INTEL_UC_FIRMWARE_INIT_FAIL:
188 	case INTEL_UC_FIRMWARE_LOAD_FAIL:
189 		return -EIO;
190 	case INTEL_UC_FIRMWARE_SELECTED:
191 		return -ESTALE;
192 	case INTEL_UC_FIRMWARE_AVAILABLE:
193 	case INTEL_UC_FIRMWARE_LOADABLE:
194 	case INTEL_UC_FIRMWARE_TRANSFERRED:
195 	case INTEL_UC_FIRMWARE_RUNNING:
196 		return 0;
197 	}
198 	return -EINVAL;
199 }
200 
201 static inline const char *intel_uc_fw_type_repr(enum intel_uc_fw_type type)
202 {
203 	switch (type) {
204 	case INTEL_UC_FW_TYPE_GUC:
205 		return "GuC";
206 	case INTEL_UC_FW_TYPE_HUC:
207 		return "HuC";
208 	case INTEL_UC_FW_TYPE_GSC:
209 		return "GSC";
210 	}
211 	return "uC";
212 }
213 
214 static inline enum intel_uc_fw_status
215 __intel_uc_fw_status(struct intel_uc_fw *uc_fw)
216 {
217 	/* shouldn't call this before checking hw/blob availability */
218 	GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED);
219 	return uc_fw->status;
220 }
221 
222 static inline bool intel_uc_fw_is_supported(struct intel_uc_fw *uc_fw)
223 {
224 	return __intel_uc_fw_status(uc_fw) != INTEL_UC_FIRMWARE_NOT_SUPPORTED;
225 }
226 
227 static inline bool intel_uc_fw_is_enabled(struct intel_uc_fw *uc_fw)
228 {
229 	return __intel_uc_fw_status(uc_fw) > INTEL_UC_FIRMWARE_DISABLED;
230 }
231 
232 static inline bool intel_uc_fw_is_available(struct intel_uc_fw *uc_fw)
233 {
234 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_AVAILABLE;
235 }
236 
237 static inline bool intel_uc_fw_is_loadable(struct intel_uc_fw *uc_fw)
238 {
239 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_LOADABLE;
240 }
241 
242 static inline bool intel_uc_fw_is_loaded(struct intel_uc_fw *uc_fw)
243 {
244 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_TRANSFERRED;
245 }
246 
247 static inline bool intel_uc_fw_is_running(struct intel_uc_fw *uc_fw)
248 {
249 	return __intel_uc_fw_status(uc_fw) == INTEL_UC_FIRMWARE_RUNNING;
250 }
251 
252 static inline bool intel_uc_fw_is_overridden(const struct intel_uc_fw *uc_fw)
253 {
254 	return uc_fw->user_overridden;
255 }
256 
257 static inline void intel_uc_fw_sanitize(struct intel_uc_fw *uc_fw)
258 {
259 	if (intel_uc_fw_is_loaded(uc_fw))
260 		intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_LOADABLE);
261 }
262 
263 static inline u32 __intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
264 {
265 	return sizeof(struct uc_css_header) + uc_fw->ucode_size;
266 }
267 
268 /**
269  * intel_uc_fw_get_upload_size() - Get size of firmware needed to be uploaded.
270  * @uc_fw: uC firmware.
271  *
272  * Get the size of the firmware and header that will be uploaded to WOPCM.
273  *
274  * Return: Upload firmware size, or zero on firmware fetch failure.
275  */
276 static inline u32 intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
277 {
278 	if (!intel_uc_fw_is_available(uc_fw))
279 		return 0;
280 
281 	return __intel_uc_fw_get_upload_size(uc_fw);
282 }
283 
284 void intel_uc_fw_init_early(struct intel_uc_fw *uc_fw,
285 			    enum intel_uc_fw_type type);
286 int intel_uc_fw_fetch(struct intel_uc_fw *uc_fw);
287 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw);
288 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, u32 offset, u32 dma_flags);
289 int intel_uc_fw_init(struct intel_uc_fw *uc_fw);
290 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw);
291 size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len);
292 int intel_uc_fw_mark_load_failed(struct intel_uc_fw *uc_fw, int err);
293 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p);
294 
295 #endif
296