b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * PCI Message Signaled Interrupt (MSI) |
| 4 | * |
| 5 | * Copyright (C) 2003-2004 Intel |
| 6 | * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com) |
| 7 | * Copyright (C) 2016 Christoph Hellwig. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/irq.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/export.h> |
| 15 | #include <linux/ioport.h> |
| 16 | #include <linux/pci.h> |
| 17 | #include <linux/proc_fs.h> |
| 18 | #include <linux/msi.h> |
| 19 | #include <linux/smp.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/acpi_iort.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/irqdomain.h> |
| 25 | #include <linux/of_irq.h> |
| 26 | |
| 27 | #include "pci.h" |
| 28 | |
| 29 | static int pci_msi_enable = 1; |
| 30 | int pci_msi_ignore_mask; |
| 31 | |
| 32 | #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) |
| 33 | |
| 34 | #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN |
| 35 | static int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
| 36 | { |
| 37 | struct irq_domain *domain; |
| 38 | |
| 39 | domain = dev_get_msi_domain(&dev->dev); |
| 40 | if (domain && irq_domain_is_hierarchy(domain)) |
| 41 | return msi_domain_alloc_irqs(domain, &dev->dev, nvec); |
| 42 | |
| 43 | return arch_setup_msi_irqs(dev, nvec, type); |
| 44 | } |
| 45 | |
| 46 | static void pci_msi_teardown_msi_irqs(struct pci_dev *dev) |
| 47 | { |
| 48 | struct irq_domain *domain; |
| 49 | |
| 50 | domain = dev_get_msi_domain(&dev->dev); |
| 51 | if (domain && irq_domain_is_hierarchy(domain)) |
| 52 | msi_domain_free_irqs(domain, &dev->dev); |
| 53 | else |
| 54 | arch_teardown_msi_irqs(dev); |
| 55 | } |
| 56 | #else |
| 57 | #define pci_msi_setup_msi_irqs arch_setup_msi_irqs |
| 58 | #define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs |
| 59 | #endif |
| 60 | |
| 61 | /* Arch hooks */ |
| 62 | |
| 63 | int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc) |
| 64 | { |
| 65 | struct msi_controller *chip = dev->bus->msi; |
| 66 | int err; |
| 67 | |
| 68 | if (!chip || !chip->setup_irq) |
| 69 | return -EINVAL; |
| 70 | |
| 71 | err = chip->setup_irq(chip, dev, desc); |
| 72 | if (err < 0) |
| 73 | return err; |
| 74 | |
| 75 | irq_set_chip_data(desc->irq, chip); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | void __weak arch_teardown_msi_irq(unsigned int irq) |
| 81 | { |
| 82 | struct msi_controller *chip = irq_get_chip_data(irq); |
| 83 | |
| 84 | if (!chip || !chip->teardown_irq) |
| 85 | return; |
| 86 | |
| 87 | chip->teardown_irq(chip, irq); |
| 88 | } |
| 89 | |
| 90 | int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
| 91 | { |
| 92 | struct msi_controller *chip = dev->bus->msi; |
| 93 | struct msi_desc *entry; |
| 94 | int ret; |
| 95 | |
| 96 | if (chip && chip->setup_irqs) |
| 97 | return chip->setup_irqs(chip, dev, nvec, type); |
| 98 | /* |
| 99 | * If an architecture wants to support multiple MSI, it needs to |
| 100 | * override arch_setup_msi_irqs() |
| 101 | */ |
| 102 | if (type == PCI_CAP_ID_MSI && nvec > 1) |
| 103 | return 1; |
| 104 | |
| 105 | for_each_pci_msi_entry(entry, dev) { |
| 106 | ret = arch_setup_msi_irq(dev, entry); |
| 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | if (ret > 0) |
| 110 | return -ENOSPC; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * We have a default implementation available as a separate non-weak |
| 118 | * function, as it is used by the Xen x86 PCI code |
| 119 | */ |
| 120 | void default_teardown_msi_irqs(struct pci_dev *dev) |
| 121 | { |
| 122 | int i; |
| 123 | struct msi_desc *entry; |
| 124 | |
| 125 | for_each_pci_msi_entry(entry, dev) |
| 126 | if (entry->irq) |
| 127 | for (i = 0; i < entry->nvec_used; i++) |
| 128 | arch_teardown_msi_irq(entry->irq + i); |
| 129 | } |
| 130 | |
| 131 | void __weak arch_teardown_msi_irqs(struct pci_dev *dev) |
| 132 | { |
| 133 | return default_teardown_msi_irqs(dev); |
| 134 | } |
| 135 | |
| 136 | static void default_restore_msi_irq(struct pci_dev *dev, int irq) |
| 137 | { |
| 138 | struct msi_desc *entry; |
| 139 | |
| 140 | entry = NULL; |
| 141 | if (dev->msix_enabled) { |
| 142 | for_each_pci_msi_entry(entry, dev) { |
| 143 | if (irq == entry->irq) |
| 144 | break; |
| 145 | } |
| 146 | } else if (dev->msi_enabled) { |
| 147 | entry = irq_get_msi_desc(irq); |
| 148 | } |
| 149 | |
| 150 | if (entry) |
| 151 | __pci_write_msi_msg(entry, &entry->msg); |
| 152 | } |
| 153 | |
| 154 | void __weak arch_restore_msi_irqs(struct pci_dev *dev) |
| 155 | { |
| 156 | return default_restore_msi_irqs(dev); |
| 157 | } |
| 158 | |
| 159 | static inline __attribute_const__ u32 msi_mask(unsigned x) |
| 160 | { |
| 161 | /* Don't shift by >= width of type */ |
| 162 | if (x >= 5) |
| 163 | return 0xffffffff; |
| 164 | return (1 << (1 << x)) - 1; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to |
| 169 | * mask all MSI interrupts by clearing the MSI enable bit does not work |
| 170 | * reliably as devices without an INTx disable bit will then generate a |
| 171 | * level IRQ which will never be cleared. |
| 172 | */ |
| 173 | u32 __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag) |
| 174 | { |
| 175 | u32 mask_bits = desc->masked; |
| 176 | |
| 177 | if (pci_msi_ignore_mask || !desc->msi_attrib.maskbit) |
| 178 | return 0; |
| 179 | |
| 180 | mask_bits &= ~mask; |
| 181 | mask_bits |= flag; |
| 182 | pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos, |
| 183 | mask_bits); |
| 184 | |
| 185 | return mask_bits; |
| 186 | } |
| 187 | |
| 188 | static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag) |
| 189 | { |
| 190 | desc->masked = __pci_msi_desc_mask_irq(desc, mask, flag); |
| 191 | } |
| 192 | |
| 193 | static void __iomem *pci_msix_desc_addr(struct msi_desc *desc) |
| 194 | { |
| 195 | if (desc->msi_attrib.is_virtual) |
| 196 | return NULL; |
| 197 | |
| 198 | return desc->mask_base + |
| 199 | desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * This internal function does not flush PCI writes to the device. |
| 204 | * All users must ensure that they read from the device before either |
| 205 | * assuming that the device state is up to date, or returning out of this |
| 206 | * file. This saves a few milliseconds when initialising devices with lots |
| 207 | * of MSI-X interrupts. |
| 208 | */ |
| 209 | u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag) |
| 210 | { |
| 211 | u32 mask_bits = desc->masked; |
| 212 | void __iomem *desc_addr; |
| 213 | |
| 214 | if (pci_msi_ignore_mask) |
| 215 | return 0; |
| 216 | |
| 217 | desc_addr = pci_msix_desc_addr(desc); |
| 218 | if (!desc_addr) |
| 219 | return 0; |
| 220 | |
| 221 | mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT; |
| 222 | if (flag & PCI_MSIX_ENTRY_CTRL_MASKBIT) |
| 223 | mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT; |
| 224 | |
| 225 | writel(mask_bits, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL); |
| 226 | |
| 227 | return mask_bits; |
| 228 | } |
| 229 | |
| 230 | static void msix_mask_irq(struct msi_desc *desc, u32 flag) |
| 231 | { |
| 232 | desc->masked = __pci_msix_desc_mask_irq(desc, flag); |
| 233 | } |
| 234 | |
| 235 | static void msi_set_mask_bit(struct irq_data *data, u32 flag) |
| 236 | { |
| 237 | struct msi_desc *desc; |
| 238 | unsigned offset; |
| 239 | if (data) { |
| 240 | desc = irq_data_get_msi_desc(data); |
| 241 | if (desc) { |
| 242 | if (desc->msi_attrib.is_msix) { |
| 243 | msix_mask_irq(desc, flag); |
| 244 | readl(desc->mask_base); /* Flush write to device */ |
| 245 | } else { |
| 246 | offset = data->irq - desc->irq; |
| 247 | msi_mask_irq(desc, 1 << offset, flag << offset); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts |
| 255 | * @data: pointer to irqdata associated to that interrupt |
| 256 | */ |
| 257 | void pci_msi_mask_irq(struct irq_data *data) |
| 258 | { |
| 259 | msi_set_mask_bit(data, 1); |
| 260 | } |
| 261 | EXPORT_SYMBOL_GPL(pci_msi_mask_irq); |
| 262 | |
| 263 | /** |
| 264 | * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts |
| 265 | * @data: pointer to irqdata associated to that interrupt |
| 266 | */ |
| 267 | void pci_msi_unmask_irq(struct irq_data *data) |
| 268 | { |
| 269 | msi_set_mask_bit(data, 0); |
| 270 | } |
| 271 | EXPORT_SYMBOL_GPL(pci_msi_unmask_irq); |
| 272 | |
| 273 | void default_restore_msi_irqs(struct pci_dev *dev) |
| 274 | { |
| 275 | struct msi_desc *entry; |
| 276 | |
| 277 | for_each_pci_msi_entry(entry, dev) |
| 278 | default_restore_msi_irq(dev, entry->irq); |
| 279 | } |
| 280 | |
| 281 | void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg) |
| 282 | { |
| 283 | struct pci_dev *dev = msi_desc_to_pci_dev(entry); |
| 284 | |
| 285 | BUG_ON(dev->current_state != PCI_D0); |
| 286 | |
| 287 | if (entry->msi_attrib.is_msix) { |
| 288 | void __iomem *base = pci_msix_desc_addr(entry); |
| 289 | |
| 290 | if (!base) { |
| 291 | WARN_ON(1); |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR); |
| 296 | msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR); |
| 297 | msg->data = readl(base + PCI_MSIX_ENTRY_DATA); |
| 298 | } else { |
| 299 | int pos = dev->msi_cap; |
| 300 | u16 data; |
| 301 | |
| 302 | pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, |
| 303 | &msg->address_lo); |
| 304 | if (entry->msi_attrib.is_64) { |
| 305 | pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, |
| 306 | &msg->address_hi); |
| 307 | pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data); |
| 308 | } else { |
| 309 | msg->address_hi = 0; |
| 310 | pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data); |
| 311 | } |
| 312 | msg->data = data; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg) |
| 317 | { |
| 318 | struct pci_dev *dev = msi_desc_to_pci_dev(entry); |
| 319 | |
| 320 | if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) { |
| 321 | /* Don't touch the hardware now */ |
| 322 | } else if (entry->msi_attrib.is_msix) { |
| 323 | void __iomem *base = pci_msix_desc_addr(entry); |
| 324 | bool unmasked = !(entry->masked & PCI_MSIX_ENTRY_CTRL_MASKBIT); |
| 325 | |
| 326 | if (!base) |
| 327 | goto skip; |
| 328 | |
| 329 | /* |
| 330 | * The specification mandates that the entry is masked |
| 331 | * when the message is modified: |
| 332 | * |
| 333 | * "If software changes the Address or Data value of an |
| 334 | * entry while the entry is unmasked, the result is |
| 335 | * undefined." |
| 336 | */ |
| 337 | if (unmasked) |
| 338 | __pci_msix_desc_mask_irq(entry, PCI_MSIX_ENTRY_CTRL_MASKBIT); |
| 339 | |
| 340 | writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR); |
| 341 | writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR); |
| 342 | writel(msg->data, base + PCI_MSIX_ENTRY_DATA); |
| 343 | |
| 344 | if (unmasked) |
| 345 | __pci_msix_desc_mask_irq(entry, 0); |
| 346 | |
| 347 | /* Ensure that the writes are visible in the device */ |
| 348 | readl(base + PCI_MSIX_ENTRY_DATA); |
| 349 | } else { |
| 350 | int pos = dev->msi_cap; |
| 351 | u16 msgctl; |
| 352 | |
| 353 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl); |
| 354 | msgctl &= ~PCI_MSI_FLAGS_QSIZE; |
| 355 | msgctl |= entry->msi_attrib.multiple << 4; |
| 356 | pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl); |
| 357 | |
| 358 | pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, |
| 359 | msg->address_lo); |
| 360 | if (entry->msi_attrib.is_64) { |
| 361 | pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, |
| 362 | msg->address_hi); |
| 363 | pci_write_config_word(dev, pos + PCI_MSI_DATA_64, |
| 364 | msg->data); |
| 365 | } else { |
| 366 | pci_write_config_word(dev, pos + PCI_MSI_DATA_32, |
| 367 | msg->data); |
| 368 | } |
| 369 | /* Ensure that the writes are visible in the device */ |
| 370 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl); |
| 371 | } |
| 372 | |
| 373 | skip: |
| 374 | entry->msg = *msg; |
| 375 | |
| 376 | if (entry->write_msi_msg) |
| 377 | entry->write_msi_msg(entry, entry->write_msi_msg_data); |
| 378 | |
| 379 | } |
| 380 | |
| 381 | void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg) |
| 382 | { |
| 383 | struct msi_desc *entry = irq_get_msi_desc(irq); |
| 384 | |
| 385 | __pci_write_msi_msg(entry, msg); |
| 386 | } |
| 387 | EXPORT_SYMBOL_GPL(pci_write_msi_msg); |
| 388 | |
| 389 | static void free_msi_irqs(struct pci_dev *dev) |
| 390 | { |
| 391 | struct list_head *msi_list = dev_to_msi_list(&dev->dev); |
| 392 | struct msi_desc *entry, *tmp; |
| 393 | struct attribute **msi_attrs; |
| 394 | struct device_attribute *dev_attr; |
| 395 | int i, count = 0; |
| 396 | |
| 397 | for_each_pci_msi_entry(entry, dev) |
| 398 | if (entry->irq) |
| 399 | for (i = 0; i < entry->nvec_used; i++) |
| 400 | BUG_ON(irq_has_action(entry->irq + i)); |
| 401 | |
| 402 | if (dev->msi_irq_groups) { |
| 403 | sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups); |
| 404 | msi_attrs = dev->msi_irq_groups[0]->attrs; |
| 405 | while (msi_attrs[count]) { |
| 406 | dev_attr = container_of(msi_attrs[count], |
| 407 | struct device_attribute, attr); |
| 408 | kfree(dev_attr->attr.name); |
| 409 | kfree(dev_attr); |
| 410 | ++count; |
| 411 | } |
| 412 | kfree(msi_attrs); |
| 413 | kfree(dev->msi_irq_groups[0]); |
| 414 | kfree(dev->msi_irq_groups); |
| 415 | dev->msi_irq_groups = NULL; |
| 416 | } |
| 417 | |
| 418 | pci_msi_teardown_msi_irqs(dev); |
| 419 | |
| 420 | list_for_each_entry_safe(entry, tmp, msi_list, list) { |
| 421 | if (entry->msi_attrib.is_msix) { |
| 422 | if (list_is_last(&entry->list, msi_list)) |
| 423 | iounmap(entry->mask_base); |
| 424 | } |
| 425 | |
| 426 | list_del(&entry->list); |
| 427 | free_msi_entry(entry); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | static void pci_intx_for_msi(struct pci_dev *dev, int enable) |
| 432 | { |
| 433 | if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG)) |
| 434 | pci_intx(dev, enable); |
| 435 | } |
| 436 | |
| 437 | static void __pci_restore_msi_state(struct pci_dev *dev) |
| 438 | { |
| 439 | u16 control; |
| 440 | struct msi_desc *entry; |
| 441 | |
| 442 | if (!dev->msi_enabled) |
| 443 | return; |
| 444 | |
| 445 | entry = irq_get_msi_desc(dev->irq); |
| 446 | |
| 447 | pci_intx_for_msi(dev, 0); |
| 448 | pci_msi_set_enable(dev, 0); |
| 449 | arch_restore_msi_irqs(dev); |
| 450 | |
| 451 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control); |
| 452 | msi_mask_irq(entry, msi_mask(entry->msi_attrib.multi_cap), |
| 453 | entry->masked); |
| 454 | control &= ~PCI_MSI_FLAGS_QSIZE; |
| 455 | control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE; |
| 456 | pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control); |
| 457 | } |
| 458 | |
| 459 | static void __pci_restore_msix_state(struct pci_dev *dev) |
| 460 | { |
| 461 | struct msi_desc *entry; |
| 462 | |
| 463 | if (!dev->msix_enabled) |
| 464 | return; |
| 465 | BUG_ON(list_empty(dev_to_msi_list(&dev->dev))); |
| 466 | |
| 467 | /* route the table */ |
| 468 | pci_intx_for_msi(dev, 0); |
| 469 | pci_msix_clear_and_set_ctrl(dev, 0, |
| 470 | PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL); |
| 471 | |
| 472 | arch_restore_msi_irqs(dev); |
| 473 | for_each_pci_msi_entry(entry, dev) |
| 474 | msix_mask_irq(entry, entry->masked); |
| 475 | |
| 476 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0); |
| 477 | } |
| 478 | |
| 479 | void pci_restore_msi_state(struct pci_dev *dev) |
| 480 | { |
| 481 | __pci_restore_msi_state(dev); |
| 482 | __pci_restore_msix_state(dev); |
| 483 | } |
| 484 | EXPORT_SYMBOL_GPL(pci_restore_msi_state); |
| 485 | |
| 486 | static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr, |
| 487 | char *buf) |
| 488 | { |
| 489 | struct msi_desc *entry; |
| 490 | unsigned long irq; |
| 491 | int retval; |
| 492 | |
| 493 | retval = kstrtoul(attr->attr.name, 10, &irq); |
| 494 | if (retval) |
| 495 | return retval; |
| 496 | |
| 497 | entry = irq_get_msi_desc(irq); |
| 498 | if (entry) |
| 499 | return sprintf(buf, "%s\n", |
| 500 | entry->msi_attrib.is_msix ? "msix" : "msi"); |
| 501 | |
| 502 | return -ENODEV; |
| 503 | } |
| 504 | |
| 505 | static int populate_msi_sysfs(struct pci_dev *pdev) |
| 506 | { |
| 507 | struct attribute **msi_attrs; |
| 508 | struct attribute *msi_attr; |
| 509 | struct device_attribute *msi_dev_attr; |
| 510 | struct attribute_group *msi_irq_group; |
| 511 | const struct attribute_group **msi_irq_groups; |
| 512 | struct msi_desc *entry; |
| 513 | int ret = -ENOMEM; |
| 514 | int num_msi = 0; |
| 515 | int count = 0; |
| 516 | int i; |
| 517 | |
| 518 | /* Determine how many msi entries we have */ |
| 519 | for_each_pci_msi_entry(entry, pdev) |
| 520 | num_msi += entry->nvec_used; |
| 521 | if (!num_msi) |
| 522 | return 0; |
| 523 | |
| 524 | /* Dynamically create the MSI attributes for the PCI device */ |
| 525 | msi_attrs = kcalloc(num_msi + 1, sizeof(void *), GFP_KERNEL); |
| 526 | if (!msi_attrs) |
| 527 | return -ENOMEM; |
| 528 | for_each_pci_msi_entry(entry, pdev) { |
| 529 | for (i = 0; i < entry->nvec_used; i++) { |
| 530 | msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL); |
| 531 | if (!msi_dev_attr) |
| 532 | goto error_attrs; |
| 533 | msi_attrs[count] = &msi_dev_attr->attr; |
| 534 | |
| 535 | sysfs_attr_init(&msi_dev_attr->attr); |
| 536 | msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d", |
| 537 | entry->irq + i); |
| 538 | if (!msi_dev_attr->attr.name) |
| 539 | goto error_attrs; |
| 540 | msi_dev_attr->attr.mode = S_IRUGO; |
| 541 | msi_dev_attr->show = msi_mode_show; |
| 542 | ++count; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL); |
| 547 | if (!msi_irq_group) |
| 548 | goto error_attrs; |
| 549 | msi_irq_group->name = "msi_irqs"; |
| 550 | msi_irq_group->attrs = msi_attrs; |
| 551 | |
| 552 | msi_irq_groups = kcalloc(2, sizeof(void *), GFP_KERNEL); |
| 553 | if (!msi_irq_groups) |
| 554 | goto error_irq_group; |
| 555 | msi_irq_groups[0] = msi_irq_group; |
| 556 | |
| 557 | ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups); |
| 558 | if (ret) |
| 559 | goto error_irq_groups; |
| 560 | pdev->msi_irq_groups = msi_irq_groups; |
| 561 | |
| 562 | return 0; |
| 563 | |
| 564 | error_irq_groups: |
| 565 | kfree(msi_irq_groups); |
| 566 | error_irq_group: |
| 567 | kfree(msi_irq_group); |
| 568 | error_attrs: |
| 569 | count = 0; |
| 570 | msi_attr = msi_attrs[count]; |
| 571 | while (msi_attr) { |
| 572 | msi_dev_attr = container_of(msi_attr, struct device_attribute, attr); |
| 573 | kfree(msi_attr->name); |
| 574 | kfree(msi_dev_attr); |
| 575 | ++count; |
| 576 | msi_attr = msi_attrs[count]; |
| 577 | } |
| 578 | kfree(msi_attrs); |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | static struct msi_desc * |
| 583 | msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd) |
| 584 | { |
| 585 | struct irq_affinity_desc *masks = NULL; |
| 586 | struct msi_desc *entry; |
| 587 | u16 control; |
| 588 | |
| 589 | if (affd) |
| 590 | masks = irq_create_affinity_masks(nvec, affd); |
| 591 | |
| 592 | /* MSI Entry Initialization */ |
| 593 | entry = alloc_msi_entry(&dev->dev, nvec, masks); |
| 594 | if (!entry) |
| 595 | goto out; |
| 596 | |
| 597 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control); |
| 598 | /* Lies, damned lies, and MSIs */ |
| 599 | if (dev->dev_flags & PCI_DEV_FLAGS_HAS_MSI_MASKING) |
| 600 | control |= PCI_MSI_FLAGS_MASKBIT; |
| 601 | |
| 602 | entry->msi_attrib.is_msix = 0; |
| 603 | entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT); |
| 604 | entry->msi_attrib.is_virtual = 0; |
| 605 | entry->msi_attrib.entry_nr = 0; |
| 606 | entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT); |
| 607 | entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */ |
| 608 | entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1; |
| 609 | entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec)); |
| 610 | |
| 611 | if (control & PCI_MSI_FLAGS_64BIT) |
| 612 | entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64; |
| 613 | else |
| 614 | entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32; |
| 615 | |
| 616 | /* Save the initial mask status */ |
| 617 | if (entry->msi_attrib.maskbit) |
| 618 | pci_read_config_dword(dev, entry->mask_pos, &entry->masked); |
| 619 | |
| 620 | out: |
| 621 | kfree(masks); |
| 622 | return entry; |
| 623 | } |
| 624 | |
| 625 | static int msi_verify_entries(struct pci_dev *dev) |
| 626 | { |
| 627 | struct msi_desc *entry; |
| 628 | |
| 629 | for_each_pci_msi_entry(entry, dev) { |
| 630 | if (!dev->no_64bit_msi || !entry->msg.address_hi) |
| 631 | continue; |
| 632 | pci_err(dev, "Device has broken 64-bit MSI but arch" |
| 633 | " tried to assign one above 4G\n"); |
| 634 | return -EIO; |
| 635 | } |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * msi_capability_init - configure device's MSI capability structure |
| 641 | * @dev: pointer to the pci_dev data structure of MSI device function |
| 642 | * @nvec: number of interrupts to allocate |
| 643 | * @affd: description of automatic IRQ affinity assignments (may be %NULL) |
| 644 | * |
| 645 | * Setup the MSI capability structure of the device with the requested |
| 646 | * number of interrupts. A return value of zero indicates the successful |
| 647 | * setup of an entry with the new MSI IRQ. A negative return value indicates |
| 648 | * an error, and a positive return value indicates the number of interrupts |
| 649 | * which could have been allocated. |
| 650 | */ |
| 651 | static int msi_capability_init(struct pci_dev *dev, int nvec, |
| 652 | struct irq_affinity *affd) |
| 653 | { |
| 654 | struct msi_desc *entry; |
| 655 | int ret; |
| 656 | unsigned mask; |
| 657 | |
| 658 | pci_msi_set_enable(dev, 0); /* Disable MSI during set up */ |
| 659 | |
| 660 | entry = msi_setup_entry(dev, nvec, affd); |
| 661 | if (!entry) |
| 662 | return -ENOMEM; |
| 663 | |
| 664 | /* All MSIs are unmasked by default; mask them all */ |
| 665 | mask = msi_mask(entry->msi_attrib.multi_cap); |
| 666 | msi_mask_irq(entry, mask, mask); |
| 667 | |
| 668 | list_add_tail(&entry->list, dev_to_msi_list(&dev->dev)); |
| 669 | |
| 670 | /* Configure MSI capability structure */ |
| 671 | ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI); |
| 672 | if (ret) { |
| 673 | msi_mask_irq(entry, mask, 0); |
| 674 | free_msi_irqs(dev); |
| 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | ret = msi_verify_entries(dev); |
| 679 | if (ret) { |
| 680 | msi_mask_irq(entry, mask, 0); |
| 681 | free_msi_irqs(dev); |
| 682 | return ret; |
| 683 | } |
| 684 | |
| 685 | ret = populate_msi_sysfs(dev); |
| 686 | if (ret) { |
| 687 | msi_mask_irq(entry, mask, 0); |
| 688 | free_msi_irqs(dev); |
| 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | /* Set MSI enabled bits */ |
| 693 | pci_intx_for_msi(dev, 0); |
| 694 | pci_msi_set_enable(dev, 1); |
| 695 | dev->msi_enabled = 1; |
| 696 | |
| 697 | pcibios_free_irq(dev); |
| 698 | dev->irq = entry->irq; |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries) |
| 703 | { |
| 704 | resource_size_t phys_addr; |
| 705 | u32 table_offset; |
| 706 | unsigned long flags; |
| 707 | u8 bir; |
| 708 | |
| 709 | pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE, |
| 710 | &table_offset); |
| 711 | bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR); |
| 712 | flags = pci_resource_flags(dev, bir); |
| 713 | if (!flags || (flags & IORESOURCE_UNSET)) |
| 714 | return NULL; |
| 715 | |
| 716 | table_offset &= PCI_MSIX_TABLE_OFFSET; |
| 717 | phys_addr = pci_resource_start(dev, bir) + table_offset; |
| 718 | |
| 719 | return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE); |
| 720 | } |
| 721 | |
| 722 | static int msix_setup_entries(struct pci_dev *dev, void __iomem *base, |
| 723 | struct msix_entry *entries, int nvec, |
| 724 | struct irq_affinity *affd) |
| 725 | { |
| 726 | struct irq_affinity_desc *curmsk, *masks = NULL; |
| 727 | struct msi_desc *entry; |
| 728 | void __iomem *addr; |
| 729 | int ret, i; |
| 730 | int vec_count = pci_msix_vec_count(dev); |
| 731 | |
| 732 | if (affd) |
| 733 | masks = irq_create_affinity_masks(nvec, affd); |
| 734 | |
| 735 | for (i = 0, curmsk = masks; i < nvec; i++) { |
| 736 | entry = alloc_msi_entry(&dev->dev, 1, curmsk); |
| 737 | if (!entry) { |
| 738 | if (!i) |
| 739 | iounmap(base); |
| 740 | else |
| 741 | free_msi_irqs(dev); |
| 742 | /* No enough memory. Don't try again */ |
| 743 | ret = -ENOMEM; |
| 744 | goto out; |
| 745 | } |
| 746 | |
| 747 | entry->msi_attrib.is_msix = 1; |
| 748 | entry->msi_attrib.is_64 = 1; |
| 749 | |
| 750 | if (entries) |
| 751 | entry->msi_attrib.entry_nr = entries[i].entry; |
| 752 | else |
| 753 | entry->msi_attrib.entry_nr = i; |
| 754 | |
| 755 | entry->msi_attrib.is_virtual = |
| 756 | entry->msi_attrib.entry_nr >= vec_count; |
| 757 | |
| 758 | entry->msi_attrib.default_irq = dev->irq; |
| 759 | entry->mask_base = base; |
| 760 | |
| 761 | addr = pci_msix_desc_addr(entry); |
| 762 | if (addr) |
| 763 | entry->masked = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL); |
| 764 | |
| 765 | list_add_tail(&entry->list, dev_to_msi_list(&dev->dev)); |
| 766 | if (masks) |
| 767 | curmsk++; |
| 768 | } |
| 769 | ret = 0; |
| 770 | out: |
| 771 | kfree(masks); |
| 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries) |
| 776 | { |
| 777 | struct msi_desc *entry; |
| 778 | |
| 779 | for_each_pci_msi_entry(entry, dev) { |
| 780 | if (entries) { |
| 781 | entries->vector = entry->irq; |
| 782 | entries++; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | static void msix_mask_all(void __iomem *base, int tsize) |
| 788 | { |
| 789 | u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT; |
| 790 | int i; |
| 791 | |
| 792 | if (pci_msi_ignore_mask) |
| 793 | return; |
| 794 | |
| 795 | for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE) |
| 796 | writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL); |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * msix_capability_init - configure device's MSI-X capability |
| 801 | * @dev: pointer to the pci_dev data structure of MSI-X device function |
| 802 | * @entries: pointer to an array of struct msix_entry entries |
| 803 | * @nvec: number of @entries |
| 804 | * @affd: Optional pointer to enable automatic affinity assignment |
| 805 | * |
| 806 | * Setup the MSI-X capability structure of device function with a |
| 807 | * single MSI-X IRQ. A return of zero indicates the successful setup of |
| 808 | * requested MSI-X entries with allocated IRQs or non-zero for otherwise. |
| 809 | **/ |
| 810 | static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries, |
| 811 | int nvec, struct irq_affinity *affd) |
| 812 | { |
| 813 | void __iomem *base; |
| 814 | int ret, tsize; |
| 815 | u16 control; |
| 816 | |
| 817 | /* |
| 818 | * Some devices require MSI-X to be enabled before the MSI-X |
| 819 | * registers can be accessed. Mask all the vectors to prevent |
| 820 | * interrupts coming in before they're fully set up. |
| 821 | */ |
| 822 | pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL | |
| 823 | PCI_MSIX_FLAGS_ENABLE); |
| 824 | |
| 825 | pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control); |
| 826 | /* Request & Map MSI-X table region */ |
| 827 | tsize = msix_table_size(control); |
| 828 | base = msix_map_region(dev, tsize); |
| 829 | if (!base) { |
| 830 | ret = -ENOMEM; |
| 831 | goto out_disable; |
| 832 | } |
| 833 | |
| 834 | ret = msix_setup_entries(dev, base, entries, nvec, affd); |
| 835 | if (ret) |
| 836 | goto out_disable; |
| 837 | |
| 838 | ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX); |
| 839 | if (ret) |
| 840 | goto out_avail; |
| 841 | |
| 842 | /* Check if all MSI entries honor device restrictions */ |
| 843 | ret = msi_verify_entries(dev); |
| 844 | if (ret) |
| 845 | goto out_free; |
| 846 | |
| 847 | msix_update_entries(dev, entries); |
| 848 | |
| 849 | ret = populate_msi_sysfs(dev); |
| 850 | if (ret) |
| 851 | goto out_free; |
| 852 | |
| 853 | /* Set MSI-X enabled bits and unmask the function */ |
| 854 | pci_intx_for_msi(dev, 0); |
| 855 | dev->msix_enabled = 1; |
| 856 | |
| 857 | /* |
| 858 | * Ensure that all table entries are masked to prevent |
| 859 | * stale entries from firing in a crash kernel. |
| 860 | * |
| 861 | * Done late to deal with a broken Marvell NVME device |
| 862 | * which takes the MSI-X mask bits into account even |
| 863 | * when MSI-X is disabled, which prevents MSI delivery. |
| 864 | */ |
| 865 | msix_mask_all(base, tsize); |
| 866 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0); |
| 867 | |
| 868 | pcibios_free_irq(dev); |
| 869 | return 0; |
| 870 | |
| 871 | out_avail: |
| 872 | if (ret < 0) { |
| 873 | /* |
| 874 | * If we had some success, report the number of IRQs |
| 875 | * we succeeded in setting up. |
| 876 | */ |
| 877 | struct msi_desc *entry; |
| 878 | int avail = 0; |
| 879 | |
| 880 | for_each_pci_msi_entry(entry, dev) { |
| 881 | if (entry->irq != 0) |
| 882 | avail++; |
| 883 | } |
| 884 | if (avail != 0) |
| 885 | ret = avail; |
| 886 | } |
| 887 | |
| 888 | out_free: |
| 889 | free_msi_irqs(dev); |
| 890 | |
| 891 | out_disable: |
| 892 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 0); |
| 893 | |
| 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * pci_msi_supported - check whether MSI may be enabled on a device |
| 899 | * @dev: pointer to the pci_dev data structure of MSI device function |
| 900 | * @nvec: how many MSIs have been requested? |
| 901 | * |
| 902 | * Look at global flags, the device itself, and its parent buses |
| 903 | * to determine if MSI/-X are supported for the device. If MSI/-X is |
| 904 | * supported return 1, else return 0. |
| 905 | **/ |
| 906 | static int pci_msi_supported(struct pci_dev *dev, int nvec) |
| 907 | { |
| 908 | struct pci_bus *bus; |
| 909 | |
| 910 | /* MSI must be globally enabled and supported by the device */ |
| 911 | if (!pci_msi_enable) |
| 912 | return 0; |
| 913 | |
| 914 | if (!dev || dev->no_msi || dev->current_state != PCI_D0) |
| 915 | return 0; |
| 916 | |
| 917 | /* |
| 918 | * You can't ask to have 0 or less MSIs configured. |
| 919 | * a) it's stupid .. |
| 920 | * b) the list manipulation code assumes nvec >= 1. |
| 921 | */ |
| 922 | if (nvec < 1) |
| 923 | return 0; |
| 924 | |
| 925 | /* |
| 926 | * Any bridge which does NOT route MSI transactions from its |
| 927 | * secondary bus to its primary bus must set NO_MSI flag on |
| 928 | * the secondary pci_bus. |
| 929 | * We expect only arch-specific PCI host bus controller driver |
| 930 | * or quirks for specific PCI bridges to be setting NO_MSI. |
| 931 | */ |
| 932 | for (bus = dev->bus; bus; bus = bus->parent) |
| 933 | if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI) |
| 934 | return 0; |
| 935 | |
| 936 | return 1; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * pci_msi_vec_count - Return the number of MSI vectors a device can send |
| 941 | * @dev: device to report about |
| 942 | * |
| 943 | * This function returns the number of MSI vectors a device requested via |
| 944 | * Multiple Message Capable register. It returns a negative errno if the |
| 945 | * device is not capable sending MSI interrupts. Otherwise, the call succeeds |
| 946 | * and returns a power of two, up to a maximum of 2^5 (32), according to the |
| 947 | * MSI specification. |
| 948 | **/ |
| 949 | int pci_msi_vec_count(struct pci_dev *dev) |
| 950 | { |
| 951 | int ret; |
| 952 | u16 msgctl; |
| 953 | |
| 954 | if (!dev->msi_cap) |
| 955 | return -EINVAL; |
| 956 | |
| 957 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl); |
| 958 | ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1); |
| 959 | |
| 960 | return ret; |
| 961 | } |
| 962 | EXPORT_SYMBOL(pci_msi_vec_count); |
| 963 | |
| 964 | static void pci_msi_shutdown(struct pci_dev *dev) |
| 965 | { |
| 966 | struct msi_desc *desc; |
| 967 | u32 mask; |
| 968 | |
| 969 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
| 970 | return; |
| 971 | |
| 972 | BUG_ON(list_empty(dev_to_msi_list(&dev->dev))); |
| 973 | desc = first_pci_msi_entry(dev); |
| 974 | |
| 975 | pci_msi_set_enable(dev, 0); |
| 976 | pci_intx_for_msi(dev, 1); |
| 977 | dev->msi_enabled = 0; |
| 978 | |
| 979 | /* Return the device with MSI unmasked as initial states */ |
| 980 | mask = msi_mask(desc->msi_attrib.multi_cap); |
| 981 | msi_mask_irq(desc, mask, 0); |
| 982 | |
| 983 | /* Restore dev->irq to its default pin-assertion IRQ */ |
| 984 | dev->irq = desc->msi_attrib.default_irq; |
| 985 | pcibios_alloc_irq(dev); |
| 986 | } |
| 987 | |
| 988 | void pci_disable_msi(struct pci_dev *dev) |
| 989 | { |
| 990 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
| 991 | return; |
| 992 | |
| 993 | pci_msi_shutdown(dev); |
| 994 | free_msi_irqs(dev); |
| 995 | } |
| 996 | EXPORT_SYMBOL(pci_disable_msi); |
| 997 | |
| 998 | /** |
| 999 | * pci_msix_vec_count - return the number of device's MSI-X table entries |
| 1000 | * @dev: pointer to the pci_dev data structure of MSI-X device function |
| 1001 | * This function returns the number of device's MSI-X table entries and |
| 1002 | * therefore the number of MSI-X vectors device is capable of sending. |
| 1003 | * It returns a negative errno if the device is not capable of sending MSI-X |
| 1004 | * interrupts. |
| 1005 | **/ |
| 1006 | int pci_msix_vec_count(struct pci_dev *dev) |
| 1007 | { |
| 1008 | u16 control; |
| 1009 | |
| 1010 | if (!dev->msix_cap) |
| 1011 | return -EINVAL; |
| 1012 | |
| 1013 | pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control); |
| 1014 | return msix_table_size(control); |
| 1015 | } |
| 1016 | EXPORT_SYMBOL(pci_msix_vec_count); |
| 1017 | |
| 1018 | static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, |
| 1019 | int nvec, struct irq_affinity *affd, int flags) |
| 1020 | { |
| 1021 | int nr_entries; |
| 1022 | int i, j; |
| 1023 | |
| 1024 | if (!pci_msi_supported(dev, nvec)) |
| 1025 | return -EINVAL; |
| 1026 | |
| 1027 | nr_entries = pci_msix_vec_count(dev); |
| 1028 | if (nr_entries < 0) |
| 1029 | return nr_entries; |
| 1030 | if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL)) |
| 1031 | return nr_entries; |
| 1032 | |
| 1033 | if (entries) { |
| 1034 | /* Check for any invalid entries */ |
| 1035 | for (i = 0; i < nvec; i++) { |
| 1036 | if (entries[i].entry >= nr_entries) |
| 1037 | return -EINVAL; /* invalid entry */ |
| 1038 | for (j = i + 1; j < nvec; j++) { |
| 1039 | if (entries[i].entry == entries[j].entry) |
| 1040 | return -EINVAL; /* duplicate entry */ |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /* Check whether driver already requested for MSI IRQ */ |
| 1046 | if (dev->msi_enabled) { |
| 1047 | pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n"); |
| 1048 | return -EINVAL; |
| 1049 | } |
| 1050 | return msix_capability_init(dev, entries, nvec, affd); |
| 1051 | } |
| 1052 | |
| 1053 | static void pci_msix_shutdown(struct pci_dev *dev) |
| 1054 | { |
| 1055 | struct msi_desc *entry; |
| 1056 | |
| 1057 | if (!pci_msi_enable || !dev || !dev->msix_enabled) |
| 1058 | return; |
| 1059 | |
| 1060 | if (pci_dev_is_disconnected(dev)) { |
| 1061 | dev->msix_enabled = 0; |
| 1062 | return; |
| 1063 | } |
| 1064 | |
| 1065 | /* Return the device with MSI-X masked as initial states */ |
| 1066 | for_each_pci_msi_entry(entry, dev) |
| 1067 | __pci_msix_desc_mask_irq(entry, 1); |
| 1068 | |
| 1069 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0); |
| 1070 | pci_intx_for_msi(dev, 1); |
| 1071 | dev->msix_enabled = 0; |
| 1072 | pcibios_alloc_irq(dev); |
| 1073 | } |
| 1074 | |
| 1075 | void pci_disable_msix(struct pci_dev *dev) |
| 1076 | { |
| 1077 | if (!pci_msi_enable || !dev || !dev->msix_enabled) |
| 1078 | return; |
| 1079 | |
| 1080 | pci_msix_shutdown(dev); |
| 1081 | free_msi_irqs(dev); |
| 1082 | } |
| 1083 | EXPORT_SYMBOL(pci_disable_msix); |
| 1084 | |
| 1085 | void pci_no_msi(void) |
| 1086 | { |
| 1087 | pci_msi_enable = 0; |
| 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * pci_msi_enabled - is MSI enabled? |
| 1092 | * |
| 1093 | * Returns true if MSI has not been disabled by the command-line option |
| 1094 | * pci=nomsi. |
| 1095 | **/ |
| 1096 | int pci_msi_enabled(void) |
| 1097 | { |
| 1098 | return pci_msi_enable; |
| 1099 | } |
| 1100 | EXPORT_SYMBOL(pci_msi_enabled); |
| 1101 | |
| 1102 | static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec, |
| 1103 | struct irq_affinity *affd) |
| 1104 | { |
| 1105 | int nvec; |
| 1106 | int rc; |
| 1107 | |
| 1108 | if (!pci_msi_supported(dev, minvec)) |
| 1109 | return -EINVAL; |
| 1110 | |
| 1111 | /* Check whether driver already requested MSI-X IRQs */ |
| 1112 | if (dev->msix_enabled) { |
| 1113 | pci_info(dev, "can't enable MSI (MSI-X already enabled)\n"); |
| 1114 | return -EINVAL; |
| 1115 | } |
| 1116 | |
| 1117 | if (maxvec < minvec) |
| 1118 | return -ERANGE; |
| 1119 | |
| 1120 | if (WARN_ON_ONCE(dev->msi_enabled)) |
| 1121 | return -EINVAL; |
| 1122 | |
| 1123 | nvec = pci_msi_vec_count(dev); |
| 1124 | if (nvec < 0) |
| 1125 | return nvec; |
| 1126 | if (nvec < minvec) |
| 1127 | return -ENOSPC; |
| 1128 | |
| 1129 | if (nvec > maxvec) |
| 1130 | nvec = maxvec; |
| 1131 | |
| 1132 | for (;;) { |
| 1133 | if (affd) { |
| 1134 | nvec = irq_calc_affinity_vectors(minvec, nvec, affd); |
| 1135 | if (nvec < minvec) |
| 1136 | return -ENOSPC; |
| 1137 | } |
| 1138 | |
| 1139 | rc = msi_capability_init(dev, nvec, affd); |
| 1140 | if (rc == 0) |
| 1141 | return nvec; |
| 1142 | |
| 1143 | if (rc < 0) |
| 1144 | return rc; |
| 1145 | if (rc < minvec) |
| 1146 | return -ENOSPC; |
| 1147 | |
| 1148 | nvec = rc; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | /* deprecated, don't use */ |
| 1153 | int pci_enable_msi(struct pci_dev *dev) |
| 1154 | { |
| 1155 | int rc = __pci_enable_msi_range(dev, 1, 1, NULL); |
| 1156 | if (rc < 0) |
| 1157 | return rc; |
| 1158 | return 0; |
| 1159 | } |
| 1160 | EXPORT_SYMBOL(pci_enable_msi); |
| 1161 | |
| 1162 | static int __pci_enable_msix_range(struct pci_dev *dev, |
| 1163 | struct msix_entry *entries, int minvec, |
| 1164 | int maxvec, struct irq_affinity *affd, |
| 1165 | int flags) |
| 1166 | { |
| 1167 | int rc, nvec = maxvec; |
| 1168 | |
| 1169 | if (maxvec < minvec) |
| 1170 | return -ERANGE; |
| 1171 | |
| 1172 | if (WARN_ON_ONCE(dev->msix_enabled)) |
| 1173 | return -EINVAL; |
| 1174 | |
| 1175 | for (;;) { |
| 1176 | if (affd) { |
| 1177 | nvec = irq_calc_affinity_vectors(minvec, nvec, affd); |
| 1178 | if (nvec < minvec) |
| 1179 | return -ENOSPC; |
| 1180 | } |
| 1181 | |
| 1182 | rc = __pci_enable_msix(dev, entries, nvec, affd, flags); |
| 1183 | if (rc == 0) |
| 1184 | return nvec; |
| 1185 | |
| 1186 | if (rc < 0) |
| 1187 | return rc; |
| 1188 | if (rc < minvec) |
| 1189 | return -ENOSPC; |
| 1190 | |
| 1191 | nvec = rc; |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * pci_enable_msix_range - configure device's MSI-X capability structure |
| 1197 | * @dev: pointer to the pci_dev data structure of MSI-X device function |
| 1198 | * @entries: pointer to an array of MSI-X entries |
| 1199 | * @minvec: minimum number of MSI-X IRQs requested |
| 1200 | * @maxvec: maximum number of MSI-X IRQs requested |
| 1201 | * |
| 1202 | * Setup the MSI-X capability structure of device function with a maximum |
| 1203 | * possible number of interrupts in the range between @minvec and @maxvec |
| 1204 | * upon its software driver call to request for MSI-X mode enabled on its |
| 1205 | * hardware device function. It returns a negative errno if an error occurs. |
| 1206 | * If it succeeds, it returns the actual number of interrupts allocated and |
| 1207 | * indicates the successful configuration of MSI-X capability structure |
| 1208 | * with new allocated MSI-X interrupts. |
| 1209 | **/ |
| 1210 | int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries, |
| 1211 | int minvec, int maxvec) |
| 1212 | { |
| 1213 | return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0); |
| 1214 | } |
| 1215 | EXPORT_SYMBOL(pci_enable_msix_range); |
| 1216 | |
| 1217 | /** |
| 1218 | * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device |
| 1219 | * @dev: PCI device to operate on |
| 1220 | * @min_vecs: minimum number of vectors required (must be >= 1) |
| 1221 | * @max_vecs: maximum (desired) number of vectors |
| 1222 | * @flags: flags or quirks for the allocation |
| 1223 | * @affd: optional description of the affinity requirements |
| 1224 | * |
| 1225 | * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI |
| 1226 | * vectors if available, and fall back to a single legacy vector |
| 1227 | * if neither is available. Return the number of vectors allocated, |
| 1228 | * (which might be smaller than @max_vecs) if successful, or a negative |
| 1229 | * error code on error. If less than @min_vecs interrupt vectors are |
| 1230 | * available for @dev the function will fail with -ENOSPC. |
| 1231 | * |
| 1232 | * To get the Linux IRQ number used for a vector that can be passed to |
| 1233 | * request_irq() use the pci_irq_vector() helper. |
| 1234 | */ |
| 1235 | int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs, |
| 1236 | unsigned int max_vecs, unsigned int flags, |
| 1237 | struct irq_affinity *affd) |
| 1238 | { |
| 1239 | struct irq_affinity msi_default_affd = {0}; |
| 1240 | int msix_vecs = -ENOSPC; |
| 1241 | int msi_vecs = -ENOSPC; |
| 1242 | |
| 1243 | if (flags & PCI_IRQ_AFFINITY) { |
| 1244 | if (!affd) |
| 1245 | affd = &msi_default_affd; |
| 1246 | } else { |
| 1247 | if (WARN_ON(affd)) |
| 1248 | affd = NULL; |
| 1249 | } |
| 1250 | |
| 1251 | if (flags & PCI_IRQ_MSIX) { |
| 1252 | msix_vecs = __pci_enable_msix_range(dev, NULL, min_vecs, |
| 1253 | max_vecs, affd, flags); |
| 1254 | if (msix_vecs > 0) |
| 1255 | return msix_vecs; |
| 1256 | } |
| 1257 | |
| 1258 | if (flags & PCI_IRQ_MSI) { |
| 1259 | msi_vecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, |
| 1260 | affd); |
| 1261 | if (msi_vecs > 0) |
| 1262 | return msi_vecs; |
| 1263 | } |
| 1264 | |
| 1265 | /* use legacy IRQ if allowed */ |
| 1266 | if (flags & PCI_IRQ_LEGACY) { |
| 1267 | if (min_vecs == 1 && dev->irq) { |
| 1268 | /* |
| 1269 | * Invoke the affinity spreading logic to ensure that |
| 1270 | * the device driver can adjust queue configuration |
| 1271 | * for the single interrupt case. |
| 1272 | */ |
| 1273 | if (affd) |
| 1274 | irq_create_affinity_masks(1, affd); |
| 1275 | pci_intx(dev, 1); |
| 1276 | return 1; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | if (msix_vecs == -ENOSPC) |
| 1281 | return -ENOSPC; |
| 1282 | return msi_vecs; |
| 1283 | } |
| 1284 | EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity); |
| 1285 | |
| 1286 | /** |
| 1287 | * pci_free_irq_vectors - free previously allocated IRQs for a device |
| 1288 | * @dev: PCI device to operate on |
| 1289 | * |
| 1290 | * Undoes the allocations and enabling in pci_alloc_irq_vectors(). |
| 1291 | */ |
| 1292 | void pci_free_irq_vectors(struct pci_dev *dev) |
| 1293 | { |
| 1294 | pci_disable_msix(dev); |
| 1295 | pci_disable_msi(dev); |
| 1296 | } |
| 1297 | EXPORT_SYMBOL(pci_free_irq_vectors); |
| 1298 | |
| 1299 | /** |
| 1300 | * pci_irq_vector - return Linux IRQ number of a device vector |
| 1301 | * @dev: PCI device to operate on |
| 1302 | * @nr: Interrupt vector index (0-based) |
| 1303 | * |
| 1304 | * @nr has the following meanings depending on the interrupt mode: |
| 1305 | * MSI-X: The index in the MSI-X vector table |
| 1306 | * MSI: The index of the enabled MSI vectors |
| 1307 | * INTx: Must be 0 |
| 1308 | * |
| 1309 | * Return: The Linux interrupt number or -EINVAl if @nr is out of range. |
| 1310 | */ |
| 1311 | int pci_irq_vector(struct pci_dev *dev, unsigned int nr) |
| 1312 | { |
| 1313 | if (dev->msix_enabled) { |
| 1314 | struct msi_desc *entry; |
| 1315 | |
| 1316 | for_each_pci_msi_entry(entry, dev) { |
| 1317 | if (entry->msi_attrib.entry_nr == nr) |
| 1318 | return entry->irq; |
| 1319 | } |
| 1320 | WARN_ON_ONCE(1); |
| 1321 | return -EINVAL; |
| 1322 | } |
| 1323 | |
| 1324 | if (dev->msi_enabled) { |
| 1325 | struct msi_desc *entry = first_pci_msi_entry(dev); |
| 1326 | |
| 1327 | if (WARN_ON_ONCE(nr >= entry->nvec_used)) |
| 1328 | return -EINVAL; |
| 1329 | } else { |
| 1330 | if (WARN_ON_ONCE(nr > 0)) |
| 1331 | return -EINVAL; |
| 1332 | } |
| 1333 | |
| 1334 | return dev->irq + nr; |
| 1335 | } |
| 1336 | EXPORT_SYMBOL(pci_irq_vector); |
| 1337 | |
| 1338 | /** |
| 1339 | * pci_irq_get_affinity - return the affinity of a particular MSI vector |
| 1340 | * @dev: PCI device to operate on |
| 1341 | * @nr: device-relative interrupt vector index (0-based). |
| 1342 | * |
| 1343 | * @nr has the following meanings depending on the interrupt mode: |
| 1344 | * MSI-X: The index in the MSI-X vector table |
| 1345 | * MSI: The index of the enabled MSI vectors |
| 1346 | * INTx: Must be 0 |
| 1347 | * |
| 1348 | * Return: A cpumask pointer or NULL if @nr is out of range |
| 1349 | */ |
| 1350 | const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr) |
| 1351 | { |
| 1352 | if (dev->msix_enabled) { |
| 1353 | struct msi_desc *entry; |
| 1354 | |
| 1355 | for_each_pci_msi_entry(entry, dev) { |
| 1356 | if (entry->msi_attrib.entry_nr == nr) |
| 1357 | return &entry->affinity->mask; |
| 1358 | } |
| 1359 | WARN_ON_ONCE(1); |
| 1360 | return NULL; |
| 1361 | } else if (dev->msi_enabled) { |
| 1362 | struct msi_desc *entry = first_pci_msi_entry(dev); |
| 1363 | |
| 1364 | if (WARN_ON_ONCE(!entry || !entry->affinity || |
| 1365 | nr >= entry->nvec_used)) |
| 1366 | return NULL; |
| 1367 | |
| 1368 | return &entry->affinity[nr].mask; |
| 1369 | } else { |
| 1370 | return cpu_possible_mask; |
| 1371 | } |
| 1372 | } |
| 1373 | EXPORT_SYMBOL(pci_irq_get_affinity); |
| 1374 | |
| 1375 | /** |
| 1376 | * pci_irq_get_node - return the NUMA node of a particular MSI vector |
| 1377 | * @pdev: PCI device to operate on |
| 1378 | * @vec: device-relative interrupt vector index (0-based). |
| 1379 | */ |
| 1380 | int pci_irq_get_node(struct pci_dev *pdev, int vec) |
| 1381 | { |
| 1382 | const struct cpumask *mask; |
| 1383 | |
| 1384 | mask = pci_irq_get_affinity(pdev, vec); |
| 1385 | if (mask) |
| 1386 | return local_memory_node(cpu_to_node(cpumask_first(mask))); |
| 1387 | return dev_to_node(&pdev->dev); |
| 1388 | } |
| 1389 | EXPORT_SYMBOL(pci_irq_get_node); |
| 1390 | |
| 1391 | struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc) |
| 1392 | { |
| 1393 | return to_pci_dev(desc->dev); |
| 1394 | } |
| 1395 | EXPORT_SYMBOL(msi_desc_to_pci_dev); |
| 1396 | |
| 1397 | void *msi_desc_to_pci_sysdata(struct msi_desc *desc) |
| 1398 | { |
| 1399 | struct pci_dev *dev = msi_desc_to_pci_dev(desc); |
| 1400 | |
| 1401 | return dev->bus->sysdata; |
| 1402 | } |
| 1403 | EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata); |
| 1404 | |
| 1405 | #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN |
| 1406 | /** |
| 1407 | * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space |
| 1408 | * @irq_data: Pointer to interrupt data of the MSI interrupt |
| 1409 | * @msg: Pointer to the message |
| 1410 | */ |
| 1411 | void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg) |
| 1412 | { |
| 1413 | struct msi_desc *desc = irq_data_get_msi_desc(irq_data); |
| 1414 | |
| 1415 | /* |
| 1416 | * For MSI-X desc->irq is always equal to irq_data->irq. For |
| 1417 | * MSI only the first interrupt of MULTI MSI passes the test. |
| 1418 | */ |
| 1419 | if (desc->irq == irq_data->irq) |
| 1420 | __pci_write_msi_msg(desc, msg); |
| 1421 | } |
| 1422 | |
| 1423 | /** |
| 1424 | * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source |
| 1425 | * @dev: Pointer to the PCI device |
| 1426 | * @desc: Pointer to the MSI descriptor |
| 1427 | * |
| 1428 | * The ID number is only used within the irqdomain. |
| 1429 | */ |
| 1430 | irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev, |
| 1431 | struct msi_desc *desc) |
| 1432 | { |
| 1433 | return (irq_hw_number_t)desc->msi_attrib.entry_nr | |
| 1434 | pci_dev_id(dev) << 11 | |
| 1435 | ((irq_hw_number_t)(pci_domain_nr(dev->bus) & 0xFFFFFFFF)) << 27; |
| 1436 | } |
| 1437 | |
| 1438 | static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc) |
| 1439 | { |
| 1440 | return !desc->msi_attrib.is_msix && desc->nvec_used > 1; |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * pci_msi_domain_check_cap - Verify that @domain supports the capabilities |
| 1445 | * for @dev |
| 1446 | * @domain: The interrupt domain to check |
| 1447 | * @info: The domain info for verification |
| 1448 | * @dev: The device to check |
| 1449 | * |
| 1450 | * Returns: |
| 1451 | * 0 if the functionality is supported |
| 1452 | * 1 if Multi MSI is requested, but the domain does not support it |
| 1453 | * -ENOTSUPP otherwise |
| 1454 | */ |
| 1455 | int pci_msi_domain_check_cap(struct irq_domain *domain, |
| 1456 | struct msi_domain_info *info, struct device *dev) |
| 1457 | { |
| 1458 | struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev)); |
| 1459 | |
| 1460 | /* Special handling to support __pci_enable_msi_range() */ |
| 1461 | if (pci_msi_desc_is_multi_msi(desc) && |
| 1462 | !(info->flags & MSI_FLAG_MULTI_PCI_MSI)) |
| 1463 | return 1; |
| 1464 | else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX)) |
| 1465 | return -ENOTSUPP; |
| 1466 | |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
| 1470 | static int pci_msi_domain_handle_error(struct irq_domain *domain, |
| 1471 | struct msi_desc *desc, int error) |
| 1472 | { |
| 1473 | /* Special handling to support __pci_enable_msi_range() */ |
| 1474 | if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC) |
| 1475 | return 1; |
| 1476 | |
| 1477 | return error; |
| 1478 | } |
| 1479 | |
| 1480 | #ifdef GENERIC_MSI_DOMAIN_OPS |
| 1481 | static void pci_msi_domain_set_desc(msi_alloc_info_t *arg, |
| 1482 | struct msi_desc *desc) |
| 1483 | { |
| 1484 | arg->desc = desc; |
| 1485 | arg->hwirq = pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc), |
| 1486 | desc); |
| 1487 | } |
| 1488 | #else |
| 1489 | #define pci_msi_domain_set_desc NULL |
| 1490 | #endif |
| 1491 | |
| 1492 | static struct msi_domain_ops pci_msi_domain_ops_default = { |
| 1493 | .set_desc = pci_msi_domain_set_desc, |
| 1494 | .msi_check = pci_msi_domain_check_cap, |
| 1495 | .handle_error = pci_msi_domain_handle_error, |
| 1496 | }; |
| 1497 | |
| 1498 | static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info) |
| 1499 | { |
| 1500 | struct msi_domain_ops *ops = info->ops; |
| 1501 | |
| 1502 | if (ops == NULL) { |
| 1503 | info->ops = &pci_msi_domain_ops_default; |
| 1504 | } else { |
| 1505 | if (ops->set_desc == NULL) |
| 1506 | ops->set_desc = pci_msi_domain_set_desc; |
| 1507 | if (ops->msi_check == NULL) |
| 1508 | ops->msi_check = pci_msi_domain_check_cap; |
| 1509 | if (ops->handle_error == NULL) |
| 1510 | ops->handle_error = pci_msi_domain_handle_error; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info) |
| 1515 | { |
| 1516 | struct irq_chip *chip = info->chip; |
| 1517 | |
| 1518 | BUG_ON(!chip); |
| 1519 | if (!chip->irq_write_msi_msg) |
| 1520 | chip->irq_write_msi_msg = pci_msi_domain_write_msg; |
| 1521 | if (!chip->irq_mask) |
| 1522 | chip->irq_mask = pci_msi_mask_irq; |
| 1523 | if (!chip->irq_unmask) |
| 1524 | chip->irq_unmask = pci_msi_unmask_irq; |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * pci_msi_create_irq_domain - Create a MSI interrupt domain |
| 1529 | * @fwnode: Optional fwnode of the interrupt controller |
| 1530 | * @info: MSI domain info |
| 1531 | * @parent: Parent irq domain |
| 1532 | * |
| 1533 | * Updates the domain and chip ops and creates a MSI interrupt domain. |
| 1534 | * |
| 1535 | * Returns: |
| 1536 | * A domain pointer or NULL in case of failure. |
| 1537 | */ |
| 1538 | struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode, |
| 1539 | struct msi_domain_info *info, |
| 1540 | struct irq_domain *parent) |
| 1541 | { |
| 1542 | struct irq_domain *domain; |
| 1543 | |
| 1544 | if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE)) |
| 1545 | info->flags &= ~MSI_FLAG_LEVEL_CAPABLE; |
| 1546 | |
| 1547 | if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS) |
| 1548 | pci_msi_domain_update_dom_ops(info); |
| 1549 | if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) |
| 1550 | pci_msi_domain_update_chip_ops(info); |
| 1551 | |
| 1552 | info->flags |= MSI_FLAG_ACTIVATE_EARLY; |
| 1553 | if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE)) |
| 1554 | info->flags |= MSI_FLAG_MUST_REACTIVATE; |
| 1555 | |
| 1556 | /* PCI-MSI is oneshot-safe */ |
| 1557 | info->chip->flags |= IRQCHIP_ONESHOT_SAFE; |
| 1558 | |
| 1559 | domain = msi_create_irq_domain(fwnode, info, parent); |
| 1560 | if (!domain) |
| 1561 | return NULL; |
| 1562 | |
| 1563 | irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI); |
| 1564 | return domain; |
| 1565 | } |
| 1566 | EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain); |
| 1567 | |
| 1568 | /* |
| 1569 | * Users of the generic MSI infrastructure expect a device to have a single ID, |
| 1570 | * so with DMA aliases we have to pick the least-worst compromise. Devices with |
| 1571 | * DMA phantom functions tend to still emit MSIs from the real function number, |
| 1572 | * so we ignore those and only consider topological aliases where either the |
| 1573 | * alias device or RID appears on a different bus number. We also make the |
| 1574 | * reasonable assumption that bridges are walked in an upstream direction (so |
| 1575 | * the last one seen wins), and the much braver assumption that the most likely |
| 1576 | * case is that of PCI->PCIe so we should always use the alias RID. This echoes |
| 1577 | * the logic from intel_irq_remapping's set_msi_sid(), which presumably works |
| 1578 | * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions |
| 1579 | * for taking ownership all we can really do is close our eyes and hope... |
| 1580 | */ |
| 1581 | static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data) |
| 1582 | { |
| 1583 | u32 *pa = data; |
| 1584 | u8 bus = PCI_BUS_NUM(*pa); |
| 1585 | |
| 1586 | if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus) |
| 1587 | *pa = alias; |
| 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
| 1592 | /** |
| 1593 | * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID) |
| 1594 | * @domain: The interrupt domain |
| 1595 | * @pdev: The PCI device. |
| 1596 | * |
| 1597 | * The RID for a device is formed from the alias, with a firmware |
| 1598 | * supplied mapping applied |
| 1599 | * |
| 1600 | * Returns: The RID. |
| 1601 | */ |
| 1602 | u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev) |
| 1603 | { |
| 1604 | struct device_node *of_node; |
| 1605 | u32 rid = pci_dev_id(pdev); |
| 1606 | |
| 1607 | pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); |
| 1608 | |
| 1609 | of_node = irq_domain_get_of_node(domain); |
| 1610 | rid = of_node ? of_msi_map_id(&pdev->dev, of_node, rid) : |
| 1611 | iort_msi_map_id(&pdev->dev, rid); |
| 1612 | |
| 1613 | return rid; |
| 1614 | } |
| 1615 | |
| 1616 | /** |
| 1617 | * pci_msi_get_device_domain - Get the MSI domain for a given PCI device |
| 1618 | * @pdev: The PCI device |
| 1619 | * |
| 1620 | * Use the firmware data to find a device-specific MSI domain |
| 1621 | * (i.e. not one that is set as a default). |
| 1622 | * |
| 1623 | * Returns: The corresponding MSI domain or NULL if none has been found. |
| 1624 | */ |
| 1625 | struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev) |
| 1626 | { |
| 1627 | struct irq_domain *dom; |
| 1628 | u32 rid = pci_dev_id(pdev); |
| 1629 | |
| 1630 | pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); |
| 1631 | dom = of_msi_map_get_device_domain(&pdev->dev, rid, DOMAIN_BUS_PCI_MSI); |
| 1632 | if (!dom) |
| 1633 | dom = iort_get_device_domain(&pdev->dev, rid, |
| 1634 | DOMAIN_BUS_PCI_MSI); |
| 1635 | return dom; |
| 1636 | } |
| 1637 | #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */ |