mce.c 14.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
 */

7
8
9
10
#include <assert.h>
#include <errno.h>
#include <string.h>

11
12
#include <arch.h>
#include <arch_helpers.h>
13
14
#include <common/bl_common.h>
#include <common/debug.h>
15
16
#include <context.h>
#include <denver.h>
17
18
19
#include <lib/el3_runtime/context_mgmt.h>
#include <lib/mmio.h>

20
#include <mce.h>
21
#include <mce_private.h>
22
23
#include <t18x_ari.h>
#include <tegra_def.h>
24
#include <tegra_platform.h>
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

/* NVG functions handlers */
static arch_mce_ops_t nvg_mce_ops = {
	.enter_cstate = nvg_enter_cstate,
	.update_cstate_info = nvg_update_cstate_info,
	.update_crossover_time = nvg_update_crossover_time,
	.read_cstate_stats = nvg_read_cstate_stats,
	.write_cstate_stats = nvg_write_cstate_stats,
	.call_enum_misc = ari_enumeration_misc,
	.is_ccx_allowed = nvg_is_ccx_allowed,
	.is_sc7_allowed = nvg_is_sc7_allowed,
	.online_core = nvg_online_core,
	.cc3_ctrl = nvg_cc3_ctrl,
	.update_reset_vector = ari_reset_vector_update,
	.roc_flush_cache = ari_roc_flush_cache,
	.roc_flush_cache_trbits = ari_roc_flush_cache_trbits,
	.roc_clean_cache = ari_roc_clean_cache,
	.read_write_mca = ari_read_write_mca,
	.update_ccplex_gsc = ari_update_ccplex_gsc,
44
	.enter_ccplex_state = ari_enter_ccplex_state,
45
46
	.read_write_uncore_perfmon = ari_read_write_uncore_perfmon,
	.misc_ccplex = ari_misc_ccplex
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
};

/* ARI functions handlers */
static arch_mce_ops_t ari_mce_ops = {
	.enter_cstate = ari_enter_cstate,
	.update_cstate_info = ari_update_cstate_info,
	.update_crossover_time = ari_update_crossover_time,
	.read_cstate_stats = ari_read_cstate_stats,
	.write_cstate_stats = ari_write_cstate_stats,
	.call_enum_misc = ari_enumeration_misc,
	.is_ccx_allowed = ari_is_ccx_allowed,
	.is_sc7_allowed = ari_is_sc7_allowed,
	.online_core = ari_online_core,
	.cc3_ctrl = ari_cc3_ctrl,
	.update_reset_vector = ari_reset_vector_update,
	.roc_flush_cache = ari_roc_flush_cache,
	.roc_flush_cache_trbits = ari_roc_flush_cache_trbits,
	.roc_clean_cache = ari_roc_clean_cache,
	.read_write_mca = ari_read_write_mca,
	.update_ccplex_gsc = ari_update_ccplex_gsc,
67
	.enter_ccplex_state = ari_enter_ccplex_state,
68
69
	.read_write_uncore_perfmon = ari_read_write_uncore_perfmon,
	.misc_ccplex = ari_misc_ccplex
70
71
};

72
typedef struct {
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
	uint32_t ari_base;
	arch_mce_ops_t *ops;
} mce_config_t;

/* Table to hold the per-CPU ARI base address and function handlers */
static mce_config_t mce_cfg_table[MCE_ARI_APERTURES_MAX] = {
	{
		/* A57 Core 0 */
		.ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_0_OFFSET,
		.ops = &ari_mce_ops,
	},
	{
		/* A57 Core 1 */
		.ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_1_OFFSET,
		.ops = &ari_mce_ops,
	},
	{
		/* A57 Core 2 */
		.ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_2_OFFSET,
		.ops = &ari_mce_ops,
	},
	{
		/* A57 Core 3 */
		.ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_3_OFFSET,
		.ops = &ari_mce_ops,
	},
	{
		/* D15 Core 0 */
		.ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_4_OFFSET,
		.ops = &nvg_mce_ops,
	},
	{
		/* D15 Core 1 */
		.ari_base = TEGRA_MMCRAB_BASE + MCE_ARI_APERTURE_5_OFFSET,
		.ops = &nvg_mce_ops,
	}
};

static uint32_t mce_get_curr_cpu_ari_base(void)
{
113
114
115
	uint64_t mpidr = read_mpidr();
	uint64_t cpuid = mpidr & (uint64_t)MPIDR_CPU_MASK;
	uint64_t impl = (read_midr() >> (uint64_t)MIDR_IMPL_SHIFT) & (uint64_t)MIDR_IMPL_MASK;
116
117
118
119
120
121
122
123

	/*
	 * T186 has 2 CPU clusters, one with Denver CPUs and the other with
	 * ARM CortexA-57 CPUs. Each cluster consists of 4 CPUs and the CPU
	 * numbers start from 0. In order to get the proper arch_mce_ops_t
	 * struct, we have to convert the Denver CPU ids to the corresponding
	 * indices in the mce_ops_table array.
	 */
124
125
126
	if (impl == DENVER_IMPL) {
		cpuid |= 0x4U;
	}
127
128
129
130
131
132

	return mce_cfg_table[cpuid].ari_base;
}

static arch_mce_ops_t *mce_get_curr_cpu_ops(void)
{
133
134
135
136
	uint64_t mpidr = read_mpidr();
	uint64_t cpuid = mpidr & (uint64_t)MPIDR_CPU_MASK;
	uint64_t impl = (read_midr() >> (uint64_t)MIDR_IMPL_SHIFT) &
			(uint64_t)MIDR_IMPL_MASK;
137
138
139
140
141
142
143
144

	/*
	 * T186 has 2 CPU clusters, one with Denver CPUs and the other with
	 * ARM CortexA-57 CPUs. Each cluster consists of 4 CPUs and the CPU
	 * numbers start from 0. In order to get the proper arch_mce_ops_t
	 * struct, we have to convert the Denver CPU ids to the corresponding
	 * indices in the mce_ops_table array.
	 */
145
146
147
	if (impl == DENVER_IMPL) {
		cpuid |= 0x4U;
	}
148
149
150
151
152
153
154

	return mce_cfg_table[cpuid].ops;
}

/*******************************************************************************
 * Common handler for all MCE commands
 ******************************************************************************/
155
int32_t mce_command_handler(uint64_t cmd, uint64_t arg0, uint64_t arg1,
156
157
			uint64_t arg2)
{
158
159
	const arch_mce_ops_t *ops;
	gp_regs_t *gp_regs = get_gpregs_ctx(cm_get_context(NON_SECURE));
160
161
	uint32_t cpu_ari_base;
	uint64_t ret64 = 0, arg3, arg4, arg5;
162
	int32_t ret = 0;
163

164
	assert(gp_regs != NULL);
165
166
167
168
169
170
171
172
173
174

	/* get a pointer to the CPU's arch_mce_ops_t struct */
	ops = mce_get_curr_cpu_ops();

	/* get the CPU's ARI base address */
	cpu_ari_base = mce_get_curr_cpu_ari_base();

	switch (cmd) {
	case MCE_CMD_ENTER_CSTATE:
		ret = ops->enter_cstate(cpu_ari_base, arg0, arg1);
175
		if (ret < 0) {
176
			ERROR("%s: enter_cstate failed(%d)\n", __func__, ret);
177
		}
178
179
180
181
182
183
184
185

		break;

	case MCE_CMD_UPDATE_CSTATE_INFO:
		/*
		 * get the parameters required for the update cstate info
		 * command
		 */
186
187
188
		arg3 = read_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X4));
		arg4 = read_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X5));
		arg5 = read_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X6));
189
190
191
192

		ret = ops->update_cstate_info(cpu_ari_base, (uint32_t)arg0,
				(uint32_t)arg1, (uint32_t)arg2, (uint8_t)arg3,
				(uint32_t)arg4, (uint8_t)arg5);
193
		if (ret < 0) {
194
195
			ERROR("%s: update_cstate_info failed(%d)\n",
				__func__, ret);
196
		}
197

198
199
200
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X4), (0));
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X5), (0));
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X6), (0));
201
202
203
204
205

		break;

	case MCE_CMD_UPDATE_CROSSOVER_TIME:
		ret = ops->update_crossover_time(cpu_ari_base, arg0, arg1);
206
		if (ret < 0) {
207
208
			ERROR("%s: update_crossover_time failed(%d)\n",
				__func__, ret);
209
		}
210
211
212
213
214
215
216

		break;

	case MCE_CMD_READ_CSTATE_STATS:
		ret64 = ops->read_cstate_stats(cpu_ari_base, arg0);

		/* update context to return cstate stats value */
217
218
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X1), (ret64));
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X2), (ret64));
219
220
221
222
223

		break;

	case MCE_CMD_WRITE_CSTATE_STATS:
		ret = ops->write_cstate_stats(cpu_ari_base, arg0, arg1);
224
		if (ret < 0) {
225
226
			ERROR("%s: write_cstate_stats failed(%d)\n",
				__func__, ret);
227
		}
228
229
230
231
232
233
234
235
236
237
238

		break;

	case MCE_CMD_IS_CCX_ALLOWED:
		ret = ops->is_ccx_allowed(cpu_ari_base, arg0, arg1);
		if (ret < 0) {
			ERROR("%s: is_ccx_allowed failed(%d)\n", __func__, ret);
			break;
		}

		/* update context to return CCx status value */
239
240
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X1),
			      (uint64_t)(ret));
241
242
243
244
245
246
247
248
249
250
251

		break;

	case MCE_CMD_IS_SC7_ALLOWED:
		ret = ops->is_sc7_allowed(cpu_ari_base, arg0, arg1);
		if (ret < 0) {
			ERROR("%s: is_sc7_allowed failed(%d)\n", __func__, ret);
			break;
		}

		/* update context to return SC7 status value */
252
253
254
255
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X1),
			      (uint64_t)(ret));
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X3),
			      (uint64_t)(ret));
256
257
258
259
260

		break;

	case MCE_CMD_ONLINE_CORE:
		ret = ops->online_core(cpu_ari_base, arg0);
261
		if (ret < 0) {
262
			ERROR("%s: online_core failed(%d)\n", __func__, ret);
263
		}
264
265
266
267
268

		break;

	case MCE_CMD_CC3_CTRL:
		ret = ops->cc3_ctrl(cpu_ari_base, arg0, arg1, arg2);
269
		if (ret < 0) {
270
			ERROR("%s: cc3_ctrl failed(%d)\n", __func__, ret);
271
		}
272
273
274
275
276
277
278
279

		break;

	case MCE_CMD_ECHO_DATA:
		ret64 = ops->call_enum_misc(cpu_ari_base, TEGRA_ARI_MISC_ECHO,
				arg0);

		/* update context to return if echo'd data matched source */
280
281
282
283
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X1),
			      ((ret64 == arg0) ? 1ULL : 0ULL));
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X2),
			      ((ret64 == arg0) ? 1ULL : 0ULL));
284
285
286
287
288
289
290
291
292
293
294

		break;

	case MCE_CMD_READ_VERSIONS:
		ret64 = ops->call_enum_misc(cpu_ari_base, TEGRA_ARI_MISC_VERSION,
			arg0);

		/*
		 * version = minor(63:32) | major(31:0). Update context
		 * to return major and minor version number.
		 */
295
296
297
298
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X1),
			      (ret64));
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X2),
			      (ret64 >> 32ULL));
299
300
301
302

		break;

	case MCE_CMD_ENUM_FEATURES:
303
		ret64 = ops->call_enum_misc(cpu_ari_base,
304
305
306
				TEGRA_ARI_MISC_FEATURE_LEAF_0, arg0);

		/* update context to return features value */
307
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X1), (ret64));
308
309
310
311
312

		break;

	case MCE_CMD_ROC_FLUSH_CACHE_TRBITS:
		ret = ops->roc_flush_cache_trbits(cpu_ari_base);
313
		if (ret < 0) {
314
315
			ERROR("%s: flush cache_trbits failed(%d)\n", __func__,
				ret);
316
		}
317
318
319
320
321

		break;

	case MCE_CMD_ROC_FLUSH_CACHE:
		ret = ops->roc_flush_cache(cpu_ari_base);
322
		if (ret < 0) {
323
			ERROR("%s: flush cache failed(%d)\n", __func__, ret);
324
		}
325
326
327
328
329

		break;

	case MCE_CMD_ROC_CLEAN_CACHE:
		ret = ops->roc_clean_cache(cpu_ari_base);
330
		if (ret < 0) {
331
			ERROR("%s: clean cache failed(%d)\n", __func__, ret);
332
		}
333
334
335
336

		break;

	case MCE_CMD_ENUM_READ_MCA:
337
		ret64 = ops->read_write_mca(cpu_ari_base, arg0, &arg1);
338
339

		/* update context to return MCA data/error */
340
341
342
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X1), (ret64));
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X2), (arg1));
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X3), (ret64));
343
344
345
346

		break;

	case MCE_CMD_ENUM_WRITE_MCA:
347
		ret64 = ops->read_write_mca(cpu_ari_base, arg0, &arg1);
348
349

		/* update context to return MCA error */
350
351
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X1), (ret64));
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X3), (ret64));
352
353
354

		break;

355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#if ENABLE_CHIP_VERIFICATION_HARNESS
	case MCE_CMD_ENABLE_LATIC:
		/*
		 * This call is not for production use. The constant value,
		 * 0xFFFF0000, is specific to allowing for enabling LATIC on
		 * pre-production parts for the chip verification harness.
		 *
		 * Enabling LATIC allows S/W to read the MINI ISPs in the
		 * CCPLEX. The ISMs are used for various measurements relevant
		 * to particular locations in the Silicon. They are small
		 * counters which can be polled to determine how fast a
		 * particular location in the Silicon is.
		 */
		ops->enter_ccplex_state(mce_get_curr_cpu_ari_base(),
			0xFFFF0000);

		break;
#endif
373
374

	case MCE_CMD_UNCORE_PERFMON_REQ:
375
		ret = ops->read_write_uncore_perfmon(cpu_ari_base, arg0, &arg1);
376
377

		/* update context to return data */
378
		write_ctx_reg((gp_regs), (uint32_t)(CTX_GPREG_X1), (arg1));
379
380
		break;

381
382
383
384
385
	case MCE_CMD_MISC_CCPLEX:
		ops->misc_ccplex(cpu_ari_base, arg0, arg1);

		break;

386
	default:
387
		ERROR("unknown MCE command (%llu)\n", cmd);
388
389
		ret = EINVAL;
		break;
390
391
392
393
394
395
396
397
	}

	return ret;
}

/*******************************************************************************
 * Handler to update the reset vector for CPUs
 ******************************************************************************/
398
int32_t mce_update_reset_vector(void)
399
{
400
	const arch_mce_ops_t *ops = mce_get_curr_cpu_ops();
401

402
	ops->update_reset_vector(mce_get_curr_cpu_ari_base());
403
404
405
406

	return 0;
}

407
static int32_t mce_update_ccplex_gsc(tegra_ari_gsc_index_t gsc_idx)
408
{
409
	const arch_mce_ops_t *ops = mce_get_curr_cpu_ops();
410
411
412
413
414
415
416
417
418

	ops->update_ccplex_gsc(mce_get_curr_cpu_ari_base(), gsc_idx);

	return 0;
}

/*******************************************************************************
 * Handler to update carveout values for Video Memory Carveout region
 ******************************************************************************/
419
int32_t mce_update_gsc_videomem(void)
420
421
422
423
424
425
426
{
	return mce_update_ccplex_gsc(TEGRA_ARI_GSC_VPR_IDX);
}

/*******************************************************************************
 * Handler to update carveout values for TZDRAM aperture
 ******************************************************************************/
427
int32_t mce_update_gsc_tzdram(void)
428
429
430
431
432
433
434
{
	return mce_update_ccplex_gsc(TEGRA_ARI_GSC_TZ_DRAM_IDX);
}

/*******************************************************************************
 * Handler to update carveout values for TZ SysRAM aperture
 ******************************************************************************/
435
int32_t mce_update_gsc_tzram(void)
436
437
438
439
440
441
442
443
444
{
	return mce_update_ccplex_gsc(TEGRA_ARI_GSC_TZRAM);
}

/*******************************************************************************
 * Handler to shutdown/reset the entire system
 ******************************************************************************/
__dead2 void mce_enter_ccplex_state(uint32_t state_idx)
{
445
	const arch_mce_ops_t *ops = mce_get_curr_cpu_ops();
446
447

	/* sanity check state value */
448
449
	if ((state_idx != TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_POWER_OFF) &&
	    (state_idx != TEGRA_ARI_MISC_CCPLEX_SHUTDOWN_REBOOT)) {
450
		panic();
451
	}
452
453
454
455

	ops->enter_ccplex_state(mce_get_curr_cpu_ari_base(), state_idx);

	/* wait till the CCPLEX powers down */
456
	for (;;) {
457
		;
458
	}
459
460

}
461

462
463
464
/*******************************************************************************
 * Handler to issue the UPDATE_CSTATE_INFO request
 ******************************************************************************/
465
void mce_update_cstate_info(const mce_cstate_info_t *cstate)
466
{
467
	const arch_mce_ops_t *ops = mce_get_curr_cpu_ops();
468
469
470
471
472
473
474

	/* issue the UPDATE_CSTATE_INFO request */
	ops->update_cstate_info(mce_get_curr_cpu_ari_base(), cstate->cluster,
		cstate->ccplex, cstate->system, cstate->system_state_force,
		cstate->wake_mask, cstate->update_wake_mask);
}

475
476
477
478
479
480
/*******************************************************************************
 * Handler to read the MCE firmware version and check if it is compatible
 * with interface header the BL3-1 was compiled against
 ******************************************************************************/
void mce_verify_firmware_version(void)
{
481
	const arch_mce_ops_t *ops;
482
483
	uint32_t cpu_ari_base;
	uint64_t version;
484
485
486
	uint32_t major, minor;

	/*
487
	 * MCE firmware is not supported on simulation platforms.
488
	 */
489
	if (tegra_platform_is_emulation()) {
490

491
		INFO("MCE firmware is not supported\n");
492

493
494
495
	} else {
		/* get a pointer to the CPU's arch_mce_ops_t struct */
		ops = mce_get_curr_cpu_ops();
496

497
498
		/* get the CPU's ARI base address */
		cpu_ari_base = mce_get_curr_cpu_ari_base();
499

500
501
502
503
504
505
506
		/*
		 * Read the MCE firmware version and extract the major and minor
		 * version fields
		 */
		version = ops->call_enum_misc(cpu_ari_base, TEGRA_ARI_MISC_VERSION, 0);
		major = (uint32_t)version;
		minor = (uint32_t)(version >> 32);
507

508
509
		INFO("MCE Version - HW=%d:%d, SW=%d:%d\n", major, minor,
			TEGRA_ARI_VERSION_MAJOR, TEGRA_ARI_VERSION_MINOR);
510

511
512
513
514
515
516
517
518
519
520
521
522
523
		/*
		 * Verify that the MCE firmware version and the interface header
		 * match
		 */
		if (major != TEGRA_ARI_VERSION_MAJOR) {
			ERROR("ARI major version mismatch\n");
			panic();
		}

		if (minor < TEGRA_ARI_VERSION_MINOR) {
			ERROR("ARI minor version mismatch\n");
			panic();
		}
524
525
	}
}