1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2014 Intel Corp. 4 * Author: Jiang Liu <jiang.liu@linux.intel.com> 5 * 6 * This file is licensed under GPLv2. 7 * 8 * This file contains common code to support Message Signaled Interrupts for 9 * PCI compatible and non PCI compatible devices. 10 */ 11 #include <linux/device.h> 12 #include <linux/irq.h> 13 #include <linux/irqdomain.h> 14 #include <linux/msi.h> 15 #include <linux/mutex.h> 16 #include <linux/pci.h> 17 #include <linux/slab.h> 18 #include <linux/seq_file.h> 19 #include <linux/sysfs.h> 20 #include <linux/types.h> 21 #include <linux/xarray.h> 22 23 #include "internals.h" 24 25 /** 26 * struct msi_device_data - MSI per device data 27 * @properties: MSI properties which are interesting to drivers 28 * @mutex: Mutex protecting the MSI descriptor store 29 * @__domains: Internal data for per device MSI domains 30 * @__iter_idx: Index to search the next entry for iterators 31 */ 32 struct msi_device_data { 33 unsigned long properties; 34 struct mutex mutex; 35 struct msi_dev_domain __domains[MSI_MAX_DEVICE_IRQDOMAINS]; 36 unsigned long __iter_idx; 37 }; 38 39 /** 40 * struct msi_ctrl - MSI internal management control structure 41 * @domid: ID of the domain on which management operations should be done 42 * @first: First (hardware) slot index to operate on 43 * @last: Last (hardware) slot index to operate on 44 * @nirqs: The number of Linux interrupts to allocate. Can be larger 45 * than the range due to PCI/multi-MSI. 46 */ 47 struct msi_ctrl { 48 unsigned int domid; 49 unsigned int first; 50 unsigned int last; 51 unsigned int nirqs; 52 }; 53 54 /* Invalid Xarray index which is outside of any searchable range */ 55 #define MSI_XA_MAX_INDEX (ULONG_MAX - 1) 56 /* The maximum domain size */ 57 #define MSI_XA_DOMAIN_SIZE (MSI_MAX_INDEX + 1) 58 59 static void msi_domain_free_locked(struct device *dev, struct msi_ctrl *ctrl); 60 static unsigned int msi_domain_get_hwsize(struct device *dev, unsigned int domid); 61 static inline int msi_sysfs_create_group(struct device *dev); 62 63 64 /** 65 * msi_alloc_desc - Allocate an initialized msi_desc 66 * @dev: Pointer to the device for which this is allocated 67 * @nvec: The number of vectors used in this entry 68 * @affinity: Optional pointer to an affinity mask array size of @nvec 69 * 70 * If @affinity is not %NULL then an affinity array[@nvec] is allocated 71 * and the affinity masks and flags from @affinity are copied. 72 * 73 * Return: pointer to allocated &msi_desc on success or %NULL on failure 74 */ 75 static struct msi_desc *msi_alloc_desc(struct device *dev, int nvec, 76 const struct irq_affinity_desc *affinity) 77 { 78 struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL); 79 80 if (!desc) 81 return NULL; 82 83 desc->dev = dev; 84 desc->nvec_used = nvec; 85 if (affinity) { 86 desc->affinity = kmemdup_array(affinity, nvec, sizeof(*desc->affinity), GFP_KERNEL); 87 if (!desc->affinity) { 88 kfree(desc); 89 return NULL; 90 } 91 } 92 return desc; 93 } 94 95 static void msi_free_desc(struct msi_desc *desc) 96 { 97 kfree(desc->affinity); 98 kfree(desc); 99 } 100 101 static int msi_insert_desc(struct device *dev, struct msi_desc *desc, 102 unsigned int domid, unsigned int index) 103 { 104 struct msi_device_data *md = dev->msi.data; 105 struct xarray *xa = &md->__domains[domid].store; 106 unsigned int hwsize; 107 int ret; 108 109 hwsize = msi_domain_get_hwsize(dev, domid); 110 111 if (index == MSI_ANY_INDEX) { 112 struct xa_limit limit = { .min = 0, .max = hwsize - 1 }; 113 unsigned int index; 114 115 /* Let the xarray allocate a free index within the limit */ 116 ret = xa_alloc(xa, &index, desc, limit, GFP_KERNEL); 117 if (ret) 118 goto fail; 119 120 desc->msi_index = index; 121 return 0; 122 } else { 123 if (index >= hwsize) { 124 ret = -ERANGE; 125 goto fail; 126 } 127 128 desc->msi_index = index; 129 ret = xa_insert(xa, index, desc, GFP_KERNEL); 130 if (ret) 131 goto fail; 132 return 0; 133 } 134 fail: 135 msi_free_desc(desc); 136 return ret; 137 } 138 139 /** 140 * msi_domain_insert_msi_desc - Allocate and initialize a MSI descriptor and 141 * insert it at @init_desc->msi_index 142 * 143 * @dev: Pointer to the device for which the descriptor is allocated 144 * @domid: The id of the interrupt domain to which the desriptor is added 145 * @init_desc: Pointer to an MSI descriptor to initialize the new descriptor 146 * 147 * Return: 0 on success or an appropriate failure code. 148 */ 149 int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid, 150 struct msi_desc *init_desc) 151 { 152 struct msi_desc *desc; 153 154 lockdep_assert_held(&dev->msi.data->mutex); 155 156 desc = msi_alloc_desc(dev, init_desc->nvec_used, init_desc->affinity); 157 if (!desc) 158 return -ENOMEM; 159 160 /* Copy type specific data to the new descriptor. */ 161 desc->pci = init_desc->pci; 162 163 return msi_insert_desc(dev, desc, domid, init_desc->msi_index); 164 } 165 166 static bool msi_desc_match(struct msi_desc *desc, enum msi_desc_filter filter) 167 { 168 switch (filter) { 169 case MSI_DESC_ALL: 170 return true; 171 case MSI_DESC_NOTASSOCIATED: 172 return !desc->irq; 173 case MSI_DESC_ASSOCIATED: 174 return !!desc->irq; 175 } 176 WARN_ON_ONCE(1); 177 return false; 178 } 179 180 static bool msi_ctrl_valid(struct device *dev, struct msi_ctrl *ctrl) 181 { 182 unsigned int hwsize; 183 184 if (WARN_ON_ONCE(ctrl->domid >= MSI_MAX_DEVICE_IRQDOMAINS || 185 (dev->msi.domain && 186 !dev->msi.data->__domains[ctrl->domid].domain))) 187 return false; 188 189 hwsize = msi_domain_get_hwsize(dev, ctrl->domid); 190 if (WARN_ON_ONCE(ctrl->first > ctrl->last || 191 ctrl->first >= hwsize || 192 ctrl->last >= hwsize)) 193 return false; 194 return true; 195 } 196 197 static void msi_domain_free_descs(struct device *dev, struct msi_ctrl *ctrl) 198 { 199 struct msi_desc *desc; 200 struct xarray *xa; 201 unsigned long idx; 202 203 lockdep_assert_held(&dev->msi.data->mutex); 204 205 if (!msi_ctrl_valid(dev, ctrl)) 206 return; 207 208 xa = &dev->msi.data->__domains[ctrl->domid].store; 209 xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) { 210 xa_erase(xa, idx); 211 212 /* Leak the descriptor when it is still referenced */ 213 if (WARN_ON_ONCE(msi_desc_match(desc, MSI_DESC_ASSOCIATED))) 214 continue; 215 msi_free_desc(desc); 216 } 217 } 218 219 /** 220 * msi_domain_free_msi_descs_range - Free a range of MSI descriptors of a device in an irqdomain 221 * @dev: Device for which to free the descriptors 222 * @domid: Id of the domain to operate on 223 * @first: Index to start freeing from (inclusive) 224 * @last: Last index to be freed (inclusive) 225 */ 226 void msi_domain_free_msi_descs_range(struct device *dev, unsigned int domid, 227 unsigned int first, unsigned int last) 228 { 229 struct msi_ctrl ctrl = { 230 .domid = domid, 231 .first = first, 232 .last = last, 233 }; 234 235 msi_domain_free_descs(dev, &ctrl); 236 } 237 238 /** 239 * msi_domain_add_simple_msi_descs - Allocate and initialize MSI descriptors 240 * @dev: Pointer to the device for which the descriptors are allocated 241 * @ctrl: Allocation control struct 242 * 243 * Return: 0 on success or an appropriate failure code. 244 */ 245 static int msi_domain_add_simple_msi_descs(struct device *dev, struct msi_ctrl *ctrl) 246 { 247 struct msi_desc *desc; 248 unsigned int idx; 249 int ret; 250 251 lockdep_assert_held(&dev->msi.data->mutex); 252 253 if (!msi_ctrl_valid(dev, ctrl)) 254 return -EINVAL; 255 256 for (idx = ctrl->first; idx <= ctrl->last; idx++) { 257 desc = msi_alloc_desc(dev, 1, NULL); 258 if (!desc) 259 goto fail_mem; 260 ret = msi_insert_desc(dev, desc, ctrl->domid, idx); 261 if (ret) 262 goto fail; 263 } 264 return 0; 265 266 fail_mem: 267 ret = -ENOMEM; 268 fail: 269 msi_domain_free_descs(dev, ctrl); 270 return ret; 271 } 272 273 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg) 274 { 275 *msg = entry->msg; 276 } 277 278 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg) 279 { 280 struct msi_desc *entry = irq_get_msi_desc(irq); 281 282 __get_cached_msi_msg(entry, msg); 283 } 284 EXPORT_SYMBOL_GPL(get_cached_msi_msg); 285 286 static void msi_device_data_release(struct device *dev, void *res) 287 { 288 struct msi_device_data *md = res; 289 int i; 290 291 for (i = 0; i < MSI_MAX_DEVICE_IRQDOMAINS; i++) { 292 msi_remove_device_irq_domain(dev, i); 293 WARN_ON_ONCE(!xa_empty(&md->__domains[i].store)); 294 xa_destroy(&md->__domains[i].store); 295 } 296 dev->msi.data = NULL; 297 } 298 299 /** 300 * msi_setup_device_data - Setup MSI device data 301 * @dev: Device for which MSI device data should be set up 302 * 303 * Return: 0 on success, appropriate error code otherwise 304 * 305 * This can be called more than once for @dev. If the MSI device data is 306 * already allocated the call succeeds. The allocated memory is 307 * automatically released when the device is destroyed. 308 */ 309 int msi_setup_device_data(struct device *dev) 310 { 311 struct msi_device_data *md; 312 int ret, i; 313 314 if (dev->msi.data) 315 return 0; 316 317 md = devres_alloc(msi_device_data_release, sizeof(*md), GFP_KERNEL); 318 if (!md) 319 return -ENOMEM; 320 321 ret = msi_sysfs_create_group(dev); 322 if (ret) { 323 devres_free(md); 324 return ret; 325 } 326 327 for (i = 0; i < MSI_MAX_DEVICE_IRQDOMAINS; i++) 328 xa_init_flags(&md->__domains[i].store, XA_FLAGS_ALLOC); 329 330 /* 331 * If @dev::msi::domain is set and is a global MSI domain, copy the 332 * pointer into the domain array so all code can operate on domain 333 * ids. The NULL pointer check is required to keep the legacy 334 * architecture specific PCI/MSI support working. 335 */ 336 if (dev->msi.domain && !irq_domain_is_msi_parent(dev->msi.domain)) 337 md->__domains[MSI_DEFAULT_DOMAIN].domain = dev->msi.domain; 338 339 mutex_init(&md->mutex); 340 dev->msi.data = md; 341 devres_add(dev, md); 342 return 0; 343 } 344 345 /** 346 * msi_lock_descs - Lock the MSI descriptor storage of a device 347 * @dev: Device to operate on 348 */ 349 void msi_lock_descs(struct device *dev) 350 { 351 mutex_lock(&dev->msi.data->mutex); 352 } 353 EXPORT_SYMBOL_GPL(msi_lock_descs); 354 355 /** 356 * msi_unlock_descs - Unlock the MSI descriptor storage of a device 357 * @dev: Device to operate on 358 */ 359 void msi_unlock_descs(struct device *dev) 360 { 361 /* Invalidate the index which was cached by the iterator */ 362 dev->msi.data->__iter_idx = MSI_XA_MAX_INDEX; 363 mutex_unlock(&dev->msi.data->mutex); 364 } 365 EXPORT_SYMBOL_GPL(msi_unlock_descs); 366 367 static struct msi_desc *msi_find_desc(struct msi_device_data *md, unsigned int domid, 368 enum msi_desc_filter filter) 369 { 370 struct xarray *xa = &md->__domains[domid].store; 371 struct msi_desc *desc; 372 373 xa_for_each_start(xa, md->__iter_idx, desc, md->__iter_idx) { 374 if (msi_desc_match(desc, filter)) 375 return desc; 376 } 377 md->__iter_idx = MSI_XA_MAX_INDEX; 378 return NULL; 379 } 380 381 /** 382 * msi_domain_first_desc - Get the first MSI descriptor of an irqdomain associated to a device 383 * @dev: Device to operate on 384 * @domid: The id of the interrupt domain which should be walked. 385 * @filter: Descriptor state filter 386 * 387 * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs() 388 * must be invoked before the call. 389 * 390 * Return: Pointer to the first MSI descriptor matching the search 391 * criteria, NULL if none found. 392 */ 393 struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid, 394 enum msi_desc_filter filter) 395 { 396 struct msi_device_data *md = dev->msi.data; 397 398 if (WARN_ON_ONCE(!md || domid >= MSI_MAX_DEVICE_IRQDOMAINS)) 399 return NULL; 400 401 lockdep_assert_held(&md->mutex); 402 403 md->__iter_idx = 0; 404 return msi_find_desc(md, domid, filter); 405 } 406 EXPORT_SYMBOL_GPL(msi_domain_first_desc); 407 408 /** 409 * msi_next_desc - Get the next MSI descriptor of a device 410 * @dev: Device to operate on 411 * @domid: The id of the interrupt domain which should be walked. 412 * @filter: Descriptor state filter 413 * 414 * The first invocation of msi_next_desc() has to be preceeded by a 415 * successful invocation of __msi_first_desc(). Consecutive invocations are 416 * only valid if the previous one was successful. All these operations have 417 * to be done within the same MSI mutex held region. 418 * 419 * Return: Pointer to the next MSI descriptor matching the search 420 * criteria, NULL if none found. 421 */ 422 struct msi_desc *msi_next_desc(struct device *dev, unsigned int domid, 423 enum msi_desc_filter filter) 424 { 425 struct msi_device_data *md = dev->msi.data; 426 427 if (WARN_ON_ONCE(!md || domid >= MSI_MAX_DEVICE_IRQDOMAINS)) 428 return NULL; 429 430 lockdep_assert_held(&md->mutex); 431 432 if (md->__iter_idx >= (unsigned long)MSI_MAX_INDEX) 433 return NULL; 434 435 md->__iter_idx++; 436 return msi_find_desc(md, domid, filter); 437 } 438 EXPORT_SYMBOL_GPL(msi_next_desc); 439 440 /** 441 * msi_domain_get_virq - Lookup the Linux interrupt number for a MSI index on a interrupt domain 442 * @dev: Device to operate on 443 * @domid: Domain ID of the interrupt domain associated to the device 444 * @index: MSI interrupt index to look for (0-based) 445 * 446 * Return: The Linux interrupt number on success (> 0), 0 if not found 447 */ 448 unsigned int msi_domain_get_virq(struct device *dev, unsigned int domid, unsigned int index) 449 { 450 struct msi_desc *desc; 451 unsigned int ret = 0; 452 bool pcimsi = false; 453 struct xarray *xa; 454 455 if (!dev->msi.data) 456 return 0; 457 458 if (WARN_ON_ONCE(index > MSI_MAX_INDEX || domid >= MSI_MAX_DEVICE_IRQDOMAINS)) 459 return 0; 460 461 /* This check is only valid for the PCI default MSI domain */ 462 if (dev_is_pci(dev) && domid == MSI_DEFAULT_DOMAIN) 463 pcimsi = to_pci_dev(dev)->msi_enabled; 464 465 msi_lock_descs(dev); 466 xa = &dev->msi.data->__domains[domid].store; 467 desc = xa_load(xa, pcimsi ? 0 : index); 468 if (desc && desc->irq) { 469 /* 470 * PCI-MSI has only one descriptor for multiple interrupts. 471 * PCI-MSIX and platform MSI use a descriptor per 472 * interrupt. 473 */ 474 if (pcimsi) { 475 if (index < desc->nvec_used) 476 ret = desc->irq + index; 477 } else { 478 ret = desc->irq; 479 } 480 } 481 482 msi_unlock_descs(dev); 483 return ret; 484 } 485 EXPORT_SYMBOL_GPL(msi_domain_get_virq); 486 487 #ifdef CONFIG_SYSFS 488 static struct attribute *msi_dev_attrs[] = { 489 NULL 490 }; 491 492 static const struct attribute_group msi_irqs_group = { 493 .name = "msi_irqs", 494 .attrs = msi_dev_attrs, 495 }; 496 497 static inline int msi_sysfs_create_group(struct device *dev) 498 { 499 return devm_device_add_group(dev, &msi_irqs_group); 500 } 501 502 static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr, 503 char *buf) 504 { 505 /* MSI vs. MSIX is per device not per interrupt */ 506 bool is_msix = dev_is_pci(dev) ? to_pci_dev(dev)->msix_enabled : false; 507 508 return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi"); 509 } 510 511 static void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc) 512 { 513 struct device_attribute *attrs = desc->sysfs_attrs; 514 int i; 515 516 if (!attrs) 517 return; 518 519 desc->sysfs_attrs = NULL; 520 for (i = 0; i < desc->nvec_used; i++) { 521 if (attrs[i].show) 522 sysfs_remove_file_from_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name); 523 kfree(attrs[i].attr.name); 524 } 525 kfree(attrs); 526 } 527 528 static int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc) 529 { 530 struct device_attribute *attrs; 531 int ret, i; 532 533 attrs = kcalloc(desc->nvec_used, sizeof(*attrs), GFP_KERNEL); 534 if (!attrs) 535 return -ENOMEM; 536 537 desc->sysfs_attrs = attrs; 538 for (i = 0; i < desc->nvec_used; i++) { 539 sysfs_attr_init(&attrs[i].attr); 540 attrs[i].attr.name = kasprintf(GFP_KERNEL, "%d", desc->irq + i); 541 if (!attrs[i].attr.name) { 542 ret = -ENOMEM; 543 goto fail; 544 } 545 546 attrs[i].attr.mode = 0444; 547 attrs[i].show = msi_mode_show; 548 549 ret = sysfs_add_file_to_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name); 550 if (ret) { 551 attrs[i].show = NULL; 552 goto fail; 553 } 554 } 555 return 0; 556 557 fail: 558 msi_sysfs_remove_desc(dev, desc); 559 return ret; 560 } 561 562 #if defined(CONFIG_PCI_MSI_ARCH_FALLBACKS) || defined(CONFIG_PCI_XEN) 563 /** 564 * msi_device_populate_sysfs - Populate msi_irqs sysfs entries for a device 565 * @dev: The device (PCI, platform etc) which will get sysfs entries 566 */ 567 int msi_device_populate_sysfs(struct device *dev) 568 { 569 struct msi_desc *desc; 570 int ret; 571 572 msi_for_each_desc(desc, dev, MSI_DESC_ASSOCIATED) { 573 if (desc->sysfs_attrs) 574 continue; 575 ret = msi_sysfs_populate_desc(dev, desc); 576 if (ret) 577 return ret; 578 } 579 return 0; 580 } 581 582 /** 583 * msi_device_destroy_sysfs - Destroy msi_irqs sysfs entries for a device 584 * @dev: The device (PCI, platform etc) for which to remove 585 * sysfs entries 586 */ 587 void msi_device_destroy_sysfs(struct device *dev) 588 { 589 struct msi_desc *desc; 590 591 msi_for_each_desc(desc, dev, MSI_DESC_ALL) 592 msi_sysfs_remove_desc(dev, desc); 593 } 594 #endif /* CONFIG_PCI_MSI_ARCH_FALLBACK || CONFIG_PCI_XEN */ 595 #else /* CONFIG_SYSFS */ 596 static inline int msi_sysfs_create_group(struct device *dev) { return 0; } 597 static inline int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc) { return 0; } 598 static inline void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc) { } 599 #endif /* !CONFIG_SYSFS */ 600 601 static struct irq_domain *msi_get_device_domain(struct device *dev, unsigned int domid) 602 { 603 struct irq_domain *domain; 604 605 lockdep_assert_held(&dev->msi.data->mutex); 606 607 if (WARN_ON_ONCE(domid >= MSI_MAX_DEVICE_IRQDOMAINS)) 608 return NULL; 609 610 domain = dev->msi.data->__domains[domid].domain; 611 if (!domain) 612 return NULL; 613 614 if (WARN_ON_ONCE(irq_domain_is_msi_parent(domain))) 615 return NULL; 616 617 return domain; 618 } 619 620 static unsigned int msi_domain_get_hwsize(struct device *dev, unsigned int domid) 621 { 622 struct msi_domain_info *info; 623 struct irq_domain *domain; 624 625 domain = msi_get_device_domain(dev, domid); 626 if (domain) { 627 info = domain->host_data; 628 return info->hwsize; 629 } 630 /* No domain, default to MSI_XA_DOMAIN_SIZE */ 631 return MSI_XA_DOMAIN_SIZE; 632 } 633 634 static inline void irq_chip_write_msi_msg(struct irq_data *data, 635 struct msi_msg *msg) 636 { 637 data->chip->irq_write_msi_msg(data, msg); 638 } 639 640 static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg) 641 { 642 struct msi_domain_info *info = domain->host_data; 643 644 /* 645 * If the MSI provider has messed with the second message and 646 * not advertized that it is level-capable, signal the breakage. 647 */ 648 WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) && 649 (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) && 650 (msg[1].address_lo || msg[1].address_hi || msg[1].data)); 651 } 652 653 /** 654 * msi_domain_set_affinity - Generic affinity setter function for MSI domains 655 * @irq_data: The irq data associated to the interrupt 656 * @mask: The affinity mask to set 657 * @force: Flag to enforce setting (disable online checks) 658 * 659 * Intended to be used by MSI interrupt controllers which are 660 * implemented with hierarchical domains. 661 * 662 * Return: IRQ_SET_MASK_* result code 663 */ 664 int msi_domain_set_affinity(struct irq_data *irq_data, 665 const struct cpumask *mask, bool force) 666 { 667 struct irq_data *parent = irq_data->parent_data; 668 struct msi_msg msg[2] = { [1] = { }, }; 669 int ret; 670 671 ret = parent->chip->irq_set_affinity(parent, mask, force); 672 if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) { 673 BUG_ON(irq_chip_compose_msi_msg(irq_data, msg)); 674 msi_check_level(irq_data->domain, msg); 675 irq_chip_write_msi_msg(irq_data, msg); 676 } 677 678 return ret; 679 } 680 681 static int msi_domain_activate(struct irq_domain *domain, 682 struct irq_data *irq_data, bool early) 683 { 684 struct msi_msg msg[2] = { [1] = { }, }; 685 686 BUG_ON(irq_chip_compose_msi_msg(irq_data, msg)); 687 msi_check_level(irq_data->domain, msg); 688 irq_chip_write_msi_msg(irq_data, msg); 689 return 0; 690 } 691 692 static void msi_domain_deactivate(struct irq_domain *domain, 693 struct irq_data *irq_data) 694 { 695 struct msi_msg msg[2]; 696 697 memset(msg, 0, sizeof(msg)); 698 irq_chip_write_msi_msg(irq_data, msg); 699 } 700 701 static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq, 702 unsigned int nr_irqs, void *arg) 703 { 704 struct msi_domain_info *info = domain->host_data; 705 struct msi_domain_ops *ops = info->ops; 706 irq_hw_number_t hwirq = ops->get_hwirq(info, arg); 707 int i, ret; 708 709 if (irq_find_mapping(domain, hwirq) > 0) 710 return -EEXIST; 711 712 if (domain->parent) { 713 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg); 714 if (ret < 0) 715 return ret; 716 } 717 718 for (i = 0; i < nr_irqs; i++) { 719 ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg); 720 if (ret < 0) { 721 if (ops->msi_free) { 722 for (i--; i >= 0; i--) 723 ops->msi_free(domain, info, virq + i); 724 } 725 irq_domain_free_irqs_top(domain, virq, nr_irqs); 726 return ret; 727 } 728 } 729 730 return 0; 731 } 732 733 static void msi_domain_free(struct irq_domain *domain, unsigned int virq, 734 unsigned int nr_irqs) 735 { 736 struct msi_domain_info *info = domain->host_data; 737 int i; 738 739 if (info->ops->msi_free) { 740 for (i = 0; i < nr_irqs; i++) 741 info->ops->msi_free(domain, info, virq + i); 742 } 743 irq_domain_free_irqs_top(domain, virq, nr_irqs); 744 } 745 746 static int msi_domain_translate(struct irq_domain *domain, struct irq_fwspec *fwspec, 747 irq_hw_number_t *hwirq, unsigned int *type) 748 { 749 struct msi_domain_info *info = domain->host_data; 750 751 /* 752 * This will catch allocations through the regular irqdomain path except 753 * for MSI domains which really support this, e.g. MBIGEN. 754 */ 755 if (!info->ops->msi_translate) 756 return -ENOTSUPP; 757 return info->ops->msi_translate(domain, fwspec, hwirq, type); 758 } 759 760 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS 761 static void msi_domain_debug_show(struct seq_file *m, struct irq_domain *d, 762 struct irq_data *irqd, int ind) 763 { 764 struct msi_desc *desc = irq_data_get_msi_desc(irqd); 765 766 if (!desc) 767 return; 768 769 seq_printf(m, "\n%*saddress_hi: 0x%08x", ind + 1, "", desc->msg.address_hi); 770 seq_printf(m, "\n%*saddress_lo: 0x%08x", ind + 1, "", desc->msg.address_lo); 771 seq_printf(m, "\n%*smsg_data: 0x%08x\n", ind + 1, "", desc->msg.data); 772 } 773 #endif 774 775 static const struct irq_domain_ops msi_domain_ops = { 776 .alloc = msi_domain_alloc, 777 .free = msi_domain_free, 778 .activate = msi_domain_activate, 779 .deactivate = msi_domain_deactivate, 780 .translate = msi_domain_translate, 781 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS 782 .debug_show = msi_domain_debug_show, 783 #endif 784 }; 785 786 static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info, 787 msi_alloc_info_t *arg) 788 { 789 return arg->hwirq; 790 } 791 792 static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev, 793 int nvec, msi_alloc_info_t *arg) 794 { 795 memset(arg, 0, sizeof(*arg)); 796 return 0; 797 } 798 799 static void msi_domain_ops_set_desc(msi_alloc_info_t *arg, 800 struct msi_desc *desc) 801 { 802 arg->desc = desc; 803 } 804 805 static int msi_domain_ops_init(struct irq_domain *domain, 806 struct msi_domain_info *info, 807 unsigned int virq, irq_hw_number_t hwirq, 808 msi_alloc_info_t *arg) 809 { 810 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip, 811 info->chip_data); 812 if (info->handler && info->handler_name) { 813 __irq_set_handler(virq, info->handler, 0, info->handler_name); 814 if (info->handler_data) 815 irq_set_handler_data(virq, info->handler_data); 816 } 817 return 0; 818 } 819 820 static struct msi_domain_ops msi_domain_ops_default = { 821 .get_hwirq = msi_domain_ops_get_hwirq, 822 .msi_init = msi_domain_ops_init, 823 .msi_prepare = msi_domain_ops_prepare, 824 .set_desc = msi_domain_ops_set_desc, 825 }; 826 827 static void msi_domain_update_dom_ops(struct msi_domain_info *info) 828 { 829 struct msi_domain_ops *ops = info->ops; 830 831 if (ops == NULL) { 832 info->ops = &msi_domain_ops_default; 833 return; 834 } 835 836 if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS)) 837 return; 838 839 if (ops->get_hwirq == NULL) 840 ops->get_hwirq = msi_domain_ops_default.get_hwirq; 841 if (ops->msi_init == NULL) 842 ops->msi_init = msi_domain_ops_default.msi_init; 843 if (ops->msi_prepare == NULL) 844 ops->msi_prepare = msi_domain_ops_default.msi_prepare; 845 if (ops->set_desc == NULL) 846 ops->set_desc = msi_domain_ops_default.set_desc; 847 } 848 849 static void msi_domain_update_chip_ops(struct msi_domain_info *info) 850 { 851 struct irq_chip *chip = info->chip; 852 853 BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask); 854 if (!chip->irq_set_affinity && !(info->flags & MSI_FLAG_NO_AFFINITY)) 855 chip->irq_set_affinity = msi_domain_set_affinity; 856 } 857 858 static struct irq_domain *__msi_create_irq_domain(struct fwnode_handle *fwnode, 859 struct msi_domain_info *info, 860 unsigned int flags, 861 struct irq_domain *parent) 862 { 863 struct irq_domain *domain; 864 865 if (info->hwsize > MSI_XA_DOMAIN_SIZE) 866 return NULL; 867 868 /* 869 * Hardware size 0 is valid for backwards compatibility and for 870 * domains which are not backed by a hardware table. Grant the 871 * maximum index space. 872 */ 873 if (!info->hwsize) 874 info->hwsize = MSI_XA_DOMAIN_SIZE; 875 876 msi_domain_update_dom_ops(info); 877 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) 878 msi_domain_update_chip_ops(info); 879 880 domain = irq_domain_create_hierarchy(parent, flags | IRQ_DOMAIN_FLAG_MSI, 0, 881 fwnode, &msi_domain_ops, info); 882 883 if (domain) { 884 irq_domain_update_bus_token(domain, info->bus_token); 885 if (info->flags & MSI_FLAG_PARENT_PM_DEV) 886 domain->pm_dev = parent->pm_dev; 887 } 888 889 return domain; 890 } 891 892 /** 893 * msi_create_irq_domain - Create an MSI interrupt domain 894 * @fwnode: Optional fwnode of the interrupt controller 895 * @info: MSI domain info 896 * @parent: Parent irq domain 897 * 898 * Return: pointer to the created &struct irq_domain or %NULL on failure 899 */ 900 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode, 901 struct msi_domain_info *info, 902 struct irq_domain *parent) 903 { 904 return __msi_create_irq_domain(fwnode, info, 0, parent); 905 } 906 907 /** 908 * msi_parent_init_dev_msi_info - Delegate initialization of device MSI info down 909 * in the domain hierarchy 910 * @dev: The device for which the domain should be created 911 * @domain: The domain in the hierarchy this op is being called on 912 * @msi_parent_domain: The IRQ_DOMAIN_FLAG_MSI_PARENT domain for the child to 913 * be created 914 * @msi_child_info: The MSI domain info of the IRQ_DOMAIN_FLAG_MSI_DEVICE 915 * domain to be created 916 * 917 * Return: true on success, false otherwise 918 * 919 * This is the most complex problem of per device MSI domains and the 920 * underlying interrupt domain hierarchy: 921 * 922 * The device domain to be initialized requests the broadest feature set 923 * possible and the underlying domain hierarchy puts restrictions on it. 924 * 925 * That's trivial for a simple parent->child relationship, but it gets 926 * interesting with an intermediate domain: root->parent->child. The 927 * intermediate 'parent' can expand the capabilities which the 'root' 928 * domain is providing. So that creates a classic hen and egg problem: 929 * Which entity is doing the restrictions/expansions? 930 * 931 * One solution is to let the root domain handle the initialization that's 932 * why there is the @domain and the @msi_parent_domain pointer. 933 */ 934 bool msi_parent_init_dev_msi_info(struct device *dev, struct irq_domain *domain, 935 struct irq_domain *msi_parent_domain, 936 struct msi_domain_info *msi_child_info) 937 { 938 struct irq_domain *parent = domain->parent; 939 940 if (WARN_ON_ONCE(!parent || !parent->msi_parent_ops || 941 !parent->msi_parent_ops->init_dev_msi_info)) 942 return false; 943 944 return parent->msi_parent_ops->init_dev_msi_info(dev, parent, msi_parent_domain, 945 msi_child_info); 946 } 947 948 /** 949 * msi_create_device_irq_domain - Create a device MSI interrupt domain 950 * @dev: Pointer to the device 951 * @domid: Domain id 952 * @template: MSI domain info bundle used as template 953 * @hwsize: Maximum number of MSI table entries (0 if unknown or unlimited) 954 * @domain_data: Optional pointer to domain specific data which is set in 955 * msi_domain_info::data 956 * @chip_data: Optional pointer to chip specific data which is set in 957 * msi_domain_info::chip_data 958 * 959 * Return: True on success, false otherwise 960 * 961 * There is no firmware node required for this interface because the per 962 * device domains are software constructs which are actually closer to the 963 * hardware reality than any firmware can describe them. 964 * 965 * The domain name and the irq chip name for a MSI device domain are 966 * composed by: "$(PREFIX)$(CHIPNAME)-$(DEVNAME)" 967 * 968 * $PREFIX: Optional prefix provided by the underlying MSI parent domain 969 * via msi_parent_ops::prefix. If that pointer is NULL the prefix 970 * is empty. 971 * $CHIPNAME: The name of the irq_chip in @template 972 * $DEVNAME: The name of the device 973 * 974 * This results in understandable chip names and hardware interrupt numbers 975 * in e.g. /proc/interrupts 976 * 977 * PCI-MSI-0000:00:1c.0 0-edge Parent domain has no prefix 978 * IR-PCI-MSI-0000:00:1c.4 0-edge Same with interrupt remapping prefix 'IR-' 979 * 980 * IR-PCI-MSIX-0000:3d:00.0 0-edge Hardware interrupt numbers reflect 981 * IR-PCI-MSIX-0000:3d:00.0 1-edge the real MSI-X index on that device 982 * IR-PCI-MSIX-0000:3d:00.0 2-edge 983 * 984 * On IMS domains the hardware interrupt number is either a table entry 985 * index or a purely software managed index but it is guaranteed to be 986 * unique. 987 * 988 * The domain pointer is stored in @dev::msi::data::__irqdomains[]. All 989 * subsequent operations on the domain depend on the domain id. 990 * 991 * The domain is automatically freed when the device is removed via devres 992 * in the context of @dev::msi::data freeing, but it can also be 993 * independently removed via @msi_remove_device_irq_domain(). 994 */ 995 bool msi_create_device_irq_domain(struct device *dev, unsigned int domid, 996 const struct msi_domain_template *template, 997 unsigned int hwsize, void *domain_data, 998 void *chip_data) 999 { 1000 struct irq_domain *domain, *parent = dev->msi.domain; 1001 struct fwnode_handle *fwnode, *fwnalloced = NULL; 1002 struct msi_domain_template *bundle; 1003 const struct msi_parent_ops *pops; 1004 1005 if (!irq_domain_is_msi_parent(parent)) 1006 return false; 1007 1008 if (domid >= MSI_MAX_DEVICE_IRQDOMAINS) 1009 return false; 1010 1011 bundle = kmemdup(template, sizeof(*bundle), GFP_KERNEL); 1012 if (!bundle) 1013 return false; 1014 1015 bundle->info.hwsize = hwsize; 1016 bundle->info.chip = &bundle->chip; 1017 bundle->info.ops = &bundle->ops; 1018 bundle->info.data = domain_data; 1019 bundle->info.chip_data = chip_data; 1020 1021 pops = parent->msi_parent_ops; 1022 snprintf(bundle->name, sizeof(bundle->name), "%s%s-%s", 1023 pops->prefix ? : "", bundle->chip.name, dev_name(dev)); 1024 bundle->chip.name = bundle->name; 1025 1026 /* 1027 * Using the device firmware node is required for wire to MSI 1028 * device domains so that the existing firmware results in a domain 1029 * match. 1030 * All other device domains like PCI/MSI use the named firmware 1031 * node as they are not guaranteed to have a fwnode. They are never 1032 * looked up and always handled in the context of the device. 1033 */ 1034 if (bundle->info.flags & MSI_FLAG_USE_DEV_FWNODE) 1035 fwnode = dev->fwnode; 1036 else 1037 fwnode = fwnalloced = irq_domain_alloc_named_fwnode(bundle->name); 1038 1039 if (!fwnode) 1040 goto free_bundle; 1041 1042 if (msi_setup_device_data(dev)) 1043 goto free_fwnode; 1044 1045 msi_lock_descs(dev); 1046 1047 if (WARN_ON_ONCE(msi_get_device_domain(dev, domid))) 1048 goto fail; 1049 1050 if (!pops->init_dev_msi_info(dev, parent, parent, &bundle->info)) 1051 goto fail; 1052 1053 domain = __msi_create_irq_domain(fwnode, &bundle->info, IRQ_DOMAIN_FLAG_MSI_DEVICE, parent); 1054 if (!domain) 1055 goto fail; 1056 1057 domain->dev = dev; 1058 dev->msi.data->__domains[domid].domain = domain; 1059 msi_unlock_descs(dev); 1060 return true; 1061 1062 fail: 1063 msi_unlock_descs(dev); 1064 free_fwnode: 1065 irq_domain_free_fwnode(fwnalloced); 1066 free_bundle: 1067 kfree(bundle); 1068 return false; 1069 } 1070 1071 /** 1072 * msi_remove_device_irq_domain - Free a device MSI interrupt domain 1073 * @dev: Pointer to the device 1074 * @domid: Domain id 1075 */ 1076 void msi_remove_device_irq_domain(struct device *dev, unsigned int domid) 1077 { 1078 struct fwnode_handle *fwnode = NULL; 1079 struct msi_domain_info *info; 1080 struct irq_domain *domain; 1081 1082 msi_lock_descs(dev); 1083 1084 domain = msi_get_device_domain(dev, domid); 1085 1086 if (!domain || !irq_domain_is_msi_device(domain)) 1087 goto unlock; 1088 1089 dev->msi.data->__domains[domid].domain = NULL; 1090 info = domain->host_data; 1091 if (irq_domain_is_msi_device(domain)) 1092 fwnode = domain->fwnode; 1093 irq_domain_remove(domain); 1094 irq_domain_free_fwnode(fwnode); 1095 kfree(container_of(info, struct msi_domain_template, info)); 1096 1097 unlock: 1098 msi_unlock_descs(dev); 1099 } 1100 1101 /** 1102 * msi_match_device_irq_domain - Match a device irq domain against a bus token 1103 * @dev: Pointer to the device 1104 * @domid: Domain id 1105 * @bus_token: Bus token to match against the domain bus token 1106 * 1107 * Return: True if device domain exists and bus tokens match. 1108 */ 1109 bool msi_match_device_irq_domain(struct device *dev, unsigned int domid, 1110 enum irq_domain_bus_token bus_token) 1111 { 1112 struct msi_domain_info *info; 1113 struct irq_domain *domain; 1114 bool ret = false; 1115 1116 msi_lock_descs(dev); 1117 domain = msi_get_device_domain(dev, domid); 1118 if (domain && irq_domain_is_msi_device(domain)) { 1119 info = domain->host_data; 1120 ret = info->bus_token == bus_token; 1121 } 1122 msi_unlock_descs(dev); 1123 return ret; 1124 } 1125 1126 static int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev, 1127 int nvec, msi_alloc_info_t *arg) 1128 { 1129 struct msi_domain_info *info = domain->host_data; 1130 struct msi_domain_ops *ops = info->ops; 1131 1132 return ops->msi_prepare(domain, dev, nvec, arg); 1133 } 1134 1135 /* 1136 * Carefully check whether the device can use reservation mode. If 1137 * reservation mode is enabled then the early activation will assign a 1138 * dummy vector to the device. If the PCI/MSI device does not support 1139 * masking of the entry then this can result in spurious interrupts when 1140 * the device driver is not absolutely careful. But even then a malfunction 1141 * of the hardware could result in a spurious interrupt on the dummy vector 1142 * and render the device unusable. If the entry can be masked then the core 1143 * logic will prevent the spurious interrupt and reservation mode can be 1144 * used. For now reservation mode is restricted to PCI/MSI. 1145 */ 1146 static bool msi_check_reservation_mode(struct irq_domain *domain, 1147 struct msi_domain_info *info, 1148 struct device *dev) 1149 { 1150 struct msi_desc *desc; 1151 1152 switch(domain->bus_token) { 1153 case DOMAIN_BUS_PCI_MSI: 1154 case DOMAIN_BUS_PCI_DEVICE_MSI: 1155 case DOMAIN_BUS_PCI_DEVICE_MSIX: 1156 case DOMAIN_BUS_VMD_MSI: 1157 break; 1158 default: 1159 return false; 1160 } 1161 1162 if (!(info->flags & MSI_FLAG_MUST_REACTIVATE)) 1163 return false; 1164 1165 if (info->flags & MSI_FLAG_NO_MASK) 1166 return false; 1167 1168 /* 1169 * Checking the first MSI descriptor is sufficient. MSIX supports 1170 * masking and MSI does so when the can_mask attribute is set. 1171 */ 1172 desc = msi_first_desc(dev, MSI_DESC_ALL); 1173 return desc->pci.msi_attrib.is_msix || desc->pci.msi_attrib.can_mask; 1174 } 1175 1176 static int msi_handle_pci_fail(struct irq_domain *domain, struct msi_desc *desc, 1177 int allocated) 1178 { 1179 switch(domain->bus_token) { 1180 case DOMAIN_BUS_PCI_MSI: 1181 case DOMAIN_BUS_PCI_DEVICE_MSI: 1182 case DOMAIN_BUS_PCI_DEVICE_MSIX: 1183 case DOMAIN_BUS_VMD_MSI: 1184 if (IS_ENABLED(CONFIG_PCI_MSI)) 1185 break; 1186 fallthrough; 1187 default: 1188 return -ENOSPC; 1189 } 1190 1191 /* Let a failed PCI multi MSI allocation retry */ 1192 if (desc->nvec_used > 1) 1193 return 1; 1194 1195 /* If there was a successful allocation let the caller know */ 1196 return allocated ? allocated : -ENOSPC; 1197 } 1198 1199 #define VIRQ_CAN_RESERVE 0x01 1200 #define VIRQ_ACTIVATE 0x02 1201 1202 static int msi_init_virq(struct irq_domain *domain, int virq, unsigned int vflags) 1203 { 1204 struct irq_data *irqd = irq_domain_get_irq_data(domain, virq); 1205 int ret; 1206 1207 if (!(vflags & VIRQ_CAN_RESERVE)) { 1208 irqd_clr_can_reserve(irqd); 1209 1210 /* 1211 * If the interrupt is managed but no CPU is available to 1212 * service it, shut it down until better times. Note that 1213 * we only do this on the !RESERVE path as x86 (the only 1214 * architecture using this flag) deals with this in a 1215 * different way by using a catch-all vector. 1216 */ 1217 if ((vflags & VIRQ_ACTIVATE) && 1218 irqd_affinity_is_managed(irqd) && 1219 !cpumask_intersects(irq_data_get_affinity_mask(irqd), 1220 cpu_online_mask)) { 1221 irqd_set_managed_shutdown(irqd); 1222 return 0; 1223 } 1224 } 1225 1226 if (!(vflags & VIRQ_ACTIVATE)) 1227 return 0; 1228 1229 ret = irq_domain_activate_irq(irqd, vflags & VIRQ_CAN_RESERVE); 1230 if (ret) 1231 return ret; 1232 /* 1233 * If the interrupt uses reservation mode, clear the activated bit 1234 * so request_irq() will assign the final vector. 1235 */ 1236 if (vflags & VIRQ_CAN_RESERVE) 1237 irqd_clr_activated(irqd); 1238 return 0; 1239 } 1240 1241 static int __msi_domain_alloc_irqs(struct device *dev, struct irq_domain *domain, 1242 struct msi_ctrl *ctrl) 1243 { 1244 struct xarray *xa = &dev->msi.data->__domains[ctrl->domid].store; 1245 struct msi_domain_info *info = domain->host_data; 1246 struct msi_domain_ops *ops = info->ops; 1247 unsigned int vflags = 0, allocated = 0; 1248 msi_alloc_info_t arg = { }; 1249 struct msi_desc *desc; 1250 unsigned long idx; 1251 int i, ret, virq; 1252 1253 ret = msi_domain_prepare_irqs(domain, dev, ctrl->nirqs, &arg); 1254 if (ret) 1255 return ret; 1256 1257 /* 1258 * This flag is set by the PCI layer as we need to activate 1259 * the MSI entries before the PCI layer enables MSI in the 1260 * card. Otherwise the card latches a random msi message. 1261 */ 1262 if (info->flags & MSI_FLAG_ACTIVATE_EARLY) 1263 vflags |= VIRQ_ACTIVATE; 1264 1265 /* 1266 * Interrupt can use a reserved vector and will not occupy 1267 * a real device vector until the interrupt is requested. 1268 */ 1269 if (msi_check_reservation_mode(domain, info, dev)) 1270 vflags |= VIRQ_CAN_RESERVE; 1271 1272 xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) { 1273 if (!msi_desc_match(desc, MSI_DESC_NOTASSOCIATED)) 1274 continue; 1275 1276 /* This should return -ECONFUSED... */ 1277 if (WARN_ON_ONCE(allocated >= ctrl->nirqs)) 1278 return -EINVAL; 1279 1280 if (ops->prepare_desc) 1281 ops->prepare_desc(domain, &arg, desc); 1282 1283 ops->set_desc(&arg, desc); 1284 1285 virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used, 1286 dev_to_node(dev), &arg, false, 1287 desc->affinity); 1288 if (virq < 0) 1289 return msi_handle_pci_fail(domain, desc, allocated); 1290 1291 for (i = 0; i < desc->nvec_used; i++) { 1292 irq_set_msi_desc_off(virq, i, desc); 1293 irq_debugfs_copy_devname(virq + i, dev); 1294 ret = msi_init_virq(domain, virq + i, vflags); 1295 if (ret) 1296 return ret; 1297 } 1298 if (info->flags & MSI_FLAG_DEV_SYSFS) { 1299 ret = msi_sysfs_populate_desc(dev, desc); 1300 if (ret) 1301 return ret; 1302 } 1303 allocated++; 1304 } 1305 return 0; 1306 } 1307 1308 static int msi_domain_alloc_simple_msi_descs(struct device *dev, 1309 struct msi_domain_info *info, 1310 struct msi_ctrl *ctrl) 1311 { 1312 if (!(info->flags & MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS)) 1313 return 0; 1314 1315 return msi_domain_add_simple_msi_descs(dev, ctrl); 1316 } 1317 1318 static int __msi_domain_alloc_locked(struct device *dev, struct msi_ctrl *ctrl) 1319 { 1320 struct msi_domain_info *info; 1321 struct msi_domain_ops *ops; 1322 struct irq_domain *domain; 1323 int ret; 1324 1325 if (!msi_ctrl_valid(dev, ctrl)) 1326 return -EINVAL; 1327 1328 domain = msi_get_device_domain(dev, ctrl->domid); 1329 if (!domain) 1330 return -ENODEV; 1331 1332 info = domain->host_data; 1333 1334 ret = msi_domain_alloc_simple_msi_descs(dev, info, ctrl); 1335 if (ret) 1336 return ret; 1337 1338 ops = info->ops; 1339 if (ops->domain_alloc_irqs) 1340 return ops->domain_alloc_irqs(domain, dev, ctrl->nirqs); 1341 1342 return __msi_domain_alloc_irqs(dev, domain, ctrl); 1343 } 1344 1345 static int msi_domain_alloc_locked(struct device *dev, struct msi_ctrl *ctrl) 1346 { 1347 int ret = __msi_domain_alloc_locked(dev, ctrl); 1348 1349 if (ret) 1350 msi_domain_free_locked(dev, ctrl); 1351 return ret; 1352 } 1353 1354 /** 1355 * msi_domain_alloc_irqs_range_locked - Allocate interrupts from a MSI interrupt domain 1356 * @dev: Pointer to device struct of the device for which the interrupts 1357 * are allocated 1358 * @domid: Id of the interrupt domain to operate on 1359 * @first: First index to allocate (inclusive) 1360 * @last: Last index to allocate (inclusive) 1361 * 1362 * Must be invoked from within a msi_lock_descs() / msi_unlock_descs() 1363 * pair. Use this for MSI irqdomains which implement their own descriptor 1364 * allocation/free. 1365 * 1366 * Return: %0 on success or an error code. 1367 */ 1368 int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid, 1369 unsigned int first, unsigned int last) 1370 { 1371 struct msi_ctrl ctrl = { 1372 .domid = domid, 1373 .first = first, 1374 .last = last, 1375 .nirqs = last + 1 - first, 1376 }; 1377 1378 return msi_domain_alloc_locked(dev, &ctrl); 1379 } 1380 1381 /** 1382 * msi_domain_alloc_irqs_range - Allocate interrupts from a MSI interrupt domain 1383 * @dev: Pointer to device struct of the device for which the interrupts 1384 * are allocated 1385 * @domid: Id of the interrupt domain to operate on 1386 * @first: First index to allocate (inclusive) 1387 * @last: Last index to allocate (inclusive) 1388 * 1389 * Return: %0 on success or an error code. 1390 */ 1391 int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid, 1392 unsigned int first, unsigned int last) 1393 { 1394 int ret; 1395 1396 msi_lock_descs(dev); 1397 ret = msi_domain_alloc_irqs_range_locked(dev, domid, first, last); 1398 msi_unlock_descs(dev); 1399 return ret; 1400 } 1401 EXPORT_SYMBOL_GPL(msi_domain_alloc_irqs_range); 1402 1403 /** 1404 * msi_domain_alloc_irqs_all_locked - Allocate all interrupts from a MSI interrupt domain 1405 * 1406 * @dev: Pointer to device struct of the device for which the interrupts 1407 * are allocated 1408 * @domid: Id of the interrupt domain to operate on 1409 * @nirqs: The number of interrupts to allocate 1410 * 1411 * This function scans all MSI descriptors of the MSI domain and allocates interrupts 1412 * for all unassigned ones. That function is to be used for MSI domain usage where 1413 * the descriptor allocation is handled at the call site, e.g. PCI/MSI[X]. 1414 * 1415 * Return: %0 on success or an error code. 1416 */ 1417 int msi_domain_alloc_irqs_all_locked(struct device *dev, unsigned int domid, int nirqs) 1418 { 1419 struct msi_ctrl ctrl = { 1420 .domid = domid, 1421 .first = 0, 1422 .last = msi_domain_get_hwsize(dev, domid) - 1, 1423 .nirqs = nirqs, 1424 }; 1425 1426 return msi_domain_alloc_locked(dev, &ctrl); 1427 } 1428 1429 static struct msi_map __msi_domain_alloc_irq_at(struct device *dev, unsigned int domid, 1430 unsigned int index, 1431 const struct irq_affinity_desc *affdesc, 1432 union msi_instance_cookie *icookie) 1433 { 1434 struct msi_ctrl ctrl = { .domid = domid, .nirqs = 1, }; 1435 struct irq_domain *domain; 1436 struct msi_map map = { }; 1437 struct msi_desc *desc; 1438 int ret; 1439 1440 domain = msi_get_device_domain(dev, domid); 1441 if (!domain) { 1442 map.index = -ENODEV; 1443 return map; 1444 } 1445 1446 desc = msi_alloc_desc(dev, 1, affdesc); 1447 if (!desc) { 1448 map.index = -ENOMEM; 1449 return map; 1450 } 1451 1452 if (icookie) 1453 desc->data.icookie = *icookie; 1454 1455 ret = msi_insert_desc(dev, desc, domid, index); 1456 if (ret) { 1457 map.index = ret; 1458 return map; 1459 } 1460 1461 ctrl.first = ctrl.last = desc->msi_index; 1462 1463 ret = __msi_domain_alloc_irqs(dev, domain, &ctrl); 1464 if (ret) { 1465 map.index = ret; 1466 msi_domain_free_locked(dev, &ctrl); 1467 } else { 1468 map.index = desc->msi_index; 1469 map.virq = desc->irq; 1470 } 1471 return map; 1472 } 1473 1474 /** 1475 * msi_domain_alloc_irq_at - Allocate an interrupt from a MSI interrupt domain at 1476 * a given index - or at the next free index 1477 * 1478 * @dev: Pointer to device struct of the device for which the interrupts 1479 * are allocated 1480 * @domid: Id of the interrupt domain to operate on 1481 * @index: Index for allocation. If @index == %MSI_ANY_INDEX the allocation 1482 * uses the next free index. 1483 * @affdesc: Optional pointer to an interrupt affinity descriptor structure 1484 * @icookie: Optional pointer to a domain specific per instance cookie. If 1485 * non-NULL the content of the cookie is stored in msi_desc::data. 1486 * Must be NULL for MSI-X allocations 1487 * 1488 * This requires a MSI interrupt domain which lets the core code manage the 1489 * MSI descriptors. 1490 * 1491 * Return: struct msi_map 1492 * 1493 * On success msi_map::index contains the allocated index number and 1494 * msi_map::virq the corresponding Linux interrupt number 1495 * 1496 * On failure msi_map::index contains the error code and msi_map::virq 1497 * is %0. 1498 */ 1499 struct msi_map msi_domain_alloc_irq_at(struct device *dev, unsigned int domid, unsigned int index, 1500 const struct irq_affinity_desc *affdesc, 1501 union msi_instance_cookie *icookie) 1502 { 1503 struct msi_map map; 1504 1505 msi_lock_descs(dev); 1506 map = __msi_domain_alloc_irq_at(dev, domid, index, affdesc, icookie); 1507 msi_unlock_descs(dev); 1508 return map; 1509 } 1510 1511 /** 1512 * msi_device_domain_alloc_wired - Allocate a "wired" interrupt on @domain 1513 * @domain: The domain to allocate on 1514 * @hwirq: The hardware interrupt number to allocate for 1515 * @type: The interrupt type 1516 * 1517 * This weirdness supports wire to MSI controllers like MBIGEN. 1518 * 1519 * @hwirq is the hardware interrupt number which is handed in from 1520 * irq_create_fwspec_mapping(). As the wire to MSI domain is sparse, but 1521 * sized in firmware, the hardware interrupt number cannot be used as MSI 1522 * index. For the underlying irq chip the MSI index is irrelevant and 1523 * all it needs is the hardware interrupt number. 1524 * 1525 * To handle this the MSI index is allocated with MSI_ANY_INDEX and the 1526 * hardware interrupt number is stored along with the type information in 1527 * msi_desc::cookie so the underlying interrupt chip and domain code can 1528 * retrieve it. 1529 * 1530 * Return: The Linux interrupt number (> 0) or an error code 1531 */ 1532 int msi_device_domain_alloc_wired(struct irq_domain *domain, unsigned int hwirq, 1533 unsigned int type) 1534 { 1535 unsigned int domid = MSI_DEFAULT_DOMAIN; 1536 union msi_instance_cookie icookie = { }; 1537 struct device *dev = domain->dev; 1538 struct msi_map map = { }; 1539 1540 if (WARN_ON_ONCE(!dev || domain->bus_token != DOMAIN_BUS_WIRED_TO_MSI)) 1541 return -EINVAL; 1542 1543 icookie.value = ((u64)type << 32) | hwirq; 1544 1545 msi_lock_descs(dev); 1546 if (WARN_ON_ONCE(msi_get_device_domain(dev, domid) != domain)) 1547 map.index = -EINVAL; 1548 else 1549 map = __msi_domain_alloc_irq_at(dev, domid, MSI_ANY_INDEX, NULL, &icookie); 1550 msi_unlock_descs(dev); 1551 1552 return map.index >= 0 ? map.virq : map.index; 1553 } 1554 1555 static void __msi_domain_free_irqs(struct device *dev, struct irq_domain *domain, 1556 struct msi_ctrl *ctrl) 1557 { 1558 struct xarray *xa = &dev->msi.data->__domains[ctrl->domid].store; 1559 struct msi_domain_info *info = domain->host_data; 1560 struct irq_data *irqd; 1561 struct msi_desc *desc; 1562 unsigned long idx; 1563 int i; 1564 1565 xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) { 1566 /* Only handle MSI entries which have an interrupt associated */ 1567 if (!msi_desc_match(desc, MSI_DESC_ASSOCIATED)) 1568 continue; 1569 1570 /* Make sure all interrupts are deactivated */ 1571 for (i = 0; i < desc->nvec_used; i++) { 1572 irqd = irq_domain_get_irq_data(domain, desc->irq + i); 1573 if (irqd && irqd_is_activated(irqd)) 1574 irq_domain_deactivate_irq(irqd); 1575 } 1576 1577 irq_domain_free_irqs(desc->irq, desc->nvec_used); 1578 if (info->flags & MSI_FLAG_DEV_SYSFS) 1579 msi_sysfs_remove_desc(dev, desc); 1580 desc->irq = 0; 1581 } 1582 } 1583 1584 static void msi_domain_free_locked(struct device *dev, struct msi_ctrl *ctrl) 1585 { 1586 struct msi_domain_info *info; 1587 struct msi_domain_ops *ops; 1588 struct irq_domain *domain; 1589 1590 if (!msi_ctrl_valid(dev, ctrl)) 1591 return; 1592 1593 domain = msi_get_device_domain(dev, ctrl->domid); 1594 if (!domain) 1595 return; 1596 1597 info = domain->host_data; 1598 ops = info->ops; 1599 1600 if (ops->domain_free_irqs) 1601 ops->domain_free_irqs(domain, dev); 1602 else 1603 __msi_domain_free_irqs(dev, domain, ctrl); 1604 1605 if (ops->msi_post_free) 1606 ops->msi_post_free(domain, dev); 1607 1608 if (info->flags & MSI_FLAG_FREE_MSI_DESCS) 1609 msi_domain_free_descs(dev, ctrl); 1610 } 1611 1612 /** 1613 * msi_domain_free_irqs_range_locked - Free a range of interrupts from a MSI interrupt domain 1614 * associated to @dev with msi_lock held 1615 * @dev: Pointer to device struct of the device for which the interrupts 1616 * are freed 1617 * @domid: Id of the interrupt domain to operate on 1618 * @first: First index to free (inclusive) 1619 * @last: Last index to free (inclusive) 1620 */ 1621 void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid, 1622 unsigned int first, unsigned int last) 1623 { 1624 struct msi_ctrl ctrl = { 1625 .domid = domid, 1626 .first = first, 1627 .last = last, 1628 }; 1629 msi_domain_free_locked(dev, &ctrl); 1630 } 1631 1632 /** 1633 * msi_domain_free_irqs_range - Free a range of interrupts from a MSI interrupt domain 1634 * associated to @dev 1635 * @dev: Pointer to device struct of the device for which the interrupts 1636 * are freed 1637 * @domid: Id of the interrupt domain to operate on 1638 * @first: First index to free (inclusive) 1639 * @last: Last index to free (inclusive) 1640 */ 1641 void msi_domain_free_irqs_range(struct device *dev, unsigned int domid, 1642 unsigned int first, unsigned int last) 1643 { 1644 msi_lock_descs(dev); 1645 msi_domain_free_irqs_range_locked(dev, domid, first, last); 1646 msi_unlock_descs(dev); 1647 } 1648 EXPORT_SYMBOL_GPL(msi_domain_free_irqs_all); 1649 1650 /** 1651 * msi_domain_free_irqs_all_locked - Free all interrupts from a MSI interrupt domain 1652 * associated to a device 1653 * @dev: Pointer to device struct of the device for which the interrupts 1654 * are freed 1655 * @domid: The id of the domain to operate on 1656 * 1657 * Must be invoked from within a msi_lock_descs() / msi_unlock_descs() 1658 * pair. Use this for MSI irqdomains which implement their own vector 1659 * allocation. 1660 */ 1661 void msi_domain_free_irqs_all_locked(struct device *dev, unsigned int domid) 1662 { 1663 msi_domain_free_irqs_range_locked(dev, domid, 0, 1664 msi_domain_get_hwsize(dev, domid) - 1); 1665 } 1666 1667 /** 1668 * msi_domain_free_irqs_all - Free all interrupts from a MSI interrupt domain 1669 * associated to a device 1670 * @dev: Pointer to device struct of the device for which the interrupts 1671 * are freed 1672 * @domid: The id of the domain to operate on 1673 */ 1674 void msi_domain_free_irqs_all(struct device *dev, unsigned int domid) 1675 { 1676 msi_lock_descs(dev); 1677 msi_domain_free_irqs_all_locked(dev, domid); 1678 msi_unlock_descs(dev); 1679 } 1680 1681 /** 1682 * msi_device_domain_free_wired - Free a wired interrupt in @domain 1683 * @domain: The domain to free the interrupt on 1684 * @virq: The Linux interrupt number to free 1685 * 1686 * This is the counterpart of msi_device_domain_alloc_wired() for the 1687 * weird wired to MSI converting domains. 1688 */ 1689 void msi_device_domain_free_wired(struct irq_domain *domain, unsigned int virq) 1690 { 1691 struct msi_desc *desc = irq_get_msi_desc(virq); 1692 struct device *dev = domain->dev; 1693 1694 if (WARN_ON_ONCE(!dev || !desc || domain->bus_token != DOMAIN_BUS_WIRED_TO_MSI)) 1695 return; 1696 1697 msi_lock_descs(dev); 1698 if (!WARN_ON_ONCE(msi_get_device_domain(dev, MSI_DEFAULT_DOMAIN) != domain)) { 1699 msi_domain_free_irqs_range_locked(dev, MSI_DEFAULT_DOMAIN, desc->msi_index, 1700 desc->msi_index); 1701 } 1702 msi_unlock_descs(dev); 1703 } 1704 1705 /** 1706 * msi_get_domain_info - Get the MSI interrupt domain info for @domain 1707 * @domain: The interrupt domain to retrieve data from 1708 * 1709 * Return: the pointer to the msi_domain_info stored in @domain->host_data. 1710 */ 1711 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain) 1712 { 1713 return (struct msi_domain_info *)domain->host_data; 1714 } 1715 1716 /** 1717 * msi_device_has_isolated_msi - True if the device has isolated MSI 1718 * @dev: The device to check 1719 * 1720 * Isolated MSI means that HW modeled by an irq_domain on the path from the 1721 * initiating device to the CPU will validate that the MSI message specifies an 1722 * interrupt number that the device is authorized to trigger. This must block 1723 * devices from triggering interrupts they are not authorized to trigger. 1724 * Currently authorization means the MSI vector is one assigned to the device. 1725 * 1726 * This is interesting for securing VFIO use cases where a rouge MSI (eg created 1727 * by abusing a normal PCI MemWr DMA) must not allow the VFIO userspace to 1728 * impact outside its security domain, eg userspace triggering interrupts on 1729 * kernel drivers, a VM triggering interrupts on the hypervisor, or a VM 1730 * triggering interrupts on another VM. 1731 */ 1732 bool msi_device_has_isolated_msi(struct device *dev) 1733 { 1734 struct irq_domain *domain = dev_get_msi_domain(dev); 1735 1736 for (; domain; domain = domain->parent) 1737 if (domain->flags & IRQ_DOMAIN_FLAG_ISOLATED_MSI) 1738 return true; 1739 return arch_is_isolated_msi(); 1740 } 1741 EXPORT_SYMBOL_GPL(msi_device_has_isolated_msi); 1742