Skip to content

Commit 1c8d7b0

Browse files
Matthew Wilcoxjbarnes993
authored andcommitted
PCI MSI: Add support for multiple MSI
Add the new API pci_enable_msi_block() to allow drivers to request multiple MSI and reimplement pci_enable_msi in terms of pci_enable_msi_block. Ensure that the architecture back ends don't have to know about multiple MSI. Signed-off-by: Matthew Wilcox <willy@linux.intel.com> Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
1 parent f2440d9 commit 1c8d7b0

7 files changed

Lines changed: 116 additions & 41 deletions

File tree

‎Documentation/PCI/MSI-HOWTO.txt‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,48 @@ This function should be called before the driver calls request_irq()
9494
since enabling MSIs disables the pin-based IRQ and the driver will not
9595
receive interrupts on the old interrupt.
9696

97-
4.2.2 pci_disable_msi
97+
4.2.2 pci_enable_msi_block
98+
99+
int pci_enable_msi_block(struct pci_dev *dev, int count)
100+
101+
This variation on the above call allows a device driver to request multiple
102+
MSIs. The MSI specification only allows interrupts to be allocated in
103+
powers of two, up to a maximum of 2^5 (32).
104+
105+
If this function returns 0, it has succeeded in allocating at least as many
106+
interrupts as the driver requested (it may have allocated more in order
107+
to satisfy the power-of-two requirement). In this case, the function
108+
enables MSI on this device and updates dev->irq to be the lowest of
109+
the new interrupts assigned to it. The other interrupts assigned to
110+
the device are in the range dev->irq to dev->irq + count - 1.
111+
112+
If this function returns a negative number, it indicates an error and
113+
the driver should not attempt to request any more MSI interrupts for
114+
this device. If this function returns a positive number, it will be
115+
less than 'count' and indicate the number of interrupts that could have
116+
been allocated. In neither case will the irq value have been
117+
updated, nor will the device have been switched into MSI mode.
118+
119+
The device driver must decide what action to take if
120+
pci_enable_msi_block() returns a value less than the number asked for.
121+
Some devices can make use of fewer interrupts than the maximum they
122+
request; in this case the driver should call pci_enable_msi_block()
123+
again. Note that it is not guaranteed to succeed, even when the
124+
'count' has been reduced to the value returned from a previous call to
125+
pci_enable_msi_block(). This is because there are multiple constraints
126+
on the number of vectors that can be allocated; pci_enable_msi_block()
127+
will return as soon as it finds any constraint that doesn't allow the
128+
call to succeed.
129+
130+
4.2.3 pci_disable_msi
98131

99132
void pci_disable_msi(struct pci_dev *dev)
100133

101-
This function should be used to undo the effect of pci_enable_msi().
102-
Calling it restores dev->irq to the pin-based interrupt number and frees
103-
the previously allocated message signaled interrupt(s). The interrupt
104-
may subsequently be assigned to another device, so drivers should not
105-
cache the value of dev->irq.
134+
This function should be used to undo the effect of pci_enable_msi() or
135+
pci_enable_msi_block(). Calling it restores dev->irq to the pin-based
136+
interrupt number and frees the previously allocated message signaled
137+
interrupt(s). The interrupt may subsequently be assigned to another
138+
device, so drivers should not cache the value of dev->irq.
106139

107140
A device driver must always call free_irq() on the interrupt(s)
108141
for which it has called request_irq() before calling this function.

‎arch/powerpc/kernel/msi.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ int arch_msi_check_device(struct pci_dev* dev, int nvec, int type)
1919
return -ENOSYS;
2020
}
2121

22+
/* PowerPC doesn't support multiple MSI yet */
23+
if (type == PCI_CAP_ID_MSI && nvec > 1)
24+
return 1;
25+
2226
if (ppc_md.msi_check_device) {
2327
pr_debug("msi: Using platform check routine.\n");
2428
return ppc_md.msi_check_device(dev, nvec, type);

‎arch/x86/kernel/io_apic.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,6 +3510,10 @@ int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
35103510
int index = 0;
35113511
#endif
35123512

3513+
/* x86 doesn't support multiple MSI yet */
3514+
if (type == PCI_CAP_ID_MSI && nvec > 1)
3515+
return 1;
3516+
35133517
irq_want = nr_irqs_gsi;
35143518
sub_handle = 0;
35153519
list_for_each_entry(msidesc, &dev->msi_list, list) {

‎drivers/pci/msi.c‎

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
4040
struct msi_desc *entry;
4141
int ret;
4242

43+
/*
44+
* If an architecture wants to support multiple MSI, it needs to
45+
* override arch_setup_msi_irqs()
46+
*/
47+
if (type == PCI_CAP_ID_MSI && nvec > 1)
48+
return 1;
49+
4350
list_for_each_entry(entry, &dev->msi_list, list) {
4451
ret = arch_setup_msi_irq(dev, entry);
4552
if (ret < 0)
@@ -58,8 +65,12 @@ void arch_teardown_msi_irqs(struct pci_dev *dev)
5865
struct msi_desc *entry;
5966

6067
list_for_each_entry(entry, &dev->msi_list, list) {
61-
if (entry->irq != 0)
62-
arch_teardown_msi_irq(entry->irq);
68+
int i, nvec;
69+
if (entry->irq == 0)
70+
continue;
71+
nvec = 1 << entry->msi_attrib.multiple;
72+
for (i = 0; i < nvec; i++)
73+
arch_teardown_msi_irq(entry->irq + i);
6374
}
6475
}
6576
#endif
@@ -163,7 +174,8 @@ static void msi_set_mask_bit(unsigned irq, u32 flag)
163174
msix_mask_irq(desc, flag);
164175
readl(desc->mask_base); /* Flush write to device */
165176
} else {
166-
msi_mask_irq(desc, 1, flag);
177+
unsigned offset = irq - desc->dev->irq;
178+
msi_mask_irq(desc, 1 << offset, flag << offset);
167179
}
168180
}
169181

@@ -229,6 +241,12 @@ void write_msi_msg_desc(struct irq_desc *desc, struct msi_msg *msg)
229241
} else {
230242
struct pci_dev *dev = entry->dev;
231243
int pos = entry->msi_attrib.pos;
244+
u16 msgctl;
245+
246+
pci_read_config_word(dev, msi_control_reg(pos), &msgctl);
247+
msgctl &= ~PCI_MSI_FLAGS_QSIZE;
248+
msgctl |= entry->msi_attrib.multiple << 4;
249+
pci_write_config_word(dev, msi_control_reg(pos), msgctl);
232250

233251
pci_write_config_dword(dev, msi_lower_address_reg(pos),
234252
msg->address_lo);
@@ -291,7 +309,7 @@ static void __pci_restore_msi_state(struct pci_dev *dev)
291309
pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
292310
msi_mask_irq(entry, msi_capable_mask(control), entry->masked);
293311
control &= ~PCI_MSI_FLAGS_QSIZE;
294-
control |= PCI_MSI_FLAGS_ENABLE;
312+
control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE;
295313
pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
296314
}
297315

@@ -332,13 +350,15 @@ EXPORT_SYMBOL_GPL(pci_restore_msi_state);
332350
/**
333351
* msi_capability_init - configure device's MSI capability structure
334352
* @dev: pointer to the pci_dev data structure of MSI device function
353+
* @nvec: number of interrupts to allocate
335354
*
336-
* Setup the MSI capability structure of device function with a single
337-
* MSI irq, regardless of device function is capable of handling
338-
* multiple messages. A return of zero indicates the successful setup
339-
* of an entry zero with the new MSI irq or non-zero for otherwise.
340-
**/
341-
static int msi_capability_init(struct pci_dev *dev)
355+
* Setup the MSI capability structure of the device with the requested
356+
* number of interrupts. A return value of zero indicates the successful
357+
* setup of an entry with the new MSI irq. A negative return value indicates
358+
* an error, and a positive return value indicates the number of interrupts
359+
* which could have been allocated.
360+
*/
361+
static int msi_capability_init(struct pci_dev *dev, int nvec)
342362
{
343363
struct msi_desc *entry;
344364
int pos, ret;
@@ -371,7 +391,7 @@ static int msi_capability_init(struct pci_dev *dev)
371391
list_add_tail(&entry->list, &dev->msi_list);
372392

373393
/* Configure MSI capability structure */
374-
ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI);
394+
ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
375395
if (ret) {
376396
msi_free_irqs(dev);
377397
return ret;
@@ -524,35 +544,48 @@ static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type)
524544
}
525545

526546
/**
527-
* pci_enable_msi - configure device's MSI capability structure
528-
* @dev: pointer to the pci_dev data structure of MSI device function
547+
* pci_enable_msi_block - configure device's MSI capability structure
548+
* @dev: device to configure
549+
* @nvec: number of interrupts to configure
529550
*
530-
* Setup the MSI capability structure of device function with
531-
* a single MSI irq upon its software driver call to request for
532-
* MSI mode enabled on its hardware device function. A return of zero
533-
* indicates the successful setup of an entry zero with the new MSI
534-
* irq or non-zero for otherwise.
535-
**/
536-
int pci_enable_msi(struct pci_dev* dev)
551+
* Allocate IRQs for a device with the MSI capability.
552+
* This function returns a negative errno if an error occurs. If it
553+
* is unable to allocate the number of interrupts requested, it returns
554+
* the number of interrupts it might be able to allocate. If it successfully
555+
* allocates at least the number of interrupts requested, it returns 0 and
556+
* updates the @dev's irq member to the lowest new interrupt number; the
557+
* other interrupt numbers allocated to this device are consecutive.
558+
*/
559+
int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
537560
{
538-
int status;
561+
int status, pos, maxvec;
562+
u16 msgctl;
563+
564+
pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
565+
if (!pos)
566+
return -EINVAL;
567+
pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
568+
maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
569+
if (nvec > maxvec)
570+
return maxvec;
539571

540-
status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI);
572+
status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
541573
if (status)
542574
return status;
543575

544576
WARN_ON(!!dev->msi_enabled);
545577

546-
/* Check whether driver already requested for MSI-X irqs */
578+
/* Check whether driver already requested MSI-X irqs */
547579
if (dev->msix_enabled) {
548580
dev_info(&dev->dev, "can't enable MSI "
549581
"(MSI-X already enabled)\n");
550582
return -EINVAL;
551583
}
552-
status = msi_capability_init(dev);
584+
585+
status = msi_capability_init(dev, nvec);
553586
return status;
554587
}
555-
EXPORT_SYMBOL(pci_enable_msi);
588+
EXPORT_SYMBOL(pci_enable_msi_block);
556589

557590
void pci_msi_shutdown(struct pci_dev *dev)
558591
{
@@ -599,8 +632,12 @@ static int msi_free_irqs(struct pci_dev* dev)
599632
struct msi_desc *entry, *tmp;
600633

601634
list_for_each_entry(entry, &dev->msi_list, list) {
602-
if (entry->irq)
603-
BUG_ON(irq_has_action(entry->irq));
635+
int i, nvec;
636+
if (!entry->irq)
637+
continue;
638+
nvec = 1 << entry->msi_attrib.multiple;
639+
for (i = 0; i < nvec; i++)
640+
BUG_ON(irq_has_action(entry->irq + i));
604641
}
605642

606643
arch_teardown_msi_irqs(dev);

‎drivers/pci/msi.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,8 @@
2020
#define msi_mask_bits_reg(base, is64bit) \
2121
( (is64bit == 1) ? base+PCI_MSI_MASK_BIT : base+PCI_MSI_MASK_BIT-4)
2222
#define msi_disable(control) control &= ~PCI_MSI_FLAGS_ENABLE
23-
#define multi_msi_capable(control) \
24-
(1 << ((control & PCI_MSI_FLAGS_QMASK) >> 1))
25-
#define multi_msi_enable(control, num) \
26-
control |= (((num >> 1) << 4) & PCI_MSI_FLAGS_QSIZE);
2723
#define is_64bit_address(control) (!!(control & PCI_MSI_FLAGS_64BIT))
2824
#define is_mask_bit_support(control) (!!(control & PCI_MSI_FLAGS_MASKBIT))
29-
#define msi_enable(control, num) multi_msi_enable(control, num); \
30-
control |= PCI_MSI_FLAGS_ENABLE
3125

3226
#define msix_table_offset_reg(base) (base + 0x04)
3327
#define msix_pba_offset_reg(base) (base + 0x08)

‎include/linux/msi.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern void write_msi_msg(unsigned int irq, struct msi_msg *msg);
2121
struct msi_desc {
2222
struct {
2323
__u8 is_msix : 1;
24+
__u8 multiple: 3; /* log2 number of messages */
2425
__u8 maskbit : 1; /* mask-pending bit supported ? */
2526
__u8 is_64 : 1; /* Address size: 0=32bit 1=64bit */
2627
__u8 pos; /* Location of the msi capability */

‎include/linux/pci.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ struct msix_entry {
789789

790790

791791
#ifndef CONFIG_PCI_MSI
792-
static inline int pci_enable_msi(struct pci_dev *dev)
792+
static inline int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
793793
{
794794
return -1;
795795
}
@@ -824,7 +824,7 @@ static inline int pci_msi_enabled(void)
824824
return 0;
825825
}
826826
#else
827-
extern int pci_enable_msi(struct pci_dev *dev);
827+
extern int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec);
828828
extern void pci_msi_shutdown(struct pci_dev *dev);
829829
extern void pci_disable_msi(struct pci_dev *dev);
830830
extern int pci_msix_table_size(struct pci_dev *dev);
@@ -846,6 +846,8 @@ static inline int pcie_aspm_enabled(void)
846846
extern int pcie_aspm_enabled(void);
847847
#endif
848848

849+
#define pci_enable_msi(pdev) pci_enable_msi_block(pdev, 1)
850+
849851
#ifdef CONFIG_HT_IRQ
850852
/* The functions a driver should call */
851853
int ht_create_irq(struct pci_dev *dev, int idx);

0 commit comments

Comments
 (0)