Linux 5.1.15
[linux/fpc-iii.git] / drivers / iommu / tegra-gart.c
blob4d80579165520b464a1f4b0e81ddedab0b3da291
1 /*
2 * IOMMU API for Graphics Address Relocation Table on Tegra20
4 * Copyright (c) 2010-2012, NVIDIA CORPORATION. All rights reserved.
6 * Author: Hiroshi DOYU <hdoyu@nvidia.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22 #define dev_fmt(fmt) "gart: " fmt
24 #include <linux/io.h>
25 #include <linux/iommu.h>
26 #include <linux/moduleparam.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/vmalloc.h>
32 #include <soc/tegra/mc.h>
34 #define GART_REG_BASE 0x24
35 #define GART_CONFIG (0x24 - GART_REG_BASE)
36 #define GART_ENTRY_ADDR (0x28 - GART_REG_BASE)
37 #define GART_ENTRY_DATA (0x2c - GART_REG_BASE)
39 #define GART_ENTRY_PHYS_ADDR_VALID BIT(31)
41 #define GART_PAGE_SHIFT 12
42 #define GART_PAGE_SIZE (1 << GART_PAGE_SHIFT)
43 #define GART_PAGE_MASK GENMASK(30, GART_PAGE_SHIFT)
45 /* bitmap of the page sizes currently supported */
46 #define GART_IOMMU_PGSIZES (GART_PAGE_SIZE)
48 struct gart_device {
49 void __iomem *regs;
50 u32 *savedata;
51 unsigned long iovmm_base; /* offset to vmm_area start */
52 unsigned long iovmm_end; /* offset to vmm_area end */
53 spinlock_t pte_lock; /* for pagetable */
54 spinlock_t dom_lock; /* for active domain */
55 unsigned int active_devices; /* number of active devices */
56 struct iommu_domain *active_domain; /* current active domain */
57 struct iommu_device iommu; /* IOMMU Core handle */
58 struct device *dev;
61 static struct gart_device *gart_handle; /* unique for a system */
63 static bool gart_debug;
66 * Any interaction between any block on PPSB and a block on APB or AHB
67 * must have these read-back to ensure the APB/AHB bus transaction is
68 * complete before initiating activity on the PPSB block.
70 #define FLUSH_GART_REGS(gart) readl_relaxed((gart)->regs + GART_CONFIG)
72 #define for_each_gart_pte(gart, iova) \
73 for (iova = gart->iovmm_base; \
74 iova < gart->iovmm_end; \
75 iova += GART_PAGE_SIZE)
77 static inline void gart_set_pte(struct gart_device *gart,
78 unsigned long iova, unsigned long pte)
80 writel_relaxed(iova, gart->regs + GART_ENTRY_ADDR);
81 writel_relaxed(pte, gart->regs + GART_ENTRY_DATA);
84 static inline unsigned long gart_read_pte(struct gart_device *gart,
85 unsigned long iova)
87 unsigned long pte;
89 writel_relaxed(iova, gart->regs + GART_ENTRY_ADDR);
90 pte = readl_relaxed(gart->regs + GART_ENTRY_DATA);
92 return pte;
95 static void do_gart_setup(struct gart_device *gart, const u32 *data)
97 unsigned long iova;
99 for_each_gart_pte(gart, iova)
100 gart_set_pte(gart, iova, data ? *(data++) : 0);
102 writel_relaxed(1, gart->regs + GART_CONFIG);
103 FLUSH_GART_REGS(gart);
106 static inline bool gart_iova_range_invalid(struct gart_device *gart,
107 unsigned long iova, size_t bytes)
109 return unlikely(iova < gart->iovmm_base || bytes != GART_PAGE_SIZE ||
110 iova + bytes > gart->iovmm_end);
113 static inline bool gart_pte_valid(struct gart_device *gart, unsigned long iova)
115 return !!(gart_read_pte(gart, iova) & GART_ENTRY_PHYS_ADDR_VALID);
118 static int gart_iommu_attach_dev(struct iommu_domain *domain,
119 struct device *dev)
121 struct gart_device *gart = gart_handle;
122 int ret = 0;
124 spin_lock(&gart->dom_lock);
126 if (gart->active_domain && gart->active_domain != domain) {
127 ret = -EBUSY;
128 } else if (dev->archdata.iommu != domain) {
129 dev->archdata.iommu = domain;
130 gart->active_domain = domain;
131 gart->active_devices++;
134 spin_unlock(&gart->dom_lock);
136 return ret;
139 static void gart_iommu_detach_dev(struct iommu_domain *domain,
140 struct device *dev)
142 struct gart_device *gart = gart_handle;
144 spin_lock(&gart->dom_lock);
146 if (dev->archdata.iommu == domain) {
147 dev->archdata.iommu = NULL;
149 if (--gart->active_devices == 0)
150 gart->active_domain = NULL;
153 spin_unlock(&gart->dom_lock);
156 static struct iommu_domain *gart_iommu_domain_alloc(unsigned type)
158 struct iommu_domain *domain;
160 if (type != IOMMU_DOMAIN_UNMANAGED)
161 return NULL;
163 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
164 if (domain) {
165 domain->geometry.aperture_start = gart_handle->iovmm_base;
166 domain->geometry.aperture_end = gart_handle->iovmm_end - 1;
167 domain->geometry.force_aperture = true;
170 return domain;
173 static void gart_iommu_domain_free(struct iommu_domain *domain)
175 WARN_ON(gart_handle->active_domain == domain);
176 kfree(domain);
179 static inline int __gart_iommu_map(struct gart_device *gart, unsigned long iova,
180 unsigned long pa)
182 if (unlikely(gart_debug && gart_pte_valid(gart, iova))) {
183 dev_err(gart->dev, "Page entry is in-use\n");
184 return -EINVAL;
187 gart_set_pte(gart, iova, GART_ENTRY_PHYS_ADDR_VALID | pa);
189 return 0;
192 static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
193 phys_addr_t pa, size_t bytes, int prot)
195 struct gart_device *gart = gart_handle;
196 int ret;
198 if (gart_iova_range_invalid(gart, iova, bytes))
199 return -EINVAL;
201 spin_lock(&gart->pte_lock);
202 ret = __gart_iommu_map(gart, iova, (unsigned long)pa);
203 spin_unlock(&gart->pte_lock);
205 return ret;
208 static inline int __gart_iommu_unmap(struct gart_device *gart,
209 unsigned long iova)
211 if (unlikely(gart_debug && !gart_pte_valid(gart, iova))) {
212 dev_err(gart->dev, "Page entry is invalid\n");
213 return -EINVAL;
216 gart_set_pte(gart, iova, 0);
218 return 0;
221 static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
222 size_t bytes)
224 struct gart_device *gart = gart_handle;
225 int err;
227 if (gart_iova_range_invalid(gart, iova, bytes))
228 return 0;
230 spin_lock(&gart->pte_lock);
231 err = __gart_iommu_unmap(gart, iova);
232 spin_unlock(&gart->pte_lock);
234 return err ? 0 : bytes;
237 static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain,
238 dma_addr_t iova)
240 struct gart_device *gart = gart_handle;
241 unsigned long pte;
243 if (gart_iova_range_invalid(gart, iova, GART_PAGE_SIZE))
244 return -EINVAL;
246 spin_lock(&gart->pte_lock);
247 pte = gart_read_pte(gart, iova);
248 spin_unlock(&gart->pte_lock);
250 return pte & GART_PAGE_MASK;
253 static bool gart_iommu_capable(enum iommu_cap cap)
255 return false;
258 static int gart_iommu_add_device(struct device *dev)
260 struct iommu_group *group;
262 if (!dev->iommu_fwspec)
263 return -ENODEV;
265 group = iommu_group_get_for_dev(dev);
266 if (IS_ERR(group))
267 return PTR_ERR(group);
269 iommu_group_put(group);
271 iommu_device_link(&gart_handle->iommu, dev);
273 return 0;
276 static void gart_iommu_remove_device(struct device *dev)
278 iommu_group_remove_device(dev);
279 iommu_device_unlink(&gart_handle->iommu, dev);
282 static int gart_iommu_of_xlate(struct device *dev,
283 struct of_phandle_args *args)
285 return 0;
288 static void gart_iommu_sync(struct iommu_domain *domain)
290 FLUSH_GART_REGS(gart_handle);
293 static const struct iommu_ops gart_iommu_ops = {
294 .capable = gart_iommu_capable,
295 .domain_alloc = gart_iommu_domain_alloc,
296 .domain_free = gart_iommu_domain_free,
297 .attach_dev = gart_iommu_attach_dev,
298 .detach_dev = gart_iommu_detach_dev,
299 .add_device = gart_iommu_add_device,
300 .remove_device = gart_iommu_remove_device,
301 .device_group = generic_device_group,
302 .map = gart_iommu_map,
303 .unmap = gart_iommu_unmap,
304 .iova_to_phys = gart_iommu_iova_to_phys,
305 .pgsize_bitmap = GART_IOMMU_PGSIZES,
306 .of_xlate = gart_iommu_of_xlate,
307 .iotlb_sync_map = gart_iommu_sync,
308 .iotlb_sync = gart_iommu_sync,
311 int tegra_gart_suspend(struct gart_device *gart)
313 u32 *data = gart->savedata;
314 unsigned long iova;
317 * All GART users shall be suspended at this point. Disable
318 * address translation to trap all GART accesses as invalid
319 * memory accesses.
321 writel_relaxed(0, gart->regs + GART_CONFIG);
322 FLUSH_GART_REGS(gart);
324 for_each_gart_pte(gart, iova)
325 *(data++) = gart_read_pte(gart, iova);
327 return 0;
330 int tegra_gart_resume(struct gart_device *gart)
332 do_gart_setup(gart, gart->savedata);
334 return 0;
337 struct gart_device *tegra_gart_probe(struct device *dev, struct tegra_mc *mc)
339 struct gart_device *gart;
340 struct resource *res;
341 int err;
343 BUILD_BUG_ON(PAGE_SHIFT != GART_PAGE_SHIFT);
345 /* the GART memory aperture is required */
346 res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1);
347 if (!res) {
348 dev_err(dev, "Memory aperture resource unavailable\n");
349 return ERR_PTR(-ENXIO);
352 gart = kzalloc(sizeof(*gart), GFP_KERNEL);
353 if (!gart)
354 return ERR_PTR(-ENOMEM);
356 gart_handle = gart;
358 gart->dev = dev;
359 gart->regs = mc->regs + GART_REG_BASE;
360 gart->iovmm_base = res->start;
361 gart->iovmm_end = res->end + 1;
362 spin_lock_init(&gart->pte_lock);
363 spin_lock_init(&gart->dom_lock);
365 do_gart_setup(gart, NULL);
367 err = iommu_device_sysfs_add(&gart->iommu, dev, NULL, "gart");
368 if (err)
369 goto free_gart;
371 iommu_device_set_ops(&gart->iommu, &gart_iommu_ops);
372 iommu_device_set_fwnode(&gart->iommu, dev->fwnode);
374 err = iommu_device_register(&gart->iommu);
375 if (err)
376 goto remove_sysfs;
378 gart->savedata = vmalloc(resource_size(res) / GART_PAGE_SIZE *
379 sizeof(u32));
380 if (!gart->savedata) {
381 err = -ENOMEM;
382 goto unregister_iommu;
385 return gart;
387 unregister_iommu:
388 iommu_device_unregister(&gart->iommu);
389 remove_sysfs:
390 iommu_device_sysfs_remove(&gart->iommu);
391 free_gart:
392 kfree(gart);
394 return ERR_PTR(err);
397 module_param(gart_debug, bool, 0644);
398 MODULE_PARM_DESC(gart_debug, "Enable GART debugging");