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