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