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> |
Varun Wadekar | 66ff012 | 2016-04-26 11:34:54 -0700 | [diff] [blame] | 8 | #include <bl_common.h> |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 9 | #include <debug.h> |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 10 | #include <platform_def.h> |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 11 | #include <smmu.h> |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include <tegra_private.h> |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 14 | |
Antonio Nino Diaz | 4b32e62 | 2018-08-16 16:52:57 +0100 | [diff] [blame] | 15 | extern void memcpy16(void *dest, const void *src, unsigned int length); |
| 16 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 17 | /* SMMU IDs currently supported by the driver */ |
| 18 | enum { |
| 19 | TEGRA_SMMU0, |
| 20 | TEGRA_SMMU1, |
| 21 | TEGRA_SMMU2 |
| 22 | }; |
| 23 | |
| 24 | static 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 | |
| 44 | static 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 Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 63 | /* |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 64 | * Save SMMU settings before "System Suspend" to TZDRAM |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 65 | */ |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 66 | void tegra_smmu_save_context(uint64_t smmu_ctx_addr) |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 67 | { |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 68 | uint32_t i, num_entries = 0; |
Pritesh Raithatha | c88654f | 2017-01-02 20:11:32 +0530 | [diff] [blame] | 69 | smmu_regs_t *smmu_ctx_regs; |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 70 | 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 Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 73 | uint32_t reg_id1, pgshift, cb_size; |
| 74 | |
| 75 | /* sanity check SMMU settings c*/ |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 76 | reg_id1 = mmio_read_32((TEGRA_SMMU0_BASE + SMMU_GNSR0_IDR1)); |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 77 | 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 Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 82 | assert((smmu_ctx_addr >= tzdram_base) && (smmu_ctx_addr <= tzdram_end)); |
| 83 | |
Pritesh Raithatha | c88654f | 2017-01-02 20:11:32 +0530 | [diff] [blame] | 84 | /* get SMMU context table */ |
| 85 | smmu_ctx_regs = plat_get_smmu_ctx(); |
| 86 | assert(smmu_ctx_regs); |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 87 | |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 88 | /* |
| 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 Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 101 | /* save SMMU register values */ |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 102 | for (i = 1; i < num_entries; i++) |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 103 | smmu_ctx_regs[i].val = mmio_read_32(smmu_ctx_regs[i].reg); |
| 104 | |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 105 | /* increment by 1 to take care of the last entry */ |
| 106 | num_entries++; |
| 107 | |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 108 | /* Save SMMU config settings */ |
| 109 | memcpy16((void *)(uintptr_t)smmu_ctx_addr, (void *)smmu_ctx_regs, |
Varun Wadekar | 27155fc | 2017-04-20 18:56:09 -0700 | [diff] [blame] | 110 | (sizeof(smmu_regs_t) * num_entries)); |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 111 | |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 112 | /* save the SMMU table address */ |
| 113 | mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV11_LO, |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 114 | (uint32_t)smmu_ctx_addr); |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 115 | mmio_write_32(TEGRA_SCRATCH_BASE + SECURE_SCRATCH_RSV11_HI, |
Varun Wadekar | 93bed2a | 2016-03-18 13:07:33 -0700 | [diff] [blame] | 116 | (uint32_t)(smmu_ctx_addr >> 32)); |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Varun Wadekar | ea709c3 | 2016-04-20 17:14:15 -0700 | [diff] [blame] | 119 | #define SMMU_NUM_CONTEXTS 64 |
| 120 | #define SMMU_CONTEXT_BANK_MAX_IDX 64 |
| 121 | |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 122 | /* |
| 123 | * Init SMMU during boot or "System Suspend" exit |
| 124 | */ |
| 125 | void tegra_smmu_init(void) |
| 126 | { |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 127 | uint32_t val, cb_idx, smmu_id, ctx_base; |
Varun Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 128 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 129 | 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 Wadekar | ea709c3 | 2016-04-20 17:14:15 -0700 | [diff] [blame] | 135 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 136 | /* 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 Wadekar | ea709c3 | 2016-04-20 17:14:15 -0700 | [diff] [blame] | 140 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 141 | /* 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 Wadekar | ea709c3 | 2016-04-20 17:14:15 -0700 | [diff] [blame] | 151 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 152 | /* 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 Wadekar | ea709c3 | 2016-04-20 17:14:15 -0700 | [diff] [blame] | 156 | |
Pritesh Raithatha | 0de6e53 | 2017-01-24 13:49:46 +0530 | [diff] [blame] | 157 | /* 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 Wadekar | 3c95993 | 2016-03-03 13:09:08 -0800 | [diff] [blame] | 162 | } |