intel_guc_fw.c 7.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 24 25 26 27 28
/*
 * Copyright © 2014 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.
 *
 * Authors:
 *    Vinit Azad <vinit.azad@intel.com>
 *    Ben Widawsky <ben@bwidawsk.net>
 *    Dave Gordon <david.s.gordon@intel.com>
 *    Alex Dai <yu.dai@intel.com>
 */
29

30
#include "gt/intel_gt.h"
31
#include "intel_guc_fw.h"
32 33
#include "i915_drv.h"

34 35 36 37 38 39 40 41
/**
 * intel_guc_fw_init_early() - initializes GuC firmware struct
 * @guc: intel_guc struct
 *
 * On platforms with GuC selects firmware for uploading
 */
void intel_guc_fw_init_early(struct intel_guc *guc)
{
42
	intel_uc_fw_init_early(&guc->fw, INTEL_UC_FW_TYPE_GUC, guc_to_gt(guc)->i915);
43 44
}

45
static void guc_prepare_xfer(struct intel_guc *guc)
46
{
47 48 49 50 51 52 53 54
	struct intel_gt *gt = guc_to_gt(guc);
	struct intel_uncore *uncore = gt->uncore;
	u32 shim_flags = GUC_DISABLE_SRAM_INIT_TO_ZEROES |
			 GUC_ENABLE_READ_CACHE_LOGIC |
			 GUC_ENABLE_MIA_CACHING |
			 GUC_ENABLE_READ_CACHE_FOR_SRAM_DATA |
			 GUC_ENABLE_READ_CACHE_FOR_WOPCM_DATA |
			 GUC_ENABLE_MIA_CLOCK_GATING;
55

56
	/* Must program this register before loading the ucode with DMA */
57 58 59 60
	intel_uncore_write(uncore, GUC_SHIM_CONTROL, shim_flags);

	if (IS_GEN9_LP(gt->i915))
		intel_uncore_write(uncore, GEN9LP_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
61
	else
62
		intel_uncore_write(uncore, GEN9_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
63

64
	if (IS_GEN(gt->i915, 9)) {
65
		/* DOP Clock Gating Enable for GuC clocks */
66 67
		intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
				 0, GEN8_DOP_CLOCK_GATE_GUC_ENABLE);
68 69

		/* allows for 5us (in 10ns units) before GT can go to RC6 */
70
		intel_uncore_write(uncore, GUC_ARAT_C6DIS, 0x1FF);
71 72 73 74
	}
}

/* Copy RSA signature from the fw image to HW for verification */
75
static void guc_xfer_rsa(struct intel_guc *guc)
76
{
77
	struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
78
	u32 rsa[UOS_RSA_SCRATCH_COUNT];
79
	size_t copied;
80 81
	int i;

82 83
	copied = intel_uc_fw_copy_rsa(&guc->fw, rsa, sizeof(rsa));
	GEM_BUG_ON(copied < sizeof(rsa));
84

85
	for (i = 0; i < UOS_RSA_SCRATCH_COUNT; i++)
86
		intel_uncore_write(uncore, UOS_RSA_SCRATCH(i), rsa[i]);
87 88
}

89
static bool guc_xfer_completed(struct intel_uncore *uncore, u32 *status)
90
{
91
	/* Did we complete the xfer? */
92
	*status = intel_uncore_read(uncore, DMA_CTRL);
93
	return !(*status & START_DMA);
94 95 96 97 98 99 100 101 102 103 104
}

/*
 * Read the GuC status register (GUC_STATUS) and store it in the
 * specified location; then return a boolean indicating whether
 * the value matches either of two values representing completion
 * of the GuC boot process.
 *
 * This is used for polling the GuC status in a wait_for()
 * loop below.
 */
105
static inline bool guc_ready(struct intel_uncore *uncore, u32 *status)
106
{
107
	u32 val = intel_uncore_read(uncore, GUC_STATUS);
108 109 110 111 112 113 114
	u32 uk_val = val & GS_UKERNEL_MASK;

	*status = val;
	return (uk_val == GS_UKERNEL_READY) ||
		((val & GS_MIA_CORE_STATE) && (uk_val == GS_UKERNEL_LAPIC_DONE));
}

115
static int guc_wait_ucode(struct intel_uncore *uncore)
116 117 118 119
{
	u32 status;
	int ret;

120
	/*
121
	 * Wait for the GuC to start up.
122 123 124
	 * NB: Docs recommend not using the interrupt for completion.
	 * Measurements indicate this should take no more than 20ms, so a
	 * timeout here indicates that the GuC has failed and is unusable.
125 126
	 * (Higher levels of the driver may decide to reset the GuC and
	 * attempt the ucode load again if this happens.)
127
	 */
128
	ret = wait_for(guc_ready(uncore, &status), 100);
129
	DRM_DEBUG_DRIVER("GuC status %#x\n", status);
130 131 132 133 134 135

	if ((status & GS_BOOTROM_MASK) == GS_BOOTROM_RSA_FAILED) {
		DRM_ERROR("GuC firmware signature verification failed\n");
		ret = -ENOEXEC;
	}

136 137
	if ((status & GS_UKERNEL_MASK) == GS_UKERNEL_EXCEPTION) {
		DRM_ERROR("GuC firmware exception. EIP: %#x\n",
138
			  intel_uncore_read(uncore, SOFT_SCRATCH(13)));
139 140 141
		ret = -ENXIO;
	}

142
	if (ret == 0 && !guc_xfer_completed(uncore, &status)) {
143 144 145 146 147
		DRM_ERROR("GuC is ready, but the xfer %08x is incomplete\n",
			  status);
		ret = -ENXIO;
	}

148 149 150
	return ret;
}

151 152 153 154 155 156 157
/*
 * Transfer the firmware image to RAM for execution by the microcontroller.
 *
 * Architecturally, the DMA engine is bidirectional, and can potentially even
 * transfer between GTT locations. This functionality is left out of the API
 * for now as there is no need for it.
 */
158
static int guc_xfer_ucode(struct intel_guc *guc)
159
{
160
	struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
161 162 163 164 165 166 167
	struct intel_uc_fw *guc_fw = &guc->fw;
	unsigned long offset;

	/*
	 * The header plus uCode will be copied to WOPCM via DMA, excluding any
	 * other components
	 */
168 169
	intel_uncore_write(uncore, DMA_COPY_SIZE,
			   guc_fw->header_size + guc_fw->ucode_size);
170 171

	/* Set the source address for the new blob */
172
	offset = intel_uc_fw_ggtt_offset(guc_fw) + guc_fw->header_offset;
173 174
	intel_uncore_write(uncore, DMA_ADDR_0_LOW, lower_32_bits(offset));
	intel_uncore_write(uncore, DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
175 176 177 178 179

	/*
	 * Set the DMA destination. Current uCode expects the code to be
	 * loaded at 8k; locations below this are used for the stack.
	 */
180 181
	intel_uncore_write(uncore, DMA_ADDR_1_LOW, 0x2000);
	intel_uncore_write(uncore, DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
182 183

	/* Finally start the DMA */
184 185
	intel_uncore_write(uncore, DMA_CTRL,
			   _MASKED_BIT_ENABLE(UOS_MOVE | START_DMA));
186

187
	return guc_wait_ucode(uncore);
188
}
189 190 191
/*
 * Load the GuC firmware blob into the MinuteIA.
 */
192
static int guc_fw_xfer(struct intel_uc_fw *guc_fw)
193
{
194
	struct intel_guc *guc = container_of(guc_fw, struct intel_guc, fw);
195
	struct intel_uncore *uncore = guc_to_gt(guc)->uncore;
196 197
	int ret;

198
	GEM_BUG_ON(guc_fw->type != INTEL_UC_FW_TYPE_GUC);
199

200
	intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
201

202
	guc_prepare_xfer(guc);
203

204 205 206 207 208
	/*
	 * Note that GuC needs the CSS header plus uKernel code to be copied
	 * by the DMA engine in one operation, whereas the RSA signature is
	 * loaded via MMIO.
	 */
209
	guc_xfer_rsa(guc);
210

211
	ret = guc_xfer_ucode(guc);
212

213
	intel_uncore_forcewake_put(uncore, FORCEWAKE_ALL);
214 215 216 217 218

	return ret;
}

/**
219
 * intel_guc_fw_upload() - load GuC uCode to device
220
 * @guc: intel_guc structure
221
 *
222 223
 * Called from intel_uc_init_hw() during driver load, resume from sleep and
 * after a GPU reset.
224
 *
225 226
 * The firmware image should have already been fetched into memory, so only
 * check that fetch succeeded, and then transfer the image to the h/w.
227 228 229
 *
 * Return:	non-zero code on error
 */
230
int intel_guc_fw_upload(struct intel_guc *guc)
231
{
232 233 234 235 236
	int ret = intel_uc_fw_upload(&guc->fw, guc_fw_xfer);
	if (!ret)
		guc->fw.status = INTEL_UC_FIRMWARE_RUNNING;

	return ret;
237
}