1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. 4 * Author: Joerg Roedel <jroedel@suse.de> 5 */ 6 7 #define pr_fmt(fmt) "iommu: " fmt 8 9 #include <linux/amba/bus.h> 10 #include <linux/device.h> 11 #include <linux/kernel.h> 12 #include <linux/bits.h> 13 #include <linux/bug.h> 14 #include <linux/types.h> 15 #include <linux/init.h> 16 #include <linux/export.h> 17 #include <linux/slab.h> 18 #include <linux/errno.h> 19 #include <linux/host1x_context_bus.h> 20 #include <linux/iommu.h> 21 #include <linux/iommufd.h> 22 #include <linux/idr.h> 23 #include <linux/err.h> 24 #include <linux/pci.h> 25 #include <linux/pci-ats.h> 26 #include <linux/bitops.h> 27 #include <linux/platform_device.h> 28 #include <linux/property.h> 29 #include <linux/fsl/mc.h> 30 #include <linux/module.h> 31 #include <linux/cc_platform.h> 32 #include <linux/cdx/cdx_bus.h> 33 #include <trace/events/iommu.h> 34 #include <linux/sched/mm.h> 35 #include <linux/msi.h> 36 #include <uapi/linux/iommufd.h> 37 38 #include "dma-iommu.h" 39 #include "iommu-priv.h" 40 41 static struct kset *iommu_group_kset; 42 static DEFINE_IDA(iommu_group_ida); 43 static DEFINE_IDA(iommu_global_pasid_ida); 44 45 static unsigned int iommu_def_domain_type __read_mostly; 46 static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT); 47 static u32 iommu_cmd_line __read_mostly; 48 49 /* Tags used with xa_tag_pointer() in group->pasid_array */ 50 enum { IOMMU_PASID_ARRAY_DOMAIN = 0, IOMMU_PASID_ARRAY_HANDLE = 1 }; 51 52 struct iommu_group { 53 struct kobject kobj; 54 struct kobject *devices_kobj; 55 struct list_head devices; 56 struct xarray pasid_array; 57 struct mutex mutex; 58 void *iommu_data; 59 void (*iommu_data_release)(void *iommu_data); 60 char *name; 61 int id; 62 struct iommu_domain *default_domain; 63 struct iommu_domain *blocking_domain; 64 struct iommu_domain *domain; 65 struct list_head entry; 66 unsigned int owner_cnt; 67 void *owner; 68 }; 69 70 struct group_device { 71 struct list_head list; 72 struct device *dev; 73 char *name; 74 }; 75 76 /* Iterate over each struct group_device in a struct iommu_group */ 77 #define for_each_group_device(group, pos) \ 78 list_for_each_entry(pos, &(group)->devices, list) 79 80 struct iommu_group_attribute { 81 struct attribute attr; 82 ssize_t (*show)(struct iommu_group *group, char *buf); 83 ssize_t (*store)(struct iommu_group *group, 84 const char *buf, size_t count); 85 }; 86 87 static const char * const iommu_group_resv_type_string[] = { 88 [IOMMU_RESV_DIRECT] = "direct", 89 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable", 90 [IOMMU_RESV_RESERVED] = "reserved", 91 [IOMMU_RESV_MSI] = "msi", 92 [IOMMU_RESV_SW_MSI] = "msi", 93 }; 94 95 #define IOMMU_CMD_LINE_DMA_API BIT(0) 96 #define IOMMU_CMD_LINE_STRICT BIT(1) 97 98 static int bus_iommu_probe(const struct bus_type *bus); 99 static int iommu_bus_notifier(struct notifier_block *nb, 100 unsigned long action, void *data); 101 static void iommu_release_device(struct device *dev); 102 static int __iommu_attach_device(struct iommu_domain *domain, 103 struct device *dev); 104 static int __iommu_attach_group(struct iommu_domain *domain, 105 struct iommu_group *group); 106 static struct iommu_domain *__iommu_paging_domain_alloc_flags(struct device *dev, 107 unsigned int type, 108 unsigned int flags); 109 110 enum { 111 IOMMU_SET_DOMAIN_MUST_SUCCEED = 1 << 0, 112 }; 113 114 static int __iommu_device_set_domain(struct iommu_group *group, 115 struct device *dev, 116 struct iommu_domain *new_domain, 117 unsigned int flags); 118 static int __iommu_group_set_domain_internal(struct iommu_group *group, 119 struct iommu_domain *new_domain, 120 unsigned int flags); 121 static int __iommu_group_set_domain(struct iommu_group *group, 122 struct iommu_domain *new_domain) 123 { 124 return __iommu_group_set_domain_internal(group, new_domain, 0); 125 } 126 static void __iommu_group_set_domain_nofail(struct iommu_group *group, 127 struct iommu_domain *new_domain) 128 { 129 WARN_ON(__iommu_group_set_domain_internal( 130 group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED)); 131 } 132 133 static int iommu_setup_default_domain(struct iommu_group *group, 134 int target_type); 135 static int iommu_create_device_direct_mappings(struct iommu_domain *domain, 136 struct device *dev); 137 static ssize_t iommu_group_store_type(struct iommu_group *group, 138 const char *buf, size_t count); 139 static struct group_device *iommu_group_alloc_device(struct iommu_group *group, 140 struct device *dev); 141 static void __iommu_group_free_device(struct iommu_group *group, 142 struct group_device *grp_dev); 143 static void iommu_domain_init(struct iommu_domain *domain, unsigned int type, 144 const struct iommu_ops *ops); 145 146 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ 147 struct iommu_group_attribute iommu_group_attr_##_name = \ 148 __ATTR(_name, _mode, _show, _store) 149 150 #define to_iommu_group_attr(_attr) \ 151 container_of(_attr, struct iommu_group_attribute, attr) 152 #define to_iommu_group(_kobj) \ 153 container_of(_kobj, struct iommu_group, kobj) 154 155 static LIST_HEAD(iommu_device_list); 156 static DEFINE_SPINLOCK(iommu_device_lock); 157 158 static const struct bus_type * const iommu_buses[] = { 159 &platform_bus_type, 160 #ifdef CONFIG_PCI 161 &pci_bus_type, 162 #endif 163 #ifdef CONFIG_ARM_AMBA 164 &amba_bustype, 165 #endif 166 #ifdef CONFIG_FSL_MC_BUS 167 &fsl_mc_bus_type, 168 #endif 169 #ifdef CONFIG_TEGRA_HOST1X_CONTEXT_BUS 170 &host1x_context_device_bus_type, 171 #endif 172 #ifdef CONFIG_CDX_BUS 173 &cdx_bus_type, 174 #endif 175 }; 176 177 /* 178 * Use a function instead of an array here because the domain-type is a 179 * bit-field, so an array would waste memory. 180 */ 181 static const char *iommu_domain_type_str(unsigned int t) 182 { 183 switch (t) { 184 case IOMMU_DOMAIN_BLOCKED: 185 return "Blocked"; 186 case IOMMU_DOMAIN_IDENTITY: 187 return "Passthrough"; 188 case IOMMU_DOMAIN_UNMANAGED: 189 return "Unmanaged"; 190 case IOMMU_DOMAIN_DMA: 191 case IOMMU_DOMAIN_DMA_FQ: 192 return "Translated"; 193 case IOMMU_DOMAIN_PLATFORM: 194 return "Platform"; 195 default: 196 return "Unknown"; 197 } 198 } 199 200 static int __init iommu_subsys_init(void) 201 { 202 struct notifier_block *nb; 203 204 if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) { 205 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH)) 206 iommu_set_default_passthrough(false); 207 else 208 iommu_set_default_translated(false); 209 210 if (iommu_default_passthrough() && cc_platform_has(CC_ATTR_MEM_ENCRYPT)) { 211 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n"); 212 iommu_set_default_translated(false); 213 } 214 } 215 216 if (!iommu_default_passthrough() && !iommu_dma_strict) 217 iommu_def_domain_type = IOMMU_DOMAIN_DMA_FQ; 218 219 pr_info("Default domain type: %s%s\n", 220 iommu_domain_type_str(iommu_def_domain_type), 221 (iommu_cmd_line & IOMMU_CMD_LINE_DMA_API) ? 222 " (set via kernel command line)" : ""); 223 224 if (!iommu_default_passthrough()) 225 pr_info("DMA domain TLB invalidation policy: %s mode%s\n", 226 iommu_dma_strict ? "strict" : "lazy", 227 (iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ? 228 " (set via kernel command line)" : ""); 229 230 nb = kcalloc(ARRAY_SIZE(iommu_buses), sizeof(*nb), GFP_KERNEL); 231 if (!nb) 232 return -ENOMEM; 233 234 for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) { 235 nb[i].notifier_call = iommu_bus_notifier; 236 bus_register_notifier(iommu_buses[i], &nb[i]); 237 } 238 239 return 0; 240 } 241 subsys_initcall(iommu_subsys_init); 242 243 static int remove_iommu_group(struct device *dev, void *data) 244 { 245 if (dev->iommu && dev->iommu->iommu_dev == data) 246 iommu_release_device(dev); 247 248 return 0; 249 } 250 251 /** 252 * iommu_device_register() - Register an IOMMU hardware instance 253 * @iommu: IOMMU handle for the instance 254 * @ops: IOMMU ops to associate with the instance 255 * @hwdev: (optional) actual instance device, used for fwnode lookup 256 * 257 * Return: 0 on success, or an error. 258 */ 259 int iommu_device_register(struct iommu_device *iommu, 260 const struct iommu_ops *ops, struct device *hwdev) 261 { 262 int err = 0; 263 264 /* We need to be able to take module references appropriately */ 265 if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner)) 266 return -EINVAL; 267 268 iommu->ops = ops; 269 if (hwdev) 270 iommu->fwnode = dev_fwnode(hwdev); 271 272 spin_lock(&iommu_device_lock); 273 list_add_tail(&iommu->list, &iommu_device_list); 274 spin_unlock(&iommu_device_lock); 275 276 for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) 277 err = bus_iommu_probe(iommu_buses[i]); 278 if (err) 279 iommu_device_unregister(iommu); 280 return err; 281 } 282 EXPORT_SYMBOL_GPL(iommu_device_register); 283 284 void iommu_device_unregister(struct iommu_device *iommu) 285 { 286 for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) 287 bus_for_each_dev(iommu_buses[i], NULL, iommu, remove_iommu_group); 288 289 spin_lock(&iommu_device_lock); 290 list_del(&iommu->list); 291 spin_unlock(&iommu_device_lock); 292 293 /* Pairs with the alloc in generic_single_device_group() */ 294 iommu_group_put(iommu->singleton_group); 295 iommu->singleton_group = NULL; 296 } 297 EXPORT_SYMBOL_GPL(iommu_device_unregister); 298 299 #if IS_ENABLED(CONFIG_IOMMUFD_TEST) 300 void iommu_device_unregister_bus(struct iommu_device *iommu, 301 const struct bus_type *bus, 302 struct notifier_block *nb) 303 { 304 bus_unregister_notifier(bus, nb); 305 iommu_device_unregister(iommu); 306 } 307 EXPORT_SYMBOL_GPL(iommu_device_unregister_bus); 308 309 /* 310 * Register an iommu driver against a single bus. This is only used by iommufd 311 * selftest to create a mock iommu driver. The caller must provide 312 * some memory to hold a notifier_block. 313 */ 314 int iommu_device_register_bus(struct iommu_device *iommu, 315 const struct iommu_ops *ops, 316 const struct bus_type *bus, 317 struct notifier_block *nb) 318 { 319 int err; 320 321 iommu->ops = ops; 322 nb->notifier_call = iommu_bus_notifier; 323 err = bus_register_notifier(bus, nb); 324 if (err) 325 return err; 326 327 spin_lock(&iommu_device_lock); 328 list_add_tail(&iommu->list, &iommu_device_list); 329 spin_unlock(&iommu_device_lock); 330 331 err = bus_iommu_probe(bus); 332 if (err) { 333 iommu_device_unregister_bus(iommu, bus, nb); 334 return err; 335 } 336 return 0; 337 } 338 EXPORT_SYMBOL_GPL(iommu_device_register_bus); 339 #endif 340 341 static struct dev_iommu *dev_iommu_get(struct device *dev) 342 { 343 struct dev_iommu *param = dev->iommu; 344 345 lockdep_assert_held(&iommu_probe_device_lock); 346 347 if (param) 348 return param; 349 350 param = kzalloc(sizeof(*param), GFP_KERNEL); 351 if (!param) 352 return NULL; 353 354 mutex_init(¶m->lock); 355 dev->iommu = param; 356 return param; 357 } 358 359 void dev_iommu_free(struct device *dev) 360 { 361 struct dev_iommu *param = dev->iommu; 362 363 dev->iommu = NULL; 364 if (param->fwspec) { 365 fwnode_handle_put(param->fwspec->iommu_fwnode); 366 kfree(param->fwspec); 367 } 368 kfree(param); 369 } 370 371 /* 372 * Internal equivalent of device_iommu_mapped() for when we care that a device 373 * actually has API ops, and don't want false positives from VFIO-only groups. 374 */ 375 static bool dev_has_iommu(struct device *dev) 376 { 377 return dev->iommu && dev->iommu->iommu_dev; 378 } 379 380 static u32 dev_iommu_get_max_pasids(struct device *dev) 381 { 382 u32 max_pasids = 0, bits = 0; 383 int ret; 384 385 if (dev_is_pci(dev)) { 386 ret = pci_max_pasids(to_pci_dev(dev)); 387 if (ret > 0) 388 max_pasids = ret; 389 } else { 390 ret = device_property_read_u32(dev, "pasid-num-bits", &bits); 391 if (!ret) 392 max_pasids = 1UL << bits; 393 } 394 395 return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids); 396 } 397 398 void dev_iommu_priv_set(struct device *dev, void *priv) 399 { 400 /* FSL_PAMU does something weird */ 401 if (!IS_ENABLED(CONFIG_FSL_PAMU)) 402 lockdep_assert_held(&iommu_probe_device_lock); 403 dev->iommu->priv = priv; 404 } 405 EXPORT_SYMBOL_GPL(dev_iommu_priv_set); 406 407 /* 408 * Init the dev->iommu and dev->iommu_group in the struct device and get the 409 * driver probed 410 */ 411 static int iommu_init_device(struct device *dev) 412 { 413 const struct iommu_ops *ops; 414 struct iommu_device *iommu_dev; 415 struct iommu_group *group; 416 int ret; 417 418 if (!dev_iommu_get(dev)) 419 return -ENOMEM; 420 /* 421 * For FDT-based systems and ACPI IORT/VIOT, the common firmware parsing 422 * is buried in the bus dma_configure path. Properly unpicking that is 423 * still a big job, so for now just invoke the whole thing. The device 424 * already having a driver bound means dma_configure has already run and 425 * either found no IOMMU to wait for, or we're in its replay call right 426 * now, so either way there's no point calling it again. 427 */ 428 if (!dev->driver && dev->bus->dma_configure) { 429 mutex_unlock(&iommu_probe_device_lock); 430 dev->bus->dma_configure(dev); 431 mutex_lock(&iommu_probe_device_lock); 432 } 433 /* 434 * At this point, relevant devices either now have a fwspec which will 435 * match ops registered with a non-NULL fwnode, or we can reasonably 436 * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can 437 * be present, and that any of their registered instances has suitable 438 * ops for probing, and thus cheekily co-opt the same mechanism. 439 */ 440 ops = iommu_fwspec_ops(dev->iommu->fwspec); 441 if (!ops) { 442 ret = -ENODEV; 443 goto err_free; 444 } 445 446 if (!try_module_get(ops->owner)) { 447 ret = -EINVAL; 448 goto err_free; 449 } 450 451 iommu_dev = ops->probe_device(dev); 452 if (IS_ERR(iommu_dev)) { 453 ret = PTR_ERR(iommu_dev); 454 goto err_module_put; 455 } 456 dev->iommu->iommu_dev = iommu_dev; 457 458 ret = iommu_device_link(iommu_dev, dev); 459 if (ret) 460 goto err_release; 461 462 group = ops->device_group(dev); 463 if (WARN_ON_ONCE(group == NULL)) 464 group = ERR_PTR(-EINVAL); 465 if (IS_ERR(group)) { 466 ret = PTR_ERR(group); 467 goto err_unlink; 468 } 469 dev->iommu_group = group; 470 471 dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev); 472 if (ops->is_attach_deferred) 473 dev->iommu->attach_deferred = ops->is_attach_deferred(dev); 474 return 0; 475 476 err_unlink: 477 iommu_device_unlink(iommu_dev, dev); 478 err_release: 479 if (ops->release_device) 480 ops->release_device(dev); 481 err_module_put: 482 module_put(ops->owner); 483 err_free: 484 dev->iommu->iommu_dev = NULL; 485 dev_iommu_free(dev); 486 return ret; 487 } 488 489 static void iommu_deinit_device(struct device *dev) 490 { 491 struct iommu_group *group = dev->iommu_group; 492 const struct iommu_ops *ops = dev_iommu_ops(dev); 493 494 lockdep_assert_held(&group->mutex); 495 496 iommu_device_unlink(dev->iommu->iommu_dev, dev); 497 498 /* 499 * release_device() must stop using any attached domain on the device. 500 * If there are still other devices in the group, they are not affected 501 * by this callback. 502 * 503 * If the iommu driver provides release_domain, the core code ensures 504 * that domain is attached prior to calling release_device. Drivers can 505 * use this to enforce a translation on the idle iommu. Typically, the 506 * global static blocked_domain is a good choice. 507 * 508 * Otherwise, the iommu driver must set the device to either an identity 509 * or a blocking translation in release_device() and stop using any 510 * domain pointer, as it is going to be freed. 511 * 512 * Regardless, if a delayed attach never occurred, then the release 513 * should still avoid touching any hardware configuration either. 514 */ 515 if (!dev->iommu->attach_deferred && ops->release_domain) 516 ops->release_domain->ops->attach_dev(ops->release_domain, dev); 517 518 if (ops->release_device) 519 ops->release_device(dev); 520 521 /* 522 * If this is the last driver to use the group then we must free the 523 * domains before we do the module_put(). 524 */ 525 if (list_empty(&group->devices)) { 526 if (group->default_domain) { 527 iommu_domain_free(group->default_domain); 528 group->default_domain = NULL; 529 } 530 if (group->blocking_domain) { 531 iommu_domain_free(group->blocking_domain); 532 group->blocking_domain = NULL; 533 } 534 group->domain = NULL; 535 } 536 537 /* Caller must put iommu_group */ 538 dev->iommu_group = NULL; 539 module_put(ops->owner); 540 dev_iommu_free(dev); 541 } 542 543 static struct iommu_domain *pasid_array_entry_to_domain(void *entry) 544 { 545 if (xa_pointer_tag(entry) == IOMMU_PASID_ARRAY_DOMAIN) 546 return xa_untag_pointer(entry); 547 return ((struct iommu_attach_handle *)xa_untag_pointer(entry))->domain; 548 } 549 550 DEFINE_MUTEX(iommu_probe_device_lock); 551 552 static int __iommu_probe_device(struct device *dev, struct list_head *group_list) 553 { 554 struct iommu_group *group; 555 struct group_device *gdev; 556 int ret; 557 558 /* 559 * Serialise to avoid races between IOMMU drivers registering in 560 * parallel and/or the "replay" calls from ACPI/OF code via client 561 * driver probe. Once the latter have been cleaned up we should 562 * probably be able to use device_lock() here to minimise the scope, 563 * but for now enforcing a simple global ordering is fine. 564 */ 565 lockdep_assert_held(&iommu_probe_device_lock); 566 567 /* Device is probed already if in a group */ 568 if (dev->iommu_group) 569 return 0; 570 571 ret = iommu_init_device(dev); 572 if (ret) 573 return ret; 574 /* 575 * And if we do now see any replay calls, they would indicate someone 576 * misusing the dma_configure path outside bus code. 577 */ 578 if (dev->driver) 579 dev_WARN(dev, "late IOMMU probe at driver bind, something fishy here!\n"); 580 581 group = dev->iommu_group; 582 gdev = iommu_group_alloc_device(group, dev); 583 mutex_lock(&group->mutex); 584 if (IS_ERR(gdev)) { 585 ret = PTR_ERR(gdev); 586 goto err_put_group; 587 } 588 589 /* 590 * The gdev must be in the list before calling 591 * iommu_setup_default_domain() 592 */ 593 list_add_tail(&gdev->list, &group->devices); 594 WARN_ON(group->default_domain && !group->domain); 595 if (group->default_domain) 596 iommu_create_device_direct_mappings(group->default_domain, dev); 597 if (group->domain) { 598 ret = __iommu_device_set_domain(group, dev, group->domain, 0); 599 if (ret) 600 goto err_remove_gdev; 601 } else if (!group->default_domain && !group_list) { 602 ret = iommu_setup_default_domain(group, 0); 603 if (ret) 604 goto err_remove_gdev; 605 } else if (!group->default_domain) { 606 /* 607 * With a group_list argument we defer the default_domain setup 608 * to the caller by providing a de-duplicated list of groups 609 * that need further setup. 610 */ 611 if (list_empty(&group->entry)) 612 list_add_tail(&group->entry, group_list); 613 } 614 615 if (group->default_domain) 616 iommu_setup_dma_ops(dev); 617 618 mutex_unlock(&group->mutex); 619 620 return 0; 621 622 err_remove_gdev: 623 list_del(&gdev->list); 624 __iommu_group_free_device(group, gdev); 625 err_put_group: 626 iommu_deinit_device(dev); 627 mutex_unlock(&group->mutex); 628 iommu_group_put(group); 629 630 return ret; 631 } 632 633 int iommu_probe_device(struct device *dev) 634 { 635 const struct iommu_ops *ops; 636 int ret; 637 638 mutex_lock(&iommu_probe_device_lock); 639 ret = __iommu_probe_device(dev, NULL); 640 mutex_unlock(&iommu_probe_device_lock); 641 if (ret) 642 return ret; 643 644 ops = dev_iommu_ops(dev); 645 if (ops->probe_finalize) 646 ops->probe_finalize(dev); 647 648 return 0; 649 } 650 651 static void __iommu_group_free_device(struct iommu_group *group, 652 struct group_device *grp_dev) 653 { 654 struct device *dev = grp_dev->dev; 655 656 sysfs_remove_link(group->devices_kobj, grp_dev->name); 657 sysfs_remove_link(&dev->kobj, "iommu_group"); 658 659 trace_remove_device_from_group(group->id, dev); 660 661 /* 662 * If the group has become empty then ownership must have been 663 * released, and the current domain must be set back to NULL or 664 * the default domain. 665 */ 666 if (list_empty(&group->devices)) 667 WARN_ON(group->owner_cnt || 668 group->domain != group->default_domain); 669 670 kfree(grp_dev->name); 671 kfree(grp_dev); 672 } 673 674 /* Remove the iommu_group from the struct device. */ 675 static void __iommu_group_remove_device(struct device *dev) 676 { 677 struct iommu_group *group = dev->iommu_group; 678 struct group_device *device; 679 680 mutex_lock(&group->mutex); 681 for_each_group_device(group, device) { 682 if (device->dev != dev) 683 continue; 684 685 list_del(&device->list); 686 __iommu_group_free_device(group, device); 687 if (dev_has_iommu(dev)) 688 iommu_deinit_device(dev); 689 else 690 dev->iommu_group = NULL; 691 break; 692 } 693 mutex_unlock(&group->mutex); 694 695 /* 696 * Pairs with the get in iommu_init_device() or 697 * iommu_group_add_device() 698 */ 699 iommu_group_put(group); 700 } 701 702 static void iommu_release_device(struct device *dev) 703 { 704 struct iommu_group *group = dev->iommu_group; 705 706 if (group) 707 __iommu_group_remove_device(dev); 708 709 /* Free any fwspec if no iommu_driver was ever attached */ 710 if (dev->iommu) 711 dev_iommu_free(dev); 712 } 713 714 static int __init iommu_set_def_domain_type(char *str) 715 { 716 bool pt; 717 int ret; 718 719 ret = kstrtobool(str, &pt); 720 if (ret) 721 return ret; 722 723 if (pt) 724 iommu_set_default_passthrough(true); 725 else 726 iommu_set_default_translated(true); 727 728 return 0; 729 } 730 early_param("iommu.passthrough", iommu_set_def_domain_type); 731 732 static int __init iommu_dma_setup(char *str) 733 { 734 int ret = kstrtobool(str, &iommu_dma_strict); 735 736 if (!ret) 737 iommu_cmd_line |= IOMMU_CMD_LINE_STRICT; 738 return ret; 739 } 740 early_param("iommu.strict", iommu_dma_setup); 741 742 void iommu_set_dma_strict(void) 743 { 744 iommu_dma_strict = true; 745 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ) 746 iommu_def_domain_type = IOMMU_DOMAIN_DMA; 747 } 748 749 static ssize_t iommu_group_attr_show(struct kobject *kobj, 750 struct attribute *__attr, char *buf) 751 { 752 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); 753 struct iommu_group *group = to_iommu_group(kobj); 754 ssize_t ret = -EIO; 755 756 if (attr->show) 757 ret = attr->show(group, buf); 758 return ret; 759 } 760 761 static ssize_t iommu_group_attr_store(struct kobject *kobj, 762 struct attribute *__attr, 763 const char *buf, size_t count) 764 { 765 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); 766 struct iommu_group *group = to_iommu_group(kobj); 767 ssize_t ret = -EIO; 768 769 if (attr->store) 770 ret = attr->store(group, buf, count); 771 return ret; 772 } 773 774 static const struct sysfs_ops iommu_group_sysfs_ops = { 775 .show = iommu_group_attr_show, 776 .store = iommu_group_attr_store, 777 }; 778 779 static int iommu_group_create_file(struct iommu_group *group, 780 struct iommu_group_attribute *attr) 781 { 782 return sysfs_create_file(&group->kobj, &attr->attr); 783 } 784 785 static void iommu_group_remove_file(struct iommu_group *group, 786 struct iommu_group_attribute *attr) 787 { 788 sysfs_remove_file(&group->kobj, &attr->attr); 789 } 790 791 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) 792 { 793 return sysfs_emit(buf, "%s\n", group->name); 794 } 795 796 /** 797 * iommu_insert_resv_region - Insert a new region in the 798 * list of reserved regions. 799 * @new: new region to insert 800 * @regions: list of regions 801 * 802 * Elements are sorted by start address and overlapping segments 803 * of the same type are merged. 804 */ 805 static int iommu_insert_resv_region(struct iommu_resv_region *new, 806 struct list_head *regions) 807 { 808 struct iommu_resv_region *iter, *tmp, *nr, *top; 809 LIST_HEAD(stack); 810 811 nr = iommu_alloc_resv_region(new->start, new->length, 812 new->prot, new->type, GFP_KERNEL); 813 if (!nr) 814 return -ENOMEM; 815 816 /* First add the new element based on start address sorting */ 817 list_for_each_entry(iter, regions, list) { 818 if (nr->start < iter->start || 819 (nr->start == iter->start && nr->type <= iter->type)) 820 break; 821 } 822 list_add_tail(&nr->list, &iter->list); 823 824 /* Merge overlapping segments of type nr->type in @regions, if any */ 825 list_for_each_entry_safe(iter, tmp, regions, list) { 826 phys_addr_t top_end, iter_end = iter->start + iter->length - 1; 827 828 /* no merge needed on elements of different types than @new */ 829 if (iter->type != new->type) { 830 list_move_tail(&iter->list, &stack); 831 continue; 832 } 833 834 /* look for the last stack element of same type as @iter */ 835 list_for_each_entry_reverse(top, &stack, list) 836 if (top->type == iter->type) 837 goto check_overlap; 838 839 list_move_tail(&iter->list, &stack); 840 continue; 841 842 check_overlap: 843 top_end = top->start + top->length - 1; 844 845 if (iter->start > top_end + 1) { 846 list_move_tail(&iter->list, &stack); 847 } else { 848 top->length = max(top_end, iter_end) - top->start + 1; 849 list_del(&iter->list); 850 kfree(iter); 851 } 852 } 853 list_splice(&stack, regions); 854 return 0; 855 } 856 857 static int 858 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions, 859 struct list_head *group_resv_regions) 860 { 861 struct iommu_resv_region *entry; 862 int ret = 0; 863 864 list_for_each_entry(entry, dev_resv_regions, list) { 865 ret = iommu_insert_resv_region(entry, group_resv_regions); 866 if (ret) 867 break; 868 } 869 return ret; 870 } 871 872 int iommu_get_group_resv_regions(struct iommu_group *group, 873 struct list_head *head) 874 { 875 struct group_device *device; 876 int ret = 0; 877 878 mutex_lock(&group->mutex); 879 for_each_group_device(group, device) { 880 struct list_head dev_resv_regions; 881 882 /* 883 * Non-API groups still expose reserved_regions in sysfs, 884 * so filter out calls that get here that way. 885 */ 886 if (!dev_has_iommu(device->dev)) 887 break; 888 889 INIT_LIST_HEAD(&dev_resv_regions); 890 iommu_get_resv_regions(device->dev, &dev_resv_regions); 891 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head); 892 iommu_put_resv_regions(device->dev, &dev_resv_regions); 893 if (ret) 894 break; 895 } 896 mutex_unlock(&group->mutex); 897 return ret; 898 } 899 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions); 900 901 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, 902 char *buf) 903 { 904 struct iommu_resv_region *region, *next; 905 struct list_head group_resv_regions; 906 int offset = 0; 907 908 INIT_LIST_HEAD(&group_resv_regions); 909 iommu_get_group_resv_regions(group, &group_resv_regions); 910 911 list_for_each_entry_safe(region, next, &group_resv_regions, list) { 912 offset += sysfs_emit_at(buf, offset, "0x%016llx 0x%016llx %s\n", 913 (long long)region->start, 914 (long long)(region->start + 915 region->length - 1), 916 iommu_group_resv_type_string[region->type]); 917 kfree(region); 918 } 919 920 return offset; 921 } 922 923 static ssize_t iommu_group_show_type(struct iommu_group *group, 924 char *buf) 925 { 926 char *type = "unknown"; 927 928 mutex_lock(&group->mutex); 929 if (group->default_domain) { 930 switch (group->default_domain->type) { 931 case IOMMU_DOMAIN_BLOCKED: 932 type = "blocked"; 933 break; 934 case IOMMU_DOMAIN_IDENTITY: 935 type = "identity"; 936 break; 937 case IOMMU_DOMAIN_UNMANAGED: 938 type = "unmanaged"; 939 break; 940 case IOMMU_DOMAIN_DMA: 941 type = "DMA"; 942 break; 943 case IOMMU_DOMAIN_DMA_FQ: 944 type = "DMA-FQ"; 945 break; 946 } 947 } 948 mutex_unlock(&group->mutex); 949 950 return sysfs_emit(buf, "%s\n", type); 951 } 952 953 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); 954 955 static IOMMU_GROUP_ATTR(reserved_regions, 0444, 956 iommu_group_show_resv_regions, NULL); 957 958 static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type, 959 iommu_group_store_type); 960 961 static void iommu_group_release(struct kobject *kobj) 962 { 963 struct iommu_group *group = to_iommu_group(kobj); 964 965 pr_debug("Releasing group %d\n", group->id); 966 967 if (group->iommu_data_release) 968 group->iommu_data_release(group->iommu_data); 969 970 ida_free(&iommu_group_ida, group->id); 971 972 /* Domains are free'd by iommu_deinit_device() */ 973 WARN_ON(group->default_domain); 974 WARN_ON(group->blocking_domain); 975 976 kfree(group->name); 977 kfree(group); 978 } 979 980 static const struct kobj_type iommu_group_ktype = { 981 .sysfs_ops = &iommu_group_sysfs_ops, 982 .release = iommu_group_release, 983 }; 984 985 /** 986 * iommu_group_alloc - Allocate a new group 987 * 988 * This function is called by an iommu driver to allocate a new iommu 989 * group. The iommu group represents the minimum granularity of the iommu. 990 * Upon successful return, the caller holds a reference to the supplied 991 * group in order to hold the group until devices are added. Use 992 * iommu_group_put() to release this extra reference count, allowing the 993 * group to be automatically reclaimed once it has no devices or external 994 * references. 995 */ 996 struct iommu_group *iommu_group_alloc(void) 997 { 998 struct iommu_group *group; 999 int ret; 1000 1001 group = kzalloc(sizeof(*group), GFP_KERNEL); 1002 if (!group) 1003 return ERR_PTR(-ENOMEM); 1004 1005 group->kobj.kset = iommu_group_kset; 1006 mutex_init(&group->mutex); 1007 INIT_LIST_HEAD(&group->devices); 1008 INIT_LIST_HEAD(&group->entry); 1009 xa_init(&group->pasid_array); 1010 1011 ret = ida_alloc(&iommu_group_ida, GFP_KERNEL); 1012 if (ret < 0) { 1013 kfree(group); 1014 return ERR_PTR(ret); 1015 } 1016 group->id = ret; 1017 1018 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, 1019 NULL, "%d", group->id); 1020 if (ret) { 1021 kobject_put(&group->kobj); 1022 return ERR_PTR(ret); 1023 } 1024 1025 group->devices_kobj = kobject_create_and_add("devices", &group->kobj); 1026 if (!group->devices_kobj) { 1027 kobject_put(&group->kobj); /* triggers .release & free */ 1028 return ERR_PTR(-ENOMEM); 1029 } 1030 1031 /* 1032 * The devices_kobj holds a reference on the group kobject, so 1033 * as long as that exists so will the group. We can therefore 1034 * use the devices_kobj for reference counting. 1035 */ 1036 kobject_put(&group->kobj); 1037 1038 ret = iommu_group_create_file(group, 1039 &iommu_group_attr_reserved_regions); 1040 if (ret) { 1041 kobject_put(group->devices_kobj); 1042 return ERR_PTR(ret); 1043 } 1044 1045 ret = iommu_group_create_file(group, &iommu_group_attr_type); 1046 if (ret) { 1047 kobject_put(group->devices_kobj); 1048 return ERR_PTR(ret); 1049 } 1050 1051 pr_debug("Allocated group %d\n", group->id); 1052 1053 return group; 1054 } 1055 EXPORT_SYMBOL_GPL(iommu_group_alloc); 1056 1057 /** 1058 * iommu_group_get_iommudata - retrieve iommu_data registered for a group 1059 * @group: the group 1060 * 1061 * iommu drivers can store data in the group for use when doing iommu 1062 * operations. This function provides a way to retrieve it. Caller 1063 * should hold a group reference. 1064 */ 1065 void *iommu_group_get_iommudata(struct iommu_group *group) 1066 { 1067 return group->iommu_data; 1068 } 1069 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata); 1070 1071 /** 1072 * iommu_group_set_iommudata - set iommu_data for a group 1073 * @group: the group 1074 * @iommu_data: new data 1075 * @release: release function for iommu_data 1076 * 1077 * iommu drivers can store data in the group for use when doing iommu 1078 * operations. This function provides a way to set the data after 1079 * the group has been allocated. Caller should hold a group reference. 1080 */ 1081 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, 1082 void (*release)(void *iommu_data)) 1083 { 1084 group->iommu_data = iommu_data; 1085 group->iommu_data_release = release; 1086 } 1087 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata); 1088 1089 /** 1090 * iommu_group_set_name - set name for a group 1091 * @group: the group 1092 * @name: name 1093 * 1094 * Allow iommu driver to set a name for a group. When set it will 1095 * appear in a name attribute file under the group in sysfs. 1096 */ 1097 int iommu_group_set_name(struct iommu_group *group, const char *name) 1098 { 1099 int ret; 1100 1101 if (group->name) { 1102 iommu_group_remove_file(group, &iommu_group_attr_name); 1103 kfree(group->name); 1104 group->name = NULL; 1105 if (!name) 1106 return 0; 1107 } 1108 1109 group->name = kstrdup(name, GFP_KERNEL); 1110 if (!group->name) 1111 return -ENOMEM; 1112 1113 ret = iommu_group_create_file(group, &iommu_group_attr_name); 1114 if (ret) { 1115 kfree(group->name); 1116 group->name = NULL; 1117 return ret; 1118 } 1119 1120 return 0; 1121 } 1122 EXPORT_SYMBOL_GPL(iommu_group_set_name); 1123 1124 static int iommu_create_device_direct_mappings(struct iommu_domain *domain, 1125 struct device *dev) 1126 { 1127 struct iommu_resv_region *entry; 1128 struct list_head mappings; 1129 unsigned long pg_size; 1130 int ret = 0; 1131 1132 pg_size = domain->pgsize_bitmap ? 1UL << __ffs(domain->pgsize_bitmap) : 0; 1133 INIT_LIST_HEAD(&mappings); 1134 1135 if (WARN_ON_ONCE(iommu_is_dma_domain(domain) && !pg_size)) 1136 return -EINVAL; 1137 1138 iommu_get_resv_regions(dev, &mappings); 1139 1140 /* We need to consider overlapping regions for different devices */ 1141 list_for_each_entry(entry, &mappings, list) { 1142 dma_addr_t start, end, addr; 1143 size_t map_size = 0; 1144 1145 if (entry->type == IOMMU_RESV_DIRECT) 1146 dev->iommu->require_direct = 1; 1147 1148 if ((entry->type != IOMMU_RESV_DIRECT && 1149 entry->type != IOMMU_RESV_DIRECT_RELAXABLE) || 1150 !iommu_is_dma_domain(domain)) 1151 continue; 1152 1153 start = ALIGN(entry->start, pg_size); 1154 end = ALIGN(entry->start + entry->length, pg_size); 1155 1156 for (addr = start; addr <= end; addr += pg_size) { 1157 phys_addr_t phys_addr; 1158 1159 if (addr == end) 1160 goto map_end; 1161 1162 phys_addr = iommu_iova_to_phys(domain, addr); 1163 if (!phys_addr) { 1164 map_size += pg_size; 1165 continue; 1166 } 1167 1168 map_end: 1169 if (map_size) { 1170 ret = iommu_map(domain, addr - map_size, 1171 addr - map_size, map_size, 1172 entry->prot, GFP_KERNEL); 1173 if (ret) 1174 goto out; 1175 map_size = 0; 1176 } 1177 } 1178 1179 } 1180 out: 1181 iommu_put_resv_regions(dev, &mappings); 1182 1183 return ret; 1184 } 1185 1186 /* This is undone by __iommu_group_free_device() */ 1187 static struct group_device *iommu_group_alloc_device(struct iommu_group *group, 1188 struct device *dev) 1189 { 1190 int ret, i = 0; 1191 struct group_device *device; 1192 1193 device = kzalloc(sizeof(*device), GFP_KERNEL); 1194 if (!device) 1195 return ERR_PTR(-ENOMEM); 1196 1197 device->dev = dev; 1198 1199 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); 1200 if (ret) 1201 goto err_free_device; 1202 1203 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); 1204 rename: 1205 if (!device->name) { 1206 ret = -ENOMEM; 1207 goto err_remove_link; 1208 } 1209 1210 ret = sysfs_create_link_nowarn(group->devices_kobj, 1211 &dev->kobj, device->name); 1212 if (ret) { 1213 if (ret == -EEXIST && i >= 0) { 1214 /* 1215 * Account for the slim chance of collision 1216 * and append an instance to the name. 1217 */ 1218 kfree(device->name); 1219 device->name = kasprintf(GFP_KERNEL, "%s.%d", 1220 kobject_name(&dev->kobj), i++); 1221 goto rename; 1222 } 1223 goto err_free_name; 1224 } 1225 1226 trace_add_device_to_group(group->id, dev); 1227 1228 dev_info(dev, "Adding to iommu group %d\n", group->id); 1229 1230 return device; 1231 1232 err_free_name: 1233 kfree(device->name); 1234 err_remove_link: 1235 sysfs_remove_link(&dev->kobj, "iommu_group"); 1236 err_free_device: 1237 kfree(device); 1238 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret); 1239 return ERR_PTR(ret); 1240 } 1241 1242 /** 1243 * iommu_group_add_device - add a device to an iommu group 1244 * @group: the group into which to add the device (reference should be held) 1245 * @dev: the device 1246 * 1247 * This function is called by an iommu driver to add a device into a 1248 * group. Adding a device increments the group reference count. 1249 */ 1250 int iommu_group_add_device(struct iommu_group *group, struct device *dev) 1251 { 1252 struct group_device *gdev; 1253 1254 gdev = iommu_group_alloc_device(group, dev); 1255 if (IS_ERR(gdev)) 1256 return PTR_ERR(gdev); 1257 1258 iommu_group_ref_get(group); 1259 dev->iommu_group = group; 1260 1261 mutex_lock(&group->mutex); 1262 list_add_tail(&gdev->list, &group->devices); 1263 mutex_unlock(&group->mutex); 1264 return 0; 1265 } 1266 EXPORT_SYMBOL_GPL(iommu_group_add_device); 1267 1268 /** 1269 * iommu_group_remove_device - remove a device from it's current group 1270 * @dev: device to be removed 1271 * 1272 * This function is called by an iommu driver to remove the device from 1273 * it's current group. This decrements the iommu group reference count. 1274 */ 1275 void iommu_group_remove_device(struct device *dev) 1276 { 1277 struct iommu_group *group = dev->iommu_group; 1278 1279 if (!group) 1280 return; 1281 1282 dev_info(dev, "Removing from iommu group %d\n", group->id); 1283 1284 __iommu_group_remove_device(dev); 1285 } 1286 EXPORT_SYMBOL_GPL(iommu_group_remove_device); 1287 1288 #if IS_ENABLED(CONFIG_LOCKDEP) && IS_ENABLED(CONFIG_IOMMU_API) 1289 /** 1290 * iommu_group_mutex_assert - Check device group mutex lock 1291 * @dev: the device that has group param set 1292 * 1293 * This function is called by an iommu driver to check whether it holds 1294 * group mutex lock for the given device or not. 1295 * 1296 * Note that this function must be called after device group param is set. 1297 */ 1298 void iommu_group_mutex_assert(struct device *dev) 1299 { 1300 struct iommu_group *group = dev->iommu_group; 1301 1302 lockdep_assert_held(&group->mutex); 1303 } 1304 EXPORT_SYMBOL_GPL(iommu_group_mutex_assert); 1305 #endif 1306 1307 static struct device *iommu_group_first_dev(struct iommu_group *group) 1308 { 1309 lockdep_assert_held(&group->mutex); 1310 return list_first_entry(&group->devices, struct group_device, list)->dev; 1311 } 1312 1313 /** 1314 * iommu_group_for_each_dev - iterate over each device in the group 1315 * @group: the group 1316 * @data: caller opaque data to be passed to callback function 1317 * @fn: caller supplied callback function 1318 * 1319 * This function is called by group users to iterate over group devices. 1320 * Callers should hold a reference count to the group during callback. 1321 * The group->mutex is held across callbacks, which will block calls to 1322 * iommu_group_add/remove_device. 1323 */ 1324 int iommu_group_for_each_dev(struct iommu_group *group, void *data, 1325 int (*fn)(struct device *, void *)) 1326 { 1327 struct group_device *device; 1328 int ret = 0; 1329 1330 mutex_lock(&group->mutex); 1331 for_each_group_device(group, device) { 1332 ret = fn(device->dev, data); 1333 if (ret) 1334 break; 1335 } 1336 mutex_unlock(&group->mutex); 1337 1338 return ret; 1339 } 1340 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev); 1341 1342 /** 1343 * iommu_group_get - Return the group for a device and increment reference 1344 * @dev: get the group that this device belongs to 1345 * 1346 * This function is called by iommu drivers and users to get the group 1347 * for the specified device. If found, the group is returned and the group 1348 * reference in incremented, else NULL. 1349 */ 1350 struct iommu_group *iommu_group_get(struct device *dev) 1351 { 1352 struct iommu_group *group = dev->iommu_group; 1353 1354 if (group) 1355 kobject_get(group->devices_kobj); 1356 1357 return group; 1358 } 1359 EXPORT_SYMBOL_GPL(iommu_group_get); 1360 1361 /** 1362 * iommu_group_ref_get - Increment reference on a group 1363 * @group: the group to use, must not be NULL 1364 * 1365 * This function is called by iommu drivers to take additional references on an 1366 * existing group. Returns the given group for convenience. 1367 */ 1368 struct iommu_group *iommu_group_ref_get(struct iommu_group *group) 1369 { 1370 kobject_get(group->devices_kobj); 1371 return group; 1372 } 1373 EXPORT_SYMBOL_GPL(iommu_group_ref_get); 1374 1375 /** 1376 * iommu_group_put - Decrement group reference 1377 * @group: the group to use 1378 * 1379 * This function is called by iommu drivers and users to release the 1380 * iommu group. Once the reference count is zero, the group is released. 1381 */ 1382 void iommu_group_put(struct iommu_group *group) 1383 { 1384 if (group) 1385 kobject_put(group->devices_kobj); 1386 } 1387 EXPORT_SYMBOL_GPL(iommu_group_put); 1388 1389 /** 1390 * iommu_group_id - Return ID for a group 1391 * @group: the group to ID 1392 * 1393 * Return the unique ID for the group matching the sysfs group number. 1394 */ 1395 int iommu_group_id(struct iommu_group *group) 1396 { 1397 return group->id; 1398 } 1399 EXPORT_SYMBOL_GPL(iommu_group_id); 1400 1401 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, 1402 unsigned long *devfns); 1403 1404 /* 1405 * To consider a PCI device isolated, we require ACS to support Source 1406 * Validation, Request Redirection, Completer Redirection, and Upstream 1407 * Forwarding. This effectively means that devices cannot spoof their 1408 * requester ID, requests and completions cannot be redirected, and all 1409 * transactions are forwarded upstream, even as it passes through a 1410 * bridge where the target device is downstream. 1411 */ 1412 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF) 1413 1414 /* 1415 * For multifunction devices which are not isolated from each other, find 1416 * all the other non-isolated functions and look for existing groups. For 1417 * each function, we also need to look for aliases to or from other devices 1418 * that may already have a group. 1419 */ 1420 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev, 1421 unsigned long *devfns) 1422 { 1423 struct pci_dev *tmp = NULL; 1424 struct iommu_group *group; 1425 1426 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS)) 1427 return NULL; 1428 1429 for_each_pci_dev(tmp) { 1430 if (tmp == pdev || tmp->bus != pdev->bus || 1431 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) || 1432 pci_acs_enabled(tmp, REQ_ACS_FLAGS)) 1433 continue; 1434 1435 group = get_pci_alias_group(tmp, devfns); 1436 if (group) { 1437 pci_dev_put(tmp); 1438 return group; 1439 } 1440 } 1441 1442 return NULL; 1443 } 1444 1445 /* 1446 * Look for aliases to or from the given device for existing groups. DMA 1447 * aliases are only supported on the same bus, therefore the search 1448 * space is quite small (especially since we're really only looking at pcie 1449 * device, and therefore only expect multiple slots on the root complex or 1450 * downstream switch ports). It's conceivable though that a pair of 1451 * multifunction devices could have aliases between them that would cause a 1452 * loop. To prevent this, we use a bitmap to track where we've been. 1453 */ 1454 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, 1455 unsigned long *devfns) 1456 { 1457 struct pci_dev *tmp = NULL; 1458 struct iommu_group *group; 1459 1460 if (test_and_set_bit(pdev->devfn & 0xff, devfns)) 1461 return NULL; 1462 1463 group = iommu_group_get(&pdev->dev); 1464 if (group) 1465 return group; 1466 1467 for_each_pci_dev(tmp) { 1468 if (tmp == pdev || tmp->bus != pdev->bus) 1469 continue; 1470 1471 /* We alias them or they alias us */ 1472 if (pci_devs_are_dma_aliases(pdev, tmp)) { 1473 group = get_pci_alias_group(tmp, devfns); 1474 if (group) { 1475 pci_dev_put(tmp); 1476 return group; 1477 } 1478 1479 group = get_pci_function_alias_group(tmp, devfns); 1480 if (group) { 1481 pci_dev_put(tmp); 1482 return group; 1483 } 1484 } 1485 } 1486 1487 return NULL; 1488 } 1489 1490 struct group_for_pci_data { 1491 struct pci_dev *pdev; 1492 struct iommu_group *group; 1493 }; 1494 1495 /* 1496 * DMA alias iterator callback, return the last seen device. Stop and return 1497 * the IOMMU group if we find one along the way. 1498 */ 1499 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque) 1500 { 1501 struct group_for_pci_data *data = opaque; 1502 1503 data->pdev = pdev; 1504 data->group = iommu_group_get(&pdev->dev); 1505 1506 return data->group != NULL; 1507 } 1508 1509 /* 1510 * Generic device_group call-back function. It just allocates one 1511 * iommu-group per device. 1512 */ 1513 struct iommu_group *generic_device_group(struct device *dev) 1514 { 1515 return iommu_group_alloc(); 1516 } 1517 EXPORT_SYMBOL_GPL(generic_device_group); 1518 1519 /* 1520 * Generic device_group call-back function. It just allocates one 1521 * iommu-group per iommu driver instance shared by every device 1522 * probed by that iommu driver. 1523 */ 1524 struct iommu_group *generic_single_device_group(struct device *dev) 1525 { 1526 struct iommu_device *iommu = dev->iommu->iommu_dev; 1527 1528 if (!iommu->singleton_group) { 1529 struct iommu_group *group; 1530 1531 group = iommu_group_alloc(); 1532 if (IS_ERR(group)) 1533 return group; 1534 iommu->singleton_group = group; 1535 } 1536 return iommu_group_ref_get(iommu->singleton_group); 1537 } 1538 EXPORT_SYMBOL_GPL(generic_single_device_group); 1539 1540 /* 1541 * Use standard PCI bus topology, isolation features, and DMA alias quirks 1542 * to find or create an IOMMU group for a device. 1543 */ 1544 struct iommu_group *pci_device_group(struct device *dev) 1545 { 1546 struct pci_dev *pdev = to_pci_dev(dev); 1547 struct group_for_pci_data data; 1548 struct pci_bus *bus; 1549 struct iommu_group *group = NULL; 1550 u64 devfns[4] = { 0 }; 1551 1552 if (WARN_ON(!dev_is_pci(dev))) 1553 return ERR_PTR(-EINVAL); 1554 1555 /* 1556 * Find the upstream DMA alias for the device. A device must not 1557 * be aliased due to topology in order to have its own IOMMU group. 1558 * If we find an alias along the way that already belongs to a 1559 * group, use it. 1560 */ 1561 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data)) 1562 return data.group; 1563 1564 pdev = data.pdev; 1565 1566 /* 1567 * Continue upstream from the point of minimum IOMMU granularity 1568 * due to aliases to the point where devices are protected from 1569 * peer-to-peer DMA by PCI ACS. Again, if we find an existing 1570 * group, use it. 1571 */ 1572 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) { 1573 if (!bus->self) 1574 continue; 1575 1576 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS)) 1577 break; 1578 1579 pdev = bus->self; 1580 1581 group = iommu_group_get(&pdev->dev); 1582 if (group) 1583 return group; 1584 } 1585 1586 /* 1587 * Look for existing groups on device aliases. If we alias another 1588 * device or another device aliases us, use the same group. 1589 */ 1590 group = get_pci_alias_group(pdev, (unsigned long *)devfns); 1591 if (group) 1592 return group; 1593 1594 /* 1595 * Look for existing groups on non-isolated functions on the same 1596 * slot and aliases of those funcions, if any. No need to clear 1597 * the search bitmap, the tested devfns are still valid. 1598 */ 1599 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns); 1600 if (group) 1601 return group; 1602 1603 /* No shared group found, allocate new */ 1604 return iommu_group_alloc(); 1605 } 1606 EXPORT_SYMBOL_GPL(pci_device_group); 1607 1608 /* Get the IOMMU group for device on fsl-mc bus */ 1609 struct iommu_group *fsl_mc_device_group(struct device *dev) 1610 { 1611 struct device *cont_dev = fsl_mc_cont_dev(dev); 1612 struct iommu_group *group; 1613 1614 group = iommu_group_get(cont_dev); 1615 if (!group) 1616 group = iommu_group_alloc(); 1617 return group; 1618 } 1619 EXPORT_SYMBOL_GPL(fsl_mc_device_group); 1620 1621 static struct iommu_domain *__iommu_alloc_identity_domain(struct device *dev) 1622 { 1623 const struct iommu_ops *ops = dev_iommu_ops(dev); 1624 struct iommu_domain *domain; 1625 1626 if (ops->identity_domain) 1627 return ops->identity_domain; 1628 1629 /* Older drivers create the identity domain via ops->domain_alloc() */ 1630 if (!ops->domain_alloc) 1631 return ERR_PTR(-EOPNOTSUPP); 1632 1633 domain = ops->domain_alloc(IOMMU_DOMAIN_IDENTITY); 1634 if (IS_ERR(domain)) 1635 return domain; 1636 if (!domain) 1637 return ERR_PTR(-ENOMEM); 1638 1639 iommu_domain_init(domain, IOMMU_DOMAIN_IDENTITY, ops); 1640 return domain; 1641 } 1642 1643 static struct iommu_domain * 1644 __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) 1645 { 1646 struct device *dev = iommu_group_first_dev(group); 1647 struct iommu_domain *dom; 1648 1649 if (group->default_domain && group->default_domain->type == req_type) 1650 return group->default_domain; 1651 1652 /* 1653 * When allocating the DMA API domain assume that the driver is going to 1654 * use PASID and make sure the RID's domain is PASID compatible. 1655 */ 1656 if (req_type & __IOMMU_DOMAIN_PAGING) { 1657 dom = __iommu_paging_domain_alloc_flags(dev, req_type, 1658 dev->iommu->max_pasids ? IOMMU_HWPT_ALLOC_PASID : 0); 1659 1660 /* 1661 * If driver does not support PASID feature then 1662 * try to allocate non-PASID domain 1663 */ 1664 if (PTR_ERR(dom) == -EOPNOTSUPP) 1665 dom = __iommu_paging_domain_alloc_flags(dev, req_type, 0); 1666 1667 return dom; 1668 } 1669 1670 if (req_type == IOMMU_DOMAIN_IDENTITY) 1671 return __iommu_alloc_identity_domain(dev); 1672 1673 return ERR_PTR(-EINVAL); 1674 } 1675 1676 /* 1677 * req_type of 0 means "auto" which means to select a domain based on 1678 * iommu_def_domain_type or what the driver actually supports. 1679 */ 1680 static struct iommu_domain * 1681 iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) 1682 { 1683 const struct iommu_ops *ops = dev_iommu_ops(iommu_group_first_dev(group)); 1684 struct iommu_domain *dom; 1685 1686 lockdep_assert_held(&group->mutex); 1687 1688 /* 1689 * Allow legacy drivers to specify the domain that will be the default 1690 * domain. This should always be either an IDENTITY/BLOCKED/PLATFORM 1691 * domain. Do not use in new drivers. 1692 */ 1693 if (ops->default_domain) { 1694 if (req_type != ops->default_domain->type) 1695 return ERR_PTR(-EINVAL); 1696 return ops->default_domain; 1697 } 1698 1699 if (req_type) 1700 return __iommu_group_alloc_default_domain(group, req_type); 1701 1702 /* The driver gave no guidance on what type to use, try the default */ 1703 dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type); 1704 if (!IS_ERR(dom)) 1705 return dom; 1706 1707 /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */ 1708 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA) 1709 return ERR_PTR(-EINVAL); 1710 dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA); 1711 if (IS_ERR(dom)) 1712 return dom; 1713 1714 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA", 1715 iommu_def_domain_type, group->name); 1716 return dom; 1717 } 1718 1719 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) 1720 { 1721 return group->default_domain; 1722 } 1723 1724 static int probe_iommu_group(struct device *dev, void *data) 1725 { 1726 struct list_head *group_list = data; 1727 int ret; 1728 1729 mutex_lock(&iommu_probe_device_lock); 1730 ret = __iommu_probe_device(dev, group_list); 1731 mutex_unlock(&iommu_probe_device_lock); 1732 if (ret == -ENODEV) 1733 ret = 0; 1734 1735 return ret; 1736 } 1737 1738 static int iommu_bus_notifier(struct notifier_block *nb, 1739 unsigned long action, void *data) 1740 { 1741 struct device *dev = data; 1742 1743 if (action == BUS_NOTIFY_ADD_DEVICE) { 1744 int ret; 1745 1746 ret = iommu_probe_device(dev); 1747 return (ret) ? NOTIFY_DONE : NOTIFY_OK; 1748 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) { 1749 iommu_release_device(dev); 1750 return NOTIFY_OK; 1751 } 1752 1753 return 0; 1754 } 1755 1756 /* 1757 * Combine the driver's chosen def_domain_type across all the devices in a 1758 * group. Drivers must give a consistent result. 1759 */ 1760 static int iommu_get_def_domain_type(struct iommu_group *group, 1761 struct device *dev, int cur_type) 1762 { 1763 const struct iommu_ops *ops = dev_iommu_ops(dev); 1764 int type; 1765 1766 if (ops->default_domain) { 1767 /* 1768 * Drivers that declare a global static default_domain will 1769 * always choose that. 1770 */ 1771 type = ops->default_domain->type; 1772 } else { 1773 if (ops->def_domain_type) 1774 type = ops->def_domain_type(dev); 1775 else 1776 return cur_type; 1777 } 1778 if (!type || cur_type == type) 1779 return cur_type; 1780 if (!cur_type) 1781 return type; 1782 1783 dev_err_ratelimited( 1784 dev, 1785 "IOMMU driver error, requesting conflicting def_domain_type, %s and %s, for devices in group %u.\n", 1786 iommu_domain_type_str(cur_type), iommu_domain_type_str(type), 1787 group->id); 1788 1789 /* 1790 * Try to recover, drivers are allowed to force IDENTITY or DMA, IDENTITY 1791 * takes precedence. 1792 */ 1793 if (type == IOMMU_DOMAIN_IDENTITY) 1794 return type; 1795 return cur_type; 1796 } 1797 1798 /* 1799 * A target_type of 0 will select the best domain type. 0 can be returned in 1800 * this case meaning the global default should be used. 1801 */ 1802 static int iommu_get_default_domain_type(struct iommu_group *group, 1803 int target_type) 1804 { 1805 struct device *untrusted = NULL; 1806 struct group_device *gdev; 1807 int driver_type = 0; 1808 1809 lockdep_assert_held(&group->mutex); 1810 1811 /* 1812 * ARM32 drivers supporting CONFIG_ARM_DMA_USE_IOMMU can declare an 1813 * identity_domain and it will automatically become their default 1814 * domain. Later on ARM_DMA_USE_IOMMU will install its UNMANAGED domain. 1815 * Override the selection to IDENTITY. 1816 */ 1817 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) { 1818 static_assert(!(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU) && 1819 IS_ENABLED(CONFIG_IOMMU_DMA))); 1820 driver_type = IOMMU_DOMAIN_IDENTITY; 1821 } 1822 1823 for_each_group_device(group, gdev) { 1824 driver_type = iommu_get_def_domain_type(group, gdev->dev, 1825 driver_type); 1826 1827 if (dev_is_pci(gdev->dev) && to_pci_dev(gdev->dev)->untrusted) { 1828 /* 1829 * No ARM32 using systems will set untrusted, it cannot 1830 * work. 1831 */ 1832 if (WARN_ON(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU))) 1833 return -1; 1834 untrusted = gdev->dev; 1835 } 1836 } 1837 1838 /* 1839 * If the common dma ops are not selected in kconfig then we cannot use 1840 * IOMMU_DOMAIN_DMA at all. Force IDENTITY if nothing else has been 1841 * selected. 1842 */ 1843 if (!IS_ENABLED(CONFIG_IOMMU_DMA)) { 1844 if (WARN_ON(driver_type == IOMMU_DOMAIN_DMA)) 1845 return -1; 1846 if (!driver_type) 1847 driver_type = IOMMU_DOMAIN_IDENTITY; 1848 } 1849 1850 if (untrusted) { 1851 if (driver_type && driver_type != IOMMU_DOMAIN_DMA) { 1852 dev_err_ratelimited( 1853 untrusted, 1854 "Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\n", 1855 group->id, iommu_domain_type_str(driver_type)); 1856 return -1; 1857 } 1858 driver_type = IOMMU_DOMAIN_DMA; 1859 } 1860 1861 if (target_type) { 1862 if (driver_type && target_type != driver_type) 1863 return -1; 1864 return target_type; 1865 } 1866 return driver_type; 1867 } 1868 1869 static void iommu_group_do_probe_finalize(struct device *dev) 1870 { 1871 const struct iommu_ops *ops = dev_iommu_ops(dev); 1872 1873 if (ops->probe_finalize) 1874 ops->probe_finalize(dev); 1875 } 1876 1877 static int bus_iommu_probe(const struct bus_type *bus) 1878 { 1879 struct iommu_group *group, *next; 1880 LIST_HEAD(group_list); 1881 int ret; 1882 1883 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group); 1884 if (ret) 1885 return ret; 1886 1887 list_for_each_entry_safe(group, next, &group_list, entry) { 1888 struct group_device *gdev; 1889 1890 mutex_lock(&group->mutex); 1891 1892 /* Remove item from the list */ 1893 list_del_init(&group->entry); 1894 1895 /* 1896 * We go to the trouble of deferred default domain creation so 1897 * that the cross-group default domain type and the setup of the 1898 * IOMMU_RESV_DIRECT will work correctly in non-hotpug scenarios. 1899 */ 1900 ret = iommu_setup_default_domain(group, 0); 1901 if (ret) { 1902 mutex_unlock(&group->mutex); 1903 return ret; 1904 } 1905 for_each_group_device(group, gdev) 1906 iommu_setup_dma_ops(gdev->dev); 1907 mutex_unlock(&group->mutex); 1908 1909 /* 1910 * FIXME: Mis-locked because the ops->probe_finalize() call-back 1911 * of some IOMMU drivers calls arm_iommu_attach_device() which 1912 * in-turn might call back into IOMMU core code, where it tries 1913 * to take group->mutex, resulting in a deadlock. 1914 */ 1915 for_each_group_device(group, gdev) 1916 iommu_group_do_probe_finalize(gdev->dev); 1917 } 1918 1919 return 0; 1920 } 1921 1922 /** 1923 * device_iommu_capable() - check for a general IOMMU capability 1924 * @dev: device to which the capability would be relevant, if available 1925 * @cap: IOMMU capability 1926 * 1927 * Return: true if an IOMMU is present and supports the given capability 1928 * for the given device, otherwise false. 1929 */ 1930 bool device_iommu_capable(struct device *dev, enum iommu_cap cap) 1931 { 1932 const struct iommu_ops *ops; 1933 1934 if (!dev_has_iommu(dev)) 1935 return false; 1936 1937 ops = dev_iommu_ops(dev); 1938 if (!ops->capable) 1939 return false; 1940 1941 return ops->capable(dev, cap); 1942 } 1943 EXPORT_SYMBOL_GPL(device_iommu_capable); 1944 1945 /** 1946 * iommu_group_has_isolated_msi() - Compute msi_device_has_isolated_msi() 1947 * for a group 1948 * @group: Group to query 1949 * 1950 * IOMMU groups should not have differing values of 1951 * msi_device_has_isolated_msi() for devices in a group. However nothing 1952 * directly prevents this, so ensure mistakes don't result in isolation failures 1953 * by checking that all the devices are the same. 1954 */ 1955 bool iommu_group_has_isolated_msi(struct iommu_group *group) 1956 { 1957 struct group_device *group_dev; 1958 bool ret = true; 1959 1960 mutex_lock(&group->mutex); 1961 for_each_group_device(group, group_dev) 1962 ret &= msi_device_has_isolated_msi(group_dev->dev); 1963 mutex_unlock(&group->mutex); 1964 return ret; 1965 } 1966 EXPORT_SYMBOL_GPL(iommu_group_has_isolated_msi); 1967 1968 /** 1969 * iommu_set_fault_handler() - set a fault handler for an iommu domain 1970 * @domain: iommu domain 1971 * @handler: fault handler 1972 * @token: user data, will be passed back to the fault handler 1973 * 1974 * This function should be used by IOMMU users which want to be notified 1975 * whenever an IOMMU fault happens. 1976 * 1977 * The fault handler itself should return 0 on success, and an appropriate 1978 * error code otherwise. 1979 */ 1980 void iommu_set_fault_handler(struct iommu_domain *domain, 1981 iommu_fault_handler_t handler, 1982 void *token) 1983 { 1984 if (WARN_ON(!domain || domain->cookie_type != IOMMU_COOKIE_NONE)) 1985 return; 1986 1987 domain->cookie_type = IOMMU_COOKIE_FAULT_HANDLER; 1988 domain->handler = handler; 1989 domain->handler_token = token; 1990 } 1991 EXPORT_SYMBOL_GPL(iommu_set_fault_handler); 1992 1993 static void iommu_domain_init(struct iommu_domain *domain, unsigned int type, 1994 const struct iommu_ops *ops) 1995 { 1996 domain->type = type; 1997 domain->owner = ops; 1998 if (!domain->ops) 1999 domain->ops = ops->default_domain_ops; 2000 2001 /* 2002 * If not already set, assume all sizes by default; the driver 2003 * may override this later 2004 */ 2005 if (!domain->pgsize_bitmap) 2006 domain->pgsize_bitmap = ops->pgsize_bitmap; 2007 } 2008 2009 static struct iommu_domain * 2010 __iommu_paging_domain_alloc_flags(struct device *dev, unsigned int type, 2011 unsigned int flags) 2012 { 2013 const struct iommu_ops *ops; 2014 struct iommu_domain *domain; 2015 2016 if (!dev_has_iommu(dev)) 2017 return ERR_PTR(-ENODEV); 2018 2019 ops = dev_iommu_ops(dev); 2020 2021 if (ops->domain_alloc_paging && !flags) 2022 domain = ops->domain_alloc_paging(dev); 2023 else if (ops->domain_alloc_paging_flags) 2024 domain = ops->domain_alloc_paging_flags(dev, flags, NULL); 2025 else if (ops->domain_alloc && !flags) 2026 domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED); 2027 else 2028 return ERR_PTR(-EOPNOTSUPP); 2029 2030 if (IS_ERR(domain)) 2031 return domain; 2032 if (!domain) 2033 return ERR_PTR(-ENOMEM); 2034 2035 iommu_domain_init(domain, type, ops); 2036 return domain; 2037 } 2038 2039 /** 2040 * iommu_paging_domain_alloc_flags() - Allocate a paging domain 2041 * @dev: device for which the domain is allocated 2042 * @flags: Bitmap of iommufd_hwpt_alloc_flags 2043 * 2044 * Allocate a paging domain which will be managed by a kernel driver. Return 2045 * allocated domain if successful, or an ERR pointer for failure. 2046 */ 2047 struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev, 2048 unsigned int flags) 2049 { 2050 return __iommu_paging_domain_alloc_flags(dev, 2051 IOMMU_DOMAIN_UNMANAGED, flags); 2052 } 2053 EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc_flags); 2054 2055 void iommu_domain_free(struct iommu_domain *domain) 2056 { 2057 switch (domain->cookie_type) { 2058 case IOMMU_COOKIE_DMA_IOVA: 2059 iommu_put_dma_cookie(domain); 2060 break; 2061 case IOMMU_COOKIE_DMA_MSI: 2062 iommu_put_msi_cookie(domain); 2063 break; 2064 case IOMMU_COOKIE_SVA: 2065 mmdrop(domain->mm); 2066 break; 2067 default: 2068 break; 2069 } 2070 if (domain->ops->free) 2071 domain->ops->free(domain); 2072 } 2073 EXPORT_SYMBOL_GPL(iommu_domain_free); 2074 2075 /* 2076 * Put the group's domain back to the appropriate core-owned domain - either the 2077 * standard kernel-mode DMA configuration or an all-DMA-blocked domain. 2078 */ 2079 static void __iommu_group_set_core_domain(struct iommu_group *group) 2080 { 2081 struct iommu_domain *new_domain; 2082 2083 if (group->owner) 2084 new_domain = group->blocking_domain; 2085 else 2086 new_domain = group->default_domain; 2087 2088 __iommu_group_set_domain_nofail(group, new_domain); 2089 } 2090 2091 static int __iommu_attach_device(struct iommu_domain *domain, 2092 struct device *dev) 2093 { 2094 int ret; 2095 2096 if (unlikely(domain->ops->attach_dev == NULL)) 2097 return -ENODEV; 2098 2099 ret = domain->ops->attach_dev(domain, dev); 2100 if (ret) 2101 return ret; 2102 dev->iommu->attach_deferred = 0; 2103 trace_attach_device_to_domain(dev); 2104 return 0; 2105 } 2106 2107 /** 2108 * iommu_attach_device - Attach an IOMMU domain to a device 2109 * @domain: IOMMU domain to attach 2110 * @dev: Device that will be attached 2111 * 2112 * Returns 0 on success and error code on failure 2113 * 2114 * Note that EINVAL can be treated as a soft failure, indicating 2115 * that certain configuration of the domain is incompatible with 2116 * the device. In this case attaching a different domain to the 2117 * device may succeed. 2118 */ 2119 int iommu_attach_device(struct iommu_domain *domain, struct device *dev) 2120 { 2121 /* Caller must be a probed driver on dev */ 2122 struct iommu_group *group = dev->iommu_group; 2123 int ret; 2124 2125 if (!group) 2126 return -ENODEV; 2127 2128 /* 2129 * Lock the group to make sure the device-count doesn't 2130 * change while we are attaching 2131 */ 2132 mutex_lock(&group->mutex); 2133 ret = -EINVAL; 2134 if (list_count_nodes(&group->devices) != 1) 2135 goto out_unlock; 2136 2137 ret = __iommu_attach_group(domain, group); 2138 2139 out_unlock: 2140 mutex_unlock(&group->mutex); 2141 return ret; 2142 } 2143 EXPORT_SYMBOL_GPL(iommu_attach_device); 2144 2145 int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain) 2146 { 2147 if (dev->iommu && dev->iommu->attach_deferred) 2148 return __iommu_attach_device(domain, dev); 2149 2150 return 0; 2151 } 2152 2153 void iommu_detach_device(struct iommu_domain *domain, struct device *dev) 2154 { 2155 /* Caller must be a probed driver on dev */ 2156 struct iommu_group *group = dev->iommu_group; 2157 2158 if (!group) 2159 return; 2160 2161 mutex_lock(&group->mutex); 2162 if (WARN_ON(domain != group->domain) || 2163 WARN_ON(list_count_nodes(&group->devices) != 1)) 2164 goto out_unlock; 2165 __iommu_group_set_core_domain(group); 2166 2167 out_unlock: 2168 mutex_unlock(&group->mutex); 2169 } 2170 EXPORT_SYMBOL_GPL(iommu_detach_device); 2171 2172 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) 2173 { 2174 /* Caller must be a probed driver on dev */ 2175 struct iommu_group *group = dev->iommu_group; 2176 2177 if (!group) 2178 return NULL; 2179 2180 return group->domain; 2181 } 2182 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev); 2183 2184 /* 2185 * For IOMMU_DOMAIN_DMA implementations which already provide their own 2186 * guarantees that the group and its default domain are valid and correct. 2187 */ 2188 struct iommu_domain *iommu_get_dma_domain(struct device *dev) 2189 { 2190 return dev->iommu_group->default_domain; 2191 } 2192 2193 static void *iommu_make_pasid_array_entry(struct iommu_domain *domain, 2194 struct iommu_attach_handle *handle) 2195 { 2196 if (handle) { 2197 handle->domain = domain; 2198 return xa_tag_pointer(handle, IOMMU_PASID_ARRAY_HANDLE); 2199 } 2200 2201 return xa_tag_pointer(domain, IOMMU_PASID_ARRAY_DOMAIN); 2202 } 2203 2204 static int __iommu_attach_group(struct iommu_domain *domain, 2205 struct iommu_group *group) 2206 { 2207 struct device *dev; 2208 2209 if (group->domain && group->domain != group->default_domain && 2210 group->domain != group->blocking_domain) 2211 return -EBUSY; 2212 2213 dev = iommu_group_first_dev(group); 2214 if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner) 2215 return -EINVAL; 2216 2217 return __iommu_group_set_domain(group, domain); 2218 } 2219 2220 /** 2221 * iommu_attach_group - Attach an IOMMU domain to an IOMMU group 2222 * @domain: IOMMU domain to attach 2223 * @group: IOMMU group that will be attached 2224 * 2225 * Returns 0 on success and error code on failure 2226 * 2227 * Note that EINVAL can be treated as a soft failure, indicating 2228 * that certain configuration of the domain is incompatible with 2229 * the group. In this case attaching a different domain to the 2230 * group may succeed. 2231 */ 2232 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) 2233 { 2234 int ret; 2235 2236 mutex_lock(&group->mutex); 2237 ret = __iommu_attach_group(domain, group); 2238 mutex_unlock(&group->mutex); 2239 2240 return ret; 2241 } 2242 EXPORT_SYMBOL_GPL(iommu_attach_group); 2243 2244 static int __iommu_device_set_domain(struct iommu_group *group, 2245 struct device *dev, 2246 struct iommu_domain *new_domain, 2247 unsigned int flags) 2248 { 2249 int ret; 2250 2251 /* 2252 * If the device requires IOMMU_RESV_DIRECT then we cannot allow 2253 * the blocking domain to be attached as it does not contain the 2254 * required 1:1 mapping. This test effectively excludes the device 2255 * being used with iommu_group_claim_dma_owner() which will block 2256 * vfio and iommufd as well. 2257 */ 2258 if (dev->iommu->require_direct && 2259 (new_domain->type == IOMMU_DOMAIN_BLOCKED || 2260 new_domain == group->blocking_domain)) { 2261 dev_warn(dev, 2262 "Firmware has requested this device have a 1:1 IOMMU mapping, rejecting configuring the device without a 1:1 mapping. Contact your platform vendor.\n"); 2263 return -EINVAL; 2264 } 2265 2266 if (dev->iommu->attach_deferred) { 2267 if (new_domain == group->default_domain) 2268 return 0; 2269 dev->iommu->attach_deferred = 0; 2270 } 2271 2272 ret = __iommu_attach_device(new_domain, dev); 2273 if (ret) { 2274 /* 2275 * If we have a blocking domain then try to attach that in hopes 2276 * of avoiding a UAF. Modern drivers should implement blocking 2277 * domains as global statics that cannot fail. 2278 */ 2279 if ((flags & IOMMU_SET_DOMAIN_MUST_SUCCEED) && 2280 group->blocking_domain && 2281 group->blocking_domain != new_domain) 2282 __iommu_attach_device(group->blocking_domain, dev); 2283 return ret; 2284 } 2285 return 0; 2286 } 2287 2288 /* 2289 * If 0 is returned the group's domain is new_domain. If an error is returned 2290 * then the group's domain will be set back to the existing domain unless 2291 * IOMMU_SET_DOMAIN_MUST_SUCCEED, otherwise an error is returned and the group's 2292 * domains is left inconsistent. This is a driver bug to fail attach with a 2293 * previously good domain. We try to avoid a kernel UAF because of this. 2294 * 2295 * IOMMU groups are really the natural working unit of the IOMMU, but the IOMMU 2296 * API works on domains and devices. Bridge that gap by iterating over the 2297 * devices in a group. Ideally we'd have a single device which represents the 2298 * requestor ID of the group, but we also allow IOMMU drivers to create policy 2299 * defined minimum sets, where the physical hardware may be able to distiguish 2300 * members, but we wish to group them at a higher level (ex. untrusted 2301 * multi-function PCI devices). Thus we attach each device. 2302 */ 2303 static int __iommu_group_set_domain_internal(struct iommu_group *group, 2304 struct iommu_domain *new_domain, 2305 unsigned int flags) 2306 { 2307 struct group_device *last_gdev; 2308 struct group_device *gdev; 2309 int result; 2310 int ret; 2311 2312 lockdep_assert_held(&group->mutex); 2313 2314 if (group->domain == new_domain) 2315 return 0; 2316 2317 if (WARN_ON(!new_domain)) 2318 return -EINVAL; 2319 2320 /* 2321 * Changing the domain is done by calling attach_dev() on the new 2322 * domain. This switch does not have to be atomic and DMA can be 2323 * discarded during the transition. DMA must only be able to access 2324 * either new_domain or group->domain, never something else. 2325 */ 2326 result = 0; 2327 for_each_group_device(group, gdev) { 2328 ret = __iommu_device_set_domain(group, gdev->dev, new_domain, 2329 flags); 2330 if (ret) { 2331 result = ret; 2332 /* 2333 * Keep trying the other devices in the group. If a 2334 * driver fails attach to an otherwise good domain, and 2335 * does not support blocking domains, it should at least 2336 * drop its reference on the current domain so we don't 2337 * UAF. 2338 */ 2339 if (flags & IOMMU_SET_DOMAIN_MUST_SUCCEED) 2340 continue; 2341 goto err_revert; 2342 } 2343 } 2344 group->domain = new_domain; 2345 return result; 2346 2347 err_revert: 2348 /* 2349 * This is called in error unwind paths. A well behaved driver should 2350 * always allow us to attach to a domain that was already attached. 2351 */ 2352 last_gdev = gdev; 2353 for_each_group_device(group, gdev) { 2354 /* 2355 * A NULL domain can happen only for first probe, in which case 2356 * we leave group->domain as NULL and let release clean 2357 * everything up. 2358 */ 2359 if (group->domain) 2360 WARN_ON(__iommu_device_set_domain( 2361 group, gdev->dev, group->domain, 2362 IOMMU_SET_DOMAIN_MUST_SUCCEED)); 2363 if (gdev == last_gdev) 2364 break; 2365 } 2366 return ret; 2367 } 2368 2369 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) 2370 { 2371 mutex_lock(&group->mutex); 2372 __iommu_group_set_core_domain(group); 2373 mutex_unlock(&group->mutex); 2374 } 2375 EXPORT_SYMBOL_GPL(iommu_detach_group); 2376 2377 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) 2378 { 2379 if (domain->type == IOMMU_DOMAIN_IDENTITY) 2380 return iova; 2381 2382 if (domain->type == IOMMU_DOMAIN_BLOCKED) 2383 return 0; 2384 2385 return domain->ops->iova_to_phys(domain, iova); 2386 } 2387 EXPORT_SYMBOL_GPL(iommu_iova_to_phys); 2388 2389 static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova, 2390 phys_addr_t paddr, size_t size, size_t *count) 2391 { 2392 unsigned int pgsize_idx, pgsize_idx_next; 2393 unsigned long pgsizes; 2394 size_t offset, pgsize, pgsize_next; 2395 unsigned long addr_merge = paddr | iova; 2396 2397 /* Page sizes supported by the hardware and small enough for @size */ 2398 pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0); 2399 2400 /* Constrain the page sizes further based on the maximum alignment */ 2401 if (likely(addr_merge)) 2402 pgsizes &= GENMASK(__ffs(addr_merge), 0); 2403 2404 /* Make sure we have at least one suitable page size */ 2405 BUG_ON(!pgsizes); 2406 2407 /* Pick the biggest page size remaining */ 2408 pgsize_idx = __fls(pgsizes); 2409 pgsize = BIT(pgsize_idx); 2410 if (!count) 2411 return pgsize; 2412 2413 /* Find the next biggest support page size, if it exists */ 2414 pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0); 2415 if (!pgsizes) 2416 goto out_set_count; 2417 2418 pgsize_idx_next = __ffs(pgsizes); 2419 pgsize_next = BIT(pgsize_idx_next); 2420 2421 /* 2422 * There's no point trying a bigger page size unless the virtual 2423 * and physical addresses are similarly offset within the larger page. 2424 */ 2425 if ((iova ^ paddr) & (pgsize_next - 1)) 2426 goto out_set_count; 2427 2428 /* Calculate the offset to the next page size alignment boundary */ 2429 offset = pgsize_next - (addr_merge & (pgsize_next - 1)); 2430 2431 /* 2432 * If size is big enough to accommodate the larger page, reduce 2433 * the number of smaller pages. 2434 */ 2435 if (offset + pgsize_next <= size) 2436 size = offset; 2437 2438 out_set_count: 2439 *count = size >> pgsize_idx; 2440 return pgsize; 2441 } 2442 2443 static int __iommu_map(struct iommu_domain *domain, unsigned long iova, 2444 phys_addr_t paddr, size_t size, int prot, gfp_t gfp) 2445 { 2446 const struct iommu_domain_ops *ops = domain->ops; 2447 unsigned long orig_iova = iova; 2448 unsigned int min_pagesz; 2449 size_t orig_size = size; 2450 phys_addr_t orig_paddr = paddr; 2451 int ret = 0; 2452 2453 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) 2454 return -EINVAL; 2455 2456 if (WARN_ON(!ops->map_pages || domain->pgsize_bitmap == 0UL)) 2457 return -ENODEV; 2458 2459 /* find out the minimum page size supported */ 2460 min_pagesz = 1 << __ffs(domain->pgsize_bitmap); 2461 2462 /* 2463 * both the virtual address and the physical one, as well as 2464 * the size of the mapping, must be aligned (at least) to the 2465 * size of the smallest page supported by the hardware 2466 */ 2467 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { 2468 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n", 2469 iova, &paddr, size, min_pagesz); 2470 return -EINVAL; 2471 } 2472 2473 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size); 2474 2475 while (size) { 2476 size_t pgsize, count, mapped = 0; 2477 2478 pgsize = iommu_pgsize(domain, iova, paddr, size, &count); 2479 2480 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n", 2481 iova, &paddr, pgsize, count); 2482 ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot, 2483 gfp, &mapped); 2484 /* 2485 * Some pages may have been mapped, even if an error occurred, 2486 * so we should account for those so they can be unmapped. 2487 */ 2488 size -= mapped; 2489 2490 if (ret) 2491 break; 2492 2493 iova += mapped; 2494 paddr += mapped; 2495 } 2496 2497 /* unroll mapping in case something went wrong */ 2498 if (ret) 2499 iommu_unmap(domain, orig_iova, orig_size - size); 2500 else 2501 trace_map(orig_iova, orig_paddr, orig_size); 2502 2503 return ret; 2504 } 2505 2506 int iommu_map(struct iommu_domain *domain, unsigned long iova, 2507 phys_addr_t paddr, size_t size, int prot, gfp_t gfp) 2508 { 2509 const struct iommu_domain_ops *ops = domain->ops; 2510 int ret; 2511 2512 might_sleep_if(gfpflags_allow_blocking(gfp)); 2513 2514 /* Discourage passing strange GFP flags */ 2515 if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 | 2516 __GFP_HIGHMEM))) 2517 return -EINVAL; 2518 2519 ret = __iommu_map(domain, iova, paddr, size, prot, gfp); 2520 if (ret == 0 && ops->iotlb_sync_map) { 2521 ret = ops->iotlb_sync_map(domain, iova, size); 2522 if (ret) 2523 goto out_err; 2524 } 2525 2526 return ret; 2527 2528 out_err: 2529 /* undo mappings already done */ 2530 iommu_unmap(domain, iova, size); 2531 2532 return ret; 2533 } 2534 EXPORT_SYMBOL_GPL(iommu_map); 2535 2536 static size_t __iommu_unmap(struct iommu_domain *domain, 2537 unsigned long iova, size_t size, 2538 struct iommu_iotlb_gather *iotlb_gather) 2539 { 2540 const struct iommu_domain_ops *ops = domain->ops; 2541 size_t unmapped_page, unmapped = 0; 2542 unsigned long orig_iova = iova; 2543 unsigned int min_pagesz; 2544 2545 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) 2546 return 0; 2547 2548 if (WARN_ON(!ops->unmap_pages || domain->pgsize_bitmap == 0UL)) 2549 return 0; 2550 2551 /* find out the minimum page size supported */ 2552 min_pagesz = 1 << __ffs(domain->pgsize_bitmap); 2553 2554 /* 2555 * The virtual address, as well as the size of the mapping, must be 2556 * aligned (at least) to the size of the smallest page supported 2557 * by the hardware 2558 */ 2559 if (!IS_ALIGNED(iova | size, min_pagesz)) { 2560 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n", 2561 iova, size, min_pagesz); 2562 return 0; 2563 } 2564 2565 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size); 2566 2567 /* 2568 * Keep iterating until we either unmap 'size' bytes (or more) 2569 * or we hit an area that isn't mapped. 2570 */ 2571 while (unmapped < size) { 2572 size_t pgsize, count; 2573 2574 pgsize = iommu_pgsize(domain, iova, iova, size - unmapped, &count); 2575 unmapped_page = ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather); 2576 if (!unmapped_page) 2577 break; 2578 2579 pr_debug("unmapped: iova 0x%lx size 0x%zx\n", 2580 iova, unmapped_page); 2581 2582 iova += unmapped_page; 2583 unmapped += unmapped_page; 2584 } 2585 2586 trace_unmap(orig_iova, size, unmapped); 2587 return unmapped; 2588 } 2589 2590 /** 2591 * iommu_unmap() - Remove mappings from a range of IOVA 2592 * @domain: Domain to manipulate 2593 * @iova: IO virtual address to start 2594 * @size: Length of the range starting from @iova 2595 * 2596 * iommu_unmap() will remove a translation created by iommu_map(). It cannot 2597 * subdivide a mapping created by iommu_map(), so it should be called with IOVA 2598 * ranges that match what was passed to iommu_map(). The range can aggregate 2599 * contiguous iommu_map() calls so long as no individual range is split. 2600 * 2601 * Returns: Number of bytes of IOVA unmapped. iova + res will be the point 2602 * unmapping stopped. 2603 */ 2604 size_t iommu_unmap(struct iommu_domain *domain, 2605 unsigned long iova, size_t size) 2606 { 2607 struct iommu_iotlb_gather iotlb_gather; 2608 size_t ret; 2609 2610 iommu_iotlb_gather_init(&iotlb_gather); 2611 ret = __iommu_unmap(domain, iova, size, &iotlb_gather); 2612 iommu_iotlb_sync(domain, &iotlb_gather); 2613 2614 return ret; 2615 } 2616 EXPORT_SYMBOL_GPL(iommu_unmap); 2617 2618 size_t iommu_unmap_fast(struct iommu_domain *domain, 2619 unsigned long iova, size_t size, 2620 struct iommu_iotlb_gather *iotlb_gather) 2621 { 2622 return __iommu_unmap(domain, iova, size, iotlb_gather); 2623 } 2624 EXPORT_SYMBOL_GPL(iommu_unmap_fast); 2625 2626 ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova, 2627 struct scatterlist *sg, unsigned int nents, int prot, 2628 gfp_t gfp) 2629 { 2630 const struct iommu_domain_ops *ops = domain->ops; 2631 size_t len = 0, mapped = 0; 2632 phys_addr_t start; 2633 unsigned int i = 0; 2634 int ret; 2635 2636 might_sleep_if(gfpflags_allow_blocking(gfp)); 2637 2638 /* Discourage passing strange GFP flags */ 2639 if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 | 2640 __GFP_HIGHMEM))) 2641 return -EINVAL; 2642 2643 while (i <= nents) { 2644 phys_addr_t s_phys = sg_phys(sg); 2645 2646 if (len && s_phys != start + len) { 2647 ret = __iommu_map(domain, iova + mapped, start, 2648 len, prot, gfp); 2649 2650 if (ret) 2651 goto out_err; 2652 2653 mapped += len; 2654 len = 0; 2655 } 2656 2657 if (sg_dma_is_bus_address(sg)) 2658 goto next; 2659 2660 if (len) { 2661 len += sg->length; 2662 } else { 2663 len = sg->length; 2664 start = s_phys; 2665 } 2666 2667 next: 2668 if (++i < nents) 2669 sg = sg_next(sg); 2670 } 2671 2672 if (ops->iotlb_sync_map) { 2673 ret = ops->iotlb_sync_map(domain, iova, mapped); 2674 if (ret) 2675 goto out_err; 2676 } 2677 return mapped; 2678 2679 out_err: 2680 /* undo mappings already done */ 2681 iommu_unmap(domain, iova, mapped); 2682 2683 return ret; 2684 } 2685 EXPORT_SYMBOL_GPL(iommu_map_sg); 2686 2687 /** 2688 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework 2689 * @domain: the iommu domain where the fault has happened 2690 * @dev: the device where the fault has happened 2691 * @iova: the faulting address 2692 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...) 2693 * 2694 * This function should be called by the low-level IOMMU implementations 2695 * whenever IOMMU faults happen, to allow high-level users, that are 2696 * interested in such events, to know about them. 2697 * 2698 * This event may be useful for several possible use cases: 2699 * - mere logging of the event 2700 * - dynamic TLB/PTE loading 2701 * - if restarting of the faulting device is required 2702 * 2703 * Returns 0 on success and an appropriate error code otherwise (if dynamic 2704 * PTE/TLB loading will one day be supported, implementations will be able 2705 * to tell whether it succeeded or not according to this return value). 2706 * 2707 * Specifically, -ENOSYS is returned if a fault handler isn't installed 2708 * (though fault handlers can also return -ENOSYS, in case they want to 2709 * elicit the default behavior of the IOMMU drivers). 2710 */ 2711 int report_iommu_fault(struct iommu_domain *domain, struct device *dev, 2712 unsigned long iova, int flags) 2713 { 2714 int ret = -ENOSYS; 2715 2716 /* 2717 * if upper layers showed interest and installed a fault handler, 2718 * invoke it. 2719 */ 2720 if (domain->handler) 2721 ret = domain->handler(domain, dev, iova, flags, 2722 domain->handler_token); 2723 2724 trace_io_page_fault(dev, iova, flags); 2725 return ret; 2726 } 2727 EXPORT_SYMBOL_GPL(report_iommu_fault); 2728 2729 static int __init iommu_init(void) 2730 { 2731 iommu_group_kset = kset_create_and_add("iommu_groups", 2732 NULL, kernel_kobj); 2733 BUG_ON(!iommu_group_kset); 2734 2735 iommu_debugfs_setup(); 2736 2737 return 0; 2738 } 2739 core_initcall(iommu_init); 2740 2741 int iommu_set_pgtable_quirks(struct iommu_domain *domain, 2742 unsigned long quirk) 2743 { 2744 if (domain->type != IOMMU_DOMAIN_UNMANAGED) 2745 return -EINVAL; 2746 if (!domain->ops->set_pgtable_quirks) 2747 return -EINVAL; 2748 return domain->ops->set_pgtable_quirks(domain, quirk); 2749 } 2750 EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks); 2751 2752 /** 2753 * iommu_get_resv_regions - get reserved regions 2754 * @dev: device for which to get reserved regions 2755 * @list: reserved region list for device 2756 * 2757 * This returns a list of reserved IOVA regions specific to this device. 2758 * A domain user should not map IOVA in these ranges. 2759 */ 2760 void iommu_get_resv_regions(struct device *dev, struct list_head *list) 2761 { 2762 const struct iommu_ops *ops = dev_iommu_ops(dev); 2763 2764 if (ops->get_resv_regions) 2765 ops->get_resv_regions(dev, list); 2766 } 2767 EXPORT_SYMBOL_GPL(iommu_get_resv_regions); 2768 2769 /** 2770 * iommu_put_resv_regions - release reserved regions 2771 * @dev: device for which to free reserved regions 2772 * @list: reserved region list for device 2773 * 2774 * This releases a reserved region list acquired by iommu_get_resv_regions(). 2775 */ 2776 void iommu_put_resv_regions(struct device *dev, struct list_head *list) 2777 { 2778 struct iommu_resv_region *entry, *next; 2779 2780 list_for_each_entry_safe(entry, next, list, list) { 2781 if (entry->free) 2782 entry->free(dev, entry); 2783 else 2784 kfree(entry); 2785 } 2786 } 2787 EXPORT_SYMBOL(iommu_put_resv_regions); 2788 2789 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start, 2790 size_t length, int prot, 2791 enum iommu_resv_type type, 2792 gfp_t gfp) 2793 { 2794 struct iommu_resv_region *region; 2795 2796 region = kzalloc(sizeof(*region), gfp); 2797 if (!region) 2798 return NULL; 2799 2800 INIT_LIST_HEAD(®ion->list); 2801 region->start = start; 2802 region->length = length; 2803 region->prot = prot; 2804 region->type = type; 2805 return region; 2806 } 2807 EXPORT_SYMBOL_GPL(iommu_alloc_resv_region); 2808 2809 void iommu_set_default_passthrough(bool cmd_line) 2810 { 2811 if (cmd_line) 2812 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API; 2813 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY; 2814 } 2815 2816 void iommu_set_default_translated(bool cmd_line) 2817 { 2818 if (cmd_line) 2819 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API; 2820 iommu_def_domain_type = IOMMU_DOMAIN_DMA; 2821 } 2822 2823 bool iommu_default_passthrough(void) 2824 { 2825 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY; 2826 } 2827 EXPORT_SYMBOL_GPL(iommu_default_passthrough); 2828 2829 const struct iommu_ops *iommu_ops_from_fwnode(const struct fwnode_handle *fwnode) 2830 { 2831 const struct iommu_ops *ops = NULL; 2832 struct iommu_device *iommu; 2833 2834 spin_lock(&iommu_device_lock); 2835 list_for_each_entry(iommu, &iommu_device_list, list) 2836 if (iommu->fwnode == fwnode) { 2837 ops = iommu->ops; 2838 break; 2839 } 2840 spin_unlock(&iommu_device_lock); 2841 return ops; 2842 } 2843 2844 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode) 2845 { 2846 const struct iommu_ops *ops = iommu_ops_from_fwnode(iommu_fwnode); 2847 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 2848 2849 if (!ops) 2850 return driver_deferred_probe_check_state(dev); 2851 2852 if (fwspec) 2853 return ops == iommu_fwspec_ops(fwspec) ? 0 : -EINVAL; 2854 2855 if (!dev_iommu_get(dev)) 2856 return -ENOMEM; 2857 2858 /* Preallocate for the overwhelmingly common case of 1 ID */ 2859 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL); 2860 if (!fwspec) 2861 return -ENOMEM; 2862 2863 fwnode_handle_get(iommu_fwnode); 2864 fwspec->iommu_fwnode = iommu_fwnode; 2865 dev_iommu_fwspec_set(dev, fwspec); 2866 return 0; 2867 } 2868 EXPORT_SYMBOL_GPL(iommu_fwspec_init); 2869 2870 void iommu_fwspec_free(struct device *dev) 2871 { 2872 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 2873 2874 if (fwspec) { 2875 fwnode_handle_put(fwspec->iommu_fwnode); 2876 kfree(fwspec); 2877 dev_iommu_fwspec_set(dev, NULL); 2878 } 2879 } 2880 2881 int iommu_fwspec_add_ids(struct device *dev, const u32 *ids, int num_ids) 2882 { 2883 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 2884 int i, new_num; 2885 2886 if (!fwspec) 2887 return -EINVAL; 2888 2889 new_num = fwspec->num_ids + num_ids; 2890 if (new_num > 1) { 2891 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num), 2892 GFP_KERNEL); 2893 if (!fwspec) 2894 return -ENOMEM; 2895 2896 dev_iommu_fwspec_set(dev, fwspec); 2897 } 2898 2899 for (i = 0; i < num_ids; i++) 2900 fwspec->ids[fwspec->num_ids + i] = ids[i]; 2901 2902 fwspec->num_ids = new_num; 2903 return 0; 2904 } 2905 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); 2906 2907 /* 2908 * Per device IOMMU features. 2909 */ 2910 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat) 2911 { 2912 if (dev_has_iommu(dev)) { 2913 const struct iommu_ops *ops = dev_iommu_ops(dev); 2914 2915 if (ops->dev_enable_feat) 2916 return ops->dev_enable_feat(dev, feat); 2917 } 2918 2919 return -ENODEV; 2920 } 2921 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature); 2922 2923 /* 2924 * The device drivers should do the necessary cleanups before calling this. 2925 */ 2926 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat) 2927 { 2928 if (dev_has_iommu(dev)) { 2929 const struct iommu_ops *ops = dev_iommu_ops(dev); 2930 2931 if (ops->dev_disable_feat) 2932 return ops->dev_disable_feat(dev, feat); 2933 } 2934 2935 return -EBUSY; 2936 } 2937 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature); 2938 2939 /** 2940 * iommu_setup_default_domain - Set the default_domain for the group 2941 * @group: Group to change 2942 * @target_type: Domain type to set as the default_domain 2943 * 2944 * Allocate a default domain and set it as the current domain on the group. If 2945 * the group already has a default domain it will be changed to the target_type. 2946 * When target_type is 0 the default domain is selected based on driver and 2947 * system preferences. 2948 */ 2949 static int iommu_setup_default_domain(struct iommu_group *group, 2950 int target_type) 2951 { 2952 struct iommu_domain *old_dom = group->default_domain; 2953 struct group_device *gdev; 2954 struct iommu_domain *dom; 2955 bool direct_failed; 2956 int req_type; 2957 int ret; 2958 2959 lockdep_assert_held(&group->mutex); 2960 2961 req_type = iommu_get_default_domain_type(group, target_type); 2962 if (req_type < 0) 2963 return -EINVAL; 2964 2965 dom = iommu_group_alloc_default_domain(group, req_type); 2966 if (IS_ERR(dom)) 2967 return PTR_ERR(dom); 2968 2969 if (group->default_domain == dom) 2970 return 0; 2971 2972 if (iommu_is_dma_domain(dom)) { 2973 ret = iommu_get_dma_cookie(dom); 2974 if (ret) { 2975 iommu_domain_free(dom); 2976 return ret; 2977 } 2978 } 2979 2980 /* 2981 * IOMMU_RESV_DIRECT and IOMMU_RESV_DIRECT_RELAXABLE regions must be 2982 * mapped before their device is attached, in order to guarantee 2983 * continuity with any FW activity 2984 */ 2985 direct_failed = false; 2986 for_each_group_device(group, gdev) { 2987 if (iommu_create_device_direct_mappings(dom, gdev->dev)) { 2988 direct_failed = true; 2989 dev_warn_once( 2990 gdev->dev->iommu->iommu_dev->dev, 2991 "IOMMU driver was not able to establish FW requested direct mapping."); 2992 } 2993 } 2994 2995 /* We must set default_domain early for __iommu_device_set_domain */ 2996 group->default_domain = dom; 2997 if (!group->domain) { 2998 /* 2999 * Drivers are not allowed to fail the first domain attach. 3000 * The only way to recover from this is to fail attaching the 3001 * iommu driver and call ops->release_device. Put the domain 3002 * in group->default_domain so it is freed after. 3003 */ 3004 ret = __iommu_group_set_domain_internal( 3005 group, dom, IOMMU_SET_DOMAIN_MUST_SUCCEED); 3006 if (WARN_ON(ret)) 3007 goto out_free_old; 3008 } else { 3009 ret = __iommu_group_set_domain(group, dom); 3010 if (ret) 3011 goto err_restore_def_domain; 3012 } 3013 3014 /* 3015 * Drivers are supposed to allow mappings to be installed in a domain 3016 * before device attachment, but some don't. Hack around this defect by 3017 * trying again after attaching. If this happens it means the device 3018 * will not continuously have the IOMMU_RESV_DIRECT map. 3019 */ 3020 if (direct_failed) { 3021 for_each_group_device(group, gdev) { 3022 ret = iommu_create_device_direct_mappings(dom, gdev->dev); 3023 if (ret) 3024 goto err_restore_domain; 3025 } 3026 } 3027 3028 out_free_old: 3029 if (old_dom) 3030 iommu_domain_free(old_dom); 3031 return ret; 3032 3033 err_restore_domain: 3034 if (old_dom) 3035 __iommu_group_set_domain_internal( 3036 group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED); 3037 err_restore_def_domain: 3038 if (old_dom) { 3039 iommu_domain_free(dom); 3040 group->default_domain = old_dom; 3041 } 3042 return ret; 3043 } 3044 3045 /* 3046 * Changing the default domain through sysfs requires the users to unbind the 3047 * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ 3048 * transition. Return failure if this isn't met. 3049 * 3050 * We need to consider the race between this and the device release path. 3051 * group->mutex is used here to guarantee that the device release path 3052 * will not be entered at the same time. 3053 */ 3054 static ssize_t iommu_group_store_type(struct iommu_group *group, 3055 const char *buf, size_t count) 3056 { 3057 struct group_device *gdev; 3058 int ret, req_type; 3059 3060 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) 3061 return -EACCES; 3062 3063 if (WARN_ON(!group) || !group->default_domain) 3064 return -EINVAL; 3065 3066 if (sysfs_streq(buf, "identity")) 3067 req_type = IOMMU_DOMAIN_IDENTITY; 3068 else if (sysfs_streq(buf, "DMA")) 3069 req_type = IOMMU_DOMAIN_DMA; 3070 else if (sysfs_streq(buf, "DMA-FQ")) 3071 req_type = IOMMU_DOMAIN_DMA_FQ; 3072 else if (sysfs_streq(buf, "auto")) 3073 req_type = 0; 3074 else 3075 return -EINVAL; 3076 3077 mutex_lock(&group->mutex); 3078 /* We can bring up a flush queue without tearing down the domain. */ 3079 if (req_type == IOMMU_DOMAIN_DMA_FQ && 3080 group->default_domain->type == IOMMU_DOMAIN_DMA) { 3081 ret = iommu_dma_init_fq(group->default_domain); 3082 if (ret) 3083 goto out_unlock; 3084 3085 group->default_domain->type = IOMMU_DOMAIN_DMA_FQ; 3086 ret = count; 3087 goto out_unlock; 3088 } 3089 3090 /* Otherwise, ensure that device exists and no driver is bound. */ 3091 if (list_empty(&group->devices) || group->owner_cnt) { 3092 ret = -EPERM; 3093 goto out_unlock; 3094 } 3095 3096 ret = iommu_setup_default_domain(group, req_type); 3097 if (ret) 3098 goto out_unlock; 3099 3100 /* Make sure dma_ops is appropriatley set */ 3101 for_each_group_device(group, gdev) 3102 iommu_setup_dma_ops(gdev->dev); 3103 3104 out_unlock: 3105 mutex_unlock(&group->mutex); 3106 return ret ?: count; 3107 } 3108 3109 /** 3110 * iommu_device_use_default_domain() - Device driver wants to handle device 3111 * DMA through the kernel DMA API. 3112 * @dev: The device. 3113 * 3114 * The device driver about to bind @dev wants to do DMA through the kernel 3115 * DMA API. Return 0 if it is allowed, otherwise an error. 3116 */ 3117 int iommu_device_use_default_domain(struct device *dev) 3118 { 3119 /* Caller is the driver core during the pre-probe path */ 3120 struct iommu_group *group = dev->iommu_group; 3121 int ret = 0; 3122 3123 if (!group) 3124 return 0; 3125 3126 mutex_lock(&group->mutex); 3127 /* We may race against bus_iommu_probe() finalising groups here */ 3128 if (!group->default_domain) { 3129 ret = -EPROBE_DEFER; 3130 goto unlock_out; 3131 } 3132 if (group->owner_cnt) { 3133 if (group->domain != group->default_domain || group->owner || 3134 !xa_empty(&group->pasid_array)) { 3135 ret = -EBUSY; 3136 goto unlock_out; 3137 } 3138 } 3139 3140 group->owner_cnt++; 3141 3142 unlock_out: 3143 mutex_unlock(&group->mutex); 3144 return ret; 3145 } 3146 3147 /** 3148 * iommu_device_unuse_default_domain() - Device driver stops handling device 3149 * DMA through the kernel DMA API. 3150 * @dev: The device. 3151 * 3152 * The device driver doesn't want to do DMA through kernel DMA API anymore. 3153 * It must be called after iommu_device_use_default_domain(). 3154 */ 3155 void iommu_device_unuse_default_domain(struct device *dev) 3156 { 3157 /* Caller is the driver core during the post-probe path */ 3158 struct iommu_group *group = dev->iommu_group; 3159 3160 if (!group) 3161 return; 3162 3163 mutex_lock(&group->mutex); 3164 if (!WARN_ON(!group->owner_cnt || !xa_empty(&group->pasid_array))) 3165 group->owner_cnt--; 3166 3167 mutex_unlock(&group->mutex); 3168 } 3169 3170 static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) 3171 { 3172 struct device *dev = iommu_group_first_dev(group); 3173 const struct iommu_ops *ops = dev_iommu_ops(dev); 3174 struct iommu_domain *domain; 3175 3176 if (group->blocking_domain) 3177 return 0; 3178 3179 if (ops->blocked_domain) { 3180 group->blocking_domain = ops->blocked_domain; 3181 return 0; 3182 } 3183 3184 /* 3185 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED create an 3186 * empty PAGING domain instead. 3187 */ 3188 domain = iommu_paging_domain_alloc(dev); 3189 if (IS_ERR(domain)) 3190 return PTR_ERR(domain); 3191 group->blocking_domain = domain; 3192 return 0; 3193 } 3194 3195 static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner) 3196 { 3197 int ret; 3198 3199 if ((group->domain && group->domain != group->default_domain) || 3200 !xa_empty(&group->pasid_array)) 3201 return -EBUSY; 3202 3203 ret = __iommu_group_alloc_blocking_domain(group); 3204 if (ret) 3205 return ret; 3206 ret = __iommu_group_set_domain(group, group->blocking_domain); 3207 if (ret) 3208 return ret; 3209 3210 group->owner = owner; 3211 group->owner_cnt++; 3212 return 0; 3213 } 3214 3215 /** 3216 * iommu_group_claim_dma_owner() - Set DMA ownership of a group 3217 * @group: The group. 3218 * @owner: Caller specified pointer. Used for exclusive ownership. 3219 * 3220 * This is to support backward compatibility for vfio which manages the dma 3221 * ownership in iommu_group level. New invocations on this interface should be 3222 * prohibited. Only a single owner may exist for a group. 3223 */ 3224 int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner) 3225 { 3226 int ret = 0; 3227 3228 if (WARN_ON(!owner)) 3229 return -EINVAL; 3230 3231 mutex_lock(&group->mutex); 3232 if (group->owner_cnt) { 3233 ret = -EPERM; 3234 goto unlock_out; 3235 } 3236 3237 ret = __iommu_take_dma_ownership(group, owner); 3238 unlock_out: 3239 mutex_unlock(&group->mutex); 3240 3241 return ret; 3242 } 3243 EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner); 3244 3245 /** 3246 * iommu_device_claim_dma_owner() - Set DMA ownership of a device 3247 * @dev: The device. 3248 * @owner: Caller specified pointer. Used for exclusive ownership. 3249 * 3250 * Claim the DMA ownership of a device. Multiple devices in the same group may 3251 * concurrently claim ownership if they present the same owner value. Returns 0 3252 * on success and error code on failure 3253 */ 3254 int iommu_device_claim_dma_owner(struct device *dev, void *owner) 3255 { 3256 /* Caller must be a probed driver on dev */ 3257 struct iommu_group *group = dev->iommu_group; 3258 int ret = 0; 3259 3260 if (WARN_ON(!owner)) 3261 return -EINVAL; 3262 3263 if (!group) 3264 return -ENODEV; 3265 3266 mutex_lock(&group->mutex); 3267 if (group->owner_cnt) { 3268 if (group->owner != owner) { 3269 ret = -EPERM; 3270 goto unlock_out; 3271 } 3272 group->owner_cnt++; 3273 goto unlock_out; 3274 } 3275 3276 ret = __iommu_take_dma_ownership(group, owner); 3277 unlock_out: 3278 mutex_unlock(&group->mutex); 3279 return ret; 3280 } 3281 EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner); 3282 3283 static void __iommu_release_dma_ownership(struct iommu_group *group) 3284 { 3285 if (WARN_ON(!group->owner_cnt || !group->owner || 3286 !xa_empty(&group->pasid_array))) 3287 return; 3288 3289 group->owner_cnt = 0; 3290 group->owner = NULL; 3291 __iommu_group_set_domain_nofail(group, group->default_domain); 3292 } 3293 3294 /** 3295 * iommu_group_release_dma_owner() - Release DMA ownership of a group 3296 * @group: The group 3297 * 3298 * Release the DMA ownership claimed by iommu_group_claim_dma_owner(). 3299 */ 3300 void iommu_group_release_dma_owner(struct iommu_group *group) 3301 { 3302 mutex_lock(&group->mutex); 3303 __iommu_release_dma_ownership(group); 3304 mutex_unlock(&group->mutex); 3305 } 3306 EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner); 3307 3308 /** 3309 * iommu_device_release_dma_owner() - Release DMA ownership of a device 3310 * @dev: The device. 3311 * 3312 * Release the DMA ownership claimed by iommu_device_claim_dma_owner(). 3313 */ 3314 void iommu_device_release_dma_owner(struct device *dev) 3315 { 3316 /* Caller must be a probed driver on dev */ 3317 struct iommu_group *group = dev->iommu_group; 3318 3319 mutex_lock(&group->mutex); 3320 if (group->owner_cnt > 1) 3321 group->owner_cnt--; 3322 else 3323 __iommu_release_dma_ownership(group); 3324 mutex_unlock(&group->mutex); 3325 } 3326 EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner); 3327 3328 /** 3329 * iommu_group_dma_owner_claimed() - Query group dma ownership status 3330 * @group: The group. 3331 * 3332 * This provides status query on a given group. It is racy and only for 3333 * non-binding status reporting. 3334 */ 3335 bool iommu_group_dma_owner_claimed(struct iommu_group *group) 3336 { 3337 unsigned int user; 3338 3339 mutex_lock(&group->mutex); 3340 user = group->owner_cnt; 3341 mutex_unlock(&group->mutex); 3342 3343 return user; 3344 } 3345 EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed); 3346 3347 static void iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid, 3348 struct iommu_domain *domain) 3349 { 3350 const struct iommu_ops *ops = dev_iommu_ops(dev); 3351 struct iommu_domain *blocked_domain = ops->blocked_domain; 3352 3353 WARN_ON(blocked_domain->ops->set_dev_pasid(blocked_domain, 3354 dev, pasid, domain)); 3355 } 3356 3357 static int __iommu_set_group_pasid(struct iommu_domain *domain, 3358 struct iommu_group *group, ioasid_t pasid, 3359 struct iommu_domain *old) 3360 { 3361 struct group_device *device, *last_gdev; 3362 int ret; 3363 3364 for_each_group_device(group, device) { 3365 ret = domain->ops->set_dev_pasid(domain, device->dev, 3366 pasid, old); 3367 if (ret) 3368 goto err_revert; 3369 } 3370 3371 return 0; 3372 3373 err_revert: 3374 last_gdev = device; 3375 for_each_group_device(group, device) { 3376 if (device == last_gdev) 3377 break; 3378 /* 3379 * If no old domain, undo the succeeded devices/pasid. 3380 * Otherwise, rollback the succeeded devices/pasid to the old 3381 * domain. And it is a driver bug to fail attaching with a 3382 * previously good domain. 3383 */ 3384 if (!old || WARN_ON(old->ops->set_dev_pasid(old, device->dev, 3385 pasid, domain))) 3386 iommu_remove_dev_pasid(device->dev, pasid, domain); 3387 } 3388 return ret; 3389 } 3390 3391 static void __iommu_remove_group_pasid(struct iommu_group *group, 3392 ioasid_t pasid, 3393 struct iommu_domain *domain) 3394 { 3395 struct group_device *device; 3396 3397 for_each_group_device(group, device) 3398 iommu_remove_dev_pasid(device->dev, pasid, domain); 3399 } 3400 3401 /* 3402 * iommu_attach_device_pasid() - Attach a domain to pasid of device 3403 * @domain: the iommu domain. 3404 * @dev: the attached device. 3405 * @pasid: the pasid of the device. 3406 * @handle: the attach handle. 3407 * 3408 * Caller should always provide a new handle to avoid race with the paths 3409 * that have lockless reference to handle if it intends to pass a valid handle. 3410 * 3411 * Return: 0 on success, or an error. 3412 */ 3413 int iommu_attach_device_pasid(struct iommu_domain *domain, 3414 struct device *dev, ioasid_t pasid, 3415 struct iommu_attach_handle *handle) 3416 { 3417 /* Caller must be a probed driver on dev */ 3418 struct iommu_group *group = dev->iommu_group; 3419 struct group_device *device; 3420 const struct iommu_ops *ops; 3421 void *entry; 3422 int ret; 3423 3424 if (!group) 3425 return -ENODEV; 3426 3427 ops = dev_iommu_ops(dev); 3428 3429 if (!domain->ops->set_dev_pasid || 3430 !ops->blocked_domain || 3431 !ops->blocked_domain->ops->set_dev_pasid) 3432 return -EOPNOTSUPP; 3433 3434 if (ops != domain->owner || pasid == IOMMU_NO_PASID) 3435 return -EINVAL; 3436 3437 mutex_lock(&group->mutex); 3438 for_each_group_device(group, device) { 3439 if (pasid >= device->dev->iommu->max_pasids) { 3440 ret = -EINVAL; 3441 goto out_unlock; 3442 } 3443 } 3444 3445 entry = iommu_make_pasid_array_entry(domain, handle); 3446 3447 /* 3448 * Entry present is a failure case. Use xa_insert() instead of 3449 * xa_reserve(). 3450 */ 3451 ret = xa_insert(&group->pasid_array, pasid, XA_ZERO_ENTRY, GFP_KERNEL); 3452 if (ret) 3453 goto out_unlock; 3454 3455 ret = __iommu_set_group_pasid(domain, group, pasid, NULL); 3456 if (ret) { 3457 xa_release(&group->pasid_array, pasid); 3458 goto out_unlock; 3459 } 3460 3461 /* 3462 * The xa_insert() above reserved the memory, and the group->mutex is 3463 * held, this cannot fail. The new domain cannot be visible until the 3464 * operation succeeds as we cannot tolerate PRIs becoming concurrently 3465 * queued and then failing attach. 3466 */ 3467 WARN_ON(xa_is_err(xa_store(&group->pasid_array, 3468 pasid, entry, GFP_KERNEL))); 3469 3470 out_unlock: 3471 mutex_unlock(&group->mutex); 3472 return ret; 3473 } 3474 EXPORT_SYMBOL_GPL(iommu_attach_device_pasid); 3475 3476 /** 3477 * iommu_replace_device_pasid - Replace the domain that a specific pasid 3478 * of the device is attached to 3479 * @domain: the new iommu domain 3480 * @dev: the attached device. 3481 * @pasid: the pasid of the device. 3482 * @handle: the attach handle. 3483 * 3484 * This API allows the pasid to switch domains. The @pasid should have been 3485 * attached. Otherwise, this fails. The pasid will keep the old configuration 3486 * if replacement failed. 3487 * 3488 * Caller should always provide a new handle to avoid race with the paths 3489 * that have lockless reference to handle if it intends to pass a valid handle. 3490 * 3491 * Return 0 on success, or an error. 3492 */ 3493 int iommu_replace_device_pasid(struct iommu_domain *domain, 3494 struct device *dev, ioasid_t pasid, 3495 struct iommu_attach_handle *handle) 3496 { 3497 /* Caller must be a probed driver on dev */ 3498 struct iommu_group *group = dev->iommu_group; 3499 struct iommu_attach_handle *entry; 3500 struct iommu_domain *curr_domain; 3501 void *curr; 3502 int ret; 3503 3504 if (!group) 3505 return -ENODEV; 3506 3507 if (!domain->ops->set_dev_pasid) 3508 return -EOPNOTSUPP; 3509 3510 if (dev_iommu_ops(dev) != domain->owner || 3511 pasid == IOMMU_NO_PASID || !handle) 3512 return -EINVAL; 3513 3514 mutex_lock(&group->mutex); 3515 entry = iommu_make_pasid_array_entry(domain, handle); 3516 curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, 3517 XA_ZERO_ENTRY, GFP_KERNEL); 3518 if (xa_is_err(curr)) { 3519 ret = xa_err(curr); 3520 goto out_unlock; 3521 } 3522 3523 /* 3524 * No domain (with or without handle) attached, hence not 3525 * a replace case. 3526 */ 3527 if (!curr) { 3528 xa_release(&group->pasid_array, pasid); 3529 ret = -EINVAL; 3530 goto out_unlock; 3531 } 3532 3533 /* 3534 * Reusing handle is problematic as there are paths that refers 3535 * the handle without lock. To avoid race, reject the callers that 3536 * attempt it. 3537 */ 3538 if (curr == entry) { 3539 WARN_ON(1); 3540 ret = -EINVAL; 3541 goto out_unlock; 3542 } 3543 3544 curr_domain = pasid_array_entry_to_domain(curr); 3545 ret = 0; 3546 3547 if (curr_domain != domain) { 3548 ret = __iommu_set_group_pasid(domain, group, 3549 pasid, curr_domain); 3550 if (ret) 3551 goto out_unlock; 3552 } 3553 3554 /* 3555 * The above xa_cmpxchg() reserved the memory, and the 3556 * group->mutex is held, this cannot fail. 3557 */ 3558 WARN_ON(xa_is_err(xa_store(&group->pasid_array, 3559 pasid, entry, GFP_KERNEL))); 3560 3561 out_unlock: 3562 mutex_unlock(&group->mutex); 3563 return ret; 3564 } 3565 EXPORT_SYMBOL_NS_GPL(iommu_replace_device_pasid, "IOMMUFD_INTERNAL"); 3566 3567 /* 3568 * iommu_detach_device_pasid() - Detach the domain from pasid of device 3569 * @domain: the iommu domain. 3570 * @dev: the attached device. 3571 * @pasid: the pasid of the device. 3572 * 3573 * The @domain must have been attached to @pasid of the @dev with 3574 * iommu_attach_device_pasid(). 3575 */ 3576 void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, 3577 ioasid_t pasid) 3578 { 3579 /* Caller must be a probed driver on dev */ 3580 struct iommu_group *group = dev->iommu_group; 3581 3582 mutex_lock(&group->mutex); 3583 __iommu_remove_group_pasid(group, pasid, domain); 3584 xa_erase(&group->pasid_array, pasid); 3585 mutex_unlock(&group->mutex); 3586 } 3587 EXPORT_SYMBOL_GPL(iommu_detach_device_pasid); 3588 3589 ioasid_t iommu_alloc_global_pasid(struct device *dev) 3590 { 3591 int ret; 3592 3593 /* max_pasids == 0 means that the device does not support PASID */ 3594 if (!dev->iommu->max_pasids) 3595 return IOMMU_PASID_INVALID; 3596 3597 /* 3598 * max_pasids is set up by vendor driver based on number of PASID bits 3599 * supported but the IDA allocation is inclusive. 3600 */ 3601 ret = ida_alloc_range(&iommu_global_pasid_ida, IOMMU_FIRST_GLOBAL_PASID, 3602 dev->iommu->max_pasids - 1, GFP_KERNEL); 3603 return ret < 0 ? IOMMU_PASID_INVALID : ret; 3604 } 3605 EXPORT_SYMBOL_GPL(iommu_alloc_global_pasid); 3606 3607 void iommu_free_global_pasid(ioasid_t pasid) 3608 { 3609 if (WARN_ON(pasid == IOMMU_PASID_INVALID)) 3610 return; 3611 3612 ida_free(&iommu_global_pasid_ida, pasid); 3613 } 3614 EXPORT_SYMBOL_GPL(iommu_free_global_pasid); 3615 3616 /** 3617 * iommu_attach_handle_get - Return the attach handle 3618 * @group: the iommu group that domain was attached to 3619 * @pasid: the pasid within the group 3620 * @type: matched domain type, 0 for any match 3621 * 3622 * Return handle or ERR_PTR(-ENOENT) on none, ERR_PTR(-EBUSY) on mismatch. 3623 * 3624 * Return the attach handle to the caller. The life cycle of an iommu attach 3625 * handle is from the time when the domain is attached to the time when the 3626 * domain is detached. Callers are required to synchronize the call of 3627 * iommu_attach_handle_get() with domain attachment and detachment. The attach 3628 * handle can only be used during its life cycle. 3629 */ 3630 struct iommu_attach_handle * 3631 iommu_attach_handle_get(struct iommu_group *group, ioasid_t pasid, unsigned int type) 3632 { 3633 struct iommu_attach_handle *handle; 3634 void *entry; 3635 3636 xa_lock(&group->pasid_array); 3637 entry = xa_load(&group->pasid_array, pasid); 3638 if (!entry || xa_pointer_tag(entry) != IOMMU_PASID_ARRAY_HANDLE) { 3639 handle = ERR_PTR(-ENOENT); 3640 } else { 3641 handle = xa_untag_pointer(entry); 3642 if (type && handle->domain->type != type) 3643 handle = ERR_PTR(-EBUSY); 3644 } 3645 xa_unlock(&group->pasid_array); 3646 3647 return handle; 3648 } 3649 EXPORT_SYMBOL_NS_GPL(iommu_attach_handle_get, "IOMMUFD_INTERNAL"); 3650 3651 /** 3652 * iommu_attach_group_handle - Attach an IOMMU domain to an IOMMU group 3653 * @domain: IOMMU domain to attach 3654 * @group: IOMMU group that will be attached 3655 * @handle: attach handle 3656 * 3657 * Returns 0 on success and error code on failure. 3658 * 3659 * This is a variant of iommu_attach_group(). It allows the caller to provide 3660 * an attach handle and use it when the domain is attached. This is currently 3661 * used by IOMMUFD to deliver the I/O page faults. 3662 * 3663 * Caller should always provide a new handle to avoid race with the paths 3664 * that have lockless reference to handle. 3665 */ 3666 int iommu_attach_group_handle(struct iommu_domain *domain, 3667 struct iommu_group *group, 3668 struct iommu_attach_handle *handle) 3669 { 3670 void *entry; 3671 int ret; 3672 3673 if (!handle) 3674 return -EINVAL; 3675 3676 mutex_lock(&group->mutex); 3677 entry = iommu_make_pasid_array_entry(domain, handle); 3678 ret = xa_insert(&group->pasid_array, 3679 IOMMU_NO_PASID, XA_ZERO_ENTRY, GFP_KERNEL); 3680 if (ret) 3681 goto out_unlock; 3682 3683 ret = __iommu_attach_group(domain, group); 3684 if (ret) { 3685 xa_release(&group->pasid_array, IOMMU_NO_PASID); 3686 goto out_unlock; 3687 } 3688 3689 /* 3690 * The xa_insert() above reserved the memory, and the group->mutex is 3691 * held, this cannot fail. The new domain cannot be visible until the 3692 * operation succeeds as we cannot tolerate PRIs becoming concurrently 3693 * queued and then failing attach. 3694 */ 3695 WARN_ON(xa_is_err(xa_store(&group->pasid_array, 3696 IOMMU_NO_PASID, entry, GFP_KERNEL))); 3697 3698 out_unlock: 3699 mutex_unlock(&group->mutex); 3700 return ret; 3701 } 3702 EXPORT_SYMBOL_NS_GPL(iommu_attach_group_handle, "IOMMUFD_INTERNAL"); 3703 3704 /** 3705 * iommu_detach_group_handle - Detach an IOMMU domain from an IOMMU group 3706 * @domain: IOMMU domain to attach 3707 * @group: IOMMU group that will be attached 3708 * 3709 * Detach the specified IOMMU domain from the specified IOMMU group. 3710 * It must be used in conjunction with iommu_attach_group_handle(). 3711 */ 3712 void iommu_detach_group_handle(struct iommu_domain *domain, 3713 struct iommu_group *group) 3714 { 3715 mutex_lock(&group->mutex); 3716 __iommu_group_set_core_domain(group); 3717 xa_erase(&group->pasid_array, IOMMU_NO_PASID); 3718 mutex_unlock(&group->mutex); 3719 } 3720 EXPORT_SYMBOL_NS_GPL(iommu_detach_group_handle, "IOMMUFD_INTERNAL"); 3721 3722 /** 3723 * iommu_replace_group_handle - replace the domain that a group is attached to 3724 * @group: IOMMU group that will be attached to the new domain 3725 * @new_domain: new IOMMU domain to replace with 3726 * @handle: attach handle 3727 * 3728 * This API allows the group to switch domains without being forced to go to 3729 * the blocking domain in-between. It allows the caller to provide an attach 3730 * handle for the new domain and use it when the domain is attached. 3731 * 3732 * If the currently attached domain is a core domain (e.g. a default_domain), 3733 * it will act just like the iommu_attach_group_handle(). 3734 * 3735 * Caller should always provide a new handle to avoid race with the paths 3736 * that have lockless reference to handle. 3737 */ 3738 int iommu_replace_group_handle(struct iommu_group *group, 3739 struct iommu_domain *new_domain, 3740 struct iommu_attach_handle *handle) 3741 { 3742 void *curr, *entry; 3743 int ret; 3744 3745 if (!new_domain || !handle) 3746 return -EINVAL; 3747 3748 mutex_lock(&group->mutex); 3749 entry = iommu_make_pasid_array_entry(new_domain, handle); 3750 ret = xa_reserve(&group->pasid_array, IOMMU_NO_PASID, GFP_KERNEL); 3751 if (ret) 3752 goto err_unlock; 3753 3754 ret = __iommu_group_set_domain(group, new_domain); 3755 if (ret) 3756 goto err_release; 3757 3758 curr = xa_store(&group->pasid_array, IOMMU_NO_PASID, entry, GFP_KERNEL); 3759 WARN_ON(xa_is_err(curr)); 3760 3761 mutex_unlock(&group->mutex); 3762 3763 return 0; 3764 err_release: 3765 xa_release(&group->pasid_array, IOMMU_NO_PASID); 3766 err_unlock: 3767 mutex_unlock(&group->mutex); 3768 return ret; 3769 } 3770 EXPORT_SYMBOL_NS_GPL(iommu_replace_group_handle, "IOMMUFD_INTERNAL"); 3771 3772 #if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU) 3773 /** 3774 * iommu_dma_prepare_msi() - Map the MSI page in the IOMMU domain 3775 * @desc: MSI descriptor, will store the MSI page 3776 * @msi_addr: MSI target address to be mapped 3777 * 3778 * The implementation of sw_msi() should take msi_addr and map it to 3779 * an IOVA in the domain and call msi_desc_set_iommu_msi_iova() with the 3780 * mapping information. 3781 * 3782 * Return: 0 on success or negative error code if the mapping failed. 3783 */ 3784 int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr) 3785 { 3786 struct device *dev = msi_desc_to_dev(desc); 3787 struct iommu_group *group = dev->iommu_group; 3788 int ret = 0; 3789 3790 if (!group) 3791 return 0; 3792 3793 mutex_lock(&group->mutex); 3794 /* An IDENTITY domain must pass through */ 3795 if (group->domain && group->domain->type != IOMMU_DOMAIN_IDENTITY) { 3796 switch (group->domain->cookie_type) { 3797 case IOMMU_COOKIE_DMA_MSI: 3798 case IOMMU_COOKIE_DMA_IOVA: 3799 ret = iommu_dma_sw_msi(group->domain, desc, msi_addr); 3800 break; 3801 case IOMMU_COOKIE_IOMMUFD: 3802 ret = iommufd_sw_msi(group->domain, desc, msi_addr); 3803 break; 3804 default: 3805 ret = -EOPNOTSUPP; 3806 break; 3807 } 3808 } 3809 mutex_unlock(&group->mutex); 3810 return ret; 3811 } 3812 #endif /* CONFIG_IRQ_MSI_IOMMU */ 3813