smmu.c 4.86 KB
Newer Older
1
/*
2
 * Copyright (c) 2016-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
 */

#include <assert.h>
8
9
#include <string.h>

10
#include <platform_def.h>
11
12
13
14

#include <common/bl_common.h>
#include <common/debug.h>

15
#include <smmu.h>
16
#include <tegra_private.h>
17

18
19
extern void memcpy16(void *dest, const void *src, unsigned int length);

20
21
/* SMMU IDs currently supported by the driver */
enum {
22
	TEGRA_SMMU0 = 0U,
23
24
25
26
27
28
	TEGRA_SMMU1,
	TEGRA_SMMU2
};

static uint32_t tegra_smmu_read_32(uint32_t smmu_id, uint32_t off)
{
29
30
	uint32_t ret = 0U;

31
#if defined(TEGRA_SMMU0_BASE)
32
33
34
	if (smmu_id == TEGRA_SMMU0) {
		ret = mmio_read_32(TEGRA_SMMU0_BASE + (uint64_t)off);
	}
35
36
37
#endif

#if defined(TEGRA_SMMU1_BASE)
38
39
40
	if (smmu_id == TEGRA_SMMU1) {
		ret = mmio_read_32(TEGRA_SMMU1_BASE + (uint64_t)off);
	}
41
42
43
#endif

#if defined(TEGRA_SMMU2_BASE)
44
45
46
	if (smmu_id == TEGRA_SMMU2) {
		ret = mmio_read_32(TEGRA_SMMU2_BASE + (uint64_t)off);
	}
47
48
#endif

49
	return ret;
50
51
52
53
54
55
}

static void tegra_smmu_write_32(uint32_t smmu_id,
			uint32_t off, uint32_t val)
{
#if defined(TEGRA_SMMU0_BASE)
56
57
58
	if (smmu_id == TEGRA_SMMU0) {
		mmio_write_32(TEGRA_SMMU0_BASE + (uint64_t)off, val);
	}
59
60
61
#endif

#if defined(TEGRA_SMMU1_BASE)
62
63
64
	if (smmu_id == TEGRA_SMMU1) {
		mmio_write_32(TEGRA_SMMU1_BASE + (uint64_t)off, val);
	}
65
66
67
#endif

#if defined(TEGRA_SMMU2_BASE)
68
69
70
	if (smmu_id == TEGRA_SMMU2) {
		mmio_write_32(TEGRA_SMMU2_BASE + (uint64_t)off, val);
	}
71
72
73
#endif
}

74
/*
75
 * Save SMMU settings before "System Suspend" to TZDRAM
76
 */
77
void tegra_smmu_save_context(uint64_t smmu_ctx_addr)
78
{
79
	uint32_t i, num_entries = 0;
80
	smmu_regs_t *smmu_ctx_regs;
81
	const plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
82
83
	uint64_t tzdram_base = params_from_bl2->tzdram_base;
	uint64_t tzdram_end = tzdram_base + params_from_bl2->tzdram_size;
84
85
86
	uint32_t reg_id1, pgshift, cb_size;

	/* sanity check SMMU settings c*/
87
	reg_id1 = mmio_read_32((TEGRA_SMMU0_BASE + SMMU_GNSR0_IDR1));
88
	pgshift = ((reg_id1 & ID1_PAGESIZE) != 0U) ? 16U : 12U;
89
90
	cb_size = ((uint32_t)2 << pgshift) * \
	((uint32_t)1 << (((reg_id1 >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1U));
91
92

	assert(!((pgshift != PGSHIFT) || (cb_size != CB_SIZE)));
93
94
	assert((smmu_ctx_addr >= tzdram_base) && (smmu_ctx_addr <= tzdram_end));

95
96
	/* get SMMU context table */
	smmu_ctx_regs = plat_get_smmu_ctx();
97
	assert(smmu_ctx_regs != NULL);
98

99
100
101
102
103
	/*
	 * smmu_ctx_regs[0].val contains the size of the context table minus
	 * the last entry. Sanity check the table size before we start with
	 * the context save operation.
	 */
104
	while ((smmu_ctx_regs[num_entries].reg != 0xFFFFFFFFU)) {
105
106
107
108
		num_entries++;
	}

	/* panic if the sizes do not match */
109
	if (num_entries != smmu_ctx_regs[0].val) {
110
		ERROR("SMMU context size mismatch!");
111
		panic();
112
	}
113

114
	/* save SMMU register values */
115
	for (i = 1U; i < num_entries; i++) {
116
		smmu_ctx_regs[i].val = mmio_read_32(smmu_ctx_regs[i].reg);
117
	}
118

119
120
121
	/* increment by 1 to take care of the last entry */
	num_entries++;

122
	/* Save SMMU config settings */
123
124
	(void)memcpy16((uint8_t *)smmu_ctx_addr, (uint8_t *)smmu_ctx_regs,
			(sizeof(smmu_regs_t) * num_entries));
125

126
	/* save the SMMU table address */
127
	mmio_write_32(TEGRA_SCRATCH_BASE + SCRATCH_SMMU_TABLE_ADDR_LO,
128
		(uint32_t)smmu_ctx_addr);
129
	mmio_write_32(TEGRA_SCRATCH_BASE + SCRATCH_SMMU_TABLE_ADDR_HI,
130
		(uint32_t)(smmu_ctx_addr >> 32));
131
132
}

133
134
135
#define SMMU_NUM_CONTEXTS		64
#define SMMU_CONTEXT_BANK_MAX_IDX	64

136
137
138
139
140
/*
 * Init SMMU during boot or "System Suspend" exit
 */
void tegra_smmu_init(void)
{
141
	uint32_t val, cb_idx, smmu_id, ctx_base;
142
143
	uint32_t smmu_counter = plat_get_num_smmu_devices();

144
	for (smmu_id = 0U; smmu_id < smmu_counter; smmu_id++) {
145
146
147
		/* Program the SMMU pagesize and reset CACHE_LOCK bit */
		val = tegra_smmu_read_32(smmu_id, SMMU_GSR0_SECURE_ACR);
		val |= SMMU_GSR0_PGSIZE_64K;
148
		val &= (uint32_t)~SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
149
150
151
152
		tegra_smmu_write_32(smmu_id, SMMU_GSR0_SECURE_ACR, val);

		/* reset CACHE LOCK bit for NS Aux. Config. Register */
		val = tegra_smmu_read_32(smmu_id, SMMU_GNSR_ACR);
153
		val &= (uint32_t)~SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
154
155
156
157
158
159
160
161
		tegra_smmu_write_32(smmu_id, SMMU_GNSR_ACR, val);

		/* disable TCU prefetch for all contexts */
		ctx_base = (SMMU_GSR0_PGSIZE_64K * SMMU_NUM_CONTEXTS)
				+ SMMU_CBn_ACTLR;
		for (cb_idx = 0; cb_idx < SMMU_CONTEXT_BANK_MAX_IDX; cb_idx++) {
			val = tegra_smmu_read_32(smmu_id,
				ctx_base + (SMMU_GSR0_PGSIZE_64K * cb_idx));
162
			val &= (uint32_t)~SMMU_CBn_ACTLR_CPRE_BIT;
163
164
165
166
167
168
			tegra_smmu_write_32(smmu_id, ctx_base +
				(SMMU_GSR0_PGSIZE_64K * cb_idx), val);
		}

		/* set CACHE LOCK bit for NS Aux. Config. Register */
		val = tegra_smmu_read_32(smmu_id, SMMU_GNSR_ACR);
169
		val |= (uint32_t)SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
170
171
172
173
		tegra_smmu_write_32(smmu_id, SMMU_GNSR_ACR, val);

		/* set CACHE LOCK bit for S Aux. Config. Register */
		val = tegra_smmu_read_32(smmu_id, SMMU_GSR0_SECURE_ACR);
174
		val |= (uint32_t)SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
175
		tegra_smmu_write_32(smmu_id, SMMU_GSR0_SECURE_ACR, val);
176
	}
177
}