xref: /linux/drivers/irqchip/irq-msi-lib.c (revision 9a754292e88f81acaf26cc5e0cec56b3276029de)
172e257c6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
272e257c6SThomas Gleixner // Copyright (C) 2022 Linutronix GmbH
372e257c6SThomas Gleixner // Copyright (C) 2022 Intel
472e257c6SThomas Gleixner 
572e257c6SThomas Gleixner #include <linux/export.h>
672e257c6SThomas Gleixner 
772e257c6SThomas Gleixner #include "irq-msi-lib.h"
872e257c6SThomas Gleixner 
972e257c6SThomas Gleixner /**
1072e257c6SThomas Gleixner  * msi_lib_init_dev_msi_info - Domain info setup for MSI domains
1172e257c6SThomas Gleixner  * @dev:		The device for which the domain is created for
1272e257c6SThomas Gleixner  * @domain:		The domain providing this callback
1372e257c6SThomas Gleixner  * @real_parent:	The real parent domain of the domain to be initialized
1472e257c6SThomas Gleixner  *			which might be a domain built on top of @domain or
1572e257c6SThomas Gleixner  *			@domain itself
1672e257c6SThomas Gleixner  * @info:		The domain info for the domain to be initialize
1772e257c6SThomas Gleixner  *
1872e257c6SThomas Gleixner  * This function is to be used for all types of MSI domains above the root
1972e257c6SThomas Gleixner  * parent domain and any intermediates. The topmost parent domain specific
2072e257c6SThomas Gleixner  * functionality is determined via @real_parent.
2172e257c6SThomas Gleixner  *
2272e257c6SThomas Gleixner  * All intermediate domains between the root and the device domain must
2372e257c6SThomas Gleixner  * have either msi_parent_ops.init_dev_msi_info = msi_parent_init_dev_msi_info
2472e257c6SThomas Gleixner  * or invoke it down the line.
2572e257c6SThomas Gleixner  */
msi_lib_init_dev_msi_info(struct device * dev,struct irq_domain * domain,struct irq_domain * real_parent,struct msi_domain_info * info)2672e257c6SThomas Gleixner bool msi_lib_init_dev_msi_info(struct device *dev, struct irq_domain *domain,
2772e257c6SThomas Gleixner 			       struct irq_domain *real_parent,
2872e257c6SThomas Gleixner 			       struct msi_domain_info *info)
2972e257c6SThomas Gleixner {
3072e257c6SThomas Gleixner 	const struct msi_parent_ops *pops = real_parent->msi_parent_ops;
318c41ccecSThomas Gleixner 	u32 required_flags;
3272e257c6SThomas Gleixner 
3372e257c6SThomas Gleixner 	/* Parent ops available? */
3472e257c6SThomas Gleixner 	if (WARN_ON_ONCE(!pops))
3572e257c6SThomas Gleixner 		return false;
3672e257c6SThomas Gleixner 
3772e257c6SThomas Gleixner 	/*
3872e257c6SThomas Gleixner 	 * MSI parent domain specific settings. For now there is only the
3972e257c6SThomas Gleixner 	 * root parent domain, e.g. NEXUS, acting as a MSI parent, but it is
4072e257c6SThomas Gleixner 	 * possible to stack MSI parents. See x86 vector -> irq remapping
4172e257c6SThomas Gleixner 	 */
4272e257c6SThomas Gleixner 	if (domain->bus_token == pops->bus_select_token) {
4372e257c6SThomas Gleixner 		if (WARN_ON_ONCE(domain != real_parent))
4472e257c6SThomas Gleixner 			return false;
4572e257c6SThomas Gleixner 	} else {
4672e257c6SThomas Gleixner 		WARN_ON_ONCE(1);
4772e257c6SThomas Gleixner 		return false;
4872e257c6SThomas Gleixner 	}
4972e257c6SThomas Gleixner 
508c41ccecSThomas Gleixner 	required_flags = pops->required_flags;
518c41ccecSThomas Gleixner 
5272e257c6SThomas Gleixner 	/* Is the target domain bus token supported? */
5372e257c6SThomas Gleixner 	switch(info->bus_token) {
548c41ccecSThomas Gleixner 	case DOMAIN_BUS_PCI_DEVICE_MSI:
558c41ccecSThomas Gleixner 	case DOMAIN_BUS_PCI_DEVICE_MSIX:
568c41ccecSThomas Gleixner 		if (WARN_ON_ONCE(!IS_ENABLED(CONFIG_PCI_MSI)))
578c41ccecSThomas Gleixner 			return false;
588c41ccecSThomas Gleixner 
598c41ccecSThomas Gleixner 		break;
60496436f4SThomas Gleixner 	case DOMAIN_BUS_DEVICE_MSI:
61496436f4SThomas Gleixner 		/*
62496436f4SThomas Gleixner 		 * Per device MSI should never have any MSI feature bits
63496436f4SThomas Gleixner 		 * set. It's sole purpose is to create a dumb interrupt
64496436f4SThomas Gleixner 		 * chip which has a device specific irq_write_msi_msg()
65496436f4SThomas Gleixner 		 * callback.
66496436f4SThomas Gleixner 		 */
67496436f4SThomas Gleixner 		if (WARN_ON_ONCE(info->flags))
68496436f4SThomas Gleixner 			return false;
69496436f4SThomas Gleixner 
70496436f4SThomas Gleixner 		/* Core managed MSI descriptors */
71496436f4SThomas Gleixner 		info->flags = MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS | MSI_FLAG_FREE_MSI_DESCS;
7264a85532SThomas Gleixner 		fallthrough;
7364a85532SThomas Gleixner 	case DOMAIN_BUS_WIRED_TO_MSI:
74496436f4SThomas Gleixner 		/* Remove PCI specific flags */
75496436f4SThomas Gleixner 		required_flags &= ~MSI_FLAG_PCI_MSI_MASK_PARENT;
76496436f4SThomas Gleixner 		break;
7772e257c6SThomas Gleixner 	default:
7872e257c6SThomas Gleixner 		/*
7972e257c6SThomas Gleixner 		 * This should never be reached. See
8072e257c6SThomas Gleixner 		 * msi_lib_irq_domain_select()
8172e257c6SThomas Gleixner 		 */
8272e257c6SThomas Gleixner 		WARN_ON_ONCE(1);
8372e257c6SThomas Gleixner 		return false;
8472e257c6SThomas Gleixner 	}
8572e257c6SThomas Gleixner 
8672e257c6SThomas Gleixner 	/*
8772e257c6SThomas Gleixner 	 * Mask out the domain specific MSI feature flags which are not
8872e257c6SThomas Gleixner 	 * supported by the real parent.
8972e257c6SThomas Gleixner 	 */
9072e257c6SThomas Gleixner 	info->flags			&= pops->supported_flags;
9172e257c6SThomas Gleixner 	/* Enforce the required flags */
928c41ccecSThomas Gleixner 	info->flags			|= required_flags;
9372e257c6SThomas Gleixner 
9472e257c6SThomas Gleixner 	/* Chip updates for all child bus types */
9572e257c6SThomas Gleixner 	if (!info->chip->irq_eoi)
9672e257c6SThomas Gleixner 		info->chip->irq_eoi	= irq_chip_eoi_parent;
977b2f8aa0SThomas Gleixner 	if (!info->chip->irq_ack)
987b2f8aa0SThomas Gleixner 		info->chip->irq_ack	= irq_chip_ack_parent;
9972e257c6SThomas Gleixner 
10072e257c6SThomas Gleixner 	/*
10172e257c6SThomas Gleixner 	 * The device MSI domain can never have a set affinity callback. It
10272e257c6SThomas Gleixner 	 * always has to rely on the parent domain to handle affinity
10372e257c6SThomas Gleixner 	 * settings. The device MSI domain just has to write the resulting
10472e257c6SThomas Gleixner 	 * MSI message into the hardware which is the whole purpose of the
10572e257c6SThomas Gleixner 	 * device MSI domain aside of mask/unmask which is provided e.g. by
10672e257c6SThomas Gleixner 	 * PCI/MSI device domains.
10772e257c6SThomas Gleixner 	 */
10872e257c6SThomas Gleixner 	info->chip->irq_set_affinity	= msi_domain_set_affinity;
10972e257c6SThomas Gleixner 	return true;
11072e257c6SThomas Gleixner }
11172e257c6SThomas Gleixner EXPORT_SYMBOL_GPL(msi_lib_init_dev_msi_info);
11272e257c6SThomas Gleixner 
11372e257c6SThomas Gleixner /**
11472e257c6SThomas Gleixner  * msi_lib_irq_domain_select - Shared select function for NEXUS domains
11572e257c6SThomas Gleixner  * @d:		Pointer to the irq domain on which select is invoked
11672e257c6SThomas Gleixner  * @fwspec:	Firmware spec describing what is searched
11772e257c6SThomas Gleixner  * @bus_token:	The bus token for which a matching irq domain is looked up
11872e257c6SThomas Gleixner  *
11972e257c6SThomas Gleixner  * Returns:	%0 if @d is not what is being looked for
12072e257c6SThomas Gleixner  *
12172e257c6SThomas Gleixner  *		%1 if @d is either the domain which is directly searched for or
12272e257c6SThomas Gleixner  *		   if @d is providing the parent MSI domain for the functionality
12372e257c6SThomas Gleixner  *			 requested with @bus_token.
12472e257c6SThomas Gleixner  */
msi_lib_irq_domain_select(struct irq_domain * d,struct irq_fwspec * fwspec,enum irq_domain_bus_token bus_token)12572e257c6SThomas Gleixner int msi_lib_irq_domain_select(struct irq_domain *d, struct irq_fwspec *fwspec,
12672e257c6SThomas Gleixner 			      enum irq_domain_bus_token bus_token)
12772e257c6SThomas Gleixner {
12872e257c6SThomas Gleixner 	const struct msi_parent_ops *ops = d->msi_parent_ops;
12972e257c6SThomas Gleixner 	u32 busmask = BIT(bus_token);
13072e257c6SThomas Gleixner 
131*880799fcSMaxime Chevallier 	if (!ops)
132*880799fcSMaxime Chevallier 		return 0;
133*880799fcSMaxime Chevallier 
13472e257c6SThomas Gleixner 	if (fwspec->fwnode != d->fwnode || fwspec->param_count != 0)
13572e257c6SThomas Gleixner 		return 0;
13672e257c6SThomas Gleixner 
13772e257c6SThomas Gleixner 	/* Handle pure domain searches */
13872e257c6SThomas Gleixner 	if (bus_token == ops->bus_select_token)
13972e257c6SThomas Gleixner 		return 1;
14072e257c6SThomas Gleixner 
141*880799fcSMaxime Chevallier 	return !!(ops->bus_select_mask & busmask);
14272e257c6SThomas Gleixner }
14372e257c6SThomas Gleixner EXPORT_SYMBOL_GPL(msi_lib_irq_domain_select);
144