|
9 | 9 |
|
10 | 10 | #define pr_fmt(fmt) "DMAR: " fmt
|
11 | 11 |
|
| 12 | +#include <linux/bitops.h> |
12 | 13 | #include <linux/dmar.h>
|
13 | 14 | #include <linux/intel-iommu.h>
|
14 | 15 | #include <linux/iommu.h>
|
@@ -294,3 +295,282 @@ void intel_pasid_clear_entry(struct device *dev, int pasid)
|
294 | 295 |
|
295 | 296 | pasid_clear_entry(pe);
|
296 | 297 | }
|
| 298 | + |
| 299 | +static inline void pasid_set_bits(u64 *ptr, u64 mask, u64 bits) |
| 300 | +{ |
| 301 | + u64 old; |
| 302 | + |
| 303 | + old = READ_ONCE(*ptr); |
| 304 | + WRITE_ONCE(*ptr, (old & ~mask) | bits); |
| 305 | +} |
| 306 | + |
| 307 | +/* |
| 308 | + * Setup the DID(Domain Identifier) field (Bit 64~79) of scalable mode |
| 309 | + * PASID entry. |
| 310 | + */ |
| 311 | +static inline void |
| 312 | +pasid_set_domain_id(struct pasid_entry *pe, u64 value) |
| 313 | +{ |
| 314 | + pasid_set_bits(&pe->val[1], GENMASK_ULL(15, 0), value); |
| 315 | +} |
| 316 | + |
| 317 | +/* |
| 318 | + * Get domain ID value of a scalable mode PASID entry. |
| 319 | + */ |
| 320 | +static inline u16 |
| 321 | +pasid_get_domain_id(struct pasid_entry *pe) |
| 322 | +{ |
| 323 | + return (u16)(READ_ONCE(pe->val[1]) & GENMASK_ULL(15, 0)); |
| 324 | +} |
| 325 | + |
| 326 | +/* |
| 327 | + * Setup the SLPTPTR(Second Level Page Table Pointer) field (Bit 12~63) |
| 328 | + * of a scalable mode PASID entry. |
| 329 | + */ |
| 330 | +static inline void |
| 331 | +pasid_set_slptr(struct pasid_entry *pe, u64 value) |
| 332 | +{ |
| 333 | + pasid_set_bits(&pe->val[0], VTD_PAGE_MASK, value); |
| 334 | +} |
| 335 | + |
| 336 | +/* |
| 337 | + * Setup the AW(Address Width) field (Bit 2~4) of a scalable mode PASID |
| 338 | + * entry. |
| 339 | + */ |
| 340 | +static inline void |
| 341 | +pasid_set_address_width(struct pasid_entry *pe, u64 value) |
| 342 | +{ |
| 343 | + pasid_set_bits(&pe->val[0], GENMASK_ULL(4, 2), value << 2); |
| 344 | +} |
| 345 | + |
| 346 | +/* |
| 347 | + * Setup the PGTT(PASID Granular Translation Type) field (Bit 6~8) |
| 348 | + * of a scalable mode PASID entry. |
| 349 | + */ |
| 350 | +static inline void |
| 351 | +pasid_set_translation_type(struct pasid_entry *pe, u64 value) |
| 352 | +{ |
| 353 | + pasid_set_bits(&pe->val[0], GENMASK_ULL(8, 6), value << 6); |
| 354 | +} |
| 355 | + |
| 356 | +/* |
| 357 | + * Enable fault processing by clearing the FPD(Fault Processing |
| 358 | + * Disable) field (Bit 1) of a scalable mode PASID entry. |
| 359 | + */ |
| 360 | +static inline void pasid_set_fault_enable(struct pasid_entry *pe) |
| 361 | +{ |
| 362 | + pasid_set_bits(&pe->val[0], 1 << 1, 0); |
| 363 | +} |
| 364 | + |
| 365 | +/* |
| 366 | + * Setup the SRE(Supervisor Request Enable) field (Bit 128) of a |
| 367 | + * scalable mode PASID entry. |
| 368 | + */ |
| 369 | +static inline void pasid_set_sre(struct pasid_entry *pe) |
| 370 | +{ |
| 371 | + pasid_set_bits(&pe->val[2], 1 << 0, 1); |
| 372 | +} |
| 373 | + |
| 374 | +/* |
| 375 | + * Setup the P(Present) field (Bit 0) of a scalable mode PASID |
| 376 | + * entry. |
| 377 | + */ |
| 378 | +static inline void pasid_set_present(struct pasid_entry *pe) |
| 379 | +{ |
| 380 | + pasid_set_bits(&pe->val[0], 1 << 0, 1); |
| 381 | +} |
| 382 | + |
| 383 | +/* |
| 384 | + * Setup Page Walk Snoop bit (Bit 87) of a scalable mode PASID |
| 385 | + * entry. |
| 386 | + */ |
| 387 | +static inline void pasid_set_page_snoop(struct pasid_entry *pe, bool value) |
| 388 | +{ |
| 389 | + pasid_set_bits(&pe->val[1], 1 << 23, value); |
| 390 | +} |
| 391 | + |
| 392 | +static void |
| 393 | +pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu, |
| 394 | + u16 did, int pasid) |
| 395 | +{ |
| 396 | + struct qi_desc desc; |
| 397 | + |
| 398 | + desc.qw0 = QI_PC_DID(did) | QI_PC_PASID_SEL | QI_PC_PASID(pasid); |
| 399 | + desc.qw1 = 0; |
| 400 | + desc.qw2 = 0; |
| 401 | + desc.qw3 = 0; |
| 402 | + |
| 403 | + qi_submit_sync(&desc, iommu); |
| 404 | +} |
| 405 | + |
| 406 | +static void |
| 407 | +iotlb_invalidation_with_pasid(struct intel_iommu *iommu, u16 did, u32 pasid) |
| 408 | +{ |
| 409 | + struct qi_desc desc; |
| 410 | + |
| 411 | + desc.qw0 = QI_EIOTLB_PASID(pasid) | QI_EIOTLB_DID(did) | |
| 412 | + QI_EIOTLB_GRAN(QI_GRAN_NONG_PASID) | QI_EIOTLB_TYPE; |
| 413 | + desc.qw1 = 0; |
| 414 | + desc.qw2 = 0; |
| 415 | + desc.qw3 = 0; |
| 416 | + |
| 417 | + qi_submit_sync(&desc, iommu); |
| 418 | +} |
| 419 | + |
| 420 | +static void |
| 421 | +devtlb_invalidation_with_pasid(struct intel_iommu *iommu, |
| 422 | + struct device *dev, int pasid) |
| 423 | +{ |
| 424 | + struct device_domain_info *info; |
| 425 | + u16 sid, qdep, pfsid; |
| 426 | + |
| 427 | + info = dev->archdata.iommu; |
| 428 | + if (!info || !info->ats_enabled) |
| 429 | + return; |
| 430 | + |
| 431 | + sid = info->bus << 8 | info->devfn; |
| 432 | + qdep = info->ats_qdep; |
| 433 | + pfsid = info->pfsid; |
| 434 | + |
| 435 | + qi_flush_dev_iotlb(iommu, sid, pfsid, qdep, 0, 64 - VTD_PAGE_SHIFT); |
| 436 | +} |
| 437 | + |
| 438 | +void intel_pasid_tear_down_entry(struct intel_iommu *iommu, |
| 439 | + struct device *dev, int pasid) |
| 440 | +{ |
| 441 | + struct pasid_entry *pte; |
| 442 | + u16 did; |
| 443 | + |
| 444 | + pte = intel_pasid_get_entry(dev, pasid); |
| 445 | + if (WARN_ON(!pte)) |
| 446 | + return; |
| 447 | + |
| 448 | + intel_pasid_clear_entry(dev, pasid); |
| 449 | + did = pasid_get_domain_id(pte); |
| 450 | + |
| 451 | + if (!ecap_coherent(iommu->ecap)) |
| 452 | + clflush_cache_range(pte, sizeof(*pte)); |
| 453 | + |
| 454 | + pasid_cache_invalidation_with_pasid(iommu, did, pasid); |
| 455 | + iotlb_invalidation_with_pasid(iommu, did, pasid); |
| 456 | + |
| 457 | + /* Device IOTLB doesn't need to be flushed in caching mode. */ |
| 458 | + if (!cap_caching_mode(iommu->cap)) |
| 459 | + devtlb_invalidation_with_pasid(iommu, dev, pasid); |
| 460 | +} |
| 461 | + |
| 462 | +/* |
| 463 | + * Set up the scalable mode pasid entry for second only translation type. |
| 464 | + */ |
| 465 | +int intel_pasid_setup_second_level(struct intel_iommu *iommu, |
| 466 | + struct dmar_domain *domain, |
| 467 | + struct device *dev, int pasid) |
| 468 | +{ |
| 469 | + struct pasid_entry *pte; |
| 470 | + struct dma_pte *pgd; |
| 471 | + u64 pgd_val; |
| 472 | + int agaw; |
| 473 | + u16 did; |
| 474 | + |
| 475 | + /* |
| 476 | + * If hardware advertises no support for second level |
| 477 | + * translation, return directly. |
| 478 | + */ |
| 479 | + if (!ecap_slts(iommu->ecap)) { |
| 480 | + pr_err("No second level translation support on %s\n", |
| 481 | + iommu->name); |
| 482 | + return -EINVAL; |
| 483 | + } |
| 484 | + |
| 485 | + /* |
| 486 | + * Skip top levels of page tables for iommu which has less agaw |
| 487 | + * than default. Unnecessary for PT mode. |
| 488 | + */ |
| 489 | + pgd = domain->pgd; |
| 490 | + for (agaw = domain->agaw; agaw > iommu->agaw; agaw--) { |
| 491 | + pgd = phys_to_virt(dma_pte_addr(pgd)); |
| 492 | + if (!dma_pte_present(pgd)) { |
| 493 | + dev_err(dev, "Invalid domain page table\n"); |
| 494 | + return -EINVAL; |
| 495 | + } |
| 496 | + } |
| 497 | + |
| 498 | + pgd_val = virt_to_phys(pgd); |
| 499 | + did = domain->iommu_did[iommu->seq_id]; |
| 500 | + |
| 501 | + pte = intel_pasid_get_entry(dev, pasid); |
| 502 | + if (!pte) { |
| 503 | + dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid); |
| 504 | + return -ENODEV; |
| 505 | + } |
| 506 | + |
| 507 | + pasid_clear_entry(pte); |
| 508 | + pasid_set_domain_id(pte, did); |
| 509 | + pasid_set_slptr(pte, pgd_val); |
| 510 | + pasid_set_address_width(pte, agaw); |
| 511 | + pasid_set_translation_type(pte, 2); |
| 512 | + pasid_set_fault_enable(pte); |
| 513 | + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); |
| 514 | + |
| 515 | + /* |
| 516 | + * Since it is a second level only translation setup, we should |
| 517 | + * set SRE bit as well (addresses are expected to be GPAs). |
| 518 | + */ |
| 519 | + pasid_set_sre(pte); |
| 520 | + pasid_set_present(pte); |
| 521 | + |
| 522 | + if (!ecap_coherent(iommu->ecap)) |
| 523 | + clflush_cache_range(pte, sizeof(*pte)); |
| 524 | + |
| 525 | + if (cap_caching_mode(iommu->cap)) { |
| 526 | + pasid_cache_invalidation_with_pasid(iommu, did, pasid); |
| 527 | + iotlb_invalidation_with_pasid(iommu, did, pasid); |
| 528 | + } else { |
| 529 | + iommu_flush_write_buffer(iommu); |
| 530 | + } |
| 531 | + |
| 532 | + return 0; |
| 533 | +} |
| 534 | + |
| 535 | +/* |
| 536 | + * Set up the scalable mode pasid entry for passthrough translation type. |
| 537 | + */ |
| 538 | +int intel_pasid_setup_pass_through(struct intel_iommu *iommu, |
| 539 | + struct dmar_domain *domain, |
| 540 | + struct device *dev, int pasid) |
| 541 | +{ |
| 542 | + u16 did = FLPT_DEFAULT_DID; |
| 543 | + struct pasid_entry *pte; |
| 544 | + |
| 545 | + pte = intel_pasid_get_entry(dev, pasid); |
| 546 | + if (!pte) { |
| 547 | + dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid); |
| 548 | + return -ENODEV; |
| 549 | + } |
| 550 | + |
| 551 | + pasid_clear_entry(pte); |
| 552 | + pasid_set_domain_id(pte, did); |
| 553 | + pasid_set_address_width(pte, iommu->agaw); |
| 554 | + pasid_set_translation_type(pte, 4); |
| 555 | + pasid_set_fault_enable(pte); |
| 556 | + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); |
| 557 | + |
| 558 | + /* |
| 559 | + * We should set SRE bit as well since the addresses are expected |
| 560 | + * to be GPAs. |
| 561 | + */ |
| 562 | + pasid_set_sre(pte); |
| 563 | + pasid_set_present(pte); |
| 564 | + |
| 565 | + if (!ecap_coherent(iommu->ecap)) |
| 566 | + clflush_cache_range(pte, sizeof(*pte)); |
| 567 | + |
| 568 | + if (cap_caching_mode(iommu->cap)) { |
| 569 | + pasid_cache_invalidation_with_pasid(iommu, did, pasid); |
| 570 | + iotlb_invalidation_with_pasid(iommu, did, pasid); |
| 571 | + } else { |
| 572 | + iommu_flush_write_buffer(iommu); |
| 573 | + } |
| 574 | + |
| 575 | + return 0; |
| 576 | +} |
0 commit comments