1a85cb24fSFrançois Tigeot /*
2a85cb24fSFrançois Tigeot * Copyright © 2016-2017 Intel Corporation
3a85cb24fSFrançois Tigeot *
4a85cb24fSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a
5a85cb24fSFrançois Tigeot * copy of this software and associated documentation files (the "Software"),
6a85cb24fSFrançois Tigeot * to deal in the Software without restriction, including without limitation
7a85cb24fSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8a85cb24fSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the
9a85cb24fSFrançois Tigeot * Software is furnished to do so, subject to the following conditions:
10a85cb24fSFrançois Tigeot *
11a85cb24fSFrançois Tigeot * The above copyright notice and this permission notice (including the next
12a85cb24fSFrançois Tigeot * paragraph) shall be included in all copies or substantial portions of the
13a85cb24fSFrançois Tigeot * Software.
14a85cb24fSFrançois Tigeot *
15a85cb24fSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16a85cb24fSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17a85cb24fSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18a85cb24fSFrançois Tigeot * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19a85cb24fSFrançois Tigeot * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20a85cb24fSFrançois Tigeot * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21a85cb24fSFrançois Tigeot * IN THE SOFTWARE.
22a85cb24fSFrançois Tigeot *
23a85cb24fSFrançois Tigeot */
24*3f2dd94aSFrançois Tigeot
25*3f2dd94aSFrançois Tigeot #include <linux/types.h>
26*3f2dd94aSFrançois Tigeot
27*3f2dd94aSFrançois Tigeot #include "intel_huc.h"
28a85cb24fSFrançois Tigeot #include "i915_drv.h"
29a85cb24fSFrançois Tigeot
30a85cb24fSFrançois Tigeot /**
31a85cb24fSFrançois Tigeot * DOC: HuC Firmware
32a85cb24fSFrançois Tigeot *
33a85cb24fSFrançois Tigeot * Motivation:
34a85cb24fSFrançois Tigeot * GEN9 introduces a new dedicated firmware for usage in media HEVC (High
35a85cb24fSFrançois Tigeot * Efficiency Video Coding) operations. Userspace can use the firmware
36a85cb24fSFrançois Tigeot * capabilities by adding HuC specific commands to batch buffers.
37a85cb24fSFrançois Tigeot *
38a85cb24fSFrançois Tigeot * Implementation:
39a85cb24fSFrançois Tigeot * The same firmware loader is used as the GuC. However, the actual
40a85cb24fSFrançois Tigeot * loading to HW is deferred until GEM initialization is done.
41a85cb24fSFrançois Tigeot *
42a85cb24fSFrançois Tigeot * Note that HuC firmware loading must be done before GuC loading.
43a85cb24fSFrançois Tigeot */
44a85cb24fSFrançois Tigeot
45a85cb24fSFrançois Tigeot #define BXT_HUC_FW_MAJOR 01
46a85cb24fSFrançois Tigeot #define BXT_HUC_FW_MINOR 07
47a85cb24fSFrançois Tigeot #define BXT_BLD_NUM 1398
48a85cb24fSFrançois Tigeot
49a85cb24fSFrançois Tigeot #define SKL_HUC_FW_MAJOR 01
50a85cb24fSFrançois Tigeot #define SKL_HUC_FW_MINOR 07
51a85cb24fSFrançois Tigeot #define SKL_BLD_NUM 1398
52a85cb24fSFrançois Tigeot
53a85cb24fSFrançois Tigeot #define KBL_HUC_FW_MAJOR 02
54a85cb24fSFrançois Tigeot #define KBL_HUC_FW_MINOR 00
55a85cb24fSFrançois Tigeot #define KBL_BLD_NUM 1810
56a85cb24fSFrançois Tigeot
57a85cb24fSFrançois Tigeot #define HUC_FW_PATH(platform, major, minor, bld_num) \
58a85cb24fSFrançois Tigeot "i915/" __stringify(platform) "_huc_ver" __stringify(major) "_" \
59a85cb24fSFrançois Tigeot __stringify(minor) "_" __stringify(bld_num) ".bin"
60a85cb24fSFrançois Tigeot
61a85cb24fSFrançois Tigeot #define I915_SKL_HUC_UCODE HUC_FW_PATH(skl, SKL_HUC_FW_MAJOR, \
62a85cb24fSFrançois Tigeot SKL_HUC_FW_MINOR, SKL_BLD_NUM)
63a85cb24fSFrançois Tigeot MODULE_FIRMWARE(I915_SKL_HUC_UCODE);
64a85cb24fSFrançois Tigeot
65a85cb24fSFrançois Tigeot #define I915_BXT_HUC_UCODE HUC_FW_PATH(bxt, BXT_HUC_FW_MAJOR, \
66a85cb24fSFrançois Tigeot BXT_HUC_FW_MINOR, BXT_BLD_NUM)
67a85cb24fSFrançois Tigeot MODULE_FIRMWARE(I915_BXT_HUC_UCODE);
68a85cb24fSFrançois Tigeot
69a85cb24fSFrançois Tigeot #define I915_KBL_HUC_UCODE HUC_FW_PATH(kbl, KBL_HUC_FW_MAJOR, \
70a85cb24fSFrançois Tigeot KBL_HUC_FW_MINOR, KBL_BLD_NUM)
71a85cb24fSFrançois Tigeot MODULE_FIRMWARE(I915_KBL_HUC_UCODE);
72a85cb24fSFrançois Tigeot
73a85cb24fSFrançois Tigeot /**
74*3f2dd94aSFrançois Tigeot * intel_huc_select_fw() - selects HuC firmware for loading
75*3f2dd94aSFrançois Tigeot * @huc: intel_huc struct
76*3f2dd94aSFrançois Tigeot */
intel_huc_select_fw(struct intel_huc * huc)77*3f2dd94aSFrançois Tigeot void intel_huc_select_fw(struct intel_huc *huc)
78*3f2dd94aSFrançois Tigeot {
79*3f2dd94aSFrançois Tigeot struct drm_i915_private *dev_priv = huc_to_i915(huc);
80*3f2dd94aSFrançois Tigeot
81*3f2dd94aSFrançois Tigeot intel_uc_fw_init(&huc->fw, INTEL_UC_FW_TYPE_HUC);
82*3f2dd94aSFrançois Tigeot
83*3f2dd94aSFrançois Tigeot if (i915_modparams.huc_firmware_path) {
84*3f2dd94aSFrançois Tigeot huc->fw.path = i915_modparams.huc_firmware_path;
85*3f2dd94aSFrançois Tigeot huc->fw.major_ver_wanted = 0;
86*3f2dd94aSFrançois Tigeot huc->fw.minor_ver_wanted = 0;
87*3f2dd94aSFrançois Tigeot } else if (IS_SKYLAKE(dev_priv)) {
88*3f2dd94aSFrançois Tigeot huc->fw.path = I915_SKL_HUC_UCODE;
89*3f2dd94aSFrançois Tigeot huc->fw.major_ver_wanted = SKL_HUC_FW_MAJOR;
90*3f2dd94aSFrançois Tigeot huc->fw.minor_ver_wanted = SKL_HUC_FW_MINOR;
91*3f2dd94aSFrançois Tigeot } else if (IS_BROXTON(dev_priv)) {
92*3f2dd94aSFrançois Tigeot huc->fw.path = I915_BXT_HUC_UCODE;
93*3f2dd94aSFrançois Tigeot huc->fw.major_ver_wanted = BXT_HUC_FW_MAJOR;
94*3f2dd94aSFrançois Tigeot huc->fw.minor_ver_wanted = BXT_HUC_FW_MINOR;
95*3f2dd94aSFrançois Tigeot } else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) {
96*3f2dd94aSFrançois Tigeot huc->fw.path = I915_KBL_HUC_UCODE;
97*3f2dd94aSFrançois Tigeot huc->fw.major_ver_wanted = KBL_HUC_FW_MAJOR;
98*3f2dd94aSFrançois Tigeot huc->fw.minor_ver_wanted = KBL_HUC_FW_MINOR;
99*3f2dd94aSFrançois Tigeot } else {
100*3f2dd94aSFrançois Tigeot DRM_ERROR("No HuC firmware known for platform with HuC!\n");
101*3f2dd94aSFrançois Tigeot return;
102*3f2dd94aSFrançois Tigeot }
103*3f2dd94aSFrançois Tigeot }
104*3f2dd94aSFrançois Tigeot
105*3f2dd94aSFrançois Tigeot /**
106a85cb24fSFrançois Tigeot * huc_ucode_xfer() - DMA's the firmware
107a85cb24fSFrançois Tigeot * @dev_priv: the drm_i915_private device
108a85cb24fSFrançois Tigeot *
109a85cb24fSFrançois Tigeot * Transfer the firmware image to RAM for execution by the microcontroller.
110a85cb24fSFrançois Tigeot *
111a85cb24fSFrançois Tigeot * Return: 0 on success, non-zero on failure
112a85cb24fSFrançois Tigeot */
huc_ucode_xfer(struct intel_uc_fw * huc_fw,struct i915_vma * vma)113*3f2dd94aSFrançois Tigeot static int huc_ucode_xfer(struct intel_uc_fw *huc_fw, struct i915_vma *vma)
114a85cb24fSFrançois Tigeot {
115*3f2dd94aSFrançois Tigeot struct intel_huc *huc = container_of(huc_fw, struct intel_huc, fw);
116*3f2dd94aSFrançois Tigeot struct drm_i915_private *dev_priv = huc_to_i915(huc);
117a85cb24fSFrançois Tigeot unsigned long offset = 0;
118a85cb24fSFrançois Tigeot u32 size;
119a85cb24fSFrançois Tigeot int ret;
120a85cb24fSFrançois Tigeot
121*3f2dd94aSFrançois Tigeot GEM_BUG_ON(huc_fw->type != INTEL_UC_FW_TYPE_HUC);
122a85cb24fSFrançois Tigeot
123a85cb24fSFrançois Tigeot intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
124a85cb24fSFrançois Tigeot
125a85cb24fSFrançois Tigeot /* Set the source address for the uCode */
126a85cb24fSFrançois Tigeot offset = guc_ggtt_offset(vma) + huc_fw->header_offset;
127a85cb24fSFrançois Tigeot I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
128a85cb24fSFrançois Tigeot I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
129a85cb24fSFrançois Tigeot
130a85cb24fSFrançois Tigeot /* Hardware doesn't look at destination address for HuC. Set it to 0,
131a85cb24fSFrançois Tigeot * but still program the correct address space.
132a85cb24fSFrançois Tigeot */
133a85cb24fSFrançois Tigeot I915_WRITE(DMA_ADDR_1_LOW, 0);
134a85cb24fSFrançois Tigeot I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
135a85cb24fSFrançois Tigeot
136a85cb24fSFrançois Tigeot size = huc_fw->header_size + huc_fw->ucode_size;
137a85cb24fSFrançois Tigeot I915_WRITE(DMA_COPY_SIZE, size);
138a85cb24fSFrançois Tigeot
139a85cb24fSFrançois Tigeot /* Start the DMA */
140a85cb24fSFrançois Tigeot I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(HUC_UKERNEL | START_DMA));
141a85cb24fSFrançois Tigeot
142a85cb24fSFrançois Tigeot /* Wait for DMA to finish */
143a85cb24fSFrançois Tigeot ret = wait_for((I915_READ(DMA_CTRL) & START_DMA) == 0, 100);
144a85cb24fSFrançois Tigeot
145a85cb24fSFrançois Tigeot DRM_DEBUG_DRIVER("HuC DMA transfer wait over with ret %d\n", ret);
146a85cb24fSFrançois Tigeot
147a85cb24fSFrançois Tigeot /* Disable the bits once DMA is over */
148a85cb24fSFrançois Tigeot I915_WRITE(DMA_CTRL, _MASKED_BIT_DISABLE(HUC_UKERNEL));
149a85cb24fSFrançois Tigeot
150a85cb24fSFrançois Tigeot intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
151a85cb24fSFrançois Tigeot
152a85cb24fSFrançois Tigeot return ret;
153a85cb24fSFrançois Tigeot }
154a85cb24fSFrançois Tigeot
155a85cb24fSFrançois Tigeot /**
156a85cb24fSFrançois Tigeot * intel_huc_init_hw() - load HuC uCode to device
157a85cb24fSFrançois Tigeot * @huc: intel_huc structure
158a85cb24fSFrançois Tigeot *
159a85cb24fSFrançois Tigeot * Called from guc_setup() during driver loading and also after a GPU reset.
160a85cb24fSFrançois Tigeot * Be note that HuC loading must be done before GuC loading.
161a85cb24fSFrançois Tigeot *
162a85cb24fSFrançois Tigeot * The firmware image should have already been fetched into memory by the
163a85cb24fSFrançois Tigeot * earlier call to intel_huc_init(), so here we need only check that
164a85cb24fSFrançois Tigeot * is succeeded, and then transfer the image to the h/w.
165a85cb24fSFrançois Tigeot *
166a85cb24fSFrançois Tigeot */
intel_huc_init_hw(struct intel_huc * huc)167*3f2dd94aSFrançois Tigeot void intel_huc_init_hw(struct intel_huc *huc)
168a85cb24fSFrançois Tigeot {
169*3f2dd94aSFrançois Tigeot intel_uc_fw_upload(&huc->fw, huc_ucode_xfer);
170a85cb24fSFrançois Tigeot }
171a85cb24fSFrançois Tigeot
172a85cb24fSFrançois Tigeot /**
173*3f2dd94aSFrançois Tigeot * intel_huc_auth() - Authenticate HuC uCode
174*3f2dd94aSFrançois Tigeot * @huc: intel_huc structure
175a85cb24fSFrançois Tigeot *
176*3f2dd94aSFrançois Tigeot * Called after HuC and GuC firmware loading during intel_uc_init_hw().
177*3f2dd94aSFrançois Tigeot *
178*3f2dd94aSFrançois Tigeot * This function pins HuC firmware image object into GGTT.
179*3f2dd94aSFrançois Tigeot * Then it invokes GuC action to authenticate passing the offset to RSA
180*3f2dd94aSFrançois Tigeot * signature through intel_guc_auth_huc(). It then waits for 50ms for
181*3f2dd94aSFrançois Tigeot * firmware verification ACK and unpins the object.
182a85cb24fSFrançois Tigeot */
intel_huc_auth(struct intel_huc * huc)183*3f2dd94aSFrançois Tigeot void intel_huc_auth(struct intel_huc *huc)
184a85cb24fSFrançois Tigeot {
185*3f2dd94aSFrançois Tigeot struct drm_i915_private *i915 = huc_to_i915(huc);
186*3f2dd94aSFrançois Tigeot struct intel_guc *guc = &i915->guc;
187a85cb24fSFrançois Tigeot struct i915_vma *vma;
188a85cb24fSFrançois Tigeot int ret;
189a85cb24fSFrançois Tigeot
190a85cb24fSFrançois Tigeot if (huc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
191a85cb24fSFrançois Tigeot return;
192a85cb24fSFrançois Tigeot
193a85cb24fSFrançois Tigeot vma = i915_gem_object_ggtt_pin(huc->fw.obj, NULL, 0, 0,
194a85cb24fSFrançois Tigeot PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
195a85cb24fSFrançois Tigeot if (IS_ERR(vma)) {
196a85cb24fSFrançois Tigeot DRM_ERROR("failed to pin huc fw object %d\n",
197a85cb24fSFrançois Tigeot (int)PTR_ERR(vma));
198a85cb24fSFrançois Tigeot return;
199a85cb24fSFrançois Tigeot }
200a85cb24fSFrançois Tigeot
201*3f2dd94aSFrançois Tigeot ret = intel_guc_auth_huc(guc,
202*3f2dd94aSFrançois Tigeot guc_ggtt_offset(vma) + huc->fw.rsa_offset);
203a85cb24fSFrançois Tigeot if (ret) {
204a85cb24fSFrançois Tigeot DRM_ERROR("HuC: GuC did not ack Auth request %d\n", ret);
205a85cb24fSFrançois Tigeot goto out;
206a85cb24fSFrançois Tigeot }
207a85cb24fSFrançois Tigeot
208a85cb24fSFrançois Tigeot /* Check authentication status, it should be done by now */
209*3f2dd94aSFrançois Tigeot ret = intel_wait_for_register(i915,
210a85cb24fSFrançois Tigeot HUC_STATUS2,
211a85cb24fSFrançois Tigeot HUC_FW_VERIFIED,
212a85cb24fSFrançois Tigeot HUC_FW_VERIFIED,
213a85cb24fSFrançois Tigeot 50);
214a85cb24fSFrançois Tigeot if (ret) {
215a85cb24fSFrançois Tigeot DRM_ERROR("HuC: Authentication failed %d\n", ret);
216a85cb24fSFrançois Tigeot goto out;
217a85cb24fSFrançois Tigeot }
218a85cb24fSFrançois Tigeot
219a85cb24fSFrançois Tigeot out:
220a85cb24fSFrançois Tigeot i915_vma_unpin(vma);
221a85cb24fSFrançois Tigeot }
222