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