xref: /linux/kernel/irq/msi.c (revision 0a1d4434db5f86c50018fe0aab299ac97dc15b76)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2014 Intel Corp.
4  * Author: Jiang Liu <jiang.liu@linux.intel.com>
5  *
6  * This file is licensed under GPLv2.
7  *
8  * This file contains common code to support Message Signaled Interrupts for
9  * PCI compatible and non PCI compatible devices.
10  */
11 #include <linux/types.h>
12 #include <linux/device.h>
13 #include <linux/irq.h>
14 #include <linux/irqdomain.h>
15 #include <linux/msi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/pci.h>
19 
20 #include "internals.h"
21 
22 /**
23  * struct msi_ctrl - MSI internal management control structure
24  * @domid:	ID of the domain on which management operations should be done
25  * @first:	First (hardware) slot index to operate on
26  * @last:	Last (hardware) slot index to operate on
27  * @nirqs:	The number of Linux interrupts to allocate. Can be larger
28  *		than the range due to PCI/multi-MSI.
29  */
30 struct msi_ctrl {
31 	unsigned int			domid;
32 	unsigned int			first;
33 	unsigned int			last;
34 	unsigned int			nirqs;
35 };
36 
37 /* Invalid Xarray index which is outside of any searchable range */
38 #define MSI_XA_MAX_INDEX	(ULONG_MAX - 1)
39 /* The maximum domain size */
40 #define MSI_XA_DOMAIN_SIZE	(MSI_MAX_INDEX + 1)
41 
42 static void msi_domain_free_locked(struct device *dev, struct msi_ctrl *ctrl);
43 static unsigned int msi_domain_get_hwsize(struct device *dev, unsigned int domid);
44 static inline int msi_sysfs_create_group(struct device *dev);
45 
46 
47 /**
48  * msi_alloc_desc - Allocate an initialized msi_desc
49  * @dev:	Pointer to the device for which this is allocated
50  * @nvec:	The number of vectors used in this entry
51  * @affinity:	Optional pointer to an affinity mask array size of @nvec
52  *
53  * If @affinity is not %NULL then an affinity array[@nvec] is allocated
54  * and the affinity masks and flags from @affinity are copied.
55  *
56  * Return: pointer to allocated &msi_desc on success or %NULL on failure
57  */
58 static struct msi_desc *msi_alloc_desc(struct device *dev, int nvec,
59 				       const struct irq_affinity_desc *affinity)
60 {
61 	struct msi_desc *desc = kzalloc(sizeof(*desc), GFP_KERNEL);
62 
63 	if (!desc)
64 		return NULL;
65 
66 	desc->dev = dev;
67 	desc->nvec_used = nvec;
68 	if (affinity) {
69 		desc->affinity = kmemdup(affinity, nvec * sizeof(*desc->affinity), GFP_KERNEL);
70 		if (!desc->affinity) {
71 			kfree(desc);
72 			return NULL;
73 		}
74 	}
75 	return desc;
76 }
77 
78 static void msi_free_desc(struct msi_desc *desc)
79 {
80 	kfree(desc->affinity);
81 	kfree(desc);
82 }
83 
84 static int msi_insert_desc(struct device *dev, struct msi_desc *desc,
85 			   unsigned int domid, unsigned int index)
86 {
87 	struct msi_device_data *md = dev->msi.data;
88 	struct xarray *xa = &md->__domains[domid].store;
89 	unsigned int hwsize;
90 	int ret;
91 
92 	hwsize = msi_domain_get_hwsize(dev, domid);
93 
94 	if (index == MSI_ANY_INDEX) {
95 		struct xa_limit limit = { .min = 0, .max = hwsize - 1 };
96 		unsigned int index;
97 
98 		/* Let the xarray allocate a free index within the limit */
99 		ret = xa_alloc(xa, &index, desc, limit, GFP_KERNEL);
100 		if (ret)
101 			goto fail;
102 
103 		desc->msi_index = index;
104 		return 0;
105 	} else {
106 		if (index >= hwsize) {
107 			ret = -ERANGE;
108 			goto fail;
109 		}
110 
111 		desc->msi_index = index;
112 		ret = xa_insert(xa, index, desc, GFP_KERNEL);
113 		if (ret)
114 			goto fail;
115 		return 0;
116 	}
117 fail:
118 	msi_free_desc(desc);
119 	return ret;
120 }
121 
122 /**
123  * msi_domain_insert_msi_desc - Allocate and initialize a MSI descriptor and
124  *				insert it at @init_desc->msi_index
125  *
126  * @dev:	Pointer to the device for which the descriptor is allocated
127  * @domid:	The id of the interrupt domain to which the desriptor is added
128  * @init_desc:	Pointer to an MSI descriptor to initialize the new descriptor
129  *
130  * Return: 0 on success or an appropriate failure code.
131  */
132 int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid,
133 			       struct msi_desc *init_desc)
134 {
135 	struct msi_desc *desc;
136 
137 	lockdep_assert_held(&dev->msi.data->mutex);
138 
139 	desc = msi_alloc_desc(dev, init_desc->nvec_used, init_desc->affinity);
140 	if (!desc)
141 		return -ENOMEM;
142 
143 	/* Copy type specific data to the new descriptor. */
144 	desc->pci = init_desc->pci;
145 
146 	return msi_insert_desc(dev, desc, domid, init_desc->msi_index);
147 }
148 
149 static bool msi_desc_match(struct msi_desc *desc, enum msi_desc_filter filter)
150 {
151 	switch (filter) {
152 	case MSI_DESC_ALL:
153 		return true;
154 	case MSI_DESC_NOTASSOCIATED:
155 		return !desc->irq;
156 	case MSI_DESC_ASSOCIATED:
157 		return !!desc->irq;
158 	}
159 	WARN_ON_ONCE(1);
160 	return false;
161 }
162 
163 static bool msi_ctrl_valid(struct device *dev, struct msi_ctrl *ctrl)
164 {
165 	unsigned int hwsize;
166 
167 	if (WARN_ON_ONCE(ctrl->domid >= MSI_MAX_DEVICE_IRQDOMAINS ||
168 			 !dev->msi.data->__domains[ctrl->domid].domain))
169 		return false;
170 
171 	hwsize = msi_domain_get_hwsize(dev, ctrl->domid);
172 	if (WARN_ON_ONCE(ctrl->first > ctrl->last ||
173 			 ctrl->first >= hwsize ||
174 			 ctrl->last >= hwsize))
175 		return false;
176 	return true;
177 }
178 
179 static void msi_domain_free_descs(struct device *dev, struct msi_ctrl *ctrl)
180 {
181 	struct msi_desc *desc;
182 	struct xarray *xa;
183 	unsigned long idx;
184 
185 	lockdep_assert_held(&dev->msi.data->mutex);
186 
187 	if (!msi_ctrl_valid(dev, ctrl))
188 		return;
189 
190 	xa = &dev->msi.data->__domains[ctrl->domid].store;
191 	xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) {
192 		xa_erase(xa, idx);
193 
194 		/* Leak the descriptor when it is still referenced */
195 		if (WARN_ON_ONCE(msi_desc_match(desc, MSI_DESC_ASSOCIATED)))
196 			continue;
197 		msi_free_desc(desc);
198 	}
199 }
200 
201 /**
202  * msi_domain_free_msi_descs_range - Free a range of MSI descriptors of a device in an irqdomain
203  * @dev:	Device for which to free the descriptors
204  * @domid:	Id of the domain to operate on
205  * @first:	Index to start freeing from (inclusive)
206  * @last:	Last index to be freed (inclusive)
207  */
208 void msi_domain_free_msi_descs_range(struct device *dev, unsigned int domid,
209 				     unsigned int first, unsigned int last)
210 {
211 	struct msi_ctrl ctrl = {
212 		.domid	= domid,
213 		.first	= first,
214 		.last	= last,
215 	};
216 
217 	msi_domain_free_descs(dev, &ctrl);
218 }
219 
220 /**
221  * msi_domain_add_simple_msi_descs - Allocate and initialize MSI descriptors
222  * @dev:	Pointer to the device for which the descriptors are allocated
223  * @ctrl:	Allocation control struct
224  *
225  * Return: 0 on success or an appropriate failure code.
226  */
227 static int msi_domain_add_simple_msi_descs(struct device *dev, struct msi_ctrl *ctrl)
228 {
229 	struct msi_desc *desc;
230 	unsigned int idx;
231 	int ret;
232 
233 	lockdep_assert_held(&dev->msi.data->mutex);
234 
235 	if (!msi_ctrl_valid(dev, ctrl))
236 		return -EINVAL;
237 
238 	for (idx = ctrl->first; idx <= ctrl->last; idx++) {
239 		desc = msi_alloc_desc(dev, 1, NULL);
240 		if (!desc)
241 			goto fail_mem;
242 		ret = msi_insert_desc(dev, desc, ctrl->domid, idx);
243 		if (ret)
244 			goto fail;
245 	}
246 	return 0;
247 
248 fail_mem:
249 	ret = -ENOMEM;
250 fail:
251 	msi_domain_free_descs(dev, ctrl);
252 	return ret;
253 }
254 
255 void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
256 {
257 	*msg = entry->msg;
258 }
259 
260 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
261 {
262 	struct msi_desc *entry = irq_get_msi_desc(irq);
263 
264 	__get_cached_msi_msg(entry, msg);
265 }
266 EXPORT_SYMBOL_GPL(get_cached_msi_msg);
267 
268 static void msi_device_data_release(struct device *dev, void *res)
269 {
270 	struct msi_device_data *md = res;
271 	int i;
272 
273 	for (i = 0; i < MSI_MAX_DEVICE_IRQDOMAINS; i++) {
274 		msi_remove_device_irq_domain(dev, i);
275 		WARN_ON_ONCE(!xa_empty(&md->__domains[i].store));
276 		xa_destroy(&md->__domains[i].store);
277 	}
278 	dev->msi.data = NULL;
279 }
280 
281 /**
282  * msi_setup_device_data - Setup MSI device data
283  * @dev:	Device for which MSI device data should be set up
284  *
285  * Return: 0 on success, appropriate error code otherwise
286  *
287  * This can be called more than once for @dev. If the MSI device data is
288  * already allocated the call succeeds. The allocated memory is
289  * automatically released when the device is destroyed.
290  */
291 int msi_setup_device_data(struct device *dev)
292 {
293 	struct msi_device_data *md;
294 	int ret, i;
295 
296 	if (dev->msi.data)
297 		return 0;
298 
299 	md = devres_alloc(msi_device_data_release, sizeof(*md), GFP_KERNEL);
300 	if (!md)
301 		return -ENOMEM;
302 
303 	ret = msi_sysfs_create_group(dev);
304 	if (ret) {
305 		devres_free(md);
306 		return ret;
307 	}
308 
309 	for (i = 0; i < MSI_MAX_DEVICE_IRQDOMAINS; i++)
310 		xa_init_flags(&md->__domains[i].store, XA_FLAGS_ALLOC);
311 
312 	/*
313 	 * If @dev::msi::domain is set and is a global MSI domain, copy the
314 	 * pointer into the domain array so all code can operate on domain
315 	 * ids. The NULL pointer check is required to keep the legacy
316 	 * architecture specific PCI/MSI support working.
317 	 */
318 	if (dev->msi.domain && !irq_domain_is_msi_parent(dev->msi.domain))
319 		md->__domains[MSI_DEFAULT_DOMAIN].domain = dev->msi.domain;
320 
321 	mutex_init(&md->mutex);
322 	dev->msi.data = md;
323 	devres_add(dev, md);
324 	return 0;
325 }
326 
327 /**
328  * msi_lock_descs - Lock the MSI descriptor storage of a device
329  * @dev:	Device to operate on
330  */
331 void msi_lock_descs(struct device *dev)
332 {
333 	mutex_lock(&dev->msi.data->mutex);
334 }
335 EXPORT_SYMBOL_GPL(msi_lock_descs);
336 
337 /**
338  * msi_unlock_descs - Unlock the MSI descriptor storage of a device
339  * @dev:	Device to operate on
340  */
341 void msi_unlock_descs(struct device *dev)
342 {
343 	/* Invalidate the index which was cached by the iterator */
344 	dev->msi.data->__iter_idx = MSI_XA_MAX_INDEX;
345 	mutex_unlock(&dev->msi.data->mutex);
346 }
347 EXPORT_SYMBOL_GPL(msi_unlock_descs);
348 
349 static struct msi_desc *msi_find_desc(struct msi_device_data *md, unsigned int domid,
350 				      enum msi_desc_filter filter)
351 {
352 	struct xarray *xa = &md->__domains[domid].store;
353 	struct msi_desc *desc;
354 
355 	xa_for_each_start(xa, md->__iter_idx, desc, md->__iter_idx) {
356 		if (msi_desc_match(desc, filter))
357 			return desc;
358 	}
359 	md->__iter_idx = MSI_XA_MAX_INDEX;
360 	return NULL;
361 }
362 
363 /**
364  * msi_domain_first_desc - Get the first MSI descriptor of an irqdomain associated to a device
365  * @dev:	Device to operate on
366  * @domid:	The id of the interrupt domain which should be walked.
367  * @filter:	Descriptor state filter
368  *
369  * Must be called with the MSI descriptor mutex held, i.e. msi_lock_descs()
370  * must be invoked before the call.
371  *
372  * Return: Pointer to the first MSI descriptor matching the search
373  *	   criteria, NULL if none found.
374  */
375 struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid,
376 				       enum msi_desc_filter filter)
377 {
378 	struct msi_device_data *md = dev->msi.data;
379 
380 	if (WARN_ON_ONCE(!md || domid >= MSI_MAX_DEVICE_IRQDOMAINS))
381 		return NULL;
382 
383 	lockdep_assert_held(&md->mutex);
384 
385 	md->__iter_idx = 0;
386 	return msi_find_desc(md, domid, filter);
387 }
388 EXPORT_SYMBOL_GPL(msi_domain_first_desc);
389 
390 /**
391  * msi_next_desc - Get the next MSI descriptor of a device
392  * @dev:	Device to operate on
393  * @domid:	The id of the interrupt domain which should be walked.
394  * @filter:	Descriptor state filter
395  *
396  * The first invocation of msi_next_desc() has to be preceeded by a
397  * successful invocation of __msi_first_desc(). Consecutive invocations are
398  * only valid if the previous one was successful. All these operations have
399  * to be done within the same MSI mutex held region.
400  *
401  * Return: Pointer to the next MSI descriptor matching the search
402  *	   criteria, NULL if none found.
403  */
404 struct msi_desc *msi_next_desc(struct device *dev, unsigned int domid,
405 			       enum msi_desc_filter filter)
406 {
407 	struct msi_device_data *md = dev->msi.data;
408 
409 	if (WARN_ON_ONCE(!md || domid >= MSI_MAX_DEVICE_IRQDOMAINS))
410 		return NULL;
411 
412 	lockdep_assert_held(&md->mutex);
413 
414 	if (md->__iter_idx >= (unsigned long)MSI_MAX_INDEX)
415 		return NULL;
416 
417 	md->__iter_idx++;
418 	return msi_find_desc(md, domid, filter);
419 }
420 EXPORT_SYMBOL_GPL(msi_next_desc);
421 
422 /**
423  * msi_domain_get_virq - Lookup the Linux interrupt number for a MSI index on a interrupt domain
424  * @dev:	Device to operate on
425  * @domid:	Domain ID of the interrupt domain associated to the device
426  * @index:	MSI interrupt index to look for (0-based)
427  *
428  * Return: The Linux interrupt number on success (> 0), 0 if not found
429  */
430 unsigned int msi_domain_get_virq(struct device *dev, unsigned int domid, unsigned int index)
431 {
432 	struct msi_desc *desc;
433 	unsigned int ret = 0;
434 	bool pcimsi = false;
435 	struct xarray *xa;
436 
437 	if (!dev->msi.data)
438 		return 0;
439 
440 	if (WARN_ON_ONCE(index > MSI_MAX_INDEX || domid >= MSI_MAX_DEVICE_IRQDOMAINS))
441 		return 0;
442 
443 	/* This check is only valid for the PCI default MSI domain */
444 	if (dev_is_pci(dev) && domid == MSI_DEFAULT_DOMAIN)
445 		pcimsi = to_pci_dev(dev)->msi_enabled;
446 
447 	msi_lock_descs(dev);
448 	xa = &dev->msi.data->__domains[domid].store;
449 	desc = xa_load(xa, pcimsi ? 0 : index);
450 	if (desc && desc->irq) {
451 		/*
452 		 * PCI-MSI has only one descriptor for multiple interrupts.
453 		 * PCI-MSIX and platform MSI use a descriptor per
454 		 * interrupt.
455 		 */
456 		if (pcimsi) {
457 			if (index < desc->nvec_used)
458 				ret = desc->irq + index;
459 		} else {
460 			ret = desc->irq;
461 		}
462 	}
463 
464 	msi_unlock_descs(dev);
465 	return ret;
466 }
467 EXPORT_SYMBOL_GPL(msi_domain_get_virq);
468 
469 #ifdef CONFIG_SYSFS
470 static struct attribute *msi_dev_attrs[] = {
471 	NULL
472 };
473 
474 static const struct attribute_group msi_irqs_group = {
475 	.name	= "msi_irqs",
476 	.attrs	= msi_dev_attrs,
477 };
478 
479 static inline int msi_sysfs_create_group(struct device *dev)
480 {
481 	return devm_device_add_group(dev, &msi_irqs_group);
482 }
483 
484 static ssize_t msi_mode_show(struct device *dev, struct device_attribute *attr,
485 			     char *buf)
486 {
487 	/* MSI vs. MSIX is per device not per interrupt */
488 	bool is_msix = dev_is_pci(dev) ? to_pci_dev(dev)->msix_enabled : false;
489 
490 	return sysfs_emit(buf, "%s\n", is_msix ? "msix" : "msi");
491 }
492 
493 static void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc)
494 {
495 	struct device_attribute *attrs = desc->sysfs_attrs;
496 	int i;
497 
498 	if (!attrs)
499 		return;
500 
501 	desc->sysfs_attrs = NULL;
502 	for (i = 0; i < desc->nvec_used; i++) {
503 		if (attrs[i].show)
504 			sysfs_remove_file_from_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name);
505 		kfree(attrs[i].attr.name);
506 	}
507 	kfree(attrs);
508 }
509 
510 static int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc)
511 {
512 	struct device_attribute *attrs;
513 	int ret, i;
514 
515 	attrs = kcalloc(desc->nvec_used, sizeof(*attrs), GFP_KERNEL);
516 	if (!attrs)
517 		return -ENOMEM;
518 
519 	desc->sysfs_attrs = attrs;
520 	for (i = 0; i < desc->nvec_used; i++) {
521 		sysfs_attr_init(&attrs[i].attr);
522 		attrs[i].attr.name = kasprintf(GFP_KERNEL, "%d", desc->irq + i);
523 		if (!attrs[i].attr.name) {
524 			ret = -ENOMEM;
525 			goto fail;
526 		}
527 
528 		attrs[i].attr.mode = 0444;
529 		attrs[i].show = msi_mode_show;
530 
531 		ret = sysfs_add_file_to_group(&dev->kobj, &attrs[i].attr, msi_irqs_group.name);
532 		if (ret) {
533 			attrs[i].show = NULL;
534 			goto fail;
535 		}
536 	}
537 	return 0;
538 
539 fail:
540 	msi_sysfs_remove_desc(dev, desc);
541 	return ret;
542 }
543 
544 #ifdef CONFIG_PCI_MSI_ARCH_FALLBACKS
545 /**
546  * msi_device_populate_sysfs - Populate msi_irqs sysfs entries for a device
547  * @dev:	The device (PCI, platform etc) which will get sysfs entries
548  */
549 int msi_device_populate_sysfs(struct device *dev)
550 {
551 	struct msi_desc *desc;
552 	int ret;
553 
554 	msi_for_each_desc(desc, dev, MSI_DESC_ASSOCIATED) {
555 		if (desc->sysfs_attrs)
556 			continue;
557 		ret = msi_sysfs_populate_desc(dev, desc);
558 		if (ret)
559 			return ret;
560 	}
561 	return 0;
562 }
563 
564 /**
565  * msi_device_destroy_sysfs - Destroy msi_irqs sysfs entries for a device
566  * @dev:		The device (PCI, platform etc) for which to remove
567  *			sysfs entries
568  */
569 void msi_device_destroy_sysfs(struct device *dev)
570 {
571 	struct msi_desc *desc;
572 
573 	msi_for_each_desc(desc, dev, MSI_DESC_ALL)
574 		msi_sysfs_remove_desc(dev, desc);
575 }
576 #endif /* CONFIG_PCI_MSI_ARCH_FALLBACK */
577 #else /* CONFIG_SYSFS */
578 static inline int msi_sysfs_create_group(struct device *dev) { return 0; }
579 static inline int msi_sysfs_populate_desc(struct device *dev, struct msi_desc *desc) { return 0; }
580 static inline void msi_sysfs_remove_desc(struct device *dev, struct msi_desc *desc) { }
581 #endif /* !CONFIG_SYSFS */
582 
583 static struct irq_domain *msi_get_device_domain(struct device *dev, unsigned int domid)
584 {
585 	struct irq_domain *domain;
586 
587 	lockdep_assert_held(&dev->msi.data->mutex);
588 
589 	if (WARN_ON_ONCE(domid >= MSI_MAX_DEVICE_IRQDOMAINS))
590 		return NULL;
591 
592 	domain = dev->msi.data->__domains[domid].domain;
593 	if (!domain)
594 		return NULL;
595 
596 	if (WARN_ON_ONCE(irq_domain_is_msi_parent(domain)))
597 		return NULL;
598 
599 	return domain;
600 }
601 
602 static unsigned int msi_domain_get_hwsize(struct device *dev, unsigned int domid)
603 {
604 	struct msi_domain_info *info;
605 	struct irq_domain *domain;
606 
607 	domain = msi_get_device_domain(dev, domid);
608 	if (domain) {
609 		info = domain->host_data;
610 		return info->hwsize;
611 	}
612 	/* No domain, no size... */
613 	return 0;
614 }
615 
616 static inline void irq_chip_write_msi_msg(struct irq_data *data,
617 					  struct msi_msg *msg)
618 {
619 	data->chip->irq_write_msi_msg(data, msg);
620 }
621 
622 static void msi_check_level(struct irq_domain *domain, struct msi_msg *msg)
623 {
624 	struct msi_domain_info *info = domain->host_data;
625 
626 	/*
627 	 * If the MSI provider has messed with the second message and
628 	 * not advertized that it is level-capable, signal the breakage.
629 	 */
630 	WARN_ON(!((info->flags & MSI_FLAG_LEVEL_CAPABLE) &&
631 		  (info->chip->flags & IRQCHIP_SUPPORTS_LEVEL_MSI)) &&
632 		(msg[1].address_lo || msg[1].address_hi || msg[1].data));
633 }
634 
635 /**
636  * msi_domain_set_affinity - Generic affinity setter function for MSI domains
637  * @irq_data:	The irq data associated to the interrupt
638  * @mask:	The affinity mask to set
639  * @force:	Flag to enforce setting (disable online checks)
640  *
641  * Intended to be used by MSI interrupt controllers which are
642  * implemented with hierarchical domains.
643  *
644  * Return: IRQ_SET_MASK_* result code
645  */
646 int msi_domain_set_affinity(struct irq_data *irq_data,
647 			    const struct cpumask *mask, bool force)
648 {
649 	struct irq_data *parent = irq_data->parent_data;
650 	struct msi_msg msg[2] = { [1] = { }, };
651 	int ret;
652 
653 	ret = parent->chip->irq_set_affinity(parent, mask, force);
654 	if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
655 		BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
656 		msi_check_level(irq_data->domain, msg);
657 		irq_chip_write_msi_msg(irq_data, msg);
658 	}
659 
660 	return ret;
661 }
662 
663 static int msi_domain_activate(struct irq_domain *domain,
664 			       struct irq_data *irq_data, bool early)
665 {
666 	struct msi_msg msg[2] = { [1] = { }, };
667 
668 	BUG_ON(irq_chip_compose_msi_msg(irq_data, msg));
669 	msi_check_level(irq_data->domain, msg);
670 	irq_chip_write_msi_msg(irq_data, msg);
671 	return 0;
672 }
673 
674 static void msi_domain_deactivate(struct irq_domain *domain,
675 				  struct irq_data *irq_data)
676 {
677 	struct msi_msg msg[2];
678 
679 	memset(msg, 0, sizeof(msg));
680 	irq_chip_write_msi_msg(irq_data, msg);
681 }
682 
683 static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
684 			    unsigned int nr_irqs, void *arg)
685 {
686 	struct msi_domain_info *info = domain->host_data;
687 	struct msi_domain_ops *ops = info->ops;
688 	irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
689 	int i, ret;
690 
691 	if (irq_find_mapping(domain, hwirq) > 0)
692 		return -EEXIST;
693 
694 	if (domain->parent) {
695 		ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
696 		if (ret < 0)
697 			return ret;
698 	}
699 
700 	for (i = 0; i < nr_irqs; i++) {
701 		ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
702 		if (ret < 0) {
703 			if (ops->msi_free) {
704 				for (i--; i > 0; i--)
705 					ops->msi_free(domain, info, virq + i);
706 			}
707 			irq_domain_free_irqs_top(domain, virq, nr_irqs);
708 			return ret;
709 		}
710 	}
711 
712 	return 0;
713 }
714 
715 static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
716 			    unsigned int nr_irqs)
717 {
718 	struct msi_domain_info *info = domain->host_data;
719 	int i;
720 
721 	if (info->ops->msi_free) {
722 		for (i = 0; i < nr_irqs; i++)
723 			info->ops->msi_free(domain, info, virq + i);
724 	}
725 	irq_domain_free_irqs_top(domain, virq, nr_irqs);
726 }
727 
728 static const struct irq_domain_ops msi_domain_ops = {
729 	.alloc		= msi_domain_alloc,
730 	.free		= msi_domain_free,
731 	.activate	= msi_domain_activate,
732 	.deactivate	= msi_domain_deactivate,
733 };
734 
735 static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info,
736 						msi_alloc_info_t *arg)
737 {
738 	return arg->hwirq;
739 }
740 
741 static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev,
742 				  int nvec, msi_alloc_info_t *arg)
743 {
744 	memset(arg, 0, sizeof(*arg));
745 	return 0;
746 }
747 
748 static void msi_domain_ops_set_desc(msi_alloc_info_t *arg,
749 				    struct msi_desc *desc)
750 {
751 	arg->desc = desc;
752 }
753 
754 static int msi_domain_ops_init(struct irq_domain *domain,
755 			       struct msi_domain_info *info,
756 			       unsigned int virq, irq_hw_number_t hwirq,
757 			       msi_alloc_info_t *arg)
758 {
759 	irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip,
760 				      info->chip_data);
761 	if (info->handler && info->handler_name) {
762 		__irq_set_handler(virq, info->handler, 0, info->handler_name);
763 		if (info->handler_data)
764 			irq_set_handler_data(virq, info->handler_data);
765 	}
766 	return 0;
767 }
768 
769 static struct msi_domain_ops msi_domain_ops_default = {
770 	.get_hwirq		= msi_domain_ops_get_hwirq,
771 	.msi_init		= msi_domain_ops_init,
772 	.msi_prepare		= msi_domain_ops_prepare,
773 	.set_desc		= msi_domain_ops_set_desc,
774 };
775 
776 static void msi_domain_update_dom_ops(struct msi_domain_info *info)
777 {
778 	struct msi_domain_ops *ops = info->ops;
779 
780 	if (ops == NULL) {
781 		info->ops = &msi_domain_ops_default;
782 		return;
783 	}
784 
785 	if (!(info->flags & MSI_FLAG_USE_DEF_DOM_OPS))
786 		return;
787 
788 	if (ops->get_hwirq == NULL)
789 		ops->get_hwirq = msi_domain_ops_default.get_hwirq;
790 	if (ops->msi_init == NULL)
791 		ops->msi_init = msi_domain_ops_default.msi_init;
792 	if (ops->msi_prepare == NULL)
793 		ops->msi_prepare = msi_domain_ops_default.msi_prepare;
794 	if (ops->set_desc == NULL)
795 		ops->set_desc = msi_domain_ops_default.set_desc;
796 }
797 
798 static void msi_domain_update_chip_ops(struct msi_domain_info *info)
799 {
800 	struct irq_chip *chip = info->chip;
801 
802 	BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask);
803 	if (!chip->irq_set_affinity)
804 		chip->irq_set_affinity = msi_domain_set_affinity;
805 }
806 
807 static struct irq_domain *__msi_create_irq_domain(struct fwnode_handle *fwnode,
808 						  struct msi_domain_info *info,
809 						  unsigned int flags,
810 						  struct irq_domain *parent)
811 {
812 	struct irq_domain *domain;
813 
814 	if (info->hwsize > MSI_XA_DOMAIN_SIZE)
815 		return NULL;
816 
817 	/*
818 	 * Hardware size 0 is valid for backwards compatibility and for
819 	 * domains which are not backed by a hardware table. Grant the
820 	 * maximum index space.
821 	 */
822 	if (!info->hwsize)
823 		info->hwsize = MSI_XA_DOMAIN_SIZE;
824 
825 	msi_domain_update_dom_ops(info);
826 	if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
827 		msi_domain_update_chip_ops(info);
828 
829 	domain = irq_domain_create_hierarchy(parent, flags | IRQ_DOMAIN_FLAG_MSI, 0,
830 					     fwnode, &msi_domain_ops, info);
831 
832 	if (domain) {
833 		if (!domain->name && info->chip)
834 			domain->name = info->chip->name;
835 		irq_domain_update_bus_token(domain, info->bus_token);
836 	}
837 
838 	return domain;
839 }
840 
841 /**
842  * msi_create_irq_domain - Create an MSI interrupt domain
843  * @fwnode:	Optional fwnode of the interrupt controller
844  * @info:	MSI domain info
845  * @parent:	Parent irq domain
846  *
847  * Return: pointer to the created &struct irq_domain or %NULL on failure
848  */
849 struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
850 					 struct msi_domain_info *info,
851 					 struct irq_domain *parent)
852 {
853 	return __msi_create_irq_domain(fwnode, info, 0, parent);
854 }
855 
856 /**
857  * msi_parent_init_dev_msi_info - Delegate initialization of device MSI info down
858  *				  in the domain hierarchy
859  * @dev:		The device for which the domain should be created
860  * @domain:		The domain in the hierarchy this op is being called on
861  * @msi_parent_domain:	The IRQ_DOMAIN_FLAG_MSI_PARENT domain for the child to
862  *			be created
863  * @msi_child_info:	The MSI domain info of the IRQ_DOMAIN_FLAG_MSI_DEVICE
864  *			domain to be created
865  *
866  * Return: true on success, false otherwise
867  *
868  * This is the most complex problem of per device MSI domains and the
869  * underlying interrupt domain hierarchy:
870  *
871  * The device domain to be initialized requests the broadest feature set
872  * possible and the underlying domain hierarchy puts restrictions on it.
873  *
874  * That's trivial for a simple parent->child relationship, but it gets
875  * interesting with an intermediate domain: root->parent->child.  The
876  * intermediate 'parent' can expand the capabilities which the 'root'
877  * domain is providing. So that creates a classic hen and egg problem:
878  * Which entity is doing the restrictions/expansions?
879  *
880  * One solution is to let the root domain handle the initialization that's
881  * why there is the @domain and the @msi_parent_domain pointer.
882  */
883 bool msi_parent_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
884 				  struct irq_domain *msi_parent_domain,
885 				  struct msi_domain_info *msi_child_info)
886 {
887 	struct irq_domain *parent = domain->parent;
888 
889 	if (WARN_ON_ONCE(!parent || !parent->msi_parent_ops ||
890 			 !parent->msi_parent_ops->init_dev_msi_info))
891 		return false;
892 
893 	return parent->msi_parent_ops->init_dev_msi_info(dev, parent, msi_parent_domain,
894 							 msi_child_info);
895 }
896 
897 /**
898  * msi_create_device_irq_domain - Create a device MSI interrupt domain
899  * @dev:		Pointer to the device
900  * @domid:		Domain id
901  * @template:		MSI domain info bundle used as template
902  * @hwsize:		Maximum number of MSI table entries (0 if unknown or unlimited)
903  * @domain_data:	Optional pointer to domain specific data which is set in
904  *			msi_domain_info::data
905  * @chip_data:		Optional pointer to chip specific data which is set in
906  *			msi_domain_info::chip_data
907  *
908  * Return: True on success, false otherwise
909  *
910  * There is no firmware node required for this interface because the per
911  * device domains are software constructs which are actually closer to the
912  * hardware reality than any firmware can describe them.
913  *
914  * The domain name and the irq chip name for a MSI device domain are
915  * composed by: "$(PREFIX)$(CHIPNAME)-$(DEVNAME)"
916  *
917  * $PREFIX:   Optional prefix provided by the underlying MSI parent domain
918  *	      via msi_parent_ops::prefix. If that pointer is NULL the prefix
919  *	      is empty.
920  * $CHIPNAME: The name of the irq_chip in @template
921  * $DEVNAME:  The name of the device
922  *
923  * This results in understandable chip names and hardware interrupt numbers
924  * in e.g. /proc/interrupts
925  *
926  * PCI-MSI-0000:00:1c.0     0-edge  Parent domain has no prefix
927  * IR-PCI-MSI-0000:00:1c.4  0-edge  Same with interrupt remapping prefix 'IR-'
928  *
929  * IR-PCI-MSIX-0000:3d:00.0 0-edge  Hardware interrupt numbers reflect
930  * IR-PCI-MSIX-0000:3d:00.0 1-edge  the real MSI-X index on that device
931  * IR-PCI-MSIX-0000:3d:00.0 2-edge
932  *
933  * On IMS domains the hardware interrupt number is either a table entry
934  * index or a purely software managed index but it is guaranteed to be
935  * unique.
936  *
937  * The domain pointer is stored in @dev::msi::data::__irqdomains[]. All
938  * subsequent operations on the domain depend on the domain id.
939  *
940  * The domain is automatically freed when the device is removed via devres
941  * in the context of @dev::msi::data freeing, but it can also be
942  * independently removed via @msi_remove_device_irq_domain().
943  */
944 bool msi_create_device_irq_domain(struct device *dev, unsigned int domid,
945 				  const struct msi_domain_template *template,
946 				  unsigned int hwsize, void *domain_data,
947 				  void *chip_data)
948 {
949 	struct irq_domain *domain, *parent = dev->msi.domain;
950 	const struct msi_parent_ops *pops;
951 	struct msi_domain_template *bundle;
952 	struct fwnode_handle *fwnode;
953 
954 	if (!irq_domain_is_msi_parent(parent))
955 		return false;
956 
957 	if (domid >= MSI_MAX_DEVICE_IRQDOMAINS)
958 		return false;
959 
960 	bundle = kmemdup(template, sizeof(*bundle), GFP_KERNEL);
961 	if (!bundle)
962 		return false;
963 
964 	bundle->info.hwsize = hwsize;
965 	bundle->info.chip = &bundle->chip;
966 	bundle->info.ops = &bundle->ops;
967 	bundle->info.data = domain_data;
968 	bundle->info.chip_data = chip_data;
969 
970 	pops = parent->msi_parent_ops;
971 	snprintf(bundle->name, sizeof(bundle->name), "%s%s-%s",
972 		 pops->prefix ? : "", bundle->chip.name, dev_name(dev));
973 	bundle->chip.name = bundle->name;
974 
975 	fwnode = irq_domain_alloc_named_fwnode(bundle->name);
976 	if (!fwnode)
977 		goto free_bundle;
978 
979 	if (msi_setup_device_data(dev))
980 		goto free_fwnode;
981 
982 	msi_lock_descs(dev);
983 
984 	if (WARN_ON_ONCE(msi_get_device_domain(dev, domid)))
985 		goto fail;
986 
987 	if (!pops->init_dev_msi_info(dev, parent, parent, &bundle->info))
988 		goto fail;
989 
990 	domain = __msi_create_irq_domain(fwnode, &bundle->info, IRQ_DOMAIN_FLAG_MSI_DEVICE, parent);
991 	if (!domain)
992 		goto fail;
993 
994 	domain->dev = dev;
995 	dev->msi.data->__domains[domid].domain = domain;
996 	msi_unlock_descs(dev);
997 	return true;
998 
999 fail:
1000 	msi_unlock_descs(dev);
1001 free_fwnode:
1002 	kfree(fwnode);
1003 free_bundle:
1004 	kfree(bundle);
1005 	return false;
1006 }
1007 
1008 /**
1009  * msi_remove_device_irq_domain - Free a device MSI interrupt domain
1010  * @dev:	Pointer to the device
1011  * @domid:	Domain id
1012  */
1013 void msi_remove_device_irq_domain(struct device *dev, unsigned int domid)
1014 {
1015 	struct msi_domain_info *info;
1016 	struct irq_domain *domain;
1017 
1018 	msi_lock_descs(dev);
1019 
1020 	domain = msi_get_device_domain(dev, domid);
1021 
1022 	if (!domain || !irq_domain_is_msi_device(domain))
1023 		goto unlock;
1024 
1025 	dev->msi.data->__domains[domid].domain = NULL;
1026 	info = domain->host_data;
1027 	irq_domain_remove(domain);
1028 	kfree(container_of(info, struct msi_domain_template, info));
1029 
1030 unlock:
1031 	msi_unlock_descs(dev);
1032 }
1033 
1034 /**
1035  * msi_match_device_irq_domain - Match a device irq domain against a bus token
1036  * @dev:	Pointer to the device
1037  * @domid:	Domain id
1038  * @bus_token:	Bus token to match against the domain bus token
1039  *
1040  * Return: True if device domain exists and bus tokens match.
1041  */
1042 bool msi_match_device_irq_domain(struct device *dev, unsigned int domid,
1043 				 enum irq_domain_bus_token bus_token)
1044 {
1045 	struct msi_domain_info *info;
1046 	struct irq_domain *domain;
1047 	bool ret = false;
1048 
1049 	msi_lock_descs(dev);
1050 	domain = msi_get_device_domain(dev, domid);
1051 	if (domain && irq_domain_is_msi_device(domain)) {
1052 		info = domain->host_data;
1053 		ret = info->bus_token == bus_token;
1054 	}
1055 	msi_unlock_descs(dev);
1056 	return ret;
1057 }
1058 
1059 int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
1060 			    int nvec, msi_alloc_info_t *arg)
1061 {
1062 	struct msi_domain_info *info = domain->host_data;
1063 	struct msi_domain_ops *ops = info->ops;
1064 
1065 	return ops->msi_prepare(domain, dev, nvec, arg);
1066 }
1067 
1068 int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
1069 			     int virq_base, int nvec, msi_alloc_info_t *arg)
1070 {
1071 	struct msi_domain_info *info = domain->host_data;
1072 	struct msi_domain_ops *ops = info->ops;
1073 	struct msi_ctrl ctrl = {
1074 		.domid	= MSI_DEFAULT_DOMAIN,
1075 		.first  = virq_base,
1076 		.last	= virq_base + nvec - 1,
1077 	};
1078 	struct msi_desc *desc;
1079 	struct xarray *xa;
1080 	int ret, virq;
1081 
1082 	if (!msi_ctrl_valid(dev, &ctrl))
1083 		return -EINVAL;
1084 
1085 	msi_lock_descs(dev);
1086 	ret = msi_domain_add_simple_msi_descs(dev, &ctrl);
1087 	if (ret)
1088 		goto unlock;
1089 
1090 	xa = &dev->msi.data->__domains[ctrl.domid].store;
1091 
1092 	for (virq = virq_base; virq < virq_base + nvec; virq++) {
1093 		desc = xa_load(xa, virq);
1094 		desc->irq = virq;
1095 
1096 		ops->set_desc(arg, desc);
1097 		ret = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1098 		if (ret)
1099 			goto fail;
1100 
1101 		irq_set_msi_desc(virq, desc);
1102 	}
1103 	msi_unlock_descs(dev);
1104 	return 0;
1105 
1106 fail:
1107 	for (--virq; virq >= virq_base; virq--)
1108 		irq_domain_free_irqs_common(domain, virq, 1);
1109 	msi_domain_free_descs(dev, &ctrl);
1110 unlock:
1111 	msi_unlock_descs(dev);
1112 	return ret;
1113 }
1114 
1115 /*
1116  * Carefully check whether the device can use reservation mode. If
1117  * reservation mode is enabled then the early activation will assign a
1118  * dummy vector to the device. If the PCI/MSI device does not support
1119  * masking of the entry then this can result in spurious interrupts when
1120  * the device driver is not absolutely careful. But even then a malfunction
1121  * of the hardware could result in a spurious interrupt on the dummy vector
1122  * and render the device unusable. If the entry can be masked then the core
1123  * logic will prevent the spurious interrupt and reservation mode can be
1124  * used. For now reservation mode is restricted to PCI/MSI.
1125  */
1126 static bool msi_check_reservation_mode(struct irq_domain *domain,
1127 				       struct msi_domain_info *info,
1128 				       struct device *dev)
1129 {
1130 	struct msi_desc *desc;
1131 
1132 	switch(domain->bus_token) {
1133 	case DOMAIN_BUS_PCI_MSI:
1134 	case DOMAIN_BUS_PCI_DEVICE_MSI:
1135 	case DOMAIN_BUS_PCI_DEVICE_MSIX:
1136 	case DOMAIN_BUS_VMD_MSI:
1137 		break;
1138 	default:
1139 		return false;
1140 	}
1141 
1142 	if (!(info->flags & MSI_FLAG_MUST_REACTIVATE))
1143 		return false;
1144 
1145 	if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask)
1146 		return false;
1147 
1148 	/*
1149 	 * Checking the first MSI descriptor is sufficient. MSIX supports
1150 	 * masking and MSI does so when the can_mask attribute is set.
1151 	 */
1152 	desc = msi_first_desc(dev, MSI_DESC_ALL);
1153 	return desc->pci.msi_attrib.is_msix || desc->pci.msi_attrib.can_mask;
1154 }
1155 
1156 static int msi_handle_pci_fail(struct irq_domain *domain, struct msi_desc *desc,
1157 			       int allocated)
1158 {
1159 	switch(domain->bus_token) {
1160 	case DOMAIN_BUS_PCI_MSI:
1161 	case DOMAIN_BUS_PCI_DEVICE_MSI:
1162 	case DOMAIN_BUS_PCI_DEVICE_MSIX:
1163 	case DOMAIN_BUS_VMD_MSI:
1164 		if (IS_ENABLED(CONFIG_PCI_MSI))
1165 			break;
1166 		fallthrough;
1167 	default:
1168 		return -ENOSPC;
1169 	}
1170 
1171 	/* Let a failed PCI multi MSI allocation retry */
1172 	if (desc->nvec_used > 1)
1173 		return 1;
1174 
1175 	/* If there was a successful allocation let the caller know */
1176 	return allocated ? allocated : -ENOSPC;
1177 }
1178 
1179 #define VIRQ_CAN_RESERVE	0x01
1180 #define VIRQ_ACTIVATE		0x02
1181 #define VIRQ_NOMASK_QUIRK	0x04
1182 
1183 static int msi_init_virq(struct irq_domain *domain, int virq, unsigned int vflags)
1184 {
1185 	struct irq_data *irqd = irq_domain_get_irq_data(domain, virq);
1186 	int ret;
1187 
1188 	if (!(vflags & VIRQ_CAN_RESERVE)) {
1189 		irqd_clr_can_reserve(irqd);
1190 		if (vflags & VIRQ_NOMASK_QUIRK)
1191 			irqd_set_msi_nomask_quirk(irqd);
1192 
1193 		/*
1194 		 * If the interrupt is managed but no CPU is available to
1195 		 * service it, shut it down until better times. Note that
1196 		 * we only do this on the !RESERVE path as x86 (the only
1197 		 * architecture using this flag) deals with this in a
1198 		 * different way by using a catch-all vector.
1199 		 */
1200 		if ((vflags & VIRQ_ACTIVATE) &&
1201 		    irqd_affinity_is_managed(irqd) &&
1202 		    !cpumask_intersects(irq_data_get_affinity_mask(irqd),
1203 					cpu_online_mask)) {
1204 			    irqd_set_managed_shutdown(irqd);
1205 			    return 0;
1206 		    }
1207 	}
1208 
1209 	if (!(vflags & VIRQ_ACTIVATE))
1210 		return 0;
1211 
1212 	ret = irq_domain_activate_irq(irqd, vflags & VIRQ_CAN_RESERVE);
1213 	if (ret)
1214 		return ret;
1215 	/*
1216 	 * If the interrupt uses reservation mode, clear the activated bit
1217 	 * so request_irq() will assign the final vector.
1218 	 */
1219 	if (vflags & VIRQ_CAN_RESERVE)
1220 		irqd_clr_activated(irqd);
1221 	return 0;
1222 }
1223 
1224 static int __msi_domain_alloc_irqs(struct device *dev, struct irq_domain *domain,
1225 				   struct msi_ctrl *ctrl)
1226 {
1227 	struct xarray *xa = &dev->msi.data->__domains[ctrl->domid].store;
1228 	struct msi_domain_info *info = domain->host_data;
1229 	struct msi_domain_ops *ops = info->ops;
1230 	unsigned int vflags = 0, allocated = 0;
1231 	msi_alloc_info_t arg = { };
1232 	struct msi_desc *desc;
1233 	unsigned long idx;
1234 	int i, ret, virq;
1235 
1236 	ret = msi_domain_prepare_irqs(domain, dev, ctrl->nirqs, &arg);
1237 	if (ret)
1238 		return ret;
1239 
1240 	/*
1241 	 * This flag is set by the PCI layer as we need to activate
1242 	 * the MSI entries before the PCI layer enables MSI in the
1243 	 * card. Otherwise the card latches a random msi message.
1244 	 */
1245 	if (info->flags & MSI_FLAG_ACTIVATE_EARLY)
1246 		vflags |= VIRQ_ACTIVATE;
1247 
1248 	/*
1249 	 * Interrupt can use a reserved vector and will not occupy
1250 	 * a real device vector until the interrupt is requested.
1251 	 */
1252 	if (msi_check_reservation_mode(domain, info, dev)) {
1253 		vflags |= VIRQ_CAN_RESERVE;
1254 		/*
1255 		 * MSI affinity setting requires a special quirk (X86) when
1256 		 * reservation mode is active.
1257 		 */
1258 		if (info->flags & MSI_FLAG_NOMASK_QUIRK)
1259 			vflags |= VIRQ_NOMASK_QUIRK;
1260 	}
1261 
1262 	xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) {
1263 		if (!msi_desc_match(desc, MSI_DESC_NOTASSOCIATED))
1264 			continue;
1265 
1266 		/* This should return -ECONFUSED... */
1267 		if (WARN_ON_ONCE(allocated >= ctrl->nirqs))
1268 			return -EINVAL;
1269 
1270 		if (ops->prepare_desc)
1271 			ops->prepare_desc(domain, &arg, desc);
1272 
1273 		ops->set_desc(&arg, desc);
1274 
1275 		virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
1276 					       dev_to_node(dev), &arg, false,
1277 					       desc->affinity);
1278 		if (virq < 0)
1279 			return msi_handle_pci_fail(domain, desc, allocated);
1280 
1281 		for (i = 0; i < desc->nvec_used; i++) {
1282 			irq_set_msi_desc_off(virq, i, desc);
1283 			irq_debugfs_copy_devname(virq + i, dev);
1284 			ret = msi_init_virq(domain, virq + i, vflags);
1285 			if (ret)
1286 				return ret;
1287 		}
1288 		if (info->flags & MSI_FLAG_DEV_SYSFS) {
1289 			ret = msi_sysfs_populate_desc(dev, desc);
1290 			if (ret)
1291 				return ret;
1292 		}
1293 		allocated++;
1294 	}
1295 	return 0;
1296 }
1297 
1298 static int msi_domain_alloc_simple_msi_descs(struct device *dev,
1299 					     struct msi_domain_info *info,
1300 					     struct msi_ctrl *ctrl)
1301 {
1302 	if (!(info->flags & MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS))
1303 		return 0;
1304 
1305 	return msi_domain_add_simple_msi_descs(dev, ctrl);
1306 }
1307 
1308 static int __msi_domain_alloc_locked(struct device *dev, struct msi_ctrl *ctrl)
1309 {
1310 	struct msi_domain_info *info;
1311 	struct msi_domain_ops *ops;
1312 	struct irq_domain *domain;
1313 	int ret;
1314 
1315 	if (!msi_ctrl_valid(dev, ctrl))
1316 		return -EINVAL;
1317 
1318 	domain = msi_get_device_domain(dev, ctrl->domid);
1319 	if (!domain)
1320 		return -ENODEV;
1321 
1322 	info = domain->host_data;
1323 
1324 	ret = msi_domain_alloc_simple_msi_descs(dev, info, ctrl);
1325 	if (ret)
1326 		return ret;
1327 
1328 	ops = info->ops;
1329 	if (ops->domain_alloc_irqs)
1330 		return ops->domain_alloc_irqs(domain, dev, ctrl->nirqs);
1331 
1332 	return __msi_domain_alloc_irqs(dev, domain, ctrl);
1333 }
1334 
1335 static int msi_domain_alloc_locked(struct device *dev, struct msi_ctrl *ctrl)
1336 {
1337 	int ret = __msi_domain_alloc_locked(dev, ctrl);
1338 
1339 	if (ret)
1340 		msi_domain_free_locked(dev, ctrl);
1341 	return ret;
1342 }
1343 
1344 /**
1345  * msi_domain_alloc_irqs_range_locked - Allocate interrupts from a MSI interrupt domain
1346  * @dev:	Pointer to device struct of the device for which the interrupts
1347  *		are allocated
1348  * @domid:	Id of the interrupt domain to operate on
1349  * @first:	First index to allocate (inclusive)
1350  * @last:	Last index to allocate (inclusive)
1351  *
1352  * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
1353  * pair. Use this for MSI irqdomains which implement their own descriptor
1354  * allocation/free.
1355  *
1356  * Return: %0 on success or an error code.
1357  */
1358 int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
1359 				       unsigned int first, unsigned int last)
1360 {
1361 	struct msi_ctrl ctrl = {
1362 		.domid	= domid,
1363 		.first	= first,
1364 		.last	= last,
1365 		.nirqs	= last + 1 - first,
1366 	};
1367 
1368 	return msi_domain_alloc_locked(dev, &ctrl);
1369 }
1370 
1371 /**
1372  * msi_domain_alloc_irqs_range - Allocate interrupts from a MSI interrupt domain
1373  * @dev:	Pointer to device struct of the device for which the interrupts
1374  *		are allocated
1375  * @domid:	Id of the interrupt domain to operate on
1376  * @first:	First index to allocate (inclusive)
1377  * @last:	Last index to allocate (inclusive)
1378  *
1379  * Return: %0 on success or an error code.
1380  */
1381 int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
1382 				unsigned int first, unsigned int last)
1383 {
1384 	int ret;
1385 
1386 	msi_lock_descs(dev);
1387 	ret = msi_domain_alloc_irqs_range_locked(dev, domid, first, last);
1388 	msi_unlock_descs(dev);
1389 	return ret;
1390 }
1391 
1392 /**
1393  * msi_domain_alloc_irqs_all_locked - Allocate all interrupts from a MSI interrupt domain
1394  *
1395  * @dev:	Pointer to device struct of the device for which the interrupts
1396  *		are allocated
1397  * @domid:	Id of the interrupt domain to operate on
1398  * @nirqs:	The number of interrupts to allocate
1399  *
1400  * This function scans all MSI descriptors of the MSI domain and allocates interrupts
1401  * for all unassigned ones. That function is to be used for MSI domain usage where
1402  * the descriptor allocation is handled at the call site, e.g. PCI/MSI[X].
1403  *
1404  * Return: %0 on success or an error code.
1405  */
1406 int msi_domain_alloc_irqs_all_locked(struct device *dev, unsigned int domid, int nirqs)
1407 {
1408 	struct msi_ctrl ctrl = {
1409 		.domid	= domid,
1410 		.first	= 0,
1411 		.last	= msi_domain_get_hwsize(dev, domid) - 1,
1412 		.nirqs	= nirqs,
1413 	};
1414 
1415 	return msi_domain_alloc_locked(dev, &ctrl);
1416 }
1417 
1418 /**
1419  * msi_domain_alloc_irq_at - Allocate an interrupt from a MSI interrupt domain at
1420  *			     a given index - or at the next free index
1421  *
1422  * @dev:	Pointer to device struct of the device for which the interrupts
1423  *		are allocated
1424  * @domid:	Id of the interrupt domain to operate on
1425  * @index:	Index for allocation. If @index == %MSI_ANY_INDEX the allocation
1426  *		uses the next free index.
1427  * @affdesc:	Optional pointer to an interrupt affinity descriptor structure
1428  * @icookie:	Optional pointer to a domain specific per instance cookie. If
1429  *		non-NULL the content of the cookie is stored in msi_desc::data.
1430  *		Must be NULL for MSI-X allocations
1431  *
1432  * This requires a MSI interrupt domain which lets the core code manage the
1433  * MSI descriptors.
1434  *
1435  * Return: struct msi_map
1436  *
1437  *	On success msi_map::index contains the allocated index number and
1438  *	msi_map::virq the corresponding Linux interrupt number
1439  *
1440  *	On failure msi_map::index contains the error code and msi_map::virq
1441  *	is %0.
1442  */
1443 struct msi_map msi_domain_alloc_irq_at(struct device *dev, unsigned int domid, unsigned int index,
1444 				       const struct irq_affinity_desc *affdesc,
1445 				       union msi_instance_cookie *icookie)
1446 {
1447 	struct msi_ctrl ctrl = { .domid	= domid, .nirqs = 1, };
1448 	struct irq_domain *domain;
1449 	struct msi_map map = { };
1450 	struct msi_desc *desc;
1451 	int ret;
1452 
1453 	msi_lock_descs(dev);
1454 	domain = msi_get_device_domain(dev, domid);
1455 	if (!domain) {
1456 		map.index = -ENODEV;
1457 		goto unlock;
1458 	}
1459 
1460 	desc = msi_alloc_desc(dev, 1, affdesc);
1461 	if (!desc) {
1462 		map.index = -ENOMEM;
1463 		goto unlock;
1464 	}
1465 
1466 	if (icookie)
1467 		desc->data.icookie = *icookie;
1468 
1469 	ret = msi_insert_desc(dev, desc, domid, index);
1470 	if (ret) {
1471 		map.index = ret;
1472 		goto unlock;
1473 	}
1474 
1475 	ctrl.first = ctrl.last = desc->msi_index;
1476 
1477 	ret = __msi_domain_alloc_irqs(dev, domain, &ctrl);
1478 	if (ret) {
1479 		map.index = ret;
1480 		msi_domain_free_locked(dev, &ctrl);
1481 	} else {
1482 		map.index = desc->msi_index;
1483 		map.virq = desc->irq;
1484 	}
1485 unlock:
1486 	msi_unlock_descs(dev);
1487 	return map;
1488 }
1489 
1490 static void __msi_domain_free_irqs(struct device *dev, struct irq_domain *domain,
1491 				   struct msi_ctrl *ctrl)
1492 {
1493 	struct xarray *xa = &dev->msi.data->__domains[ctrl->domid].store;
1494 	struct msi_domain_info *info = domain->host_data;
1495 	struct irq_data *irqd;
1496 	struct msi_desc *desc;
1497 	unsigned long idx;
1498 	int i;
1499 
1500 	xa_for_each_range(xa, idx, desc, ctrl->first, ctrl->last) {
1501 		/* Only handle MSI entries which have an interrupt associated */
1502 		if (!msi_desc_match(desc, MSI_DESC_ASSOCIATED))
1503 			continue;
1504 
1505 		/* Make sure all interrupts are deactivated */
1506 		for (i = 0; i < desc->nvec_used; i++) {
1507 			irqd = irq_domain_get_irq_data(domain, desc->irq + i);
1508 			if (irqd && irqd_is_activated(irqd))
1509 				irq_domain_deactivate_irq(irqd);
1510 		}
1511 
1512 		irq_domain_free_irqs(desc->irq, desc->nvec_used);
1513 		if (info->flags & MSI_FLAG_DEV_SYSFS)
1514 			msi_sysfs_remove_desc(dev, desc);
1515 		desc->irq = 0;
1516 	}
1517 }
1518 
1519 static void msi_domain_free_locked(struct device *dev, struct msi_ctrl *ctrl)
1520 {
1521 	struct msi_domain_info *info;
1522 	struct msi_domain_ops *ops;
1523 	struct irq_domain *domain;
1524 
1525 	if (!msi_ctrl_valid(dev, ctrl))
1526 		return;
1527 
1528 	domain = msi_get_device_domain(dev, ctrl->domid);
1529 	if (!domain)
1530 		return;
1531 
1532 	info = domain->host_data;
1533 	ops = info->ops;
1534 
1535 	if (ops->domain_free_irqs)
1536 		ops->domain_free_irqs(domain, dev);
1537 	else
1538 		__msi_domain_free_irqs(dev, domain, ctrl);
1539 
1540 	if (ops->msi_post_free)
1541 		ops->msi_post_free(domain, dev);
1542 
1543 	if (info->flags & MSI_FLAG_FREE_MSI_DESCS)
1544 		msi_domain_free_descs(dev, ctrl);
1545 }
1546 
1547 /**
1548  * msi_domain_free_irqs_range_locked - Free a range of interrupts from a MSI interrupt domain
1549  *				       associated to @dev with msi_lock held
1550  * @dev:	Pointer to device struct of the device for which the interrupts
1551  *		are freed
1552  * @domid:	Id of the interrupt domain to operate on
1553  * @first:	First index to free (inclusive)
1554  * @last:	Last index to free (inclusive)
1555  */
1556 void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
1557 				       unsigned int first, unsigned int last)
1558 {
1559 	struct msi_ctrl ctrl = {
1560 		.domid	= domid,
1561 		.first	= first,
1562 		.last	= last,
1563 	};
1564 	msi_domain_free_locked(dev, &ctrl);
1565 }
1566 
1567 /**
1568  * msi_domain_free_irqs_range - Free a range of interrupts from a MSI interrupt domain
1569  *				associated to @dev
1570  * @dev:	Pointer to device struct of the device for which the interrupts
1571  *		are freed
1572  * @domid:	Id of the interrupt domain to operate on
1573  * @first:	First index to free (inclusive)
1574  * @last:	Last index to free (inclusive)
1575  */
1576 void msi_domain_free_irqs_range(struct device *dev, unsigned int domid,
1577 				unsigned int first, unsigned int last)
1578 {
1579 	msi_lock_descs(dev);
1580 	msi_domain_free_irqs_range_locked(dev, domid, first, last);
1581 	msi_unlock_descs(dev);
1582 }
1583 
1584 /**
1585  * msi_domain_free_irqs_all_locked - Free all interrupts from a MSI interrupt domain
1586  *				     associated to a device
1587  * @dev:	Pointer to device struct of the device for which the interrupts
1588  *		are freed
1589  * @domid:	The id of the domain to operate on
1590  *
1591  * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
1592  * pair. Use this for MSI irqdomains which implement their own vector
1593  * allocation.
1594  */
1595 void msi_domain_free_irqs_all_locked(struct device *dev, unsigned int domid)
1596 {
1597 	msi_domain_free_irqs_range_locked(dev, domid, 0,
1598 					  msi_domain_get_hwsize(dev, domid) - 1);
1599 }
1600 
1601 /**
1602  * msi_domain_free_irqs_all - Free all interrupts from a MSI interrupt domain
1603  *			      associated to a device
1604  * @dev:	Pointer to device struct of the device for which the interrupts
1605  *		are freed
1606  * @domid:	The id of the domain to operate on
1607  */
1608 void msi_domain_free_irqs_all(struct device *dev, unsigned int domid)
1609 {
1610 	msi_lock_descs(dev);
1611 	msi_domain_free_irqs_all_locked(dev, domid);
1612 	msi_unlock_descs(dev);
1613 }
1614 
1615 /**
1616  * msi_get_domain_info - Get the MSI interrupt domain info for @domain
1617  * @domain:	The interrupt domain to retrieve data from
1618  *
1619  * Return: the pointer to the msi_domain_info stored in @domain->host_data.
1620  */
1621 struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
1622 {
1623 	return (struct msi_domain_info *)domain->host_data;
1624 }
1625