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-mapping.h>
19 #include <linux/err.h>
20 #include <linux/interrupt.h>
21 #include <linux/io-pgtable.h>
22 #include <linux/iommu.h>
23 #include <linux/iopoll.h>
24 #include <linux/module.h>
26 #include <linux/of_address.h>
27 #include <linux/of_iommu.h>
28 #include <linux/of_platform.h>
29 #include <linux/pci.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32 #include <linux/swab.h>
33 #include <linux/types.h>
35 #include "dma-iommu.h"
37 #define DART_MAX_STREAMS 16
38 #define DART_MAX_TTBR 4
39 #define MAX_DARTS_PER_DEVICE 2
41 #define DART_STREAM_ALL 0xffff
43 #define DART_PARAMS1 0x00
44 #define DART_PARAMS_PAGE_SHIFT GENMASK(27, 24)
46 #define DART_PARAMS2 0x04
47 #define DART_PARAMS_BYPASS_SUPPORT BIT(0)
49 #define DART_STREAM_COMMAND 0x20
50 #define DART_STREAM_COMMAND_BUSY BIT(2)
51 #define DART_STREAM_COMMAND_INVALIDATE BIT(20)
53 #define DART_STREAM_SELECT 0x34
55 #define DART_ERROR 0x40
56 #define DART_ERROR_STREAM GENMASK(27, 24)
57 #define DART_ERROR_CODE GENMASK(11, 0)
58 #define DART_ERROR_FLAG BIT(31)
60 #define DART_ERROR_READ_FAULT BIT(4)
61 #define DART_ERROR_WRITE_FAULT BIT(3)
62 #define DART_ERROR_NO_PTE BIT(2)
63 #define DART_ERROR_NO_PMD BIT(1)
64 #define DART_ERROR_NO_TTBR BIT(0)
66 #define DART_CONFIG 0x60
67 #define DART_CONFIG_LOCK BIT(15)
69 #define DART_STREAM_COMMAND_BUSY_TIMEOUT 100
71 #define DART_ERROR_ADDR_HI 0x54
72 #define DART_ERROR_ADDR_LO 0x50
74 #define DART_STREAMS_ENABLE 0xfc
76 #define DART_TCR(sid) (0x100 + 4 * (sid))
77 #define DART_TCR_TRANSLATE_ENABLE BIT(7)
78 #define DART_TCR_BYPASS0_ENABLE BIT(8)
79 #define DART_TCR_BYPASS1_ENABLE BIT(12)
81 #define DART_TTBR(sid, idx) (0x200 + 16 * (sid) + 4 * (idx))
82 #define DART_TTBR_VALID BIT(31)
83 #define DART_TTBR_SHIFT 12
85 struct apple_dart_hw {
87 enum io_pgtable_fmt fmt;
91 * Private structure associated with each DART device.
94 * @hw: SoC-specific hardware data
95 * @regs: mapped MMIO region
96 * @irq: interrupt number, can be shared with other DARTs
97 * @clks: clocks associated with this DART
98 * @num_clks: number of @clks
99 * @lock: lock for hardware operations involving this dart
100 * @pgsize: pagesize supported by this DART
101 * @supports_bypass: indicates if this DART supports bypass mode
102 * @force_bypass: force bypass mode due to pagesize mismatch?
103 * @sid2group: maps stream ids to iommu_groups
104 * @iommu: iommu core device
108 const struct apple_dart_hw *hw;
113 struct clk_bulk_data *clks;
119 u32 supports_bypass : 1;
120 u32 force_bypass : 1;
122 struct iommu_group *sid2group[DART_MAX_STREAMS];
123 struct iommu_device iommu;
127 * Convenience struct to identify streams.
129 * The normal variant is used inside apple_dart_master_cfg which isn't written
131 * The atomic variant is used inside apple_dart_domain where we have to guard
132 * against races from potential parallel calls to attach/detach_device.
133 * Note that even inside the atomic variant the apple_dart pointer is not
134 * protected: This pointer is initialized once under the domain init mutex
135 * and never changed again afterwards. Devices with different dart pointers
136 * cannot be attached to the same domain.
139 * @sid stream id bitmap
141 struct apple_dart_stream_map {
142 struct apple_dart *dart;
143 unsigned long sidmap;
145 struct apple_dart_atomic_stream_map {
146 struct apple_dart *dart;
151 * This structure is attached to each iommu domain handled by a DART.
153 * @pgtbl_ops: pagetable ops allocated by io-pgtable
154 * @finalized: true if the domain has been completely initialized
155 * @init_lock: protects domain initialization
156 * @stream_maps: streams attached to this domain (valid for DMA/UNMANAGED only)
157 * @domain: core iommu domain pointer
159 struct apple_dart_domain {
160 struct io_pgtable_ops *pgtbl_ops;
163 struct mutex init_lock;
164 struct apple_dart_atomic_stream_map stream_maps[MAX_DARTS_PER_DEVICE];
166 struct iommu_domain domain;
170 * This structure is attached to devices with dev_iommu_priv_set() on of_xlate
171 * and contains a list of streams bound to this device.
172 * So far the worst case seen is a single device with two streams
173 * from different darts, such that this simple static array is enough.
175 * @streams: streams for this device
177 struct apple_dart_master_cfg {
178 struct apple_dart_stream_map stream_maps[MAX_DARTS_PER_DEVICE];
182 * Helper macro to iterate over apple_dart_master_cfg.stream_maps and
183 * apple_dart_domain.stream_maps
185 * @i int used as loop variable
186 * @base pointer to base struct (apple_dart_master_cfg or apple_dart_domain)
187 * @stream pointer to the apple_dart_streams struct for each loop iteration
189 #define for_each_stream_map(i, base, stream_map) \
190 for (i = 0, stream_map = &(base)->stream_maps[0]; \
191 i < MAX_DARTS_PER_DEVICE && stream_map->dart; \
192 stream_map = &(base)->stream_maps[++i])
194 static struct platform_driver apple_dart_driver;
195 static const struct iommu_ops apple_dart_iommu_ops;
197 static struct apple_dart_domain *to_dart_domain(struct iommu_domain *dom)
199 return container_of(dom, struct apple_dart_domain, domain);
203 apple_dart_hw_enable_translation(struct apple_dart_stream_map *stream_map)
207 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
208 writel(DART_TCR_TRANSLATE_ENABLE,
209 stream_map->dart->regs + DART_TCR(sid));
212 static void apple_dart_hw_disable_dma(struct apple_dart_stream_map *stream_map)
216 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
217 writel(0, stream_map->dart->regs + DART_TCR(sid));
221 apple_dart_hw_enable_bypass(struct apple_dart_stream_map *stream_map)
225 WARN_ON(!stream_map->dart->supports_bypass);
226 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
227 writel(DART_TCR_BYPASS0_ENABLE | DART_TCR_BYPASS1_ENABLE,
228 stream_map->dart->regs + DART_TCR(sid));
231 static void apple_dart_hw_set_ttbr(struct apple_dart_stream_map *stream_map,
232 u8 idx, phys_addr_t paddr)
236 WARN_ON(paddr & ((1 << DART_TTBR_SHIFT) - 1));
237 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
238 writel(DART_TTBR_VALID | (paddr >> DART_TTBR_SHIFT),
239 stream_map->dart->regs + DART_TTBR(sid, idx));
242 static void apple_dart_hw_clear_ttbr(struct apple_dart_stream_map *stream_map,
247 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
248 writel(0, stream_map->dart->regs + DART_TTBR(sid, idx));
252 apple_dart_hw_clear_all_ttbrs(struct apple_dart_stream_map *stream_map)
256 for (i = 0; i < DART_MAX_TTBR; ++i)
257 apple_dart_hw_clear_ttbr(stream_map, i);
261 apple_dart_hw_stream_command(struct apple_dart_stream_map *stream_map,
268 spin_lock_irqsave(&stream_map->dart->lock, flags);
270 writel(stream_map->sidmap, stream_map->dart->regs + DART_STREAM_SELECT);
271 writel(command, stream_map->dart->regs + DART_STREAM_COMMAND);
273 ret = readl_poll_timeout_atomic(
274 stream_map->dart->regs + DART_STREAM_COMMAND, command_reg,
275 !(command_reg & DART_STREAM_COMMAND_BUSY), 1,
276 DART_STREAM_COMMAND_BUSY_TIMEOUT);
278 spin_unlock_irqrestore(&stream_map->dart->lock, flags);
281 dev_err(stream_map->dart->dev,
282 "busy bit did not clear after command %x for streams %lx\n",
283 command, stream_map->sidmap);
291 apple_dart_hw_invalidate_tlb(struct apple_dart_stream_map *stream_map)
293 return apple_dart_hw_stream_command(stream_map,
294 DART_STREAM_COMMAND_INVALIDATE);
297 static int apple_dart_hw_reset(struct apple_dart *dart)
300 struct apple_dart_stream_map stream_map;
302 config = readl(dart->regs + DART_CONFIG);
303 if (config & DART_CONFIG_LOCK) {
304 dev_err(dart->dev, "DART is locked down until reboot: %08x\n",
309 stream_map.dart = dart;
310 stream_map.sidmap = DART_STREAM_ALL;
311 apple_dart_hw_disable_dma(&stream_map);
312 apple_dart_hw_clear_all_ttbrs(&stream_map);
314 /* enable all streams globally since TCR is used to control isolation */
315 writel(DART_STREAM_ALL, dart->regs + DART_STREAMS_ENABLE);
317 /* clear any pending errors before the interrupt is unmasked */
318 writel(readl(dart->regs + DART_ERROR), dart->regs + DART_ERROR);
320 return apple_dart_hw_invalidate_tlb(&stream_map);
323 static void apple_dart_domain_flush_tlb(struct apple_dart_domain *domain)
326 struct apple_dart_atomic_stream_map *domain_stream_map;
327 struct apple_dart_stream_map stream_map;
329 for_each_stream_map(i, domain, domain_stream_map) {
330 stream_map.dart = domain_stream_map->dart;
331 stream_map.sidmap = atomic64_read(&domain_stream_map->sidmap);
332 apple_dart_hw_invalidate_tlb(&stream_map);
336 static void apple_dart_flush_iotlb_all(struct iommu_domain *domain)
338 apple_dart_domain_flush_tlb(to_dart_domain(domain));
341 static void apple_dart_iotlb_sync(struct iommu_domain *domain,
342 struct iommu_iotlb_gather *gather)
344 apple_dart_domain_flush_tlb(to_dart_domain(domain));
347 static void apple_dart_iotlb_sync_map(struct iommu_domain *domain,
348 unsigned long iova, size_t size)
350 apple_dart_domain_flush_tlb(to_dart_domain(domain));
353 static phys_addr_t apple_dart_iova_to_phys(struct iommu_domain *domain,
356 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
357 struct io_pgtable_ops *ops = dart_domain->pgtbl_ops;
362 return ops->iova_to_phys(ops, iova);
365 static int apple_dart_map_pages(struct iommu_domain *domain, unsigned long iova,
366 phys_addr_t paddr, size_t pgsize,
367 size_t pgcount, int prot, gfp_t gfp,
370 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
371 struct io_pgtable_ops *ops = dart_domain->pgtbl_ops;
376 return ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, gfp,
380 static size_t apple_dart_unmap_pages(struct iommu_domain *domain,
381 unsigned long iova, size_t pgsize,
383 struct iommu_iotlb_gather *gather)
385 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
386 struct io_pgtable_ops *ops = dart_domain->pgtbl_ops;
388 return ops->unmap_pages(ops, iova, pgsize, pgcount, gather);
392 apple_dart_setup_translation(struct apple_dart_domain *domain,
393 struct apple_dart_stream_map *stream_map)
396 struct io_pgtable_cfg *pgtbl_cfg =
397 &io_pgtable_ops_to_pgtable(domain->pgtbl_ops)->cfg;
399 for (i = 0; i < pgtbl_cfg->apple_dart_cfg.n_ttbrs; ++i)
400 apple_dart_hw_set_ttbr(stream_map, i,
401 pgtbl_cfg->apple_dart_cfg.ttbr[i]);
402 for (; i < DART_MAX_TTBR; ++i)
403 apple_dart_hw_clear_ttbr(stream_map, i);
405 apple_dart_hw_enable_translation(stream_map);
406 apple_dart_hw_invalidate_tlb(stream_map);
409 static int apple_dart_finalize_domain(struct iommu_domain *domain,
410 struct apple_dart_master_cfg *cfg)
412 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
413 struct apple_dart *dart = cfg->stream_maps[0].dart;
414 struct io_pgtable_cfg pgtbl_cfg;
418 mutex_lock(&dart_domain->init_lock);
420 if (dart_domain->finalized)
423 for (i = 0; i < MAX_DARTS_PER_DEVICE; ++i) {
424 dart_domain->stream_maps[i].dart = cfg->stream_maps[i].dart;
425 atomic64_set(&dart_domain->stream_maps[i].sidmap,
426 cfg->stream_maps[i].sidmap);
429 pgtbl_cfg = (struct io_pgtable_cfg){
430 .pgsize_bitmap = dart->pgsize,
432 .oas = dart->hw->oas,
434 .iommu_dev = dart->dev,
437 dart_domain->pgtbl_ops =
438 alloc_io_pgtable_ops(dart->hw->fmt, &pgtbl_cfg, domain);
439 if (!dart_domain->pgtbl_ops) {
444 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
445 domain->geometry.aperture_start = 0;
446 domain->geometry.aperture_end = DMA_BIT_MASK(32);
447 domain->geometry.force_aperture = true;
449 dart_domain->finalized = true;
452 mutex_unlock(&dart_domain->init_lock);
457 apple_dart_mod_streams(struct apple_dart_atomic_stream_map *domain_maps,
458 struct apple_dart_stream_map *master_maps,
463 for (i = 0; i < MAX_DARTS_PER_DEVICE; ++i) {
464 if (domain_maps[i].dart != master_maps[i].dart)
468 for (i = 0; i < MAX_DARTS_PER_DEVICE; ++i) {
469 if (!domain_maps[i].dart)
472 atomic64_or(master_maps[i].sidmap,
473 &domain_maps[i].sidmap);
475 atomic64_and(~master_maps[i].sidmap,
476 &domain_maps[i].sidmap);
482 static int apple_dart_domain_add_streams(struct apple_dart_domain *domain,
483 struct apple_dart_master_cfg *cfg)
485 return apple_dart_mod_streams(domain->stream_maps, cfg->stream_maps,
489 static int apple_dart_domain_remove_streams(struct apple_dart_domain *domain,
490 struct apple_dart_master_cfg *cfg)
492 return apple_dart_mod_streams(domain->stream_maps, cfg->stream_maps,
496 static int apple_dart_attach_dev(struct iommu_domain *domain,
500 struct apple_dart_stream_map *stream_map;
501 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
502 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
504 if (cfg->stream_maps[0].dart->force_bypass &&
505 domain->type != IOMMU_DOMAIN_IDENTITY)
507 if (!cfg->stream_maps[0].dart->supports_bypass &&
508 domain->type == IOMMU_DOMAIN_IDENTITY)
511 ret = apple_dart_finalize_domain(domain, cfg);
515 switch (domain->type) {
516 case IOMMU_DOMAIN_DMA:
517 case IOMMU_DOMAIN_UNMANAGED:
518 ret = apple_dart_domain_add_streams(dart_domain, cfg);
522 for_each_stream_map(i, cfg, stream_map)
523 apple_dart_setup_translation(dart_domain, stream_map);
525 case IOMMU_DOMAIN_BLOCKED:
526 for_each_stream_map(i, cfg, stream_map)
527 apple_dart_hw_disable_dma(stream_map);
529 case IOMMU_DOMAIN_IDENTITY:
530 for_each_stream_map(i, cfg, stream_map)
531 apple_dart_hw_enable_bypass(stream_map);
538 static void apple_dart_detach_dev(struct iommu_domain *domain,
542 struct apple_dart_stream_map *stream_map;
543 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
544 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
546 for_each_stream_map(i, cfg, stream_map)
547 apple_dart_hw_disable_dma(stream_map);
549 if (domain->type == IOMMU_DOMAIN_DMA ||
550 domain->type == IOMMU_DOMAIN_UNMANAGED)
551 apple_dart_domain_remove_streams(dart_domain, cfg);
554 static struct iommu_device *apple_dart_probe_device(struct device *dev)
556 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
557 struct apple_dart_stream_map *stream_map;
561 return ERR_PTR(-ENODEV);
563 for_each_stream_map(i, cfg, stream_map)
565 dev, stream_map->dart->dev,
566 DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER);
568 return &cfg->stream_maps[0].dart->iommu;
571 static void apple_dart_release_device(struct device *dev)
573 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
575 dev_iommu_priv_set(dev, NULL);
579 static struct iommu_domain *apple_dart_domain_alloc(unsigned int type)
581 struct apple_dart_domain *dart_domain;
583 if (type != IOMMU_DOMAIN_DMA && type != IOMMU_DOMAIN_UNMANAGED &&
584 type != IOMMU_DOMAIN_IDENTITY && type != IOMMU_DOMAIN_BLOCKED)
587 dart_domain = kzalloc(sizeof(*dart_domain), GFP_KERNEL);
591 mutex_init(&dart_domain->init_lock);
593 /* no need to allocate pgtbl_ops or do any other finalization steps */
594 if (type == IOMMU_DOMAIN_IDENTITY || type == IOMMU_DOMAIN_BLOCKED)
595 dart_domain->finalized = true;
597 return &dart_domain->domain;
600 static void apple_dart_domain_free(struct iommu_domain *domain)
602 struct apple_dart_domain *dart_domain = to_dart_domain(domain);
604 if (dart_domain->pgtbl_ops)
605 free_io_pgtable_ops(dart_domain->pgtbl_ops);
610 static int apple_dart_of_xlate(struct device *dev, struct of_phandle_args *args)
612 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
613 struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
614 struct apple_dart *dart = platform_get_drvdata(iommu_pdev);
615 struct apple_dart *cfg_dart;
618 if (args->args_count != 1)
623 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
626 dev_iommu_priv_set(dev, cfg);
628 cfg_dart = cfg->stream_maps[0].dart;
630 if (cfg_dart->supports_bypass != dart->supports_bypass)
632 if (cfg_dart->force_bypass != dart->force_bypass)
634 if (cfg_dart->pgsize != dart->pgsize)
638 for (i = 0; i < MAX_DARTS_PER_DEVICE; ++i) {
639 if (cfg->stream_maps[i].dart == dart) {
640 cfg->stream_maps[i].sidmap |= 1 << sid;
644 for (i = 0; i < MAX_DARTS_PER_DEVICE; ++i) {
645 if (!cfg->stream_maps[i].dart) {
646 cfg->stream_maps[i].dart = dart;
647 cfg->stream_maps[i].sidmap = 1 << sid;
655 static DEFINE_MUTEX(apple_dart_groups_lock);
657 static void apple_dart_release_group(void *iommu_data)
660 struct apple_dart_stream_map *stream_map;
661 struct apple_dart_master_cfg *group_master_cfg = iommu_data;
663 mutex_lock(&apple_dart_groups_lock);
665 for_each_stream_map(i, group_master_cfg, stream_map)
666 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
667 stream_map->dart->sid2group[sid] = NULL;
670 mutex_unlock(&apple_dart_groups_lock);
673 static struct iommu_group *apple_dart_device_group(struct device *dev)
676 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
677 struct apple_dart_stream_map *stream_map;
678 struct apple_dart_master_cfg *group_master_cfg;
679 struct iommu_group *group = NULL;
680 struct iommu_group *res = ERR_PTR(-EINVAL);
682 mutex_lock(&apple_dart_groups_lock);
684 for_each_stream_map(i, cfg, stream_map) {
685 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS) {
686 struct iommu_group *stream_group =
687 stream_map->dart->sid2group[sid];
689 if (group && group != stream_group) {
690 res = ERR_PTR(-EINVAL);
694 group = stream_group;
699 res = iommu_group_ref_get(group);
705 group = pci_device_group(dev);
708 group = generic_device_group(dev);
710 res = ERR_PTR(-ENOMEM);
714 group_master_cfg = kmemdup(cfg, sizeof(*group_master_cfg), GFP_KERNEL);
715 if (!group_master_cfg) {
716 iommu_group_put(group);
720 iommu_group_set_iommudata(group, group_master_cfg,
721 apple_dart_release_group);
723 for_each_stream_map(i, cfg, stream_map)
724 for_each_set_bit(sid, &stream_map->sidmap, DART_MAX_STREAMS)
725 stream_map->dart->sid2group[sid] = group;
730 mutex_unlock(&apple_dart_groups_lock);
734 static int apple_dart_def_domain_type(struct device *dev)
736 struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev);
738 if (cfg->stream_maps[0].dart->force_bypass)
739 return IOMMU_DOMAIN_IDENTITY;
740 if (!cfg->stream_maps[0].dart->supports_bypass)
741 return IOMMU_DOMAIN_DMA;
746 #ifndef CONFIG_PCIE_APPLE_MSI_DOORBELL_ADDR
747 /* Keep things compiling when CONFIG_PCI_APPLE isn't selected */
748 #define CONFIG_PCIE_APPLE_MSI_DOORBELL_ADDR 0
750 #define DOORBELL_ADDR (CONFIG_PCIE_APPLE_MSI_DOORBELL_ADDR & PAGE_MASK)
752 static void apple_dart_get_resv_regions(struct device *dev,
753 struct list_head *head)
755 if (IS_ENABLED(CONFIG_PCIE_APPLE) && dev_is_pci(dev)) {
756 struct iommu_resv_region *region;
757 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
759 region = iommu_alloc_resv_region(DOORBELL_ADDR,
765 list_add_tail(®ion->list, head);
768 iommu_dma_get_resv_regions(dev, head);
771 static const struct iommu_ops apple_dart_iommu_ops = {
772 .domain_alloc = apple_dart_domain_alloc,
773 .probe_device = apple_dart_probe_device,
774 .release_device = apple_dart_release_device,
775 .device_group = apple_dart_device_group,
776 .of_xlate = apple_dart_of_xlate,
777 .def_domain_type = apple_dart_def_domain_type,
778 .get_resv_regions = apple_dart_get_resv_regions,
779 .pgsize_bitmap = -1UL, /* Restricted during dart probe */
780 .owner = THIS_MODULE,
781 .default_domain_ops = &(const struct iommu_domain_ops) {
782 .attach_dev = apple_dart_attach_dev,
783 .detach_dev = apple_dart_detach_dev,
784 .map_pages = apple_dart_map_pages,
785 .unmap_pages = apple_dart_unmap_pages,
786 .flush_iotlb_all = apple_dart_flush_iotlb_all,
787 .iotlb_sync = apple_dart_iotlb_sync,
788 .iotlb_sync_map = apple_dart_iotlb_sync_map,
789 .iova_to_phys = apple_dart_iova_to_phys,
790 .free = apple_dart_domain_free,
794 static irqreturn_t apple_dart_irq(int irq, void *dev)
796 struct apple_dart *dart = dev;
797 const char *fault_name = NULL;
798 u32 error = readl(dart->regs + DART_ERROR);
799 u32 error_code = FIELD_GET(DART_ERROR_CODE, error);
800 u32 addr_lo = readl(dart->regs + DART_ERROR_ADDR_LO);
801 u32 addr_hi = readl(dart->regs + DART_ERROR_ADDR_HI);
802 u64 addr = addr_lo | (((u64)addr_hi) << 32);
803 u8 stream_idx = FIELD_GET(DART_ERROR_STREAM, error);
805 if (!(error & DART_ERROR_FLAG))
808 /* there should only be a single bit set but let's use == to be sure */
809 if (error_code == DART_ERROR_READ_FAULT)
810 fault_name = "READ FAULT";
811 else if (error_code == DART_ERROR_WRITE_FAULT)
812 fault_name = "WRITE FAULT";
813 else if (error_code == DART_ERROR_NO_PTE)
814 fault_name = "NO PTE FOR IOVA";
815 else if (error_code == DART_ERROR_NO_PMD)
816 fault_name = "NO PMD FOR IOVA";
817 else if (error_code == DART_ERROR_NO_TTBR)
818 fault_name = "NO TTBR FOR IOVA";
820 fault_name = "unknown";
824 "translation fault: status:0x%x stream:%d code:0x%x (%s) at 0x%llx",
825 error, stream_idx, error_code, fault_name, addr);
827 writel(error, dart->regs + DART_ERROR);
831 static int apple_dart_probe(struct platform_device *pdev)
835 struct resource *res;
836 struct apple_dart *dart;
837 struct device *dev = &pdev->dev;
839 dart = devm_kzalloc(dev, sizeof(*dart), GFP_KERNEL);
844 dart->hw = of_device_get_match_data(dev);
845 spin_lock_init(&dart->lock);
847 dart->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
848 if (IS_ERR(dart->regs))
849 return PTR_ERR(dart->regs);
851 if (resource_size(res) < 0x4000) {
852 dev_err(dev, "MMIO region too small (%pr)\n", res);
856 dart->irq = platform_get_irq(pdev, 0);
860 ret = devm_clk_bulk_get_all(dev, &dart->clks);
863 dart->num_clks = ret;
865 ret = clk_bulk_prepare_enable(dart->num_clks, dart->clks);
869 ret = apple_dart_hw_reset(dart);
871 goto err_clk_disable;
873 dart_params[0] = readl(dart->regs + DART_PARAMS1);
874 dart_params[1] = readl(dart->regs + DART_PARAMS2);
875 dart->pgsize = 1 << FIELD_GET(DART_PARAMS_PAGE_SHIFT, dart_params[0]);
876 dart->supports_bypass = dart_params[1] & DART_PARAMS_BYPASS_SUPPORT;
877 dart->force_bypass = dart->pgsize > PAGE_SIZE;
879 ret = request_irq(dart->irq, apple_dart_irq, IRQF_SHARED,
880 "apple-dart fault handler", dart);
882 goto err_clk_disable;
884 platform_set_drvdata(pdev, dart);
886 ret = iommu_device_sysfs_add(&dart->iommu, dev, NULL, "apple-dart.%s",
887 dev_name(&pdev->dev));
891 ret = iommu_device_register(&dart->iommu, &apple_dart_iommu_ops, dev);
893 goto err_sysfs_remove;
897 "DART [pagesize %x, bypass support: %d, bypass forced: %d] initialized\n",
898 dart->pgsize, dart->supports_bypass, dart->force_bypass);
902 iommu_device_sysfs_remove(&dart->iommu);
904 free_irq(dart->irq, dart);
906 clk_bulk_disable_unprepare(dart->num_clks, dart->clks);
911 static int apple_dart_remove(struct platform_device *pdev)
913 struct apple_dart *dart = platform_get_drvdata(pdev);
915 apple_dart_hw_reset(dart);
916 free_irq(dart->irq, dart);
918 iommu_device_unregister(&dart->iommu);
919 iommu_device_sysfs_remove(&dart->iommu);
921 clk_bulk_disable_unprepare(dart->num_clks, dart->clks);
926 static const struct apple_dart_hw apple_dart_hw_t8103 = {
930 static const struct apple_dart_hw apple_dart_hw_t6000 = {
935 static const struct of_device_id apple_dart_of_match[] = {
936 { .compatible = "apple,t8103-dart", .data = &apple_dart_hw_t8103 },
937 { .compatible = "apple,t6000-dart", .data = &apple_dart_hw_t6000 },
940 MODULE_DEVICE_TABLE(of, apple_dart_of_match);
942 static struct platform_driver apple_dart_driver = {
944 .name = "apple-dart",
945 .of_match_table = apple_dart_of_match,
946 .suppress_bind_attrs = true,
948 .probe = apple_dart_probe,
949 .remove = apple_dart_remove,
952 module_platform_driver(apple_dart_driver);
954 MODULE_DESCRIPTION("IOMMU API for Apple's DART");
955 MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
956 MODULE_LICENSE("GPL v2");