1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc. 4 * Author: Joerg Roedel <jroedel@suse.de> 5 * Leo Duran <leo.duran@amd.com> 6 */ 7 8 #define pr_fmt(fmt) "AMD-Vi: " fmt 9 #define dev_fmt(fmt) pr_fmt(fmt) 10 11 #include <linux/ratelimit.h> 12 #include <linux/pci.h> 13 #include <linux/acpi.h> 14 #include <linux/pci-ats.h> 15 #include <linux/bitmap.h> 16 #include <linux/slab.h> 17 #include <linux/string_choices.h> 18 #include <linux/debugfs.h> 19 #include <linux/scatterlist.h> 20 #include <linux/dma-map-ops.h> 21 #include <linux/dma-direct.h> 22 #include <linux/idr.h> 23 #include <linux/iommu-helper.h> 24 #include <linux/delay.h> 25 #include <linux/amd-iommu.h> 26 #include <linux/notifier.h> 27 #include <linux/export.h> 28 #include <linux/irq.h> 29 #include <linux/irqchip/irq-msi-lib.h> 30 #include <linux/msi.h> 31 #include <linux/irqdomain.h> 32 #include <linux/percpu.h> 33 #include <linux/cc_platform.h> 34 #include <asm/irq_remapping.h> 35 #include <asm/io_apic.h> 36 #include <asm/apic.h> 37 #include <asm/hw_irq.h> 38 #include <asm/proto.h> 39 #include <asm/iommu.h> 40 #include <asm/gart.h> 41 #include <asm/dma.h> 42 #include <uapi/linux/iommufd.h> 43 #include <linux/generic_pt/iommu.h> 44 45 #include "amd_iommu.h" 46 #include "iommufd.h" 47 #include "../irq_remapping.h" 48 #include "../iommu-pages.h" 49 50 #define CMD_SET_TYPE(cmd, t) ((cmd)->data[1] |= ((t) << 28)) 51 52 /* Reserved IOVA ranges */ 53 #define MSI_RANGE_START (0xfee00000) 54 #define MSI_RANGE_END (0xfeefffff) 55 #define HT_RANGE_START (0xfd00000000ULL) 56 #define HT_RANGE_END (0xffffffffffULL) 57 58 LIST_HEAD(ioapic_map); 59 LIST_HEAD(hpet_map); 60 LIST_HEAD(acpihid_map); 61 62 const struct iommu_ops amd_iommu_ops; 63 64 int amd_iommu_max_glx_val = -1; 65 66 /* 67 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap 68 * to know which ones are already in use. 69 */ 70 DEFINE_IDA(pdom_ids); 71 72 static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev, 73 struct iommu_domain *old); 74 75 static void set_dte_entry(struct amd_iommu *iommu, 76 struct iommu_dev_data *dev_data, 77 phys_addr_t top_paddr, unsigned int top_level); 78 79 static int device_flush_dte(struct iommu_dev_data *dev_data); 80 81 static void amd_iommu_change_top(struct pt_iommu *iommu_table, 82 phys_addr_t top_paddr, unsigned int top_level); 83 84 static void iommu_flush_dte_sync(struct amd_iommu *iommu, u16 devid); 85 86 static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid); 87 static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain); 88 static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain, 89 bool enable); 90 91 static void clone_aliases(struct amd_iommu *iommu, struct device *dev); 92 93 static int iommu_completion_wait(struct amd_iommu *iommu); 94 95 static int __amd_iommu_complete_ppr(struct device *dev, u32 pasid, 96 int status, int tag, bool gn); 97 98 /**************************************************************************** 99 * 100 * Helper functions 101 * 102 ****************************************************************************/ 103 104 static __always_inline void amd_iommu_atomic128_set(__int128 *ptr, __int128 val) 105 { 106 /* 107 * Note: 108 * We use arch_cmpxchg128_local() because: 109 * - Need cmpxchg16b instruction mainly for 128-bit store to DTE 110 * (not necessary for cmpxchg since this function is already 111 * protected by a spin_lock for this DTE). 112 * - Neither need LOCK_PREFIX nor try loop because of the spin_lock. 113 */ 114 arch_cmpxchg128_local(ptr, *ptr, val); 115 } 116 117 static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_entry *new) 118 { 119 struct dev_table_entry old; 120 121 old.data128[1] = ptr->data128[1]; 122 /* 123 * Preserve DTE_DATA2_INTR_MASK. This needs to be 124 * done here since it requires to be inside 125 * spin_lock(&dev_data->dte_lock) context. 126 */ 127 new->data[2] &= ~DTE_DATA2_INTR_MASK; 128 new->data[2] |= old.data[2] & DTE_DATA2_INTR_MASK; 129 130 amd_iommu_atomic128_set(&ptr->data128[1], new->data128[1]); 131 } 132 133 static void write_dte_lower128(struct dev_table_entry *ptr, struct dev_table_entry *new) 134 { 135 amd_iommu_atomic128_set(&ptr->data128[0], new->data128[0]); 136 } 137 138 /* 139 * Note: 140 * IOMMU reads the entire Device Table entry in a single 256-bit transaction 141 * but the driver is programming DTE using 2 128-bit cmpxchg. So, the driver 142 * need to ensure the following: 143 * - DTE[V|GV] bit is being written last when setting. 144 * - DTE[V|GV] bit is being written first when clearing. 145 * 146 * This function is used only by code, which updates DMA translation part of the DTE. 147 * So, only consider control bits related to DMA when updating the entry. 148 */ 149 static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data, 150 struct dev_table_entry *new) 151 { 152 unsigned long flags; 153 struct dev_table_entry *dev_table = get_dev_table(iommu); 154 struct dev_table_entry *ptr = &dev_table[dev_data->devid]; 155 156 spin_lock_irqsave(&dev_data->dte_lock, flags); 157 158 if (!(ptr->data[0] & DTE_FLAG_V)) { 159 /* Existing DTE is not valid. */ 160 write_dte_upper128(ptr, new); 161 write_dte_lower128(ptr, new); 162 iommu_flush_dte_sync(iommu, dev_data->devid); 163 } else if (!(new->data[0] & DTE_FLAG_V)) { 164 /* Existing DTE is valid. New DTE is not valid. */ 165 write_dte_lower128(ptr, new); 166 write_dte_upper128(ptr, new); 167 iommu_flush_dte_sync(iommu, dev_data->devid); 168 } else if (!FIELD_GET(DTE_FLAG_GV, ptr->data[0])) { 169 /* 170 * Both DTEs are valid. 171 * Existing DTE has no guest page table. 172 */ 173 write_dte_upper128(ptr, new); 174 write_dte_lower128(ptr, new); 175 iommu_flush_dte_sync(iommu, dev_data->devid); 176 } else if (!FIELD_GET(DTE_FLAG_GV, new->data[0])) { 177 /* 178 * Both DTEs are valid. 179 * Existing DTE has guest page table, 180 * new DTE has no guest page table, 181 */ 182 write_dte_lower128(ptr, new); 183 write_dte_upper128(ptr, new); 184 iommu_flush_dte_sync(iommu, dev_data->devid); 185 } else if (FIELD_GET(DTE_GPT_LEVEL_MASK, ptr->data[2]) != 186 FIELD_GET(DTE_GPT_LEVEL_MASK, new->data[2])) { 187 /* 188 * Both DTEs are valid and have guest page table, 189 * but have different number of levels. So, we need 190 * to upadte both upper and lower 128-bit value, which 191 * require disabling and flushing. 192 */ 193 struct dev_table_entry clear = {}; 194 195 /* First disable DTE */ 196 write_dte_lower128(ptr, &clear); 197 iommu_flush_dte_sync(iommu, dev_data->devid); 198 199 /* Then update DTE */ 200 write_dte_upper128(ptr, new); 201 write_dte_lower128(ptr, new); 202 iommu_flush_dte_sync(iommu, dev_data->devid); 203 } else { 204 /* 205 * Both DTEs are valid and have guest page table, 206 * and same number of levels. We just need to only 207 * update the lower 128-bit. So no need to disable DTE. 208 */ 209 write_dte_lower128(ptr, new); 210 } 211 212 spin_unlock_irqrestore(&dev_data->dte_lock, flags); 213 } 214 215 void amd_iommu_update_dte(struct amd_iommu *iommu, 216 struct iommu_dev_data *dev_data, 217 struct dev_table_entry *new) 218 { 219 update_dte256(iommu, dev_data, new); 220 clone_aliases(iommu, dev_data->dev); 221 device_flush_dte(dev_data); 222 iommu_completion_wait(iommu); 223 } 224 225 static void get_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data, 226 struct dev_table_entry *dte) 227 { 228 unsigned long flags; 229 struct dev_table_entry *ptr; 230 struct dev_table_entry *dev_table = get_dev_table(iommu); 231 232 ptr = &dev_table[dev_data->devid]; 233 234 spin_lock_irqsave(&dev_data->dte_lock, flags); 235 dte->data128[0] = ptr->data128[0]; 236 dte->data128[1] = ptr->data128[1]; 237 spin_unlock_irqrestore(&dev_data->dte_lock, flags); 238 } 239 240 static inline bool pdom_is_v2_pgtbl_mode(struct protection_domain *pdom) 241 { 242 return (pdom && (pdom->pd_mode == PD_MODE_V2)); 243 } 244 245 static inline bool pdom_is_in_pt_mode(struct protection_domain *pdom) 246 { 247 return (pdom->domain.type == IOMMU_DOMAIN_IDENTITY); 248 } 249 250 /* 251 * We cannot support PASID w/ existing v1 page table in the same domain 252 * since it will be nested. However, existing domain w/ v2 page table 253 * or passthrough mode can be used for PASID. 254 */ 255 static inline bool pdom_is_sva_capable(struct protection_domain *pdom) 256 { 257 return pdom_is_v2_pgtbl_mode(pdom) || pdom_is_in_pt_mode(pdom); 258 } 259 260 static inline int get_acpihid_device_id(struct device *dev, 261 struct acpihid_map_entry **entry) 262 { 263 struct acpi_device *adev = ACPI_COMPANION(dev); 264 struct acpihid_map_entry *p, *p1 = NULL; 265 int hid_count = 0; 266 bool fw_bug; 267 268 if (!adev) 269 return -ENODEV; 270 271 list_for_each_entry(p, &acpihid_map, list) { 272 if (acpi_dev_hid_uid_match(adev, p->hid, 273 p->uid[0] ? p->uid : NULL)) { 274 p1 = p; 275 fw_bug = false; 276 hid_count = 1; 277 break; 278 } 279 280 /* 281 * Count HID matches w/o UID, raise FW_BUG but allow exactly one match 282 */ 283 if (acpi_dev_hid_match(adev, p->hid)) { 284 p1 = p; 285 hid_count++; 286 fw_bug = true; 287 } 288 } 289 290 if (!p1) 291 return -EINVAL; 292 if (fw_bug) 293 dev_err_once(dev, FW_BUG "No ACPI device matched UID, but %d device%s matched HID.\n", 294 hid_count, str_plural(hid_count)); 295 if (hid_count > 1) 296 return -EINVAL; 297 if (entry) 298 *entry = p1; 299 300 return p1->devid; 301 } 302 303 static inline int get_device_sbdf_id(struct device *dev) 304 { 305 int sbdf; 306 307 if (dev_is_pci(dev)) 308 sbdf = get_pci_sbdf_id(to_pci_dev(dev)); 309 else 310 sbdf = get_acpihid_device_id(dev, NULL); 311 312 return sbdf; 313 } 314 315 struct dev_table_entry *get_dev_table(struct amd_iommu *iommu) 316 { 317 struct dev_table_entry *dev_table; 318 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; 319 320 BUG_ON(pci_seg == NULL); 321 dev_table = pci_seg->dev_table; 322 BUG_ON(dev_table == NULL); 323 324 return dev_table; 325 } 326 327 static inline u16 get_device_segment(struct device *dev) 328 { 329 u16 seg; 330 331 if (dev_is_pci(dev)) { 332 struct pci_dev *pdev = to_pci_dev(dev); 333 334 seg = pci_domain_nr(pdev->bus); 335 } else { 336 u32 devid = get_acpihid_device_id(dev, NULL); 337 338 seg = PCI_SBDF_TO_SEGID(devid); 339 } 340 341 return seg; 342 } 343 344 /* Writes the specific IOMMU for a device into the PCI segment rlookup table */ 345 void amd_iommu_set_rlookup_table(struct amd_iommu *iommu, u16 devid) 346 { 347 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; 348 349 pci_seg->rlookup_table[devid] = iommu; 350 } 351 352 static struct amd_iommu *__rlookup_amd_iommu(u16 seg, u16 devid) 353 { 354 struct amd_iommu_pci_seg *pci_seg; 355 356 for_each_pci_segment(pci_seg) { 357 if (pci_seg->id != seg) 358 continue; 359 /* IVRS may not describe every device on the bus */ 360 if (devid > pci_seg->last_bdf) 361 return NULL; 362 return pci_seg->rlookup_table[devid]; 363 } 364 return NULL; 365 } 366 367 static struct amd_iommu *rlookup_amd_iommu(struct device *dev) 368 { 369 u16 seg = get_device_segment(dev); 370 int devid = get_device_sbdf_id(dev); 371 372 if (devid < 0) 373 return NULL; 374 return __rlookup_amd_iommu(seg, PCI_SBDF_TO_DEVID(devid)); 375 } 376 377 static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid) 378 { 379 struct iommu_dev_data *dev_data; 380 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; 381 382 dev_data = kzalloc_obj(*dev_data); 383 if (!dev_data) 384 return NULL; 385 386 mutex_init(&dev_data->mutex); 387 spin_lock_init(&dev_data->dte_lock); 388 dev_data->devid = devid; 389 ratelimit_default_init(&dev_data->rs); 390 391 llist_add(&dev_data->dev_data_list, &pci_seg->dev_data_list); 392 return dev_data; 393 } 394 395 struct iommu_dev_data *search_dev_data(struct amd_iommu *iommu, u16 devid) 396 { 397 struct iommu_dev_data *dev_data; 398 struct llist_node *node; 399 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; 400 401 if (llist_empty(&pci_seg->dev_data_list)) 402 return NULL; 403 404 node = pci_seg->dev_data_list.first; 405 llist_for_each_entry(dev_data, node, dev_data_list) { 406 if (dev_data->devid == devid) 407 return dev_data; 408 } 409 410 return NULL; 411 } 412 413 static int clone_alias(struct pci_dev *pdev_origin, u16 alias, void *data) 414 { 415 struct dev_table_entry new; 416 struct amd_iommu *iommu; 417 struct iommu_dev_data *dev_data, *alias_data; 418 struct pci_dev *pdev = data; 419 u16 devid = pci_dev_id(pdev); 420 int ret = 0; 421 422 if (devid == alias) 423 return 0; 424 425 iommu = rlookup_amd_iommu(&pdev->dev); 426 if (!iommu) 427 return 0; 428 429 /* Copy the data from pdev */ 430 dev_data = dev_iommu_priv_get(&pdev->dev); 431 if (!dev_data) { 432 pr_err("%s : Failed to get dev_data for 0x%x\n", __func__, devid); 433 ret = -EINVAL; 434 goto out; 435 } 436 get_dte256(iommu, dev_data, &new); 437 438 /* Setup alias */ 439 alias_data = find_dev_data(iommu, alias); 440 if (!alias_data) { 441 pr_err("%s : Failed to get alias dev_data for 0x%x\n", __func__, alias); 442 ret = -EINVAL; 443 goto out; 444 } 445 update_dte256(iommu, alias_data, &new); 446 447 amd_iommu_set_rlookup_table(iommu, alias); 448 out: 449 return ret; 450 } 451 452 static void clone_aliases(struct amd_iommu *iommu, struct device *dev) 453 { 454 struct pci_dev *pdev; 455 456 if (!dev_is_pci(dev)) 457 return; 458 pdev = to_pci_dev(dev); 459 460 /* 461 * The IVRS alias stored in the alias table may not be 462 * part of the PCI DMA aliases if its bus differs 463 * from the original device. 464 */ 465 clone_alias(pdev, iommu->pci_seg->alias_table[pci_dev_id(pdev)], pdev); 466 467 pci_for_each_dma_alias(pdev, clone_alias, pdev); 468 } 469 470 static void setup_aliases(struct amd_iommu *iommu, struct device *dev) 471 { 472 struct pci_dev *pdev = to_pci_dev(dev); 473 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; 474 u16 ivrs_alias; 475 476 /* For ACPI HID devices, there are no aliases */ 477 if (!dev_is_pci(dev)) 478 return; 479 480 /* 481 * Add the IVRS alias to the pci aliases if it is on the same 482 * bus. The IVRS table may know about a quirk that we don't. 483 */ 484 ivrs_alias = pci_seg->alias_table[pci_dev_id(pdev)]; 485 if (ivrs_alias != pci_dev_id(pdev) && 486 PCI_BUS_NUM(ivrs_alias) == pdev->bus->number) 487 pci_add_dma_alias(pdev, ivrs_alias & 0xff, 1); 488 489 clone_aliases(iommu, dev); 490 } 491 492 static struct iommu_dev_data *find_dev_data(struct amd_iommu *iommu, u16 devid) 493 { 494 struct iommu_dev_data *dev_data; 495 496 dev_data = search_dev_data(iommu, devid); 497 498 if (dev_data == NULL) { 499 dev_data = alloc_dev_data(iommu, devid); 500 if (!dev_data) 501 return NULL; 502 503 if (translation_pre_enabled(iommu)) 504 dev_data->defer_attach = true; 505 } 506 507 return dev_data; 508 } 509 510 /* 511 * Find or create an IOMMU group for an acpihid device. 512 */ 513 static struct iommu_group *acpihid_device_group(struct device *dev) 514 { 515 struct acpihid_map_entry *p, *entry = NULL; 516 int devid; 517 518 devid = get_acpihid_device_id(dev, &entry); 519 if (devid < 0) 520 return ERR_PTR(devid); 521 522 list_for_each_entry(p, &acpihid_map, list) { 523 if ((devid == p->devid) && p->group) 524 entry->group = p->group; 525 } 526 527 if (!entry->group) 528 entry->group = generic_device_group(dev); 529 else 530 iommu_group_ref_get(entry->group); 531 532 return entry->group; 533 } 534 535 static inline bool pdev_pasid_supported(struct iommu_dev_data *dev_data) 536 { 537 return (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP); 538 } 539 540 static u32 pdev_get_caps(struct pci_dev *pdev) 541 { 542 int features; 543 u32 flags = 0; 544 545 if (pci_ats_supported(pdev)) 546 flags |= AMD_IOMMU_DEVICE_FLAG_ATS_SUP; 547 548 if (pci_pri_supported(pdev)) 549 flags |= AMD_IOMMU_DEVICE_FLAG_PRI_SUP; 550 551 features = pci_pasid_features(pdev); 552 if (features >= 0) { 553 flags |= AMD_IOMMU_DEVICE_FLAG_PASID_SUP; 554 555 if (features & PCI_PASID_CAP_EXEC) 556 flags |= AMD_IOMMU_DEVICE_FLAG_EXEC_SUP; 557 558 if (features & PCI_PASID_CAP_PRIV) 559 flags |= AMD_IOMMU_DEVICE_FLAG_PRIV_SUP; 560 } 561 562 return flags; 563 } 564 565 static inline int pdev_enable_cap_ats(struct pci_dev *pdev) 566 { 567 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 568 int ret = -EINVAL; 569 570 if (dev_data->ats_enabled) 571 return 0; 572 573 if (amd_iommu_iotlb_sup && 574 (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP)) { 575 ret = pci_enable_ats(pdev, PAGE_SHIFT); 576 if (!ret) { 577 dev_data->ats_enabled = 1; 578 dev_data->ats_qdep = pci_ats_queue_depth(pdev); 579 } 580 } 581 582 return ret; 583 } 584 585 static inline void pdev_disable_cap_ats(struct pci_dev *pdev) 586 { 587 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 588 589 if (dev_data->ats_enabled) { 590 pci_disable_ats(pdev); 591 dev_data->ats_enabled = 0; 592 } 593 } 594 595 static inline int pdev_enable_cap_pri(struct pci_dev *pdev) 596 { 597 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 598 int ret = -EINVAL; 599 600 if (dev_data->pri_enabled) 601 return 0; 602 603 if (!dev_data->ats_enabled) 604 return 0; 605 606 if (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) { 607 /* 608 * First reset the PRI state of the device. 609 * FIXME: Hardcode number of outstanding requests for now 610 */ 611 if (!pci_reset_pri(pdev) && !pci_enable_pri(pdev, 32)) { 612 dev_data->pri_enabled = 1; 613 dev_data->pri_tlp = pci_prg_resp_pasid_required(pdev); 614 615 ret = 0; 616 } 617 } 618 619 return ret; 620 } 621 622 static inline void pdev_disable_cap_pri(struct pci_dev *pdev) 623 { 624 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 625 626 if (dev_data->pri_enabled) { 627 pci_disable_pri(pdev); 628 dev_data->pri_enabled = 0; 629 } 630 } 631 632 static inline int pdev_enable_cap_pasid(struct pci_dev *pdev) 633 { 634 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 635 int ret = -EINVAL; 636 637 if (dev_data->pasid_enabled) 638 return 0; 639 640 if (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) { 641 /* Only allow access to user-accessible pages */ 642 ret = pci_enable_pasid(pdev, 0); 643 if (!ret) 644 dev_data->pasid_enabled = 1; 645 } 646 647 return ret; 648 } 649 650 static inline void pdev_disable_cap_pasid(struct pci_dev *pdev) 651 { 652 struct iommu_dev_data *dev_data = dev_iommu_priv_get(&pdev->dev); 653 654 if (dev_data->pasid_enabled) { 655 pci_disable_pasid(pdev); 656 dev_data->pasid_enabled = 0; 657 } 658 } 659 660 static void pdev_enable_caps(struct pci_dev *pdev) 661 { 662 pdev_enable_cap_pasid(pdev); 663 pdev_enable_cap_ats(pdev); 664 pdev_enable_cap_pri(pdev); 665 } 666 667 static void pdev_disable_caps(struct pci_dev *pdev) 668 { 669 pdev_disable_cap_ats(pdev); 670 pdev_disable_cap_pasid(pdev); 671 pdev_disable_cap_pri(pdev); 672 } 673 674 /* 675 * This function checks if the driver got a valid device from the caller to 676 * avoid dereferencing invalid pointers. 677 */ 678 static bool check_device(struct device *dev) 679 { 680 struct amd_iommu_pci_seg *pci_seg; 681 struct amd_iommu *iommu; 682 int devid, sbdf; 683 684 if (!dev) 685 return false; 686 687 sbdf = get_device_sbdf_id(dev); 688 if (sbdf < 0) 689 return false; 690 devid = PCI_SBDF_TO_DEVID(sbdf); 691 692 iommu = rlookup_amd_iommu(dev); 693 if (!iommu) 694 return false; 695 696 /* Out of our scope? */ 697 pci_seg = iommu->pci_seg; 698 if (devid > pci_seg->last_bdf) 699 return false; 700 701 return true; 702 } 703 704 static int iommu_init_device(struct amd_iommu *iommu, struct device *dev) 705 { 706 struct iommu_dev_data *dev_data; 707 int devid, sbdf; 708 709 if (dev_iommu_priv_get(dev)) 710 return 0; 711 712 sbdf = get_device_sbdf_id(dev); 713 if (sbdf < 0) 714 return sbdf; 715 716 devid = PCI_SBDF_TO_DEVID(sbdf); 717 dev_data = find_dev_data(iommu, devid); 718 if (!dev_data) 719 return -ENOMEM; 720 721 dev_data->dev = dev; 722 723 /* 724 * The dev_iommu_priv_set() needes to be called before setup_aliases. 725 * Otherwise, subsequent call to dev_iommu_priv_get() will fail. 726 */ 727 dev_iommu_priv_set(dev, dev_data); 728 setup_aliases(iommu, dev); 729 730 /* 731 * By default we use passthrough mode for IOMMUv2 capable device. 732 * But if amd_iommu=force_isolation is set (e.g. to debug DMA to 733 * invalid address), we ignore the capability for the device so 734 * it'll be forced to go into translation mode. 735 */ 736 if ((iommu_default_passthrough() || !amd_iommu_force_isolation) && 737 dev_is_pci(dev) && amd_iommu_gt_ppr_supported()) { 738 dev_data->flags = pdev_get_caps(to_pci_dev(dev)); 739 } 740 741 return 0; 742 } 743 744 static void iommu_ignore_device(struct amd_iommu *iommu, struct device *dev) 745 { 746 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; 747 struct dev_table_entry *dev_table = get_dev_table(iommu); 748 int devid, sbdf; 749 750 sbdf = get_device_sbdf_id(dev); 751 if (sbdf < 0) 752 return; 753 754 devid = PCI_SBDF_TO_DEVID(sbdf); 755 pci_seg->rlookup_table[devid] = NULL; 756 memset(&dev_table[devid], 0, sizeof(struct dev_table_entry)); 757 758 setup_aliases(iommu, dev); 759 } 760 761 762 /**************************************************************************** 763 * 764 * Interrupt handling functions 765 * 766 ****************************************************************************/ 767 768 static void dump_dte_entry(struct amd_iommu *iommu, u16 devid) 769 { 770 int i; 771 struct dev_table_entry dte; 772 struct iommu_dev_data *dev_data = find_dev_data(iommu, devid); 773 774 get_dte256(iommu, dev_data, &dte); 775 776 for (i = 0; i < 4; ++i) 777 pr_err("DTE[%d]: %016llx\n", i, dte.data[i]); 778 } 779 780 static void dump_command(unsigned long phys_addr) 781 { 782 struct iommu_cmd *cmd = iommu_phys_to_virt(phys_addr); 783 int i; 784 785 for (i = 0; i < 4; ++i) 786 pr_err("CMD[%d]: %08x\n", i, cmd->data[i]); 787 } 788 789 static void amd_iommu_report_rmp_hw_error(struct amd_iommu *iommu, volatile u32 *event) 790 { 791 struct iommu_dev_data *dev_data = NULL; 792 int devid, vmg_tag, flags; 793 struct pci_dev *pdev; 794 u64 spa; 795 796 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK; 797 vmg_tag = (event[1]) & 0xFFFF; 798 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK; 799 spa = ((u64)event[3] << 32) | (event[2] & 0xFFFFFFF8); 800 801 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid), 802 devid & 0xff); 803 if (pdev) 804 dev_data = dev_iommu_priv_get(&pdev->dev); 805 806 if (dev_data) { 807 if (__ratelimit(&dev_data->rs)) { 808 pci_err(pdev, "Event logged [RMP_HW_ERROR vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n", 809 vmg_tag, spa, flags); 810 } 811 } else { 812 pr_err_ratelimited("Event logged [RMP_HW_ERROR device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, spa=0x%llx, flags=0x%04x]\n", 813 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 814 vmg_tag, spa, flags); 815 } 816 817 if (pdev) 818 pci_dev_put(pdev); 819 } 820 821 static void amd_iommu_report_rmp_fault(struct amd_iommu *iommu, volatile u32 *event) 822 { 823 struct iommu_dev_data *dev_data = NULL; 824 int devid, flags_rmp, vmg_tag, flags; 825 struct pci_dev *pdev; 826 u64 gpa; 827 828 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK; 829 flags_rmp = (event[0] >> EVENT_FLAGS_SHIFT) & 0xFF; 830 vmg_tag = (event[1]) & 0xFFFF; 831 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK; 832 gpa = ((u64)event[3] << 32) | event[2]; 833 834 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid), 835 devid & 0xff); 836 if (pdev) 837 dev_data = dev_iommu_priv_get(&pdev->dev); 838 839 if (dev_data) { 840 if (__ratelimit(&dev_data->rs)) { 841 pci_err(pdev, "Event logged [RMP_PAGE_FAULT vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n", 842 vmg_tag, gpa, flags_rmp, flags); 843 } 844 } else { 845 pr_err_ratelimited("Event logged [RMP_PAGE_FAULT device=%04x:%02x:%02x.%x, vmg_tag=0x%04x, gpa=0x%llx, flags_rmp=0x%04x, flags=0x%04x]\n", 846 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 847 vmg_tag, gpa, flags_rmp, flags); 848 } 849 850 if (pdev) 851 pci_dev_put(pdev); 852 } 853 854 #define IS_IOMMU_MEM_TRANSACTION(flags) \ 855 (((flags) & EVENT_FLAG_I) == 0) 856 857 #define IS_WRITE_REQUEST(flags) \ 858 ((flags) & EVENT_FLAG_RW) 859 860 static void amd_iommu_report_page_fault(struct amd_iommu *iommu, 861 u16 devid, u16 domain_id, 862 u64 address, int flags) 863 { 864 struct iommu_dev_data *dev_data = NULL; 865 struct pci_dev *pdev; 866 867 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid), 868 devid & 0xff); 869 if (pdev) 870 dev_data = dev_iommu_priv_get(&pdev->dev); 871 872 if (dev_data) { 873 /* 874 * If this is a DMA fault (for which the I(nterrupt) 875 * bit will be unset), allow report_iommu_fault() to 876 * prevent logging it. 877 */ 878 if (IS_IOMMU_MEM_TRANSACTION(flags)) { 879 /* Device not attached to domain properly */ 880 if (dev_data->domain == NULL) { 881 pr_err_ratelimited("Event logged [Device not attached to domain properly]\n"); 882 pr_err_ratelimited(" device=%04x:%02x:%02x.%x domain=0x%04x\n", 883 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), 884 PCI_FUNC(devid), domain_id); 885 goto out; 886 } 887 888 if (!report_iommu_fault(&dev_data->domain->domain, 889 &pdev->dev, address, 890 IS_WRITE_REQUEST(flags) ? 891 IOMMU_FAULT_WRITE : 892 IOMMU_FAULT_READ)) 893 goto out; 894 } 895 896 if (__ratelimit(&dev_data->rs)) { 897 pci_err(pdev, "Event logged [IO_PAGE_FAULT domain=0x%04x address=0x%llx flags=0x%04x]\n", 898 domain_id, address, flags); 899 } 900 } else { 901 pr_err_ratelimited("Event logged [IO_PAGE_FAULT device=%04x:%02x:%02x.%x domain=0x%04x address=0x%llx flags=0x%04x]\n", 902 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 903 domain_id, address, flags); 904 } 905 906 out: 907 if (pdev) 908 pci_dev_put(pdev); 909 } 910 911 static void amd_iommu_report_ppr_err(struct amd_iommu *iommu, volatile u32 *event, 912 u16 devid, u64 address, int flags) 913 { 914 struct pci_dev *pdev; 915 struct device *dev = iommu->iommu.dev; 916 u32 pasid = PPR_PASID(*((u64 *)event)); 917 int tag = event[1] & 0x03FF; 918 bool gn; 919 920 dev_err_ratelimited(dev, "Event logged [INVALID_PPR_REQUEST device=%04x:%02x:%02x.%x " 921 "pasid=0x%05x address=0x%llx flags=0x%04x tag=0x%03x]\n", 922 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), 923 PCI_FUNC(devid), pasid, address, flags, tag); 924 925 /* Skip COMPLETE_PPR_REQUEST response if RX=1 */ 926 if (flags & EVENT_FLAG_PPR_RX) 927 return; 928 929 pdev = pci_get_domain_bus_and_slot(iommu->pci_seg->id, PCI_BUS_NUM(devid), 930 devid & 0xff); 931 if (!pdev) 932 return; 933 934 if (!dev_iommu_priv_get(&pdev->dev)) { 935 pci_dev_put(pdev); 936 return; 937 } 938 939 gn = (flags & EVENT_FLAG_PPR_GN); 940 941 __amd_iommu_complete_ppr(&pdev->dev, pasid, IOMMU_PAGE_RESP_FAILURE, tag, gn); 942 pci_dev_put(pdev); 943 } 944 945 static void iommu_print_event(struct amd_iommu *iommu, void *__evt) 946 { 947 struct device *dev = iommu->iommu.dev; 948 int type, devid, flags; 949 volatile u32 *event = __evt; 950 int count = 0; 951 u64 address, ctrl; 952 u32 pasid; 953 954 retry: 955 type = (event[1] >> EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK; 956 devid = (event[0] >> EVENT_DEVID_SHIFT) & EVENT_DEVID_MASK; 957 pasid = (event[0] & EVENT_DOMID_MASK_HI) | 958 (event[1] & EVENT_DOMID_MASK_LO); 959 flags = (event[1] >> EVENT_FLAGS_SHIFT) & EVENT_FLAGS_MASK; 960 address = (u64)(((u64)event[3]) << 32) | event[2]; 961 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET); 962 963 if (type == 0) { 964 /* Did we hit the erratum? */ 965 if (++count == LOOP_TIMEOUT) { 966 pr_err("No event written to event log\n"); 967 return; 968 } 969 udelay(1); 970 goto retry; 971 } 972 973 if (type == EVENT_TYPE_IO_FAULT) { 974 amd_iommu_report_page_fault(iommu, devid, pasid, address, flags); 975 return; 976 } 977 978 switch (type) { 979 case EVENT_TYPE_ILL_DEV: 980 dev_err(dev, "Event logged [ILLEGAL_DEV_TABLE_ENTRY device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n", 981 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 982 pasid, address, flags); 983 dev_err(dev, "Control Reg : 0x%llx\n", ctrl); 984 dump_dte_entry(iommu, devid); 985 break; 986 case EVENT_TYPE_DEV_TAB_ERR: 987 dev_err(dev, "Event logged [DEV_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x " 988 "address=0x%llx flags=0x%04x]\n", 989 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 990 address, flags); 991 break; 992 case EVENT_TYPE_PAGE_TAB_ERR: 993 dev_err(dev, "Event logged [PAGE_TAB_HARDWARE_ERROR device=%04x:%02x:%02x.%x pasid=0x%04x address=0x%llx flags=0x%04x]\n", 994 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 995 pasid, address, flags); 996 break; 997 case EVENT_TYPE_ILL_CMD: 998 dev_err(dev, "Event logged [ILLEGAL_COMMAND_ERROR address=0x%llx]\n", address); 999 dump_command(address); 1000 break; 1001 case EVENT_TYPE_CMD_HARD_ERR: 1002 dev_err(dev, "Event logged [COMMAND_HARDWARE_ERROR address=0x%llx flags=0x%04x]\n", 1003 address, flags); 1004 break; 1005 case EVENT_TYPE_IOTLB_INV_TO: 1006 dev_err(dev, "Event logged [IOTLB_INV_TIMEOUT device=%04x:%02x:%02x.%x address=0x%llx]\n", 1007 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 1008 address); 1009 break; 1010 case EVENT_TYPE_INV_DEV_REQ: 1011 dev_err(dev, "Event logged [INVALID_DEVICE_REQUEST device=%04x:%02x:%02x.%x pasid=0x%05x address=0x%llx flags=0x%04x]\n", 1012 iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid), PCI_FUNC(devid), 1013 pasid, address, flags); 1014 break; 1015 case EVENT_TYPE_RMP_FAULT: 1016 amd_iommu_report_rmp_fault(iommu, event); 1017 break; 1018 case EVENT_TYPE_RMP_HW_ERR: 1019 amd_iommu_report_rmp_hw_error(iommu, event); 1020 break; 1021 case EVENT_TYPE_INV_PPR_REQ: 1022 amd_iommu_report_ppr_err(iommu, event, devid, address, flags); 1023 break; 1024 default: 1025 dev_err(dev, "Event logged [UNKNOWN event[0]=0x%08x event[1]=0x%08x event[2]=0x%08x event[3]=0x%08x\n", 1026 event[0], event[1], event[2], event[3]); 1027 } 1028 1029 /* 1030 * To detect the hardware errata 732 we need to clear the 1031 * entry back to zero. This issue does not exist on SNP 1032 * enabled system. Also this buffer is not writeable on 1033 * SNP enabled system. 1034 */ 1035 if (!amd_iommu_snp_en) 1036 memset(__evt, 0, 4 * sizeof(u32)); 1037 } 1038 1039 static void iommu_poll_events(struct amd_iommu *iommu) 1040 { 1041 u32 head, tail; 1042 1043 head = readl(iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); 1044 tail = readl(iommu->mmio_base + MMIO_EVT_TAIL_OFFSET); 1045 1046 while (head != tail) { 1047 iommu_print_event(iommu, iommu->evt_buf + head); 1048 1049 /* Update head pointer of hardware ring-buffer */ 1050 head = (head + EVTLOG_ENTRY_SIZE) % amd_iommu_evtlog_size; 1051 writel(head, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET); 1052 } 1053 1054 } 1055 1056 #ifdef CONFIG_IRQ_REMAP 1057 static int (*iommu_ga_log_notifier)(u32); 1058 1059 int amd_iommu_register_ga_log_notifier(int (*notifier)(u32)) 1060 { 1061 iommu_ga_log_notifier = notifier; 1062 1063 /* 1064 * Ensure all in-flight IRQ handlers run to completion before returning 1065 * to the caller, e.g. to ensure module code isn't unloaded while it's 1066 * being executed in the IRQ handler. 1067 */ 1068 if (!notifier) 1069 synchronize_rcu(); 1070 1071 return 0; 1072 } 1073 EXPORT_SYMBOL(amd_iommu_register_ga_log_notifier); 1074 1075 static void iommu_poll_ga_log(struct amd_iommu *iommu) 1076 { 1077 u32 head, tail; 1078 1079 if (iommu->ga_log == NULL) 1080 return; 1081 1082 head = readl(iommu->mmio_base + MMIO_GA_HEAD_OFFSET); 1083 tail = readl(iommu->mmio_base + MMIO_GA_TAIL_OFFSET); 1084 1085 while (head != tail) { 1086 volatile u64 *raw; 1087 u64 log_entry; 1088 1089 raw = (u64 *)(iommu->ga_log + head); 1090 1091 /* Avoid memcpy function-call overhead */ 1092 log_entry = *raw; 1093 1094 /* Update head pointer of hardware ring-buffer */ 1095 head = (head + GA_ENTRY_SIZE) % GA_LOG_SIZE; 1096 writel(head, iommu->mmio_base + MMIO_GA_HEAD_OFFSET); 1097 1098 /* Handle GA entry */ 1099 switch (GA_REQ_TYPE(log_entry)) { 1100 case GA_GUEST_NR: 1101 if (!iommu_ga_log_notifier) 1102 break; 1103 1104 pr_debug("%s: devid=%#x, ga_tag=%#x\n", 1105 __func__, GA_DEVID(log_entry), 1106 GA_TAG(log_entry)); 1107 1108 if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0) 1109 pr_err("GA log notifier failed.\n"); 1110 break; 1111 default: 1112 break; 1113 } 1114 } 1115 } 1116 1117 static void 1118 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) 1119 { 1120 if (!irq_remapping_enabled || !dev_is_pci(dev) || 1121 !pci_dev_has_default_msi_parent_domain(to_pci_dev(dev))) 1122 return; 1123 1124 dev_set_msi_domain(dev, iommu->ir_domain); 1125 } 1126 1127 #else /* CONFIG_IRQ_REMAP */ 1128 static inline void 1129 amd_iommu_set_pci_msi_domain(struct device *dev, struct amd_iommu *iommu) { } 1130 #endif /* !CONFIG_IRQ_REMAP */ 1131 1132 static void amd_iommu_handle_irq(void *data, const char *evt_type, 1133 u32 int_mask, u32 overflow_mask, 1134 void (*int_handler)(struct amd_iommu *), 1135 void (*overflow_handler)(struct amd_iommu *)) 1136 { 1137 struct amd_iommu *iommu = (struct amd_iommu *) data; 1138 u32 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET); 1139 u32 mask = int_mask | overflow_mask; 1140 1141 while (status & mask) { 1142 /* Enable interrupt sources again */ 1143 writel(mask, iommu->mmio_base + MMIO_STATUS_OFFSET); 1144 1145 if (int_handler) { 1146 pr_devel("Processing IOMMU (ivhd%d) %s Log\n", 1147 iommu->index, evt_type); 1148 int_handler(iommu); 1149 } 1150 1151 if ((status & overflow_mask) && overflow_handler) 1152 overflow_handler(iommu); 1153 1154 /* 1155 * Hardware bug: ERBT1312 1156 * When re-enabling interrupt (by writing 1 1157 * to clear the bit), the hardware might also try to set 1158 * the interrupt bit in the event status register. 1159 * In this scenario, the bit will be set, and disable 1160 * subsequent interrupts. 1161 * 1162 * Workaround: The IOMMU driver should read back the 1163 * status register and check if the interrupt bits are cleared. 1164 * If not, driver will need to go through the interrupt handler 1165 * again and re-clear the bits 1166 */ 1167 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET); 1168 } 1169 } 1170 1171 irqreturn_t amd_iommu_int_thread_evtlog(int irq, void *data) 1172 { 1173 amd_iommu_handle_irq(data, "Evt", MMIO_STATUS_EVT_INT_MASK, 1174 MMIO_STATUS_EVT_OVERFLOW_MASK, 1175 iommu_poll_events, amd_iommu_restart_event_logging); 1176 1177 return IRQ_HANDLED; 1178 } 1179 1180 irqreturn_t amd_iommu_int_thread_pprlog(int irq, void *data) 1181 { 1182 amd_iommu_handle_irq(data, "PPR", MMIO_STATUS_PPR_INT_MASK, 1183 MMIO_STATUS_PPR_OVERFLOW_MASK, 1184 amd_iommu_poll_ppr_log, amd_iommu_restart_ppr_log); 1185 1186 return IRQ_HANDLED; 1187 } 1188 1189 irqreturn_t amd_iommu_int_thread_galog(int irq, void *data) 1190 { 1191 #ifdef CONFIG_IRQ_REMAP 1192 amd_iommu_handle_irq(data, "GA", MMIO_STATUS_GALOG_INT_MASK, 1193 MMIO_STATUS_GALOG_OVERFLOW_MASK, 1194 iommu_poll_ga_log, amd_iommu_restart_ga_log); 1195 #endif 1196 1197 return IRQ_HANDLED; 1198 } 1199 1200 irqreturn_t amd_iommu_int_thread(int irq, void *data) 1201 { 1202 amd_iommu_int_thread_evtlog(irq, data); 1203 amd_iommu_int_thread_pprlog(irq, data); 1204 amd_iommu_int_thread_galog(irq, data); 1205 1206 return IRQ_HANDLED; 1207 } 1208 1209 /**************************************************************************** 1210 * 1211 * IOMMU command queuing functions 1212 * 1213 ****************************************************************************/ 1214 1215 static void dump_command_buffer(struct amd_iommu *iommu) 1216 { 1217 struct iommu_cmd *cmd; 1218 u32 head, tail; 1219 int i; 1220 1221 head = readl(iommu->mmio_base + MMIO_CMD_HEAD_OFFSET); 1222 tail = readl(iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); 1223 1224 pr_err("CMD Buffer head=%llu tail=%llu\n", MMIO_CMD_BUFFER_HEAD(head), 1225 MMIO_CMD_BUFFER_TAIL(tail)); 1226 1227 for (i = 0; i < CMD_BUFFER_ENTRIES; i++) { 1228 cmd = (struct iommu_cmd *)(iommu->cmd_buf + i * sizeof(*cmd)); 1229 pr_err("%3d: %08x %08x %08x %08x\n", i, cmd->data[0], cmd->data[1], cmd->data[2], 1230 cmd->data[3]); 1231 } 1232 } 1233 1234 static int wait_on_sem(struct amd_iommu *iommu, u64 data) 1235 { 1236 int i = 0; 1237 1238 /* 1239 * cmd_sem holds a monotonically non-decreasing completion sequence 1240 * number. 1241 */ 1242 while ((__s64)(READ_ONCE(*iommu->cmd_sem) - data) < 0 && 1243 i < LOOP_TIMEOUT) { 1244 udelay(1); 1245 i += 1; 1246 } 1247 1248 if (i == LOOP_TIMEOUT) { 1249 1250 pr_alert("IOMMU %04x:%02x:%02x.%01x: Completion-Wait loop timed out\n", 1251 iommu->pci_seg->id, PCI_BUS_NUM(iommu->devid), 1252 PCI_SLOT(iommu->devid), PCI_FUNC(iommu->devid)); 1253 1254 if (amd_iommu_dump) 1255 DO_ONCE_LITE(dump_command_buffer, iommu); 1256 1257 return -EIO; 1258 } 1259 1260 return 0; 1261 } 1262 1263 static void copy_cmd_to_buffer(struct amd_iommu *iommu, 1264 struct iommu_cmd *cmd) 1265 { 1266 u8 *target; 1267 u32 tail; 1268 1269 /* Copy command to buffer */ 1270 tail = iommu->cmd_buf_tail; 1271 target = iommu->cmd_buf + tail; 1272 memcpy(target, cmd, sizeof(*cmd)); 1273 1274 tail = (tail + sizeof(*cmd)) % CMD_BUFFER_SIZE; 1275 iommu->cmd_buf_tail = tail; 1276 1277 /* Tell the IOMMU about it */ 1278 writel(tail, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET); 1279 } 1280 1281 static void build_completion_wait(struct iommu_cmd *cmd, 1282 struct amd_iommu *iommu, 1283 u64 data) 1284 { 1285 u64 paddr = iommu->cmd_sem_paddr; 1286 1287 memset(cmd, 0, sizeof(*cmd)); 1288 cmd->data[0] = lower_32_bits(paddr) | CMD_COMPL_WAIT_STORE_MASK; 1289 cmd->data[1] = upper_32_bits(paddr); 1290 cmd->data[2] = lower_32_bits(data); 1291 cmd->data[3] = upper_32_bits(data); 1292 CMD_SET_TYPE(cmd, CMD_COMPL_WAIT); 1293 } 1294 1295 static void build_inv_dte(struct iommu_cmd *cmd, u16 devid) 1296 { 1297 memset(cmd, 0, sizeof(*cmd)); 1298 cmd->data[0] = devid; 1299 CMD_SET_TYPE(cmd, CMD_INV_DEV_ENTRY); 1300 } 1301 1302 /* 1303 * Builds an invalidation address which is suitable for one page or multiple 1304 * pages. Sets the size bit (S) as needed if more than one page is flushed. 1305 */ 1306 static inline u64 build_inv_address(u64 address, u64 last) 1307 { 1308 unsigned int sz_lg2; 1309 1310 address &= GENMASK_U64(63, 12); 1311 sz_lg2 = fls64(address ^ last); 1312 if (sz_lg2 <= 12) 1313 return address; 1314 1315 /* 1316 * Encode sz_lg2 according to Table 14: Example Page Size Encodings 1317 * 1318 * See "Note *": 1319 * Address bits 51:32 can be used to encode page sizes greater 1320 * that 4 Gbytes. 1321 * Which we take to mean that the highest page size has bit 1322 * [51]=0, [50:12]=1 1323 * and that coding happens when sz_lg2 is 52. Fall back to full 1324 * invalidation if the size is too big. 1325 * 1326 */ 1327 if (unlikely(sz_lg2 > 52)) 1328 return CMD_INV_IOMMU_ALL_PAGES_ADDRESS | 1329 CMD_INV_IOMMU_PAGES_SIZE_MASK; 1330 1331 /* 1332 * The sz_lg2 calculation with fls() ensures that: 1333 * address & BIT(sz_lg2 - 1) == 0 1334 * Therefore only the 1's need to be added. 8KB requires no 1's 1335 */ 1336 if (sz_lg2 > 13) 1337 address |= GENMASK_U64(sz_lg2 - 2, 12); 1338 return address | CMD_INV_IOMMU_PAGES_SIZE_MASK; 1339 } 1340 1341 static void build_inv_iommu_pages(struct iommu_cmd *cmd, u64 address, 1342 u64 last, u16 domid, ioasid_t pasid, 1343 u32 flags) 1344 { 1345 u64 inv_address = build_inv_address(address, last); 1346 1347 memset(cmd, 0, sizeof(*cmd)); 1348 1349 cmd->data[1] |= domid; 1350 cmd->data[2] = lower_32_bits(inv_address); 1351 cmd->data[3] = upper_32_bits(inv_address); 1352 cmd->data[2] |= flags; 1353 if (flags & CMD_INV_IOMMU_PAGES_GN_MASK) 1354 cmd->data[0] |= pasid; 1355 CMD_SET_TYPE(cmd, CMD_INV_IOMMU_PAGES); 1356 } 1357 1358 static void build_inv_iotlb_pages(struct iommu_cmd *cmd, u16 devid, int qdep, 1359 u64 address, u64 last, 1360 ioasid_t pasid, bool gn) 1361 { 1362 u64 inv_address = build_inv_address(address, last); 1363 1364 memset(cmd, 0, sizeof(*cmd)); 1365 1366 cmd->data[0] = devid; 1367 cmd->data[0] |= (qdep & 0xff) << 24; 1368 cmd->data[1] = devid; 1369 cmd->data[2] = lower_32_bits(inv_address); 1370 cmd->data[3] = upper_32_bits(inv_address); 1371 if (gn) { 1372 cmd->data[0] |= ((pasid >> 8) & 0xff) << 16; 1373 cmd->data[1] |= (pasid & 0xff) << 16; 1374 cmd->data[2] |= CMD_INV_IOMMU_PAGES_GN_MASK; 1375 } 1376 1377 CMD_SET_TYPE(cmd, CMD_INV_IOTLB_PAGES); 1378 } 1379 1380 static void build_complete_ppr(struct iommu_cmd *cmd, u16 devid, u32 pasid, 1381 int status, int tag, bool gn) 1382 { 1383 memset(cmd, 0, sizeof(*cmd)); 1384 1385 cmd->data[0] = devid; 1386 if (gn) { 1387 cmd->data[1] = pasid; 1388 cmd->data[2] = CMD_INV_IOMMU_PAGES_GN_MASK; 1389 } 1390 cmd->data[3] = tag & 0x1ff; 1391 cmd->data[3] |= (status & PPR_STATUS_MASK) << PPR_STATUS_SHIFT; 1392 1393 CMD_SET_TYPE(cmd, CMD_COMPLETE_PPR); 1394 } 1395 1396 static void build_inv_all(struct iommu_cmd *cmd) 1397 { 1398 memset(cmd, 0, sizeof(*cmd)); 1399 CMD_SET_TYPE(cmd, CMD_INV_ALL); 1400 } 1401 1402 static void build_inv_irt(struct iommu_cmd *cmd, u16 devid) 1403 { 1404 memset(cmd, 0, sizeof(*cmd)); 1405 cmd->data[0] = devid; 1406 CMD_SET_TYPE(cmd, CMD_INV_IRT); 1407 } 1408 1409 /* 1410 * Writes the command to the IOMMUs command buffer and informs the 1411 * hardware about the new command. 1412 */ 1413 static int __iommu_queue_command_sync(struct amd_iommu *iommu, 1414 struct iommu_cmd *cmd, 1415 bool sync) 1416 { 1417 unsigned int count = 0; 1418 u32 left, next_tail; 1419 1420 next_tail = (iommu->cmd_buf_tail + sizeof(*cmd)) % CMD_BUFFER_SIZE; 1421 again: 1422 left = (iommu->cmd_buf_head - next_tail) % CMD_BUFFER_SIZE; 1423 1424 if (left <= 0x20) { 1425 /* Skip udelay() the first time around */ 1426 if (count++) { 1427 if (count == LOOP_TIMEOUT) { 1428 pr_err("Command buffer timeout\n"); 1429 return -EIO; 1430 } 1431 1432 udelay(1); 1433 } 1434 1435 /* Update head and recheck remaining space */ 1436 iommu->cmd_buf_head = readl(iommu->mmio_base + 1437 MMIO_CMD_HEAD_OFFSET); 1438 1439 goto again; 1440 } 1441 1442 copy_cmd_to_buffer(iommu, cmd); 1443 1444 /* Do we need to make sure all commands are processed? */ 1445 iommu->need_sync = sync; 1446 1447 return 0; 1448 } 1449 1450 static int iommu_queue_command_sync(struct amd_iommu *iommu, 1451 struct iommu_cmd *cmd, 1452 bool sync) 1453 { 1454 unsigned long flags; 1455 int ret; 1456 1457 raw_spin_lock_irqsave(&iommu->lock, flags); 1458 ret = __iommu_queue_command_sync(iommu, cmd, sync); 1459 raw_spin_unlock_irqrestore(&iommu->lock, flags); 1460 1461 return ret; 1462 } 1463 1464 static int iommu_queue_command(struct amd_iommu *iommu, struct iommu_cmd *cmd) 1465 { 1466 return iommu_queue_command_sync(iommu, cmd, true); 1467 } 1468 1469 static u64 get_cmdsem_val(struct amd_iommu *iommu) 1470 { 1471 lockdep_assert_held(&iommu->lock); 1472 return ++iommu->cmd_sem_val; 1473 } 1474 1475 /* 1476 * This function queues a completion wait command into the command 1477 * buffer of an IOMMU 1478 */ 1479 static int iommu_completion_wait(struct amd_iommu *iommu) 1480 { 1481 struct iommu_cmd cmd; 1482 unsigned long flags; 1483 int ret; 1484 u64 data; 1485 1486 raw_spin_lock_irqsave(&iommu->lock, flags); 1487 1488 if (!iommu->need_sync) { 1489 /* 1490 * No command has been queued since the last completion-wait. 1491 * A concurrent CPU may have already queued that CWAIT and 1492 * cleared need_sync; need_sync == false only means a covering 1493 * CWAIT is queued, not that all prior commands have completed. 1494 * Wait for the last allocated sequence number so that any 1495 * command queued before this call (possibly on another CPU) 1496 * is guaranteed to have completed before returning. 1497 */ 1498 data = iommu->cmd_sem_val; 1499 raw_spin_unlock_irqrestore(&iommu->lock, flags); 1500 return wait_on_sem(iommu, data); 1501 } 1502 1503 data = get_cmdsem_val(iommu); 1504 build_completion_wait(&cmd, iommu, data); 1505 1506 ret = __iommu_queue_command_sync(iommu, &cmd, false); 1507 raw_spin_unlock_irqrestore(&iommu->lock, flags); 1508 1509 if (ret) 1510 return ret; 1511 1512 return wait_on_sem(iommu, data); 1513 } 1514 1515 static void domain_flush_complete(struct protection_domain *domain) 1516 { 1517 struct pdom_iommu_info *pdom_iommu_info; 1518 unsigned long i; 1519 1520 lockdep_assert_held(&domain->lock); 1521 1522 /* 1523 * Devices of this domain are behind this IOMMU 1524 * We need to wait for completion of all commands. 1525 */ 1526 xa_for_each(&domain->iommu_array, i, pdom_iommu_info) 1527 iommu_completion_wait(pdom_iommu_info->iommu); 1528 } 1529 1530 static int iommu_flush_dte(struct amd_iommu *iommu, u16 devid) 1531 { 1532 struct iommu_cmd cmd; 1533 1534 build_inv_dte(&cmd, devid); 1535 1536 return iommu_queue_command(iommu, &cmd); 1537 } 1538 1539 static void iommu_flush_dte_sync(struct amd_iommu *iommu, u16 devid) 1540 { 1541 int ret; 1542 1543 ret = iommu_flush_dte(iommu, devid); 1544 if (!ret) 1545 iommu_completion_wait(iommu); 1546 } 1547 1548 static void amd_iommu_flush_dte_all(struct amd_iommu *iommu) 1549 { 1550 u32 devid; 1551 u16 last_bdf = iommu->pci_seg->last_bdf; 1552 1553 for (devid = 0; devid <= last_bdf; ++devid) 1554 iommu_flush_dte(iommu, devid); 1555 1556 iommu_completion_wait(iommu); 1557 } 1558 1559 /* 1560 * This function uses heavy locking and may disable irqs for some time. But 1561 * this is no issue because it is only called during resume. 1562 */ 1563 static void amd_iommu_flush_tlb_all(struct amd_iommu *iommu) 1564 { 1565 u32 dom_id; 1566 u16 last_bdf = iommu->pci_seg->last_bdf; 1567 1568 for (dom_id = 0; dom_id <= last_bdf; ++dom_id) { 1569 struct iommu_cmd cmd; 1570 build_inv_iommu_pages(&cmd, 0, U64_MAX, 1571 dom_id, IOMMU_NO_PASID, 1572 CMD_INV_IOMMU_PAGES_PDE_MASK); 1573 iommu_queue_command(iommu, &cmd); 1574 } 1575 1576 iommu_completion_wait(iommu); 1577 } 1578 1579 static void amd_iommu_flush_tlb_domid(struct amd_iommu *iommu, u32 dom_id) 1580 { 1581 struct iommu_cmd cmd; 1582 1583 build_inv_iommu_pages(&cmd, 0, U64_MAX, 1584 dom_id, IOMMU_NO_PASID, 1585 CMD_INV_IOMMU_PAGES_PDE_MASK); 1586 iommu_queue_command(iommu, &cmd); 1587 1588 iommu_completion_wait(iommu); 1589 } 1590 1591 static int iommu_flush_pages_v1_hdom_ids(struct protection_domain *pdom, 1592 u64 address, u64 last, u32 flags) 1593 { 1594 int ret = 0; 1595 struct amd_iommu_viommu *aviommu; 1596 1597 list_for_each_entry(aviommu, &pdom->viommu_list, pdom_list) { 1598 unsigned long i; 1599 struct guest_domain_mapping_info *gdom_info; 1600 struct amd_iommu *iommu = container_of(aviommu->core.iommu_dev, 1601 struct amd_iommu, iommu); 1602 1603 xa_lock(&aviommu->gdomid_array); 1604 xa_for_each(&aviommu->gdomid_array, i, gdom_info) { 1605 struct iommu_cmd cmd; 1606 1607 pr_debug("%s: iommu=%#x, hdom_id=%#x\n", __func__, 1608 iommu->devid, gdom_info->hdom_id); 1609 build_inv_iommu_pages(&cmd, address, last, gdom_info->hdom_id, 1610 IOMMU_NO_PASID, flags); 1611 ret |= iommu_queue_command(iommu, &cmd); 1612 } 1613 xa_unlock(&aviommu->gdomid_array); 1614 } 1615 return ret; 1616 } 1617 1618 static void amd_iommu_flush_all(struct amd_iommu *iommu) 1619 { 1620 struct iommu_cmd cmd; 1621 1622 build_inv_all(&cmd); 1623 1624 iommu_queue_command(iommu, &cmd); 1625 iommu_completion_wait(iommu); 1626 } 1627 1628 static void iommu_flush_irt(struct amd_iommu *iommu, u16 devid) 1629 { 1630 struct iommu_cmd cmd; 1631 1632 build_inv_irt(&cmd, devid); 1633 1634 iommu_queue_command(iommu, &cmd); 1635 } 1636 1637 static void amd_iommu_flush_irt_all(struct amd_iommu *iommu) 1638 { 1639 u32 devid; 1640 u16 last_bdf = iommu->pci_seg->last_bdf; 1641 1642 if (iommu->irtcachedis_enabled) 1643 return; 1644 1645 for (devid = 0; devid <= last_bdf; devid++) 1646 iommu_flush_irt(iommu, devid); 1647 1648 iommu_completion_wait(iommu); 1649 } 1650 1651 void amd_iommu_flush_all_caches(struct amd_iommu *iommu) 1652 { 1653 if (check_feature(FEATURE_IA)) { 1654 amd_iommu_flush_all(iommu); 1655 } else { 1656 amd_iommu_flush_dte_all(iommu); 1657 amd_iommu_flush_irt_all(iommu); 1658 amd_iommu_flush_tlb_all(iommu); 1659 } 1660 } 1661 1662 /* 1663 * Command send function for flushing on-device TLB 1664 */ 1665 static int device_flush_iotlb(struct iommu_dev_data *dev_data, u64 address, 1666 u64 last, ioasid_t pasid, bool gn) 1667 { 1668 struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data); 1669 struct iommu_cmd cmd; 1670 int qdep = dev_data->ats_qdep; 1671 1672 build_inv_iotlb_pages(&cmd, dev_data->devid, qdep, address, 1673 last, pasid, gn); 1674 1675 return iommu_queue_command(iommu, &cmd); 1676 } 1677 1678 static int device_flush_dte_alias(struct pci_dev *pdev, u16 alias, void *data) 1679 { 1680 struct amd_iommu *iommu = data; 1681 1682 return iommu_flush_dte(iommu, alias); 1683 } 1684 1685 /* 1686 * Command send function for invalidating a device table entry 1687 */ 1688 static int device_flush_dte(struct iommu_dev_data *dev_data) 1689 { 1690 struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data); 1691 struct pci_dev *pdev = NULL; 1692 struct amd_iommu_pci_seg *pci_seg; 1693 u16 alias; 1694 int ret; 1695 1696 if (dev_is_pci(dev_data->dev)) 1697 pdev = to_pci_dev(dev_data->dev); 1698 1699 if (pdev) 1700 ret = pci_for_each_dma_alias(pdev, 1701 device_flush_dte_alias, iommu); 1702 else 1703 ret = iommu_flush_dte(iommu, dev_data->devid); 1704 if (ret) 1705 return ret; 1706 1707 pci_seg = iommu->pci_seg; 1708 alias = pci_seg->alias_table[dev_data->devid]; 1709 if (alias != dev_data->devid) { 1710 ret = iommu_flush_dte(iommu, alias); 1711 if (ret) 1712 return ret; 1713 } 1714 1715 if (dev_data->ats_enabled) { 1716 /* Invalidate the entire contents of an IOTLB */ 1717 ret = device_flush_iotlb(dev_data, 0, U64_MAX, 1718 IOMMU_NO_PASID, false); 1719 } 1720 1721 return ret; 1722 } 1723 1724 static int domain_flush_pages_v2(struct protection_domain *pdom, 1725 u64 address, u64 last, u32 flags) 1726 { 1727 struct iommu_dev_data *dev_data; 1728 struct iommu_cmd cmd; 1729 int ret = 0; 1730 1731 lockdep_assert_held(&pdom->lock); 1732 list_for_each_entry(dev_data, &pdom->dev_list, list) { 1733 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev); 1734 u16 domid = dev_data->gcr3_info.domid; 1735 1736 build_inv_iommu_pages(&cmd, address, last, domid, 1737 IOMMU_NO_PASID, 1738 flags | CMD_INV_IOMMU_PAGES_GN_MASK); 1739 1740 ret |= iommu_queue_command(iommu, &cmd); 1741 } 1742 1743 return ret; 1744 } 1745 1746 static int domain_flush_pages_v1(struct protection_domain *pdom, 1747 u64 address, u64 last, u32 flags) 1748 { 1749 struct pdom_iommu_info *pdom_iommu_info; 1750 struct iommu_cmd cmd; 1751 int ret = 0; 1752 unsigned long i; 1753 1754 lockdep_assert_held(&pdom->lock); 1755 1756 build_inv_iommu_pages(&cmd, address, last, 1757 pdom->id, IOMMU_NO_PASID, flags); 1758 1759 xa_for_each(&pdom->iommu_array, i, pdom_iommu_info) { 1760 /* 1761 * Devices of this domain are behind this IOMMU 1762 * We need a TLB flush 1763 */ 1764 ret |= iommu_queue_command(pdom_iommu_info->iommu, &cmd); 1765 } 1766 1767 /* 1768 * A domain w/ v1 table can be a nest parent, which can have 1769 * multiple nested domains. Each nested domain has 1:1 mapping 1770 * between gDomID and hDomID. Therefore, flush every hDomID 1771 * associated to this nest parent domain. 1772 * 1773 * See drivers/iommu/amd/nested.c: amd_iommu_alloc_domain_nested() 1774 */ 1775 if (!list_empty(&pdom->viommu_list)) 1776 ret |= iommu_flush_pages_v1_hdom_ids(pdom, address, last, flags); 1777 1778 return ret; 1779 } 1780 1781 /* 1782 * TLB invalidation function which is called from the mapping functions. 1783 * It flushes range of PTEs of the domain. 1784 */ 1785 static void __domain_flush_pages(struct protection_domain *domain, 1786 u64 address, u64 last, u32 flags) 1787 { 1788 struct iommu_dev_data *dev_data; 1789 int ret = 0; 1790 ioasid_t pasid = IOMMU_NO_PASID; 1791 bool gn = false; 1792 1793 lockdep_assert_held(&domain->lock); 1794 1795 if (pdom_is_v2_pgtbl_mode(domain)) { 1796 gn = true; 1797 ret = domain_flush_pages_v2(domain, address, last, flags); 1798 } else { 1799 ret = domain_flush_pages_v1(domain, address, last, flags); 1800 } 1801 1802 list_for_each_entry(dev_data, &domain->dev_list, list) { 1803 1804 if (!dev_data->ats_enabled) 1805 continue; 1806 1807 ret |= device_flush_iotlb(dev_data, address, last, pasid, gn); 1808 } 1809 1810 WARN_ON(ret); 1811 } 1812 1813 void amd_iommu_domain_flush_pages(struct protection_domain *domain, 1814 u64 address, u64 last, u32 flags) 1815 { 1816 lockdep_assert_held(&domain->lock); 1817 1818 if (likely(!amd_iommu_np_cache) || 1819 unlikely(address == 0 && last == U64_MAX)) { 1820 __domain_flush_pages(domain, address, last, flags); 1821 1822 /* Wait until IOMMU TLB and all device IOTLB flushes are complete */ 1823 domain_flush_complete(domain); 1824 1825 return; 1826 } 1827 1828 /* 1829 * When NpCache is on, we infer that we run in a VM and use a vIOMMU. 1830 * In such setups it is best to avoid flushes of ranges which are not 1831 * naturally aligned, since it would lead to flushes of unmodified 1832 * PTEs. Such flushes would require the hypervisor to do more work than 1833 * necessary. Therefore, perform repeated flushes of aligned ranges 1834 * until you cover the range. Each iteration flushes the smaller 1835 * between the natural alignment of the address that we flush and the 1836 * greatest naturally aligned region that fits in the range. 1837 */ 1838 while (address <= last) { 1839 unsigned int sz_lg2 = ilog2(last - address + 1); 1840 u64 flush_last; 1841 1842 if (likely(address)) 1843 sz_lg2 = min_t(unsigned int, sz_lg2, __ffs64(address)); 1844 1845 flush_last = address + (1ULL << sz_lg2) - 1; 1846 __domain_flush_pages(domain, address, flush_last, flags); 1847 if (check_add_overflow(flush_last, 1, &address)) 1848 break; 1849 } 1850 1851 /* Wait until IOMMU TLB and all device IOTLB flushes are complete */ 1852 domain_flush_complete(domain); 1853 } 1854 1855 /* Flush the whole IO/TLB for a given protection domain - including PDE */ 1856 static void amd_iommu_domain_flush_all(struct protection_domain *domain) 1857 { 1858 amd_iommu_domain_flush_pages(domain, 0, U64_MAX, 1859 CMD_INV_IOMMU_PAGES_PDE_MASK); 1860 } 1861 1862 void amd_iommu_dev_flush_pasid_pages(struct iommu_dev_data *dev_data, 1863 ioasid_t pasid, u64 address, u64 last) 1864 { 1865 struct iommu_cmd cmd; 1866 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev); 1867 1868 build_inv_iommu_pages(&cmd, address, last, 1869 dev_data->gcr3_info.domid, pasid, 1870 CMD_INV_IOMMU_PAGES_GN_MASK | 1871 CMD_INV_IOMMU_PAGES_PDE_MASK); 1872 iommu_queue_command(iommu, &cmd); 1873 1874 if (dev_data->ats_enabled) 1875 device_flush_iotlb(dev_data, address, last, pasid, true); 1876 1877 iommu_completion_wait(iommu); 1878 } 1879 1880 static void dev_flush_pasid_all(struct iommu_dev_data *dev_data, 1881 ioasid_t pasid) 1882 { 1883 amd_iommu_dev_flush_pasid_pages(dev_data, pasid, 0, U64_MAX); 1884 } 1885 1886 static int __amd_iommu_complete_ppr(struct device *dev, u32 pasid, 1887 int status, int tag, bool gn) 1888 { 1889 struct iommu_dev_data *dev_data; 1890 struct amd_iommu *iommu; 1891 struct iommu_cmd cmd; 1892 1893 dev_data = dev_iommu_priv_get(dev); 1894 iommu = get_amd_iommu_from_dev(dev); 1895 1896 build_complete_ppr(&cmd, dev_data->devid, pasid, status, tag, gn); 1897 1898 return iommu_queue_command(iommu, &cmd); 1899 } 1900 1901 int amd_iommu_complete_ppr(struct device *dev, u32 pasid, int status, int tag) 1902 { 1903 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev); 1904 bool gn; 1905 1906 gn = pdom_is_v2_pgtbl_mode(dev_data->domain); 1907 1908 return __amd_iommu_complete_ppr(dev, pasid, status, tag, gn); 1909 } 1910 1911 /**************************************************************************** 1912 * 1913 * The next functions belong to the domain allocation. A domain is 1914 * allocated for every IOMMU as the default domain. If device isolation 1915 * is enabled, every device get its own domain. The most important thing 1916 * about domains is the page table mapping the DMA address space they 1917 * contain. 1918 * 1919 ****************************************************************************/ 1920 int amd_iommu_pdom_id_alloc(void) 1921 { 1922 return ida_alloc_range(&pdom_ids, 1, MAX_DOMAIN_ID - 1, GFP_ATOMIC); 1923 } 1924 1925 int amd_iommu_pdom_id_reserve(u16 id, gfp_t gfp) 1926 { 1927 return ida_alloc_range(&pdom_ids, id, id, gfp); 1928 } 1929 1930 void amd_iommu_pdom_id_free(int id) 1931 { 1932 ida_free(&pdom_ids, id); 1933 } 1934 1935 void amd_iommu_pdom_id_destroy(void) 1936 { 1937 ida_destroy(&pdom_ids); 1938 } 1939 1940 static void free_gcr3_tbl_level1(u64 *tbl) 1941 { 1942 u64 *ptr; 1943 int i; 1944 1945 for (i = 0; i < 512; ++i) { 1946 if (!(tbl[i] & GCR3_VALID)) 1947 continue; 1948 1949 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK); 1950 1951 iommu_free_pages(ptr); 1952 } 1953 } 1954 1955 static void free_gcr3_tbl_level2(u64 *tbl) 1956 { 1957 u64 *ptr; 1958 int i; 1959 1960 for (i = 0; i < 512; ++i) { 1961 if (!(tbl[i] & GCR3_VALID)) 1962 continue; 1963 1964 ptr = iommu_phys_to_virt(tbl[i] & PAGE_MASK); 1965 1966 free_gcr3_tbl_level1(ptr); 1967 } 1968 } 1969 1970 static void free_gcr3_table(struct gcr3_tbl_info *gcr3_info) 1971 { 1972 if (gcr3_info->glx == 2) 1973 free_gcr3_tbl_level2(gcr3_info->gcr3_tbl); 1974 else if (gcr3_info->glx == 1) 1975 free_gcr3_tbl_level1(gcr3_info->gcr3_tbl); 1976 else 1977 WARN_ON_ONCE(gcr3_info->glx != 0); 1978 1979 gcr3_info->glx = 0; 1980 1981 /* Free per device domain ID */ 1982 amd_iommu_pdom_id_free(gcr3_info->domid); 1983 1984 iommu_free_pages(gcr3_info->gcr3_tbl); 1985 gcr3_info->gcr3_tbl = NULL; 1986 } 1987 1988 /* 1989 * Number of GCR3 table levels required. Level must be 4-Kbyte 1990 * page and can contain up to 512 entries. 1991 */ 1992 static int get_gcr3_levels(int pasids) 1993 { 1994 int levels; 1995 1996 if (pasids == -1) 1997 return amd_iommu_max_glx_val; 1998 1999 levels = get_count_order(pasids); 2000 2001 return levels ? (DIV_ROUND_UP(levels, 9) - 1) : levels; 2002 } 2003 2004 static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info, 2005 struct amd_iommu *iommu, int pasids) 2006 { 2007 int levels = get_gcr3_levels(pasids); 2008 int nid = iommu ? dev_to_node(&iommu->dev->dev) : NUMA_NO_NODE; 2009 int domid; 2010 2011 if (levels > amd_iommu_max_glx_val) 2012 return -EINVAL; 2013 2014 if (gcr3_info->gcr3_tbl) 2015 return -EBUSY; 2016 2017 /* Allocate per device domain ID */ 2018 domid = amd_iommu_pdom_id_alloc(); 2019 if (domid <= 0) 2020 return -ENOSPC; 2021 gcr3_info->domid = domid; 2022 2023 gcr3_info->gcr3_tbl = iommu_alloc_pages_node_sz(nid, GFP_ATOMIC, SZ_4K); 2024 if (gcr3_info->gcr3_tbl == NULL) { 2025 amd_iommu_pdom_id_free(domid); 2026 return -ENOMEM; 2027 } 2028 2029 gcr3_info->glx = levels; 2030 2031 return 0; 2032 } 2033 2034 static u64 *__get_gcr3_pte(struct gcr3_tbl_info *gcr3_info, 2035 ioasid_t pasid, bool alloc) 2036 { 2037 int index; 2038 u64 *pte; 2039 u64 *root = gcr3_info->gcr3_tbl; 2040 int level = gcr3_info->glx; 2041 2042 while (true) { 2043 2044 index = (pasid >> (9 * level)) & 0x1ff; 2045 pte = &root[index]; 2046 2047 if (level == 0) 2048 break; 2049 2050 if (!(*pte & GCR3_VALID)) { 2051 if (!alloc) 2052 return NULL; 2053 2054 root = (void *)get_zeroed_page(GFP_ATOMIC); 2055 if (root == NULL) 2056 return NULL; 2057 2058 *pte = iommu_virt_to_phys(root) | GCR3_VALID; 2059 } 2060 2061 root = iommu_phys_to_virt(*pte & PAGE_MASK); 2062 2063 level -= 1; 2064 } 2065 2066 return pte; 2067 } 2068 2069 static int update_gcr3(struct iommu_dev_data *dev_data, 2070 ioasid_t pasid, unsigned long gcr3, bool set) 2071 { 2072 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info; 2073 u64 *pte; 2074 2075 pte = __get_gcr3_pte(gcr3_info, pasid, true); 2076 if (pte == NULL) 2077 return -ENOMEM; 2078 2079 if (set) 2080 *pte = (gcr3 & PAGE_MASK) | GCR3_VALID; 2081 else 2082 *pte = 0; 2083 2084 dev_flush_pasid_all(dev_data, pasid); 2085 return 0; 2086 } 2087 2088 int amd_iommu_set_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid, 2089 unsigned long gcr3) 2090 { 2091 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info; 2092 int ret; 2093 2094 iommu_group_mutex_assert(dev_data->dev); 2095 2096 ret = update_gcr3(dev_data, pasid, gcr3, true); 2097 if (ret) 2098 return ret; 2099 2100 gcr3_info->pasid_cnt++; 2101 return ret; 2102 } 2103 2104 int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid) 2105 { 2106 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info; 2107 int ret; 2108 2109 iommu_group_mutex_assert(dev_data->dev); 2110 2111 ret = update_gcr3(dev_data, pasid, 0, false); 2112 if (ret) 2113 return ret; 2114 2115 gcr3_info->pasid_cnt--; 2116 return ret; 2117 } 2118 2119 /* 2120 * Note: 2121 * The old value for GCR3 table and GPT have been cleared from caller. 2122 */ 2123 static void set_dte_gcr3_table(struct iommu_dev_data *dev_data, 2124 struct dev_table_entry *new) 2125 { 2126 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info; 2127 u64 gcr3 = iommu_virt_to_phys(gcr3_info->gcr3_tbl); 2128 2129 new->data[0] |= DTE_FLAG_TV | 2130 (dev_data->ppr ? DTE_FLAG_PPR : 0) | 2131 (pdom_is_v2_pgtbl_mode(dev_data->domain) ? DTE_FLAG_GIOV : 0) | 2132 DTE_FLAG_GV | 2133 FIELD_PREP(DTE_GLX, gcr3_info->glx) | 2134 FIELD_PREP(DTE_GCR3_14_12, gcr3 >> 12) | 2135 DTE_FLAG_IR | DTE_FLAG_IW; 2136 2137 new->data[1] |= FIELD_PREP(DTE_DOMID_MASK, dev_data->gcr3_info.domid) | 2138 FIELD_PREP(DTE_GCR3_30_15, gcr3 >> 15) | 2139 (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0) | 2140 FIELD_PREP(DTE_GCR3_51_31, gcr3 >> 31); 2141 2142 /* Guest page table can only support 4 and 5 levels */ 2143 if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL) 2144 new->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_5_LEVEL); 2145 else 2146 new->data[2] |= FIELD_PREP(DTE_GPT_LEVEL_MASK, GUEST_PGTABLE_4_LEVEL); 2147 } 2148 2149 void amd_iommu_set_dte_v1(struct iommu_dev_data *dev_data, 2150 struct protection_domain *domain, u16 domid, 2151 struct pt_iommu_amdv1_hw_info *pt_info, 2152 struct dev_table_entry *new) 2153 { 2154 u64 host_pt_root = __sme_set(pt_info->host_pt_root); 2155 2156 /* Note Dirty tracking is used for v1 table only for now */ 2157 new->data[0] |= DTE_FLAG_TV | 2158 FIELD_PREP(DTE_MODE_MASK, pt_info->mode) | 2159 (domain->dirty_tracking ? DTE_FLAG_HAD : 0) | 2160 FIELD_PREP(DTE_HOST_TRP, host_pt_root >> 12) | 2161 DTE_FLAG_IR | DTE_FLAG_IW; 2162 2163 new->data[1] |= FIELD_PREP(DTE_DOMID_MASK, domid) | 2164 (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0); 2165 } 2166 2167 static void set_dte_v1(struct iommu_dev_data *dev_data, 2168 struct protection_domain *domain, u16 domid, 2169 phys_addr_t top_paddr, unsigned int top_level, 2170 struct dev_table_entry *new) 2171 { 2172 struct pt_iommu_amdv1_hw_info pt_info; 2173 2174 /* 2175 * When updating the IO pagetable, the new top and level 2176 * are provided as parameters. For other operations i.e. 2177 * device attach, retrieve the current pagetable info 2178 * via the IOMMU PT API. 2179 */ 2180 if (top_paddr) { 2181 pt_info.host_pt_root = top_paddr; 2182 pt_info.mode = top_level + 1; 2183 } else { 2184 WARN_ON(top_paddr || top_level); 2185 pt_iommu_amdv1_hw_info(&domain->amdv1, &pt_info); 2186 } 2187 2188 amd_iommu_set_dte_v1(dev_data, domain, domid, &pt_info, new); 2189 } 2190 2191 static void set_dte_passthrough(struct iommu_dev_data *dev_data, 2192 struct protection_domain *domain, 2193 struct dev_table_entry *new) 2194 { 2195 new->data[0] |= DTE_FLAG_TV | DTE_FLAG_IR | DTE_FLAG_IW; 2196 2197 new->data[1] |= FIELD_PREP(DTE_DOMID_MASK, domain->id) | 2198 (dev_data->ats_enabled ? DTE_FLAG_IOTLB : 0); 2199 2200 } 2201 2202 static void set_dte_entry(struct amd_iommu *iommu, 2203 struct iommu_dev_data *dev_data, 2204 phys_addr_t top_paddr, unsigned int top_level) 2205 { 2206 u32 old_domid; 2207 struct dev_table_entry new = {}; 2208 struct protection_domain *domain = dev_data->domain; 2209 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info; 2210 struct dev_table_entry *dte = &get_dev_table(iommu)[dev_data->devid]; 2211 2212 amd_iommu_make_clear_dte(dev_data, &new); 2213 2214 old_domid = READ_ONCE(dte->data[1]) & DTE_DOMID_MASK; 2215 if (gcr3_info->gcr3_tbl) 2216 set_dte_gcr3_table(dev_data, &new); 2217 else if (domain->domain.type == IOMMU_DOMAIN_IDENTITY) 2218 set_dte_passthrough(dev_data, domain, &new); 2219 else if ((domain->domain.type & __IOMMU_DOMAIN_PAGING) && 2220 domain->pd_mode == PD_MODE_V1) 2221 set_dte_v1(dev_data, domain, domain->id, top_paddr, top_level, &new); 2222 else 2223 WARN_ON(true); 2224 2225 amd_iommu_update_dte(iommu, dev_data, &new); 2226 2227 /* 2228 * A kdump kernel might be replacing a domain ID that was copied from 2229 * the previous kernel--if so, it needs to flush the translation cache 2230 * entries for the old domain ID that is being overwritten 2231 */ 2232 if (old_domid) { 2233 amd_iommu_flush_tlb_domid(iommu, old_domid); 2234 } 2235 } 2236 2237 /* 2238 * Clear DMA-remap related flags to block all DMA (blockeded domain) 2239 */ 2240 static void clear_dte_entry(struct amd_iommu *iommu, struct iommu_dev_data *dev_data) 2241 { 2242 struct dev_table_entry new = {}; 2243 2244 amd_iommu_make_clear_dte(dev_data, &new); 2245 amd_iommu_update_dte(iommu, dev_data, &new); 2246 } 2247 2248 /* Update and flush DTE for the given device */ 2249 static void dev_update_dte(struct iommu_dev_data *dev_data, bool set) 2250 { 2251 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev); 2252 2253 if (set) 2254 set_dte_entry(iommu, dev_data, 0, 0); 2255 else 2256 clear_dte_entry(iommu, dev_data); 2257 } 2258 2259 /* 2260 * If domain is SVA capable then initialize GCR3 table. Also if domain is 2261 * in v2 page table mode then update GCR3[0]. 2262 */ 2263 static int init_gcr3_table(struct iommu_dev_data *dev_data, 2264 struct protection_domain *pdom) 2265 { 2266 struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data); 2267 int max_pasids = dev_data->max_pasids; 2268 struct pt_iommu_x86_64_hw_info pt_info; 2269 int ret = 0; 2270 2271 /* 2272 * If domain is in pt mode then setup GCR3 table only if device 2273 * is PASID capable 2274 */ 2275 if (pdom_is_in_pt_mode(pdom) && !pdev_pasid_supported(dev_data)) 2276 return ret; 2277 2278 /* 2279 * By default, setup GCR3 table to support MAX PASIDs 2280 * supported by the device/IOMMU. 2281 */ 2282 ret = setup_gcr3_table(&dev_data->gcr3_info, iommu, 2283 max_pasids > 0 ? max_pasids : 1); 2284 if (ret) 2285 return ret; 2286 2287 /* Setup GCR3[0] only if domain is setup with v2 page table mode */ 2288 if (!pdom_is_v2_pgtbl_mode(pdom)) 2289 return ret; 2290 2291 pt_iommu_x86_64_hw_info(&pdom->amdv2, &pt_info); 2292 ret = update_gcr3(dev_data, 0, __sme_set(pt_info.gcr3_pt), true); 2293 if (ret) 2294 free_gcr3_table(&dev_data->gcr3_info); 2295 2296 return ret; 2297 } 2298 2299 static void destroy_gcr3_table(struct iommu_dev_data *dev_data, 2300 struct protection_domain *pdom) 2301 { 2302 struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info; 2303 2304 if (pdom_is_v2_pgtbl_mode(pdom)) 2305 update_gcr3(dev_data, 0, 0, false); 2306 2307 if (gcr3_info->gcr3_tbl == NULL) 2308 return; 2309 2310 free_gcr3_table(gcr3_info); 2311 } 2312 2313 static int pdom_attach_iommu(struct amd_iommu *iommu, 2314 struct protection_domain *pdom) 2315 { 2316 struct pdom_iommu_info *pdom_iommu_info, *curr; 2317 unsigned long flags; 2318 int ret = 0; 2319 2320 spin_lock_irqsave(&pdom->lock, flags); 2321 2322 pdom_iommu_info = xa_load(&pdom->iommu_array, iommu->index); 2323 if (pdom_iommu_info) { 2324 pdom_iommu_info->refcnt++; 2325 goto out_unlock; 2326 } 2327 2328 pdom_iommu_info = kzalloc_obj(*pdom_iommu_info, GFP_ATOMIC); 2329 if (!pdom_iommu_info) { 2330 ret = -ENOMEM; 2331 goto out_unlock; 2332 } 2333 2334 pdom_iommu_info->iommu = iommu; 2335 pdom_iommu_info->refcnt = 1; 2336 2337 curr = xa_cmpxchg(&pdom->iommu_array, iommu->index, 2338 NULL, pdom_iommu_info, GFP_ATOMIC); 2339 if (curr) { 2340 kfree(pdom_iommu_info); 2341 ret = -ENOSPC; 2342 goto out_unlock; 2343 } 2344 2345 out_unlock: 2346 spin_unlock_irqrestore(&pdom->lock, flags); 2347 return ret; 2348 } 2349 2350 static void pdom_detach_iommu(struct amd_iommu *iommu, 2351 struct protection_domain *pdom) 2352 { 2353 struct pdom_iommu_info *pdom_iommu_info; 2354 unsigned long flags; 2355 2356 spin_lock_irqsave(&pdom->lock, flags); 2357 2358 pdom_iommu_info = xa_load(&pdom->iommu_array, iommu->index); 2359 if (!pdom_iommu_info) { 2360 spin_unlock_irqrestore(&pdom->lock, flags); 2361 return; 2362 } 2363 2364 pdom_iommu_info->refcnt--; 2365 if (pdom_iommu_info->refcnt == 0) { 2366 xa_erase(&pdom->iommu_array, iommu->index); 2367 kfree(pdom_iommu_info); 2368 } 2369 2370 spin_unlock_irqrestore(&pdom->lock, flags); 2371 } 2372 2373 /* 2374 * If a device is not yet associated with a domain, this function makes the 2375 * device visible in the domain 2376 */ 2377 static int attach_device(struct device *dev, 2378 struct protection_domain *domain) 2379 { 2380 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev); 2381 struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data); 2382 struct pci_dev *pdev; 2383 unsigned long flags; 2384 int ret = 0; 2385 2386 mutex_lock(&dev_data->mutex); 2387 2388 if (dev_data->domain != NULL) { 2389 ret = -EBUSY; 2390 goto out; 2391 } 2392 2393 /* Do reference counting */ 2394 ret = pdom_attach_iommu(iommu, domain); 2395 if (ret) 2396 goto out; 2397 2398 /* Setup GCR3 table */ 2399 if (pdom_is_sva_capable(domain)) { 2400 ret = init_gcr3_table(dev_data, domain); 2401 if (ret) { 2402 pdom_detach_iommu(iommu, domain); 2403 goto out; 2404 } 2405 } 2406 2407 pdev = dev_is_pci(dev_data->dev) ? to_pci_dev(dev_data->dev) : NULL; 2408 if (pdev && pdom_is_sva_capable(domain)) { 2409 pdev_enable_caps(pdev); 2410 2411 /* 2412 * Device can continue to function even if IOPF 2413 * enablement failed. Hence in error path just 2414 * disable device PRI support. 2415 */ 2416 if (amd_iommu_iopf_add_device(iommu, dev_data)) 2417 pdev_disable_cap_pri(pdev); 2418 } else if (pdev) { 2419 pdev_enable_cap_ats(pdev); 2420 } 2421 2422 /* Update data structures */ 2423 dev_data->domain = domain; 2424 spin_lock_irqsave(&domain->lock, flags); 2425 list_add(&dev_data->list, &domain->dev_list); 2426 spin_unlock_irqrestore(&domain->lock, flags); 2427 2428 /* Update device table */ 2429 dev_update_dte(dev_data, true); 2430 2431 out: 2432 mutex_unlock(&dev_data->mutex); 2433 2434 return ret; 2435 } 2436 2437 /* 2438 * Removes a device from a protection domain (with devtable_lock held) 2439 */ 2440 static void detach_device(struct device *dev) 2441 { 2442 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev); 2443 struct amd_iommu *iommu = get_amd_iommu_from_dev_data(dev_data); 2444 struct protection_domain *domain = dev_data->domain; 2445 unsigned long flags; 2446 2447 mutex_lock(&dev_data->mutex); 2448 2449 /* 2450 * First check if the device is still attached. It might already 2451 * be detached from its domain because the generic 2452 * iommu_detach_group code detached it and we try again here in 2453 * our alias handling. 2454 */ 2455 if (WARN_ON(!dev_data->domain)) 2456 goto out; 2457 2458 /* Remove IOPF handler */ 2459 if (dev_data->ppr) { 2460 iopf_queue_flush_dev(dev); 2461 amd_iommu_iopf_remove_device(iommu, dev_data); 2462 } 2463 2464 if (dev_is_pci(dev)) 2465 pdev_disable_caps(to_pci_dev(dev)); 2466 2467 /* Clear DTE and flush the entry */ 2468 dev_update_dte(dev_data, false); 2469 2470 /* Flush IOTLB and wait for the flushes to finish */ 2471 spin_lock_irqsave(&domain->lock, flags); 2472 amd_iommu_domain_flush_all(domain); 2473 list_del(&dev_data->list); 2474 spin_unlock_irqrestore(&domain->lock, flags); 2475 2476 /* Clear GCR3 table */ 2477 if (pdom_is_sva_capable(domain)) 2478 destroy_gcr3_table(dev_data, domain); 2479 2480 /* Update data structures */ 2481 dev_data->domain = NULL; 2482 2483 /* decrease reference counters - needs to happen after the flushes */ 2484 pdom_detach_iommu(iommu, domain); 2485 2486 out: 2487 mutex_unlock(&dev_data->mutex); 2488 } 2489 2490 static struct iommu_device *amd_iommu_probe_device(struct device *dev) 2491 { 2492 struct iommu_device *iommu_dev; 2493 struct amd_iommu *iommu; 2494 struct iommu_dev_data *dev_data; 2495 int ret; 2496 2497 if (!check_device(dev)) 2498 return ERR_PTR(-ENODEV); 2499 2500 iommu = rlookup_amd_iommu(dev); 2501 if (!iommu) 2502 return ERR_PTR(-ENODEV); 2503 2504 /* Not registered yet? */ 2505 if (!iommu->iommu.ops) 2506 return ERR_PTR(-ENODEV); 2507 2508 if (dev_iommu_priv_get(dev)) 2509 return &iommu->iommu; 2510 2511 ret = iommu_init_device(iommu, dev); 2512 if (ret) { 2513 dev_err(dev, "Failed to initialize - trying to proceed anyway\n"); 2514 iommu_dev = ERR_PTR(ret); 2515 iommu_ignore_device(iommu, dev); 2516 goto out_err; 2517 } 2518 2519 amd_iommu_set_pci_msi_domain(dev, iommu); 2520 iommu_dev = &iommu->iommu; 2521 2522 /* 2523 * If IOMMU and device supports PASID then it will contain max 2524 * supported PASIDs, else it will be zero. 2525 */ 2526 dev_data = dev_iommu_priv_get(dev); 2527 if (amd_iommu_pasid_supported() && dev_is_pci(dev) && 2528 pdev_pasid_supported(dev_data)) { 2529 dev_data->max_pasids = min_t(u32, iommu->iommu.max_pasids, 2530 pci_max_pasids(to_pci_dev(dev))); 2531 } 2532 2533 if (amd_iommu_pgtable == PD_MODE_NONE) { 2534 pr_warn_once("%s: DMA translation not supported by iommu.\n", 2535 __func__); 2536 iommu_dev = ERR_PTR(-ENODEV); 2537 goto out_err; 2538 } 2539 2540 iommu_completion_wait(iommu); 2541 2542 if (FEATURE_NUM_INT_REMAP_SUP_2K(amd_iommu_efr2)) 2543 dev_data->max_irqs = MAX_IRQS_PER_TABLE_2K; 2544 else 2545 dev_data->max_irqs = MAX_IRQS_PER_TABLE_512; 2546 2547 if (dev_is_pci(dev)) 2548 pci_prepare_ats(to_pci_dev(dev), PAGE_SHIFT); 2549 2550 out_err: 2551 return iommu_dev; 2552 } 2553 2554 static void amd_iommu_release_device(struct device *dev) 2555 { 2556 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev); 2557 2558 WARN_ON(dev_data->domain); 2559 2560 /* 2561 * We keep dev_data around for unplugged devices and reuse it when the 2562 * device is re-plugged - not doing so would introduce a ton of races. 2563 */ 2564 } 2565 2566 static struct iommu_group *amd_iommu_device_group(struct device *dev) 2567 { 2568 if (dev_is_pci(dev)) 2569 return pci_device_group(dev); 2570 2571 return acpihid_device_group(dev); 2572 } 2573 2574 /***************************************************************************** 2575 * 2576 * The following functions belong to the exported interface of AMD IOMMU 2577 * 2578 * This interface allows access to lower level functions of the IOMMU 2579 * like protection domain handling and assignement of devices to domains 2580 * which is not possible with the dma_ops interface. 2581 * 2582 *****************************************************************************/ 2583 2584 static void protection_domain_init(struct protection_domain *domain) 2585 { 2586 spin_lock_init(&domain->lock); 2587 INIT_LIST_HEAD(&domain->dev_list); 2588 INIT_LIST_HEAD(&domain->dev_data_list); 2589 INIT_LIST_HEAD(&domain->viommu_list); 2590 xa_init(&domain->iommu_array); 2591 } 2592 2593 struct protection_domain *protection_domain_alloc(void) 2594 { 2595 struct protection_domain *domain; 2596 int domid; 2597 2598 domain = kzalloc_obj(*domain); 2599 if (!domain) 2600 return NULL; 2601 2602 domid = amd_iommu_pdom_id_alloc(); 2603 if (domid <= 0) { 2604 kfree(domain); 2605 return NULL; 2606 } 2607 domain->id = domid; 2608 2609 protection_domain_init(domain); 2610 2611 return domain; 2612 } 2613 2614 static bool amd_iommu_hd_support(struct amd_iommu *iommu) 2615 { 2616 if (amd_iommu_hatdis) 2617 return false; 2618 2619 return iommu && (iommu->features & FEATURE_HDSUP); 2620 } 2621 2622 static spinlock_t *amd_iommu_get_top_lock(struct pt_iommu *iommupt) 2623 { 2624 struct protection_domain *pdom = 2625 container_of(iommupt, struct protection_domain, iommu); 2626 2627 return &pdom->lock; 2628 } 2629 2630 /* 2631 * Update all HW references to the domain with a new pgtable configuration. 2632 */ 2633 static void amd_iommu_change_top(struct pt_iommu *iommu_table, 2634 phys_addr_t top_paddr, unsigned int top_level) 2635 { 2636 struct protection_domain *pdom = 2637 container_of(iommu_table, struct protection_domain, iommu); 2638 struct iommu_dev_data *dev_data; 2639 2640 lockdep_assert_held(&pdom->lock); 2641 2642 /* Update the DTE for all devices attached to this domain */ 2643 list_for_each_entry(dev_data, &pdom->dev_list, list) { 2644 struct amd_iommu *iommu = rlookup_amd_iommu(dev_data->dev); 2645 2646 /* Update the HW references with the new level and top ptr */ 2647 set_dte_entry(iommu, dev_data, top_paddr, top_level); 2648 clone_aliases(iommu, dev_data->dev); 2649 } 2650 2651 list_for_each_entry(dev_data, &pdom->dev_list, list) 2652 device_flush_dte(dev_data); 2653 2654 domain_flush_complete(pdom); 2655 } 2656 2657 /* 2658 * amd_iommu_iotlb_sync_map() is used to generate flushes for non-present to 2659 * present (ie mapping) operations. It is a NOP if the IOMMU doesn't have non 2660 * present caching (like hypervisor shadowing). 2661 */ 2662 static int amd_iommu_iotlb_sync_map(struct iommu_domain *dom, 2663 unsigned long iova, size_t size) 2664 { 2665 struct protection_domain *domain = to_pdomain(dom); 2666 unsigned long flags; 2667 2668 if (likely(!amd_iommu_np_cache)) 2669 return 0; 2670 2671 spin_lock_irqsave(&domain->lock, flags); 2672 amd_iommu_domain_flush_pages(domain, iova, iova + size - 1, 2673 CMD_INV_IOMMU_PAGES_PDE_MASK); 2674 spin_unlock_irqrestore(&domain->lock, flags); 2675 return 0; 2676 } 2677 2678 static void amd_iommu_flush_iotlb_all(struct iommu_domain *domain) 2679 { 2680 struct protection_domain *dom = to_pdomain(domain); 2681 unsigned long flags; 2682 2683 spin_lock_irqsave(&dom->lock, flags); 2684 amd_iommu_domain_flush_all(dom); 2685 spin_unlock_irqrestore(&dom->lock, flags); 2686 } 2687 2688 static void amd_iommu_iotlb_sync(struct iommu_domain *domain, 2689 struct iommu_iotlb_gather *gather) 2690 { 2691 struct protection_domain *dom = to_pdomain(domain); 2692 unsigned long flags; 2693 2694 spin_lock_irqsave(&dom->lock, flags); 2695 amd_iommu_domain_flush_pages(dom, gather->start, gather->end, 2696 iommu_pages_list_empty(&gather->freelist) ? 2697 0 : CMD_INV_IOMMU_PAGES_PDE_MASK); 2698 spin_unlock_irqrestore(&dom->lock, flags); 2699 iommu_put_pages_list(&gather->freelist); 2700 } 2701 2702 static const struct pt_iommu_driver_ops amd_hw_driver_ops_v1 = { 2703 .get_top_lock = amd_iommu_get_top_lock, 2704 .change_top = amd_iommu_change_top, 2705 }; 2706 2707 static const struct iommu_domain_ops amdv1_ops = { 2708 IOMMU_PT_DOMAIN_OPS(amdv1), 2709 .iotlb_sync_map = amd_iommu_iotlb_sync_map, 2710 .flush_iotlb_all = amd_iommu_flush_iotlb_all, 2711 .iotlb_sync = amd_iommu_iotlb_sync, 2712 .attach_dev = amd_iommu_attach_device, 2713 .free = amd_iommu_domain_free, 2714 .enforce_cache_coherency = amd_iommu_enforce_cache_coherency, 2715 }; 2716 2717 static const struct iommu_dirty_ops amdv1_dirty_ops = { 2718 IOMMU_PT_DIRTY_OPS(amdv1), 2719 .set_dirty_tracking = amd_iommu_set_dirty_tracking, 2720 }; 2721 2722 static struct iommu_domain *amd_iommu_domain_alloc_paging_v1(struct device *dev, 2723 u32 flags) 2724 { 2725 struct pt_iommu_amdv1_cfg cfg = {}; 2726 struct protection_domain *domain; 2727 int ret; 2728 2729 if (amd_iommu_hatdis) 2730 return ERR_PTR(-EOPNOTSUPP); 2731 2732 domain = protection_domain_alloc(); 2733 if (!domain) 2734 return ERR_PTR(-ENOMEM); 2735 2736 domain->pd_mode = PD_MODE_V1; 2737 domain->iommu.driver_ops = &amd_hw_driver_ops_v1; 2738 domain->iommu.nid = dev_to_node(dev); 2739 if (flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING) 2740 domain->domain.dirty_ops = &amdv1_dirty_ops; 2741 2742 /* 2743 * Someday FORCE_COHERENCE should be set by 2744 * amd_iommu_enforce_cache_coherency() like VT-d does. 2745 */ 2746 cfg.common.features = BIT(PT_FEAT_DYNAMIC_TOP) | 2747 BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES) | 2748 BIT(PT_FEAT_AMDV1_FORCE_COHERENCE); 2749 2750 /* 2751 * AMD's IOMMU can flush as many pages as necessary in a single flush. 2752 * Unless we run in a virtual machine, which can be inferred according 2753 * to whether "non-present cache" is on, it is probably best to prefer 2754 * (potentially) too extensive TLB flushing (i.e., more misses) over 2755 * multiple TLB flushes (i.e., more flushes). For virtual machines the 2756 * hypervisor needs to synchronize the host IOMMU PTEs with those of 2757 * the guest, and the trade-off is different: unnecessary TLB flushes 2758 * should be avoided. 2759 */ 2760 if (amd_iommu_np_cache) 2761 cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE_NO_GAPS); 2762 else 2763 cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE); 2764 2765 cfg.common.hw_max_vasz_lg2 = amd_iommu_hpt_vasize; 2766 cfg.common.hw_max_oasz_lg2 = 52; 2767 cfg.starting_level = 2; 2768 domain->domain.ops = &amdv1_ops; 2769 2770 ret = pt_iommu_amdv1_init(&domain->amdv1, &cfg, GFP_KERNEL); 2771 if (ret) { 2772 amd_iommu_domain_free(&domain->domain); 2773 return ERR_PTR(ret); 2774 } 2775 2776 /* 2777 * Narrow the supported page sizes to those selected by the kernel 2778 * command line. 2779 */ 2780 domain->domain.pgsize_bitmap &= amd_iommu_pgsize_bitmap; 2781 return &domain->domain; 2782 } 2783 2784 static const struct iommu_domain_ops amdv2_ops = { 2785 IOMMU_PT_DOMAIN_OPS(x86_64), 2786 .iotlb_sync_map = amd_iommu_iotlb_sync_map, 2787 .flush_iotlb_all = amd_iommu_flush_iotlb_all, 2788 .iotlb_sync = amd_iommu_iotlb_sync, 2789 .attach_dev = amd_iommu_attach_device, 2790 .free = amd_iommu_domain_free, 2791 /* 2792 * Note the AMDv2 page table format does not support a Force Coherency 2793 * bit, so enforce_cache_coherency should not be set. However VFIO is 2794 * not prepared to handle a case where some domains will support 2795 * enforcement and others do not. VFIO and iommufd will have to be fixed 2796 * before it can fully use the V2 page table. See the comment in 2797 * iommufd_hwpt_paging_alloc(). For now leave things as they have 2798 * historically been and lie about enforce_cache_coherencey. 2799 */ 2800 .enforce_cache_coherency = amd_iommu_enforce_cache_coherency, 2801 }; 2802 2803 static struct iommu_domain *amd_iommu_domain_alloc_paging_v2(struct device *dev, 2804 u32 flags) 2805 { 2806 struct pt_iommu_x86_64_cfg cfg = {}; 2807 struct protection_domain *domain; 2808 int ret; 2809 2810 if (!amd_iommu_v2_pgtbl_supported()) 2811 return ERR_PTR(-EOPNOTSUPP); 2812 2813 domain = protection_domain_alloc(); 2814 if (!domain) 2815 return ERR_PTR(-ENOMEM); 2816 2817 domain->pd_mode = PD_MODE_V2; 2818 domain->iommu.nid = dev_to_node(dev); 2819 2820 cfg.common.features = BIT(PT_FEAT_X86_64_AMD_ENCRYPT_TABLES); 2821 if (amd_iommu_np_cache) 2822 cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE_NO_GAPS); 2823 else 2824 cfg.common.features |= BIT(PT_FEAT_FLUSH_RANGE); 2825 2826 /* 2827 * The v2 table behaves differently if it is attached to PASID 0 vs a 2828 * non-zero PASID. On PASID 0 it has no sign extension and the full 2829 * 57/48 bits decode the lower addresses. Otherwise it behaves like a 2830 * normal sign extended x86 page table. Since we want the domain to work 2831 * in both modes the top bit is removed and PT_FEAT_SIGN_EXTEND is not 2832 * set which creates a table that is compatible in both modes. 2833 */ 2834 if (amd_iommu_gpt_level == PAGE_MODE_5_LEVEL) { 2835 cfg.common.hw_max_vasz_lg2 = 56; 2836 cfg.top_level = 4; 2837 } else { 2838 cfg.common.hw_max_vasz_lg2 = 47; 2839 cfg.top_level = 3; 2840 } 2841 cfg.common.hw_max_oasz_lg2 = 52; 2842 domain->domain.ops = &amdv2_ops; 2843 2844 ret = pt_iommu_x86_64_init(&domain->amdv2, &cfg, GFP_KERNEL); 2845 if (ret) { 2846 amd_iommu_domain_free(&domain->domain); 2847 return ERR_PTR(ret); 2848 } 2849 return &domain->domain; 2850 } 2851 2852 static inline bool is_nest_parent_supported(u32 flags) 2853 { 2854 /* Only allow nest parent when these features are supported */ 2855 return check_feature(FEATURE_GT) && 2856 check_feature(FEATURE_GIOSUP) && 2857 check_feature2(FEATURE_GCR3TRPMODE); 2858 } 2859 2860 static struct iommu_domain * 2861 amd_iommu_domain_alloc_paging_flags(struct device *dev, u32 flags, 2862 const struct iommu_user_data *user_data) 2863 2864 { 2865 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev); 2866 const u32 supported_flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING | 2867 IOMMU_HWPT_ALLOC_PASID | 2868 IOMMU_HWPT_ALLOC_NEST_PARENT; 2869 2870 if ((flags & ~supported_flags) || user_data) 2871 return ERR_PTR(-EOPNOTSUPP); 2872 2873 switch (flags & supported_flags) { 2874 case IOMMU_HWPT_ALLOC_DIRTY_TRACKING: 2875 case IOMMU_HWPT_ALLOC_NEST_PARENT: 2876 case IOMMU_HWPT_ALLOC_DIRTY_TRACKING | IOMMU_HWPT_ALLOC_NEST_PARENT: 2877 /* 2878 * Allocate domain with v1 page table for dirty tracking 2879 * and/or Nest parent. 2880 */ 2881 if ((flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING) && 2882 !amd_iommu_hd_support(iommu)) 2883 break; 2884 2885 if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && 2886 !is_nest_parent_supported(flags)) 2887 break; 2888 2889 return amd_iommu_domain_alloc_paging_v1(dev, flags); 2890 case IOMMU_HWPT_ALLOC_PASID: 2891 /* Allocate domain with v2 page table if IOMMU supports PASID. */ 2892 if (!amd_iommu_pasid_supported()) 2893 break; 2894 return amd_iommu_domain_alloc_paging_v2(dev, flags); 2895 case 0: { 2896 struct iommu_domain *ret; 2897 2898 /* If nothing specific is required use the kernel commandline default */ 2899 if (amd_iommu_pgtable == PD_MODE_V1) { 2900 ret = amd_iommu_domain_alloc_paging_v1(dev, flags); 2901 if (ret != ERR_PTR(-EOPNOTSUPP)) 2902 return ret; 2903 return amd_iommu_domain_alloc_paging_v2(dev, flags); 2904 } 2905 ret = amd_iommu_domain_alloc_paging_v2(dev, flags); 2906 if (ret != ERR_PTR(-EOPNOTSUPP)) 2907 return ret; 2908 return amd_iommu_domain_alloc_paging_v1(dev, flags); 2909 } 2910 default: 2911 break; 2912 } 2913 return ERR_PTR(-EOPNOTSUPP); 2914 } 2915 2916 void amd_iommu_domain_free(struct iommu_domain *dom) 2917 { 2918 struct protection_domain *domain = to_pdomain(dom); 2919 2920 WARN_ON(!list_empty(&domain->dev_list)); 2921 pt_iommu_deinit(&domain->iommu); 2922 amd_iommu_pdom_id_free(domain->id); 2923 kfree(domain); 2924 } 2925 2926 static int blocked_domain_attach_device(struct iommu_domain *domain, 2927 struct device *dev, 2928 struct iommu_domain *old) 2929 { 2930 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev); 2931 2932 if (dev_data->domain) 2933 detach_device(dev); 2934 2935 /* Clear DTE and flush the entry */ 2936 mutex_lock(&dev_data->mutex); 2937 dev_update_dte(dev_data, false); 2938 mutex_unlock(&dev_data->mutex); 2939 2940 return 0; 2941 } 2942 2943 static int blocked_domain_set_dev_pasid(struct iommu_domain *domain, 2944 struct device *dev, ioasid_t pasid, 2945 struct iommu_domain *old) 2946 { 2947 amd_iommu_remove_dev_pasid(dev, pasid, old); 2948 return 0; 2949 } 2950 2951 static struct iommu_domain blocked_domain = { 2952 .type = IOMMU_DOMAIN_BLOCKED, 2953 .ops = &(const struct iommu_domain_ops) { 2954 .attach_dev = blocked_domain_attach_device, 2955 .set_dev_pasid = blocked_domain_set_dev_pasid, 2956 } 2957 }; 2958 2959 static struct protection_domain identity_domain; 2960 2961 static int amd_iommu_identity_attach(struct iommu_domain *dom, struct device *dev, 2962 struct iommu_domain *old) 2963 { 2964 /* 2965 * Don't allow attaching a device to the identity domain if SNP is 2966 * enabled and SNP Mode0 support is not present. 2967 */ 2968 if (amd_iommu_snp_en && !amd_iommu_snp_mode0_sup) 2969 return -EINVAL; 2970 2971 return amd_iommu_attach_device(dom, dev, old); 2972 } 2973 2974 static const struct iommu_domain_ops identity_domain_ops = { 2975 .attach_dev = amd_iommu_identity_attach, 2976 }; 2977 2978 void amd_iommu_init_identity_domain(void) 2979 { 2980 struct iommu_domain *domain = &identity_domain.domain; 2981 2982 domain->type = IOMMU_DOMAIN_IDENTITY; 2983 domain->ops = &identity_domain_ops; 2984 domain->owner = &amd_iommu_ops; 2985 2986 identity_domain.id = amd_iommu_pdom_id_alloc(); 2987 2988 protection_domain_init(&identity_domain); 2989 } 2990 2991 static int amd_iommu_attach_device(struct iommu_domain *dom, struct device *dev, 2992 struct iommu_domain *old) 2993 { 2994 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev); 2995 struct protection_domain *domain = to_pdomain(dom); 2996 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev); 2997 int ret; 2998 2999 /* 3000 * Skip attach device to domain if new domain is same as 3001 * devices current domain 3002 */ 3003 if (dev_data->domain == domain) 3004 return 0; 3005 3006 dev_data->defer_attach = false; 3007 3008 /* 3009 * Restrict to devices with compatible IOMMU hardware support 3010 * when enforcement of dirty tracking is enabled. 3011 */ 3012 if (dom->dirty_ops && !amd_iommu_hd_support(iommu)) 3013 return -EINVAL; 3014 3015 if (dev_data->domain) 3016 detach_device(dev); 3017 3018 ret = attach_device(dev, domain); 3019 3020 #ifdef CONFIG_IRQ_REMAP 3021 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) { 3022 if (dom->type == IOMMU_DOMAIN_UNMANAGED) 3023 dev_data->use_vapic = 1; 3024 else 3025 dev_data->use_vapic = 0; 3026 } 3027 #endif 3028 3029 return ret; 3030 } 3031 3032 static bool amd_iommu_capable(struct device *dev, enum iommu_cap cap) 3033 { 3034 switch (cap) { 3035 case IOMMU_CAP_CACHE_COHERENCY: 3036 return true; 3037 case IOMMU_CAP_NOEXEC: 3038 return false; 3039 case IOMMU_CAP_PRE_BOOT_PROTECTION: 3040 return amdr_ivrs_remap_support; 3041 case IOMMU_CAP_ENFORCE_CACHE_COHERENCY: 3042 return true; 3043 case IOMMU_CAP_DIRTY_TRACKING: { 3044 struct amd_iommu *iommu = get_amd_iommu_from_dev(dev); 3045 3046 return amd_iommu_hd_support(iommu); 3047 } 3048 case IOMMU_CAP_PCI_ATS_SUPPORTED: { 3049 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev); 3050 3051 return amd_iommu_iotlb_sup && 3052 (dev_data->flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP); 3053 } 3054 default: 3055 break; 3056 } 3057 3058 return false; 3059 } 3060 3061 static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain, 3062 bool enable) 3063 { 3064 struct protection_domain *pdomain = to_pdomain(domain); 3065 struct dev_table_entry *dte; 3066 struct iommu_dev_data *dev_data; 3067 bool domain_flush = false; 3068 struct amd_iommu *iommu; 3069 unsigned long flags; 3070 u64 new; 3071 3072 spin_lock_irqsave(&pdomain->lock, flags); 3073 if (!(pdomain->dirty_tracking ^ enable)) { 3074 spin_unlock_irqrestore(&pdomain->lock, flags); 3075 return 0; 3076 } 3077 3078 list_for_each_entry(dev_data, &pdomain->dev_list, list) { 3079 spin_lock(&dev_data->dte_lock); 3080 iommu = get_amd_iommu_from_dev_data(dev_data); 3081 dte = &get_dev_table(iommu)[dev_data->devid]; 3082 new = dte->data[0]; 3083 new = (enable ? new | DTE_FLAG_HAD : new & ~DTE_FLAG_HAD); 3084 dte->data[0] = new; 3085 spin_unlock(&dev_data->dte_lock); 3086 3087 /* Flush device DTE */ 3088 device_flush_dte(dev_data); 3089 domain_flush = true; 3090 } 3091 3092 /* Flush IOTLB to mark IOPTE dirty on the next translation(s) */ 3093 if (domain_flush) 3094 amd_iommu_domain_flush_all(pdomain); 3095 3096 pdomain->dirty_tracking = enable; 3097 spin_unlock_irqrestore(&pdomain->lock, flags); 3098 3099 return 0; 3100 } 3101 3102 static void amd_iommu_get_resv_regions(struct device *dev, 3103 struct list_head *head) 3104 { 3105 struct iommu_resv_region *region; 3106 struct unity_map_entry *entry; 3107 struct amd_iommu *iommu; 3108 struct amd_iommu_pci_seg *pci_seg; 3109 int devid, sbdf; 3110 3111 sbdf = get_device_sbdf_id(dev); 3112 if (sbdf < 0) 3113 return; 3114 3115 devid = PCI_SBDF_TO_DEVID(sbdf); 3116 iommu = get_amd_iommu_from_dev(dev); 3117 pci_seg = iommu->pci_seg; 3118 3119 list_for_each_entry(entry, &pci_seg->unity_map, list) { 3120 int type, prot = 0; 3121 size_t length; 3122 3123 if (devid < entry->devid_start || devid > entry->devid_end) 3124 continue; 3125 3126 type = IOMMU_RESV_DIRECT; 3127 length = entry->address_end - entry->address_start; 3128 if (entry->prot & IOMMU_PROT_IR) 3129 prot |= IOMMU_READ; 3130 if (entry->prot & IOMMU_PROT_IW) 3131 prot |= IOMMU_WRITE; 3132 3133 region = iommu_alloc_resv_region(entry->address_start, 3134 length, prot, type, 3135 GFP_KERNEL); 3136 if (!region) { 3137 dev_err(dev, "Out of memory allocating dm-regions\n"); 3138 return; 3139 } 3140 list_add_tail(®ion->list, head); 3141 } 3142 3143 region = iommu_alloc_resv_region(MSI_RANGE_START, 3144 MSI_RANGE_END - MSI_RANGE_START + 1, 3145 0, IOMMU_RESV_MSI, GFP_KERNEL); 3146 if (!region) 3147 return; 3148 list_add_tail(®ion->list, head); 3149 3150 if (amd_iommu_ht_range_ignore()) 3151 return; 3152 3153 region = iommu_alloc_resv_region(HT_RANGE_START, 3154 HT_RANGE_END - HT_RANGE_START + 1, 3155 0, IOMMU_RESV_RESERVED, GFP_KERNEL); 3156 if (!region) 3157 return; 3158 list_add_tail(®ion->list, head); 3159 } 3160 3161 static bool amd_iommu_is_attach_deferred(struct device *dev) 3162 { 3163 struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev); 3164 3165 return dev_data->defer_attach; 3166 } 3167 3168 static int amd_iommu_def_domain_type(struct device *dev) 3169 { 3170 struct iommu_dev_data *dev_data; 3171 3172 dev_data = dev_iommu_priv_get(dev); 3173 if (!dev_data) 3174 return 0; 3175 3176 /* Always use DMA domain for untrusted device */ 3177 if (dev_is_pci(dev) && to_pci_dev(dev)->untrusted) 3178 return IOMMU_DOMAIN_DMA; 3179 3180 /* 3181 * Do not identity map IOMMUv2 capable devices when: 3182 * - memory encryption is active, because some of those devices 3183 * (AMD GPUs) don't have the encryption bit in their DMA-mask 3184 * and require remapping. 3185 * - SNP is enabled, because it prohibits DTE[Mode]=0. 3186 */ 3187 if (pdev_pasid_supported(dev_data) && 3188 !cc_platform_has(CC_ATTR_MEM_ENCRYPT) && 3189 !amd_iommu_snp_en) { 3190 return IOMMU_DOMAIN_IDENTITY; 3191 } 3192 3193 return 0; 3194 } 3195 3196 static bool amd_iommu_enforce_cache_coherency(struct iommu_domain *domain) 3197 { 3198 /* IOMMU_PTE_FC is always set */ 3199 return true; 3200 } 3201 3202 const struct iommu_ops amd_iommu_ops = { 3203 .capable = amd_iommu_capable, 3204 .hw_info = amd_iommufd_hw_info, 3205 .blocked_domain = &blocked_domain, 3206 .release_domain = &blocked_domain, 3207 .identity_domain = &identity_domain.domain, 3208 .domain_alloc_paging_flags = amd_iommu_domain_alloc_paging_flags, 3209 .domain_alloc_sva = amd_iommu_domain_alloc_sva, 3210 .probe_device = amd_iommu_probe_device, 3211 .release_device = amd_iommu_release_device, 3212 .device_group = amd_iommu_device_group, 3213 .get_resv_regions = amd_iommu_get_resv_regions, 3214 .is_attach_deferred = amd_iommu_is_attach_deferred, 3215 .def_domain_type = amd_iommu_def_domain_type, 3216 .page_response = amd_iommu_page_response, 3217 .get_viommu_size = amd_iommufd_get_viommu_size, 3218 .viommu_init = amd_iommufd_viommu_init, 3219 }; 3220 3221 #ifdef CONFIG_IRQ_REMAP 3222 3223 /***************************************************************************** 3224 * 3225 * Interrupt Remapping Implementation 3226 * 3227 *****************************************************************************/ 3228 3229 static struct irq_chip amd_ir_chip; 3230 static DEFINE_SPINLOCK(iommu_table_lock); 3231 3232 static int iommu_flush_dev_irt(struct pci_dev *unused, u16 devid, void *data) 3233 { 3234 int ret; 3235 struct iommu_cmd cmd; 3236 struct amd_iommu *iommu = data; 3237 3238 build_inv_irt(&cmd, devid); 3239 ret = __iommu_queue_command_sync(iommu, &cmd, true); 3240 return ret; 3241 } 3242 3243 static void iommu_flush_irt_and_complete(struct amd_iommu *iommu, u16 devid) 3244 { 3245 int ret; 3246 u64 data; 3247 unsigned long flags; 3248 struct iommu_cmd cmd; 3249 struct pci_dev *pdev = NULL; 3250 struct iommu_dev_data *dev_data = search_dev_data(iommu, devid); 3251 3252 if (iommu->irtcachedis_enabled) 3253 return; 3254 3255 if (dev_data && dev_data->dev && dev_is_pci(dev_data->dev)) 3256 pdev = to_pci_dev(dev_data->dev); 3257 3258 raw_spin_lock_irqsave(&iommu->lock, flags); 3259 data = get_cmdsem_val(iommu); 3260 build_completion_wait(&cmd, iommu, data); 3261 3262 if (pdev) 3263 ret = pci_for_each_dma_alias(pdev, iommu_flush_dev_irt, iommu); 3264 else 3265 ret = iommu_flush_dev_irt(NULL, devid, iommu); 3266 if (ret) 3267 goto out_err; 3268 3269 ret = __iommu_queue_command_sync(iommu, &cmd, false); 3270 if (ret) 3271 goto out_err; 3272 raw_spin_unlock_irqrestore(&iommu->lock, flags); 3273 3274 wait_on_sem(iommu, data); 3275 return; 3276 3277 out_err: 3278 raw_spin_unlock_irqrestore(&iommu->lock, flags); 3279 } 3280 3281 static inline u8 iommu_get_int_tablen(struct iommu_dev_data *dev_data) 3282 { 3283 if (dev_data && dev_data->max_irqs == MAX_IRQS_PER_TABLE_2K) 3284 return DTE_INTTABLEN_2K; 3285 return DTE_INTTABLEN_512; 3286 } 3287 3288 static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid, 3289 struct irq_remap_table *table) 3290 { 3291 u64 new; 3292 struct dev_table_entry *dte = &get_dev_table(iommu)[devid]; 3293 struct iommu_dev_data *dev_data = search_dev_data(iommu, devid); 3294 3295 if (dev_data) 3296 spin_lock(&dev_data->dte_lock); 3297 3298 new = READ_ONCE(dte->data[2]); 3299 new &= ~DTE_IRQ_PHYS_ADDR_MASK; 3300 new |= iommu_virt_to_phys(table->table); 3301 new |= DTE_IRQ_REMAP_INTCTL; 3302 new |= iommu_get_int_tablen(dev_data); 3303 new |= DTE_IRQ_REMAP_ENABLE; 3304 WRITE_ONCE(dte->data[2], new); 3305 3306 if (dev_data) 3307 spin_unlock(&dev_data->dte_lock); 3308 } 3309 3310 static struct irq_remap_table *get_irq_table(struct amd_iommu *iommu, u16 devid) 3311 { 3312 struct irq_remap_table *table; 3313 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; 3314 3315 if (WARN_ONCE(!pci_seg->rlookup_table[devid], 3316 "%s: no iommu for devid %x:%x\n", 3317 __func__, pci_seg->id, devid)) 3318 return NULL; 3319 3320 table = pci_seg->irq_lookup_table[devid]; 3321 if (WARN_ONCE(!table, "%s: no table for devid %x:%x\n", 3322 __func__, pci_seg->id, devid)) 3323 return NULL; 3324 3325 return table; 3326 } 3327 3328 static struct irq_remap_table *__alloc_irq_table(int nid, size_t size) 3329 { 3330 struct irq_remap_table *table; 3331 3332 table = kzalloc_obj(*table); 3333 if (!table) 3334 return NULL; 3335 3336 table->table = iommu_alloc_pages_node_sz( 3337 nid, GFP_KERNEL, max(DTE_INTTAB_ALIGNMENT, size)); 3338 if (!table->table) { 3339 kfree(table); 3340 return NULL; 3341 } 3342 raw_spin_lock_init(&table->lock); 3343 3344 return table; 3345 } 3346 3347 static void set_remap_table_entry(struct amd_iommu *iommu, u16 devid, 3348 struct irq_remap_table *table) 3349 { 3350 struct amd_iommu_pci_seg *pci_seg = iommu->pci_seg; 3351 3352 pci_seg->irq_lookup_table[devid] = table; 3353 set_dte_irq_entry(iommu, devid, table); 3354 iommu_flush_dte(iommu, devid); 3355 } 3356 3357 static int set_remap_table_entry_alias(struct pci_dev *pdev, u16 alias, 3358 void *data) 3359 { 3360 struct irq_remap_table *table = data; 3361 struct amd_iommu_pci_seg *pci_seg; 3362 struct amd_iommu *iommu = rlookup_amd_iommu(&pdev->dev); 3363 3364 if (!iommu) 3365 return -EINVAL; 3366 3367 pci_seg = iommu->pci_seg; 3368 pci_seg->irq_lookup_table[alias] = table; 3369 set_dte_irq_entry(iommu, alias, table); 3370 iommu_flush_dte(pci_seg->rlookup_table[alias], alias); 3371 3372 return 0; 3373 } 3374 3375 static inline size_t get_irq_table_size(unsigned int max_irqs) 3376 { 3377 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir)) 3378 return max_irqs * sizeof(u32); 3379 3380 return max_irqs * (sizeof(u64) * 2); 3381 } 3382 3383 static struct irq_remap_table *alloc_irq_table(struct amd_iommu *iommu, 3384 u16 devid, struct pci_dev *pdev, 3385 unsigned int max_irqs) 3386 { 3387 struct irq_remap_table *table = NULL; 3388 struct irq_remap_table *new_table = NULL; 3389 struct amd_iommu_pci_seg *pci_seg; 3390 unsigned long flags; 3391 int nid = iommu->dev ? dev_to_node(&iommu->dev->dev) : NUMA_NO_NODE; 3392 u16 alias; 3393 3394 spin_lock_irqsave(&iommu_table_lock, flags); 3395 3396 pci_seg = iommu->pci_seg; 3397 table = pci_seg->irq_lookup_table[devid]; 3398 if (table) 3399 goto out_unlock; 3400 3401 alias = pci_seg->alias_table[devid]; 3402 table = pci_seg->irq_lookup_table[alias]; 3403 if (table) { 3404 set_remap_table_entry(iommu, devid, table); 3405 goto out_wait; 3406 } 3407 spin_unlock_irqrestore(&iommu_table_lock, flags); 3408 3409 /* Nothing there yet, allocate new irq remapping table */ 3410 new_table = __alloc_irq_table(nid, get_irq_table_size(max_irqs)); 3411 if (!new_table) 3412 return NULL; 3413 3414 spin_lock_irqsave(&iommu_table_lock, flags); 3415 3416 table = pci_seg->irq_lookup_table[devid]; 3417 if (table) 3418 goto out_unlock; 3419 3420 table = pci_seg->irq_lookup_table[alias]; 3421 if (table) { 3422 set_remap_table_entry(iommu, devid, table); 3423 goto out_wait; 3424 } 3425 3426 table = new_table; 3427 new_table = NULL; 3428 3429 if (pdev) 3430 pci_for_each_dma_alias(pdev, set_remap_table_entry_alias, 3431 table); 3432 else 3433 set_remap_table_entry(iommu, devid, table); 3434 3435 if (devid != alias) 3436 set_remap_table_entry(iommu, alias, table); 3437 3438 out_wait: 3439 iommu_completion_wait(iommu); 3440 3441 out_unlock: 3442 spin_unlock_irqrestore(&iommu_table_lock, flags); 3443 3444 if (new_table) { 3445 iommu_free_pages(new_table->table); 3446 kfree(new_table); 3447 } 3448 return table; 3449 } 3450 3451 static int alloc_irq_index(struct amd_iommu *iommu, u16 devid, int count, 3452 bool align, struct pci_dev *pdev, 3453 unsigned long max_irqs) 3454 { 3455 struct irq_remap_table *table; 3456 int index, c, alignment = 1; 3457 unsigned long flags; 3458 3459 table = alloc_irq_table(iommu, devid, pdev, max_irqs); 3460 if (!table) 3461 return -ENODEV; 3462 3463 if (align) 3464 alignment = roundup_pow_of_two(count); 3465 3466 raw_spin_lock_irqsave(&table->lock, flags); 3467 3468 /* Scan table for free entries */ 3469 for (index = ALIGN(table->min_index, alignment), c = 0; 3470 index < max_irqs;) { 3471 if (!iommu->irte_ops->is_allocated(table, index)) { 3472 c += 1; 3473 } else { 3474 c = 0; 3475 index = ALIGN(index + 1, alignment); 3476 continue; 3477 } 3478 3479 if (c == count) { 3480 for (; c != 0; --c) 3481 iommu->irte_ops->set_allocated(table, index - c + 1); 3482 3483 index -= count - 1; 3484 goto out; 3485 } 3486 3487 index++; 3488 } 3489 3490 index = -ENOSPC; 3491 3492 out: 3493 raw_spin_unlock_irqrestore(&table->lock, flags); 3494 3495 return index; 3496 } 3497 3498 static int __modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index, 3499 struct irte_ga *irte) 3500 { 3501 struct irq_remap_table *table; 3502 struct irte_ga *entry; 3503 unsigned long flags; 3504 u128 old; 3505 3506 table = get_irq_table(iommu, devid); 3507 if (!table) 3508 return -ENOMEM; 3509 3510 raw_spin_lock_irqsave(&table->lock, flags); 3511 3512 entry = (struct irte_ga *)table->table; 3513 entry = &entry[index]; 3514 3515 /* 3516 * We use cmpxchg16 to atomically update the 128-bit IRTE, 3517 * and it cannot be updated by the hardware or other processors 3518 * behind us, so the return value of cmpxchg16 should be the 3519 * same as the old value. 3520 */ 3521 old = entry->irte; 3522 WARN_ON(!try_cmpxchg128(&entry->irte, &old, irte->irte)); 3523 3524 raw_spin_unlock_irqrestore(&table->lock, flags); 3525 3526 return 0; 3527 } 3528 3529 static int modify_irte_ga(struct amd_iommu *iommu, u16 devid, int index, 3530 struct irte_ga *irte) 3531 { 3532 int ret; 3533 3534 ret = __modify_irte_ga(iommu, devid, index, irte); 3535 if (ret) 3536 return ret; 3537 3538 iommu_flush_irt_and_complete(iommu, devid); 3539 3540 return 0; 3541 } 3542 3543 static int modify_irte(struct amd_iommu *iommu, 3544 u16 devid, int index, union irte *irte) 3545 { 3546 struct irq_remap_table *table; 3547 unsigned long flags; 3548 3549 table = get_irq_table(iommu, devid); 3550 if (!table) 3551 return -ENOMEM; 3552 3553 raw_spin_lock_irqsave(&table->lock, flags); 3554 table->table[index] = irte->val; 3555 raw_spin_unlock_irqrestore(&table->lock, flags); 3556 3557 iommu_flush_irt_and_complete(iommu, devid); 3558 3559 return 0; 3560 } 3561 3562 static void free_irte(struct amd_iommu *iommu, u16 devid, int index) 3563 { 3564 struct irq_remap_table *table; 3565 unsigned long flags; 3566 3567 table = get_irq_table(iommu, devid); 3568 if (!table) 3569 return; 3570 3571 raw_spin_lock_irqsave(&table->lock, flags); 3572 iommu->irte_ops->clear_allocated(table, index); 3573 raw_spin_unlock_irqrestore(&table->lock, flags); 3574 3575 iommu_flush_irt_and_complete(iommu, devid); 3576 } 3577 3578 static void irte_prepare(void *entry, 3579 u32 delivery_mode, bool dest_mode, 3580 u8 vector, u32 dest_apicid, int devid) 3581 { 3582 union irte *irte = (union irte *) entry; 3583 3584 irte->val = 0; 3585 irte->fields.vector = vector; 3586 irte->fields.int_type = delivery_mode; 3587 irte->fields.destination = dest_apicid; 3588 irte->fields.dm = dest_mode; 3589 irte->fields.valid = 1; 3590 } 3591 3592 static void irte_ga_prepare(void *entry, 3593 u32 delivery_mode, bool dest_mode, 3594 u8 vector, u32 dest_apicid, int devid) 3595 { 3596 struct irte_ga *irte = (struct irte_ga *) entry; 3597 3598 irte->lo.val = 0; 3599 irte->hi.val = 0; 3600 irte->lo.fields_remap.int_type = delivery_mode; 3601 irte->lo.fields_remap.dm = dest_mode; 3602 irte->hi.fields.vector = vector; 3603 irte->lo.fields_remap.destination = APICID_TO_IRTE_DEST_LO(dest_apicid); 3604 irte->hi.fields.destination = APICID_TO_IRTE_DEST_HI(dest_apicid); 3605 irte->lo.fields_remap.valid = 1; 3606 } 3607 3608 static void irte_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index) 3609 { 3610 union irte *irte = (union irte *) entry; 3611 3612 irte->fields.valid = 1; 3613 modify_irte(iommu, devid, index, irte); 3614 } 3615 3616 static void irte_ga_activate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index) 3617 { 3618 struct irte_ga *irte = (struct irte_ga *) entry; 3619 3620 irte->lo.fields_remap.valid = 1; 3621 modify_irte_ga(iommu, devid, index, irte); 3622 } 3623 3624 static void irte_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index) 3625 { 3626 union irte *irte = (union irte *) entry; 3627 3628 irte->fields.valid = 0; 3629 modify_irte(iommu, devid, index, irte); 3630 } 3631 3632 static void irte_ga_deactivate(struct amd_iommu *iommu, void *entry, u16 devid, u16 index) 3633 { 3634 struct irte_ga *irte = (struct irte_ga *) entry; 3635 3636 irte->lo.fields_remap.valid = 0; 3637 modify_irte_ga(iommu, devid, index, irte); 3638 } 3639 3640 static void irte_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index, 3641 u8 vector, u32 dest_apicid) 3642 { 3643 union irte *irte = (union irte *) entry; 3644 3645 irte->fields.vector = vector; 3646 irte->fields.destination = dest_apicid; 3647 modify_irte(iommu, devid, index, irte); 3648 } 3649 3650 static void irte_ga_set_affinity(struct amd_iommu *iommu, void *entry, u16 devid, u16 index, 3651 u8 vector, u32 dest_apicid) 3652 { 3653 struct irte_ga *irte = (struct irte_ga *) entry; 3654 3655 if (!irte->lo.fields_remap.guest_mode) { 3656 irte->hi.fields.vector = vector; 3657 irte->lo.fields_remap.destination = 3658 APICID_TO_IRTE_DEST_LO(dest_apicid); 3659 irte->hi.fields.destination = 3660 APICID_TO_IRTE_DEST_HI(dest_apicid); 3661 modify_irte_ga(iommu, devid, index, irte); 3662 } 3663 } 3664 3665 #define IRTE_ALLOCATED (~1U) 3666 static void irte_set_allocated(struct irq_remap_table *table, int index) 3667 { 3668 table->table[index] = IRTE_ALLOCATED; 3669 } 3670 3671 static void irte_ga_set_allocated(struct irq_remap_table *table, int index) 3672 { 3673 struct irte_ga *ptr = (struct irte_ga *)table->table; 3674 struct irte_ga *irte = &ptr[index]; 3675 3676 memset(&irte->lo.val, 0, sizeof(u64)); 3677 memset(&irte->hi.val, 0, sizeof(u64)); 3678 irte->hi.fields.vector = 0xff; 3679 } 3680 3681 static bool irte_is_allocated(struct irq_remap_table *table, int index) 3682 { 3683 union irte *ptr = (union irte *)table->table; 3684 union irte *irte = &ptr[index]; 3685 3686 return irte->val != 0; 3687 } 3688 3689 static bool irte_ga_is_allocated(struct irq_remap_table *table, int index) 3690 { 3691 struct irte_ga *ptr = (struct irte_ga *)table->table; 3692 struct irte_ga *irte = &ptr[index]; 3693 3694 return irte->hi.fields.vector != 0; 3695 } 3696 3697 static void irte_clear_allocated(struct irq_remap_table *table, int index) 3698 { 3699 table->table[index] = 0; 3700 } 3701 3702 static void irte_ga_clear_allocated(struct irq_remap_table *table, int index) 3703 { 3704 struct irte_ga *ptr = (struct irte_ga *)table->table; 3705 struct irte_ga *irte = &ptr[index]; 3706 3707 memset(&irte->lo.val, 0, sizeof(u64)); 3708 memset(&irte->hi.val, 0, sizeof(u64)); 3709 } 3710 3711 static int get_devid(struct irq_alloc_info *info) 3712 { 3713 switch (info->type) { 3714 case X86_IRQ_ALLOC_TYPE_IOAPIC: 3715 return get_ioapic_devid(info->devid); 3716 case X86_IRQ_ALLOC_TYPE_HPET: 3717 return get_hpet_devid(info->devid); 3718 case X86_IRQ_ALLOC_TYPE_PCI_MSI: 3719 case X86_IRQ_ALLOC_TYPE_PCI_MSIX: 3720 return get_device_sbdf_id(msi_desc_to_dev(info->desc)); 3721 default: 3722 WARN_ON_ONCE(1); 3723 return -1; 3724 } 3725 } 3726 3727 struct irq_remap_ops amd_iommu_irq_ops = { 3728 .prepare = amd_iommu_prepare, 3729 .enable = amd_iommu_enable, 3730 .disable = amd_iommu_disable, 3731 .reenable = amd_iommu_reenable, 3732 .enable_faulting = amd_iommu_enable_faulting, 3733 }; 3734 3735 static void fill_msi_msg(struct msi_msg *msg, u32 index) 3736 { 3737 msg->data = index; 3738 msg->address_lo = 0; 3739 msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW; 3740 /* 3741 * The struct msi_msg.dest_mode_logical is used to set the DM bit 3742 * in MSI Message Address Register. For device w/ 2K int-remap support, 3743 * this bit must be set to 1 regardless of the actual destination 3744 * mode, which is signified by the IRTE[DM]. 3745 */ 3746 if (FEATURE_NUM_INT_REMAP_SUP_2K(amd_iommu_efr2)) 3747 msg->arch_addr_lo.dest_mode_logical = true; 3748 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH; 3749 } 3750 3751 static void irq_remapping_prepare_irte(struct amd_ir_data *data, 3752 struct irq_cfg *irq_cfg, 3753 struct irq_alloc_info *info, 3754 int devid, int index, int sub_handle) 3755 { 3756 struct irq_2_irte *irte_info = &data->irq_2_irte; 3757 struct amd_iommu *iommu = data->iommu; 3758 3759 if (!iommu) 3760 return; 3761 3762 data->irq_2_irte.devid = devid; 3763 data->irq_2_irte.index = index + sub_handle; 3764 iommu->irte_ops->prepare(data->entry, APIC_DELIVERY_MODE_FIXED, 3765 apic->dest_mode_logical, irq_cfg->vector, 3766 irq_cfg->dest_apicid, devid); 3767 3768 switch (info->type) { 3769 case X86_IRQ_ALLOC_TYPE_IOAPIC: 3770 case X86_IRQ_ALLOC_TYPE_HPET: 3771 case X86_IRQ_ALLOC_TYPE_PCI_MSI: 3772 case X86_IRQ_ALLOC_TYPE_PCI_MSIX: 3773 fill_msi_msg(&data->msi_entry, irte_info->index); 3774 break; 3775 3776 default: 3777 BUG_ON(1); 3778 break; 3779 } 3780 } 3781 3782 struct amd_irte_ops irte_32_ops = { 3783 .prepare = irte_prepare, 3784 .activate = irte_activate, 3785 .deactivate = irte_deactivate, 3786 .set_affinity = irte_set_affinity, 3787 .set_allocated = irte_set_allocated, 3788 .is_allocated = irte_is_allocated, 3789 .clear_allocated = irte_clear_allocated, 3790 }; 3791 3792 struct amd_irte_ops irte_128_ops = { 3793 .prepare = irte_ga_prepare, 3794 .activate = irte_ga_activate, 3795 .deactivate = irte_ga_deactivate, 3796 .set_affinity = irte_ga_set_affinity, 3797 .set_allocated = irte_ga_set_allocated, 3798 .is_allocated = irte_ga_is_allocated, 3799 .clear_allocated = irte_ga_clear_allocated, 3800 }; 3801 3802 static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq, 3803 unsigned int nr_irqs, void *arg) 3804 { 3805 struct irq_alloc_info *info = arg; 3806 struct irq_data *irq_data; 3807 struct amd_ir_data *data = NULL; 3808 struct amd_iommu *iommu; 3809 struct irq_cfg *cfg; 3810 struct iommu_dev_data *dev_data; 3811 unsigned long max_irqs; 3812 int i, ret, devid, seg, sbdf; 3813 int index; 3814 3815 if (!info) 3816 return -EINVAL; 3817 if (nr_irqs > 1 && info->type != X86_IRQ_ALLOC_TYPE_PCI_MSI) 3818 return -EINVAL; 3819 3820 sbdf = get_devid(info); 3821 if (sbdf < 0) 3822 return -EINVAL; 3823 3824 seg = PCI_SBDF_TO_SEGID(sbdf); 3825 devid = PCI_SBDF_TO_DEVID(sbdf); 3826 iommu = __rlookup_amd_iommu(seg, devid); 3827 if (!iommu) 3828 return -EINVAL; 3829 3830 dev_data = search_dev_data(iommu, devid); 3831 max_irqs = dev_data ? dev_data->max_irqs : MAX_IRQS_PER_TABLE_512; 3832 3833 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg); 3834 if (ret < 0) 3835 return ret; 3836 3837 if (info->type == X86_IRQ_ALLOC_TYPE_IOAPIC) { 3838 struct irq_remap_table *table; 3839 3840 table = alloc_irq_table(iommu, devid, NULL, max_irqs); 3841 if (table) { 3842 if (!table->min_index) { 3843 /* 3844 * Keep the first 32 indexes free for IOAPIC 3845 * interrupts. 3846 */ 3847 table->min_index = 32; 3848 for (i = 0; i < 32; ++i) 3849 iommu->irte_ops->set_allocated(table, i); 3850 } 3851 WARN_ON(table->min_index != 32); 3852 index = info->ioapic.pin; 3853 } else { 3854 index = -ENOMEM; 3855 } 3856 } else if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI || 3857 info->type == X86_IRQ_ALLOC_TYPE_PCI_MSIX) { 3858 bool align = (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI); 3859 3860 index = alloc_irq_index(iommu, devid, nr_irqs, align, 3861 msi_desc_to_pci_dev(info->desc), 3862 max_irqs); 3863 } else { 3864 index = alloc_irq_index(iommu, devid, nr_irqs, false, NULL, 3865 max_irqs); 3866 } 3867 3868 if (index < 0) { 3869 pr_warn("Failed to allocate IRTE\n"); 3870 ret = index; 3871 goto out_free_parent; 3872 } 3873 3874 for (i = 0; i < nr_irqs; i++) { 3875 irq_data = irq_domain_get_irq_data(domain, virq + i); 3876 cfg = irq_data ? irqd_cfg(irq_data) : NULL; 3877 if (!cfg) { 3878 ret = -EINVAL; 3879 goto out_free_data; 3880 } 3881 3882 ret = -ENOMEM; 3883 data = kzalloc_obj(*data); 3884 if (!data) 3885 goto out_free_data; 3886 3887 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir)) 3888 data->entry = kzalloc_obj(union irte); 3889 else 3890 data->entry = kzalloc_obj(struct irte_ga); 3891 if (!data->entry) { 3892 kfree(data); 3893 goto out_free_data; 3894 } 3895 3896 data->iommu = iommu; 3897 irq_data->hwirq = (devid << 16) + i; 3898 irq_data->chip_data = data; 3899 irq_data->chip = &amd_ir_chip; 3900 irq_remapping_prepare_irte(data, cfg, info, devid, index, i); 3901 } 3902 3903 return 0; 3904 3905 out_free_data: 3906 for (i--; i >= 0; i--) { 3907 irq_data = irq_domain_get_irq_data(domain, virq + i); 3908 if (irq_data) 3909 kfree(irq_data->chip_data); 3910 } 3911 for (i = 0; i < nr_irqs; i++) 3912 free_irte(iommu, devid, index + i); 3913 out_free_parent: 3914 irq_domain_free_irqs_common(domain, virq, nr_irqs); 3915 return ret; 3916 } 3917 3918 static void irq_remapping_free(struct irq_domain *domain, unsigned int virq, 3919 unsigned int nr_irqs) 3920 { 3921 struct irq_2_irte *irte_info; 3922 struct irq_data *irq_data; 3923 struct amd_ir_data *data; 3924 int i; 3925 3926 for (i = 0; i < nr_irqs; i++) { 3927 irq_data = irq_domain_get_irq_data(domain, virq + i); 3928 if (irq_data && irq_data->chip_data) { 3929 data = irq_data->chip_data; 3930 irte_info = &data->irq_2_irte; 3931 free_irte(data->iommu, irte_info->devid, irte_info->index); 3932 kfree(data->entry); 3933 kfree(data); 3934 } 3935 } 3936 irq_domain_free_irqs_common(domain, virq, nr_irqs); 3937 } 3938 3939 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu, 3940 struct amd_ir_data *ir_data, 3941 struct irq_2_irte *irte_info, 3942 struct irq_cfg *cfg); 3943 3944 static int irq_remapping_activate(struct irq_domain *domain, 3945 struct irq_data *irq_data, bool reserve) 3946 { 3947 struct amd_ir_data *data = irq_data->chip_data; 3948 struct irq_2_irte *irte_info = &data->irq_2_irte; 3949 struct amd_iommu *iommu = data->iommu; 3950 struct irq_cfg *cfg = irqd_cfg(irq_data); 3951 3952 if (!iommu) 3953 return 0; 3954 3955 iommu->irte_ops->activate(iommu, data->entry, irte_info->devid, 3956 irte_info->index); 3957 amd_ir_update_irte(irq_data, iommu, data, irte_info, cfg); 3958 return 0; 3959 } 3960 3961 static void irq_remapping_deactivate(struct irq_domain *domain, 3962 struct irq_data *irq_data) 3963 { 3964 struct amd_ir_data *data = irq_data->chip_data; 3965 struct irq_2_irte *irte_info = &data->irq_2_irte; 3966 struct amd_iommu *iommu = data->iommu; 3967 3968 if (iommu) 3969 iommu->irte_ops->deactivate(iommu, data->entry, irte_info->devid, 3970 irte_info->index); 3971 } 3972 3973 static int irq_remapping_select(struct irq_domain *d, struct irq_fwspec *fwspec, 3974 enum irq_domain_bus_token bus_token) 3975 { 3976 struct amd_iommu *iommu; 3977 int devid = -1; 3978 3979 if (!amd_iommu_irq_remap) 3980 return 0; 3981 3982 if (x86_fwspec_is_ioapic(fwspec)) 3983 devid = get_ioapic_devid(fwspec->param[0]); 3984 else if (x86_fwspec_is_hpet(fwspec)) 3985 devid = get_hpet_devid(fwspec->param[0]); 3986 3987 if (devid < 0) 3988 return 0; 3989 iommu = __rlookup_amd_iommu((devid >> 16), (devid & 0xffff)); 3990 3991 return iommu && iommu->ir_domain == d; 3992 } 3993 3994 static const struct irq_domain_ops amd_ir_domain_ops = { 3995 .select = irq_remapping_select, 3996 .alloc = irq_remapping_alloc, 3997 .free = irq_remapping_free, 3998 .activate = irq_remapping_activate, 3999 .deactivate = irq_remapping_deactivate, 4000 }; 4001 4002 static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu, 4003 bool ga_log_intr) 4004 { 4005 if (cpu >= 0) { 4006 entry->lo.fields_vapic.destination = 4007 APICID_TO_IRTE_DEST_LO(cpu); 4008 entry->hi.fields.destination = 4009 APICID_TO_IRTE_DEST_HI(cpu); 4010 entry->lo.fields_vapic.is_run = true; 4011 entry->lo.fields_vapic.ga_log_intr = false; 4012 } else { 4013 entry->lo.fields_vapic.is_run = false; 4014 entry->lo.fields_vapic.ga_log_intr = ga_log_intr; 4015 } 4016 } 4017 4018 /* 4019 * Update the pCPU information for an IRTE that is configured to post IRQs to 4020 * a vCPU, without issuing an IOMMU invalidation for the IRTE. 4021 * 4022 * If the vCPU is associated with a pCPU (@cpu >= 0), configure the Destination 4023 * with the pCPU's APIC ID, set IsRun, and clear GALogIntr. If the vCPU isn't 4024 * associated with a pCPU (@cpu < 0), clear IsRun and set/clear GALogIntr based 4025 * on input from the caller (e.g. KVM only requests GALogIntr when the vCPU is 4026 * blocking and requires a notification wake event). I.e. treat vCPUs that are 4027 * associated with a pCPU as running. This API is intended to be used when a 4028 * vCPU is scheduled in/out (or stops running for any reason), to do a fast 4029 * update of IsRun, GALogIntr, and (conditionally) Destination. 4030 * 4031 * Per the IOMMU spec, the Destination, IsRun, and GATag fields are not cached 4032 * and thus don't require an invalidation to ensure the IOMMU consumes fresh 4033 * information. 4034 */ 4035 int amd_iommu_update_ga(void *data, int cpu, bool ga_log_intr) 4036 { 4037 struct amd_ir_data *ir_data = (struct amd_ir_data *)data; 4038 struct irte_ga *entry = (struct irte_ga *) ir_data->entry; 4039 4040 if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) 4041 return -EINVAL; 4042 4043 if (!entry || !entry->lo.fields_vapic.guest_mode) 4044 return 0; 4045 4046 if (!ir_data->iommu) 4047 return -ENODEV; 4048 4049 __amd_iommu_update_ga(entry, cpu, ga_log_intr); 4050 4051 return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, 4052 ir_data->irq_2_irte.index, entry); 4053 } 4054 EXPORT_SYMBOL(amd_iommu_update_ga); 4055 4056 int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_intr) 4057 { 4058 struct amd_ir_data *ir_data = (struct amd_ir_data *)data; 4059 struct irte_ga *entry = (struct irte_ga *) ir_data->entry; 4060 u64 valid; 4061 4062 if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) 4063 return -EINVAL; 4064 4065 if (!entry) 4066 return 0; 4067 4068 valid = entry->lo.fields_vapic.valid; 4069 4070 entry->lo.val = 0; 4071 entry->hi.val = 0; 4072 4073 entry->lo.fields_vapic.valid = valid; 4074 entry->lo.fields_vapic.guest_mode = 1; 4075 entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr; 4076 entry->hi.fields.vector = ir_data->ga_vector; 4077 entry->lo.fields_vapic.ga_tag = ir_data->ga_tag; 4078 4079 __amd_iommu_update_ga(entry, cpu, ga_log_intr); 4080 4081 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, 4082 ir_data->irq_2_irte.index, entry); 4083 } 4084 EXPORT_SYMBOL(amd_iommu_activate_guest_mode); 4085 4086 int amd_iommu_deactivate_guest_mode(void *data) 4087 { 4088 struct amd_ir_data *ir_data = (struct amd_ir_data *)data; 4089 struct irte_ga *entry = (struct irte_ga *) ir_data->entry; 4090 struct irq_cfg *cfg = ir_data->cfg; 4091 u64 valid; 4092 4093 if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) 4094 return -EINVAL; 4095 4096 if (!entry || !entry->lo.fields_vapic.guest_mode) 4097 return 0; 4098 4099 valid = entry->lo.fields_remap.valid; 4100 4101 entry->lo.val = 0; 4102 entry->hi.val = 0; 4103 4104 entry->lo.fields_remap.valid = valid; 4105 entry->lo.fields_remap.dm = apic->dest_mode_logical; 4106 entry->lo.fields_remap.int_type = APIC_DELIVERY_MODE_FIXED; 4107 entry->hi.fields.vector = cfg->vector; 4108 entry->lo.fields_remap.destination = 4109 APICID_TO_IRTE_DEST_LO(cfg->dest_apicid); 4110 entry->hi.fields.destination = 4111 APICID_TO_IRTE_DEST_HI(cfg->dest_apicid); 4112 4113 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, 4114 ir_data->irq_2_irte.index, entry); 4115 } 4116 EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode); 4117 4118 static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info) 4119 { 4120 int ret; 4121 struct amd_iommu_pi_data *pi_data = info; 4122 struct amd_ir_data *ir_data = data->chip_data; 4123 struct irq_2_irte *irte_info = &ir_data->irq_2_irte; 4124 struct iommu_dev_data *dev_data; 4125 4126 if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) 4127 return -EINVAL; 4128 4129 if (ir_data->iommu == NULL) 4130 return -EINVAL; 4131 4132 dev_data = search_dev_data(ir_data->iommu, irte_info->devid); 4133 4134 /* Note: 4135 * This device has never been set up for guest mode. 4136 * we should not modify the IRTE 4137 */ 4138 if (!dev_data || !dev_data->use_vapic) 4139 return -EINVAL; 4140 4141 ir_data->cfg = irqd_cfg(data); 4142 4143 if (pi_data) { 4144 pi_data->ir_data = ir_data; 4145 4146 ir_data->ga_root_ptr = (pi_data->vapic_addr >> 12); 4147 ir_data->ga_vector = pi_data->vector; 4148 ir_data->ga_tag = pi_data->ga_tag; 4149 if (pi_data->is_guest_mode) 4150 ret = amd_iommu_activate_guest_mode(ir_data, pi_data->cpu, 4151 pi_data->ga_log_intr); 4152 else 4153 ret = amd_iommu_deactivate_guest_mode(ir_data); 4154 } else { 4155 ret = amd_iommu_deactivate_guest_mode(ir_data); 4156 } 4157 4158 return ret; 4159 } 4160 4161 4162 static void amd_ir_update_irte(struct irq_data *irqd, struct amd_iommu *iommu, 4163 struct amd_ir_data *ir_data, 4164 struct irq_2_irte *irte_info, 4165 struct irq_cfg *cfg) 4166 { 4167 4168 /* 4169 * Atomically updates the IRTE with the new destination, vector 4170 * and flushes the interrupt entry cache. 4171 */ 4172 iommu->irte_ops->set_affinity(iommu, ir_data->entry, irte_info->devid, 4173 irte_info->index, cfg->vector, 4174 cfg->dest_apicid); 4175 } 4176 4177 static int amd_ir_set_affinity(struct irq_data *data, 4178 const struct cpumask *mask, bool force) 4179 { 4180 struct amd_ir_data *ir_data = data->chip_data; 4181 struct irq_2_irte *irte_info = &ir_data->irq_2_irte; 4182 struct irq_cfg *cfg = irqd_cfg(data); 4183 struct irq_data *parent = data->parent_data; 4184 struct amd_iommu *iommu = ir_data->iommu; 4185 int ret; 4186 4187 if (!iommu) 4188 return -ENODEV; 4189 4190 ret = parent->chip->irq_set_affinity(parent, mask, force); 4191 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE) 4192 return ret; 4193 4194 amd_ir_update_irte(data, iommu, ir_data, irte_info, cfg); 4195 /* 4196 * After this point, all the interrupts will start arriving 4197 * at the new destination. So, time to cleanup the previous 4198 * vector allocation. 4199 */ 4200 vector_schedule_cleanup(cfg); 4201 4202 return IRQ_SET_MASK_OK_DONE; 4203 } 4204 4205 static void ir_compose_msi_msg(struct irq_data *irq_data, struct msi_msg *msg) 4206 { 4207 struct amd_ir_data *ir_data = irq_data->chip_data; 4208 4209 *msg = ir_data->msi_entry; 4210 } 4211 4212 static struct irq_chip amd_ir_chip = { 4213 .name = "AMD-IR", 4214 .irq_ack = apic_ack_irq, 4215 .irq_set_affinity = amd_ir_set_affinity, 4216 .irq_set_vcpu_affinity = amd_ir_set_vcpu_affinity, 4217 .irq_compose_msi_msg = ir_compose_msi_msg, 4218 }; 4219 4220 static const struct msi_parent_ops amdvi_msi_parent_ops = { 4221 .supported_flags = X86_VECTOR_MSI_FLAGS_SUPPORTED | MSI_FLAG_MULTI_PCI_MSI, 4222 .bus_select_token = DOMAIN_BUS_AMDVI, 4223 .bus_select_mask = MATCH_PCI_MSI, 4224 .prefix = "IR-", 4225 .init_dev_msi_info = msi_parent_init_dev_msi_info, 4226 }; 4227 4228 int amd_iommu_create_irq_domain(struct amd_iommu *iommu) 4229 { 4230 struct irq_domain_info info = { 4231 .fwnode = irq_domain_alloc_named_id_fwnode("AMD-IR", iommu->index), 4232 .ops = &amd_ir_domain_ops, 4233 .domain_flags = IRQ_DOMAIN_FLAG_ISOLATED_MSI, 4234 .host_data = iommu, 4235 .parent = arch_get_ir_parent_domain(), 4236 }; 4237 4238 if (!info.fwnode) 4239 return -ENOMEM; 4240 4241 iommu->ir_domain = msi_create_parent_irq_domain(&info, &amdvi_msi_parent_ops); 4242 if (!iommu->ir_domain) { 4243 irq_domain_free_fwnode(info.fwnode); 4244 return -ENOMEM; 4245 } 4246 return 0; 4247 } 4248 #endif 4249 4250 MODULE_IMPORT_NS("GENERIC_PT_IOMMU"); 4251