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 
93 #ifdef CONFIG_DRM_I915_DEBUG_GUC
94 void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
95 			       enum intel_uc_fw_status status);
96 #else
97 static inline void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
98 					     enum intel_uc_fw_status status)
99 {
100 	uc_fw->__status = status;
101 }
102 #endif
103 
104 static inline
105 const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status)
106 {
107 	switch (status) {
108 	case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
109 		return "N/A";
110 	case INTEL_UC_FIRMWARE_UNINITIALIZED:
111 		return "UNINITIALIZED";
112 	case INTEL_UC_FIRMWARE_DISABLED:
113 		return "DISABLED";
114 	case INTEL_UC_FIRMWARE_SELECTED:
115 		return "SELECTED";
116 	case INTEL_UC_FIRMWARE_MISSING:
117 		return "MISSING";
118 	case INTEL_UC_FIRMWARE_ERROR:
119 		return "ERROR";
120 	case INTEL_UC_FIRMWARE_AVAILABLE:
121 		return "AVAILABLE";
122 	case INTEL_UC_FIRMWARE_LOADABLE:
123 		return "LOADABLE";
124 	case INTEL_UC_FIRMWARE_FAIL:
125 		return "FAIL";
126 	case INTEL_UC_FIRMWARE_TRANSFERRED:
127 		return "TRANSFERRED";
128 	case INTEL_UC_FIRMWARE_RUNNING:
129 		return "RUNNING";
130 	}
131 	return "<invalid>";
132 }
133 
134 static inline int intel_uc_fw_status_to_error(enum intel_uc_fw_status status)
135 {
136 	switch (status) {
137 	case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
138 		return -ENODEV;
139 	case INTEL_UC_FIRMWARE_UNINITIALIZED:
140 		return -EACCES;
141 	case INTEL_UC_FIRMWARE_DISABLED:
142 		return -EPERM;
143 	case INTEL_UC_FIRMWARE_MISSING:
144 		return -ENOENT;
145 	case INTEL_UC_FIRMWARE_ERROR:
146 		return -ENOEXEC;
147 	case INTEL_UC_FIRMWARE_FAIL:
148 		return -EIO;
149 	case INTEL_UC_FIRMWARE_SELECTED:
150 		return -ESTALE;
151 	case INTEL_UC_FIRMWARE_AVAILABLE:
152 	case INTEL_UC_FIRMWARE_LOADABLE:
153 	case INTEL_UC_FIRMWARE_TRANSFERRED:
154 	case INTEL_UC_FIRMWARE_RUNNING:
155 		return 0;
156 	}
157 	return -EINVAL;
158 }
159 
160 static inline const char *intel_uc_fw_type_repr(enum intel_uc_fw_type type)
161 {
162 	switch (type) {
163 	case INTEL_UC_FW_TYPE_GUC:
164 		return "GuC";
165 	case INTEL_UC_FW_TYPE_HUC:
166 		return "HuC";
167 	}
168 	return "uC";
169 }
170 
171 static inline enum intel_uc_fw_status
172 __intel_uc_fw_status(struct intel_uc_fw *uc_fw)
173 {
174 	/* shouldn't call this before checking hw/blob availability */
175 	GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED);
176 	return uc_fw->status;
177 }
178 
179 static inline bool intel_uc_fw_is_supported(struct intel_uc_fw *uc_fw)
180 {
181 	return __intel_uc_fw_status(uc_fw) != INTEL_UC_FIRMWARE_NOT_SUPPORTED;
182 }
183 
184 static inline bool intel_uc_fw_is_enabled(struct intel_uc_fw *uc_fw)
185 {
186 	return __intel_uc_fw_status(uc_fw) > INTEL_UC_FIRMWARE_DISABLED;
187 }
188 
189 static inline bool intel_uc_fw_is_available(struct intel_uc_fw *uc_fw)
190 {
191 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_AVAILABLE;
192 }
193 
194 static inline bool intel_uc_fw_is_loadable(struct intel_uc_fw *uc_fw)
195 {
196 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_LOADABLE;
197 }
198 
199 static inline bool intel_uc_fw_is_loaded(struct intel_uc_fw *uc_fw)
200 {
201 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_TRANSFERRED;
202 }
203 
204 static inline bool intel_uc_fw_is_running(struct intel_uc_fw *uc_fw)
205 {
206 	return __intel_uc_fw_status(uc_fw) == INTEL_UC_FIRMWARE_RUNNING;
207 }
208 
209 static inline bool intel_uc_fw_is_overridden(const struct intel_uc_fw *uc_fw)
210 {
211 	return uc_fw->user_overridden;
212 }
213 
214 static inline void intel_uc_fw_sanitize(struct intel_uc_fw *uc_fw)
215 {
216 	if (intel_uc_fw_is_loaded(uc_fw))
217 		intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_LOADABLE);
218 }
219 
220 static inline u32 __intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
221 {
222 	return sizeof(struct uc_css_header) + uc_fw->ucode_size;
223 }
224 
225 /**
226  * intel_uc_fw_get_upload_size() - Get size of firmware needed to be uploaded.
227  * @uc_fw: uC firmware.
228  *
229  * Get the size of the firmware and header that will be uploaded to WOPCM.
230  *
231  * Return: Upload firmware size, or zero on firmware fetch failure.
232  */
233 static inline u32 intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
234 {
235 	if (!intel_uc_fw_is_available(uc_fw))
236 		return 0;
237 
238 	return __intel_uc_fw_get_upload_size(uc_fw);
239 }
240 
241 void intel_uc_fw_init_early(struct intel_uc_fw *uc_fw,
242 			    enum intel_uc_fw_type type, bool supported,
243 			    enum intel_platform platform, u8 rev);
244 int intel_uc_fw_fetch(struct intel_uc_fw *uc_fw);
245 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw);
246 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, u32 offset, u32 dma_flags);
247 int intel_uc_fw_init(struct intel_uc_fw *uc_fw);
248 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw);
249 size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len);
250 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p);
251 
252 #endif
253