1 // SPDX-License-Identifier: GPL-2.0-only
3 * Apple DART (Device Address Resolution Table) IOMMU driver
5 * Copyright (C) 2021 The Asahi Linux Contributors
7 * Based on arm/arm-smmu/arm-ssmu.c and arm/arm-smmu-v3/arm-smmu-v3.c
8 * Copyright (C) 2013 ARM Limited
9 * Copyright (C) 2015 ARM Limited
10 * and on exynos-iommu.c
11 * Copyright (c) 2011,2016 Samsung Electronics Co., Ltd.
14 #include <linux/atomic.h>
15 #include <linux/bitfield.h>
16 #include <linux/clk.h>
17 #include <linux/dev_printk.h>
18 #include <linux/dma-iommu.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/err.h>
21 #include <linux/interrupt.h>
22 #include <linux/io-pgtable.h>
23 #include <linux/iommu.h>
24 #include <linux/iopoll.h>
25 #include <linux/module.h>
27 #include <linux/of_address.h>
28 #include <linux/of_iommu.h>
29 #include <linux/of_platform.h>
30 #include <linux/pci.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <linux/swab.h>
34 #include <linux/types.h>
36 #define DART_MAX_STREAMS 16
37 #define DART_MAX_TTBR 4
38 #define MAX_DARTS_PER_DEVICE 2
40 #define DART_STREAM_ALL 0xffff
42 #define DART_PARAMS1 0x00
43 #define DART_PARAMS_PAGE_SHIFT GENMASK(27, 24)
45 #define DART_PARAMS2 0x04
46 #define DART_PARAMS_BYPASS_SUPPORT BIT(0)
48 #define DART_STREAM_COMMAND 0x20
49 #define DART_STREAM_COMMAND_BUSY BIT(2)
50 #define DART_STREAM_COMMAND_INVALIDATE BIT(20)
52 #define DART_STREAM_SELECT 0x34
54 #define DART_ERROR 0x40
55 #define DART_ERROR_STREAM GENMASK(27, 24)
56 #define DART_ERROR_CODE GENMASK(11, 0)
57 #define DART_ERROR_FLAG BIT(31)
59 #define DART_ERROR_READ_FAULT BIT(4)
60 #define DART_ERROR_WRITE_FAULT BIT(3)
61 #define DART_ERROR_NO_PTE BIT(2)
62 #define DART_ERROR_NO_PMD BIT(1)
63 #define DART_ERROR_NO_TTBR BIT(0)
65 #define DART_CONFIG 0x60
66 #define DART_CONFIG_LOCK BIT(15)
68 #define DART_STREAM_COMMAND_BUSY_TIMEOUT 100
70 #define DART_ERROR_ADDR_HI 0x54
71 #define DART_ERROR_ADDR_LO 0x50
73 #define DART_STREAMS_ENABLE 0xfc
75 #define DART_TCR(sid) (0x100 + 4 * (sid))
76 #define DART_TCR_TRANSLATE_ENABLE BIT(7)
77 #define DART_TCR_BYPASS0_ENABLE BIT(8)
78 #define DART_TCR_BYPASS1_ENABLE BIT(12)
80 #define DART_TTBR(sid, idx) (0x200 + 16 * (sid) + 4 * (idx))
81 #define DART_TTBR_VALID BIT(31)
82 #define DART_TTBR_SHIFT 12
85 * Private structure associated with each DART device.
88 * @regs: mapped MMIO region
89 * @irq: interrupt number, can be shared with other DARTs
90 * @clks: clocks associated with this DART
91 * @num_clks: number of @clks
92 * @lock: lock for hardware operations involving this dart
93 * @pgsize: pagesize supported by this DART
94 * @supports_bypass: indicates if this DART supports bypass mode
95 * @force_bypass: force bypass mode due to pagesize mismatch?
96 * @sid2group: maps stream ids to iommu_groups
97 * @iommu: iommu core device
105 struct clk_bulk_data *clks;
111 u32 supports_bypass : 1;
112 u32 force_bypass : 1;
114 struct iommu_group *sid2group[DART_MAX_STREAMS];
115 struct iommu_device iommu;
119 * Convenience struct to identify streams.
121 * The normal variant is used inside apple_dart_master_cfg which isn't written
123 * The atomic variant is used inside apple_dart_domain where we have to guard
124 * against races from potential parallel calls to attach/detach_device.
125 * Note that even inside the atomic variant the apple_dart pointer is not
126 * protected: This pointer is initialized once under the domain init mutex
127 * and never changed again afterwards. Devices with different dart pointers
128 * cannot be attached to the same domain.
131 * @sid stream id bitmap
133 struct apple_dart_stream_map {
134 struct apple_dart *dart;
135 unsigned long sidmap;
137 struct apple_dart_atomic_stream_map {
138 struct apple_dart *dart;
143 * This structure is attached to each iommu domain handled by a DART.
145 * @pgtbl_ops: pagetable ops allocated by io-pgtable
146 * @finalized: true if the domain has been completely initialized
147 * @init_lock: protects domain initialization
148 * @stream_maps: streams attached to this domain (valid for DMA/UNMANAGED only)
149 * @domain: core iommu domain pointer
151 struct apple_dart_domain {
152 struct io_pgtable_ops *pgtbl_ops;
155 struct mutex init_lock;
156 struct apple_dart_atomic_stream_map stream_maps[MAX_DARTS_PER_DEVICE];
158 struct iommu_domain domain;
162 * This structure is attached to devices with dev_iommu_priv_set() on of_xlate
163 * and contains a list of streams bound to this device.
164 * So far the worst case seen is a single device with two streams
165 * from different darts, such that this simple static array is enough.
167 * @streams: streams for this device
169 struct apple_dart_master_cfg {
170 struct apple_dart_stream_map stream_maps[MAX_DARTS_PER_DEVICE];
174 * Helper macro to iterate over apple_dart_master_cfg.stream_maps and
175 * apple_dart_domain.stream_maps
177 * @i int used as loop variable
178 * @base pointer to base struct (apple_dart_master_cfg or apple_dart_domain)
179 * @stream pointer to the apple_dart_streams struct for each loop iteration
181 #define for_each_stream_map(i, base, stream_map) \
182 for (i = 0, stream_map = &(base)->stream_maps[0]; \
183 i < MAX_DARTS_PER_DEVICE && stream_map->dart; \
184 stream_map = &(base)->stream_maps[++i])
186 static struct platform_driver apple_dart_driver;
187 static const struct iommu_ops apple_dart_iommu_ops;
189 static struct apple_dart_domain *to_dart_domain(struct iommu_domain *dom)
191 return container_of(dom, struct apple_dart_domain, domain);
195 apple_dart_hw_enable_translation(struct apple_dart_stream_map *stream_map)
199 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
200 writel(DART_TCR_TRANSLATE_ENABLE,
201 stream_map->dart->regs + DART_TCR(sid));
204 static void apple_dart_hw_disable_dma(struct apple_dart_stream_map *stream_map)
208 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
209 writel(0, stream_map->dart->regs + DART_TCR(sid));
213 apple_dart_hw_enable_bypass(struct apple_dart_stream_map *stream_map)
217 WARN_ON(!stream_map->dart->supports_bypass);
218 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
219 writel(DART_TCR_BYPASS0_ENABLE | DART_TCR_BYPASS1_ENABLE,
220 stream_map->dart->regs + DART_TCR(sid));
223 static void apple_dart_hw_set_ttbr(struct apple_dart_stream_map *stream_map,
224 u8 idx, phys_addr_t paddr)
228 WARN_ON(paddr & ((1 << DART_TTBR_SHIFT) - 1));
229 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
230 writel(DART_TTBR_VALID | (paddr >> DART_TTBR_SHIFT),
231 stream_map->dart->regs + DART_TTBR(sid, idx));
234 static void apple_dart_hw_clear_ttbr(struct apple_dart_stream_map *stream_map,
239 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
240 writel(0, stream_map->dart->regs + DART_TTBR(sid, idx));
244 apple_dart_hw_clear_all_ttbrs(struct apple_dart_stream_map *stream_map)
248 for (i = 0; i < DART_MAX_TTBR; ++i)
249 apple_dart_hw_clear_ttbr(stream_map, i);
253 apple_dart_hw_stream_command(struct apple_dart_stream_map *stream_map,
260 spin_lock_irqsave(&stream_map->dart->lock, flags);
262 writel(stream_map->sidmap, stream_map->dart->regs + DART_STREAM_SELECT);
263 writel(command, stream_map->dart->regs + DART_STREAM_COMMAND);
265 ret = readl_poll_timeout_atomic(
266 stream_map->dart->regs + DART_STREAM_COMMAND, command_reg,
267 !(command_reg & DART_STREAM_COMMAND_BUSY), 1,
268 DART_STREAM_COMMAND_BUSY_TIMEOUT);
270 spin_unlock_irqrestore(&stream_map->dart->lock, flags);
273 dev_err(stream_map->dart->dev,
274 "busy bit did not clear after command %x for streams %lx\n",
275 command, stream_map->sidmap);
283 apple_dart_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map)
285 return apple_dart_hw_stream_command(stream_map,
286 DART_STREAM_COMMAND_INVALIDATE);
289 static int apple_dart_hw_reset(struct apple_dart *dart)
292 struct apple_dart_stream_map stream_map;
294 config = readl(dart->regs + DART_CONFIG);
295 if (config & DART_CONFIG_LOCK) {
296 dev_err(dart->dev, "DART is locked down until reboot: %08x\n",
301 stream_map.dart = dart;
302 stream_map.sidmap = DART_STREAM_ALL;
303 apple_dart_hw_disable_dma(&stream_map);
304 apple_dart_hw_clear_all_ttbrs(&stream_map);
306 /* enable all streams globally since TCR is used to control isolation */
307 writel(DART_STREAM_ALL, dart->regs + DART_STREAMS_ENABLE);
309 /* clear any pending errors before the interrupt is unmasked */
310 writel(readl(dart->regs + DART_ERROR), dart->regs + DART_ERROR);
312 return apple_dart_hw_invalidate_tlb(&stream_map);
315 static void apple_dart_domain_flush_tlb(struct apple_dart_domain *domain)
318 struct apple_dart_atomic_stream_map *domain_stream_map;
319 struct apple_dart_stream_map stream_map;
321 for_each_stream_map(i, domain, domain_stream_map) {
322 stream_map.dart = domain_stream_map->dart;
323 stream_map.sidmap = atomic64_read(&domain_stream_map->sidmap);
324 apple_dart_hw_invalidate_tlb(&stream_map);
328 static void apple_dart_flush_iotlb_all(struct iommu_domain *domain)
330 apple_dart_domain_flush_tlb(to_dart_domain(domain));
333 static void apple_dart_iotlb_sync(struct iommu_domain *domain,
334 struct iommu_iotlb_gather *gather)
336 apple_dart_domain_flush_tlb(to_dart_domain(domain));
339 static void apple_dart_iotlb_sync_map(struct iommu_domain *domain,
340 unsigned long iova, size_t size)
342 apple_dart_domain_flush_tlb(to_dart_domain(domain));
345 static phys_addr_t apple_dart_iova_to_phys(struct iommu_domain *domain,
348 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
349 struct io_pgtable_ops *ops = dart_domain->pgtbl_ops;
354 return ops->iova_to_phys(ops, iova);
357 static int apple_dart_map_pages(struct iommu_domain *domain, unsigned long iova,
358 phys_addr_t paddr, size_t pgsize,
359 size_t pgcount, int prot, gfp_t gfp,
362 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
363 struct io_pgtable_ops *ops = dart_domain->pgtbl_ops;
368 return ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, gfp,
372 static size_t apple_dart_unmap_pages(struct iommu_domain *domain,
373 unsigned long iova, size_t pgsize,
375 struct iommu_iotlb_gather *gather)
377 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
378 struct io_pgtable_ops *ops = dart_domain->pgtbl_ops;
380 return ops->unmap_pages(ops, iova, pgsize, pgcount, gather);
384 apple_dart_setup_translation(struct apple_dart_domain *domain,
385 struct apple_dart_stream_map *stream_map)
388 struct io_pgtable_cfg *pgtbl_cfg =
389 &io_pgtable_ops_to_pgtable(domain->pgtbl_ops)->cfg;
391 for (i = 0; i < pgtbl_cfg->apple_dart_cfg.n_ttbrs; ++i)
392 apple_dart_hw_set_ttbr(stream_map, i,
393 pgtbl_cfg->apple_dart_cfg.ttbr[i]);
394 for (; i < DART_MAX_TTBR; ++i)
395 apple_dart_hw_clear_ttbr(stream_map, i);
397 apple_dart_hw_enable_translation(stream_map);
398 apple_dart_hw_invalidate_tlb(stream_map);
401 static int apple_dart_finalize_domain(struct iommu_domain *domain,
402 struct apple_dart_master_cfg *cfg)
404 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
405 struct apple_dart *dart = cfg->stream_maps[0].dart;
406 struct io_pgtable_cfg pgtbl_cfg;
410 mutex_lock(&dart_domain->init_lock);
412 if (dart_domain->finalized)
415 for (i = 0; i < MAX_DARTS_PER_DEVICE; ++i) {
416 dart_domain->stream_maps[i].dart = cfg->stream_maps[i].dart;
417 atomic64_set(&dart_domain->stream_maps[i].sidmap,
418 cfg->stream_maps[i].sidmap);
421 pgtbl_cfg = (struct io_pgtable_cfg){
422 .pgsize_bitmap = dart->pgsize,
426 .iommu_dev = dart->dev,
429 dart_domain->pgtbl_ops =
430 alloc_io_pgtable_ops(APPLE_DART, &pgtbl_cfg, domain);
431 if (!dart_domain->pgtbl_ops) {
436 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
437 domain->geometry.aperture_start = 0;
438 domain->geometry.aperture_end = DMA_BIT_MASK(32);
439 domain->geometry.force_aperture = true;
441 dart_domain->finalized = true;
444 mutex_unlock(&dart_domain->init_lock);
449 apple_dart_mod_streams(struct apple_dart_atomic_stream_map *domain_maps,
450 struct apple_dart_stream_map *master_maps,
455 for (i = 0; i < MAX_DARTS_PER_DEVICE; ++i) {
456 if (domain_maps[i].dart != master_maps[i].dart)
460 for (i = 0; i < MAX_DARTS_PER_DEVICE; ++i) {
461 if (!domain_maps[i].dart)
464 atomic64_or(master_maps[i].sidmap,
465 &domain_maps[i].sidmap);
467 atomic64_and(~master_maps[i].sidmap,
468 &domain_maps[i].sidmap);
474 static int apple_dart_domain_add_streams(struct apple_dart_domain *domain,
475 struct apple_dart_master_cfg *cfg)
477 return apple_dart_mod_streams(domain->stream_maps, cfg->stream_maps,
481 static int apple_dart_domain_remove_streams(struct apple_dart_domain *domain,
482 struct apple_dart_master_cfg *cfg)
484 return apple_dart_mod_streams(domain->stream_maps, cfg->stream_maps,
488 static int apple_dart_attach_dev(struct iommu_domain *domain,
492 struct apple_dart_stream_map *stream_map;
493 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
494 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
496 if (cfg->stream_maps[0].dart->force_bypass &&
497 domain->type != IOMMU_DOMAIN_IDENTITY)
499 if (!cfg->stream_maps[0].dart->supports_bypass &&
500 domain->type == IOMMU_DOMAIN_IDENTITY)
503 ret = apple_dart_finalize_domain(domain, cfg);
507 switch (domain->type) {
508 case IOMMU_DOMAIN_DMA:
509 case IOMMU_DOMAIN_UNMANAGED:
510 ret = apple_dart_domain_add_streams(dart_domain, cfg);
514 for_each_stream_map(i, cfg, stream_map)
515 apple_dart_setup_translation(dart_domain, stream_map);
517 case IOMMU_DOMAIN_BLOCKED:
518 for_each_stream_map(i, cfg, stream_map)
519 apple_dart_hw_disable_dma(stream_map);
521 case IOMMU_DOMAIN_IDENTITY:
522 for_each_stream_map(i, cfg, stream_map)
523 apple_dart_hw_enable_bypass(stream_map);
530 static void apple_dart_detach_dev(struct iommu_domain *domain,
534 struct apple_dart_stream_map *stream_map;
535 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
536 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
538 for_each_stream_map(i, cfg, stream_map)
539 apple_dart_hw_disable_dma(stream_map);
541 if (domain->type == IOMMU_DOMAIN_DMA ||
542 domain->type == IOMMU_DOMAIN_UNMANAGED)
543 apple_dart_domain_remove_streams(dart_domain, cfg);
546 static struct iommu_device *apple_dart_probe_device(struct device *dev)
548 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
549 struct apple_dart_stream_map *stream_map;
553 return ERR_PTR(-ENODEV);
555 for_each_stream_map(i, cfg, stream_map)
557 dev, stream_map->dart->dev,
558 DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER);
560 return &cfg->stream_maps[0].dart->iommu;
563 static void apple_dart_release_device(struct device *dev)
565 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
570 dev_iommu_priv_set(dev, NULL);
574 static struct iommu_domain *apple_dart_domain_alloc(unsigned int type)
576 struct apple_dart_domain *dart_domain;
578 if (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_UNMANAGED &&
579 type != IOMMU_DOMAIN_IDENTITY && type != IOMMU_DOMAIN_BLOCKED)
582 dart_domain = kzalloc(sizeof(*dart_domain), GFP_KERNEL);
586 iommu_get_dma_cookie(&dart_domain->domain);
587 mutex_init(&dart_domain->init_lock);
589 /* no need to allocate pgtbl_ops or do any other finalization steps */
590 if (type == IOMMU_DOMAIN_IDENTITY || type == IOMMU_DOMAIN_BLOCKED)
591 dart_domain->finalized = true;
593 return &dart_domain->domain;
596 static void apple_dart_domain_free(struct iommu_domain *domain)
598 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
600 if (dart_domain->pgtbl_ops)
601 free_io_pgtable_ops(dart_domain->pgtbl_ops);
606 static int apple_dart_of_xlate(struct device *dev, struct of_phandle_args *args)
608 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
609 struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
610 struct apple_dart *dart = platform_get_drvdata(iommu_pdev);
611 struct apple_dart *cfg_dart;
614 if (args->args_count != 1)
619 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
622 dev_iommu_priv_set(dev, cfg);
624 cfg_dart = cfg->stream_maps[0].dart;
626 if (cfg_dart->supports_bypass != dart->supports_bypass)
628 if (cfg_dart->force_bypass != dart->force_bypass)
630 if (cfg_dart->pgsize != dart->pgsize)
634 for (i = 0; i < MAX_DARTS_PER_DEVICE; ++i) {
635 if (cfg->stream_maps[i].dart == dart) {
636 cfg->stream_maps[i].sidmap |= 1 << sid;
640 for (i = 0; i < MAX_DARTS_PER_DEVICE; ++i) {
641 if (!cfg->stream_maps[i].dart) {
642 cfg->stream_maps[i].dart = dart;
643 cfg->stream_maps[i].sidmap = 1 << sid;
651 static DEFINE_MUTEX(apple_dart_groups_lock);
653 static void apple_dart_release_group(void *iommu_data)
656 struct apple_dart_stream_map *stream_map;
657 struct apple_dart_master_cfg *group_master_cfg = iommu_data;
659 mutex_lock(&apple_dart_groups_lock);
661 for_each_stream_map(i, group_master_cfg, stream_map)
662 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
663 stream_map->dart->sid2group[sid] = NULL;
666 mutex_unlock(&apple_dart_groups_lock);
669 static struct iommu_group *apple_dart_device_group(struct device *dev)
672 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
673 struct apple_dart_stream_map *stream_map;
674 struct apple_dart_master_cfg *group_master_cfg;
675 struct iommu_group *group = NULL;
676 struct iommu_group *res = ERR_PTR(-EINVAL);
678 mutex_lock(&apple_dart_groups_lock);
680 for_each_stream_map(i, cfg, stream_map) {
681 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS) {
682 struct iommu_group *stream_group =
683 stream_map->dart->sid2group[sid];
685 if (group && group != stream_group) {
686 res = ERR_PTR(-EINVAL);
690 group = stream_group;
695 res = iommu_group_ref_get(group);
701 group = pci_device_group(dev);
704 group = generic_device_group(dev);
706 res = ERR_PTR(-ENOMEM);
710 group_master_cfg = kzalloc(sizeof(*group_master_cfg), GFP_KERNEL);
711 if (!group_master_cfg) {
712 iommu_group_put(group);
716 memcpy(group_master_cfg, cfg, sizeof(*group_master_cfg));
717 iommu_group_set_iommudata(group, group_master_cfg,
718 apple_dart_release_group);
720 for_each_stream_map(i, cfg, stream_map)
721 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
722 stream_map->dart->sid2group[sid] = group;
727 mutex_unlock(&apple_dart_groups_lock);
731 static int apple_dart_def_domain_type(struct device *dev)
733 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
735 if (cfg->stream_maps[0].dart->force_bypass)
736 return IOMMU_DOMAIN_IDENTITY;
737 if (!cfg->stream_maps[0].dart->supports_bypass)
738 return IOMMU_DOMAIN_DMA;
743 static const struct iommu_ops apple_dart_iommu_ops = {
744 .domain_alloc = apple_dart_domain_alloc,
745 .domain_free = apple_dart_domain_free,
746 .attach_dev = apple_dart_attach_dev,
747 .detach_dev = apple_dart_detach_dev,
748 .map_pages = apple_dart_map_pages,
749 .unmap_pages = apple_dart_unmap_pages,
750 .flush_iotlb_all = apple_dart_flush_iotlb_all,
751 .iotlb_sync = apple_dart_iotlb_sync,
752 .iotlb_sync_map = apple_dart_iotlb_sync_map,
753 .iova_to_phys = apple_dart_iova_to_phys,
754 .probe_device = apple_dart_probe_device,
755 .release_device = apple_dart_release_device,
756 .device_group = apple_dart_device_group,
757 .of_xlate = apple_dart_of_xlate,
758 .def_domain_type = apple_dart_def_domain_type,
759 .pgsize_bitmap = -1UL, /* Restricted during dart probe */
760 .owner = THIS_MODULE,
763 static irqreturn_t apple_dart_irq(int irq, void *dev)
765 struct apple_dart *dart = dev;
766 const char *fault_name = NULL;
767 u32 error = readl(dart->regs + DART_ERROR);
768 u32 error_code = FIELD_GET(DART_ERROR_CODE, error);
769 u32 addr_lo = readl(dart->regs + DART_ERROR_ADDR_LO);
770 u32 addr_hi = readl(dart->regs + DART_ERROR_ADDR_HI);
771 u64 addr = addr_lo | (((u64)addr_hi) << 32);
772 u8 stream_idx = FIELD_GET(DART_ERROR_STREAM, error);
774 if (!(error & DART_ERROR_FLAG))
777 /* there should only be a single bit set but let's use == to be sure */
778 if (error_code == DART_ERROR_READ_FAULT)
779 fault_name = "READ FAULT";
780 else if (error_code == DART_ERROR_WRITE_FAULT)
781 fault_name = "WRITE FAULT";
782 else if (error_code == DART_ERROR_NO_PTE)
783 fault_name = "NO PTE FOR IOVA";
784 else if (error_code == DART_ERROR_NO_PMD)
785 fault_name = "NO PMD FOR IOVA";
786 else if (error_code == DART_ERROR_NO_TTBR)
787 fault_name = "NO TTBR FOR IOVA";
789 fault_name = "unknown";
793 "translation fault: status:0x%x stream:%d code:0x%x (%s) at 0x%llx",
794 error, stream_idx, error_code, fault_name, addr);
796 writel(error, dart->regs + DART_ERROR);
800 static int apple_dart_set_bus_ops(const struct iommu_ops *ops)
804 if (!iommu_present(&platform_bus_type)) {
805 ret = bus_set_iommu(&platform_bus_type, ops);
810 if (!iommu_present(&pci_bus_type)) {
811 ret = bus_set_iommu(&pci_bus_type, ops);
813 bus_set_iommu(&platform_bus_type, NULL);
821 static int apple_dart_probe(struct platform_device *pdev)
825 struct resource *res;
826 struct apple_dart *dart;
827 struct device *dev = &pdev->dev;
829 dart = devm_kzalloc(dev, sizeof(*dart), GFP_KERNEL);
834 spin_lock_init(&dart->lock);
836 dart->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
837 if (IS_ERR(dart->regs))
838 return PTR_ERR(dart->regs);
840 if (resource_size(res) < 0x4000) {
841 dev_err(dev, "MMIO region too small (%pr)\n", res);
845 dart->irq = platform_get_irq(pdev, 0);
849 ret = devm_clk_bulk_get_all(dev, &dart->clks);
852 dart->num_clks = ret;
854 ret = clk_bulk_prepare_enable(dart->num_clks, dart->clks);
858 ret = apple_dart_hw_reset(dart);
860 goto err_clk_disable;
862 dart_params[0] = readl(dart->regs + DART_PARAMS1);
863 dart_params[1] = readl(dart->regs + DART_PARAMS2);
864 dart->pgsize = 1 << FIELD_GET(DART_PARAMS_PAGE_SHIFT, dart_params[0]);
865 dart->supports_bypass = dart_params[1] & DART_PARAMS_BYPASS_SUPPORT;
866 dart->force_bypass = dart->pgsize > PAGE_SIZE;
868 ret = request_irq(dart->irq, apple_dart_irq, IRQF_SHARED,
869 "apple-dart fault handler", dart);
871 goto err_clk_disable;
873 platform_set_drvdata(pdev, dart);
875 ret = apple_dart_set_bus_ops(&apple_dart_iommu_ops);
879 ret = iommu_device_sysfs_add(&dart->iommu, dev, NULL, "apple-dart.%s",
880 dev_name(&pdev->dev));
882 goto err_remove_bus_ops;
884 ret = iommu_device_register(&dart->iommu, &apple_dart_iommu_ops, dev);
886 goto err_sysfs_remove;
890 "DART [pagesize %x, bypass support: %d, bypass forced: %d] initialized\n",
891 dart->pgsize, dart->supports_bypass, dart->force_bypass);
895 iommu_device_sysfs_remove(&dart->iommu);
897 apple_dart_set_bus_ops(NULL);
899 free_irq(dart->irq, dart);
901 clk_bulk_disable_unprepare(dart->num_clks, dart->clks);
906 static int apple_dart_remove(struct platform_device *pdev)
908 struct apple_dart *dart = platform_get_drvdata(pdev);
910 apple_dart_hw_reset(dart);
911 free_irq(dart->irq, dart);
912 apple_dart_set_bus_ops(NULL);
914 iommu_device_unregister(&dart->iommu);
915 iommu_device_sysfs_remove(&dart->iommu);
917 clk_bulk_disable_unprepare(dart->num_clks, dart->clks);
922 static const struct of_device_id apple_dart_of_match[] = {
923 { .compatible = "apple,t8103-dart", .data = NULL },
926 MODULE_DEVICE_TABLE(of, apple_dart_of_match);
928 static struct platform_driver apple_dart_driver = {
930 .name = "apple-dart",
931 .of_match_table = apple_dart_of_match,
932 .suppress_bind_attrs = true,
934 .probe = apple_dart_probe,
935 .remove = apple_dart_remove,
938 module_platform_driver(apple_dart_driver);
940 MODULE_DESCRIPTION("IOMMU API for Apple's DART");
941 MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
942 MODULE_LICENSE("GPL v2");