Merge tag 'drm-next-2022-08-03' of git://anongit.freedesktop.org/drm/drm
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / gt / uc / intel_uc_fw.h
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         bool loaded_via_gsc;
114 };
115
116 #ifdef CONFIG_DRM_I915_DEBUG_GUC
117 void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
118                                enum intel_uc_fw_status status);
119 #else
120 static inline void intel_uc_fw_change_status(struct intel_uc_fw *uc_fw,
121                                              enum intel_uc_fw_status status)
122 {
123         uc_fw->__status = status;
124 }
125 #endif
126
127 static inline
128 const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status)
129 {
130         switch (status) {
131         case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
132                 return "N/A";
133         case INTEL_UC_FIRMWARE_UNINITIALIZED:
134                 return "UNINITIALIZED";
135         case INTEL_UC_FIRMWARE_DISABLED:
136                 return "DISABLED";
137         case INTEL_UC_FIRMWARE_SELECTED:
138                 return "SELECTED";
139         case INTEL_UC_FIRMWARE_MISSING:
140                 return "MISSING";
141         case INTEL_UC_FIRMWARE_ERROR:
142                 return "ERROR";
143         case INTEL_UC_FIRMWARE_AVAILABLE:
144                 return "AVAILABLE";
145         case INTEL_UC_FIRMWARE_INIT_FAIL:
146                 return "INIT FAIL";
147         case INTEL_UC_FIRMWARE_LOADABLE:
148                 return "LOADABLE";
149         case INTEL_UC_FIRMWARE_LOAD_FAIL:
150                 return "LOAD FAIL";
151         case INTEL_UC_FIRMWARE_TRANSFERRED:
152                 return "TRANSFERRED";
153         case INTEL_UC_FIRMWARE_RUNNING:
154                 return "RUNNING";
155         }
156         return "<invalid>";
157 }
158
159 static inline int intel_uc_fw_status_to_error(enum intel_uc_fw_status status)
160 {
161         switch (status) {
162         case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
163                 return -ENODEV;
164         case INTEL_UC_FIRMWARE_UNINITIALIZED:
165                 return -EACCES;
166         case INTEL_UC_FIRMWARE_DISABLED:
167                 return -EPERM;
168         case INTEL_UC_FIRMWARE_MISSING:
169                 return -ENOENT;
170         case INTEL_UC_FIRMWARE_ERROR:
171                 return -ENOEXEC;
172         case INTEL_UC_FIRMWARE_INIT_FAIL:
173         case INTEL_UC_FIRMWARE_LOAD_FAIL:
174                 return -EIO;
175         case INTEL_UC_FIRMWARE_SELECTED:
176                 return -ESTALE;
177         case INTEL_UC_FIRMWARE_AVAILABLE:
178         case INTEL_UC_FIRMWARE_LOADABLE:
179         case INTEL_UC_FIRMWARE_TRANSFERRED:
180         case INTEL_UC_FIRMWARE_RUNNING:
181                 return 0;
182         }
183         return -EINVAL;
184 }
185
186 static inline const char *intel_uc_fw_type_repr(enum intel_uc_fw_type type)
187 {
188         switch (type) {
189         case INTEL_UC_FW_TYPE_GUC:
190                 return "GuC";
191         case INTEL_UC_FW_TYPE_HUC:
192                 return "HuC";
193         }
194         return "uC";
195 }
196
197 static inline enum intel_uc_fw_status
198 __intel_uc_fw_status(struct intel_uc_fw *uc_fw)
199 {
200         /* shouldn't call this before checking hw/blob availability */
201         GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED);
202         return uc_fw->status;
203 }
204
205 static inline bool intel_uc_fw_is_supported(struct intel_uc_fw *uc_fw)
206 {
207         return __intel_uc_fw_status(uc_fw) != INTEL_UC_FIRMWARE_NOT_SUPPORTED;
208 }
209
210 static inline bool intel_uc_fw_is_enabled(struct intel_uc_fw *uc_fw)
211 {
212         return __intel_uc_fw_status(uc_fw) > INTEL_UC_FIRMWARE_DISABLED;
213 }
214
215 static inline bool intel_uc_fw_is_available(struct intel_uc_fw *uc_fw)
216 {
217         return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_AVAILABLE;
218 }
219
220 static inline bool intel_uc_fw_is_loadable(struct intel_uc_fw *uc_fw)
221 {
222         return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_LOADABLE;
223 }
224
225 static inline bool intel_uc_fw_is_loaded(struct intel_uc_fw *uc_fw)
226 {
227         return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_TRANSFERRED;
228 }
229
230 static inline bool intel_uc_fw_is_running(struct intel_uc_fw *uc_fw)
231 {
232         return __intel_uc_fw_status(uc_fw) == INTEL_UC_FIRMWARE_RUNNING;
233 }
234
235 static inline bool intel_uc_fw_is_overridden(const struct intel_uc_fw *uc_fw)
236 {
237         return uc_fw->user_overridden;
238 }
239
240 static inline void intel_uc_fw_sanitize(struct intel_uc_fw *uc_fw)
241 {
242         if (intel_uc_fw_is_loaded(uc_fw))
243                 intel_uc_fw_change_status(uc_fw, INTEL_UC_FIRMWARE_LOADABLE);
244 }
245
246 static inline u32 __intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
247 {
248         return sizeof(struct uc_css_header) + uc_fw->ucode_size;
249 }
250
251 /**
252  * intel_uc_fw_get_upload_size() - Get size of firmware needed to be uploaded.
253  * @uc_fw: uC firmware.
254  *
255  * Get the size of the firmware and header that will be uploaded to WOPCM.
256  *
257  * Return: Upload firmware size, or zero on firmware fetch failure.
258  */
259 static inline u32 intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
260 {
261         if (!intel_uc_fw_is_available(uc_fw))
262                 return 0;
263
264         return __intel_uc_fw_get_upload_size(uc_fw);
265 }
266
267 void intel_uc_fw_init_early(struct intel_uc_fw *uc_fw,
268                             enum intel_uc_fw_type type);
269 int intel_uc_fw_fetch(struct intel_uc_fw *uc_fw);
270 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw);
271 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, u32 offset, u32 dma_flags);
272 int intel_uc_fw_init(struct intel_uc_fw *uc_fw);
273 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw);
274 size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len);
275 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p);
276
277 #endif