Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 4b32e62 | 2018-08-16 16:52:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved. |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 10 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | |
| 12 | #include <common/bl_common.h> |
| 13 | #include <common/debug.h> |
| 14 | |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 15 | #include <smmu.h> |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 16 | #include <tegra_private.h> |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 17 | |
Antonio Nino Diaz | 4b32e62 | 2018-08-16 16:52:57 +0100 | [diff] [blame] | 18 | extern void memcpy16(void *dest, const void *src, unsigned int length); |
| 19 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 20 | /* SMMU IDs currently supported by the driver */ |
| 21 | enum { |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 22 | TEGRA_SMMU0 = 0U, |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 23 | TEGRA_SMMU1, |
| 24 | TEGRA_SMMU2 |
| 25 | }; |
| 26 | |
| 27 | static uint32_t tegra_smmu_read_32(uint32_t smmu_id, uint32_t off) |
| 28 | { |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 29 | uint32_t ret = 0U; |
| 30 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 31 | #if defined(TEGRA_SMMU0_BASE) |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 32 | if (smmu_id == TEGRA_SMMU0) { |
| 33 | ret = mmio_read_32(TEGRA_SMMU0_BASE + (uint64_t)off); |
| 34 | } |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | #if defined(TEGRA_SMMU1_BASE) |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 38 | if (smmu_id == TEGRA_SMMU1) { |
| 39 | ret = mmio_read_32(TEGRA_SMMU1_BASE + (uint64_t)off); |
| 40 | } |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #if defined(TEGRA_SMMU2_BASE) |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 44 | if (smmu_id == TEGRA_SMMU2) { |
| 45 | ret = mmio_read_32(TEGRA_SMMU2_BASE + (uint64_t)off); |
| 46 | } |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 47 | #endif |
| 48 | |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 49 | return ret; |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void tegra_smmu_write_32(uint32_t smmu_id, |
| 53 | uint32_t off, uint32_t val) |
| 54 | { |
| 55 | #if defined(TEGRA_SMMU0_BASE) |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 56 | if (smmu_id == TEGRA_SMMU0) { |
| 57 | mmio_write_32(TEGRA_SMMU0_BASE + (uint64_t)off, val); |
| 58 | } |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 59 | #endif |
| 60 | |
| 61 | #if defined(TEGRA_SMMU1_BASE) |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 62 | if (smmu_id == TEGRA_SMMU1) { |
| 63 | mmio_write_32(TEGRA_SMMU1_BASE + (uint64_t)off, val); |
| 64 | } |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 65 | #endif |
| 66 | |
| 67 | #if defined(TEGRA_SMMU2_BASE) |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 68 | if (smmu_id == TEGRA_SMMU2) { |
| 69 | mmio_write_32(TEGRA_SMMU2_BASE + (uint64_t)off, val); |
| 70 | } |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 71 | #endif |
| 72 | } |
| 73 | |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 74 | /* |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 75 | * Save SMMU settings before "System Suspend" to TZDRAM |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 76 | */ |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 77 | void tegra_smmu_save_context(uint64_t smmu_ctx_addr) |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 78 | { |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 79 | uint32_t i, num_entries = 0; |
Pritesh Raithatha | c88654f | 2017-01-02 20:11:32 +0530 | [diff] [blame] | 80 | smmu_regs_t *smmu_ctx_regs; |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 81 | const plat_params_from_bl2_t *params_from_bl2 = bl31_get_plat_params(); |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 82 | uint64_t tzdram_base = params_from_bl2->tzdram_base; |
| 83 | uint64_t tzdram_end = tzdram_base + params_from_bl2->tzdram_size; |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 84 | uint32_t reg_id1, pgshift, cb_size; |
| 85 | |
| 86 | /* sanity check SMMU settings c*/ |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 87 | reg_id1 = mmio_read_32((TEGRA_SMMU0_BASE + SMMU_GNSR0_IDR1)); |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 88 | pgshift = ((reg_id1 & ID1_PAGESIZE) != 0U) ? 16U : 12U; |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 89 | cb_size = ((uint32_t)2 << pgshift) * \ |
| 90 | ((uint32_t)1 << (((reg_id1 >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1U)); |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 91 | |
| 92 | assert(!((pgshift != PGSHIFT) || (cb_size != CB_SIZE))); |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 93 | assert((smmu_ctx_addr >= tzdram_base) && (smmu_ctx_addr <= tzdram_end)); |
| 94 | |
Pritesh Raithatha | c88654f | 2017-01-02 20:11:32 +0530 | [diff] [blame] | 95 | /* get SMMU context table */ |
| 96 | smmu_ctx_regs = plat_get_smmu_ctx(); |
Anthony Zhou | 4408e88 | 2017-07-07 14:29:51 +0800 | [diff] [blame] | 97 | assert(smmu_ctx_regs != NULL); |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 98 | |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 99 | /* |
| 100 | * smmu_ctx_regs[0].val contains the size of the context table minus |
| 101 | * the last entry. Sanity check the table size before we start with |
| 102 | * the context save operation. |
| 103 | */ |
Varun Wadekar | 4ede3a6 | 2017-11-08 14:03:16 -0800 | [diff] [blame] | 104 | while ((smmu_ctx_regs[num_entries].reg != 0xFFFFFFFFU)) { |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 105 | num_entries++; |
| 106 | } |
| 107 | |
| 108 | /* panic if the sizes do not match */ |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 109 | if (num_entries != smmu_ctx_regs[0].val) { |
Varun Wadekar | 4ede3a6 | 2017-11-08 14:03:16 -0800 | [diff] [blame] | 110 | ERROR("SMMU context size mismatch!"); |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 111 | panic(); |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 112 | } |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 113 | |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 114 | /* save SMMU register values */ |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 115 | for (i = 1U; i < num_entries; i++) { |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 116 | smmu_ctx_regs[i].val = mmio_read_32(smmu_ctx_regs[i].reg); |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 117 | } |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 118 | |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 119 | /* increment by 1 to take care of the last entry */ |
| 120 | num_entries++; |
| 121 | |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 122 | /* Save SMMU config settings */ |
Anthony Zhou | 0844b97 | 2017-06-28 16:35:54 +0800 | [diff] [blame] | 123 | (void)memcpy16((uint8_t *)smmu_ctx_addr, (uint8_t *)smmu_ctx_regs, |
| 124 | (sizeof(smmu_regs_t) * num_entries)); |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 125 | |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 126 | /* save the SMMU table address */ |
Steven Kao | 186485e | 2017-10-23 18:22:09 +0800 | [diff] [blame] | 127 | mmio_write_32(TEGRA_SCRATCH_BASE + SCRATCH_SMMU_TABLE_ADDR_LO, |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 128 | (uint32_t)smmu_ctx_addr); |
Steven Kao | 186485e | 2017-10-23 18:22:09 +0800 | [diff] [blame] | 129 | mmio_write_32(TEGRA_SCRATCH_BASE + SCRATCH_SMMU_TABLE_ADDR_HI, |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 130 | (uint32_t)(smmu_ctx_addr >> 32)); |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Varun Wadekar | ea709c3 | 2016-04-20 17:14:15 -0700 | [diff] [blame] | 133 | #define SMMU_NUM_CONTEXTS 64 |
| 134 | #define SMMU_CONTEXT_BANK_MAX_IDX 64 |
| 135 | |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 136 | /* |
| 137 | * Init SMMU during boot or "System Suspend" exit |
| 138 | */ |
| 139 | void tegra_smmu_init(void) |
| 140 | { |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 141 | uint32_t val, cb_idx, smmu_id, ctx_base; |
Steven Kao | 7fd30f5 | 2017-07-25 11:29:46 +0800 | [diff] [blame] | 142 | uint32_t smmu_counter = plat_get_num_smmu_devices(); |
| 143 | |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 144 | for (smmu_id = 0U; smmu_id < smmu_counter; smmu_id++) { |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 145 | /* Program the SMMU pagesize and reset CACHE_LOCK bit */ |
| 146 | val = tegra_smmu_read_32(smmu_id, SMMU_GSR0_SECURE_ACR); |
| 147 | val |= SMMU_GSR0_PGSIZE_64K; |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 148 | val &= (uint32_t)~SMMU_ACR_CACHE_LOCK_ENABLE_BIT; |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 149 | tegra_smmu_write_32(smmu_id, SMMU_GSR0_SECURE_ACR, val); |
Varun Wadekar | ea709c3 | 2016-04-20 17:14:15 -0700 | [diff] [blame] | 150 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 151 | /* reset CACHE LOCK bit for NS Aux. Config. Register */ |
| 152 | val = tegra_smmu_read_32(smmu_id, SMMU_GNSR_ACR); |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 153 | val &= (uint32_t)~SMMU_ACR_CACHE_LOCK_ENABLE_BIT; |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 154 | tegra_smmu_write_32(smmu_id, SMMU_GNSR_ACR, val); |
Varun Wadekar | ea709c3 | 2016-04-20 17:14:15 -0700 | [diff] [blame] | 155 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 156 | /* disable TCU prefetch for all contexts */ |
| 157 | ctx_base = (SMMU_GSR0_PGSIZE_64K * SMMU_NUM_CONTEXTS) |
| 158 | + SMMU_CBn_ACTLR; |
| 159 | for (cb_idx = 0; cb_idx < SMMU_CONTEXT_BANK_MAX_IDX; cb_idx++) { |
| 160 | val = tegra_smmu_read_32(smmu_id, |
| 161 | ctx_base + (SMMU_GSR0_PGSIZE_64K * cb_idx)); |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 162 | val &= (uint32_t)~SMMU_CBn_ACTLR_CPRE_BIT; |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 163 | tegra_smmu_write_32(smmu_id, ctx_base + |
| 164 | (SMMU_GSR0_PGSIZE_64K * cb_idx), val); |
| 165 | } |
Varun Wadekar | ea709c3 | 2016-04-20 17:14:15 -0700 | [diff] [blame] | 166 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 167 | /* set CACHE LOCK bit for NS Aux. Config. Register */ |
| 168 | val = tegra_smmu_read_32(smmu_id, SMMU_GNSR_ACR); |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 169 | val |= (uint32_t)SMMU_ACR_CACHE_LOCK_ENABLE_BIT; |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 170 | tegra_smmu_write_32(smmu_id, SMMU_GNSR_ACR, val); |
Varun Wadekar | ea709c3 | 2016-04-20 17:14:15 -0700 | [diff] [blame] | 171 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 172 | /* set CACHE LOCK bit for S Aux. Config. Register */ |
| 173 | val = tegra_smmu_read_32(smmu_id, SMMU_GSR0_SECURE_ACR); |
Anthony Zhou | 0e07e45 | 2017-07-26 17:16:54 +0800 | [diff] [blame] | 174 | val |= (uint32_t)SMMU_ACR_CACHE_LOCK_ENABLE_BIT; |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 175 | tegra_smmu_write_32(smmu_id, SMMU_GSR0_SECURE_ACR, val); |
| 176 | } |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 177 | } |