xref: /linux/drivers/pci/msi/msi.c (revision 249ebf3f65f8530beb2cbfb91bff1d83ba88d23c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCI Message Signaled Interrupt (MSI)
4  *
5  * Copyright (C) 2003-2004 Intel
6  * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7  * Copyright (C) 2016 Christoph Hellwig.
8  */
9 #include <linux/bitfield.h>
10 #include <linux/err.h>
11 #include <linux/export.h>
12 #include <linux/irq.h>
13 
14 #include "../pci.h"
15 #include "msi.h"
16 
17 int pci_msi_enable = 1;
18 int pci_msi_ignore_mask;
19 
20 /**
21  * pci_msi_supported - check whether MSI may be enabled on a device
22  * @dev: pointer to the pci_dev data structure of MSI device function
23  * @nvec: how many MSIs have been requested?
24  *
25  * Look at global flags, the device itself, and its parent buses
26  * to determine if MSI/-X are supported for the device. If MSI/-X is
27  * supported return 1, else return 0.
28  **/
29 static int pci_msi_supported(struct pci_dev *dev, int nvec)
30 {
31 	struct pci_bus *bus;
32 
33 	/* MSI must be globally enabled and supported by the device */
34 	if (!pci_msi_enable)
35 		return 0;
36 
37 	if (!dev || dev->no_msi)
38 		return 0;
39 
40 	/*
41 	 * You can't ask to have 0 or less MSIs configured.
42 	 *  a) it's stupid ..
43 	 *  b) the list manipulation code assumes nvec >= 1.
44 	 */
45 	if (nvec < 1)
46 		return 0;
47 
48 	/*
49 	 * Any bridge which does NOT route MSI transactions from its
50 	 * secondary bus to its primary bus must set NO_MSI flag on
51 	 * the secondary pci_bus.
52 	 *
53 	 * The NO_MSI flag can either be set directly by:
54 	 * - arch-specific PCI host bus controller drivers (deprecated)
55 	 * - quirks for specific PCI bridges
56 	 *
57 	 * or indirectly by platform-specific PCI host bridge drivers by
58 	 * advertising the 'msi_domain' property, which results in
59 	 * the NO_MSI flag when no MSI domain is found for this bridge
60 	 * at probe time.
61 	 */
62 	for (bus = dev->bus; bus; bus = bus->parent)
63 		if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
64 			return 0;
65 
66 	return 1;
67 }
68 
69 static void pcim_msi_release(void *pcidev)
70 {
71 	struct pci_dev *dev = pcidev;
72 
73 	dev->is_msi_managed = false;
74 	pci_free_irq_vectors(dev);
75 }
76 
77 /*
78  * Needs to be separate from pcim_release to prevent an ordering problem
79  * vs. msi_device_data_release() in the MSI core code.
80  */
81 static int pcim_setup_msi_release(struct pci_dev *dev)
82 {
83 	int ret;
84 
85 	if (!pci_is_managed(dev) || dev->is_msi_managed)
86 		return 0;
87 
88 	ret = devm_add_action(&dev->dev, pcim_msi_release, dev);
89 	if (ret)
90 		return ret;
91 
92 	dev->is_msi_managed = true;
93 	return 0;
94 }
95 
96 /*
97  * Ordering vs. devres: msi device data has to be installed first so that
98  * pcim_msi_release() is invoked before it on device release.
99  */
100 static int pci_setup_msi_context(struct pci_dev *dev)
101 {
102 	int ret = msi_setup_device_data(&dev->dev);
103 
104 	if (ret)
105 		return ret;
106 
107 	return pcim_setup_msi_release(dev);
108 }
109 
110 /*
111  * Helper functions for mask/unmask and MSI message handling
112  */
113 
114 void pci_msi_update_mask(struct msi_desc *desc, u32 clear, u32 set)
115 {
116 	raw_spinlock_t *lock = &to_pci_dev(desc->dev)->msi_lock;
117 	unsigned long flags;
118 
119 	if (!desc->pci.msi_attrib.can_mask)
120 		return;
121 
122 	raw_spin_lock_irqsave(lock, flags);
123 	desc->pci.msi_mask &= ~clear;
124 	desc->pci.msi_mask |= set;
125 	pci_write_config_dword(msi_desc_to_pci_dev(desc), desc->pci.mask_pos,
126 			       desc->pci.msi_mask);
127 	raw_spin_unlock_irqrestore(lock, flags);
128 }
129 
130 /**
131  * pci_msi_mask_irq - Generic IRQ chip callback to mask PCI/MSI interrupts
132  * @data:	pointer to irqdata associated to that interrupt
133  */
134 void pci_msi_mask_irq(struct irq_data *data)
135 {
136 	struct msi_desc *desc = irq_data_get_msi_desc(data);
137 
138 	__pci_msi_mask_desc(desc, BIT(data->irq - desc->irq));
139 }
140 EXPORT_SYMBOL_GPL(pci_msi_mask_irq);
141 
142 /**
143  * pci_msi_unmask_irq - Generic IRQ chip callback to unmask PCI/MSI interrupts
144  * @data:	pointer to irqdata associated to that interrupt
145  */
146 void pci_msi_unmask_irq(struct irq_data *data)
147 {
148 	struct msi_desc *desc = irq_data_get_msi_desc(data);
149 
150 	__pci_msi_unmask_desc(desc, BIT(data->irq - desc->irq));
151 }
152 EXPORT_SYMBOL_GPL(pci_msi_unmask_irq);
153 
154 void __pci_read_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
155 {
156 	struct pci_dev *dev = msi_desc_to_pci_dev(entry);
157 
158 	BUG_ON(dev->current_state != PCI_D0);
159 
160 	if (entry->pci.msi_attrib.is_msix) {
161 		void __iomem *base = pci_msix_desc_addr(entry);
162 
163 		if (WARN_ON_ONCE(entry->pci.msi_attrib.is_virtual))
164 			return;
165 
166 		msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR);
167 		msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR);
168 		msg->data = readl(base + PCI_MSIX_ENTRY_DATA);
169 	} else {
170 		int pos = dev->msi_cap;
171 		u16 data;
172 
173 		pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
174 				      &msg->address_lo);
175 		if (entry->pci.msi_attrib.is_64) {
176 			pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
177 					      &msg->address_hi);
178 			pci_read_config_word(dev, pos + PCI_MSI_DATA_64, &data);
179 		} else {
180 			msg->address_hi = 0;
181 			pci_read_config_word(dev, pos + PCI_MSI_DATA_32, &data);
182 		}
183 		msg->data = data;
184 	}
185 }
186 
187 static inline void pci_write_msg_msi(struct pci_dev *dev, struct msi_desc *desc,
188 				     struct msi_msg *msg)
189 {
190 	int pos = dev->msi_cap;
191 	u16 msgctl;
192 
193 	pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
194 	msgctl &= ~PCI_MSI_FLAGS_QSIZE;
195 	msgctl |= FIELD_PREP(PCI_MSI_FLAGS_QSIZE, desc->pci.msi_attrib.multiple);
196 	pci_write_config_word(dev, pos + PCI_MSI_FLAGS, msgctl);
197 
198 	pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO, msg->address_lo);
199 	if (desc->pci.msi_attrib.is_64) {
200 		pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,  msg->address_hi);
201 		pci_write_config_word(dev, pos + PCI_MSI_DATA_64, msg->data);
202 	} else {
203 		pci_write_config_word(dev, pos + PCI_MSI_DATA_32, msg->data);
204 	}
205 	/* Ensure that the writes are visible in the device */
206 	pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
207 }
208 
209 static inline void pci_write_msg_msix(struct msi_desc *desc, struct msi_msg *msg)
210 {
211 	void __iomem *base = pci_msix_desc_addr(desc);
212 	u32 ctrl = desc->pci.msix_ctrl;
213 	bool unmasked = !(ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT);
214 
215 	if (desc->pci.msi_attrib.is_virtual)
216 		return;
217 	/*
218 	 * The specification mandates that the entry is masked
219 	 * when the message is modified:
220 	 *
221 	 * "If software changes the Address or Data value of an
222 	 * entry while the entry is unmasked, the result is
223 	 * undefined."
224 	 */
225 	if (unmasked)
226 		pci_msix_write_vector_ctrl(desc, ctrl | PCI_MSIX_ENTRY_CTRL_MASKBIT);
227 
228 	writel(msg->address_lo, base + PCI_MSIX_ENTRY_LOWER_ADDR);
229 	writel(msg->address_hi, base + PCI_MSIX_ENTRY_UPPER_ADDR);
230 	writel(msg->data, base + PCI_MSIX_ENTRY_DATA);
231 
232 	if (unmasked)
233 		pci_msix_write_vector_ctrl(desc, ctrl);
234 
235 	/* Ensure that the writes are visible in the device */
236 	readl(base + PCI_MSIX_ENTRY_DATA);
237 }
238 
239 void __pci_write_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
240 {
241 	struct pci_dev *dev = msi_desc_to_pci_dev(entry);
242 
243 	if (dev->current_state != PCI_D0 || pci_dev_is_disconnected(dev)) {
244 		/* Don't touch the hardware now */
245 	} else if (entry->pci.msi_attrib.is_msix) {
246 		pci_write_msg_msix(entry, msg);
247 	} else {
248 		pci_write_msg_msi(dev, entry, msg);
249 	}
250 
251 	entry->msg = *msg;
252 
253 	if (entry->write_msi_msg)
254 		entry->write_msi_msg(entry, entry->write_msi_msg_data);
255 }
256 
257 void pci_write_msi_msg(unsigned int irq, struct msi_msg *msg)
258 {
259 	struct msi_desc *entry = irq_get_msi_desc(irq);
260 
261 	__pci_write_msi_msg(entry, msg);
262 }
263 EXPORT_SYMBOL_GPL(pci_write_msi_msg);
264 
265 
266 /* PCI/MSI specific functionality */
267 
268 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
269 {
270 	if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
271 		pci_intx(dev, enable);
272 }
273 
274 static void pci_msi_set_enable(struct pci_dev *dev, int enable)
275 {
276 	u16 control;
277 
278 	pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
279 	control &= ~PCI_MSI_FLAGS_ENABLE;
280 	if (enable)
281 		control |= PCI_MSI_FLAGS_ENABLE;
282 	pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
283 }
284 
285 static int msi_setup_msi_desc(struct pci_dev *dev, int nvec,
286 			      struct irq_affinity_desc *masks)
287 {
288 	struct msi_desc desc;
289 	u16 control;
290 
291 	/* MSI Entry Initialization */
292 	memset(&desc, 0, sizeof(desc));
293 
294 	pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
295 	/* Lies, damned lies, and MSIs */
296 	if (dev->dev_flags & PCI_DEV_FLAGS_HAS_MSI_MASKING)
297 		control |= PCI_MSI_FLAGS_MASKBIT;
298 	/* Respect XEN's mask disabling */
299 	if (pci_msi_ignore_mask)
300 		control &= ~PCI_MSI_FLAGS_MASKBIT;
301 
302 	desc.nvec_used			= nvec;
303 	desc.pci.msi_attrib.is_64	= !!(control & PCI_MSI_FLAGS_64BIT);
304 	desc.pci.msi_attrib.can_mask	= !!(control & PCI_MSI_FLAGS_MASKBIT);
305 	desc.pci.msi_attrib.default_irq	= dev->irq;
306 	desc.pci.msi_attrib.multi_cap	= FIELD_GET(PCI_MSI_FLAGS_QMASK, control);
307 	desc.pci.msi_attrib.multiple	= ilog2(__roundup_pow_of_two(nvec));
308 	desc.affinity			= masks;
309 
310 	if (control & PCI_MSI_FLAGS_64BIT)
311 		desc.pci.mask_pos = dev->msi_cap + PCI_MSI_MASK_64;
312 	else
313 		desc.pci.mask_pos = dev->msi_cap + PCI_MSI_MASK_32;
314 
315 	/* Save the initial mask status */
316 	if (desc.pci.msi_attrib.can_mask)
317 		pci_read_config_dword(dev, desc.pci.mask_pos, &desc.pci.msi_mask);
318 
319 	return msi_insert_msi_desc(&dev->dev, &desc);
320 }
321 
322 static int msi_verify_entries(struct pci_dev *dev)
323 {
324 	struct msi_desc *entry;
325 
326 	if (!dev->no_64bit_msi)
327 		return 0;
328 
329 	msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) {
330 		if (entry->msg.address_hi) {
331 			pci_err(dev, "arch assigned 64-bit MSI address %#x%08x but device only supports 32 bits\n",
332 				entry->msg.address_hi, entry->msg.address_lo);
333 			break;
334 		}
335 	}
336 	return !entry ? 0 : -EIO;
337 }
338 
339 /**
340  * msi_capability_init - configure device's MSI capability structure
341  * @dev: pointer to the pci_dev data structure of MSI device function
342  * @nvec: number of interrupts to allocate
343  * @affd: description of automatic IRQ affinity assignments (may be %NULL)
344  *
345  * Setup the MSI capability structure of the device with the requested
346  * number of interrupts.  A return value of zero indicates the successful
347  * setup of an entry with the new MSI IRQ.  A negative return value indicates
348  * an error, and a positive return value indicates the number of interrupts
349  * which could have been allocated.
350  */
351 static int msi_capability_init(struct pci_dev *dev, int nvec,
352 			       struct irq_affinity *affd)
353 {
354 	struct irq_affinity_desc *masks = NULL;
355 	struct msi_desc *entry;
356 	int ret;
357 
358 	/* Reject multi-MSI early on irq domain enabled architectures */
359 	if (nvec > 1 && !pci_msi_domain_supports(dev, MSI_FLAG_MULTI_PCI_MSI, ALLOW_LEGACY))
360 		return 1;
361 
362 	/*
363 	 * Disable MSI during setup in the hardware, but mark it enabled
364 	 * so that setup code can evaluate it.
365 	 */
366 	pci_msi_set_enable(dev, 0);
367 	dev->msi_enabled = 1;
368 
369 	if (affd)
370 		masks = irq_create_affinity_masks(nvec, affd);
371 
372 	msi_lock_descs(&dev->dev);
373 	ret = msi_setup_msi_desc(dev, nvec, masks);
374 	if (ret)
375 		goto fail;
376 
377 	/* All MSIs are unmasked by default; mask them all */
378 	entry = msi_first_desc(&dev->dev, MSI_DESC_ALL);
379 	pci_msi_mask(entry, msi_multi_mask(entry));
380 
381 	/* Configure MSI capability structure */
382 	ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSI);
383 	if (ret)
384 		goto err;
385 
386 	ret = msi_verify_entries(dev);
387 	if (ret)
388 		goto err;
389 
390 	/* Set MSI enabled bits	*/
391 	pci_intx_for_msi(dev, 0);
392 	pci_msi_set_enable(dev, 1);
393 
394 	pcibios_free_irq(dev);
395 	dev->irq = entry->irq;
396 	goto unlock;
397 
398 err:
399 	pci_msi_unmask(entry, msi_multi_mask(entry));
400 	pci_free_msi_irqs(dev);
401 fail:
402 	dev->msi_enabled = 0;
403 unlock:
404 	msi_unlock_descs(&dev->dev);
405 	kfree(masks);
406 	return ret;
407 }
408 
409 int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
410 			   struct irq_affinity *affd)
411 {
412 	int nvec;
413 	int rc;
414 
415 	if (!pci_msi_supported(dev, minvec) || dev->current_state != PCI_D0)
416 		return -EINVAL;
417 
418 	/* Check whether driver already requested MSI-X IRQs */
419 	if (dev->msix_enabled) {
420 		pci_info(dev, "can't enable MSI (MSI-X already enabled)\n");
421 		return -EINVAL;
422 	}
423 
424 	if (maxvec < minvec)
425 		return -ERANGE;
426 
427 	if (WARN_ON_ONCE(dev->msi_enabled))
428 		return -EINVAL;
429 
430 	nvec = pci_msi_vec_count(dev);
431 	if (nvec < 0)
432 		return nvec;
433 	if (nvec < minvec)
434 		return -ENOSPC;
435 
436 	if (nvec > maxvec)
437 		nvec = maxvec;
438 
439 	rc = pci_setup_msi_context(dev);
440 	if (rc)
441 		return rc;
442 
443 	if (!pci_setup_msi_device_domain(dev))
444 		return -ENODEV;
445 
446 	for (;;) {
447 		if (affd) {
448 			nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
449 			if (nvec < minvec)
450 				return -ENOSPC;
451 		}
452 
453 		rc = msi_capability_init(dev, nvec, affd);
454 		if (rc == 0)
455 			return nvec;
456 
457 		if (rc < 0)
458 			return rc;
459 		if (rc < minvec)
460 			return -ENOSPC;
461 
462 		nvec = rc;
463 	}
464 }
465 
466 /**
467  * pci_msi_vec_count - Return the number of MSI vectors a device can send
468  * @dev: device to report about
469  *
470  * This function returns the number of MSI vectors a device requested via
471  * Multiple Message Capable register. It returns a negative errno if the
472  * device is not capable sending MSI interrupts. Otherwise, the call succeeds
473  * and returns a power of two, up to a maximum of 2^5 (32), according to the
474  * MSI specification.
475  **/
476 int pci_msi_vec_count(struct pci_dev *dev)
477 {
478 	int ret;
479 	u16 msgctl;
480 
481 	if (!dev->msi_cap)
482 		return -EINVAL;
483 
484 	pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &msgctl);
485 	ret = 1 << FIELD_GET(PCI_MSI_FLAGS_QMASK, msgctl);
486 
487 	return ret;
488 }
489 EXPORT_SYMBOL(pci_msi_vec_count);
490 
491 /*
492  * Architecture override returns true when the PCI MSI message should be
493  * written by the generic restore function.
494  */
495 bool __weak arch_restore_msi_irqs(struct pci_dev *dev)
496 {
497 	return true;
498 }
499 
500 void __pci_restore_msi_state(struct pci_dev *dev)
501 {
502 	struct msi_desc *entry;
503 	u16 control;
504 
505 	if (!dev->msi_enabled)
506 		return;
507 
508 	entry = irq_get_msi_desc(dev->irq);
509 
510 	pci_intx_for_msi(dev, 0);
511 	pci_msi_set_enable(dev, 0);
512 	if (arch_restore_msi_irqs(dev))
513 		__pci_write_msi_msg(entry, &entry->msg);
514 
515 	pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
516 	pci_msi_update_mask(entry, 0, 0);
517 	control &= ~PCI_MSI_FLAGS_QSIZE;
518 	control |= PCI_MSI_FLAGS_ENABLE |
519 		   FIELD_PREP(PCI_MSI_FLAGS_QSIZE, entry->pci.msi_attrib.multiple);
520 	pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
521 }
522 
523 void pci_msi_shutdown(struct pci_dev *dev)
524 {
525 	struct msi_desc *desc;
526 
527 	if (!pci_msi_enable || !dev || !dev->msi_enabled)
528 		return;
529 
530 	pci_msi_set_enable(dev, 0);
531 	pci_intx_for_msi(dev, 1);
532 	dev->msi_enabled = 0;
533 
534 	/* Return the device with MSI unmasked as initial states */
535 	desc = msi_first_desc(&dev->dev, MSI_DESC_ALL);
536 	if (!WARN_ON_ONCE(!desc))
537 		pci_msi_unmask(desc, msi_multi_mask(desc));
538 
539 	/* Restore dev->irq to its default pin-assertion IRQ */
540 	dev->irq = desc->pci.msi_attrib.default_irq;
541 	pcibios_alloc_irq(dev);
542 }
543 
544 /* PCI/MSI-X specific functionality */
545 
546 static void pci_msix_clear_and_set_ctrl(struct pci_dev *dev, u16 clear, u16 set)
547 {
548 	u16 ctrl;
549 
550 	pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
551 	ctrl &= ~clear;
552 	ctrl |= set;
553 	pci_write_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, ctrl);
554 }
555 
556 static void __iomem *msix_map_region(struct pci_dev *dev,
557 				     unsigned int nr_entries)
558 {
559 	resource_size_t phys_addr;
560 	u32 table_offset;
561 	unsigned long flags;
562 	u8 bir;
563 
564 	pci_read_config_dword(dev, dev->msix_cap + PCI_MSIX_TABLE,
565 			      &table_offset);
566 	bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
567 	flags = pci_resource_flags(dev, bir);
568 	if (!flags || (flags & IORESOURCE_UNSET))
569 		return NULL;
570 
571 	table_offset &= PCI_MSIX_TABLE_OFFSET;
572 	phys_addr = pci_resource_start(dev, bir) + table_offset;
573 
574 	return ioremap(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
575 }
576 
577 /**
578  * msix_prepare_msi_desc - Prepare a half initialized MSI descriptor for operation
579  * @dev:	The PCI device for which the descriptor is prepared
580  * @desc:	The MSI descriptor for preparation
581  *
582  * This is separate from msix_setup_msi_descs() below to handle dynamic
583  * allocations for MSI-X after initial enablement.
584  *
585  * Ideally the whole MSI-X setup would work that way, but there is no way to
586  * support this for the legacy arch_setup_msi_irqs() mechanism and for the
587  * fake irq domains like the x86 XEN one. Sigh...
588  *
589  * The descriptor is zeroed and only @desc::msi_index and @desc::affinity
590  * are set. When called from msix_setup_msi_descs() then the is_virtual
591  * attribute is initialized as well.
592  *
593  * Fill in the rest.
594  */
595 void msix_prepare_msi_desc(struct pci_dev *dev, struct msi_desc *desc)
596 {
597 	desc->nvec_used				= 1;
598 	desc->pci.msi_attrib.is_msix		= 1;
599 	desc->pci.msi_attrib.is_64		= 1;
600 	desc->pci.msi_attrib.default_irq	= dev->irq;
601 	desc->pci.mask_base			= dev->msix_base;
602 	desc->pci.msi_attrib.can_mask		= !pci_msi_ignore_mask &&
603 						  !desc->pci.msi_attrib.is_virtual;
604 
605 	if (desc->pci.msi_attrib.can_mask) {
606 		void __iomem *addr = pci_msix_desc_addr(desc);
607 
608 		desc->pci.msix_ctrl = readl(addr + PCI_MSIX_ENTRY_VECTOR_CTRL);
609 	}
610 }
611 
612 static int msix_setup_msi_descs(struct pci_dev *dev, struct msix_entry *entries,
613 				int nvec, struct irq_affinity_desc *masks)
614 {
615 	int ret = 0, i, vec_count = pci_msix_vec_count(dev);
616 	struct irq_affinity_desc *curmsk;
617 	struct msi_desc desc;
618 
619 	memset(&desc, 0, sizeof(desc));
620 
621 	for (i = 0, curmsk = masks; i < nvec; i++, curmsk++) {
622 		desc.msi_index = entries ? entries[i].entry : i;
623 		desc.affinity = masks ? curmsk : NULL;
624 		desc.pci.msi_attrib.is_virtual = desc.msi_index >= vec_count;
625 
626 		msix_prepare_msi_desc(dev, &desc);
627 
628 		ret = msi_insert_msi_desc(&dev->dev, &desc);
629 		if (ret)
630 			break;
631 	}
632 	return ret;
633 }
634 
635 static void msix_update_entries(struct pci_dev *dev, struct msix_entry *entries)
636 {
637 	struct msi_desc *desc;
638 
639 	if (entries) {
640 		msi_for_each_desc(desc, &dev->dev, MSI_DESC_ALL) {
641 			entries->vector = desc->irq;
642 			entries++;
643 		}
644 	}
645 }
646 
647 static void msix_mask_all(void __iomem *base, int tsize)
648 {
649 	u32 ctrl = PCI_MSIX_ENTRY_CTRL_MASKBIT;
650 	int i;
651 
652 	if (pci_msi_ignore_mask)
653 		return;
654 
655 	for (i = 0; i < tsize; i++, base += PCI_MSIX_ENTRY_SIZE)
656 		writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
657 }
658 
659 static int msix_setup_interrupts(struct pci_dev *dev, struct msix_entry *entries,
660 				 int nvec, struct irq_affinity *affd)
661 {
662 	struct irq_affinity_desc *masks = NULL;
663 	int ret;
664 
665 	if (affd)
666 		masks = irq_create_affinity_masks(nvec, affd);
667 
668 	msi_lock_descs(&dev->dev);
669 	ret = msix_setup_msi_descs(dev, entries, nvec, masks);
670 	if (ret)
671 		goto out_free;
672 
673 	ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
674 	if (ret)
675 		goto out_free;
676 
677 	/* Check if all MSI entries honor device restrictions */
678 	ret = msi_verify_entries(dev);
679 	if (ret)
680 		goto out_free;
681 
682 	msix_update_entries(dev, entries);
683 	goto out_unlock;
684 
685 out_free:
686 	pci_free_msi_irqs(dev);
687 out_unlock:
688 	msi_unlock_descs(&dev->dev);
689 	kfree(masks);
690 	return ret;
691 }
692 
693 /**
694  * msix_capability_init - configure device's MSI-X capability
695  * @dev: pointer to the pci_dev data structure of MSI-X device function
696  * @entries: pointer to an array of struct msix_entry entries
697  * @nvec: number of @entries
698  * @affd: Optional pointer to enable automatic affinity assignment
699  *
700  * Setup the MSI-X capability structure of device function with a
701  * single MSI-X IRQ. A return of zero indicates the successful setup of
702  * requested MSI-X entries with allocated IRQs or non-zero for otherwise.
703  **/
704 static int msix_capability_init(struct pci_dev *dev, struct msix_entry *entries,
705 				int nvec, struct irq_affinity *affd)
706 {
707 	int ret, tsize;
708 	u16 control;
709 
710 	/*
711 	 * Some devices require MSI-X to be enabled before the MSI-X
712 	 * registers can be accessed.  Mask all the vectors to prevent
713 	 * interrupts coming in before they're fully set up.
714 	 */
715 	pci_msix_clear_and_set_ctrl(dev, 0, PCI_MSIX_FLAGS_MASKALL |
716 				    PCI_MSIX_FLAGS_ENABLE);
717 
718 	/* Mark it enabled so setup functions can query it */
719 	dev->msix_enabled = 1;
720 
721 	pci_read_config_word(dev, dev->msix_cap + PCI_MSIX_FLAGS, &control);
722 	/* Request & Map MSI-X table region */
723 	tsize = msix_table_size(control);
724 	dev->msix_base = msix_map_region(dev, tsize);
725 	if (!dev->msix_base) {
726 		ret = -ENOMEM;
727 		goto out_disable;
728 	}
729 
730 	ret = msix_setup_interrupts(dev, entries, nvec, affd);
731 	if (ret)
732 		goto out_disable;
733 
734 	/* Disable INTX */
735 	pci_intx_for_msi(dev, 0);
736 
737 	/*
738 	 * Ensure that all table entries are masked to prevent
739 	 * stale entries from firing in a crash kernel.
740 	 *
741 	 * Done late to deal with a broken Marvell NVME device
742 	 * which takes the MSI-X mask bits into account even
743 	 * when MSI-X is disabled, which prevents MSI delivery.
744 	 */
745 	msix_mask_all(dev->msix_base, tsize);
746 	pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
747 
748 	pcibios_free_irq(dev);
749 	return 0;
750 
751 out_disable:
752 	dev->msix_enabled = 0;
753 	pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE, 0);
754 
755 	return ret;
756 }
757 
758 static bool pci_msix_validate_entries(struct pci_dev *dev, struct msix_entry *entries, int nvec)
759 {
760 	bool nogap;
761 	int i, j;
762 
763 	if (!entries)
764 		return true;
765 
766 	nogap = pci_msi_domain_supports(dev, MSI_FLAG_MSIX_CONTIGUOUS, DENY_LEGACY);
767 
768 	for (i = 0; i < nvec; i++) {
769 		/* Check for duplicate entries */
770 		for (j = i + 1; j < nvec; j++) {
771 			if (entries[i].entry == entries[j].entry)
772 				return false;
773 		}
774 		/* Check for unsupported gaps */
775 		if (nogap && entries[i].entry != i)
776 			return false;
777 	}
778 	return true;
779 }
780 
781 int __pci_enable_msix_range(struct pci_dev *dev, struct msix_entry *entries, int minvec,
782 			    int maxvec, struct irq_affinity *affd, int flags)
783 {
784 	int hwsize, rc, nvec = maxvec;
785 
786 	if (maxvec < minvec)
787 		return -ERANGE;
788 
789 	if (dev->msi_enabled) {
790 		pci_info(dev, "can't enable MSI-X (MSI already enabled)\n");
791 		return -EINVAL;
792 	}
793 
794 	if (WARN_ON_ONCE(dev->msix_enabled))
795 		return -EINVAL;
796 
797 	/* Check MSI-X early on irq domain enabled architectures */
798 	if (!pci_msi_domain_supports(dev, MSI_FLAG_PCI_MSIX, ALLOW_LEGACY))
799 		return -ENOTSUPP;
800 
801 	if (!pci_msi_supported(dev, nvec) || dev->current_state != PCI_D0)
802 		return -EINVAL;
803 
804 	hwsize = pci_msix_vec_count(dev);
805 	if (hwsize < 0)
806 		return hwsize;
807 
808 	if (!pci_msix_validate_entries(dev, entries, nvec))
809 		return -EINVAL;
810 
811 	if (hwsize < nvec) {
812 		/* Keep the IRQ virtual hackery working */
813 		if (flags & PCI_IRQ_VIRTUAL)
814 			hwsize = nvec;
815 		else
816 			nvec = hwsize;
817 	}
818 
819 	if (nvec < minvec)
820 		return -ENOSPC;
821 
822 	rc = pci_setup_msi_context(dev);
823 	if (rc)
824 		return rc;
825 
826 	if (!pci_setup_msix_device_domain(dev, hwsize))
827 		return -ENODEV;
828 
829 	for (;;) {
830 		if (affd) {
831 			nvec = irq_calc_affinity_vectors(minvec, nvec, affd);
832 			if (nvec < minvec)
833 				return -ENOSPC;
834 		}
835 
836 		rc = msix_capability_init(dev, entries, nvec, affd);
837 		if (rc == 0)
838 			return nvec;
839 
840 		if (rc < 0)
841 			return rc;
842 		if (rc < minvec)
843 			return -ENOSPC;
844 
845 		nvec = rc;
846 	}
847 }
848 
849 void __pci_restore_msix_state(struct pci_dev *dev)
850 {
851 	struct msi_desc *entry;
852 	bool write_msg;
853 
854 	if (!dev->msix_enabled)
855 		return;
856 
857 	/* route the table */
858 	pci_intx_for_msi(dev, 0);
859 	pci_msix_clear_and_set_ctrl(dev, 0,
860 				PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
861 
862 	write_msg = arch_restore_msi_irqs(dev);
863 
864 	msi_lock_descs(&dev->dev);
865 	msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) {
866 		if (write_msg)
867 			__pci_write_msi_msg(entry, &entry->msg);
868 		pci_msix_write_vector_ctrl(entry, entry->pci.msix_ctrl);
869 	}
870 	msi_unlock_descs(&dev->dev);
871 
872 	pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
873 }
874 
875 void pci_msix_shutdown(struct pci_dev *dev)
876 {
877 	struct msi_desc *desc;
878 
879 	if (!pci_msi_enable || !dev || !dev->msix_enabled)
880 		return;
881 
882 	if (pci_dev_is_disconnected(dev)) {
883 		dev->msix_enabled = 0;
884 		return;
885 	}
886 
887 	/* Return the device with MSI-X masked as initial states */
888 	msi_for_each_desc(desc, &dev->dev, MSI_DESC_ALL)
889 		pci_msix_mask(desc);
890 
891 	pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_ENABLE, 0);
892 	pci_intx_for_msi(dev, 1);
893 	dev->msix_enabled = 0;
894 	pcibios_alloc_irq(dev);
895 }
896 
897 /* Common interfaces */
898 
899 void pci_free_msi_irqs(struct pci_dev *dev)
900 {
901 	pci_msi_teardown_msi_irqs(dev);
902 
903 	if (dev->msix_base) {
904 		iounmap(dev->msix_base);
905 		dev->msix_base = NULL;
906 	}
907 }
908 
909 /* Misc. infrastructure */
910 
911 struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
912 {
913 	return to_pci_dev(desc->dev);
914 }
915 EXPORT_SYMBOL(msi_desc_to_pci_dev);
916 
917 void pci_no_msi(void)
918 {
919 	pci_msi_enable = 0;
920 }
921