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
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
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)
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 */
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
,
89 writel_relaxed(iova
, gart
->regs
+ GART_ENTRY_ADDR
);
90 pte
= readl_relaxed(gart
->regs
+ GART_ENTRY_DATA
);
95 static void do_gart_setup(struct gart_device
*gart
, const u32
*data
)
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
,
121 struct gart_device
*gart
= gart_handle
;
124 spin_lock(&gart
->dom_lock
);
126 if (gart
->active_domain
&& gart
->active_domain
!= domain
) {
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
);
139 static void gart_iommu_detach_dev(struct iommu_domain
*domain
,
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
)
163 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
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;
173 static void gart_iommu_domain_free(struct iommu_domain
*domain
)
175 WARN_ON(gart_handle
->active_domain
== domain
);
179 static inline int __gart_iommu_map(struct gart_device
*gart
, unsigned long iova
,
182 if (unlikely(gart_debug
&& gart_pte_valid(gart
, iova
))) {
183 dev_err(gart
->dev
, "Page entry is in-use\n");
187 gart_set_pte(gart
, iova
, GART_ENTRY_PHYS_ADDR_VALID
| pa
);
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
;
198 if (gart_iova_range_invalid(gart
, iova
, bytes
))
201 spin_lock(&gart
->pte_lock
);
202 ret
= __gart_iommu_map(gart
, iova
, (unsigned long)pa
);
203 spin_unlock(&gart
->pte_lock
);
208 static inline int __gart_iommu_unmap(struct gart_device
*gart
,
211 if (unlikely(gart_debug
&& !gart_pte_valid(gart
, iova
))) {
212 dev_err(gart
->dev
, "Page entry is invalid\n");
216 gart_set_pte(gart
, iova
, 0);
221 static size_t gart_iommu_unmap(struct iommu_domain
*domain
, unsigned long iova
,
224 struct gart_device
*gart
= gart_handle
;
227 if (gart_iova_range_invalid(gart
, iova
, bytes
))
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
,
240 struct gart_device
*gart
= gart_handle
;
243 if (gart_iova_range_invalid(gart
, iova
, GART_PAGE_SIZE
))
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
)
258 static int gart_iommu_add_device(struct device
*dev
)
260 struct iommu_group
*group
;
262 if (!dev
->iommu_fwspec
)
265 group
= iommu_group_get_for_dev(dev
);
267 return PTR_ERR(group
);
269 iommu_group_put(group
);
271 iommu_device_link(&gart_handle
->iommu
, dev
);
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
)
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
;
317 * All GART users shall be suspended at this point. Disable
318 * address translation to trap all GART accesses as invalid
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
);
330 int tegra_gart_resume(struct gart_device
*gart
)
332 do_gart_setup(gart
, gart
->savedata
);
337 struct gart_device
*tegra_gart_probe(struct device
*dev
, struct tegra_mc
*mc
)
339 struct gart_device
*gart
;
340 struct resource
*res
;
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);
348 dev_err(dev
, "Memory aperture resource unavailable\n");
349 return ERR_PTR(-ENXIO
);
352 gart
= kzalloc(sizeof(*gart
), GFP_KERNEL
);
354 return ERR_PTR(-ENOMEM
);
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");
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
);
378 gart
->savedata
= vmalloc(resource_size(res
) / GART_PAGE_SIZE
*
380 if (!gart
->savedata
) {
382 goto unregister_iommu
;
388 iommu_device_unregister(&gart
->iommu
);
390 iommu_device_sysfs_remove(&gart
->iommu
);
397 module_param(gart_debug
, bool, 0644);
398 MODULE_PARM_DESC(gart_debug
, "Enable GART debugging");