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