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