blob: 5fadd55790ebb980f02d8e5caa2bf4e5753c5585 [file] [log] [blame]
Anusha Srivatsabd1328582017-01-18 08:05:53 -08001/*
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
Anusha Srivatsacd69098572017-01-18 08:05:54 -080043#define BXT_HUC_FW_MAJOR 01
44#define BXT_HUC_FW_MINOR 07
45#define BXT_BLD_NUM 1398
46
Anusha Srivatsabd1328582017-01-18 08:05:53 -080047#define SKL_HUC_FW_MAJOR 01
48#define SKL_HUC_FW_MINOR 07
49#define SKL_BLD_NUM 1398
50
Anusha Srivatsaf2ec71d2017-01-18 08:05:55 -080051#define KBL_HUC_FW_MAJOR 02
52#define KBL_HUC_FW_MINOR 00
53#define KBL_BLD_NUM 1810
54
Anusha Srivatsabd1328582017-01-18 08:05:53 -080055#define HUC_FW_PATH(platform, major, minor, bld_num) \
56 "i915/" __stringify(platform) "_huc_ver" __stringify(major) "_" \
57 __stringify(minor) "_" __stringify(bld_num) ".bin"
58
59#define I915_SKL_HUC_UCODE HUC_FW_PATH(skl, SKL_HUC_FW_MAJOR, \
60 SKL_HUC_FW_MINOR, SKL_BLD_NUM)
61MODULE_FIRMWARE(I915_SKL_HUC_UCODE);
62
Anusha Srivatsacd69098572017-01-18 08:05:54 -080063#define I915_BXT_HUC_UCODE HUC_FW_PATH(bxt, BXT_HUC_FW_MAJOR, \
64 BXT_HUC_FW_MINOR, BXT_BLD_NUM)
65MODULE_FIRMWARE(I915_BXT_HUC_UCODE);
Anusha Srivatsaf2ec71d2017-01-18 08:05:55 -080066
67#define I915_KBL_HUC_UCODE HUC_FW_PATH(kbl, KBL_HUC_FW_MAJOR, \
68 KBL_HUC_FW_MINOR, KBL_BLD_NUM)
69MODULE_FIRMWARE(I915_KBL_HUC_UCODE);
70
Anusha Srivatsabd1328582017-01-18 08:05:53 -080071/**
72 * huc_ucode_xfer() - DMA's the firmware
73 * @dev_priv: the drm_i915_private device
74 *
75 * Transfer the firmware image to RAM for execution by the microcontroller.
76 *
77 * Return: 0 on success, non-zero on failure
78 */
79static int huc_ucode_xfer(struct drm_i915_private *dev_priv)
80{
81 struct intel_uc_fw *huc_fw = &dev_priv->huc.fw;
82 struct i915_vma *vma;
83 unsigned long offset = 0;
84 u32 size;
85 int ret;
86
87 ret = i915_gem_object_set_to_gtt_domain(huc_fw->obj, false);
88 if (ret) {
89 DRM_DEBUG_DRIVER("set-domain failed %d\n", ret);
90 return ret;
91 }
92
93 vma = i915_gem_object_ggtt_pin(huc_fw->obj, NULL, 0, 0,
94 PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
95 if (IS_ERR(vma)) {
96 DRM_DEBUG_DRIVER("pin failed %d\n", (int)PTR_ERR(vma));
97 return PTR_ERR(vma);
98 }
99
100 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
101
102 /* init WOPCM */
103 I915_WRITE(GUC_WOPCM_SIZE, intel_guc_wopcm_size(dev_priv));
104 I915_WRITE(DMA_GUC_WOPCM_OFFSET, GUC_WOPCM_OFFSET_VALUE |
105 HUC_LOADING_AGENT_GUC);
106
107 /* Set the source address for the uCode */
108 offset = guc_ggtt_offset(vma) + huc_fw->header_offset;
109 I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
110 I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
111
112 /* Hardware doesn't look at destination address for HuC. Set it to 0,
113 * but still program the correct address space.
114 */
115 I915_WRITE(DMA_ADDR_1_LOW, 0);
116 I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
117
118 size = huc_fw->header_size + huc_fw->ucode_size;
119 I915_WRITE(DMA_COPY_SIZE, size);
120
121 /* Start the DMA */
122 I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(HUC_UKERNEL | START_DMA));
123
124 /* Wait for DMA to finish */
125 ret = wait_for((I915_READ(DMA_CTRL) & START_DMA) == 0, 100);
126
127 DRM_DEBUG_DRIVER("HuC DMA transfer wait over with ret %d\n", ret);
128
129 /* Disable the bits once DMA is over */
130 I915_WRITE(DMA_CTRL, _MASKED_BIT_DISABLE(HUC_UKERNEL));
131
132 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
133
134 /*
135 * We keep the object pages for reuse during resume. But we can unpin it
136 * now that DMA has completed, so it doesn't continue to take up space.
137 */
138 i915_vma_unpin(vma);
139
140 return ret;
141}
142
143/**
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100144 * intel_huc_init_fw() - select and prepare firmware for loading
145 * @huc: intel_huc struct
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800146 *
147 * Called early during driver load, but after GEM is initialised. The loading
148 * will continue only when driver explicitly specify firmware name and version.
149 * All other cases are considered as INTEL_UC_FIRMWARE_NONE either because HW
150 * is not capable or driver yet support it. And there will be no error message
151 * for INTEL_UC_FIRMWARE_NONE cases.
152 *
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100153 * The DMA-copying to HW is done later when intel_huc_init_hw() is called.
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800154 */
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100155void intel_huc_init_fw(struct intel_huc *huc)
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800156{
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100157 struct drm_i915_private *dev_priv = huc_to_i915(huc);
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800158
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100159 huc->fw.path = NULL;
160 huc->fw.fetch_status = INTEL_UC_FIRMWARE_NONE;
161 huc->fw.load_status = INTEL_UC_FIRMWARE_NONE;
162 huc->fw.fw = INTEL_UC_FW_TYPE_HUC;
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800163
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800164 if (IS_SKYLAKE(dev_priv)) {
Arkadiusz Hiler8fc2a4e2017-03-14 15:28:12 +0100165 huc->fw.path = I915_SKL_HUC_UCODE;
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100166 huc->fw.major_ver_wanted = SKL_HUC_FW_MAJOR;
167 huc->fw.minor_ver_wanted = SKL_HUC_FW_MINOR;
Anusha Srivatsacd69098572017-01-18 08:05:54 -0800168 } else if (IS_BROXTON(dev_priv)) {
Arkadiusz Hiler8fc2a4e2017-03-14 15:28:12 +0100169 huc->fw.path = I915_BXT_HUC_UCODE;
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100170 huc->fw.major_ver_wanted = BXT_HUC_FW_MAJOR;
171 huc->fw.minor_ver_wanted = BXT_HUC_FW_MINOR;
Anusha Srivatsaf2ec71d2017-01-18 08:05:55 -0800172 } else if (IS_KABYLAKE(dev_priv)) {
Arkadiusz Hiler8fc2a4e2017-03-14 15:28:12 +0100173 huc->fw.path = I915_KBL_HUC_UCODE;
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100174 huc->fw.major_ver_wanted = KBL_HUC_FW_MAJOR;
175 huc->fw.minor_ver_wanted = KBL_HUC_FW_MINOR;
Arkadiusz Hiler8fc2a4e2017-03-14 15:28:12 +0100176 } else {
177 DRM_ERROR("No HuC firmware known for platform with HuC!\n");
Anusha Srivatsa13e867f2017-03-01 11:58:55 -0800178 return;
Arkadiusz Hiler8fc2a4e2017-03-14 15:28:12 +0100179 }
Arkadiusz Hiler29ad6a32017-03-14 15:28:09 +0100180
181 intel_uc_prepare_fw(dev_priv, &huc->fw);
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800182}
183
184/**
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100185 * intel_huc_init_hw() - load HuC uCode to device
186 * @huc: intel_huc structure
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800187 *
188 * Called from guc_setup() during driver loading and also after a GPU reset.
189 * Be note that HuC loading must be done before GuC loading.
190 *
191 * The firmware image should have already been fetched into memory by the
192 * earlier call to intel_huc_init(), so here we need only check that
193 * is succeeded, and then transfer the image to the h/w.
194 *
195 * Return: non-zero code on error
196 */
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100197int intel_huc_init_hw(struct intel_huc *huc)
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800198{
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100199 struct drm_i915_private *dev_priv = huc_to_i915(huc);
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800200 int err;
201
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100202 if (huc->fw.fetch_status == INTEL_UC_FIRMWARE_NONE)
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800203 return 0;
204
205 DRM_DEBUG_DRIVER("%s fw status: fetch %s, load %s\n",
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100206 huc->fw.path,
207 intel_uc_fw_status_repr(huc->fw.fetch_status),
208 intel_uc_fw_status_repr(huc->fw.load_status));
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800209
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100210 if (huc->fw.fetch_status == INTEL_UC_FIRMWARE_SUCCESS &&
211 huc->fw.load_status == INTEL_UC_FIRMWARE_FAIL)
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800212 return -ENOEXEC;
213
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100214 huc->fw.load_status = INTEL_UC_FIRMWARE_PENDING;
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800215
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100216 switch (huc->fw.fetch_status) {
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800217 case INTEL_UC_FIRMWARE_FAIL:
218 /* something went wrong :( */
219 err = -EIO;
220 goto fail;
221
222 case INTEL_UC_FIRMWARE_NONE:
223 case INTEL_UC_FIRMWARE_PENDING:
224 default:
225 /* "can't happen" */
226 WARN_ONCE(1, "HuC fw %s invalid fetch_status %s [%d]\n",
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100227 huc->fw.path,
228 intel_uc_fw_status_repr(huc->fw.fetch_status),
229 huc->fw.fetch_status);
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800230 err = -ENXIO;
231 goto fail;
232
233 case INTEL_UC_FIRMWARE_SUCCESS:
234 break;
235 }
236
237 err = huc_ucode_xfer(dev_priv);
238 if (err)
239 goto fail;
240
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100241 huc->fw.load_status = INTEL_UC_FIRMWARE_SUCCESS;
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800242
243 DRM_DEBUG_DRIVER("%s fw status: fetch %s, load %s\n",
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100244 huc->fw.path,
245 intel_uc_fw_status_repr(huc->fw.fetch_status),
246 intel_uc_fw_status_repr(huc->fw.load_status));
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800247
248 return 0;
249
250fail:
Arkadiusz Hiler882d1db2017-03-14 15:28:07 +0100251 if (huc->fw.load_status == INTEL_UC_FIRMWARE_PENDING)
252 huc->fw.load_status = INTEL_UC_FIRMWARE_FAIL;
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800253
254 DRM_ERROR("Failed to complete HuC uCode load with ret %d\n", err);
255
256 return err;
257}
258
259/**
260 * intel_huc_fini() - clean up resources allocated for HuC
261 * @dev_priv: the drm_i915_private device
262 *
263 * Cleans up by releasing the huc firmware GEM obj.
264 */
265void intel_huc_fini(struct drm_i915_private *dev_priv)
266{
267 struct intel_uc_fw *huc_fw = &dev_priv->huc.fw;
Chris Wilson65300b12017-02-14 13:34:20 +0000268 struct drm_i915_gem_object *obj;
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800269
Chris Wilson65300b12017-02-14 13:34:20 +0000270 obj = fetch_and_zero(&huc_fw->obj);
271 if (obj)
272 i915_gem_object_put(obj);
Anusha Srivatsabd1328582017-01-18 08:05:53 -0800273
274 huc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
275}
276
Anusha Srivatsadac84a32017-01-18 08:05:57 -0800277/**
278 * intel_guc_auth_huc() - authenticate ucode
279 * @dev_priv: the drm_i915_device
280 *
281 * Triggers a HuC fw authentication request to the GuC via intel_guc_action_
282 * authenticate_huc interface.
283 */
284void intel_guc_auth_huc(struct drm_i915_private *dev_priv)
285{
286 struct intel_guc *guc = &dev_priv->guc;
287 struct intel_huc *huc = &dev_priv->huc;
288 struct i915_vma *vma;
289 int ret;
290 u32 data[2];
291
Michał Winiarski7e8d12b2017-01-20 20:23:46 +0100292 if (huc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
293 return;
294
Anusha Srivatsadac84a32017-01-18 08:05:57 -0800295 vma = i915_gem_object_ggtt_pin(huc->fw.obj, NULL, 0, 0,
296 PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
297 if (IS_ERR(vma)) {
298 DRM_ERROR("failed to pin huc fw object %d\n",
299 (int)PTR_ERR(vma));
300 return;
301 }
302
303 /* Specify auth action and where public signature is. */
304 data[0] = INTEL_GUC_ACTION_AUTHENTICATE_HUC;
Michał Winiarski3139b4a2017-01-20 20:23:47 +0100305 data[1] = guc_ggtt_offset(vma) + huc->fw.rsa_offset;
Anusha Srivatsadac84a32017-01-18 08:05:57 -0800306
307 ret = intel_guc_send(guc, data, ARRAY_SIZE(data));
308 if (ret) {
309 DRM_ERROR("HuC: GuC did not ack Auth request %d\n", ret);
310 goto out;
311 }
312
313 /* Check authentication status, it should be done by now */
314 ret = intel_wait_for_register(dev_priv,
315 HUC_STATUS2,
316 HUC_FW_VERIFIED,
317 HUC_FW_VERIFIED,
318 50);
319
320 if (ret) {
321 DRM_ERROR("HuC: Authentication failed %d\n", ret);
322 goto out;
323 }
324
325out:
326 i915_vma_unpin(vma);
327}
328