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/types.h>
10 #include "intel_uc_fw_abi.h"
11 #include "intel_device_info.h"
12 #include "i915_gem.h"
13 #include "i915_vma.h"
14
15 struct drm_printer;
16 struct drm_i915_private;
17 struct intel_gt;
18
19 /* Home of GuC, HuC and DMC firmwares */
20 #define INTEL_UC_FIRMWARE_URL "https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/tree/i915"
21
22 /*
23 * +------------+---------------------------------------------------+
24 * | PHASE | FIRMWARE STATUS TRANSITIONS |
25 * +============+===================================================+
26 * | | UNINITIALIZED |
27 * +------------+- / | \ -+
28 * | | DISABLED <--/ | \--> NOT_SUPPORTED |
29 * | init_early | V |
30 * | | SELECTED |
31 * +------------+- / | \ -+
32 * | | MISSING <--/ | \--> ERROR |
33 * | fetch | V |
34 * | | AVAILABLE |
35 * +------------+- | \ -+
36 * | | | \--> INIT FAIL |
37 * | init | V |
38 * | | /------> LOADABLE <----<-----------\ |
39 * +------------+- \ / \ \ \ -+
40 * | | LOAD FAIL <--< \--> TRANSFERRED \ |
41 * | upload | \ / \ / |
42 * | | \---------/ \--> RUNNING |
43 * +------------+---------------------------------------------------+
44 */
45
46 enum intel_uc_fw_status {
47 INTEL_UC_FIRMWARE_NOT_SUPPORTED = -1, /* no uc HW */
48 INTEL_UC_FIRMWARE_UNINITIALIZED = 0, /* used to catch checks done too early */
49 INTEL_UC_FIRMWARE_DISABLED, /* disabled */
50 INTEL_UC_FIRMWARE_SELECTED, /* selected the blob we want to load */
51 INTEL_UC_FIRMWARE_MISSING, /* blob not found on the system */
52 INTEL_UC_FIRMWARE_ERROR, /* invalid format or version */
53 INTEL_UC_FIRMWARE_AVAILABLE, /* blob found and copied in mem */
54 INTEL_UC_FIRMWARE_INIT_FAIL, /* failed to prepare fw objects for load */
55 INTEL_UC_FIRMWARE_LOADABLE, /* all fw-required objects are ready */
56 INTEL_UC_FIRMWARE_LOAD_FAIL, /* failed to xfer or init/auth the fw */
57 INTEL_UC_FIRMWARE_TRANSFERRED, /* dma xfer done */
58 INTEL_UC_FIRMWARE_RUNNING /* init/auth done */
59 };
60
61 enum intel_uc_fw_type {
62 INTEL_UC_FW_TYPE_GUC = 0,
63 INTEL_UC_FW_TYPE_HUC
64 };
65 #define INTEL_UC_FW_NUM_TYPES 2
66
67 /*
68 * The firmware build process will generate a version header file with major and
69 * minor version defined. The versions are built into CSS header of firmware.
70 * i915 kernel driver set the minimal firmware version required per platform.
71 */
72 struct intel_uc_fw_file {
73 const char *path;
74 u16 major_ver;
75 u16 minor_ver;
76 u16 patch_ver;
77 };
78
79 /*
80 * This structure encapsulates all the data needed during the process
81 * of fetching, caching, and loading the firmware image into the uC.
82 */
83 struct intel_uc_fw {
84 enum intel_uc_fw_type type;
85 union {
86 const enum intel_uc_fw_status status;
87 enum intel_uc_fw_status __status; /* no accidental overwrites */
88 };
89 struct intel_uc_fw_file file_wanted;
90 struct intel_uc_fw_file file_selected;
91 bool user_overridden;
92 size_t size;
93 struct drm_i915_gem_object *obj;
94
95 /**
96 * @dummy: A vma used in binding the uc fw to ggtt. We can't define this
97 * vma on the stack as it can lead to a stack overflow, so we define it
98 * here. Safe to have 1 copy per uc fw because the binding is single
99 * threaded as it done during driver load (inherently single threaded)
100 * or during a GT reset (mutex guarantees single threaded).
101 */
102 struct i915_vma_resource dummy;
103 struct i915_vma *rsa_data;
104
105 u32 rsa_size;
106 u32 ucode_size;
107 u32 private_data_size;
108
109 bool loaded_via_gsc;
110 };
111
112 #define MAKE_UC_VER(maj, min, pat) ((pat) | ((min) << 8) | ((maj) << 16))
113 #define GET_UC_VER(uc) (MAKE_UC_VER((uc)->fw.file_selected.major_ver, \
114 (uc)->fw.file_selected.minor_ver, \
115 (uc)->fw.file_selected.patch_ver))
116
117 #ifdef CONFIG_DRM_I915_DEBUG_GUC
118 void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
119 enum intel_uc_fw_status status);
120 #else
intel_uc_fw_change_status(struct intel_uc_fw * uc_fw,enum intel_uc_fw_status status)121 static inline void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
122 enum intel_uc_fw_status status)
123 {
124 uc_fw->__status = status;
125 }
126 #endif
127
128 static inline
intel_uc_fw_status_repr(enum intel_uc_fw_status status)129 const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status)
130 {
131 switch (status) {
132 case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
133 return "N/A";
134 case INTEL_UC_FIRMWARE_UNINITIALIZED:
135 return "UNINITIALIZED";
136 case INTEL_UC_FIRMWARE_DISABLED:
137 return "DISABLED";
138 case INTEL_UC_FIRMWARE_SELECTED:
139 return "SELECTED";
140 case INTEL_UC_FIRMWARE_MISSING:
141 return "MISSING";
142 case INTEL_UC_FIRMWARE_ERROR:
143 return "ERROR";
144 case INTEL_UC_FIRMWARE_AVAILABLE:
145 return "AVAILABLE";
146 case INTEL_UC_FIRMWARE_INIT_FAIL:
147 return "INIT FAIL";
148 case INTEL_UC_FIRMWARE_LOADABLE:
149 return "LOADABLE";
150 case INTEL_UC_FIRMWARE_LOAD_FAIL:
151 return "LOAD FAIL";
152 case INTEL_UC_FIRMWARE_TRANSFERRED:
153 return "TRANSFERRED";
154 case INTEL_UC_FIRMWARE_RUNNING:
155 return "RUNNING";
156 }
157 return "<invalid>";
158 }
159
intel_uc_fw_status_to_error(enum intel_uc_fw_status status)160 static inline int intel_uc_fw_status_to_error(enum intel_uc_fw_status status)
161 {
162 switch (status) {
163 case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
164 return -ENODEV;
165 case INTEL_UC_FIRMWARE_UNINITIALIZED:
166 return -EACCES;
167 case INTEL_UC_FIRMWARE_DISABLED:
168 return -EPERM;
169 case INTEL_UC_FIRMWARE_MISSING:
170 return -ENOENT;
171 case INTEL_UC_FIRMWARE_ERROR:
172 return -ENOEXEC;
173 case INTEL_UC_FIRMWARE_INIT_FAIL:
174 case INTEL_UC_FIRMWARE_LOAD_FAIL:
175 return -EIO;
176 case INTEL_UC_FIRMWARE_SELECTED:
177 return -ESTALE;
178 case INTEL_UC_FIRMWARE_AVAILABLE:
179 case INTEL_UC_FIRMWARE_LOADABLE:
180 case INTEL_UC_FIRMWARE_TRANSFERRED:
181 case INTEL_UC_FIRMWARE_RUNNING:
182 return 0;
183 }
184 return -EINVAL;
185 }
186
intel_uc_fw_type_repr(enum intel_uc_fw_type type)187 static inline const char *intel_uc_fw_type_repr(enum intel_uc_fw_type type)
188 {
189 switch (type) {
190 case INTEL_UC_FW_TYPE_GUC:
191 return "GuC";
192 case INTEL_UC_FW_TYPE_HUC:
193 return "HuC";
194 }
195 return "uC";
196 }
197
198 static inline enum intel_uc_fw_status
__intel_uc_fw_status(struct intel_uc_fw * uc_fw)199 __intel_uc_fw_status(struct intel_uc_fw *uc_fw)
200 {
201 /* shouldn't call this before checking hw/blob availability */
202 GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED);
203 return uc_fw->status;
204 }
205
intel_uc_fw_is_supported(struct intel_uc_fw * uc_fw)206 static inline bool intel_uc_fw_is_supported(struct intel_uc_fw *uc_fw)
207 {
208 return __intel_uc_fw_status(uc_fw) != INTEL_UC_FIRMWARE_NOT_SUPPORTED;
209 }
210
intel_uc_fw_is_enabled(struct intel_uc_fw * uc_fw)211 static inline bool intel_uc_fw_is_enabled(struct intel_uc_fw *uc_fw)
212 {
213 return __intel_uc_fw_status(uc_fw) > INTEL_UC_FIRMWARE_DISABLED;
214 }
215
intel_uc_fw_is_available(struct intel_uc_fw * uc_fw)216 static inline bool intel_uc_fw_is_available(struct intel_uc_fw *uc_fw)
217 {
218 return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_AVAILABLE;
219 }
220
intel_uc_fw_is_loadable(struct intel_uc_fw * uc_fw)221 static inline bool intel_uc_fw_is_loadable(struct intel_uc_fw *uc_fw)
222 {
223 return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_LOADABLE;
224 }
225
intel_uc_fw_is_loaded(struct intel_uc_fw * uc_fw)226 static inline bool intel_uc_fw_is_loaded(struct intel_uc_fw *uc_fw)
227 {
228 return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_TRANSFERRED;
229 }
230
intel_uc_fw_is_running(struct intel_uc_fw * uc_fw)231 static inline bool intel_uc_fw_is_running(struct intel_uc_fw *uc_fw)
232 {
233 return __intel_uc_fw_status(uc_fw) == INTEL_UC_FIRMWARE_RUNNING;
234 }
235
intel_uc_fw_is_overridden(const struct intel_uc_fw * uc_fw)236 static inline bool intel_uc_fw_is_overridden(const struct intel_uc_fw *uc_fw)
237 {
238 return uc_fw->user_overridden;
239 }
240
intel_uc_fw_sanitize(struct intel_uc_fw * uc_fw)241 static inline void intel_uc_fw_sanitize(struct intel_uc_fw *uc_fw)
242 {
243 if (intel_uc_fw_is_loaded(uc_fw))
244 intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_LOADABLE);
245 }
246
__intel_uc_fw_get_upload_size(struct intel_uc_fw * uc_fw)247 static inline u32 __intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
248 {
249 return sizeof(struct uc_css_header) + uc_fw->ucode_size;
250 }
251
252 /**
253 * intel_uc_fw_get_upload_size() - Get size of firmware needed to be uploaded.
254 * @uc_fw: uC firmware.
255 *
256 * Get the size of the firmware and header that will be uploaded to WOPCM.
257 *
258 * Return: Upload firmware size, or zero on firmware fetch failure.
259 */
intel_uc_fw_get_upload_size(struct intel_uc_fw * uc_fw)260 static inline u32 intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
261 {
262 if (!intel_uc_fw_is_available(uc_fw))
263 return 0;
264
265 return __intel_uc_fw_get_upload_size(uc_fw);
266 }
267
268 void intel_uc_fw_init_early(struct intel_uc_fw *uc_fw,
269 enum intel_uc_fw_type type);
270 int intel_uc_fw_fetch(struct intel_uc_fw *uc_fw);
271 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw);
272 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, u32 offset, u32 dma_flags);
273 int intel_uc_fw_init(struct intel_uc_fw *uc_fw);
274 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw);
275 size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len);
276 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p);
277
278 #endif
279