3 * Purpose: PCI Message Signaled Interrupt (MSI)
5 * Copyright (C) 2003-2004 Intel
6 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
10 #include <linux/irq.h>
11 #include <linux/interrupt.h>
12 #include <linux/init.h>
13 #include <linux/config.h>
14 #include <linux/ioport.h>
15 #include <linux/smp_lock.h>
16 #include <linux/pci.h>
17 #include <linux/proc_fs.h>
19 #include <asm/errno.h>
25 static spinlock_t msi_lock
= SPIN_LOCK_UNLOCKED
;
26 static struct msi_desc
* msi_desc
[NR_IRQS
] = { [0 ... NR_IRQS
-1] = NULL
};
27 static kmem_cache_t
* msi_cachep
;
29 static int pci_msi_enable
= 1;
30 static int last_alloc_vector
= 0;
31 static int nr_released_vectors
= 0;
32 static int nr_reserved_vectors
= NR_HP_RESERVED_VECTORS
;
33 static int nr_msix_devices
= 0;
35 #ifndef CONFIG_X86_IO_APIC
36 int vector_irq
[NR_VECTORS
] = { [0 ... NR_VECTORS
- 1] = -1};
37 u8 irq_vector
[NR_IRQ_VECTORS
] = { FIRST_DEVICE_VECTOR
, 0 };
40 static void msi_cache_ctor(void *p
, kmem_cache_t
*cache
, unsigned long flags
)
42 memset(p
, 0, NR_IRQS
* sizeof(struct msi_desc
));
45 static int msi_cache_init(void)
47 msi_cachep
= kmem_cache_create("msi_cache",
48 NR_IRQS
* sizeof(struct msi_desc
),
49 0, SLAB_HWCACHE_ALIGN
, msi_cache_ctor
, NULL
);
56 static void msi_set_mask_bit(unsigned int vector
, int flag
)
58 struct msi_desc
*entry
;
60 entry
= (struct msi_desc
*)msi_desc
[vector
];
61 if (!entry
|| !entry
->dev
|| !entry
->mask_base
)
63 switch (entry
->msi_attrib
.type
) {
69 pos
= entry
->mask_base
;
70 pci_read_config_dword(entry
->dev
, pos
, &mask_bits
);
73 pci_write_config_dword(entry
->dev
, pos
, mask_bits
);
78 int offset
= entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
+
79 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET
;
80 writel(flag
, entry
->mask_base
+ offset
);
89 static void set_msi_affinity(unsigned int vector
, cpumask_t cpu_mask
)
91 struct msi_desc
*entry
;
92 struct msg_address address
;
94 entry
= (struct msi_desc
*)msi_desc
[vector
];
95 if (!entry
|| !entry
->dev
)
98 switch (entry
->msi_attrib
.type
) {
103 if (!(pos
= pci_find_capability(entry
->dev
, PCI_CAP_ID_MSI
)))
106 pci_read_config_dword(entry
->dev
, msi_lower_address_reg(pos
),
107 &address
.lo_address
.value
);
108 address
.lo_address
.value
&= MSI_ADDRESS_DEST_ID_MASK
;
109 address
.lo_address
.value
|= (cpu_mask_to_apicid(cpu_mask
) <<
110 MSI_TARGET_CPU_SHIFT
);
111 entry
->msi_attrib
.current_cpu
= cpu_mask_to_apicid(cpu_mask
);
112 pci_write_config_dword(entry
->dev
, msi_lower_address_reg(pos
),
113 address
.lo_address
.value
);
116 case PCI_CAP_ID_MSIX
:
118 int offset
= entry
->msi_attrib
.entry_nr
* PCI_MSIX_ENTRY_SIZE
+
119 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
;
121 address
.lo_address
.value
= readl(entry
->mask_base
+ offset
);
122 address
.lo_address
.value
&= MSI_ADDRESS_DEST_ID_MASK
;
123 address
.lo_address
.value
|= (cpu_mask_to_apicid(cpu_mask
) <<
124 MSI_TARGET_CPU_SHIFT
);
125 entry
->msi_attrib
.current_cpu
= cpu_mask_to_apicid(cpu_mask
);
126 writel(address
.lo_address
.value
, entry
->mask_base
+ offset
);
134 #ifdef CONFIG_IRQBALANCE
135 static inline void move_msi(int vector
)
137 if (!cpus_empty(pending_irq_balance_cpumask
[vector
])) {
138 set_msi_affinity(vector
, pending_irq_balance_cpumask
[vector
]);
139 cpus_clear(pending_irq_balance_cpumask
[vector
]);
142 #endif /* CONFIG_IRQBALANCE */
143 #endif /* CONFIG_SMP */
145 static void mask_MSI_irq(unsigned int vector
)
147 msi_set_mask_bit(vector
, 1);
150 static void unmask_MSI_irq(unsigned int vector
)
152 msi_set_mask_bit(vector
, 0);
155 static unsigned int startup_msi_irq_wo_maskbit(unsigned int vector
)
157 struct msi_desc
*entry
;
160 spin_lock_irqsave(&msi_lock
, flags
);
161 entry
= msi_desc
[vector
];
162 if (!entry
|| !entry
->dev
) {
163 spin_unlock_irqrestore(&msi_lock
, flags
);
166 entry
->msi_attrib
.state
= 1; /* Mark it active */
167 spin_unlock_irqrestore(&msi_lock
, flags
);
169 return 0; /* never anything pending */
172 static void release_msi(unsigned int vector
);
173 static void shutdown_msi_irq(unsigned int vector
)
178 #define shutdown_msi_irq_wo_maskbit shutdown_msi_irq
179 static void enable_msi_irq_wo_maskbit(unsigned int vector
) {}
180 static void disable_msi_irq_wo_maskbit(unsigned int vector
) {}
181 static void ack_msi_irq_wo_maskbit(unsigned int vector
) {}
182 static void end_msi_irq_wo_maskbit(unsigned int vector
)
188 static unsigned int startup_msi_irq_w_maskbit(unsigned int vector
)
190 struct msi_desc
*entry
;
193 spin_lock_irqsave(&msi_lock
, flags
);
194 entry
= msi_desc
[vector
];
195 if (!entry
|| !entry
->dev
) {
196 spin_unlock_irqrestore(&msi_lock
, flags
);
199 entry
->msi_attrib
.state
= 1; /* Mark it active */
200 spin_unlock_irqrestore(&msi_lock
, flags
);
202 unmask_MSI_irq(vector
);
203 return 0; /* never anything pending */
206 #define shutdown_msi_irq_w_maskbit shutdown_msi_irq
207 #define enable_msi_irq_w_maskbit unmask_MSI_irq
208 #define disable_msi_irq_w_maskbit mask_MSI_irq
209 #define ack_msi_irq_w_maskbit mask_MSI_irq
211 static void end_msi_irq_w_maskbit(unsigned int vector
)
214 unmask_MSI_irq(vector
);
219 * Interrupt Type for MSI-X PCI/PCI-X/PCI-Express Devices,
220 * which implement the MSI-X Capability Structure.
222 static struct hw_interrupt_type msix_irq_type
= {
223 .typename
= "PCI-MSI-X",
224 .startup
= startup_msi_irq_w_maskbit
,
225 .shutdown
= shutdown_msi_irq_w_maskbit
,
226 .enable
= enable_msi_irq_w_maskbit
,
227 .disable
= disable_msi_irq_w_maskbit
,
228 .ack
= ack_msi_irq_w_maskbit
,
229 .end
= end_msi_irq_w_maskbit
,
230 .set_affinity
= set_msi_irq_affinity
234 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
235 * which implement the MSI Capability Structure with
236 * Mask-and-Pending Bits.
238 static struct hw_interrupt_type msi_irq_w_maskbit_type
= {
239 .typename
= "PCI-MSI",
240 .startup
= startup_msi_irq_w_maskbit
,
241 .shutdown
= shutdown_msi_irq_w_maskbit
,
242 .enable
= enable_msi_irq_w_maskbit
,
243 .disable
= disable_msi_irq_w_maskbit
,
244 .ack
= ack_msi_irq_w_maskbit
,
245 .end
= end_msi_irq_w_maskbit
,
246 .set_affinity
= set_msi_irq_affinity
250 * Interrupt Type for MSI PCI/PCI-X/PCI-Express Devices,
251 * which implement the MSI Capability Structure without
252 * Mask-and-Pending Bits.
254 static struct hw_interrupt_type msi_irq_wo_maskbit_type
= {
255 .typename
= "PCI-MSI",
256 .startup
= startup_msi_irq_wo_maskbit
,
257 .shutdown
= shutdown_msi_irq_wo_maskbit
,
258 .enable
= enable_msi_irq_wo_maskbit
,
259 .disable
= disable_msi_irq_wo_maskbit
,
260 .ack
= ack_msi_irq_wo_maskbit
,
261 .end
= end_msi_irq_wo_maskbit
,
262 .set_affinity
= set_msi_irq_affinity
265 static void msi_data_init(struct msg_data
*msi_data
,
268 memset(msi_data
, 0, sizeof(struct msg_data
));
269 msi_data
->vector
= (u8
)vector
;
270 msi_data
->delivery_mode
= MSI_DELIVERY_MODE
;
271 msi_data
->level
= MSI_LEVEL_MODE
;
272 msi_data
->trigger
= MSI_TRIGGER_MODE
;
275 static void msi_address_init(struct msg_address
*msi_address
)
277 unsigned int dest_id
;
279 memset(msi_address
, 0, sizeof(struct msg_address
));
280 msi_address
->hi_address
= (u32
)0;
281 dest_id
= (MSI_ADDRESS_HEADER
<< MSI_ADDRESS_HEADER_SHIFT
);
282 msi_address
->lo_address
.u
.dest_mode
= MSI_DEST_MODE
;
283 msi_address
->lo_address
.u
.redirection_hint
= MSI_REDIRECTION_HINT_MODE
;
284 msi_address
->lo_address
.u
.dest_id
= dest_id
;
285 msi_address
->lo_address
.value
|= (MSI_TARGET_CPU
<< MSI_TARGET_CPU_SHIFT
);
288 static int msi_free_vector(struct pci_dev
* dev
, int vector
, int reassign
);
289 static int assign_msi_vector(void)
291 static int new_vector_avail
= 1;
296 * msi_lock is provided to ensure that successful allocation of MSI
297 * vector is assigned unique among drivers.
299 spin_lock_irqsave(&msi_lock
, flags
);
301 if (!new_vector_avail
) {
305 * vector_irq[] = -1 indicates that this specific vector is:
306 * - assigned for MSI (since MSI have no associated IRQ) or
307 * - assigned for legacy if less than 16, or
308 * - having no corresponding 1:1 vector-to-IOxAPIC IRQ mapping
309 * vector_irq[] = 0 indicates that this vector, previously
310 * assigned for MSI, is freed by hotplug removed operations.
311 * This vector will be reused for any subsequent hotplug added
313 * vector_irq[] > 0 indicates that this vector is assigned for
314 * IOxAPIC IRQs. This vector and its value provides a 1-to-1
315 * vector-to-IOxAPIC IRQ mapping.
317 for (vector
= FIRST_DEVICE_VECTOR
; vector
< NR_IRQS
; vector
++) {
318 if (vector_irq
[vector
] != 0)
320 free_vector
= vector
;
321 if (!msi_desc
[vector
])
327 spin_unlock_irqrestore(&msi_lock
, flags
);
330 vector_irq
[free_vector
] = -1;
331 nr_released_vectors
--;
332 spin_unlock_irqrestore(&msi_lock
, flags
);
333 if (msi_desc
[free_vector
] != NULL
) {
337 /* free all linked vectors before re-assign */
339 spin_lock_irqsave(&msi_lock
, flags
);
340 dev
= msi_desc
[free_vector
]->dev
;
341 tail
= msi_desc
[free_vector
]->link
.tail
;
342 spin_unlock_irqrestore(&msi_lock
, flags
);
343 msi_free_vector(dev
, tail
, 1);
344 } while (free_vector
!= tail
);
349 vector
= assign_irq_vector(AUTO_ASSIGN
);
350 last_alloc_vector
= vector
;
351 if (vector
== LAST_DEVICE_VECTOR
)
352 new_vector_avail
= 0;
354 spin_unlock_irqrestore(&msi_lock
, flags
);
358 static int get_new_vector(void)
362 if ((vector
= assign_msi_vector()) > 0)
363 set_intr_gate(vector
, interrupt
[vector
]);
368 static int msi_init(void)
370 static int status
= -ENOMEM
;
375 if ((status
= msi_cache_init()) < 0) {
377 printk(KERN_INFO
"WARNING: MSI INIT FAILURE\n");
380 last_alloc_vector
= assign_irq_vector(AUTO_ASSIGN
);
381 if (last_alloc_vector
< 0) {
383 printk(KERN_INFO
"WARNING: ALL VECTORS ARE BUSY\n");
387 vector_irq
[last_alloc_vector
] = 0;
388 nr_released_vectors
++;
389 printk(KERN_INFO
"MSI INIT SUCCESS\n");
394 static int get_msi_vector(struct pci_dev
*dev
)
396 return get_new_vector();
399 static struct msi_desc
* alloc_msi_entry(void)
401 struct msi_desc
*entry
;
403 entry
= (struct msi_desc
*) kmem_cache_alloc(msi_cachep
, SLAB_KERNEL
);
407 memset(entry
, 0, sizeof(struct msi_desc
));
408 entry
->link
.tail
= entry
->link
.head
= 0; /* single message */
414 static void attach_msi_entry(struct msi_desc
*entry
, int vector
)
418 spin_lock_irqsave(&msi_lock
, flags
);
419 msi_desc
[vector
] = entry
;
420 spin_unlock_irqrestore(&msi_lock
, flags
);
423 static void irq_handler_init(int cap_id
, int pos
, int mask
)
425 spin_lock(&irq_desc
[pos
].lock
);
426 if (cap_id
== PCI_CAP_ID_MSIX
)
427 irq_desc
[pos
].handler
= &msix_irq_type
;
430 irq_desc
[pos
].handler
= &msi_irq_wo_maskbit_type
;
432 irq_desc
[pos
].handler
= &msi_irq_w_maskbit_type
;
434 spin_unlock(&irq_desc
[pos
].lock
);
437 static void enable_msi_mode(struct pci_dev
*dev
, int pos
, int type
)
441 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
442 if (type
== PCI_CAP_ID_MSI
) {
443 /* Set enabled bits to single MSI & enable MSI_enable bit */
444 msi_enable(control
, 1);
445 pci_write_config_word(dev
, msi_control_reg(pos
), control
);
447 msix_enable(control
);
448 pci_write_config_word(dev
, msi_control_reg(pos
), control
);
450 if (pci_find_capability(dev
, PCI_CAP_ID_EXP
)) {
451 /* PCI Express Endpoint device detected */
453 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
454 cmd
|= PCI_COMMAND_INTX_DISABLE
;
455 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
459 static void disable_msi_mode(struct pci_dev
*dev
, int pos
, int type
)
463 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
464 if (type
== PCI_CAP_ID_MSI
) {
465 /* Set enabled bits to single MSI & enable MSI_enable bit */
466 msi_disable(control
);
467 pci_write_config_word(dev
, msi_control_reg(pos
), control
);
469 msix_disable(control
);
470 pci_write_config_word(dev
, msi_control_reg(pos
), control
);
472 if (pci_find_capability(dev
, PCI_CAP_ID_EXP
)) {
473 /* PCI Express Endpoint device detected */
475 pci_read_config_word(dev
, PCI_COMMAND
, &cmd
);
476 cmd
&= ~PCI_COMMAND_INTX_DISABLE
;
477 pci_write_config_word(dev
, PCI_COMMAND
, cmd
);
481 static int msi_lookup_vector(struct pci_dev
*dev
, int type
)
486 spin_lock_irqsave(&msi_lock
, flags
);
487 for (vector
= FIRST_DEVICE_VECTOR
; vector
< NR_IRQS
; vector
++) {
488 if (!msi_desc
[vector
] || msi_desc
[vector
]->dev
!= dev
||
489 msi_desc
[vector
]->msi_attrib
.type
!= type
||
490 msi_desc
[vector
]->msi_attrib
.default_vector
!= dev
->irq
)
492 spin_unlock_irqrestore(&msi_lock
, flags
);
493 /* This pre-assigned MSI vector for this device
494 already exits. Override dev->irq with this vector */
498 spin_unlock_irqrestore(&msi_lock
, flags
);
503 void pci_scan_msi_device(struct pci_dev
*dev
)
508 if (pci_find_capability(dev
, PCI_CAP_ID_MSIX
) > 0)
510 else if (pci_find_capability(dev
, PCI_CAP_ID_MSI
) > 0)
511 nr_reserved_vectors
++;
515 * msi_capability_init - configure device's MSI capability structure
516 * @dev: pointer to the pci_dev data structure of MSI device function
518 * Setup the MSI capability structure of device funtion with a single
519 * MSI vector, regardless of device function is capable of handling
520 * multiple messages. A return of zero indicates the successful setup
521 * of an entry zero with the new MSI vector or non-zero for otherwise.
523 static int msi_capability_init(struct pci_dev
*dev
)
525 struct msi_desc
*entry
;
526 struct msg_address address
;
527 struct msg_data data
;
531 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
);
532 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
533 /* MSI Entry Initialization */
534 if (!(entry
= alloc_msi_entry()))
537 if ((vector
= get_msi_vector(dev
)) < 0) {
538 kmem_cache_free(msi_cachep
, entry
);
541 entry
->link
.head
= vector
;
542 entry
->link
.tail
= vector
;
543 entry
->msi_attrib
.type
= PCI_CAP_ID_MSI
;
544 entry
->msi_attrib
.state
= 0; /* Mark it not active */
545 entry
->msi_attrib
.entry_nr
= 0;
546 entry
->msi_attrib
.maskbit
= is_mask_bit_support(control
);
547 entry
->msi_attrib
.default_vector
= dev
->irq
; /* Save IOAPIC IRQ */
550 if (is_mask_bit_support(control
)) {
551 entry
->mask_base
= msi_mask_bits_reg(pos
,
552 is_64bit_address(control
));
554 /* Replace with MSI handler */
555 irq_handler_init(PCI_CAP_ID_MSI
, vector
, entry
->msi_attrib
.maskbit
);
556 /* Configure MSI capability structure */
557 msi_address_init(&address
);
558 msi_data_init(&data
, vector
);
559 entry
->msi_attrib
.current_cpu
= ((address
.lo_address
.u
.dest_id
>>
560 MSI_TARGET_CPU_SHIFT
) & MSI_TARGET_CPU_MASK
);
561 pci_write_config_dword(dev
, msi_lower_address_reg(pos
),
562 address
.lo_address
.value
);
563 if (is_64bit_address(control
)) {
564 pci_write_config_dword(dev
,
565 msi_upper_address_reg(pos
), address
.hi_address
);
566 pci_write_config_word(dev
,
567 msi_data_reg(pos
, 1), *((u32
*)&data
));
569 pci_write_config_word(dev
,
570 msi_data_reg(pos
, 0), *((u32
*)&data
));
571 if (entry
->msi_attrib
.maskbit
) {
572 unsigned int maskbits
, temp
;
573 /* All MSIs are unmasked by default, Mask them all */
574 pci_read_config_dword(dev
,
575 msi_mask_bits_reg(pos
, is_64bit_address(control
)),
577 temp
= (1 << multi_msi_capable(control
));
578 temp
= ((temp
- 1) & ~temp
);
580 pci_write_config_dword(dev
,
581 msi_mask_bits_reg(pos
, is_64bit_address(control
)),
584 attach_msi_entry(entry
, vector
);
585 /* Set MSI enabled bits */
586 enable_msi_mode(dev
, pos
, PCI_CAP_ID_MSI
);
592 * msix_capability_init - configure device's MSI-X capability
593 * @dev: pointer to the pci_dev data structure of MSI-X device function
595 * Setup the MSI-X capability structure of device funtion with a
596 * single MSI-X vector. A return of zero indicates the successful setup of
597 * requested MSI-X entries with allocated vectors or non-zero for otherwise.
599 static int msix_capability_init(struct pci_dev
*dev
,
600 struct msix_entry
*entries
, int nvec
)
602 struct msi_desc
*head
= NULL
, *tail
= NULL
, *entry
= NULL
;
603 struct msg_address address
;
604 struct msg_data data
;
605 int vector
, pos
, i
, j
, nr_entries
, temp
= 0;
606 u32 phys_addr
, table_offset
;
611 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
612 /* Request & Map MSI-X table region */
613 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
614 nr_entries
= multi_msix_capable(control
);
615 pci_read_config_dword(dev
, msix_table_offset_reg(pos
),
617 bir
= (u8
)(table_offset
& PCI_MSIX_FLAGS_BIRMASK
);
618 phys_addr
= pci_resource_start (dev
, bir
);
619 phys_addr
+= (u32
)(table_offset
& ~PCI_MSIX_FLAGS_BIRMASK
);
620 if (!request_mem_region(phys_addr
,
621 nr_entries
* PCI_MSIX_ENTRY_SIZE
,
622 "MSI-X vector table"))
624 base
= ioremap_nocache(phys_addr
, nr_entries
* PCI_MSIX_ENTRY_SIZE
);
626 release_mem_region(phys_addr
, nr_entries
* PCI_MSIX_ENTRY_SIZE
);
629 /* MSI-X Table Initialization */
630 for (i
= 0; i
< nvec
; i
++) {
631 entry
= alloc_msi_entry();
634 if ((vector
= get_msi_vector(dev
)) < 0)
637 j
= entries
[i
].entry
;
638 entries
[i
].vector
= vector
;
639 entry
->msi_attrib
.type
= PCI_CAP_ID_MSIX
;
640 entry
->msi_attrib
.state
= 0; /* Mark it not active */
641 entry
->msi_attrib
.entry_nr
= j
;
642 entry
->msi_attrib
.maskbit
= 1;
643 entry
->msi_attrib
.default_vector
= dev
->irq
;
645 entry
->mask_base
= (unsigned long)base
;
647 entry
->link
.head
= vector
;
648 entry
->link
.tail
= vector
;
651 entry
->link
.head
= temp
;
652 entry
->link
.tail
= tail
->link
.tail
;
653 tail
->link
.tail
= vector
;
654 head
->link
.head
= vector
;
658 /* Replace with MSI-X handler */
659 irq_handler_init(PCI_CAP_ID_MSIX
, vector
, 1);
660 /* Configure MSI-X capability structure */
661 msi_address_init(&address
);
662 msi_data_init(&data
, vector
);
663 entry
->msi_attrib
.current_cpu
=
664 ((address
.lo_address
.u
.dest_id
>>
665 MSI_TARGET_CPU_SHIFT
) & MSI_TARGET_CPU_MASK
);
666 writel(address
.lo_address
.value
,
667 base
+ j
* PCI_MSIX_ENTRY_SIZE
+
668 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
);
669 writel(address
.hi_address
,
670 base
+ j
* PCI_MSIX_ENTRY_SIZE
+
671 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
);
673 base
+ j
* PCI_MSIX_ENTRY_SIZE
+
674 PCI_MSIX_ENTRY_DATA_OFFSET
);
675 attach_msi_entry(entry
, vector
);
679 for (; i
>= 0; i
--) {
680 vector
= (entries
+ i
)->vector
;
681 msi_free_vector(dev
, vector
, 0);
682 (entries
+ i
)->vector
= 0;
686 /* Set MSI-X enabled bits */
687 enable_msi_mode(dev
, pos
, PCI_CAP_ID_MSIX
);
693 * pci_enable_msi - configure device's MSI capability structure
694 * @dev: pointer to the pci_dev data structure of MSI device function
696 * Setup the MSI capability structure of device function with
697 * a single MSI vector upon its software driver call to request for
698 * MSI mode enabled on its hardware device function. A return of zero
699 * indicates the successful setup of an entry zero with the new MSI
700 * vector or non-zero for otherwise.
702 int pci_enable_msi(struct pci_dev
* dev
)
704 int pos
, temp
= dev
->irq
, status
= -EINVAL
;
707 if (!pci_msi_enable
|| !dev
)
710 if ((status
= msi_init()) < 0)
713 if (!(pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
)))
716 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
717 if (control
& PCI_MSI_FLAGS_ENABLE
)
718 return 0; /* Already in MSI mode */
720 if (!msi_lookup_vector(dev
, PCI_CAP_ID_MSI
)) {
724 spin_lock_irqsave(&msi_lock
, flags
);
725 if (!vector_irq
[dev
->irq
]) {
726 msi_desc
[dev
->irq
]->msi_attrib
.state
= 0;
727 vector_irq
[dev
->irq
] = -1;
728 nr_released_vectors
--;
729 spin_unlock_irqrestore(&msi_lock
, flags
);
730 enable_msi_mode(dev
, pos
, PCI_CAP_ID_MSI
);
733 spin_unlock_irqrestore(&msi_lock
, flags
);
736 /* Check whether driver already requested for MSI-X vectors */
737 if ((pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
)) > 0 &&
738 !msi_lookup_vector(dev
, PCI_CAP_ID_MSIX
)) {
739 printk(KERN_INFO
"Can't enable MSI. Device already had MSI-X vectors assigned\n");
743 status
= msi_capability_init(dev
);
746 nr_reserved_vectors
--; /* Only MSI capable */
747 else if (nr_msix_devices
> 0)
748 nr_msix_devices
--; /* Both MSI and MSI-X capable,
749 but choose enabling MSI */
755 void pci_disable_msi(struct pci_dev
* dev
)
757 struct msi_desc
*entry
;
758 int pos
, default_vector
;
762 if (!dev
|| !(pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
)))
765 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
766 if (!(control
& PCI_MSI_FLAGS_ENABLE
))
769 spin_lock_irqsave(&msi_lock
, flags
);
770 entry
= msi_desc
[dev
->irq
];
771 if (!entry
|| !entry
->dev
|| entry
->msi_attrib
.type
!= PCI_CAP_ID_MSI
) {
772 spin_unlock_irqrestore(&msi_lock
, flags
);
775 if (entry
->msi_attrib
.state
) {
776 spin_unlock_irqrestore(&msi_lock
, flags
);
777 printk(KERN_DEBUG
"Driver[%d:%d:%d] unloaded wo doing free_irq on vector->%d\n",
778 dev
->bus
->number
, PCI_SLOT(dev
->devfn
), PCI_FUNC(dev
->devfn
),
780 BUG_ON(entry
->msi_attrib
.state
> 0);
782 vector_irq
[dev
->irq
] = 0; /* free it */
783 nr_released_vectors
++;
784 default_vector
= entry
->msi_attrib
.default_vector
;
785 spin_unlock_irqrestore(&msi_lock
, flags
);
786 /* Restore dev->irq to its default pin-assertion vector */
787 dev
->irq
= default_vector
;
788 disable_msi_mode(dev
, pci_find_capability(dev
, PCI_CAP_ID_MSI
),
793 static void release_msi(unsigned int vector
)
795 struct msi_desc
*entry
;
798 spin_lock_irqsave(&msi_lock
, flags
);
799 entry
= msi_desc
[vector
];
800 if (entry
&& entry
->dev
)
801 entry
->msi_attrib
.state
= 0; /* Mark it not active */
802 spin_unlock_irqrestore(&msi_lock
, flags
);
805 static int msi_free_vector(struct pci_dev
* dev
, int vector
, int reassign
)
807 struct msi_desc
*entry
;
808 int head
, entry_nr
, type
;
809 unsigned long base
= 0L;
812 spin_lock_irqsave(&msi_lock
, flags
);
813 entry
= msi_desc
[vector
];
814 if (!entry
|| entry
->dev
!= dev
) {
815 spin_unlock_irqrestore(&msi_lock
, flags
);
818 type
= entry
->msi_attrib
.type
;
819 entry_nr
= entry
->msi_attrib
.entry_nr
;
820 head
= entry
->link
.head
;
821 base
= entry
->mask_base
;
822 msi_desc
[entry
->link
.head
]->link
.tail
= entry
->link
.tail
;
823 msi_desc
[entry
->link
.tail
]->link
.head
= entry
->link
.head
;
826 vector_irq
[vector
] = 0;
827 nr_released_vectors
++;
829 msi_desc
[vector
] = NULL
;
830 spin_unlock_irqrestore(&msi_lock
, flags
);
832 kmem_cache_free(msi_cachep
, entry
);
834 if (type
== PCI_CAP_ID_MSIX
) {
837 entry_nr
* PCI_MSIX_ENTRY_SIZE
+
838 PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET
);
840 if (head
== vector
) {
842 * Detect last MSI-X vector to be released.
843 * Release the MSI-X memory-mapped table.
846 u32 phys_addr
, table_offset
;
850 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
851 pci_read_config_word(dev
, msi_control_reg(pos
),
853 nr_entries
= multi_msix_capable(control
);
854 pci_read_config_dword(dev
, msix_table_offset_reg(pos
),
856 bir
= (u8
)(table_offset
& PCI_MSIX_FLAGS_BIRMASK
);
857 phys_addr
= pci_resource_start (dev
, bir
);
858 phys_addr
+= (u32
)(table_offset
&
859 ~PCI_MSIX_FLAGS_BIRMASK
);
860 iounmap((void*)base
);
861 release_mem_region(phys_addr
,
862 nr_entries
* PCI_MSIX_ENTRY_SIZE
);
869 static int reroute_msix_table(int head
, struct msix_entry
*entries
, int *nvec
)
871 int vector
= head
, tail
= 0;
872 int i
= 0, j
= 0, nr_entries
= 0;
873 unsigned long base
= 0L;
876 spin_lock_irqsave(&msi_lock
, flags
);
877 while (head
!= tail
) {
879 tail
= msi_desc
[vector
]->link
.tail
;
880 if (entries
[0].entry
== msi_desc
[vector
]->msi_attrib
.entry_nr
)
884 if (*nvec
> nr_entries
) {
885 spin_unlock_irqrestore(&msi_lock
, flags
);
889 vector
= ((j
> 0) ? j
: head
);
890 for (i
= 0; i
< *nvec
; i
++) {
891 j
= msi_desc
[vector
]->msi_attrib
.entry_nr
;
892 msi_desc
[vector
]->msi_attrib
.state
= 0; /* Mark it not active */
893 vector_irq
[vector
] = -1; /* Mark it busy */
894 nr_released_vectors
--;
895 entries
[i
].vector
= vector
;
896 if (j
!= (entries
+ i
)->entry
) {
897 base
= msi_desc
[vector
]->mask_base
;
898 msi_desc
[vector
]->msi_attrib
.entry_nr
=
899 (entries
+ i
)->entry
;
900 writel( readl(base
+ j
* PCI_MSIX_ENTRY_SIZE
+
901 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
), base
+
902 (entries
+ i
)->entry
* PCI_MSIX_ENTRY_SIZE
+
903 PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET
);
904 writel( readl(base
+ j
* PCI_MSIX_ENTRY_SIZE
+
905 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
), base
+
906 (entries
+ i
)->entry
* PCI_MSIX_ENTRY_SIZE
+
907 PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET
);
908 writel( (readl(base
+ j
* PCI_MSIX_ENTRY_SIZE
+
909 PCI_MSIX_ENTRY_DATA_OFFSET
) & 0xff00) | vector
,
910 base
+ (entries
+i
)->entry
*PCI_MSIX_ENTRY_SIZE
+
911 PCI_MSIX_ENTRY_DATA_OFFSET
);
913 vector
= msi_desc
[vector
]->link
.tail
;
915 spin_unlock_irqrestore(&msi_lock
, flags
);
921 * pci_enable_msix - configure device's MSI-X capability structure
922 * @dev: pointer to the pci_dev data structure of MSI-X device function
923 * @data: pointer to an array of MSI-X entries
924 * @nvec: number of MSI-X vectors requested for allocation by device driver
926 * Setup the MSI-X capability structure of device function with the number
927 * of requested vectors upon its software driver call to request for
928 * MSI-X mode enabled on its hardware device function. A return of zero
929 * indicates the successful configuration of MSI-X capability structure
930 * with new allocated MSI-X vectors. A return of < 0 indicates a failure.
931 * Or a return of > 0 indicates that driver request is exceeding the number
932 * of vectors available. Driver should use the returned value to re-send
935 int pci_enable_msix(struct pci_dev
* dev
, struct msix_entry
*entries
, int nvec
)
937 int status
, pos
, nr_entries
, free_vectors
;
942 if (!pci_msi_enable
|| !dev
|| !entries
)
945 if ((status
= msi_init()) < 0)
948 if (!(pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
)))
951 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
952 if (control
& PCI_MSIX_FLAGS_ENABLE
)
953 return -EINVAL
; /* Already in MSI-X mode */
955 nr_entries
= multi_msix_capable(control
);
956 if (nvec
> nr_entries
)
959 /* Check for any invalid entries */
960 for (i
= 0; i
< nvec
; i
++) {
961 if (entries
[i
].entry
>= nr_entries
)
962 return -EINVAL
; /* invalid entry */
963 for (j
= i
+ 1; j
< nvec
; j
++) {
964 if (entries
[i
].entry
== entries
[j
].entry
)
965 return -EINVAL
; /* duplicate entry */
969 if (!msi_lookup_vector(dev
, PCI_CAP_ID_MSIX
)) {
972 /* Reroute MSI-X table */
973 if (reroute_msix_table(dev
->irq
, entries
, &nr_entries
)) {
974 /* #requested > #previous-assigned */
979 enable_msi_mode(dev
, pos
, PCI_CAP_ID_MSIX
);
982 /* Check whether driver already requested for MSI vector */
983 if (pci_find_capability(dev
, PCI_CAP_ID_MSI
) > 0 &&
984 !msi_lookup_vector(dev
, PCI_CAP_ID_MSI
)) {
985 printk(KERN_INFO
"Can't enable MSI-X. Device already had MSI vector assigned\n");
990 spin_lock_irqsave(&msi_lock
, flags
);
992 * msi_lock is provided to ensure that enough vectors resources are
993 * available before granting.
995 free_vectors
= pci_vector_resources(last_alloc_vector
,
996 nr_released_vectors
);
997 /* Ensure that each MSI/MSI-X device has one vector reserved by
998 default to avoid any MSI-X driver to take all available
1000 free_vectors
-= nr_reserved_vectors
;
1001 /* Find the average of free vectors among MSI-X devices */
1002 if (nr_msix_devices
> 0)
1003 free_vectors
/= nr_msix_devices
;
1004 spin_unlock_irqrestore(&msi_lock
, flags
);
1006 if (nvec
> free_vectors
) {
1007 if (free_vectors
> 0)
1008 return free_vectors
;
1013 status
= msix_capability_init(dev
, entries
, nvec
);
1014 if (!status
&& nr_msix_devices
> 0)
1020 void pci_disable_msix(struct pci_dev
* dev
)
1025 if (!dev
|| !(pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
)))
1028 pci_read_config_word(dev
, msi_control_reg(pos
), &control
);
1029 if (!(control
& PCI_MSIX_FLAGS_ENABLE
))
1033 if (!msi_lookup_vector(dev
, PCI_CAP_ID_MSIX
)) {
1034 int state
, vector
, head
, tail
= 0, warning
= 0;
1035 unsigned long flags
;
1037 vector
= head
= dev
->irq
;
1038 spin_lock_irqsave(&msi_lock
, flags
);
1039 while (head
!= tail
) {
1040 state
= msi_desc
[vector
]->msi_attrib
.state
;
1044 vector_irq
[vector
] = 0; /* free it */
1045 nr_released_vectors
++;
1047 tail
= msi_desc
[vector
]->link
.tail
;
1050 spin_unlock_irqrestore(&msi_lock
, flags
);
1053 printk(KERN_DEBUG
"Driver[%d:%d:%d] unloaded wo doing free_irq on all vectors\n",
1054 dev
->bus
->number
, PCI_SLOT(dev
->devfn
),
1055 PCI_FUNC(dev
->devfn
));
1056 BUG_ON(warning
> 0);
1059 disable_msi_mode(dev
,
1060 pci_find_capability(dev
, PCI_CAP_ID_MSIX
),
1068 * msi_remove_pci_irq_vectors - reclaim MSI(X) vectors to unused state
1069 * @dev: pointer to the pci_dev data structure of MSI(X) device function
1071 * Being called during hotplug remove, from which the device funciton
1072 * is hot-removed. All previous assigned MSI/MSI-X vectors, if
1073 * allocated for this device function, are reclaimed to unused state,
1074 * which may be used later on.
1076 void msi_remove_pci_irq_vectors(struct pci_dev
* dev
)
1078 int state
, pos
, temp
;
1079 unsigned long flags
;
1081 if (!pci_msi_enable
|| !dev
)
1084 temp
= dev
->irq
; /* Save IOAPIC IRQ */
1085 if ((pos
= pci_find_capability(dev
, PCI_CAP_ID_MSI
)) > 0 &&
1086 !msi_lookup_vector(dev
, PCI_CAP_ID_MSI
)) {
1087 spin_lock_irqsave(&msi_lock
, flags
);
1088 state
= msi_desc
[dev
->irq
]->msi_attrib
.state
;
1089 spin_unlock_irqrestore(&msi_lock
, flags
);
1091 printk(KERN_DEBUG
"Driver[%d:%d:%d] unloaded wo doing free_irq on vector->%d\n",
1092 dev
->bus
->number
, PCI_SLOT(dev
->devfn
),
1093 PCI_FUNC(dev
->devfn
), dev
->irq
);
1095 } else /* Release MSI vector assigned to this device */
1096 msi_free_vector(dev
, dev
->irq
, 0);
1097 dev
->irq
= temp
; /* Restore IOAPIC IRQ */
1099 if ((pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
)) > 0 &&
1100 !msi_lookup_vector(dev
, PCI_CAP_ID_MSIX
)) {
1101 int vector
, head
, tail
= 0, warning
= 0;
1102 unsigned long base
= 0L;
1104 vector
= head
= dev
->irq
;
1105 while (head
!= tail
) {
1106 spin_lock_irqsave(&msi_lock
, flags
);
1107 state
= msi_desc
[vector
]->msi_attrib
.state
;
1108 tail
= msi_desc
[vector
]->link
.tail
;
1109 base
= msi_desc
[vector
]->mask_base
;
1110 spin_unlock_irqrestore(&msi_lock
, flags
);
1113 else if (vector
!= head
) /* Release MSI-X vector */
1114 msi_free_vector(dev
, vector
, 0);
1117 msi_free_vector(dev
, vector
, 0);
1119 /* Force to release the MSI-X memory-mapped table */
1120 u32 phys_addr
, table_offset
;
1124 pci_read_config_word(dev
, msi_control_reg(pos
),
1126 pci_read_config_dword(dev
, msix_table_offset_reg(pos
),
1128 bir
= (u8
)(table_offset
& PCI_MSIX_FLAGS_BIRMASK
);
1129 phys_addr
= pci_resource_start (dev
, bir
);
1130 phys_addr
+= (u32
)(table_offset
&
1131 ~PCI_MSIX_FLAGS_BIRMASK
);
1132 iounmap((void*)base
);
1133 release_mem_region(phys_addr
, PCI_MSIX_ENTRY_SIZE
*
1134 multi_msix_capable(control
));
1135 printk(KERN_DEBUG
"Driver[%d:%d:%d] unloaded wo doing free_irq on all vectors\n",
1136 dev
->bus
->number
, PCI_SLOT(dev
->devfn
),
1137 PCI_FUNC(dev
->devfn
));
1138 BUG_ON(warning
> 0);
1140 dev
->irq
= temp
; /* Restore IOAPIC IRQ */
1144 EXPORT_SYMBOL(pci_enable_msi
);
1145 EXPORT_SYMBOL(pci_disable_msi
);
1146 EXPORT_SYMBOL(pci_enable_msix
);
1147 EXPORT_SYMBOL(pci_disable_msix
);