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