1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2020 Unisoc, Inc.
6 * Author: Chunyan Zhang <chunyan.zhang@unisoc.com>
10 #include <linux/device.h>
11 #include <linux/dma-iommu.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/errno.h>
14 #include <linux/iommu.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
17 #include <linux/of_platform.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
21 #define SPRD_IOMMU_PAGE_SHIFT 12
22 #define SPRD_IOMMU_PAGE_SIZE SZ_4K
24 #define SPRD_EX_CFG 0x0
25 #define SPRD_IOMMU_VAOR_BYPASS BIT(4)
26 #define SPRD_IOMMU_GATE_EN BIT(1)
27 #define SPRD_IOMMU_EN BIT(0)
28 #define SPRD_EX_UPDATE 0x4
29 #define SPRD_EX_FIRST_VPN 0x8
30 #define SPRD_EX_VPN_RANGE 0xc
31 #define SPRD_EX_FIRST_PPN 0x10
32 #define SPRD_EX_DEFAULT_PPN 0x14
34 #define SPRD_IOMMU_VERSION 0x0
35 #define SPRD_VERSION_MASK GENMASK(15, 8)
36 #define SPRD_VERSION_SHIFT 0x8
37 #define SPRD_VAU_CFG 0x4
38 #define SPRD_VAU_UPDATE 0x8
39 #define SPRD_VAU_AUTH_CFG 0xc
40 #define SPRD_VAU_FIRST_PPN 0x10
41 #define SPRD_VAU_DEFAULT_PPN_RD 0x14
42 #define SPRD_VAU_DEFAULT_PPN_WR 0x18
43 #define SPRD_VAU_FIRST_VPN 0x1c
44 #define SPRD_VAU_VPN_RANGE 0x20
46 enum sprd_iommu_version {
52 * struct sprd_iommu_device - high-level sprd IOMMU device representation,
53 * including hardware information and configuration, also driver data, etc
55 * @ver: sprd IOMMU IP version
56 * @prot_page_va: protect page base virtual address
57 * @prot_page_pa: protect page base physical address, data would be
58 * written to here while translation fault
59 * @base: mapped base address for accessing registers
60 * @dev: pointer to basic device structure
61 * @iommu: IOMMU core representation
63 * @eb: gate clock which controls IOMMU access
65 struct sprd_iommu_device {
66 enum sprd_iommu_version ver;
68 dma_addr_t prot_page_pa;
71 struct iommu_device iommu;
72 struct iommu_group *group;
76 struct sprd_iommu_domain {
77 spinlock_t pgtlock; /* lock for page table */
78 struct iommu_domain domain;
79 u32 *pgt_va; /* page table virtual address base */
80 dma_addr_t pgt_pa; /* page table physical address base */
81 struct sprd_iommu_device *sdev;
84 static const struct iommu_ops sprd_iommu_ops;
86 static struct sprd_iommu_domain *to_sprd_domain(struct iommu_domain *dom)
88 return container_of(dom, struct sprd_iommu_domain, domain);
92 sprd_iommu_write(struct sprd_iommu_device *sdev, unsigned int reg, u32 val)
94 writel_relaxed(val, sdev->base + reg);
98 sprd_iommu_read(struct sprd_iommu_device *sdev, unsigned int reg)
100 return readl_relaxed(sdev->base + reg);
104 sprd_iommu_update_bits(struct sprd_iommu_device *sdev, unsigned int reg,
105 u32 mask, u32 shift, u32 val)
107 u32 t = sprd_iommu_read(sdev, reg);
109 t = (t & (~(mask << shift))) | ((val & mask) << shift);
110 sprd_iommu_write(sdev, reg, t);
114 sprd_iommu_get_version(struct sprd_iommu_device *sdev)
116 int ver = (sprd_iommu_read(sdev, SPRD_IOMMU_VERSION) &
117 SPRD_VERSION_MASK) >> SPRD_VERSION_SHIFT;
129 sprd_iommu_pgt_size(struct iommu_domain *domain)
131 return ((domain->geometry.aperture_end -
132 domain->geometry.aperture_start + 1) >>
133 SPRD_IOMMU_PAGE_SHIFT) * sizeof(u32);
136 static struct iommu_domain *sprd_iommu_domain_alloc(unsigned int domain_type)
138 struct sprd_iommu_domain *dom;
140 if (domain_type != IOMMU_DOMAIN_DMA && domain_type != IOMMU_DOMAIN_UNMANAGED)
143 dom = kzalloc(sizeof(*dom), GFP_KERNEL);
147 if (iommu_get_dma_cookie(&dom->domain)) {
152 spin_lock_init(&dom->pgtlock);
154 dom->domain.geometry.aperture_start = 0;
155 dom->domain.geometry.aperture_end = SZ_256M - 1;
160 static void sprd_iommu_domain_free(struct iommu_domain *domain)
162 struct sprd_iommu_domain *dom = to_sprd_domain(domain);
164 iommu_put_dma_cookie(domain);
168 static void sprd_iommu_first_vpn(struct sprd_iommu_domain *dom)
170 struct sprd_iommu_device *sdev = dom->sdev;
174 if (sdev->ver == SPRD_IOMMU_EX)
175 reg = SPRD_EX_FIRST_VPN;
177 reg = SPRD_VAU_FIRST_VPN;
179 val = dom->domain.geometry.aperture_start >> SPRD_IOMMU_PAGE_SHIFT;
180 sprd_iommu_write(sdev, reg, val);
183 static void sprd_iommu_vpn_range(struct sprd_iommu_domain *dom)
185 struct sprd_iommu_device *sdev = dom->sdev;
189 if (sdev->ver == SPRD_IOMMU_EX)
190 reg = SPRD_EX_VPN_RANGE;
192 reg = SPRD_VAU_VPN_RANGE;
194 val = (dom->domain.geometry.aperture_end -
195 dom->domain.geometry.aperture_start) >> SPRD_IOMMU_PAGE_SHIFT;
196 sprd_iommu_write(sdev, reg, val);
199 static void sprd_iommu_first_ppn(struct sprd_iommu_domain *dom)
201 u32 val = dom->pgt_pa >> SPRD_IOMMU_PAGE_SHIFT;
202 struct sprd_iommu_device *sdev = dom->sdev;
205 if (sdev->ver == SPRD_IOMMU_EX)
206 reg = SPRD_EX_FIRST_PPN;
208 reg = SPRD_VAU_FIRST_PPN;
210 sprd_iommu_write(sdev, reg, val);
213 static void sprd_iommu_default_ppn(struct sprd_iommu_device *sdev)
215 u32 val = sdev->prot_page_pa >> SPRD_IOMMU_PAGE_SHIFT;
217 if (sdev->ver == SPRD_IOMMU_EX) {
218 sprd_iommu_write(sdev, SPRD_EX_DEFAULT_PPN, val);
219 } else if (sdev->ver == SPRD_IOMMU_VAU) {
220 sprd_iommu_write(sdev, SPRD_VAU_DEFAULT_PPN_RD, val);
221 sprd_iommu_write(sdev, SPRD_VAU_DEFAULT_PPN_WR, val);
225 static void sprd_iommu_hw_en(struct sprd_iommu_device *sdev, bool en)
227 unsigned int reg_cfg;
230 if (sdev->ver == SPRD_IOMMU_EX)
231 reg_cfg = SPRD_EX_CFG;
233 reg_cfg = SPRD_VAU_CFG;
235 mask = SPRD_IOMMU_EN | SPRD_IOMMU_GATE_EN;
237 sprd_iommu_update_bits(sdev, reg_cfg, mask, 0, val);
240 static int sprd_iommu_attach_device(struct iommu_domain *domain,
243 struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev);
244 struct sprd_iommu_domain *dom = to_sprd_domain(domain);
245 size_t pgt_size = sprd_iommu_pgt_size(domain);
248 pr_err("There's already a device attached to this domain.\n");
252 dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size, &dom->pgt_pa, GFP_KERNEL);
258 sprd_iommu_first_ppn(dom);
259 sprd_iommu_first_vpn(dom);
260 sprd_iommu_vpn_range(dom);
261 sprd_iommu_default_ppn(sdev);
262 sprd_iommu_hw_en(sdev, true);
267 static void sprd_iommu_detach_device(struct iommu_domain *domain,
270 struct sprd_iommu_domain *dom = to_sprd_domain(domain);
271 struct sprd_iommu_device *sdev = dom->sdev;
272 size_t pgt_size = sprd_iommu_pgt_size(domain);
277 dma_free_coherent(sdev->dev, pgt_size, dom->pgt_va, dom->pgt_pa);
278 sprd_iommu_hw_en(sdev, false);
282 static int sprd_iommu_map(struct iommu_domain *domain, unsigned long iova,
283 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
285 struct sprd_iommu_domain *dom = to_sprd_domain(domain);
286 unsigned int page_num = size >> SPRD_IOMMU_PAGE_SHIFT;
290 u32 pabase = (u32)paddr;
291 unsigned long start = domain->geometry.aperture_start;
292 unsigned long end = domain->geometry.aperture_end;
295 pr_err("No sprd_iommu_device attached to the domain\n");
299 if (iova < start || (iova + size) > (end + 1)) {
300 dev_err(dom->sdev->dev, "(iova(0x%lx) + sixe(%zx)) are not in the range!\n",
305 pgt_base_iova = dom->pgt_va + ((iova - start) >> SPRD_IOMMU_PAGE_SHIFT);
307 spin_lock_irqsave(&dom->pgtlock, flags);
308 for (i = 0; i < page_num; i++) {
309 pgt_base_iova[i] = pabase >> SPRD_IOMMU_PAGE_SHIFT;
310 pabase += SPRD_IOMMU_PAGE_SIZE;
312 spin_unlock_irqrestore(&dom->pgtlock, flags);
317 static size_t sprd_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
318 size_t size, struct iommu_iotlb_gather *iotlb_gather)
320 struct sprd_iommu_domain *dom = to_sprd_domain(domain);
323 unsigned int page_num = size >> SPRD_IOMMU_PAGE_SHIFT;
324 unsigned long start = domain->geometry.aperture_start;
325 unsigned long end = domain->geometry.aperture_end;
327 if (iova < start || (iova + size) > (end + 1))
330 pgt_base_iova = dom->pgt_va + ((iova - start) >> SPRD_IOMMU_PAGE_SHIFT);
332 spin_lock_irqsave(&dom->pgtlock, flags);
333 memset(pgt_base_iova, 0, page_num * sizeof(u32));
334 spin_unlock_irqrestore(&dom->pgtlock, flags);
339 static void sprd_iommu_sync_map(struct iommu_domain *domain,
340 unsigned long iova, size_t size)
342 struct sprd_iommu_domain *dom = to_sprd_domain(domain);
345 if (dom->sdev->ver == SPRD_IOMMU_EX)
346 reg = SPRD_EX_UPDATE;
348 reg = SPRD_VAU_UPDATE;
350 /* clear IOMMU TLB buffer after page table updated */
351 sprd_iommu_write(dom->sdev, reg, 0xffffffff);
354 static void sprd_iommu_sync(struct iommu_domain *domain,
355 struct iommu_iotlb_gather *iotlb_gather)
357 sprd_iommu_sync_map(domain, 0, 0);
360 static phys_addr_t sprd_iommu_iova_to_phys(struct iommu_domain *domain,
363 struct sprd_iommu_domain *dom = to_sprd_domain(domain);
366 unsigned long start = domain->geometry.aperture_start;
367 unsigned long end = domain->geometry.aperture_end;
369 if (WARN_ON(iova < start || iova > end))
372 spin_lock_irqsave(&dom->pgtlock, flags);
373 pa = *(dom->pgt_va + ((iova - start) >> SPRD_IOMMU_PAGE_SHIFT));
374 pa = (pa << SPRD_IOMMU_PAGE_SHIFT) + ((iova - start) & (SPRD_IOMMU_PAGE_SIZE - 1));
375 spin_unlock_irqrestore(&dom->pgtlock, flags);
380 static struct iommu_device *sprd_iommu_probe_device(struct device *dev)
382 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
383 struct sprd_iommu_device *sdev;
385 if (!fwspec || fwspec->ops != &sprd_iommu_ops)
386 return ERR_PTR(-ENODEV);
388 sdev = dev_iommu_priv_get(dev);
393 static void sprd_iommu_release_device(struct device *dev)
395 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
397 if (!fwspec || fwspec->ops != &sprd_iommu_ops)
400 iommu_fwspec_free(dev);
403 static struct iommu_group *sprd_iommu_device_group(struct device *dev)
405 struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev);
407 return iommu_group_ref_get(sdev->group);
410 static int sprd_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
412 struct platform_device *pdev;
414 if (!dev_iommu_priv_get(dev)) {
415 pdev = of_find_device_by_node(args->np);
416 dev_iommu_priv_set(dev, platform_get_drvdata(pdev));
417 platform_device_put(pdev);
424 static const struct iommu_ops sprd_iommu_ops = {
425 .domain_alloc = sprd_iommu_domain_alloc,
426 .domain_free = sprd_iommu_domain_free,
427 .attach_dev = sprd_iommu_attach_device,
428 .detach_dev = sprd_iommu_detach_device,
429 .map = sprd_iommu_map,
430 .unmap = sprd_iommu_unmap,
431 .iotlb_sync_map = sprd_iommu_sync_map,
432 .iotlb_sync = sprd_iommu_sync,
433 .iova_to_phys = sprd_iommu_iova_to_phys,
434 .probe_device = sprd_iommu_probe_device,
435 .release_device = sprd_iommu_release_device,
436 .device_group = sprd_iommu_device_group,
437 .of_xlate = sprd_iommu_of_xlate,
438 .pgsize_bitmap = ~0UL << SPRD_IOMMU_PAGE_SHIFT,
441 static const struct of_device_id sprd_iommu_of_match[] = {
442 { .compatible = "sprd,iommu-v1" },
445 MODULE_DEVICE_TABLE(of, sprd_iommu_of_match);
448 * Clock is not required, access to some of IOMMUs is controlled by gate
449 * clk, enabled clocks for that kind of IOMMUs before accessing.
450 * Return 0 for success or no clocks found.
452 static int sprd_iommu_clk_enable(struct sprd_iommu_device *sdev)
456 eb = clk_get_optional(sdev->dev, 0);
464 return clk_prepare_enable(eb);
467 static void sprd_iommu_clk_disable(struct sprd_iommu_device *sdev)
470 clk_disable_unprepare(sdev->eb);
473 static int sprd_iommu_probe(struct platform_device *pdev)
475 struct sprd_iommu_device *sdev;
476 struct device *dev = &pdev->dev;
480 sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL);
484 base = devm_platform_ioremap_resource(pdev, 0);
486 dev_err(dev, "Failed to get ioremap resource.\n");
487 return PTR_ERR(base);
491 sdev->prot_page_va = dma_alloc_coherent(dev, SPRD_IOMMU_PAGE_SIZE,
492 &sdev->prot_page_pa, GFP_KERNEL);
493 if (!sdev->prot_page_va)
496 platform_set_drvdata(pdev, sdev);
499 /* All the client devices are in the same iommu-group */
500 sdev->group = iommu_group_alloc();
501 if (IS_ERR(sdev->group)) {
502 ret = PTR_ERR(sdev->group);
506 ret = iommu_device_sysfs_add(&sdev->iommu, dev, NULL, dev_name(dev));
510 iommu_device_set_ops(&sdev->iommu, &sprd_iommu_ops);
511 iommu_device_set_fwnode(&sdev->iommu, &dev->of_node->fwnode);
513 ret = iommu_device_register(&sdev->iommu);
517 if (!iommu_present(&platform_bus_type))
518 bus_set_iommu(&platform_bus_type, &sprd_iommu_ops);
520 ret = sprd_iommu_clk_enable(sdev);
522 goto unregister_iommu;
524 ret = sprd_iommu_get_version(sdev);
526 dev_err(dev, "IOMMU version(%d) is invalid.\n", ret);
534 sprd_iommu_clk_disable(sdev);
536 iommu_device_unregister(&sdev->iommu);
538 iommu_device_sysfs_remove(&sdev->iommu);
540 iommu_group_put(sdev->group);
542 dma_free_coherent(sdev->dev, SPRD_IOMMU_PAGE_SIZE, sdev->prot_page_va, sdev->prot_page_pa);
546 static int sprd_iommu_remove(struct platform_device *pdev)
548 struct sprd_iommu_device *sdev = platform_get_drvdata(pdev);
550 dma_free_coherent(sdev->dev, SPRD_IOMMU_PAGE_SIZE, sdev->prot_page_va, sdev->prot_page_pa);
552 iommu_group_put(sdev->group);
555 bus_set_iommu(&platform_bus_type, NULL);
557 platform_set_drvdata(pdev, NULL);
558 iommu_device_sysfs_remove(&sdev->iommu);
559 iommu_device_unregister(&sdev->iommu);
564 static struct platform_driver sprd_iommu_driver = {
566 .name = "sprd-iommu",
567 .of_match_table = sprd_iommu_of_match,
568 .suppress_bind_attrs = true,
570 .probe = sprd_iommu_probe,
571 .remove = sprd_iommu_remove,
573 module_platform_driver(sprd_iommu_driver);
575 MODULE_DESCRIPTION("IOMMU driver for Unisoc SoCs");
576 MODULE_ALIAS("platform:sprd-iommu");
577 MODULE_LICENSE("GPL");