blob: 8a06781c24960d569cd1242114466880a166b967 [file] [log] [blame]
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +00001/*
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
27#include "intel_uc_fw.h"
28#include "i915_drv.h"
29
30/**
31 * intel_uc_fw_fetch - fetch uC firmware
32 *
33 * @dev_priv: device private
34 * @uc_fw: uC firmware
35 *
36 * Fetch uC firmware into GEM obj.
37 */
38void intel_uc_fw_fetch(struct drm_i915_private *dev_priv,
39 struct intel_uc_fw *uc_fw)
40{
41 struct pci_dev *pdev = dev_priv->drm.pdev;
42 struct drm_i915_gem_object *obj;
43 const struct firmware *fw = NULL;
44 struct uc_css_header *css;
45 size_t size;
46 int err;
47
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +000048 DRM_DEBUG_DRIVER("%s fw fetch %s\n",
49 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path);
50
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +000051 if (!uc_fw->path)
52 return;
53
54 uc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +000055 DRM_DEBUG_DRIVER("%s fw fetch %s\n",
56 intel_uc_fw_type_repr(uc_fw->type),
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +000057 intel_uc_fw_status_repr(uc_fw->fetch_status));
58
59 err = request_firmware(&fw, uc_fw->path, &pdev->dev);
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +000060 if (err) {
61 DRM_DEBUG_DRIVER("%s fw request_firmware err=%d\n",
62 intel_uc_fw_type_repr(uc_fw->type), err);
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +000063 goto fail;
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +000064 }
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +000065
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +000066 DRM_DEBUG_DRIVER("%s fw size %zu ptr %p\n",
67 intel_uc_fw_type_repr(uc_fw->type), fw->size, fw);
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +000068
69 /* Check the size of the blob before examining buffer contents */
70 if (fw->size < sizeof(struct uc_css_header)) {
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +000071 DRM_WARN("%s: Unexpected firmware size (%zu, min %zu)\n",
72 intel_uc_fw_type_repr(uc_fw->type),
73 fw->size, sizeof(struct uc_css_header));
74 err = -ENODATA;
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +000075 goto fail;
76 }
77
78 css = (struct uc_css_header *)fw->data;
79
80 /* Firmware bits always start from header */
81 uc_fw->header_offset = 0;
82 uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
83 css->key_size_dw - css->exponent_size_dw) *
84 sizeof(u32);
85
86 if (uc_fw->header_size != sizeof(struct uc_css_header)) {
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +000087 DRM_WARN("%s: Mismatched firmware header definition\n",
88 intel_uc_fw_type_repr(uc_fw->type));
89 err = -ENOEXEC;
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +000090 goto fail;
91 }
92
93 /* then, uCode */
94 uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
95 uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
96
Michal Wajdeczkoafb34842017-10-16 14:47:16 +000097 /* Header and uCode will be loaded to WOPCM */
98 size = uc_fw->header_size + uc_fw->ucode_size;
99 if (size > intel_guc_wopcm_size(dev_priv)) {
100 DRM_WARN("%s: Firmware is too large to fit in WOPCM\n",
101 intel_uc_fw_type_repr(uc_fw->type));
102 err = -E2BIG;
103 goto fail;
104 }
105
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000106 /* now RSA */
107 if (css->key_size_dw != UOS_RSA_SCRATCH_MAX_COUNT) {
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +0000108 DRM_WARN("%s: Mismatched firmware RSA key size (%u)\n",
109 intel_uc_fw_type_repr(uc_fw->type), css->key_size_dw);
110 err = -ENOEXEC;
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000111 goto fail;
112 }
113 uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
114 uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
115
116 /* At least, it should have header, uCode and RSA. Size of all three. */
117 size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
118 if (fw->size < size) {
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +0000119 DRM_WARN("%s: Truncated firmware (%zu, expected %zu)\n",
120 intel_uc_fw_type_repr(uc_fw->type), fw->size, size);
121 err = -ENOEXEC;
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000122 goto fail;
123 }
124
125 /*
126 * The GuC firmware image has the version number embedded at a
127 * well-known offset within the firmware blob; note that major / minor
128 * version are TWO bytes each (i.e. u16), although all pointers and
129 * offsets are defined in terms of bytes (u8).
130 */
131 switch (uc_fw->type) {
132 case INTEL_UC_FW_TYPE_GUC:
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000133 uc_fw->major_ver_found = css->guc.sw_version >> 16;
134 uc_fw->minor_ver_found = css->guc.sw_version & 0xFFFF;
135 break;
136
137 case INTEL_UC_FW_TYPE_HUC:
138 uc_fw->major_ver_found = css->huc.sw_version >> 16;
139 uc_fw->minor_ver_found = css->huc.sw_version & 0xFFFF;
140 break;
141
142 default:
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +0000143 MISSING_CASE(uc_fw->type);
144 break;
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000145 }
146
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +0000147 DRM_DEBUG_DRIVER("%s fw version %u.%u (wanted %u.%u)\n",
148 intel_uc_fw_type_repr(uc_fw->type),
149 uc_fw->major_ver_found, uc_fw->minor_ver_found,
150 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
151
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000152 if (uc_fw->major_ver_wanted == 0 && uc_fw->minor_ver_wanted == 0) {
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +0000153 DRM_NOTE("%s: Skipping firmware version check\n",
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000154 intel_uc_fw_type_repr(uc_fw->type));
155 } else if (uc_fw->major_ver_found != uc_fw->major_ver_wanted ||
156 uc_fw->minor_ver_found < uc_fw->minor_ver_wanted) {
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +0000157 DRM_NOTE("%s: Wrong firmware version (%u.%u, required %u.%u)\n",
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000158 intel_uc_fw_type_repr(uc_fw->type),
159 uc_fw->major_ver_found, uc_fw->minor_ver_found,
160 uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
161 err = -ENOEXEC;
162 goto fail;
163 }
164
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000165 obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
166 if (IS_ERR(obj)) {
167 err = PTR_ERR(obj);
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +0000168 DRM_DEBUG_DRIVER("%s fw object_create err=%d\n",
169 intel_uc_fw_type_repr(uc_fw->type), err);
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000170 goto fail;
171 }
172
173 uc_fw->obj = obj;
174 uc_fw->size = fw->size;
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +0000175 uc_fw->fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
176 DRM_DEBUG_DRIVER("%s fw fetch %s\n",
177 intel_uc_fw_type_repr(uc_fw->type),
178 intel_uc_fw_status_repr(uc_fw->fetch_status));
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000179
180 release_firmware(fw);
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000181 return;
182
183fail:
Michal Wajdeczko5f99afd2017-10-16 14:47:18 +0000184 uc_fw->fetch_status = INTEL_UC_FIRMWARE_FAIL;
185 DRM_DEBUG_DRIVER("%s fw fetch %s\n",
186 intel_uc_fw_type_repr(uc_fw->type),
187 intel_uc_fw_status_repr(uc_fw->fetch_status));
188
189 DRM_WARN("%s: Failed to fetch firmware %s (error %d)\n",
190 intel_uc_fw_type_repr(uc_fw->type), uc_fw->path, err);
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000191
192 release_firmware(fw); /* OK even if fw is NULL */
Michal Wajdeczkoa16b4312017-10-04 15:33:25 +0000193}
194
195/**
196 * intel_uc_fw_fini - cleanup uC firmware
197 *
198 * @uc_fw: uC firmware
199 *
200 * Cleans up uC firmware by releasing the firmware GEM obj.
201 */
202void intel_uc_fw_fini(struct intel_uc_fw *uc_fw)
203{
204 struct drm_i915_gem_object *obj;
205
206 obj = fetch_and_zero(&uc_fw->obj);
207 if (obj)
208 i915_gem_object_put(obj);
209
210 uc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
211}