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