smmu_v3.c 1.43 KB
Newer Older
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
1
/*
2
 * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
3
4
5
6
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

7
#include <common/debug.h>
8
#include <cdefs.h>
9
10
11
#include <drivers/arm/smmu_v3.h>
#include <lib/mmio.h>

12
13
/* SMMU poll number of retries */
#define SMMU_POLL_RETRY		1000000
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
14

15
16
static int __init smmuv3_poll(uintptr_t smmu_reg, uint32_t mask,
				uint32_t value)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
17
{
18
19
20
21
22
23
24
25
26
27
28
29
	uint32_t reg_val, retries = SMMU_POLL_RETRY;

	do {
		reg_val = mmio_read_32(smmu_reg);
		if ((reg_val & mask) == value)
			return 0;
	} while (--retries != 0U);

	ERROR("Failed to poll SMMUv3 register @%p\n", (void *)smmu_reg);
	ERROR("Read value 0x%x, expected 0x%x\n", reg_val,
		value == 0U ? reg_val & ~mask : reg_val | mask);
	return -1;
30
31
}

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
32
33
/*
 * Initialize the SMMU by invalidating all secure caches and TLBs.
34
35
 * Abort all incoming transactions in order to implement a default
 * deny policy on reset
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
36
 */
37
int __init smmuv3_init(uintptr_t smmu_base)
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
38
39
40
41
42
43
{
	/*
	 * Invalidation of secure caches and TLBs is required only if the SMMU
	 * supports secure state. If not, it's implementation defined as to how
	 * SMMU_S_INIT register is accessed.
	 */
44
45
	if ((mmio_read_32(smmu_base + SMMU_S_IDR1) &
			SMMU_S_IDR1_SECURE_IMPL) != 0U) {
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
46

47
48
		/* Initiate invalidation */
		mmio_write_32(smmu_base + SMMU_S_INIT, SMMU_S_INIT_INV_ALL);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
49

50
51
52
53
		/* Wait for global invalidation operation to finish */
		return smmuv3_poll(smmu_base + SMMU_S_INIT,
					SMMU_S_INIT_INV_ALL, 0U);
	}
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
54
55
	return 0;
}