1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * intel-pasid.c - PASID idr, table and entry manipulation 4 * 5 * Copyright (C) 2018 Intel Corporation 6 * 7 * Author: Lu Baolu <baolu.lu@linux.intel.com> 8 */ 9 10 #define pr_fmt(fmt) "DMAR: " fmt 11 12 #include <linux/bitops.h> 13 #include <linux/cpufeature.h> 14 #include <linux/dmar.h> 15 #include <linux/iommu.h> 16 #include <linux/memory.h> 17 #include <linux/pci.h> 18 #include <linux/pci-ats.h> 19 #include <linux/spinlock.h> 20 21 #include "iommu.h" 22 #include "pasid.h" 23 #include "../iommu-pages.h" 24 25 /* 26 * Intel IOMMU system wide PASID name space: 27 */ 28 u32 intel_pasid_max_id = PASID_MAX; 29 30 /* 31 * Per device pasid table management: 32 */ 33 34 /* 35 * Allocate a pasid table for @dev. It should be called in a 36 * single-thread context. 37 */ 38 int intel_pasid_alloc_table(struct device *dev) 39 { 40 struct device_domain_info *info; 41 struct pasid_table *pasid_table; 42 struct pasid_dir_entry *dir; 43 u32 max_pasid = 0; 44 int order, size; 45 46 might_sleep(); 47 info = dev_iommu_priv_get(dev); 48 if (WARN_ON(!info || !dev_is_pci(dev))) 49 return -ENODEV; 50 if (WARN_ON(info->pasid_table)) 51 return -EEXIST; 52 53 pasid_table = kzalloc(sizeof(*pasid_table), GFP_KERNEL); 54 if (!pasid_table) 55 return -ENOMEM; 56 57 if (info->pasid_supported) 58 max_pasid = min_t(u32, pci_max_pasids(to_pci_dev(dev)), 59 intel_pasid_max_id); 60 61 size = max_pasid >> (PASID_PDE_SHIFT - 3); 62 order = size ? get_order(size) : 0; 63 dir = iommu_alloc_pages_node_sz(info->iommu->node, GFP_KERNEL, 64 1 << (order + PAGE_SHIFT)); 65 if (!dir) { 66 kfree(pasid_table); 67 return -ENOMEM; 68 } 69 70 pasid_table->table = dir; 71 pasid_table->max_pasid = 1 << (order + PAGE_SHIFT + 3); 72 info->pasid_table = pasid_table; 73 74 if (!ecap_coherent(info->iommu->ecap)) 75 clflush_cache_range(pasid_table->table, (1 << order) * PAGE_SIZE); 76 77 return 0; 78 } 79 80 void intel_pasid_free_table(struct device *dev) 81 { 82 struct device_domain_info *info; 83 struct pasid_table *pasid_table; 84 struct pasid_dir_entry *dir; 85 struct pasid_entry *table; 86 int i, max_pde; 87 88 info = dev_iommu_priv_get(dev); 89 if (!info || !dev_is_pci(dev) || !info->pasid_table) 90 return; 91 92 pasid_table = info->pasid_table; 93 info->pasid_table = NULL; 94 95 /* Free scalable mode PASID directory tables: */ 96 dir = pasid_table->table; 97 max_pde = pasid_table->max_pasid >> PASID_PDE_SHIFT; 98 for (i = 0; i < max_pde; i++) { 99 table = get_pasid_table_from_pde(&dir[i]); 100 iommu_free_pages(table); 101 } 102 103 iommu_free_pages(pasid_table->table); 104 kfree(pasid_table); 105 } 106 107 struct pasid_table *intel_pasid_get_table(struct device *dev) 108 { 109 struct device_domain_info *info; 110 111 info = dev_iommu_priv_get(dev); 112 if (!info) 113 return NULL; 114 115 return info->pasid_table; 116 } 117 118 static int intel_pasid_get_dev_max_id(struct device *dev) 119 { 120 struct device_domain_info *info; 121 122 info = dev_iommu_priv_get(dev); 123 if (!info || !info->pasid_table) 124 return 0; 125 126 return info->pasid_table->max_pasid; 127 } 128 129 static struct pasid_entry *intel_pasid_get_entry(struct device *dev, u32 pasid) 130 { 131 struct device_domain_info *info; 132 struct pasid_table *pasid_table; 133 struct pasid_dir_entry *dir; 134 struct pasid_entry *entries; 135 int dir_index, index; 136 137 pasid_table = intel_pasid_get_table(dev); 138 if (WARN_ON(!pasid_table || pasid >= intel_pasid_get_dev_max_id(dev))) 139 return NULL; 140 141 dir = pasid_table->table; 142 info = dev_iommu_priv_get(dev); 143 dir_index = pasid >> PASID_PDE_SHIFT; 144 index = pasid & PASID_PTE_MASK; 145 146 retry: 147 entries = get_pasid_table_from_pde(&dir[dir_index]); 148 if (!entries) { 149 u64 tmp; 150 151 entries = iommu_alloc_pages_node_sz(info->iommu->node, 152 GFP_ATOMIC, SZ_4K); 153 if (!entries) 154 return NULL; 155 156 /* 157 * The pasid directory table entry won't be freed after 158 * allocation. No worry about the race with free and 159 * clear. However, this entry might be populated by others 160 * while we are preparing it. Use theirs with a retry. 161 */ 162 tmp = 0ULL; 163 if (!try_cmpxchg64(&dir[dir_index].val, &tmp, 164 (u64)virt_to_phys(entries) | PASID_PTE_PRESENT)) { 165 iommu_free_pages(entries); 166 goto retry; 167 } 168 if (!ecap_coherent(info->iommu->ecap)) { 169 clflush_cache_range(entries, VTD_PAGE_SIZE); 170 clflush_cache_range(&dir[dir_index].val, sizeof(*dir)); 171 } 172 } 173 174 return &entries[index]; 175 } 176 177 /* 178 * Interfaces for PASID table entry manipulation: 179 */ 180 static void 181 intel_pasid_clear_entry(struct device *dev, u32 pasid, bool fault_ignore) 182 { 183 struct pasid_entry *pe; 184 185 pe = intel_pasid_get_entry(dev, pasid); 186 if (WARN_ON(!pe)) 187 return; 188 189 if (fault_ignore && pasid_pte_is_present(pe)) 190 pasid_clear_entry_with_fpd(pe); 191 else 192 pasid_clear_entry(pe); 193 } 194 195 static void 196 pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu, 197 u16 did, u32 pasid) 198 { 199 struct qi_desc desc; 200 201 desc.qw0 = QI_PC_DID(did) | QI_PC_GRAN(QI_PC_PASID_SEL) | 202 QI_PC_PASID(pasid) | QI_PC_TYPE; 203 desc.qw1 = 0; 204 desc.qw2 = 0; 205 desc.qw3 = 0; 206 207 qi_submit_sync(iommu, &desc, 1, 0); 208 } 209 210 static void 211 devtlb_invalidation_with_pasid(struct intel_iommu *iommu, 212 struct device *dev, u32 pasid) 213 { 214 struct device_domain_info *info; 215 u16 sid, qdep, pfsid; 216 217 info = dev_iommu_priv_get(dev); 218 if (!info || !info->ats_enabled) 219 return; 220 221 if (pci_dev_is_disconnected(to_pci_dev(dev))) 222 return; 223 224 sid = PCI_DEVID(info->bus, info->devfn); 225 qdep = info->ats_qdep; 226 pfsid = info->pfsid; 227 228 /* 229 * When PASID 0 is used, it indicates RID2PASID(DMA request w/o PASID), 230 * devTLB flush w/o PASID should be used. For non-zero PASID under 231 * SVA usage, device could do DMA with multiple PASIDs. It is more 232 * efficient to flush devTLB specific to the PASID. 233 */ 234 if (pasid == IOMMU_NO_PASID) 235 qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT); 236 else 237 qi_flush_dev_iotlb_pasid(iommu, sid, pfsid, pasid, qdep, 0, 64 - VTD_PAGE_SHIFT); 238 } 239 240 void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev, 241 u32 pasid, bool fault_ignore) 242 { 243 struct pasid_entry *pte; 244 u16 did, pgtt; 245 246 spin_lock(&iommu->lock); 247 pte = intel_pasid_get_entry(dev, pasid); 248 if (WARN_ON(!pte)) { 249 spin_unlock(&iommu->lock); 250 return; 251 } 252 253 if (!pasid_pte_is_present(pte)) { 254 if (!pasid_pte_is_fault_disabled(pte)) { 255 WARN_ON(READ_ONCE(pte->val[0]) != 0); 256 spin_unlock(&iommu->lock); 257 return; 258 } 259 260 /* 261 * When a PASID is used for SVA by a device, it's possible 262 * that the pasid entry is non-present with the Fault 263 * Processing Disabled bit set. Clear the pasid entry and 264 * drain the PRQ for the PASID before return. 265 */ 266 pasid_clear_entry(pte); 267 spin_unlock(&iommu->lock); 268 intel_iommu_drain_pasid_prq(dev, pasid); 269 270 return; 271 } 272 273 did = pasid_get_domain_id(pte); 274 pgtt = pasid_pte_get_pgtt(pte); 275 intel_pasid_clear_entry(dev, pasid, fault_ignore); 276 spin_unlock(&iommu->lock); 277 278 if (!ecap_coherent(iommu->ecap)) 279 clflush_cache_range(pte, sizeof(*pte)); 280 281 pasid_cache_invalidation_with_pasid(iommu, did, pasid); 282 283 if (pgtt == PASID_ENTRY_PGTT_PT || pgtt == PASID_ENTRY_PGTT_FL_ONLY) 284 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0); 285 else 286 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); 287 288 devtlb_invalidation_with_pasid(iommu, dev, pasid); 289 if (!fault_ignore) 290 intel_iommu_drain_pasid_prq(dev, pasid); 291 } 292 293 /* 294 * This function flushes cache for a newly setup pasid table entry. 295 * Caller of it should not modify the in-use pasid table entries. 296 */ 297 static void pasid_flush_caches(struct intel_iommu *iommu, 298 struct pasid_entry *pte, 299 u32 pasid, u16 did) 300 { 301 if (!ecap_coherent(iommu->ecap)) 302 clflush_cache_range(pte, sizeof(*pte)); 303 304 if (cap_caching_mode(iommu->cap)) { 305 pasid_cache_invalidation_with_pasid(iommu, did, pasid); 306 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0); 307 } else { 308 iommu_flush_write_buffer(iommu); 309 } 310 } 311 312 /* 313 * This function is supposed to be used after caller updates the fields 314 * except for the SSADE and P bit of a pasid table entry. It does the 315 * below: 316 * - Flush cacheline if needed 317 * - Flush the caches per Table 28 ”Guidance to Software for Invalidations“ 318 * of VT-d spec 5.0. 319 */ 320 static void intel_pasid_flush_present(struct intel_iommu *iommu, 321 struct device *dev, 322 u32 pasid, u16 did, 323 struct pasid_entry *pte) 324 { 325 if (!ecap_coherent(iommu->ecap)) 326 clflush_cache_range(pte, sizeof(*pte)); 327 328 /* 329 * VT-d spec 5.0 table28 states guides for cache invalidation: 330 * 331 * - PASID-selective-within-Domain PASID-cache invalidation 332 * - PASID-selective PASID-based IOTLB invalidation 333 * - If (pasid is RID_PASID) 334 * - Global Device-TLB invalidation to affected functions 335 * Else 336 * - PASID-based Device-TLB invalidation (with S=1 and 337 * Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions 338 */ 339 pasid_cache_invalidation_with_pasid(iommu, did, pasid); 340 qi_flush_piotlb(iommu, did, pasid, 0, -1, 0); 341 342 devtlb_invalidation_with_pasid(iommu, dev, pasid); 343 } 344 345 /* 346 * Set up the scalable mode pasid table entry for first only 347 * translation type. 348 */ 349 static void pasid_pte_config_first_level(struct intel_iommu *iommu, 350 struct pasid_entry *pte, 351 phys_addr_t fsptptr, u16 did, 352 int flags) 353 { 354 lockdep_assert_held(&iommu->lock); 355 356 pasid_clear_entry(pte); 357 358 /* Setup the first level page table pointer: */ 359 pasid_set_flptr(pte, fsptptr); 360 361 if (flags & PASID_FLAG_FL5LP) 362 pasid_set_flpm(pte, 1); 363 364 if (flags & PASID_FLAG_PAGE_SNOOP) 365 pasid_set_pgsnp(pte); 366 367 pasid_set_domain_id(pte, did); 368 pasid_set_address_width(pte, iommu->agaw); 369 pasid_set_page_snoop(pte, flags & PASID_FLAG_PWSNP); 370 371 /* Setup Present and PASID Granular Transfer Type: */ 372 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_FL_ONLY); 373 pasid_set_present(pte); 374 } 375 376 int intel_pasid_setup_first_level(struct intel_iommu *iommu, struct device *dev, 377 phys_addr_t fsptptr, u32 pasid, u16 did, 378 int flags) 379 { 380 struct pasid_entry *pte; 381 382 if (!ecap_flts(iommu->ecap)) { 383 pr_err("No first level translation support on %s\n", 384 iommu->name); 385 return -EINVAL; 386 } 387 388 if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) { 389 pr_err("No 5-level paging support for first-level on %s\n", 390 iommu->name); 391 return -EINVAL; 392 } 393 394 spin_lock(&iommu->lock); 395 pte = intel_pasid_get_entry(dev, pasid); 396 if (!pte) { 397 spin_unlock(&iommu->lock); 398 return -ENODEV; 399 } 400 401 if (pasid_pte_is_present(pte)) { 402 spin_unlock(&iommu->lock); 403 return -EBUSY; 404 } 405 406 pasid_pte_config_first_level(iommu, pte, fsptptr, did, flags); 407 408 spin_unlock(&iommu->lock); 409 410 pasid_flush_caches(iommu, pte, pasid, did); 411 412 return 0; 413 } 414 415 int intel_pasid_replace_first_level(struct intel_iommu *iommu, 416 struct device *dev, phys_addr_t fsptptr, 417 u32 pasid, u16 did, u16 old_did, 418 int flags) 419 { 420 struct pasid_entry *pte, new_pte; 421 422 if (!ecap_flts(iommu->ecap)) { 423 pr_err("No first level translation support on %s\n", 424 iommu->name); 425 return -EINVAL; 426 } 427 428 if ((flags & PASID_FLAG_FL5LP) && !cap_fl5lp_support(iommu->cap)) { 429 pr_err("No 5-level paging support for first-level on %s\n", 430 iommu->name); 431 return -EINVAL; 432 } 433 434 pasid_pte_config_first_level(iommu, &new_pte, fsptptr, did, flags); 435 436 spin_lock(&iommu->lock); 437 pte = intel_pasid_get_entry(dev, pasid); 438 if (!pte) { 439 spin_unlock(&iommu->lock); 440 return -ENODEV; 441 } 442 443 if (!pasid_pte_is_present(pte)) { 444 spin_unlock(&iommu->lock); 445 return -EINVAL; 446 } 447 448 WARN_ON(old_did != pasid_get_domain_id(pte)); 449 450 *pte = new_pte; 451 spin_unlock(&iommu->lock); 452 453 intel_pasid_flush_present(iommu, dev, pasid, old_did, pte); 454 intel_iommu_drain_pasid_prq(dev, pasid); 455 456 return 0; 457 } 458 459 /* 460 * Set up the scalable mode pasid entry for second only translation type. 461 */ 462 static void pasid_pte_config_second_level(struct intel_iommu *iommu, 463 struct pasid_entry *pte, 464 struct dmar_domain *domain, u16 did) 465 { 466 struct pt_iommu_vtdss_hw_info pt_info; 467 468 lockdep_assert_held(&iommu->lock); 469 470 pt_iommu_vtdss_hw_info(&domain->sspt, &pt_info); 471 pasid_clear_entry(pte); 472 pasid_set_domain_id(pte, did); 473 pasid_set_slptr(pte, pt_info.ssptptr); 474 pasid_set_address_width(pte, pt_info.aw); 475 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_SL_ONLY); 476 pasid_set_fault_enable(pte); 477 pasid_set_page_snoop(pte, !(domain->sspt.vtdss_pt.common.features & 478 BIT(PT_FEAT_DMA_INCOHERENT))); 479 if (domain->dirty_tracking) 480 pasid_set_ssade(pte); 481 482 pasid_set_present(pte); 483 } 484 485 int intel_pasid_setup_second_level(struct intel_iommu *iommu, 486 struct dmar_domain *domain, 487 struct device *dev, u32 pasid) 488 { 489 struct pasid_entry *pte; 490 u16 did; 491 492 493 /* 494 * If hardware advertises no support for second level 495 * translation, return directly. 496 */ 497 if (!ecap_slts(iommu->ecap)) { 498 pr_err("No second level translation support on %s\n", 499 iommu->name); 500 return -EINVAL; 501 } 502 503 did = domain_id_iommu(domain, iommu); 504 505 spin_lock(&iommu->lock); 506 pte = intel_pasid_get_entry(dev, pasid); 507 if (!pte) { 508 spin_unlock(&iommu->lock); 509 return -ENODEV; 510 } 511 512 if (pasid_pte_is_present(pte)) { 513 spin_unlock(&iommu->lock); 514 return -EBUSY; 515 } 516 517 pasid_pte_config_second_level(iommu, pte, domain, did); 518 spin_unlock(&iommu->lock); 519 520 pasid_flush_caches(iommu, pte, pasid, did); 521 522 return 0; 523 } 524 525 int intel_pasid_replace_second_level(struct intel_iommu *iommu, 526 struct dmar_domain *domain, 527 struct device *dev, u16 old_did, 528 u32 pasid) 529 { 530 struct pasid_entry *pte, new_pte; 531 u16 did; 532 533 /* 534 * If hardware advertises no support for second level 535 * translation, return directly. 536 */ 537 if (!ecap_slts(iommu->ecap)) { 538 pr_err("No second level translation support on %s\n", 539 iommu->name); 540 return -EINVAL; 541 } 542 543 did = domain_id_iommu(domain, iommu); 544 545 pasid_pte_config_second_level(iommu, &new_pte, domain, did); 546 547 spin_lock(&iommu->lock); 548 pte = intel_pasid_get_entry(dev, pasid); 549 if (!pte) { 550 spin_unlock(&iommu->lock); 551 return -ENODEV; 552 } 553 554 if (!pasid_pte_is_present(pte)) { 555 spin_unlock(&iommu->lock); 556 return -EINVAL; 557 } 558 559 WARN_ON(old_did != pasid_get_domain_id(pte)); 560 561 *pte = new_pte; 562 spin_unlock(&iommu->lock); 563 564 intel_pasid_flush_present(iommu, dev, pasid, old_did, pte); 565 intel_iommu_drain_pasid_prq(dev, pasid); 566 567 return 0; 568 } 569 570 /* 571 * Set up dirty tracking on a second only or nested translation type. 572 */ 573 int intel_pasid_setup_dirty_tracking(struct intel_iommu *iommu, 574 struct device *dev, u32 pasid, 575 bool enabled) 576 { 577 struct pasid_entry *pte; 578 u16 did, pgtt; 579 580 spin_lock(&iommu->lock); 581 582 pte = intel_pasid_get_entry(dev, pasid); 583 if (!pte) { 584 spin_unlock(&iommu->lock); 585 dev_err_ratelimited( 586 dev, "Failed to get pasid entry of PASID %d\n", pasid); 587 return -ENODEV; 588 } 589 590 did = pasid_get_domain_id(pte); 591 pgtt = pasid_pte_get_pgtt(pte); 592 if (pgtt != PASID_ENTRY_PGTT_SL_ONLY && 593 pgtt != PASID_ENTRY_PGTT_NESTED) { 594 spin_unlock(&iommu->lock); 595 dev_err_ratelimited( 596 dev, 597 "Dirty tracking not supported on translation type %d\n", 598 pgtt); 599 return -EOPNOTSUPP; 600 } 601 602 if (pasid_get_ssade(pte) == enabled) { 603 spin_unlock(&iommu->lock); 604 return 0; 605 } 606 607 if (enabled) 608 pasid_set_ssade(pte); 609 else 610 pasid_clear_ssade(pte); 611 spin_unlock(&iommu->lock); 612 613 if (!ecap_coherent(iommu->ecap)) 614 clflush_cache_range(pte, sizeof(*pte)); 615 616 /* 617 * From VT-d spec table 25 "Guidance to Software for Invalidations": 618 * 619 * - PASID-selective-within-Domain PASID-cache invalidation 620 * If (PGTT=SS or Nested) 621 * - Domain-selective IOTLB invalidation 622 * Else 623 * - PASID-selective PASID-based IOTLB invalidation 624 * - If (pasid is RID_PASID) 625 * - Global Device-TLB invalidation to affected functions 626 * Else 627 * - PASID-based Device-TLB invalidation (with S=1 and 628 * Addr[63:12]=0x7FFFFFFF_FFFFF) to affected functions 629 */ 630 pasid_cache_invalidation_with_pasid(iommu, did, pasid); 631 632 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); 633 634 devtlb_invalidation_with_pasid(iommu, dev, pasid); 635 636 return 0; 637 } 638 639 /* 640 * Set up the scalable mode pasid entry for passthrough translation type. 641 */ 642 static void pasid_pte_config_pass_through(struct intel_iommu *iommu, 643 struct pasid_entry *pte, u16 did) 644 { 645 lockdep_assert_held(&iommu->lock); 646 647 pasid_clear_entry(pte); 648 pasid_set_domain_id(pte, did); 649 pasid_set_address_width(pte, iommu->agaw); 650 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_PT); 651 pasid_set_fault_enable(pte); 652 pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); 653 pasid_set_present(pte); 654 } 655 656 int intel_pasid_setup_pass_through(struct intel_iommu *iommu, 657 struct device *dev, u32 pasid) 658 { 659 u16 did = FLPT_DEFAULT_DID; 660 struct pasid_entry *pte; 661 662 spin_lock(&iommu->lock); 663 pte = intel_pasid_get_entry(dev, pasid); 664 if (!pte) { 665 spin_unlock(&iommu->lock); 666 return -ENODEV; 667 } 668 669 if (pasid_pte_is_present(pte)) { 670 spin_unlock(&iommu->lock); 671 return -EBUSY; 672 } 673 674 pasid_pte_config_pass_through(iommu, pte, did); 675 spin_unlock(&iommu->lock); 676 677 pasid_flush_caches(iommu, pte, pasid, did); 678 679 return 0; 680 } 681 682 int intel_pasid_replace_pass_through(struct intel_iommu *iommu, 683 struct device *dev, u16 old_did, 684 u32 pasid) 685 { 686 struct pasid_entry *pte, new_pte; 687 u16 did = FLPT_DEFAULT_DID; 688 689 pasid_pte_config_pass_through(iommu, &new_pte, did); 690 691 spin_lock(&iommu->lock); 692 pte = intel_pasid_get_entry(dev, pasid); 693 if (!pte) { 694 spin_unlock(&iommu->lock); 695 return -ENODEV; 696 } 697 698 if (!pasid_pte_is_present(pte)) { 699 spin_unlock(&iommu->lock); 700 return -EINVAL; 701 } 702 703 WARN_ON(old_did != pasid_get_domain_id(pte)); 704 705 *pte = new_pte; 706 spin_unlock(&iommu->lock); 707 708 intel_pasid_flush_present(iommu, dev, pasid, old_did, pte); 709 intel_iommu_drain_pasid_prq(dev, pasid); 710 711 return 0; 712 } 713 714 /* 715 * Set the page snoop control for a pasid entry which has been set up. 716 */ 717 void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu, 718 struct device *dev, u32 pasid) 719 { 720 struct pasid_entry *pte; 721 u16 did; 722 723 spin_lock(&iommu->lock); 724 pte = intel_pasid_get_entry(dev, pasid); 725 if (WARN_ON(!pte || !pasid_pte_is_present(pte))) { 726 spin_unlock(&iommu->lock); 727 return; 728 } 729 730 pasid_set_pgsnp(pte); 731 did = pasid_get_domain_id(pte); 732 spin_unlock(&iommu->lock); 733 734 intel_pasid_flush_present(iommu, dev, pasid, did, pte); 735 } 736 737 static void pasid_pte_config_nestd(struct intel_iommu *iommu, 738 struct pasid_entry *pte, 739 struct iommu_hwpt_vtd_s1 *s1_cfg, 740 struct dmar_domain *s2_domain, 741 u16 did) 742 { 743 struct pt_iommu_vtdss_hw_info pt_info; 744 745 lockdep_assert_held(&iommu->lock); 746 747 pt_iommu_vtdss_hw_info(&s2_domain->sspt, &pt_info); 748 749 pasid_clear_entry(pte); 750 751 if (s1_cfg->addr_width == ADDR_WIDTH_5LEVEL) 752 pasid_set_flpm(pte, 1); 753 754 pasid_set_flptr(pte, s1_cfg->pgtbl_addr); 755 756 if (s1_cfg->flags & IOMMU_VTD_S1_SRE) { 757 pasid_set_sre(pte); 758 if (s1_cfg->flags & IOMMU_VTD_S1_WPE) 759 pasid_set_wpe(pte); 760 } 761 762 if (s1_cfg->flags & IOMMU_VTD_S1_EAFE) 763 pasid_set_eafe(pte); 764 765 if (s2_domain->force_snooping) 766 pasid_set_pgsnp(pte); 767 768 pasid_set_slptr(pte, pt_info.ssptptr); 769 pasid_set_fault_enable(pte); 770 pasid_set_domain_id(pte, did); 771 pasid_set_address_width(pte, pt_info.aw); 772 pasid_set_page_snoop(pte, !(s2_domain->sspt.vtdss_pt.common.features & 773 BIT(PT_FEAT_DMA_INCOHERENT))); 774 if (s2_domain->dirty_tracking) 775 pasid_set_ssade(pte); 776 pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED); 777 pasid_set_present(pte); 778 } 779 780 /** 781 * intel_pasid_setup_nested() - Set up PASID entry for nested translation. 782 * @iommu: IOMMU which the device belong to 783 * @dev: Device to be set up for translation 784 * @pasid: PASID to be programmed in the device PASID table 785 * @domain: User stage-1 domain nested on a stage-2 domain 786 * 787 * This is used for nested translation. The input domain should be 788 * nested type and nested on a parent with 'is_nested_parent' flag 789 * set. 790 */ 791 int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev, 792 u32 pasid, struct dmar_domain *domain) 793 { 794 struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg; 795 struct dmar_domain *s2_domain = domain->s2_domain; 796 u16 did = domain_id_iommu(domain, iommu); 797 struct pasid_entry *pte; 798 799 /* Address width should match the address width supported by hardware */ 800 switch (s1_cfg->addr_width) { 801 case ADDR_WIDTH_4LEVEL: 802 break; 803 case ADDR_WIDTH_5LEVEL: 804 if (!cap_fl5lp_support(iommu->cap)) { 805 dev_err_ratelimited(dev, 806 "5-level paging not supported\n"); 807 return -EINVAL; 808 } 809 break; 810 default: 811 dev_err_ratelimited(dev, "Invalid stage-1 address width %d\n", 812 s1_cfg->addr_width); 813 return -EINVAL; 814 } 815 816 if ((s1_cfg->flags & IOMMU_VTD_S1_SRE) && !ecap_srs(iommu->ecap)) { 817 pr_err_ratelimited("No supervisor request support on %s\n", 818 iommu->name); 819 return -EINVAL; 820 } 821 822 if ((s1_cfg->flags & IOMMU_VTD_S1_EAFE) && !ecap_eafs(iommu->ecap)) { 823 pr_err_ratelimited("No extended access flag support on %s\n", 824 iommu->name); 825 return -EINVAL; 826 } 827 828 spin_lock(&iommu->lock); 829 pte = intel_pasid_get_entry(dev, pasid); 830 if (!pte) { 831 spin_unlock(&iommu->lock); 832 return -ENODEV; 833 } 834 if (pasid_pte_is_present(pte)) { 835 spin_unlock(&iommu->lock); 836 return -EBUSY; 837 } 838 839 pasid_pte_config_nestd(iommu, pte, s1_cfg, s2_domain, did); 840 spin_unlock(&iommu->lock); 841 842 pasid_flush_caches(iommu, pte, pasid, did); 843 844 return 0; 845 } 846 847 int intel_pasid_replace_nested(struct intel_iommu *iommu, 848 struct device *dev, u32 pasid, 849 u16 old_did, struct dmar_domain *domain) 850 { 851 struct iommu_hwpt_vtd_s1 *s1_cfg = &domain->s1_cfg; 852 struct dmar_domain *s2_domain = domain->s2_domain; 853 u16 did = domain_id_iommu(domain, iommu); 854 struct pasid_entry *pte, new_pte; 855 856 /* Address width should match the address width supported by hardware */ 857 switch (s1_cfg->addr_width) { 858 case ADDR_WIDTH_4LEVEL: 859 break; 860 case ADDR_WIDTH_5LEVEL: 861 if (!cap_fl5lp_support(iommu->cap)) { 862 dev_err_ratelimited(dev, 863 "5-level paging not supported\n"); 864 return -EINVAL; 865 } 866 break; 867 default: 868 dev_err_ratelimited(dev, "Invalid stage-1 address width %d\n", 869 s1_cfg->addr_width); 870 return -EINVAL; 871 } 872 873 if ((s1_cfg->flags & IOMMU_VTD_S1_SRE) && !ecap_srs(iommu->ecap)) { 874 pr_err_ratelimited("No supervisor request support on %s\n", 875 iommu->name); 876 return -EINVAL; 877 } 878 879 if ((s1_cfg->flags & IOMMU_VTD_S1_EAFE) && !ecap_eafs(iommu->ecap)) { 880 pr_err_ratelimited("No extended access flag support on %s\n", 881 iommu->name); 882 return -EINVAL; 883 } 884 885 pasid_pte_config_nestd(iommu, &new_pte, s1_cfg, s2_domain, did); 886 887 spin_lock(&iommu->lock); 888 pte = intel_pasid_get_entry(dev, pasid); 889 if (!pte) { 890 spin_unlock(&iommu->lock); 891 return -ENODEV; 892 } 893 894 if (!pasid_pte_is_present(pte)) { 895 spin_unlock(&iommu->lock); 896 return -EINVAL; 897 } 898 899 WARN_ON(old_did != pasid_get_domain_id(pte)); 900 901 *pte = new_pte; 902 spin_unlock(&iommu->lock); 903 904 intel_pasid_flush_present(iommu, dev, pasid, old_did, pte); 905 intel_iommu_drain_pasid_prq(dev, pasid); 906 907 return 0; 908 } 909 910 /* 911 * Interfaces to setup or teardown a pasid table to the scalable-mode 912 * context table entry: 913 */ 914 915 static void device_pasid_table_teardown(struct device *dev, u8 bus, u8 devfn) 916 { 917 struct device_domain_info *info = dev_iommu_priv_get(dev); 918 struct intel_iommu *iommu = info->iommu; 919 struct context_entry *context; 920 u16 did; 921 922 spin_lock(&iommu->lock); 923 context = iommu_context_addr(iommu, bus, devfn, false); 924 if (!context) { 925 spin_unlock(&iommu->lock); 926 return; 927 } 928 929 did = context_domain_id(context); 930 context_clear_entry(context); 931 __iommu_flush_cache(iommu, context, sizeof(*context)); 932 spin_unlock(&iommu->lock); 933 intel_context_flush_no_pasid(info, context, did); 934 } 935 936 static int pci_pasid_table_teardown(struct pci_dev *pdev, u16 alias, void *data) 937 { 938 struct device *dev = data; 939 940 if (dev == &pdev->dev) 941 device_pasid_table_teardown(dev, PCI_BUS_NUM(alias), alias & 0xff); 942 943 return 0; 944 } 945 946 void intel_pasid_teardown_sm_context(struct device *dev) 947 { 948 struct device_domain_info *info = dev_iommu_priv_get(dev); 949 950 if (!dev_is_pci(dev)) { 951 device_pasid_table_teardown(dev, info->bus, info->devfn); 952 return; 953 } 954 955 pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_teardown, dev); 956 } 957 958 /* 959 * Get the PASID directory size for scalable mode context entry. 960 * Value of X in the PDTS field of a scalable mode context entry 961 * indicates PASID directory with 2^(X + 7) entries. 962 */ 963 static unsigned long context_get_sm_pds(struct pasid_table *table) 964 { 965 unsigned long pds, max_pde; 966 967 max_pde = table->max_pasid >> PASID_PDE_SHIFT; 968 pds = find_first_bit(&max_pde, MAX_NR_PASID_BITS); 969 if (pds < 7) 970 return 0; 971 972 return pds - 7; 973 } 974 975 static int context_entry_set_pasid_table(struct context_entry *context, 976 struct device *dev) 977 { 978 struct device_domain_info *info = dev_iommu_priv_get(dev); 979 struct pasid_table *table = info->pasid_table; 980 struct intel_iommu *iommu = info->iommu; 981 unsigned long pds; 982 983 context_clear_entry(context); 984 985 pds = context_get_sm_pds(table); 986 context->lo = (u64)virt_to_phys(table->table) | context_pdts(pds); 987 context_set_sm_rid2pasid(context, IOMMU_NO_PASID); 988 989 if (info->ats_supported) 990 context_set_sm_dte(context); 991 if (info->pasid_supported) 992 context_set_pasid(context); 993 if (info->pri_supported) 994 context_set_sm_pre(context); 995 996 context_set_fault_enable(context); 997 context_set_present(context); 998 __iommu_flush_cache(iommu, context, sizeof(*context)); 999 1000 return 0; 1001 } 1002 1003 static int device_pasid_table_setup(struct device *dev, u8 bus, u8 devfn) 1004 { 1005 struct device_domain_info *info = dev_iommu_priv_get(dev); 1006 struct intel_iommu *iommu = info->iommu; 1007 struct context_entry *context; 1008 1009 spin_lock(&iommu->lock); 1010 context = iommu_context_addr(iommu, bus, devfn, true); 1011 if (!context) { 1012 spin_unlock(&iommu->lock); 1013 return -ENOMEM; 1014 } 1015 1016 if (context_present(context) && !context_copied(iommu, bus, devfn)) { 1017 spin_unlock(&iommu->lock); 1018 return 0; 1019 } 1020 1021 if (context_copied(iommu, bus, devfn)) { 1022 context_clear_entry(context); 1023 __iommu_flush_cache(iommu, context, sizeof(*context)); 1024 1025 /* 1026 * For kdump cases, old valid entries may be cached due to 1027 * the in-flight DMA and copied pgtable, but there is no 1028 * unmapping behaviour for them, thus we need explicit cache 1029 * flushes for all affected domain IDs and PASIDs used in 1030 * the copied PASID table. Given that we have no idea about 1031 * which domain IDs and PASIDs were used in the copied tables, 1032 * upgrade them to global PASID and IOTLB cache invalidation. 1033 */ 1034 iommu->flush.flush_context(iommu, 0, 1035 PCI_DEVID(bus, devfn), 1036 DMA_CCMD_MASK_NOBIT, 1037 DMA_CCMD_DEVICE_INVL); 1038 qi_flush_pasid_cache(iommu, 0, QI_PC_GLOBAL, 0); 1039 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_GLOBAL_FLUSH); 1040 devtlb_invalidation_with_pasid(iommu, dev, IOMMU_NO_PASID); 1041 1042 /* 1043 * At this point, the device is supposed to finish reset at 1044 * its driver probe stage, so no in-flight DMA will exist, 1045 * and we don't need to worry anymore hereafter. 1046 */ 1047 clear_context_copied(iommu, bus, devfn); 1048 } 1049 1050 context_entry_set_pasid_table(context, dev); 1051 spin_unlock(&iommu->lock); 1052 1053 /* 1054 * It's a non-present to present mapping. If hardware doesn't cache 1055 * non-present entry we don't need to flush the caches. If it does 1056 * cache non-present entries, then it does so in the special 1057 * domain #0, which we have to flush: 1058 */ 1059 if (cap_caching_mode(iommu->cap)) { 1060 iommu->flush.flush_context(iommu, 0, 1061 PCI_DEVID(bus, devfn), 1062 DMA_CCMD_MASK_NOBIT, 1063 DMA_CCMD_DEVICE_INVL); 1064 iommu->flush.flush_iotlb(iommu, 0, 0, 0, DMA_TLB_DSI_FLUSH); 1065 } 1066 1067 return 0; 1068 } 1069 1070 static int pci_pasid_table_setup(struct pci_dev *pdev, u16 alias, void *data) 1071 { 1072 struct device *dev = data; 1073 1074 if (dev != &pdev->dev) 1075 return 0; 1076 1077 return device_pasid_table_setup(dev, PCI_BUS_NUM(alias), alias & 0xff); 1078 } 1079 1080 /* 1081 * Set the device's PASID table to its context table entry. 1082 * 1083 * The PASID table is set to the context entries of both device itself 1084 * and its alias requester ID for DMA. 1085 */ 1086 int intel_pasid_setup_sm_context(struct device *dev) 1087 { 1088 struct device_domain_info *info = dev_iommu_priv_get(dev); 1089 1090 if (!dev_is_pci(dev)) 1091 return device_pasid_table_setup(dev, info->bus, info->devfn); 1092 1093 return pci_for_each_dma_alias(to_pci_dev(dev), pci_pasid_table_setup, dev); 1094 } 1095 1096 /* 1097 * Global Device-TLB invalidation following changes in a context entry which 1098 * was present. 1099 */ 1100 static void __context_flush_dev_iotlb(struct device_domain_info *info) 1101 { 1102 if (!info->ats_enabled) 1103 return; 1104 1105 qi_flush_dev_iotlb(info->iommu, PCI_DEVID(info->bus, info->devfn), 1106 info->pfsid, info->ats_qdep, 0, MAX_AGAW_PFN_WIDTH); 1107 1108 /* 1109 * There is no guarantee that the device DMA is stopped when it reaches 1110 * here. Therefore, always attempt the extra device TLB invalidation 1111 * quirk. The impact on performance is acceptable since this is not a 1112 * performance-critical path. 1113 */ 1114 quirk_extra_dev_tlb_flush(info, 0, MAX_AGAW_PFN_WIDTH, IOMMU_NO_PASID, 1115 info->ats_qdep); 1116 } 1117 1118 /* 1119 * Cache invalidations after change in a context table entry that was present 1120 * according to the Spec 6.5.3.3 (Guidance to Software for Invalidations). 1121 * This helper can only be used when IOMMU is working in the legacy mode or 1122 * IOMMU is in scalable mode but all PASID table entries of the device are 1123 * non-present. 1124 */ 1125 void intel_context_flush_no_pasid(struct device_domain_info *info, 1126 struct context_entry *context, u16 did) 1127 { 1128 struct intel_iommu *iommu = info->iommu; 1129 1130 /* 1131 * Device-selective context-cache invalidation. The Domain-ID field 1132 * of the Context-cache Invalidate Descriptor is ignored by hardware 1133 * when operating in scalable mode. Therefore the @did value doesn't 1134 * matter in scalable mode. 1135 */ 1136 iommu->flush.flush_context(iommu, did, PCI_DEVID(info->bus, info->devfn), 1137 DMA_CCMD_MASK_NOBIT, DMA_CCMD_DEVICE_INVL); 1138 1139 /* 1140 * For legacy mode: 1141 * - Domain-selective IOTLB invalidation 1142 * - Global Device-TLB invalidation to all affected functions 1143 */ 1144 if (!sm_supported(iommu)) { 1145 iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH); 1146 __context_flush_dev_iotlb(info); 1147 1148 return; 1149 } 1150 1151 __context_flush_dev_iotlb(info); 1152 } 1153