Anusha Srivatsa | bd132858 | 2017-01-18 08:05:53 -0800 | [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 | #include <linux/firmware.h> |
| 25 | #include "i915_drv.h" |
| 26 | #include "intel_uc.h" |
| 27 | |
| 28 | /** |
| 29 | * DOC: HuC Firmware |
| 30 | * |
| 31 | * Motivation: |
| 32 | * GEN9 introduces a new dedicated firmware for usage in media HEVC (High |
| 33 | * Efficiency Video Coding) operations. Userspace can use the firmware |
| 34 | * capabilities by adding HuC specific commands to batch buffers. |
| 35 | * |
| 36 | * Implementation: |
| 37 | * The same firmware loader is used as the GuC. However, the actual |
| 38 | * loading to HW is deferred until GEM initialization is done. |
| 39 | * |
| 40 | * Note that HuC firmware loading must be done before GuC loading. |
| 41 | */ |
| 42 | |
| 43 | #define SKL_HUC_FW_MAJOR 01 |
| 44 | #define SKL_HUC_FW_MINOR 07 |
| 45 | #define SKL_BLD_NUM 1398 |
| 46 | |
| 47 | #define HUC_FW_PATH(platform, major, minor, bld_num) \ |
| 48 | "i915/" __stringify(platform) "_huc_ver" __stringify(major) "_" \ |
| 49 | __stringify(minor) "_" __stringify(bld_num) ".bin" |
| 50 | |
| 51 | #define I915_SKL_HUC_UCODE HUC_FW_PATH(skl, SKL_HUC_FW_MAJOR, \ |
| 52 | SKL_HUC_FW_MINOR, SKL_BLD_NUM) |
| 53 | MODULE_FIRMWARE(I915_SKL_HUC_UCODE); |
| 54 | |
| 55 | /** |
| 56 | * huc_ucode_xfer() - DMA's the firmware |
| 57 | * @dev_priv: the drm_i915_private device |
| 58 | * |
| 59 | * Transfer the firmware image to RAM for execution by the microcontroller. |
| 60 | * |
| 61 | * Return: 0 on success, non-zero on failure |
| 62 | */ |
| 63 | static int huc_ucode_xfer(struct drm_i915_private *dev_priv) |
| 64 | { |
| 65 | struct intel_uc_fw *huc_fw = &dev_priv->huc.fw; |
| 66 | struct i915_vma *vma; |
| 67 | unsigned long offset = 0; |
| 68 | u32 size; |
| 69 | int ret; |
| 70 | |
| 71 | ret = i915_gem_object_set_to_gtt_domain(huc_fw->obj, false); |
| 72 | if (ret) { |
| 73 | DRM_DEBUG_DRIVER("set-domain failed %d\n", ret); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | vma = i915_gem_object_ggtt_pin(huc_fw->obj, NULL, 0, 0, |
| 78 | PIN_OFFSET_BIAS | GUC_WOPCM_TOP); |
| 79 | if (IS_ERR(vma)) { |
| 80 | DRM_DEBUG_DRIVER("pin failed %d\n", (int)PTR_ERR(vma)); |
| 81 | return PTR_ERR(vma); |
| 82 | } |
| 83 | |
| 84 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 85 | |
| 86 | /* init WOPCM */ |
| 87 | I915_WRITE(GUC_WOPCM_SIZE, intel_guc_wopcm_size(dev_priv)); |
| 88 | I915_WRITE(DMA_GUC_WOPCM_OFFSET, GUC_WOPCM_OFFSET_VALUE | |
| 89 | HUC_LOADING_AGENT_GUC); |
| 90 | |
| 91 | /* Set the source address for the uCode */ |
| 92 | offset = guc_ggtt_offset(vma) + huc_fw->header_offset; |
| 93 | I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset)); |
| 94 | I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF); |
| 95 | |
| 96 | /* Hardware doesn't look at destination address for HuC. Set it to 0, |
| 97 | * but still program the correct address space. |
| 98 | */ |
| 99 | I915_WRITE(DMA_ADDR_1_LOW, 0); |
| 100 | I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM); |
| 101 | |
| 102 | size = huc_fw->header_size + huc_fw->ucode_size; |
| 103 | I915_WRITE(DMA_COPY_SIZE, size); |
| 104 | |
| 105 | /* Start the DMA */ |
| 106 | I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(HUC_UKERNEL | START_DMA)); |
| 107 | |
| 108 | /* Wait for DMA to finish */ |
| 109 | ret = wait_for((I915_READ(DMA_CTRL) & START_DMA) == 0, 100); |
| 110 | |
| 111 | DRM_DEBUG_DRIVER("HuC DMA transfer wait over with ret %d\n", ret); |
| 112 | |
| 113 | /* Disable the bits once DMA is over */ |
| 114 | I915_WRITE(DMA_CTRL, _MASKED_BIT_DISABLE(HUC_UKERNEL)); |
| 115 | |
| 116 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 117 | |
| 118 | /* |
| 119 | * We keep the object pages for reuse during resume. But we can unpin it |
| 120 | * now that DMA has completed, so it doesn't continue to take up space. |
| 121 | */ |
| 122 | i915_vma_unpin(vma); |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * intel_huc_init() - initiate HuC firmware loading request |
| 129 | * @dev_priv: the drm_i915_private device |
| 130 | * |
| 131 | * Called early during driver load, but after GEM is initialised. The loading |
| 132 | * will continue only when driver explicitly specify firmware name and version. |
| 133 | * All other cases are considered as INTEL_UC_FIRMWARE_NONE either because HW |
| 134 | * is not capable or driver yet support it. And there will be no error message |
| 135 | * for INTEL_UC_FIRMWARE_NONE cases. |
| 136 | * |
| 137 | * The DMA-copying to HW is done later when intel_huc_load() is called. |
| 138 | */ |
| 139 | void intel_huc_init(struct drm_i915_private *dev_priv) |
| 140 | { |
| 141 | struct intel_huc *huc = &dev_priv->huc; |
| 142 | struct intel_uc_fw *huc_fw = &huc->fw; |
| 143 | const char *fw_path = NULL; |
| 144 | |
| 145 | huc_fw->path = NULL; |
| 146 | huc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE; |
| 147 | huc_fw->load_status = INTEL_UC_FIRMWARE_NONE; |
| 148 | huc_fw->fw = INTEL_UC_FW_TYPE_HUC; |
| 149 | |
| 150 | if (!HAS_HUC_UCODE(dev_priv)) |
| 151 | return; |
| 152 | |
| 153 | if (IS_SKYLAKE(dev_priv)) { |
| 154 | fw_path = I915_SKL_HUC_UCODE; |
| 155 | huc_fw->major_ver_wanted = SKL_HUC_FW_MAJOR; |
| 156 | huc_fw->minor_ver_wanted = SKL_HUC_FW_MINOR; |
| 157 | } |
| 158 | |
| 159 | huc_fw->path = fw_path; |
| 160 | huc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING; |
| 161 | |
| 162 | DRM_DEBUG_DRIVER("HuC firmware pending, path %s\n", fw_path); |
| 163 | |
| 164 | WARN(huc_fw->path == NULL, "HuC present but no fw path\n"); |
| 165 | |
| 166 | intel_uc_fw_fetch(dev_priv, huc_fw); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * intel_huc_load() - load HuC uCode to device |
| 171 | * @dev_priv: the drm_i915_private device |
| 172 | * |
| 173 | * Called from guc_setup() during driver loading and also after a GPU reset. |
| 174 | * Be note that HuC loading must be done before GuC loading. |
| 175 | * |
| 176 | * The firmware image should have already been fetched into memory by the |
| 177 | * earlier call to intel_huc_init(), so here we need only check that |
| 178 | * is succeeded, and then transfer the image to the h/w. |
| 179 | * |
| 180 | * Return: non-zero code on error |
| 181 | */ |
| 182 | int intel_huc_load(struct drm_i915_private *dev_priv) |
| 183 | { |
| 184 | struct intel_uc_fw *huc_fw = &dev_priv->huc.fw; |
| 185 | int err; |
| 186 | |
| 187 | if (huc_fw->fetch_status == INTEL_UC_FIRMWARE_NONE) |
| 188 | return 0; |
| 189 | |
| 190 | DRM_DEBUG_DRIVER("%s fw status: fetch %s, load %s\n", |
| 191 | huc_fw->path, |
| 192 | intel_uc_fw_status_repr(huc_fw->fetch_status), |
| 193 | intel_uc_fw_status_repr(huc_fw->load_status)); |
| 194 | |
| 195 | if (huc_fw->fetch_status == INTEL_UC_FIRMWARE_SUCCESS && |
| 196 | huc_fw->load_status == INTEL_UC_FIRMWARE_FAIL) |
| 197 | return -ENOEXEC; |
| 198 | |
| 199 | huc_fw->load_status = INTEL_UC_FIRMWARE_PENDING; |
| 200 | |
| 201 | switch (huc_fw->fetch_status) { |
| 202 | case INTEL_UC_FIRMWARE_FAIL: |
| 203 | /* something went wrong :( */ |
| 204 | err = -EIO; |
| 205 | goto fail; |
| 206 | |
| 207 | case INTEL_UC_FIRMWARE_NONE: |
| 208 | case INTEL_UC_FIRMWARE_PENDING: |
| 209 | default: |
| 210 | /* "can't happen" */ |
| 211 | WARN_ONCE(1, "HuC fw %s invalid fetch_status %s [%d]\n", |
| 212 | huc_fw->path, |
| 213 | intel_uc_fw_status_repr(huc_fw->fetch_status), |
| 214 | huc_fw->fetch_status); |
| 215 | err = -ENXIO; |
| 216 | goto fail; |
| 217 | |
| 218 | case INTEL_UC_FIRMWARE_SUCCESS: |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | err = huc_ucode_xfer(dev_priv); |
| 223 | if (err) |
| 224 | goto fail; |
| 225 | |
| 226 | huc_fw->load_status = INTEL_UC_FIRMWARE_SUCCESS; |
| 227 | |
| 228 | DRM_DEBUG_DRIVER("%s fw status: fetch %s, load %s\n", |
| 229 | huc_fw->path, |
| 230 | intel_uc_fw_status_repr(huc_fw->fetch_status), |
| 231 | intel_uc_fw_status_repr(huc_fw->load_status)); |
| 232 | |
| 233 | return 0; |
| 234 | |
| 235 | fail: |
| 236 | if (huc_fw->load_status == INTEL_UC_FIRMWARE_PENDING) |
| 237 | huc_fw->load_status = INTEL_UC_FIRMWARE_FAIL; |
| 238 | |
| 239 | DRM_ERROR("Failed to complete HuC uCode load with ret %d\n", err); |
| 240 | |
| 241 | return err; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * intel_huc_fini() - clean up resources allocated for HuC |
| 246 | * @dev_priv: the drm_i915_private device |
| 247 | * |
| 248 | * Cleans up by releasing the huc firmware GEM obj. |
| 249 | */ |
| 250 | void intel_huc_fini(struct drm_i915_private *dev_priv) |
| 251 | { |
| 252 | struct intel_uc_fw *huc_fw = &dev_priv->huc.fw; |
| 253 | |
| 254 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 255 | if (huc_fw->obj) |
| 256 | i915_gem_object_put(huc_fw->obj); |
| 257 | huc_fw->obj = NULL; |
| 258 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 259 | |
| 260 | huc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE; |
| 261 | } |
| 262 | |