]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 ([email protected]) | |
7 | */ | |
8 | ||
1ce03373 | 9 | #include <linux/err.h> |
1da177e4 LT |
10 | #include <linux/mm.h> |
11 | #include <linux/irq.h> | |
12 | #include <linux/interrupt.h> | |
13 | #include <linux/init.h> | |
1da177e4 | 14 | #include <linux/ioport.h> |
1da177e4 LT |
15 | #include <linux/pci.h> |
16 | #include <linux/proc_fs.h> | |
3b7d1921 | 17 | #include <linux/msi.h> |
4fdadebc | 18 | #include <linux/smp.h> |
1da177e4 LT |
19 | |
20 | #include <asm/errno.h> | |
21 | #include <asm/io.h> | |
1da177e4 LT |
22 | |
23 | #include "pci.h" | |
24 | #include "msi.h" | |
25 | ||
1da177e4 | 26 | static int pci_msi_enable = 1; |
1da177e4 | 27 | |
6a9e7f20 AB |
28 | /* Arch hooks */ |
29 | ||
30 | int __attribute__ ((weak)) | |
31 | arch_msi_check_device(struct pci_dev *dev, int nvec, int type) | |
32 | { | |
33 | return 0; | |
34 | } | |
35 | ||
36 | int __attribute__ ((weak)) | |
37 | arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *entry) | |
38 | { | |
39 | return 0; | |
40 | } | |
41 | ||
42 | int __attribute__ ((weak)) | |
43 | arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) | |
44 | { | |
45 | struct msi_desc *entry; | |
46 | int ret; | |
47 | ||
48 | list_for_each_entry(entry, &dev->msi_list, list) { | |
49 | ret = arch_setup_msi_irq(dev, entry); | |
50 | if (ret) | |
51 | return ret; | |
52 | } | |
53 | ||
54 | return 0; | |
55 | } | |
56 | ||
57 | void __attribute__ ((weak)) arch_teardown_msi_irq(unsigned int irq) | |
58 | { | |
59 | return; | |
60 | } | |
61 | ||
62 | void __attribute__ ((weak)) | |
63 | arch_teardown_msi_irqs(struct pci_dev *dev) | |
64 | { | |
65 | struct msi_desc *entry; | |
66 | ||
67 | list_for_each_entry(entry, &dev->msi_list, list) { | |
68 | if (entry->irq != 0) | |
69 | arch_teardown_msi_irq(entry->irq); | |
70 | } | |
71 | } | |
72 | ||
b1cbf4e4 EB |
73 | static void msi_set_enable(struct pci_dev *dev, int enable) |
74 | { | |
75 | int pos; | |
76 | u16 control; | |
77 | ||
78 | pos = pci_find_capability(dev, PCI_CAP_ID_MSI); | |
79 | if (pos) { | |
80 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control); | |
81 | control &= ~PCI_MSI_FLAGS_ENABLE; | |
82 | if (enable) | |
83 | control |= PCI_MSI_FLAGS_ENABLE; | |
84 | pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control); | |
85 | } | |
86 | } | |
87 | ||
88 | static void msix_set_enable(struct pci_dev *dev, int enable) | |
89 | { | |
90 | int pos; | |
91 | u16 control; | |
92 | ||
93 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); | |
94 | if (pos) { | |
95 | pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control); | |
96 | control &= ~PCI_MSIX_FLAGS_ENABLE; | |
97 | if (enable) | |
98 | control |= PCI_MSIX_FLAGS_ENABLE; | |
99 | pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control); | |
100 | } | |
101 | } | |
102 | ||
988cbb15 MW |
103 | static void msix_flush_writes(unsigned int irq) |
104 | { | |
105 | struct msi_desc *entry; | |
106 | ||
107 | entry = get_irq_msi(irq); | |
108 | BUG_ON(!entry || !entry->dev); | |
109 | switch (entry->msi_attrib.type) { | |
110 | case PCI_CAP_ID_MSI: | |
111 | /* nothing to do */ | |
112 | break; | |
113 | case PCI_CAP_ID_MSIX: | |
114 | { | |
115 | int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE + | |
116 | PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET; | |
117 | readl(entry->mask_base + offset); | |
118 | break; | |
119 | } | |
120 | default: | |
121 | BUG(); | |
122 | break; | |
123 | } | |
124 | } | |
125 | ||
8e149e09 | 126 | static void msi_set_mask_bits(unsigned int irq, u32 mask, u32 flag) |
1da177e4 LT |
127 | { |
128 | struct msi_desc *entry; | |
129 | ||
5b912c10 | 130 | entry = get_irq_msi(irq); |
277bc33b | 131 | BUG_ON(!entry || !entry->dev); |
1da177e4 LT |
132 | switch (entry->msi_attrib.type) { |
133 | case PCI_CAP_ID_MSI: | |
277bc33b | 134 | if (entry->msi_attrib.maskbit) { |
c54c1879 ST |
135 | int pos; |
136 | u32 mask_bits; | |
277bc33b EB |
137 | |
138 | pos = (long)entry->mask_base; | |
139 | pci_read_config_dword(entry->dev, pos, &mask_bits); | |
8e149e09 YL |
140 | mask_bits &= ~(mask); |
141 | mask_bits |= flag & mask; | |
277bc33b | 142 | pci_write_config_dword(entry->dev, pos, mask_bits); |
58e0543e EB |
143 | } else { |
144 | msi_set_enable(entry->dev, !flag); | |
277bc33b | 145 | } |
1da177e4 | 146 | break; |
1da177e4 LT |
147 | case PCI_CAP_ID_MSIX: |
148 | { | |
149 | int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE + | |
150 | PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET; | |
151 | writel(flag, entry->mask_base + offset); | |
348e3fd1 | 152 | readl(entry->mask_base + offset); |
1da177e4 LT |
153 | break; |
154 | } | |
155 | default: | |
277bc33b | 156 | BUG(); |
1da177e4 LT |
157 | break; |
158 | } | |
392ee1e6 | 159 | entry->msi_attrib.masked = !!flag; |
1da177e4 LT |
160 | } |
161 | ||
3b7d1921 | 162 | void read_msi_msg(unsigned int irq, struct msi_msg *msg) |
1da177e4 | 163 | { |
5b912c10 | 164 | struct msi_desc *entry = get_irq_msi(irq); |
0366f8f7 EB |
165 | switch(entry->msi_attrib.type) { |
166 | case PCI_CAP_ID_MSI: | |
167 | { | |
168 | struct pci_dev *dev = entry->dev; | |
169 | int pos = entry->msi_attrib.pos; | |
170 | u16 data; | |
171 | ||
172 | pci_read_config_dword(dev, msi_lower_address_reg(pos), | |
173 | &msg->address_lo); | |
174 | if (entry->msi_attrib.is_64) { | |
175 | pci_read_config_dword(dev, msi_upper_address_reg(pos), | |
176 | &msg->address_hi); | |
177 | pci_read_config_word(dev, msi_data_reg(pos, 1), &data); | |
178 | } else { | |
179 | msg->address_hi = 0; | |
cbf5d9e6 | 180 | pci_read_config_word(dev, msi_data_reg(pos, 0), &data); |
0366f8f7 EB |
181 | } |
182 | msg->data = data; | |
183 | break; | |
184 | } | |
185 | case PCI_CAP_ID_MSIX: | |
186 | { | |
187 | void __iomem *base; | |
188 | base = entry->mask_base + | |
189 | entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; | |
190 | ||
191 | msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET); | |
192 | msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET); | |
193 | msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET); | |
194 | break; | |
195 | } | |
196 | default: | |
197 | BUG(); | |
198 | } | |
199 | } | |
1da177e4 | 200 | |
3b7d1921 | 201 | void write_msi_msg(unsigned int irq, struct msi_msg *msg) |
0366f8f7 | 202 | { |
5b912c10 | 203 | struct msi_desc *entry = get_irq_msi(irq); |
1da177e4 LT |
204 | switch (entry->msi_attrib.type) { |
205 | case PCI_CAP_ID_MSI: | |
206 | { | |
0366f8f7 EB |
207 | struct pci_dev *dev = entry->dev; |
208 | int pos = entry->msi_attrib.pos; | |
209 | ||
210 | pci_write_config_dword(dev, msi_lower_address_reg(pos), | |
211 | msg->address_lo); | |
212 | if (entry->msi_attrib.is_64) { | |
213 | pci_write_config_dword(dev, msi_upper_address_reg(pos), | |
214 | msg->address_hi); | |
215 | pci_write_config_word(dev, msi_data_reg(pos, 1), | |
216 | msg->data); | |
217 | } else { | |
218 | pci_write_config_word(dev, msi_data_reg(pos, 0), | |
219 | msg->data); | |
220 | } | |
1da177e4 LT |
221 | break; |
222 | } | |
223 | case PCI_CAP_ID_MSIX: | |
224 | { | |
0366f8f7 EB |
225 | void __iomem *base; |
226 | base = entry->mask_base + | |
227 | entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; | |
228 | ||
229 | writel(msg->address_lo, | |
230 | base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET); | |
231 | writel(msg->address_hi, | |
232 | base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET); | |
233 | writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET); | |
1da177e4 LT |
234 | break; |
235 | } | |
236 | default: | |
0366f8f7 | 237 | BUG(); |
1da177e4 | 238 | } |
392ee1e6 | 239 | entry->msg = *msg; |
1da177e4 | 240 | } |
0366f8f7 | 241 | |
3b7d1921 | 242 | void mask_msi_irq(unsigned int irq) |
1da177e4 | 243 | { |
8e149e09 | 244 | msi_set_mask_bits(irq, 1, 1); |
988cbb15 | 245 | msix_flush_writes(irq); |
1da177e4 LT |
246 | } |
247 | ||
3b7d1921 | 248 | void unmask_msi_irq(unsigned int irq) |
1da177e4 | 249 | { |
8e149e09 | 250 | msi_set_mask_bits(irq, 1, 0); |
988cbb15 | 251 | msix_flush_writes(irq); |
1da177e4 LT |
252 | } |
253 | ||
032de8e2 | 254 | static int msi_free_irqs(struct pci_dev* dev); |
c54c1879 | 255 | |
1da177e4 | 256 | |
1da177e4 LT |
257 | static struct msi_desc* alloc_msi_entry(void) |
258 | { | |
259 | struct msi_desc *entry; | |
260 | ||
3e916c05 | 261 | entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL); |
1da177e4 LT |
262 | if (!entry) |
263 | return NULL; | |
264 | ||
4aa9bc95 ME |
265 | INIT_LIST_HEAD(&entry->list); |
266 | entry->irq = 0; | |
1da177e4 LT |
267 | entry->dev = NULL; |
268 | ||
269 | return entry; | |
270 | } | |
271 | ||
ba698ad4 DM |
272 | static void pci_intx_for_msi(struct pci_dev *dev, int enable) |
273 | { | |
274 | if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG)) | |
275 | pci_intx(dev, enable); | |
276 | } | |
277 | ||
8fed4b65 | 278 | static void __pci_restore_msi_state(struct pci_dev *dev) |
41017f0c | 279 | { |
392ee1e6 | 280 | int pos; |
41017f0c | 281 | u16 control; |
392ee1e6 | 282 | struct msi_desc *entry; |
41017f0c | 283 | |
b1cbf4e4 EB |
284 | if (!dev->msi_enabled) |
285 | return; | |
286 | ||
392ee1e6 EB |
287 | entry = get_irq_msi(dev->irq); |
288 | pos = entry->msi_attrib.pos; | |
41017f0c | 289 | |
ba698ad4 | 290 | pci_intx_for_msi(dev, 0); |
b1cbf4e4 | 291 | msi_set_enable(dev, 0); |
392ee1e6 EB |
292 | write_msi_msg(dev->irq, &entry->msg); |
293 | if (entry->msi_attrib.maskbit) | |
8e149e09 YL |
294 | msi_set_mask_bits(dev->irq, entry->msi_attrib.maskbits_mask, |
295 | entry->msi_attrib.masked); | |
392ee1e6 EB |
296 | |
297 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control); | |
298 | control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE); | |
299 | if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked) | |
300 | control |= PCI_MSI_FLAGS_ENABLE; | |
41017f0c | 301 | pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control); |
8fed4b65 ME |
302 | } |
303 | ||
304 | static void __pci_restore_msix_state(struct pci_dev *dev) | |
41017f0c | 305 | { |
41017f0c | 306 | int pos; |
41017f0c | 307 | struct msi_desc *entry; |
392ee1e6 | 308 | u16 control; |
41017f0c | 309 | |
ded86d8d EB |
310 | if (!dev->msix_enabled) |
311 | return; | |
312 | ||
41017f0c | 313 | /* route the table */ |
ba698ad4 | 314 | pci_intx_for_msi(dev, 0); |
b1cbf4e4 | 315 | msix_set_enable(dev, 0); |
41017f0c | 316 | |
4aa9bc95 ME |
317 | list_for_each_entry(entry, &dev->msi_list, list) { |
318 | write_msi_msg(entry->irq, &entry->msg); | |
8e149e09 | 319 | msi_set_mask_bits(entry->irq, 1, entry->msi_attrib.masked); |
41017f0c | 320 | } |
41017f0c | 321 | |
314e77b3 ME |
322 | BUG_ON(list_empty(&dev->msi_list)); |
323 | entry = list_entry(dev->msi_list.next, struct msi_desc, list); | |
4aa9bc95 | 324 | pos = entry->msi_attrib.pos; |
392ee1e6 EB |
325 | pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control); |
326 | control &= ~PCI_MSIX_FLAGS_MASKALL; | |
327 | control |= PCI_MSIX_FLAGS_ENABLE; | |
328 | pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control); | |
41017f0c | 329 | } |
8fed4b65 ME |
330 | |
331 | void pci_restore_msi_state(struct pci_dev *dev) | |
332 | { | |
333 | __pci_restore_msi_state(dev); | |
334 | __pci_restore_msix_state(dev); | |
335 | } | |
94688cf2 | 336 | EXPORT_SYMBOL_GPL(pci_restore_msi_state); |
41017f0c | 337 | |
1da177e4 LT |
338 | /** |
339 | * msi_capability_init - configure device's MSI capability structure | |
340 | * @dev: pointer to the pci_dev data structure of MSI device function | |
341 | * | |
eaae4b3a | 342 | * Setup the MSI capability structure of device function with a single |
1ce03373 | 343 | * MSI irq, regardless of device function is capable of handling |
1da177e4 | 344 | * multiple messages. A return of zero indicates the successful setup |
1ce03373 | 345 | * of an entry zero with the new MSI irq or non-zero for otherwise. |
1da177e4 LT |
346 | **/ |
347 | static int msi_capability_init(struct pci_dev *dev) | |
348 | { | |
349 | struct msi_desc *entry; | |
7fe3730d | 350 | int pos, ret; |
1da177e4 LT |
351 | u16 control; |
352 | ||
b1cbf4e4 EB |
353 | msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */ |
354 | ||
1da177e4 LT |
355 | pos = pci_find_capability(dev, PCI_CAP_ID_MSI); |
356 | pci_read_config_word(dev, msi_control_reg(pos), &control); | |
357 | /* MSI Entry Initialization */ | |
f7feaca7 EB |
358 | entry = alloc_msi_entry(); |
359 | if (!entry) | |
360 | return -ENOMEM; | |
1ce03373 | 361 | |
1da177e4 | 362 | entry->msi_attrib.type = PCI_CAP_ID_MSI; |
0366f8f7 | 363 | entry->msi_attrib.is_64 = is_64bit_address(control); |
1da177e4 LT |
364 | entry->msi_attrib.entry_nr = 0; |
365 | entry->msi_attrib.maskbit = is_mask_bit_support(control); | |
392ee1e6 | 366 | entry->msi_attrib.masked = 1; |
1ce03373 | 367 | entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */ |
0366f8f7 | 368 | entry->msi_attrib.pos = pos; |
1da177e4 LT |
369 | if (is_mask_bit_support(control)) { |
370 | entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos, | |
371 | is_64bit_address(control)); | |
372 | } | |
3b7d1921 EB |
373 | entry->dev = dev; |
374 | if (entry->msi_attrib.maskbit) { | |
375 | unsigned int maskbits, temp; | |
376 | /* All MSIs are unmasked by default, Mask them all */ | |
377 | pci_read_config_dword(dev, | |
378 | msi_mask_bits_reg(pos, is_64bit_address(control)), | |
379 | &maskbits); | |
380 | temp = (1 << multi_msi_capable(control)); | |
381 | temp = ((temp - 1) & ~temp); | |
382 | maskbits |= temp; | |
383 | pci_write_config_dword(dev, | |
384 | msi_mask_bits_reg(pos, is_64bit_address(control)), | |
385 | maskbits); | |
8e149e09 | 386 | entry->msi_attrib.maskbits_mask = temp; |
3b7d1921 | 387 | } |
0dd11f9b | 388 | list_add_tail(&entry->list, &dev->msi_list); |
9c831334 | 389 | |
1da177e4 | 390 | /* Configure MSI capability structure */ |
9c831334 | 391 | ret = arch_setup_msi_irqs(dev, 1, PCI_CAP_ID_MSI); |
7fe3730d | 392 | if (ret) { |
032de8e2 | 393 | msi_free_irqs(dev); |
7fe3730d | 394 | return ret; |
fd58e55f | 395 | } |
f7feaca7 | 396 | |
1da177e4 | 397 | /* Set MSI enabled bits */ |
ba698ad4 | 398 | pci_intx_for_msi(dev, 0); |
b1cbf4e4 EB |
399 | msi_set_enable(dev, 1); |
400 | dev->msi_enabled = 1; | |
1da177e4 | 401 | |
7fe3730d | 402 | dev->irq = entry->irq; |
1da177e4 LT |
403 | return 0; |
404 | } | |
405 | ||
406 | /** | |
407 | * msix_capability_init - configure device's MSI-X capability | |
408 | * @dev: pointer to the pci_dev data structure of MSI-X device function | |
8f7020d3 RD |
409 | * @entries: pointer to an array of struct msix_entry entries |
410 | * @nvec: number of @entries | |
1da177e4 | 411 | * |
eaae4b3a | 412 | * Setup the MSI-X capability structure of device function with a |
1ce03373 EB |
413 | * single MSI-X irq. A return of zero indicates the successful setup of |
414 | * requested MSI-X entries with allocated irqs or non-zero for otherwise. | |
1da177e4 LT |
415 | **/ |
416 | static int msix_capability_init(struct pci_dev *dev, | |
417 | struct msix_entry *entries, int nvec) | |
418 | { | |
4aa9bc95 | 419 | struct msi_desc *entry; |
9c831334 | 420 | int pos, i, j, nr_entries, ret; |
a0454b40 GG |
421 | unsigned long phys_addr; |
422 | u32 table_offset; | |
1da177e4 LT |
423 | u16 control; |
424 | u8 bir; | |
425 | void __iomem *base; | |
426 | ||
b1cbf4e4 EB |
427 | msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */ |
428 | ||
1da177e4 LT |
429 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
430 | /* Request & Map MSI-X table region */ | |
431 | pci_read_config_word(dev, msi_control_reg(pos), &control); | |
432 | nr_entries = multi_msix_capable(control); | |
a0454b40 GG |
433 | |
434 | pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset); | |
1da177e4 | 435 | bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK); |
a0454b40 GG |
436 | table_offset &= ~PCI_MSIX_FLAGS_BIRMASK; |
437 | phys_addr = pci_resource_start (dev, bir) + table_offset; | |
1da177e4 LT |
438 | base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE); |
439 | if (base == NULL) | |
440 | return -ENOMEM; | |
441 | ||
442 | /* MSI-X Table Initialization */ | |
443 | for (i = 0; i < nvec; i++) { | |
f7feaca7 EB |
444 | entry = alloc_msi_entry(); |
445 | if (!entry) | |
1da177e4 | 446 | break; |
1da177e4 LT |
447 | |
448 | j = entries[i].entry; | |
1da177e4 | 449 | entry->msi_attrib.type = PCI_CAP_ID_MSIX; |
0366f8f7 | 450 | entry->msi_attrib.is_64 = 1; |
1da177e4 LT |
451 | entry->msi_attrib.entry_nr = j; |
452 | entry->msi_attrib.maskbit = 1; | |
392ee1e6 | 453 | entry->msi_attrib.masked = 1; |
1ce03373 | 454 | entry->msi_attrib.default_irq = dev->irq; |
0366f8f7 | 455 | entry->msi_attrib.pos = pos; |
1da177e4 LT |
456 | entry->dev = dev; |
457 | entry->mask_base = base; | |
f7feaca7 | 458 | |
0dd11f9b | 459 | list_add_tail(&entry->list, &dev->msi_list); |
1da177e4 | 460 | } |
9c831334 ME |
461 | |
462 | ret = arch_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX); | |
463 | if (ret) { | |
464 | int avail = 0; | |
465 | list_for_each_entry(entry, &dev->msi_list, list) { | |
466 | if (entry->irq != 0) { | |
467 | avail++; | |
9c831334 | 468 | } |
1da177e4 | 469 | } |
9c831334 | 470 | |
032de8e2 ME |
471 | msi_free_irqs(dev); |
472 | ||
92db6d10 EB |
473 | /* If we had some success report the number of irqs |
474 | * we succeeded in setting up. | |
475 | */ | |
9c831334 ME |
476 | if (avail == 0) |
477 | avail = ret; | |
92db6d10 | 478 | return avail; |
1da177e4 | 479 | } |
9c831334 ME |
480 | |
481 | i = 0; | |
482 | list_for_each_entry(entry, &dev->msi_list, list) { | |
483 | entries[i].vector = entry->irq; | |
484 | set_irq_msi(entry->irq, entry); | |
485 | i++; | |
486 | } | |
1da177e4 | 487 | /* Set MSI-X enabled bits */ |
ba698ad4 | 488 | pci_intx_for_msi(dev, 0); |
b1cbf4e4 EB |
489 | msix_set_enable(dev, 1); |
490 | dev->msix_enabled = 1; | |
1da177e4 LT |
491 | |
492 | return 0; | |
493 | } | |
494 | ||
24334a12 | 495 | /** |
17bbc12a | 496 | * pci_msi_check_device - check whether MSI may be enabled on a device |
24334a12 | 497 | * @dev: pointer to the pci_dev data structure of MSI device function |
c9953a73 | 498 | * @nvec: how many MSIs have been requested ? |
b1e2303d | 499 | * @type: are we checking for MSI or MSI-X ? |
24334a12 | 500 | * |
0306ebfa | 501 | * Look at global flags, the device itself, and its parent busses |
17bbc12a ME |
502 | * to determine if MSI/-X are supported for the device. If MSI/-X is |
503 | * supported return 0, else return an error code. | |
24334a12 | 504 | **/ |
c9953a73 | 505 | static int pci_msi_check_device(struct pci_dev* dev, int nvec, int type) |
24334a12 BG |
506 | { |
507 | struct pci_bus *bus; | |
c9953a73 | 508 | int ret; |
24334a12 | 509 | |
0306ebfa | 510 | /* MSI must be globally enabled and supported by the device */ |
24334a12 BG |
511 | if (!pci_msi_enable || !dev || dev->no_msi) |
512 | return -EINVAL; | |
513 | ||
314e77b3 ME |
514 | /* |
515 | * You can't ask to have 0 or less MSIs configured. | |
516 | * a) it's stupid .. | |
517 | * b) the list manipulation code assumes nvec >= 1. | |
518 | */ | |
519 | if (nvec < 1) | |
520 | return -ERANGE; | |
521 | ||
0306ebfa BG |
522 | /* Any bridge which does NOT route MSI transactions from it's |
523 | * secondary bus to it's primary bus must set NO_MSI flag on | |
524 | * the secondary pci_bus. | |
525 | * We expect only arch-specific PCI host bus controller driver | |
526 | * or quirks for specific PCI bridges to be setting NO_MSI. | |
527 | */ | |
24334a12 BG |
528 | for (bus = dev->bus; bus; bus = bus->parent) |
529 | if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI) | |
530 | return -EINVAL; | |
531 | ||
c9953a73 ME |
532 | ret = arch_msi_check_device(dev, nvec, type); |
533 | if (ret) | |
534 | return ret; | |
535 | ||
b1e2303d ME |
536 | if (!pci_find_capability(dev, type)) |
537 | return -EINVAL; | |
538 | ||
24334a12 BG |
539 | return 0; |
540 | } | |
541 | ||
1da177e4 LT |
542 | /** |
543 | * pci_enable_msi - configure device's MSI capability structure | |
544 | * @dev: pointer to the pci_dev data structure of MSI device function | |
545 | * | |
546 | * Setup the MSI capability structure of device function with | |
1ce03373 | 547 | * a single MSI irq upon its software driver call to request for |
1da177e4 LT |
548 | * MSI mode enabled on its hardware device function. A return of zero |
549 | * indicates the successful setup of an entry zero with the new MSI | |
1ce03373 | 550 | * irq or non-zero for otherwise. |
1da177e4 LT |
551 | **/ |
552 | int pci_enable_msi(struct pci_dev* dev) | |
553 | { | |
b1e2303d | 554 | int status; |
1da177e4 | 555 | |
c9953a73 ME |
556 | status = pci_msi_check_device(dev, 1, PCI_CAP_ID_MSI); |
557 | if (status) | |
558 | return status; | |
1da177e4 | 559 | |
ded86d8d | 560 | WARN_ON(!!dev->msi_enabled); |
1da177e4 | 561 | |
1ce03373 | 562 | /* Check whether driver already requested for MSI-X irqs */ |
b1cbf4e4 EB |
563 | if (dev->msix_enabled) { |
564 | printk(KERN_INFO "PCI: %s: Can't enable MSI. " | |
565 | "Device already has MSI-X enabled\n", | |
566 | pci_name(dev)); | |
567 | return -EINVAL; | |
1da177e4 LT |
568 | } |
569 | status = msi_capability_init(dev); | |
1da177e4 LT |
570 | return status; |
571 | } | |
4cc086fa | 572 | EXPORT_SYMBOL(pci_enable_msi); |
1da177e4 | 573 | |
d52877c7 | 574 | void pci_msi_shutdown(struct pci_dev* dev) |
1da177e4 LT |
575 | { |
576 | struct msi_desc *entry; | |
1da177e4 | 577 | |
128bc5fc | 578 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
ded86d8d EB |
579 | return; |
580 | ||
b1cbf4e4 | 581 | msi_set_enable(dev, 0); |
ba698ad4 | 582 | pci_intx_for_msi(dev, 1); |
b1cbf4e4 | 583 | dev->msi_enabled = 0; |
7bd007e4 | 584 | |
314e77b3 ME |
585 | BUG_ON(list_empty(&dev->msi_list)); |
586 | entry = list_entry(dev->msi_list.next, struct msi_desc, list); | |
8e149e09 YL |
587 | /* Return the the pci reset with msi irqs unmasked */ |
588 | if (entry->msi_attrib.maskbit) { | |
589 | u32 mask = entry->msi_attrib.maskbits_mask; | |
590 | msi_set_mask_bits(dev->irq, mask, ~mask); | |
591 | } | |
d52877c7 | 592 | if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) |
1da177e4 | 593 | return; |
e387b9ee ME |
594 | |
595 | /* Restore dev->irq to its default pin-assertion irq */ | |
d52877c7 YL |
596 | dev->irq = entry->msi_attrib.default_irq; |
597 | } | |
598 | void pci_disable_msi(struct pci_dev* dev) | |
599 | { | |
600 | struct msi_desc *entry; | |
601 | ||
602 | if (!pci_msi_enable || !dev || !dev->msi_enabled) | |
603 | return; | |
604 | ||
605 | pci_msi_shutdown(dev); | |
606 | ||
607 | entry = list_entry(dev->msi_list.next, struct msi_desc, list); | |
608 | if (!entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) | |
609 | return; | |
610 | ||
611 | msi_free_irqs(dev); | |
1da177e4 | 612 | } |
4cc086fa | 613 | EXPORT_SYMBOL(pci_disable_msi); |
1da177e4 | 614 | |
032de8e2 | 615 | static int msi_free_irqs(struct pci_dev* dev) |
1da177e4 | 616 | { |
032de8e2 | 617 | struct msi_desc *entry, *tmp; |
7ede9c1f | 618 | |
b3b7cc7b DM |
619 | list_for_each_entry(entry, &dev->msi_list, list) { |
620 | if (entry->irq) | |
621 | BUG_ON(irq_has_action(entry->irq)); | |
622 | } | |
1da177e4 | 623 | |
032de8e2 | 624 | arch_teardown_msi_irqs(dev); |
1da177e4 | 625 | |
032de8e2 ME |
626 | list_for_each_entry_safe(entry, tmp, &dev->msi_list, list) { |
627 | if (entry->msi_attrib.type == PCI_CAP_ID_MSIX) { | |
032de8e2 ME |
628 | writel(1, entry->mask_base + entry->msi_attrib.entry_nr |
629 | * PCI_MSIX_ENTRY_SIZE | |
630 | + PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET); | |
78b7611c EB |
631 | |
632 | if (list_is_last(&entry->list, &dev->msi_list)) | |
633 | iounmap(entry->mask_base); | |
032de8e2 ME |
634 | } |
635 | list_del(&entry->list); | |
636 | kfree(entry); | |
1da177e4 LT |
637 | } |
638 | ||
639 | return 0; | |
640 | } | |
641 | ||
1da177e4 LT |
642 | /** |
643 | * pci_enable_msix - configure device's MSI-X capability structure | |
644 | * @dev: pointer to the pci_dev data structure of MSI-X device function | |
70549ad9 | 645 | * @entries: pointer to an array of MSI-X entries |
1ce03373 | 646 | * @nvec: number of MSI-X irqs requested for allocation by device driver |
1da177e4 LT |
647 | * |
648 | * Setup the MSI-X capability structure of device function with the number | |
1ce03373 | 649 | * of requested irqs upon its software driver call to request for |
1da177e4 LT |
650 | * MSI-X mode enabled on its hardware device function. A return of zero |
651 | * indicates the successful configuration of MSI-X capability structure | |
1ce03373 | 652 | * with new allocated MSI-X irqs. A return of < 0 indicates a failure. |
1da177e4 | 653 | * Or a return of > 0 indicates that driver request is exceeding the number |
1ce03373 | 654 | * of irqs available. Driver should use the returned value to re-send |
1da177e4 LT |
655 | * its request. |
656 | **/ | |
657 | int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec) | |
658 | { | |
92db6d10 | 659 | int status, pos, nr_entries; |
ded86d8d | 660 | int i, j; |
1da177e4 | 661 | u16 control; |
1da177e4 | 662 | |
c9953a73 | 663 | if (!entries) |
1da177e4 LT |
664 | return -EINVAL; |
665 | ||
c9953a73 ME |
666 | status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX); |
667 | if (status) | |
668 | return status; | |
669 | ||
b64c05e7 | 670 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
1da177e4 | 671 | pci_read_config_word(dev, msi_control_reg(pos), &control); |
1da177e4 LT |
672 | nr_entries = multi_msix_capable(control); |
673 | if (nvec > nr_entries) | |
674 | return -EINVAL; | |
675 | ||
676 | /* Check for any invalid entries */ | |
677 | for (i = 0; i < nvec; i++) { | |
678 | if (entries[i].entry >= nr_entries) | |
679 | return -EINVAL; /* invalid entry */ | |
680 | for (j = i + 1; j < nvec; j++) { | |
681 | if (entries[i].entry == entries[j].entry) | |
682 | return -EINVAL; /* duplicate entry */ | |
683 | } | |
684 | } | |
ded86d8d | 685 | WARN_ON(!!dev->msix_enabled); |
7bd007e4 | 686 | |
1ce03373 | 687 | /* Check whether driver already requested for MSI irq */ |
b1cbf4e4 | 688 | if (dev->msi_enabled) { |
1da177e4 | 689 | printk(KERN_INFO "PCI: %s: Can't enable MSI-X. " |
1ce03373 | 690 | "Device already has an MSI irq assigned\n", |
1da177e4 | 691 | pci_name(dev)); |
1da177e4 LT |
692 | return -EINVAL; |
693 | } | |
1da177e4 | 694 | status = msix_capability_init(dev, entries, nvec); |
1da177e4 LT |
695 | return status; |
696 | } | |
4cc086fa | 697 | EXPORT_SYMBOL(pci_enable_msix); |
1da177e4 | 698 | |
fc4afc7b | 699 | static void msix_free_all_irqs(struct pci_dev *dev) |
1da177e4 | 700 | { |
032de8e2 | 701 | msi_free_irqs(dev); |
fc4afc7b ME |
702 | } |
703 | ||
d52877c7 | 704 | void pci_msix_shutdown(struct pci_dev* dev) |
fc4afc7b | 705 | { |
128bc5fc | 706 | if (!pci_msi_enable || !dev || !dev->msix_enabled) |
ded86d8d EB |
707 | return; |
708 | ||
b1cbf4e4 | 709 | msix_set_enable(dev, 0); |
ba698ad4 | 710 | pci_intx_for_msi(dev, 1); |
b1cbf4e4 | 711 | dev->msix_enabled = 0; |
d52877c7 YL |
712 | } |
713 | void pci_disable_msix(struct pci_dev* dev) | |
714 | { | |
715 | if (!pci_msi_enable || !dev || !dev->msix_enabled) | |
716 | return; | |
717 | ||
718 | pci_msix_shutdown(dev); | |
7bd007e4 | 719 | |
fc4afc7b | 720 | msix_free_all_irqs(dev); |
1da177e4 | 721 | } |
4cc086fa | 722 | EXPORT_SYMBOL(pci_disable_msix); |
1da177e4 LT |
723 | |
724 | /** | |
1ce03373 | 725 | * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state |
1da177e4 LT |
726 | * @dev: pointer to the pci_dev data structure of MSI(X) device function |
727 | * | |
eaae4b3a | 728 | * Being called during hotplug remove, from which the device function |
1ce03373 | 729 | * is hot-removed. All previous assigned MSI/MSI-X irqs, if |
1da177e4 LT |
730 | * allocated for this device function, are reclaimed to unused state, |
731 | * which may be used later on. | |
732 | **/ | |
733 | void msi_remove_pci_irq_vectors(struct pci_dev* dev) | |
734 | { | |
1da177e4 LT |
735 | if (!pci_msi_enable || !dev) |
736 | return; | |
737 | ||
032de8e2 ME |
738 | if (dev->msi_enabled) |
739 | msi_free_irqs(dev); | |
1da177e4 | 740 | |
fc4afc7b ME |
741 | if (dev->msix_enabled) |
742 | msix_free_all_irqs(dev); | |
1da177e4 LT |
743 | } |
744 | ||
309e57df MW |
745 | void pci_no_msi(void) |
746 | { | |
747 | pci_msi_enable = 0; | |
748 | } | |
c9953a73 | 749 | |
4aa9bc95 ME |
750 | void pci_msi_init_pci_dev(struct pci_dev *dev) |
751 | { | |
752 | INIT_LIST_HEAD(&dev->msi_list); | |
753 | } |