blob: 4b9edb622f8276c58f613e5536d5e82267eb4fbd [file] [log] [blame]
Varun Wadekar3c959932016-03-03 13:09:08 -08001/*
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +01002 * Copyright (c) 2016-2018, 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
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010015extern void memcpy16(void *dest, const void *src, unsigned int length);
16
Pritesh Raithatha0de6e532017-01-24 13:49:46 +053017/* SMMU IDs currently supported by the driver */
18enum {
19 TEGRA_SMMU0,
20 TEGRA_SMMU1,
21 TEGRA_SMMU2
22};
23
24static uint32_t tegra_smmu_read_32(uint32_t smmu_id, uint32_t off)
25{
26#if defined(TEGRA_SMMU0_BASE)
27 if (smmu_id == TEGRA_SMMU0)
28 return mmio_read_32(TEGRA_SMMU0_BASE + off);
29#endif
30
31#if defined(TEGRA_SMMU1_BASE)
32 if (smmu_id == TEGRA_SMMU1)
33 return mmio_read_32(TEGRA_SMMU1_BASE + off);
34#endif
35
36#if defined(TEGRA_SMMU2_BASE)
37 if (smmu_id == TEGRA_SMMU2)
38 return mmio_read_32(TEGRA_SMMU2_BASE + off);
39#endif
40
41 return 0;
42}
43
44static void tegra_smmu_write_32(uint32_t smmu_id,
45 uint32_t off, uint32_t val)
46{
47#if defined(TEGRA_SMMU0_BASE)
48 if (smmu_id == TEGRA_SMMU0)
49 mmio_write_32(TEGRA_SMMU0_BASE + off, val);
50#endif
51
52#if defined(TEGRA_SMMU1_BASE)
53 if (smmu_id == TEGRA_SMMU1)
54 mmio_write_32(TEGRA_SMMU1_BASE + off, val);
55#endif
56
57#if defined(TEGRA_SMMU2_BASE)
58 if (smmu_id == TEGRA_SMMU2)
59 mmio_write_32(TEGRA_SMMU2_BASE + off, val);
60#endif
61}
62
Varun Wadekar3c959932016-03-03 13:09:08 -080063/*
Varun Wadekar93bed2a2016-03-18 13:07:33 -070064 * Save SMMU settings before "System Suspend" to TZDRAM
Varun Wadekar3c959932016-03-03 13:09:08 -080065 */
Varun Wadekar93bed2a2016-03-18 13:07:33 -070066void tegra_smmu_save_context(uint64_t smmu_ctx_addr)
Varun Wadekar3c959932016-03-03 13:09:08 -080067{
Varun Wadekar27155fc2017-04-20 18:56:09 -070068 uint32_t i, num_entries = 0;
Pritesh Raithathac88654f2017-01-02 20:11:32 +053069 smmu_regs_t *smmu_ctx_regs;
Varun Wadekar93bed2a2016-03-18 13:07:33 -070070 plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params();
71 uint64_t tzdram_base = params_from_bl2->tzdram_base;
72 uint64_t tzdram_end = tzdram_base + params_from_bl2->tzdram_size;
Varun Wadekar3c959932016-03-03 13:09:08 -080073 uint32_t reg_id1, pgshift, cb_size;
74
75 /* sanity check SMMU settings c*/
Pritesh Raithatha0de6e532017-01-24 13:49:46 +053076 reg_id1 = mmio_read_32((TEGRA_SMMU0_BASE + SMMU_GNSR0_IDR1));
Varun Wadekar3c959932016-03-03 13:09:08 -080077 pgshift = (reg_id1 & ID1_PAGESIZE) ? 16 : 12;
78 cb_size = (2 << pgshift) * \
79 (1 << (((reg_id1 >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1));
80
81 assert(!((pgshift != PGSHIFT) || (cb_size != CB_SIZE)));
Varun Wadekar93bed2a2016-03-18 13:07:33 -070082 assert((smmu_ctx_addr >= tzdram_base) && (smmu_ctx_addr <= tzdram_end));
83
Pritesh Raithathac88654f2017-01-02 20:11:32 +053084 /* get SMMU context table */
85 smmu_ctx_regs = plat_get_smmu_ctx();
86 assert(smmu_ctx_regs);
Varun Wadekar3c959932016-03-03 13:09:08 -080087
Varun Wadekar27155fc2017-04-20 18:56:09 -070088 /*
89 * smmu_ctx_regs[0].val contains the size of the context table minus
90 * the last entry. Sanity check the table size before we start with
91 * the context save operation.
92 */
93 while (smmu_ctx_regs[num_entries].val != 0xFFFFFFFFU) {
94 num_entries++;
95 }
96
97 /* panic if the sizes do not match */
98 if (num_entries != smmu_ctx_regs[0].val)
99 panic();
100
Varun Wadekar3c959932016-03-03 13:09:08 -0800101 /* save SMMU register values */
Varun Wadekar27155fc2017-04-20 18:56:09 -0700102 for (i = 1; i < num_entries; i++)
Varun Wadekar3c959932016-03-03 13:09:08 -0800103 smmu_ctx_regs[i].val = mmio_read_32(smmu_ctx_regs[i].reg);
104
Varun Wadekar27155fc2017-04-20 18:56:09 -0700105 /* increment by 1 to take care of the last entry */
106 num_entries++;
107
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700108 /* Save SMMU config settings */
109 memcpy16((void *)(uintptr_t)smmu_ctx_addr, (void *)smmu_ctx_regs,
Varun Wadekar27155fc2017-04-20 18:56:09 -0700110 (sizeof(smmu_regs_t) * num_entries));
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700111
Varun Wadekar3c959932016-03-03 13:09:08 -0800112 /* save the SMMU table address */
113 mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV11_LO,
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700114 (uint32_t)smmu_ctx_addr);
Varun Wadekar3c959932016-03-03 13:09:08 -0800115 mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV11_HI,
Varun Wadekar93bed2a2016-03-18 13:07:33 -0700116 (uint32_t)(smmu_ctx_addr >> 32));
Varun Wadekar3c959932016-03-03 13:09:08 -0800117}
118
Varun Wadekarea709c32016-04-20 17:14:15 -0700119#define SMMU_NUM_CONTEXTS 64
120#define SMMU_CONTEXT_BANK_MAX_IDX 64
121
Varun Wadekar3c959932016-03-03 13:09:08 -0800122/*
123 * Init SMMU during boot or "System Suspend" exit
124 */
125void tegra_smmu_init(void)
126{
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530127 uint32_t val, cb_idx, smmu_id, ctx_base;
Varun Wadekar3c959932016-03-03 13:09:08 -0800128
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530129 for (smmu_id = 0; smmu_id < NUM_SMMU_DEVICES; smmu_id++) {
130 /* Program the SMMU pagesize and reset CACHE_LOCK bit */
131 val = tegra_smmu_read_32(smmu_id, SMMU_GSR0_SECURE_ACR);
132 val |= SMMU_GSR0_PGSIZE_64K;
133 val &= ~SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
134 tegra_smmu_write_32(smmu_id, SMMU_GSR0_SECURE_ACR, val);
Varun Wadekarea709c32016-04-20 17:14:15 -0700135
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530136 /* reset CACHE LOCK bit for NS Aux. Config. Register */
137 val = tegra_smmu_read_32(smmu_id, SMMU_GNSR_ACR);
138 val &= ~SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
139 tegra_smmu_write_32(smmu_id, SMMU_GNSR_ACR, val);
Varun Wadekarea709c32016-04-20 17:14:15 -0700140
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530141 /* disable TCU prefetch for all contexts */
142 ctx_base = (SMMU_GSR0_PGSIZE_64K * SMMU_NUM_CONTEXTS)
143 + SMMU_CBn_ACTLR;
144 for (cb_idx = 0; cb_idx < SMMU_CONTEXT_BANK_MAX_IDX; cb_idx++) {
145 val = tegra_smmu_read_32(smmu_id,
146 ctx_base + (SMMU_GSR0_PGSIZE_64K * cb_idx));
147 val &= ~SMMU_CBn_ACTLR_CPRE_BIT;
148 tegra_smmu_write_32(smmu_id, ctx_base +
149 (SMMU_GSR0_PGSIZE_64K * cb_idx), val);
150 }
Varun Wadekarea709c32016-04-20 17:14:15 -0700151
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530152 /* set CACHE LOCK bit for NS Aux. Config. Register */
153 val = tegra_smmu_read_32(smmu_id, SMMU_GNSR_ACR);
154 val |= SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
155 tegra_smmu_write_32(smmu_id, SMMU_GNSR_ACR, val);
Varun Wadekarea709c32016-04-20 17:14:15 -0700156
Pritesh Raithatha0de6e532017-01-24 13:49:46 +0530157 /* set CACHE LOCK bit for S Aux. Config. Register */
158 val = tegra_smmu_read_32(smmu_id, SMMU_GSR0_SECURE_ACR);
159 val |= SMMU_ACR_CACHE_LOCK_ENABLE_BIT;
160 tegra_smmu_write_32(smmu_id, SMMU_GSR0_SECURE_ACR, val);
161 }
Varun Wadekar3c959932016-03-03 13:09:08 -0800162}