2 * Copyright © 2016-2017 Intel Corporation
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:
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
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
25 #include <linux/bitfield.h>
26 #include <linux/firmware.h>
27 #include <drm/drm_print.h>
29 #include "intel_uc_fw.h"
33 * intel_uc_fw_fetch - fetch uC firmware
35 * @dev_priv: device private
38 * Fetch uC firmware into GEM obj.
40 void intel_uc_fw_fetch(struct drm_i915_private *dev_priv,
41 struct intel_uc_fw *uc_fw)
43 struct pci_dev *pdev = dev_priv->drm.pdev;
44 struct drm_i915_gem_object *obj;
45 const struct firmware *fw = NULL;
46 struct uc_css_header *css;
51 dev_info(dev_priv->drm.dev,
52 "%s: No firmware was defined for %s!\n",
53 intel_uc_fw_type_repr(uc_fw->type),
54 intel_platform_name(INTEL_INFO(dev_priv)->platform));
58 DRM_DEBUG_DRIVER("%s fw fetch %s\n",
59 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path);
61 uc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
62 DRM_DEBUG_DRIVER("%s fw fetch %s\n",
63 intel_uc_fw_type_repr(uc_fw->type),
64 intel_uc_fw_status_repr(uc_fw->fetch_status));
66 err = request_firmware(&fw, uc_fw->path, &pdev->dev);
68 DRM_DEBUG_DRIVER("%s fw request_firmware err=%d\n",
69 intel_uc_fw_type_repr(uc_fw->type), err);
73 DRM_DEBUG_DRIVER("%s fw size %zu ptr %p\n",
74 intel_uc_fw_type_repr(uc_fw->type), fw->size, fw);
76 /* Check the size of the blob before examining buffer contents */
77 if (fw->size < sizeof(struct uc_css_header)) {
78 DRM_WARN("%s: Unexpected firmware size (%zu, min %zu)\n",
79 intel_uc_fw_type_repr(uc_fw->type),
80 fw->size, sizeof(struct uc_css_header));
85 css = (struct uc_css_header *)fw->data;
87 /* Firmware bits always start from header */
88 uc_fw->header_offset = 0;
89 uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
90 css->key_size_dw - css->exponent_size_dw) *
93 if (uc_fw->header_size != sizeof(struct uc_css_header)) {
94 DRM_WARN("%s: Mismatched firmware header definition\n",
95 intel_uc_fw_type_repr(uc_fw->type));
101 uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
102 uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
105 if (css->key_size_dw != UOS_RSA_SCRATCH_COUNT) {
106 DRM_WARN("%s: Mismatched firmware RSA key size (%u)\n",
107 intel_uc_fw_type_repr(uc_fw->type), css->key_size_dw);
111 uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
112 uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
114 /* At least, it should have header, uCode and RSA. Size of all three. */
115 size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
116 if (fw->size < size) {
117 DRM_WARN("%s: Truncated firmware (%zu, expected %zu)\n",
118 intel_uc_fw_type_repr(uc_fw->type), fw->size, size);
123 /* Get version numbers from the CSS header */
124 switch (uc_fw->type) {
125 case INTEL_UC_FW_TYPE_GUC:
126 uc_fw->major_ver_found = FIELD_GET(CSS_SW_VERSION_GUC_MAJOR,
128 uc_fw->minor_ver_found = FIELD_GET(CSS_SW_VERSION_GUC_MINOR,
132 case INTEL_UC_FW_TYPE_HUC:
133 uc_fw->major_ver_found = FIELD_GET(CSS_SW_VERSION_HUC_MAJOR,
135 uc_fw->minor_ver_found = FIELD_GET(CSS_SW_VERSION_HUC_MINOR,
140 MISSING_CASE(uc_fw->type);
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);
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);
162 obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
165 DRM_DEBUG_DRIVER("%s fw object_create err=%d\n",
166 intel_uc_fw_type_repr(uc_fw->type), err);
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));
177 release_firmware(fw);
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));
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);
191 release_firmware(fw); /* OK even if fw is NULL */
194 static void intel_uc_fw_ggtt_bind(struct intel_uc_fw *uc_fw)
196 struct drm_i915_gem_object *obj = uc_fw->obj;
197 struct i915_ggtt *ggtt = &to_i915(obj->base.dev)->ggtt;
198 struct i915_vma dummy = {
199 .node.start = intel_uc_fw_ggtt_offset(uc_fw),
200 .node.size = obj->base.size,
201 .pages = obj->mm.pages,
205 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
206 GEM_BUG_ON(dummy.node.size > ggtt->uc_fw.size);
208 /* uc_fw->obj cache domains were not controlled across suspend */
209 drm_clflush_sg(dummy.pages);
211 ggtt->vm.insert_entries(&ggtt->vm, &dummy, I915_CACHE_NONE, 0);
214 static void intel_uc_fw_ggtt_unbind(struct intel_uc_fw *uc_fw)
216 struct drm_i915_gem_object *obj = uc_fw->obj;
217 struct i915_ggtt *ggtt = &to_i915(obj->base.dev)->ggtt;
218 u64 start = intel_uc_fw_ggtt_offset(uc_fw);
220 ggtt->vm.clear_range(&ggtt->vm, start, obj->base.size);
224 * intel_uc_fw_upload - load uC firmware using custom loader
225 * @uc_fw: uC firmware
226 * @xfer: custom uC firmware loader function
228 * Loads uC firmware using custom loader and updates internal flags.
230 * Return: 0 on success, non-zero on failure.
232 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw,
233 int (*xfer)(struct intel_uc_fw *uc_fw))
237 DRM_DEBUG_DRIVER("%s fw load %s\n",
238 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path);
240 if (uc_fw->fetch_status != INTEL_UC_FIRMWARE_SUCCESS)
243 uc_fw->load_status = INTEL_UC_FIRMWARE_PENDING;
244 DRM_DEBUG_DRIVER("%s fw load %s\n",
245 intel_uc_fw_type_repr(uc_fw->type),
246 intel_uc_fw_status_repr(uc_fw->load_status));
248 intel_uc_fw_ggtt_bind(uc_fw);
250 /* Call custom loader */
255 intel_uc_fw_ggtt_unbind(uc_fw);
257 uc_fw->load_status = INTEL_UC_FIRMWARE_SUCCESS;
258 DRM_DEBUG_DRIVER("%s fw load %s\n",
259 intel_uc_fw_type_repr(uc_fw->type),
260 intel_uc_fw_status_repr(uc_fw->load_status));
262 DRM_INFO("%s: Loaded firmware %s (version %u.%u)\n",
263 intel_uc_fw_type_repr(uc_fw->type),
265 uc_fw->major_ver_found, uc_fw->minor_ver_found);
270 uc_fw->load_status = INTEL_UC_FIRMWARE_FAIL;
271 DRM_DEBUG_DRIVER("%s fw load %s\n",
272 intel_uc_fw_type_repr(uc_fw->type),
273 intel_uc_fw_status_repr(uc_fw->load_status));
275 DRM_WARN("%s: Failed to load firmware %s (error %d)\n",
276 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path, err);
281 int intel_uc_fw_init(struct intel_uc_fw *uc_fw)
285 if (uc_fw->fetch_status != INTEL_UC_FIRMWARE_SUCCESS)
288 err = i915_gem_object_pin_pages(uc_fw->obj);
290 DRM_DEBUG_DRIVER("%s fw pin-pages err=%d\n",
291 intel_uc_fw_type_repr(uc_fw->type), err);
296 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw)
298 if (uc_fw->fetch_status != INTEL_UC_FIRMWARE_SUCCESS)
301 i915_gem_object_unpin_pages(uc_fw->obj);
304 u32 intel_uc_fw_ggtt_offset(struct intel_uc_fw *uc_fw)
306 struct drm_i915_private *i915 = to_i915(uc_fw->obj->base.dev);
307 struct i915_ggtt *ggtt = &i915->ggtt;
308 struct drm_mm_node *node = &ggtt->uc_fw;
310 GEM_BUG_ON(!node->allocated);
311 GEM_BUG_ON(upper_32_bits(node->start));
312 GEM_BUG_ON(upper_32_bits(node->start + node->size - 1));
314 return lower_32_bits(node->start);
318 * intel_uc_fw_cleanup_fetch - cleanup uC firmware
320 * @uc_fw: uC firmware
322 * Cleans up uC firmware by releasing the firmware GEM obj.
324 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw)
326 struct drm_i915_gem_object *obj;
328 obj = fetch_and_zero(&uc_fw->obj);
330 i915_gem_object_put(obj);
332 uc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
336 * intel_uc_fw_dump - dump information about uC firmware
337 * @uc_fw: uC firmware
338 * @p: the &drm_printer
340 * Pretty printer for uC firmware.
342 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p)
344 drm_printf(p, "%s firmware: %s\n",
345 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path);
346 drm_printf(p, "\tstatus: fetch %s, load %s\n",
347 intel_uc_fw_status_repr(uc_fw->fetch_status),
348 intel_uc_fw_status_repr(uc_fw->load_status));
349 drm_printf(p, "\tversion: wanted %u.%u, found %u.%u\n",
350 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted,
351 uc_fw->major_ver_found, uc_fw->minor_ver_found);
352 drm_printf(p, "\theader: offset %u, size %u\n",
353 uc_fw->header_offset, uc_fw->header_size);
354 drm_printf(p, "\tuCode: offset %u, size %u\n",
355 uc_fw->ucode_offset, uc_fw->ucode_size);
356 drm_printf(p, "\tRSA: offset %u, size %u\n",
357 uc_fw->rsa_offset, uc_fw->rsa_size);