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