Thomas Gleixner | a61127c | 2019-05-29 16:57:49 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 2 | /* |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 3 | * IOMMU API for Graphics Address Relocation Table on Tegra20 |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2010-2012, NVIDIA CORPORATION. All rights reserved. |
| 6 | * |
Paul Gortmaker | 39fcbbcc | 2018-12-01 14:19:15 -0500 | [diff] [blame] | 7 | * Author: Hiroshi DOYU <hdoyu@nvidia.com> |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Dmitry Osipenko | 5dd82cd | 2018-12-12 23:39:04 +0300 | [diff] [blame] | 10 | #define dev_fmt(fmt) "gart: " fmt |
| 11 | |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 12 | #include <linux/io.h> |
| 13 | #include <linux/iommu.h> |
Dmitry Osipenko | 4f821c1 | 2018-12-12 23:38:44 +0300 | [diff] [blame] | 14 | #include <linux/moduleparam.h> |
Dmitry Osipenko | ce2785a | 2018-12-12 23:38:56 +0300 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
Dmitry Osipenko | 4f821c1 | 2018-12-12 23:38:44 +0300 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/vmalloc.h> |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 19 | |
Dmitry Osipenko | ce2785a | 2018-12-12 23:38:56 +0300 | [diff] [blame] | 20 | #include <soc/tegra/mc.h> |
| 21 | |
Hiroshi DOYU | 774dfc9 | 2012-05-10 10:45:32 +0300 | [diff] [blame] | 22 | #define GART_REG_BASE 0x24 |
| 23 | #define GART_CONFIG (0x24 - GART_REG_BASE) |
| 24 | #define GART_ENTRY_ADDR (0x28 - GART_REG_BASE) |
| 25 | #define GART_ENTRY_DATA (0x2c - GART_REG_BASE) |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 26 | |
| 27 | #define GART_ENTRY_PHYS_ADDR_VALID BIT(31) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 28 | |
| 29 | #define GART_PAGE_SHIFT 12 |
| 30 | #define GART_PAGE_SIZE (1 << GART_PAGE_SHIFT) |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 31 | #define GART_PAGE_MASK GENMASK(30, GART_PAGE_SHIFT) |
| 32 | |
| 33 | /* bitmap of the page sizes currently supported */ |
| 34 | #define GART_IOMMU_PGSIZES (GART_PAGE_SIZE) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 35 | |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 36 | struct gart_device { |
| 37 | void __iomem *regs; |
| 38 | u32 *savedata; |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 39 | unsigned long iovmm_base; /* offset to vmm_area start */ |
| 40 | unsigned long iovmm_end; /* offset to vmm_area end */ |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 41 | spinlock_t pte_lock; /* for pagetable */ |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 42 | spinlock_t dom_lock; /* for active domain */ |
| 43 | unsigned int active_devices; /* number of active devices */ |
Dmitry Osipenko | 7d849b7 | 2018-12-12 23:39:02 +0300 | [diff] [blame] | 44 | struct iommu_domain *active_domain; /* current active domain */ |
Joerg Roedel | c184ae8 | 2017-08-10 00:17:28 +0200 | [diff] [blame] | 45 | struct iommu_device iommu; /* IOMMU Core handle */ |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 46 | struct device *dev; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static struct gart_device *gart_handle; /* unique for a system */ |
| 50 | |
Dmitry Osipenko | 40c9b88 | 2018-04-09 23:07:19 +0300 | [diff] [blame] | 51 | static bool gart_debug; |
| 52 | |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 53 | /* |
| 54 | * Any interaction between any block on PPSB and a block on APB or AHB |
| 55 | * must have these read-back to ensure the APB/AHB bus transaction is |
| 56 | * complete before initiating activity on the PPSB block. |
| 57 | */ |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 58 | #define FLUSH_GART_REGS(gart) readl_relaxed((gart)->regs + GART_CONFIG) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 59 | |
| 60 | #define for_each_gart_pte(gart, iova) \ |
| 61 | for (iova = gart->iovmm_base; \ |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 62 | iova < gart->iovmm_end; \ |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 63 | iova += GART_PAGE_SIZE) |
| 64 | |
| 65 | static inline void gart_set_pte(struct gart_device *gart, |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 66 | unsigned long iova, unsigned long pte) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 67 | { |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 68 | writel_relaxed(iova, gart->regs + GART_ENTRY_ADDR); |
| 69 | writel_relaxed(pte, gart->regs + GART_ENTRY_DATA); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static inline unsigned long gart_read_pte(struct gart_device *gart, |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 73 | unsigned long iova) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 74 | { |
| 75 | unsigned long pte; |
| 76 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 77 | writel_relaxed(iova, gart->regs + GART_ENTRY_ADDR); |
| 78 | pte = readl_relaxed(gart->regs + GART_ENTRY_DATA); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 79 | |
| 80 | return pte; |
| 81 | } |
| 82 | |
| 83 | static void do_gart_setup(struct gart_device *gart, const u32 *data) |
| 84 | { |
| 85 | unsigned long iova; |
| 86 | |
| 87 | for_each_gart_pte(gart, iova) |
| 88 | gart_set_pte(gart, iova, data ? *(data++) : 0); |
| 89 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 90 | writel_relaxed(1, gart->regs + GART_CONFIG); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 91 | FLUSH_GART_REGS(gart); |
| 92 | } |
| 93 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 94 | static inline bool gart_iova_range_invalid(struct gart_device *gart, |
| 95 | unsigned long iova, size_t bytes) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 96 | { |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 97 | return unlikely(iova < gart->iovmm_base || bytes != GART_PAGE_SIZE || |
| 98 | iova + bytes > gart->iovmm_end); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 99 | } |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 100 | |
| 101 | static inline bool gart_pte_valid(struct gart_device *gart, unsigned long iova) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 102 | { |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 103 | return !!(gart_read_pte(gart, iova) & GART_ENTRY_PHYS_ADDR_VALID); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int gart_iommu_attach_dev(struct iommu_domain *domain, |
| 107 | struct device *dev) |
| 108 | { |
Dmitry Osipenko | cc0e120 | 2018-12-12 23:39:05 +0300 | [diff] [blame] | 109 | struct gart_device *gart = gart_handle; |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 110 | int ret = 0; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 111 | |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 112 | spin_lock(&gart->dom_lock); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 113 | |
Dmitry Osipenko | 7d849b7 | 2018-12-12 23:39:02 +0300 | [diff] [blame] | 114 | if (gart->active_domain && gart->active_domain != domain) { |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 115 | ret = -EBUSY; |
Joerg Roedel | a5616e2 | 2020-06-25 15:08:29 +0200 | [diff] [blame] | 116 | } else if (dev_iommu_priv_get(dev) != domain) { |
| 117 | dev_iommu_priv_set(dev, domain); |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 118 | gart->active_domain = domain; |
| 119 | gart->active_devices++; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 120 | } |
Dmitry Osipenko | c3086fa | 2018-12-12 23:39:00 +0300 | [diff] [blame] | 121 | |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 122 | spin_unlock(&gart->dom_lock); |
| 123 | |
| 124 | return ret; |
Dmitry Osipenko | c3086fa | 2018-12-12 23:39:00 +0300 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void gart_iommu_detach_dev(struct iommu_domain *domain, |
| 128 | struct device *dev) |
| 129 | { |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 130 | struct gart_device *gart = gart_handle; |
Dmitry Osipenko | c3086fa | 2018-12-12 23:39:00 +0300 | [diff] [blame] | 131 | |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 132 | spin_lock(&gart->dom_lock); |
| 133 | |
Joerg Roedel | a5616e2 | 2020-06-25 15:08:29 +0200 | [diff] [blame] | 134 | if (dev_iommu_priv_get(dev) == domain) { |
| 135 | dev_iommu_priv_set(dev, NULL); |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 136 | |
| 137 | if (--gart->active_devices == 0) |
| 138 | gart->active_domain = NULL; |
| 139 | } |
| 140 | |
| 141 | spin_unlock(&gart->dom_lock); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Joerg Roedel | b5cbb38 | 2015-03-26 13:43:13 +0100 | [diff] [blame] | 144 | static struct iommu_domain *gart_iommu_domain_alloc(unsigned type) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 145 | { |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 146 | struct iommu_domain *domain; |
Thierry Reding | 836a8ac | 2015-03-27 11:07:26 +0100 | [diff] [blame] | 147 | |
Joerg Roedel | b5cbb38 | 2015-03-26 13:43:13 +0100 | [diff] [blame] | 148 | if (type != IOMMU_DOMAIN_UNMANAGED) |
| 149 | return NULL; |
| 150 | |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 151 | domain = kzalloc(sizeof(*domain), GFP_KERNEL); |
| 152 | if (domain) { |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 153 | domain->geometry.aperture_start = gart_handle->iovmm_base; |
| 154 | domain->geometry.aperture_end = gart_handle->iovmm_end - 1; |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 155 | domain->geometry.force_aperture = true; |
| 156 | } |
Thierry Reding | 836a8ac | 2015-03-27 11:07:26 +0100 | [diff] [blame] | 157 | |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 158 | return domain; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Joerg Roedel | b5cbb38 | 2015-03-26 13:43:13 +0100 | [diff] [blame] | 161 | static void gart_iommu_domain_free(struct iommu_domain *domain) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 162 | { |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 163 | WARN_ON(gart_handle->active_domain == domain); |
| 164 | kfree(domain); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 165 | } |
| 166 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 167 | static inline int __gart_iommu_map(struct gart_device *gart, unsigned long iova, |
| 168 | unsigned long pa) |
| 169 | { |
| 170 | if (unlikely(gart_debug && gart_pte_valid(gart, iova))) { |
| 171 | dev_err(gart->dev, "Page entry is in-use\n"); |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | gart_set_pte(gart, iova, GART_ENTRY_PHYS_ADDR_VALID | pa); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 180 | static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova, |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 181 | phys_addr_t pa, size_t bytes, int prot, gfp_t gfp) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 182 | { |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 183 | struct gart_device *gart = gart_handle; |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 184 | int ret; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 185 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 186 | if (gart_iova_range_invalid(gart, iova, bytes)) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 187 | return -EINVAL; |
| 188 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 189 | spin_lock(&gart->pte_lock); |
| 190 | ret = __gart_iommu_map(gart, iova, (unsigned long)pa); |
| 191 | spin_unlock(&gart->pte_lock); |
| 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static inline int __gart_iommu_unmap(struct gart_device *gart, |
| 197 | unsigned long iova) |
| 198 | { |
| 199 | if (unlikely(gart_debug && !gart_pte_valid(gart, iova))) { |
| 200 | dev_err(gart->dev, "Page entry is invalid\n"); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 201 | return -EINVAL; |
| 202 | } |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 203 | |
| 204 | gart_set_pte(gart, iova, 0); |
| 205 | |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova, |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 210 | size_t bytes, struct iommu_iotlb_gather *gather) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 211 | { |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 212 | struct gart_device *gart = gart_handle; |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 213 | int err; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 214 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 215 | if (gart_iova_range_invalid(gart, iova, bytes)) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 216 | return 0; |
| 217 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 218 | spin_lock(&gart->pte_lock); |
| 219 | err = __gart_iommu_unmap(gart, iova); |
| 220 | spin_unlock(&gart->pte_lock); |
| 221 | |
| 222 | return err ? 0 : bytes; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain, |
Varun Sethi | bb5547a | 2013-03-29 01:23:58 +0530 | [diff] [blame] | 226 | dma_addr_t iova) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 227 | { |
Dmitry Osipenko | e7e23670 | 2018-12-12 23:39:06 +0300 | [diff] [blame] | 228 | struct gart_device *gart = gart_handle; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 229 | unsigned long pte; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 230 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 231 | if (gart_iova_range_invalid(gart, iova, GART_PAGE_SIZE)) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 232 | return -EINVAL; |
| 233 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 234 | spin_lock(&gart->pte_lock); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 235 | pte = gart_read_pte(gart, iova); |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 236 | spin_unlock(&gart->pte_lock); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 237 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 238 | return pte & GART_PAGE_MASK; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Joerg Roedel | 7c2aa64 | 2014-09-05 10:51:37 +0200 | [diff] [blame] | 241 | static bool gart_iommu_capable(enum iommu_cap cap) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 242 | { |
Joerg Roedel | 7c2aa64 | 2014-09-05 10:51:37 +0200 | [diff] [blame] | 243 | return false; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Joerg Roedel | b287ba7 | 2020-04-29 15:37:04 +0200 | [diff] [blame] | 246 | static struct iommu_device *gart_iommu_probe_device(struct device *dev) |
Robin Murphy | 15f9a31 | 2017-07-21 13:12:37 +0100 | [diff] [blame] | 247 | { |
Joerg Roedel | 8c3d692 | 2020-03-26 16:08:29 +0100 | [diff] [blame] | 248 | if (!dev_iommu_fwspec_get(dev)) |
Joerg Roedel | b287ba7 | 2020-04-29 15:37:04 +0200 | [diff] [blame] | 249 | return ERR_PTR(-ENODEV); |
Dmitry Osipenko | 4b6f0ea | 2018-12-12 23:38:46 +0300 | [diff] [blame] | 250 | |
Joerg Roedel | b287ba7 | 2020-04-29 15:37:04 +0200 | [diff] [blame] | 251 | return &gart_handle->iommu; |
Robin Murphy | 15f9a31 | 2017-07-21 13:12:37 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Joerg Roedel | b287ba7 | 2020-04-29 15:37:04 +0200 | [diff] [blame] | 254 | static void gart_iommu_release_device(struct device *dev) |
Robin Murphy | 15f9a31 | 2017-07-21 13:12:37 +0100 | [diff] [blame] | 255 | { |
Robin Murphy | 15f9a31 | 2017-07-21 13:12:37 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Dmitry Osipenko | 4b6f0ea | 2018-12-12 23:38:46 +0300 | [diff] [blame] | 258 | static int gart_iommu_of_xlate(struct device *dev, |
| 259 | struct of_phandle_args *args) |
| 260 | { |
| 261 | return 0; |
| 262 | } |
| 263 | |
Yong Wu | 2ebbd25 | 2021-01-07 20:29:04 +0800 | [diff] [blame] | 264 | static void gart_iommu_sync_map(struct iommu_domain *domain, unsigned long iova, |
| 265 | size_t size) |
Dmitry Osipenko | 2fc0ac1 | 2018-12-12 23:38:48 +0300 | [diff] [blame] | 266 | { |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 267 | FLUSH_GART_REGS(gart_handle); |
Dmitry Osipenko | 2fc0ac1 | 2018-12-12 23:38:48 +0300 | [diff] [blame] | 268 | } |
| 269 | |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 270 | static void gart_iommu_sync(struct iommu_domain *domain, |
| 271 | struct iommu_iotlb_gather *gather) |
| 272 | { |
Yong Wu | 862c371 | 2021-01-07 20:29:06 +0800 | [diff] [blame] | 273 | size_t length = gather->end - gather->start + 1; |
Yong Wu | 2ebbd25 | 2021-01-07 20:29:04 +0800 | [diff] [blame] | 274 | |
| 275 | gart_iommu_sync_map(domain, gather->start, length); |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 278 | static const struct iommu_ops gart_iommu_ops = { |
Joerg Roedel | 7c2aa64 | 2014-09-05 10:51:37 +0200 | [diff] [blame] | 279 | .capable = gart_iommu_capable, |
Joerg Roedel | b5cbb38 | 2015-03-26 13:43:13 +0100 | [diff] [blame] | 280 | .domain_alloc = gart_iommu_domain_alloc, |
| 281 | .domain_free = gart_iommu_domain_free, |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 282 | .attach_dev = gart_iommu_attach_dev, |
| 283 | .detach_dev = gart_iommu_detach_dev, |
Joerg Roedel | b287ba7 | 2020-04-29 15:37:04 +0200 | [diff] [blame] | 284 | .probe_device = gart_iommu_probe_device, |
| 285 | .release_device = gart_iommu_release_device, |
Robin Murphy | 15f9a31 | 2017-07-21 13:12:37 +0100 | [diff] [blame] | 286 | .device_group = generic_device_group, |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 287 | .map = gart_iommu_map, |
| 288 | .unmap = gart_iommu_unmap, |
| 289 | .iova_to_phys = gart_iommu_iova_to_phys, |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 290 | .pgsize_bitmap = GART_IOMMU_PGSIZES, |
Dmitry Osipenko | 4b6f0ea | 2018-12-12 23:38:46 +0300 | [diff] [blame] | 291 | .of_xlate = gart_iommu_of_xlate, |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 292 | .iotlb_sync_map = gart_iommu_sync_map, |
Dmitry Osipenko | 2fc0ac1 | 2018-12-12 23:38:48 +0300 | [diff] [blame] | 293 | .iotlb_sync = gart_iommu_sync, |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 294 | }; |
| 295 | |
Dmitry Osipenko | ce2785a | 2018-12-12 23:38:56 +0300 | [diff] [blame] | 296 | int tegra_gart_suspend(struct gart_device *gart) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 297 | { |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 298 | u32 *data = gart->savedata; |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 299 | unsigned long iova; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 300 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 301 | /* |
| 302 | * All GART users shall be suspended at this point. Disable |
| 303 | * address translation to trap all GART accesses as invalid |
| 304 | * memory accesses. |
| 305 | */ |
| 306 | writel_relaxed(0, gart->regs + GART_CONFIG); |
| 307 | FLUSH_GART_REGS(gart); |
| 308 | |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 309 | for_each_gart_pte(gart, iova) |
| 310 | *(data++) = gart_read_pte(gart, iova); |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 311 | |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
Dmitry Osipenko | ce2785a | 2018-12-12 23:38:56 +0300 | [diff] [blame] | 315 | int tegra_gart_resume(struct gart_device *gart) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 316 | { |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 317 | do_gart_setup(gart, gart->savedata); |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 318 | |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | |
Dmitry Osipenko | ce2785a | 2018-12-12 23:38:56 +0300 | [diff] [blame] | 322 | struct gart_device *tegra_gart_probe(struct device *dev, struct tegra_mc *mc) |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 323 | { |
| 324 | struct gart_device *gart; |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 325 | struct resource *res; |
| 326 | int err; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 327 | |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 328 | BUILD_BUG_ON(PAGE_SHIFT != GART_PAGE_SHIFT); |
| 329 | |
| 330 | /* the GART memory aperture is required */ |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 331 | res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1); |
| 332 | if (!res) { |
| 333 | dev_err(dev, "Memory aperture resource unavailable\n"); |
Dmitry Osipenko | ce2785a | 2018-12-12 23:38:56 +0300 | [diff] [blame] | 334 | return ERR_PTR(-ENXIO); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Dmitry Osipenko | 167d67d | 2018-12-12 23:39:03 +0300 | [diff] [blame] | 337 | gart = kzalloc(sizeof(*gart), GFP_KERNEL); |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 338 | if (!gart) |
Dmitry Osipenko | ce2785a | 2018-12-12 23:38:56 +0300 | [diff] [blame] | 339 | return ERR_PTR(-ENOMEM); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 340 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 341 | gart_handle = gart; |
| 342 | |
| 343 | gart->dev = dev; |
| 344 | gart->regs = mc->regs + GART_REG_BASE; |
| 345 | gart->iovmm_base = res->start; |
| 346 | gart->iovmm_end = res->end + 1; |
| 347 | spin_lock_init(&gart->pte_lock); |
| 348 | spin_lock_init(&gart->dom_lock); |
| 349 | |
| 350 | do_gart_setup(gart, NULL); |
| 351 | |
| 352 | err = iommu_device_sysfs_add(&gart->iommu, dev, NULL, "gart"); |
| 353 | if (err) |
Dmitry Osipenko | 167d67d | 2018-12-12 23:39:03 +0300 | [diff] [blame] | 354 | goto free_gart; |
Joerg Roedel | c184ae8 | 2017-08-10 00:17:28 +0200 | [diff] [blame] | 355 | |
Robin Murphy | 2d471b2 | 2021-04-01 14:56:26 +0100 | [diff] [blame] | 356 | err = iommu_device_register(&gart->iommu, &gart_iommu_ops, dev); |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 357 | if (err) |
Dmitry Osipenko | ae95c46 | 2018-12-12 23:38:45 +0300 | [diff] [blame] | 358 | goto remove_sysfs; |
Joerg Roedel | c184ae8 | 2017-08-10 00:17:28 +0200 | [diff] [blame] | 359 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 360 | gart->savedata = vmalloc(resource_size(res) / GART_PAGE_SIZE * |
| 361 | sizeof(u32)); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 362 | if (!gart->savedata) { |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 363 | err = -ENOMEM; |
Dmitry Osipenko | ae95c46 | 2018-12-12 23:38:45 +0300 | [diff] [blame] | 364 | goto unregister_iommu; |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Dmitry Osipenko | ce2785a | 2018-12-12 23:38:56 +0300 | [diff] [blame] | 367 | return gart; |
Dmitry Osipenko | ae95c46 | 2018-12-12 23:38:45 +0300 | [diff] [blame] | 368 | |
| 369 | unregister_iommu: |
| 370 | iommu_device_unregister(&gart->iommu); |
| 371 | remove_sysfs: |
| 372 | iommu_device_sysfs_remove(&gart->iommu); |
Dmitry Osipenko | 167d67d | 2018-12-12 23:39:03 +0300 | [diff] [blame] | 373 | free_gart: |
| 374 | kfree(gart); |
Dmitry Osipenko | ae95c46 | 2018-12-12 23:38:45 +0300 | [diff] [blame] | 375 | |
Dmitry Osipenko | 7072230 | 2018-12-12 23:39:07 +0300 | [diff] [blame] | 376 | return ERR_PTR(err); |
Hiroshi DOYU | d53e54b | 2011-11-16 17:36:37 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Paul Gortmaker | 39fcbbcc | 2018-12-01 14:19:15 -0500 | [diff] [blame] | 379 | module_param(gart_debug, bool, 0644); |
Dmitry Osipenko | 40c9b88 | 2018-04-09 23:07:19 +0300 | [diff] [blame] | 380 | MODULE_PARM_DESC(gart_debug, "Enable GART debugging"); |