blob: c3df53e480ea6887b4b16aa0cfabaf023703b0a8 [file] [log] [blame]
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
5 */
6
Daniel Kurtzc68a2922014-11-03 10:53:27 +08007#include <linux/compiler.h>
8#include <linux/delay.h>
9#include <linux/device.h>
Shunqian Zheng4f0aba62016-06-24 10:13:29 +080010#include <linux/dma-iommu.h>
Joerg Roedel461a6942017-04-26 15:46:20 +020011#include <linux/dma-mapping.h>
Daniel Kurtzc68a2922014-11-03 10:53:27 +080012#include <linux/errno.h>
13#include <linux/interrupt.h>
14#include <linux/io.h>
15#include <linux/iommu.h>
Tomasz Figa0416bf62018-03-23 15:38:05 +080016#include <linux/iopoll.h>
Daniel Kurtzc68a2922014-11-03 10:53:27 +080017#include <linux/list.h>
18#include <linux/mm.h>
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_platform.h>
22#include <linux/platform_device.h>
23#include <linux/slab.h>
24#include <linux/spinlock.h>
25
26/** MMU register offsets */
27#define RK_MMU_DTE_ADDR 0x00 /* Directory table address */
28#define RK_MMU_STATUS 0x04
29#define RK_MMU_COMMAND 0x08
30#define RK_MMU_PAGE_FAULT_ADDR 0x0C /* IOVA of last page fault */
31#define RK_MMU_ZAP_ONE_LINE 0x10 /* Shootdown one IOTLB entry */
32#define RK_MMU_INT_RAWSTAT 0x14 /* IRQ status ignoring mask */
33#define RK_MMU_INT_CLEAR 0x18 /* Acknowledge and re-arm irq */
34#define RK_MMU_INT_MASK 0x1C /* IRQ enable */
35#define RK_MMU_INT_STATUS 0x20 /* IRQ status after masking */
36#define RK_MMU_AUTO_GATING 0x24
37
38#define DTE_ADDR_DUMMY 0xCAFEBABE
Tomasz Figa0416bf62018-03-23 15:38:05 +080039
40#define RK_MMU_POLL_PERIOD_US 100
41#define RK_MMU_FORCE_RESET_TIMEOUT_US 100000
42#define RK_MMU_POLL_TIMEOUT_US 1000
Daniel Kurtzc68a2922014-11-03 10:53:27 +080043
44/* RK_MMU_STATUS fields */
45#define RK_MMU_STATUS_PAGING_ENABLED BIT(0)
46#define RK_MMU_STATUS_PAGE_FAULT_ACTIVE BIT(1)
47#define RK_MMU_STATUS_STALL_ACTIVE BIT(2)
48#define RK_MMU_STATUS_IDLE BIT(3)
49#define RK_MMU_STATUS_REPLAY_BUFFER_EMPTY BIT(4)
50#define RK_MMU_STATUS_PAGE_FAULT_IS_WRITE BIT(5)
51#define RK_MMU_STATUS_STALL_NOT_ACTIVE BIT(31)
52
53/* RK_MMU_COMMAND command values */
54#define RK_MMU_CMD_ENABLE_PAGING 0 /* Enable memory translation */
55#define RK_MMU_CMD_DISABLE_PAGING 1 /* Disable memory translation */
56#define RK_MMU_CMD_ENABLE_STALL 2 /* Stall paging to allow other cmds */
57#define RK_MMU_CMD_DISABLE_STALL 3 /* Stop stall re-enables paging */
58#define RK_MMU_CMD_ZAP_CACHE 4 /* Shoot down entire IOTLB */
59#define RK_MMU_CMD_PAGE_FAULT_DONE 5 /* Clear page fault */
60#define RK_MMU_CMD_FORCE_RESET 6 /* Reset all registers */
61
62/* RK_MMU_INT_* register fields */
63#define RK_MMU_IRQ_PAGE_FAULT 0x01 /* page fault */
64#define RK_MMU_IRQ_BUS_ERROR 0x02 /* bus read error */
65#define RK_MMU_IRQ_MASK (RK_MMU_IRQ_PAGE_FAULT | RK_MMU_IRQ_BUS_ERROR)
66
67#define NUM_DT_ENTRIES 1024
68#define NUM_PT_ENTRIES 1024
69
70#define SPAGE_ORDER 12
71#define SPAGE_SIZE (1 << SPAGE_ORDER)
72
73 /*
74 * Support mapping any size that fits in one page table:
75 * 4 KiB to 4 MiB
76 */
77#define RK_IOMMU_PGSIZE_BITMAP 0x007ff000
78
Daniel Kurtzc68a2922014-11-03 10:53:27 +080079struct rk_iommu_domain {
80 struct list_head iommus;
Shunqian Zheng4f0aba62016-06-24 10:13:29 +080081 struct platform_device *pdev;
Daniel Kurtzc68a2922014-11-03 10:53:27 +080082 u32 *dt; /* page directory table */
Shunqian Zheng4f0aba62016-06-24 10:13:29 +080083 dma_addr_t dt_dma;
Daniel Kurtzc68a2922014-11-03 10:53:27 +080084 spinlock_t iommus_lock; /* lock for iommus list */
85 spinlock_t dt_lock; /* lock for modifying page directory table */
Joerg Roedelbcd516a2015-03-26 13:43:17 +010086
87 struct iommu_domain domain;
Daniel Kurtzc68a2922014-11-03 10:53:27 +080088};
89
90struct rk_iommu {
91 struct device *dev;
ZhengShunQiancd6438c2016-01-19 15:03:00 +080092 void __iomem **bases;
93 int num_mmu;
Simon Xuec3aa4742017-07-24 10:37:15 +080094 bool reset_disabled;
Joerg Roedelc9d9f232017-03-31 16:26:03 +020095 struct iommu_device iommu;
Daniel Kurtzc68a2922014-11-03 10:53:27 +080096 struct list_head node; /* entry in rk_iommu_domain.iommus */
97 struct iommu_domain *domain; /* domain to which iommu is attached */
98};
99
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800100static inline void rk_table_flush(struct rk_iommu_domain *dom, dma_addr_t dma,
101 unsigned int count)
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800102{
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800103 size_t size = count * sizeof(u32); /* count of u32 entry */
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800104
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800105 dma_sync_single_for_device(&dom->pdev->dev, dma, size, DMA_TO_DEVICE);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800106}
107
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100108static struct rk_iommu_domain *to_rk_domain(struct iommu_domain *dom)
109{
110 return container_of(dom, struct rk_iommu_domain, domain);
111}
112
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800113/*
114 * The Rockchip rk3288 iommu uses a 2-level page table.
115 * The first level is the "Directory Table" (DT).
116 * The DT consists of 1024 4-byte Directory Table Entries (DTEs), each pointing
117 * to a "Page Table".
118 * The second level is the 1024 Page Tables (PT).
119 * Each PT consists of 1024 4-byte Page Table Entries (PTEs), each pointing to
120 * a 4 KB page of physical memory.
121 *
122 * The DT and each PT fits in a single 4 KB page (4-bytes * 1024 entries).
123 * Each iommu device has a MMU_DTE_ADDR register that contains the physical
124 * address of the start of the DT page.
125 *
126 * The structure of the page table is as follows:
127 *
128 * DT
129 * MMU_DTE_ADDR -> +-----+
130 * | |
131 * +-----+ PT
132 * | DTE | -> +-----+
133 * +-----+ | | Memory
134 * | | +-----+ Page
135 * | | | PTE | -> +-----+
136 * +-----+ +-----+ | |
137 * | | | |
138 * | | | |
139 * +-----+ | |
140 * | |
141 * | |
142 * +-----+
143 */
144
145/*
146 * Each DTE has a PT address and a valid bit:
147 * +---------------------+-----------+-+
148 * | PT address | Reserved |V|
149 * +---------------------+-----------+-+
150 * 31:12 - PT address (PTs always starts on a 4 KB boundary)
151 * 11: 1 - Reserved
152 * 0 - 1 if PT @ PT address is valid
153 */
154#define RK_DTE_PT_ADDRESS_MASK 0xfffff000
155#define RK_DTE_PT_VALID BIT(0)
156
157static inline phys_addr_t rk_dte_pt_address(u32 dte)
158{
159 return (phys_addr_t)dte & RK_DTE_PT_ADDRESS_MASK;
160}
161
162static inline bool rk_dte_is_pt_valid(u32 dte)
163{
164 return dte & RK_DTE_PT_VALID;
165}
166
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800167static inline u32 rk_mk_dte(dma_addr_t pt_dma)
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800168{
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800169 return (pt_dma & RK_DTE_PT_ADDRESS_MASK) | RK_DTE_PT_VALID;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800170}
171
172/*
173 * Each PTE has a Page address, some flags and a valid bit:
174 * +---------------------+---+-------+-+
175 * | Page address |Rsv| Flags |V|
176 * +---------------------+---+-------+-+
177 * 31:12 - Page address (Pages always start on a 4 KB boundary)
178 * 11: 9 - Reserved
179 * 8: 1 - Flags
180 * 8 - Read allocate - allocate cache space on read misses
181 * 7 - Read cache - enable cache & prefetch of data
182 * 6 - Write buffer - enable delaying writes on their way to memory
183 * 5 - Write allocate - allocate cache space on write misses
184 * 4 - Write cache - different writes can be merged together
185 * 3 - Override cache attributes
186 * if 1, bits 4-8 control cache attributes
187 * if 0, the system bus defaults are used
188 * 2 - Writable
189 * 1 - Readable
190 * 0 - 1 if Page @ Page address is valid
191 */
192#define RK_PTE_PAGE_ADDRESS_MASK 0xfffff000
193#define RK_PTE_PAGE_FLAGS_MASK 0x000001fe
194#define RK_PTE_PAGE_WRITABLE BIT(2)
195#define RK_PTE_PAGE_READABLE BIT(1)
196#define RK_PTE_PAGE_VALID BIT(0)
197
198static inline phys_addr_t rk_pte_page_address(u32 pte)
199{
200 return (phys_addr_t)pte & RK_PTE_PAGE_ADDRESS_MASK;
201}
202
203static inline bool rk_pte_is_page_valid(u32 pte)
204{
205 return pte & RK_PTE_PAGE_VALID;
206}
207
208/* TODO: set cache flags per prot IOMMU_CACHE */
209static u32 rk_mk_pte(phys_addr_t page, int prot)
210{
211 u32 flags = 0;
212 flags |= (prot & IOMMU_READ) ? RK_PTE_PAGE_READABLE : 0;
213 flags |= (prot & IOMMU_WRITE) ? RK_PTE_PAGE_WRITABLE : 0;
214 page &= RK_PTE_PAGE_ADDRESS_MASK;
215 return page | flags | RK_PTE_PAGE_VALID;
216}
217
218static u32 rk_mk_pte_invalid(u32 pte)
219{
220 return pte & ~RK_PTE_PAGE_VALID;
221}
222
223/*
224 * rk3288 iova (IOMMU Virtual Address) format
225 * 31 22.21 12.11 0
226 * +-----------+-----------+-------------+
227 * | DTE index | PTE index | Page offset |
228 * +-----------+-----------+-------------+
229 * 31:22 - DTE index - index of DTE in DT
230 * 21:12 - PTE index - index of PTE in PT @ DTE.pt_address
231 * 11: 0 - Page offset - offset into page @ PTE.page_address
232 */
233#define RK_IOVA_DTE_MASK 0xffc00000
234#define RK_IOVA_DTE_SHIFT 22
235#define RK_IOVA_PTE_MASK 0x003ff000
236#define RK_IOVA_PTE_SHIFT 12
237#define RK_IOVA_PAGE_MASK 0x00000fff
238#define RK_IOVA_PAGE_SHIFT 0
239
240static u32 rk_iova_dte_index(dma_addr_t iova)
241{
242 return (u32)(iova & RK_IOVA_DTE_MASK) >> RK_IOVA_DTE_SHIFT;
243}
244
245static u32 rk_iova_pte_index(dma_addr_t iova)
246{
247 return (u32)(iova & RK_IOVA_PTE_MASK) >> RK_IOVA_PTE_SHIFT;
248}
249
250static u32 rk_iova_page_offset(dma_addr_t iova)
251{
252 return (u32)(iova & RK_IOVA_PAGE_MASK) >> RK_IOVA_PAGE_SHIFT;
253}
254
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800255static u32 rk_iommu_read(void __iomem *base, u32 offset)
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800256{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800257 return readl(base + offset);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800258}
259
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800260static void rk_iommu_write(void __iomem *base, u32 offset, u32 value)
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800261{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800262 writel(value, base + offset);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800263}
264
265static void rk_iommu_command(struct rk_iommu *iommu, u32 command)
266{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800267 int i;
268
269 for (i = 0; i < iommu->num_mmu; i++)
270 writel(command, iommu->bases[i] + RK_MMU_COMMAND);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800271}
272
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800273static void rk_iommu_base_command(void __iomem *base, u32 command)
274{
275 writel(command, base + RK_MMU_COMMAND);
276}
Tomasz Figabf2a5e72018-03-23 15:38:06 +0800277static void rk_iommu_zap_lines(struct rk_iommu *iommu, dma_addr_t iova_start,
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800278 size_t size)
279{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800280 int i;
Tomasz Figabf2a5e72018-03-23 15:38:06 +0800281 dma_addr_t iova_end = iova_start + size;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800282 /*
283 * TODO(djkurtz): Figure out when it is more efficient to shootdown the
284 * entire iotlb rather than iterate over individual iovas.
285 */
Tomasz Figabf2a5e72018-03-23 15:38:06 +0800286 for (i = 0; i < iommu->num_mmu; i++) {
287 dma_addr_t iova;
288
289 for (iova = iova_start; iova < iova_end; iova += SPAGE_SIZE)
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800290 rk_iommu_write(iommu->bases[i], RK_MMU_ZAP_ONE_LINE, iova);
Tomasz Figabf2a5e72018-03-23 15:38:06 +0800291 }
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800292}
293
294static bool rk_iommu_is_stall_active(struct rk_iommu *iommu)
295{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800296 bool active = true;
297 int i;
298
299 for (i = 0; i < iommu->num_mmu; i++)
John Keepingfbedd9b2016-04-05 15:05:46 +0100300 active &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) &
301 RK_MMU_STATUS_STALL_ACTIVE);
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800302
303 return active;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800304}
305
306static bool rk_iommu_is_paging_enabled(struct rk_iommu *iommu)
307{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800308 bool enable = true;
309 int i;
310
311 for (i = 0; i < iommu->num_mmu; i++)
John Keepingfbedd9b2016-04-05 15:05:46 +0100312 enable &= !!(rk_iommu_read(iommu->bases[i], RK_MMU_STATUS) &
313 RK_MMU_STATUS_PAGING_ENABLED);
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800314
315 return enable;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800316}
317
Tomasz Figa0416bf62018-03-23 15:38:05 +0800318static bool rk_iommu_is_reset_done(struct rk_iommu *iommu)
319{
320 bool done = true;
321 int i;
322
323 for (i = 0; i < iommu->num_mmu; i++)
324 done &= rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR) == 0;
325
326 return done;
327}
328
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800329static int rk_iommu_enable_stall(struct rk_iommu *iommu)
330{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800331 int ret, i;
Tomasz Figa0416bf62018-03-23 15:38:05 +0800332 bool val;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800333
334 if (rk_iommu_is_stall_active(iommu))
335 return 0;
336
337 /* Stall can only be enabled if paging is enabled */
338 if (!rk_iommu_is_paging_enabled(iommu))
339 return 0;
340
341 rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_STALL);
342
Tomasz Figa0416bf62018-03-23 15:38:05 +0800343 ret = readx_poll_timeout(rk_iommu_is_stall_active, iommu, val,
344 val, RK_MMU_POLL_PERIOD_US,
345 RK_MMU_POLL_TIMEOUT_US);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800346 if (ret)
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800347 for (i = 0; i < iommu->num_mmu; i++)
348 dev_err(iommu->dev, "Enable stall request timed out, status: %#08x\n",
349 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800350
351 return ret;
352}
353
354static int rk_iommu_disable_stall(struct rk_iommu *iommu)
355{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800356 int ret, i;
Tomasz Figa0416bf62018-03-23 15:38:05 +0800357 bool val;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800358
359 if (!rk_iommu_is_stall_active(iommu))
360 return 0;
361
362 rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_STALL);
363
Tomasz Figa0416bf62018-03-23 15:38:05 +0800364 ret = readx_poll_timeout(rk_iommu_is_stall_active, iommu, val,
365 !val, RK_MMU_POLL_PERIOD_US,
366 RK_MMU_POLL_TIMEOUT_US);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800367 if (ret)
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800368 for (i = 0; i < iommu->num_mmu; i++)
369 dev_err(iommu->dev, "Disable stall request timed out, status: %#08x\n",
370 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800371
372 return ret;
373}
374
375static int rk_iommu_enable_paging(struct rk_iommu *iommu)
376{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800377 int ret, i;
Tomasz Figa0416bf62018-03-23 15:38:05 +0800378 bool val;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800379
380 if (rk_iommu_is_paging_enabled(iommu))
381 return 0;
382
383 rk_iommu_command(iommu, RK_MMU_CMD_ENABLE_PAGING);
384
Tomasz Figa0416bf62018-03-23 15:38:05 +0800385 ret = readx_poll_timeout(rk_iommu_is_paging_enabled, iommu, val,
386 val, RK_MMU_POLL_PERIOD_US,
387 RK_MMU_POLL_TIMEOUT_US);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800388 if (ret)
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800389 for (i = 0; i < iommu->num_mmu; i++)
390 dev_err(iommu->dev, "Enable paging request timed out, status: %#08x\n",
391 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800392
393 return ret;
394}
395
396static int rk_iommu_disable_paging(struct rk_iommu *iommu)
397{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800398 int ret, i;
Tomasz Figa0416bf62018-03-23 15:38:05 +0800399 bool val;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800400
401 if (!rk_iommu_is_paging_enabled(iommu))
402 return 0;
403
404 rk_iommu_command(iommu, RK_MMU_CMD_DISABLE_PAGING);
405
Tomasz Figa0416bf62018-03-23 15:38:05 +0800406 ret = readx_poll_timeout(rk_iommu_is_paging_enabled, iommu, val,
407 !val, RK_MMU_POLL_PERIOD_US,
408 RK_MMU_POLL_TIMEOUT_US);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800409 if (ret)
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800410 for (i = 0; i < iommu->num_mmu; i++)
411 dev_err(iommu->dev, "Disable paging request timed out, status: %#08x\n",
412 rk_iommu_read(iommu->bases[i], RK_MMU_STATUS));
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800413
414 return ret;
415}
416
417static int rk_iommu_force_reset(struct rk_iommu *iommu)
418{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800419 int ret, i;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800420 u32 dte_addr;
Tomasz Figa0416bf62018-03-23 15:38:05 +0800421 bool val;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800422
Simon Xuec3aa4742017-07-24 10:37:15 +0800423 if (iommu->reset_disabled)
424 return 0;
425
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800426 /*
427 * Check if register DTE_ADDR is working by writing DTE_ADDR_DUMMY
428 * and verifying that upper 5 nybbles are read back.
429 */
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800430 for (i = 0; i < iommu->num_mmu; i++) {
431 rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, DTE_ADDR_DUMMY);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800432
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800433 dte_addr = rk_iommu_read(iommu->bases[i], RK_MMU_DTE_ADDR);
434 if (dte_addr != (DTE_ADDR_DUMMY & RK_DTE_PT_ADDRESS_MASK)) {
435 dev_err(iommu->dev, "Error during raw reset. MMU_DTE_ADDR is not functioning\n");
436 return -EFAULT;
437 }
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800438 }
439
440 rk_iommu_command(iommu, RK_MMU_CMD_FORCE_RESET);
441
Tomasz Figa0416bf62018-03-23 15:38:05 +0800442 ret = readx_poll_timeout(rk_iommu_is_reset_done, iommu, val,
443 val, RK_MMU_FORCE_RESET_TIMEOUT_US,
444 RK_MMU_POLL_TIMEOUT_US);
445 if (ret) {
446 dev_err(iommu->dev, "FORCE_RESET command timed out\n");
447 return ret;
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800448 }
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800449
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800450 return 0;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800451}
452
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800453static void log_iova(struct rk_iommu *iommu, int index, dma_addr_t iova)
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800454{
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800455 void __iomem *base = iommu->bases[index];
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800456 u32 dte_index, pte_index, page_offset;
457 u32 mmu_dte_addr;
458 phys_addr_t mmu_dte_addr_phys, dte_addr_phys;
459 u32 *dte_addr;
460 u32 dte;
461 phys_addr_t pte_addr_phys = 0;
462 u32 *pte_addr = NULL;
463 u32 pte = 0;
464 phys_addr_t page_addr_phys = 0;
465 u32 page_flags = 0;
466
467 dte_index = rk_iova_dte_index(iova);
468 pte_index = rk_iova_pte_index(iova);
469 page_offset = rk_iova_page_offset(iova);
470
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800471 mmu_dte_addr = rk_iommu_read(base, RK_MMU_DTE_ADDR);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800472 mmu_dte_addr_phys = (phys_addr_t)mmu_dte_addr;
473
474 dte_addr_phys = mmu_dte_addr_phys + (4 * dte_index);
475 dte_addr = phys_to_virt(dte_addr_phys);
476 dte = *dte_addr;
477
478 if (!rk_dte_is_pt_valid(dte))
479 goto print_it;
480
481 pte_addr_phys = rk_dte_pt_address(dte) + (pte_index * 4);
482 pte_addr = phys_to_virt(pte_addr_phys);
483 pte = *pte_addr;
484
485 if (!rk_pte_is_page_valid(pte))
486 goto print_it;
487
488 page_addr_phys = rk_pte_page_address(pte) + page_offset;
489 page_flags = pte & RK_PTE_PAGE_FLAGS_MASK;
490
491print_it:
492 dev_err(iommu->dev, "iova = %pad: dte_index: %#03x pte_index: %#03x page_offset: %#03x\n",
493 &iova, dte_index, pte_index, page_offset);
494 dev_err(iommu->dev, "mmu_dte_addr: %pa dte@%pa: %#08x valid: %u pte@%pa: %#08x valid: %u page@%pa flags: %#03x\n",
495 &mmu_dte_addr_phys, &dte_addr_phys, dte,
496 rk_dte_is_pt_valid(dte), &pte_addr_phys, pte,
497 rk_pte_is_page_valid(pte), &page_addr_phys, page_flags);
498}
499
500static irqreturn_t rk_iommu_irq(int irq, void *dev_id)
501{
502 struct rk_iommu *iommu = dev_id;
503 u32 status;
504 u32 int_status;
505 dma_addr_t iova;
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800506 irqreturn_t ret = IRQ_NONE;
507 int i;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800508
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800509 for (i = 0; i < iommu->num_mmu; i++) {
510 int_status = rk_iommu_read(iommu->bases[i], RK_MMU_INT_STATUS);
511 if (int_status == 0)
512 continue;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800513
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800514 ret = IRQ_HANDLED;
515 iova = rk_iommu_read(iommu->bases[i], RK_MMU_PAGE_FAULT_ADDR);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800516
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800517 if (int_status & RK_MMU_IRQ_PAGE_FAULT) {
518 int flags;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800519
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800520 status = rk_iommu_read(iommu->bases[i], RK_MMU_STATUS);
521 flags = (status & RK_MMU_STATUS_PAGE_FAULT_IS_WRITE) ?
522 IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800523
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800524 dev_err(iommu->dev, "Page fault at %pad of type %s\n",
525 &iova,
526 (flags == IOMMU_FAULT_WRITE) ? "write" : "read");
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800527
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800528 log_iova(iommu, i, iova);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800529
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800530 /*
531 * Report page fault to any installed handlers.
532 * Ignore the return code, though, since we always zap cache
533 * and clear the page fault anyway.
534 */
535 if (iommu->domain)
536 report_iommu_fault(iommu->domain, iommu->dev, iova,
537 flags);
538 else
539 dev_err(iommu->dev, "Page fault while iommu not attached to domain?\n");
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800540
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800541 rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
542 rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_PAGE_FAULT_DONE);
543 }
544
545 if (int_status & RK_MMU_IRQ_BUS_ERROR)
546 dev_err(iommu->dev, "BUS_ERROR occurred at %pad\n", &iova);
547
548 if (int_status & ~RK_MMU_IRQ_MASK)
549 dev_err(iommu->dev, "unexpected int_status: %#08x\n",
550 int_status);
551
552 rk_iommu_write(iommu->bases[i], RK_MMU_INT_CLEAR, int_status);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800553 }
554
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800555 return ret;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800556}
557
558static phys_addr_t rk_iommu_iova_to_phys(struct iommu_domain *domain,
559 dma_addr_t iova)
560{
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100561 struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800562 unsigned long flags;
563 phys_addr_t pt_phys, phys = 0;
564 u32 dte, pte;
565 u32 *page_table;
566
567 spin_lock_irqsave(&rk_domain->dt_lock, flags);
568
569 dte = rk_domain->dt[rk_iova_dte_index(iova)];
570 if (!rk_dte_is_pt_valid(dte))
571 goto out;
572
573 pt_phys = rk_dte_pt_address(dte);
574 page_table = (u32 *)phys_to_virt(pt_phys);
575 pte = page_table[rk_iova_pte_index(iova)];
576 if (!rk_pte_is_page_valid(pte))
577 goto out;
578
579 phys = rk_pte_page_address(pte) + rk_iova_page_offset(iova);
580out:
581 spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
582
583 return phys;
584}
585
586static void rk_iommu_zap_iova(struct rk_iommu_domain *rk_domain,
587 dma_addr_t iova, size_t size)
588{
589 struct list_head *pos;
590 unsigned long flags;
591
592 /* shootdown these iova from all iommus using this domain */
593 spin_lock_irqsave(&rk_domain->iommus_lock, flags);
594 list_for_each(pos, &rk_domain->iommus) {
595 struct rk_iommu *iommu;
596 iommu = list_entry(pos, struct rk_iommu, node);
597 rk_iommu_zap_lines(iommu, iova, size);
598 }
599 spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
600}
601
Tomasz Figad4dd9202015-04-20 20:43:44 +0900602static void rk_iommu_zap_iova_first_last(struct rk_iommu_domain *rk_domain,
603 dma_addr_t iova, size_t size)
604{
605 rk_iommu_zap_iova(rk_domain, iova, SPAGE_SIZE);
606 if (size > SPAGE_SIZE)
607 rk_iommu_zap_iova(rk_domain, iova + size - SPAGE_SIZE,
608 SPAGE_SIZE);
609}
610
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800611static u32 *rk_dte_get_page_table(struct rk_iommu_domain *rk_domain,
612 dma_addr_t iova)
613{
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800614 struct device *dev = &rk_domain->pdev->dev;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800615 u32 *page_table, *dte_addr;
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800616 u32 dte_index, dte;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800617 phys_addr_t pt_phys;
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800618 dma_addr_t pt_dma;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800619
620 assert_spin_locked(&rk_domain->dt_lock);
621
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800622 dte_index = rk_iova_dte_index(iova);
623 dte_addr = &rk_domain->dt[dte_index];
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800624 dte = *dte_addr;
625 if (rk_dte_is_pt_valid(dte))
626 goto done;
627
628 page_table = (u32 *)get_zeroed_page(GFP_ATOMIC | GFP_DMA32);
629 if (!page_table)
630 return ERR_PTR(-ENOMEM);
631
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800632 pt_dma = dma_map_single(dev, page_table, SPAGE_SIZE, DMA_TO_DEVICE);
633 if (dma_mapping_error(dev, pt_dma)) {
634 dev_err(dev, "DMA mapping error while allocating page table\n");
635 free_page((unsigned long)page_table);
636 return ERR_PTR(-ENOMEM);
637 }
638
639 dte = rk_mk_dte(pt_dma);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800640 *dte_addr = dte;
641
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800642 rk_table_flush(rk_domain, pt_dma, NUM_PT_ENTRIES);
643 rk_table_flush(rk_domain,
644 rk_domain->dt_dma + dte_index * sizeof(u32), 1);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800645done:
646 pt_phys = rk_dte_pt_address(dte);
647 return (u32 *)phys_to_virt(pt_phys);
648}
649
650static size_t rk_iommu_unmap_iova(struct rk_iommu_domain *rk_domain,
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800651 u32 *pte_addr, dma_addr_t pte_dma,
652 size_t size)
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800653{
654 unsigned int pte_count;
655 unsigned int pte_total = size / SPAGE_SIZE;
656
657 assert_spin_locked(&rk_domain->dt_lock);
658
659 for (pte_count = 0; pte_count < pte_total; pte_count++) {
660 u32 pte = pte_addr[pte_count];
661 if (!rk_pte_is_page_valid(pte))
662 break;
663
664 pte_addr[pte_count] = rk_mk_pte_invalid(pte);
665 }
666
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800667 rk_table_flush(rk_domain, pte_dma, pte_count);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800668
669 return pte_count * SPAGE_SIZE;
670}
671
672static int rk_iommu_map_iova(struct rk_iommu_domain *rk_domain, u32 *pte_addr,
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800673 dma_addr_t pte_dma, dma_addr_t iova,
674 phys_addr_t paddr, size_t size, int prot)
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800675{
676 unsigned int pte_count;
677 unsigned int pte_total = size / SPAGE_SIZE;
678 phys_addr_t page_phys;
679
680 assert_spin_locked(&rk_domain->dt_lock);
681
682 for (pte_count = 0; pte_count < pte_total; pte_count++) {
683 u32 pte = pte_addr[pte_count];
684
685 if (rk_pte_is_page_valid(pte))
686 goto unwind;
687
688 pte_addr[pte_count] = rk_mk_pte(paddr, prot);
689
690 paddr += SPAGE_SIZE;
691 }
692
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800693 rk_table_flush(rk_domain, pte_dma, pte_total);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800694
Tomasz Figad4dd9202015-04-20 20:43:44 +0900695 /*
696 * Zap the first and last iova to evict from iotlb any previously
697 * mapped cachelines holding stale values for its dte and pte.
698 * We only zap the first and last iova, since only they could have
699 * dte or pte shared with an existing mapping.
700 */
701 rk_iommu_zap_iova_first_last(rk_domain, iova, size);
702
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800703 return 0;
704unwind:
705 /* Unmap the range of iovas that we just mapped */
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800706 rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma,
707 pte_count * SPAGE_SIZE);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800708
709 iova += pte_count * SPAGE_SIZE;
710 page_phys = rk_pte_page_address(pte_addr[pte_count]);
711 pr_err("iova: %pad already mapped to %pa cannot remap to phys: %pa prot: %#x\n",
712 &iova, &page_phys, &paddr, prot);
713
714 return -EADDRINUSE;
715}
716
717static int rk_iommu_map(struct iommu_domain *domain, unsigned long _iova,
718 phys_addr_t paddr, size_t size, int prot)
719{
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100720 struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800721 unsigned long flags;
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800722 dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800723 u32 *page_table, *pte_addr;
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800724 u32 dte_index, pte_index;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800725 int ret;
726
727 spin_lock_irqsave(&rk_domain->dt_lock, flags);
728
729 /*
730 * pgsize_bitmap specifies iova sizes that fit in one page table
731 * (1024 4-KiB pages = 4 MiB).
732 * So, size will always be 4096 <= size <= 4194304.
733 * Since iommu_map() guarantees that both iova and size will be
734 * aligned, we will always only be mapping from a single dte here.
735 */
736 page_table = rk_dte_get_page_table(rk_domain, iova);
737 if (IS_ERR(page_table)) {
738 spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
739 return PTR_ERR(page_table);
740 }
741
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800742 dte_index = rk_domain->dt[rk_iova_dte_index(iova)];
743 pte_index = rk_iova_pte_index(iova);
744 pte_addr = &page_table[pte_index];
745 pte_dma = rk_dte_pt_address(dte_index) + pte_index * sizeof(u32);
746 ret = rk_iommu_map_iova(rk_domain, pte_addr, pte_dma, iova,
747 paddr, size, prot);
748
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800749 spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
750
751 return ret;
752}
753
754static size_t rk_iommu_unmap(struct iommu_domain *domain, unsigned long _iova,
755 size_t size)
756{
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100757 struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800758 unsigned long flags;
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800759 dma_addr_t pte_dma, iova = (dma_addr_t)_iova;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800760 phys_addr_t pt_phys;
761 u32 dte;
762 u32 *pte_addr;
763 size_t unmap_size;
764
765 spin_lock_irqsave(&rk_domain->dt_lock, flags);
766
767 /*
768 * pgsize_bitmap specifies iova sizes that fit in one page table
769 * (1024 4-KiB pages = 4 MiB).
770 * So, size will always be 4096 <= size <= 4194304.
771 * Since iommu_unmap() guarantees that both iova and size will be
772 * aligned, we will always only be unmapping from a single dte here.
773 */
774 dte = rk_domain->dt[rk_iova_dte_index(iova)];
775 /* Just return 0 if iova is unmapped */
776 if (!rk_dte_is_pt_valid(dte)) {
777 spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
778 return 0;
779 }
780
781 pt_phys = rk_dte_pt_address(dte);
782 pte_addr = (u32 *)phys_to_virt(pt_phys) + rk_iova_pte_index(iova);
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800783 pte_dma = pt_phys + rk_iova_pte_index(iova) * sizeof(u32);
784 unmap_size = rk_iommu_unmap_iova(rk_domain, pte_addr, pte_dma, size);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800785
786 spin_unlock_irqrestore(&rk_domain->dt_lock, flags);
787
788 /* Shootdown iotlb entries for iova range that was just unmapped */
789 rk_iommu_zap_iova(rk_domain, iova, unmap_size);
790
791 return unmap_size;
792}
793
794static struct rk_iommu *rk_iommu_from_dev(struct device *dev)
795{
796 struct iommu_group *group;
797 struct device *iommu_dev;
798 struct rk_iommu *rk_iommu;
799
800 group = iommu_group_get(dev);
801 if (!group)
802 return NULL;
803 iommu_dev = iommu_group_get_iommudata(group);
804 rk_iommu = dev_get_drvdata(iommu_dev);
805 iommu_group_put(group);
806
807 return rk_iommu;
808}
809
810static int rk_iommu_attach_device(struct iommu_domain *domain,
811 struct device *dev)
812{
813 struct rk_iommu *iommu;
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100814 struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800815 unsigned long flags;
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800816 int ret, i;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800817
818 /*
819 * Allow 'virtual devices' (e.g., drm) to attach to domain.
820 * Such a device does not belong to an iommu group.
821 */
822 iommu = rk_iommu_from_dev(dev);
823 if (!iommu)
824 return 0;
825
826 ret = rk_iommu_enable_stall(iommu);
827 if (ret)
828 return ret;
829
830 ret = rk_iommu_force_reset(iommu);
831 if (ret)
Tomasz Figaf6717d72018-03-23 15:38:04 +0800832 goto out_disable_stall;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800833
834 iommu->domain = domain;
835
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800836 for (i = 0; i < iommu->num_mmu; i++) {
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800837 rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR,
838 rk_domain->dt_dma);
John Keepingae8a7912016-06-01 16:46:10 +0100839 rk_iommu_base_command(iommu->bases[i], RK_MMU_CMD_ZAP_CACHE);
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800840 rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, RK_MMU_IRQ_MASK);
841 }
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800842
843 ret = rk_iommu_enable_paging(iommu);
844 if (ret)
Tomasz Figaf6717d72018-03-23 15:38:04 +0800845 goto out_disable_stall;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800846
847 spin_lock_irqsave(&rk_domain->iommus_lock, flags);
848 list_add_tail(&iommu->node, &rk_domain->iommus);
849 spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
850
Heiko Stuebnerec4292d2015-05-21 09:57:29 +0200851 dev_dbg(dev, "Attached to iommu domain\n");
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800852
Tomasz Figaf6717d72018-03-23 15:38:04 +0800853out_disable_stall:
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800854 rk_iommu_disable_stall(iommu);
Tomasz Figaf6717d72018-03-23 15:38:04 +0800855 return ret;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800856}
857
858static void rk_iommu_detach_device(struct iommu_domain *domain,
859 struct device *dev)
860{
861 struct rk_iommu *iommu;
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100862 struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800863 unsigned long flags;
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800864 int i;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800865
866 /* Allow 'virtual devices' (eg drm) to detach from domain */
867 iommu = rk_iommu_from_dev(dev);
868 if (!iommu)
869 return;
870
871 spin_lock_irqsave(&rk_domain->iommus_lock, flags);
872 list_del_init(&iommu->node);
873 spin_unlock_irqrestore(&rk_domain->iommus_lock, flags);
874
875 /* Ignore error while disabling, just keep going */
876 rk_iommu_enable_stall(iommu);
877 rk_iommu_disable_paging(iommu);
ZhengShunQiancd6438c2016-01-19 15:03:00 +0800878 for (i = 0; i < iommu->num_mmu; i++) {
879 rk_iommu_write(iommu->bases[i], RK_MMU_INT_MASK, 0);
880 rk_iommu_write(iommu->bases[i], RK_MMU_DTE_ADDR, 0);
881 }
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800882 rk_iommu_disable_stall(iommu);
883
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800884 iommu->domain = NULL;
885
Heiko Stuebnerec4292d2015-05-21 09:57:29 +0200886 dev_dbg(dev, "Detached from iommu domain\n");
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800887}
888
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100889static struct iommu_domain *rk_iommu_domain_alloc(unsigned type)
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800890{
891 struct rk_iommu_domain *rk_domain;
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800892 struct platform_device *pdev;
893 struct device *iommu_dev;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800894
Shunqian Zhenga93db2f2016-06-24 10:13:30 +0800895 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_DMA)
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100896 return NULL;
897
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800898 /* Register a pdev per domain, so DMA API can base on this *dev
899 * even some virtual master doesn't have an iommu slave
900 */
901 pdev = platform_device_register_simple("rk_iommu_domain",
902 PLATFORM_DEVID_AUTO, NULL, 0);
903 if (IS_ERR(pdev))
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100904 return NULL;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800905
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800906 rk_domain = devm_kzalloc(&pdev->dev, sizeof(*rk_domain), GFP_KERNEL);
907 if (!rk_domain)
908 goto err_unreg_pdev;
909
910 rk_domain->pdev = pdev;
911
Shunqian Zhenga93db2f2016-06-24 10:13:30 +0800912 if (type == IOMMU_DOMAIN_DMA &&
913 iommu_get_dma_cookie(&rk_domain->domain))
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800914 goto err_unreg_pdev;
915
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800916 /*
917 * rk32xx iommus use a 2 level pagetable.
918 * Each level1 (dt) and level2 (pt) table has 1024 4-byte entries.
919 * Allocate one 4 KiB page for each table.
920 */
921 rk_domain->dt = (u32 *)get_zeroed_page(GFP_KERNEL | GFP_DMA32);
922 if (!rk_domain->dt)
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800923 goto err_put_cookie;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800924
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800925 iommu_dev = &pdev->dev;
926 rk_domain->dt_dma = dma_map_single(iommu_dev, rk_domain->dt,
927 SPAGE_SIZE, DMA_TO_DEVICE);
928 if (dma_mapping_error(iommu_dev, rk_domain->dt_dma)) {
929 dev_err(iommu_dev, "DMA map error for DT\n");
930 goto err_free_dt;
931 }
932
933 rk_table_flush(rk_domain, rk_domain->dt_dma, NUM_DT_ENTRIES);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800934
935 spin_lock_init(&rk_domain->iommus_lock);
936 spin_lock_init(&rk_domain->dt_lock);
937 INIT_LIST_HEAD(&rk_domain->iommus);
938
Shunqian Zhenga93db2f2016-06-24 10:13:30 +0800939 rk_domain->domain.geometry.aperture_start = 0;
940 rk_domain->domain.geometry.aperture_end = DMA_BIT_MASK(32);
941 rk_domain->domain.geometry.force_aperture = true;
942
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100943 return &rk_domain->domain;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800944
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800945err_free_dt:
946 free_page((unsigned long)rk_domain->dt);
947err_put_cookie:
Shunqian Zhenga93db2f2016-06-24 10:13:30 +0800948 if (type == IOMMU_DOMAIN_DMA)
949 iommu_put_dma_cookie(&rk_domain->domain);
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800950err_unreg_pdev:
951 platform_device_unregister(pdev);
952
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100953 return NULL;
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800954}
955
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100956static void rk_iommu_domain_free(struct iommu_domain *domain)
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800957{
Joerg Roedelbcd516a2015-03-26 13:43:17 +0100958 struct rk_iommu_domain *rk_domain = to_rk_domain(domain);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800959 int i;
960
961 WARN_ON(!list_empty(&rk_domain->iommus));
962
963 for (i = 0; i < NUM_DT_ENTRIES; i++) {
964 u32 dte = rk_domain->dt[i];
965 if (rk_dte_is_pt_valid(dte)) {
966 phys_addr_t pt_phys = rk_dte_pt_address(dte);
967 u32 *page_table = phys_to_virt(pt_phys);
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800968 dma_unmap_single(&rk_domain->pdev->dev, pt_phys,
969 SPAGE_SIZE, DMA_TO_DEVICE);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800970 free_page((unsigned long)page_table);
971 }
972 }
973
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800974 dma_unmap_single(&rk_domain->pdev->dev, rk_domain->dt_dma,
975 SPAGE_SIZE, DMA_TO_DEVICE);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800976 free_page((unsigned long)rk_domain->dt);
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800977
Shunqian Zhenga93db2f2016-06-24 10:13:30 +0800978 if (domain->type == IOMMU_DOMAIN_DMA)
979 iommu_put_dma_cookie(&rk_domain->domain);
Shunqian Zheng4f0aba62016-06-24 10:13:29 +0800980
981 platform_device_unregister(rk_domain->pdev);
Daniel Kurtzc68a2922014-11-03 10:53:27 +0800982}
983
984static bool rk_iommu_is_dev_iommu_master(struct device *dev)
985{
986 struct device_node *np = dev->of_node;
987 int ret;
988
989 /*
990 * An iommu master has an iommus property containing a list of phandles
991 * to iommu nodes, each with an #iommu-cells property with value 0.
992 */
993 ret = of_count_phandle_with_args(np, "iommus", "#iommu-cells");
994 return (ret > 0);
995}
996
997static int rk_iommu_group_set_iommudata(struct iommu_group *group,
998 struct device *dev)
999{
1000 struct device_node *np = dev->of_node;
1001 struct platform_device *pd;
1002 int ret;
1003 struct of_phandle_args args;
1004
1005 /*
1006 * An iommu master has an iommus property containing a list of phandles
1007 * to iommu nodes, each with an #iommu-cells property with value 0.
1008 */
1009 ret = of_parse_phandle_with_args(np, "iommus", "#iommu-cells", 0,
1010 &args);
1011 if (ret) {
Rob Herring6bd4f1c2017-07-18 16:43:09 -05001012 dev_err(dev, "of_parse_phandle_with_args(%pOF) => %d\n",
1013 np, ret);
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001014 return ret;
1015 }
1016 if (args.args_count != 0) {
Rob Herring6bd4f1c2017-07-18 16:43:09 -05001017 dev_err(dev, "incorrect number of iommu params found for %pOF (found %d, expected 0)\n",
1018 args.np, args.args_count);
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001019 return -EINVAL;
1020 }
1021
1022 pd = of_find_device_by_node(args.np);
1023 of_node_put(args.np);
1024 if (!pd) {
Rob Herring6bd4f1c2017-07-18 16:43:09 -05001025 dev_err(dev, "iommu %pOF not found\n", args.np);
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001026 return -EPROBE_DEFER;
1027 }
1028
1029 /* TODO(djkurtz): handle multiple slave iommus for a single master */
1030 iommu_group_set_iommudata(group, &pd->dev, NULL);
1031
1032 return 0;
1033}
1034
1035static int rk_iommu_add_device(struct device *dev)
1036{
1037 struct iommu_group *group;
Joerg Roedelc9d9f232017-03-31 16:26:03 +02001038 struct rk_iommu *iommu;
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001039 int ret;
1040
1041 if (!rk_iommu_is_dev_iommu_master(dev))
1042 return -ENODEV;
1043
1044 group = iommu_group_get(dev);
1045 if (!group) {
1046 group = iommu_group_alloc();
1047 if (IS_ERR(group)) {
1048 dev_err(dev, "Failed to allocate IOMMU group\n");
1049 return PTR_ERR(group);
1050 }
1051 }
1052
1053 ret = iommu_group_add_device(group, dev);
1054 if (ret)
1055 goto err_put_group;
1056
1057 ret = rk_iommu_group_set_iommudata(group, dev);
1058 if (ret)
1059 goto err_remove_device;
1060
Joerg Roedelc9d9f232017-03-31 16:26:03 +02001061 iommu = rk_iommu_from_dev(dev);
1062 if (iommu)
1063 iommu_device_link(&iommu->iommu, dev);
1064
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001065 iommu_group_put(group);
1066
1067 return 0;
1068
1069err_remove_device:
1070 iommu_group_remove_device(dev);
1071err_put_group:
1072 iommu_group_put(group);
1073 return ret;
1074}
1075
1076static void rk_iommu_remove_device(struct device *dev)
1077{
Joerg Roedelc9d9f232017-03-31 16:26:03 +02001078 struct rk_iommu *iommu;
1079
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001080 if (!rk_iommu_is_dev_iommu_master(dev))
1081 return;
1082
Joerg Roedelc9d9f232017-03-31 16:26:03 +02001083 iommu = rk_iommu_from_dev(dev);
1084 if (iommu)
1085 iommu_device_unlink(&iommu->iommu, dev);
1086
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001087 iommu_group_remove_device(dev);
1088}
1089
1090static const struct iommu_ops rk_iommu_ops = {
Joerg Roedelbcd516a2015-03-26 13:43:17 +01001091 .domain_alloc = rk_iommu_domain_alloc,
1092 .domain_free = rk_iommu_domain_free,
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001093 .attach_dev = rk_iommu_attach_device,
1094 .detach_dev = rk_iommu_detach_device,
1095 .map = rk_iommu_map,
1096 .unmap = rk_iommu_unmap,
Simon Xuee6d0f472016-06-24 10:13:27 +08001097 .map_sg = default_iommu_map_sg,
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001098 .add_device = rk_iommu_add_device,
1099 .remove_device = rk_iommu_remove_device,
1100 .iova_to_phys = rk_iommu_iova_to_phys,
1101 .pgsize_bitmap = RK_IOMMU_PGSIZE_BITMAP,
1102};
1103
Shunqian Zheng4f0aba62016-06-24 10:13:29 +08001104static int rk_iommu_domain_probe(struct platform_device *pdev)
1105{
1106 struct device *dev = &pdev->dev;
1107
1108 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms), GFP_KERNEL);
1109 if (!dev->dma_parms)
1110 return -ENOMEM;
1111
1112 /* Set dma_ops for dev, otherwise it would be dummy_dma_ops */
1113 arch_setup_dma_ops(dev, 0, DMA_BIT_MASK(32), NULL, false);
1114
1115 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
1116 dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
1117
1118 return 0;
1119}
1120
1121static struct platform_driver rk_iommu_domain_driver = {
1122 .probe = rk_iommu_domain_probe,
1123 .driver = {
1124 .name = "rk_iommu_domain",
1125 },
1126};
1127
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001128static int rk_iommu_probe(struct platform_device *pdev)
1129{
1130 struct device *dev = &pdev->dev;
1131 struct rk_iommu *iommu;
1132 struct resource *res;
Shunqian Zheng3d08f4342016-06-24 10:13:28 +08001133 int num_res = pdev->num_resources;
Jeffy Chend0b912b2018-03-23 15:38:03 +08001134 int err, i, irq;
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001135
1136 iommu = devm_kzalloc(dev, sizeof(*iommu), GFP_KERNEL);
1137 if (!iommu)
1138 return -ENOMEM;
1139
1140 platform_set_drvdata(pdev, iommu);
1141 iommu->dev = dev;
ZhengShunQiancd6438c2016-01-19 15:03:00 +08001142 iommu->num_mmu = 0;
Shunqian Zheng3d08f4342016-06-24 10:13:28 +08001143
1144 iommu->bases = devm_kzalloc(dev, sizeof(*iommu->bases) * num_res,
ZhengShunQiancd6438c2016-01-19 15:03:00 +08001145 GFP_KERNEL);
1146 if (!iommu->bases)
1147 return -ENOMEM;
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001148
Shunqian Zheng3d08f4342016-06-24 10:13:28 +08001149 for (i = 0; i < num_res; i++) {
ZhengShunQiancd6438c2016-01-19 15:03:00 +08001150 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
Tomeu Vizoso8d7f2d82016-03-21 12:00:23 +01001151 if (!res)
1152 continue;
ZhengShunQiancd6438c2016-01-19 15:03:00 +08001153 iommu->bases[i] = devm_ioremap_resource(&pdev->dev, res);
1154 if (IS_ERR(iommu->bases[i]))
1155 continue;
1156 iommu->num_mmu++;
1157 }
1158 if (iommu->num_mmu == 0)
1159 return PTR_ERR(iommu->bases[0]);
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001160
Jeffy Chend0b912b2018-03-23 15:38:03 +08001161 i = 0;
1162 while ((irq = platform_get_irq(pdev, i++)) != -ENXIO) {
1163 if (irq < 0)
1164 return irq;
Simon Xue03f732f2017-07-24 10:37:14 +08001165
Jeffy Chend0b912b2018-03-23 15:38:03 +08001166 err = devm_request_irq(iommu->dev, irq, rk_iommu_irq,
1167 IRQF_SHARED, dev_name(dev), iommu);
1168 if (err)
1169 return err;
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001170 }
1171
Simon Xuec3aa4742017-07-24 10:37:15 +08001172 iommu->reset_disabled = device_property_read_bool(dev,
1173 "rockchip,disable-mmu-reset");
1174
Joerg Roedelc9d9f232017-03-31 16:26:03 +02001175 err = iommu_device_sysfs_add(&iommu->iommu, dev, NULL, dev_name(dev));
1176 if (err)
1177 return err;
1178
1179 iommu_device_set_ops(&iommu->iommu, &rk_iommu_ops);
1180 err = iommu_device_register(&iommu->iommu);
Jeffy Chen6d9ffaa2018-03-23 15:38:02 +08001181 if (err)
1182 iommu_device_sysfs_remove(&iommu->iommu);
Joerg Roedelc9d9f232017-03-31 16:26:03 +02001183
1184 return err;
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001185}
1186
Marc Zyngier1a4e90f2018-02-20 20:25:04 +00001187static void rk_iommu_shutdown(struct platform_device *pdev)
1188{
1189 struct rk_iommu *iommu = platform_get_drvdata(pdev);
1190
1191 /*
1192 * Be careful not to try to shutdown an otherwise unused
1193 * IOMMU, as it is likely not to be clocked, and accessing it
1194 * would just block. An IOMMU without a domain is likely to be
1195 * unused, so let's use this as a (weak) guard.
1196 */
1197 if (iommu && iommu->domain) {
1198 rk_iommu_enable_stall(iommu);
1199 rk_iommu_disable_paging(iommu);
1200 rk_iommu_force_reset(iommu);
1201 }
1202}
1203
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001204static const struct of_device_id rk_iommu_dt_ids[] = {
1205 { .compatible = "rockchip,iommu" },
1206 { /* sentinel */ }
1207};
1208MODULE_DEVICE_TABLE(of, rk_iommu_dt_ids);
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001209
1210static struct platform_driver rk_iommu_driver = {
1211 .probe = rk_iommu_probe,
Marc Zyngier1a4e90f2018-02-20 20:25:04 +00001212 .shutdown = rk_iommu_shutdown,
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001213 .driver = {
1214 .name = "rk_iommu",
Arnd Bergmannd9e7eb12015-04-10 23:58:24 +02001215 .of_match_table = rk_iommu_dt_ids,
Jeffy Chen98b72b92018-03-23 15:38:01 +08001216 .suppress_bind_attrs = true,
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001217 },
1218};
1219
1220static int __init rk_iommu_init(void)
1221{
Thierry Reding425061b2015-02-06 11:44:07 +01001222 struct device_node *np;
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001223 int ret;
1224
Thierry Reding425061b2015-02-06 11:44:07 +01001225 np = of_find_matching_node(NULL, rk_iommu_dt_ids);
1226 if (!np)
1227 return 0;
1228
1229 of_node_put(np);
1230
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001231 ret = bus_set_iommu(&platform_bus_type, &rk_iommu_ops);
1232 if (ret)
1233 return ret;
1234
Shunqian Zheng4f0aba62016-06-24 10:13:29 +08001235 ret = platform_driver_register(&rk_iommu_domain_driver);
1236 if (ret)
1237 return ret;
1238
1239 ret = platform_driver_register(&rk_iommu_driver);
1240 if (ret)
1241 platform_driver_unregister(&rk_iommu_domain_driver);
1242 return ret;
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001243}
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001244subsys_initcall(rk_iommu_init);
Daniel Kurtzc68a2922014-11-03 10:53:27 +08001245
1246MODULE_DESCRIPTION("IOMMU API for Rockchip");
1247MODULE_AUTHOR("Simon Xue <xxm@rock-chips.com> and Daniel Kurtz <djkurtz@chromium.org>");
1248MODULE_ALIAS("platform:rockchip-iommu");
1249MODULE_LICENSE("GPL v2");