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