blob: fa3f0002a2dd4b5cbaf88c572773134074721bc4 [file] [log] [blame]
Varun Wadekar3c959932016-03-03 13:09:08 -08001/*
Pritesh Raithatha9eb5db52017-01-02 19:42:31 +05302 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
Varun Wadekar3c959932016-03-03 13:09:08 -08003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekar3c959932016-03-03 13:09:08 -08005 */
6
7#include <assert.h>
Varun Wadekar66ff0122016-04-26 11:34:54 -07008#include <bl_common.h>
Varun Wadekar3c959932016-03-03 13:09:08 -08009#include <debug.h>
Varun Wadekar93bed2a2016-03-18 13:07:33 -070010#include <platform_def.h>
Varun Wadekar3c959932016-03-03 13:09:08 -080011#include <smmu.h>
Varun Wadekar93bed2a2016-03-18 13:07:33 -070012#include <string.h>
13#include <tegra_private.h>
Varun Wadekar3c959932016-03-03 13:09:08 -080014
Pritesh Raithatha0de6e532017-01-24 13:49:46 +053015/* SMMU IDs currently supported by the driver */
16enum {
17 TEGRA_SMMU0,
18 TEGRA_SMMU1,
19 TEGRA_SMMU2
20};
21
22static uint32_t tegra_smmu_read_32(uint32_t smmu_id, uint32_t off)
23{
24#if defined(TEGRA_SMMU0_BASE)
25 if (smmu_id == TEGRA_SMMU0)
26 return mmio_read_32(TEGRA_SMMU0_BASE + off);
27#endif
28
29#if defined(TEGRA_SMMU1_BASE)
30 if (smmu_id == TEGRA_SMMU1)
31 return mmio_read_32(TEGRA_SMMU1_BASE + off);
32#endif
33
34#if defined(TEGRA_SMMU2_BASE)
35 if (smmu_id == TEGRA_SMMU2)
36 return mmio_read_32(TEGRA_SMMU2_BASE + off);
37#endif
38
39 return 0;
40}
41
42static void tegra_smmu_write_32(uint32_t smmu_id,
43 uint32_t off, uint32_t val)
44{
45#if defined(TEGRA_SMMU0_BASE)
46 if (smmu_id == TEGRA_SMMU0)
47 mmio_write_32(TEGRA_SMMU0_BASE + off, val);
48#endif
49
50#if defined(TEGRA_SMMU1_BASE)
51 if (smmu_id == TEGRA_SMMU1)
52 mmio_write_32(TEGRA_SMMU1_BASE + off, val);
53#endif
54
55#if defined(TEGRA_SMMU2_BASE)
56 if (smmu_id == TEGRA_SMMU2)
57 mmio_write_32(TEGRA_SMMU2_BASE + off, val);
58#endif
59}
60
Varun Wadekar3c959932016-03-03 13:09:08 -080061/*
Varun Wadekar93bed2a2016-03-18 13:07:33 -070062 * Save SMMU settings before "System Suspend" to TZDRAM
Varun Wadekar3c959932016-03-03 13:09:08 -080063 */
Varun Wadekar93bed2a2016-03-18 13:07:33 -070064void tegra_smmu_save_context(uint64_t smmu_ctx_addr)
Varun Wadekar3c959932016-03-03 13:09:08 -080065{
Varun Wadekar27155fc2017-04-20 18:56:09 -070066 uint32_t i, num_entries = 0;
Pritesh Raithathac88654f2017-01-02 20:11:32 +053067 smmu_regs_t *smmu_ctx_regs;
Varun Wadekar93bed2a2016-03-18 13:07:33 -070068 plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
69 uint64_t tzdram_base = params_from_bl2->tzdram_base;
70 uint64_t tzdram_end = tzdram_base + params_from_bl2->tzdram_size;
Varun Wadekar3c959932016-03-03 13:09:08 -080071 uint32_t reg_id1, pgshift, cb_size;
72
73 /* sanity check SMMU settings c*/
Pritesh Raithatha0de6e532017-01-24 13:49:46 +053074 reg_id1 = mmio_read_32((TEGRA_SMMU0_BASE + SMMU_GNSR0_IDR1));
Varun Wadekar3c959932016-03-03 13:09:08 -080075 pgshift = (reg_id1 & ID1_PAGESIZE) ? 16 : 12;
76 cb_size = (2 << pgshift) * \
77 (1 << (((reg_id1 >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1));
78
79 assert(!((pgshift != PGSHIFT) || (cb_size != CB_SIZE)));
Varun Wadekar93bed2a2016-03-18 13:07:33 -070080 assert((smmu_ctx_addr >= tzdram_base) && (smmu_ctx_addr <= tzdram_end));
81
Pritesh Raithathac88654f2017-01-02 20:11:32 +053082 /* get SMMU context table */
83 smmu_ctx_regs = plat_get_smmu_ctx();
84 assert(smmu_ctx_regs);
Varun Wadekar3c959932016-03-03 13:09:08 -080085
Varun Wadekar27155fc2017-04-20 18:56:09 -070086 /*
87 * smmu_ctx_regs[0].val contains the size of the context table minus
88 * the last entry. Sanity check the table size before we start with
89 * the context save operation.
90 */
91 while (smmu_ctx_regs[num_entries].val != 0xFFFFFFFFU) {
92 num_entries++;
93 }
94
95 /* panic if the sizes do not match */
96 if (num_entries != smmu_ctx_regs[0].val)
97 panic();
98
Varun Wadekar3c959932016-03-03 13:09:08 -080099 /* save SMMU register values */
Varun Wadekar27155fc2017-04-20 18:56:09 -0700100 for (i = 1; i < num_entries; i++)
Varun Wadekar3c959932016-03-03 13:09:08 -0800101 smmu_ctx_regs[i].val = mmio_read_32(smmu_ctx_regs[i].reg);
102
Varun Wadekar27155fc2017-04-20 18:56:09 -0700103 /* increment by 1 to take care of the last entry */
104 num_entries++;
105
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700106 /* Save SMMU config settings */
107 memcpy16((void *)(uintptr_t)smmu_ctx_addr, (void *)smmu_ctx_regs,
Varun Wadekar27155fc2017-04-20 18:56:09 -0700108 (sizeof(smmu_regs_t) * num_entries));
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700109
Varun Wadekar3c959932016-03-03 13:09:08 -0800110 /* save the SMMU table address */
111 mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV11_LO,
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700112 (uint32_t)smmu_ctx_addr);
Varun Wadekar3c959932016-03-03 13:09:08 -0800113 mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV11_HI,
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700114 (uint32_t)(smmu_ctx_addr >> 32));
Varun Wadekar3c959932016-03-03 13:09:08 -0800115}
116
Varun Wadekarea709c32016-04-20 17:14:15 -0700117#define SMMU_NUM_CONTEXTS 64
118#define SMMU_CONTEXT_BANK_MAX_IDX 64
119
Varun Wadekar3c959932016-03-03 13:09:08 -0800120/*
121 * Init SMMU during boot or "System Suspend" exit
122 */
123void tegra_smmu_init(void)
124{
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530125 uint32_t val, cb_idx, smmu_id, ctx_base;
Varun Wadekar3c959932016-03-03 13:09:08 -0800126
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530127 for (smmu_id = 0; smmu_id < NUM_SMMU_DEVICES; smmu_id++) {
128 /* Program the SMMU pagesize and reset CACHE_LOCK bit */
129 val = tegra_smmu_read_32(smmu_id, SMMU_GSR0_SECURE_ACR);
130 val |= SMMU_GSR0_PGSIZE_64K;
131 val &= ~SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
132 tegra_smmu_write_32(smmu_id, SMMU_GSR0_SECURE_ACR, val);
Varun Wadekarea709c32016-04-20 17:14:15 -0700133
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530134 /* reset CACHE LOCK bit for NS Aux. Config. Register */
135 val = tegra_smmu_read_32(smmu_id, SMMU_GNSR_ACR);
136 val &= ~SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
137 tegra_smmu_write_32(smmu_id, SMMU_GNSR_ACR, val);
Varun Wadekarea709c32016-04-20 17:14:15 -0700138
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530139 /* disable TCU prefetch for all contexts */
140 ctx_base = (SMMU_GSR0_PGSIZE_64K * SMMU_NUM_CONTEXTS)
141 + SMMU_CBn_ACTLR;
142 for (cb_idx = 0; cb_idx < SMMU_CONTEXT_BANK_MAX_IDX; cb_idx++) {
143 val = tegra_smmu_read_32(smmu_id,
144 ctx_base + (SMMU_GSR0_PGSIZE_64K * cb_idx));
145 val &= ~SMMU_CBn_ACTLR_CPRE_BIT;
146 tegra_smmu_write_32(smmu_id, ctx_base +
147 (SMMU_GSR0_PGSIZE_64K * cb_idx), val);
148 }
Varun Wadekarea709c32016-04-20 17:14:15 -0700149
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530150 /* set CACHE LOCK bit for NS Aux. Config. Register */
151 val = tegra_smmu_read_32(smmu_id, SMMU_GNSR_ACR);
152 val |= SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
153 tegra_smmu_write_32(smmu_id, SMMU_GNSR_ACR, val);
Varun Wadekarea709c32016-04-20 17:14:15 -0700154
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530155 /* set CACHE LOCK bit for S Aux. Config. Register */
156 val = tegra_smmu_read_32(smmu_id, SMMU_GSR0_SECURE_ACR);
157 val |= SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
158 tegra_smmu_write_32(smmu_id, SMMU_GSR0_SECURE_ACR, val);
159 }
Varun Wadekar3c959932016-03-03 13:09:08 -0800160}