]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - drivers/gpu/drm/i915/intel_uc_fw.c
Merge branch 'drm-next-5.1' of git://people.freedesktop.org/~agd5f/linux into drm...
[thirdparty/kernel/stable.git] / drivers / gpu / drm / i915 / intel_uc_fw.c
1 /*
2 * Copyright © 2016-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 #include <linux/firmware.h>
26 #include <drm/drm_print.h>
27
28 #include "intel_uc_fw.h"
29 #include "i915_drv.h"
30
31 /**
32 * intel_uc_fw_fetch - fetch uC firmware
33 *
34 * @dev_priv: device private
35 * @uc_fw: uC firmware
36 *
37 * Fetch uC firmware into GEM obj.
38 */
39 void intel_uc_fw_fetch(struct drm_i915_private *dev_priv,
40 struct intel_uc_fw *uc_fw)
41 {
42 struct pci_dev *pdev = dev_priv->drm.pdev;
43 struct drm_i915_gem_object *obj;
44 const struct firmware *fw = NULL;
45 struct uc_css_header *css;
46 size_t size;
47 int err;
48
49 if (!uc_fw->path) {
50 dev_info(dev_priv->drm.dev,
51 "%s: No firmware was defined for %s!\n",
52 intel_uc_fw_type_repr(uc_fw->type),
53 intel_platform_name(INTEL_INFO(dev_priv)->platform));
54 return;
55 }
56
57 DRM_DEBUG_DRIVER("%s fw fetch %s\n",
58 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path);
59
60 uc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
61 DRM_DEBUG_DRIVER("%s fw fetch %s\n",
62 intel_uc_fw_type_repr(uc_fw->type),
63 intel_uc_fw_status_repr(uc_fw->fetch_status));
64
65 err = request_firmware(&fw, uc_fw->path, &pdev->dev);
66 if (err) {
67 DRM_DEBUG_DRIVER("%s fw request_firmware err=%d\n",
68 intel_uc_fw_type_repr(uc_fw->type), err);
69 goto fail;
70 }
71
72 DRM_DEBUG_DRIVER("%s fw size %zu ptr %p\n",
73 intel_uc_fw_type_repr(uc_fw->type), fw->size, fw);
74
75 /* Check the size of the blob before examining buffer contents */
76 if (fw->size < sizeof(struct uc_css_header)) {
77 DRM_WARN("%s: Unexpected firmware size (%zu, min %zu)\n",
78 intel_uc_fw_type_repr(uc_fw->type),
79 fw->size, sizeof(struct uc_css_header));
80 err = -ENODATA;
81 goto fail;
82 }
83
84 css = (struct uc_css_header *)fw->data;
85
86 /* Firmware bits always start from header */
87 uc_fw->header_offset = 0;
88 uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
89 css->key_size_dw - css->exponent_size_dw) *
90 sizeof(u32);
91
92 if (uc_fw->header_size != sizeof(struct uc_css_header)) {
93 DRM_WARN("%s: Mismatched firmware header definition\n",
94 intel_uc_fw_type_repr(uc_fw->type));
95 err = -ENOEXEC;
96 goto fail;
97 }
98
99 /* then, uCode */
100 uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
101 uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
102
103 /* now RSA */
104 if (css->key_size_dw != UOS_RSA_SCRATCH_COUNT) {
105 DRM_WARN("%s: Mismatched firmware RSA key size (%u)\n",
106 intel_uc_fw_type_repr(uc_fw->type), css->key_size_dw);
107 err = -ENOEXEC;
108 goto fail;
109 }
110 uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
111 uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
112
113 /* At least, it should have header, uCode and RSA. Size of all three. */
114 size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
115 if (fw->size < size) {
116 DRM_WARN("%s: Truncated firmware (%zu, expected %zu)\n",
117 intel_uc_fw_type_repr(uc_fw->type), fw->size, size);
118 err = -ENOEXEC;
119 goto fail;
120 }
121
122 /*
123 * The GuC firmware image has the version number embedded at a
124 * well-known offset within the firmware blob; note that major / minor
125 * version are TWO bytes each (i.e. u16), although all pointers and
126 * offsets are defined in terms of bytes (u8).
127 */
128 switch (uc_fw->type) {
129 case INTEL_UC_FW_TYPE_GUC:
130 uc_fw->major_ver_found = css->guc.sw_version >> 16;
131 uc_fw->minor_ver_found = css->guc.sw_version & 0xFFFF;
132 break;
133
134 case INTEL_UC_FW_TYPE_HUC:
135 uc_fw->major_ver_found = css->huc.sw_version >> 16;
136 uc_fw->minor_ver_found = css->huc.sw_version & 0xFFFF;
137 break;
138
139 default:
140 MISSING_CASE(uc_fw->type);
141 break;
142 }
143
144 DRM_DEBUG_DRIVER("%s fw version %u.%u (wanted %u.%u)\n",
145 intel_uc_fw_type_repr(uc_fw->type),
146 uc_fw->major_ver_found, uc_fw->minor_ver_found,
147 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
148
149 if (uc_fw->major_ver_wanted == 0 && uc_fw->minor_ver_wanted == 0) {
150 DRM_NOTE("%s: Skipping firmware version check\n",
151 intel_uc_fw_type_repr(uc_fw->type));
152 } else if (uc_fw->major_ver_found != uc_fw->major_ver_wanted ||
153 uc_fw->minor_ver_found < uc_fw->minor_ver_wanted) {
154 DRM_NOTE("%s: Wrong firmware version (%u.%u, required %u.%u)\n",
155 intel_uc_fw_type_repr(uc_fw->type),
156 uc_fw->major_ver_found, uc_fw->minor_ver_found,
157 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
158 err = -ENOEXEC;
159 goto fail;
160 }
161
162 obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
163 if (IS_ERR(obj)) {
164 err = PTR_ERR(obj);
165 DRM_DEBUG_DRIVER("%s fw object_create err=%d\n",
166 intel_uc_fw_type_repr(uc_fw->type), err);
167 goto fail;
168 }
169
170 uc_fw->obj = obj;
171 uc_fw->size = fw->size;
172 uc_fw->fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
173 DRM_DEBUG_DRIVER("%s fw fetch %s\n",
174 intel_uc_fw_type_repr(uc_fw->type),
175 intel_uc_fw_status_repr(uc_fw->fetch_status));
176
177 release_firmware(fw);
178 return;
179
180 fail:
181 uc_fw->fetch_status = INTEL_UC_FIRMWARE_FAIL;
182 DRM_DEBUG_DRIVER("%s fw fetch %s\n",
183 intel_uc_fw_type_repr(uc_fw->type),
184 intel_uc_fw_status_repr(uc_fw->fetch_status));
185
186 DRM_WARN("%s: Failed to fetch firmware %s (error %d)\n",
187 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path, err);
188 DRM_INFO("%s: Firmware can be downloaded from %s\n",
189 intel_uc_fw_type_repr(uc_fw->type), INTEL_UC_FIRMWARE_URL);
190
191 release_firmware(fw); /* OK even if fw is NULL */
192 }
193
194 /**
195 * intel_uc_fw_upload - load uC firmware using custom loader
196 * @uc_fw: uC firmware
197 * @xfer: custom uC firmware loader function
198 *
199 * Loads uC firmware using custom loader and updates internal flags.
200 *
201 * Return: 0 on success, non-zero on failure.
202 */
203 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw,
204 int (*xfer)(struct intel_uc_fw *uc_fw,
205 struct i915_vma *vma))
206 {
207 struct i915_vma *vma;
208 u32 ggtt_pin_bias;
209 int err;
210
211 DRM_DEBUG_DRIVER("%s fw load %s\n",
212 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path);
213
214 if (uc_fw->fetch_status != INTEL_UC_FIRMWARE_SUCCESS)
215 return -ENOEXEC;
216
217 uc_fw->load_status = INTEL_UC_FIRMWARE_PENDING;
218 DRM_DEBUG_DRIVER("%s fw load %s\n",
219 intel_uc_fw_type_repr(uc_fw->type),
220 intel_uc_fw_status_repr(uc_fw->load_status));
221
222 /* Pin object with firmware */
223 err = i915_gem_object_set_to_gtt_domain(uc_fw->obj, false);
224 if (err) {
225 DRM_DEBUG_DRIVER("%s fw set-domain err=%d\n",
226 intel_uc_fw_type_repr(uc_fw->type), err);
227 goto fail;
228 }
229
230 ggtt_pin_bias = to_i915(uc_fw->obj->base.dev)->ggtt.pin_bias;
231 vma = i915_gem_object_ggtt_pin(uc_fw->obj, NULL, 0, 0,
232 PIN_OFFSET_BIAS | ggtt_pin_bias);
233 if (IS_ERR(vma)) {
234 err = PTR_ERR(vma);
235 DRM_DEBUG_DRIVER("%s fw ggtt-pin err=%d\n",
236 intel_uc_fw_type_repr(uc_fw->type), err);
237 goto fail;
238 }
239
240 /* Call custom loader */
241 err = xfer(uc_fw, vma);
242
243 /*
244 * We keep the object pages for reuse during resume. But we can unpin it
245 * now that DMA has completed, so it doesn't continue to take up space.
246 */
247 i915_vma_unpin(vma);
248
249 if (err)
250 goto fail;
251
252 uc_fw->load_status = INTEL_UC_FIRMWARE_SUCCESS;
253 DRM_DEBUG_DRIVER("%s fw load %s\n",
254 intel_uc_fw_type_repr(uc_fw->type),
255 intel_uc_fw_status_repr(uc_fw->load_status));
256
257 DRM_INFO("%s: Loaded firmware %s (version %u.%u)\n",
258 intel_uc_fw_type_repr(uc_fw->type),
259 uc_fw->path,
260 uc_fw->major_ver_found, uc_fw->minor_ver_found);
261
262 return 0;
263
264 fail:
265 uc_fw->load_status = INTEL_UC_FIRMWARE_FAIL;
266 DRM_DEBUG_DRIVER("%s fw load %s\n",
267 intel_uc_fw_type_repr(uc_fw->type),
268 intel_uc_fw_status_repr(uc_fw->load_status));
269
270 DRM_WARN("%s: Failed to load firmware %s (error %d)\n",
271 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path, err);
272
273 return err;
274 }
275
276 /**
277 * intel_uc_fw_fini - cleanup uC firmware
278 *
279 * @uc_fw: uC firmware
280 *
281 * Cleans up uC firmware by releasing the firmware GEM obj.
282 */
283 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw)
284 {
285 struct drm_i915_gem_object *obj;
286
287 obj = fetch_and_zero(&uc_fw->obj);
288 if (obj)
289 i915_gem_object_put(obj);
290
291 uc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
292 }
293
294 /**
295 * intel_uc_fw_dump - dump information about uC firmware
296 * @uc_fw: uC firmware
297 * @p: the &drm_printer
298 *
299 * Pretty printer for uC firmware.
300 */
301 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p)
302 {
303 drm_printf(p, "%s firmware: %s\n",
304 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path);
305 drm_printf(p, "\tstatus: fetch %s, load %s\n",
306 intel_uc_fw_status_repr(uc_fw->fetch_status),
307 intel_uc_fw_status_repr(uc_fw->load_status));
308 drm_printf(p, "\tversion: wanted %u.%u, found %u.%u\n",
309 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted,
310 uc_fw->major_ver_found, uc_fw->minor_ver_found);
311 drm_printf(p, "\theader: offset %u, size %u\n",
312 uc_fw->header_offset, uc_fw->header_size);
313 drm_printf(p, "\tuCode: offset %u, size %u\n",
314 uc_fw->ucode_offset, uc_fw->ucode_size);
315 drm_printf(p, "\tRSA: offset %u, size %u\n",
316 uc_fw->rsa_offset, uc_fw->rsa_size);
317 }