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 };
66 #define INTEL_UC_FW_NUM_TYPES 2
67 
68 /*
69  * The firmware build process will generate a version header file with major and
70  * minor version defined. The versions are built into CSS header of firmware.
71  * i915 kernel driver set the minimal firmware version required per platform.
72  */
73 struct intel_uc_fw_file {
74 	const char *path;
75 	u16 major_ver;
76 	u16 minor_ver;
77 	u16 patch_ver;
78 };
79 
80 /*
81  * This structure encapsulates all the data needed during the process
82  * of fetching, caching, and loading the firmware image into the uC.
83  */
84 struct intel_uc_fw {
85 	enum intel_uc_fw_type type;
86 	union {
87 		const enum intel_uc_fw_status status;
88 		enum intel_uc_fw_status __status; /* no accidental overwrites */
89 	};
90 	struct intel_uc_fw_file file_wanted;
91 	struct intel_uc_fw_file file_selected;
92 	bool user_overridden;
93 	size_t size;
94 	struct drm_i915_gem_object *obj;
95 
96 	/**
97 	 * @dummy: A vma used in binding the uc fw to ggtt. We can't define this
98 	 * vma on the stack as it can lead to a stack overflow, so we define it
99 	 * here. Safe to have 1 copy per uc fw because the binding is single
100 	 * threaded as it done during driver load (inherently single threaded)
101 	 * or during a GT reset (mutex guarantees single threaded).
102 	 */
103 	struct i915_vma_resource dummy;
104 	struct i915_vma *rsa_data;
105 
106 	u32 rsa_size;
107 	u32 ucode_size;
108 	u32 private_data_size;
109 
110 	bool loaded_via_gsc;
111 };
112 
113 #define MAKE_UC_VER(maj, min, pat)	((pat) | ((min) << 8) | ((maj) << 16))
114 #define GET_UC_VER(uc)			(MAKE_UC_VER((uc)->fw.file_selected.major_ver, \
115 						     (uc)->fw.file_selected.minor_ver, \
116 						     (uc)->fw.file_selected.patch_ver))
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 	}
209 	return "uC";
210 }
211 
212 static inline enum intel_uc_fw_status
213 __intel_uc_fw_status(struct intel_uc_fw *uc_fw)
214 {
215 	/* shouldn't call this before checking hw/blob availability */
216 	GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED);
217 	return uc_fw->status;
218 }
219 
220 static inline bool intel_uc_fw_is_supported(struct intel_uc_fw *uc_fw)
221 {
222 	return __intel_uc_fw_status(uc_fw) != INTEL_UC_FIRMWARE_NOT_SUPPORTED;
223 }
224 
225 static inline bool intel_uc_fw_is_enabled(struct intel_uc_fw *uc_fw)
226 {
227 	return __intel_uc_fw_status(uc_fw) > INTEL_UC_FIRMWARE_DISABLED;
228 }
229 
230 static inline bool intel_uc_fw_is_available(struct intel_uc_fw *uc_fw)
231 {
232 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_AVAILABLE;
233 }
234 
235 static inline bool intel_uc_fw_is_loadable(struct intel_uc_fw *uc_fw)
236 {
237 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_LOADABLE;
238 }
239 
240 static inline bool intel_uc_fw_is_loaded(struct intel_uc_fw *uc_fw)
241 {
242 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_TRANSFERRED;
243 }
244 
245 static inline bool intel_uc_fw_is_running(struct intel_uc_fw *uc_fw)
246 {
247 	return __intel_uc_fw_status(uc_fw) == INTEL_UC_FIRMWARE_RUNNING;
248 }
249 
250 static inline bool intel_uc_fw_is_overridden(const struct intel_uc_fw *uc_fw)
251 {
252 	return uc_fw->user_overridden;
253 }
254 
255 static inline void intel_uc_fw_sanitize(struct intel_uc_fw *uc_fw)
256 {
257 	if (intel_uc_fw_is_loaded(uc_fw))
258 		intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_LOADABLE);
259 }
260 
261 static inline u32 __intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
262 {
263 	return sizeof(struct uc_css_header) + uc_fw->ucode_size;
264 }
265 
266 /**
267  * intel_uc_fw_get_upload_size() - Get size of firmware needed to be uploaded.
268  * @uc_fw: uC firmware.
269  *
270  * Get the size of the firmware and header that will be uploaded to WOPCM.
271  *
272  * Return: Upload firmware size, or zero on firmware fetch failure.
273  */
274 static inline u32 intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
275 {
276 	if (!intel_uc_fw_is_available(uc_fw))
277 		return 0;
278 
279 	return __intel_uc_fw_get_upload_size(uc_fw);
280 }
281 
282 void intel_uc_fw_init_early(struct intel_uc_fw *uc_fw,
283 			    enum intel_uc_fw_type type);
284 int intel_uc_fw_fetch(struct intel_uc_fw *uc_fw);
285 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw);
286 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, u32 offset, u32 dma_flags);
287 int intel_uc_fw_init(struct intel_uc_fw *uc_fw);
288 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw);
289 size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len);
290 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p);
291 
292 #endif
293