xref: /linux/drivers/gpu/drm/xe/xe_uc_fw.h (revision 99fea6828879381405dba598627aea79fa6edd78)
1dd08ebf6SMatthew Brost /* SPDX-License-Identifier: MIT */
2dd08ebf6SMatthew Brost /*
3dd08ebf6SMatthew Brost  * Copyright © 2022 Intel Corporation
4dd08ebf6SMatthew Brost  */
5dd08ebf6SMatthew Brost 
6dd08ebf6SMatthew Brost #ifndef _XE_UC_FW_H_
7dd08ebf6SMatthew Brost #define _XE_UC_FW_H_
8dd08ebf6SMatthew Brost 
9dd08ebf6SMatthew Brost #include <linux/errno.h>
10dd08ebf6SMatthew Brost 
11dd08ebf6SMatthew Brost #include "xe_macros.h"
12ea9f879dSLucas De Marchi #include "xe_uc_fw_abi.h"
13ea9f879dSLucas De Marchi #include "xe_uc_fw_types.h"
14dd08ebf6SMatthew Brost 
15dd08ebf6SMatthew Brost struct drm_printer;
16dd08ebf6SMatthew Brost 
17dd08ebf6SMatthew Brost int xe_uc_fw_init(struct xe_uc_fw *uc_fw);
18dd08ebf6SMatthew Brost size_t xe_uc_fw_copy_rsa(struct xe_uc_fw *uc_fw, void *dst, u32 max_len);
19dd08ebf6SMatthew Brost int xe_uc_fw_upload(struct xe_uc_fw *uc_fw, u32 offset, u32 dma_flags);
20dd08ebf6SMatthew Brost void xe_uc_fw_print(struct xe_uc_fw *uc_fw, struct drm_printer *p);
21dd08ebf6SMatthew Brost 
22dd08ebf6SMatthew Brost static inline u32 xe_uc_fw_rsa_offset(struct xe_uc_fw *uc_fw)
23dd08ebf6SMatthew Brost {
24dd08ebf6SMatthew Brost 	return sizeof(struct uc_css_header) + uc_fw->ucode_size;
25dd08ebf6SMatthew Brost }
26dd08ebf6SMatthew Brost 
27dd08ebf6SMatthew Brost static inline void xe_uc_fw_change_status(struct xe_uc_fw *uc_fw,
28dd08ebf6SMatthew Brost 					  enum xe_uc_fw_status status)
29dd08ebf6SMatthew Brost {
30dd08ebf6SMatthew Brost 	uc_fw->__status = status;
31dd08ebf6SMatthew Brost }
32dd08ebf6SMatthew Brost 
33dd08ebf6SMatthew Brost static inline
34dd08ebf6SMatthew Brost const char *xe_uc_fw_status_repr(enum xe_uc_fw_status status)
35dd08ebf6SMatthew Brost {
36dd08ebf6SMatthew Brost 	switch (status) {
37dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_NOT_SUPPORTED:
38dd08ebf6SMatthew Brost 		return "N/A";
39dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_UNINITIALIZED:
40dd08ebf6SMatthew Brost 		return "UNINITIALIZED";
41dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_DISABLED:
42dd08ebf6SMatthew Brost 		return "DISABLED";
43dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_SELECTED:
44dd08ebf6SMatthew Brost 		return "SELECTED";
45dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_MISSING:
46dd08ebf6SMatthew Brost 		return "MISSING";
47dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_ERROR:
48dd08ebf6SMatthew Brost 		return "ERROR";
49dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_AVAILABLE:
50dd08ebf6SMatthew Brost 		return "AVAILABLE";
51dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_INIT_FAIL:
52dd08ebf6SMatthew Brost 		return "INIT FAIL";
53dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_LOADABLE:
54dd08ebf6SMatthew Brost 		return "LOADABLE";
55dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_LOAD_FAIL:
56dd08ebf6SMatthew Brost 		return "LOAD FAIL";
57dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_TRANSFERRED:
58dd08ebf6SMatthew Brost 		return "TRANSFERRED";
59dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_RUNNING:
60dd08ebf6SMatthew Brost 		return "RUNNING";
61dd08ebf6SMatthew Brost 	}
62dd08ebf6SMatthew Brost 	return "<invalid>";
63dd08ebf6SMatthew Brost }
64dd08ebf6SMatthew Brost 
65dd08ebf6SMatthew Brost static inline int xe_uc_fw_status_to_error(enum xe_uc_fw_status status)
66dd08ebf6SMatthew Brost {
67dd08ebf6SMatthew Brost 	switch (status) {
68dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_NOT_SUPPORTED:
69dd08ebf6SMatthew Brost 		return -ENODEV;
70dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_UNINITIALIZED:
71dd08ebf6SMatthew Brost 		return -EACCES;
72dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_DISABLED:
73dd08ebf6SMatthew Brost 		return -EPERM;
74dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_MISSING:
75dd08ebf6SMatthew Brost 		return -ENOENT;
76dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_ERROR:
77dd08ebf6SMatthew Brost 		return -ENOEXEC;
78dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_INIT_FAIL:
79dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_LOAD_FAIL:
80dd08ebf6SMatthew Brost 		return -EIO;
81dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_SELECTED:
82dd08ebf6SMatthew Brost 		return -ESTALE;
83dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_AVAILABLE:
84dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_LOADABLE:
85dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_TRANSFERRED:
86dd08ebf6SMatthew Brost 	case XE_UC_FIRMWARE_RUNNING:
87dd08ebf6SMatthew Brost 		return 0;
88dd08ebf6SMatthew Brost 	}
89dd08ebf6SMatthew Brost 	return -EINVAL;
90dd08ebf6SMatthew Brost }
91dd08ebf6SMatthew Brost 
92dd08ebf6SMatthew Brost static inline const char *xe_uc_fw_type_repr(enum xe_uc_fw_type type)
93dd08ebf6SMatthew Brost {
94dd08ebf6SMatthew Brost 	switch (type) {
95dd08ebf6SMatthew Brost 	case XE_UC_FW_TYPE_GUC:
96dd08ebf6SMatthew Brost 		return "GuC";
97dd08ebf6SMatthew Brost 	case XE_UC_FW_TYPE_HUC:
98dd08ebf6SMatthew Brost 		return "HuC";
99dd08ebf6SMatthew Brost 	}
100dd08ebf6SMatthew Brost 	return "uC";
101dd08ebf6SMatthew Brost }
102dd08ebf6SMatthew Brost 
103dd08ebf6SMatthew Brost static inline enum xe_uc_fw_status
104dd08ebf6SMatthew Brost __xe_uc_fw_status(struct xe_uc_fw *uc_fw)
105dd08ebf6SMatthew Brost {
106dd08ebf6SMatthew Brost 	/* shouldn't call this before checking hw/blob availability */
107*99fea682SFrancois Dugast 	XE_WARN_ON(uc_fw->status == XE_UC_FIRMWARE_UNINITIALIZED);
108dd08ebf6SMatthew Brost 	return uc_fw->status;
109dd08ebf6SMatthew Brost }
110dd08ebf6SMatthew Brost 
111dd08ebf6SMatthew Brost static inline bool xe_uc_fw_is_supported(struct xe_uc_fw *uc_fw)
112dd08ebf6SMatthew Brost {
113dd08ebf6SMatthew Brost 	return __xe_uc_fw_status(uc_fw) != XE_UC_FIRMWARE_NOT_SUPPORTED;
114dd08ebf6SMatthew Brost }
115dd08ebf6SMatthew Brost 
116dd08ebf6SMatthew Brost static inline bool xe_uc_fw_is_enabled(struct xe_uc_fw *uc_fw)
117dd08ebf6SMatthew Brost {
118dd08ebf6SMatthew Brost 	return __xe_uc_fw_status(uc_fw) > XE_UC_FIRMWARE_DISABLED;
119dd08ebf6SMatthew Brost }
120dd08ebf6SMatthew Brost 
121dd08ebf6SMatthew Brost static inline bool xe_uc_fw_is_disabled(struct xe_uc_fw *uc_fw)
122dd08ebf6SMatthew Brost {
123dd08ebf6SMatthew Brost 	return __xe_uc_fw_status(uc_fw) == XE_UC_FIRMWARE_DISABLED;
124dd08ebf6SMatthew Brost }
125dd08ebf6SMatthew Brost 
126dd08ebf6SMatthew Brost static inline bool xe_uc_fw_is_available(struct xe_uc_fw *uc_fw)
127dd08ebf6SMatthew Brost {
128dd08ebf6SMatthew Brost 	return __xe_uc_fw_status(uc_fw) >= XE_UC_FIRMWARE_AVAILABLE;
129dd08ebf6SMatthew Brost }
130dd08ebf6SMatthew Brost 
131dd08ebf6SMatthew Brost static inline bool xe_uc_fw_is_loadable(struct xe_uc_fw *uc_fw)
132dd08ebf6SMatthew Brost {
133dd08ebf6SMatthew Brost 	return __xe_uc_fw_status(uc_fw) >= XE_UC_FIRMWARE_LOADABLE;
134dd08ebf6SMatthew Brost }
135dd08ebf6SMatthew Brost 
136dd08ebf6SMatthew Brost static inline bool xe_uc_fw_is_loaded(struct xe_uc_fw *uc_fw)
137dd08ebf6SMatthew Brost {
138dd08ebf6SMatthew Brost 	return __xe_uc_fw_status(uc_fw) >= XE_UC_FIRMWARE_TRANSFERRED;
139dd08ebf6SMatthew Brost }
140dd08ebf6SMatthew Brost 
141dd08ebf6SMatthew Brost static inline bool xe_uc_fw_is_running(struct xe_uc_fw *uc_fw)
142dd08ebf6SMatthew Brost {
143dd08ebf6SMatthew Brost 	return __xe_uc_fw_status(uc_fw) == XE_UC_FIRMWARE_RUNNING;
144dd08ebf6SMatthew Brost }
145dd08ebf6SMatthew Brost 
146dd08ebf6SMatthew Brost static inline bool xe_uc_fw_is_overridden(const struct xe_uc_fw *uc_fw)
147dd08ebf6SMatthew Brost {
148dd08ebf6SMatthew Brost 	return uc_fw->user_overridden;
149dd08ebf6SMatthew Brost }
150dd08ebf6SMatthew Brost 
151dd08ebf6SMatthew Brost static inline void xe_uc_fw_sanitize(struct xe_uc_fw *uc_fw)
152dd08ebf6SMatthew Brost {
153dd08ebf6SMatthew Brost 	if (xe_uc_fw_is_loaded(uc_fw))
154dd08ebf6SMatthew Brost 		xe_uc_fw_change_status(uc_fw, XE_UC_FIRMWARE_LOADABLE);
155dd08ebf6SMatthew Brost }
156dd08ebf6SMatthew Brost 
157dd08ebf6SMatthew Brost static inline u32 __xe_uc_fw_get_upload_size(struct xe_uc_fw *uc_fw)
158dd08ebf6SMatthew Brost {
159dd08ebf6SMatthew Brost 	return sizeof(struct uc_css_header) + uc_fw->ucode_size;
160dd08ebf6SMatthew Brost }
161dd08ebf6SMatthew Brost 
162dd08ebf6SMatthew Brost /**
163dd08ebf6SMatthew Brost  * xe_uc_fw_get_upload_size() - Get size of firmware needed to be uploaded.
164dd08ebf6SMatthew Brost  * @uc_fw: uC firmware.
165dd08ebf6SMatthew Brost  *
166dd08ebf6SMatthew Brost  * Get the size of the firmware and header that will be uploaded to WOPCM.
167dd08ebf6SMatthew Brost  *
168dd08ebf6SMatthew Brost  * Return: Upload firmware size, or zero on firmware fetch failure.
169dd08ebf6SMatthew Brost  */
170dd08ebf6SMatthew Brost static inline u32 xe_uc_fw_get_upload_size(struct xe_uc_fw *uc_fw)
171dd08ebf6SMatthew Brost {
172dd08ebf6SMatthew Brost 	if (!xe_uc_fw_is_available(uc_fw))
173dd08ebf6SMatthew Brost 		return 0;
174dd08ebf6SMatthew Brost 
175dd08ebf6SMatthew Brost 	return __xe_uc_fw_get_upload_size(uc_fw);
176dd08ebf6SMatthew Brost }
177dd08ebf6SMatthew Brost 
178ad55ead7SLucas De Marchi #define XE_UC_FIRMWARE_URL "https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git"
179dd08ebf6SMatthew Brost 
180dd08ebf6SMatthew Brost #endif
181