intel_huc.c 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright © 2016-2017 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */
24 25 26

#include <linux/types.h>

27
#include "gt/intel_gt.h"
28
#include "intel_huc.h"
29 30
#include "i915_drv.h"

31 32
void intel_huc_init_early(struct intel_huc *huc)
{
33
	struct drm_i915_private *i915 = huc_to_gt(huc)->i915;
34

35
	intel_huc_fw_init_early(huc);
36 37 38 39 40 41 42 43 44 45

	if (INTEL_GEN(i915) >= 11) {
		huc->status.reg = GEN11_HUC_KERNEL_LOAD_INFO;
		huc->status.mask = HUC_LOAD_SUCCESSFUL;
		huc->status.value = HUC_LOAD_SUCCESSFUL;
	} else {
		huc->status.reg = HUC_STATUS2;
		huc->status.mask = HUC_FW_VERIFIED;
		huc->status.value = HUC_FW_VERIFIED;
	}
46 47
}

48 49
static int intel_huc_rsa_data_create(struct intel_huc *huc)
{
50 51
	struct intel_gt *gt = huc_to_gt(huc);
	struct intel_guc *guc = &gt->uc.guc;
52
	struct i915_vma *vma;
53
	size_t copied;
54 55 56 57 58 59 60 61 62 63 64 65
	void *vaddr;

	/*
	 * HuC firmware will sit above GUC_GGTT_TOP and will not map
	 * through GTT. Unfortunately, this means GuC cannot perform
	 * the HuC auth. as the rsa offset now falls within the GuC
	 * inaccessible range. We resort to perma-pinning an additional
	 * vma within the accessible range that only contains the rsa
	 * signature. The GuC can use this extra pinning to perform
	 * the authentication since its GGTT offset will be GuC
	 * accessible.
	 */
66
	GEM_BUG_ON(huc->fw.rsa_size > PAGE_SIZE);
67 68 69 70 71 72 73 74 75 76
	vma = intel_guc_allocate_vma(guc, PAGE_SIZE);
	if (IS_ERR(vma))
		return PTR_ERR(vma);

	vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
	if (IS_ERR(vaddr)) {
		i915_vma_unpin_and_release(&vma, 0);
		return PTR_ERR(vaddr);
	}

77 78 79 80 81
	copied = intel_uc_fw_copy_rsa(&huc->fw, vaddr, vma->size);
	GEM_BUG_ON(copied < huc->fw.rsa_size);

	i915_gem_object_unpin_map(vma->obj);

82 83 84 85 86 87 88
	huc->rsa_data = vma;

	return 0;
}

static void intel_huc_rsa_data_destroy(struct intel_huc *huc)
{
89
	i915_vma_unpin_and_release(&huc->rsa_data, 0);
90 91 92 93 94 95
}

int intel_huc_init(struct intel_huc *huc)
{
	int err;

96
	err = intel_uc_fw_init(&huc->fw);
97 98 99
	if (err)
		return err;

100 101 102 103 104 105 106 107 108 109 110 111 112 113
	/*
	 * HuC firmware image is outside GuC accessible range.
	 * Copy the RSA signature out of the image into
	 * a perma-pinned region set aside for it
	 */
	err = intel_huc_rsa_data_create(huc);
	if (err)
		goto out_fini;

	return 0;

out_fini:
	intel_uc_fw_fini(&huc->fw);
	return err;
114 115 116 117 118 119 120 121
}

void intel_huc_fini(struct intel_huc *huc)
{
	intel_uc_fw_fini(&huc->fw);
	intel_huc_rsa_data_destroy(huc);
}

122
/**
123 124 125 126
 * intel_huc_auth() - Authenticate HuC uCode
 * @huc: intel_huc structure
 *
 * Called after HuC and GuC firmware loading during intel_uc_init_hw().
127
 *
128 129 130 131
 * This function pins HuC firmware image object into GGTT.
 * Then it invokes GuC action to authenticate passing the offset to RSA
 * signature through intel_guc_auth_huc(). It then waits for 50ms for
 * firmware verification ACK and unpins the object.
132
 */
133
int intel_huc_auth(struct intel_huc *huc)
134
{
135 136
	struct intel_gt *gt = huc_to_gt(huc);
	struct intel_guc *guc = &gt->uc.guc;
137 138
	int ret;

139 140
	GEM_BUG_ON(!intel_uc_fw_is_loaded(&huc->fw));
	GEM_BUG_ON(intel_huc_is_authenticated(huc));
141

142 143 144 145
	ret = i915_inject_load_error(gt->i915, -ENXIO);
	if (ret)
		goto fail;

146
	ret = intel_guc_auth_huc(guc,
147
				 intel_guc_ggtt_offset(guc, huc->rsa_data));
148 149
	if (ret) {
		DRM_ERROR("HuC: GuC did not ack Auth request %d\n", ret);
150
		goto fail;
151 152 153
	}

	/* Check authentication status, it should be done by now */
154
	ret = __intel_wait_for_register(gt->uncore,
155 156 157 158
					huc->status.reg,
					huc->status.mask,
					huc->status.value,
					2, 50, NULL);
159
	if (ret) {
160
		DRM_ERROR("HuC: Firmware not verified %d\n", ret);
161
		goto fail;
162 163
	}

164
	huc->fw.status = INTEL_UC_FIRMWARE_RUNNING;
165 166 167
	return 0;

fail:
168
	i915_probe_error(gt->i915, "HuC: Authentication failed %d\n", ret);
169
	huc->fw.status = INTEL_UC_FIRMWARE_FAIL;
170
	return ret;
171
}
172 173 174 175 176 177 178 179

/**
 * intel_huc_check_status() - check HuC status
 * @huc: intel_huc structure
 *
 * This function reads status register to verify if HuC
 * firmware was successfully loaded.
 *
180 181 182
 * Returns: 1 if HuC firmware is loaded and verified,
 * 0 if HuC firmware is not loaded and -ENODEV if HuC
 * is not present on this platform.
183 184 185
 */
int intel_huc_check_status(struct intel_huc *huc)
{
186
	struct intel_gt *gt = huc_to_gt(huc);
187
	intel_wakeref_t wakeref;
188
	u32 status = 0;
189

190
	if (!intel_huc_is_supported(huc))
191 192
		return -ENODEV;

193 194
	with_intel_runtime_pm(&gt->i915->runtime_pm, wakeref)
		status = intel_uncore_read(gt->uncore, huc->status.reg);
195

196
	return (status & huc->status.mask) == huc->status.value;
197
}