1 /*
2  * Copyright © 2014-2017 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #ifndef _INTEL_UC_FW_H_
26 #define _INTEL_UC_FW_H_
27 
28 #include <linux/types.h>
29 #include "intel_uc_fw_abi.h"
30 #include "i915_gem.h"
31 
32 struct drm_printer;
33 struct drm_i915_private;
34 struct intel_gt;
35 
36 /* Home of GuC, HuC and DMC firmwares */
37 #define INTEL_UC_FIRMWARE_URL "https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/tree/i915"
38 
39 enum intel_uc_fw_status {
40 	INTEL_UC_FIRMWARE_FAIL = -3, /* failed to xfer or init/auth the fw */
41 	INTEL_UC_FIRMWARE_MISSING = -2, /* blob not found on the system */
42 	INTEL_UC_FIRMWARE_NOT_SUPPORTED = -1, /* no uc HW */
43 	INTEL_UC_FIRMWARE_UNINITIALIZED = 0, /* used to catch checks done too early */
44 	INTEL_UC_FIRMWARE_SELECTED, /* selected the blob we want to load */
45 	INTEL_UC_FIRMWARE_AVAILABLE, /* blob found and copied in mem */
46 	INTEL_UC_FIRMWARE_TRANSFERRED, /* dma xfer done */
47 	INTEL_UC_FIRMWARE_RUNNING /* init/auth done */
48 };
49 
50 enum intel_uc_fw_type {
51 	INTEL_UC_FW_TYPE_GUC = 0,
52 	INTEL_UC_FW_TYPE_HUC
53 };
54 #define INTEL_UC_FW_NUM_TYPES 2
55 
56 /*
57  * This structure encapsulates all the data needed during the process
58  * of fetching, caching, and loading the firmware image into the uC.
59  */
60 struct intel_uc_fw {
61 	enum intel_uc_fw_type type;
62 	enum intel_uc_fw_status status;
63 	const char *path;
64 	bool user_overridden;
65 	size_t size;
66 	struct drm_i915_gem_object *obj;
67 
68 	/*
69 	 * The firmware build process will generate a version header file with major and
70 	 * minor version defined. The versions are built into CSS header of firmware.
71 	 * i915 kernel driver set the minimal firmware version required per platform.
72 	 */
73 	u16 major_ver_wanted;
74 	u16 minor_ver_wanted;
75 	u16 major_ver_found;
76 	u16 minor_ver_found;
77 
78 	u32 rsa_size;
79 	u32 ucode_size;
80 };
81 
82 static inline
83 const char *intel_uc_fw_status_repr(enum intel_uc_fw_status status)
84 {
85 	switch (status) {
86 	case INTEL_UC_FIRMWARE_FAIL:
87 		return "FAIL";
88 	case INTEL_UC_FIRMWARE_MISSING:
89 		return "MISSING";
90 	case INTEL_UC_FIRMWARE_NOT_SUPPORTED:
91 		return "N/A";
92 	case INTEL_UC_FIRMWARE_UNINITIALIZED:
93 		return "UNINITIALIZED";
94 	case INTEL_UC_FIRMWARE_SELECTED:
95 		return "SELECTED";
96 	case INTEL_UC_FIRMWARE_AVAILABLE:
97 		return "AVAILABLE";
98 	case INTEL_UC_FIRMWARE_TRANSFERRED:
99 		return "TRANSFERRED";
100 	case INTEL_UC_FIRMWARE_RUNNING:
101 		return "RUNNING";
102 	}
103 	return "<invalid>";
104 }
105 
106 static inline const char *intel_uc_fw_type_repr(enum intel_uc_fw_type type)
107 {
108 	switch (type) {
109 	case INTEL_UC_FW_TYPE_GUC:
110 		return "GuC";
111 	case INTEL_UC_FW_TYPE_HUC:
112 		return "HuC";
113 	}
114 	return "uC";
115 }
116 
117 static inline enum intel_uc_fw_status
118 __intel_uc_fw_status(struct intel_uc_fw *uc_fw)
119 {
120 	/* shouldn't call this before checking hw/blob availability */
121 	GEM_BUG_ON(uc_fw->status == INTEL_UC_FIRMWARE_UNINITIALIZED);
122 	return uc_fw->status;
123 }
124 
125 static inline bool intel_uc_fw_is_available(struct intel_uc_fw *uc_fw)
126 {
127 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_AVAILABLE;
128 }
129 
130 static inline bool intel_uc_fw_is_loaded(struct intel_uc_fw *uc_fw)
131 {
132 	return __intel_uc_fw_status(uc_fw) >= INTEL_UC_FIRMWARE_TRANSFERRED;
133 }
134 
135 static inline bool intel_uc_fw_is_running(struct intel_uc_fw *uc_fw)
136 {
137 	return __intel_uc_fw_status(uc_fw) == INTEL_UC_FIRMWARE_RUNNING;
138 }
139 
140 static inline bool intel_uc_fw_supported(struct intel_uc_fw *uc_fw)
141 {
142 	return __intel_uc_fw_status(uc_fw) != INTEL_UC_FIRMWARE_NOT_SUPPORTED;
143 }
144 
145 static inline bool intel_uc_fw_is_overridden(const struct intel_uc_fw *uc_fw)
146 {
147 	return uc_fw->user_overridden;
148 }
149 
150 static inline void intel_uc_fw_sanitize(struct intel_uc_fw *uc_fw)
151 {
152 	if (intel_uc_fw_is_loaded(uc_fw))
153 		uc_fw->status = INTEL_UC_FIRMWARE_AVAILABLE;
154 }
155 
156 /**
157  * intel_uc_fw_get_upload_size() - Get size of firmware needed to be uploaded.
158  * @uc_fw: uC firmware.
159  *
160  * Get the size of the firmware and header that will be uploaded to WOPCM.
161  *
162  * Return: Upload firmware size, or zero on firmware fetch failure.
163  */
164 static inline u32 intel_uc_fw_get_upload_size(struct intel_uc_fw *uc_fw)
165 {
166 	if (!intel_uc_fw_is_available(uc_fw))
167 		return 0;
168 
169 	return sizeof(struct uc_css_header) + uc_fw->ucode_size;
170 }
171 
172 void intel_uc_fw_init_early(struct intel_uc_fw *uc_fw,
173 			    enum intel_uc_fw_type type,
174 			    struct drm_i915_private *i915);
175 void intel_uc_fw_fetch(struct intel_uc_fw *uc_fw,
176 		       struct drm_i915_private *i915);
177 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw);
178 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw, struct intel_gt *gt,
179 		       u32 wopcm_offset, u32 dma_flags);
180 int intel_uc_fw_init(struct intel_uc_fw *uc_fw);
181 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw);
182 size_t intel_uc_fw_copy_rsa(struct intel_uc_fw *uc_fw, void *dst, u32 max_len);
183 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p);
184 
185 #endif
186