1 // SPDX-License-Identifier: MIT
3 * Copyright © 2014-2019 Intel Corporation
6 #include "gt/intel_gsc.h"
7 #include "gt/intel_gt.h"
8 #include "intel_gsc_binary_headers.h"
9 #include "intel_gsc_uc_heci_cmd_submit.h"
10 #include "intel_huc.h"
11 #include "intel_huc_fw.h"
12 #include "intel_huc_print.h"
14 #include "pxp/intel_pxp_huc.h"
15 #include "pxp/intel_pxp_cmd_interface_43.h"
17 struct mtl_huc_auth_msg_in {
18 struct intel_gsc_mtl_header header;
19 struct pxp43_new_huc_auth_in huc_in;
22 struct mtl_huc_auth_msg_out {
23 struct intel_gsc_mtl_header header;
24 struct pxp43_huc_auth_out huc_out;
27 int intel_huc_fw_auth_via_gsccs(struct intel_huc *huc)
29 struct intel_gt *gt = huc_to_gt(huc);
30 struct drm_i915_gem_object *obj;
31 struct mtl_huc_auth_msg_in *msg_in;
32 struct mtl_huc_auth_msg_out *msg_out;
41 obj = huc->heci_pkt->obj;
42 pkt_offset = i915_ggtt_offset(huc->heci_pkt);
44 pkt_vaddr = i915_gem_object_pin_map_unlocked(obj,
45 intel_gt_coherent_map_type(gt, obj, true));
46 if (IS_ERR(pkt_vaddr))
47 return PTR_ERR(pkt_vaddr);
50 msg_out = pkt_vaddr + PXP43_HUC_AUTH_INOUT_SIZE;
52 intel_gsc_uc_heci_cmd_emit_mtl_header(&msg_in->header,
56 msg_in->huc_in.header.api_version = PXP_APIVER(4, 3);
57 msg_in->huc_in.header.command_id = PXP43_CMDID_NEW_HUC_AUTH;
58 msg_in->huc_in.header.status = 0;
59 msg_in->huc_in.header.buffer_len = sizeof(msg_in->huc_in) -
60 sizeof(msg_in->huc_in.header);
61 msg_in->huc_in.huc_base_address = huc->fw.vma_res.start;
62 msg_in->huc_in.huc_size = huc->fw.obj->base.size;
65 err = intel_gsc_uc_heci_cmd_submit_packet(>->uc.gsc,
66 pkt_offset, sizeof(*msg_in),
67 pkt_offset + PXP43_HUC_AUTH_INOUT_SIZE,
68 PXP43_HUC_AUTH_INOUT_SIZE);
70 huc_err(huc, "failed to submit GSC request to auth: %d\n", err);
74 if (msg_out->header.flags & GSC_OUTFLAG_MSG_PENDING) {
75 msg_in->header.gsc_message_handle = msg_out->header.gsc_message_handle;
79 } while (--retry && err == -EBUSY);
84 if (msg_out->header.message_size != sizeof(*msg_out)) {
85 huc_err(huc, "invalid GSC reply length %u [expected %zu]\n",
86 msg_out->header.message_size, sizeof(*msg_out));
92 * The GSC will return PXP_STATUS_OP_NOT_PERMITTED if the HuC is already
93 * loaded. If the same error is ever returned with HuC not loaded we'll
94 * still catch it when we check the authentication bit later.
96 if (msg_out->huc_out.header.status != PXP_STATUS_SUCCESS &&
97 msg_out->huc_out.header.status != PXP_STATUS_OP_NOT_PERMITTED) {
98 huc_err(huc, "auth failed with GSC error = 0x%x\n",
99 msg_out->huc_out.header.status);
105 i915_gem_object_unpin_map(obj);
109 static bool css_valid(const void *data, size_t size)
111 const struct uc_css_header *css = data;
113 if (unlikely(size < sizeof(struct uc_css_header)))
116 if (css->module_type != 0x6)
119 if (css->module_vendor != PCI_VENDOR_ID_INTEL)
125 static inline u32 entry_offset(const struct intel_gsc_cpd_entry *entry)
127 return entry->offset & INTEL_GSC_CPD_ENTRY_OFFSET_MASK;
130 int intel_huc_fw_get_binary_info(struct intel_uc_fw *huc_fw, const void *data, size_t size)
132 struct intel_huc *huc = container_of(huc_fw, struct intel_huc, fw);
133 const struct intel_gsc_cpd_header_v2 *header = data;
134 const struct intel_gsc_cpd_entry *entry;
135 size_t min_size = sizeof(*header);
138 if (!huc_fw->has_gsc_headers) {
139 huc_err(huc, "Invalid FW type for GSC header parsing!\n");
143 if (size < sizeof(*header)) {
144 huc_err(huc, "FW too small! %zu < %zu\n", size, min_size);
149 * The GSC-enabled HuC binary starts with a directory header, followed
150 * by a series of entries. Each entry is identified by a name and
151 * points to a specific section of the binary containing the relevant
152 * data. The entries we're interested in are:
153 * - "HUCP.man": points to the GSC manifest header for the HuC, which
154 * contains the version info.
155 * - "huc_fw": points to the legacy-style binary that can be used for
156 * load via the DMA. This entry only contains a valid CSS
157 * on binaries for platforms that support 2-step HuC load
158 * via dma and auth via GSC (like MTL).
160 * --------------------------------------------------
161 * [ intel_gsc_cpd_header_v2 ]
162 * --------------------------------------------------
163 * [ intel_gsc_cpd_entry[] ]
169 * [ offset >----------------------------]------o
174 * [ offset >----------------------------]----------o
175 * -------------------------------------------------- | |
177 * -------------------------------------------------- | |
178 * [ intel_gsc_manifest_header ]<-----o |
180 * [ intel_gsc_version fw_version ] |
182 * -------------------------------------------------- |
184 * -------------------------------------------------- |
185 * [ data[] ]<---------o
188 * --------------------------------------------------
191 if (header->header_marker != INTEL_GSC_CPD_HEADER_MARKER) {
192 huc_err(huc, "invalid marker for CPD header: 0x%08x!\n",
193 header->header_marker);
197 /* we only have binaries with header v2 and entry v1 for now */
198 if (header->header_version != 2 || header->entry_version != 1) {
199 huc_err(huc, "invalid CPD header/entry version %u:%u!\n",
200 header->header_version, header->entry_version);
204 if (header->header_length < sizeof(struct intel_gsc_cpd_header_v2)) {
205 huc_err(huc, "invalid CPD header length %u!\n",
206 header->header_length);
210 min_size = header->header_length + sizeof(*entry) * header->num_of_entries;
211 if (size < min_size) {
212 huc_err(huc, "FW too small! %zu < %zu\n", size, min_size);
216 entry = data + header->header_length;
218 for (i = 0; i < header->num_of_entries; i++, entry++) {
219 if (strcmp(entry->name, "HUCP.man") == 0)
220 intel_uc_fw_version_from_gsc_manifest(&huc_fw->file_selected.ver,
221 data + entry_offset(entry));
223 if (strcmp(entry->name, "huc_fw") == 0) {
224 u32 offset = entry_offset(entry);
226 if (offset < size && css_valid(data + offset, size - offset))
227 huc_fw->dma_start_offset = offset;
234 int intel_huc_fw_load_and_auth_via_gsc(struct intel_huc *huc)
238 if (!intel_huc_is_loaded_by_gsc(huc))
241 if (!intel_uc_fw_is_loadable(&huc->fw))
245 * If we abort a suspend, HuC might still be loaded when the mei
246 * component gets re-bound and this function called again. If so, just
247 * mark the HuC as loaded.
249 if (intel_huc_is_authenticated(huc, INTEL_HUC_AUTH_BY_GSC)) {
250 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_RUNNING);
254 GEM_WARN_ON(intel_uc_fw_is_loaded(&huc->fw));
256 ret = intel_pxp_huc_load_and_auth(huc_to_gt(huc)->i915->pxp);
260 intel_uc_fw_change_status(&huc->fw, INTEL_UC_FIRMWARE_TRANSFERRED);
262 return intel_huc_wait_for_auth_complete(huc, INTEL_HUC_AUTH_BY_GSC);
266 * intel_huc_fw_upload() - load HuC uCode to device via DMA transfer
267 * @huc: intel_huc structure
269 * Called from intel_uc_init_hw() during driver load, resume from sleep and
270 * after a GPU reset. Note that HuC must be loaded before GuC.
272 * The firmware image should have already been fetched into memory, so only
273 * check that fetch succeeded, and then transfer the image to the h/w.
275 * Return: non-zero code on error
277 int intel_huc_fw_upload(struct intel_huc *huc)
279 if (intel_huc_is_loaded_by_gsc(huc))
282 /* HW doesn't look at destination address for HuC, so set it to 0 */
283 return intel_uc_fw_upload(&huc->fw, 0, HUC_UKERNEL);