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