1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
5 */
6
7 #include <linux/adreno-smmu-priv.h>
8 #include <linux/io-pgtable.h>
9 #include "msm_drv.h"
10 #include "msm_mmu.h"
11
12 struct msm_iommu {
13 struct msm_mmu base;
14 struct iommu_domain *domain;
15 atomic_t pagetables;
16 };
17
18 #define to_msm_iommu(x) container_of(x, struct msm_iommu, base)
19
20 struct msm_iommu_pagetable {
21 struct msm_mmu base;
22 struct msm_mmu *parent;
23 struct io_pgtable_ops *pgtbl_ops;
24 const struct iommu_flush_ops *tlb;
25 struct device *iommu_dev;
26 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */
27 phys_addr_t ttbr;
28 u32 asid;
29 };
to_pagetable(struct msm_mmu * mmu)30 static struct msm_iommu_pagetable *to_pagetable(struct msm_mmu *mmu)
31 {
32 return container_of(mmu, struct msm_iommu_pagetable, base);
33 }
34
35 /* based on iommu_pgsize() in iommu.c: */
calc_pgsize(struct msm_iommu_pagetable * pagetable,unsigned long iova,phys_addr_t paddr,size_t size,size_t * count)36 static size_t calc_pgsize(struct msm_iommu_pagetable *pagetable,
37 unsigned long iova, phys_addr_t paddr,
38 size_t size, size_t *count)
39 {
40 unsigned int pgsize_idx, pgsize_idx_next;
41 unsigned long pgsizes;
42 size_t offset, pgsize, pgsize_next;
43 unsigned long addr_merge = paddr | iova;
44
45 /* Page sizes supported by the hardware and small enough for @size */
46 pgsizes = pagetable->pgsize_bitmap & GENMASK(__fls(size), 0);
47
48 /* Constrain the page sizes further based on the maximum alignment */
49 if (likely(addr_merge))
50 pgsizes &= GENMASK(__ffs(addr_merge), 0);
51
52 /* Make sure we have at least one suitable page size */
53 BUG_ON(!pgsizes);
54
55 /* Pick the biggest page size remaining */
56 pgsize_idx = __fls(pgsizes);
57 pgsize = BIT(pgsize_idx);
58 if (!count)
59 return pgsize;
60
61 /* Find the next biggest support page size, if it exists */
62 pgsizes = pagetable->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
63 if (!pgsizes)
64 goto out_set_count;
65
66 pgsize_idx_next = __ffs(pgsizes);
67 pgsize_next = BIT(pgsize_idx_next);
68
69 /*
70 * There's no point trying a bigger page size unless the virtual
71 * and physical addresses are similarly offset within the larger page.
72 */
73 if ((iova ^ paddr) & (pgsize_next - 1))
74 goto out_set_count;
75
76 /* Calculate the offset to the next page size alignment boundary */
77 offset = pgsize_next - (addr_merge & (pgsize_next - 1));
78
79 /*
80 * If size is big enough to accommodate the larger page, reduce
81 * the number of smaller pages.
82 */
83 if (offset + pgsize_next <= size)
84 size = offset;
85
86 out_set_count:
87 *count = size >> pgsize_idx;
88 return pgsize;
89 }
90
msm_iommu_pagetable_unmap(struct msm_mmu * mmu,u64 iova,size_t size)91 static int msm_iommu_pagetable_unmap(struct msm_mmu *mmu, u64 iova,
92 size_t size)
93 {
94 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
95 struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
96
97 while (size) {
98 size_t unmapped, pgsize, count;
99
100 pgsize = calc_pgsize(pagetable, iova, iova, size, &count);
101
102 unmapped = ops->unmap_pages(ops, iova, pgsize, count, NULL);
103 if (!unmapped)
104 break;
105
106 iova += unmapped;
107 size -= unmapped;
108 }
109
110 iommu_flush_iotlb_all(to_msm_iommu(pagetable->parent)->domain);
111
112 return (size == 0) ? 0 : -EINVAL;
113 }
114
msm_iommu_pagetable_map(struct msm_mmu * mmu,u64 iova,struct sg_table * sgt,size_t len,int prot)115 static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova,
116 struct sg_table *sgt, size_t len, int prot)
117 {
118 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
119 struct io_pgtable_ops *ops = pagetable->pgtbl_ops;
120 struct scatterlist *sg;
121 u64 addr = iova;
122 unsigned int i;
123
124 for_each_sgtable_sg(sgt, sg, i) {
125 size_t size = sg->length;
126 phys_addr_t phys = sg_phys(sg);
127
128 while (size) {
129 size_t pgsize, count, mapped = 0;
130 int ret;
131
132 pgsize = calc_pgsize(pagetable, addr, phys, size, &count);
133
134 ret = ops->map_pages(ops, addr, phys, pgsize, count,
135 prot, GFP_KERNEL, &mapped);
136
137 /* map_pages could fail after mapping some of the pages,
138 * so update the counters before error handling.
139 */
140 phys += mapped;
141 addr += mapped;
142 size -= mapped;
143
144 if (ret) {
145 msm_iommu_pagetable_unmap(mmu, iova, addr - iova);
146 return -EINVAL;
147 }
148 }
149 }
150
151 return 0;
152 }
153
msm_iommu_pagetable_destroy(struct msm_mmu * mmu)154 static void msm_iommu_pagetable_destroy(struct msm_mmu *mmu)
155 {
156 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu);
157 struct msm_iommu *iommu = to_msm_iommu(pagetable->parent);
158 struct adreno_smmu_priv *adreno_smmu =
159 dev_get_drvdata(pagetable->parent->dev);
160
161 /*
162 * If this is the last attached pagetable for the parent,
163 * disable TTBR0 in the arm-smmu driver
164 */
165 if (atomic_dec_return(&iommu->pagetables) == 0)
166 adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, NULL);
167
168 free_io_pgtable_ops(pagetable->pgtbl_ops);
169 kfree(pagetable);
170 }
171
msm_iommu_pagetable_params(struct msm_mmu * mmu,phys_addr_t * ttbr,int * asid)172 int msm_iommu_pagetable_params(struct msm_mmu *mmu,
173 phys_addr_t *ttbr, int *asid)
174 {
175 struct msm_iommu_pagetable *pagetable;
176
177 if (mmu->type != MSM_MMU_IOMMU_PAGETABLE)
178 return -EINVAL;
179
180 pagetable = to_pagetable(mmu);
181
182 if (ttbr)
183 *ttbr = pagetable->ttbr;
184
185 if (asid)
186 *asid = pagetable->asid;
187
188 return 0;
189 }
190
191 static const struct msm_mmu_funcs pagetable_funcs = {
192 .map = msm_iommu_pagetable_map,
193 .unmap = msm_iommu_pagetable_unmap,
194 .destroy = msm_iommu_pagetable_destroy,
195 };
196
msm_iommu_tlb_flush_all(void * cookie)197 static void msm_iommu_tlb_flush_all(void *cookie)
198 {
199 struct msm_iommu_pagetable *pagetable = cookie;
200 struct adreno_smmu_priv *adreno_smmu;
201
202 if (!pm_runtime_get_if_in_use(pagetable->iommu_dev))
203 return;
204
205 adreno_smmu = dev_get_drvdata(pagetable->parent->dev);
206
207 pagetable->tlb->tlb_flush_all((void *)adreno_smmu->cookie);
208
209 pm_runtime_put_autosuspend(pagetable->iommu_dev);
210 }
211
msm_iommu_tlb_flush_walk(unsigned long iova,size_t size,size_t granule,void * cookie)212 static void msm_iommu_tlb_flush_walk(unsigned long iova, size_t size,
213 size_t granule, void *cookie)
214 {
215 struct msm_iommu_pagetable *pagetable = cookie;
216 struct adreno_smmu_priv *adreno_smmu;
217
218 if (!pm_runtime_get_if_in_use(pagetable->iommu_dev))
219 return;
220
221 adreno_smmu = dev_get_drvdata(pagetable->parent->dev);
222
223 pagetable->tlb->tlb_flush_walk(iova, size, granule, (void *)adreno_smmu->cookie);
224
225 pm_runtime_put_autosuspend(pagetable->iommu_dev);
226 }
227
msm_iommu_tlb_add_page(struct iommu_iotlb_gather * gather,unsigned long iova,size_t granule,void * cookie)228 static void msm_iommu_tlb_add_page(struct iommu_iotlb_gather *gather,
229 unsigned long iova, size_t granule, void *cookie)
230 {
231 }
232
233 static const struct iommu_flush_ops tlb_ops = {
234 .tlb_flush_all = msm_iommu_tlb_flush_all,
235 .tlb_flush_walk = msm_iommu_tlb_flush_walk,
236 .tlb_add_page = msm_iommu_tlb_add_page,
237 };
238
239 static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
240 unsigned long iova, int flags, void *arg);
241
msm_iommu_pagetable_create(struct msm_mmu * parent)242 struct msm_mmu *msm_iommu_pagetable_create(struct msm_mmu *parent)
243 {
244 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(parent->dev);
245 struct msm_iommu *iommu = to_msm_iommu(parent);
246 struct msm_iommu_pagetable *pagetable;
247 const struct io_pgtable_cfg *ttbr1_cfg = NULL;
248 struct io_pgtable_cfg ttbr0_cfg;
249 int ret;
250
251 /* Get the pagetable configuration from the domain */
252 if (adreno_smmu->cookie)
253 ttbr1_cfg = adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie);
254
255 /*
256 * If you hit this WARN_ONCE() you are probably missing an entry in
257 * qcom_smmu_impl_of_match[] in arm-smmu-qcom.c
258 */
259 if (WARN_ONCE(!ttbr1_cfg, "No per-process page tables"))
260 return ERR_PTR(-ENODEV);
261
262 /*
263 * Defer setting the fault handler until we have a valid adreno_smmu
264 * to avoid accidentially installing a GPU specific fault handler for
265 * the display's iommu
266 */
267 iommu_set_fault_handler(iommu->domain, msm_fault_handler, iommu);
268
269 pagetable = kzalloc(sizeof(*pagetable), GFP_KERNEL);
270 if (!pagetable)
271 return ERR_PTR(-ENOMEM);
272
273 msm_mmu_init(&pagetable->base, parent->dev, &pagetable_funcs,
274 MSM_MMU_IOMMU_PAGETABLE);
275
276 /* Clone the TTBR1 cfg as starting point for TTBR0 cfg: */
277 ttbr0_cfg = *ttbr1_cfg;
278
279 /* The incoming cfg will have the TTBR1 quirk enabled */
280 ttbr0_cfg.quirks &= ~IO_PGTABLE_QUIRK_ARM_TTBR1;
281 ttbr0_cfg.tlb = &tlb_ops;
282
283 pagetable->pgtbl_ops = alloc_io_pgtable_ops(ARM_64_LPAE_S1,
284 &ttbr0_cfg, pagetable);
285
286 if (!pagetable->pgtbl_ops) {
287 kfree(pagetable);
288 return ERR_PTR(-ENOMEM);
289 }
290
291 /*
292 * If this is the first pagetable that we've allocated, send it back to
293 * the arm-smmu driver as a trigger to set up TTBR0
294 */
295 if (atomic_inc_return(&iommu->pagetables) == 1) {
296 /* Enable stall on iommu fault: */
297 adreno_smmu->set_stall(adreno_smmu->cookie, true);
298
299 ret = adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, &ttbr0_cfg);
300 if (ret) {
301 free_io_pgtable_ops(pagetable->pgtbl_ops);
302 kfree(pagetable);
303 return ERR_PTR(ret);
304 }
305 }
306
307 /* Needed later for TLB flush */
308 pagetable->parent = parent;
309 pagetable->tlb = ttbr1_cfg->tlb;
310 pagetable->iommu_dev = ttbr1_cfg->iommu_dev;
311 pagetable->pgsize_bitmap = ttbr0_cfg.pgsize_bitmap;
312 pagetable->ttbr = ttbr0_cfg.arm_lpae_s1_cfg.ttbr;
313
314 /*
315 * TODO we would like each set of page tables to have a unique ASID
316 * to optimize TLB invalidation. But iommu_flush_iotlb_all() will
317 * end up flushing the ASID used for TTBR1 pagetables, which is not
318 * what we want. So for now just use the same ASID as TTBR1.
319 */
320 pagetable->asid = 0;
321
322 return &pagetable->base;
323 }
324
msm_fault_handler(struct iommu_domain * domain,struct device * dev,unsigned long iova,int flags,void * arg)325 static int msm_fault_handler(struct iommu_domain *domain, struct device *dev,
326 unsigned long iova, int flags, void *arg)
327 {
328 struct msm_iommu *iommu = arg;
329 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(iommu->base.dev);
330 struct adreno_smmu_fault_info info, *ptr = NULL;
331
332 if (adreno_smmu->get_fault_info) {
333 adreno_smmu->get_fault_info(adreno_smmu->cookie, &info);
334 ptr = &info;
335 }
336
337 if (iommu->base.handler)
338 return iommu->base.handler(iommu->base.arg, iova, flags, ptr);
339
340 pr_warn_ratelimited("*** fault: iova=%16lx, flags=%d\n", iova, flags);
341 return 0;
342 }
343
msm_iommu_resume_translation(struct msm_mmu * mmu)344 static void msm_iommu_resume_translation(struct msm_mmu *mmu)
345 {
346 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(mmu->dev);
347
348 adreno_smmu->resume_translation(adreno_smmu->cookie, true);
349 }
350
msm_iommu_detach(struct msm_mmu * mmu)351 static void msm_iommu_detach(struct msm_mmu *mmu)
352 {
353 struct msm_iommu *iommu = to_msm_iommu(mmu);
354
355 iommu_detach_device(iommu->domain, mmu->dev);
356 }
357
msm_iommu_map(struct msm_mmu * mmu,uint64_t iova,struct sg_table * sgt,size_t len,int prot)358 static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova,
359 struct sg_table *sgt, size_t len, int prot)
360 {
361 struct msm_iommu *iommu = to_msm_iommu(mmu);
362 size_t ret;
363
364 /* The arm-smmu driver expects the addresses to be sign extended */
365 if (iova & BIT_ULL(48))
366 iova |= GENMASK_ULL(63, 49);
367
368 ret = iommu_map_sgtable(iommu->domain, iova, sgt, prot);
369 WARN_ON(!ret);
370
371 return (ret == len) ? 0 : -EINVAL;
372 }
373
msm_iommu_unmap(struct msm_mmu * mmu,uint64_t iova,size_t len)374 static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, size_t len)
375 {
376 struct msm_iommu *iommu = to_msm_iommu(mmu);
377
378 if (iova & BIT_ULL(48))
379 iova |= GENMASK_ULL(63, 49);
380
381 iommu_unmap(iommu->domain, iova, len);
382
383 return 0;
384 }
385
msm_iommu_destroy(struct msm_mmu * mmu)386 static void msm_iommu_destroy(struct msm_mmu *mmu)
387 {
388 struct msm_iommu *iommu = to_msm_iommu(mmu);
389 iommu_domain_free(iommu->domain);
390 kfree(iommu);
391 }
392
393 static const struct msm_mmu_funcs funcs = {
394 .detach = msm_iommu_detach,
395 .map = msm_iommu_map,
396 .unmap = msm_iommu_unmap,
397 .destroy = msm_iommu_destroy,
398 .resume_translation = msm_iommu_resume_translation,
399 };
400
msm_iommu_new(struct device * dev,struct iommu_domain * domain)401 struct msm_mmu *msm_iommu_new(struct device *dev, struct iommu_domain *domain)
402 {
403 struct msm_iommu *iommu;
404 int ret;
405
406 if (!domain)
407 return ERR_PTR(-ENODEV);
408
409 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
410 if (!iommu)
411 return ERR_PTR(-ENOMEM);
412
413 iommu->domain = domain;
414 msm_mmu_init(&iommu->base, dev, &funcs, MSM_MMU_IOMMU);
415
416 atomic_set(&iommu->pagetables, 0);
417
418 ret = iommu_attach_device(iommu->domain, dev);
419 if (ret) {
420 kfree(iommu);
421 return ERR_PTR(ret);
422 }
423
424 return &iommu->base;
425 }
426