1 // SPDX-License-Identifier: GPL-2.0-only
3 * IOMMU API for Graphics Address Relocation Table on Tegra20
5 * Copyright (c) 2010-2012, NVIDIA CORPORATION. All rights reserved.
7 * Author: Hiroshi DOYU <hdoyu@nvidia.com>
10 #define dev_fmt(fmt) "gart: " fmt
13 #include <linux/iommu.h>
14 #include <linux/moduleparam.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/vmalloc.h>
20 #include <soc/tegra/mc.h>
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)
27 #define GART_ENTRY_PHYS_ADDR_VALID BIT(31)
29 #define GART_PAGE_SHIFT 12
30 #define GART_PAGE_SIZE (1 << GART_PAGE_SHIFT)
31 #define GART_PAGE_MASK GENMASK(30, GART_PAGE_SHIFT)
33 /* bitmap of the page sizes currently supported */
34 #define GART_IOMMU_PGSIZES (GART_PAGE_SIZE)
39 unsigned long iovmm_base
; /* offset to vmm_area start */
40 unsigned long iovmm_end
; /* offset to vmm_area end */
41 spinlock_t pte_lock
; /* for pagetable */
42 spinlock_t dom_lock
; /* for active domain */
43 unsigned int active_devices
; /* number of active devices */
44 struct iommu_domain
*active_domain
; /* current active domain */
45 struct iommu_device iommu
; /* IOMMU Core handle */
49 static struct gart_device
*gart_handle
; /* unique for a system */
51 static bool gart_debug
;
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.
58 #define FLUSH_GART_REGS(gart) readl_relaxed((gart)->regs + GART_CONFIG)
60 #define for_each_gart_pte(gart, iova) \
61 for (iova = gart->iovmm_base; \
62 iova < gart->iovmm_end; \
63 iova += GART_PAGE_SIZE)
65 static inline void gart_set_pte(struct gart_device
*gart
,
66 unsigned long iova
, unsigned long pte
)
68 writel_relaxed(iova
, gart
->regs
+ GART_ENTRY_ADDR
);
69 writel_relaxed(pte
, gart
->regs
+ GART_ENTRY_DATA
);
72 static inline unsigned long gart_read_pte(struct gart_device
*gart
,
77 writel_relaxed(iova
, gart
->regs
+ GART_ENTRY_ADDR
);
78 pte
= readl_relaxed(gart
->regs
+ GART_ENTRY_DATA
);
83 static void do_gart_setup(struct gart_device
*gart
, const u32
*data
)
87 for_each_gart_pte(gart
, iova
)
88 gart_set_pte(gart
, iova
, data
? *(data
++) : 0);
90 writel_relaxed(1, gart
->regs
+ GART_CONFIG
);
91 FLUSH_GART_REGS(gart
);
94 static inline bool gart_iova_range_invalid(struct gart_device
*gart
,
95 unsigned long iova
, size_t bytes
)
97 return unlikely(iova
< gart
->iovmm_base
|| bytes
!= GART_PAGE_SIZE
||
98 iova
+ bytes
> gart
->iovmm_end
);
101 static inline bool gart_pte_valid(struct gart_device
*gart
, unsigned long iova
)
103 return !!(gart_read_pte(gart
, iova
) & GART_ENTRY_PHYS_ADDR_VALID
);
106 static int gart_iommu_attach_dev(struct iommu_domain
*domain
,
109 struct gart_device
*gart
= gart_handle
;
112 spin_lock(&gart
->dom_lock
);
114 if (gart
->active_domain
&& gart
->active_domain
!= domain
) {
116 } else if (dev
->archdata
.iommu
!= domain
) {
117 dev
->archdata
.iommu
= domain
;
118 gart
->active_domain
= domain
;
119 gart
->active_devices
++;
122 spin_unlock(&gart
->dom_lock
);
127 static void gart_iommu_detach_dev(struct iommu_domain
*domain
,
130 struct gart_device
*gart
= gart_handle
;
132 spin_lock(&gart
->dom_lock
);
134 if (dev
->archdata
.iommu
== domain
) {
135 dev
->archdata
.iommu
= NULL
;
137 if (--gart
->active_devices
== 0)
138 gart
->active_domain
= NULL
;
141 spin_unlock(&gart
->dom_lock
);
144 static struct iommu_domain
*gart_iommu_domain_alloc(unsigned type
)
146 struct iommu_domain
*domain
;
148 if (type
!= IOMMU_DOMAIN_UNMANAGED
)
151 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
153 domain
->geometry
.aperture_start
= gart_handle
->iovmm_base
;
154 domain
->geometry
.aperture_end
= gart_handle
->iovmm_end
- 1;
155 domain
->geometry
.force_aperture
= true;
161 static void gart_iommu_domain_free(struct iommu_domain
*domain
)
163 WARN_ON(gart_handle
->active_domain
== domain
);
167 static inline int __gart_iommu_map(struct gart_device
*gart
, unsigned long iova
,
170 if (unlikely(gart_debug
&& gart_pte_valid(gart
, iova
))) {
171 dev_err(gart
->dev
, "Page entry is in-use\n");
175 gart_set_pte(gart
, iova
, GART_ENTRY_PHYS_ADDR_VALID
| pa
);
180 static int gart_iommu_map(struct iommu_domain
*domain
, unsigned long iova
,
181 phys_addr_t pa
, size_t bytes
, int prot
)
183 struct gart_device
*gart
= gart_handle
;
186 if (gart_iova_range_invalid(gart
, iova
, bytes
))
189 spin_lock(&gart
->pte_lock
);
190 ret
= __gart_iommu_map(gart
, iova
, (unsigned long)pa
);
191 spin_unlock(&gart
->pte_lock
);
196 static inline int __gart_iommu_unmap(struct gart_device
*gart
,
199 if (unlikely(gart_debug
&& !gart_pte_valid(gart
, iova
))) {
200 dev_err(gart
->dev
, "Page entry is invalid\n");
204 gart_set_pte(gart
, iova
, 0);
209 static size_t gart_iommu_unmap(struct iommu_domain
*domain
, unsigned long iova
,
212 struct gart_device
*gart
= gart_handle
;
215 if (gart_iova_range_invalid(gart
, iova
, bytes
))
218 spin_lock(&gart
->pte_lock
);
219 err
= __gart_iommu_unmap(gart
, iova
);
220 spin_unlock(&gart
->pte_lock
);
222 return err
? 0 : bytes
;
225 static phys_addr_t
gart_iommu_iova_to_phys(struct iommu_domain
*domain
,
228 struct gart_device
*gart
= gart_handle
;
231 if (gart_iova_range_invalid(gart
, iova
, GART_PAGE_SIZE
))
234 spin_lock(&gart
->pte_lock
);
235 pte
= gart_read_pte(gart
, iova
);
236 spin_unlock(&gart
->pte_lock
);
238 return pte
& GART_PAGE_MASK
;
241 static bool gart_iommu_capable(enum iommu_cap cap
)
246 static int gart_iommu_add_device(struct device
*dev
)
248 struct iommu_group
*group
;
250 if (!dev
->iommu_fwspec
)
253 group
= iommu_group_get_for_dev(dev
);
255 return PTR_ERR(group
);
257 iommu_group_put(group
);
259 iommu_device_link(&gart_handle
->iommu
, dev
);
264 static void gart_iommu_remove_device(struct device
*dev
)
266 iommu_group_remove_device(dev
);
267 iommu_device_unlink(&gart_handle
->iommu
, dev
);
270 static int gart_iommu_of_xlate(struct device
*dev
,
271 struct of_phandle_args
*args
)
276 static void gart_iommu_sync(struct iommu_domain
*domain
)
278 FLUSH_GART_REGS(gart_handle
);
281 static const struct iommu_ops gart_iommu_ops
= {
282 .capable
= gart_iommu_capable
,
283 .domain_alloc
= gart_iommu_domain_alloc
,
284 .domain_free
= gart_iommu_domain_free
,
285 .attach_dev
= gart_iommu_attach_dev
,
286 .detach_dev
= gart_iommu_detach_dev
,
287 .add_device
= gart_iommu_add_device
,
288 .remove_device
= gart_iommu_remove_device
,
289 .device_group
= generic_device_group
,
290 .map
= gart_iommu_map
,
291 .unmap
= gart_iommu_unmap
,
292 .iova_to_phys
= gart_iommu_iova_to_phys
,
293 .pgsize_bitmap
= GART_IOMMU_PGSIZES
,
294 .of_xlate
= gart_iommu_of_xlate
,
295 .iotlb_sync_map
= gart_iommu_sync
,
296 .iotlb_sync
= gart_iommu_sync
,
299 int tegra_gart_suspend(struct gart_device
*gart
)
301 u32
*data
= gart
->savedata
;
305 * All GART users shall be suspended at this point. Disable
306 * address translation to trap all GART accesses as invalid
309 writel_relaxed(0, gart
->regs
+ GART_CONFIG
);
310 FLUSH_GART_REGS(gart
);
312 for_each_gart_pte(gart
, iova
)
313 *(data
++) = gart_read_pte(gart
, iova
);
318 int tegra_gart_resume(struct gart_device
*gart
)
320 do_gart_setup(gart
, gart
->savedata
);
325 struct gart_device
*tegra_gart_probe(struct device
*dev
, struct tegra_mc
*mc
)
327 struct gart_device
*gart
;
328 struct resource
*res
;
331 BUILD_BUG_ON(PAGE_SHIFT
!= GART_PAGE_SHIFT
);
333 /* the GART memory aperture is required */
334 res
= platform_get_resource(to_platform_device(dev
), IORESOURCE_MEM
, 1);
336 dev_err(dev
, "Memory aperture resource unavailable\n");
337 return ERR_PTR(-ENXIO
);
340 gart
= kzalloc(sizeof(*gart
), GFP_KERNEL
);
342 return ERR_PTR(-ENOMEM
);
347 gart
->regs
= mc
->regs
+ GART_REG_BASE
;
348 gart
->iovmm_base
= res
->start
;
349 gart
->iovmm_end
= res
->end
+ 1;
350 spin_lock_init(&gart
->pte_lock
);
351 spin_lock_init(&gart
->dom_lock
);
353 do_gart_setup(gart
, NULL
);
355 err
= iommu_device_sysfs_add(&gart
->iommu
, dev
, NULL
, "gart");
359 iommu_device_set_ops(&gart
->iommu
, &gart_iommu_ops
);
360 iommu_device_set_fwnode(&gart
->iommu
, dev
->fwnode
);
362 err
= iommu_device_register(&gart
->iommu
);
366 gart
->savedata
= vmalloc(resource_size(res
) / GART_PAGE_SIZE
*
368 if (!gart
->savedata
) {
370 goto unregister_iommu
;
376 iommu_device_unregister(&gart
->iommu
);
378 iommu_device_sysfs_remove(&gart
->iommu
);
385 module_param(gart_debug
, bool, 0644);
386 MODULE_PARM_DESC(gart_debug
, "Enable GART debugging");