Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 2 | /* |
Paul Gortmaker | 669a047 | 2018-12-01 14:19:10 -0500 | [diff] [blame] | 3 | * IOMMU API for Rockchip |
| 4 | * |
| 5 | * Module Authors: Simon Xue <xxm@rock-chips.com> |
| 6 | * Daniel Kurtz <djkurtz@chromium.org> |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 9 | #include <linux/clk.h> |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 10 | #include <linux/compiler.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/device.h> |
Joerg Roedel | 461a694 | 2017-04-26 15:46:20 +0200 | [diff] [blame] | 13 | #include <linux/dma-mapping.h> |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 14 | #include <linux/errno.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/iommu.h> |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 18 | #include <linux/iopoll.h> |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 19 | #include <linux/list.h> |
| 20 | #include <linux/mm.h> |
Paul Gortmaker | 669a047 | 2018-12-01 14:19:10 -0500 | [diff] [blame] | 21 | #include <linux/init.h> |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 22 | #include <linux/of.h> |
| 23 | #include <linux/of_platform.h> |
| 24 | #include <linux/platform_device.h> |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/spinlock.h> |
| 28 | |
| 29 | /** MMU register offsets */ |
| 30 | #define RK_MMU_DTE_ADDR 0x00 /* Directory table address */ |
| 31 | #define RK_MMU_STATUS 0x04 |
| 32 | #define RK_MMU_COMMAND 0x08 |
| 33 | #define RK_MMU_PAGE_FAULT_ADDR 0x0C /* IOVA of last page fault */ |
| 34 | #define RK_MMU_ZAP_ONE_LINE 0x10 /* Shootdown one IOTLB entry */ |
| 35 | #define RK_MMU_INT_RAWSTAT 0x14 /* IRQ status ignoring mask */ |
| 36 | #define RK_MMU_INT_CLEAR 0x18 /* Acknowledge and re-arm irq */ |
| 37 | #define RK_MMU_INT_MASK 0x1C /* IRQ enable */ |
| 38 | #define RK_MMU_INT_STATUS 0x20 /* IRQ status after masking */ |
| 39 | #define RK_MMU_AUTO_GATING 0x24 |
| 40 | |
| 41 | #define DTE_ADDR_DUMMY 0xCAFEBABE |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 42 | |
| 43 | #define RK_MMU_POLL_PERIOD_US 100 |
| 44 | #define RK_MMU_FORCE_RESET_TIMEOUT_US 100000 |
| 45 | #define RK_MMU_POLL_TIMEOUT_US 1000 |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 46 | |
| 47 | /* RK_MMU_STATUS fields */ |
| 48 | #define RK_MMU_STATUS_PAGING_ENABLED BIT(0) |
| 49 | #define RK_MMU_STATUS_PAGE_FAULT_ACTIVE BIT(1) |
| 50 | #define RK_MMU_STATUS_STALL_ACTIVE BIT(2) |
| 51 | #define RK_MMU_STATUS_IDLE BIT(3) |
| 52 | #define RK_MMU_STATUS_REPLAY_BUFFER_EMPTY BIT(4) |
| 53 | #define RK_MMU_STATUS_PAGE_FAULT_IS_WRITE BIT(5) |
| 54 | #define RK_MMU_STATUS_STALL_NOT_ACTIVE BIT(31) |
| 55 | |
| 56 | /* RK_MMU_COMMAND command values */ |
| 57 | #define RK_MMU_CMD_ENABLE_PAGING 0 /* Enable memory translation */ |
| 58 | #define RK_MMU_CMD_DISABLE_PAGING 1 /* Disable memory translation */ |
| 59 | #define RK_MMU_CMD_ENABLE_STALL 2 /* Stall paging to allow other cmds */ |
| 60 | #define RK_MMU_CMD_DISABLE_STALL 3 /* Stop stall re-enables paging */ |
| 61 | #define RK_MMU_CMD_ZAP_CACHE 4 /* Shoot down entire IOTLB */ |
| 62 | #define RK_MMU_CMD_PAGE_FAULT_DONE 5 /* Clear page fault */ |
| 63 | #define RK_MMU_CMD_FORCE_RESET 6 /* Reset all registers */ |
| 64 | |
| 65 | /* RK_MMU_INT_* register fields */ |
| 66 | #define RK_MMU_IRQ_PAGE_FAULT 0x01 /* page fault */ |
| 67 | #define RK_MMU_IRQ_BUS_ERROR 0x02 /* bus read error */ |
| 68 | #define RK_MMU_IRQ_MASK (RK_MMU_IRQ_PAGE_FAULT | RK_MMU_IRQ_BUS_ERROR) |
| 69 | |
| 70 | #define NUM_DT_ENTRIES 1024 |
| 71 | #define NUM_PT_ENTRIES 1024 |
| 72 | |
| 73 | #define SPAGE_ORDER 12 |
| 74 | #define SPAGE_SIZE (1 << SPAGE_ORDER) |
| 75 | |
| 76 | /* |
| 77 | * Support mapping any size that fits in one page table: |
| 78 | * 4 KiB to 4 MiB |
| 79 | */ |
| 80 | #define RK_IOMMU_PGSIZE_BITMAP 0x007ff000 |
| 81 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 82 | struct rk_iommu_domain { |
| 83 | struct list_head iommus; |
| 84 | u32 *dt; /* page directory table */ |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 85 | dma_addr_t dt_dma; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 86 | spinlock_t iommus_lock; /* lock for iommus list */ |
| 87 | spinlock_t dt_lock; /* lock for modifying page directory table */ |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 88 | |
| 89 | struct iommu_domain domain; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 90 | }; |
| 91 | |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 92 | /* list of clocks required by IOMMU */ |
| 93 | static const char * const rk_iommu_clocks[] = { |
| 94 | "aclk", "iface", |
| 95 | }; |
| 96 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 97 | struct rk_iommu_ops { |
| 98 | phys_addr_t (*pt_address)(u32 dte); |
| 99 | u32 (*mk_dtentries)(dma_addr_t pt_dma); |
| 100 | u32 (*mk_ptentries)(phys_addr_t page, int prot); |
| 101 | phys_addr_t (*dte_addr_phys)(u32 addr); |
| 102 | u32 (*dma_addr_dte)(dma_addr_t dt_dma); |
| 103 | u64 dma_bit_mask; |
| 104 | }; |
| 105 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 106 | struct rk_iommu { |
| 107 | struct device *dev; |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 108 | void __iomem **bases; |
| 109 | int num_mmu; |
Heiko Stuebner | f925815 | 2019-09-25 20:43:46 +0200 | [diff] [blame] | 110 | int num_irq; |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 111 | struct clk_bulk_data *clocks; |
| 112 | int num_clocks; |
Simon Xue | c3aa474 | 2017-07-24 10:37:15 +0800 | [diff] [blame] | 113 | bool reset_disabled; |
Joerg Roedel | c9d9f23 | 2017-03-31 16:26:03 +0200 | [diff] [blame] | 114 | struct iommu_device iommu; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 115 | struct list_head node; /* entry in rk_iommu_domain.iommus */ |
| 116 | struct iommu_domain *domain; /* domain to which iommu is attached */ |
Jeffy Chen | 57c2695 | 2018-03-23 15:38:14 +0800 | [diff] [blame] | 117 | struct iommu_group *group; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 118 | }; |
| 119 | |
Jeffy Chen | 5fd577c | 2018-03-23 15:38:11 +0800 | [diff] [blame] | 120 | struct rk_iommudata { |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 121 | struct device_link *link; /* runtime PM link from IOMMU to master */ |
Jeffy Chen | 5fd577c | 2018-03-23 15:38:11 +0800 | [diff] [blame] | 122 | struct rk_iommu *iommu; |
| 123 | }; |
| 124 | |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 125 | static struct device *dma_dev; |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 126 | static const struct rk_iommu_ops *rk_ops; |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 127 | |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 128 | static inline void rk_table_flush(struct rk_iommu_domain *dom, dma_addr_t dma, |
| 129 | unsigned int count) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 130 | { |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 131 | size_t size = count * sizeof(u32); /* count of u32 entry */ |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 132 | |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 133 | dma_sync_single_for_device(dma_dev, dma, size, DMA_TO_DEVICE); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 134 | } |
| 135 | |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 136 | static struct rk_iommu_domain *to_rk_domain(struct iommu_domain *dom) |
| 137 | { |
| 138 | return container_of(dom, struct rk_iommu_domain, domain); |
| 139 | } |
| 140 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 141 | /* |
| 142 | * The Rockchip rk3288 iommu uses a 2-level page table. |
| 143 | * The first level is the "Directory Table" (DT). |
| 144 | * The DT consists of 1024 4-byte Directory Table Entries (DTEs), each pointing |
| 145 | * to a "Page Table". |
| 146 | * The second level is the 1024 Page Tables (PT). |
| 147 | * Each PT consists of 1024 4-byte Page Table Entries (PTEs), each pointing to |
| 148 | * a 4 KB page of physical memory. |
| 149 | * |
| 150 | * The DT and each PT fits in a single 4 KB page (4-bytes * 1024 entries). |
| 151 | * Each iommu device has a MMU_DTE_ADDR register that contains the physical |
| 152 | * address of the start of the DT page. |
| 153 | * |
| 154 | * The structure of the page table is as follows: |
| 155 | * |
| 156 | * DT |
| 157 | * MMU_DTE_ADDR -> +-----+ |
| 158 | * | | |
| 159 | * +-----+ PT |
| 160 | * | DTE | -> +-----+ |
| 161 | * +-----+ | | Memory |
| 162 | * | | +-----+ Page |
| 163 | * | | | PTE | -> +-----+ |
| 164 | * +-----+ +-----+ | | |
| 165 | * | | | | |
| 166 | * | | | | |
| 167 | * +-----+ | | |
| 168 | * | | |
| 169 | * | | |
| 170 | * +-----+ |
| 171 | */ |
| 172 | |
| 173 | /* |
| 174 | * Each DTE has a PT address and a valid bit: |
| 175 | * +---------------------+-----------+-+ |
| 176 | * | PT address | Reserved |V| |
| 177 | * +---------------------+-----------+-+ |
| 178 | * 31:12 - PT address (PTs always starts on a 4 KB boundary) |
| 179 | * 11: 1 - Reserved |
| 180 | * 0 - 1 if PT @ PT address is valid |
| 181 | */ |
| 182 | #define RK_DTE_PT_ADDRESS_MASK 0xfffff000 |
| 183 | #define RK_DTE_PT_VALID BIT(0) |
| 184 | |
| 185 | static inline phys_addr_t rk_dte_pt_address(u32 dte) |
| 186 | { |
| 187 | return (phys_addr_t)dte & RK_DTE_PT_ADDRESS_MASK; |
| 188 | } |
| 189 | |
Benjamin Gaignard | c55356c | 2021-06-04 18:44:41 +0200 | [diff] [blame] | 190 | /* |
| 191 | * In v2: |
| 192 | * 31:12 - PT address bit 31:0 |
| 193 | * 11: 8 - PT address bit 35:32 |
| 194 | * 7: 4 - PT address bit 39:36 |
| 195 | * 3: 1 - Reserved |
| 196 | * 0 - 1 if PT @ PT address is valid |
| 197 | */ |
| 198 | #define RK_DTE_PT_ADDRESS_MASK_V2 GENMASK_ULL(31, 4) |
| 199 | #define DTE_HI_MASK1 GENMASK(11, 8) |
| 200 | #define DTE_HI_MASK2 GENMASK(7, 4) |
| 201 | #define DTE_HI_SHIFT1 24 /* shift bit 8 to bit 32 */ |
| 202 | #define DTE_HI_SHIFT2 32 /* shift bit 4 to bit 36 */ |
Alex Bee | f7ff3cf | 2021-11-24 03:13:25 +0100 | [diff] [blame] | 203 | #define PAGE_DESC_HI_MASK1 GENMASK_ULL(35, 32) |
| 204 | #define PAGE_DESC_HI_MASK2 GENMASK_ULL(39, 36) |
Benjamin Gaignard | c55356c | 2021-06-04 18:44:41 +0200 | [diff] [blame] | 205 | |
| 206 | static inline phys_addr_t rk_dte_pt_address_v2(u32 dte) |
| 207 | { |
| 208 | u64 dte_v2 = dte; |
| 209 | |
| 210 | dte_v2 = ((dte_v2 & DTE_HI_MASK2) << DTE_HI_SHIFT2) | |
| 211 | ((dte_v2 & DTE_HI_MASK1) << DTE_HI_SHIFT1) | |
| 212 | (dte_v2 & RK_DTE_PT_ADDRESS_MASK); |
| 213 | |
| 214 | return (phys_addr_t)dte_v2; |
| 215 | } |
| 216 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 217 | static inline bool rk_dte_is_pt_valid(u32 dte) |
| 218 | { |
| 219 | return dte & RK_DTE_PT_VALID; |
| 220 | } |
| 221 | |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 222 | static inline u32 rk_mk_dte(dma_addr_t pt_dma) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 223 | { |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 224 | return (pt_dma & RK_DTE_PT_ADDRESS_MASK) | RK_DTE_PT_VALID; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 225 | } |
| 226 | |
Benjamin Gaignard | c55356c | 2021-06-04 18:44:41 +0200 | [diff] [blame] | 227 | static inline u32 rk_mk_dte_v2(dma_addr_t pt_dma) |
| 228 | { |
| 229 | pt_dma = (pt_dma & RK_DTE_PT_ADDRESS_MASK) | |
| 230 | ((pt_dma & PAGE_DESC_HI_MASK1) >> DTE_HI_SHIFT1) | |
| 231 | (pt_dma & PAGE_DESC_HI_MASK2) >> DTE_HI_SHIFT2; |
| 232 | |
| 233 | return (pt_dma & RK_DTE_PT_ADDRESS_MASK_V2) | RK_DTE_PT_VALID; |
| 234 | } |
| 235 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 236 | /* |
| 237 | * Each PTE has a Page address, some flags and a valid bit: |
| 238 | * +---------------------+---+-------+-+ |
| 239 | * | Page address |Rsv| Flags |V| |
| 240 | * +---------------------+---+-------+-+ |
| 241 | * 31:12 - Page address (Pages always start on a 4 KB boundary) |
| 242 | * 11: 9 - Reserved |
| 243 | * 8: 1 - Flags |
| 244 | * 8 - Read allocate - allocate cache space on read misses |
| 245 | * 7 - Read cache - enable cache & prefetch of data |
| 246 | * 6 - Write buffer - enable delaying writes on their way to memory |
| 247 | * 5 - Write allocate - allocate cache space on write misses |
| 248 | * 4 - Write cache - different writes can be merged together |
| 249 | * 3 - Override cache attributes |
| 250 | * if 1, bits 4-8 control cache attributes |
| 251 | * if 0, the system bus defaults are used |
| 252 | * 2 - Writable |
| 253 | * 1 - Readable |
| 254 | * 0 - 1 if Page @ Page address is valid |
| 255 | */ |
| 256 | #define RK_PTE_PAGE_ADDRESS_MASK 0xfffff000 |
| 257 | #define RK_PTE_PAGE_FLAGS_MASK 0x000001fe |
| 258 | #define RK_PTE_PAGE_WRITABLE BIT(2) |
| 259 | #define RK_PTE_PAGE_READABLE BIT(1) |
| 260 | #define RK_PTE_PAGE_VALID BIT(0) |
| 261 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 262 | static inline bool rk_pte_is_page_valid(u32 pte) |
| 263 | { |
| 264 | return pte & RK_PTE_PAGE_VALID; |
| 265 | } |
| 266 | |
| 267 | /* TODO: set cache flags per prot IOMMU_CACHE */ |
| 268 | static u32 rk_mk_pte(phys_addr_t page, int prot) |
| 269 | { |
| 270 | u32 flags = 0; |
| 271 | flags |= (prot & IOMMU_READ) ? RK_PTE_PAGE_READABLE : 0; |
| 272 | flags |= (prot & IOMMU_WRITE) ? RK_PTE_PAGE_WRITABLE : 0; |
| 273 | page &= RK_PTE_PAGE_ADDRESS_MASK; |
| 274 | return page | flags | RK_PTE_PAGE_VALID; |
| 275 | } |
| 276 | |
Benjamin Gaignard | c55356c | 2021-06-04 18:44:41 +0200 | [diff] [blame] | 277 | /* |
| 278 | * In v2: |
| 279 | * 31:12 - Page address bit 31:0 |
| 280 | * 11:9 - Page address bit 34:32 |
| 281 | * 8:4 - Page address bit 39:35 |
| 282 | * 3 - Security |
| 283 | * 2 - Readable |
| 284 | * 1 - Writable |
| 285 | * 0 - 1 if Page @ Page address is valid |
| 286 | */ |
| 287 | #define RK_PTE_PAGE_READABLE_V2 BIT(2) |
| 288 | #define RK_PTE_PAGE_WRITABLE_V2 BIT(1) |
| 289 | |
| 290 | static u32 rk_mk_pte_v2(phys_addr_t page, int prot) |
| 291 | { |
| 292 | u32 flags = 0; |
| 293 | |
| 294 | flags |= (prot & IOMMU_READ) ? RK_PTE_PAGE_READABLE_V2 : 0; |
| 295 | flags |= (prot & IOMMU_WRITE) ? RK_PTE_PAGE_WRITABLE_V2 : 0; |
| 296 | |
| 297 | return rk_mk_dte_v2(page) | flags; |
| 298 | } |
| 299 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 300 | static u32 rk_mk_pte_invalid(u32 pte) |
| 301 | { |
| 302 | return pte & ~RK_PTE_PAGE_VALID; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * rk3288 iova (IOMMU Virtual Address) format |
| 307 | * 31 22.21 12.11 0 |
| 308 | * +-----------+-----------+-------------+ |
| 309 | * | DTE index | PTE index | Page offset | |
| 310 | * +-----------+-----------+-------------+ |
| 311 | * 31:22 - DTE index - index of DTE in DT |
| 312 | * 21:12 - PTE index - index of PTE in PT @ DTE.pt_address |
| 313 | * 11: 0 - Page offset - offset into page @ PTE.page_address |
| 314 | */ |
| 315 | #define RK_IOVA_DTE_MASK 0xffc00000 |
| 316 | #define RK_IOVA_DTE_SHIFT 22 |
| 317 | #define RK_IOVA_PTE_MASK 0x003ff000 |
| 318 | #define RK_IOVA_PTE_SHIFT 12 |
| 319 | #define RK_IOVA_PAGE_MASK 0x00000fff |
| 320 | #define RK_IOVA_PAGE_SHIFT 0 |
| 321 | |
| 322 | static u32 rk_iova_dte_index(dma_addr_t iova) |
| 323 | { |
| 324 | return (u32)(iova & RK_IOVA_DTE_MASK) >> RK_IOVA_DTE_SHIFT; |
| 325 | } |
| 326 | |
| 327 | static u32 rk_iova_pte_index(dma_addr_t iova) |
| 328 | { |
| 329 | return (u32)(iova & RK_IOVA_PTE_MASK) >> RK_IOVA_PTE_SHIFT; |
| 330 | } |
| 331 | |
| 332 | static u32 rk_iova_page_offset(dma_addr_t iova) |
| 333 | { |
| 334 | return (u32)(iova & RK_IOVA_PAGE_MASK) >> RK_IOVA_PAGE_SHIFT; |
| 335 | } |
| 336 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 337 | static u32 rk_iommu_read(void __iomem *base, u32 offset) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 338 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 339 | return readl(base + offset); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 340 | } |
| 341 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 342 | static void rk_iommu_write(void __iomem *base, u32 offset, u32 value) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 343 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 344 | writel(value, base + offset); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | static void rk_iommu_command(struct rk_iommu *iommu, u32 command) |
| 348 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 349 | int i; |
| 350 | |
| 351 | for (i = 0; i < iommu->num_mmu; i++) |
| 352 | writel(command, iommu->bases[i] + RK_MMU_COMMAND); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 353 | } |
| 354 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 355 | static void rk_iommu_base_command(void __iomem *base, u32 command) |
| 356 | { |
| 357 | writel(command, base + RK_MMU_COMMAND); |
| 358 | } |
Tomasz Figa | bf2a5e7 | 2018-03-23 15:38:06 +0800 | [diff] [blame] | 359 | static void rk_iommu_zap_lines(struct rk_iommu *iommu, dma_addr_t iova_start, |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 360 | size_t size) |
| 361 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 362 | int i; |
Tomasz Figa | bf2a5e7 | 2018-03-23 15:38:06 +0800 | [diff] [blame] | 363 | dma_addr_t iova_end = iova_start + size; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 364 | /* |
| 365 | * TODO(djkurtz): Figure out when it is more efficient to shootdown the |
| 366 | * entire iotlb rather than iterate over individual iovas. |
| 367 | */ |
Tomasz Figa | bf2a5e7 | 2018-03-23 15:38:06 +0800 | [diff] [blame] | 368 | for (i = 0; i < iommu->num_mmu; i++) { |
| 369 | dma_addr_t iova; |
| 370 | |
| 371 | for (iova = iova_start; iova < iova_end; iova += SPAGE_SIZE) |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 372 | rk_iommu_write(iommu->bases[i], RK_MMU_ZAP_ONE_LINE, iova); |
Tomasz Figa | bf2a5e7 | 2018-03-23 15:38:06 +0800 | [diff] [blame] | 373 | } |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static bool rk_iommu_is_stall_active(struct rk_iommu *iommu) |
| 377 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 378 | bool active = true; |
| 379 | int i; |
| 380 | |
| 381 | for (i = 0; i < iommu->num_mmu; i++) |
John Keeping | fbedd9b | 2016-04-05 15:05:46 +0100 | [diff] [blame] | 382 | active &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) & |
| 383 | RK_MMU_STATUS_STALL_ACTIVE); |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 384 | |
| 385 | return active; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static bool rk_iommu_is_paging_enabled(struct rk_iommu *iommu) |
| 389 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 390 | bool enable = true; |
| 391 | int i; |
| 392 | |
| 393 | for (i = 0; i < iommu->num_mmu; i++) |
John Keeping | fbedd9b | 2016-04-05 15:05:46 +0100 | [diff] [blame] | 394 | enable &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) & |
| 395 | RK_MMU_STATUS_PAGING_ENABLED); |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 396 | |
| 397 | return enable; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 398 | } |
| 399 | |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 400 | static bool rk_iommu_is_reset_done(struct rk_iommu *iommu) |
| 401 | { |
| 402 | bool done = true; |
| 403 | int i; |
| 404 | |
| 405 | for (i = 0; i < iommu->num_mmu; i++) |
| 406 | done &= rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR) == 0; |
| 407 | |
| 408 | return done; |
| 409 | } |
| 410 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 411 | static int rk_iommu_enable_stall(struct rk_iommu *iommu) |
| 412 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 413 | int ret, i; |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 414 | bool val; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 415 | |
| 416 | if (rk_iommu_is_stall_active(iommu)) |
| 417 | return 0; |
| 418 | |
| 419 | /* Stall can only be enabled if paging is enabled */ |
| 420 | if (!rk_iommu_is_paging_enabled(iommu)) |
| 421 | return 0; |
| 422 | |
| 423 | rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_STALL); |
| 424 | |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 425 | ret = readx_poll_timeout(rk_iommu_is_stall_active, iommu, val, |
| 426 | val, RK_MMU_POLL_PERIOD_US, |
| 427 | RK_MMU_POLL_TIMEOUT_US); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 428 | if (ret) |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 429 | for (i = 0; i < iommu->num_mmu; i++) |
| 430 | dev_err(iommu->dev, "Enable stall request timed out, status: %#08x\n", |
| 431 | rk_iommu_read(iommu->bases[i], RK_MMU_STATUS)); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | static int rk_iommu_disable_stall(struct rk_iommu *iommu) |
| 437 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 438 | int ret, i; |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 439 | bool val; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 440 | |
| 441 | if (!rk_iommu_is_stall_active(iommu)) |
| 442 | return 0; |
| 443 | |
| 444 | rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_STALL); |
| 445 | |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 446 | ret = readx_poll_timeout(rk_iommu_is_stall_active, iommu, val, |
| 447 | !val, RK_MMU_POLL_PERIOD_US, |
| 448 | RK_MMU_POLL_TIMEOUT_US); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 449 | if (ret) |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 450 | for (i = 0; i < iommu->num_mmu; i++) |
| 451 | dev_err(iommu->dev, "Disable stall request timed out, status: %#08x\n", |
| 452 | rk_iommu_read(iommu->bases[i], RK_MMU_STATUS)); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 453 | |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | static int rk_iommu_enable_paging(struct rk_iommu *iommu) |
| 458 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 459 | int ret, i; |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 460 | bool val; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 461 | |
| 462 | if (rk_iommu_is_paging_enabled(iommu)) |
| 463 | return 0; |
| 464 | |
| 465 | rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_PAGING); |
| 466 | |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 467 | ret = readx_poll_timeout(rk_iommu_is_paging_enabled, iommu, val, |
| 468 | val, RK_MMU_POLL_PERIOD_US, |
| 469 | RK_MMU_POLL_TIMEOUT_US); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 470 | if (ret) |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 471 | for (i = 0; i < iommu->num_mmu; i++) |
| 472 | dev_err(iommu->dev, "Enable paging request timed out, status: %#08x\n", |
| 473 | rk_iommu_read(iommu->bases[i], RK_MMU_STATUS)); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 474 | |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | static int rk_iommu_disable_paging(struct rk_iommu *iommu) |
| 479 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 480 | int ret, i; |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 481 | bool val; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 482 | |
| 483 | if (!rk_iommu_is_paging_enabled(iommu)) |
| 484 | return 0; |
| 485 | |
| 486 | rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_PAGING); |
| 487 | |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 488 | ret = readx_poll_timeout(rk_iommu_is_paging_enabled, iommu, val, |
| 489 | !val, RK_MMU_POLL_PERIOD_US, |
| 490 | RK_MMU_POLL_TIMEOUT_US); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 491 | if (ret) |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 492 | for (i = 0; i < iommu->num_mmu; i++) |
| 493 | dev_err(iommu->dev, "Disable paging request timed out, status: %#08x\n", |
| 494 | rk_iommu_read(iommu->bases[i], RK_MMU_STATUS)); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | static int rk_iommu_force_reset(struct rk_iommu *iommu) |
| 500 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 501 | int ret, i; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 502 | u32 dte_addr; |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 503 | bool val; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 504 | |
Simon Xue | c3aa474 | 2017-07-24 10:37:15 +0800 | [diff] [blame] | 505 | if (iommu->reset_disabled) |
| 506 | return 0; |
| 507 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 508 | /* |
| 509 | * Check if register DTE_ADDR is working by writing DTE_ADDR_DUMMY |
| 510 | * and verifying that upper 5 nybbles are read back. |
| 511 | */ |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 512 | for (i = 0; i < iommu->num_mmu; i++) { |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 513 | dte_addr = rk_ops->pt_address(DTE_ADDR_DUMMY); |
| 514 | rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, dte_addr); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 515 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 516 | if (dte_addr != rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR)) { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 517 | dev_err(iommu->dev, "Error during raw reset. MMU_DTE_ADDR is not functioning\n"); |
| 518 | return -EFAULT; |
| 519 | } |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | rk_iommu_command(iommu, RK_MMU_CMD_FORCE_RESET); |
| 523 | |
Tomasz Figa | 0416bf6 | 2018-03-23 15:38:05 +0800 | [diff] [blame] | 524 | ret = readx_poll_timeout(rk_iommu_is_reset_done, iommu, val, |
| 525 | val, RK_MMU_FORCE_RESET_TIMEOUT_US, |
| 526 | RK_MMU_POLL_TIMEOUT_US); |
| 527 | if (ret) { |
| 528 | dev_err(iommu->dev, "FORCE_RESET command timed out\n"); |
| 529 | return ret; |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 530 | } |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 531 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 532 | return 0; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 533 | } |
| 534 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 535 | static inline phys_addr_t rk_dte_addr_phys(u32 addr) |
| 536 | { |
| 537 | return (phys_addr_t)addr; |
| 538 | } |
| 539 | |
| 540 | static inline u32 rk_dma_addr_dte(dma_addr_t dt_dma) |
| 541 | { |
| 542 | return dt_dma; |
| 543 | } |
| 544 | |
Benjamin Gaignard | c55356c | 2021-06-04 18:44:41 +0200 | [diff] [blame] | 545 | #define DT_HI_MASK GENMASK_ULL(39, 32) |
Benjamin Gaignard | c987b65 | 2021-07-12 12:12:32 +0200 | [diff] [blame] | 546 | #define DTE_BASE_HI_MASK GENMASK(11, 4) |
Benjamin Gaignard | c55356c | 2021-06-04 18:44:41 +0200 | [diff] [blame] | 547 | #define DT_SHIFT 28 |
| 548 | |
| 549 | static inline phys_addr_t rk_dte_addr_phys_v2(u32 addr) |
| 550 | { |
Benjamin Gaignard | c987b65 | 2021-07-12 12:12:32 +0200 | [diff] [blame] | 551 | u64 addr64 = addr; |
| 552 | return (phys_addr_t)(addr64 & RK_DTE_PT_ADDRESS_MASK) | |
| 553 | ((addr64 & DTE_BASE_HI_MASK) << DT_SHIFT); |
Benjamin Gaignard | c55356c | 2021-06-04 18:44:41 +0200 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static inline u32 rk_dma_addr_dte_v2(dma_addr_t dt_dma) |
| 557 | { |
| 558 | return (dt_dma & RK_DTE_PT_ADDRESS_MASK) | |
| 559 | ((dt_dma & DT_HI_MASK) >> DT_SHIFT); |
| 560 | } |
| 561 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 562 | static void log_iova(struct rk_iommu *iommu, int index, dma_addr_t iova) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 563 | { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 564 | void __iomem *base = iommu->bases[index]; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 565 | u32 dte_index, pte_index, page_offset; |
| 566 | u32 mmu_dte_addr; |
| 567 | phys_addr_t mmu_dte_addr_phys, dte_addr_phys; |
| 568 | u32 *dte_addr; |
| 569 | u32 dte; |
| 570 | phys_addr_t pte_addr_phys = 0; |
| 571 | u32 *pte_addr = NULL; |
| 572 | u32 pte = 0; |
| 573 | phys_addr_t page_addr_phys = 0; |
| 574 | u32 page_flags = 0; |
| 575 | |
| 576 | dte_index = rk_iova_dte_index(iova); |
| 577 | pte_index = rk_iova_pte_index(iova); |
| 578 | page_offset = rk_iova_page_offset(iova); |
| 579 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 580 | mmu_dte_addr = rk_iommu_read(base, RK_MMU_DTE_ADDR); |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 581 | mmu_dte_addr_phys = rk_ops->dte_addr_phys(mmu_dte_addr); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 582 | |
| 583 | dte_addr_phys = mmu_dte_addr_phys + (4 * dte_index); |
| 584 | dte_addr = phys_to_virt(dte_addr_phys); |
| 585 | dte = *dte_addr; |
| 586 | |
| 587 | if (!rk_dte_is_pt_valid(dte)) |
| 588 | goto print_it; |
| 589 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 590 | pte_addr_phys = rk_ops->pt_address(dte) + (pte_index * 4); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 591 | pte_addr = phys_to_virt(pte_addr_phys); |
| 592 | pte = *pte_addr; |
| 593 | |
| 594 | if (!rk_pte_is_page_valid(pte)) |
| 595 | goto print_it; |
| 596 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 597 | page_addr_phys = rk_ops->pt_address(pte) + page_offset; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 598 | page_flags = pte & RK_PTE_PAGE_FLAGS_MASK; |
| 599 | |
| 600 | print_it: |
| 601 | dev_err(iommu->dev, "iova = %pad: dte_index: %#03x pte_index: %#03x page_offset: %#03x\n", |
| 602 | &iova, dte_index, pte_index, page_offset); |
| 603 | dev_err(iommu->dev, "mmu_dte_addr: %pa dte@%pa: %#08x valid: %u pte@%pa: %#08x valid: %u page@%pa flags: %#03x\n", |
| 604 | &mmu_dte_addr_phys, &dte_addr_phys, dte, |
| 605 | rk_dte_is_pt_valid(dte), &pte_addr_phys, pte, |
| 606 | rk_pte_is_page_valid(pte), &page_addr_phys, page_flags); |
| 607 | } |
| 608 | |
| 609 | static irqreturn_t rk_iommu_irq(int irq, void *dev_id) |
| 610 | { |
| 611 | struct rk_iommu *iommu = dev_id; |
| 612 | u32 status; |
| 613 | u32 int_status; |
| 614 | dma_addr_t iova; |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 615 | irqreturn_t ret = IRQ_NONE; |
Marc Zyngier | 3fc7c5c | 2018-08-24 16:06:36 +0100 | [diff] [blame] | 616 | int i, err; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 617 | |
Marc Zyngier | 3fc7c5c | 2018-08-24 16:06:36 +0100 | [diff] [blame] | 618 | err = pm_runtime_get_if_in_use(iommu->dev); |
Robin Murphy | 5b47748 | 2019-11-11 18:55:18 +0000 | [diff] [blame] | 619 | if (!err || WARN_ON_ONCE(err < 0)) |
Marc Zyngier | 3fc7c5c | 2018-08-24 16:06:36 +0100 | [diff] [blame] | 620 | return ret; |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 621 | |
| 622 | if (WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks))) |
| 623 | goto out; |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 624 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 625 | for (i = 0; i < iommu->num_mmu; i++) { |
| 626 | int_status = rk_iommu_read(iommu->bases[i], RK_MMU_INT_STATUS); |
| 627 | if (int_status == 0) |
| 628 | continue; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 629 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 630 | ret = IRQ_HANDLED; |
| 631 | iova = rk_iommu_read(iommu->bases[i], RK_MMU_PAGE_FAULT_ADDR); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 632 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 633 | if (int_status & RK_MMU_IRQ_PAGE_FAULT) { |
| 634 | int flags; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 635 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 636 | status = rk_iommu_read(iommu->bases[i], RK_MMU_STATUS); |
| 637 | flags = (status & RK_MMU_STATUS_PAGE_FAULT_IS_WRITE) ? |
| 638 | IOMMU_FAULT_WRITE : IOMMU_FAULT_READ; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 639 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 640 | dev_err(iommu->dev, "Page fault at %pad of type %s\n", |
| 641 | &iova, |
| 642 | (flags == IOMMU_FAULT_WRITE) ? "write" : "read"); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 643 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 644 | log_iova(iommu, i, iova); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 645 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 646 | /* |
| 647 | * Report page fault to any installed handlers. |
| 648 | * Ignore the return code, though, since we always zap cache |
| 649 | * and clear the page fault anyway. |
| 650 | */ |
| 651 | if (iommu->domain) |
| 652 | report_iommu_fault(iommu->domain, iommu->dev, iova, |
| 653 | flags); |
| 654 | else |
| 655 | dev_err(iommu->dev, "Page fault while iommu not attached to domain?\n"); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 656 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 657 | rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE); |
| 658 | rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_PAGE_FAULT_DONE); |
| 659 | } |
| 660 | |
| 661 | if (int_status & RK_MMU_IRQ_BUS_ERROR) |
| 662 | dev_err(iommu->dev, "BUS_ERROR occurred at %pad\n", &iova); |
| 663 | |
| 664 | if (int_status & ~RK_MMU_IRQ_MASK) |
| 665 | dev_err(iommu->dev, "unexpected int_status: %#08x\n", |
| 666 | int_status); |
| 667 | |
| 668 | rk_iommu_write(iommu->bases[i], RK_MMU_INT_CLEAR, int_status); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 669 | } |
| 670 | |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 671 | clk_bulk_disable(iommu->num_clocks, iommu->clocks); |
| 672 | |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 673 | out: |
| 674 | pm_runtime_put(iommu->dev); |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 675 | return ret; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static phys_addr_t rk_iommu_iova_to_phys(struct iommu_domain *domain, |
| 679 | dma_addr_t iova) |
| 680 | { |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 681 | struct rk_iommu_domain *rk_domain = to_rk_domain(domain); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 682 | unsigned long flags; |
| 683 | phys_addr_t pt_phys, phys = 0; |
| 684 | u32 dte, pte; |
| 685 | u32 *page_table; |
| 686 | |
| 687 | spin_lock_irqsave(&rk_domain->dt_lock, flags); |
| 688 | |
| 689 | dte = rk_domain->dt[rk_iova_dte_index(iova)]; |
| 690 | if (!rk_dte_is_pt_valid(dte)) |
| 691 | goto out; |
| 692 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 693 | pt_phys = rk_ops->pt_address(dte); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 694 | page_table = (u32 *)phys_to_virt(pt_phys); |
| 695 | pte = page_table[rk_iova_pte_index(iova)]; |
| 696 | if (!rk_pte_is_page_valid(pte)) |
| 697 | goto out; |
| 698 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 699 | phys = rk_ops->pt_address(pte) + rk_iova_page_offset(iova); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 700 | out: |
| 701 | spin_unlock_irqrestore(&rk_domain->dt_lock, flags); |
| 702 | |
| 703 | return phys; |
| 704 | } |
| 705 | |
| 706 | static void rk_iommu_zap_iova(struct rk_iommu_domain *rk_domain, |
| 707 | dma_addr_t iova, size_t size) |
| 708 | { |
| 709 | struct list_head *pos; |
| 710 | unsigned long flags; |
| 711 | |
| 712 | /* shootdown these iova from all iommus using this domain */ |
| 713 | spin_lock_irqsave(&rk_domain->iommus_lock, flags); |
| 714 | list_for_each(pos, &rk_domain->iommus) { |
| 715 | struct rk_iommu *iommu; |
Marc Zyngier | 3fc7c5c | 2018-08-24 16:06:36 +0100 | [diff] [blame] | 716 | int ret; |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 717 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 718 | iommu = list_entry(pos, struct rk_iommu, node); |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 719 | |
| 720 | /* Only zap TLBs of IOMMUs that are powered on. */ |
Marc Zyngier | 3fc7c5c | 2018-08-24 16:06:36 +0100 | [diff] [blame] | 721 | ret = pm_runtime_get_if_in_use(iommu->dev); |
| 722 | if (WARN_ON_ONCE(ret < 0)) |
| 723 | continue; |
| 724 | if (ret) { |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 725 | WARN_ON(clk_bulk_enable(iommu->num_clocks, |
| 726 | iommu->clocks)); |
| 727 | rk_iommu_zap_lines(iommu, iova, size); |
| 728 | clk_bulk_disable(iommu->num_clocks, iommu->clocks); |
| 729 | pm_runtime_put(iommu->dev); |
| 730 | } |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 731 | } |
| 732 | spin_unlock_irqrestore(&rk_domain->iommus_lock, flags); |
| 733 | } |
| 734 | |
Tomasz Figa | d4dd920 | 2015-04-20 20:43:44 +0900 | [diff] [blame] | 735 | static void rk_iommu_zap_iova_first_last(struct rk_iommu_domain *rk_domain, |
| 736 | dma_addr_t iova, size_t size) |
| 737 | { |
| 738 | rk_iommu_zap_iova(rk_domain, iova, SPAGE_SIZE); |
| 739 | if (size > SPAGE_SIZE) |
| 740 | rk_iommu_zap_iova(rk_domain, iova + size - SPAGE_SIZE, |
| 741 | SPAGE_SIZE); |
| 742 | } |
| 743 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 744 | static u32 *rk_dte_get_page_table(struct rk_iommu_domain *rk_domain, |
| 745 | dma_addr_t iova) |
| 746 | { |
| 747 | u32 *page_table, *dte_addr; |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 748 | u32 dte_index, dte; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 749 | phys_addr_t pt_phys; |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 750 | dma_addr_t pt_dma; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 751 | |
| 752 | assert_spin_locked(&rk_domain->dt_lock); |
| 753 | |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 754 | dte_index = rk_iova_dte_index(iova); |
| 755 | dte_addr = &rk_domain->dt[dte_index]; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 756 | dte = *dte_addr; |
| 757 | if (rk_dte_is_pt_valid(dte)) |
| 758 | goto done; |
| 759 | |
| 760 | page_table = (u32 *)get_zeroed_page(GFP_ATOMIC | GFP_DMA32); |
| 761 | if (!page_table) |
| 762 | return ERR_PTR(-ENOMEM); |
| 763 | |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 764 | pt_dma = dma_map_single(dma_dev, page_table, SPAGE_SIZE, DMA_TO_DEVICE); |
| 765 | if (dma_mapping_error(dma_dev, pt_dma)) { |
| 766 | dev_err(dma_dev, "DMA mapping error while allocating page table\n"); |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 767 | free_page((unsigned long)page_table); |
| 768 | return ERR_PTR(-ENOMEM); |
| 769 | } |
| 770 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 771 | dte = rk_ops->mk_dtentries(pt_dma); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 772 | *dte_addr = dte; |
| 773 | |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 774 | rk_table_flush(rk_domain, |
| 775 | rk_domain->dt_dma + dte_index * sizeof(u32), 1); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 776 | done: |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 777 | pt_phys = rk_ops->pt_address(dte); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 778 | return (u32 *)phys_to_virt(pt_phys); |
| 779 | } |
| 780 | |
| 781 | static size_t rk_iommu_unmap_iova(struct rk_iommu_domain *rk_domain, |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 782 | u32 *pte_addr, dma_addr_t pte_dma, |
| 783 | size_t size) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 784 | { |
| 785 | unsigned int pte_count; |
| 786 | unsigned int pte_total = size / SPAGE_SIZE; |
| 787 | |
| 788 | assert_spin_locked(&rk_domain->dt_lock); |
| 789 | |
| 790 | for (pte_count = 0; pte_count < pte_total; pte_count++) { |
| 791 | u32 pte = pte_addr[pte_count]; |
| 792 | if (!rk_pte_is_page_valid(pte)) |
| 793 | break; |
| 794 | |
| 795 | pte_addr[pte_count] = rk_mk_pte_invalid(pte); |
| 796 | } |
| 797 | |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 798 | rk_table_flush(rk_domain, pte_dma, pte_count); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 799 | |
| 800 | return pte_count * SPAGE_SIZE; |
| 801 | } |
| 802 | |
| 803 | static int rk_iommu_map_iova(struct rk_iommu_domain *rk_domain, u32 *pte_addr, |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 804 | dma_addr_t pte_dma, dma_addr_t iova, |
| 805 | phys_addr_t paddr, size_t size, int prot) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 806 | { |
| 807 | unsigned int pte_count; |
| 808 | unsigned int pte_total = size / SPAGE_SIZE; |
| 809 | phys_addr_t page_phys; |
| 810 | |
| 811 | assert_spin_locked(&rk_domain->dt_lock); |
| 812 | |
| 813 | for (pte_count = 0; pte_count < pte_total; pte_count++) { |
| 814 | u32 pte = pte_addr[pte_count]; |
| 815 | |
| 816 | if (rk_pte_is_page_valid(pte)) |
| 817 | goto unwind; |
| 818 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 819 | pte_addr[pte_count] = rk_ops->mk_ptentries(paddr, prot); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 820 | |
| 821 | paddr += SPAGE_SIZE; |
| 822 | } |
| 823 | |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 824 | rk_table_flush(rk_domain, pte_dma, pte_total); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 825 | |
Tomasz Figa | d4dd920 | 2015-04-20 20:43:44 +0900 | [diff] [blame] | 826 | /* |
| 827 | * Zap the first and last iova to evict from iotlb any previously |
| 828 | * mapped cachelines holding stale values for its dte and pte. |
| 829 | * We only zap the first and last iova, since only they could have |
| 830 | * dte or pte shared with an existing mapping. |
| 831 | */ |
| 832 | rk_iommu_zap_iova_first_last(rk_domain, iova, size); |
| 833 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 834 | return 0; |
| 835 | unwind: |
| 836 | /* Unmap the range of iovas that we just mapped */ |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 837 | rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma, |
| 838 | pte_count * SPAGE_SIZE); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 839 | |
| 840 | iova += pte_count * SPAGE_SIZE; |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 841 | page_phys = rk_ops->pt_address(pte_addr[pte_count]); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 842 | pr_err("iova: %pad already mapped to %pa cannot remap to phys: %pa prot: %#x\n", |
| 843 | &iova, &page_phys, &paddr, prot); |
| 844 | |
| 845 | return -EADDRINUSE; |
| 846 | } |
| 847 | |
| 848 | static int rk_iommu_map(struct iommu_domain *domain, unsigned long _iova, |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 849 | phys_addr_t paddr, size_t size, int prot, gfp_t gfp) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 850 | { |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 851 | struct rk_iommu_domain *rk_domain = to_rk_domain(domain); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 852 | unsigned long flags; |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 853 | dma_addr_t pte_dma, iova = (dma_addr_t)_iova; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 854 | u32 *page_table, *pte_addr; |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 855 | u32 dte_index, pte_index; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 856 | int ret; |
| 857 | |
| 858 | spin_lock_irqsave(&rk_domain->dt_lock, flags); |
| 859 | |
| 860 | /* |
| 861 | * pgsize_bitmap specifies iova sizes that fit in one page table |
| 862 | * (1024 4-KiB pages = 4 MiB). |
| 863 | * So, size will always be 4096 <= size <= 4194304. |
| 864 | * Since iommu_map() guarantees that both iova and size will be |
| 865 | * aligned, we will always only be mapping from a single dte here. |
| 866 | */ |
| 867 | page_table = rk_dte_get_page_table(rk_domain, iova); |
| 868 | if (IS_ERR(page_table)) { |
| 869 | spin_unlock_irqrestore(&rk_domain->dt_lock, flags); |
| 870 | return PTR_ERR(page_table); |
| 871 | } |
| 872 | |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 873 | dte_index = rk_domain->dt[rk_iova_dte_index(iova)]; |
| 874 | pte_index = rk_iova_pte_index(iova); |
| 875 | pte_addr = &page_table[pte_index]; |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 876 | |
| 877 | pte_dma = rk_ops->pt_address(dte_index) + pte_index * sizeof(u32); |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 878 | ret = rk_iommu_map_iova(rk_domain, pte_addr, pte_dma, iova, |
| 879 | paddr, size, prot); |
| 880 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 881 | spin_unlock_irqrestore(&rk_domain->dt_lock, flags); |
| 882 | |
| 883 | return ret; |
| 884 | } |
| 885 | |
| 886 | static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova, |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 887 | size_t size, struct iommu_iotlb_gather *gather) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 888 | { |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 889 | struct rk_iommu_domain *rk_domain = to_rk_domain(domain); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 890 | unsigned long flags; |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 891 | dma_addr_t pte_dma, iova = (dma_addr_t)_iova; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 892 | phys_addr_t pt_phys; |
| 893 | u32 dte; |
| 894 | u32 *pte_addr; |
| 895 | size_t unmap_size; |
| 896 | |
| 897 | spin_lock_irqsave(&rk_domain->dt_lock, flags); |
| 898 | |
| 899 | /* |
| 900 | * pgsize_bitmap specifies iova sizes that fit in one page table |
| 901 | * (1024 4-KiB pages = 4 MiB). |
| 902 | * So, size will always be 4096 <= size <= 4194304. |
| 903 | * Since iommu_unmap() guarantees that both iova and size will be |
| 904 | * aligned, we will always only be unmapping from a single dte here. |
| 905 | */ |
| 906 | dte = rk_domain->dt[rk_iova_dte_index(iova)]; |
| 907 | /* Just return 0 if iova is unmapped */ |
| 908 | if (!rk_dte_is_pt_valid(dte)) { |
| 909 | spin_unlock_irqrestore(&rk_domain->dt_lock, flags); |
| 910 | return 0; |
| 911 | } |
| 912 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 913 | pt_phys = rk_ops->pt_address(dte); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 914 | pte_addr = (u32 *)phys_to_virt(pt_phys) + rk_iova_pte_index(iova); |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 915 | pte_dma = pt_phys + rk_iova_pte_index(iova) * sizeof(u32); |
| 916 | unmap_size = rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma, size); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 917 | |
| 918 | spin_unlock_irqrestore(&rk_domain->dt_lock, flags); |
| 919 | |
| 920 | /* Shootdown iotlb entries for iova range that was just unmapped */ |
| 921 | rk_iommu_zap_iova(rk_domain, iova, unmap_size); |
| 922 | |
| 923 | return unmap_size; |
| 924 | } |
| 925 | |
| 926 | static struct rk_iommu *rk_iommu_from_dev(struct device *dev) |
| 927 | { |
Joerg Roedel | 8b9cc3b | 2020-06-25 15:08:28 +0200 | [diff] [blame] | 928 | struct rk_iommudata *data = dev_iommu_priv_get(dev); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 929 | |
Jeffy Chen | 5fd577c | 2018-03-23 15:38:11 +0800 | [diff] [blame] | 930 | return data ? data->iommu : NULL; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 931 | } |
| 932 | |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 933 | /* Must be called with iommu powered on and attached */ |
| 934 | static void rk_iommu_disable(struct rk_iommu *iommu) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 935 | { |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 936 | int i; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 937 | |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 938 | /* Ignore error while disabling, just keep going */ |
| 939 | WARN_ON(clk_bulk_enable(iommu->num_clocks, iommu->clocks)); |
| 940 | rk_iommu_enable_stall(iommu); |
| 941 | rk_iommu_disable_paging(iommu); |
| 942 | for (i = 0; i < iommu->num_mmu; i++) { |
| 943 | rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, 0); |
| 944 | rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, 0); |
| 945 | } |
| 946 | rk_iommu_disable_stall(iommu); |
| 947 | clk_bulk_disable(iommu->num_clocks, iommu->clocks); |
| 948 | } |
| 949 | |
| 950 | /* Must be called with iommu powered on and attached */ |
| 951 | static int rk_iommu_enable(struct rk_iommu *iommu) |
| 952 | { |
| 953 | struct iommu_domain *domain = iommu->domain; |
| 954 | struct rk_iommu_domain *rk_domain = to_rk_domain(domain); |
| 955 | int ret, i; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 956 | |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 957 | ret = clk_bulk_enable(iommu->num_clocks, iommu->clocks); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 958 | if (ret) |
| 959 | return ret; |
| 960 | |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 961 | ret = rk_iommu_enable_stall(iommu); |
| 962 | if (ret) |
| 963 | goto out_disable_clocks; |
| 964 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 965 | ret = rk_iommu_force_reset(iommu); |
| 966 | if (ret) |
Tomasz Figa | f6717d7 | 2018-03-23 15:38:04 +0800 | [diff] [blame] | 967 | goto out_disable_stall; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 968 | |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 969 | for (i = 0; i < iommu->num_mmu; i++) { |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 970 | rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 971 | rk_ops->dma_addr_dte(rk_domain->dt_dma)); |
John Keeping | ae8a791 | 2016-06-01 16:46:10 +0100 | [diff] [blame] | 972 | rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE); |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 973 | rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, RK_MMU_IRQ_MASK); |
| 974 | } |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 975 | |
| 976 | ret = rk_iommu_enable_paging(iommu); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 977 | |
Tomasz Figa | f6717d7 | 2018-03-23 15:38:04 +0800 | [diff] [blame] | 978 | out_disable_stall: |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 979 | rk_iommu_disable_stall(iommu); |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 980 | out_disable_clocks: |
| 981 | clk_bulk_disable(iommu->num_clocks, iommu->clocks); |
Tomasz Figa | f6717d7 | 2018-03-23 15:38:04 +0800 | [diff] [blame] | 982 | return ret; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | static void rk_iommu_detach_device(struct iommu_domain *domain, |
| 986 | struct device *dev) |
| 987 | { |
| 988 | struct rk_iommu *iommu; |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 989 | struct rk_iommu_domain *rk_domain = to_rk_domain(domain); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 990 | unsigned long flags; |
Marc Zyngier | 3fc7c5c | 2018-08-24 16:06:36 +0100 | [diff] [blame] | 991 | int ret; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 992 | |
| 993 | /* Allow 'virtual devices' (eg drm) to detach from domain */ |
| 994 | iommu = rk_iommu_from_dev(dev); |
| 995 | if (!iommu) |
| 996 | return; |
| 997 | |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 998 | dev_dbg(dev, "Detaching from iommu domain\n"); |
| 999 | |
| 1000 | /* iommu already detached */ |
| 1001 | if (iommu->domain != domain) |
| 1002 | return; |
| 1003 | |
| 1004 | iommu->domain = NULL; |
| 1005 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1006 | spin_lock_irqsave(&rk_domain->iommus_lock, flags); |
| 1007 | list_del_init(&iommu->node); |
| 1008 | spin_unlock_irqrestore(&rk_domain->iommus_lock, flags); |
| 1009 | |
Marc Zyngier | 3fc7c5c | 2018-08-24 16:06:36 +0100 | [diff] [blame] | 1010 | ret = pm_runtime_get_if_in_use(iommu->dev); |
| 1011 | WARN_ON_ONCE(ret < 0); |
| 1012 | if (ret > 0) { |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1013 | rk_iommu_disable(iommu); |
| 1014 | pm_runtime_put(iommu->dev); |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 1015 | } |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1016 | } |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1017 | |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1018 | static int rk_iommu_attach_device(struct iommu_domain *domain, |
| 1019 | struct device *dev) |
| 1020 | { |
| 1021 | struct rk_iommu *iommu; |
| 1022 | struct rk_iommu_domain *rk_domain = to_rk_domain(domain); |
| 1023 | unsigned long flags; |
| 1024 | int ret; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1025 | |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1026 | /* |
| 1027 | * Allow 'virtual devices' (e.g., drm) to attach to domain. |
| 1028 | * Such a device does not belong to an iommu group. |
| 1029 | */ |
| 1030 | iommu = rk_iommu_from_dev(dev); |
| 1031 | if (!iommu) |
| 1032 | return 0; |
| 1033 | |
| 1034 | dev_dbg(dev, "Attaching to iommu domain\n"); |
| 1035 | |
| 1036 | /* iommu already attached */ |
| 1037 | if (iommu->domain == domain) |
| 1038 | return 0; |
| 1039 | |
| 1040 | if (iommu->domain) |
| 1041 | rk_iommu_detach_device(iommu->domain, dev); |
| 1042 | |
| 1043 | iommu->domain = domain; |
| 1044 | |
| 1045 | spin_lock_irqsave(&rk_domain->iommus_lock, flags); |
| 1046 | list_add_tail(&iommu->node, &rk_domain->iommus); |
| 1047 | spin_unlock_irqrestore(&rk_domain->iommus_lock, flags); |
| 1048 | |
Marc Zyngier | 3fc7c5c | 2018-08-24 16:06:36 +0100 | [diff] [blame] | 1049 | ret = pm_runtime_get_if_in_use(iommu->dev); |
| 1050 | if (!ret || WARN_ON_ONCE(ret < 0)) |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1051 | return 0; |
| 1052 | |
| 1053 | ret = rk_iommu_enable(iommu); |
| 1054 | if (ret) |
| 1055 | rk_iommu_detach_device(iommu->domain, dev); |
| 1056 | |
| 1057 | pm_runtime_put(iommu->dev); |
| 1058 | |
| 1059 | return ret; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1060 | } |
| 1061 | |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 1062 | static struct iommu_domain *rk_iommu_domain_alloc(unsigned type) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1063 | { |
| 1064 | struct rk_iommu_domain *rk_domain; |
| 1065 | |
Shunqian Zheng | a93db2f | 2016-06-24 10:13:30 +0800 | [diff] [blame] | 1066 | if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA) |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 1067 | return NULL; |
| 1068 | |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 1069 | if (!dma_dev) |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 1070 | return NULL; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1071 | |
Ezequiel Garcia | 42bb97b | 2019-10-02 14:29:23 -0300 | [diff] [blame] | 1072 | rk_domain = kzalloc(sizeof(*rk_domain), GFP_KERNEL); |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 1073 | if (!rk_domain) |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 1074 | return NULL; |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 1075 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1076 | /* |
| 1077 | * rk32xx iommus use a 2 level pagetable. |
| 1078 | * Each level1 (dt) and level2 (pt) table has 1024 4-byte entries. |
| 1079 | * Allocate one 4 KiB page for each table. |
| 1080 | */ |
| 1081 | rk_domain->dt = (u32 *)get_zeroed_page(GFP_KERNEL | GFP_DMA32); |
| 1082 | if (!rk_domain->dt) |
Robin Murphy | b811a45 | 2021-08-11 13:21:22 +0100 | [diff] [blame] | 1083 | goto err_free_domain; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1084 | |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 1085 | rk_domain->dt_dma = dma_map_single(dma_dev, rk_domain->dt, |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 1086 | SPAGE_SIZE, DMA_TO_DEVICE); |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 1087 | if (dma_mapping_error(dma_dev, rk_domain->dt_dma)) { |
| 1088 | dev_err(dma_dev, "DMA map error for DT\n"); |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 1089 | goto err_free_dt; |
| 1090 | } |
| 1091 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1092 | spin_lock_init(&rk_domain->iommus_lock); |
| 1093 | spin_lock_init(&rk_domain->dt_lock); |
| 1094 | INIT_LIST_HEAD(&rk_domain->iommus); |
| 1095 | |
Shunqian Zheng | a93db2f | 2016-06-24 10:13:30 +0800 | [diff] [blame] | 1096 | rk_domain->domain.geometry.aperture_start = 0; |
| 1097 | rk_domain->domain.geometry.aperture_end = DMA_BIT_MASK(32); |
| 1098 | rk_domain->domain.geometry.force_aperture = true; |
| 1099 | |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 1100 | return &rk_domain->domain; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1101 | |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 1102 | err_free_dt: |
| 1103 | free_page((unsigned long)rk_domain->dt); |
Ezequiel Garcia | 42bb97b | 2019-10-02 14:29:23 -0300 | [diff] [blame] | 1104 | err_free_domain: |
| 1105 | kfree(rk_domain); |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 1106 | |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 1107 | return NULL; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 1110 | static void rk_iommu_domain_free(struct iommu_domain *domain) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1111 | { |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 1112 | struct rk_iommu_domain *rk_domain = to_rk_domain(domain); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1113 | int i; |
| 1114 | |
| 1115 | WARN_ON(!list_empty(&rk_domain->iommus)); |
| 1116 | |
| 1117 | for (i = 0; i < NUM_DT_ENTRIES; i++) { |
| 1118 | u32 dte = rk_domain->dt[i]; |
| 1119 | if (rk_dte_is_pt_valid(dte)) { |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 1120 | phys_addr_t pt_phys = rk_ops->pt_address(dte); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1121 | u32 *page_table = phys_to_virt(pt_phys); |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 1122 | dma_unmap_single(dma_dev, pt_phys, |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 1123 | SPAGE_SIZE, DMA_TO_DEVICE); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1124 | free_page((unsigned long)page_table); |
| 1125 | } |
| 1126 | } |
| 1127 | |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 1128 | dma_unmap_single(dma_dev, rk_domain->dt_dma, |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 1129 | SPAGE_SIZE, DMA_TO_DEVICE); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1130 | free_page((unsigned long)rk_domain->dt); |
Shunqian Zheng | 4f0aba6 | 2016-06-24 10:13:29 +0800 | [diff] [blame] | 1131 | |
Ezequiel Garcia | 42bb97b | 2019-10-02 14:29:23 -0300 | [diff] [blame] | 1132 | kfree(rk_domain); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1133 | } |
| 1134 | |
Joerg Roedel | d826044 | 2020-04-29 15:37:03 +0200 | [diff] [blame] | 1135 | static struct iommu_device *rk_iommu_probe_device(struct device *dev) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1136 | { |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1137 | struct rk_iommudata *data; |
Joerg Roedel | d826044 | 2020-04-29 15:37:03 +0200 | [diff] [blame] | 1138 | struct rk_iommu *iommu; |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1139 | |
Joerg Roedel | 8b9cc3b | 2020-06-25 15:08:28 +0200 | [diff] [blame] | 1140 | data = dev_iommu_priv_get(dev); |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1141 | if (!data) |
Joerg Roedel | d826044 | 2020-04-29 15:37:03 +0200 | [diff] [blame] | 1142 | return ERR_PTR(-ENODEV); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1143 | |
Joerg Roedel | c9d9f23 | 2017-03-31 16:26:03 +0200 | [diff] [blame] | 1144 | iommu = rk_iommu_from_dev(dev); |
Joerg Roedel | c9d9f23 | 2017-03-31 16:26:03 +0200 | [diff] [blame] | 1145 | |
Rafael J. Wysocki | ea4f640 | 2019-02-01 01:54:21 +0100 | [diff] [blame] | 1146 | data->link = device_link_add(dev, iommu->dev, |
| 1147 | DL_FLAG_STATELESS | DL_FLAG_PM_RUNTIME); |
Jeffy Chen | 5fd577c | 2018-03-23 15:38:11 +0800 | [diff] [blame] | 1148 | |
Joerg Roedel | d826044 | 2020-04-29 15:37:03 +0200 | [diff] [blame] | 1149 | return &iommu->iommu; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Joerg Roedel | d826044 | 2020-04-29 15:37:03 +0200 | [diff] [blame] | 1152 | static void rk_iommu_release_device(struct device *dev) |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1153 | { |
Joerg Roedel | 8b9cc3b | 2020-06-25 15:08:28 +0200 | [diff] [blame] | 1154 | struct rk_iommudata *data = dev_iommu_priv_get(dev); |
Joerg Roedel | c9d9f23 | 2017-03-31 16:26:03 +0200 | [diff] [blame] | 1155 | |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1156 | device_link_del(data->link); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Jeffy Chen | 57c2695 | 2018-03-23 15:38:14 +0800 | [diff] [blame] | 1159 | static struct iommu_group *rk_iommu_device_group(struct device *dev) |
| 1160 | { |
| 1161 | struct rk_iommu *iommu; |
| 1162 | |
| 1163 | iommu = rk_iommu_from_dev(dev); |
| 1164 | |
| 1165 | return iommu_group_ref_get(iommu->group); |
| 1166 | } |
| 1167 | |
Jeffy Chen | 5fd577c | 2018-03-23 15:38:11 +0800 | [diff] [blame] | 1168 | static int rk_iommu_of_xlate(struct device *dev, |
| 1169 | struct of_phandle_args *args) |
| 1170 | { |
| 1171 | struct platform_device *iommu_dev; |
| 1172 | struct rk_iommudata *data; |
| 1173 | |
| 1174 | data = devm_kzalloc(dma_dev, sizeof(*data), GFP_KERNEL); |
| 1175 | if (!data) |
| 1176 | return -ENOMEM; |
| 1177 | |
| 1178 | iommu_dev = of_find_device_by_node(args->np); |
| 1179 | |
| 1180 | data->iommu = platform_get_drvdata(iommu_dev); |
Joerg Roedel | 8b9cc3b | 2020-06-25 15:08:28 +0200 | [diff] [blame] | 1181 | dev_iommu_priv_set(dev, data); |
Jeffy Chen | 5fd577c | 2018-03-23 15:38:11 +0800 | [diff] [blame] | 1182 | |
Arnd Bergmann | 40fa84e | 2018-04-04 12:23:53 +0200 | [diff] [blame] | 1183 | platform_device_put(iommu_dev); |
Jeffy Chen | 5fd577c | 2018-03-23 15:38:11 +0800 | [diff] [blame] | 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1188 | static const struct iommu_ops rk_iommu_ops = { |
Joerg Roedel | bcd516a | 2015-03-26 13:43:17 +0100 | [diff] [blame] | 1189 | .domain_alloc = rk_iommu_domain_alloc, |
| 1190 | .domain_free = rk_iommu_domain_free, |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1191 | .attach_dev = rk_iommu_attach_device, |
| 1192 | .detach_dev = rk_iommu_detach_device, |
| 1193 | .map = rk_iommu_map, |
| 1194 | .unmap = rk_iommu_unmap, |
Joerg Roedel | d826044 | 2020-04-29 15:37:03 +0200 | [diff] [blame] | 1195 | .probe_device = rk_iommu_probe_device, |
| 1196 | .release_device = rk_iommu_release_device, |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1197 | .iova_to_phys = rk_iommu_iova_to_phys, |
Jeffy Chen | 57c2695 | 2018-03-23 15:38:14 +0800 | [diff] [blame] | 1198 | .device_group = rk_iommu_device_group, |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1199 | .pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP, |
Jeffy Chen | 5fd577c | 2018-03-23 15:38:11 +0800 | [diff] [blame] | 1200 | .of_xlate = rk_iommu_of_xlate, |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1201 | }; |
| 1202 | |
| 1203 | static int rk_iommu_probe(struct platform_device *pdev) |
| 1204 | { |
| 1205 | struct device *dev = &pdev->dev; |
| 1206 | struct rk_iommu *iommu; |
| 1207 | struct resource *res; |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 1208 | const struct rk_iommu_ops *ops; |
Shunqian Zheng | 3d08f434 | 2016-06-24 10:13:28 +0800 | [diff] [blame] | 1209 | int num_res = pdev->num_resources; |
Heiko Stuebner | f925815 | 2019-09-25 20:43:46 +0200 | [diff] [blame] | 1210 | int err, i; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1211 | |
| 1212 | iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL); |
| 1213 | if (!iommu) |
| 1214 | return -ENOMEM; |
| 1215 | |
| 1216 | platform_set_drvdata(pdev, iommu); |
| 1217 | iommu->dev = dev; |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 1218 | iommu->num_mmu = 0; |
Shunqian Zheng | 3d08f434 | 2016-06-24 10:13:28 +0800 | [diff] [blame] | 1219 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 1220 | ops = of_device_get_match_data(dev); |
| 1221 | if (!rk_ops) |
| 1222 | rk_ops = ops; |
| 1223 | |
| 1224 | /* |
| 1225 | * That should not happen unless different versions of the |
| 1226 | * hardware block are embedded the same SoC |
| 1227 | */ |
| 1228 | if (WARN_ON(rk_ops != ops)) |
| 1229 | return -EINVAL; |
| 1230 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 1231 | iommu->bases = devm_kcalloc(dev, num_res, sizeof(*iommu->bases), |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 1232 | GFP_KERNEL); |
| 1233 | if (!iommu->bases) |
| 1234 | return -ENOMEM; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1235 | |
Shunqian Zheng | 3d08f434 | 2016-06-24 10:13:28 +0800 | [diff] [blame] | 1236 | for (i = 0; i < num_res; i++) { |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 1237 | res = platform_get_resource(pdev, IORESOURCE_MEM, i); |
Tomeu Vizoso | 8d7f2d8 | 2016-03-21 12:00:23 +0100 | [diff] [blame] | 1238 | if (!res) |
| 1239 | continue; |
ZhengShunQian | cd6438c | 2016-01-19 15:03:00 +0800 | [diff] [blame] | 1240 | iommu->bases[i] = devm_ioremap_resource(&pdev->dev, res); |
| 1241 | if (IS_ERR(iommu->bases[i])) |
| 1242 | continue; |
| 1243 | iommu->num_mmu++; |
| 1244 | } |
| 1245 | if (iommu->num_mmu == 0) |
| 1246 | return PTR_ERR(iommu->bases[0]); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1247 | |
Heiko Stuebner | f925815 | 2019-09-25 20:43:46 +0200 | [diff] [blame] | 1248 | iommu->num_irq = platform_irq_count(pdev); |
| 1249 | if (iommu->num_irq < 0) |
| 1250 | return iommu->num_irq; |
| 1251 | |
Simon Xue | c3aa474 | 2017-07-24 10:37:15 +0800 | [diff] [blame] | 1252 | iommu->reset_disabled = device_property_read_bool(dev, |
| 1253 | "rockchip,disable-mmu-reset"); |
| 1254 | |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 1255 | iommu->num_clocks = ARRAY_SIZE(rk_iommu_clocks); |
| 1256 | iommu->clocks = devm_kcalloc(iommu->dev, iommu->num_clocks, |
| 1257 | sizeof(*iommu->clocks), GFP_KERNEL); |
| 1258 | if (!iommu->clocks) |
| 1259 | return -ENOMEM; |
| 1260 | |
| 1261 | for (i = 0; i < iommu->num_clocks; ++i) |
| 1262 | iommu->clocks[i].id = rk_iommu_clocks[i]; |
| 1263 | |
Heiko Stuebner | 2f8c7f2 | 2018-04-17 14:09:15 +0200 | [diff] [blame] | 1264 | /* |
| 1265 | * iommu clocks should be present for all new devices and devicetrees |
| 1266 | * but there are older devicetrees without clocks out in the wild. |
| 1267 | * So clocks as optional for the time being. |
| 1268 | */ |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 1269 | err = devm_clk_bulk_get(iommu->dev, iommu->num_clocks, iommu->clocks); |
Heiko Stuebner | 2f8c7f2 | 2018-04-17 14:09:15 +0200 | [diff] [blame] | 1270 | if (err == -ENOENT) |
| 1271 | iommu->num_clocks = 0; |
| 1272 | else if (err) |
Joerg Roedel | c9d9f23 | 2017-03-31 16:26:03 +0200 | [diff] [blame] | 1273 | return err; |
| 1274 | |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 1275 | err = clk_bulk_prepare(iommu->num_clocks, iommu->clocks); |
| 1276 | if (err) |
| 1277 | return err; |
| 1278 | |
Jeffy Chen | 57c2695 | 2018-03-23 15:38:14 +0800 | [diff] [blame] | 1279 | iommu->group = iommu_group_alloc(); |
| 1280 | if (IS_ERR(iommu->group)) { |
| 1281 | err = PTR_ERR(iommu->group); |
| 1282 | goto err_unprepare_clocks; |
| 1283 | } |
| 1284 | |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 1285 | err = iommu_device_sysfs_add(&iommu->iommu, dev, NULL, dev_name(dev)); |
| 1286 | if (err) |
Jeffy Chen | 57c2695 | 2018-03-23 15:38:14 +0800 | [diff] [blame] | 1287 | goto err_put_group; |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 1288 | |
Robin Murphy | 2d471b2 | 2021-04-01 14:56:26 +0100 | [diff] [blame] | 1289 | err = iommu_device_register(&iommu->iommu, &rk_iommu_ops, dev); |
Jeffy Chen | 6d9ffaa | 2018-03-23 15:38:02 +0800 | [diff] [blame] | 1290 | if (err) |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 1291 | goto err_remove_sysfs; |
Joerg Roedel | c9d9f23 | 2017-03-31 16:26:03 +0200 | [diff] [blame] | 1292 | |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 1293 | /* |
| 1294 | * Use the first registered IOMMU device for domain to use with DMA |
| 1295 | * API, since a domain might not physically correspond to a single |
| 1296 | * IOMMU device.. |
| 1297 | */ |
| 1298 | if (!dma_dev) |
| 1299 | dma_dev = &pdev->dev; |
| 1300 | |
Jeffy Chen | 4d88a8a | 2018-03-23 15:38:12 +0800 | [diff] [blame] | 1301 | bus_set_iommu(&platform_bus_type, &rk_iommu_ops); |
| 1302 | |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1303 | pm_runtime_enable(dev); |
| 1304 | |
Heiko Stuebner | f925815 | 2019-09-25 20:43:46 +0200 | [diff] [blame] | 1305 | for (i = 0; i < iommu->num_irq; i++) { |
| 1306 | int irq = platform_get_irq(pdev, i); |
| 1307 | |
Marc Zyngier | 1aa55ca | 2018-08-24 16:06:37 +0100 | [diff] [blame] | 1308 | if (irq < 0) |
| 1309 | return irq; |
| 1310 | |
| 1311 | err = devm_request_irq(iommu->dev, irq, rk_iommu_irq, |
| 1312 | IRQF_SHARED, dev_name(dev), iommu); |
| 1313 | if (err) { |
| 1314 | pm_runtime_disable(dev); |
| 1315 | goto err_remove_sysfs; |
| 1316 | } |
| 1317 | } |
| 1318 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 1319 | dma_set_mask_and_coherent(dev, rk_ops->dma_bit_mask); |
| 1320 | |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 1321 | return 0; |
| 1322 | err_remove_sysfs: |
| 1323 | iommu_device_sysfs_remove(&iommu->iommu); |
Jeffy Chen | 57c2695 | 2018-03-23 15:38:14 +0800 | [diff] [blame] | 1324 | err_put_group: |
| 1325 | iommu_group_put(iommu->group); |
Tomasz Figa | f2e3a5f | 2018-03-23 15:38:08 +0800 | [diff] [blame] | 1326 | err_unprepare_clocks: |
| 1327 | clk_bulk_unprepare(iommu->num_clocks, iommu->clocks); |
Joerg Roedel | c9d9f23 | 2017-03-31 16:26:03 +0200 | [diff] [blame] | 1328 | return err; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Marc Zyngier | 1a4e90f | 2018-02-20 20:25:04 +0000 | [diff] [blame] | 1331 | static void rk_iommu_shutdown(struct platform_device *pdev) |
| 1332 | { |
Heiko Stuebner | 74bc2ab | 2018-08-27 12:56:24 +0200 | [diff] [blame] | 1333 | struct rk_iommu *iommu = platform_get_drvdata(pdev); |
Heiko Stuebner | f925815 | 2019-09-25 20:43:46 +0200 | [diff] [blame] | 1334 | int i; |
Heiko Stuebner | 74bc2ab | 2018-08-27 12:56:24 +0200 | [diff] [blame] | 1335 | |
Heiko Stuebner | f925815 | 2019-09-25 20:43:46 +0200 | [diff] [blame] | 1336 | for (i = 0; i < iommu->num_irq; i++) { |
| 1337 | int irq = platform_get_irq(pdev, i); |
| 1338 | |
Heiko Stuebner | 74bc2ab | 2018-08-27 12:56:24 +0200 | [diff] [blame] | 1339 | devm_free_irq(iommu->dev, irq, iommu); |
Heiko Stuebner | f925815 | 2019-09-25 20:43:46 +0200 | [diff] [blame] | 1340 | } |
Heiko Stuebner | 74bc2ab | 2018-08-27 12:56:24 +0200 | [diff] [blame] | 1341 | |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1342 | pm_runtime_force_suspend(&pdev->dev); |
Marc Zyngier | 1a4e90f | 2018-02-20 20:25:04 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1345 | static int __maybe_unused rk_iommu_suspend(struct device *dev) |
| 1346 | { |
| 1347 | struct rk_iommu *iommu = dev_get_drvdata(dev); |
| 1348 | |
| 1349 | if (!iommu->domain) |
| 1350 | return 0; |
| 1351 | |
| 1352 | rk_iommu_disable(iommu); |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
| 1356 | static int __maybe_unused rk_iommu_resume(struct device *dev) |
| 1357 | { |
| 1358 | struct rk_iommu *iommu = dev_get_drvdata(dev); |
| 1359 | |
| 1360 | if (!iommu->domain) |
| 1361 | return 0; |
| 1362 | |
| 1363 | return rk_iommu_enable(iommu); |
| 1364 | } |
| 1365 | |
| 1366 | static const struct dev_pm_ops rk_iommu_pm_ops = { |
| 1367 | SET_RUNTIME_PM_OPS(rk_iommu_suspend, rk_iommu_resume, NULL) |
| 1368 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 1369 | pm_runtime_force_resume) |
| 1370 | }; |
| 1371 | |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 1372 | static struct rk_iommu_ops iommu_data_ops_v1 = { |
| 1373 | .pt_address = &rk_dte_pt_address, |
| 1374 | .mk_dtentries = &rk_mk_dte, |
| 1375 | .mk_ptentries = &rk_mk_pte, |
| 1376 | .dte_addr_phys = &rk_dte_addr_phys, |
| 1377 | .dma_addr_dte = &rk_dma_addr_dte, |
| 1378 | .dma_bit_mask = DMA_BIT_MASK(32), |
| 1379 | }; |
| 1380 | |
Benjamin Gaignard | c55356c | 2021-06-04 18:44:41 +0200 | [diff] [blame] | 1381 | static struct rk_iommu_ops iommu_data_ops_v2 = { |
| 1382 | .pt_address = &rk_dte_pt_address_v2, |
| 1383 | .mk_dtentries = &rk_mk_dte_v2, |
| 1384 | .mk_ptentries = &rk_mk_pte_v2, |
| 1385 | .dte_addr_phys = &rk_dte_addr_phys_v2, |
| 1386 | .dma_addr_dte = &rk_dma_addr_dte_v2, |
| 1387 | .dma_bit_mask = DMA_BIT_MASK(40), |
| 1388 | }; |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 1389 | |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1390 | static const struct of_device_id rk_iommu_dt_ids[] = { |
Benjamin Gaignard | 227014b | 2021-06-04 18:44:40 +0200 | [diff] [blame] | 1391 | { .compatible = "rockchip,iommu", |
| 1392 | .data = &iommu_data_ops_v1, |
| 1393 | }, |
Benjamin Gaignard | c55356c | 2021-06-04 18:44:41 +0200 | [diff] [blame] | 1394 | { .compatible = "rockchip,rk3568-iommu", |
| 1395 | .data = &iommu_data_ops_v2, |
| 1396 | }, |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1397 | { /* sentinel */ } |
| 1398 | }; |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1399 | |
| 1400 | static struct platform_driver rk_iommu_driver = { |
| 1401 | .probe = rk_iommu_probe, |
Marc Zyngier | 1a4e90f | 2018-02-20 20:25:04 +0000 | [diff] [blame] | 1402 | .shutdown = rk_iommu_shutdown, |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1403 | .driver = { |
| 1404 | .name = "rk_iommu", |
Arnd Bergmann | d9e7eb1 | 2015-04-10 23:58:24 +0200 | [diff] [blame] | 1405 | .of_match_table = rk_iommu_dt_ids, |
Jeffy Chen | 0f181d3 | 2018-03-23 15:38:13 +0800 | [diff] [blame] | 1406 | .pm = &rk_iommu_pm_ops, |
Jeffy Chen | 98b72b9 | 2018-03-23 15:38:01 +0800 | [diff] [blame] | 1407 | .suppress_bind_attrs = true, |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1408 | }, |
| 1409 | }; |
| 1410 | |
| 1411 | static int __init rk_iommu_init(void) |
| 1412 | { |
Jeffy Chen | 9176a30 | 2018-03-23 15:38:10 +0800 | [diff] [blame] | 1413 | return platform_driver_register(&rk_iommu_driver); |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1414 | } |
Daniel Kurtz | c68a292 | 2014-11-03 10:53:27 +0800 | [diff] [blame] | 1415 | subsys_initcall(rk_iommu_init); |