xref: /linux/drivers/irqchip/irq-msi-lib.c (revision 6efc0ab3b05de0d7bab8ec0597214e4788251071)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2022 Linutronix GmbH
3 // Copyright (C) 2022 Intel
4 
5 #include <linux/export.h>
6 
7 #include "irq-msi-lib.h"
8 
9 /**
10  * msi_lib_init_dev_msi_info - Domain info setup for MSI domains
11  * @dev:		The device for which the domain is created for
12  * @domain:		The domain providing this callback
13  * @real_parent:	The real parent domain of the domain to be initialized
14  *			which might be a domain built on top of @domain or
15  *			@domain itself
16  * @info:		The domain info for the domain to be initialize
17  *
18  * This function is to be used for all types of MSI domains above the root
19  * parent domain and any intermediates. The topmost parent domain specific
20  * functionality is determined via @real_parent.
21  *
22  * All intermediate domains between the root and the device domain must
23  * have either msi_parent_ops.init_dev_msi_info = msi_parent_init_dev_msi_info
24  * or invoke it down the line.
25  */
26 bool msi_lib_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
27 			       struct irq_domain *real_parent,
28 			       struct msi_domain_info *info)
29 {
30 	const struct msi_parent_ops *pops = real_parent->msi_parent_ops;
31 	u32 required_flags;
32 
33 	/* Parent ops available? */
34 	if (WARN_ON_ONCE(!pops))
35 		return false;
36 
37 	/*
38 	 * MSI parent domain specific settings. For now there is only the
39 	 * root parent domain, e.g. NEXUS, acting as a MSI parent, but it is
40 	 * possible to stack MSI parents. See x86 vector -> irq remapping
41 	 */
42 	if (domain->bus_token == pops->bus_select_token) {
43 		if (WARN_ON_ONCE(domain != real_parent))
44 			return false;
45 	} else {
46 		WARN_ON_ONCE(1);
47 		return false;
48 	}
49 
50 	required_flags = pops->required_flags;
51 
52 	/* Is the target domain bus token supported? */
53 	switch(info->bus_token) {
54 	case DOMAIN_BUS_PCI_DEVICE_MSI:
55 	case DOMAIN_BUS_PCI_DEVICE_MSIX:
56 		if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PCI_MSI)))
57 			return false;
58 
59 		break;
60 	case DOMAIN_BUS_DEVICE_MSI:
61 		/*
62 		 * Per device MSI should never have any MSI feature bits
63 		 * set. It's sole purpose is to create a dumb interrupt
64 		 * chip which has a device specific irq_write_msi_msg()
65 		 * callback.
66 		 */
67 		if (WARN_ON_ONCE(info->flags))
68 			return false;
69 
70 		/* Core managed MSI descriptors */
71 		info->flags = MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS | MSI_FLAG_FREE_MSI_DESCS;
72 		fallthrough;
73 	case DOMAIN_BUS_WIRED_TO_MSI:
74 		/* Remove PCI specific flags */
75 		required_flags &= ~MSI_FLAG_PCI_MSI_MASK_PARENT;
76 		break;
77 	default:
78 		/*
79 		 * This should never be reached. See
80 		 * msi_lib_irq_domain_select()
81 		 */
82 		WARN_ON_ONCE(1);
83 		return false;
84 	}
85 
86 	/*
87 	 * Mask out the domain specific MSI feature flags which are not
88 	 * supported by the real parent.
89 	 */
90 	info->flags			&= pops->supported_flags;
91 	/* Enforce the required flags */
92 	info->flags			|= required_flags;
93 
94 	/* Chip updates for all child bus types */
95 	if (!info->chip->irq_eoi)
96 		info->chip->irq_eoi	= irq_chip_eoi_parent;
97 	if (!info->chip->irq_ack)
98 		info->chip->irq_ack	= irq_chip_ack_parent;
99 
100 	/*
101 	 * The device MSI domain can never have a set affinity callback. It
102 	 * always has to rely on the parent domain to handle affinity
103 	 * settings. The device MSI domain just has to write the resulting
104 	 * MSI message into the hardware which is the whole purpose of the
105 	 * device MSI domain aside of mask/unmask which is provided e.g. by
106 	 * PCI/MSI device domains.
107 	 */
108 	info->chip->irq_set_affinity	= msi_domain_set_affinity;
109 	return true;
110 }
111 EXPORT_SYMBOL_GPL(msi_lib_init_dev_msi_info);
112 
113 /**
114  * msi_lib_irq_domain_select - Shared select function for NEXUS domains
115  * @d:		Pointer to the irq domain on which select is invoked
116  * @fwspec:	Firmware spec describing what is searched
117  * @bus_token:	The bus token for which a matching irq domain is looked up
118  *
119  * Returns:	%0 if @d is not what is being looked for
120  *
121  *		%1 if @d is either the domain which is directly searched for or
122  *		   if @d is providing the parent MSI domain for the functionality
123  *			 requested with @bus_token.
124  */
125 int msi_lib_irq_domain_select(struct irq_domain *d, struct irq_fwspec *fwspec,
126 			      enum irq_domain_bus_token bus_token)
127 {
128 	const struct msi_parent_ops *ops = d->msi_parent_ops;
129 	u32 busmask = BIT(bus_token);
130 
131 	if (fwspec->fwnode != d->fwnode || fwspec->param_count != 0)
132 		return 0;
133 
134 	/* Handle pure domain searches */
135 	if (bus_token == ops->bus_select_token)
136 		return 1;
137 
138 	return ops && !!(ops->bus_select_mask & busmask);
139 }
140 EXPORT_SYMBOL_GPL(msi_lib_irq_domain_select);
141