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 <linux/kmemleak.h> 10 #include "msm_drv.h" 11 #include "msm_gpu_trace.h" 12 #include "msm_mmu.h" 13 14 struct msm_iommu { 15 struct msm_mmu base; 16 struct iommu_domain *domain; 17 18 struct mutex init_lock; /* protects pagetables counter and prr_page */ 19 int pagetables; 20 struct page *prr_page; 21 22 struct kmem_cache *pt_cache; 23 }; 24 25 #define to_msm_iommu(x) container_of(x, struct msm_iommu, base) 26 27 struct msm_iommu_pagetable { 28 struct msm_mmu base; 29 struct msm_mmu *parent; 30 struct io_pgtable_ops *pgtbl_ops; 31 const struct iommu_flush_ops *tlb; 32 struct device *iommu_dev; 33 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */ 34 phys_addr_t ttbr; 35 u32 asid; 36 37 /** @root_page_table: Stores the root page table pointer. */ 38 void *root_page_table; 39 }; 40 static struct msm_iommu_pagetable *to_pagetable(struct msm_mmu *mmu) 41 { 42 return container_of(mmu, struct msm_iommu_pagetable, base); 43 } 44 45 /* based on iommu_pgsize() in iommu.c: */ 46 static size_t calc_pgsize(struct msm_iommu_pagetable *pagetable, 47 unsigned long iova, phys_addr_t paddr, 48 size_t size, size_t *count) 49 { 50 unsigned int pgsize_idx, pgsize_idx_next; 51 unsigned long pgsizes; 52 size_t offset, pgsize, pgsize_next; 53 unsigned long addr_merge = paddr | iova; 54 55 /* Page sizes supported by the hardware and small enough for @size */ 56 pgsizes = pagetable->pgsize_bitmap & GENMASK(__fls(size), 0); 57 58 /* Constrain the page sizes further based on the maximum alignment */ 59 if (likely(addr_merge)) 60 pgsizes &= GENMASK(__ffs(addr_merge), 0); 61 62 /* Make sure we have at least one suitable page size */ 63 BUG_ON(!pgsizes); 64 65 /* Pick the biggest page size remaining */ 66 pgsize_idx = __fls(pgsizes); 67 pgsize = BIT(pgsize_idx); 68 if (!count) 69 return pgsize; 70 71 /* Find the next biggest support page size, if it exists */ 72 pgsizes = pagetable->pgsize_bitmap & ~GENMASK(pgsize_idx, 0); 73 if (!pgsizes) 74 goto out_set_count; 75 76 pgsize_idx_next = __ffs(pgsizes); 77 pgsize_next = BIT(pgsize_idx_next); 78 79 /* 80 * There's no point trying a bigger page size unless the virtual 81 * and physical addresses are similarly offset within the larger page. 82 */ 83 if ((iova ^ paddr) & (pgsize_next - 1)) 84 goto out_set_count; 85 86 /* Calculate the offset to the next page size alignment boundary */ 87 offset = pgsize_next - (addr_merge & (pgsize_next - 1)); 88 89 /* 90 * If size is big enough to accommodate the larger page, reduce 91 * the number of smaller pages. 92 */ 93 if (offset + pgsize_next <= size) 94 size = offset; 95 96 out_set_count: 97 *count = size >> pgsize_idx; 98 return pgsize; 99 } 100 101 static int msm_iommu_pagetable_unmap(struct msm_mmu *mmu, u64 iova, 102 size_t size) 103 { 104 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu); 105 struct io_pgtable_ops *ops = pagetable->pgtbl_ops; 106 int ret = 0; 107 108 while (size) { 109 size_t pgsize, count; 110 ssize_t unmapped; 111 112 pgsize = calc_pgsize(pagetable, iova, iova, size, &count); 113 114 unmapped = ops->unmap_pages(ops, iova, pgsize, count, NULL); 115 if (unmapped <= 0) { 116 ret = -EINVAL; 117 /* 118 * Continue attempting to unamp the remained of the 119 * range, so we don't end up with some dangling 120 * mapped pages 121 */ 122 unmapped = PAGE_SIZE; 123 } 124 125 iova += unmapped; 126 size -= unmapped; 127 } 128 129 iommu_flush_iotlb_all(to_msm_iommu(pagetable->parent)->domain); 130 131 return ret; 132 } 133 134 static int msm_iommu_pagetable_map_prr(struct msm_mmu *mmu, u64 iova, size_t len, int prot) 135 { 136 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu); 137 struct io_pgtable_ops *ops = pagetable->pgtbl_ops; 138 struct msm_iommu *iommu = to_msm_iommu(pagetable->parent); 139 phys_addr_t phys = page_to_phys(iommu->prr_page); 140 u64 addr = iova; 141 142 while (len) { 143 size_t mapped = 0; 144 size_t size = PAGE_SIZE; 145 int ret; 146 147 ret = ops->map_pages(ops, addr, phys, size, 1, prot, GFP_KERNEL, &mapped); 148 149 /* map_pages could fail after mapping some of the pages, 150 * so update the counters before error handling. 151 */ 152 addr += mapped; 153 len -= mapped; 154 155 if (ret) { 156 msm_iommu_pagetable_unmap(mmu, iova, addr - iova); 157 return -EINVAL; 158 } 159 } 160 161 return 0; 162 } 163 164 static int msm_iommu_pagetable_map(struct msm_mmu *mmu, u64 iova, 165 struct sg_table *sgt, size_t off, size_t len, 166 int prot) 167 { 168 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu); 169 struct io_pgtable_ops *ops = pagetable->pgtbl_ops; 170 struct scatterlist *sg; 171 u64 addr = iova; 172 unsigned int i; 173 174 if (!sgt) 175 return msm_iommu_pagetable_map_prr(mmu, iova, len, prot); 176 177 for_each_sgtable_sg(sgt, sg, i) { 178 size_t size = sg->length; 179 phys_addr_t phys = sg_phys(sg); 180 181 if (!len) 182 break; 183 184 if (size <= off) { 185 off -= size; 186 continue; 187 } 188 189 phys += off; 190 size -= off; 191 size = min_t(size_t, size, len); 192 off = 0; 193 194 while (size) { 195 size_t pgsize, count, mapped = 0; 196 int ret; 197 198 pgsize = calc_pgsize(pagetable, addr, phys, size, &count); 199 200 ret = ops->map_pages(ops, addr, phys, pgsize, count, 201 prot, GFP_KERNEL, &mapped); 202 203 /* map_pages could fail after mapping some of the pages, 204 * so update the counters before error handling. 205 */ 206 phys += mapped; 207 addr += mapped; 208 size -= mapped; 209 len -= mapped; 210 211 if (ret) { 212 msm_iommu_pagetable_unmap(mmu, iova, addr - iova); 213 return -EINVAL; 214 } 215 } 216 } 217 218 return 0; 219 } 220 221 static void msm_iommu_pagetable_destroy(struct msm_mmu *mmu) 222 { 223 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu); 224 struct msm_iommu *iommu = to_msm_iommu(pagetable->parent); 225 struct adreno_smmu_priv *adreno_smmu = 226 dev_get_drvdata(pagetable->parent->dev); 227 228 /* 229 * If this is the last attached pagetable for the parent, 230 * disable TTBR0 in the arm-smmu driver 231 */ 232 mutex_lock(&iommu->init_lock); 233 if (--iommu->pagetables == 0) { 234 adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, NULL); 235 236 if (adreno_smmu->set_prr_bit) { 237 adreno_smmu->set_prr_bit(adreno_smmu->cookie, false); 238 __free_page(iommu->prr_page); 239 iommu->prr_page = NULL; 240 } 241 } 242 mutex_unlock(&iommu->init_lock); 243 244 free_io_pgtable_ops(pagetable->pgtbl_ops); 245 kfree(pagetable); 246 } 247 248 int msm_iommu_pagetable_params(struct msm_mmu *mmu, 249 phys_addr_t *ttbr, int *asid) 250 { 251 struct msm_iommu_pagetable *pagetable; 252 253 if (mmu->type != MSM_MMU_IOMMU_PAGETABLE) 254 return -EINVAL; 255 256 pagetable = to_pagetable(mmu); 257 258 if (ttbr) 259 *ttbr = pagetable->ttbr; 260 261 if (asid) 262 *asid = pagetable->asid; 263 264 return 0; 265 } 266 267 struct iommu_domain_geometry *msm_iommu_get_geometry(struct msm_mmu *mmu) 268 { 269 struct msm_iommu *iommu = to_msm_iommu(mmu); 270 271 return &iommu->domain->geometry; 272 } 273 274 int 275 msm_iommu_pagetable_walk(struct msm_mmu *mmu, unsigned long iova, uint64_t ptes[4]) 276 { 277 struct msm_iommu_pagetable *pagetable; 278 struct arm_lpae_io_pgtable_walk_data wd = {}; 279 280 if (mmu->type != MSM_MMU_IOMMU_PAGETABLE) 281 return -EINVAL; 282 283 pagetable = to_pagetable(mmu); 284 285 if (!pagetable->pgtbl_ops->pgtable_walk) 286 return -EINVAL; 287 288 pagetable->pgtbl_ops->pgtable_walk(pagetable->pgtbl_ops, iova, &wd); 289 290 for (int i = 0; i < ARRAY_SIZE(wd.ptes); i++) 291 ptes[i] = wd.ptes[i]; 292 293 return 0; 294 } 295 296 static void 297 msm_iommu_pagetable_prealloc_count(struct msm_mmu *mmu, struct msm_mmu_prealloc *p, 298 uint64_t iova, size_t len) 299 { 300 u64 pt_count; 301 302 /* 303 * L1, L2 and L3 page tables. 304 * 305 * We could optimize L3 allocation by iterating over the sgt and merging 306 * 2M contiguous blocks, but it's simpler to over-provision and return 307 * the pages if they're not used. 308 * 309 * The first level descriptor (v8 / v7-lpae page table format) encodes 310 * 30 bits of address. The second level encodes 29. For the 3rd it is 311 * 39. 312 * 313 * https://developer.arm.com/documentation/ddi0406/c/System-Level-Architecture/Virtual-Memory-System-Architecture--VMSA-/Long-descriptor-translation-table-format/Long-descriptor-translation-table-format-descriptors?lang=en#BEIHEFFB 314 */ 315 pt_count = ((ALIGN(iova + len, 1ull << 39) - ALIGN_DOWN(iova, 1ull << 39)) >> 39) + 316 ((ALIGN(iova + len, 1ull << 30) - ALIGN_DOWN(iova, 1ull << 30)) >> 30) + 317 ((ALIGN(iova + len, 1ull << 21) - ALIGN_DOWN(iova, 1ull << 21)) >> 21); 318 319 p->count += pt_count; 320 } 321 322 static struct kmem_cache * 323 get_pt_cache(struct msm_mmu *mmu) 324 { 325 struct msm_iommu_pagetable *pagetable = to_pagetable(mmu); 326 return to_msm_iommu(pagetable->parent)->pt_cache; 327 } 328 329 static int 330 msm_iommu_pagetable_prealloc_allocate(struct msm_mmu *mmu, struct msm_mmu_prealloc *p) 331 { 332 struct kmem_cache *pt_cache = get_pt_cache(mmu); 333 334 if (!p->count) { 335 p->pages = NULL; 336 return 0; 337 } 338 339 p->pages = kvmalloc_objs(*p->pages, p->count); 340 if (!p->pages) 341 return -ENOMEM; 342 343 if (!kmem_cache_alloc_bulk(pt_cache, GFP_KERNEL, p->count, p->pages)) { 344 kvfree(p->pages); 345 p->pages = NULL; 346 p->count = 0; 347 return -ENOMEM; 348 } 349 350 return 0; 351 } 352 353 static void 354 msm_iommu_pagetable_prealloc_cleanup(struct msm_mmu *mmu, struct msm_mmu_prealloc *p) 355 { 356 struct kmem_cache *pt_cache = get_pt_cache(mmu); 357 uint32_t remaining_pt_count = p->count - p->ptr; 358 359 if (!p->pages) 360 return; 361 362 if (p->count > 0) 363 trace_msm_mmu_prealloc_cleanup(p->count, remaining_pt_count); 364 365 kmem_cache_free_bulk(pt_cache, remaining_pt_count, &p->pages[p->ptr]); 366 kvfree(p->pages); 367 } 368 369 /** 370 * msm_iommu_pagetable_alloc_pt() - Custom page table allocator 371 * @cookie: Cookie passed at page table allocation time. 372 * @size: Size of the page table. This size should be fixed, 373 * and determined at creation time based on the granule size. 374 * @gfp: GFP flags. 375 * 376 * We want a custom allocator so we can use a cache for page table 377 * allocations and amortize the cost of the over-reservation that's 378 * done to allow asynchronous VM operations. 379 * 380 * Return: non-NULL on success, NULL if the allocation failed for any 381 * reason. 382 */ 383 static void * 384 msm_iommu_pagetable_alloc_pt(void *cookie, size_t size, gfp_t gfp) 385 { 386 struct msm_iommu_pagetable *pagetable = cookie; 387 struct msm_mmu_prealloc *p = pagetable->base.prealloc; 388 void *page; 389 390 /* Allocation of the root page table happening during init. */ 391 if (unlikely(!pagetable->root_page_table)) { 392 struct page *p; 393 394 p = alloc_pages_node(dev_to_node(pagetable->iommu_dev), 395 gfp | __GFP_ZERO, get_order(size)); 396 page = p ? page_address(p) : NULL; 397 pagetable->root_page_table = page; 398 return page; 399 } 400 401 if (WARN_ON(!p) || WARN_ON(p->ptr >= p->count)) 402 return NULL; 403 404 page = p->pages[p->ptr++]; 405 memset(page, 0, size); 406 407 /* 408 * Page table entries don't use virtual addresses, which trips out 409 * kmemleak. kmemleak_alloc_phys() might work, but physical addresses 410 * are mixed with other fields, and I fear kmemleak won't detect that 411 * either. 412 * 413 * Let's just ignore memory passed to the page-table driver for now. 414 */ 415 kmemleak_ignore(page); 416 417 return page; 418 } 419 420 421 /** 422 * msm_iommu_pagetable_free_pt() - Custom page table free function 423 * @cookie: Cookie passed at page table allocation time. 424 * @data: Page table to free. 425 * @size: Size of the page table. This size should be fixed, 426 * and determined at creation time based on the granule size. 427 */ 428 static void 429 msm_iommu_pagetable_free_pt(void *cookie, void *data, size_t size) 430 { 431 struct msm_iommu_pagetable *pagetable = cookie; 432 433 if (unlikely(pagetable->root_page_table == data)) { 434 free_pages((unsigned long)data, get_order(size)); 435 pagetable->root_page_table = NULL; 436 return; 437 } 438 439 kmem_cache_free(get_pt_cache(&pagetable->base), data); 440 } 441 442 static const struct msm_mmu_funcs pagetable_funcs = { 443 .prealloc_count = msm_iommu_pagetable_prealloc_count, 444 .prealloc_allocate = msm_iommu_pagetable_prealloc_allocate, 445 .prealloc_cleanup = msm_iommu_pagetable_prealloc_cleanup, 446 .map = msm_iommu_pagetable_map, 447 .unmap = msm_iommu_pagetable_unmap, 448 .destroy = msm_iommu_pagetable_destroy, 449 }; 450 451 static void msm_iommu_tlb_flush_all(void *cookie) 452 { 453 struct msm_iommu_pagetable *pagetable = cookie; 454 struct adreno_smmu_priv *adreno_smmu; 455 456 if (!pm_runtime_get_if_in_use(pagetable->iommu_dev)) 457 return; 458 459 adreno_smmu = dev_get_drvdata(pagetable->parent->dev); 460 461 pagetable->tlb->tlb_flush_all((void *)adreno_smmu->cookie); 462 463 pm_runtime_put_autosuspend(pagetable->iommu_dev); 464 } 465 466 static void msm_iommu_tlb_flush_walk(unsigned long iova, size_t size, 467 size_t granule, void *cookie) 468 { 469 struct msm_iommu_pagetable *pagetable = cookie; 470 struct adreno_smmu_priv *adreno_smmu; 471 472 if (!pm_runtime_get_if_in_use(pagetable->iommu_dev)) 473 return; 474 475 adreno_smmu = dev_get_drvdata(pagetable->parent->dev); 476 477 pagetable->tlb->tlb_flush_walk(iova, size, granule, (void *)adreno_smmu->cookie); 478 479 pm_runtime_put_autosuspend(pagetable->iommu_dev); 480 } 481 482 static void msm_iommu_tlb_add_page(struct iommu_iotlb_gather *gather, 483 unsigned long iova, size_t granule, void *cookie) 484 { 485 } 486 487 static const struct iommu_flush_ops tlb_ops = { 488 .tlb_flush_all = msm_iommu_tlb_flush_all, 489 .tlb_flush_walk = msm_iommu_tlb_flush_walk, 490 .tlb_add_page = msm_iommu_tlb_add_page, 491 }; 492 493 static int msm_gpu_fault_handler(struct iommu_domain *domain, struct device *dev, 494 unsigned long iova, int flags, void *arg); 495 496 static size_t get_tblsz(const struct io_pgtable_cfg *cfg) 497 { 498 int pg_shift, bits_per_level; 499 500 pg_shift = __ffs(cfg->pgsize_bitmap); 501 /* arm_lpae_iopte is u64: */ 502 bits_per_level = pg_shift - ilog2(sizeof(u64)); 503 504 return sizeof(u64) << bits_per_level; 505 } 506 507 struct msm_mmu *msm_iommu_pagetable_create(struct msm_mmu *parent, bool kernel_managed) 508 { 509 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(parent->dev); 510 struct msm_iommu *iommu = to_msm_iommu(parent); 511 struct msm_iommu_pagetable *pagetable; 512 const struct io_pgtable_cfg *ttbr1_cfg = NULL; 513 struct io_pgtable_cfg ttbr0_cfg; 514 int ret; 515 516 /* Get the pagetable configuration from the domain */ 517 if (adreno_smmu->cookie) 518 ttbr1_cfg = adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie); 519 520 /* 521 * If you hit this WARN_ONCE() you are probably missing an entry in 522 * qcom_smmu_impl_of_match[] in arm-smmu-qcom.c 523 */ 524 if (WARN_ONCE(!ttbr1_cfg, "No per-process page tables")) 525 return ERR_PTR(-ENODEV); 526 527 pagetable = kzalloc_obj(*pagetable); 528 if (!pagetable) 529 return ERR_PTR(-ENOMEM); 530 531 msm_mmu_init(&pagetable->base, parent->dev, &pagetable_funcs, 532 MSM_MMU_IOMMU_PAGETABLE); 533 534 /* Clone the TTBR1 cfg as starting point for TTBR0 cfg: */ 535 ttbr0_cfg = *ttbr1_cfg; 536 537 /* The incoming cfg will have the TTBR1 quirk enabled */ 538 ttbr0_cfg.quirks &= ~IO_PGTABLE_QUIRK_ARM_TTBR1; 539 ttbr0_cfg.tlb = &tlb_ops; 540 541 if (!kernel_managed) { 542 ttbr0_cfg.quirks |= IO_PGTABLE_QUIRK_NO_WARN; 543 544 /* 545 * With userspace managed VM (aka VM_BIND), we need to pre- 546 * allocate pages ahead of time for map/unmap operations, 547 * handing them to io-pgtable via custom alloc/free ops as 548 * needed: 549 */ 550 ttbr0_cfg.alloc = msm_iommu_pagetable_alloc_pt; 551 ttbr0_cfg.free = msm_iommu_pagetable_free_pt; 552 553 /* 554 * Restrict to single page granules. Otherwise we may run 555 * into a situation where userspace wants to unmap/remap 556 * only a part of a larger block mapping, which is not 557 * possible without unmapping the entire block. Which in 558 * turn could cause faults if the GPU is accessing other 559 * parts of the block mapping. 560 * 561 * Note that prior to commit 33729a5fc0ca ("iommu/io-pgtable-arm: 562 * Remove split on unmap behavior)" this was handled in 563 * io-pgtable-arm. But this apparently does not work 564 * correctly on SMMUv3. 565 */ 566 WARN_ON(!(ttbr0_cfg.pgsize_bitmap & PAGE_SIZE)); 567 ttbr0_cfg.pgsize_bitmap = PAGE_SIZE; 568 } 569 570 pagetable->iommu_dev = ttbr1_cfg->iommu_dev; 571 pagetable->pgtbl_ops = alloc_io_pgtable_ops(ARM_64_LPAE_S1, 572 &ttbr0_cfg, pagetable); 573 574 if (!pagetable->pgtbl_ops) { 575 kfree(pagetable); 576 return ERR_PTR(-ENOMEM); 577 } 578 579 /* 580 * If this is the first pagetable that we've allocated, send it back to 581 * the arm-smmu driver as a trigger to set up TTBR0 582 */ 583 mutex_lock(&iommu->init_lock); 584 if (iommu->pagetables++ == 0) { 585 ret = adreno_smmu->set_ttbr0_cfg(adreno_smmu->cookie, &ttbr0_cfg); 586 if (ret) { 587 iommu->pagetables--; 588 mutex_unlock(&iommu->init_lock); 589 free_io_pgtable_ops(pagetable->pgtbl_ops); 590 kfree(pagetable); 591 return ERR_PTR(ret); 592 } 593 594 BUG_ON(iommu->prr_page); 595 if (adreno_smmu->set_prr_bit) { 596 /* 597 * We need a zero'd page for two reasons: 598 * 599 * 1) Reserve a known physical address to use when 600 * mapping NULL / sparsely resident regions 601 * 2) Read back zero 602 * 603 * It appears the hw drops writes to the PRR region 604 * on the floor, but reads actually return whatever 605 * is in the PRR page. 606 */ 607 iommu->prr_page = alloc_page(GFP_KERNEL | __GFP_ZERO); 608 adreno_smmu->set_prr_addr(adreno_smmu->cookie, 609 page_to_phys(iommu->prr_page)); 610 adreno_smmu->set_prr_bit(adreno_smmu->cookie, true); 611 } 612 } 613 mutex_unlock(&iommu->init_lock); 614 615 /* Needed later for TLB flush */ 616 pagetable->parent = parent; 617 pagetable->tlb = ttbr1_cfg->tlb; 618 pagetable->pgsize_bitmap = ttbr0_cfg.pgsize_bitmap; 619 pagetable->ttbr = ttbr0_cfg.arm_lpae_s1_cfg.ttbr; 620 621 /* 622 * TODO we would like each set of page tables to have a unique ASID 623 * to optimize TLB invalidation. But iommu_flush_iotlb_all() will 624 * end up flushing the ASID used for TTBR1 pagetables, which is not 625 * what we want. So for now just use the same ASID as TTBR1. 626 */ 627 pagetable->asid = 0; 628 629 return &pagetable->base; 630 } 631 632 static int msm_gpu_fault_handler(struct iommu_domain *domain, struct device *dev, 633 unsigned long iova, int flags, void *arg) 634 { 635 struct msm_iommu *iommu = arg; 636 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(iommu->base.dev); 637 struct adreno_smmu_fault_info info, *ptr = NULL; 638 639 if (adreno_smmu->get_fault_info) { 640 adreno_smmu->get_fault_info(adreno_smmu->cookie, &info); 641 ptr = &info; 642 } 643 644 if (iommu->base.handler) 645 return iommu->base.handler(iommu->base.arg, iova, flags, ptr); 646 647 pr_warn_ratelimited("*** fault: iova=%16lx, flags=%d\n", iova, flags); 648 649 return 0; 650 } 651 652 static int msm_disp_fault_handler(struct iommu_domain *domain, struct device *dev, 653 unsigned long iova, int flags, void *arg) 654 { 655 struct msm_iommu *iommu = arg; 656 657 if (iommu->base.handler) 658 return iommu->base.handler(iommu->base.arg, iova, flags, NULL); 659 660 return -ENOSYS; 661 } 662 663 static void msm_iommu_set_stall(struct msm_mmu *mmu, bool enable) 664 { 665 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(mmu->dev); 666 667 if (adreno_smmu->set_stall) 668 adreno_smmu->set_stall(adreno_smmu->cookie, enable); 669 } 670 671 static void msm_iommu_detach(struct msm_mmu *mmu) 672 { 673 struct msm_iommu *iommu = to_msm_iommu(mmu); 674 675 iommu_detach_device(iommu->domain, mmu->dev); 676 } 677 678 static int msm_iommu_map(struct msm_mmu *mmu, uint64_t iova, 679 struct sg_table *sgt, size_t off, size_t len, 680 int prot) 681 { 682 struct msm_iommu *iommu = to_msm_iommu(mmu); 683 ssize_t ret; 684 685 WARN_ON(off != 0); 686 687 /* The arm-smmu driver expects the addresses to be sign extended */ 688 if (iova & BIT_ULL(48)) 689 iova |= GENMASK_ULL(63, 49); 690 691 ret = iommu_map_sgtable(iommu->domain, iova, sgt, prot); 692 if (ret < 0) 693 return ret; 694 695 return (ret == len) ? 0 : -EINVAL; 696 } 697 698 static int msm_iommu_unmap(struct msm_mmu *mmu, uint64_t iova, size_t len) 699 { 700 struct msm_iommu *iommu = to_msm_iommu(mmu); 701 702 if (iova & BIT_ULL(48)) 703 iova |= GENMASK_ULL(63, 49); 704 705 iommu_unmap(iommu->domain, iova, len); 706 707 return 0; 708 } 709 710 static void msm_iommu_destroy(struct msm_mmu *mmu) 711 { 712 struct msm_iommu *iommu = to_msm_iommu(mmu); 713 iommu_domain_free(iommu->domain); 714 kmem_cache_destroy(iommu->pt_cache); 715 kfree(iommu); 716 } 717 718 static const struct msm_mmu_funcs funcs = { 719 .detach = msm_iommu_detach, 720 .map = msm_iommu_map, 721 .unmap = msm_iommu_unmap, 722 .destroy = msm_iommu_destroy, 723 .set_stall = msm_iommu_set_stall, 724 }; 725 726 struct msm_mmu *msm_iommu_new(struct device *dev, unsigned long quirks) 727 { 728 struct iommu_domain *domain; 729 struct msm_iommu *iommu; 730 int ret; 731 732 if (!device_iommu_mapped(dev)) 733 return ERR_PTR(-ENODEV); 734 735 domain = iommu_paging_domain_alloc(dev); 736 if (IS_ERR(domain)) 737 return ERR_CAST(domain); 738 739 iommu_set_pgtable_quirks(domain, quirks); 740 741 iommu = kzalloc_obj(*iommu); 742 if (!iommu) { 743 iommu_domain_free(domain); 744 return ERR_PTR(-ENOMEM); 745 } 746 747 iommu->domain = domain; 748 msm_mmu_init(&iommu->base, dev, &funcs, MSM_MMU_IOMMU); 749 750 mutex_init(&iommu->init_lock); 751 752 ret = iommu_attach_device(iommu->domain, dev); 753 if (ret) { 754 iommu_domain_free(domain); 755 kfree(iommu); 756 return ERR_PTR(ret); 757 } 758 759 return &iommu->base; 760 } 761 762 struct msm_mmu *msm_iommu_disp_new(struct device *dev, unsigned long quirks) 763 { 764 struct msm_iommu *iommu; 765 struct msm_mmu *mmu; 766 767 mmu = msm_iommu_new(dev, quirks); 768 if (IS_ERR(mmu)) 769 return mmu; 770 771 iommu = to_msm_iommu(mmu); 772 iommu_set_fault_handler(iommu->domain, msm_disp_fault_handler, iommu); 773 774 return mmu; 775 } 776 777 struct msm_mmu *msm_iommu_gpu_new(struct device *dev, struct msm_gpu *gpu, unsigned long quirks) 778 { 779 struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(dev); 780 struct msm_iommu *iommu; 781 struct msm_mmu *mmu; 782 783 mmu = msm_iommu_new(dev, quirks); 784 if (IS_ERR(mmu)) 785 return mmu; 786 787 iommu = to_msm_iommu(mmu); 788 if (adreno_smmu->cookie) { 789 const struct io_pgtable_cfg *cfg = 790 adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie); 791 size_t tblsz = get_tblsz(cfg); 792 793 iommu->pt_cache = 794 kmem_cache_create("msm-mmu-pt", tblsz, tblsz, 0, NULL); 795 } 796 iommu_set_fault_handler(iommu->domain, msm_gpu_fault_handler, iommu); 797 798 /* Enable stall on iommu fault: */ 799 if (adreno_smmu->set_stall) 800 adreno_smmu->set_stall(adreno_smmu->cookie, true); 801 802 return mmu; 803 } 804