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