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