]>
Commit | Line | Data |
---|---|---|
7328c8f4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
df62ab5e | 3 | * PCI Message Signaled Interrupt (MSI) |
1da177e4 LT |
4 | * |
5 | * Copyright (C) 2003-2004 Intel | |
6 | * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com) | |
aff17164 | 7 | * Copyright (C) 2016 Christoph Hellwig. |
1da177e4 LT |
8 | */ |
9 | ||
1ce03373 | 10 | #include <linux/err.h> |
1da177e4 LT |
11 | #include <linux/mm.h> |
12 | #include <linux/irq.h> | |
13 | #include <linux/interrupt.h> | |
363c75db | 14 | #include <linux/export.h> |
1da177e4 | 15 | #include <linux/ioport.h> |
1da177e4 LT |
16 | #include <linux/pci.h> |
17 | #include <linux/proc_fs.h> | |
3b7d1921 | 18 | #include <linux/msi.h> |
4fdadebc | 19 | #include <linux/smp.h> |
500559a9 HS |
20 | #include <linux/errno.h> |
21 | #include <linux/io.h> | |
be2021ba | 22 | #include <linux/acpi_iort.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
3878eaef | 24 | #include <linux/irqdomain.h> |
b6eec9b7 | 25 | #include <linux/of_irq.h> |
1da177e4 LT |
26 | |
27 | #include "pci.h" | |
1da177e4 | 28 | |
1da177e4 | 29 | static int pci_msi_enable = 1; |
38737d82 | 30 | int pci_msi_ignore_mask; |
1da177e4 | 31 | |
527eee29 BH |
32 | #define msix_table_size(flags) ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) |
33 | ||
8e047ada | 34 | #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN |
8e047ada JL |
35 | static int pci_msi_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
36 | { | |
37 | struct irq_domain *domain; | |
38 | ||
47feb418 | 39 | domain = dev_get_msi_domain(&dev->dev); |
3845d295 | 40 | if (domain && irq_domain_is_hierarchy(domain)) |
699c4cec | 41 | return msi_domain_alloc_irqs(domain, &dev->dev, nvec); |
8e047ada JL |
42 | |
43 | return arch_setup_msi_irqs(dev, nvec, type); | |
44 | } | |
45 | ||
46 | static void pci_msi_teardown_msi_irqs(struct pci_dev *dev) | |
47 | { | |
48 | struct irq_domain *domain; | |
49 | ||
47feb418 | 50 | domain = dev_get_msi_domain(&dev->dev); |
3845d295 | 51 | if (domain && irq_domain_is_hierarchy(domain)) |
699c4cec | 52 | msi_domain_free_irqs(domain, &dev->dev); |
8e047ada JL |
53 | else |
54 | arch_teardown_msi_irqs(dev); | |
55 | } | |
56 | #else | |
57 | #define pci_msi_setup_msi_irqs arch_setup_msi_irqs | |
58 | #define pci_msi_teardown_msi_irqs arch_teardown_msi_irqs | |
59 | #endif | |
527eee29 | 60 | |
6a9e7f20 AB |
61 | /* Arch hooks */ |
62 | ||
4287d824 TP |
63 | int __weak arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc) |
64 | { | |
2291ec09 | 65 | struct msi_controller *chip = dev->bus->msi; |
0cbdcfcf TR |
66 | int err; |
67 | ||
68 | if (!chip || !chip->setup_irq) | |
69 | return -EINVAL; | |
70 | ||
71 | err = chip->setup_irq(chip, dev, desc); | |
72 | if (err < 0) | |
73 | return err; | |
74 | ||
75 | irq_set_chip_data(desc->irq, chip); | |
76 | ||
77 | return 0; | |
4287d824 TP |
78 | } |
79 | ||
80 | void __weak arch_teardown_msi_irq(unsigned int irq) | |
6a9e7f20 | 81 | { |
c2791b80 | 82 | struct msi_controller *chip = irq_get_chip_data(irq); |
0cbdcfcf TR |
83 | |
84 | if (!chip || !chip->teardown_irq) | |
85 | return; | |
86 | ||
87 | chip->teardown_irq(chip, irq); | |
6a9e7f20 AB |
88 | } |
89 | ||
4287d824 | 90 | int __weak arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
6a9e7f20 | 91 | { |
339e5b44 | 92 | struct msi_controller *chip = dev->bus->msi; |
6a9e7f20 AB |
93 | struct msi_desc *entry; |
94 | int ret; | |
95 | ||
339e5b44 LS |
96 | if (chip && chip->setup_irqs) |
97 | return chip->setup_irqs(chip, dev, nvec, type); | |
1c8d7b0a MW |
98 | /* |
99 | * If an architecture wants to support multiple MSI, it needs to | |
100 | * override arch_setup_msi_irqs() | |
101 | */ | |
102 | if (type == PCI_CAP_ID_MSI && nvec > 1) | |
103 | return 1; | |
104 | ||
5004e98a | 105 | for_each_pci_msi_entry(entry, dev) { |
6a9e7f20 | 106 | ret = arch_setup_msi_irq(dev, entry); |
b5fbf533 | 107 | if (ret < 0) |
6a9e7f20 | 108 | return ret; |
b5fbf533 ME |
109 | if (ret > 0) |
110 | return -ENOSPC; | |
6a9e7f20 AB |
111 | } |
112 | ||
113 | return 0; | |
114 | } | |
1525bf0d | 115 | |
4287d824 TP |
116 | /* |
117 | * We have a default implementation available as a separate non-weak | |
118 | * function, as it is used by the Xen x86 PCI code | |
119 | */ | |
1525bf0d | 120 | void default_teardown_msi_irqs(struct pci_dev *dev) |
6a9e7f20 | 121 | { |
63a7b17e | 122 | int i; |
6a9e7f20 AB |
123 | struct msi_desc *entry; |
124 | ||
5004e98a | 125 | for_each_pci_msi_entry(entry, dev) |
63a7b17e JL |
126 | if (entry->irq) |
127 | for (i = 0; i < entry->nvec_used; i++) | |
128 | arch_teardown_msi_irq(entry->irq + i); | |
6a9e7f20 AB |
129 | } |
130 | ||
4287d824 TP |
131 | void __weak arch_teardown_msi_irqs(struct pci_dev *dev) |
132 | { | |
133 | return default_teardown_msi_irqs(dev); | |
134 | } | |
76ccc297 | 135 | |
ac8344c4 | 136 | static void default_restore_msi_irq(struct pci_dev *dev, int irq) |
76ccc297 KRW |
137 | { |
138 | struct msi_desc *entry; | |
139 | ||
140 | entry = NULL; | |
141 | if (dev->msix_enabled) { | |
5004e98a | 142 | for_each_pci_msi_entry(entry, dev) { |
76ccc297 KRW |
143 | if (irq == entry->irq) |
144 | break; | |
145 | } | |
146 | } else if (dev->msi_enabled) { | |
147 | entry = irq_get_msi_desc(irq); | |
148 | } | |
149 | ||
150 | if (entry) | |
83a18912 | 151 | __pci_write_msi_msg(entry, &entry->msg); |
76ccc297 | 152 | } |
4287d824 | 153 | |
ac8344c4 | 154 | void __weak arch_restore_msi_irqs(struct pci_dev *dev) |
4287d824 | 155 | { |
ac8344c4 | 156 | return default_restore_msi_irqs(dev); |
4287d824 | 157 | } |
76ccc297 | 158 | |
bffac3c5 MW |
159 | static inline __attribute_const__ u32 msi_mask(unsigned x) |
160 | { | |
0b49ec37 MW |
161 | /* Don't shift by >= width of type */ |
162 | if (x >= 5) | |
163 | return 0xffffffff; | |
164 | return (1 << (1 << x)) - 1; | |
bffac3c5 MW |
165 | } |
166 | ||
ce6fce42 MW |
167 | /* |
168 | * PCI 2.3 does not specify mask bits for each MSI interrupt. Attempting to | |
169 | * mask all MSI interrupts by clearing the MSI enable bit does not work | |
170 | * reliably as devices without an INTx disable bit will then generate a | |
171 | * level IRQ which will never be cleared. | |
ce6fce42 | 172 | */ |
23ed8d57 | 173 | u32 __pci_msi_desc_mask_irq(struct msi_desc *desc, u32 mask, u32 flag) |
1da177e4 | 174 | { |
f2440d9a | 175 | u32 mask_bits = desc->masked; |
1da177e4 | 176 | |
38737d82 | 177 | if (pci_msi_ignore_mask || !desc->msi_attrib.maskbit) |
12abb8ba | 178 | return 0; |
f2440d9a MW |
179 | |
180 | mask_bits &= ~mask; | |
181 | mask_bits |= flag; | |
e39758e0 JL |
182 | pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->mask_pos, |
183 | mask_bits); | |
12abb8ba HS |
184 | |
185 | return mask_bits; | |
186 | } | |
187 | ||
188 | static void msi_mask_irq(struct msi_desc *desc, u32 mask, u32 flag) | |
189 | { | |
23ed8d57 | 190 | desc->masked = __pci_msi_desc_mask_irq(desc, mask, flag); |
f2440d9a MW |
191 | } |
192 | ||
5eb6d660 CH |
193 | static void __iomem *pci_msix_desc_addr(struct msi_desc *desc) |
194 | { | |
d7cc609f LG |
195 | if (desc->msi_attrib.is_virtual) |
196 | return NULL; | |
197 | ||
5eb6d660 CH |
198 | return desc->mask_base + |
199 | desc->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE; | |
200 | } | |
201 | ||
f2440d9a MW |
202 | /* |
203 | * This internal function does not flush PCI writes to the device. | |
204 | * All users must ensure that they read from the device before either | |
205 | * assuming that the device state is up to date, or returning out of this | |
206 | * file. This saves a few milliseconds when initialising devices with lots | |
207 | * of MSI-X interrupts. | |
208 | */ | |
23ed8d57 | 209 | u32 __pci_msix_desc_mask_irq(struct msi_desc *desc, u32 flag) |
f2440d9a MW |
210 | { |
211 | u32 mask_bits = desc->masked; | |
d7cc609f | 212 | void __iomem *desc_addr; |
38737d82 YW |
213 | |
214 | if (pci_msi_ignore_mask) | |
215 | return 0; | |
d7cc609f LG |
216 | desc_addr = pci_msix_desc_addr(desc); |
217 | if (!desc_addr) | |
218 | return 0; | |
38737d82 | 219 | |
8d805286 SY |
220 | mask_bits &= ~PCI_MSIX_ENTRY_CTRL_MASKBIT; |
221 | if (flag) | |
222 | mask_bits |= PCI_MSIX_ENTRY_CTRL_MASKBIT; | |
d7cc609f LG |
223 | |
224 | writel(mask_bits, desc_addr + PCI_MSIX_ENTRY_VECTOR_CTRL); | |
12abb8ba HS |
225 | |
226 | return mask_bits; | |
227 | } | |
228 | ||
229 | static void msix_mask_irq(struct msi_desc *desc, u32 flag) | |
230 | { | |
23ed8d57 | 231 | desc->masked = __pci_msix_desc_mask_irq(desc, flag); |
f2440d9a | 232 | } |
24d27553 | 233 | |
1c9db525 | 234 | static void msi_set_mask_bit(struct irq_data *data, u32 flag) |
f2440d9a | 235 | { |
c391f262 | 236 | struct msi_desc *desc = irq_data_get_msi_desc(data); |
24d27553 | 237 | |
f2440d9a MW |
238 | if (desc->msi_attrib.is_msix) { |
239 | msix_mask_irq(desc, flag); | |
240 | readl(desc->mask_base); /* Flush write to device */ | |
241 | } else { | |
a281b788 | 242 | unsigned offset = data->irq - desc->irq; |
1c8d7b0a | 243 | msi_mask_irq(desc, 1 << offset, flag << offset); |
1da177e4 | 244 | } |
f2440d9a MW |
245 | } |
246 | ||
23ed8d57 | 247 | /** |
f6b6aefe | 248 | * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts |
23ed8d57 TG |
249 | * @data: pointer to irqdata associated to that interrupt |
250 | */ | |
251 | void pci_msi_mask_irq(struct irq_data *data) | |
f2440d9a | 252 | { |
1c9db525 | 253 | msi_set_mask_bit(data, 1); |
f2440d9a | 254 | } |
a4289dc2 | 255 | EXPORT_SYMBOL_GPL(pci_msi_mask_irq); |
f2440d9a | 256 | |
23ed8d57 | 257 | /** |
f6b6aefe | 258 | * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts |
23ed8d57 TG |
259 | * @data: pointer to irqdata associated to that interrupt |
260 | */ | |
261 | void pci_msi_unmask_irq(struct irq_data *data) | |
f2440d9a | 262 | { |
1c9db525 | 263 | msi_set_mask_bit(data, 0); |
1da177e4 | 264 | } |
a4289dc2 | 265 | EXPORT_SYMBOL_GPL(pci_msi_unmask_irq); |
1da177e4 | 266 | |
ac8344c4 D |
267 | void default_restore_msi_irqs(struct pci_dev *dev) |
268 | { | |
269 | struct msi_desc *entry; | |
270 | ||
5004e98a | 271 | for_each_pci_msi_entry(entry, dev) |
ac8344c4 | 272 | default_restore_msi_irq(dev, entry->irq); |
ac8344c4 D |
273 | } |
274 | ||
891d4a48 | 275 | void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg) |
1da177e4 | 276 | { |
e39758e0 JL |
277 | struct pci_dev *dev = msi_desc_to_pci_dev(entry); |
278 | ||
279 | BUG_ON(dev->current_state != PCI_D0); | |
30da5524 BH |
280 | |
281 | if (entry->msi_attrib.is_msix) { | |
5eb6d660 | 282 | void __iomem *base = pci_msix_desc_addr(entry); |
30da5524 | 283 | |
d7cc609f LG |
284 | if (!base) { |
285 | WARN_ON(1); | |
286 | return; | |
287 | } | |
288 | ||
30da5524 BH |
289 | msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR); |
290 | msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR); | |
291 | msg->data = readl(base + PCI_MSIX_ENTRY_DATA); | |
292 | } else { | |
f5322169 | 293 | int pos = dev->msi_cap; |
30da5524 BH |
294 | u16 data; |
295 | ||
9925ad0c BH |
296 | pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, |
297 | &msg->address_lo); | |
30da5524 | 298 | if (entry->msi_attrib.is_64) { |
9925ad0c BH |
299 | pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, |
300 | &msg->address_hi); | |
2f221349 | 301 | pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data); |
30da5524 BH |
302 | } else { |
303 | msg->address_hi = 0; | |
2f221349 | 304 | pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data); |
30da5524 BH |
305 | } |
306 | msg->data = data; | |
307 | } | |
308 | } | |
309 | ||
83a18912 | 310 | void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg) |
3145e941 | 311 | { |
e39758e0 JL |
312 | struct pci_dev *dev = msi_desc_to_pci_dev(entry); |
313 | ||
0170591b | 314 | if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) { |
fcd097f3 BH |
315 | /* Don't touch the hardware now */ |
316 | } else if (entry->msi_attrib.is_msix) { | |
5eb6d660 | 317 | void __iomem *base = pci_msix_desc_addr(entry); |
24d27553 | 318 | |
d7cc609f LG |
319 | if (!base) |
320 | goto skip; | |
321 | ||
2c21fd4b HS |
322 | writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR); |
323 | writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR); | |
324 | writel(msg->data, base + PCI_MSIX_ENTRY_DATA); | |
24d27553 | 325 | } else { |
f5322169 | 326 | int pos = dev->msi_cap; |
1c8d7b0a MW |
327 | u16 msgctl; |
328 | ||
f84ecd28 | 329 | pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl); |
1c8d7b0a MW |
330 | msgctl &= ~PCI_MSI_FLAGS_QSIZE; |
331 | msgctl |= entry->msi_attrib.multiple << 4; | |
f84ecd28 | 332 | pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl); |
0366f8f7 | 333 | |
9925ad0c BH |
334 | pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, |
335 | msg->address_lo); | |
0366f8f7 | 336 | if (entry->msi_attrib.is_64) { |
9925ad0c BH |
337 | pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI, |
338 | msg->address_hi); | |
2f221349 BH |
339 | pci_write_config_word(dev, pos + PCI_MSI_DATA_64, |
340 | msg->data); | |
0366f8f7 | 341 | } else { |
2f221349 BH |
342 | pci_write_config_word(dev, pos + PCI_MSI_DATA_32, |
343 | msg->data); | |
0366f8f7 | 344 | } |
1da177e4 | 345 | } |
d7cc609f LG |
346 | |
347 | skip: | |
392ee1e6 | 348 | entry->msg = *msg; |
d7cc609f LG |
349 | |
350 | if (entry->write_msi_msg) | |
351 | entry->write_msi_msg(entry, entry->write_msi_msg_data); | |
352 | ||
1da177e4 | 353 | } |
0366f8f7 | 354 | |
83a18912 | 355 | void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg) |
3145e941 | 356 | { |
dced35ae | 357 | struct msi_desc *entry = irq_get_msi_desc(irq); |
3145e941 | 358 | |
83a18912 | 359 | __pci_write_msi_msg(entry, msg); |
3145e941 | 360 | } |
83a18912 | 361 | EXPORT_SYMBOL_GPL(pci_write_msi_msg); |
3145e941 | 362 | |
f56e4481 HS |
363 | static void free_msi_irqs(struct pci_dev *dev) |
364 | { | |
5004e98a | 365 | struct list_head *msi_list = dev_to_msi_list(&dev->dev); |
f56e4481 | 366 | struct msi_desc *entry, *tmp; |
1c51b50c GKH |
367 | struct attribute **msi_attrs; |
368 | struct device_attribute *dev_attr; | |
63a7b17e | 369 | int i, count = 0; |
f56e4481 | 370 | |
5004e98a | 371 | for_each_pci_msi_entry(entry, dev) |
63a7b17e JL |
372 | if (entry->irq) |
373 | for (i = 0; i < entry->nvec_used; i++) | |
374 | BUG_ON(irq_has_action(entry->irq + i)); | |
f56e4481 | 375 | |
8e047ada | 376 | pci_msi_teardown_msi_irqs(dev); |
f56e4481 | 377 | |
5004e98a | 378 | list_for_each_entry_safe(entry, tmp, msi_list, list) { |
f56e4481 | 379 | if (entry->msi_attrib.is_msix) { |
5004e98a | 380 | if (list_is_last(&entry->list, msi_list)) |
f56e4481 HS |
381 | iounmap(entry->mask_base); |
382 | } | |
424eb391 | 383 | |
f56e4481 | 384 | list_del(&entry->list); |
81efbadd | 385 | free_msi_entry(entry); |
f56e4481 | 386 | } |
1c51b50c GKH |
387 | |
388 | if (dev->msi_irq_groups) { | |
389 | sysfs_remove_groups(&dev->dev.kobj, dev->msi_irq_groups); | |
390 | msi_attrs = dev->msi_irq_groups[0]->attrs; | |
b701c0b1 | 391 | while (msi_attrs[count]) { |
1c51b50c GKH |
392 | dev_attr = container_of(msi_attrs[count], |
393 | struct device_attribute, attr); | |
394 | kfree(dev_attr->attr.name); | |
395 | kfree(dev_attr); | |
396 | ++count; | |
397 | } | |
398 | kfree(msi_attrs); | |
399 | kfree(dev->msi_irq_groups[0]); | |
400 | kfree(dev->msi_irq_groups); | |
401 | dev->msi_irq_groups = NULL; | |
402 | } | |
f56e4481 | 403 | } |
c54c1879 | 404 | |
ba698ad4 DM |
405 | static void pci_intx_for_msi(struct pci_dev *dev, int enable) |
406 | { | |
407 | if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG)) | |
408 | pci_intx(dev, enable); | |
409 | } | |
410 | ||
8fed4b65 | 411 | static void __pci_restore_msi_state(struct pci_dev *dev) |
41017f0c | 412 | { |
41017f0c | 413 | u16 control; |
392ee1e6 | 414 | struct msi_desc *entry; |
41017f0c | 415 | |
b1cbf4e4 EB |
416 | if (!dev->msi_enabled) |
417 | return; | |
418 | ||
dced35ae | 419 | entry = irq_get_msi_desc(dev->irq); |
41017f0c | 420 | |
ba698ad4 | 421 | pci_intx_for_msi(dev, 0); |
61b64abd | 422 | pci_msi_set_enable(dev, 0); |
ac8344c4 | 423 | arch_restore_msi_irqs(dev); |
392ee1e6 | 424 | |
f5322169 | 425 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control); |
31ea5d4d YW |
426 | msi_mask_irq(entry, msi_mask(entry->msi_attrib.multi_cap), |
427 | entry->masked); | |
abad2ec9 | 428 | control &= ~PCI_MSI_FLAGS_QSIZE; |
1c8d7b0a | 429 | control |= (entry->msi_attrib.multiple << 4) | PCI_MSI_FLAGS_ENABLE; |
f5322169 | 430 | pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control); |
8fed4b65 ME |
431 | } |
432 | ||
433 | static void __pci_restore_msix_state(struct pci_dev *dev) | |
41017f0c | 434 | { |
41017f0c | 435 | struct msi_desc *entry; |
41017f0c | 436 | |
ded86d8d EB |
437 | if (!dev->msix_enabled) |
438 | return; | |
5004e98a | 439 | BUG_ON(list_empty(dev_to_msi_list(&dev->dev))); |
ded86d8d | 440 | |
41017f0c | 441 | /* route the table */ |
ba698ad4 | 442 | pci_intx_for_msi(dev, 0); |
61b64abd | 443 | pci_msix_clear_and_set_ctrl(dev, 0, |
66f0d0c4 | 444 | PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL); |
41017f0c | 445 | |
ac8344c4 | 446 | arch_restore_msi_irqs(dev); |
5004e98a | 447 | for_each_pci_msi_entry(entry, dev) |
f2440d9a | 448 | msix_mask_irq(entry, entry->masked); |
41017f0c | 449 | |
61b64abd | 450 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0); |
41017f0c | 451 | } |
8fed4b65 ME |
452 | |
453 | void pci_restore_msi_state(struct pci_dev *dev) | |
454 | { | |
455 | __pci_restore_msi_state(dev); | |
456 | __pci_restore_msix_state(dev); | |
457 | } | |
94688cf2 | 458 | EXPORT_SYMBOL_GPL(pci_restore_msi_state); |
41017f0c | 459 | |
1c51b50c | 460 | static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr, |
da8d1c8b NH |
461 | char *buf) |
462 | { | |
1c51b50c GKH |
463 | struct msi_desc *entry; |
464 | unsigned long irq; | |
465 | int retval; | |
da8d1c8b | 466 | |
1c51b50c GKH |
467 | retval = kstrtoul(attr->attr.name, 10, &irq); |
468 | if (retval) | |
469 | return retval; | |
da8d1c8b | 470 | |
e11ece5a YW |
471 | entry = irq_get_msi_desc(irq); |
472 | if (entry) | |
473 | return sprintf(buf, "%s\n", | |
474 | entry->msi_attrib.is_msix ? "msix" : "msi"); | |
475 | ||
1c51b50c | 476 | return -ENODEV; |
da8d1c8b NH |
477 | } |
478 | ||
da8d1c8b NH |
479 | static int populate_msi_sysfs(struct pci_dev *pdev) |
480 | { | |
1c51b50c GKH |
481 | struct attribute **msi_attrs; |
482 | struct attribute *msi_attr; | |
483 | struct device_attribute *msi_dev_attr; | |
484 | struct attribute_group *msi_irq_group; | |
485 | const struct attribute_group **msi_irq_groups; | |
da8d1c8b | 486 | struct msi_desc *entry; |
1c51b50c GKH |
487 | int ret = -ENOMEM; |
488 | int num_msi = 0; | |
da8d1c8b | 489 | int count = 0; |
a8676066 | 490 | int i; |
da8d1c8b | 491 | |
1c51b50c | 492 | /* Determine how many msi entries we have */ |
5004e98a | 493 | for_each_pci_msi_entry(entry, pdev) |
a8676066 | 494 | num_msi += entry->nvec_used; |
1c51b50c GKH |
495 | if (!num_msi) |
496 | return 0; | |
da8d1c8b | 497 | |
1c51b50c | 498 | /* Dynamically create the MSI attributes for the PCI device */ |
6396bb22 | 499 | msi_attrs = kcalloc(num_msi + 1, sizeof(void *), GFP_KERNEL); |
1c51b50c GKH |
500 | if (!msi_attrs) |
501 | return -ENOMEM; | |
5004e98a | 502 | for_each_pci_msi_entry(entry, pdev) { |
a8676066 RB |
503 | for (i = 0; i < entry->nvec_used; i++) { |
504 | msi_dev_attr = kzalloc(sizeof(*msi_dev_attr), GFP_KERNEL); | |
505 | if (!msi_dev_attr) | |
506 | goto error_attrs; | |
507 | msi_attrs[count] = &msi_dev_attr->attr; | |
508 | ||
509 | sysfs_attr_init(&msi_dev_attr->attr); | |
510 | msi_dev_attr->attr.name = kasprintf(GFP_KERNEL, "%d", | |
511 | entry->irq + i); | |
512 | if (!msi_dev_attr->attr.name) | |
513 | goto error_attrs; | |
514 | msi_dev_attr->attr.mode = S_IRUGO; | |
515 | msi_dev_attr->show = msi_mode_show; | |
516 | ++count; | |
517 | } | |
da8d1c8b NH |
518 | } |
519 | ||
1c51b50c GKH |
520 | msi_irq_group = kzalloc(sizeof(*msi_irq_group), GFP_KERNEL); |
521 | if (!msi_irq_group) | |
522 | goto error_attrs; | |
523 | msi_irq_group->name = "msi_irqs"; | |
524 | msi_irq_group->attrs = msi_attrs; | |
525 | ||
6396bb22 | 526 | msi_irq_groups = kcalloc(2, sizeof(void *), GFP_KERNEL); |
1c51b50c GKH |
527 | if (!msi_irq_groups) |
528 | goto error_irq_group; | |
529 | msi_irq_groups[0] = msi_irq_group; | |
530 | ||
531 | ret = sysfs_create_groups(&pdev->dev.kobj, msi_irq_groups); | |
532 | if (ret) | |
533 | goto error_irq_groups; | |
534 | pdev->msi_irq_groups = msi_irq_groups; | |
535 | ||
da8d1c8b NH |
536 | return 0; |
537 | ||
1c51b50c GKH |
538 | error_irq_groups: |
539 | kfree(msi_irq_groups); | |
540 | error_irq_group: | |
541 | kfree(msi_irq_group); | |
542 | error_attrs: | |
543 | count = 0; | |
544 | msi_attr = msi_attrs[count]; | |
545 | while (msi_attr) { | |
546 | msi_dev_attr = container_of(msi_attr, struct device_attribute, attr); | |
547 | kfree(msi_attr->name); | |
548 | kfree(msi_dev_attr); | |
549 | ++count; | |
550 | msi_attr = msi_attrs[count]; | |
da8d1c8b | 551 | } |
29237756 | 552 | kfree(msi_attrs); |
da8d1c8b NH |
553 | return ret; |
554 | } | |
555 | ||
e75eafb9 | 556 | static struct msi_desc * |
c66d4bd1 | 557 | msi_setup_entry(struct pci_dev *dev, int nvec, struct irq_affinity *affd) |
d873b4d4 | 558 | { |
bec04037 | 559 | struct irq_affinity_desc *masks = NULL; |
d873b4d4 | 560 | struct msi_desc *entry; |
e75eafb9 TG |
561 | u16 control; |
562 | ||
8e1101d2 | 563 | if (affd) |
61e1c590 | 564 | masks = irq_create_affinity_masks(nvec, affd); |
8e1101d2 | 565 | |
d873b4d4 | 566 | /* MSI Entry Initialization */ |
e75eafb9 | 567 | entry = alloc_msi_entry(&dev->dev, nvec, masks); |
d873b4d4 | 568 | if (!entry) |
e75eafb9 | 569 | goto out; |
d873b4d4 YW |
570 | |
571 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control); | |
572 | ||
573 | entry->msi_attrib.is_msix = 0; | |
574 | entry->msi_attrib.is_64 = !!(control & PCI_MSI_FLAGS_64BIT); | |
d7cc609f | 575 | entry->msi_attrib.is_virtual = 0; |
d873b4d4 YW |
576 | entry->msi_attrib.entry_nr = 0; |
577 | entry->msi_attrib.maskbit = !!(control & PCI_MSI_FLAGS_MASKBIT); | |
578 | entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */ | |
d873b4d4 | 579 | entry->msi_attrib.multi_cap = (control & PCI_MSI_FLAGS_QMASK) >> 1; |
63a7b17e | 580 | entry->msi_attrib.multiple = ilog2(__roundup_pow_of_two(nvec)); |
d873b4d4 YW |
581 | |
582 | if (control & PCI_MSI_FLAGS_64BIT) | |
583 | entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_64; | |
584 | else | |
585 | entry->mask_pos = dev->msi_cap + PCI_MSI_MASK_32; | |
586 | ||
587 | /* Save the initial mask status */ | |
588 | if (entry->msi_attrib.maskbit) | |
589 | pci_read_config_dword(dev, entry->mask_pos, &entry->masked); | |
590 | ||
e75eafb9 TG |
591 | out: |
592 | kfree(masks); | |
d873b4d4 YW |
593 | return entry; |
594 | } | |
595 | ||
f144d149 BH |
596 | static int msi_verify_entries(struct pci_dev *dev) |
597 | { | |
598 | struct msi_desc *entry; | |
599 | ||
5004e98a | 600 | for_each_pci_msi_entry(entry, dev) { |
f144d149 BH |
601 | if (!dev->no_64bit_msi || !entry->msg.address_hi) |
602 | continue; | |
7506dc79 | 603 | pci_err(dev, "Device has broken 64-bit MSI but arch" |
f144d149 BH |
604 | " tried to assign one above 4G\n"); |
605 | return -EIO; | |
606 | } | |
607 | return 0; | |
608 | } | |
609 | ||
1da177e4 LT |
610 | /** |
611 | * msi_capability_init - configure device's MSI capability structure | |
612 | * @dev: pointer to the pci_dev data structure of MSI device function | |
1c8d7b0a | 613 | * @nvec: number of interrupts to allocate |
f6b6aefe | 614 | * @affd: description of automatic IRQ affinity assignments (may be %NULL) |
1da177e4 | 615 | * |
1c8d7b0a MW |
616 | * Setup the MSI capability structure of the device with the requested |
617 | * number of interrupts. A return value of zero indicates the successful | |
f6b6aefe | 618 | * setup of an entry with the new MSI IRQ. A negative return value indicates |
1c8d7b0a MW |
619 | * an error, and a positive return value indicates the number of interrupts |
620 | * which could have been allocated. | |
621 | */ | |
61e1c590 | 622 | static int msi_capability_init(struct pci_dev *dev, int nvec, |
c66d4bd1 | 623 | struct irq_affinity *affd) |
1da177e4 LT |
624 | { |
625 | struct msi_desc *entry; | |
f465136d | 626 | int ret; |
f2440d9a | 627 | unsigned mask; |
1da177e4 | 628 | |
61b64abd | 629 | pci_msi_set_enable(dev, 0); /* Disable MSI during set up */ |
110828c9 | 630 | |
61e1c590 | 631 | entry = msi_setup_entry(dev, nvec, affd); |
f7feaca7 EB |
632 | if (!entry) |
633 | return -ENOMEM; | |
1ce03373 | 634 | |
f6b6aefe | 635 | /* All MSIs are unmasked by default; mask them all */ |
31ea5d4d | 636 | mask = msi_mask(entry->msi_attrib.multi_cap); |
f2440d9a MW |
637 | msi_mask_irq(entry, mask, mask); |
638 | ||
5004e98a | 639 | list_add_tail(&entry->list, dev_to_msi_list(&dev->dev)); |
9c831334 | 640 | |
1da177e4 | 641 | /* Configure MSI capability structure */ |
8e047ada | 642 | ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI); |
7fe3730d | 643 | if (ret) { |
7ba1930d | 644 | msi_mask_irq(entry, mask, ~mask); |
f56e4481 | 645 | free_msi_irqs(dev); |
7fe3730d | 646 | return ret; |
fd58e55f | 647 | } |
f7feaca7 | 648 | |
f144d149 BH |
649 | ret = msi_verify_entries(dev); |
650 | if (ret) { | |
651 | msi_mask_irq(entry, mask, ~mask); | |
652 | free_msi_irqs(dev); | |
653 | return ret; | |
654 | } | |
655 | ||
da8d1c8b NH |
656 | ret = populate_msi_sysfs(dev); |
657 | if (ret) { | |
658 | msi_mask_irq(entry, mask, ~mask); | |
659 | free_msi_irqs(dev); | |
660 | return ret; | |
661 | } | |
662 | ||
f6b6aefe | 663 | /* Set MSI enabled bits */ |
ba698ad4 | 664 | pci_intx_for_msi(dev, 0); |
61b64abd | 665 | pci_msi_set_enable(dev, 1); |
b1cbf4e4 | 666 | dev->msi_enabled = 1; |
1da177e4 | 667 | |
5f226991 | 668 | pcibios_free_irq(dev); |
7fe3730d | 669 | dev->irq = entry->irq; |
1da177e4 LT |
670 | return 0; |
671 | } | |
672 | ||
520fe9dc | 673 | static void __iomem *msix_map_region(struct pci_dev *dev, unsigned nr_entries) |
5a05a9d8 | 674 | { |
4302e0fb | 675 | resource_size_t phys_addr; |
5a05a9d8 | 676 | u32 table_offset; |
6a878e50 | 677 | unsigned long flags; |
5a05a9d8 HS |
678 | u8 bir; |
679 | ||
909094c6 BH |
680 | pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE, |
681 | &table_offset); | |
4d18760c | 682 | bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR); |
6a878e50 YW |
683 | flags = pci_resource_flags(dev, bir); |
684 | if (!flags || (flags & IORESOURCE_UNSET)) | |
685 | return NULL; | |
686 | ||
4d18760c | 687 | table_offset &= PCI_MSIX_TABLE_OFFSET; |
5a05a9d8 HS |
688 | phys_addr = pci_resource_start(dev, bir) + table_offset; |
689 | ||
690 | return ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE); | |
691 | } | |
692 | ||
520fe9dc | 693 | static int msix_setup_entries(struct pci_dev *dev, void __iomem *base, |
e75eafb9 | 694 | struct msix_entry *entries, int nvec, |
c66d4bd1 | 695 | struct irq_affinity *affd) |
d9d7070e | 696 | { |
bec04037 | 697 | struct irq_affinity_desc *curmsk, *masks = NULL; |
d9d7070e | 698 | struct msi_desc *entry; |
e75eafb9 | 699 | int ret, i; |
d7cc609f | 700 | int vec_count = pci_msix_vec_count(dev); |
4ef33685 | 701 | |
8e1101d2 | 702 | if (affd) |
61e1c590 | 703 | masks = irq_create_affinity_masks(nvec, affd); |
4ef33685 | 704 | |
e75eafb9 TG |
705 | for (i = 0, curmsk = masks; i < nvec; i++) { |
706 | entry = alloc_msi_entry(&dev->dev, 1, curmsk); | |
d9d7070e HS |
707 | if (!entry) { |
708 | if (!i) | |
709 | iounmap(base); | |
710 | else | |
711 | free_msi_irqs(dev); | |
712 | /* No enough memory. Don't try again */ | |
e75eafb9 TG |
713 | ret = -ENOMEM; |
714 | goto out; | |
d9d7070e HS |
715 | } |
716 | ||
717 | entry->msi_attrib.is_msix = 1; | |
718 | entry->msi_attrib.is_64 = 1; | |
3ac020e0 CH |
719 | if (entries) |
720 | entry->msi_attrib.entry_nr = entries[i].entry; | |
721 | else | |
722 | entry->msi_attrib.entry_nr = i; | |
d7cc609f LG |
723 | |
724 | entry->msi_attrib.is_virtual = | |
725 | entry->msi_attrib.entry_nr >= vec_count; | |
726 | ||
d9d7070e | 727 | entry->msi_attrib.default_irq = dev->irq; |
d9d7070e HS |
728 | entry->mask_base = base; |
729 | ||
5004e98a | 730 | list_add_tail(&entry->list, dev_to_msi_list(&dev->dev)); |
e75eafb9 TG |
731 | if (masks) |
732 | curmsk++; | |
d9d7070e | 733 | } |
e75eafb9 TG |
734 | ret = 0; |
735 | out: | |
736 | kfree(masks); | |
3adfb572 | 737 | return ret; |
d9d7070e HS |
738 | } |
739 | ||
75cb3426 | 740 | static void msix_program_entries(struct pci_dev *dev, |
520fe9dc | 741 | struct msix_entry *entries) |
75cb3426 HS |
742 | { |
743 | struct msi_desc *entry; | |
744 | int i = 0; | |
d7cc609f | 745 | void __iomem *desc_addr; |
75cb3426 | 746 | |
5004e98a | 747 | for_each_pci_msi_entry(entry, dev) { |
3ac020e0 CH |
748 | if (entries) |
749 | entries[i++].vector = entry->irq; | |
d7cc609f LG |
750 | |
751 | desc_addr = pci_msix_desc_addr(entry); | |
752 | if (desc_addr) | |
753 | entry->masked = readl(desc_addr + | |
754 | PCI_MSIX_ENTRY_VECTOR_CTRL); | |
755 | else | |
756 | entry->masked = 0; | |
757 | ||
75cb3426 | 758 | msix_mask_irq(entry, 1); |
75cb3426 HS |
759 | } |
760 | } | |
761 | ||
1da177e4 LT |
762 | /** |
763 | * msix_capability_init - configure device's MSI-X capability | |
764 | * @dev: pointer to the pci_dev data structure of MSI-X device function | |
8f7020d3 RD |
765 | * @entries: pointer to an array of struct msix_entry entries |
766 | * @nvec: number of @entries | |
f6b6aefe | 767 | * @affd: Optional pointer to enable automatic affinity assignment |
1da177e4 | 768 | * |
eaae4b3a | 769 | * Setup the MSI-X capability structure of device function with a |
f6b6aefe BH |
770 | * single MSI-X IRQ. A return of zero indicates the successful setup of |
771 | * requested MSI-X entries with allocated IRQs or non-zero for otherwise. | |
1da177e4 | 772 | **/ |
e75eafb9 | 773 | static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries, |
c66d4bd1 | 774 | int nvec, struct irq_affinity *affd) |
1da177e4 | 775 | { |
520fe9dc | 776 | int ret; |
5a05a9d8 | 777 | u16 control; |
1da177e4 LT |
778 | void __iomem *base; |
779 | ||
f598282f | 780 | /* Ensure MSI-X is disabled while it is set up */ |
61b64abd | 781 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0); |
f598282f | 782 | |
66f0d0c4 | 783 | pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control); |
1da177e4 | 784 | /* Request & Map MSI-X table region */ |
527eee29 | 785 | base = msix_map_region(dev, msix_table_size(control)); |
5a05a9d8 | 786 | if (!base) |
1da177e4 LT |
787 | return -ENOMEM; |
788 | ||
61e1c590 | 789 | ret = msix_setup_entries(dev, base, entries, nvec, affd); |
d9d7070e HS |
790 | if (ret) |
791 | return ret; | |
9c831334 | 792 | |
8e047ada | 793 | ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX); |
583871d4 | 794 | if (ret) |
2adc7907 | 795 | goto out_avail; |
9c831334 | 796 | |
f144d149 BH |
797 | /* Check if all MSI entries honor device restrictions */ |
798 | ret = msi_verify_entries(dev); | |
799 | if (ret) | |
800 | goto out_free; | |
801 | ||
f598282f MW |
802 | /* |
803 | * Some devices require MSI-X to be enabled before we can touch the | |
804 | * MSI-X registers. We need to mask all the vectors to prevent | |
805 | * interrupts coming in before they're fully set up. | |
806 | */ | |
61b64abd | 807 | pci_msix_clear_and_set_ctrl(dev, 0, |
66f0d0c4 | 808 | PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE); |
f598282f | 809 | |
75cb3426 | 810 | msix_program_entries(dev, entries); |
f598282f | 811 | |
da8d1c8b | 812 | ret = populate_msi_sysfs(dev); |
2adc7907 AG |
813 | if (ret) |
814 | goto out_free; | |
da8d1c8b | 815 | |
f598282f | 816 | /* Set MSI-X enabled bits and unmask the function */ |
ba698ad4 | 817 | pci_intx_for_msi(dev, 0); |
b1cbf4e4 | 818 | dev->msix_enabled = 1; |
61b64abd | 819 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0); |
8d181018 | 820 | |
5f226991 | 821 | pcibios_free_irq(dev); |
1da177e4 | 822 | return 0; |
583871d4 | 823 | |
2adc7907 | 824 | out_avail: |
583871d4 HS |
825 | if (ret < 0) { |
826 | /* | |
f6b6aefe | 827 | * If we had some success, report the number of IRQs |
583871d4 HS |
828 | * we succeeded in setting up. |
829 | */ | |
d9d7070e | 830 | struct msi_desc *entry; |
583871d4 HS |
831 | int avail = 0; |
832 | ||
5004e98a | 833 | for_each_pci_msi_entry(entry, dev) { |
583871d4 HS |
834 | if (entry->irq != 0) |
835 | avail++; | |
836 | } | |
837 | if (avail != 0) | |
838 | ret = avail; | |
839 | } | |
840 | ||
2adc7907 | 841 | out_free: |
583871d4 HS |
842 | free_msi_irqs(dev); |
843 | ||
844 | return ret; | |
1da177e4 LT |
845 | } |
846 | ||
24334a12 | 847 | /** |
a06cd74c | 848 | * pci_msi_supported - check whether MSI may be enabled on a device |
24334a12 | 849 | * @dev: pointer to the pci_dev data structure of MSI device function |
f6b6aefe | 850 | * @nvec: how many MSIs have been requested? |
24334a12 | 851 | * |
f7625980 | 852 | * Look at global flags, the device itself, and its parent buses |
17bbc12a | 853 | * to determine if MSI/-X are supported for the device. If MSI/-X is |
a06cd74c | 854 | * supported return 1, else return 0. |
24334a12 | 855 | **/ |
a06cd74c | 856 | static int pci_msi_supported(struct pci_dev *dev, int nvec) |
24334a12 BG |
857 | { |
858 | struct pci_bus *bus; | |
859 | ||
0306ebfa | 860 | /* MSI must be globally enabled and supported by the device */ |
27e20603 | 861 | if (!pci_msi_enable) |
a06cd74c | 862 | return 0; |
27e20603 AG |
863 | |
864 | if (!dev || dev->no_msi || dev->current_state != PCI_D0) | |
a06cd74c | 865 | return 0; |
24334a12 | 866 | |
314e77b3 ME |
867 | /* |
868 | * You can't ask to have 0 or less MSIs configured. | |
869 | * a) it's stupid .. | |
870 | * b) the list manipulation code assumes nvec >= 1. | |
871 | */ | |
872 | if (nvec < 1) | |
a06cd74c | 873 | return 0; |
314e77b3 | 874 | |
500559a9 HS |
875 | /* |
876 | * Any bridge which does NOT route MSI transactions from its | |
877 | * secondary bus to its primary bus must set NO_MSI flag on | |
0306ebfa BG |
878 | * the secondary pci_bus. |
879 | * We expect only arch-specific PCI host bus controller driver | |
880 | * or quirks for specific PCI bridges to be setting NO_MSI. | |
881 | */ | |
24334a12 BG |
882 | for (bus = dev->bus; bus; bus = bus->parent) |
883 | if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI) | |
a06cd74c | 884 | return 0; |
24334a12 | 885 | |
a06cd74c | 886 | return 1; |
24334a12 BG |
887 | } |
888 | ||
d1ac1d26 AG |
889 | /** |
890 | * pci_msi_vec_count - Return the number of MSI vectors a device can send | |
891 | * @dev: device to report about | |
892 | * | |
893 | * This function returns the number of MSI vectors a device requested via | |
894 | * Multiple Message Capable register. It returns a negative errno if the | |
895 | * device is not capable sending MSI interrupts. Otherwise, the call succeeds | |
896 | * and returns a power of two, up to a maximum of 2^5 (32), according to the | |
897 | * MSI specification. | |
898 | **/ | |
899 | int pci_msi_vec_count(struct pci_dev *dev) | |
900 | { | |
901 | int ret; | |
902 | u16 msgctl; | |
903 | ||
904 | if (!dev->msi_cap) | |
905 | return -EINVAL; | |
906 | ||
907 | pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl); | |
908 | ret = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1); | |
909 | ||
910 | return ret; | |
911 | } | |
912 | EXPORT_SYMBOL(pci_msi_vec_count); | |
913 | ||
688769f6 | 914 | static void pci_msi_shutdown(struct pci_dev *dev) |
1da177e4 | 915 | { |
f2440d9a MW |
916 | struct msi_desc *desc; |
917 | u32 mask; | |
1da177e4 | 918 | |
128bc5fc | 919 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
ded86d8d EB |
920 | return; |
921 | ||
5004e98a | 922 | BUG_ON(list_empty(dev_to_msi_list(&dev->dev))); |
4a7cc831 | 923 | desc = first_pci_msi_entry(dev); |
110828c9 | 924 | |
61b64abd | 925 | pci_msi_set_enable(dev, 0); |
ba698ad4 | 926 | pci_intx_for_msi(dev, 1); |
b1cbf4e4 | 927 | dev->msi_enabled = 0; |
7bd007e4 | 928 | |
12abb8ba | 929 | /* Return the device with MSI unmasked as initial states */ |
31ea5d4d | 930 | mask = msi_mask(desc->msi_attrib.multi_cap); |
12abb8ba | 931 | /* Keep cached state to be restored */ |
23ed8d57 | 932 | __pci_msi_desc_mask_irq(desc, mask, ~mask); |
e387b9ee | 933 | |
f6b6aefe | 934 | /* Restore dev->irq to its default pin-assertion IRQ */ |
f2440d9a | 935 | dev->irq = desc->msi_attrib.default_irq; |
5f226991 | 936 | pcibios_alloc_irq(dev); |
d52877c7 | 937 | } |
24d27553 | 938 | |
500559a9 | 939 | void pci_disable_msi(struct pci_dev *dev) |
d52877c7 | 940 | { |
d52877c7 YL |
941 | if (!pci_msi_enable || !dev || !dev->msi_enabled) |
942 | return; | |
943 | ||
944 | pci_msi_shutdown(dev); | |
f56e4481 | 945 | free_msi_irqs(dev); |
1da177e4 | 946 | } |
4cc086fa | 947 | EXPORT_SYMBOL(pci_disable_msi); |
1da177e4 | 948 | |
a52e2e35 | 949 | /** |
ff1aa430 | 950 | * pci_msix_vec_count - return the number of device's MSI-X table entries |
a52e2e35 | 951 | * @dev: pointer to the pci_dev data structure of MSI-X device function |
ff1aa430 AG |
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 | **/ | |
957 | int pci_msix_vec_count(struct pci_dev *dev) | |
a52e2e35 | 958 | { |
a52e2e35 RW |
959 | u16 control; |
960 | ||
520fe9dc | 961 | if (!dev->msix_cap) |
ff1aa430 | 962 | return -EINVAL; |
a52e2e35 | 963 | |
f84ecd28 | 964 | pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control); |
527eee29 | 965 | return msix_table_size(control); |
a52e2e35 | 966 | } |
ff1aa430 | 967 | EXPORT_SYMBOL(pci_msix_vec_count); |
a52e2e35 | 968 | |
e75eafb9 | 969 | static int __pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, |
d7cc609f | 970 | int nvec, struct irq_affinity *affd, int flags) |
1da177e4 | 971 | { |
5ec09405 | 972 | int nr_entries; |
ded86d8d | 973 | int i, j; |
1da177e4 | 974 | |
a06cd74c AG |
975 | if (!pci_msi_supported(dev, nvec)) |
976 | return -EINVAL; | |
c9953a73 | 977 | |
ff1aa430 AG |
978 | nr_entries = pci_msix_vec_count(dev); |
979 | if (nr_entries < 0) | |
980 | return nr_entries; | |
d7cc609f | 981 | if (nvec > nr_entries && !(flags & PCI_IRQ_VIRTUAL)) |
57fbf52c | 982 | return nr_entries; |
1da177e4 | 983 | |
3ac020e0 CH |
984 | if (entries) { |
985 | /* Check for any invalid entries */ | |
986 | for (i = 0; i < nvec; i++) { | |
987 | if (entries[i].entry >= nr_entries) | |
988 | return -EINVAL; /* invalid entry */ | |
989 | for (j = i + 1; j < nvec; j++) { | |
990 | if (entries[i].entry == entries[j].entry) | |
991 | return -EINVAL; /* duplicate entry */ | |
992 | } | |
1da177e4 LT |
993 | } |
994 | } | |
7bd007e4 | 995 | |
f6b6aefe | 996 | /* Check whether driver already requested for MSI IRQ */ |
500559a9 | 997 | if (dev->msi_enabled) { |
7506dc79 | 998 | pci_info(dev, "can't enable MSI-X (MSI IRQ already assigned)\n"); |
1da177e4 LT |
999 | return -EINVAL; |
1000 | } | |
61e1c590 | 1001 | return msix_capability_init(dev, entries, nvec, affd); |
e75eafb9 TG |
1002 | } |
1003 | ||
688769f6 | 1004 | static void pci_msix_shutdown(struct pci_dev *dev) |
fc4afc7b | 1005 | { |
12abb8ba HS |
1006 | struct msi_desc *entry; |
1007 | ||
128bc5fc | 1008 | if (!pci_msi_enable || !dev || !dev->msix_enabled) |
ded86d8d EB |
1009 | return; |
1010 | ||
0170591b KB |
1011 | if (pci_dev_is_disconnected(dev)) { |
1012 | dev->msix_enabled = 0; | |
1013 | return; | |
1014 | } | |
1015 | ||
12abb8ba | 1016 | /* Return the device with MSI-X masked as initial states */ |
5004e98a | 1017 | for_each_pci_msi_entry(entry, dev) { |
12abb8ba | 1018 | /* Keep cached states to be restored */ |
23ed8d57 | 1019 | __pci_msix_desc_mask_irq(entry, 1); |
12abb8ba HS |
1020 | } |
1021 | ||
61b64abd | 1022 | pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0); |
ba698ad4 | 1023 | pci_intx_for_msi(dev, 1); |
b1cbf4e4 | 1024 | dev->msix_enabled = 0; |
5f226991 | 1025 | pcibios_alloc_irq(dev); |
d52877c7 | 1026 | } |
c901851f | 1027 | |
500559a9 | 1028 | void pci_disable_msix(struct pci_dev *dev) |
d52877c7 YL |
1029 | { |
1030 | if (!pci_msi_enable || !dev || !dev->msix_enabled) | |
1031 | return; | |
1032 | ||
1033 | pci_msix_shutdown(dev); | |
f56e4481 | 1034 | free_msi_irqs(dev); |
1da177e4 | 1035 | } |
4cc086fa | 1036 | EXPORT_SYMBOL(pci_disable_msix); |
1da177e4 | 1037 | |
309e57df MW |
1038 | void pci_no_msi(void) |
1039 | { | |
1040 | pci_msi_enable = 0; | |
1041 | } | |
c9953a73 | 1042 | |
07ae95f9 AP |
1043 | /** |
1044 | * pci_msi_enabled - is MSI enabled? | |
1045 | * | |
1046 | * Returns true if MSI has not been disabled by the command-line option | |
1047 | * pci=nomsi. | |
1048 | **/ | |
1049 | int pci_msi_enabled(void) | |
d389fec6 | 1050 | { |
07ae95f9 | 1051 | return pci_msi_enable; |
d389fec6 | 1052 | } |
07ae95f9 | 1053 | EXPORT_SYMBOL(pci_msi_enabled); |
d389fec6 | 1054 | |
4ef33685 | 1055 | static int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec, |
c66d4bd1 | 1056 | struct irq_affinity *affd) |
302a2523 | 1057 | { |
034cd97e | 1058 | int nvec; |
302a2523 AG |
1059 | int rc; |
1060 | ||
a06cd74c AG |
1061 | if (!pci_msi_supported(dev, minvec)) |
1062 | return -EINVAL; | |
034cd97e | 1063 | |
f6b6aefe | 1064 | /* Check whether driver already requested MSI-X IRQs */ |
034cd97e | 1065 | if (dev->msix_enabled) { |
7506dc79 | 1066 | pci_info(dev, "can't enable MSI (MSI-X already enabled)\n"); |
034cd97e AG |
1067 | return -EINVAL; |
1068 | } | |
1069 | ||
302a2523 AG |
1070 | if (maxvec < minvec) |
1071 | return -ERANGE; | |
1072 | ||
4c1ef72e TZ |
1073 | if (WARN_ON_ONCE(dev->msi_enabled)) |
1074 | return -EINVAL; | |
1075 | ||
034cd97e AG |
1076 | nvec = pci_msi_vec_count(dev); |
1077 | if (nvec < 0) | |
1078 | return nvec; | |
4ef33685 | 1079 | if (nvec < minvec) |
948b7620 | 1080 | return -ENOSPC; |
4ef33685 CH |
1081 | |
1082 | if (nvec > maxvec) | |
034cd97e AG |
1083 | nvec = maxvec; |
1084 | ||
4ef33685 | 1085 | for (;;) { |
61e1c590 | 1086 | if (affd) { |
6f9a22bc | 1087 | nvec = irq_calc_affinity_vectors(minvec, nvec, affd); |
4ef33685 CH |
1088 | if (nvec < minvec) |
1089 | return -ENOSPC; | |
1090 | } | |
1091 | ||
61e1c590 | 1092 | rc = msi_capability_init(dev, nvec, affd); |
4ef33685 CH |
1093 | if (rc == 0) |
1094 | return nvec; | |
1095 | ||
4ef33685 | 1096 | if (rc < 0) |
302a2523 | 1097 | return rc; |
4ef33685 CH |
1098 | if (rc < minvec) |
1099 | return -ENOSPC; | |
1100 | ||
1101 | nvec = rc; | |
1102 | } | |
1103 | } | |
1104 | ||
4fe03955 CH |
1105 | /* deprecated, don't use */ |
1106 | int pci_enable_msi(struct pci_dev *dev) | |
4ef33685 | 1107 | { |
4fe03955 CH |
1108 | int rc = __pci_enable_msi_range(dev, 1, 1, NULL); |
1109 | if (rc < 0) | |
1110 | return rc; | |
1111 | return 0; | |
4ef33685 | 1112 | } |
4fe03955 | 1113 | EXPORT_SYMBOL(pci_enable_msi); |
4ef33685 CH |
1114 | |
1115 | static int __pci_enable_msix_range(struct pci_dev *dev, | |
61e1c590 | 1116 | struct msix_entry *entries, int minvec, |
d7cc609f LG |
1117 | int maxvec, struct irq_affinity *affd, |
1118 | int flags) | |
4ef33685 | 1119 | { |
e75eafb9 | 1120 | int rc, nvec = maxvec; |
4ef33685 CH |
1121 | |
1122 | if (maxvec < minvec) | |
1123 | return -ERANGE; | |
1124 | ||
4c1ef72e TZ |
1125 | if (WARN_ON_ONCE(dev->msix_enabled)) |
1126 | return -EINVAL; | |
1127 | ||
4ef33685 | 1128 | for (;;) { |
61e1c590 | 1129 | if (affd) { |
6f9a22bc | 1130 | nvec = irq_calc_affinity_vectors(minvec, nvec, affd); |
4ef33685 | 1131 | if (nvec < minvec) |
302a2523 | 1132 | return -ENOSPC; |
302a2523 | 1133 | } |
302a2523 | 1134 | |
d7cc609f | 1135 | rc = __pci_enable_msix(dev, entries, nvec, affd, flags); |
4ef33685 CH |
1136 | if (rc == 0) |
1137 | return nvec; | |
1138 | ||
4ef33685 CH |
1139 | if (rc < 0) |
1140 | return rc; | |
1141 | if (rc < minvec) | |
1142 | return -ENOSPC; | |
1143 | ||
1144 | nvec = rc; | |
1145 | } | |
302a2523 | 1146 | } |
302a2523 AG |
1147 | |
1148 | /** | |
1149 | * pci_enable_msix_range - configure device's MSI-X capability structure | |
1150 | * @dev: pointer to the pci_dev data structure of MSI-X device function | |
1151 | * @entries: pointer to an array of MSI-X entries | |
f6b6aefe BH |
1152 | * @minvec: minimum number of MSI-X IRQs requested |
1153 | * @maxvec: maximum number of MSI-X IRQs requested | |
302a2523 AG |
1154 | * |
1155 | * Setup the MSI-X capability structure of device function with a maximum | |
1156 | * possible number of interrupts in the range between @minvec and @maxvec | |
1157 | * upon its software driver call to request for MSI-X mode enabled on its | |
1158 | * hardware device function. It returns a negative errno if an error occurs. | |
1159 | * If it succeeds, it returns the actual number of interrupts allocated and | |
1160 | * indicates the successful configuration of MSI-X capability structure | |
1161 | * with new allocated MSI-X interrupts. | |
1162 | **/ | |
1163 | int pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries, | |
4ef33685 | 1164 | int minvec, int maxvec) |
302a2523 | 1165 | { |
d7cc609f | 1166 | return __pci_enable_msix_range(dev, entries, minvec, maxvec, NULL, 0); |
302a2523 AG |
1167 | } |
1168 | EXPORT_SYMBOL(pci_enable_msix_range); | |
3878eaef | 1169 | |
aff17164 | 1170 | /** |
402723ad | 1171 | * pci_alloc_irq_vectors_affinity - allocate multiple IRQs for a device |
aff17164 CH |
1172 | * @dev: PCI device to operate on |
1173 | * @min_vecs: minimum number of vectors required (must be >= 1) | |
1174 | * @max_vecs: maximum (desired) number of vectors | |
1175 | * @flags: flags or quirks for the allocation | |
402723ad | 1176 | * @affd: optional description of the affinity requirements |
aff17164 CH |
1177 | * |
1178 | * Allocate up to @max_vecs interrupt vectors for @dev, using MSI-X or MSI | |
1179 | * vectors if available, and fall back to a single legacy vector | |
1180 | * if neither is available. Return the number of vectors allocated, | |
1181 | * (which might be smaller than @max_vecs) if successful, or a negative | |
1182 | * error code on error. If less than @min_vecs interrupt vectors are | |
1183 | * available for @dev the function will fail with -ENOSPC. | |
1184 | * | |
1185 | * To get the Linux IRQ number used for a vector that can be passed to | |
1186 | * request_irq() use the pci_irq_vector() helper. | |
1187 | */ | |
402723ad CH |
1188 | int pci_alloc_irq_vectors_affinity(struct pci_dev *dev, unsigned int min_vecs, |
1189 | unsigned int max_vecs, unsigned int flags, | |
c66d4bd1 | 1190 | struct irq_affinity *affd) |
aff17164 | 1191 | { |
c66d4bd1 | 1192 | struct irq_affinity msi_default_affd = {0}; |
77f88abd ML |
1193 | int msix_vecs = -ENOSPC; |
1194 | int msi_vecs = -ENOSPC; | |
aff17164 | 1195 | |
402723ad CH |
1196 | if (flags & PCI_IRQ_AFFINITY) { |
1197 | if (!affd) | |
1198 | affd = &msi_default_affd; | |
1199 | } else { | |
1200 | if (WARN_ON(affd)) | |
1201 | affd = NULL; | |
1202 | } | |
61e1c590 | 1203 | |
4fe0d154 | 1204 | if (flags & PCI_IRQ_MSIX) { |
77f88abd | 1205 | msix_vecs = __pci_enable_msix_range(dev, NULL, min_vecs, |
d7cc609f | 1206 | max_vecs, affd, flags); |
77f88abd ML |
1207 | if (msix_vecs > 0) |
1208 | return msix_vecs; | |
aff17164 CH |
1209 | } |
1210 | ||
4fe0d154 | 1211 | if (flags & PCI_IRQ_MSI) { |
77f88abd ML |
1212 | msi_vecs = __pci_enable_msi_range(dev, min_vecs, max_vecs, |
1213 | affd); | |
1214 | if (msi_vecs > 0) | |
1215 | return msi_vecs; | |
aff17164 CH |
1216 | } |
1217 | ||
f6b6aefe | 1218 | /* use legacy IRQ if allowed */ |
862290f9 CH |
1219 | if (flags & PCI_IRQ_LEGACY) { |
1220 | if (min_vecs == 1 && dev->irq) { | |
c66d4bd1 ML |
1221 | /* |
1222 | * Invoke the affinity spreading logic to ensure that | |
1223 | * the device driver can adjust queue configuration | |
1224 | * for the single interrupt case. | |
1225 | */ | |
1226 | if (affd) | |
1227 | irq_create_affinity_masks(1, affd); | |
862290f9 CH |
1228 | pci_intx(dev, 1); |
1229 | return 1; | |
1230 | } | |
5d0bdf28 CH |
1231 | } |
1232 | ||
77f88abd ML |
1233 | if (msix_vecs == -ENOSPC) |
1234 | return -ENOSPC; | |
1235 | return msi_vecs; | |
aff17164 | 1236 | } |
402723ad | 1237 | EXPORT_SYMBOL(pci_alloc_irq_vectors_affinity); |
aff17164 CH |
1238 | |
1239 | /** | |
1240 | * pci_free_irq_vectors - free previously allocated IRQs for a device | |
1241 | * @dev: PCI device to operate on | |
1242 | * | |
1243 | * Undoes the allocations and enabling in pci_alloc_irq_vectors(). | |
1244 | */ | |
1245 | void pci_free_irq_vectors(struct pci_dev *dev) | |
1246 | { | |
1247 | pci_disable_msix(dev); | |
1248 | pci_disable_msi(dev); | |
1249 | } | |
1250 | EXPORT_SYMBOL(pci_free_irq_vectors); | |
1251 | ||
1252 | /** | |
1253 | * pci_irq_vector - return Linux IRQ number of a device vector | |
1254 | * @dev: PCI device to operate on | |
1255 | * @nr: device-relative interrupt vector index (0-based). | |
1256 | */ | |
1257 | int pci_irq_vector(struct pci_dev *dev, unsigned int nr) | |
1258 | { | |
1259 | if (dev->msix_enabled) { | |
1260 | struct msi_desc *entry; | |
1261 | int i = 0; | |
1262 | ||
1263 | for_each_pci_msi_entry(entry, dev) { | |
1264 | if (i == nr) | |
1265 | return entry->irq; | |
1266 | i++; | |
1267 | } | |
1268 | WARN_ON_ONCE(1); | |
1269 | return -EINVAL; | |
1270 | } | |
1271 | ||
1272 | if (dev->msi_enabled) { | |
1273 | struct msi_desc *entry = first_pci_msi_entry(dev); | |
1274 | ||
1275 | if (WARN_ON_ONCE(nr >= entry->nvec_used)) | |
1276 | return -EINVAL; | |
1277 | } else { | |
1278 | if (WARN_ON_ONCE(nr > 0)) | |
1279 | return -EINVAL; | |
1280 | } | |
1281 | ||
1282 | return dev->irq + nr; | |
1283 | } | |
1284 | EXPORT_SYMBOL(pci_irq_vector); | |
1285 | ||
ee8d41e5 | 1286 | /** |
f6b6aefe | 1287 | * pci_irq_get_affinity - return the affinity of a particular MSI vector |
ee8d41e5 TG |
1288 | * @dev: PCI device to operate on |
1289 | * @nr: device-relative interrupt vector index (0-based). | |
1290 | */ | |
1291 | const struct cpumask *pci_irq_get_affinity(struct pci_dev *dev, int nr) | |
1292 | { | |
1293 | if (dev->msix_enabled) { | |
1294 | struct msi_desc *entry; | |
1295 | int i = 0; | |
1296 | ||
1297 | for_each_pci_msi_entry(entry, dev) { | |
1298 | if (i == nr) | |
bec04037 | 1299 | return &entry->affinity->mask; |
ee8d41e5 TG |
1300 | i++; |
1301 | } | |
1302 | WARN_ON_ONCE(1); | |
1303 | return NULL; | |
1304 | } else if (dev->msi_enabled) { | |
1305 | struct msi_desc *entry = first_pci_msi_entry(dev); | |
1306 | ||
d1d111e0 JB |
1307 | if (WARN_ON_ONCE(!entry || !entry->affinity || |
1308 | nr >= entry->nvec_used)) | |
ee8d41e5 TG |
1309 | return NULL; |
1310 | ||
bec04037 | 1311 | return &entry->affinity[nr].mask; |
ee8d41e5 TG |
1312 | } else { |
1313 | return cpu_possible_mask; | |
1314 | } | |
1315 | } | |
1316 | EXPORT_SYMBOL(pci_irq_get_affinity); | |
1317 | ||
27ddb689 | 1318 | /** |
f6b6aefe | 1319 | * pci_irq_get_node - return the NUMA node of a particular MSI vector |
27ddb689 SL |
1320 | * @pdev: PCI device to operate on |
1321 | * @vec: device-relative interrupt vector index (0-based). | |
1322 | */ | |
1323 | int pci_irq_get_node(struct pci_dev *pdev, int vec) | |
1324 | { | |
1325 | const struct cpumask *mask; | |
1326 | ||
1327 | mask = pci_irq_get_affinity(pdev, vec); | |
1328 | if (mask) | |
1329 | return local_memory_node(cpu_to_node(cpumask_first(mask))); | |
1330 | return dev_to_node(&pdev->dev); | |
1331 | } | |
1332 | EXPORT_SYMBOL(pci_irq_get_node); | |
1333 | ||
25a98bd4 JL |
1334 | struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc) |
1335 | { | |
1336 | return to_pci_dev(desc->dev); | |
1337 | } | |
a4289dc2 | 1338 | EXPORT_SYMBOL(msi_desc_to_pci_dev); |
25a98bd4 | 1339 | |
c179c9b9 JL |
1340 | void *msi_desc_to_pci_sysdata(struct msi_desc *desc) |
1341 | { | |
1342 | struct pci_dev *dev = msi_desc_to_pci_dev(desc); | |
1343 | ||
1344 | return dev->bus->sysdata; | |
1345 | } | |
1346 | EXPORT_SYMBOL_GPL(msi_desc_to_pci_sysdata); | |
1347 | ||
3878eaef JL |
1348 | #ifdef CONFIG_PCI_MSI_IRQ_DOMAIN |
1349 | /** | |
1350 | * pci_msi_domain_write_msg - Helper to write MSI message to PCI config space | |
1351 | * @irq_data: Pointer to interrupt data of the MSI interrupt | |
1352 | * @msg: Pointer to the message | |
1353 | */ | |
1354 | void pci_msi_domain_write_msg(struct irq_data *irq_data, struct msi_msg *msg) | |
1355 | { | |
507a883e | 1356 | struct msi_desc *desc = irq_data_get_msi_desc(irq_data); |
3878eaef JL |
1357 | |
1358 | /* | |
1359 | * For MSI-X desc->irq is always equal to irq_data->irq. For | |
1360 | * MSI only the first interrupt of MULTI MSI passes the test. | |
1361 | */ | |
1362 | if (desc->irq == irq_data->irq) | |
1363 | __pci_write_msi_msg(desc, msg); | |
1364 | } | |
1365 | ||
1366 | /** | |
1367 | * pci_msi_domain_calc_hwirq - Generate a unique ID for an MSI source | |
1368 | * @dev: Pointer to the PCI device | |
f6b6aefe | 1369 | * @desc: Pointer to the MSI descriptor |
3878eaef JL |
1370 | * |
1371 | * The ID number is only used within the irqdomain. | |
1372 | */ | |
1373 | irq_hw_number_t pci_msi_domain_calc_hwirq(struct pci_dev *dev, | |
1374 | struct msi_desc *desc) | |
1375 | { | |
1376 | return (irq_hw_number_t)desc->msi_attrib.entry_nr | | |
4e544bac | 1377 | pci_dev_id(dev) << 11 | |
3878eaef JL |
1378 | (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 27; |
1379 | } | |
1380 | ||
1381 | static inline bool pci_msi_desc_is_multi_msi(struct msi_desc *desc) | |
1382 | { | |
1383 | return !desc->msi_attrib.is_msix && desc->nvec_used > 1; | |
1384 | } | |
1385 | ||
1386 | /** | |
f6b6aefe BH |
1387 | * pci_msi_domain_check_cap - Verify that @domain supports the capabilities |
1388 | * for @dev | |
3878eaef JL |
1389 | * @domain: The interrupt domain to check |
1390 | * @info: The domain info for verification | |
1391 | * @dev: The device to check | |
1392 | * | |
1393 | * Returns: | |
1394 | * 0 if the functionality is supported | |
1395 | * 1 if Multi MSI is requested, but the domain does not support it | |
1396 | * -ENOTSUPP otherwise | |
1397 | */ | |
1398 | int pci_msi_domain_check_cap(struct irq_domain *domain, | |
1399 | struct msi_domain_info *info, struct device *dev) | |
1400 | { | |
1401 | struct msi_desc *desc = first_pci_msi_entry(to_pci_dev(dev)); | |
1402 | ||
4fe03955 | 1403 | /* Special handling to support __pci_enable_msi_range() */ |
3878eaef JL |
1404 | if (pci_msi_desc_is_multi_msi(desc) && |
1405 | !(info->flags & MSI_FLAG_MULTI_PCI_MSI)) | |
1406 | return 1; | |
1407 | else if (desc->msi_attrib.is_msix && !(info->flags & MSI_FLAG_PCI_MSIX)) | |
1408 | return -ENOTSUPP; | |
1409 | ||
1410 | return 0; | |
1411 | } | |
1412 | ||
1413 | static int pci_msi_domain_handle_error(struct irq_domain *domain, | |
1414 | struct msi_desc *desc, int error) | |
1415 | { | |
4fe03955 | 1416 | /* Special handling to support __pci_enable_msi_range() */ |
3878eaef JL |
1417 | if (pci_msi_desc_is_multi_msi(desc) && error == -ENOSPC) |
1418 | return 1; | |
1419 | ||
1420 | return error; | |
1421 | } | |
1422 | ||
1423 | #ifdef GENERIC_MSI_DOMAIN_OPS | |
1424 | static void pci_msi_domain_set_desc(msi_alloc_info_t *arg, | |
1425 | struct msi_desc *desc) | |
1426 | { | |
1427 | arg->desc = desc; | |
1428 | arg->hwirq = pci_msi_domain_calc_hwirq(msi_desc_to_pci_dev(desc), | |
1429 | desc); | |
1430 | } | |
1431 | #else | |
1432 | #define pci_msi_domain_set_desc NULL | |
1433 | #endif | |
1434 | ||
1435 | static struct msi_domain_ops pci_msi_domain_ops_default = { | |
1436 | .set_desc = pci_msi_domain_set_desc, | |
1437 | .msi_check = pci_msi_domain_check_cap, | |
1438 | .handle_error = pci_msi_domain_handle_error, | |
1439 | }; | |
1440 | ||
1441 | static void pci_msi_domain_update_dom_ops(struct msi_domain_info *info) | |
1442 | { | |
1443 | struct msi_domain_ops *ops = info->ops; | |
1444 | ||
1445 | if (ops == NULL) { | |
1446 | info->ops = &pci_msi_domain_ops_default; | |
1447 | } else { | |
1448 | if (ops->set_desc == NULL) | |
1449 | ops->set_desc = pci_msi_domain_set_desc; | |
1450 | if (ops->msi_check == NULL) | |
1451 | ops->msi_check = pci_msi_domain_check_cap; | |
1452 | if (ops->handle_error == NULL) | |
1453 | ops->handle_error = pci_msi_domain_handle_error; | |
1454 | } | |
1455 | } | |
1456 | ||
1457 | static void pci_msi_domain_update_chip_ops(struct msi_domain_info *info) | |
1458 | { | |
1459 | struct irq_chip *chip = info->chip; | |
1460 | ||
1461 | BUG_ON(!chip); | |
1462 | if (!chip->irq_write_msi_msg) | |
1463 | chip->irq_write_msi_msg = pci_msi_domain_write_msg; | |
0701c53e MZ |
1464 | if (!chip->irq_mask) |
1465 | chip->irq_mask = pci_msi_mask_irq; | |
1466 | if (!chip->irq_unmask) | |
1467 | chip->irq_unmask = pci_msi_unmask_irq; | |
3878eaef JL |
1468 | } |
1469 | ||
1470 | /** | |
be5436c8 MZ |
1471 | * pci_msi_create_irq_domain - Create a MSI interrupt domain |
1472 | * @fwnode: Optional fwnode of the interrupt controller | |
3878eaef JL |
1473 | * @info: MSI domain info |
1474 | * @parent: Parent irq domain | |
1475 | * | |
1476 | * Updates the domain and chip ops and creates a MSI interrupt domain. | |
1477 | * | |
1478 | * Returns: | |
1479 | * A domain pointer or NULL in case of failure. | |
1480 | */ | |
be5436c8 | 1481 | struct irq_domain *pci_msi_create_irq_domain(struct fwnode_handle *fwnode, |
3878eaef JL |
1482 | struct msi_domain_info *info, |
1483 | struct irq_domain *parent) | |
1484 | { | |
0380839d MZ |
1485 | struct irq_domain *domain; |
1486 | ||
6988e0e0 MZ |
1487 | if (WARN_ON(info->flags & MSI_FLAG_LEVEL_CAPABLE)) |
1488 | info->flags &= ~MSI_FLAG_LEVEL_CAPABLE; | |
1489 | ||
3878eaef JL |
1490 | if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS) |
1491 | pci_msi_domain_update_dom_ops(info); | |
1492 | if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS) | |
1493 | pci_msi_domain_update_chip_ops(info); | |
1494 | ||
f3b0946d | 1495 | info->flags |= MSI_FLAG_ACTIVATE_EARLY; |
25e960ef TG |
1496 | if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE)) |
1497 | info->flags |= MSI_FLAG_MUST_REACTIVATE; | |
f3b0946d | 1498 | |
923aa4c3 HK |
1499 | /* PCI-MSI is oneshot-safe */ |
1500 | info->chip->flags |= IRQCHIP_ONESHOT_SAFE; | |
1501 | ||
be5436c8 | 1502 | domain = msi_create_irq_domain(fwnode, info, parent); |
0380839d MZ |
1503 | if (!domain) |
1504 | return NULL; | |
1505 | ||
96f0d93a | 1506 | irq_domain_update_bus_token(domain, DOMAIN_BUS_PCI_MSI); |
0380839d | 1507 | return domain; |
3878eaef | 1508 | } |
a4289dc2 | 1509 | EXPORT_SYMBOL_GPL(pci_msi_create_irq_domain); |
3878eaef | 1510 | |
235b2c77 RM |
1511 | /* |
1512 | * Users of the generic MSI infrastructure expect a device to have a single ID, | |
1513 | * so with DMA aliases we have to pick the least-worst compromise. Devices with | |
1514 | * DMA phantom functions tend to still emit MSIs from the real function number, | |
1515 | * so we ignore those and only consider topological aliases where either the | |
1516 | * alias device or RID appears on a different bus number. We also make the | |
1517 | * reasonable assumption that bridges are walked in an upstream direction (so | |
1518 | * the last one seen wins), and the much braver assumption that the most likely | |
1519 | * case is that of PCI->PCIe so we should always use the alias RID. This echoes | |
1520 | * the logic from intel_irq_remapping's set_msi_sid(), which presumably works | |
1521 | * well enough in practice; in the face of the horrible PCIe<->PCI-X conditions | |
1522 | * for taking ownership all we can really do is close our eyes and hope... | |
1523 | */ | |
b6eec9b7 DD |
1524 | static int get_msi_id_cb(struct pci_dev *pdev, u16 alias, void *data) |
1525 | { | |
1526 | u32 *pa = data; | |
235b2c77 RM |
1527 | u8 bus = PCI_BUS_NUM(*pa); |
1528 | ||
1529 | if (pdev->bus->number != bus || PCI_BUS_NUM(alias) != bus) | |
1530 | *pa = alias; | |
b6eec9b7 | 1531 | |
b6eec9b7 DD |
1532 | return 0; |
1533 | } | |
235b2c77 | 1534 | |
b6eec9b7 DD |
1535 | /** |
1536 | * pci_msi_domain_get_msi_rid - Get the MSI requester id (RID) | |
1537 | * @domain: The interrupt domain | |
1538 | * @pdev: The PCI device. | |
1539 | * | |
1540 | * The RID for a device is formed from the alias, with a firmware | |
1541 | * supplied mapping applied | |
1542 | * | |
1543 | * Returns: The RID. | |
1544 | */ | |
1545 | u32 pci_msi_domain_get_msi_rid(struct irq_domain *domain, struct pci_dev *pdev) | |
1546 | { | |
1547 | struct device_node *of_node; | |
4e544bac | 1548 | u32 rid = pci_dev_id(pdev); |
b6eec9b7 DD |
1549 | |
1550 | pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); | |
1551 | ||
1552 | of_node = irq_domain_get_of_node(domain); | |
be2021ba TN |
1553 | rid = of_node ? of_msi_map_rid(&pdev->dev, of_node, rid) : |
1554 | iort_msi_map_rid(&pdev->dev, rid); | |
b6eec9b7 DD |
1555 | |
1556 | return rid; | |
1557 | } | |
54fa97ee MZ |
1558 | |
1559 | /** | |
1560 | * pci_msi_get_device_domain - Get the MSI domain for a given PCI device | |
1561 | * @pdev: The PCI device | |
1562 | * | |
1563 | * Use the firmware data to find a device-specific MSI domain | |
235b2c77 | 1564 | * (i.e. not one that is set as a default). |
54fa97ee | 1565 | * |
235b2c77 | 1566 | * Returns: The corresponding MSI domain or NULL if none has been found. |
54fa97ee MZ |
1567 | */ |
1568 | struct irq_domain *pci_msi_get_device_domain(struct pci_dev *pdev) | |
1569 | { | |
be2021ba | 1570 | struct irq_domain *dom; |
4e544bac | 1571 | u32 rid = pci_dev_id(pdev); |
54fa97ee MZ |
1572 | |
1573 | pci_for_each_dma_alias(pdev, get_msi_id_cb, &rid); | |
be2021ba TN |
1574 | dom = of_msi_map_get_device_domain(&pdev->dev, rid); |
1575 | if (!dom) | |
1576 | dom = iort_get_device_domain(&pdev->dev, rid); | |
1577 | return dom; | |
54fa97ee | 1578 | } |
3878eaef | 1579 | #endif /* CONFIG_PCI_MSI_IRQ_DOMAIN */ |