xref: /openbsd/sys/dev/pci/drm/i915/gt/uc/intel_uc_fw.h (revision f005ef32)
1c349dbc7Sjsg /* SPDX-License-Identifier: MIT */
2c349dbc7Sjsg /*
3c349dbc7Sjsg  * Copyright © 2014-2019 Intel Corporation
4c349dbc7Sjsg  */
5c349dbc7Sjsg 
6c349dbc7Sjsg #ifndef _INTEL_UC_FW_H_
7c349dbc7Sjsg #define _INTEL_UC_FW_H_
8c349dbc7Sjsg 
9*f005ef32Sjsg #include <linux/sizes.h>
10c349dbc7Sjsg #include <linux/types.h>
11c349dbc7Sjsg #include "intel_uc_fw_abi.h"
12c349dbc7Sjsg #include "intel_device_info.h"
13c349dbc7Sjsg #include "i915_gem.h"
141bb76ff1Sjsg #include "i915_vma.h"
15c349dbc7Sjsg 
16c349dbc7Sjsg struct drm_printer;
17c349dbc7Sjsg struct drm_i915_private;
18c349dbc7Sjsg struct intel_gt;
19c349dbc7Sjsg 
20c349dbc7Sjsg /* Home of GuC, HuC and DMC firmwares */
21c349dbc7Sjsg #define INTEL_UC_FIRMWARE_URL "https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/tree/i915"
22c349dbc7Sjsg 
23c349dbc7Sjsg /*
24c349dbc7Sjsg  * +------------+---------------------------------------------------+
25c349dbc7Sjsg  * |   PHASE    |           FIRMWARE STATUS TRANSITIONS             |
26c349dbc7Sjsg  * +============+===================================================+
27c349dbc7Sjsg  * |            |               UNINITIALIZED                       |
28c349dbc7Sjsg  * +------------+-               /   |   \                         -+
29c349dbc7Sjsg  * |            |   DISABLED <--/    |    \--> NOT_SUPPORTED        |
30c349dbc7Sjsg  * | init_early |                    V                              |
31c349dbc7Sjsg  * |            |                 SELECTED                          |
32c349dbc7Sjsg  * +------------+-               /   |   \                         -+
33c349dbc7Sjsg  * |            |    MISSING <--/    |    \--> ERROR                |
34c349dbc7Sjsg  * |   fetch    |                    V                              |
35c349dbc7Sjsg  * |            |                 AVAILABLE                         |
36669f2e97Sjsg  * +------------+-                   |   \                         -+
37669f2e97Sjsg  * |            |                    |    \--> INIT FAIL            |
38c349dbc7Sjsg  * |   init     |                    V                              |
39c349dbc7Sjsg  * |            |        /------> LOADABLE <----<-----------\       |
40c349dbc7Sjsg  * +------------+-       \         /    \        \           \     -+
41669f2e97Sjsg  * |            |    LOAD FAIL <--<      \--> TRANSFERRED     \     |
42c349dbc7Sjsg  * |   upload   |                  \           /   \          /     |
43c349dbc7Sjsg  * |            |                   \---------/     \--> RUNNING    |
44c349dbc7Sjsg  * +------------+---------------------------------------------------+
45c349dbc7Sjsg  */
46c349dbc7Sjsg 
47c349dbc7Sjsg enum intel_uc_fw_status {
48c349dbc7Sjsg 	INTEL_UC_FIRMWARE_NOT_SUPPORTED = -1, /* no uc HW */
49c349dbc7Sjsg 	INTEL_UC_FIRMWARE_UNINITIALIZED = 0, /* used to catch checks done too early */
50c349dbc7Sjsg 	INTEL_UC_FIRMWARE_DISABLED, /* disabled */
51c349dbc7Sjsg 	INTEL_UC_FIRMWARE_SELECTED, /* selected the blob we want to load */
52c349dbc7Sjsg 	INTEL_UC_FIRMWARE_MISSING, /* blob not found on the system */
53c349dbc7Sjsg 	INTEL_UC_FIRMWARE_ERROR, /* invalid format or version */
54c349dbc7Sjsg 	INTEL_UC_FIRMWARE_AVAILABLE, /* blob found and copied in mem */
55669f2e97Sjsg 	INTEL_UC_FIRMWARE_INIT_FAIL, /* failed to prepare fw objects for load */
56c349dbc7Sjsg 	INTEL_UC_FIRMWARE_LOADABLE, /* all fw-required objects are ready */
57669f2e97Sjsg 	INTEL_UC_FIRMWARE_LOAD_FAIL, /* failed to xfer or init/auth the fw */
58c349dbc7Sjsg 	INTEL_UC_FIRMWARE_TRANSFERRED, /* dma xfer done */
59c349dbc7Sjsg 	INTEL_UC_FIRMWARE_RUNNING /* init/auth done */
60c349dbc7Sjsg };
61c349dbc7Sjsg 
62c349dbc7Sjsg enum intel_uc_fw_type {
63c349dbc7Sjsg 	INTEL_UC_FW_TYPE_GUC = 0,
64*f005ef32Sjsg 	INTEL_UC_FW_TYPE_HUC,
65*f005ef32Sjsg 	INTEL_UC_FW_TYPE_GSC,
66c349dbc7Sjsg };
67*f005ef32Sjsg #define INTEL_UC_FW_NUM_TYPES 3
68*f005ef32Sjsg 
69*f005ef32Sjsg struct intel_uc_fw_ver {
70*f005ef32Sjsg 	u32 major;
71*f005ef32Sjsg 	u32 minor;
72*f005ef32Sjsg 	u32 patch;
73*f005ef32Sjsg 	u32 build;
74*f005ef32Sjsg };
75c349dbc7Sjsg 
76c349dbc7Sjsg /*
771bb76ff1Sjsg  * The firmware build process will generate a version header file with major and
781bb76ff1Sjsg  * minor version defined. The versions are built into CSS header of firmware.
791bb76ff1Sjsg  * i915 kernel driver set the minimal firmware version required per platform.
801bb76ff1Sjsg  */
811bb76ff1Sjsg struct intel_uc_fw_file {
821bb76ff1Sjsg 	const char *path;
83*f005ef32Sjsg 	struct intel_uc_fw_ver ver;
841bb76ff1Sjsg };
851bb76ff1Sjsg 
861bb76ff1Sjsg /*
87c349dbc7Sjsg  * This structure encapsulates all the data needed during the process
88c349dbc7Sjsg  * of fetching, caching, and loading the firmware image into the uC.
89c349dbc7Sjsg  */
90c349dbc7Sjsg struct intel_uc_fw {
91c349dbc7Sjsg 	enum intel_uc_fw_type type;
92c349dbc7Sjsg 	union {
93c349dbc7Sjsg 		const enum intel_uc_fw_status status;
94c349dbc7Sjsg 		enum intel_uc_fw_status __status; /* no accidental overwrites */
95c349dbc7Sjsg 	};
961bb76ff1Sjsg 	struct intel_uc_fw_file file_wanted;
971bb76ff1Sjsg 	struct intel_uc_fw_file file_selected;
98c349dbc7Sjsg 	bool user_overridden;
99c349dbc7Sjsg 	size_t size;
100c349dbc7Sjsg 	struct drm_i915_gem_object *obj;
101c349dbc7Sjsg 
1021bb76ff1Sjsg 	/**
103*f005ef32Sjsg 	 * @needs_ggtt_mapping: indicates whether the fw object needs to be
104*f005ef32Sjsg 	 * pinned to ggtt. If true, the fw is pinned at init time and unpinned
105*f005ef32Sjsg 	 * during driver unload.
106c349dbc7Sjsg 	 */
107*f005ef32Sjsg 	bool needs_ggtt_mapping;
108*f005ef32Sjsg 
109*f005ef32Sjsg 	/**
110*f005ef32Sjsg 	 * @vma_res: A vma resource used in binding the uc fw to ggtt. The fw is
111*f005ef32Sjsg 	 * pinned in a reserved area of the ggtt (above the maximum address
112*f005ef32Sjsg 	 * usable by GuC); therefore, we can't use the normal vma functions to
113*f005ef32Sjsg 	 * do the pinning and we instead use this resource to do so.
114*f005ef32Sjsg 	 */
115*f005ef32Sjsg 	struct i915_vma_resource vma_res;
1161bb76ff1Sjsg 	struct i915_vma *rsa_data;
117c349dbc7Sjsg 
118c349dbc7Sjsg 	u32 rsa_size;
119c349dbc7Sjsg 	u32 ucode_size;
1205ca02815Sjsg 	u32 private_data_size;
1211bb76ff1Sjsg 
122*f005ef32Sjsg 	u32 dma_start_offset;
123*f005ef32Sjsg 
124*f005ef32Sjsg 	bool has_gsc_headers;
125c349dbc7Sjsg };
126c349dbc7Sjsg 
127*f005ef32Sjsg /*
128*f005ef32Sjsg  * When we load the uC binaries, we pin them in a reserved section at the top of
129*f005ef32Sjsg  * the GGTT, which is ~18 MBs. On multi-GT systems where the GTs share the GGTT,
130*f005ef32Sjsg  * we also need to make sure that each binary is pinned to a unique location
131*f005ef32Sjsg  * during load, because the different GT can go through the FW load at the same
132*f005ef32Sjsg  * time (see uc_fw_ggtt_offset() for details).
133*f005ef32Sjsg  * Given that the available space is much greater than what is required by the
134*f005ef32Sjsg  * binaries, to keep things simple instead of dynamically partitioning the
135*f005ef32Sjsg  * reserved section to make space for all the blobs we can just reserve a static
136*f005ef32Sjsg  * chunk for each binary.
137*f005ef32Sjsg  */
138*f005ef32Sjsg #define INTEL_UC_RSVD_GGTT_PER_FW SZ_2M
1391bb76ff1Sjsg 
140c349dbc7Sjsg #ifdef CONFIG_DRM_I915_DEBUG_GUC
141c349dbc7Sjsg void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
142c349dbc7Sjsg 			       enum intel_uc_fw_status status);
143c349dbc7Sjsg #else
intel_uc_fw_change_status(struct intel_uc_fw * uc_fw,enum intel_uc_fw_status status)144c349dbc7Sjsg static inline void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
145c349dbc7Sjsg 					     enum intel_uc_fw_status status)
146c349dbc7Sjsg {
147c349dbc7Sjsg 	uc_fw->__status = status;
148c349dbc7Sjsg }
149c349dbc7Sjsg #endif
150c349dbc7Sjsg 
151c349dbc7Sjsg static inline
intel_uc_fw_status_repr(enum intel_uc_fw_status status)152c349dbc7Sjsg const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status)
153c349dbc7Sjsg {
154c349dbc7Sjsg 	switch (status) {
155c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
156c349dbc7Sjsg 		return "N/A";
157c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_UNINITIALIZED:
158c349dbc7Sjsg 		return "UNINITIALIZED";
159c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_DISABLED:
160c349dbc7Sjsg 		return "DISABLED";
161c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_SELECTED:
162c349dbc7Sjsg 		return "SELECTED";
163c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_MISSING:
164c349dbc7Sjsg 		return "MISSING";
165c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_ERROR:
166c349dbc7Sjsg 		return "ERROR";
167c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_AVAILABLE:
168c349dbc7Sjsg 		return "AVAILABLE";
169669f2e97Sjsg 	case INTEL_UC_FIRMWARE_INIT_FAIL:
170669f2e97Sjsg 		return "INIT FAIL";
171c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_LOADABLE:
172c349dbc7Sjsg 		return "LOADABLE";
173669f2e97Sjsg 	case INTEL_UC_FIRMWARE_LOAD_FAIL:
174669f2e97Sjsg 		return "LOAD FAIL";
175c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_TRANSFERRED:
176c349dbc7Sjsg 		return "TRANSFERRED";
177c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_RUNNING:
178c349dbc7Sjsg 		return "RUNNING";
179c349dbc7Sjsg 	}
180c349dbc7Sjsg 	return "<invalid>";
181c349dbc7Sjsg }
182c349dbc7Sjsg 
intel_uc_fw_status_to_error(enum intel_uc_fw_status status)183c349dbc7Sjsg static inline int intel_uc_fw_status_to_error(enum intel_uc_fw_status status)
184c349dbc7Sjsg {
185c349dbc7Sjsg 	switch (status) {
186c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
187c349dbc7Sjsg 		return -ENODEV;
188c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_UNINITIALIZED:
189c349dbc7Sjsg 		return -EACCES;
190c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_DISABLED:
191c349dbc7Sjsg 		return -EPERM;
192c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_MISSING:
193c349dbc7Sjsg 		return -ENOENT;
194c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_ERROR:
195c349dbc7Sjsg 		return -ENOEXEC;
196669f2e97Sjsg 	case INTEL_UC_FIRMWARE_INIT_FAIL:
197669f2e97Sjsg 	case INTEL_UC_FIRMWARE_LOAD_FAIL:
198c349dbc7Sjsg 		return -EIO;
199c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_SELECTED:
200c349dbc7Sjsg 		return -ESTALE;
201c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_AVAILABLE:
202c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_LOADABLE:
203c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_TRANSFERRED:
204c349dbc7Sjsg 	case INTEL_UC_FIRMWARE_RUNNING:
205c349dbc7Sjsg 		return 0;
206c349dbc7Sjsg 	}
207c349dbc7Sjsg 	return -EINVAL;
208c349dbc7Sjsg }
209c349dbc7Sjsg 
intel_uc_fw_type_repr(enum intel_uc_fw_type type)210c349dbc7Sjsg static inline const char *intel_uc_fw_type_repr(enum intel_uc_fw_type type)
211c349dbc7Sjsg {
212c349dbc7Sjsg 	switch (type) {
213c349dbc7Sjsg 	case INTEL_UC_FW_TYPE_GUC:
214c349dbc7Sjsg 		return "GuC";
215c349dbc7Sjsg 	case INTEL_UC_FW_TYPE_HUC:
216c349dbc7Sjsg 		return "HuC";
217*f005ef32Sjsg 	case INTEL_UC_FW_TYPE_GSC:
218*f005ef32Sjsg 		return "GSC";
219c349dbc7Sjsg 	}
220c349dbc7Sjsg 	return "uC";
221c349dbc7Sjsg }
222c349dbc7Sjsg 
223c349dbc7Sjsg static inline enum intel_uc_fw_status
__intel_uc_fw_status(struct intel_uc_fw * uc_fw)224c349dbc7Sjsg __intel_uc_fw_status(struct intel_uc_fw *uc_fw)
225c349dbc7Sjsg {
226c349dbc7Sjsg 	/* shouldn't call this before checking hw/blob availability */
227c349dbc7Sjsg 	GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED);
228c349dbc7Sjsg 	return uc_fw->status;
229c349dbc7Sjsg }
230c349dbc7Sjsg 
intel_uc_fw_is_supported(struct intel_uc_fw * uc_fw)231c349dbc7Sjsg static inline bool intel_uc_fw_is_supported(struct intel_uc_fw *uc_fw)
232c349dbc7Sjsg {
233c349dbc7Sjsg 	return __intel_uc_fw_status(uc_fw) != INTEL_UC_FIRMWARE_NOT_SUPPORTED;
234c349dbc7Sjsg }
235c349dbc7Sjsg 
intel_uc_fw_is_enabled(struct intel_uc_fw * uc_fw)236c349dbc7Sjsg static inline bool intel_uc_fw_is_enabled(struct intel_uc_fw *uc_fw)
237c349dbc7Sjsg {
238c349dbc7Sjsg 	return __intel_uc_fw_status(uc_fw) > INTEL_UC_FIRMWARE_DISABLED;
239c349dbc7Sjsg }
240c349dbc7Sjsg 
intel_uc_fw_is_available(struct intel_uc_fw * uc_fw)241c349dbc7Sjsg static inline bool intel_uc_fw_is_available(struct intel_uc_fw *uc_fw)
242c349dbc7Sjsg {
243c349dbc7Sjsg 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_AVAILABLE;
244c349dbc7Sjsg }
245c349dbc7Sjsg 
intel_uc_fw_is_loadable(struct intel_uc_fw * uc_fw)246c349dbc7Sjsg static inline bool intel_uc_fw_is_loadable(struct intel_uc_fw *uc_fw)
247c349dbc7Sjsg {
248c349dbc7Sjsg 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_LOADABLE;
249c349dbc7Sjsg }
250c349dbc7Sjsg 
intel_uc_fw_is_loaded(struct intel_uc_fw * uc_fw)251c349dbc7Sjsg static inline bool intel_uc_fw_is_loaded(struct intel_uc_fw *uc_fw)
252c349dbc7Sjsg {
253c349dbc7Sjsg 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_TRANSFERRED;
254c349dbc7Sjsg }
255c349dbc7Sjsg 
intel_uc_fw_is_running(struct intel_uc_fw * uc_fw)256c349dbc7Sjsg static inline bool intel_uc_fw_is_running(struct intel_uc_fw *uc_fw)
257c349dbc7Sjsg {
258c349dbc7Sjsg 	return __intel_uc_fw_status(uc_fw) == INTEL_UC_FIRMWARE_RUNNING;
259c349dbc7Sjsg }
260c349dbc7Sjsg 
intel_uc_fw_is_overridden(const struct intel_uc_fw * uc_fw)261c349dbc7Sjsg static inline bool intel_uc_fw_is_overridden(const struct intel_uc_fw *uc_fw)
262c349dbc7Sjsg {
263c349dbc7Sjsg 	return uc_fw->user_overridden;
264c349dbc7Sjsg }
265c349dbc7Sjsg 
intel_uc_fw_sanitize(struct intel_uc_fw * uc_fw)266c349dbc7Sjsg static inline void intel_uc_fw_sanitize(struct intel_uc_fw *uc_fw)
267c349dbc7Sjsg {
268c349dbc7Sjsg 	if (intel_uc_fw_is_loaded(uc_fw))
269c349dbc7Sjsg 		intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_LOADABLE);
270c349dbc7Sjsg }
271c349dbc7Sjsg 
__intel_uc_fw_get_upload_size(struct intel_uc_fw * uc_fw)272c349dbc7Sjsg static inline u32 __intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
273c349dbc7Sjsg {
274c349dbc7Sjsg 	return sizeof(struct uc_css_header) + uc_fw->ucode_size;
275c349dbc7Sjsg }
276c349dbc7Sjsg 
277c349dbc7Sjsg /**
278c349dbc7Sjsg  * intel_uc_fw_get_upload_size() - Get size of firmware needed to be uploaded.
279c349dbc7Sjsg  * @uc_fw: uC firmware.
280c349dbc7Sjsg  *
281c349dbc7Sjsg  * Get the size of the firmware and header that will be uploaded to WOPCM.
282c349dbc7Sjsg  *
283c349dbc7Sjsg  * Return: Upload firmware size, or zero on firmware fetch failure.
284c349dbc7Sjsg  */
intel_uc_fw_get_upload_size(struct intel_uc_fw * uc_fw)285c349dbc7Sjsg static inline u32 intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
286c349dbc7Sjsg {
287c349dbc7Sjsg 	if (!intel_uc_fw_is_available(uc_fw))
288c349dbc7Sjsg 		return 0;
289c349dbc7Sjsg 
290c349dbc7Sjsg 	return __intel_uc_fw_get_upload_size(uc_fw);
291c349dbc7Sjsg }
292c349dbc7Sjsg 
293*f005ef32Sjsg void intel_uc_fw_version_from_gsc_manifest(struct intel_uc_fw_ver *ver,
294*f005ef32Sjsg 					   const void *data);
295*f005ef32Sjsg int intel_uc_check_file_version(struct intel_uc_fw *uc_fw, bool *old_ver);
296c349dbc7Sjsg void intel_uc_fw_init_early(struct intel_uc_fw *uc_fw,
297*f005ef32Sjsg 			    enum intel_uc_fw_type type,
298*f005ef32Sjsg 			    bool needs_ggtt_mapping);
299c349dbc7Sjsg int intel_uc_fw_fetch(struct intel_uc_fw *uc_fw);
300c349dbc7Sjsg void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw);
301c349dbc7Sjsg int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, u32 offset, u32 dma_flags);
302c349dbc7Sjsg int intel_uc_fw_init(struct intel_uc_fw *uc_fw);
303c349dbc7Sjsg void intel_uc_fw_fini(struct intel_uc_fw *uc_fw);
304*f005ef32Sjsg void intel_uc_fw_resume_mapping(struct intel_uc_fw *uc_fw);
305c349dbc7Sjsg size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len);
306*f005ef32Sjsg int intel_uc_fw_mark_load_failed(struct intel_uc_fw *uc_fw, int err);
307c349dbc7Sjsg void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p);
308c349dbc7Sjsg 
309c349dbc7Sjsg #endif
310