|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +// Copyright (C) 2019-2020 NVIDIA CORPORATION. All rights reserved. |
| 3 | + |
| 4 | +#include <linux/bitfield.h> |
| 5 | +#include <linux/delay.h> |
| 6 | +#include <linux/of.h> |
| 7 | +#include <linux/platform_device.h> |
| 8 | +#include <linux/slab.h> |
| 9 | + |
| 10 | +#include "arm-smmu.h" |
| 11 | + |
| 12 | +/* |
| 13 | + * Tegra194 has three ARM MMU-500 Instances. |
| 14 | + * Two of them are used together and must be programmed identically for |
| 15 | + * interleaved IOVA accesses across them and translates accesses from |
| 16 | + * non-isochronous HW devices. |
| 17 | + * Third one is used for translating accesses from isochronous HW devices. |
| 18 | + * This implementation supports programming of the two instances that must |
| 19 | + * be programmed identically. |
| 20 | + * The third instance usage is through standard arm-smmu driver itself and |
| 21 | + * is out of scope of this implementation. |
| 22 | + */ |
| 23 | +#define NUM_SMMU_INSTANCES 2 |
| 24 | + |
| 25 | +struct nvidia_smmu { |
| 26 | + struct arm_smmu_device smmu; |
| 27 | + void __iomem *bases[NUM_SMMU_INSTANCES]; |
| 28 | +}; |
| 29 | + |
| 30 | +static inline void __iomem *nvidia_smmu_page(struct arm_smmu_device *smmu, |
| 31 | + unsigned int inst, int page) |
| 32 | +{ |
| 33 | + struct nvidia_smmu *nvidia_smmu; |
| 34 | + |
| 35 | + nvidia_smmu = container_of(smmu, struct nvidia_smmu, smmu); |
| 36 | + return nvidia_smmu->bases[inst] + (page << smmu->pgshift); |
| 37 | +} |
| 38 | + |
| 39 | +static u32 nvidia_smmu_read_reg(struct arm_smmu_device *smmu, |
| 40 | + int page, int offset) |
| 41 | +{ |
| 42 | + void __iomem *reg = nvidia_smmu_page(smmu, 0, page) + offset; |
| 43 | + |
| 44 | + return readl_relaxed(reg); |
| 45 | +} |
| 46 | + |
| 47 | +static void nvidia_smmu_write_reg(struct arm_smmu_device *smmu, |
| 48 | + int page, int offset, u32 val) |
| 49 | +{ |
| 50 | + unsigned int i; |
| 51 | + |
| 52 | + for (i = 0; i < NUM_SMMU_INSTANCES; i++) { |
| 53 | + void __iomem *reg = nvidia_smmu_page(smmu, i, page) + offset; |
| 54 | + |
| 55 | + writel_relaxed(val, reg); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +static u64 nvidia_smmu_read_reg64(struct arm_smmu_device *smmu, |
| 60 | + int page, int offset) |
| 61 | +{ |
| 62 | + void __iomem *reg = nvidia_smmu_page(smmu, 0, page) + offset; |
| 63 | + |
| 64 | + return readq_relaxed(reg); |
| 65 | +} |
| 66 | + |
| 67 | +static void nvidia_smmu_write_reg64(struct arm_smmu_device *smmu, |
| 68 | + int page, int offset, u64 val) |
| 69 | +{ |
| 70 | + unsigned int i; |
| 71 | + |
| 72 | + for (i = 0; i < NUM_SMMU_INSTANCES; i++) { |
| 73 | + void __iomem *reg = nvidia_smmu_page(smmu, i, page) + offset; |
| 74 | + |
| 75 | + writeq_relaxed(val, reg); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +static void nvidia_smmu_tlb_sync(struct arm_smmu_device *smmu, int page, |
| 80 | + int sync, int status) |
| 81 | +{ |
| 82 | + unsigned int delay; |
| 83 | + |
| 84 | + arm_smmu_writel(smmu, page, sync, 0); |
| 85 | + |
| 86 | + for (delay = 1; delay < TLB_LOOP_TIMEOUT; delay *= 2) { |
| 87 | + unsigned int spin_cnt; |
| 88 | + |
| 89 | + for (spin_cnt = TLB_SPIN_COUNT; spin_cnt > 0; spin_cnt--) { |
| 90 | + u32 val = 0; |
| 91 | + unsigned int i; |
| 92 | + |
| 93 | + for (i = 0; i < NUM_SMMU_INSTANCES; i++) { |
| 94 | + void __iomem *reg; |
| 95 | + |
| 96 | + reg = nvidia_smmu_page(smmu, i, page) + status; |
| 97 | + val |= readl_relaxed(reg); |
| 98 | + } |
| 99 | + |
| 100 | + if (!(val & ARM_SMMU_sTLBGSTATUS_GSACTIVE)) |
| 101 | + return; |
| 102 | + |
| 103 | + cpu_relax(); |
| 104 | + } |
| 105 | + |
| 106 | + udelay(delay); |
| 107 | + } |
| 108 | + |
| 109 | + dev_err_ratelimited(smmu->dev, |
| 110 | + "TLB sync timed out -- SMMU may be deadlocked\n"); |
| 111 | +} |
| 112 | + |
| 113 | +static int nvidia_smmu_reset(struct arm_smmu_device *smmu) |
| 114 | +{ |
| 115 | + unsigned int i; |
| 116 | + |
| 117 | + for (i = 0; i < NUM_SMMU_INSTANCES; i++) { |
| 118 | + u32 val; |
| 119 | + void __iomem *reg = nvidia_smmu_page(smmu, i, ARM_SMMU_GR0) + |
| 120 | + ARM_SMMU_GR0_sGFSR; |
| 121 | + |
| 122 | + /* clear global FSR */ |
| 123 | + val = readl_relaxed(reg); |
| 124 | + writel_relaxed(val, reg); |
| 125 | + } |
| 126 | + |
| 127 | + return 0; |
| 128 | +} |
| 129 | + |
| 130 | +static const struct arm_smmu_impl nvidia_smmu_impl = { |
| 131 | + .read_reg = nvidia_smmu_read_reg, |
| 132 | + .write_reg = nvidia_smmu_write_reg, |
| 133 | + .read_reg64 = nvidia_smmu_read_reg64, |
| 134 | + .write_reg64 = nvidia_smmu_write_reg64, |
| 135 | + .reset = nvidia_smmu_reset, |
| 136 | + .tlb_sync = nvidia_smmu_tlb_sync, |
| 137 | +}; |
| 138 | + |
| 139 | +struct arm_smmu_device *nvidia_smmu_impl_init(struct arm_smmu_device *smmu) |
| 140 | +{ |
| 141 | + struct resource *res; |
| 142 | + struct device *dev = smmu->dev; |
| 143 | + struct nvidia_smmu *nvidia_smmu; |
| 144 | + struct platform_device *pdev = to_platform_device(dev); |
| 145 | + |
| 146 | + nvidia_smmu = devm_kzalloc(dev, sizeof(*nvidia_smmu), GFP_KERNEL); |
| 147 | + if (!nvidia_smmu) |
| 148 | + return ERR_PTR(-ENOMEM); |
| 149 | + |
| 150 | + /* |
| 151 | + * Copy the data from struct arm_smmu_device *smmu allocated in |
| 152 | + * arm-smmu.c. The smmu from struct nvidia_smmu replaces the smmu |
| 153 | + * pointer used in arm-smmu.c once this function returns. |
| 154 | + * This is necessary to derive nvidia_smmu from smmu pointer passed |
| 155 | + * through arm_smmu_impl function calls subsequently. |
| 156 | + */ |
| 157 | + nvidia_smmu->smmu = *smmu; |
| 158 | + /* Instance 0 is ioremapped by arm-smmu.c. */ |
| 159 | + nvidia_smmu->bases[0] = smmu->base; |
| 160 | + |
| 161 | + res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 162 | + if (!res) |
| 163 | + return ERR_PTR(-ENODEV); |
| 164 | + |
| 165 | + nvidia_smmu->bases[1] = devm_ioremap_resource(dev, res); |
| 166 | + if (IS_ERR(nvidia_smmu->bases[1])) |
| 167 | + return ERR_CAST(nvidia_smmu->bases[1]); |
| 168 | + |
| 169 | + nvidia_smmu->smmu.impl = &nvidia_smmu_impl; |
| 170 | + |
| 171 | + /* |
| 172 | + * Free the struct arm_smmu_device *smmu allocated in arm-smmu.c. |
| 173 | + * Once this function returns, arm-smmu.c would use arm_smmu_device |
| 174 | + * allocated as part of struct nvidia_smmu. |
| 175 | + */ |
| 176 | + devm_kfree(dev, smmu); |
| 177 | + |
| 178 | + return &nvidia_smmu->smmu; |
| 179 | +} |
0 commit comments